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

84 lines
2.5 KiB
Lua
Raw Normal View History

--biters may revive depending of storage.biter_reanimator.forces["biter force index"]
2020-01-19 07:06:38 +01:00
--0 = no extra life
--0.25 = 25% chance for another life
--1.5 = 1 extra life + 50% chance of another life
--3 = 3 extra lifes
local math_random = math.random
2020-01-19 09:38:30 +01:00
local function register_unit(unit, extra_lifes, unit_group)
if storage.biter_reanimator.units[unit.unit_number] then
2021-03-24 16:46:00 +01:00
return
end
storage.biter_reanimator.units[unit.unit_number] = { extra_lifes, unit_group }
2021-03-24 16:46:00 +01:00
--game.print("bitey number " .. unit.unit_number .. ", i have " .. extra_lifes .. " extra lives left!")
2020-01-19 09:38:30 +01:00
end
2020-01-19 07:06:38 +01:00
local function reanimate(entity)
local extra_lifes = storage.biter_reanimator.units[entity.unit_number][1]
local unit_group = storage.biter_reanimator.units[entity.unit_number][2]
2020-01-19 07:06:38 +01:00
2021-03-24 16:46:00 +01:00
if extra_lifes <= 0 then
storage.biter_reanimator.units[entity.unit_number] = nil
2021-03-24 16:46:00 +01:00
return
end
if extra_lifes < 1 then
if math_random(1, 10000) > extra_lifes * 10000 then
storage.biter_reanimator.units[entity.unit_number] = nil
2021-03-24 16:46:00 +01:00
return
end
end
local revived_entity = entity.clone({ position = entity.position, surface = entity.surface, force = entity.force })
2024-10-14 23:46:34 +02:00
revived_entity.health = revived_entity.max_health
2021-03-24 16:46:00 +01:00
register_unit(revived_entity, extra_lifes - 1, unit_group)
if unit_group then
if unit_group.valid then
unit_group.add_member(revived_entity)
end
end
storage.biter_reanimator.units[entity.unit_number] = nil
2021-03-24 16:46:00 +01:00
entity.destroy()
2020-01-19 07:06:38 +01:00
end
2021-03-24 16:46:00 +01:00
local function on_entity_died(event)
local entity = event.entity
if not entity.valid then
return
end
if entity.type ~= 'unit' then
return
end
local extra_lifes = 0
if storage.biter_reanimator.forces[entity.force.index] then
extra_lifes = storage.biter_reanimator.forces[entity.force.index]
2021-03-24 16:46:00 +01:00
end
register_unit(entity, extra_lifes, false)
reanimate(entity)
2020-01-19 07:06:38 +01:00
end
2020-01-19 09:38:30 +01:00
local function on_unit_added_to_group(event)
2021-03-24 16:46:00 +01:00
local unit = event.unit
local group = event.group
local extra_lifes = storage.biter_reanimator.forces[unit.force.index]
2021-03-24 16:46:00 +01:00
if extra_lifes then
register_unit(unit, extra_lifes, group)
else
register_unit(unit, 0, group)
end
2020-01-19 09:38:30 +01:00
end
2020-01-19 07:06:38 +01:00
local function on_init()
storage.biter_reanimator = {}
storage.biter_reanimator.forces = {}
storage.biter_reanimator.units = {}
2020-01-19 07:06:38 +01:00
end
local Event = require 'utils.event'
Event.on_init(on_init)
2020-01-19 09:38:30 +01:00
Event.add(defines.events.on_entity_died, on_entity_died)
2021-03-24 16:46:00 +01:00
Event.add(defines.events.on_unit_added_to_group, on_unit_added_to_group)