Skip to content

Commit

Permalink
Add Spring.GetUnitCommandCount for getting a units command queue count (
Browse files Browse the repository at this point in the history
#1814)

Also mark `Spring.GetUnitCommands(unitID, 0)` as deprecated (but still return the expected output as before)
  • Loading branch information
saurtron authored Dec 16, 2024
1 parent b983f34 commit d7f4de7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<const CFactoryCAI*>(commandAI);
const CCommandQueue* queue = (factoryCAI == nullptr)? &commandAI->commandQue : &factoryCAI->newUnitCommands;

lua_pushnumber(L, queue->size());

return 1;
}

/***
*
Expand Down
1 change: 1 addition & 0 deletions rts/Lua/LuaSyncedRead.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d7f4de7

Please sign in to comment.