Skip to content

Commit

Permalink
from otserv
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyx14 committed Jul 26, 2024
1 parent edafdc3 commit bffcd62
Show file tree
Hide file tree
Showing 17 changed files with 574 additions and 240 deletions.
3 changes: 2 additions & 1 deletion src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,8 @@ void Combat::addDistanceEffect(std::shared_ptr<Creature> caster, const Position

void Combat::doChainEffect(const Position &origin, const Position &dest, uint8_t effect) {
if (effect > 0) {
stdext::arraylist<Direction> dirList(128);
std::vector<Direction> dirList;

FindPathParams fpp;
fpp.minTargetDist = 0;
fpp.maxTargetDist = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/combat/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1926,7 +1926,7 @@ bool ConditionFeared::getFleeDirection(std::shared_ptr<Creature> creature) {
return false;
}

bool ConditionFeared::getFleePath(std::shared_ptr<Creature> creature, const Position &pos, stdext::arraylist<Direction> &dirList) {
bool ConditionFeared::getFleePath(std::shared_ptr<Creature> creature, const Position &pos, std::vector<Direction> &dirList) {
const std::vector<uint8_t> walkSize { 15, 9, 3, 1 };
bool found = false;
std::ptrdiff_t found_size = 0;
Expand Down Expand Up @@ -2029,7 +2029,7 @@ bool ConditionFeared::startCondition(std::shared_ptr<Creature> creature) {

bool ConditionFeared::executeCondition(std::shared_ptr<Creature> creature, int32_t interval) {
Position currentPos = creature->getPosition();
stdext::arraylist<Direction> listDir(128);
std::vector<Direction> listDir;

g_logger().debug("[ConditionFeared::executeCondition] Executing condition, current position is {}", currentPos.toString());

Expand All @@ -2039,7 +2039,7 @@ bool ConditionFeared::executeCondition(std::shared_ptr<Creature> creature, int32
}

if (getFleePath(creature, currentPos, listDir)) {
g_dispatcher().addEvent([id = creature->getID(), listDir = listDir.data()] {
g_dispatcher().addEvent([id = creature->getID(), listDir] {
g_game().forcePlayerAutoWalk(id, listDir);
},
"ConditionFeared::executeCondition");
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/combat/condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ class ConditionFeared final : public Condition {
private:
bool canWalkTo(std::shared_ptr<Creature> creature, Position pos, Direction moveDirection) const;
bool getFleeDirection(std::shared_ptr<Creature> creature);
bool getFleePath(std::shared_ptr<Creature> creature, const Position &pos, stdext::arraylist<Direction> &dirList);
bool getFleePath(std::shared_ptr<Creature> creature, const Position &pos, std::vector<Direction> &dirList);
bool getRandomDirection(std::shared_ptr<Creature> creature, Position pos);
bool isStuck(std::shared_ptr<Creature> creature, Position pos) const;

Expand Down
20 changes: 12 additions & 8 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ bool Creature::getNextStep(Direction &dir, uint32_t &) {
return false;
}

dir = listWalkDir.front();
listWalkDir.pop_front();
dir = listWalkDir.back();
listWalkDir.pop_back();
onWalk(dir);
return true;
}
Expand Down Expand Up @@ -814,14 +814,14 @@ bool Creature::dropCorpse(std::shared_ptr<Creature> lastHitCreature, std::shared
player->sendLootMessage(lootMessage.str());
}

stdext::arraylist<Direction> dirList(128);
FindPathParams fpp;
fpp.minTargetDist = 0;
fpp.maxTargetDist = 1;
fpp.fullPathSearch = true;
fpp.clearSight = true;
fpp.maxSearchDist = 0;

std::vector<Direction> dirList;
auto isReachable = g_game().map.getPathMatching(player->getPosition(), dirList, FrozenPathingConditionCall(corpse->getPosition()), fpp);

if (player->checkAutoLoot(monster->isRewardBoss()) && isReachable) {
Expand Down Expand Up @@ -1078,7 +1078,8 @@ void Creature::goToFollowCreature() {
}

bool executeOnFollow = true;
stdext::arraylist<Direction> listDir(128);
std::vector<Direction> listDir;
listDir.reserve(128);

FindPathParams fpp;
getPathSearchParams(followCreature, fpp);
Expand All @@ -1101,7 +1102,7 @@ void Creature::goToFollowCreature() {
hasFollowPath = getPathTo(followCreature->getPosition(), listDir, fpp);
}

startAutoWalk(listDir.data());
startAutoWalk(listDir);

if (executeOnFollow) {
onFollowCreatureComplete(followCreature);
Expand Down Expand Up @@ -1737,12 +1738,15 @@ bool Creature::isInvisible() const {
!= conditions.end();
}

bool Creature::getPathTo(const Position &targetPos, stdext::arraylist<Direction> &dirList, const FindPathParams &fpp) {
bool Creature::getPathTo(const Position &targetPos, std::vector<Direction> &dirList, const FindPathParams &fpp) {
metrics::method_latency measure(__METHOD_NAME__);
return g_game().map.getPathMatching(getCreature(), dirList, FrozenPathingConditionCall(targetPos), fpp);
if (fpp.maxSearchDist != 0 || fpp.keepDistance) {
return g_game().map.getPathMatchingCond(getCreature(), targetPos, dirList, FrozenPathingConditionCall(targetPos), fpp);
}
return g_game().map.getPathMatching(getCreature(), targetPos, dirList, FrozenPathingConditionCall(targetPos), fpp);
}

bool Creature::getPathTo(const Position &targetPos, stdext::arraylist<Direction> &dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch /*= true*/, bool clearSight /*= true*/, int32_t maxSearchDist /*= 7*/) {
bool Creature::getPathTo(const Position &targetPos, std::vector<Direction> &dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch /*= true*/, bool clearSight /*= true*/, int32_t maxSearchDist /*= 7*/) {
FindPathParams fpp;
fpp.fullPathSearch = fullPathSearch;
fpp.maxSearchDist = maxSearchDist;
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,8 @@ class Creature : virtual public Thing, public SharedObject {

double getDamageRatio(std::shared_ptr<Creature> attacker) const;

bool getPathTo(const Position &targetPos, stdext::arraylist<Direction> &dirList, const FindPathParams &fpp);
bool getPathTo(const Position &targetPos, stdext::arraylist<Direction> &dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch = true, bool clearSight = true, int32_t maxSearchDist = 7);
bool getPathTo(const Position &targetPos, std::vector<Direction> &dirList, const FindPathParams &fpp);
bool getPathTo(const Position &targetPos, std::vector<Direction> &dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch = true, bool clearSight = true, int32_t maxSearchDist = 7);

struct CountBlock_t {
int32_t total;
Expand Down Expand Up @@ -720,7 +720,7 @@ class Creature : virtual public Thing, public SharedObject {
CreatureEventList eventsList;
ConditionList conditions;

std::deque<Direction> listWalkDir;
std::vector<Direction> listWalkDir;

std::weak_ptr<Tile> m_tile;
std::weak_ptr<Creature> m_attackedCreature;
Expand Down
38 changes: 17 additions & 21 deletions src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,27 +1129,23 @@ void Monster::pushItems(std::shared_ptr<Tile> tile, const Direction &nextDirecti
// We can not use iterators here since we can push the item to another tile
// which will invalidate the iterator.
// start from the end to minimize the amount of traffic
TileItemVector* items;
if (!(items = tile->getItemList())) {
return;
}
uint32_t moveCount = 0;
uint32_t removeCount = 0;
auto it = items->begin();
while (it != items->end()) {
std::shared_ptr<Item> item = *it;
if (item && item->hasProperty(CONST_PROP_MOVABLE) && (item->hasProperty(CONST_PROP_BLOCKPATH) || item->hasProperty(CONST_PROP_BLOCKSOLID)) && item->canBeMoved()) {
if (moveCount < 20 && pushItem(item, nextDirection)) {
++moveCount;
} else if (!item->isCorpse() && g_game().internalRemoveItem(item) == RETURNVALUE_NOERROR) {
++removeCount;
if (const auto items = tile->getItemList()) {
uint32_t moveCount = 0;
uint32_t removeCount = 0;
int32_t downItemSize = tile->getDownItemCount();
for (int32_t i = downItemSize; --i >= 0;) {
const auto &item = items->at(i);
if (item && item->hasProperty(CONST_PROP_MOVABLE) && (item->hasProperty(CONST_PROP_BLOCKPATH) || item->hasProperty(CONST_PROP_BLOCKSOLID)) && item->canBeMoved()) {
if (moveCount < 20 && pushItem(item, nextDirection)) {
++moveCount;
} else if (!item->isCorpse() && g_game().internalRemoveItem(item) == RETURNVALUE_NOERROR) {
++removeCount;
}
}
} else {
it++;
}
}
if (removeCount > 0) {
g_game().addMagicEffect(tile->getPosition(), CONST_ME_POFF);
if (removeCount > 0) {
g_game().addMagicEffect(tile->getPosition(), CONST_ME_POFF);
}
}
}

Expand Down Expand Up @@ -1257,12 +1253,12 @@ void Monster::doWalkBack(uint32_t &flags, Direction &nextDirection, bool &result
return;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ void Npc::onThinkWalk(uint32_t interval) {

if (Direction newDirection;
getRandomStep(newDirection)) {
listWalkDir.push_front(newDirection);
listWalkDir.emplace_back(newDirection);
addEventWalk();
}

Expand Down
4 changes: 2 additions & 2 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7818,9 +7818,9 @@ SoundEffect_t Player::getAttackSoundEffect() const {
bool Player::canAutoWalk(const Position &toPosition, const std::function<void()> &function, uint32_t delay /* = 500*/) {
if (!Position::areInRange<1, 1>(getPosition(), toPosition)) {
// Check if can walk to the toPosition and send event to use function
stdext::arraylist<Direction> listDir(128);
std::vector<Direction> listDir;
if (getPathTo(toPosition, listDir, 0, 1, true, true)) {
g_dispatcher().addEvent([creatureId = getID(), dirs = listDir.data()] { g_game().playerAutoWalk(creatureId, dirs); }, __FUNCTION__);
g_dispatcher().addEvent([creatureId = getID(), listDir] { g_game().playerAutoWalk(creatureId, listDir); }, __FUNCTION__);

std::shared_ptr<Task> task = createPlayerTask(delay, function, __FUNCTION__);
setNextWalkActionTask(task);
Expand Down
Loading

0 comments on commit bffcd62

Please sign in to comment.