-
Notifications
You must be signed in to change notification settings - Fork 0
/
idle_rezbot_notifier.lua
124 lines (111 loc) · 3.43 KB
/
idle_rezbot_notifier.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function widget:GetInfo()
return {
name = "Idle Rezbot Notifier",
desc = "Notifies you when a rezbot is idle(alt + h) to toggle. Then, press alt + s to go to the rezbot(with the camera) and alt + d to go back.",
author = "hihoman23",
date = "2023",
license = "GNU GPL, v3 or later",
layer = 0,
enabled = true -- loaded by default?
}
end
local rezNames = {
["armrectr"] = true,
["cornecro"] = true
}
local rezzers = {}
local myTeam = Spring.GetMyTeamID()
local pingIdles = true
local lastPingedRezzer
local prevCamX
local prevCamZ
local prevSelectedUnits
function DeselectAllUnits()
for _, v in pairs(Spring.GetSelectedUnits()) do
Spring.DeselectUnit(v)
end
end
--moves camera to specific x and z positions
function MoveCam(x,z)
local camState = Spring.GetCameraState()
camState.px = x
camState.py = camState.py
camState.pz = z
Spring.SetCameraState(camState, 0)
end
-- returns x, z
function GetCamPos()
local camState = Spring.GetCameraState()
local x, z = camState.px, camState.pz
return x, z
end
function widget:UnitIdle(unitID)
if rezzers[unitID] and pingIdles then
local ux, uy, uz = Spring.GetUnitPosition(unitID)
Spring.MarkerAddPoint(ux, uy, uz, "rezzer idle", true)
lastPingedRezzer = unitID
end
end
function widget:KeyPress(key, mods, isRepeat)
if (key == 104) and (mods.alt) then -- alt + h
pingIdles = not pingIdles
if pingIdles then
Spring.Echo("idle rezbot notifications activated")
else
Spring.Echo("idle rezbot notifications disabled")
end
end
if (key == 115) and (mods.alt) then -- alt + s
if lastPingedRezzer then
local selUnits = Spring.GetSelectedUnits()
if not (selUnits[1] == lastPingedRezzer) then
prevCamX, prevCamZ = GetCamPos()
prevSelectedUnits = selUnits
end
local ux, _, uz = Spring.GetUnitPosition(lastPingedRezzer)
MoveCam(ux, uz)
DeselectAllUnits()
Spring.SelectUnit(lastPingedRezzer)
end
end
if (key == 100) and (mods.alt) then -- alt + d
if prevCamX then
MoveCam(prevCamX, prevCamZ)
DeselectAllUnits()
Spring.SelectUnitArray(prevSelectedUnits)
end
end
end
function InitUnit(unitID, unitDefID, unitTeam)
if unitTeam == myTeam then
local def = UnitDefs[unitDefID]
if rezNames[def.name] then
rezzers[unitID] = true
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 rezzers[unitID] then
rezzers[unitID] = nil
end
end
function widget:Initialize()
if Spring.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 Spring.GetSpectatingState() then
widgetHandler:RemoveWidget(self)
end
end