コード例 #1
0
        private void PlanActions()
        {
            _actions.Clear();
            var worldActions = GetWorldActions();

            _actions = ActionPlanner.ConstructActionPlan(this, Goal, worldActions);
        }
コード例 #2
0
        void Start()
        {
            m_memory        = new Dictionary <string, string>();
            m_actionPlanner = new ActionPlanner();

            goal = new Goal("Goal1", null, "Goal1_Happy", 100);

            m_goals = new List <Goal>();

            //               Action : Name : PreCondition : Effect : Cost
            m_actions = new List <Action>();
//            m_actions.Add(new Action("A1", "A1_Happy", "Goal1_Happy", 2));
//            m_actions.Add(new Action("A2", "A2_Happy", "Goal1_Happy", 2));
//            m_actions.Add(new Action("A3", "", "A1_Happy", 3));
//            m_actions.Add(new Action("A4", "A4_Happy", "A1_Happy", 2));
//            m_actions.Add(new Action("A5", "", "A2_Happy", 8));
//            m_actions.Add(new Action("A6", "A6_Happy", "A2_Happy", 1));
//            m_actions.Add(new Action("A7", "", "A4_Happy", 1));
//            m_actions.Add(new Action("A8", "", "A4_Happy", 4));
//            m_actions.Add(new Action("A9", "", "A6_Happy", 4));
        }
コード例 #3
0
ファイル: Search.cs プロジェクト: garrafote/unity3d-goap
 public WorldStateNode(GoapGraph graph, WorldState state, ActionPlanner.Action action)
 {
     Graph = graph;
     State = state;
     Action = action;
 }
コード例 #4
0
ファイル: Search.cs プロジェクト: garrafote/unity3d-goap
 public GoapGraph(ActionPlanner planner)
 {
     this.planner = planner;
 }
コード例 #5
0
ファイル: GOAPTest.cs プロジェクト: garrafote/unity3d-goap
    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));
        }
    }
コード例 #6
0
    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();
    }