z_mirror/PipeVisualizer
git clone https://git.raiguard.me/z_mirror/PipeVisualizer
9443616
main
1local renderer =require ( "scripts.renderer" ) 2 3--- @class EntityData 4--- @field connections table<FluidSystemID, PipeConnectionExt[]> 5--- @field connection_objects table<FluidSystemID, RenderObjectID[]> 6--- @field shape RenderObjectID? 7--- @field entity LuaEntity 8--- @field fluidbox LuaFluidBox 9--- @field unit_number UnitNumber 10 11--- @class PipeConnectionExt: PipeConnection 12--- @field direction defines.direction 13--- @field shape_position MapPosition 14--- @field target_owner LuaEntity? 15 16--- Assumes that the positions are in exact cardinals. 17--- @param connection PipeConnection 18--- @return defines.direction 19local function get_cardinal_direction ( connection ) 20local from , to =connection . position , connection . target_position 21if from . y > to . y then 22return defines . direction . north 23elseif from . x < to . x then 24return defines . direction . east 25elseif from . y < to . y then 26return defines . direction . south 27else 28return defines . direction . west 29end 30end 31 32--- @class EntityDataModule 33local entity_data ={} 34 35--- @param iterator Iterator 36--- @param entity LuaEntity 37--- @return EntityData? 38function entity_data . create ( iterator , entity ) 39local unit_number =entity . unit_number 40if not unit_number then 41return 42end 43 44local data =iterator . entities [ unit_number ] 45if data then 46-- ---@diagnostic disable-next-line: missing-fields 47-- entity.surface.create_entity({ 48-- name = "flying-text", 49-- text = "Redraw", 50-- color = { r = 1, g = 0.3, b = 0.3 }, 51-- position = entity.position, 52-- }) 53entity_data . remove ( iterator , data ) 54end 55 56--- @type EntityData 57local data ={ 58connection_objects ={} , 59connections ={} , 60entity =entity , 61fluidbox =entity . fluidbox , 62unit_number =unit_number , 63} 64 65for i =1 , # data . fluidbox do 66--- @cast i uint 67local id =data . fluidbox . get_fluid_system_id ( i ) 68if not id then 69goto continue 70end 71--- @type PipeConnectionExt 72local connections =data . fluidbox . get_pipe_connections ( i ) 73for _ , connection in pairs ( connections ) do 74connection . direction =get_cardinal_direction ( connection ) 75connection . shape_position ={ 76x =connection . position . x + ( connection . target_position . x - connection . position . x ) / 2 , 77y =connection . position . y + ( connection . target_position . y - connection . position . y ) / 2 , 78} 79if connection . target then 80connection . target_owner =connection . target . owner 81end 82end 83data . connections [ id ] =connections 84:: continue :: 85end 86 87iterator . entities [ unit_number ] =data 88return data 89end 90 91--- @param iterator Iterator 92--- @param data EntityData 93function entity_data . remove ( iterator , data ) 94renderer . clear ( data ) 95iterator . entities [ data . unit_number ] =nil 96end 97 98--- @param iterator Iterator 99--- @param data EntityData 100--- @param fluid_system_id FluidSystemID 101function entity_data . remove_system ( iterator , data , fluid_system_id ) 102if renderer . clear_system ( iterator , data , fluid_system_id ) then 103iterator . entities [ data . unit_number ] =nil 104end 105end 106 107--- @param iterator Iterator 108--- @param entity LuaEntity 109--- @return EntityData? 110function entity_data . get ( iterator , entity ) 111local unit_number =entity . unit_number 112if not unit_number then 113return 114end 115return iterator . entities [ unit_number ] 116end 117 118--- @param iterator Iterator 119--- @param entity LuaEntity 120--- @return EntityData? 121function entity_data . get_or_create ( iterator , entity ) 122local data =entity_data . get ( iterator , entity ) 123if data then 124return data 125end 126return entity_data . create ( iterator , entity ) 127end 128 129return entity_data