1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-08 00:39:30 +02:00
ComfyFactorio/modules/rocks_heal_over_time.lua

55 lines
1.5 KiB
Lua
Raw Normal View History

2019-10-12 22:48:16 +02:00
-- rocks and other entities heal over time -- by mewmew
2021-03-24 21:14:55 +02:00
local Event = require 'utils.event'
2019-02-16 18:36:12 +02:00
2019-10-12 22:48:16 +02:00
local entity_whitelist = {
2021-03-24 17:46:00 +02:00
['rock-big'] = true,
['sand-rock-big'] = true,
['rock-huge'] = true,
['mineable-wreckage'] = true
2019-10-12 22:48:16 +02:00
}
2019-02-16 18:36:12 +02:00
2019-10-12 22:48:16 +02:00
local function process_entity(v, key)
2021-03-24 17:46:00 +02:00
if not v.entity then
global.entities_regenerate_health[key] = nil
return
end
if not v.entity.valid then
global.entities_regenerate_health[key] = nil
return
end
if v.last_damage + 36000 < game.tick then
v.entity.health = v.entity.health + math.floor(v.entity.prototype.max_health * 0.02)
if v.entity.prototype.max_health == v.entity.health then
global.entities_regenerate_health[key] = nil
end
end
2019-02-16 18:36:12 +02:00
end
local function on_entity_damaged(event)
2021-03-24 17:46:00 +02:00
if not event.entity.valid then
return
end
if event.entity.force.index ~= 3 then
return
end
if not entity_whitelist[event.entity.name] then
return
end
global.entities_regenerate_health[tostring(event.entity.position.x) .. '_' .. tostring(event.entity.position.y)] = {last_damage = game.tick, entity = event.entity}
2019-02-16 18:36:12 +02:00
end
2021-03-24 21:14:55 +02:00
local function tick()
2021-03-24 17:46:00 +02:00
for key, entity in pairs(global.entities_regenerate_health) do
process_entity(entity, key)
end
2019-02-16 18:36:12 +02:00
end
2021-03-24 17:46:00 +02:00
2021-03-24 21:14:55 +02:00
local function on_init()
2021-03-24 17:46:00 +02:00
global.entities_regenerate_health = {}
end
2021-03-24 21:14:55 +02:00
Event.on_nth_tick(1800, tick)
Event.on_init(on_init)
Event.add(defines.events.on_entity_damaged, on_entity_damaged)