Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow negative index at GetUnitCurrentCommand to index from queue end. #1813

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
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())
if (cmdIndex >= queue->size() || cmdIndex < 0)
lhog marked this conversation as resolved.
Show resolved Hide resolved
return 0;

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