-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrol.lua
196 lines (161 loc) · 6.25 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
require 'src/utilities'
local settings = require 'src/settings'
require 'src/forcefield'
require 'src/emitter'
require 'src/gui'
require 'src/config-changes'
require 'remote'
-- on load game for first time or when settings change
function init()
if not global.forcefields then
global.forcefields = {}
end
global.forcefields.version = ConfigChanges.currentVersion
end
script.on_init(function(_)
settings:verifySettings()
init()
end)
script.on_configuration_changed(function (data)
if data.mod_changes and data.mod_changes.ForceFields2 and data.mod_changes.ForceFields2.old_version then
ConfigChanges:onConfigurationChanged()
settings:verifySettings()
end
end)
-- on loading the map
script.on_event(defines.events.on_tick, nil)
script.on_load(function(event)
if global.forcefields and global.forcefields.ticking then
script.on_event(defines.events.on_tick, function(_) Emitter:onTick() end)
end
end)
-- When the player quick paste settings
local onEntitySettingsPasted = function(event)
if event.source.name == settings.emitterName and event.destination.name == settings.emitterName then
Emitter:onEntitySettingsPasted(event)
end
end
script.on_event(defines.events.on_entity_settings_pasted, onEntitySettingsPasted)
-- When entities get created
local onEntityCreated = function(event)
local createdEntity = event.created_entity or event.entity
if (not createdEntity) or (not createdEntity.valid) then return end
if createdEntity.name == settings.emitterName then
Emitter:onEmitterBuilt(createdEntity)
end
end
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_event(defines.events.on_entity_cloned, function(event)
if event.destination.name == settings.emitterName then
-- Step 1: simulate the create event for the destination
onEntityCreated{
entity = event.destination,
}
-- Step 2: simulate copy pasting without a player
onEntitySettingsPasted{
player_index = nil,
source = event.source,
destination = event.destination,
}
elseif settings.forcefieldTypes[event.destination.name] ~= nil then
-- Field walls cannot be cloned, they are generated by the emitter
log("ERROR: Something tried cloning a forcefield wall!")
event.destination.destroy{raise_destroy = true}
end
end)
-- When entities get damaged (creates a trigger entity)
script.on_event(defines.events.on_entity_damaged, function(event)
-- Check if a forcefield is damaged
if settings.forcefieldTypes[event.entity.name] ~= nil then
Forcefield:onForcefieldDamaged(event.entity)
end
end)
-- When entities get destroyed
script.on_event({defines.events.on_entity_died ,
defines.events.script_raised_destroy}, function(event)
if not event.entity then return end
if settings.forcefieldTypes[event.entity.name] ~= nil then
Forcefield:onForcefieldDied(event.entity)
elseif event.entity.name == settings.emitterName then
Emitter:onEmitterDied(event.entity)
end
end)
script.on_event({defines.events.on_pre_surface_cleared,
defines.events.on_pre_surface_deleted}, function(event)
local surface = game.surfaces[event.surface_index]
-- Step 1: delete all emitters
for _,emitter in pairs(surface.find_entities_filtered{
name = settings.emitterName,
}) do
emitter.destroy{raise_destroy = true}
end
-- Step 2: delete all forcefields
local forcefieldNames = {}
for forcefieldType,_ in pairs(settings.forcefieldTypes) do
forcefieldNames[#forcefieldNames + 1] = forcefieldType
end
for _,forcefield in pairs(surface.find_entities_filtered{
name = forcefieldNames,
}) do
forcefield.destroy{raise_destroy = true}
end
end)
-- When entities get mined/deconstructed
script.on_event(defines.events.on_pre_player_mined_item, function(event)
if settings.forcefieldTypes[event.entity.name] ~= nil then
Forcefield:onForcefieldMined(event.entity, event.player_index)
elseif event.entity.name == settings.emitterName then
Emitter:onEmitterMined(event.entity, event.player_index)
end
end)
script.on_event(defines.events.on_robot_pre_mined, function(event)
if event.entity.name == settings.emitterName then
Emitter:onEmitterMined(event.entity)
end
end)
script.on_event(defines.events.on_marked_for_deconstruction, function(event)
if settings.forcefieldTypes[event.entity.name] ~= nil then
-- Forcefields itself can't be deconstructed
event.entity.cancel_deconstruction(game.players[event.player_index].force)
elseif event.entity.name == settings.emitterName then
local emitterTable = Emitter:findEmitter(event.entity)
if emitterTable ~= nil then
emitterTable["disabled"] = true
Forcefield:degradeLinkedFields(emitterTable)
end
end
end)
script.on_event(defines.events.on_cancelled_deconstruction, function(event)
if event.entity.name == settings.emitterName then
local emitterTable = Emitter:findEmitter(event.entity)
if emitterTable ~= nil then
emitterTable["disabled"] = false
Emitter:setActive(emitterTable, true)
end
end
end)
-- When the player clicks on the emitter to open the gui
script.on_event(defines.events.on_gui_opened, function(event)
if event.gui_type == defines.gui_type.entity and event.entity.name == settings.emitterName then
Gui:onOpenGui(event.entity, event.player_index)
end
end)
-- When the player wants to close the gui
script.on_event(defines.events.on_gui_closed, function(event)
Gui:onCloseGui(event.element, event.player_index)
end)
-- When the player clicks on a button
script.on_event({defines.events.on_gui_click ,
defines.events.on_gui_text_changed}, function(event)
if Gui.guiInteractionHandlers[event.element.name] then
Gui.guiInteractionHandlers[event.element.name](Gui, event)
-- Check for wall config button pressed
elseif string.find(event.element.name, Gui.guiElementNames.configOption) then
Gui.guiInteractionHandlers[Gui.guiElementNames.configOption](Gui, event)
elseif string.find(event.element.name, Gui.guiElementNames.configRowOption) then
Gui.guiInteractionHandlers[Gui.guiElementNames.configRowOption](Gui, event)
end
end)