-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_center_camera_action.lua
113 lines (100 loc) · 2.79 KB
/
cmd_center_camera_action.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
function widget:GetInfo()
return {
name = "Center On Action",
desc = "Adds button that sends camera to attacked units.",
author = "hihoman23",
date = "2023",
license = "GNU GPL, v3 or later",
layer = 0,
enabled = true -- loaded by default?
}
end
--change keybinds at line 98
--disables widget for scout spam
local toIgnore = {
["armflea"] = true, --Tick
["armpw"] = true, --Pawn
["armfav"] = true, --Rover
["corak"] = true, --Grunt
["corfav"] = true, --Rascal
}
local myTeam = Spring.GetMyTeamID()
local spGetUnitPosition = Spring.GetUnitPosition
local attackedUnitX
local attackedUnitZ
local whenAttacked
local prevCamPosX
local prevCamPosZ
local prevSelectedUnit
local letterNPressed = false
local letterMPressed = false
local reactTime = 15 --how long you have to press the button to send camera to attacked unit
local selectedUnits
local defending = false
function DeselectAllUnits()
for i, 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
function widget:GameFrame(n)
--Spring.Echo(attackedUnitX)
if attackedUnitX ~= nil then
if letterMPressed then
local camState = Spring.GetCameraState()
if prevCamPosX == nil then
prevCamPosX = camState.px
prevCamPosZ = camState.pz
prevSelectedUnit = Spring.GetSelectedUnits()
end
MoveCam(attackedUnitX, attackedUnitZ)
DeselectAllUnits()
do return end
end
if whenAttacked + reactTime <= Spring.GetGameSeconds() then
attackedUnitX = nil
return
end
if letterNPressed then
MoveCam(prevCamPosX, prevCamPosZ)
Spring.SelectUnitArray(prevSelectedUnit)
prevCamPosX = nil
end
end
end
function widget:UnitDamaged(unitID, unitDefID, unitTeam)
if unitTeam == myTeam then
local def = UnitDefs[unitDefID]
if toIgnore[def.name] == nil then
local ux, uy, uz = spGetUnitPosition(unitID)
attackedUnitX = ux
attackedUnitZ = uz
whenAttacked = Spring.GetGameSeconds()
end
end
end
--letter n = 110
--letter m = 109
function widget:KeyPress(key)
if key == 110 then
letterNPressed = true
end
if key == 109 then
letterMPressed = true
end
end
function widget:KeyRelease(key)
if key == 110 then
letterNPressed = false
end
if key == 109 then
letterMPressed = false
end
end