diff options
Diffstat (limited to 'disengage_damaged_units.lua')
-rw-r--r-- | disengage_damaged_units.lua | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/disengage_damaged_units.lua b/disengage_damaged_units.lua new file mode 100644 index 0000000..eecd364 --- /dev/null +++ b/disengage_damaged_units.lua @@ -0,0 +1,53 @@ +function widget:GetInfo() + return { + name = "DisengageDamagedUnits", + desc = "Disengages damaged units from the battle", + author = "UnicodeSnowman", + date = "May 2023", + license = "GPL v2", + layer = 0, + enabled = true + } +end + +-- Some defines to make the language server happier. +widgetHandler = assert(widgetHandler) +widget = assert(widget) +Spring = assert(Spring) +WG = assert(WG) + +-- Units below this damage threshold will be disengaged. TODO make this +-- configurable. +local damageThreshold = 0.3 + +-- Spring commands for more speed. +local spGetSelectedUnits = Spring.GetSelectedUnits +local spGetUnitHealth = Spring.GetUnitHealth +local spGiveOrderToUnit = Spring.GiveOrderToUnit +local spSelectUnitArray = Spring.SelectUnitArray + +local function disengageDamagedUnits() + local newSelection = {} + + for _, uid in pairs(spGetSelectedUnits()) do + local health, maxHealth, _, _, _ = spGetUnitHealth(uid) + local percent = health / maxHealth + + if percent < damageThreshold then + Spring.Echo("Disengaging " .. uid) + spGiveOrderToUnit(uid, WG.CMD_DISENGAGE, {}, 0) + else + table.insert(newSelection, uid) + end + end + + spSelectUnitArray(newSelection) +end + +function widget:Initialize() + -- The units with health below the health threshold will be disegaged from + -- combat and be sent back to the fallback position. + widgetHandler:AddAction( + "disengage_damaged_units", disengageDamagedUnits, nil, "p") +end + |