z_mirror/PipeVisualizer

git clone https://git.raiguard.me/z_mirror/PipeVisualizer

Caleb HeuerStore pipe connections in global, split and clean up code9371b54

2.0
3.3 KiB122 linesraw
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)
20  local from, to = connection.position, connection.target_position
21  if from.y > to.y then
22    return defines.direction.north
23  elseif from.x < to.x then
24    return defines.direction.east
25  elseif from.y < to.y then
26    return defines.direction.south
27  else
28    return defines.direction.west
29  end
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)
39  local unit_number = entity.unit_number
40  if not unit_number then
41    return
42  end
43
44  local data = iterator.entities[unit_number]
45  if data then
46    entity_data.remove(iterator, data)
47  end
48
49  --- @type EntityData
50  local data = {
51    connections = {},
52    connection_objects = {},
53    entity = entity,
54    fluidbox = entity.fluidbox,
55    unit_number = unit_number,
56  }
57
58  for i = 1, #data.fluidbox do
59    --- @cast i uint
60    local id = data.fluidbox.get_fluid_system_id(i)
61    if not id then
62      goto continue
63    end
64    --- @type PipeConnectionExt
65    local connections = data.fluidbox.get_pipe_connections(i)
66    for _, connection in pairs(connections) do
67      connection.direction = get_cardinal_direction(connection)
68      connection.shape_position = {
69        x = connection.position.x + (connection.target_position.x - connection.position.x) / 2,
70        y = connection.position.y + (connection.target_position.y - connection.position.y) / 2,
71      }
72      if connection.target then
73        connection.target_owner = connection.target.owner
74      end
75    end
76    data.connections[id] = connections
77    ::continue::
78  end
79
80  iterator.entities[unit_number] = data
81  return data
82end
83
84--- @param iterator Iterator
85--- @param data EntityData
86function entity_data.remove(iterator, data)
87  renderer.clear(data)
88  iterator.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)
95  if renderer.clear_system(iterator, data, fluid_system_id) then
96    iterator.entities[data.unit_number] = nil
97  end
98end
99
100--- @param iterator Iterator
101--- @param entity LuaEntity
102--- @return EntityData?
103function entity_data.get(iterator, entity)
104  local unit_number = entity.unit_number
105  if not unit_number then
106    return
107  end
108  return 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)
115  local data = entity_data.get(iterator, entity)
116  if data then
117    return data
118  end
119  return entity_data.create(iterator, entity)
120end
121
122return entity_data