game2004/server/gameserver/mapsearchnode.cc
aozhiwei b4fb10686c 1
2020-08-06 18:49:47 +08:00

68 lines
1.6 KiB
C++

#include "precompile.h"
#include "mapsearchnode.h"
#include "mapservice.h"
#include "human.h"
#include "room.h"
float MapSearchNode::GoalDistanceEstimate(MapSearchNode &node_goal)
{
float xd = float(((float)x - (float)node_goal.x));
float yd = float(((float)y - (float)node_goal.y));
return xd + yd;
}
bool MapSearchNode::IsGoal(MapSearchNode &node_goal)
{
if (x == node_goal.x && y == node_goal.y) {
return true;
} else {
return false;
}
}
bool MapSearchNode::GetSuccessors(AStarSearch<MapSearchNode> *astarsearch, MapSearchNode *parent_node)
{
int parent_x = -1;
int parent_y = -1;
if (parent_node) {
parent_x = parent_node->x;
parent_y = parent_node->y;
}
MapSearchNode new_node;
new_node.hum = hum;
for (int index = 0; index < GRID9_AGROUD_CELL_NUM; ++index) {
int cell_x = x + GRID9_AGROUND_CELL_INDEX[index][0];
int cell_y = y + GRID9_AGROUND_CELL_INDEX[index][1];
if (parent_x == cell_x && parent_y == cell_y) {
continue;
}
if (hum->room->map_service->GetMap(cell_x, cell_y ) < TILE_STATE_CLOSED) {
new_node = MapSearchNode(cell_x, cell_y);
astarsearch->AddSuccessor(new_node);
}
}
return true;
}
float MapSearchNode::GetCost(MapSearchNode &successor)
{
if (x != successor.x && y != successor.y) {
return (float)(hum->room->map_service->GetMap(x, y) + 0.41421356);
}
return (float)hum->room->map_service->GetMap(x, y);
}
bool MapSearchNode::IsSameState(MapSearchNode &rhs)
{
if (x == rhs.x && y == rhs.y) {
return true;
} else {
return false;
}
}