void Execute() { var planner = new ActionPlanner(); planner.Clear(); var scout = planner.CreateAction("Scout"); scout.SetPrecondition ("Alive", true); scout.SetPostcondition("EnemyVisible", true); var approach =planner.CreateAction("Approach"); approach.SetPrecondition ("EnemyVisible", true); approach.SetPostcondition("NearEnemy", true); var aim =planner.CreateAction("Aim"); aim.SetPrecondition ("EnemyVisible", true); aim.SetPrecondition ("WeaponLoaded", true); aim.SetPostcondition("EnemyLinedUp", true); var shoot =planner.CreateAction("Shoot"); shoot.SetPrecondition ("EnemyLinedUp", true); shoot.SetPostcondition("EnemyAlive", false); var load =planner.CreateAction("Load"); load.SetPrecondition ("ArmedWithGun", true); load.SetPostcondition("WeaponLoaded", true); var detonateBomb =planner.CreateAction("DetonateBomb", 5); detonateBomb.SetPrecondition ("ArmedWithBomb", true); detonateBomb.SetPrecondition ("NearEnemy", true); detonateBomb.SetPostcondition("Alive", false); detonateBomb.SetPostcondition("EnemyAlive", false); var flee =planner.CreateAction("Flee"); flee.SetPrecondition ("EnemyVisible", true); flee.SetPostcondition("EnemyVisible", false); Debug.Log(planner.ToString()); var state = new WorldState(); state[planner.StringToAtom("EnemyVisible")] = false; state[planner.StringToAtom("ArmedWithGun")] = true; state[planner.StringToAtom("WeaponLoaded")] = false; state[planner.StringToAtom("EnemyLinedUp")] = false; state[planner.StringToAtom("EnemyAlive")] = true; state[planner.StringToAtom("ArmedWithBomb")] = true; state[planner.StringToAtom("NearEnemy")] = false; state[planner.StringToAtom("Alive")] = true; Debug.LogFormat("State: {0}", state.ToString(planner.Atoms)); var goal = new WorldState(); goal[planner.StringToAtom("EnemyAlive")] = false; Debug.LogFormat("Goal: {0}", goal.ToString(planner.Atoms)); var search = new Fringe(GoapGraph.PlannerHeuristic); var graph = new GoapGraph(planner); var stateNode = new WorldStateNode(graph, state, null); var goalNode = new WorldStateNode(graph, goal, null); var path = search.FindPath(stateNode, goalNode); foreach (WorldStateNode node in path) { Debug.Log(node.State.ToString(planner.Atoms)); } }
public void Execute() { var sb = new StringBuilder(); var planner = new ActionPlanner(); planner.Clear(); foreach (Transform actionViewTransform in actionsContainer.container) { var actionView = actionViewTransform.GetComponent<ActionView>(); if (!actionView) continue; var action = planner.CreateAction(actionView.GetName()); action.Cost = actionView.GetCost(); foreach (var atom in actionView.GetPreconditions()) { action.SetPrecondition(atom.GetName(), atom.GetState()); } foreach (var atom in actionView.GetPostconditions()) { action.SetPostcondition(atom.GetName(), atom.GetState()); } } sb.AppendLine(".:: Action PLanner ::."); sb.AppendLine(planner.ToString()); var state = new WorldState(); foreach (Transform atom in initialStateContainer.container) { var atomView = atom.GetComponent<AtomView>(); if (atomView) state[planner.StringToAtom(atomView.GetName())] = atomView.GetState(); } sb.AppendLine(".:: Initial State ::."); sb.AppendFormat("{0}\n\n", state.ToString(planner.Atoms)); var goal = new WorldState(); foreach (Transform atom in goalContainer.container) { var atomView = atom.GetComponent<AtomView>(); if (atomView) goal[planner.StringToAtom(atomView.GetName())] = atomView.GetState(); } sb.AppendLine(".:: Goal ::."); sb.AppendFormat("{0}\n\n", goal.ToString(planner.Atoms)); var search = new Fringe(GoapGraph.PlannerHeuristic); var graph = new GoapGraph(planner); var stateNode = new WorldStateNode(graph, state, null); var goalNode = new WorldStateNode(graph, goal, null); var path = search.FindPath(stateNode, goalNode); var padding = 0; foreach (WorldStateNode node in path) { if (node.Action == null) continue; padding = System.Math.Max(node.Action.Name.Length, padding); } var format = "{0," + padding + "}: {1}\n"; sb.AppendLine(".:: Action Plan ::."); foreach (WorldStateNode node in path) { sb.AppendFormat(format, node.Action == null ? "" : node.Action.Name, node.State.ToString(planner.Atoms)); } outputText.text = sb.ToString(); }