Skip to content

Commit

Permalink
feat: monster return-to-spawn behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Jan 17, 2024
1 parent 9aaee3b commit ac7c9af
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,11 @@ void Monster::updateIdleStatus() {
bool idle = false;
if (conditions.empty()) {
if (!isSummon() && targetList.empty()) {
idle = true;
if (isInSpawnLocation()) {
idle = true;
} else {
isWalkingBack = true;
}
} else if (const auto &master = getMaster()) {
if ((!isSummon() && totalPlayersOnScreen == 0 || isSummon() && master->getMonster() && master->getMonster()->totalPlayersOnScreen == 0) && getFaction() != FACTION_DEFAULT) {
idle = true;
Expand All @@ -696,6 +700,13 @@ void Monster::updateIdleStatus() {
setIdle(idle);
}

bool Monster::isInSpawnLocation() const {
if (!spawnMonster) {
return true;
}
return position == masterPos || masterPos == Position();
}

void Monster::onAddCondition(ConditionType_t type) {
onConditionStatusChange(type);
}
Expand Down Expand Up @@ -1159,6 +1170,8 @@ bool Monster::getNextStep(Direction &nextDirection, uint32_t &flags) {

if (getFollowCreature() && hasFollowPath) {
doFollowCreature(flags, nextDirection, result);
} else if (isWalkingBack) {
doWalkBack(flags, nextDirection, result);
} else {
doRandomStep(nextDirection, result);
}
Expand Down Expand Up @@ -1187,6 +1200,31 @@ void Monster::doRandomStep(Direction &nextDirection, bool &result) {
}
}

void Monster::doWalkBack(uint32_t &flags, Direction &nextDirection, bool &result) {
result = Creature::getNextStep(nextDirection, flags);
if (result) {
flags |= FLAG_PATHFINDING;
} else {
if (ignoreFieldDamage) {
ignoreFieldDamage = false;
updateMapCache();
}

int32_t distance = std::max<int32_t>(Position::getDistanceX(position, masterPos), Position::getDistanceY(position, masterPos));
if (distance == 0) {
isWalkingBack = false;
return;
}

stdext::arraylist<Direction> listDir(128);
if (!getPathTo(masterPos, listDir, 0, std::max<int32_t>(0, distance - 5), true, true, distance)) {
isWalkingBack = false;
return;
}
startAutoWalk(listDir.data());
}
}

void Monster::doFollowCreature(uint32_t &flags, Direction &nextDirection, bool &result) {
randomStepping = false;
result = Creature::getNextStep(nextDirection, flags);
Expand Down
3 changes: 3 additions & 0 deletions src/creatures/monsters/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ class Monster final : public Creature {

Position masterPos;

bool isWalkingBack = false;
bool isIdle = true;
bool extraMeleeAttack = false;
bool randomStepping = false;
Expand Down Expand Up @@ -424,6 +425,7 @@ class Monster final : public Creature {
bool canUseSpell(const Position &pos, const Position &targetPos, const spellBlock_t &sb, uint32_t interval, bool &inRange, bool &resetTicks);
bool getRandomStep(const Position &creaturePos, Direction &direction);
bool getDanceStep(const Position &creaturePos, Direction &direction, bool keepAttack = true, bool keepDistance = true);
bool isInSpawnLocation() const;
bool isInSpawnRange(const Position &pos) const;
bool canWalkTo(Position pos, Direction direction);

Expand Down Expand Up @@ -457,6 +459,7 @@ class Monster final : public Creature {

static std::vector<std::pair<int8_t, int8_t>> getPushItemLocationOptions(const Direction &direction);

void doWalkBack(uint32_t &flags, Direction &nextDirection, bool &result);
void doFollowCreature(uint32_t &flags, Direction &nextDirection, bool &result);
void doRandomStep(Direction &nextDirection, bool &result);

Expand Down

0 comments on commit ac7c9af

Please sign in to comment.