-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathentity_died_events.lua
More file actions
362 lines (320 loc) · 11.2 KB
/
entity_died_events.lua
File metadata and controls
362 lines (320 loc) · 11.2 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
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
local Event = require 'utils.event'
local Task = require 'utils.task'
local Token = require 'utils.token'
local Global = require 'utils.global'
local math = require 'utils.math'
local table = require 'utils.table'
local max = math.max
local random = math.random
local round = math.round
local set_timeout_in_ticks = Task.set_timeout_in_ticks
local ceil = math.ceil
local draw_arc = rendering.draw_arc
local fast_remove = table.fast_remove
local tau = 2 * math.pi
local start_angle = -tau / 4
local update_rate = 4 -- ticks between updates
local time_to_live = update_rate + 1
local pole_respawn_time = 60 * 60
local no_coin_entity = {}
Global.register(
{no_coin_entity = no_coin_entity},
function(tbl)
no_coin_entity = tbl.no_coin_entity
end
)
local entity_drop_amount = {
--[[['small-biter'] = {low = -62, high = 1},
['small-spitter'] = {low = -62, high = 1},
['medium-biter'] = {low = -14, high = 1},
['medium-spitter'] = {low = -14, high = 1},
['big-biter'] = {low = -2, high = 1},
['big-spitter'] = {low = -2, high = 1},
['behemoth-biter'] = {low = 1, high = 1},
['behemoth-spitter'] = {low = 1, high = 1}, ]]
['biter-spawner'] = {low = 8, high = 24},
['spitter-spawner'] = {low = 8, high = 24},
['small-worm-turret'] = {low = 3, high = 10},
['medium-worm-turret'] = {low = 8, high = 24},
['big-worm-turret'] = {low = 15, high = 30},
['behemoth-worm-turret'] = {low = 25, high = 45}
}
local spill_items =
Token.register(
function(data)
local stack = {name = 'coin', count = data.count}
data.surface.spill_item_stack(data.position, stack, true)
end
)
local entity_spawn_map = {
['medium-biter'] = {name = 'small-biter', count = 2, chance = 1},
['big-biter'] = {name = 'medium-biter', count = 2, chance = 1},
['behemoth-biter'] = {name = 'big-biter', count = 2, chance = 1},
['medium-spitter'] = {name = 'small-worm-turret', count = 1, chance = 0.2},
['big-spitter'] = {name = 'medium-worm-turret', count = 1, chance = 0.2},
['behemoth-spitter'] = {name = 'big-worm-turret', count = 1, chance = 0.2},
['biter-spawner'] = {type = 'biter', count = 5, chance = 1},
['spitter-spawner'] = {type = 'spitter', count = 5, chance = 1},
['behemoth-worm-turret'] = {name = 'behemoth-spitter', count = 2, chance = 1},
['stone-furnace'] = {type = 'cause', count = 2, chance = 1},
['steel-furnace'] = {type = 'cause', count = 2, chance = 1},
['electric-furnace'] = {type = 'cause', count = 4, chance = 1},
['assembling-machine-1'] = {type = 'cause', count = 4, chance = 1},
['assembling-machine-2'] = {type = 'cause', count = 4, chance = 1},
['assembling-machine-3'] = {type = 'cause', count = 4, chance = 1},
['chemical-plant'] = {type = 'cause', count = 4, chance = 1},
['centrifuge'] = {type = 'cause', count = 6, chance = 1},
['pumpjack'] = {type = 'cause', count = 6, chance = 1},
['storage-tank'] = {type = 'cause', count = 4, chance = 1},
['oil-refinery'] = {type = 'cause', count = 8, chance = 1},
['offshore-pump'] = {type = 'cause', count = 2, chance = 1},
['boiler'] = {type = 'cause', count = 2, chance = 1},
['heat-exchanger'] = {type = 'cause', count = 4, chance = 1},
['steam-engine'] = {type = 'cause', count = 6, chance = 1},
['steam-turbine'] = {type = 'cause', count = 10, chance = 1},
['nuclear-reactor'] = {type = 'cause', count = 20, chance = 1},
['rocket-silo'] = {type = 'cause', count = 40, chance = 1},
['train-stop'] = {type = 'cause', count = 2, chance = 1},
['burner-mining-drill'] = {type = 'cause', count = 2, chance = 1},
['electric-mining-drill'] = {type = 'cause', count = 4, chance = 1},
['lab'] = {type = 'cause', count = 6, chance = 1},
['solar-panel'] = {type = 'cause', count = 4, chance = 1},
['accumulator'] = {type = 'cause', count = 2, chance = 1},
['beacon'] = {type = 'cause', count = 6, chance = 1},
['radar'] = {type = 'cause', count = 4, chance = 1}
}
local unit_levels = {
biter = {
'small-biter',
'medium-biter',
'big-biter',
'behemoth-biter'
},
spitter = {
'small-spitter',
'medium-spitter',
'big-spitter',
'behemoth-spitter'
}
}
local worms = {
['small-worm-turret'] = true,
['medium-worm-turret'] = true,
['big-worm-turret'] = true,
['behemoth-worm-turret'] = true
}
local allowed_cause_source = {
['small-biter'] = true,
['medium-biter'] = true,
['big-biter'] = true,
['behemoth-biter'] = true,
['small-spitter'] = true,
['medium-spitter'] = true,
['big-spitter'] = true,
['behemoth-spitter'] = true
}
local turret_evolution_factor = {
['gun-turret'] = 0.001,
['laser-turret'] = 0.002,
['flamethrower-turret'] = 0.0015,
['artillery-turret'] = 0.004
}
local spawn_worm =
Token.register(
function(data)
local surface = data.surface
local name = data.name
local position = data.position
local p = surface.find_non_colliding_position(name, position, 8, 1)
if p then
local entity = surface.create_entity({name = data.name, position = data.position})
no_coin_entity[entity.unit_number] = true
end
end
)
local function get_level()
local ef = game.forces.enemy.evolution_factor
if ef == 0 then
return 1
else
return ceil(ef * 4)
end
end
local spawn_units =
Token.register(
function(data)
local surface = data.surface
local name = data.name
local position = data.position
for _ = 1, data.count do
local p = surface.find_non_colliding_position(name, position, 8, 1)
if p then
surface.create_entity {name = name, position = p}
end
end
end
)
local spawn_player =
Token.register(
function(player)
if player and player.valid then
local respawn_delay
if global.config.player_respawn_time.enabled then
local decrement_amount = round(game.forces.enemy.evolution_factor * global.config.player_respawn_time.decrement_amount)
local adjusted_respawn = global.config.player_respawn_time.max_time - decrement_amount
respawn_delay = max(global.config.player_respawn_time.min_time, adjusted_respawn) -- limit smallest delay to min_time, if evolution goes above 100%
else
respawn_delay = global.config.player_respawn_time.max_time
end
player.ticks_to_respawn = respawn_delay
end
end
)
local function has_valid_turret(turrets)
for i = #turrets, 1, -1 do
local turret = turrets[i]
if turret.valid then
return true
else
fast_remove(turrets, i)
end
end
return false
end
local pole_callback
pole_callback =
Token.register(
function(data)
if not has_valid_turret(data.turrets) then
return
end
local tick = data.tick
local now = game.tick
if now >= tick then
data.surface.create_entity({name = data.name, force = 'enemy', position = data.position})
return
end
local fraction = ((now - tick) / pole_respawn_time) + 1
draw_arc(
{
color = {1 - fraction, fraction, 0},
max_radius = 0.5,
min_radius = 0.4,
start_angle = start_angle,
angle = fraction * tau,
target = data.position,
surface = data.surface,
time_to_live = time_to_live
}
)
set_timeout_in_ticks(update_rate, pole_callback, data)
end
)
local filter = {area = nil, name = 'laser-turret', force = 'enemy'}
local function do_pole(entity)
if entity.type ~= 'electric-pole' then
return
end
local supply_area_distance = entity.prototype.supply_area_distance
if not supply_area_distance then
return
end
local surface = entity.surface
local position = entity.position
local x, y = position.x, position.y
local d = supply_area_distance / 2
filter.area = {{x - d, y - d}, {x + d, y + d}}
local turrets = surface.find_entities_filtered(filter)
if #turrets == 0 then
return
end
set_timeout_in_ticks(
update_rate,
pole_callback,
{
name = entity.name,
position = position,
surface = surface,
tick = game.tick + pole_respawn_time,
turrets = turrets
}
)
end
Event.add(
defines.events.on_entity_died,
function(event)
local entity = event.entity
if not entity or not entity.valid then
return
end
local entity_force = entity.force
local entity_name = entity.name
if entity_force.name == 'enemy' then
do_pole(entity)
local factor = turret_evolution_factor[entity_name]
if factor then
local old = entity_force.evolution_factor
local new = old + (1 - old) * factor
entity_force.evolution_factor = math.min(new, 1)
end
end
local bounds = entity_drop_amount[entity_name]
if bounds then
local unit_number = entity.unit_number
if no_coin_entity[unit_number] then
no_coin_entity[unit_number] = nil
else
local count = math.random(bounds.low, bounds.high)
if count > 0 then
set_timeout_in_ticks(
1,
spill_items,
{count = count, surface = entity.surface, position = entity.position}
)
end
end
end
local spawn = entity_spawn_map[entity_name]
if spawn then
local chance = spawn.chance
if chance == 1 or random() <= chance then
local name = spawn.name
if name == nil then
local type = spawn.type
if type == 'cause' then
local cause = event.cause
if not cause then
return
end
name = cause.name
if not allowed_cause_source[cause.name] then
return
end
else
name = unit_levels[type][get_level()]
end
end
if worms[name] then
set_timeout_in_ticks(
5,
spawn_worm,
{surface = entity.surface, name = name, position = entity.position}
)
else
set_timeout_in_ticks(
5,
spawn_units,
{surface = entity.surface, name = name, position = entity.position, count = spawn.count}
)
end
end
end
end
)
Event.add(
defines.events.on_player_died,
function(event)
local player = game.get_player(event.player_index)
set_timeout_in_ticks(1, spawn_player, player)
end
)