Skip to content

Commit

Permalink
walk fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Oct 23, 2023
1 parent 07e7b43 commit f36ceab
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 42 deletions.
88 changes: 53 additions & 35 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,23 @@ void Creature::onThink(uint32_t interval) {
}
}

auto onThink = [self = getCreature(), interval] {
// scripting event - onThink
const auto &thinkEvents = self->getCreatureEvents(CREATURE_EVENT_THINK);
for (const auto creatureEventPtr : thinkEvents) {
creatureEventPtr->executeOnThink(self->static_self_cast<Creature>(), interval);
}
};

if (isUpdatingPath) {
isUpdatingPath = false;
goToFollowCreature();
goToFollowCreature(onThink);
}

// scripting event - onThink
const CreatureEventList &thinkEvents = getCreatureEvents(CREATURE_EVENT_THINK);
for (const auto creatureEventPtr : thinkEvents) {
creatureEventPtr->executeOnThink(static_self_cast<Creature>(), interval);
else {
onThink();
}


}

void Creature::onAttacking(uint32_t interval) {
Expand Down Expand Up @@ -225,17 +232,19 @@ bool Creature::getNextStep(Direction &dir, uint32_t &) {
}

void Creature::startAutoWalk(const std::vector<Direction> &listDir, bool ignoreConditions /* = false*/) {
listWalkDir.clear();

if (!ignoreConditions && (hasCondition(CONDITION_ROOTED) || hasCondition(CONDITION_FEARED))) {
return;
}

listWalkDir = { listDir.begin(), listDir.end() };

size_t size = 0;
for (auto it = listDir.begin(); it != listDir.end() && size <= 1; ++it) {
++size;
if (listWalkDir.empty()) {
return;
}
addEventWalk(size == 1);

addEventWalk(listWalkDir.size() == 1);
}

void Creature::addEventWalk(bool firstStep) {
Expand Down Expand Up @@ -591,6 +600,7 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &creature, const s

const auto &followCreature = getFollowCreature();
if (followCreature && (creature == getCreature() || creature == followCreature)) {
g_logger().info(hasFollowPath() ? "has follow" : "without folow");
if (hasFollowPath()) {
isUpdatingPath = true;
g_dispatcher().addEvent(std::bind(&Game::updateCreatureWalk, &g_game(), getID()), "Game::updateCreatureWalk");
Expand Down Expand Up @@ -950,6 +960,30 @@ bool Creature::setAttackedCreature(std::shared_ptr<Creature> creature) {
return true;
}

void Creature::executeAsyncPathTo(bool executeOnFollow, FindPathParams &fpp, std::function<void()> &&onComplete) {
pathFinderEventId.fetch_add(1);
g_dispatcher().asyncEvent([executeOnFollow, eventId = pathFinderEventId.load(), self = getCreature(), targetPos = getFollowCreature()->getPosition(), fpp = std::move(fpp), onComplete = std::move(onComplete)] {
if (eventId != self->pathFinderEventId.load()) {
return; // cancel pathfinder
}

stdext::arraylist<Direction> listDir(128);
self->getPathTo(targetPos, listDir, fpp);
g_dispatcher().addEvent([=, listDir = listDir.data()] {
self->startAutoWalk(listDir);

if (executeOnFollow) {
self->onFollowCreatureComplete(self->getFollowCreature());
}

if(onComplete){
onComplete();
}
}, "Creature::goToFollowCreature");
});

}

void Creature::getPathSearchParams(const std::shared_ptr<Creature> &, FindPathParams &fpp) {
fpp.fullPathSearch = !hasFollowPath();
fpp.clearSight = true;
Expand All @@ -958,14 +992,17 @@ void Creature::getPathSearchParams(const std::shared_ptr<Creature> &, FindPathPa
fpp.maxTargetDist = 1;
}

void Creature::goToFollowCreature() {
void Creature::goToFollowCreature(std::function<void()> &&onComplete) {
const auto &followCreature = getFollowCreature();
if (!followCreature) {
return;
}

if (isSummon() && !getMonster()->isFamiliar() && !canFollowMaster()) {
listWalkDir.clear();
if (onComplete) {
onComplete();
}
return;
}

Expand All @@ -980,43 +1017,24 @@ void Creature::goToFollowCreature() {
monster->getDistanceStep(followCreature->getPosition(), dir, true);
} else if (!monster->getDistanceStep(followCreature->getPosition(), dir)) { // maxTargetDist > 1
// if we can't get anything then let the A* calculate
listWalkDir.clear();

pathFinderEventId.fetch_add(1);
g_dispatcher().asyncEvent([eventId = pathFinderEventId.load(), self = getCreature(), targetPos = getFollowCreature()->getPosition(), fpp = std::move(fpp)] {
if (eventId != self->pathFinderEventId.load()) {
return; // cancel pathfinder
}

stdext::arraylist<Direction> listDir(128);
self->getPathTo(targetPos, listDir, fpp);
g_dispatcher().addEvent([self, listDir = listDir.data()] { self->startAutoWalk(listDir); }, "Creature::goToFollowCreature");
});
executeAsyncPathTo(false,fpp, std::move(onComplete));

return;
}

if (dir != DIRECTION_NONE) {
listWalkDir.clear();
startAutoWalk({ dir });
}

onFollowCreatureComplete(followCreature);
if (onComplete) {
onComplete();
}
return;
}

listWalkDir.clear();

pathFinderEventId.fetch_add(1);
g_dispatcher().asyncEvent([eventId = pathFinderEventId.load(), self = getCreature(), targetPos = getFollowCreature()->getPosition(), fpp = std::move(fpp)] {
if (eventId != self->pathFinderEventId.load()) {
return; // cancel pathfinder
}

stdext::arraylist<Direction> listDir(128);
self->getPathTo(targetPos, listDir, fpp);
g_dispatcher().addEvent([self, listDir = listDir.data()] { self->startAutoWalk(listDir); }, "Creature::goToFollowCreature");
});
executeAsyncPathTo(true, fpp, std::move(onComplete));
}

bool Creature::canFollowMaster() {
Expand Down
3 changes: 2 additions & 1 deletion src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class Creature : virtual public Thing, public SharedObject {
void startAutoWalk(const std::vector<Direction> &listDir, bool ignoreConditions = false);
void addEventWalk(bool firstStep = false);
void stopEventWalk();
virtual void goToFollowCreature();
virtual void goToFollowCreature(std::function<void()>&& onComplete = nullptr);

// walk events
virtual void onWalk(Direction &dir);
Expand Down Expand Up @@ -778,4 +778,5 @@ class Creature : virtual public Thing, public SharedObject {
bool canFollowMaster();
bool isLostSummon();
void handleLostSummon(bool teleportSummons);
void executeAsyncPathTo(bool executeOnFollow, FindPathParams &fpp, std::function<void()> &&onComplete);
};
12 changes: 7 additions & 5 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4209,17 +4209,19 @@ bool Player::setAttackedCreature(std::shared_ptr<Creature> creature) {
return true;
}

void Player::goToFollowCreature() {
void Player::goToFollowCreature(std::function<void()> &&onComplete) {
if (!walkTask) {
if ((OTSYS_TIME() - lastFailedFollow) < 2000) {
return;
}

Creature::goToFollowCreature();
Creature::goToFollowCreature([self = getPlayer()] {
if (self->getFollowCreature() && !self->hasFollowPath()) {
self->lastFailedFollow = OTSYS_TIME();
}
});


if (getFollowCreature() && !hasFollowPath()) {
lastFailedFollow = OTSYS_TIME();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ class Player final : public Creature, public Cylinder, public Bankable {

// follow functions
bool setFollowCreature(std::shared_ptr<Creature> creature) override;
void goToFollowCreature() override;
void goToFollowCreature(std::function<void()> &&onComplete = nullptr) override;

// follow events
void onFollowCreature(const std::shared_ptr<Creature> &) override;
Expand Down

0 comments on commit f36ceab

Please sign in to comment.