コード例 #1
0
//-----------------------------------------------------------------------------------
bool Path::FindPathStep()
{
    if (m_openList.empty() && m_hasBegun)
    {
        return false;
    }
    PathNode* activeNode = FindLowestFNodeOnOpenListAndRemoveIt();
    m_closedList.push_back(activeNode);
    if (activeNode->position == m_currentGoal)
    {
        RecursivelyBuildPathToStartFromNode(activeNode);
        m_hasBegun = false;
        m_openList.clear();
        m_closedList.clear();
        m_currentGoal = -Vector2Int::ONE;
        return true;
    }
    std::vector<PathNode*> validPositions;
    FindValidAdjacentPositions(validPositions, activeNode, m_currentGoal);
    for (PathNode* positionNode : validPositions)
    {
        MapPosition position = positionNode->position;
        if (IsPositionOnClosedList(positionNode->position))
            continue;
        float localG = positionNode->g; //The computed local G.
        float parentG = activeNode->g; //The parent's actual G, which includes the entire cost to get here.
        float h = positionNode->h; //The computed H for the proposed posiion
        PathNode* existingNode = FindOpenNodeWithPosInOpenList(position);
        if (existingNode)
        {
            if (localG + parentG < existingNode->g)
            {
                existingNode->UpdateVariables(activeNode, localG + parentG, h);
            }
            continue;
        }
        PathNode* newNode = new PathNode(position, activeNode, localG + parentG, h);
        m_openList.push_back(newNode);
    }
    return false;
}
コード例 #2
0
ファイル: PathFinder.cpp プロジェクト: ntaylorbishop/Paradise
//---------------------------------------------------------------------------------------------------------------------------
bool PathFinder::RunOneStep(int movementProperties) {
	if(m_openList.empty())
		return false;

	PathNode* activeNode = FindLowestFNodeOnOpenListAndRemoveIt();
	AddToClosedList(activeNode);

	if (activeNode->m_position == m_goal) {
		WalkClosedListAndConstructPath();
		m_isFinished = true;
		return true;
	}

	std::vector<IntVector2> validPositions = MapProxy::FindValidAdjacentPositions(activeNode->m_position, movementProperties);

	for (IntVector2& position : validPositions) {
		if (IsPositionOnClosedList(position))
			continue;
		float gLocal = MapProxy::ComputeLogalG(activeNode->m_position, position, movementProperties);
		float gParent = activeNode->gLocal + activeNode->gParent;
		float h = MapProxy::ComputeHueristic(position, m_goal);

		PathNode* existingNode = FindOpenNodeWithPos(position);
		if (existingNode) {
			if ((gLocal + gParent) < (existingNode->gLocal + existingNode->gParent)) {
				UpdateNodeValues(existingNode, gLocal, gParent, activeNode);
			}
				continue;
		}

		PathNode* newNode = CreateNode(position, activeNode, gLocal, gParent, h);
		AddNodeToOpenList(newNode);
	}

	return false;
}