-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_follow_mouse.lua
78 lines (67 loc) · 2.61 KB
/
unit_follow_mouse.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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- file: write_unit_defs.lua
-- brief: writes unit defs to file
-- author: Tim Pitman
--
-- Copyright (C) 2008.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widget:GetInfo()
return {
name = "FollowMouse",
desc = "Selected units follow the mouse cursor when CTRL-F is held down",
author = "Aztek",
date = "Jun 8, 2008",
license = "GNU GPL, v2 or later",
layer = -10,
enabled = false -- loaded by default?
}
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- include keyboard key definitions
include("keysym.h.lua")
-- static delay timer
delayTimer = 0
spGetKeyState = Spring.GetKeyState
spGetModKeyState = Spring.GetModKeyState
spGetMouseState = Spring.GetMouseState
spTraceScreenRay = Spring.TraceScreenRay
spGetSelectedUnits = Spring.GetSelectedUnits
spGetUnitDefID = Spring.GetUnitDefID
spGiveOrderToUnit = Spring.GiveOrderToUnit
local function MoveUnit(unitID, x, y, z)
-- Spring.Echo('MoveUnit called')
spGiveOrderToUnit(unitID, CMD.MOVE, { x, y, z }, { "" })
end
function widget:Update(deltaTime)
-- wait at least 5 cycles between sending orders
-- currently this is how I'm getting rid of the constant "Can't reach destination" errors.
if (delayTimer > 5) then
delayTimer = 0
local fKeyPressed = spGetKeyState(KEYSYMS.F)
local alt,ctrl,meta,shift = spGetModKeyState()
if (ctrl and fKeyPressed) then
local mousePosX, mousePosY = spGetMouseState()
local itemUnderMouse, moveToPos = spTraceScreenRay(mousePosX, mousePosY, true)
if (itemUnderMouse == "ground") then
local units = spGetSelectedUnits()
for i,unitID in ipairs(units) do
local unitDefID = spGetUnitDefID(unitID)
if (UnitDefs[unitDefID].canMove) then
-- TODO: there's got to be a better way to do this...
MoveUnit(unitID, moveToPos[1], moveToPos[2], moveToPos[3])
end
end
end
end
else
delayTimer = delayTimer + 1
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------