Skip to content

Commit

Permalink
Refactoring goToFollowCreature into Npc, Player and Monster (#4811)
Browse files Browse the repository at this point in the history
* Make the logic of goToFollowCreature more readable: move to inher class

* Rename updateWalkPathToFollowCreature

* Fix summon check
  • Loading branch information
ramon-bernardo authored Nov 7, 2024
1 parent b548076 commit 88179e4
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 71 deletions.
59 changes: 12 additions & 47 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ class Creature : virtual public Thing
void startAutoWalk(const std::vector<Direction>& 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);
Expand All @@ -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; }
Expand Down
60 changes: 47 additions & 13 deletions src/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class Npc final : public Creature

void loadNpcTypeInfo();

void goToFollowCreature() override;

std::unique_ptr<NpcEventsHandler> npcEventHandler;
bool fromLua = false;
NpcType* npcType;
Expand Down
20 changes: 12 additions & 8 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 88179e4

Please sign in to comment.