-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunit_transport_preserve_queue.lua
70 lines (65 loc) · 2.12 KB
/
unit_transport_preserve_queue.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
function widget:GetInfo()
return {
name = "Unit Transport Preserve (Command) Queue",
desc = "Restores command queue of units after they are unloaded from air transport. Initial move/guard commands in the unit's command queue are discarded.",
author = "IM1, DrWizzard",
date = "July 2023",
license = "GNU GPL, v2 or later",
version = 0.3,
layer = -1,
enabled = true
}
end
local CMD_MOVE = CMD.MOVE
local CMD_GUARD = CMD.GUARD
local CMD_LOAD_UNITS = CMD.LOAD_UNITS
local CMD_LOAD_ONTO = CMD.LOAD_ONTO
local CMD_FIGHT = CMD.FIGHT
local CMD_RECLAIM = CMD.RECLAIM
local CMD_REPAIR = CMD.REPAIR
local CMD_WAIT = CMD.WAIT
local CMD_INSERT = CMD.INSERT
local CMD_OPT_SHIFT = CMD.OPT_SHIFT
local spGiveOrderArrayToUnit = Spring.GiveOrderArrayToUnit
local spGetCommandQueue = Spring.GetCommandQueue
local player_team_id = Spring.GetLocalTeamID()
local recentlyUnloaded = {}
function widget:Initialize()
if Spring.GetSpectatingState() then
widgetHandler:removeWidget()
end
end
function widget:UnitUnloaded(unitID, unitDefID, unitTeam, transportID, transportTeam)
if unitTeam == player_team_id then
local commands = spGetCommandQueue(unitID, -1)
if commands and commands[1] and commands[1].id==CMD_WAIT then
return
end
recentlyUnloaded[#recentlyUnloaded + 1] = {unitID, commands}
end
end
function widget:Update()
if #recentlyUnloaded == 0 then
return
end
for j=1, #recentlyUnloaded do
local kvp = recentlyUnloaded[j]
local unitID = kvp[1]
local oldBuildQueue = kvp[2]
if oldBuildQueue ~= nil then
local orders = {}
local hasSeenValidCommandToPreserve = false
for i = 1, #oldBuildQueue do
local cmd = oldBuildQueue[i]
if cmd['id'] ~= CMD_MOVE and cmd['id'] ~= CMD_GUARD and cmd['id'] ~= CMD_LOAD_UNITS and cmd['id'] ~= CMD_LOAD_ONTO and cmd['id'] ~= CMD_FIGHT and cmd['id'] ~= CMD_RECLAIM and cmd['id'] ~= CMD_REPAIR then
hasSeenValidCommandToPreserve = true
end
if hasSeenValidCommandToPreserve then
orders[#orders+1] = {cmd.id, cmd.params, cmd.options}
end
end
spGiveOrderArrayToUnit(unitID, orders)
end
end
recentlyUnloaded = {}
end