How to use Spring.GiveOrderToUnit
Posted: 20 Sep 2007, 02:58
I'm working on learning how to give units orders using LUA scripting. Since there's only a few scarce examples and the source code itself to go from I'm having quite a hard time doing it, so I thought I'd try to structure it into this thread and hopefully make some useful eventual wiki documentation. Since I basically have no idea what I'm doing there will probably be lots of errors in here for a while, a work in progress. I'd appreciate if people who knows better can point out any blatant errors, and hopefully fill in some of the blanks.
From the current wiki, this is what we're mainly dealing with AFAIK:
Ok, so basically we want to give the unit an order, wich is complicated by the fact that there's a queue of orders and several different order types. The main problem is figuring out exactly what each parameter is. Since there's different types of commands with assumedly different parameters this isn't easy. I havn't found any relevant source code or documentation on the parameters so I'll have to try and guess from example code.
The first parameter is straight forward, simply wich unit will receive the order. The second parameter, cmdID is a bit trickier. I think this refers to wich command is to be given. I found this list in the spring source code wich I think is what goes in here:
CMD_INSERT
CMD_REMOVE
These are used to put a command into the command queue (=waypoint list) at a specified location.
Now for the really tricky part, the params and options parameters. Params is a table (kind of struct/array) containing the parameters for the order itself, such as coordinates for a move order or a unit to guard for a guard order, etc. Im really not sure what options does at the moment.
For a simple example let's consider the CMD_MOVE, from the recent popular customformations widget:
cmdTag is set to:
at the beginning of the code. pos is a table with the destination coordinates, {x, z, y}. The options state in this case I think is simply modifieing the order in a way similar to a meta keys being pressed if the order was given normally ingame, like shift for waypoints. So if you put {"shift"} here there is a waypoint added at the end of the order queue.
Next, we have the CMD_INSERT and CMD_REMOVE. The example in this case is the saboteur script:
Ok, so looking at this code, inside the param table (third parameter of the function, being the "parameters" for the CMD_INSERT as well as the order being inserted, yeah confusing I know). The first variable, -1, cmd.tag, 0 respectively, is the position in the queue. I'm not sure yet wich position in the queue is refered to with what number. The second one is obviously what type of command is to be inserted. In the two first examples, I have no idea what the third thing, CMD_OPT_INTERNAL+1, is. The 4th, set to unitID2, is the target of the capture command. In the last example with stop, there's a zero at the third parameter, wich I have no clue about.
Future topics:
List of all parameters for all commands.
How to make and use Custom commands.
Variants, such as Spring.GiveOrderArrayToUnitMap (heh, no clue what that means).
From the current wiki, this is what we're mainly dealing with AFAIK:
Code: Select all
Spring.GiveOrderToUnit
( number unitID,
number cmdID,
params = {number, etc...},
options = {"alt", "ctrl", "shift", "right"} ) -> nil
The first parameter is straight forward, simply wich unit will receive the order. The second parameter, cmdID is a bit trickier. I think this refers to wich command is to be given. I found this list in the spring source code wich I think is what goes in here:
I think you have to write actually a period instead of the underscores, I have seen people redefine the command names to use an underscore. Then there's a pair of special command ids, namely:00012 #define CMD_STOP 0
00013 #define CMD_INSERT 1
00014 #define CMD_REMOVE 2
00015 #define CMD_WAIT 5
00016 #define CMD_TIMEWAIT 6
00017 #define CMD_DEATHWAIT 7
00018 #define CMD_SQUADWAIT 8
00019 #define CMD_GATHERWAIT 9
00020 #define CMD_MOVE 10
00021 #define CMD_PATROL 15
00022 #define CMD_FIGHT 16
00023 #define CMD_ATTACK 20
00024 #define CMD_AREA_ATTACK 21
00025 #define CMD_GUARD 25
00026 #define CMD_AISELECT 30
00027 #define CMD_GROUPSELECT 35
00028 #define CMD_GROUPADD 36
00029 #define CMD_GROUPCLEAR 37
00030 #define CMD_REPAIR 40
00031 #define CMD_FIRE_STATE 45
00032 #define CMD_MOVE_STATE 50
00033 #define CMD_SETBASE 55
00034 #define CMD_INTERNAL 60
00035 #define CMD_SELFD 65
00036 #define CMD_SET_WANTED_MAX_SPEED 70
00037 #define CMD_LOAD_UNITS 75
00038 #define CMD_LOAD_ONTO 76
00039 #define CMD_UNLOAD_UNITS 80
00040 #define CMD_UNLOAD_UNIT 81
00041 #define CMD_ONOFF 85
00042 #define CMD_RECLAIM 90
00043 #define CMD_CLOAK 95
00044 #define CMD_STOCKPILE 100
00045 #define CMD_DGUN 105
00046 #define CMD_RESTORE 110
00047 #define CMD_REPEAT 115
00048 #define CMD_TRAJECTORY 120
00049 #define CMD_RESURRECT 125
00050 #define CMD_CAPTURE 130
00051 #define CMD_AUTOREPAIRLEVEL 135
00052 #define CMD_LOOPBACKATTACK 140
00053 #define CMD_IDLEMODE 145
CMD_INSERT
CMD_REMOVE
These are used to put a command into the command queue (=waypoint list) at a specified location.
Now for the really tricky part, the params and options parameters. Params is a table (kind of struct/array) containing the parameters for the order itself, such as coordinates for a move order or a unit to guard for a guard order, etc. Im really not sure what options does at the moment.
For a simple example let's consider the CMD_MOVE, from the recent popular customformations widget:
Code: Select all
Spring.GiveOrderToUnit(unitID, cmdTag, pos, keyState)
Code: Select all
local cmdTag = CMD.MOVE
Next, we have the CMD_INSERT and CMD_REMOVE. The example in this case is the saboteur script:
Code: Select all
GiveOrderToUnit(unitID,CMD_INSERT,{-1,CMD_CAPTURE,CMD_OPT_INTERNAL+1,unitID2},{"alt"});
GiveOrderToUnit(unitID,CMD_INSERT,{cmd.tag,CMD_CAPTURE,CMD_OPT_INTERNAL+1,unitID2},{});
GiveOrderToUnit(unitID,CMD_INSERT,{0,CMD_STOP,0},{"alt"});
Future topics:
List of all parameters for all commands.
How to make and use Custom commands.
Variants, such as Spring.GiveOrderArrayToUnitMap (heh, no clue what that means).