-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_unit_reclaim_destruct.lua
94 lines (78 loc) · 2.64 KB
/
cmd_unit_reclaim_destruct.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function widget:GetInfo()
return {
name = "Reclaim Destruct",
desc = "Adds a command(action handler) to all units that makes all constructors that can relcaim without moving reclaim that unit. Keybind to self_reclaim.",
author = "hihoman23",
date = "January 2024",
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true,
}
end
include("keysym.h.lua")
local constructors = {} -- will get filled up with each constructor in the form of {unitID, buildRange^2}
local GetSelectedUnits = Spring.GetSelectedUnits
local GetSpectatingState = Spring.GetSpectatingState
local GetUnitPosition = Spring.GetUnitPosition
local GiveOrderToUnitArray = Spring.GiveOrderToUnitArray
local GetSelectedUnits = Spring.GetSelectedUnits
local myTeam = Spring.GetMyTeamID()
local CMD_INSERT = CMD.INSERT
local function distSq(x1, z1, x2, z2)
return (x1 - x2)*(x1 - x2) + (x1 - x2)*(x1 - x2)
end
local function getValidCons(x, z, target)
local validCons = {}
for con, dist in pairs(constructors) do
local conX, _, conZ = GetUnitPosition(con)
if (distSq(conX, conZ, x, z) <= dist) and (target ~= con) then
validCons[#validCons+1] = con
end
end
return validCons
end
local function reclaimSelectedUnits()
for _, unitID in pairs(GetSelectedUnits()) do
local ux, _, uz = GetUnitPosition(unitID)
GiveOrderToUnitArray(
getValidCons(ux, uz),
CMD_INSERT,
{0,CMD.RECLAIM, CMD.OPT_SHIFT,unitID},
{'alt'}
)
end
end
function InitUnit(unitID, unitDefID, unitTeam)
if unitTeam == myTeam then
local def = UnitDefs[unitDefID]
if def.isBuilder then
constructors[unitID] = def.buildDistance*def.buildDistance
end
end
end
function widget:UnitCreated(unitID, unitDefID, unitTeam, bID)
InitUnit(unitID, unitDefID, unitTeam)
end
function widget:UnitGiven(unitID, unitDefID, unitTeam)
InitUnit(unitID, unitDefID, unitTeam)
end
function widget:UnitDestroyed(unitID, unitDefID, unitTeam)
if constructors[unitID] then
constructors[unitID] = nil
end
end
function widget:Initialize()
widgetHandler:AddAction("self_reclaim", reclaimSelectedUnits, nil, "p")
if GetSpectatingState() then
widgetHandler:RemoveWidget(self)
do return end
end
for _, unitID in pairs(Spring.GetTeamUnits(myTeam)) do
InitUnit(unitID, Spring.GetUnitDefID(unitID), myTeam)
end
end
function widget:PlayerChanged(playerID)
if GetSpectatingState() then
widgetHandler:RemoveWidget(self)
end
end