void Pathfinding::calculate(BattleUnit *unit, Position endPosition) { Position startPosition = unit->getPosition(); _movementType = unit->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(); // look for a possible fast and accurate bresenham path and skip A* if (startPosition.z == endPosition.z && bresenhamPath(startPosition,endPosition)) { std::reverse(_path.begin(), _path.end()); //paths are stored in reverse order return; } // Now try through A*. aStarPath(startPosition, endPosition); }
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(); } }