Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Spring.GetUnitCurrentCommandID a cmdID from a units queue. #1815

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ bool LuaSyncedRead::PushEntries(lua_State* L)

REGISTER_LUA_CFUNC(GetUnitCommands);
REGISTER_LUA_CFUNC(GetUnitCurrentCommand);
REGISTER_LUA_CFUNC(GetUnitCurrentCommandID);
REGISTER_LUA_CFUNC(GetFactoryCounts);
REGISTER_LUA_CFUNC(GetFactoryCommands);

Expand Down Expand Up @@ -6007,6 +6008,41 @@ int LuaSyncedRead::GetUnitCurrentCommand(lua_State* L)
return 3 + numParams;
}

/*** Get the cmdID for a specific command in a units queue.
*
* @number unitID
* @number cmdIndex Command index to get. If negative will count from the end of the queue,
* for example -1 will be the last command.
* @treturn number cmdID
*/
int LuaSyncedRead::GetUnitCurrentCommandID(lua_State* L)
{
const CUnit* unit = ParseAllyUnit(L, __func__, 1);

if (unit == nullptr)
return 0;

const CCommandAI* commandAI = unit->commandAI; // never null
const CFactoryCAI* factoryCAI = dynamic_cast<const CFactoryCAI*>(commandAI);
const CCommandQueue* queue = (factoryCAI == nullptr)? &commandAI->commandQue : &factoryCAI->newUnitCommands;

// - 1 to convert from lua index to C index
int cmdIndex = luaL_optint(L, 2, 1);
if (cmdIndex > 0) {
// - 1 to convert from lua index to C index
cmdIndex -= 1;
} else {
cmdIndex = queue->size()-cmdIndex;
}
if (cmdIndex >= queue->size() || cmdIndex < 0)
return 0;

const Command& cmd = queue->at(cmdIndex);
lua_pushnumber(L, cmd.GetID());

return 1;
}


/*** Parameters for command options
*
Expand Down
1 change: 1 addition & 0 deletions rts/Lua/LuaSyncedRead.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class LuaSyncedRead {

static int GetUnitCommands(lua_State* L);
static int GetUnitCurrentCommand(lua_State* L);
static int GetUnitCurrentCommandID(lua_State* L);
static int GetFactoryCounts(lua_State* L);
static int GetFactoryCommands(lua_State* L);

Expand Down