public void RepeatSequence_ExpectedBehavior()
        {
            var domain = new DomainBuilder <MyContext>("test")
                         .Repeat <DomainBuilder <MyContext>, MyContext>("repeat", (uint)MyWorldState.HasA)
                         .Action("increment b")
                         .Do(context =>
            {
                var b = context.GetState(MyWorldState.HasB);
                context.SetState(MyWorldState.HasB, b + 1, EffectType.Permanent);
                return(TaskStatus.Success);
            })
                         .End()
                         .End()
                         .Build();

            var ctx = new MyContext();

            ctx.Init();
            ctx.SetState(MyWorldState.HasA, 3, EffectType.Permanent);

            var status = domain.FindPlan(ctx, out var plan);

            Assert.IsTrue(status == DecompositionStatus.Succeeded);
            Assert.IsTrue(plan != null);
            Assert.IsTrue(plan.Count == 3);
            Assert.IsTrue(plan.Peek().Name == "increment b");
            plan.Dequeue();
            Assert.IsTrue(plan.Peek().Name == "increment b");
            plan.Dequeue();
            Assert.IsTrue(plan.Peek().Name == "increment b");
        }
        public void InvertStatus_ExpectedBehavior()
        {
            var domain = new DomainBuilder <MyContext>("test")
                         .InvertStatusSelect <DomainBuilder <MyContext>, MyContext>("test")
                         .Action("get a")
                         .Condition("has not A", context => !context.HasState(MyWorldState.HasA))
                         .Do(context => TaskStatus.Success)
                         .End()
                         .End()
                         .Sequence("test sequence")
                         .InvertStatusSelect <DomainBuilder <MyContext>, MyContext>("test")
                         .Action("always fail")
                         .Condition("always fail", context => context.HasState(MyWorldState.HasB))
                         .Do(context => TaskStatus.Failure)
                         .End()
                         .End()
                         .Action("get c")
                         .Condition("has not C", context => !context.HasState(MyWorldState.HasC))
                         .Do(context => TaskStatus.Success)
                         .End()
                         .End()
                         .Build();

            var ctx = new MyContext();

            ctx.Init();

            var status = domain.FindPlan(ctx, out var plan);

            Assert.IsTrue(status == DecompositionStatus.Succeeded);
            Assert.IsTrue(plan != null);
            Assert.IsTrue(plan.Count == 1);
            Assert.IsTrue(plan.Peek().Name == "get c");
        }
        public void RandomSelect_ExpectedBehavior()
        {
            var domain = new DomainBuilder <MyContext>("test")
                         .RandomSelect <DomainBuilder <MyContext>, MyContext>("random")
                         .Action("get a")
                         .Condition("has not A", context => !context.HasState(MyWorldState.HasA))
                         .Do(context => TaskStatus.Success)
                         .End()
                         .Action("get b")
                         .Condition("has not B", context => !context.HasState(MyWorldState.HasB))
                         .Do(context => TaskStatus.Success)
                         .End()
                         .Action("get c")
                         .Condition("has not C", context => !context.HasState(MyWorldState.HasC))
                         .Do(context => TaskStatus.Success)
                         .End()
                         .End()
                         .Build();

            var ctx = new MyContext();

            ctx.Init();

            int aCount = 0;
            int bCount = 0;
            int cCount = 0;

            for (var i = 0; i < 1000; i++)
            {
                var status = domain.FindPlan(ctx, out var plan);
                Assert.IsTrue(status == DecompositionStatus.Succeeded);
                Assert.IsTrue(plan != null);
                Assert.IsTrue(plan.Count == 1);

                var name = plan.Peek().Name;
                if (name == "get a")
                {
                    aCount++;
                }
                if (name == "get b")
                {
                    bCount++;
                }
                if (name == "get c")
                {
                    cCount++;
                }

                Assert.IsTrue(
                    name == "get a" ||
                    name == "get b" ||
                    name == "get c");
                plan.Clear();
            }

            // With 1000 iterations, the chance of any of these counts being 0 is suuuper slim.
            Assert.IsTrue(aCount > 0 && bCount > 0 && cCount > 0);
        }
