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.8 KiB142 linesraw
1local flib_bounding_box = require("__flib__/bounding-box")
2
3local entity_data = require("__PipeVisualizer__/scripts/entity-data")
4local iterator = require("__PipeVisualizer__/scripts/iterator")
5local util = require("__PipeVisualizer__/scripts/util")
6
7--- @alias BuiltEvent EventData.on_built_entity|EventData.on_robot_built_entity|EventData.on_entity_cloned|EventData.script_raised_built|EventData.script_raised_revive
8--- @alias DestroyedEvent EventData.on_player_mined_entity|EventData.on_robot_mined_entity|EventData.on_entity_died|EventData.script_raised_destroy
9
10--- @param iterator Iterator
11--- @param entity LuaEntity
12--- @return boolean
13local function is_relevant(iterator, entity)
14  if iterator.in_overlay then
15    local overlay = global.overlay[iterator.player_index]
16    if not overlay or not overlay.last_position then
17      return false
18    end
19    local box =
20      flib_bounding_box.from_dimensions(overlay.last_position, overlay.dimensions.width, overlay.dimensions.height)
21    return flib_bounding_box.contains_position(box, entity.position)
22  end
23
24  local fluidbox = entity.fluidbox
25  for _, fluid_system_id in util.iterate_fluid_systems(fluidbox) do
26    if iterator.systems[fluid_system_id] then
27      return true
28    end
29  end
30  return false
31end
32
33--- @param it Iterator
34--- @param data EntityData
35local function update_neighbours(it, data)
36  for _, connections in pairs(data.connections) do
37    for _, connection in pairs(connections) do
38      if connection.target_owner then
39        iterator.request2(it, connection.target_owner)
40      end
41    end
42  end
43end
44
45--- @param e BuiltEvent
46local function on_entity_built(e)
47  if not global.iterator then
48    return
49  end
50
51  local entity = e.entity or e.created_entity or e.destination
52  if not entity.valid then
53    return
54  end
55
56  local unit_number = entity.unit_number
57  if not unit_number then
58    return
59  end
60
61  for _, it in pairs(global.iterator) do
62    if is_relevant(it, entity) then
63      iterator.request2(it, entity, true)
64    end
65  end
66end
67
68--- @param e DestroyedEvent
69local function on_entity_destroyed(e)
70  if not global.iterator then
71    return
72  end
73
74  local entity = e.entity
75  if not entity.valid then
76    return
77  end
78
79  local unit_number = entity.unit_number
80  if not unit_number then
81    return
82  end
83
84  for _, it in pairs(global.iterator) do
85    local data = it.entities[unit_number]
86    if not data then
87      goto continue
88    end
89
90    update_neighbours(it, data)
91    entity_data.remove(it, data)
92
93    ::continue::
94  end
95end
96
97--- @param e EventData.on_player_rotated_entity|EventData.on_entity_settings_pasted
98local function on_entity_updated(e)
99  if not global.iterator then
100    return
101  end
102
103  local entity = e.entity or e.destination
104  if not entity.valid then
105    return
106  end
107
108  local unit_number = entity.unit_number
109  if not unit_number then
110    return
111  end
112
113  for _, it in pairs(global.iterator) do
114    local data = it.entities[unit_number]
115    if not data then
116      if is_relevant(it, entity) then
117        iterator.request2(it, entity)
118      end
119      return
120    end
121    update_neighbours(it, data)
122    iterator.request2(it, entity, true)
123  end
124end
125
126local entity_detection = {}
127
128entity_detection.events = {
129  [defines.events.on_built_entity] = on_entity_built,
130  [defines.events.on_entity_cloned] = on_entity_built,
131  [defines.events.on_entity_died] = on_entity_destroyed,
132  [defines.events.on_entity_settings_pasted] = on_entity_updated,
133  [defines.events.on_player_mined_entity] = on_entity_destroyed,
134  [defines.events.on_player_rotated_entity] = on_entity_updated,
135  [defines.events.on_robot_built_entity] = on_entity_built,
136  [defines.events.on_robot_mined_entity] = on_entity_destroyed,
137  [defines.events.script_raised_built] = on_entity_built,
138  [defines.events.script_raised_destroy] = on_entity_destroyed,
139  [defines.events.script_raised_revive] = on_entity_built,
140}
141
142return entity_detection