Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaner Creature follow logic #4634

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 13 additions & 39 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ class Creature : virtual public Thing
void startAutoWalk(const std::vector<Direction>& listDir);
void addEventWalk(bool firstStep = false);
void stopEventWalk();
virtual void goToFollowCreature();

// walk events
virtual void onWalk(Direction& dir);
Expand All @@ -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; }
Expand Down Expand Up @@ -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);
Expand Down
43 changes: 40 additions & 3 deletions src/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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