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 NewNode; // push each possible move except allowing the search to go backwards if( (Astar::GetMap(x - 1, y) == Map::mi_FloorValue) && !((parent_x == x - 1) && (parent_y == y))) { NewNode = MapSearchNode(x - 1, y); astarsearch->AddSuccessor(NewNode); } if( (Astar::GetMap(x, y - 1) == Map::mi_FloorValue) && !((parent_x == x) && (parent_y == y - 1))) { NewNode = MapSearchNode(x, y - 1); astarsearch->AddSuccessor(NewNode); } if( (Astar::GetMap( x + 1, y ) == Map::mi_FloorValue) && !((parent_x == x + 1) && (parent_y == y))) { NewNode = MapSearchNode(x + 1, y); astarsearch->AddSuccessor(NewNode); } if( (Astar::GetMap(x, y + 1) == Map::mi_FloorValue) && !((parent_x == x) && (parent_y == y + 1))) { NewNode = MapSearchNode(x, y + 1); astarsearch->AddSuccessor(NewNode); } return true; }
// This generates the successors to the given Node. It uses a helper function called // AddSuccessor to give the successors to the AStar class. The A* specific initialisation // is done for each node internally, so here you just set the state information that // is specific to the application bool MapSearchNode::GetSuccessors(AStarSearch<MapSearchNode>* astarsearch, MapSearchNode *parent_node, Map* map) { int parent_x = -1; int parent_y = -1; if( parent_node ) { parent_x = parent_node->x; parent_y = parent_node->y; } MapSearchNode NewNode; // push each possible move except allowing the search to go backwards Cell* LeftCell = map->getCell(x - 1, y); if (LeftCell != NULL && LeftCell->isCellWalkable() && !(parent_x == x - 1 && parent_y == y)) { NewNode = MapSearchNode(x - 1, y); astarsearch->AddSuccessor(NewNode); } Cell* AboveCell = map->getCell(x, y - 1); if (AboveCell != NULL && AboveCell->isCellWalkable() && !(parent_x == x && parent_y == y - 1)) { NewNode = MapSearchNode(x, y - 1); astarsearch->AddSuccessor(NewNode); } Cell* rightCell = map->getCell(x + 1, y); if (rightCell != NULL && rightCell->isCellWalkable() && !(parent_x == x + 1 && parent_y == y)) { NewNode = MapSearchNode(x + 1, y); astarsearch->AddSuccessor(NewNode); } Cell* bottomCell = map->getCell(x, y + 1); if (bottomCell != NULL && bottomCell->isCellWalkable() && !(parent_x == x && parent_y == y + 1)) { NewNode = MapSearchNode(x, y + 1); astarsearch->AddSuccessor(NewNode); } return true; }
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 NewNode; if(!mystery->isCollision(x - 1, y) && !((parent_x == x-1) && (parent_y == y)) ) { NewNode = MapSearchNode( x-1, y, mystery ); astarsearch->AddSuccessor( NewNode ); } if(!mystery->isCollision(x, y -1) && !((parent_x == x) && (parent_y == y-1)) ) { NewNode = MapSearchNode( x, y-1, mystery ); astarsearch->AddSuccessor( NewNode ); } if(!mystery->isCollision(x + 1, y) && !((parent_x == x+1) && (parent_y == y)) ) { NewNode = MapSearchNode( x+1, y, mystery ); astarsearch->AddSuccessor( NewNode ); } if(!mystery->isCollision(x, y + 1) && !((parent_x == x) && (parent_y == y+1)) ) { NewNode = MapSearchNode( x, y+1, mystery ); astarsearch->AddSuccessor( NewNode ); } return true; }
//------------------------------------------------------------------------------------- void NavTileHandle::bresenhamLine(int x0, int y0, int x1, int y1, std::vector<MapSearchNode>& results) { // Optimization: it would be preferable to calculate in // advance the size of "result" and to use a fixed-size array // instead of a list. bool steep = abs(y1 - y0) > abs(x1 - x0); if (steep) { swap(x0, y0); swap(x1, y1); } if (x0 > x1) { swap(x0, x1); swap(y0, y1); } int deltax = x1 - x0; int deltay = abs(y1 - y0); int error = 0; int ystep; int y = y0; if (y0 < y1) ystep = 1; else ystep = -1; for (int x = x0; x <= x1; x++) { if (steep) results.push_back(MapSearchNode(y, x)); else results.push_back(MapSearchNode(x, y)); error += deltay; if (2 * error >= deltax) { y += ystep; error -= deltax; } } }
bool MapSearchNode::addSuccessor(AStarSearch<MapSearchNode>* astarsearch, int16_t x_, int16_t y_, int16_t parent_x, int16_t parent_y) { const auto& map = *((PathFinder*)astarsearch)->map; if ((map.isMapCoordValid(x_, y_) == true) && (map[(int32_t)x_][(int32_t)y_].Passable() == true) && !((parent_x == x_) && (parent_y == y_))) { auto direction_ = getPlayerDirection( PairFloat((float)parent_x, (float)parent_y), PairFloat((float)x_, (float)y_)); auto searchNode = MapSearchNode(map, x_, y_, direction_); astarsearch->AddSuccessor(searchNode); return true; } return false; }
//------------------------------------------------------------------------------------- // This generates the successors to the given Node. It uses a helper function called // AddSuccessor to give the successors to the AStar class. The A* specific initialisation // is done for each node internally, so here you just set the state information that // is specific to the application bool NavTileHandle::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 NewNode; // push each possible move except allowing the search to go backwards if( (NavTileHandle::pCurrNavTileHandle->getMap( x-1, y ) < TILE_STATE_CLOSED) && !((parent_x == x-1) && (parent_y == y)) ) { NewNode = MapSearchNode( x-1, y ); astarsearch->AddSuccessor( NewNode ); } if( (NavTileHandle::pCurrNavTileHandle->getMap( x, y-1 ) < TILE_STATE_CLOSED) && !((parent_x == x) && (parent_y == y-1)) ) { NewNode = MapSearchNode( x, y-1 ); astarsearch->AddSuccessor( NewNode ); } if( (NavTileHandle::pCurrNavTileHandle->getMap( x+1, y ) < TILE_STATE_CLOSED) && !((parent_x == x+1) && (parent_y == y)) ) { NewNode = MapSearchNode( x+1, y ); astarsearch->AddSuccessor( NewNode ); } if( (NavTileHandle::pCurrNavTileHandle->getMap( x, y+1 ) < TILE_STATE_CLOSED) && !((parent_x == x) && (parent_y == y+1)) ) { NewNode = MapSearchNode( x, y+1 ); astarsearch->AddSuccessor( NewNode ); } // 如果是8方向移动 if(NavTileHandle::pCurrNavTileHandle->direction8()) { if( (NavTileHandle::pCurrNavTileHandle->getMap( x + 1, y + 1 ) < TILE_STATE_CLOSED) && !((parent_x == x + 1) && (parent_y == y + 1)) ) { NewNode = MapSearchNode( x + 1, y + 1 ); astarsearch->AddSuccessor( NewNode ); } if( (NavTileHandle::pCurrNavTileHandle->getMap( x + 1, y-1 ) < TILE_STATE_CLOSED) && !((parent_x == x + 1) && (parent_y == y-1)) ) { NewNode = MapSearchNode( x + 1, y-1 ); astarsearch->AddSuccessor( NewNode ); } if( (NavTileHandle::pCurrNavTileHandle->getMap( x - 1, y + 1) < TILE_STATE_CLOSED) && !((parent_x == x - 1) && (parent_y == y + 1)) ) { NewNode = MapSearchNode( x - 1, y + 1); astarsearch->AddSuccessor( NewNode ); } if( (NavTileHandle::pCurrNavTileHandle->getMap( x - 1, y - 1 ) < TILE_STATE_CLOSED) && !((parent_x == x - 1) && (parent_y == y - 1)) ) { NewNode = MapSearchNode( x - 1, y - 1 ); astarsearch->AddSuccessor( NewNode ); } } return true; }