Skip to content

Commit

Permalink
First adjustments on NRH-A Pathfinding
Browse files Browse the repository at this point in the history
There was some missing code that was needed to be merged, related mostly to map.cpp and A* Nodes.

Co-Authored-By: Nathan Heinz <NathanRHeinz@gmail.com>
  • Loading branch information
ralke23 and NRH-AA committed Apr 13, 2024
1 parent af9bbe3 commit d5670ec
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/creature.cpp
Expand Up @@ -1012,7 +1012,7 @@ bool Creature::setFollowCreature(Creature* creature)
onWalkAborted();
}

hasFollowPath = false;
//hasFollowPath = false;
followCreature = creature;
creature->addFollowedByCreature(this);
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
Expand Down
4 changes: 2 additions & 2 deletions src/creature.h
Expand Up @@ -68,8 +68,8 @@ class Item;
class Tile;

static constexpr int32_t EVENT_CREATURECOUNT = 10;
static constexpr int32_t EVENT_CREATURE_THINK_INTERVAL = 200;
static constexpr int32_t EVENT_CREATURE_PATH_INTERVAL = 200;
static constexpr int32_t EVENT_CREATURE_THINK_INTERVAL = 1000;
static constexpr int32_t EVENT_CREATURE_PATH_INTERVAL = 2000;
static constexpr int32_t EVENT_CHECK_CREATURE_INTERVAL = (EVENT_CREATURE_THINK_INTERVAL / EVENT_CREATURECOUNT);

class FrozenPathingConditionCall
Expand Down
33 changes: 20 additions & 13 deletions src/map.cpp
Expand Up @@ -752,25 +752,25 @@ bool Map::getPathMatching(const Creature& creature, const Position& targetPos, s
}
}

const float walkCost = AStarNodes::getMapWalkCost(n, pos);
const float tileCost = AStarNodes::getTileWalkCost(creature, tile);
const float newf = n->f + walkCost + tileCost;
const float distEnd = Position::getDistanceX(pos, targetPos) + Position::getDistanceY(pos, targetPos) + newf;
// The cost to walk to this neighbor
const double g = n->g + AStarNodes::getMapWalkCost(n, pos) + AStarNodes::getTileWalkCost(creature, tile);
const double h = AStarNodes::calculateHeuristic(pos, targetPos);
const double newf = h + g;

if (neighborNode) {
if (neighborNode->f <= newf) {
// The node on the closed/open list is cheaper than this one
continue;
}

neighborNode->g = g;
neighborNode->f = newf;
neighborNode->h = distEnd;
neighborNode->parent = n;
nodes.addNode(neighborNode);
}
else {
// Does not exist in the open/closed list, create a new node
nodes.createNewNode(n, pos.x, pos.y, distEnd, newf);
nodes.createNewNode(n, pos.x, pos.y, g, newf);
}
}

Expand Down Expand Up @@ -827,7 +827,7 @@ AStarNodes::AStarNodes(uint16_t x, uint16_t y) : nodes(), nodeMap()
firstNode->parent = nullptr;
firstNode->x = x;
firstNode->y = y;
firstNode->h = 0;
firstNode->g = 0;
firstNode->f = 0;

// Add node to node vector and map
Expand All @@ -836,13 +836,13 @@ AStarNodes::AStarNodes(uint16_t x, uint16_t y) : nodes(), nodeMap()
nodeMap[x][y] = firstNode;
}

void AStarNodes::createNewNode(AStarNode* parent, uint16_t x, uint16_t y, float h, float f)
void AStarNodes::createNewNode(AStarNode* parent, uint16_t x, uint16_t y, double g, double f)
{
AStarNode* newNode = new AStarNode;
newNode->parent = parent;
newNode->x = x;
newNode->y = y;
newNode->h = h;
newNode->g = g;
newNode->f = f;

nodes.push_back(newNode);
Expand All @@ -855,13 +855,20 @@ AStarNode* AStarNodes::getBestNode()
return nullptr;
}

std::sort(nodes.begin(), nodes.end(), [](AStarNode* left, AStarNode* right) {return left->h > right->h; });
std::sort(nodes.begin(), nodes.end(), [](AStarNode* left, AStarNode* right) { return left->f > right->f; });
AStarNode* retNode = nodes.back();
nodes.pop_back();
return retNode;
}

float AStarNodes::getMapWalkCost(AStarNode* node, const Position& neighborPos)
double AStarNodes::calculateHeuristic(const Position& p1, const Position& p2)
{
uint16_t dx = std::abs(p1.x - p2.x);
uint16_t dy = std::abs(p1.y - p2.y);
return std::sqrt((dx * dx) + (dy * dy));
}

double AStarNodes::getMapWalkCost(AStarNode* node, const Position& neighborPos)
{
if (std::abs(node->x - neighborPos.x) == std::abs(node->y - neighborPos.y)) {
// diagonal movement extra cost
Expand All @@ -870,9 +877,9 @@ float AStarNodes::getMapWalkCost(AStarNode* node, const Position& neighborPos)
return MAP_NORMALWALKCOST;
}

float AStarNodes::getTileWalkCost(const Creature& creature, const Tile* tile)
double AStarNodes::getTileWalkCost(const Creature& creature, const Tile* tile)
{
float cost = 0;
double cost = 0;
if (tile->getTopVisibleCreature(&creature)) {
// destroy creature cost
cost += MAP_NORMALWALKCOST * 3;
Expand Down
17 changes: 9 additions & 8 deletions src/map.h
Expand Up @@ -40,29 +40,30 @@ static constexpr int32_t MAP_MAX_LAYERS = 16;

struct FindPathParams;

static constexpr float MAP_NORMALWALKCOST = 1.0f;
static constexpr float MAP_DIAGONALWALKCOST = 2.5f;

struct AStarNode {
AStarNode* parent;
float h;
float f;
double g;
double f;
uint16_t x, y;
};

static constexpr float MAP_NORMALWALKCOST = 1.0f;
static constexpr float MAP_DIAGONALWALKCOST = 2.5f;

class AStarNodes
{
public:
AStarNodes(uint16_t x, uint16_t y);

void createNewNode(AStarNode* parent, uint16_t x, uint16_t y, float h, float f);
void createNewNode(AStarNode* parent, uint16_t x, uint16_t y, double g, double f);
void addNode(AStarNode* node) { nodes.push_back(node); };

AStarNode* getBestNode();
AStarNode* getNodeByPosition(uint16_t x, uint16_t y) { return nodeMap[x][y]; };

static float getMapWalkCost(AStarNode* node, const Position& neighborPos);
static float getTileWalkCost(const Creature& creature, const Tile* tile);
static double calculateHeuristic(const Position& p1, const Position& p2);
static double getMapWalkCost(AStarNode* node, const Position& neighborPos);
static double getTileWalkCost(const Creature& creature, const Tile* tile);

private:
std::vector<AStarNode*> nodes;
Expand Down
1 change: 1 addition & 0 deletions src/monster.cpp
Expand Up @@ -884,6 +884,7 @@ void Monster::doAttacking(uint32_t interval)
}
}

// ensure ranged creatures turn to player
if (updateLook && lastMeleeAttack == 0 && !isFleeing()) {
updateLookDirection();
}
Expand Down

0 comments on commit d5670ec

Please sign in to comment.