Exemplo n.º 1
0
        public override IEnumerable <AStarNode> GetNeighbours(AStarNode node)
        {
            //Find unsatisfied goal tokens
            GOAPNode goal    = ((GOAPNode)(node.Goal));
            GOAPNode current = ((GOAPNode)(node.Value));
            IEnumerable <WorldStateToken> unsatisfied = goal.WorldState.Where(x =>
            {
                object value = current.WorldState.GetValue(x.Name);
                if (value != null)
                {
                    return(((bool)value) != x.Value);
                }
                else
                {
                    return(true);
                }
            });

            //Find actions to satisfy them
            foreach (WorldStateToken token in unsatisfied)
            {
                IEnumerable <Action> actions = null;
                try
                {
                    actions = effectActions[token.Name][token.Value];
                }
                catch (KeyNotFoundException keyNotFound)
                {
                    break;
                }
                foreach (Action action in actions)
                {
                    //Modify current and goal states to include the action
                    GOAPNode newState  = (GOAPNode)current.Clone();
                    GOAPNode goalState = (GOAPNode)node.Goal.Clone();
                    newState.LastAction = action;
                    foreach (WorldStateToken effect in action.Effect)
                    {
                        newState.WorldState.SetToken(effect);
                    }
                    foreach (WorldStateToken precondition in action.Precondition)
                    {
                        goalState.WorldState.SetToken(precondition);
                    }
                    //Next
                    yield return(new AStarNode(newState, goalState, action.Cost, this));
                }
            }
        }
Exemplo n.º 2
0
        public override float GetHeuristic(AStarNode node)
        {
            GOAPNode state = ((GOAPNode)node.Value);

            return(state.WorldState.DifferenceFrom(((GOAPNode)(node.Goal)).WorldState));
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            NavMap  map = new NavMap();
            NavNode a   = new NavNode(new Point(0, 0));
            NavNode b   = new NavNode(new Point(2, 2));
            NavNode c   = new NavNode(new Point(4, 4));
            NavNode e   = new NavNode(new Point(6, 4));
            NavNode f   = new NavNode(new Point(6, 0));
            NavNode g   = new NavNode(new Point(6, 6));
            NavNode h   = new NavNode(new Point(3, 0));

            a.Edges.Add(b, 3);
            b.Edges.Add(c, 2.5f);
            b.Edges.Add(h, 4f);
            c.Edges.Add(f, 6);
            f.Edges.Add(g, 7);
            g.Edges.Add(e, 2);
            h.Edges.Add(e, 7);

            AStarEngine engine = new AStarEngine();

            IEnumerable <NavNode> plan = engine.FindSolution(a, e, map).Select(x => ((NavNode)x));

            foreach (NavNode navNode in plan)
            {
                Console.Out.WriteLine("({0}, {1})", navNode.Position.X, navNode.Position.Y);
            }


            GOAPMap goapMap   = new GOAPMap();
            Action  buildFire = new Action("build_fire", new List <WorldStateToken>()
            {
                new WorldStateToken("has_wood", true)
            }, new List <WorldStateToken>()
            {
                new WorldStateToken("has_fire", true)
            }, 10);
            Action cookFood = new Action("cook_food", new List <WorldStateToken>()
            {
                new WorldStateToken("has_fire", true), new WorldStateToken("has_raw_food", true)
            }, new List <WorldStateToken>()
            {
                new WorldStateToken("has_cooked_food", true)
            }, 10);
            Action huntFood = new Action("hunt_food", new List <WorldStateToken>()
            {
                new WorldStateToken("has_weapon", true)
            }, new List <WorldStateToken> {
                new WorldStateToken("has_raw_food", true)
            }, 10);
            Action chopWood = new Action("chop_wood", new List <WorldStateToken>()
            {
            }, new List <WorldStateToken> {
                new WorldStateToken("has_wood", true)
            }, 10);
            Action getWeapon = new Action("get_weapon", new List <WorldStateToken>()
            {
            }, new List <WorldStateToken> {
                new WorldStateToken("has_weapon", true)
            }, 10);
            Action buyCrisps = new Action("buy_crisps", new List <WorldStateToken>()
            {
                new WorldStateToken("has_money", true)
            }, new List <WorldStateToken> {
                new WorldStateToken("has_cooked_food", true)
            }, 20);

            goapMap.AddAction(buildFire);
            goapMap.AddAction(cookFood);
            goapMap.AddAction(huntFood);
            goapMap.AddAction(chopWood);
            goapMap.AddAction(getWeapon);

            GOAPNode   start          = new GOAPNode(new WorldState(), null);
            WorldState goalWorldState = new WorldState();

            goalWorldState.SetToken("has_cooked_food", true);
            GOAPNode goal = new GOAPNode(goalWorldState, null);
            IEnumerable <GOAPNode> actionPlan = engine.FindSolution(start, goal, goapMap).Select(x => ((GOAPNode)x));

            printPlan(actionPlan);

            Console.Out.WriteLine("\nAdd easy option\n");
            goapMap.AddAction(buyCrisps);

            Console.Out.WriteLine("Replanning\n");
            actionPlan = engine.FindSolution(start, goal, goapMap).Select(x => ((GOAPNode)x));

            printPlan(actionPlan);

            start.WorldState.SetToken("has_money", true);

            Console.Out.WriteLine("\nSetting precondition and Replanning\n");
            actionPlan = engine.FindSolution(start, goal, goapMap).Select(x => ((GOAPNode)x));

            printPlan(actionPlan);

            Console.ReadKey();
        }