1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-11-25 22:32:18 +02:00
Files
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 20:14:55 +01:00
local Event = require 'utils.event'
2019-02-16 17:36:12 +01:00
2019-10-12 22:48:16 +02:00
local entity_whitelist = {
2024-09-25 23:25:57 +02:00
['big-rock'] = true,
['big-sand-rock'] = true,
['huge-rock'] = true,
2021-03-24 16:46:00 +01:00
['mineable-wreckage'] = true
2019-10-12 22:48:16 +02:00
}
2019-02-16 17:36:12 +01:00
2019-10-12 22:48:16 +02:00
local function process_entity(v, key)
2021-03-24 16:46:00 +01:00
if not v.entity then
storage.entities_regenerate_health[key] = nil
2021-03-24 16:46:00 +01:00
return
end
if not v.entity.valid then
storage.entities_regenerate_health[key] = nil
2021-03-24 16:46:00 +01:00
return
end
if v.last_damage + 36000 < game.tick then
2024-10-14 23:46:34 +02:00
v.entity.health = v.entity.health + math.floor(v.entity.max_health * 0.02)
if v.entity.max_health == v.entity.health then
storage.entities_regenerate_health[key] = nil
2021-03-24 16:46:00 +01:00
end
end
2019-02-16 17:36:12 +01:00
end
local function on_entity_damaged(event)
2021-03-24 16:46:00 +01: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
storage.entities_regenerate_health[tostring(event.entity.position.x) .. '_' .. tostring(event.entity.position.y)] = { last_damage = game.tick, entity = event.entity }
2019-02-16 17:36:12 +01:00
end
2021-03-24 20:14:55 +01:00
local function tick()
for key, entity in pairs(storage.entities_regenerate_health) do
2021-03-24 16:46:00 +01:00
process_entity(entity, key)
end
2019-02-16 17:36:12 +01:00
end
2021-03-24 16:46:00 +01:00
2021-03-24 20:14:55 +01:00
local function on_init()
storage.entities_regenerate_health = {}
2021-03-24 16:46:00 +01:00
end
2021-03-24 20:14:55 +01:00
Event.on_nth_tick(1800, tick)
Event.on_init(on_init)
Event.add(defines.events.on_entity_damaged, on_entity_damaged)