From bb26d9af996ec742fdfe65197598c9bd73c92451 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 23 Oct 2023 22:32:18 -0300 Subject: [PATCH 01/19] init --- src/creatures/monsters/monster.cpp | 112 +++++++++++++---------------- src/creatures/monsters/monster.hpp | 71 +++++++++--------- 2 files changed, 90 insertions(+), 93 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 9bef75a5ecb..02d108dd1a9 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -289,77 +289,68 @@ void Monster::onCreatureSay(std::shared_ptr creature, SpeakClasses typ } } -void Monster::addFriend(std::shared_ptr creature) { +void Monster::addFriend(const std::shared_ptr &creature) { assert(creature.get() != this); - friendList.try_emplace(creature->getID(), creature); + + const auto &it = std::find_if(friendList.begin(), friendList.end(), [id = creature->getID()](const std::weak_ptr &ref) { + const auto &creature = ref.lock(); + return creature && creature->getID() == id; + }); + + if (it == friendList.end()) { + friendList.emplace_back(creature); + } } -void Monster::removeFriend(std::shared_ptr creature) { - friendList.erase(creature->getID()); +void Monster::removeFriend(const std::shared_ptr &creature) { + std::erase_if(targetList, [id = creature->getID()](const std::weak_ptr &ref) { + const auto &creature = ref.lock(); + return !creature || creature->getID() == id; + }); } -void Monster::addTarget(std::shared_ptr creature, bool pushFront /* = false*/) { +void Monster::addTarget(const std::shared_ptr &creature, bool pushFront /* = false*/) { assert(creature.get() != this); - auto cid = creature->getID(); - targetListMap.try_emplace(cid, creature); - if (std::find(targetIDList.begin(), targetIDList.end(), cid) == targetIDList.end()) { + + const auto &it = getTargetInterator(creature); + if (it == targetList.end()) { if (pushFront) { - targetIDList.push_front(cid); + targetList.emplace_front(creature); } else { - targetIDList.push_back(cid); + targetList.emplace_back(creature); } + if (!getMaster() && getFaction() != FACTION_DEFAULT && creature->getPlayer()) { totalPlayersOnScreen++; } } } -void Monster::removeTarget(std::shared_ptr creature) { +void Monster::removeTarget(const std::shared_ptr &creature) { if (!creature) { return; } - auto it = std::find(targetIDList.begin(), targetIDList.end(), creature->getID()); - if (it != targetIDList.end()) { + const auto &it = getTargetInterator(creature); + if (it != targetList.end()) { if (!getMaster() && getFaction() != FACTION_DEFAULT && creature->getPlayer()) { totalPlayersOnScreen--; } - targetIDList.erase(it); - targetListMap.erase(creature->getID()); + targetList.erase(it); } } void Monster::updateTargetList() { - auto friendIterator = friendList.begin(); - while (friendIterator != friendList.end()) { - auto creature = (*friendIterator).second.lock(); - if (!creature || creature->getHealth() <= 0 || !canSee(creature->getPosition())) { - friendIterator = friendList.erase(friendIterator); - } else { - ++friendIterator; - } - } - - auto targetIterator = targetIDList.begin(); - while (targetIterator != targetIDList.end()) { - const uint32_t targetId = *targetIterator; + std::erase_if(friendList, [&](const std::weak_ptr &ref) { + const auto &creature = ref.lock(); + return !creature || creature->getHealth() <= 0 || !canSee(creature->getPosition()); + }); - auto itTLM = targetListMap.find(targetId); - const bool existTarget = itTLM != targetListMap.end(); - - if (existTarget) { - const auto &creature = itTLM->second.lock(); - if (!creature || creature->getHealth() <= 0 || !canSee(creature->getPosition())) { - targetIterator = targetIDList.erase(targetIterator); - targetListMap.erase(itTLM); - } else { - ++targetIterator; - } - } else { - targetIterator = targetIDList.erase(targetIterator); - } - } + std::erase_if(targetList, [&](const std::weak_ptr &ref) { + const auto &creature = ref.lock(); + return !creature || creature->getHealth() <= 0 || !canSee(creature->getPosition()); + }); for (const auto &spectator : Spectators().find(position, true)) { if (spectator.get() != this && canSee(spectator->getPosition())) { @@ -369,8 +360,7 @@ void Monster::updateTargetList() { } void Monster::clearTargetList() { - targetIDList.clear(); - targetListMap.clear(); + targetList.clear(); } void Monster::clearFriendList() { @@ -446,7 +436,7 @@ void Monster::onCreatureLeave(std::shared_ptr creature) { // update targetList if (isOpponent(creature)) { removeTarget(creature); - if (targetIDList.empty()) { + if (targetList.empty()) { updateIdleStatus(); } } @@ -473,11 +463,11 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL } } - std::list> resultList; + std::vector> resultList; const Position &myPos = getPosition(); - for (auto cid : targetIDList) { - auto creature = targetListMap[cid].lock(); + for (const auto &cref : targetList) { + const auto &creature = cref.lock(); if (creature && isTarget(creature)) { if ((static_self_cast()->targetDistance == 1) || canUseAttack(myPos, creature)) { resultList.push_back(creature); @@ -600,20 +590,20 @@ void Monster::onFollowCreatureComplete(std::shared_ptr creature) { if (!creature) { return; } - auto it = std::find(targetIDList.begin(), targetIDList.end(), creature->getID()); - if (it != targetIDList.end()) { - auto target = targetListMap[*it].lock(); + + const auto &it = getTargetInterator(creature); + if (it != targetList.end()) { + const auto &target = (*it).lock(); if (!target) { return; } - targetIDList.erase(it); + + targetList.erase(it); if (hasFollowPath) { - targetIDList.push_front(target->getID()); + targetList.emplace_front(target); } else if (!isSummon()) { - targetIDList.push_back(target->getID()); - } else { - targetListMap.erase(target->getID()); + targetList.emplace_back(target); } } } @@ -666,8 +656,8 @@ bool Monster::selectTarget(std::shared_ptr creature) { return false; } - auto it = std::find(targetIDList.begin(), targetIDList.end(), creature->getID()); - if (it == targetIDList.end()) { + const auto &it = getTargetInterator(creature); + if (it == targetList.end()) { // Target not found in our target list. return false; } @@ -702,7 +692,7 @@ void Monster::updateIdleStatus() { auto master = getMaster(); if (conditions.empty()) { - if (!isSummon() && targetIDList.empty()) { + if (!isSummon() && targetList.empty()) { idle = true; } else if (master && (!isSummon() && totalPlayersOnScreen == 0 || isSummon() && master->getMonster() && master->getMonster()->totalPlayersOnScreen == 0) && getFaction() != FACTION_DEFAULT) { idle = true; @@ -795,7 +785,7 @@ void Monster::onThink(uint32_t interval) { // This happens just after a master orders an attack, so lets follow it aswell. setFollowCreature(attackedCreature); } - } else if (!attackedCreature && !targetIDList.empty()) { + } else if (!attackedCreature && !targetList.empty()) { if (!followCreature || !hasFollowPath) { searchTarget(TARGETSEARCH_NEAREST); } else if (isFleeing()) { diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index adcb0258503..76309e8f954 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -18,10 +18,6 @@ class Game; class Spawn; using CreatureHashSet = phmap::flat_hash_set>; -using CreatureList = std::list>; - -using CreatureWeakHashMap = phmap::flat_hash_map>; -using CreatureIDList = std::list; class Monster final : public Creature { public: @@ -190,31 +186,36 @@ class Monster final : public Creature { bool searchTarget(TargetSearchType_t searchType = TARGETSEARCH_DEFAULT); bool selectTarget(std::shared_ptr creature); - CreatureList getTargetList() { - std::list> list; - for (auto it = targetIDList.begin(); it != targetIDList.end();) { - auto cid = *it; - if (auto targetCreature = targetListMap[cid].lock()) { - list.push_back(targetCreature); - ++it; - } else { - it = targetIDList.erase(it); - targetListMap.erase(cid); + auto getTargetList() { + CreatureVector list; + list.reserve(targetList.size()); + + std::erase_if(targetList, [&](const std::weak_ptr &ref) { + if (const auto &creature = ref.lock()) { + list.emplace_back(creature); + return false; } - } + + return true; + }); + return list; } - CreatureHashSet getFriendList() { - CreatureHashSet set; - for (auto it = friendList.begin(); it != friendList.end();) { - if (auto friendCreature = it->second.lock()) { - set.insert(friendCreature); - ++it; - } else { - it = friendList.erase(it); + + auto getFriendList() { + CreatureVector list; + list.reserve(friendList.size()); + + std::erase_if(friendList, [&](const std::weak_ptr &ref) { + if (const auto &creature = ref.lock()) { + list.emplace_back(creature); + return false; } - } - return set; + + return true; + }); + + return list; } bool isTarget(std::shared_ptr creature); @@ -339,9 +340,15 @@ class Monster final : public Creature { } private: - CreatureWeakHashMap friendList; - CreatureIDList targetIDList; - CreatureWeakHashMap targetListMap; + auto getTargetInterator(const std::shared_ptr &creature) { + return std::find_if(targetList.begin(), targetList.end(), [id = creature->getID()](const std::weak_ptr &ref) { + const auto &creature = ref.lock(); + return creature && creature->getID() == id; + }); + } + + std::vector> friendList; + std::deque> targetList; time_t timeToChangeFiendish = 0; @@ -390,10 +397,10 @@ class Monster final : public Creature { void updateLookDirection(); - void addFriend(std::shared_ptr creature); - void removeFriend(std::shared_ptr creature); - void addTarget(std::shared_ptr creature, bool pushFront = false); - void removeTarget(std::shared_ptr creature); + void addFriend(const std::shared_ptr &creature); + void removeFriend(const std::shared_ptr &creature); + void addTarget(const std::shared_ptr &creature, bool pushFront = false); + void removeTarget(const std::shared_ptr &creature); void death(std::shared_ptr lastHitCreature) override; std::shared_ptr getCorpse(std::shared_ptr lastHitCreature, std::shared_ptr mostDamageCreature) override; From c5d3d401bdff88c88ec665cb59a361ef7a4d32d1 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 23 Oct 2023 23:42:19 -0300 Subject: [PATCH 02/19] init --- src/creatures/monsters/monster.cpp | 55 ++++++++++++------------------ src/creatures/monsters/monster.hpp | 24 +++++++++++-- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 02d108dd1a9..2537065abdd 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -292,14 +292,7 @@ void Monster::onCreatureSay(std::shared_ptr creature, SpeakClasses typ void Monster::addFriend(const std::shared_ptr &creature) { assert(creature.get() != this); - const auto &it = std::find_if(friendList.begin(), friendList.end(), [id = creature->getID()](const std::weak_ptr &ref) { - const auto &creature = ref.lock(); - return creature && creature->getID() == id; - }); - - if (it == friendList.end()) { - friendList.emplace_back(creature); - } + friendList.emplace(creature); } void Monster::removeFriend(const std::shared_ptr &creature) { @@ -383,16 +376,13 @@ void Monster::onCreatureEnter(std::shared_ptr creature) { onCreatureFound(creature, true); } -bool Monster::isFriend(std::shared_ptr creature) const { +bool Monster::isFriend(const std::shared_ptr &creature) const { if (isSummon() && getMaster()->getPlayer()) { - std::shared_ptr masterPlayer = getMaster()->getPlayer(); - std::shared_ptr tmpPlayer = nullptr; - - if (creature->getPlayer()) { - tmpPlayer = creature->getPlayer(); - } else { - std::shared_ptr creatureMaster = creature->getMaster(); + const auto &masterPlayer = getMaster()->getPlayer(); + auto tmpPlayer = creature->getPlayer(); + if (!tmpPlayer) { + const auto &creatureMaster = creature->getMaster(); if (creatureMaster && creatureMaster->getPlayer()) { tmpPlayer = creatureMaster->getPlayer(); } @@ -401,27 +391,26 @@ bool Monster::isFriend(std::shared_ptr creature) const { if (tmpPlayer && (tmpPlayer == getMaster() || masterPlayer->isPartner(tmpPlayer))) { return true; } - } else if (creature->getMonster() && !creature->isSummon()) { - return true; } - return false; + return creature->getMonster() && !creature->isSummon(); } -bool Monster::isOpponent(std::shared_ptr creature) const { +bool Monster::isOpponent(const std::shared_ptr &creature) const { if (isSummon() && getMaster()->getPlayer()) { - if (creature != getMaster()) { - return true; - } - } else if (creature->getPlayer() && creature->getPlayer()->hasFlag(PlayerFlags_t::IgnoredByMonsters)) { + return creature != getMaster(); + } + + if (creature->getPlayer() && creature->getPlayer()->hasFlag(PlayerFlags_t::IgnoredByMonsters)) { return false; - } else { - if (getFaction() != FACTION_DEFAULT) { - return isEnemyFaction(creature->getFaction()) || creature->getFaction() == FACTION_PLAYER; - } - if ((creature->getPlayer()) || (creature->getMaster() && creature->getMaster()->getPlayer())) { - return true; - } + } + + if (getFaction() != FACTION_DEFAULT) { + return isEnemyFaction(creature->getFaction()) || creature->getFaction() == FACTION_PLAYER; + } + + if ((creature->getPlayer()) || (creature->getMaster() && creature->getMaster()->getPlayer())) { + return true; } return false; @@ -503,7 +492,7 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL } } else { int32_t minRange = std::numeric_limits::max(); - for (auto creature : getTargetList()) { + for (const auto &creature : getTargetList()) { if (!isTarget(creature)) { continue; } @@ -578,7 +567,7 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL } // lets just pick the first target in the list - for (auto target : getTargetList()) { + for (const auto &target : getTargetList()) { if (selectTarget(target)) { return true; } diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index 76309e8f954..844e7e37b2a 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -12,6 +12,7 @@ #include "creatures/monsters/monsters.hpp" #include "declarations.hpp" #include "items/tile.hpp" +#include class Creature; class Game; @@ -340,6 +341,23 @@ class Monster final : public Creature { } private: + struct MHash { + public: + size_t operator()(const std::weak_ptr &a) const { + uint64_t x = a.lock() ? a.lock()->getID() : 0; + x ^= x >> 33U; + x *= UINT64_C(0xff51afd7ed558ccd); + x ^= x >> 33U; + return static_cast(x); + } + }; + struct MCompare { + bool operator()(const std::weak_ptr &a, const std::weak_ptr &b) const { + const uint32_t idA = a.lock() ? a.lock()->getID() : 0; + const uint32_t idB = b.lock() ? b.lock()->getID() : 0; + return idA == idB; + } + }; auto getTargetInterator(const std::shared_ptr &creature) { return std::find_if(targetList.begin(), targetList.end(), [id = creature->getID()](const std::weak_ptr &ref) { const auto &creature = ref.lock(); @@ -347,7 +365,7 @@ class Monster final : public Creature { }); } - std::vector> friendList; + std::unordered_set, MHash, MCompare> friendList; std::deque> targetList; time_t timeToChangeFiendish = 0; @@ -431,8 +449,8 @@ class Monster final : public Creature { void onThinkDefense(uint32_t interval); void onThinkSound(uint32_t interval); - bool isFriend(std::shared_ptr creature) const; - bool isOpponent(std::shared_ptr creature) const; + bool isFriend(const std::shared_ptr &creature) const; + bool isOpponent(const std::shared_ptr &creature) const; uint64_t getLostExperience() const override { return skillLoss ? mType->info.experience : 0; From 7f7c7ee5341d528751debfdc48b94e13bfed0b76 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Tue, 24 Oct 2023 00:07:01 -0300 Subject: [PATCH 03/19] std::unordered_map --- src/creatures/monsters/monster.cpp | 6 +++--- src/creatures/monsters/monster.hpp | 26 +++----------------------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 2537065abdd..c4f59e7aadf 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -292,7 +292,7 @@ void Monster::onCreatureSay(std::shared_ptr creature, SpeakClasses typ void Monster::addFriend(const std::shared_ptr &creature) { assert(creature.get() != this); - friendList.emplace(creature); + friendList.try_emplace(creature->getID(), creature); } void Monster::removeFriend(const std::shared_ptr &creature) { @@ -335,8 +335,8 @@ void Monster::removeTarget(const std::shared_ptr &creature) { } void Monster::updateTargetList() { - std::erase_if(friendList, [&](const std::weak_ptr &ref) { - const auto &creature = ref.lock(); + std::erase_if(friendList, [&](const auto &it) { + const auto &creature = it.second.lock(); return !creature || creature->getHealth() <= 0 || !canSee(creature->getPosition()); }); diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index 844e7e37b2a..6b78ea8890e 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -12,14 +12,11 @@ #include "creatures/monsters/monsters.hpp" #include "declarations.hpp" #include "items/tile.hpp" -#include class Creature; class Game; class Spawn; -using CreatureHashSet = phmap::flat_hash_set>; - class Monster final : public Creature { public: static std::shared_ptr createMonster(const std::string &name); @@ -207,8 +204,8 @@ class Monster final : public Creature { CreatureVector list; list.reserve(friendList.size()); - std::erase_if(friendList, [&](const std::weak_ptr &ref) { - if (const auto &creature = ref.lock()) { + std::erase_if(friendList, [&](const auto &it) { + if (const auto &creature = it.second.lock()) { list.emplace_back(creature); return false; } @@ -341,23 +338,6 @@ class Monster final : public Creature { } private: - struct MHash { - public: - size_t operator()(const std::weak_ptr &a) const { - uint64_t x = a.lock() ? a.lock()->getID() : 0; - x ^= x >> 33U; - x *= UINT64_C(0xff51afd7ed558ccd); - x ^= x >> 33U; - return static_cast(x); - } - }; - struct MCompare { - bool operator()(const std::weak_ptr &a, const std::weak_ptr &b) const { - const uint32_t idA = a.lock() ? a.lock()->getID() : 0; - const uint32_t idB = b.lock() ? b.lock()->getID() : 0; - return idA == idB; - } - }; auto getTargetInterator(const std::shared_ptr &creature) { return std::find_if(targetList.begin(), targetList.end(), [id = creature->getID()](const std::weak_ptr &ref) { const auto &creature = ref.lock(); @@ -365,7 +345,7 @@ class Monster final : public Creature { }); } - std::unordered_set, MHash, MCompare> friendList; + std::unordered_map> friendList; std::deque> targetList; time_t timeToChangeFiendish = 0; From a85f58903a146112e1968923b9b05ec1e582d623 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Tue, 24 Oct 2023 14:00:35 -0300 Subject: [PATCH 04/19] fix smell --- src/creatures/monsters/monster.cpp | 16 ++++++++-------- src/creatures/monsters/monster.hpp | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index c4f59e7aadf..bf8d730c635 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -297,8 +297,8 @@ void Monster::addFriend(const std::shared_ptr &creature) { void Monster::removeFriend(const std::shared_ptr &creature) { std::erase_if(targetList, [id = creature->getID()](const std::weak_ptr &ref) { - const auto &creature = ref.lock(); - return !creature || creature->getID() == id; + const auto &target = ref.lock(); + return !target || target->getID() == id; }); } @@ -335,14 +335,14 @@ void Monster::removeTarget(const std::shared_ptr &creature) { } void Monster::updateTargetList() { - std::erase_if(friendList, [&](const auto &it) { - const auto &creature = it.second.lock(); - return !creature || creature->getHealth() <= 0 || !canSee(creature->getPosition()); + std::erase_if(friendList, [=](const auto &it) { + const auto &target = it.second.lock(); + return !target || target->getHealth() <= 0 || !canSee(target->getPosition()); }); - std::erase_if(targetList, [&](const std::weak_ptr &ref) { - const auto &creature = ref.lock(); - return !creature || creature->getHealth() <= 0 || !canSee(creature->getPosition()); + std::erase_if(targetList, [=](const std::weak_ptr &ref) { + const auto &target = ref.lock(); + return !target || target->getHealth() <= 0 || !canSee(target->getPosition()); }); for (const auto &spectator : Spectators().find(position, true)) { diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index 6b78ea8890e..f9fa98a189b 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -188,7 +188,7 @@ class Monster final : public Creature { CreatureVector list; list.reserve(targetList.size()); - std::erase_if(targetList, [&](const std::weak_ptr &ref) { + std::erase_if(targetList, [&list](const std::weak_ptr &ref) { if (const auto &creature = ref.lock()) { list.emplace_back(creature); return false; @@ -204,7 +204,7 @@ class Monster final : public Creature { CreatureVector list; list.reserve(friendList.size()); - std::erase_if(friendList, [&](const auto &it) { + std::erase_if(friendList, [&list](const auto &it) { if (const auto &creature = it.second.lock()) { list.emplace_back(creature); return false; @@ -339,9 +339,9 @@ class Monster final : public Creature { private: auto getTargetInterator(const std::shared_ptr &creature) { - return std::find_if(targetList.begin(), targetList.end(), [id = creature->getID()](const std::weak_ptr &ref) { - const auto &creature = ref.lock(); - return creature && creature->getID() == id; + return std::ranges::find_if(targetList.begin(), targetList.end(), [id = creature->getID()](const std::weak_ptr &ref) { + const auto &target = ref.lock(); + return target && target->getID() == id; }); } From c7e3e0214dc035f627530d3e9283e316fdda8366 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Tue, 24 Oct 2023 14:28:17 -0300 Subject: [PATCH 05/19] Update monster.cpp --- src/creatures/monsters/monster.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index bf8d730c635..25be733f254 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -335,12 +335,12 @@ void Monster::removeTarget(const std::shared_ptr &creature) { } void Monster::updateTargetList() { - std::erase_if(friendList, [=](const auto &it) { + std::erase_if(friendList, [this](const auto &it) { const auto &target = it.second.lock(); return !target || target->getHealth() <= 0 || !canSee(target->getPosition()); }); - std::erase_if(targetList, [=](const std::weak_ptr &ref) { + std::erase_if(targetList, [this](const std::weak_ptr &ref) { const auto &target = ref.lock(); return !target || target->getHealth() <= 0 || !canSee(target->getPosition()); }); From 506ab9f9876cca03adbed2ad3c8a5c402fc1fcf7 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Tue, 24 Oct 2023 16:05:02 -0300 Subject: [PATCH 06/19] fix smell --- src/creatures/monsters/monster.cpp | 7 +++++-- src/creatures/monsters/monster.hpp | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 25be733f254..95917278e5e 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -572,7 +572,10 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL return true; } } - return false; + + return std::ranges::any_of(getTargetList(), [this](const std::shared_ptr &creature) { + return selectTarget(creature); + }); } void Monster::onFollowCreatureComplete(std::shared_ptr creature) { @@ -640,7 +643,7 @@ bool Monster::isTarget(std::shared_ptr creature) { return true; } -bool Monster::selectTarget(std::shared_ptr creature) { +bool Monster::selectTarget(const std::shared_ptr &creature) { if (!isTarget(creature)) { return false; } diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index f9fa98a189b..7b6359e1fd0 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -182,7 +182,7 @@ class Monster final : public Creature { } bool searchTarget(TargetSearchType_t searchType = TARGETSEARCH_DEFAULT); - bool selectTarget(std::shared_ptr creature); + bool selectTarget(const std::shared_ptr &creature); auto getTargetList() { CreatureVector list; From bef3bb024d97c7a174c40ca9f170ef9240ded6d5 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Tue, 24 Oct 2023 16:32:21 -0300 Subject: [PATCH 07/19] Update monster.cpp --- src/creatures/monsters/monster.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 95917278e5e..5e0fb7fb7a3 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -567,12 +567,6 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL } // lets just pick the first target in the list - for (const auto &target : getTargetList()) { - if (selectTarget(target)) { - return true; - } - } - return std::ranges::any_of(getTargetList(), [this](const std::shared_ptr &creature) { return selectTarget(creature); }); From 862980b30fb866d76f46ba0453374870c9569945 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Tue, 24 Oct 2023 18:14:41 -0300 Subject: [PATCH 08/19] cleanup --- src/creatures/monsters/monster.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 5e0fb7fb7a3..78d1c8062ee 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -578,12 +578,11 @@ void Monster::onFollowCreatureComplete(std::shared_ptr creature) { } const auto &it = getTargetInterator(creature); - if (it != targetList.end()) { - const auto &target = (*it).lock(); - if (!target) { - return; - } + if (it == targetList.end()) { + return; + } + if (const auto &target = (*it).lock()) { targetList.erase(it); if (hasFollowPath) { From 7a8c4bf250ed3bf955329a9a090292bb16de055e Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Wed, 25 Oct 2023 01:53:05 +0000 Subject: [PATCH 09/19] Lua code format - (Stylua) --- data-otservbr-global/npc/percybald.lua | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/data-otservbr-global/npc/percybald.lua b/data-otservbr-global/npc/percybald.lua index 957504acc49..be04fe59109 100644 --- a/data-otservbr-global/npc/percybald.lua +++ b/data-otservbr-global/npc/percybald.lua @@ -144,17 +144,17 @@ local function creatureSayCallback(npc, creature, type, message) npcHandler:say({ "Great! To clarify, donating 30,000 silver tokens and 25,000 gold tokens will entitle you to a unique outfit. ...", "For 15,000 silver tokens and 12,500 gold tokens, you will receive the {armor}. For an additional 7,500 silver tokens and 6,250 gold tokens each, you can also receive the {shield} and {crown}. ...", - "What will you choose?" + "What will you choose?", }, npc, creature) npcHandler:setTopic(playerId, 13) - + -- Topic 13: User already accepted to learn about donations, further actions here elseif npcHandler:getTopic(playerId) == 13 then npcHandler:say("If you haven't made up your mind, please come back when you are ready.", npc, creature) npcHandler:setTopic(playerId, 0) elseif npcHandler:getTopic(playerId) == 14 then if player:getStorageValue(Storage.OutfitQuest.RoyalCostumeOutfit) < 1 then - if (player:removeItem(22516, 15000) and player:removeItem(22721, 12500)) then + if player:removeItem(22516, 15000) and player:removeItem(22721, 12500) then npcHandler:say("Take this armor as a token of great gratitude. Let us forever remember this day, my friend!", npc, creature) player:addOutfit(1457) player:addOutfit(1456) @@ -170,7 +170,7 @@ local function creatureSayCallback(npc, creature, type, message) elseif npcHandler:getTopic(playerId) == 15 then if player:getStorageValue(Storage.OutfitQuest.RoyalCostumeOutfit) == 1 then if player:getStorageValue(Storage.OutfitQuest.RoyalCostumeOutfit) < 2 then - if (player:removeItem(22516, 7500) and player:removeItem(22721, 6250)) then + if player:removeItem(22516, 7500) and player:removeItem(22721, 6250) then npcHandler:say("Take this sheild as a token of great gratitude. Let us forever remember this day, my friend. ", npc, creature) player:addOutfitAddon(1457, 1) player:addOutfitAddon(1456, 1) @@ -193,7 +193,7 @@ local function creatureSayCallback(npc, creature, type, message) elseif npcHandler:getTopic(playerId) == 16 then if player:getStorageValue(Storage.OutfitQuest.RoyalCostumeOutfit) == 2 then if player:getStorageValue(Storage.OutfitQuest.RoyalCostumeOutfit) < 3 then - if (player:removeItem(22516, 7500) and player:removeItem(22721, 6250)) then + if player:removeItem(22516, 7500) and player:removeItem(22721, 6250) then npcHandler:say("Take this crown as a token of great gratitude. Let us forever remember this day, my friend. ", npc, creature) player:addOutfitAddon(1457, 2) player:addOutfitAddon(1456, 2) @@ -214,16 +214,14 @@ local function creatureSayCallback(npc, creature, type, message) end npcHandler:setTopic(playerId, 13) end - + -- Handle options for armor, shield, and crown elseif (MsgContains(message, "armor")) and npcHandler:getTopic(playerId) == 13 then npcHandler:say("Would you like to donate 15,000 silver tokens and 12,500 gold tokens for a unique red armor?", npc, creature) npcHandler:setTopic(playerId, 14) - elseif (MsgContains(message, "shield")) and npcHandler:getTopic(playerId) == 13 then npcHandler:say("Would you like to donate 7,500 silver tokens and 6,250 gold tokens for a unique shield?", npc, creature) npcHandler:setTopic(playerId, 15) - elseif (MsgContains(message, "crown")) and npcHandler:getTopic(playerId) == 13 then npcHandler:say("Would you like to donate 7,500 silver tokens and 6,250 gold tokens for a unique crown?", npc, creature) npcHandler:setTopic(playerId, 16) From 509053b3e79c0f92ee2df5f5ac14d00a5f51b915 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Thu, 2 Nov 2023 19:48:13 -0300 Subject: [PATCH 10/19] fix compile --- src/creatures/monsters/monster.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 78d1c8062ee..6e341262dd0 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -178,7 +178,7 @@ void Monster::onRemoveCreature(std::shared_ptr creature, bool isLogout } } -void Monster::onCreatureMove(std::shared_ptr creature, std::shared_ptr newTile, const Position &newPos, std::shared_ptr oldTile, const Position &oldPos, bool teleport) { +void Monster::onCreatureMove(const std::shared_ptr &creature, const std::shared_ptr &newTile, const Position &newPos, const std::shared_ptr &oldTile, const Position &oldPos, bool teleport) { Creature::onCreatureMove(creature, newTile, newPos, oldTile, oldPos, teleport); if (mType->info.creatureMoveEvent != -1) { @@ -572,7 +572,7 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL }); } -void Monster::onFollowCreatureComplete(std::shared_ptr creature) { +void Monster::onFollowCreatureComplete(const std::shared_ptr &creature) { if (!creature) { return; } @@ -845,7 +845,7 @@ void Monster::doAttacking(uint32_t interval) { } } -bool Monster::canUseAttack(const Position &pos, std::shared_ptr target) const { +bool Monster::canUseAttack(const Position &pos, const std::shared_ptr &target) const { if (isHostile()) { const Position &targetPos = target->getPosition(); uint32_t distance = std::max(Position::getDistanceX(pos, targetPos), Position::getDistanceY(pos, targetPos)); @@ -2041,7 +2041,7 @@ bool Monster::isImmune(CombatType_t combatType) const { return mType->info.m_damageImmunities[combatTypeToIndex(combatType)]; } -void Monster::getPathSearchParams(std::shared_ptr creature, FindPathParams &fpp) { +void Monster::getPathSearchParams(const std::shared_ptr &creature, FindPathParams &fpp) { Creature::getPathSearchParams(creature, fpp); fpp.minTargetDist = 1; From da5260e1d0813f121b1aa8ae444560e9b05c8216 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Thu, 2 Nov 2023 20:31:07 -0300 Subject: [PATCH 11/19] getTargetIterator --- src/creatures/monsters/monster.cpp | 8 ++++---- src/creatures/monsters/monster.hpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 6e341262dd0..19ca81b076a 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -305,7 +305,7 @@ void Monster::removeFriend(const std::shared_ptr &creature) { void Monster::addTarget(const std::shared_ptr &creature, bool pushFront /* = false*/) { assert(creature.get() != this); - const auto &it = getTargetInterator(creature); + const auto &it = getTargetIterator(creature); if (it == targetList.end()) { if (pushFront) { targetList.emplace_front(creature); @@ -324,7 +324,7 @@ void Monster::removeTarget(const std::shared_ptr &creature) { return; } - const auto &it = getTargetInterator(creature); + const auto &it = getTargetIterator(creature); if (it != targetList.end()) { if (!getMaster() && getFaction() != FACTION_DEFAULT && creature->getPlayer()) { totalPlayersOnScreen--; @@ -577,7 +577,7 @@ void Monster::onFollowCreatureComplete(const std::shared_ptr &creature return; } - const auto &it = getTargetInterator(creature); + const auto &it = getTargetIterator(creature); if (it == targetList.end()) { return; } @@ -641,7 +641,7 @@ bool Monster::selectTarget(const std::shared_ptr &creature) { return false; } - const auto &it = getTargetInterator(creature); + const auto &it = getTargetIterator(creature); if (it == targetList.end()) { // Target not found in our target list. return false; diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index 468aa3f4e19..e66aa98281e 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -338,7 +338,7 @@ class Monster final : public Creature { } private: - auto getTargetInterator(const std::shared_ptr &creature) { + auto getTargetIterator(const std::shared_ptr &creature) { return std::ranges::find_if(targetList.begin(), targetList.end(), [id = creature->getID()](const std::weak_ptr &ref) { const auto &target = ref.lock(); return target && target->getID() == id; From ac1c20abb87f8b3a9a7c57cfd12e489a0423810d Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Sat, 4 Nov 2023 15:47:52 +0000 Subject: [PATCH 12/19] Code format - (Clang-format) --- src/creatures/monsters/monster.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index aca809cc06b..15590faf48c 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -199,7 +199,7 @@ class Monster final : public Creature { return list; } - + auto getFriendList() { CreatureVector list; list.reserve(friendList.size()); From aada0c1e792d484de7d8cd17f407d324eb0cdddb Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 13 Nov 2023 15:51:22 -0300 Subject: [PATCH 13/19] improve --- src/creatures/monsters/monster.cpp | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index feff8ae5961..1da1192e4e8 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -291,13 +291,12 @@ void Monster::onCreatureSay(std::shared_ptr creature, SpeakClasses typ void Monster::addFriend(const std::shared_ptr &creature) { assert(creature.get() != this); - friendList.try_emplace(creature->getID(), creature); } void Monster::removeFriend(const std::shared_ptr &creature) { - std::erase_if(targetList, [id = creature->getID()](const std::weak_ptr &ref) { - const auto &target = ref.lock(); + std::erase_if(friendList, [id = creature->getID()](const auto &it) { + const auto &target = it.second.lock(); return !target || target->getID() == id; }); } @@ -577,28 +576,15 @@ void Monster::onFollowCreatureComplete(const std::shared_ptr &creature return; } - const auto &it = getTargetIterator(creature); - if (it == targetList.end()) { - return; - } - - if (const auto &target = (*it).lock()) { - targetList.erase(it); - - if (hasFollowPath) { - targetList.emplace_front(target); - } else if (!isSummon()) { - targetList.emplace_back(target); - } + removeTarget(creature); + if (hasFollowPath || !isSummon()) { + addTarget(creature, hasFollowPath); } // Change the target if necessary - if (!hasFollowPath && !isSummon() && !targetIDList.empty() && targetIDList.front() != creature->getID()) { - const auto &itMap = targetListMap.find(targetIDList.front()); - if (itMap != targetListMap.end()) { - if (const auto &target = itMap->second.lock()) { - selectTarget(target); - } + if (!hasFollowPath && !isSummon() && !targetList.empty() && targetList.front().lock() != creature) { + if (const auto &target = targetList.front().lock()) { + selectTarget(target); } } } From 0f9465f116173ecfd23ec2e62dd2baca94a52cc5 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 13 Nov 2023 20:13:30 -0300 Subject: [PATCH 14/19] improve --- src/creatures/monsters/monster.cpp | 57 +++++++++++++++++------------- src/creatures/monsters/monster.hpp | 4 +-- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 1da1192e4e8..f8686e1d959 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -301,36 +301,44 @@ void Monster::removeFriend(const std::shared_ptr &creature) { }); } -void Monster::addTarget(const std::shared_ptr &creature, bool pushFront /* = false*/) { +bool Monster::addTarget(const std::shared_ptr &creature, bool pushFront /* = false*/) { assert(creature.get() != this); const auto &it = getTargetIterator(creature); - if (it == targetList.end()) { - if (pushFront) { - targetList.emplace_front(creature); - } else { - targetList.emplace_back(creature); - } + if (it != targetList.end()) { + return false; + } - if (!getMaster() && getFaction() != FACTION_DEFAULT && creature->getPlayer()) { - totalPlayersOnScreen++; - } + if (pushFront) { + targetList.emplace_front(creature); + } else { + targetList.emplace_back(creature); + } + + if (!getMaster() && getFaction() != FACTION_DEFAULT && creature->getPlayer()) { + totalPlayersOnScreen++; } + + return true; } -void Monster::removeTarget(const std::shared_ptr &creature) { +bool Monster::removeTarget(const std::shared_ptr &creature) { if (!creature) { return; } const auto &it = getTargetIterator(creature); - if (it != targetList.end()) { - if (!getMaster() && getFaction() != FACTION_DEFAULT && creature->getPlayer()) { - totalPlayersOnScreen--; - } + if (it == targetList.end()) { + return false; + } - targetList.erase(it); + if (!getMaster() && getFaction() != FACTION_DEFAULT && creature->getPlayer()) { + totalPlayersOnScreen--; } + + targetList.erase(it); + + return true; } void Monster::updateTargetList() { @@ -576,15 +584,16 @@ void Monster::onFollowCreatureComplete(const std::shared_ptr &creature return; } - removeTarget(creature); - if (hasFollowPath || !isSummon()) { - addTarget(creature, hasFollowPath); - } + if (removeTarget(creature)) { + if (hasFollowPath || !isSummon()) { + addTarget(creature, hasFollowPath); + } - // Change the target if necessary - if (!hasFollowPath && !isSummon() && !targetList.empty() && targetList.front().lock() != creature) { - if (const auto &target = targetList.front().lock()) { - selectTarget(target); + // Change the target if necessary + if (!hasFollowPath && !isSummon() && !targetList.empty() && targetList.front().lock() != creature) { + if (const auto &target = targetList.front().lock()) { + selectTarget(target); + } } } } diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index 15590faf48c..2422ae7679b 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -397,8 +397,8 @@ class Monster final : public Creature { void addFriend(const std::shared_ptr &creature); void removeFriend(const std::shared_ptr &creature); - void addTarget(const std::shared_ptr &creature, bool pushFront = false); - void removeTarget(const std::shared_ptr &creature); + bool addTarget(const std::shared_ptr &creature, bool pushFront = false); + bool removeTarget(const std::shared_ptr &creature); void death(std::shared_ptr lastHitCreature) override; std::shared_ptr getCorpse(std::shared_ptr lastHitCreature, std::shared_ptr mostDamageCreature) override; From 76b25e59c01a7b2ab930d21f1761a5f243ee454b Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 13 Nov 2023 20:14:26 -0300 Subject: [PATCH 15/19] Update monster.cpp --- src/creatures/monsters/monster.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index f8686e1d959..06bf722ec15 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -324,7 +324,7 @@ bool Monster::addTarget(const std::shared_ptr &creature, bool pushFron bool Monster::removeTarget(const std::shared_ptr &creature) { if (!creature) { - return; + return false; } const auto &it = getTargetIterator(creature); From c26b73d06cd45bb8dec17437ef2f4dce90fd1ff9 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 13 Nov 2023 21:27:34 -0300 Subject: [PATCH 16/19] improve --- src/creatures/creature.cpp | 4 +- src/creatures/monsters/monster.cpp | 64 ++++++++++++++---------------- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/creatures/creature.cpp b/src/creatures/creature.cpp index 22763139d2d..46804d6b51b 100644 --- a/src/creatures/creature.cpp +++ b/src/creatures/creature.cpp @@ -1022,9 +1022,7 @@ void Creature::goToFollowCreature() { } } - if (followCreature->getPlayer() && followCreature->getPlayer()->isDisconnected()) { - hasFollowPath = false; - } else if (listDir.empty()) { + if (listDir.empty()) { hasFollowPath = getPathTo(followCreature->getPosition(), listDir, fpp); } diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 06bf722ec15..955f29169bc 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -225,13 +225,9 @@ void Monster::onCreatureMove(const std::shared_ptr &creature, const st } updateIdleStatus(); - if (!m_attackedCreature.expired()) { - return; - } if (!isSummon()) { - auto followCreature = getFollowCreature(); - if (followCreature) { + if (const auto &followCreature = getFollowCreature()) { const Position &followPosition = followCreature->getPosition(); const Position &pos = getPosition(); @@ -239,12 +235,11 @@ void Monster::onCreatureMove(const std::shared_ptr &creature, const st int32_t offset_y = Position::getDistanceY(followPosition, pos); if ((offset_x > 1 || offset_y > 1) && mType->info.changeTargetChance > 0) { Direction dir = getDirectionTo(pos, followPosition); - const Position &checkPosition = getNextPosition(dir, pos); + const auto &checkPosition = getNextPosition(dir, pos); - auto nextTile = g_game().map.getTile(checkPosition); - if (nextTile) { - auto topCreature = nextTile->getTopCreature(); - if (topCreature && followCreature != topCreature && isOpponent(topCreature)) { + if (const auto &nextTile = g_game().map.getTile(checkPosition)) { + const auto &topCreature = nextTile->getTopCreature(); + if (followCreature != topCreature && isOpponent(topCreature)) { selectTarget(topCreature); } } @@ -404,6 +399,10 @@ bool Monster::isFriend(const std::shared_ptr &creature) const { } bool Monster::isOpponent(const std::shared_ptr &creature) const { + if (!creature) { + return false; + } + if (isSummon() && getMaster()->getPlayer()) { return creature != getMaster(); } @@ -580,21 +579,8 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL } void Monster::onFollowCreatureComplete(const std::shared_ptr &creature) { - if (!creature) { - return; - } - - if (removeTarget(creature)) { - if (hasFollowPath || !isSummon()) { - addTarget(creature, hasFollowPath); - } - - // Change the target if necessary - if (!hasFollowPath && !isSummon() && !targetList.empty() && targetList.front().lock() != creature) { - if (const auto &target = targetList.front().lock()) { - selectTarget(target); - } - } + if (removeTarget(creature) && (hasFollowPath || !isSummon())) { + addTarget(creature, hasFollowPath); } } @@ -634,10 +620,17 @@ bool Monster::isTarget(std::shared_ptr creature) { if (creature->getPosition().z != getPosition().z) { return false; } - Faction_t targetFaction = creature->getFaction(); - if (getFaction() != FACTION_DEFAULT && !isSummon()) { - return isEnemyFaction(targetFaction); + + if (!isSummon()) { + if (creature->getPlayer() && creature->getPlayer()->isDisconnected()) { + return false; + } + + if (getFaction() != FACTION_DEFAULT) { + return isEnemyFaction(creature->getFaction()); + } } + return true; } @@ -758,8 +751,8 @@ void Monster::onThink(uint32_t interval) { if (!isIdle) { addEventWalk(); - auto attackedCreature = getAttackedCreature(); - auto followCreature = getFollowCreature(); + const auto &attackedCreature = getAttackedCreature(); + const auto &followCreature = getFollowCreature(); if (isSummon()) { if (!attackedCreature) { if (getMaster() && getMaster()->getAttackedCreature()) { @@ -775,11 +768,12 @@ void Monster::onThink(uint32_t interval) { // This happens just after a master orders an attack, so lets follow it aswell. setFollowCreature(attackedCreature); } - } else if (!attackedCreature && !targetList.empty()) { - if (!followCreature || !hasFollowPath) { - searchTarget(TARGETSEARCH_NEAREST); - } else if (isFleeing()) { - if (attackedCreature && !canUseAttack(getPosition(), attackedCreature)) { + } else { + const bool attackedCreatureIsDisconnected = attackedCreature && attackedCreature->getPlayer() && attackedCreature->getPlayer()->isDisconnected(); + if ((!attackedCreature || attackedCreatureIsDisconnected) && !targetList.empty()) { + if (!followCreature || !hasFollowPath || attackedCreatureIsDisconnected) { + searchTarget(TARGETSEARCH_NEAREST); + } else if (isFleeing() && attackedCreature && !canUseAttack(getPosition(), attackedCreature)) { searchTarget(TARGETSEARCH_DEFAULT); } } From a20903c1b31ba683172baf59082b911a05470f8b Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Mon, 13 Nov 2023 21:52:54 -0300 Subject: [PATCH 17/19] fix smell --- src/creatures/monsters/monster.cpp | 74 +++++++++++++++--------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 955f29169bc..17be80487c6 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -672,13 +672,13 @@ void Monster::setIdle(bool idle) { void Monster::updateIdleStatus() { bool idle = false; - auto master = getMaster(); - if (conditions.empty()) { if (!isSummon() && targetList.empty()) { idle = true; - } else if (master && (!isSummon() && totalPlayersOnScreen == 0 || isSummon() && master->getMonster() && master->getMonster()->totalPlayersOnScreen == 0) && getFaction() != FACTION_DEFAULT) { - idle = true; + } else if (const auto &master = getMaster()) { + if ((!isSummon() && totalPlayersOnScreen == 0 || isSummon() && master->getMonster() && master->getMonster()->totalPlayersOnScreen == 0) && getFaction() != FACTION_DEFAULT) { + idle = true; + } } } @@ -748,42 +748,44 @@ void Monster::onThink(uint32_t interval) { } else { updateIdleStatus(); - if (!isIdle) { - addEventWalk(); - - const auto &attackedCreature = getAttackedCreature(); - const auto &followCreature = getFollowCreature(); - if (isSummon()) { - if (!attackedCreature) { - if (getMaster() && getMaster()->getAttackedCreature()) { - // This happens if the monster is summoned during combat - selectTarget(getMaster()->getAttackedCreature()); - } else if (getMaster() != followCreature) { - // Our master has not ordered us to attack anything, lets follow him around instead. - setFollowCreature(getMaster()); - } - } else if (attackedCreature.get() == this) { - setFollowCreature(nullptr); - } else if (followCreature != attackedCreature) { - // This happens just after a master orders an attack, so lets follow it aswell. - setFollowCreature(attackedCreature); + if (isIdle) { + return; + } + + addEventWalk(); + + const auto &attackedCreature = getAttackedCreature(); + const auto &followCreature = getFollowCreature(); + if (isSummon()) { + if (!attackedCreature) { + if (getMaster() && getMaster()->getAttackedCreature()) { + // This happens if the monster is summoned during combat + selectTarget(getMaster()->getAttackedCreature()); + } else if (getMaster() != followCreature) { + // Our master has not ordered us to attack anything, lets follow him around instead. + setFollowCreature(getMaster()); } - } else { - const bool attackedCreatureIsDisconnected = attackedCreature && attackedCreature->getPlayer() && attackedCreature->getPlayer()->isDisconnected(); - if ((!attackedCreature || attackedCreatureIsDisconnected) && !targetList.empty()) { - if (!followCreature || !hasFollowPath || attackedCreatureIsDisconnected) { - searchTarget(TARGETSEARCH_NEAREST); - } else if (isFleeing() && attackedCreature && !canUseAttack(getPosition(), attackedCreature)) { - searchTarget(TARGETSEARCH_DEFAULT); - } + } else if (attackedCreature.get() == this) { + setFollowCreature(nullptr); + } else if (followCreature != attackedCreature) { + // This happens just after a master orders an attack, so lets follow it aswell. + setFollowCreature(attackedCreature); + } + } else { + const bool attackedCreatureIsDisconnected = attackedCreature && attackedCreature->getPlayer() && attackedCreature->getPlayer()->isDisconnected(); + if ((!attackedCreature || attackedCreatureIsDisconnected) && !targetList.empty()) { + if (!followCreature || !hasFollowPath || attackedCreatureIsDisconnected) { + searchTarget(TARGETSEARCH_NEAREST); + } else if (isFleeing() && attackedCreature && !canUseAttack(getPosition(), attackedCreature)) { + searchTarget(TARGETSEARCH_DEFAULT); } } - - onThinkTarget(interval); - onThinkYell(interval); - onThinkDefense(interval); - onThinkSound(interval); } + + onThinkTarget(interval); + onThinkYell(interval); + onThinkDefense(interval); + onThinkSound(interval); } } From 20ac862a65f0bd21739fcd7f37561a98840d8147 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Wed, 15 Nov 2023 14:37:02 -0300 Subject: [PATCH 18/19] rework Monster::onThink condition to try fix smells --- src/creatures/monsters/monster.cpp | 75 +++++++++++++++--------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 17be80487c6..41bff900ecf 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -745,48 +745,47 @@ void Monster::onThink(uint32_t interval) { if (!isInSpawnRange(position)) { g_game().internalTeleport(static_self_cast(), masterPos); setIdle(true); - } else { - updateIdleStatus(); + return; + } - if (isIdle) { - return; - } + updateIdleStatus(); - addEventWalk(); + if (isIdle) { + return; + } - const auto &attackedCreature = getAttackedCreature(); - const auto &followCreature = getFollowCreature(); - if (isSummon()) { - if (!attackedCreature) { - if (getMaster() && getMaster()->getAttackedCreature()) { - // This happens if the monster is summoned during combat - selectTarget(getMaster()->getAttackedCreature()); - } else if (getMaster() != followCreature) { - // Our master has not ordered us to attack anything, lets follow him around instead. - setFollowCreature(getMaster()); - } - } else if (attackedCreature.get() == this) { - setFollowCreature(nullptr); - } else if (followCreature != attackedCreature) { - // This happens just after a master orders an attack, so lets follow it aswell. - setFollowCreature(attackedCreature); - } - } else { - const bool attackedCreatureIsDisconnected = attackedCreature && attackedCreature->getPlayer() && attackedCreature->getPlayer()->isDisconnected(); - if ((!attackedCreature || attackedCreatureIsDisconnected) && !targetList.empty()) { - if (!followCreature || !hasFollowPath || attackedCreatureIsDisconnected) { - searchTarget(TARGETSEARCH_NEAREST); - } else if (isFleeing() && attackedCreature && !canUseAttack(getPosition(), attackedCreature)) { - searchTarget(TARGETSEARCH_DEFAULT); - } - } - } + addEventWalk(); - onThinkTarget(interval); - onThinkYell(interval); - onThinkDefense(interval); - onThinkSound(interval); - } + const auto &attackedCreature = getAttackedCreature(); + const auto &followCreature = getFollowCreature(); + if (isSummon()) { + if (attackedCreature.get() == this) { + setFollowCreature(nullptr); + } else if (attackedCreature && followCreature != attackedCreature) { + // This happens just after a master orders an attack, so lets follow it aswell. + setFollowCreature(attackedCreature); + } else if (getMaster() && getMaster()->getAttackedCreature()) { + // This happens if the monster is summoned during combat + selectTarget(getMaster()->getAttackedCreature()); + } else if (getMaster() != followCreature) { + // Our master has not ordered us to attack anything, lets follow him around instead. + setFollowCreature(getMaster()); + } + } else if(!targetList.empty()) { + const bool attackedCreatureIsDisconnected = attackedCreature && attackedCreature->getPlayer() && attackedCreature->getPlayer()->isDisconnected(); + if (!attackedCreature || attackedCreatureIsDisconnected) { + if (!followCreature || !hasFollowPath || attackedCreatureIsDisconnected) { + searchTarget(TARGETSEARCH_NEAREST); + } else if (attackedCreature && isFleeing() && !canUseAttack(getPosition(), attackedCreature)) { + searchTarget(TARGETSEARCH_DEFAULT); + } + } + } + + onThinkTarget(interval); + onThinkYell(interval); + onThinkDefense(interval); + onThinkSound(interval); } void Monster::doAttacking(uint32_t interval) { From a46ecc263394aabdb3c93c4b9238efdfe6953915 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Wed, 15 Nov 2023 17:37:42 +0000 Subject: [PATCH 19/19] Code format - (Clang-format) --- src/creatures/monsters/monster.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 41bff900ecf..b2660933a27 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -746,7 +746,7 @@ void Monster::onThink(uint32_t interval) { g_game().internalTeleport(static_self_cast(), masterPos); setIdle(true); return; - } + } updateIdleStatus(); @@ -760,7 +760,7 @@ void Monster::onThink(uint32_t interval) { const auto &followCreature = getFollowCreature(); if (isSummon()) { if (attackedCreature.get() == this) { - setFollowCreature(nullptr); + setFollowCreature(nullptr); } else if (attackedCreature && followCreature != attackedCreature) { // This happens just after a master orders an attack, so lets follow it aswell. setFollowCreature(attackedCreature); @@ -771,14 +771,14 @@ void Monster::onThink(uint32_t interval) { // Our master has not ordered us to attack anything, lets follow him around instead. setFollowCreature(getMaster()); } - } else if(!targetList.empty()) { + } else if (!targetList.empty()) { const bool attackedCreatureIsDisconnected = attackedCreature && attackedCreature->getPlayer() && attackedCreature->getPlayer()->isDisconnected(); if (!attackedCreature || attackedCreatureIsDisconnected) { if (!followCreature || !hasFollowPath || attackedCreatureIsDisconnected) { searchTarget(TARGETSEARCH_NEAREST); } else if (attackedCreature && isFleeing() && !canUseAttack(getPosition(), attackedCreature)) { - searchTarget(TARGETSEARCH_DEFAULT); - } + searchTarget(TARGETSEARCH_DEFAULT); + } } }