From d7f4de7c80ebdd98dfce942a43060dc8d971aecb Mon Sep 17 00:00:00 2001 From: saurtron Date: Mon, 16 Dec 2024 23:31:23 +0100 Subject: [PATCH] Add Spring.GetUnitCommandCount for getting a units command queue count (#1814) Also mark `Spring.GetUnitCommands(unitID, 0)` as deprecated (but still return the expected output as before) --- rts/Lua/LuaSyncedRead.cpp | 26 ++++++++++++++++++++++++++ rts/Lua/LuaSyncedRead.h | 1 + 2 files changed, 27 insertions(+) diff --git a/rts/Lua/LuaSyncedRead.cpp b/rts/Lua/LuaSyncedRead.cpp index a0fcb78d5c..bd6ea4548c 100644 --- a/rts/Lua/LuaSyncedRead.cpp +++ b/rts/Lua/LuaSyncedRead.cpp @@ -267,6 +267,7 @@ bool LuaSyncedRead::PushEntries(lua_State* L) REGISTER_LUA_CFUNC(GetUnitBlocking); REGISTER_LUA_CFUNC(GetUnitMoveTypeData); + REGISTER_LUA_CFUNC(GetUnitCommandCount); REGISTER_LUA_CFUNC(GetUnitCommands); REGISTER_LUA_CFUNC(GetUnitCurrentCommand); REGISTER_LUA_CFUNC(GetFactoryCounts); @@ -6123,6 +6124,11 @@ int LuaSyncedRead::GetUnitCommands(lua_State* L) // *get wants the actual commands PackCommandQueue(L, *queue, numCmds); } else { + static bool deprecatedMsgDone = false; + if (!deprecatedMsgDone) { + LOG_L(L_WARNING, "Getting the command count using GetUnitCommands/GetCommandQueue is deprecated. Please use Spring.GetUnitCommandCount instead."); + deprecatedMsgDone = true; + } // *get just wants the queue's size lua_pushnumber(L, queue->size()); } @@ -6166,6 +6172,26 @@ int LuaSyncedRead::GetFactoryCommands(lua_State* L) return 1; } +/*** Get the number of commands in a units queue. + * + * @number unitID + */ +int LuaSyncedRead::GetUnitCommandCount(lua_State* L) +{ + const CUnit* unit = ParseAllyUnit(L, __func__, 1); + + if (unit == nullptr) + return 0; + + const CCommandAI* commandAI = unit->commandAI; + + const CFactoryCAI* factoryCAI = dynamic_cast(commandAI); + const CCommandQueue* queue = (factoryCAI == nullptr)? &commandAI->commandQue : &factoryCAI->newUnitCommands; + + lua_pushnumber(L, queue->size()); + + return 1; +} /*** * diff --git a/rts/Lua/LuaSyncedRead.h b/rts/Lua/LuaSyncedRead.h index e6fae6952a..9b7e27c140 100644 --- a/rts/Lua/LuaSyncedRead.h +++ b/rts/Lua/LuaSyncedRead.h @@ -169,6 +169,7 @@ class LuaSyncedRead { static int GetUnitBlocking(lua_State* L); static int GetUnitMoveTypeData(lua_State* L); + static int GetUnitCommandCount(lua_State* L); static int GetUnitCommands(lua_State* L); static int GetUnitCurrentCommand(lua_State* L); static int GetFactoryCounts(lua_State* L);