Skip to content

Instantly share code, notes, and snippets.

@L-e-x-o-n
Last active June 11, 2023 19:39
Show Gist options
  • Save L-e-x-o-n/5452c80026f9f20a9185c098466f8ae7 to your computer and use it in GitHub Desktop.
Save L-e-x-o-n/5452c80026f9f20a9185c098466f8ae7 to your computer and use it in GitHub Desktop.
On action 'prioritise' nearby constructors will prioritise assisting or repairing the selected units
function widget:GetInfo()
return {
name = "Prioritise",
desc = "On action 'prioritise' nearby constructors will prioritise assisting or repairing the selected units",
author = "Lexon",
date = "05.05.2023",
license = "GNU GPL, v2 or later",
layer = 10,
enabled = true
}
end
-- From how far will constructors come to assist the target
local conSearchRange = 700
local ignoreMobileConstructors = false
local myTeamId
local spGetSelectedUnits = Spring.GetSelectedUnits
local spGetUnitsInSphere = Spring.GetUnitsInSphere
local spGetUnitDefID = Spring.GetUnitDefID
local spGiveOrderToUnit = Spring.GiveOrderToUnit
local spGetUnitSeparation = Spring.GetUnitSeparation
local spGetUnitPosition = Spring.GetUnitPosition
local spGetUnitHealth = Spring.GetUnitHealth
local function GetNearbyConstructors(target, alreadyBuilt)
local x, y, z = spGetUnitPosition(target)
local unitsInRange = spGetUnitsInSphere(x, y, z, conSearchRange, myTeamId)
local unitDefID
local nearbyCons = {}
for _, value in pairs(unitsInRange) do
unitDefID = spGetUnitDefID(value)
if UnitDefs[unitDefID].canAssist or (alreadyBuilt and UnitDefs[unitDefID].canRepair) then
-- Only add nanos if they are in range of the target
if UnitDefs[unitDefID].maxAcc == 0 then
if spGetUnitSeparation(target, value, true) < UnitDefs[unitDefID].buildDistance then
nearbyCons[#nearbyCons + 1] = value
end
-- Non nanos can move to the target
elseif not ignoreMobileConstructors then
nearbyCons[#nearbyCons + 1] = value
end
end
end
return nearbyCons
end
local function Prioritise()
local selectedUnits = spGetSelectedUnits()
if #selectedUnits < 1 then return true end
for _, selectedUnit in ipairs(selectedUnits) do
local _, _, _, _, buildProgress = spGetUnitHealth(selectedUnit)
local nearbyCons = GetNearbyConstructors(selectedUnit, buildProgress >= 0.99)
Spring.Echo("Giving order..." .. #nearbyCons)
for _, value in pairs(nearbyCons) do
spGiveOrderToUnit(value, CMD.INSERT, {0, CMD.REPAIR, CMD.OPT_SHIFT, selectedUnit}, {"alt"} )
end
end
end
function widget:Initialize()
myTeamId = Spring.GetMyTeamID()
widgetHandler:AddAction("prioritise", Prioritise, nil, 'p')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment