Skip to content

Commit

Permalink
Move Game::getPathToEx to Creature::getPathTo
Browse files Browse the repository at this point in the history
  • Loading branch information
marksamman committed Mar 19, 2014
1 parent 32a32df commit eed12d4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 41 deletions.
20 changes: 18 additions & 2 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ void Creature::goToFollowCreature()
if (!monster->getDistanceStep(followCreature->getPosition(), dir)) {
// if we can't get anything then let the A* calculate
listWalkDir.clear();
if (g_game.getPathToEx(this, followCreature->getPosition(), listWalkDir, fpp)) {
if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) {
hasFollowPath = true;
startAutoWalk(listWalkDir);
} else {
Expand All @@ -962,7 +962,7 @@ void Creature::goToFollowCreature()
}
} else {
listWalkDir.clear();
if (g_game.getPathToEx(this, followCreature->getPosition(), listWalkDir, fpp)) {
if (getPathTo(followCreature->getPosition(), listWalkDir, fpp)) {
hasFollowPath = true;
startAutoWalk(listWalkDir);
} else {
Expand Down Expand Up @@ -1644,3 +1644,19 @@ bool Creature::isInvisible() const
return condition->getType() == CONDITION_INVISIBLE;
}) != conditions.end();
}

bool Creature::getPathTo(const Position& targetPos, std::list<Direction>& dirList, const FindPathParams& fpp) const
{
return g_game.getMap()->getPathMatching(*this, dirList, FrozenPathingConditionCall(targetPos), fpp);
}

bool Creature::getPathTo(const Position& targetPos, std::list<Direction>& dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch /*= true*/, bool clearSight /*= true*/, int32_t maxSearchDist /*= 0*/) const
{
FindPathParams fpp;
fpp.fullPathSearch = fullPathSearch;
fpp.maxSearchDist = maxSearchDist;
fpp.clearSight = clearSight;
fpp.minTargetDist = minTargetDist;
fpp.maxTargetDist = maxTargetDist;
return getPathTo(targetPos, dirList, fpp);
}
3 changes: 3 additions & 0 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ class Creature : virtual public Thing

virtual double getDamageRatio(Creature* attacker) const;

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

