From a1359942f2a30f5b85978f53903c91526cac0715 Mon Sep 17 00:00:00 2001 From: Pavel Solodovnikov Date: Thu, 28 Dec 2023 01:25:23 +0300 Subject: [PATCH] keybind.cpp: Fix wrap-around behavior in `kf_JumpToResourceExtractor()` 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 --- src/keybind.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/keybind.cpp b/src/keybind.cpp index 457b085559a..920ffe7c186 100644 --- a/src/keybind.cpp +++ b/src/keybind.cpp @@ -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); @@ -1643,7 +1650,6 @@ void kf_JumpToResourceExtractor() { addConsoleMessage(_("Unable to locate any oil derricks!"), LEFT_JUSTIFY, SYSTEM_MESSAGE); } - } void keybindInformResourceExtractorRemoved(const STRUCTURE* psResourceExtractor)