z_mirror/PipeVisualizer
git clone https://git.raiguard.me/z_mirror/PipeVisualizer
9371b54
2.0
1local renderer =require ( "__PipeVisualizer__/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 46entity_data . remove ( iterator , data ) 47end 48 49--- @type EntityData 50local data ={ 51connections ={} , 52connection_objects ={} , 53entity =entity , 54fluidbox =entity . fluidbox , 55unit_number =unit_number , 56} 57 58for i =1 , # data . fluidbox do 59--- @cast i uint 60local id =data . fluidbox . get_fluid_system_id ( i ) 61if not id then 62goto continue 63end 64--- @type PipeConnectionExt 65local connections =data . fluidbox . get_pipe_connections ( i ) 66for _ , connection in pairs ( connections ) do 67connection . direction =get_cardinal_direction ( connection ) 68connection . shape_position ={ 69x =connection . position . x + ( connection . target_position . x - connection . position . x ) / 2 , 70y =connection . position . y + ( connection . target_position . y - connection . position . y ) / 2 , 71} 72if connection . target then 73connection . target_owner =connection . target . owner 74end 75end 76data . connections [ id ] =connections 77:: continue :: 78end 79 80iterator . entities [ unit_number ] =data 81return data 82end 83 84--- @param iterator Iterator 85--- @param data EntityData 86function entity_data . remove ( iterator , data ) 87renderer . clear ( data ) 88iterator . entities [ data . unit_number ] =nil 89end 90 91--- @param iterator Iterator 92--- @param data EntityData 93--- @param fluid_system_id FluidSystemID 94function entity_data . remove_system ( iterator , data , fluid_system_id ) 95if renderer . clear_system ( iterator , data , fluid_system_id ) then 96iterator . entities [ data . unit_number ] =nil 97end 98end 99 100--- @param iterator Iterator 101--- @param entity LuaEntity 102--- @return EntityData? 103function entity_data . get ( iterator , entity ) 104local unit_number =entity . unit_number 105if not unit_number then 106return 107end 108return iterator . entities [ unit_number ] 109end 110 111--- @param iterator Iterator 112--- @param entity LuaEntity 113--- @return EntityData? 114function entity_data . get_or_create ( iterator , entity ) 115local data =entity_data . get ( iterator , entity ) 116if data then 117return data 118end 119return entity_data . create ( iterator , entity ) 120end 121 122return entity_data