1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/maps/amap/wall_health_booster.lua

148 lines
3.4 KiB
Lua
Raw Normal View History

2021-02-06 05:15:20 +02:00
-- All entities that own a unit_number of a chosen force gain damage resistance.
-- ignores entity health regeneration
-- Use Public.set_health_modifier(force_index, modifier) to modify health.
-- 1 = original health, 2 = 200% total health, 4 = 400% total health,..
local Global = require 'utils.global'
local Event = require 'utils.event'
local Public = {}
local math_round = math.round
local fhb = {}
Global.register(
fhb,
function(tbl)
fhb = tbl
end
)
function Public.set_health_modifier(force_index, modifier)
2021-03-24 17:46:00 +02:00
if not game.forces[force_index] then
return
end
if not modifier then
return
end
if not fhb[force_index] then
fhb[force_index] = {}
end
fhb[force_index].m = math_round(1 / modifier, 4)
2021-02-06 05:15:20 +02:00
end
function Public.reset_tables()
2021-03-24 19:22:45 +02:00
for k, _ in pairs(fhb) do
2021-03-24 17:46:00 +02:00
fhb[k] = nil
end
2021-02-06 05:15:20 +02:00
end
local function on_entity_damaged(event)
2021-03-24 17:46:00 +02:00
local entity = event.entity
2021-02-06 05:15:20 +02:00
2021-03-24 17:46:00 +02:00
if not entity then
return
end
if not entity.valid then
return
2021-02-06 05:15:20 +02:00
end
2021-03-24 17:46:00 +02:00
if not (entity.name == 'stone-wall') then
if not (entity.name == 'gun-turret') then
if not (entity.name == 'laser-turret') then
-- if not (entity.name == 'flamethrower-turret') then
return
--end
end
end
end
2021-02-06 05:15:20 +02:00
2021-03-24 17:46:00 +02:00
local unit_number = entity.unit_number
if not unit_number then
return
end
2021-02-06 05:15:20 +02:00
2021-03-24 17:46:00 +02:00
local boost = fhb[entity.force.index]
if not boost then
return
end
if not boost[unit_number] then
boost[unit_number] = entity.prototype.max_health
end
2021-02-06 05:15:20 +02:00
2021-03-24 17:46:00 +02:00
local new_health = boost[unit_number] - event.final_damage_amount * boost.m
boost[unit_number] = new_health
entity.health = new_health
2021-02-06 05:15:20 +02:00
end
local function on_entity_died(event)
2021-03-24 17:46:00 +02:00
local entity = event.entity
if not entity then
return
end
if not entity.valid then
return
end
2021-02-06 05:15:20 +02:00
2021-03-24 17:46:00 +02:00
if not (entity.name == 'stone-wall') then
if not (entity.name == 'gun-turret') then
if not (entity.name == 'laser-turret') then
-- if not (entity.name == 'flamethrower-turret') then
return
--end
end
end
end
local unit_number = entity.unit_number
if not unit_number then
return
end
local boost = fhb[entity.force.index]
if not boost then
return
end
boost[unit_number] = nil
2021-02-06 05:15:20 +02:00
end
local function on_player_repaired_entity(event)
2021-03-24 17:46:00 +02:00
local entity = event.entity
if not entity then
return
end
if not entity.valid then
return
end
if not (entity.name == 'stone-wall') then
if not (entity.name == 'gun-turret') then
if not (entity.name == 'laser-turret') then
-- if not (entity.name == 'flamethrower-turret') then
return
--end
end
end
end
local unit_number = entity.unit_number
local boost = fhb[entity.force.index]
if not unit_number then
return
end
if not boost then
return
end
boost[unit_number] = entity.health
2021-02-06 05:15:20 +02:00
end
local function on_init()
2021-03-24 17:46:00 +02:00
Public.reset_tables()
2021-02-06 05:15:20 +02:00
end
Event.on_init(on_init)
Event.add(defines.events.on_entity_damaged, on_entity_damaged)
Event.add(defines.events.on_entity_died, on_entity_died)
Event.add(defines.events.on_player_repaired_entity, on_player_repaired_entity)
return Public