diff --git a/rts/Lua/LuaSyncedRead.cpp b/rts/Lua/LuaSyncedRead.cpp index d6164188d6..3ee63f133f 100644 --- a/rts/Lua/LuaSyncedRead.cpp +++ b/rts/Lua/LuaSyncedRead.cpp @@ -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); @@ -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(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 * diff --git a/rts/Lua/LuaSyncedRead.h b/rts/Lua/LuaSyncedRead.h index e6fae6952a..7b6422a6f3 100644 --- a/rts/Lua/LuaSyncedRead.h +++ b/rts/Lua/LuaSyncedRead.h @@ -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);