Ticket #1771: cmd_state_reverse_toggle.lua

File cmd_state_reverse_toggle.lua, 1.3 KB (added by Google Frog, 3 years ago)

Adds reverse toggle for fire and movestates

Line 
1function widget:GetInfo()
2  return {
3    name      = "State Reverse Toggle",
4    desc      = "Makes fire and movestates reverse toggleable",
5    author    = "Google Frog",
6    date      = "Oct 2, 2009",
7    license   = "GNU GPL, v2 or later",
8    layer     = 0,
9    enabled   = true  --  loaded by default?
10  }
11end
12
13local spGetSelectedUnits = Spring.GetSelectedUnits
14local spGiveOrderToUnit = Spring.GiveOrderToUnit
15
16local CMD_FIRE_STATE = CMD.FIRE_STATE
17local CMD_MOVE_STATE = CMD.MOVE_STATE
18
19function widget:CommandNotify(id, params, options)
20       
21        if id == CMD_FIRE_STATE then
22                if options.right then
23                        local units = spGetSelectedUnits()
24                        local state = params[1]
25                        if state == 0 then
26                                state = 1
27                        elseif state == 1 then
28                                state = 2
29                        else
30                                state = 0
31                        end
32                        for _,sid in ipairs(units) do
33                                spGiveOrderToUnit(sid, CMD_FIRE_STATE, { state }, {})   
34                        end
35                        return true
36                end
37        end     
38       
39        if id == CMD_MOVE_STATE then
40                if options.right then
41                        local units = spGetSelectedUnits()
42                        local state = params[1]
43                        if state == 0 then
44                                state = 1
45                        elseif state == 1 then
46                                state = 2
47                        else
48                                state = 0
49                        end
50                        for _,sid in ipairs(units) do
51                                spGiveOrderToUnit(sid, CMD_MOVE_STATE, { state }, {})   
52                        end
53                        return true
54                end
55        end
56
57end