private void BuildPlanHandler() { // Create planner. var planner = new AntAIPlanner(); // planner.DebugMode = true; planner.LoadScenario(_scenario); // Create current world state and set current conditions. var current = new AntAICondition(); current.BeginUpdate(planner); for (int i = 0, n = _worldState.list.Length; i < n; i++) { current.Set(_scenario.conditions.GetName(_worldState.list[i].id), _worldState.list[i].value); } current.EndUpdate(); // Create our goal world state. AntAIScenarioGoal defaultGoal = null; for (int i = 0, n = _scenario.goals.Length; i < n; i++) { if (_scenario.goals[i].isDefault) { defaultGoal = _scenario.goals[i]; break; } } A.Assert(defaultGoal == null, "Default goal not found!"); if (defaultGoal == null) { return; } // Copy conditions from the scenario goal to the goal conditions. var goal = new AntAICondition(); goal.BeginUpdate(planner); for (int i = 0, n = defaultGoal.conditions.Length; i < n; i++) { goal.Set(_scenario.conditions.GetName(defaultGoal.conditions[i].id), defaultGoal.conditions[i].value); } goal.EndUpdate(); // Create and build the plan. var plan = new AntAIPlan(); planner.MakePlan(ref plan, current, goal); // Call event when plan is ready. if (EventBuildPlan != null) { EventBuildPlan(this, plan, planner); } }
public AntAICondition defaultGoal; // Цель по умолчанию. public AntAIAgent() { sense = null; currentState = null; defaultState = null; worldState = new AntAICondition(); planner = new AntAIPlanner(); currentPlan = new AntAIPlan(); currentGoal = null; }
private void ReconstructPlan(ref AntAIPlan aPlan, List <AntAINode> aClosed, AntAINode aGoal) { aPlan.Reset(); int index; AntAINode current = aGoal; while (current != null && current.parent != null) { aPlan.Insert(current.action); index = FindEqual(aClosed, current.parent); current = (index == -1) ? aClosed[0] : aClosed[index]; } }
private void OnPlanUpdated(AntAIPlan aPlan) { float v = 0.0f; _currentPlan = aPlan; UpdatePlan(_totalDrag + _planNodesPosition, out v); UpdateBlackboardNode(); if (_blackboardNode != null) { var p = _totalDrag + _planNodesPosition; p.y += v; _blackboardNode.rect.position = p; } }
/// <summary> /// Builds the plan. /// </summary> /// <param name="aPlan">Reference to the result Plan.</param> /// <param name="aCurrent">Current World State.</param> /// <param name="aGoal">Goal World State that we want reach.</param> public void MakePlan(ref AntAIPlan aPlan, AntAICondition aCurrent, AntAICondition aGoal) { #if UNITY_EDITOR DebugConditions = aCurrent.Clone(); #endif var opened = new List <AntAINode>(); var closed = new List <AntAINode>(); opened.Add(new AntAINode { world = aCurrent, parent = null, cost = 0, heuristic = aCurrent.Heuristic(aGoal), sum = aCurrent.Heuristic(aGoal), action = string.Empty }); AntAINode current = opened[0]; while (opened.Count > 0) { // Find lowest rank. current = opened[0]; for (int i = 1, n = opened.Count; i < n; i++) { if (opened[i].sum < current.sum) { current = opened[i]; } } opened.Remove(current); if (current.world.Match(aGoal)) { // Plan is found! ReconstructPlan(ref aPlan, closed, current); aPlan.isSuccess = true; if (EventPlanUpdated != null) { EventPlanUpdated(aPlan); } return; } closed.Add(current); // Get neighbors. List <AntAIAction> neighbors = GetPossibleTransitions(current.world); AntAICondition neighbor; int openedIndex = -1; int closedIndex = -1; int cost = -1; for (int i = 0, n = neighbors.Count; i < n; i++) { cost = current.cost + neighbors[i].cost; neighbor = current.world.Clone(); neighbor.Act(neighbors[i].post); openedIndex = FindEqual(opened, neighbor); closedIndex = FindEqual(closed, neighbor); if (openedIndex > -1 && cost < opened[openedIndex].cost) { opened.RemoveAt(openedIndex); openedIndex = -1; } if (closedIndex > -1 && cost < closed[closedIndex].cost) { closed.RemoveAt(closedIndex); closedIndex = -1; } if (openedIndex == -1 && closedIndex == -1) { opened.Add(new AntAINode { world = neighbor, cost = cost, heuristic = neighbor.Heuristic(aGoal), sum = cost + neighbor.Heuristic(aGoal), action = neighbors[i].name, parent = current.world }); } } } // Failed plan. ReconstructPlan(ref aPlan, closed, current); aPlan.isSuccess = false; if (EventPlanUpdated != null) { EventPlanUpdated(aPlan); } }