Ejemplo n.º 1
0
/**
 * 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 &currentPos = 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;
}
Ejemplo n.º 2
0
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();
    }

}
Ejemplo n.º 3
0
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();
	}

}