void OnlinePlanExpansionExecution::GetReachableReadyNodes(_Out_ IOlcbpPlan::NodeQueue& actionQ, _Out_ IOlcbpPlan::NodeQueue& goalQ) { IOlcbpPlan::NodeQueue Q; IOlcbpPlan::NodeID currNodeId; IOlcbpPlan::NodeSerializedSet planRoots; IOlcbpPlan::NodeSet visitedNodes; _ASSERTE(m_planRootNodeId != IOlcbpPlan::NullNodeID); Q.push(m_planRootNodeId); visitedNodes.insert(m_planRootNodeId); goalQ.push(m_planRootNodeId); // Do a BFS on the plan an collect only ready nodes (i.e nodes with WaitOnParentsCount = 0) // Add ready action nodes to the action Q // Add ready goal nodes to the goal Q while(!Q.empty()) { currNodeId = Q.front(); Q.pop(); if (!m_pOlcbpPlan->Contains(currNodeId)) { LogWarning("A non existing node was there in the update queue, skipping it"); continue; } for (auto childNodeId : m_pOlcbpPlan->GetAdjacentNodes(currNodeId)) { if (visitedNodes.count(childNodeId) == 0) { if (IsNodeReady(childNodeId)) { if (IsGoalNode(childNodeId)) { goalQ.push(childNodeId); } else if (IsActionNode(childNodeId)) { actionQ.push(childNodeId); } Q.push(childNodeId); } visitedNodes.insert(childNodeId); } } } }
int OnlinePlanExpansionExecution::AdaptSnippet(_In_ IOlcbpPlan::NodeID snippetRootGoalId) { int nodeAddRemoveDelta = 0; IOlcbpPlan::NodeQueue Q; IOlcbpPlan::NodeSerializedSet planRoots; IOlcbpPlan::NodeSet visitedNodes; IOlcbpPlan::NodeID parentNodeId = snippetRootGoalId; Q.push(parentNodeId); visitedNodes.insert(parentNodeId); // Do a BFS on the plan an collect only ready nodes (i.e nodes with WaitOnParentsCount = 0) // Add ready action nodes to the action Q // Add ready goal nodes to the goal Q while(!Q.empty()) { parentNodeId = Q.front(); Q.pop(); IOlcbpPlan::NodeSerializedSet originalSnippetNodes = m_pOlcbpPlan->GetAdjacentNodes(parentNodeId); for (auto childNodeId : originalSnippetNodes) { if (visitedNodes.count(childNodeId) != 0) continue; if (IsActionNode(childNodeId)) { ((Action*)m_pOlcbpPlan->GetNode(childNodeId))->InitializeConditions(); for (auto pExpression : ((Action*)m_pOlcbpPlan->GetNode(childNodeId))->PreCondition()->Expressions()) { _ASSERTE(pExpression->ExpressionType() == EXPRESSION_Leaf); if (((ConditionEx*)pExpression)->ContainsParameter(PARAM_ResourceId) && ((ConditionEx*)pExpression)->Parameter(PARAM_ResourceId) == RESOURCE_Supply) { int requiredSupplyAmmount = ((ConditionEx*)pExpression)->Parameter(PARAM_Amount); if (g_Game->Self()->Resources()->AvailableSupply() >= requiredSupplyAmmount) continue; UnlinkNodes(parentNodeId, childNodeId); PlanStepParameters params; params[PARAM_EntityClassId] = g_Game->Self()->Race()->GetResourceSource(RESOURCE_Supply); params[PARAM_Amount] = 1; auto pBuildInfraGoal = g_GoalFactory.GetGoal(GOALEX_BuildInfrastructure, params, true); IOlcbpPlan::NodeID buildInfroNodeId = m_pOlcbpPlan->AddNode(pBuildInfraGoal, pBuildInfraGoal->Id()); m_nodeData[pBuildInfraGoal->Id()] = OlcbpPlanNodeData(); m_nodeData[pBuildInfraGoal->Id()].ID = pBuildInfraGoal->Id(); // Add Data record for the new node SetNodeSatisfyingGoal(pBuildInfraGoal->Id(), snippetRootGoalId); LinkNodes(parentNodeId, buildInfroNodeId); LinkNodes(buildInfroNodeId, childNodeId); ++nodeAddRemoveDelta; } } } Q.push(childNodeId); visitedNodes.insert(childNodeId); } } return nodeAddRemoveDelta; }
void OnlinePlanExpansionExecution::CoverFailedGoals() { IOlcbpPlan::NodeQueue Q; IOlcbpPlan::NodeID currNodeId; IOlcbpPlan::NodeSerializedSet planRoots; IOlcbpPlan::NodeSet visitedNodes; IOlcbpPlan::NodeSet toCover; _ASSERTE(m_planRootNodeId != IOlcbpPlan::NullNodeID); Q.push(m_planRootNodeId); visitedNodes.insert(m_planRootNodeId); while (!Q.empty()) { currNodeId = Q.front(); Q.pop(); if (!m_pOlcbpPlan->Contains(currNodeId)) { LogWarning("A non existing node was there in the update queue, skipping it"); continue; } for (auto childNodeId : m_pOlcbpPlan->GetAdjacentNodes(currNodeId)) { if (visitedNodes.count(childNodeId) == 0) { if (IsGoalNode(childNodeId) && IsNodeDone(childNodeId)) { auto goalNode = m_pOlcbpPlan->GetNode(childNodeId); if (m_coverGoals.count(childNodeId) == 0 && !goalNode->SuccessConditionsSatisfied(*g_Game) && m_backupNodes.count(childNodeId) == 0) { m_coverGoals.insert(childNodeId); toCover.insert(childNodeId); } } visitedNodes.insert(childNodeId); Q.push(childNodeId); } } } for (auto nodeId : toCover) { auto goalNode = m_pOlcbpPlan->GetNode(nodeId); GoalEx* backupNode = (GoalEx*)goalNode->Clone(); m_backupNodes.insert(MakePair(backupNode->Id(), nodeId)); backupNode->SetState(ESTATE_NotPrepared); m_pOlcbpPlan->AddNode(backupNode, backupNode->Id()); m_nodeData[backupNode->Id()] = OlcbpPlanNodeData(); m_nodeData[backupNode->Id()].ID = backupNode->Id(); SetNodeSatisfyingGoal(backupNode->Id(), m_planRootNodeId); LinkNodes(m_planRootNodeId, backupNode->Id()); auto& rootNodeData = GetNodeData(m_planRootNodeId); rootNodeData.SetWaitOnChildrenCount(rootNodeData.WaitOnChildrenCount + 1); } }