forked from wolfhowlmedia/factorio-helicopters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
428 lines (336 loc) · 10.1 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
math3d = require("math3d")
require("logic.mtMgr")
require("logic.util")
require("logic.timer")
require("logic.simpleNoise")
require("logic.heliBase")
require("logic.heliAttack")
require("logic.heliPad")
require("logic.heliController")
require("logic.gui.remoteGui")
require("logic.gui.gaugeGui")
Entity = require("stdlib.entity.entity")
mod_gui = require("mod-gui")
function playerIsInHeli(p)
return p.driving and string.find(heliBaseEntityNames, p.vehicle.name .. ",", 1, true)
end
function OnLoad(e)
if global.helis then
for _, heli in pairs(global.helis) do
if not heli.type or heli.type == "heliAttack" then
setmetatable(heli, {__index = heliAttack})
end
end
end
setMetatablesInGlobal("remoteGuis", {__index = remoteGui})
setMetatablesInGlobal("heliPads", {__index = heliPad})
setMetatablesInGlobal("heliControllers", {__index = heliController})
--restore gui metatables
if global.remoteGuis then
for _,remotegui in pairs(global.remoteGuis) do
for _,gui in pairs(remotegui.guis) do
if gui.prefix then
local n = string.gsub(gui.prefix, "heli_(%a+)_.*", "%1")
if _G[n] then
setmetatable(gui, {__index = _G[n]})
end
end
end
end
end
callInGlobal("helis", "OnLoad")
mtMgr.OnLoad()
end
function OnConfigChanged(e)
if global.helis then
for k, curHeli in pairs(global.helis) do
if not curHeli.curState then
if curHeli.goUp then
curHeli:changeState(curHeli.engineStarting)
else
curHeli:changeState(curHeli.descend)
end
end
if not curHeli.surface then
curHeli.surface = curHeli.baseEnt.surface
end
if not curHeli.type then
curHeli.type = "heliAttack"
end
if curHeli.hasLandedCollider and not curHeli.childs.collisionEnt.valid then
curHeli:setCollider("landed")
end
if not curHeli.deactivatedInserters then
curHeli.deactivatedInserters = {}
end
curHeli:reassignCurState()
end
end
if global.heliPads then
for k, curPad in pairs(global.heliPads) do
if not curPad.surface then
curPad.surface = curPad.baseEnt.surface
end
end
end
for k, p in pairs(game.players) do
local flow = mod_gui.get_button_flow(p)
if flow.heli_remote_btn and flow.heli_remote_btn.valid then
flow.heli_remote_btn.destroy()
end
OnArmorInventoryChanged({player_index = p.index})
reSetGaugeGui(p)
end
if global.heliControllers then
for k, curController in pairs(global.heliControllers) do
if not curController.heli.remoteController then
curController.heli.remoteController = curController
end
end
end
--fixing left open guis when saved
if global.remoteGuis then
global.remoteGuis = {}
for _,p in pairs(game.players) do
local flow = mod_gui.get_frame_flow(p)
if flow["heli_heliSelectionGui_rootFrame"] then
flow["heli_heliSelectionGui_rootFrame"].destroy()
end
end
end
end
function OnTick(e)
checkAndTickInGlobal("helis")
checkAndTickInGlobal("remoteGuis")
checkAndTickInGlobal("heliControllers")
OnTimerTick()
end
function OnBuilt(e)
local ent = e.created_entity
if ent.name == "heli-placement-entity-_-" then
local newHeli = insertInGlobal("helis", heliAttack.new(ent))
if global.remoteGuis then
for _,rg in pairs(global.remoteGuis) do
rg:OnHeliBuilt(newHeli)
end
end
elseif ent.name == "heli-pad-placement-entity" then
local newPad = insertInGlobal("heliPads", heliPad.new(ent))
callInGlobal("remoteGuis", "OnHeliPadBuilt", newPad)
elseif ent.type == "inserter" then
ent.active = true
end
end
function OnRemoved(e)
local ent = e.entity
if ent.valid then
local entName = ent.name
if string.find(heliEntityNames, entName .. ",", 1, true) then
for i,val in ipairs(global.helis) do
if val:isBaseOrChild(ent) then
val:destroy()
table.remove(global.helis, i)
if global.remoteGuis then
for _,rg in pairs(global.remoteGuis) do
rg:OnHeliRemoved(val)
end
end
end
end
end
if entName == "heli-pad-entity" then
local i = getHeliPadIndexFromBaseEntity(ent)
if i then
global.heliPads[i]:destroy()
callInGlobal("remoteGuis", "OnHeliPadRemoved", global.heliPads[i])
table.remove(global.heliPads, i)
end
end
end
end
function OnHeliUp(e)
local p = game.players[e.player_index]
if playerIsInHeli(p) then
getHeliFromBaseEntity(p.vehicle):OnUp()
end
end
function OnHeliDown(e)
local p = game.players[e.player_index]
if playerIsInHeli(p) then
getHeliFromBaseEntity(p.vehicle):OnDown()
end
end
function OnHeliIncreaseMaxHeight(e)
local p = game.players[e.player_index]
if playerIsInHeli(p) then
getHeliFromBaseEntity(p.vehicle):OnIncreaseMaxHeight()
end
end
function OnHeliDecreaseMaxHeight(e)
local p = game.players[e.player_index]
if playerIsInHeli(p) then
getHeliFromBaseEntity(p.vehicle):OnDecreaseMaxHeight()
end
end
function OnHeliToggleFloodlight(e)
local p = game.players[e.player_index]
if playerIsInHeli(p) then
getHeliFromBaseEntity(p.vehicle):OnToggleFloodlight()
end
end
function OnHeliFollow(e)
local p = game.players[e.player_index]
if playerHasEquipment(p, "heli-remote-equipment") then
local heli, dist = findNearestAvailableHeli(p.position, p.force, p)
if heli then
assignHeliController(p, heli, p, true)
p.add_custom_alert(heli.baseEnt, {type = "item", name = "heli-item"}, {"heli-alert-follow", chopDecimal(dist)}, true)
end
end
end
function OnRemoteOpen(e)
local p = game.players[e.player_index]
if playerHasEquipment(p, "heli-remote-equipment") then
toggleRemoteGui(p)
end
end
function OnPlacedEquipment(e)
if e.equipment.name == "heli-remote-equipment" then
local p = game.players[e.player_index]
setRemoteBtn(p, true)
end
end
function OnRemovedEquipment(e)
if e.equipment == "heli-remote-equipment" then
local p = game.players[e.player_index]
if not equipmentGridHasItem(e.grid, "heli-remote-equipment") then
setRemoteBtn(p, false)
end
end
end
function OnArmorInventoryChanged(e)
local p = game.players[e.player_index]
if playerHasEquipment(p, "heli-remote-equipment") then
setRemoteBtn(p, true)
else
setRemoteBtn(p, false)
end
end
function OnGuiClick(e)
local name = e.element.name
if name:match("^heli_") then
local p = game.players[e.player_index]
if name == "heli_remote_btn" then
toggleRemoteGui(p)
elseif gaugeGui.hasMyPrefix(name) then
local i = searchIndexInTable(global.gaugeGuis, p, "player")
if i then
global.gaugeGuis[i]:OnGuiClick(e)
end
elseif remoteGui.hasMyPrefix(name) then
local i = searchIndexInTable(global.remoteGuis, p, "player")
if i then
global.remoteGuis[i]:OnGuiClick(e)
end
end
end
end
function OnGuiTextChanged(e)
local name = e.element.name
if name:match("^heli_") then
local p = game.players[e.player_index]
local i = searchIndexInTable(global.remoteGuis, p, "player")
if i then
global.remoteGuis[i]:OnGuiTextChanged(e)
end
end
end
function OnPlayerChangedForce(e)
local p = game.players[e.player_index]
callInGlobal("remoteGuis", "OnPlayerChangedForce", p)
end
function OnPlayerDied(e)
local p = game.players[e.player_index]
setRemoteBtn(p, false)
callInGlobal("remoteGuis", "OnPlayerDied", p)
end
function OnPlayerLeft(e)
local p = game.players[e.player_index]
local i = searchIndexInTable(global.remoteGuis, p, "player")
if i then
global.remoteGuis[i]:destroy()
table.remove(global.remoteGuis, i)
end
callInGlobal("remoteGuis", "OnPlayerLeft", p)
end
function OnPlayerRespawned(e)
callInGlobal("remoteGuis", "OnPlayerRespawned", game.players[e.player_index])
end
function OnDrivingStateChanged(e)
local p = game.players[e.player_index]
local ent = e.entity
if ent then
local entName = ent.name
if string.find(heliEntityNames, entName .. ",", 1, true) then
local heli
for i, curHeli in ipairs(global.helis) do
if curHeli:isBaseOrChild(ent) then
heli = curHeli
break
end
end
reSetGaugeGui(p)
if not p.driving then
heli:OnPlayerEjected(p)
end
end
end
end
function OnPlayerJoined(e)
OnArmorInventoryChanged(e)
end
function OnPlayerCreated(e)
OnArmorInventoryChanged(e)
end
function OnRuntimeSettingsChanged(e)
local name = e.setting
if name:match("^heli-") then
local p = game.players[e.player_index]
if e.setting_type == "runtime-per-user" then
local val = p.mod_settings[name].value
if name == "heli-gaugeGui-show" then
reSetGaugeGui(p)
end
--elseif e.setting_type == "runtime-global" then
-- local val = settings.global[name]
end
end
end
script.on_event(defines.events.on_built_entity, OnBuilt)
script.on_event(defines.events.on_robot_built_entity, OnBuilt)
script.on_load(OnLoad)
script.on_configuration_changed(OnConfigChanged)
script.on_event(defines.events.on_tick, OnTick)
script.on_event(defines.events.on_player_mined_entity, OnRemoved)
script.on_event(defines.events.on_robot_mined_entity, OnRemoved)
script.on_event(defines.events.on_entity_died, OnRemoved)
script.on_event("heli-up", OnHeliUp)
script.on_event("heli-down", OnHeliDown)
script.on_event("heli-zaa-height-increase", OnHeliIncreaseMaxHeight)
script.on_event("heli-zab-height-decrease", OnHeliDecreaseMaxHeight)
script.on_event("heli-zba-toogle-floodlight", OnHeliToggleFloodlight)
script.on_event("heli-zca-remote-heli-follow", OnHeliFollow)
script.on_event("heli-zcb-remote-open", OnRemoteOpen)
script.on_event(defines.events.on_player_placed_equipment, OnPlacedEquipment)
script.on_event(defines.events.on_player_removed_equipment, OnRemovedEquipment)
script.on_event(defines.events.on_gui_click, OnGuiClick)
script.on_event(defines.events.on_gui_text_changed, OnGuiTextChanged)
script.on_event(defines.events.on_player_changed_force, OnPlayerChangedForce)
script.on_event(defines.events.on_player_died, OnPlayerDied)
script.on_event(defines.events.on_player_left_game, OnPlayerLeft)
script.on_event(defines.events.on_player_respawned, OnPlayerRespawned)
script.on_event(defines.events.on_player_joined_game, OnPlayerJoined)
script.on_event(defines.events.on_player_created, OnPlayerCreated)
script.on_event(defines.events.on_player_armor_inventory_changed, OnArmorInventoryChanged)
script.on_event(defines.events.on_player_driving_changed_state, OnDrivingStateChanged)
script.on_event(defines.events.on_runtime_mod_setting_changed, OnRuntimeSettingsChanged)