From 4256fc497ba958e24691c75dced9d47cef1bc4be Mon Sep 17 00:00:00 2001 From: MewMew Date: Tue, 21 Apr 2020 09:27:18 +0200 Subject: [PATCH] new module --- modules/force_health_booster.lua | 75 ++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 modules/force_health_booster.lua diff --git a/modules/force_health_booster.lua b/modules/force_health_booster.lua new file mode 100644 index 00000000..b9d512fb --- /dev/null +++ b/modules/force_health_booster.lua @@ -0,0 +1,75 @@ +-- 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 set_health_modifier(force_index, modifier) + 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) +end + +function Public.reset_tables() + fhb = {} +end + +local function on_entity_damaged(event) + local entity = event.entity + if not entity and not entity.valid then return 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 + if not boost[unit_number] then boost[unit_number] = entity.prototype.max_health end + + local new_health = boost[unit_number] - event.final_damage_amount * boost.m + boost[unit_number] = new_health + entity.health = new_health +end + +local function on_entity_died(event) + local entity = event.entity + if not entity and not entity.valid then return 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 +end + +local function on_player_repaired_entity(event) + local entity = event.entity + if not entity and not entity.valid then return 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] = entity.health +end + +local function on_init() + Public.reset_tables() +end + +local Event = require 'utils.event' +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) \ No newline at end of file