1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-24 03:47:58 +02:00
ComfyFactorio/modules/wave_defense/threat_events.lua

129 lines
5.4 KiB
Lua
Raw Normal View History

2019-10-08 17:41:15 +02:00
local threat_values = require "modules.wave_defense.threat_values"
local math_random = math.random
2019-10-10 04:33:51 +02:00
local function remove_unit(entity)
2019-10-14 06:52:17 +02:00
if not global.wave_defense.active_units[entity.unit_number] then return end
global.wave_defense.active_units[entity.unit_number] = nil
2019-10-10 04:33:51 +02:00
global.wave_defense.active_biter_count = global.wave_defense.active_biter_count - 1
end
function build_nest()
if global.wave_defense.threat < 1000 then return end
2019-10-14 06:52:17 +02:00
if math_random(1, global.wave_defense.nest_building_chance) ~= 1 then return end
local unit = wave_defense_get_random_unit()
if not unit then return end
2019-10-10 07:24:49 +02:00
local position = unit.surface.find_non_colliding_position("biter-spawner", unit.position, 8, 1)
2019-10-10 04:33:51 +02:00
if not position then return end
2019-10-10 07:24:49 +02:00
local r = global.wave_defense.nest_building_density
if unit.surface.count_entities_filtered({type = "unit-spawner", area = {{position.x - r, position.y - r},{position.x + r, position.y + r}}}) > 0 then return end
2019-10-10 04:33:51 +02:00
unit.surface.create_entity({name = "biter-spawner", position = position, force = unit.force})
unit.surface.create_entity({name = "blood-explosion-huge", position = position})
unit.surface.create_entity({name = "blood-explosion-huge", position = unit.position})
remove_unit(unit)
unit.destroy()
global.wave_defense.threat = global.wave_defense.threat - 500
end
function build_worm()
if global.wave_defense.threat < 1000 then return end
if math_random(1, global.wave_defense.worm_building_chance) ~= 1 then return end
2019-10-14 06:52:17 +02:00
local unit = wave_defense_get_random_unit()
if not unit then return end
2019-10-10 04:33:51 +02:00
wave_defense_set_worm_raffle(global.wave_defense.wave_number)
local worm = wave_defense_roll_worm_name()
2019-10-10 07:24:49 +02:00
local position = unit.surface.find_non_colliding_position(worm, unit.position, 8, 1)
2019-10-10 04:33:51 +02:00
if not position then return end
2019-10-10 07:24:49 +02:00
local r = global.wave_defense.worm_building_density
if unit.surface.count_entities_filtered({type = "turret", area = {{position.x - r, position.y - r},{position.x + r, position.y + r}}}) > 0 then return end
2019-10-10 04:33:51 +02:00
unit.surface.create_entity({name = worm, position = position, force = unit.force})
unit.surface.create_entity({name = "blood-explosion-huge", position = position})
unit.surface.create_entity({name = "blood-explosion-huge", position = unit.position})
remove_unit(unit)
unit.destroy()
global.wave_defense.threat = global.wave_defense.threat - threat_values[worm]
end
2019-10-08 17:41:15 +02:00
local function get_circle_vectors(radius)
local vectors = {}
for x = radius * -1, radius, 1 do
for y = radius * -1, radius, 1 do
if math.sqrt(x^2 + y^2) <= radius then
vectors[#vectors + 1] = {x, y}
end
end
end
return vectors
end
local acid_nova_entities = {
["small-biter"] = {projectile = "acid-stream-worm-small", vectors = get_circle_vectors(3), amount = 8, threat_cost = 32},
["medium-biter"] = {projectile = "acid-stream-worm-medium", vectors = get_circle_vectors(4), amount = 8, threat_cost = 64},
["big-biter"] = {projectile = "acid-stream-worm-big", vectors = get_circle_vectors(5), amount = 8, threat_cost = 96},
["behemoth-biter"] = {projectile = "acid-stream-worm-behemoth", vectors = get_circle_vectors(6), amount = 8, threat_cost = 128},
}
local function acid_nova(entity)
if not acid_nova_entities[entity.name] then return end
2019-10-12 02:19:37 +02:00
if global.wave_defense.threat < 25000 then return end
2019-10-08 23:50:57 +02:00
if math_random(1, 16) ~= 1 then return end
2019-10-08 17:41:15 +02:00
for _ = 1, acid_nova_entities[entity.name].amount, 1 do
2019-10-08 23:50:57 +02:00
local i = math_random(1, #acid_nova_entities[entity.name].vectors)
2019-10-08 17:41:15 +02:00
entity.surface.create_entity({
name = acid_nova_entities[entity.name].projectile,
position = entity.position,
force = entity.force.name,
source = entity.position,
target = {x = entity.position.x + acid_nova_entities[entity.name].vectors[i][1], y = entity.position.y + acid_nova_entities[entity.name].vectors[i][2]},
max_range = radius,
speed = 0.001
})
end
global.wave_defense.threat = global.wave_defense.threat - acid_nova_entities[entity.name].threat_cost
return true
end
2019-10-08 23:50:57 +02:00
local function shred_simple_entities(entity)
2019-10-10 07:24:49 +02:00
if global.wave_defense.threat < 5000 then return end
2019-10-12 02:19:37 +02:00
local simple_entities = entity.surface.find_entities_filtered({type = "simple-entity", area = {{entity.position.x - 2, entity.position.y - 2},{entity.position.x + 2, entity.position.y + 2}}})
2019-10-08 23:50:57 +02:00
if #simple_entities == 0 then return end
2019-10-09 03:25:00 +02:00
if #simple_entities > 1 then table.shuffle_table(simple_entities) end
local r = math.floor(global.wave_defense.threat * global.wave_defense.simple_entity_shredding_count_modifier)
if r < 1 then r = 1 end
local count = math.random(1, r)
2019-10-08 23:50:57 +02:00
local damage_dealt = 0
for i = 1, count, 1 do
if not simple_entities[i] then break end
if simple_entities[i].valid then
if simple_entities[i].health then
damage_dealt = damage_dealt + simple_entities[i].health
simple_entities[i].die("neutral", simple_entities[i])
end
end
end
if damage_dealt == 0 then return end
2019-10-09 03:25:00 +02:00
local threat_cost = math.floor(damage_dealt * global.wave_defense.simple_entity_shredding_cost_modifier)
2019-10-08 23:50:57 +02:00
if threat_cost < 1 then threat_cost = 1 end
global.wave_defense.threat = global.wave_defense.threat - threat_cost
end
local function on_entity_died(event)
if not event.entity.valid then return end
if event.entity.type == "unit" then
remove_unit(event.entity)
acid_nova(event.entity)
end
if event.entity.force.index == 3 then
if event.cause then
if event.cause.valid then
if event.cause.force.index == 2 then
shred_simple_entities(event.entity)
end
end
end
end
2019-10-08 17:41:15 +02:00
end
local event = require 'utils.event'
event.add(defines.events.on_entity_died, on_entity_died)