From 6e32660ea4d3c95efa177fc1cdc7a097f7efb3c1 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Mon, 28 Oct 2024 14:13:53 -0300 Subject: [PATCH] refactor: direction check with std::ranges::find_if (#2992) Co-authored-by: Beats --- src/creatures/combat/condition.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/creatures/combat/condition.cpp b/src/creatures/combat/condition.cpp index 5cf1905c2e8..bc262a41c76 100644 --- a/src/creatures/combat/condition.cpp +++ b/src/creatures/combat/condition.cpp @@ -1873,12 +1873,15 @@ bool ConditionFeared::getRandomDirection(std::shared_ptr creature, Pos DIRECTION_NORTHWEST }; - std::ranges::shuffle(directions.begin(), directions.end(), getRandomGenerator()); - for (Direction dir : directions) { - if (canWalkTo(creature, pos, dir)) { - this->fleeIndx = static_cast(dir); - return true; - } + std::ranges::shuffle(directions, getRandomGenerator()); + + auto it = std::ranges::find_if(directions, [&](Direction dir) { + return canWalkTo(creature, pos, dir); + }); + + if (it != directions.end()) { + this->fleeIndx = static_cast(*it); + return true; } return false;