Skip to content

Commit

Permalink
refactor: direction check with std::ranges::find_if (opentibiabr#2992)
Browse files Browse the repository at this point in the history
Co-authored-by: Beats <daniel15042014@hotmail.com>
  • Loading branch information
dudantas and beats-dh authored Oct 28, 2024
1 parent ac5216a commit 6e32660
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/creatures/combat/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1873,12 +1873,15 @@ bool ConditionFeared::getRandomDirection(std::shared_ptr<Creature> 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<uint8_t>(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<uint8_t>(*it);
return true;
}

return false;
Expand Down

0 comments on commit 6e32660

Please sign in to comment.