Skip to content

Commit

Permalink
Allow negative index at GetUnitCurrentCommand to index from queue end. (
Browse files Browse the repository at this point in the history
#1813)

* Allow negative index at GetUnitCurrentCommand to index from the end of
the queue.
* Make forward compat possible

---------

Co-authored-by: sprunk <spr.ng@o2.pl>
  • Loading branch information
saurtron and sprunk authored Dec 16, 2024
1 parent eae2809 commit 63b8152
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion rts/Lua/LuaConstEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ bool LuaConstEngine::PushEntries(lua_State* L)


lua_pushliteral(L, "FeatureSupport");
lua_createtable(L, 0, 2);
lua_createtable(L, 0, 3);
LuaPushNamedBool(L, "NegativeGetUnitCurrentCommand", true);
LuaPushNamedBool(L, "hasExitOnlyYardmaps", true);
LuaPushNamedNumber(L, "rmlUiApiVersion", 1);
lua_rawset(L, -3);
Expand Down
15 changes: 12 additions & 3 deletions rts/Lua/LuaSyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5977,6 +5977,10 @@ static void PackCommandQueue(lua_State* L, const CCommandQueue& commands, size_t
/***
*
* @function Spring.GetUnitCurrentCommand
*
* @number unitID Unit id.
* @number cmdIndex Command index to get. If negative will count from the end of the queue,
* for example -1 will be the last command.
*/
int LuaSyncedRead::GetUnitCurrentCommand(lua_State* L)
{
Expand All @@ -5989,10 +5993,15 @@ int LuaSyncedRead::GetUnitCurrentCommand(lua_State* L)
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
const unsigned int cmdIndex = luaL_optint(L, 2, 1) - 1;
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())
if (cmdIndex >= queue->size() || cmdIndex < 0)
return 0;

const Command& cmd = queue->at(cmdIndex);
Expand Down

0 comments on commit 63b8152

Please sign in to comment.