diff --git a/src/creatures/creature.cpp b/src/creatures/creature.cpp index 57b38aa4390..381677ca611 100644 --- a/src/creatures/creature.cpp +++ b/src/creatures/creature.cpp @@ -1014,14 +1014,16 @@ void Creature::goToFollowCreature() { } } - if (listDir.empty()) { - hasFollowPath = getPathTo(getFollowCreature()->getPosition(), listDir, fpp); + if (followCreature->getPlayer() && followCreature->getPlayer()->isDisconnected()) { + hasFollowPath = false; + } else if (listDir.empty()) { + hasFollowPath = getPathTo(followCreature->getPosition(), listDir, fpp); } startAutoWalk(listDir.data()); if (executeOnFollow) { - onFollowCreatureComplete(getFollowCreature()); + onFollowCreatureComplete(followCreature); } } diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 97c18e6d855..d29845864bd 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -601,20 +601,28 @@ void Monster::onFollowCreatureComplete(const std::shared_ptr &creature return; } - auto it = std::find(targetIDList.begin(), targetIDList.end(), creature->getID()); - if (it == targetIDList.end()) { - return; - } + const auto &it = std::find(targetIDList.begin(), targetIDList.end(), creature->getID()); + if (it != targetIDList.end()) { + if (const auto &target = targetListMap[*it].lock()) { + targetIDList.erase(it); - if (const auto &target = targetListMap[*it].lock()) { - targetIDList.erase(it); + if (hasFollowPath) { + targetIDList.push_front(target->getID()); + } else if (!isSummon()) { + targetIDList.push_back(target->getID()); + } else { + targetListMap.erase(target->getID()); + } + } + } - if (hasFollowPath) { - targetIDList.push_front(target->getID()); - } else if (!isSummon()) { - targetIDList.push_back(target->getID()); - } else { - targetListMap.erase(target->getID()); + // 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); + } } } } diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index d26952bc54c..72002db7b4d 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -2548,14 +2548,6 @@ BlockType_t Player::blockHit(std::shared_ptr attacker, CombatType_t co return blockType; } -uint32_t Player::getIP() const { - if (client) { - return client->getIP(); - } - - return 0; -} - void Player::death(std::shared_ptr lastHitCreature) { loginPosition = town->getTemplePosition(); diff --git a/src/creatures/players/player.hpp b/src/creatures/players/player.hpp index 418f8659e49..adf4b35c0ca 100644 --- a/src/creatures/players/player.hpp +++ b/src/creatures/players/player.hpp @@ -471,7 +471,14 @@ class Player final : public Creature, public Cylinder, public Bankable { client->disconnect(); } } - uint32_t getIP() const; + + uint32_t getIP() const { + return client ? client->getIP() : 0; + } + + bool isDisconnected() const { + return getIP() == 0; + } void addContainer(uint8_t cid, std::shared_ptr container); void closeContainer(uint8_t cid);