diff --git a/src/creature.cpp b/src/creature.cpp index ae27b9ab68..565e009377 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -939,9 +939,9 @@ bool Creature::setAttackedCreature(Creature* creature) return true; } -void Creature::getPathSearchParams(const Creature*, FindPathParams& fpp) const +void Creature::buildFindPathParams(const Creature*, FindPathParams& fpp, bool fullPathSearch) const { - fpp.fullPathSearch = !hasFollowPath; + fpp.fullPathSearch = fullPathSearch; fpp.clearSight = true; fpp.maxSearchDist = 12; fpp.minTargetDist = 1; @@ -952,47 +952,21 @@ void Creature::goToFollowCreature() { if (followCreature) { FindPathParams fpp; - getPathSearchParams(followCreature, fpp); - - Monster* monster = getMonster(); - if (monster && !monster->getMaster() && (monster->isFleeing() || fpp.maxTargetDist > 1)) { - Direction dir = DIRECTION_NONE; - - if (monster->isFleeing()) { - monster->getDistanceStep(followCreature->getPosition(), dir, true); - } else { // maxTargetDist > 1 - if (!monster->getDistanceStep(followCreature->getPosition(), dir)) { - // if we can't get anything then let the A* calculate - listWalkDir.clear(); - if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) { - hasFollowPath = true; - startAutoWalk(); - } else { - hasFollowPath = false; - } - return; - } - } + buildFindPathParams(followCreature, fpp, !hasFollowPath); - if (dir != DIRECTION_NONE) { - listWalkDir.clear(); - listWalkDir.push_back(dir); - - hasFollowPath = true; - startAutoWalk(); - } - } else { - listWalkDir.clear(); - if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) { - hasFollowPath = true; - startAutoWalk(); - } else { - hasFollowPath = false; - } + if (updateFollowPath(fpp)) { + startAutoWalk(); } } - onFollowCreatureComplete(followCreature); + onGoToFollowCreatureComplete(followCreature); +} + +bool Creature::updateFollowPath(FindPathParams& fpp) +{ + listWalkDir.clear(); + + return hasFollowPath = getPathTo(followCreature->getPosition(), listWalkDir, fpp); } bool Creature::setFollowCreature(Creature* creature) diff --git a/src/creature.h b/src/creature.h index 170a4b5abe..abce3b454f 100644 --- a/src/creature.h +++ b/src/creature.h @@ -174,7 +174,6 @@ class Creature : virtual public Thing void startAutoWalk(const std::vector& listDir); void addEventWalk(bool firstStep = false); void stopEventWalk(); - virtual void goToFollowCreature(); // walk events virtual void onWalk(Direction& dir); @@ -184,10 +183,12 @@ class Creature : virtual public Thing // follow functions Creature* getFollowCreature() const { return followCreature; } virtual bool setFollowCreature(Creature* creature); + virtual void goToFollowCreature(); + bool updateFollowPath(FindPathParams& findPathParams); // follow events virtual void onFollowCreature(const Creature*) {} - virtual void onFollowCreatureComplete(const Creature*) {} + virtual void onGoToFollowCreatureComplete(const Creature*) {} // combat functions Creature* getAttackedCreature() { return attackedCreature; } @@ -436,7 +437,7 @@ class Creature : virtual public Thing virtual uint64_t getLostExperience() const { return 0; } virtual void dropLoot(Container*, Creature*) {} virtual uint16_t getLookCorpse() const { return 0; } - virtual void getPathSearchParams(const Creature* creature, FindPathParams& fpp) const; + virtual void buildFindPathParams(const Creature* creature, FindPathParams& fpp, bool fullPathSearch) const; virtual void death(Creature*) {} virtual bool dropCorpse(Creature* lastHitCreature, Creature* mostDamageCreature, bool lastHitUnjustified, bool mostDamageUnjustified); diff --git a/src/monster.cpp b/src/monster.cpp index 5f838345cb..503776617a 100644 --- a/src/monster.cpp +++ b/src/monster.cpp @@ -581,7 +581,44 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL return false; } -void Monster::onFollowCreatureComplete(const Creature* creature) +void Monster::goToFollowCreature() +{ + if (followCreature) { + FindPathParams findPathParams; + buildFindPathParams(followCreature, findPathParams, !hasFollowPath); + + if (!getMaster() && (isFleeing() || findPathParams.maxTargetDist > 1)) { + Direction dir = DIRECTION_NONE; + + if (isFleeing()) { + getDistanceStep(followCreature->getPosition(), dir, true); + } else { // maxTargetDist > 1 + if (!getDistanceStep(followCreature->getPosition(), dir)) { + // if we can't get anything then let the A* calculate + if (updateFollowPath(findPathParams)) { + startAutoWalk(); + } + return; + } + } + + if (dir != DIRECTION_NONE) { + listWalkDir.clear(); + listWalkDir.push_back(dir); + + hasFollowPath = true; + startAutoWalk(); + } + } else { + Creature::goToFollowCreature(); + return; + } + } + + onGoToFollowCreatureComplete(followCreature); +} + +void Monster::onGoToFollowCreatureComplete(const Creature* creature) { if (creature) { auto it = std::find(targetList.begin(), targetList.end(), creature); @@ -1995,9 +2032,9 @@ bool Monster::challengeCreature(Creature* creature, bool force /* = false*/) return result; } -void Monster::getPathSearchParams(const Creature* creature, FindPathParams& fpp) const +void Monster::buildFindPathParams(const Creature* creature, FindPathParams& fpp, bool fullPathSearch) const { - Creature::getPathSearchParams(creature, fpp); + Creature::buildFindPathParams(creature, fpp, fullPathSearch); fpp.minTargetDist = 1; fpp.maxTargetDist = mType->info.targetDistance; diff --git a/src/monster.h b/src/monster.h index 1ac5a46893..46728f4002 100644 --- a/src/monster.h +++ b/src/monster.h @@ -97,7 +97,9 @@ class Monster final : public Creature void onWalk() override; void onWalkComplete() override; bool getNextStep(Direction& direction, uint32_t& flags) override; - void onFollowCreatureComplete(const Creature* creature) override; + + void goToFollowCreature() override; + void onGoToFollowCreatureComplete(const Creature* creature) override; void onThink(uint32_t interval) override; @@ -211,7 +213,7 @@ class Monster final : public Creature void dropLoot(Container* corpse, Creature* lastHitCreature) override; uint32_t getDamageImmunities() const override { return mType->info.damageImmunities; } uint32_t getConditionImmunities() const override { return mType->info.conditionImmunities; } - void getPathSearchParams(const Creature* creature, FindPathParams& fpp) const override; + void buildFindPathParams(const Creature*, FindPathParams& fpp, bool fullPathSearch) const override; bool useCacheMap() const override { return !randomStepping; } friend class LuaScriptInterface; diff --git a/src/player.cpp b/src/player.cpp index 3c057705df..d87c00a16c 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -3351,9 +3351,9 @@ void Player::goToFollowCreature() } } -void Player::getPathSearchParams(const Creature* creature, FindPathParams& fpp) const +void Player::buildFindPathParams(const Creature* creature, FindPathParams& fpp, bool fullPathSearch) const { - Creature::getPathSearchParams(creature, fpp); + Creature::buildFindPathParams(creature, fpp, fullPathSearch); fpp.fullPathSearch = true; } diff --git a/src/player.h b/src/player.h index e7d0e5e9ab..94b784505b 100644 --- a/src/player.h +++ b/src/player.h @@ -1302,7 +1302,7 @@ class Player final : public Creature, public Cylinder uint32_t getConditionImmunities() const override { return conditionImmunities; } uint32_t getConditionSuppressions() const override { return conditionSuppressions; } uint16_t getLookCorpse() const override; - void getPathSearchParams(const Creature* creature, FindPathParams& fpp) const override; + void buildFindPathParams(const Creature* creature, FindPathParams& fpp, bool fullPathSearch) const override; friend class Game; friend class Npc;