summaryrefslogtreecommitdiff
path: root/disengage_damaged_units.lua
diff options
context:
space:
mode:
authorUnicodeSnowman <unicodesnowman@codeberg.org>2023-06-22 11:49:13 -0600
committerUnicodeSnowman <unicodesnowman@codeberg.org>2023-06-22 11:49:13 -0600
commit1316f89a696686acf646a019502093527f68a04b (patch)
tree3fda14ddbfa3706b5f4db9ae269ea965e04f171f /disengage_damaged_units.lua
downloadbar-widgets-1316f89a696686acf646a019502093527f68a04b.tar.gz
bar-widgets-1316f89a696686acf646a019502093527f68a04b.tar.bz2
bar-widgets-1316f89a696686acf646a019502093527f68a04b.zip
Added disengage units lua file
Diffstat (limited to 'disengage_damaged_units.lua')
-rw-r--r--disengage_damaged_units.lua53
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
+