From 3493cc35878469b898b9b5faeed6a2625360df79 Mon Sep 17 00:00:00 2001 From: Saurtron Date: Fri, 13 Dec 2024 00:31:50 +0100 Subject: [PATCH 1/2] Add Spring.GetUnitCurrentCommandID to get the cmdID for a specific command at the units queue. --- rts/Lua/LuaSyncedRead.cpp | 36 ++++++++++++++++++++++++++++++++++++ rts/Lua/LuaSyncedRead.h | 1 + 2 files changed, 37 insertions(+) diff --git a/rts/Lua/LuaSyncedRead.cpp b/rts/Lua/LuaSyncedRead.cpp index d6164188d6..c913568a3e 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 + unsigned 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); From 52db73c7841da8e9a81f5e58d64a02c68411ab4c Mon Sep 17 00:00:00 2001 From: Saurtron Date: Fri, 13 Dec 2024 00:42:41 +0100 Subject: [PATCH 2/2] Unsigned can't be negative. --- rts/Lua/LuaSyncedRead.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rts/Lua/LuaSyncedRead.cpp b/rts/Lua/LuaSyncedRead.cpp index c913568a3e..3ee63f133f 100644 --- a/rts/Lua/LuaSyncedRead.cpp +++ b/rts/Lua/LuaSyncedRead.cpp @@ -6027,7 +6027,7 @@ int LuaSyncedRead::GetUnitCurrentCommandID(lua_State* L) const CCommandQueue* queue = (factoryCAI == nullptr)? &commandAI->commandQue : &factoryCAI->newUnitCommands; // - 1 to convert from lua index to C index - unsigned int cmdIndex = luaL_optint(L, 2, 1); + int cmdIndex = luaL_optint(L, 2, 1); if (cmdIndex > 0) { // - 1 to convert from lua index to C index cmdIndex -= 1;