Skip to content

Commit

Permalink
keybind.cpp: Fix wrap-around behavior in kf_JumpToResourceExtractor()
Browse files Browse the repository at this point in the history
Previously, upon reaching the `end()` of
`apsExtractorLists[selectedPlayer]`, the function
erroneously reported the "Unable to locate any oil derricks!"
message until the next iteration, which needed to check
for `std::next(*psOldRE)`, but this check was missing.

Signed-off-by: Pavel Solodovnikov <[email protected]>
  • Loading branch information
ManManson committed Dec 27, 2023
1 parent 44b438a commit a135994
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/keybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,16 +1625,23 @@ void kf_JumpToResourceExtractor()
/* not supported if a spectator */
SPECTATOR_NO_OP();

if (psOldRE.has_value() && *psOldRE != apsExtractorLists[selectedPlayer].end())
if (apsExtractorLists[selectedPlayer].empty())
{
++(*psOldRE);
addConsoleMessage(_("Unable to locate any oil derricks!"), LEFT_JUSTIFY, SYSTEM_MESSAGE);
return;
}
else

if (!psOldRE.has_value() || std::next(*psOldRE) == apsExtractorLists[selectedPlayer].end())
{
// Reset the pointer if `psOldRE` is either not initialized yet or points to the last element.
psOldRE.emplace(apsExtractorLists[selectedPlayer].begin());
}
else
{
++(*psOldRE);
}

if (*psOldRE != apsExtractorLists[selectedPlayer].end() && **psOldRE != nullptr)
if (**psOldRE != nullptr)
{
playerPos.r.y = 0; // face north
setViewPos(map_coord((**psOldRE)->pos.x), map_coord((**psOldRE)->pos.y), true);
Expand All @@ -1643,7 +1650,6 @@ void kf_JumpToResourceExtractor()
{
addConsoleMessage(_("Unable to locate any oil derricks!"), LEFT_JUSTIFY, SYSTEM_MESSAGE);
}

}

void keybindInformResourceExtractorRemoved(const STRUCTURE* psResourceExtractor)
Expand Down

0 comments on commit a135994

Please sign in to comment.