From 88179e469029faf06df4a63bd12816b8eaa63832 Mon Sep 17 00:00:00 2001 From: Ramon Bernardo Date: Thu, 7 Nov 2024 20:56:40 -0300 Subject: [PATCH] Refactoring goToFollowCreature into Npc, Player and Monster (#4811) * Make the logic of goToFollowCreature more readable: move to inher class * Rename updateWalkPathToFollowCreature * Fix summon check --- src/creature.cpp | 59 ++++++++++------------------------------------- src/creature.h | 4 ++-- src/monster.cpp | 60 +++++++++++++++++++++++++++++++++++++----------- src/monster.h | 3 ++- src/npc.cpp | 11 +++++++++ src/npc.h | 2 ++ src/player.cpp | 20 +++++++++------- 7 files changed, 88 insertions(+), 71 deletions(-) diff --git a/src/creature.cpp b/src/creature.cpp index 07e038955a..d260a7ab9d 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -349,6 +349,18 @@ void Creature::onCreatureDisappear(const Creature* creature, bool isLogout) } } +void Creature::updateFollowCreaturePath(FindPathParams& fpp) +{ + listWalkDir.clear(); + + if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) { + hasFollowPath = true; + startAutoWalk(); + } else { + hasFollowPath = false; + } +} + void Creature::onChangeZone(ZoneType_t zone) { if (attackedCreature && zone == ZONE_PROTECTION) { @@ -741,53 +753,6 @@ void Creature::getPathSearchParams(const Creature*, FindPathParams& fpp) const fpp.maxTargetDist = 1; } -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; - } - } - - 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; - } - } - } - - onFollowCreatureComplete(followCreature); -} - bool Creature::setFollowCreature(Creature* creature) { if (creature) { diff --git a/src/creature.h b/src/creature.h index 8ce9ecace0..305087587d 100644 --- a/src/creature.h +++ b/src/creature.h @@ -180,7 +180,8 @@ class Creature : virtual public Thing void startAutoWalk(const std::vector& listDir); void addEventWalk(bool firstStep = false); void stopEventWalk(); - virtual void goToFollowCreature(); + virtual void goToFollowCreature() = 0; + void updateFollowCreaturePath(FindPathParams& fpp); // walk events virtual void onWalk(Direction& dir); @@ -193,7 +194,6 @@ class Creature : virtual public Thing // follow events virtual void onFollowCreature(const Creature*) {} - virtual void onFollowCreatureComplete(const Creature*) {} // combat functions Creature* getAttackedCreature() { return attackedCreature; } diff --git a/src/monster.cpp b/src/monster.cpp index df985dfa38..ebe613d704 100644 --- a/src/monster.cpp +++ b/src/monster.cpp @@ -576,22 +576,56 @@ bool Monster::searchTarget(TargetSearchType_t searchType /*= TARGETSEARCH_DEFAUL return false; } -void Monster::onFollowCreatureComplete(const Creature* creature) +void Monster::goToFollowCreature() { - if (creature) { - auto it = std::find(targetList.begin(), targetList.end(), creature); - if (it != targetList.end()) { - Creature* target = (*it); - targetList.erase(it); - - if (hasFollowPath) { - targetList.push_front(target); - } else if (!isSummon()) { - targetList.push_back(target); - } else { - target->decrementReferenceCounter(); + if (!followCreature) { + return; + } + + FindPathParams fpp; + getPathSearchParams(followCreature, fpp); + + if (!isSummon()) { + Direction dir = DIRECTION_NONE; + + if (isFleeing()) { + getDistanceStep(followCreature->getPosition(), dir, true); + } else if (fpp.maxTargetDist > 1) { + if (!getDistanceStep(followCreature->getPosition(), dir)) { + // if we can't get anything then let the A* calculate + updateFollowCreaturePath(fpp); + return; } } + + if (dir != DIRECTION_NONE) { + listWalkDir.clear(); + listWalkDir.push_back(dir); + + hasFollowPath = true; + startAutoWalk(); + } + } else { + updateFollowCreaturePath(fpp); + } + + onFollowCreatureComplete(); +} + +void Monster::onFollowCreatureComplete() +{ + auto it = std::find(targetList.begin(), targetList.end(), followCreature); + if (it != targetList.end()) { + Creature* target = (*it); + targetList.erase(it); + + if (hasFollowPath) { + targetList.push_front(target); + } else if (!isSummon()) { + targetList.push_back(target); + } else { + target->decrementReferenceCounter(); + } } } diff --git a/src/monster.h b/src/monster.h index b0be069d42..93800b4f7e 100644 --- a/src/monster.h +++ b/src/monster.h @@ -98,7 +98,8 @@ 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 onFollowCreatureComplete(); void onThink(uint32_t interval) override; diff --git a/src/npc.cpp b/src/npc.cpp index 5744377d88..0fb0f4a2e7 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -410,6 +410,17 @@ void Npc::loadNpcTypeInfo() sightY = npcType->sightY; } +void Npc::goToFollowCreature() +{ + if (!followCreature) { + return; + } + + FindPathParams fpp; + getPathSearchParams(followCreature, fpp); + updateFollowCreaturePath(fpp); +} + void Npc::onCreatureAppear(Creature* creature, bool isLogin) { Creature::onCreatureAppear(creature, isLogin); diff --git a/src/npc.h b/src/npc.h index 20e42734ca..2d493f50e6 100644 --- a/src/npc.h +++ b/src/npc.h @@ -222,6 +222,8 @@ class Npc final : public Creature void loadNpcTypeInfo(); + void goToFollowCreature() override; + std::unique_ptr npcEventHandler; bool fromLua = false; NpcType* npcType; diff --git a/src/player.cpp b/src/player.cpp index 380bd802c3..852a216ff0 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -3356,16 +3356,20 @@ bool Player::setAttackedCreature(Creature* creature) void Player::goToFollowCreature() { - if (!walkTask) { - if ((OTSYS_TIME() - lastFailedFollow) < 2000) { - return; - } + if (walkTask || !followCreature) { + return; + } - Creature::goToFollowCreature(); + if ((OTSYS_TIME() - lastFailedFollow) < 2000) { + return; + } - if (followCreature && !hasFollowPath) { - lastFailedFollow = OTSYS_TIME(); - } + FindPathParams fpp; + getPathSearchParams(followCreature, fpp); + updateFollowCreaturePath(fpp); + + if (!hasFollowPath) { + lastFailedFollow = OTSYS_TIME(); } }