本文整理汇总了C++中AStarNode::getNeighbours方法的典型用法代码示例。如果您正苦于以下问题:C++ AStarNode::getNeighbours方法的具体用法?C++ AStarNode::getNeighbours怎么用?C++ AStarNode::getNeighbours使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AStarNode
的用法示例。
在下文中一共展示了AStarNode::getNeighbours方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: compute_path
/**
* Compute a path from (x1,y1) to (x2,y2)
* Store waypoint inside path
* limit is the maximum number of explored node
* @return true if a path is found
*/
bool MapCollision::compute_path(FPoint start_pos, FPoint end_pos, std::vector<FPoint> &path, MOVEMENTTYPE movement_type, unsigned int limit) {
if (is_outside_map(end_pos.x, end_pos.y)) return false;
if (limit == 0)
limit = std::max(map_size.x, map_size.y);
// path must be empty
if (!path.empty())
path.clear();
// convert start & end to MapCollision precision
Point start = map_to_collision(start_pos);
Point end = map_to_collision(end_pos);
// if the target square has an entity, temporarily clear it to compute the path
bool target_blocks = false;
int target_blocks_type = colmap[end.x][end.y];
if (colmap[end.x][end.y] == BLOCKS_ENTITIES || colmap[end.x][end.y] == BLOCKS_ENEMIES) {
target_blocks = true;
unblock(end_pos.x, end_pos.y);
}
Point current = start;
AStarNode* node = new AStarNode(start);
node->setActualCost(0);
node->setEstimatedCost((float)calcDist(start,end));
node->setParent(current);
AStarContainer open(map_size.x, map_size.y, limit);
AStarCloseContainer close(map_size.x, map_size.y, limit);
open.add(node);
while (!open.isEmpty() && (unsigned)close.getSize() < limit) {
node = open.get_shortest_f();
current.x = node->getX();
current.y = node->getY();
close.add(node);
open.remove(node);
if ( current.x == end.x && current.y == end.y)
break; //path found !
//limit evaluated nodes to the size of the map
std::list<Point> neighbours = node->getNeighbours(map_size.x, map_size.y);
// for every neighbour of current node
for (std::list<Point>::iterator it=neighbours.begin(); it != neighbours.end(); ++it) {
Point neighbour = *it;
// do not exceed the node limit when adding nodes
if ((unsigned)open.getSize() >= limit) {
break;
}
// if neighbour is not free of any collision, skip it
if (!is_valid_tile(neighbour.x,neighbour.y,movement_type, false))
continue;
// if nabour is already in close, skip it
if(close.exists(neighbour))
continue;
// if neighbour isn't inside open, add it as a new Node
if(!open.exists(neighbour)) {
AStarNode* newNode = new AStarNode(neighbour.x,neighbour.y);
newNode->setActualCost(node->getActualCost()+(float)calcDist(current,neighbour));
newNode->setParent(current);
newNode->setEstimatedCost((float)calcDist(neighbour,end));
open.add(newNode);
}
// else, update it's cost if better
else {
AStarNode* i = open.get(neighbour.x, neighbour.y);
if (node->getActualCost()+(float)calcDist(current,neighbour) < i->getActualCost()) {
Point pos(i->getX(), i->getY());
Point parent_pos(node->getX(), node->getY());
open.updateParent(pos, parent_pos, node->getActualCost()+(float)calcDist(current,neighbour));
}
}
}
}
if (current.x != end.x || current.y != end.y) {
//couldnt find the target so map a path to the closest node found
node = close.get_shortest_h();
current.x = node->getX();
current.y = node->getY();
while (current.x != start.x || current.y != start.y) {
path.push_back(collision_to_map(current));
current = close.get(current.x, current.y)->getParent();
//.........这里部分代码省略.........