KDvoid Pathfinding::searchLowestCostNodeInOpenList ( const CCPoint& tTargetTile ) { PathfindingNode* pLowestCostNode = KD_NULL; CCObject* pObject; CCARRAY_FOREACH ( m_pOpenList, pObject ) { PathfindingNode* pNode = (PathfindingNode*) pObject; if ( pLowestCostNode == KD_NULL ) { pLowestCostNode = pNode; } else { if ( pNode->getF ( ) < pLowestCostNode->getF ( ) ) { pLowestCostNode = pNode; } } }
/** * Use Dijkstra's algorithm to locate all tiles reachable to @a *unit with a TU cost no more than @a tuMax. * @param unit Pointer to the unit. * @param tuMax The maximum cost of the path to each tile. * @return An array of reachable tiles, sorted in ascending order of cost. The first tile is the start location. */ std::vector<int> Pathfinding::findReachable(BattleUnit *unit, int tuMax) { const Position &start = unit->getPosition(); for (std::vector<PathfindingNode>::iterator it = _nodes.begin(); it != _nodes.end(); ++it) { it->reset(); } PathfindingNode *startNode = getNode(start); startNode->connect(0, 0, 0); PathfindingOpenSet unvisited; unvisited.push(startNode); std::vector<PathfindingNode*> reachable; while (!unvisited.empty()) { PathfindingNode *currentNode = unvisited.pop(); Position const ¤tPos = currentNode->getPosition(); // Try all reachable neighbours. for (int direction = 0; direction < 10; direction++) { Position nextPos; int tuCost = getTUCost(currentPos, direction, &nextPos, unit); if (tuCost == 255) // Skip unreachable / blocked continue; if (tuCost > tuMax) // Run out of TUs continue; PathfindingNode *nextNode = getNode(nextPos); if (nextNode->isChecked()) // Our algorithm means this node is already at minimum cost. continue; int totalTuCost = currentNode->getTUCost() + tuCost; // If this node is unvisited or visited from a better path. if (!nextNode->inOpenSet() || nextNode->getTUCost() > totalTuCost) { nextNode->connect(totalTuCost, currentNode, direction); unvisited.push(nextNode); } } currentNode->setChecked(); reachable.push_back(currentNode); } std::sort(reachable.begin(), reachable.end(), MinNodeCosts()); std::vector<int> tiles; tiles.reserve(reachable.size()); for (std::vector<PathfindingNode*>::const_iterator it = reachable.begin(); it != reachable.end(); ++it) { tiles.push_back(_save->getTileIndex((*it)->getPosition())); } return tiles; }
/** * Calculate the shortest path using a simple A-Star algorithm. * The unit information and movement type must have already been set. * The path information is set only if a valid path is found. * @param startPosition The position to start from. * @param endPosition The position we want to reach. * @return True if a path exists, false otherwise. */ bool Pathfinding::aStarPath(const Position &startPosition, const Position &endPosition) { // reset every node, so we have to check them all for (std::vector<PathfindingNode>::iterator it = _nodes.begin(); it != _nodes.end(); ++it) it->reset(); // start position is the first one in our "open" list PathfindingNode *start = getNode(startPosition); start->connect(0, 0, 0, endPosition); PathfindingOpenSet openList; openList.push(start); // if the open list is empty, we've reached the end while(!openList.empty()) { PathfindingNode *currentNode = openList.pop(); Position const ¤tPos = currentNode->getPosition(); currentNode->setChecked(); if (currentPos == endPosition) // We found our target. { _path.clear(); PathfindingNode *pf = currentNode; while (pf->getPrevNode()) { _path.push_back(pf->getPrevDir()); pf = pf->getPrevNode(); } return true; } // Try all reachable neighbours. for (int direction = 0; direction < 10; direction++) { Position nextPos; int tuCost = getTUCost(currentPos, direction, &nextPos, _unit); if (tuCost == 255) // Skip unreachable / blocked continue; PathfindingNode *nextNode = getNode(nextPos); if (nextNode->isChecked()) // Our algorithm means this node is already at minimum cost. continue; int totalTuCost = currentNode->getTUCost() + tuCost; // If this node is unvisited or visited from a better path. if (!nextNode->inOpenSet() || nextNode->getTUCost() > totalTuCost) { nextNode->connect(totalTuCost, currentNode, direction, endPosition); openList.push(nextNode); } } } // Unble to reach the target return false; }
void Pathfinding::calculate(BattleUnit *unit, Position &endPosition) { std::list<PathfindingNode*> openList; Position currentPos, nextPos, startPosition = unit->getPosition(); int tuCost; _movementType = MT_WALK; // should be parameter _unit = unit; Tile *destinationTile = _save->getTile(endPosition); // check if destination is not blocked if (isBlocked(destinationTile, MapData::O_FLOOR) || isBlocked(destinationTile, MapData::O_OBJECT)) return; // the following check avoids that the unit walks behind the stairs if we click behind the stairs to make it go up the stairs. // it only works if the unit is on one of the 2 tiles on the stairs, or on the tile right in front of the stairs. if (isOnStairs(startPosition, endPosition)) { endPosition.z++; destinationTile = _save->getTile(endPosition); } // check if we have floor, else lower destination (for non flying units only, because otherwise they never reached this place) while (canFallDown(destinationTile)) { endPosition.z--; destinationTile = _save->getTile(endPosition); } _path.clear(); // reset every node, so we have to check them all for (int i = 0; i < _size; ++i) _nodes[i]->reset(); // start position is the first one in our "open" list openList.push_back(getNode(startPosition)); openList.front()->check(0, 0, 0, 0); // if the open list is empty, we've reached the end while(!openList.empty()) { // this algorithm expands in all directions for (int direction = 0; direction < 8; direction++) { currentPos = openList.front()->getPosition(); tuCost = getTUCost(currentPos, direction, &nextPos, unit); if(tuCost < 255) { if( (!getNode(nextPos)->isChecked() || getNode(nextPos)->getTUCost() > getNode(currentPos)->getTUCost() + tuCost) && (!getNode(endPosition)->isChecked() || getNode(endPosition)->getTUCost() > getNode(currentPos)->getTUCost() + tuCost) ) { getNode(nextPos)->check(getNode(currentPos)->getTUCost() + tuCost, getNode(currentPos)->getStepsNum() + 1, getNode(currentPos), direction); openList.push_back(getNode(nextPos)); } } } openList.pop_front(); } if(!getNode(endPosition)->isChecked()) return; //Backward tracking of the path PathfindingNode* pf = getNode(endPosition); for (int i = getNode(endPosition)->getStepsNum(); i > 0; i--) { _path.push_back(pf->getPrevDir()); pf=pf->getPrevNode(); } }
void Pathfinding::calculate(BattleUnit *unit, Position endPosition) { std::list<PathfindingNode*> openList; PathfindingNode *currentNode, *nextNode; Position currentPos, nextPos, startPosition = unit->getPosition(); int tuCost, totalTuCost = 0; _movementType = unit->getUnit()->getArmor()->getMovementType(); _unit = unit; Tile *destinationTile = _save->getTile(endPosition); // check if destination is not blocked if (isBlocked(destinationTile, MapData::O_FLOOR) || isBlocked(destinationTile, MapData::O_OBJECT)) return; // the following check avoids that the unit walks behind the stairs if we click behind the stairs to make it go up the stairs. // it only works if the unit is on one of the 2 tiles on the stairs, or on the tile right in front of the stairs. if (isOnStairs(startPosition, endPosition)) { endPosition.z++; destinationTile = _save->getTile(endPosition); } // check if we have floor, else lower destination (for non flying units only, because otherwise they never reached this place) while (canFallDown(destinationTile) && _movementType != MT_FLY) { endPosition.z--; destinationTile = _save->getTile(endPosition); } _path.clear(); if (startPosition.z == endPosition.z && bresenhamPath(startPosition, endPosition)) return; _path.clear(); // reset every node, so we have to check them all for (int i = 0; i < _size; ++i) _nodes[i]->reset(); // start position is the first one in our "open" list openList.push_back(getNode(startPosition)); openList.front()->check(0, 0, 0, 0); // if the open list is empty, we've reached the end while(!openList.empty()) { currentPos = openList.front()->getPosition(); currentNode = getNode(currentPos); // this algorithm expands in all directions for (int direction = 0; direction < 10; direction++) { tuCost = getTUCost(currentPos, direction, &nextPos, unit); if(tuCost < 255) // check if we can go to this node (ie is not blocked) { nextNode = getNode(nextPos); totalTuCost = currentNode->getTUCost() + tuCost; // if we haven't checked this node, or the current cost tu cost is lower than our previous path, push this node in the open list to visit later. if( (!nextNode->isChecked() || nextNode->getTUCost() > totalTuCost) && // this will keep pushing back nodes, as long as we did not reach the end position or there are still possible shorter paths (!getNode(endPosition)->isChecked() || getNode(endPosition)->getTUCost() > totalTuCost) ) { nextNode->check(totalTuCost, currentNode->getStepsNum() + 1, currentNode, direction); openList.push_back(nextNode); } } } openList.pop_front(); } if(!getNode(endPosition)->isChecked()) return; //Backward tracking of the path PathfindingNode* pf = getNode(endPosition); for (int i = getNode(endPosition)->getStepsNum(); i > 0; i--) { _path.push_back(pf->getPrevDir()); pf=pf->getPrevNode(); } }
CCArray* Pathfinding::search ( const CCPoint& tStartTile, const CCPoint& tTargetTile ) { this->setOpenList ( CCArray::create ( ) ); this->setClosedList ( CCArray::create ( ) ); CCLOG ( "In search, within thread" ); // Add the first node to the open list PathfindingNode* pNode = PathfindingNode::create ( ); pNode->setTilePos ( tStartTile ); pNode->setParent ( KD_NULL ); pNode->setG ( 0 ); pNode->setH ( 0 ); pNode->setF ( pNode->getG ( ) + pNode->getH ( ) ); m_pOpenList->addObject ( pNode ); this->searchLowestCostNodeInOpenList ( tTargetTile ); // Retrieve path CCArray* pPathToPlayer = CCArray::create ( ); pNode = this->isOnList ( tTargetTile, m_pClosedList ); if ( pNode ) { CCLOG ( "Path found..." ); pPathToPlayer->addObject ( pNode ); if ( l_bPathfindingDebuggingTiles ) { // Debugging pathfinding GameMgr->getCoordinateFunctions ( )->debugTile ( pNode->getTilePos ( ) ); } PathfindingNode* pParentnode = pNode->getParent ( ); while ( pParentnode ) { CCLOG ( "%f %f", pNode->getTilePos ( ).x, pNode->getTilePos ( ).y ); pNode = pParentnode; pParentnode = pNode->getParent ( ); pPathToPlayer->addObject ( pNode ); if ( l_bPathfindingDebuggingTiles ) { // Debugging pathfinding GameMgr->getCoordinateFunctions ( )->debugTile ( pNode->getTilePos ( ) ); } } } else { CCLOG ( "No path found" ); } return pPathToPlayer; }