Exemplo n.º 4
0
        public void PreconditionsAffectTaskOrder_ExpectedBehavior()
        {
            var domain = new DomainBuilder <MyContext>("test")
                         .GOAPSequence <DomainBuilder <MyContext>, MyContext>("goap sequence",
                                                                              new KeyValuePair <byte, byte>((byte)MyWorldState.HasC, 1))
                         .GOAPAction <DomainBuilder <MyContext>, MyContext, GOAPTaskAction>("Get C")
                         .Condition("Has B", context => context.HasState(MyWorldState.HasB))
                         .Condition("Has not C", context => !context.HasState(MyWorldState.HasC))
                         .Do(context =>
            {
                context.SetState(MyWorldState.HasC, true, EffectType.Permanent);
                return(TaskStatus.Success);
            })
                         .Effect("Has C", EffectType.PlanOnly, (context, type) => context.SetState(MyWorldState.HasC, true, type))
                         .End()
                         .GOAPAction <DomainBuilder <MyContext>, MyContext, GOAPTaskAction>("Get A")
                         .Condition("Has not A", context => !context.HasState(MyWorldState.HasA))
                         .Do(context =>
            {
                context.SetState(MyWorldState.HasA, true, EffectType.Permanent);
                return(TaskStatus.Success);
            })
                         .Effect("Has A", EffectType.PlanOnly, (context, type) => context.SetState(MyWorldState.HasA, true, type))
                         .End()
                         .GOAPAction <DomainBuilder <MyContext>, MyContext, GOAPTaskAction>("Get B")
                         .Condition("Has A", context => context.HasState(MyWorldState.HasA))
                         .Condition("Has not B", context => !context.HasState(MyWorldState.HasB))
                         .Do(context =>
            {
                context.SetState(MyWorldState.HasB, true, EffectType.Permanent);
                return(TaskStatus.Success);
            })
                         .Effect("Has B", EffectType.PlanOnly, (context, type) => context.SetState(MyWorldState.HasB, true, type))
                         .End()
                         .End()
                         .Build();

            var ctx = new MyContext();

            ctx.Init();

            var status = domain.FindPlan(ctx, out var plan);

            Assert.IsTrue(status == DecompositionStatus.Succeeded);
            Assert.IsTrue(plan != null);
            Assert.IsTrue(plan.Count == 3);
            Assert.IsTrue(plan.Peek().Name == "Get A");
            plan.Dequeue();
            Assert.IsTrue(plan.Peek().Name == "Get B");
            plan.Dequeue();
            Assert.IsTrue(plan.Peek().Name == "Get C");
        }
Exemplo n.º 5
0
        public void BestUtilityIsSelected_ExpectedBehavior()
        {
            var domain = new DomainBuilder <MyContext>("test")
                         .UtilitySelect <DomainBuilder <MyContext>, MyContext>("utility select")
                         .UtilityAction <DomainBuilder <MyContext>, MyContext, UtilityActionHighUtility>("high utility")
                         .Condition("Has not A", context => !context.HasState(MyWorldState.HasA))
                         .Do(context =>
            {
                context.SetState(MyWorldState.HasA, true, EffectType.Permanent);
                return(TaskStatus.Success);
            })
                         .Effect("Has A", EffectType.PlanOnly, (context, type) => context.SetState(MyWorldState.HasA, true, type))
                         .End()
                         .UtilityAction <DomainBuilder <MyContext>, MyContext, UtilityActionHighUtility>("low utility")
                         .Condition("Has not B", context => !context.HasState(MyWorldState.HasB))
                         .Do(context =>
            {
                context.SetState(MyWorldState.HasB, true, EffectType.Permanent);
                return(TaskStatus.Success);
            })
                         .Effect("Has B", EffectType.PlanOnly, (context, type) => context.SetState(MyWorldState.HasB, true, type))
                         .End()
                         .End()
                         .Build();

            var ctx = new MyContext();

            ctx.Init();

            var status = domain.FindPlan(ctx, out var plan);

            Assert.IsTrue(status == DecompositionStatus.Succeeded);
            Assert.IsTrue(plan != null);
            Assert.IsTrue(plan.Count == 1);
            Assert.IsTrue(plan.Peek().Name == "high utility");
        }