-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol.lua
307 lines (289 loc) · 10.7 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
-- Copyright 2021 Sil3ntStorm https://github.com/Sil3ntStorm
--
-- Licensed under MS-RL, see https://opensource.org/licenses/MS-RL
require("utils.utils")
local function createLight(lamp)
return rendering.draw_light{
sprite = 'utility/light_small',
color = color,
target = lamp,
surface = lamp.surface,
scale = settings.global['rgb-default-lamps-lightSize'].value
}
end
local function updateLight(entry, r, g, b)
if r <= 1 and g <= 1 and b <= 1 then
if entry.light then
rendering.destroy(entry.light)
global.rgb_default_lamps[entry.lamp.unit_number].light = nil
end
if entry.glow then
rendering.destroy(entry.glow)
global.rgb_default_lamps[entry.lamp.unit_number].glow = nil
end
return
end
local color = {math.max(2, math.min(255, r)), math.max(2, math.min(255, g)), math.max(2, math.min(255, b))}
if entry.light then
rendering.set_color(entry.light, color)
else
entry.light = createLight(entry.lamp)
end
if entry.glow then
rendering.set_color(entry.glow, color)
else
entry.glow = rendering.draw_sprite{
sprite = 'rgb-lamp-light',
tint = color,
render_layer = 'light-effect',
target = entry.lamp,
surface = entry.lamp.surface,
x_scale = 1.1,
y_scale = 1.1
}
end
end
local function processLamp(lamp)
if not lamp or not lamp.valid then
return
end
if settings.global['rgb-default-lamps-alwaysEnabled'].value == false then
local ctrl = lamp.get_control_behavior()
if ctrl and ctrl.disabled then
updateLight(global.rgb_default_lamps[lamp.unit_number], -1, -1, -1)
return
end
end
if not lamp.is_connected_to_electric_network() then
updateLight(global.rgb_default_lamps[lamp.unit_number], -1, -1, -1)
return
end
if not lamp.get_circuit_network(defines.wire_type.green, defines.circuit_connector_id.lamp) and not lamp.get_circuit_network(defines.wire_type.red, defines.circuit_connector_id.lamp) then
-- Not connected to any circuit wire
updateLight(global.rgb_default_lamps[lamp.unit_number], -1, -1, -1)
return
end
local r = lamp.get_merged_signal({type='virtual', name='signal-R'}, defines.circuit_connector_id.lamp)
local g = lamp.get_merged_signal({type='virtual', name='signal-G'}, defines.circuit_connector_id.lamp)
local b = lamp.get_merged_signal({type='virtual', name='signal-B'}, defines.circuit_connector_id.lamp)
updateLight(global.rgb_default_lamps[lamp.unit_number], r, g, b)
end
local function initEntity(entity)
if not (entity and entity.valid) then
log('initEntity called with an invalid entity')
return
end
table.insert(global.rgb_default_tracked, entity.unit_number)
local epole = nil
if settings.startup['rgb-default-lamps-lampsHavePoles'].value then
epole = entity.surface.create_entity{
name = 'rgb-default-lamp-tiny-pole',
position = entity.position,
force = entity.force
}
end
global.rgb_default_lamps[entity.unit_number] = {lamp = entity, light = nil, glow = nil, pole = epole}
end
local function cleanupEntity(destroyed_unit_number)
if destroyed_unit_number == nil or global.rgb_default_lamps[destroyed_unit_number] == nil then
log('cleanupEntity called with invalid / untracked entity')
return
end
if global.rgb_default_lamps[destroyed_unit_number].light then
rendering.destroy(global.rgb_default_lamps[destroyed_unit_number].light)
end
if global.rgb_default_lamps[destroyed_unit_number].glow then
rendering.destroy(global.rgb_default_lamps[destroyed_unit_number].glow)
end
if global.rgb_default_lamps[destroyed_unit_number].pole then
global.rgb_default_lamps[destroyed_unit_number].pole.destroy()
end
global.rgb_default_lamps[destroyed_unit_number] = nil
ArrayRemove(global.rgb_default_tracked, function(t,i,j)
return t[i] ~= destroyed_unit_number
end)
end
local function onEntityCreated(event)
if (event.created_entity and event.created_entity.valid and event.created_entity.name == 'small-lamp') then
initEntity(event.created_entity)
elseif (event.entity and event.entity.valid and event.entity.name == 'small-lamp') then
-- script_raised_built
initEntity(event.entity)
end
end
local function onEntityDeleted(event)
if not (event.entity and event.entity.valid) then
log('onEntityDeleted called with an invalid entity')
return
end
local destroyed_unit_name = event.entity.name
local destroyed_unit_number = event.entity.unit_number
if destroyed_unit_name == 'small-lamp' then
cleanupEntity(destroyed_unit_number)
end
end
local function connectNeighbor(entity)
if (entity.name == 'entity-ghost' and entity.ghost_name ~= 'small-lamp') then
return
end
if (entity.name ~= 'entity-ghost' and entity.name ~= 'small-lamp') then
return
end
local result = entity.surface.find_entities_filtered({name='small-lamp', position=entity.position, radius=1.0})
for _, found in pairs(result) do
entity.connect_neighbour({
wire = defines.wire_type.green,
target_entity = found,
})
end
result = entity.surface.find_entities_filtered({ghost_name='small-lamp', position=entity.position, radius=1.0})
for _, found in pairs(result) do
entity.connect_neighbour({
wire = defines.wire_type.green,
target_entity = found,
})
end
end
local function doUpgradePlanner(entity, plr)
if (entity.force ~= plr.force) then
return
end
local sur = entity.surface
local pos = entity.position
local frc = entity.force
local create_name = nil
if (entity.name == 'small-lamp') then
create_name = 'pipe'
elseif (entity.name == 'pipe') then
create_name = 'small-lamp'
elseif (entity.name == 'entity-ghost') then
if (entity.ghost_name == 'pipe') then
create_name = 'small-lamp'
elseif (entity.ghost_name == 'small-lamp') then
create_name = 'pipe'
end
end
if not create_name then
return
end
if (entity.to_be_deconstructed()) then
entity.cancel_deconstruction(frc, plr)
return
end
entity.order_deconstruction(frc, plr)
nEnt = sur.create_entity{
name = 'entity-ghost',
position = pos,
force = frc,
player = plr,
inner_name = create_name,
expires = false,
}
if (create_name == 'small-lamp') then
connectNeighbor(nEnt)
end
end
local function onSelection(data)
if (data.item ~= 'rgb-default-lamp-upgplan' or not settings.startup['rgb-default-lamps-upgradePipes'].value) then
return
end
plr = game.get_player(data.player_index)
if (not plr.force.technologies['optics'].researched) then
return
end
for _, entity in pairs(data.entities) do
doUpgradePlanner(entity, plr)
end
end
local function onSettingsChanged(data)
local mc = data.mod_changes
if not mc or not mc['rgb-default-lamps'] then
return
end
if not mc['rgb-default-lamps'].old_version then
return
end
if data.mod_startup_settings_changed then
local copied = {}
for k, v in pairs(global.rgb_default_lamps) do
copied[k] = v
end
for _, lamp in pairs(copied) do
if (lamp.lamp and lamp.lamp.valid) then
cleanupEntity(lamp.lamp.unit_number)
initEntity(lamp.lamp)
else
cleanupEntity(_)
log('Invalid entity onSettingsChanged:' .. _)
end
end
end
end
local function onRTSettingChanged(event)
-- if strsub(event.setting, 0, 18) ~= 'rgb-default-lamps-' then
-- return
-- end
if event.setting ~= 'rgb-default-lamps-lightSize' and event.setting ~= 'rgb-default-lamps-alwaysEnabled' then
return
end
local copied = {}
for k, v in pairs(global.rgb_default_lamps) do
copied[k] = v
end
for _, lamp in pairs(copied) do
if event.setting == 'rgb-default-lamps-lightSize' then
if lamp.light then
rendering.destroy(lamp.light)
lamp.light = createLight(lamp.lamp)
end
elseif event.setting == 'rgb-default-lamps-alwaysEnabled' then
processLamp(lamp.lamp)
end
end
end
script.on_init(function()
if not global.rgb_default_lamps then
global.rgb_default_lamps = {}
end
if not global.rgb_default_tracked then
global.rgb_default_tracked = {}
end
for _, surface in pairs(game.surfaces) do
lamps = surface.find_entities_filtered({name='small-lamp'})
for _, lamp in pairs(lamps) do
initEntity(lamp)
end
end
log('Tracking ' .. #global.rgb_default_lamps .. ' existing lamps')
end)
script.on_event({defines.events.on_pre_player_mined_item, defines.events.on_robot_pre_mined, defines.events.on_entity_died, defines.events.script_raised_destroy}, onEntityDeleted)
script.on_event({defines.events.on_built_entity, defines.events.on_robot_built_entity, defines.events.script_raised_built, defines.events.script_raised_revive}, onEntityCreated);
script.on_configuration_changed(onSettingsChanged)
script.on_event({defines.events.on_runtime_mod_setting_changed}, onRTSettingChanged)
script.on_event({defines.events.on_player_selected_area, defines.events.on_player_alt_selected_area}, onSelection)
script.on_event(defines.events.on_tick, function(event)
if event.tick % settings.global['rgb-default-lamps-nthTick'].value > 0 then
return
end
-- this should've already happened / been initialized by migration / on_init...
-- But the game doesn't seem to run those (or not before on_tick), so we'll do this again here...
if not global.rgb_default_lamps then
global.rgb_default_lamps = {}
end
if not global.rgb_default_tracked then
global.rgb_default_tracked = {}
end
local lastIt = global.rgb_default_lamps_last or 1
local target = math.min(#global.rgb_default_tracked, lastIt + settings.global['rgb-default-lamps-perTick'].value - 1)
for i = lastIt, target, 1 do
if global.rgb_default_lamps[global.rgb_default_tracked[i]] then
processLamp(global.rgb_default_lamps[global.rgb_default_tracked[i]].lamp)
end
end
target = target + 1
if target > #global.rgb_default_tracked then
target = 1
end
global.rgb_default_lamps_last = target
end
)