-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontrol.lua
359 lines (312 loc) · 10.6 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
require "functions"
require "constants"
require "config"
require "wellgen"
require "pollutiondetection"
require "pollutionfx"
require "fans"
require "spills"
require "wallnukerai"
require "tracker-hooks"
require "caches"
function initGlobal(markDirty)
if not global.nvday then
global.nvday = {}
end
local nvday = global.nvday
if nvday.chunk_cache == nil then
nvday.chunk_cache = {}
end
if nvday.pollution_detectors == nil then
nvday.pollution_detectors = {}
end
if nvday.pollution_fans == nil then
nvday.pollution_fans = {}
end
if nvday.boreholes == nil then
nvday.boreholes = {}
end
if nvday.borers == nil then
nvday.borers = {}
end
if nvday.spills == nil then
nvday.spills = {}
end
if nvday.deaeros == nil then
nvday.deaeros = {}
end
if nvday.deaeros.indices == nil then
nvday.deaeros.indices = {}
end
if nvday.deaeros.cache == nil then
nvday.deaeros.cache = {}
end
if nvday.worm_eggs == nil then
nvday.worm_eggs = {}
end
nvday.dirty = markDirty
end
local function addCommands()
commands.add_command("hatchAllWormEggs", {"cmd.identify-help"}, function(event)
for _,egg in ipairs(global.nvday.worm_eggs) do
hatchWormEgg(egg, game.forces.enemy.evolution_factor)
end
global.nvday.worm_eggs = {}
end)
commands.add_command("spawnNuker", {"cmd.identify-help"}, function(event)
local player = game.players[event.player_index]
local sel = player.selected
if not (sel and sel.valid) then player.print("No entity is selected.") return end
local target = player.character
local targets = sel.surface.find_entities_filtered{type = "wall", force = player.force, area = getRadiusAABB(player, 200)}
if targets and #targets > 0 then
target = targets[math.random(1, #targets)]
end
spawnNuker(sel.surface, sel, target)
end)
end
addCommands()
script.on_configuration_changed(function(data)
initGlobal(true)
--setupTrackers()
setPollutionAndEvoSettings(global.nvday)
local names = {}
for i = 1,4 do
table.insert(names, "air-filter-machine-" .. i)
end
local nvday = global.nvday
local n = 0
for _,entity in pairs(game.surfaces.nauvis.find_entities_filtered{name = names}) do
addDeaerosolizer(nvday, entity)
n = n+1
--entity.force.print("NauvisDay: Recaching Deaero #" .. entity.unit_number .. " @ " .. serpent.block(entity.position))
end
local c = 0
local tiles = game.surfaces.nauvis.find_tiles_filtered{name = "polluted-water"}
c = #tiles
for _,tile in pairs(tiles) do
if tile.valid then
local hid = game.surfaces.nauvis.get_hidden_tile(tile.position)
if hid then
else
game.surfaces.nauvis.set_hidden_tile(tile.position, game.tile_prototypes["water"])
end
end
end
--[[
for chunk in game.surfaces.nauvis.get_chunks() do
for x = chunk.x*32,chunk.x*32+31 do
for y = chunk.y*32,chunk.y*32+31 do
local tile = game.surfaces.nauvis.get_tile(x, y)
if tile.valid and stringStartsWith(tile.name, "polluted-") then
local pos = {x, y}
local hid = game.surfaces.nauvis.get_hidden_tile(pos)
if (hid and hid.valid) then
else
game.surfaces.nauvis.set_hidden_tile(pos, game.tile_prototypes["water"])
c = c+1
end
end
end
end
end
--]]
game.print("NauvisDay: Recached " .. n .. " deaerosolizers and repaired " .. c .. " sludge tiles in cache.")
log("NauvisDay: Recached " .. n .. " deaerosolizers and repaired " .. c .. " sludge tiles in cache.")
end)
script.on_init(function()
initGlobal(true)
--setupTrackers()
setPollutionAndEvoSettings(global.nvday)
end)
script.on_load(function()
--setupTrackers()
end)
script.on_event(defines.events.on_console_command, function(event)
setPollutionAndEvoSettings(global.nvday)
end)
local function onEntityRotated(event)
local nvday = global.nvday
rotatePollutionFan(nvday, event.entity)
end
local function onTileMined(event)
local amt = 0
local miner = event.player_index and game.players[event.player_index].character or event.robot
if miner then
for _,pair in ipairs(event.tiles) do
if stringStartsWith(pair.old_tile.name, "polluted-") then
amt = amt+1
end
end
if amt > 0 then
game.surfaces[event.surface_index].pollute(miner.position, Config.pollutedWaterTileCleanup*Config.pollutedWaterTileRelease*2)
miner.damage(miner.prototype.max_health*0.05, miner.force, "poison")
end
end
end
local function onEntityAdded(event)
local nvday = global.nvday
local entity = event.created_entity
if entity.name == "storage-machine" and entity.force.technologies["pollution-storage-2"].researched then
entity = upgradeStorageMachine(entity)
end
trackEntityAddition(entity, nvday)
if string.find(entity.name, "air-filter-machine-", 1, true) then
if entity.type == "assembling-machine" then --check type since AirFilter mod is otherwise going to be caught here
entity.set_recipe("air-cleaning-action")
--game.print("Initializing Deaero.")
--entity.operable = false
else
if event.player_index then
game.players[event.player_index].print("There is no point in using this with NauvisDay installed. Use its deaerosolizers instead.")
elseif entity.force then
entity.force.print("There is no point in using this with NauvisDay installed. Use its deaerosolizers instead.")
end
end
end
if entity.name == "venting-machine" then
entity.set_recipe("pollution-venting-action")
--entity.operable = false
end
end
local function onEntityRemoved(event)
local nvday = global.nvday
trackEntityRemoval(event.entity, nvday)
fluidSpill(event.entity)
checkPollutionBlock(event.entity)
doSpawnerDestructionSpawns(event.entity)
doTreeFarmTreeDeath(event.entity)
end
local function onEntityDied(event)
if event.entity.name == "wall-nuker" then
onWallNukerDeath(event)
end
onEntityRemoved(event)
end
local function onEntityDamaged(event)
local target = event.entity
local source = event.cause
local type = event.damage_type
local amount = event.final_damage_amount
if source and source.name == "wall-nuker" and target.type ~= "player" then
source.damage(game.entity_prototypes[source.name].max_health/10, source.force, type.name)
end
end
local function onGameTick(event)
local nvday = global.nvday
if nvday.dirty then
nvday.chunk_cache = {}
for chunk in game.surfaces["nauvis"].get_chunks() do
table.insert(nvday.chunk_cache, chunk)
end
--[[
for name,func in pairs(tracker["add"]) do
local entities = game.surfaces["nauvis"].find_entities_filtered({name=name})
for _,entity in pairs(entities) do
func(entity)
end
end
--]]
nvday.dirty = false
if game.active_mods["air-filtering"] then
game.print("NauvisDay: Detected AirFilters mod; NauvisDay contains all the features it does and far more, making AirFilters somewhat useless to have installed together with it.")
end
end
runTickHooks(nvday, event.tick)
local tick = event.tick
doAmbientPollutionEffects(nvday, tick)
if tick%3600 == 0 then --check once every 60 seconds
setPollutionAndEvoSettings(nvday)
end
ensureNoEarlyAttacks(tick)
if tick%15 == 0 then
for _,player in pairs(game.players) do
if player.selected == nil then
setSpillGui(player, nil)
end
end
tickSpilledFluids(nvday, tick%60 == 0)
end
if tick%60 == 0 then
local evo = game.forces.enemy.evolution_factor
local cap = getInterpolatedValue(maxAttackSizeCurveLookup, evo)
cap = math.max(10, math.ceil(cap*Config.attackSize))
if not cap then game.print("ERROR: NULL INTERPOLATE FROM " .. serpent.block(evo) .. " into " .. serpent.block(maxAttackSizeCurveLookup.values)) end
game.map_settings.unit_group.max_unit_group_size = math.ceil(cap) --[[@as uint]] --200 is vanilla
for i,egg in ipairs(nvday.worm_eggs) do
if egg.entity.valid then
local age = tick-egg.laid
if age > baseWormHatchTime/getInterpolatedValue(wormHatchSpeedCurveLookup, evo) then
hatchWormEgg(egg, evo)
table.remove(nvday.worm_eggs, i)
break
end
end
end
end
if game.forces.enemy.evolution_factor < 0 then
game.forces.enemy.evolution_factor = 0
end
end
script.on_event(defines.events.on_selected_entity_changed, handleFluidSpillTooltip)
script.on_event(defines.events.on_entity_died, onEntityDied)
script.on_event(defines.events.on_pre_player_mined_item, onEntityRemoved)
script.on_event(defines.events.on_robot_pre_mined, onEntityRemoved)
script.on_event(defines.events.script_raised_destroy, onEntityRemoved)
script.on_event(defines.events.on_built_entity, onEntityAdded)
script.on_event(defines.events.on_robot_built_entity, onEntityAdded)
script.on_event(defines.events.on_player_mined_tile, onTileMined)
script.on_event(defines.events.on_robot_mined_tile, onTileMined)
script.on_event(defines.events.on_player_rotated_entity, onEntityRotated)
script.on_event(defines.events.on_entity_damaged, onEntityDamaged)
script.on_event(defines.events.on_player_flushed_fluid, function(event)
--if event.fluid == "waste" then
spillFluidsAt(event.fluid, event.amount, event.entity)
--end
end)
script.on_event(defines.events.on_resource_depleted, function(event)
if Config.depleteWells and event.entity.prototype.resource_category == "basic-fluid" then
if not (event.entity.prototype == "crude-oil" and game.active_mods["Fracking"]) then
event.entity.surface.create_entity{name="pollution-well", position=event.entity.position, amount=1}
event.entity.destroy()
end
return
end
if event.entity.prototype.resource_category == "borehole" then
event.entity.surface.create_entity{name="filled-borehole", position=event.entity.position, amount=1, force = event.entity.force}
event.entity.destroy()
return
end
end)
script.on_event(defines.events.on_chunk_generated, function(event)
if Config.depleteWells then
return
end
table.insert(global.nvday.chunk_cache, event.area)
local rand = game.create_random_generator()
local x = (event.area.left_top.x+event.area.right_bottom.x)/2
local y = (event.area.left_top.y+event.area.right_bottom.y)/2
local seed = createSeed(event.surface, x+4, y+4) --[[@as uint]]
rand.re_seed(seed)
local f0 = 1/160 --was 512, then 256, then 192
local f1 = rand(0, 2147483647)/2147483647
--game.print("Chunk at " .. x .. ", " .. y .. " with chance " .. f0 .. " / " .. f1)
if f1 < f0 then
--game.print("Genning Chunk at " .. x .. ", " .. y)
x = x-16+rand(0, 32)
y = y-16+rand(0, 32)
generateEmptyWells(event.surface, event.area, x, y, rand)
end
end)
local function onFinishedResearch(event)
local tech = event.research
local force = event.research.force
local nvday = global.nvday
if tech.name == "pollution-storage-2" then
for _,e in pairs(game.surfaces.nauvis.find_entities_filtered{name = "storage-machine", force = force}) do
local repl = upgradeStorageMachine(e)
end
end
end
script.on_event(defines.events.on_tick, onGameTick)
script.on_event(defines.events.on_research_finished, onFinishedResearch)