forked from robot256/factorio_shortwave_fix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
318 lines (269 loc) · 10.1 KB
/
control.lua
File metadata and controls
318 lines (269 loc) · 10.1 KB
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
308
309
310
311
312
313
314
315
316
317
318
local math2d = require("math2d")
local function check_state(force)
if not storage[force.index] then
storage[force.index] = {}
end
end
local function get_channel_string(radio, toggle)
local cb = radio.get_control_behavior()
if cb.sections_count == 0 then
cb.add_section() -- Make sure there is always one section
else
while cb.sections_count > 1 do
cb.remove_section(2)
end
end
-- If "toggle" is true, it means that the toggle-entity keybind is *about to* change the enable state of this combinator.
-- It doesn't actually happen until after the linked custom input has executed, so we have to assume that it will toggle in the future.
if (not cb.enabled and not toggle) or (cb.enabled and toggle) then
return
end
local section = cb.get_section(1)
if not section then
return
end
local channel_slot = section.get_slot(1)
if not channel_slot or not channel_slot.value then
return
end
local channel_string = channel_slot.value.name..":"..channel_slot.min
return channel_string
end
local function check_channels()
for team,channels in pairs(storage) do
for channel,link in pairs(channels) do
if not link or not link.valid then
log("Shortwave origin link became invalid for channel "..tostring(team).."."..channel..", channel removed.")
channels[channel] = nil
else
local link_red = link.get_wire_connector(defines.wire_connector_id.circuit_red)
local link_green = link.get_wire_connector(defines.wire_connector_id.circuit_green)
if link_red.connection_count == 0 and link_green.connection_count == 0 then
link.destroy()
channels[channel] = nil
end
end
end
end
end
local function radio_link(radio)
local links = radio.surface.find_entities_filtered{
name = "shortwave-link",
area = math2d.bounding_box.create_from_centre(radio.position, 0.5)
}
local link = links and links[1]
if not link then
link = radio.surface.create_entity{
name = "shortwave-link",
position = radio.position,
force = radio.force,
}
end
link.operable = false
return link
end
local function radio_port(radio)
local ports = radio.surface.find_entities_filtered{
name = "shortwave-port",
area = math2d.bounding_box.create_from_centre(radio.position, 0.5)
}
local ghosts = radio.surface.find_entities_filtered{
ghost_name = "shortwave-port",
area = math2d.bounding_box.create_from_centre(radio.position, 0.5)
}
local port = ports and ports[1]
if not port and ghosts and ghosts[1] then
local ddd
ddd, port = ghosts[1].revive()
table.remove(ghosts, 1)
end
if not port then
port = radio.surface.create_entity({
name = "shortwave-port",
position = radio.position,
force = radio.force,
})
end
for _, ghost in ipairs(ghosts) do
ghost.destroy()
end
port.operable = false
return port
end
local function radio_tune(radio, toggle)
local team = radio.force.index
local link = radio_link(radio)
local port = radio_port(radio)
local link_red = link.get_wire_connector(defines.wire_connector_id.circuit_red)
local link_green = link.get_wire_connector(defines.wire_connector_id.circuit_green)
for _, connection in pairs(link_red.connections) do
if connection.target.owner.name == "shortwave-link" then
link_red.disconnect_from(connection.target, defines.wire_origin.script)
end
end
for _, connection in pairs(link_green.connections) do
if connection.target.owner.name == "shortwave-link" then
link_green.disconnect_from(connection.target, defines.wire_origin.script)
end
end
local channel = get_channel_string(radio, toggle)
if not channel then
--game.print("Radio disabled")
return
end
if not storage[team][channel] then
storage[team][channel] = radio.surface.create_entity{
name = "shortwave-link",
position = { 0, 0 },
force = radio.force,
}
end
local relay = storage[team][channel]
local relay_red = relay.get_wire_connector(defines.wire_connector_id.circuit_red)
local relay_green = relay.get_wire_connector(defines.wire_connector_id.circuit_green)
link_red.connect_to(relay_red, false, defines.wire_origin.script)
link_green.connect_to(relay_green, false, defines.wire_origin.script)
local port_red = port.get_wire_connector(defines.wire_connector_id.combinator_input_red)
local port_green = port.get_wire_connector(defines.wire_connector_id.combinator_input_green)
link_red.connect_to(port_red, false, defines.wire_origin.script)
link_green.connect_to(port_green, false, defines.wire_origin.script)
--game.print("Radio tuned")
end
local function OnEntityCreated(event)
local entity = event.entity or event.destination
-- check for blueprints missing io port or radio body
if entity.name == "entity-ghost" then
local r = entity.surface.count_entities_filtered({
ghost_name = 'shortwave-radio',
area = {
left_top = { x = entity.position.x - 0.1, y = entity.position.y - 0.1 },
right_bottom = { x = entity.position.x + 0.1, y = entity.position.y + 0.1 },
}
}) > 0
local p = entity.surface.count_entities_filtered({
ghost_name = 'shortwave-port',
area = {
left_top = { x = entity.position.x - 0.1, y = entity.position.y - 0.1 },
right_bottom = { x = entity.position.x + 0.1, y = entity.position.y + 0.1 },
}
}) > 0
if (p and not r) and not event.item then
game.print("Broken shortwave blueprint! Cannot blueprint I/O port alone.")
entity.destroy()
return
end
elseif entity.name == "shortwave-radio" then
check_state(entity.force)
radio_tune(entity)
check_channels()
end
end
local function OnEntityRemoved(event)
local entity = event.entity
check_state(entity.force)
radio_link(entity).destroy()
radio_port(entity).destroy()
check_channels()
end
local function OnEntitySettingChanged(event)
local entity = event.entity or event.destination
local toggle = false
if event.input_name == "shortwave-toggle" then
entity = event.player_index and game.players[event.player_index].selected
toggle = true
end
if not entity or not entity.valid then
return
end
if entity.name == "shortwave-radio" then
check_state(entity.force)
radio_tune(entity, toggle)
check_channels()
end
end
remote.add_interface('shortwave', {
get_channel_merged_signals = function(force, channel)
local team = force.valid and force.index
if storage[team] and storage[team][channel] then
return storage[team][channel].get_signals(defines.wire_connector_id.circuit_red, defines.wire_connector_id.circuit_green)
end
end,
get_channel = function(radio)
if radio.valid and radio.name == 'shortwave-radio' then
return get_channel_string(radio)
end
end,
get_relay = function(force, channel)
local team = force.valid and force.index
return storage[team] and storage[team][channel]
end,
get_relays = function()
return storage
end,
get_force_relays = function(force)
local team = force.valid and force.index
return storage[team]
end,
get_relay_channel = function(relay)
local team = relay.valid and relay.force.index
for channel, entity in pairs(storage[team] or {}) do
if relay == entity then
return channel
end
end
end,
}
)
-- When radios are created
local built_filters = {
{filter = "name", name = "shortwave-radio"},
{filter = "name", name = "shortwave-port"},
{filter = "ghost", ghost_name = "shortwave-radio"},
{filter = "ghost", ghost_name = "shortwave-port"},
}
script.on_event(defines.events.on_built_entity, OnEntityCreated, built_filters)
script.on_event(defines.events.on_robot_built_entity, OnEntityCreated, built_filters)
script.on_event(defines.events.script_raised_built, OnEntityCreated, built_filters)
script.on_event(defines.events.on_entity_cloned, OnEntityCreated, built_filters)
script.on_event(defines.events.script_raised_revive, OnEntityCreated, built_filters)
script.on_event(defines.events.on_space_platform_built_entity, OnEntityCreated, built_filters)
-- When radios are destroyed
local mined_filters = {{filter = "name", name = "shortwave-radio"}}
script.on_event(defines.events.on_player_mined_entity, OnEntityRemoved, mined_filters)
script.on_event(defines.events.on_robot_pre_mined, OnEntityRemoved, mined_filters)
script.on_event(defines.events.on_entity_died, OnEntityRemoved, mined_filters)
script.on_event(defines.events.script_raised_destroy, OnEntityRemoved, mined_filters)
script.on_event(defines.events.on_space_platform_mined_entity, OnEntityRemoved, mined_filters)
-- When player changes settings
script.on_event(defines.events.on_gui_closed, OnEntitySettingChanged)
script.on_event(defines.events.on_entity_settings_pasted, OnEntitySettingChanged)
script.on_event("shortwave-toggle", OnEntitySettingChanged)
-- When player pipettes radio
script.on_event(defines.events.on_player_pipette, function(event)
local player = game.players[event.player_index]
if event.item.name == "shortwave-port" then
player.cursor_stack.clear()
player.pipette_entity({name="shortwave-radio", quality=event.quality})
end
end)
-- No work on startup
--script.on_init(function()
--end)
--script.on_load(function()
--end)
-- Console commands
commands.add_command("shortwave-dump", "Dump storage to log", function() log(serpent.block(storage)) end)
------------------------------------------------------------------------------------
-- FIND LOCAL VARIABLES THAT ARE USED GLOBALLY --
-- (Thanks to eradicator!) --
------------------------------------------------------------------------------------
setmetatable(_ENV,{
__newindex=function (self,key,value) --locked_global_write
error('\n\n[ER Global Lock] Forbidden global *write*:\n'
.. serpent.line{key=key or '<nil>',value=value or '<nil>'}..'\n')
end,
__index =function (self,key) --locked_global_read
error('\n\n[ER Global Lock] Forbidden global *read*:\n'
.. serpent.line{key=key or '<nil>'}..'\n')
end ,
})