完成a星算法集成

This commit is contained in:
aozhiwei 2020-08-06 17:28:34 +08:00
parent 83be6833b6
commit dd6369d5e6
5 changed files with 84 additions and 7 deletions

View File

@ -378,3 +378,22 @@ const int TERMINATOR_BUFF_ID = 1033;
const int TURN_OVER_SKILL_ID = 41001;
/*
1 2 3
4 5 6
7 8 9
*/
const int GRID9_AGROUD_CELL_NUM = 8;
const int GRID9_AGROUND_CELL_INDEX[GRID9_AGROUD_CELL_NUM][2] =
{
{-1, -1}, //1
{0, -1}, //2
{1, -1}, //3
{-1, 0}, //4
{1, 0}, //6
{-1, 1}, //7
{0, 1}, //8
{1, 1} //9
};

View File

@ -1,6 +1,7 @@
#include "precompile.h"
#include "mapsearchnode.h"
#include "mapservice.h"
float MapSearchNode::GoalDistanceEstimate(MapSearchNode &node_goal)
{
@ -21,19 +22,36 @@ bool MapSearchNode::IsGoal(MapSearchNode &node_goal)
bool MapSearchNode::GetSuccessors(AStarSearch<MapSearchNode> *astarsearch, MapSearchNode *parent_node)
{
return false;
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.map_service = map_service;
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 ((map_service->GetMap(cell_x, cell_y ) < TILE_STATE_CLOSED) &&
!((parent_x == cell_x) && (parent_y == cell_y))
) {
new_node = MapSearchNode(cell_x, cell_y);
astarsearch->AddSuccessor(new_node);
}
}
return true;
}
float MapSearchNode::GetCost(MapSearchNode &successor)
{
#if 1
return 1.0f;
#else
if (x != successor.x && y != successor.y) {
return (float) (NavTileHandle::pCurrNavTileHandle->getMap( x, y ) + 0.41421356/* ±¾ÉíÓÐÖÁÉÙ1µÄÖµ */); //sqrt(2)
return (float)(map_service->GetMap(x, y) + 0.41421356);
}
return (float) NavTileHandle::pCurrNavTileHandle->getMap( x, y );
#endif
return (float)map_service->GetMap(x, y);
}
bool MapSearchNode::IsSameState(MapSearchNode &rhs)

View File

@ -2,12 +2,15 @@
#include "framework/stlastar.h"
class MapService;
class MapSearchNode
{
public:
int x = 0; // the (x,y) positions of the node
int y = 0;
MapService* map_service = nullptr;
MapSearchNode() {}
MapSearchNode(int px, int py) {x = px; y = py; }
float GoalDistanceEstimate(MapSearchNode &node_goal);

View File

@ -227,12 +227,36 @@ void MapService::GetColliders(Room* room,
}
}
int MapService::GetMap(int x, int y)
{
if (!IsValidPos(x, y)) {
return TILE_STATE_CLOSED;
}
list_head* head = &map_cells_[x * y];
if (list_empty(head)) {
return TILE_STATE_OPENED_COST1;
} else {
return TILE_STATE_CLOSED;
}
}
int MapService::GetGridId(float world_x, float world_y)
{
int grid_id = (int)(world_x/cell_width_) + (int)(world_y/cell_width_) * map_width_;
return grid_id;
}
bool MapService::IsValidPos(int x, int y)
{
if (x < 0 ||
y < 0 ||
x >= map_width_ ||
y >= map_height_) {
return false;
}
return true;
}
FindPathStatus* MapService::AddFindPathRequest(Human* hum,
const a8::Vec2& start_pos,
const a8::Vec2& end_pos,

View File

@ -5,6 +5,17 @@
class Human;
class ColliderComponent;
enum TILE_STATE
{
TILE_STATE_OPENED_COST0 = 0,
TILE_STATE_OPENED_COST1 = 1,
TILE_STATE_OPENED_COST2 = 2,
TILE_STATE_OPENED_COST3 = 3,
TILE_STATE_OPENED_COST4 = 4,
TILE_STATE_OPENED_COST5 = 5,
TILE_STATE_CLOSED = 9
};
struct CellNode
{
ColliderComponent* collider;
@ -52,6 +63,7 @@ class MapService
float world_x,
float world_y,
std::set<ColliderComponent*>& colliders);
int GetMap(int x, int y);
FindPathStatus* AddFindPathRequest(Human* hum,
const a8::Vec2& start_pos,
@ -65,6 +77,7 @@ class MapService
private:
int GetGridId(float world_x, float world_y);
bool IsValidPos(int x, int y);
private:
list_head* map_cells_ = nullptr;