2021-11-14 22:01:01 +02:00
|
|
|
local Public = require 'modules.wave_defense.table'
|
2019-10-29 12:26:59 +02:00
|
|
|
local side_target_types = {
|
2020-10-22 13:32:18 +02:00
|
|
|
['accumulator'] = true,
|
|
|
|
['assembling-machine'] = true,
|
|
|
|
['boiler'] = true,
|
|
|
|
['furnace'] = true,
|
|
|
|
['generator'] = true,
|
|
|
|
['lab'] = true,
|
|
|
|
['lamp'] = true,
|
|
|
|
['mining-drill'] = true,
|
|
|
|
['power-switch'] = true,
|
|
|
|
['radar'] = true,
|
|
|
|
['reactor'] = true,
|
|
|
|
['roboport'] = true,
|
|
|
|
['rocket-silo'] = true,
|
|
|
|
['solar-panel'] = true
|
2019-10-29 12:26:59 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:45:47 +02:00
|
|
|
local function get_random_target()
|
2021-11-14 22:01:01 +02:00
|
|
|
local side_target_count = Public.get('side_target_count')
|
|
|
|
local side_targets = Public.get('side_targets')
|
2020-11-17 13:45:47 +02:00
|
|
|
local r = math.random(1, side_target_count)
|
|
|
|
if not side_targets[r] then
|
|
|
|
table.remove(side_targets, r)
|
2021-11-14 22:01:01 +02:00
|
|
|
Public.set('side_target_count', side_target_count - 1)
|
2020-10-22 13:32:18 +02:00
|
|
|
return
|
|
|
|
end
|
2020-11-17 13:45:47 +02:00
|
|
|
if not side_targets[r].valid then
|
|
|
|
table.remove(side_targets, r)
|
2021-11-14 22:01:01 +02:00
|
|
|
Public.set('side_target_count', side_target_count - 1)
|
2020-10-22 13:32:18 +02:00
|
|
|
return
|
|
|
|
end
|
2021-11-14 22:01:01 +02:00
|
|
|
side_targets = Public.get('side_targets')
|
2020-11-17 13:45:47 +02:00
|
|
|
return side_targets[r]
|
2019-10-29 12:26:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Public.get_side_target()
|
2021-11-14 22:01:01 +02:00
|
|
|
local enable_side_target = Public.get('enable_side_target')
|
2020-11-17 13:45:47 +02:00
|
|
|
if not enable_side_target then
|
|
|
|
return
|
|
|
|
end
|
2021-11-14 22:01:01 +02:00
|
|
|
local side_target_count = Public.get('side_target_count')
|
2020-10-22 13:32:18 +02:00
|
|
|
for _ = 1, 512, 1 do
|
2020-11-17 13:45:47 +02:00
|
|
|
if side_target_count == 0 then
|
2020-10-22 13:32:18 +02:00
|
|
|
return
|
|
|
|
end
|
2020-11-17 13:45:47 +02:00
|
|
|
local target = get_random_target()
|
2020-10-22 13:32:18 +02:00
|
|
|
if target then
|
|
|
|
return target
|
|
|
|
end
|
|
|
|
end
|
2019-10-29 12:26:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function add_entity(entity)
|
2021-11-14 22:01:01 +02:00
|
|
|
local enable_side_target = Public.get('enable_side_target')
|
2020-11-17 13:45:47 +02:00
|
|
|
|
|
|
|
if not enable_side_target then
|
|
|
|
return
|
|
|
|
end
|
2020-05-23 21:51:15 +02:00
|
|
|
|
2021-11-14 22:01:01 +02:00
|
|
|
local surface_index = Public.get('surface_index')
|
2020-10-22 13:32:18 +02:00
|
|
|
--skip entities that are on another surface
|
2020-11-17 13:45:47 +02:00
|
|
|
if entity.surface.index ~= surface_index then
|
2020-10-22 13:32:18 +02:00
|
|
|
return
|
|
|
|
end
|
2020-11-17 13:45:47 +02:00
|
|
|
|
2021-11-14 22:01:01 +02:00
|
|
|
local side_target_count = Public.get('side_target_count')
|
2020-11-17 13:45:47 +02:00
|
|
|
if side_target_count >= 512 then
|
2020-10-22 13:32:18 +02:00
|
|
|
return
|
|
|
|
end
|
2020-05-23 21:51:15 +02:00
|
|
|
|
2021-11-14 22:01:01 +02:00
|
|
|
local side_targets = Public.get('side_targets')
|
2020-10-22 13:32:18 +02:00
|
|
|
--add entity to the side target list
|
2020-11-17 13:45:47 +02:00
|
|
|
table.insert(side_targets, entity)
|
2021-11-14 22:01:01 +02:00
|
|
|
Public.set('side_target_count', side_target_count + 1)
|
2019-10-29 12:26:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_built_entity(event)
|
2020-10-22 13:32:18 +02:00
|
|
|
if not event.created_entity then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not event.created_entity.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not side_target_types[event.created_entity.type] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
add_entity(event.created_entity)
|
2019-10-29 12:26:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_robot_built_entity(event)
|
2020-10-22 13:32:18 +02:00
|
|
|
if not event.created_entity then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not event.created_entity.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not side_target_types[event.created_entity.type] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
add_entity(event.created_entity)
|
2019-10-29 12:26:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local event = require 'utils.event'
|
|
|
|
event.add(defines.events.on_built_entity, on_built_entity)
|
|
|
|
event.add(defines.events.on_robot_built_entity, on_robot_built_entity)
|
|
|
|
|
2020-05-23 21:51:15 +02:00
|
|
|
return Public
|