void useThing2() {
++useCount;
}
Expand Down
39 changes: 9 additions & 30 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ void Game::playerMoveCreature(uint32_t playerId, uint32_t movingCreatureId,
if (!Position::areInRange<1, 1, 0>(movingCreatureOrigPos, player->getPosition())) {
//need to walk to the creature first before moving it
std::list<Direction> listDir;
if (getPathToEx(player, movingCreatureOrigPos, listDir, 0, 1, true, true)) {
if (player->getPathTo(movingCreatureOrigPos, listDir, 0, 1, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk,
this, player->getID(), listDir)));
SchedulerTask* task = createSchedulerTask(1500, std::bind(&Game::playerMoveCreature, this,
Expand Down Expand Up @@ -1156,8 +1156,7 @@ void Game::playerMoveItem(uint32_t playerId, const Position& fromPos,
if (!Position::areInRange<1, 1, 0>(playerPos, mapFromPos)) {
//need to walk to the item first before using it
std::list<Direction> listDir;

if (getPathToEx(player, item->getPosition(), listDir, 0, 1, true, true)) {
if (player->getPathTo(item->getPosition(), listDir, 0, 1, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk,
this, player->getID(), listDir)));

Expand Down Expand Up @@ -1216,7 +1215,7 @@ void Game::playerMoveItem(uint32_t playerId, const Position& fromPos,
}

std::list<Direction> listDir;
if (getPathToEx(player, walkPos, listDir, 0, 0, true, true)) {
if (player->getPathTo(walkPos, listDir, 0, 0, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk,
this, player->getID(), listDir)));

Expand Down Expand Up @@ -2170,7 +2169,7 @@ void Game::playerUseItemEx(uint32_t playerId, const Position& fromPos, uint8_t f
}

std::list<Direction> listDir;
if (getPathToEx(player, walkToPos, listDir, 0, 1, true, true)) {
if (player->getPathTo(walkToPos, listDir, 0, 1, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir)));

SchedulerTask* task = createSchedulerTask(400, std::bind(&Game::playerUseItemEx, this,
Expand Down Expand Up @@ -2228,7 +2227,7 @@ void Game::playerUseItem(uint32_t playerId, const Position& pos, uint8_t stackPo
if (ret != RET_NOERROR) {
if (ret == RET_TOOFARAWAY) {
std::list<Direction> listDir;
if (getPathToEx(player, pos, listDir, 0, 1, true, true)) {
if (player->getPathTo(pos, listDir, 0, 1, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk,
this, player->getID(), listDir)));

Expand Down Expand Up @@ -2322,8 +2321,7 @@ void Game::playerUseWithCreature(uint32_t playerId, const Position& fromPos, uin
}

std::list<Direction> listDir;

if (getPathToEx(player, walkToPos, listDir, 0, 1, true, true)) {
if (player->getPathTo(walkToPos, listDir, 0, 1, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk,
this, player->getID(), listDir)));

Expand Down Expand Up @@ -2434,7 +2432,7 @@ void Game::playerRotateItem(uint32_t playerId, const Position& pos, uint8_t stac

if (pos.x != 0xFFFF && !Position::areInRange<1, 1, 0>(pos, player->getPosition())) {
std::list<Direction> listDir;
if (getPathToEx(player, pos, listDir, 0, 1, true, true)) {
if (player->getPathTo(pos, listDir, 0, 1, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk,
this, player->getID(), listDir)));

Expand Down Expand Up @@ -2533,7 +2531,7 @@ void Game::playerBrowseField(uint32_t playerId, const Position& pos)

if (!Position::areInRange<1, 1, 0>(playerPos, pos)) {
std::list<Direction> listDir;
if (getPathToEx(player, pos, listDir, 0, 1, true, true)) {
if (player->getPathTo(pos, listDir, 0, 1, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk,
this, player->getID(), listDir)));
SchedulerTask* task = createSchedulerTask(400, std::bind(
Expand Down Expand Up @@ -2654,7 +2652,7 @@ void Game::playerRequestTrade(uint32_t playerId, const Position& pos, uint8_t st
return;
} else if (!Position::areInRange<1, 1, 0>(tradeItemPosition, playerPosition)) {
std::list<Direction> listDir;
if (getPathToEx(player, pos, listDir, 0, 1, true, true)) {
if (player->getPathTo(pos, listDir, 0, 1, true, true)) {
g_dispatcher.addTask(createTask(std::bind(&Game::playerAutoWalk,
this, player->getID(), listDir)));

Expand Down Expand Up @@ -3692,25 +3690,6 @@ bool Game::internalCreatureSay(Creature* creature, SpeakClasses type, const std:
return true;
}

bool Game::getPathToEx(const Creature* creature, const Position& targetPos,
std::list<Direction>& dirList, const FindPathParams& fpp)
{
return map.getPathMatching(*creature, dirList, FrozenPathingConditionCall(targetPos), fpp);
}

bool Game::getPathToEx(const Creature* creature, const Position& targetPos, std::list<Direction>& dirList,
int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch /*= true*/,
bool clearSight /*= true*/, int32_t maxSearchDist /*= 0*/)
{
FindPathParams fpp;
fpp.fullPathSearch = fullPathSearch;
fpp.maxSearchDist = maxSearchDist;
fpp.clearSight = clearSight;
fpp.minTargetDist = minTargetDist;
fpp.maxTargetDist = maxTargetDist;
return getPathToEx(creature, targetPos, dirList, fpp);
}

void Game::checkCreatureWalk(uint32_t creatureId)
{
Creature* creature = getCreatureByID(creatureId);
Expand Down
7 changes: 0 additions & 7 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,6 @@ class Game
int32_t rangex = Map::maxClientViewportX, int32_t rangey = Map::maxClientViewportY) const;
bool isSightClear(const Position& fromPos, const Position& toPos, bool sameFloor) const;

bool getPathToEx(const Creature* creature, const Position& targetPos, std::list<Direction>& dirList,
const FindPathParams& fpp);

bool getPathToEx(const Creature* creature, const Position& targetPos, std::list<Direction>& dirList,
int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch = true,
bool clearSight = true, int32_t maxSearchDist = 0);

void changeSpeed(Creature* creature, int32_t varSpeedDelta);
void internalCreatureChangeOutfit(Creature* creature, const Outfit_t& oufit);
void internalCreatureChangeVisible(Creature* creature, bool visible);
Expand Down
2 changes: 1 addition & 1 deletion src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8093,7 +8093,7 @@ int32_t LuaScriptInterface::luaCreatureGetPathTo(lua_State* L)
}

std::list<Direction> dirList;
if (g_game.getPathToEx(creature, position, dirList, fpp)) {
if (creature->getPathTo(position, dirList, fpp)) {
lua_createtable(L, dirList.size(), 0);

int32_t index = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ bool Npc::getRandomStep(Direction& dir)
void Npc::doMoveTo(const Position& target)
{
std::list<Direction> listDir;
if (g_game.getPathToEx(this, target, listDir, 1, 1, true, true)) {
if (getPathTo(target, listDir, 1, 1, true, true)) {
startAutoWalk(listDir);
}
}
Expand Down

0 comments on commit eed12d4

Please sign in to comment.