public void InterruptionTest()
        {
            var tickProvider = new TestTickSupplier();
            var root         = new BehaviourTreeBuilder <TestBlackboard>()
                               .Sequence("Sequence")
                               .Inverter("Inverter")
                               .Do("Reach limit", TestActions.ReachLimitAction)
                               .End()
                               .Wait("Wait 5 ticks", 5, tickProvider)
                               .End()
                               .Build();
            var metadata   = new BehaviourTreeMetadata <TestBlackboard>(root);
            var data       = metadata.CreateExecutionData();
            var executor   = new BehaviourTreeExecutor <TestBlackboard>(root);
            var blackboard = new TestBlackboard();

            executor.Start(data);

            for (var i = 0; i < 5; i++)
            {
                executor.Tick(data, blackboard);
                Assert.AreEqual(BehaviourTreeStatus.Running, data.Statuses[root.Id]);
                Assert.AreEqual(1, blackboard.Counter1);
                tickProvider.Tick();
            }

            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Success, data.Statuses[root.Id]);
            Assert.AreEqual(1, blackboard.Counter1);
            Assert.AreEqual(0, data.Stack.Count);
        }
        public void RepeatUntilStatusReachedNodeTest()
        {
            var root = new BehaviourTreeBuilder <TestBlackboard>()
                       .UntilSucceess("Repeat until success")
                       .Do("Reach limit", TestActions.ReachLimitAction)
                       .End()
                       .Build();
            var metadata   = new BehaviourTreeMetadata <TestBlackboard>(root);
            var data       = metadata.CreateExecutionData();
            var executor   = new BehaviourTreeExecutor <TestBlackboard>(root);
            var blackboard = new TestBlackboard();

            executor.Start(data);

            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Running, data.Statuses[root.Id]);
            Assert.AreEqual(1, blackboard.Counter1);

            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Running, data.Statuses[root.Id]);
            Assert.AreEqual(2, blackboard.Counter1);

            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Success, data.Statuses[root.Id]);
            Assert.AreEqual(3, blackboard.Counter1);
            Assert.AreEqual(0, data.Stack.Count);
        }
        public void OnComplete(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> execution, BehaviourTreeStatus status)
        {
            if (status == BehaviourTreeStatus.Success)
            {
                ++execution.Variables[_succeededCountId];
                if (SucceedPolicy == ParallelPolicy.RequireOne)
                {
                    Stop(executor, execution, BehaviourTreeStatus.Success);
                    return;
                }
            }
            else if (status == BehaviourTreeStatus.Failure)
            {
                ++execution.Variables[_failedCountId];
                if (FailurePolicy == ParallelPolicy.RequireOne)
                {
                    Stop(executor, execution, BehaviourTreeStatus.Failure);
                    return;
                }
            }

            if (FailurePolicy == ParallelPolicy.RequireAll && execution.Variables[_failedCountId] == ChildrenCount)
            {
                Stop(executor, execution, BehaviourTreeStatus.Failure);
            }
            else if (SucceedPolicy == ParallelPolicy.RequireAll && execution.Variables[_succeededCountId] == ChildrenCount)
            {
                Stop(executor, execution, BehaviourTreeStatus.Success);
            }
        }
 private void Stop(BehaviourTreeExecutor <TBlackboard> executor,
                   BehaviourTreeExecutionData <TBlackboard> data,
                   BehaviourTreeStatus status)
 {
     AbortRunningChildren(executor, data);
     executor.Stop(data, this, status);
 }
        public void InvertRunningTest()
        {
            var tickProvider = new TestTickSupplier();
            var root         = new BehaviourTreeBuilder <TestBlackboard>()
                               .Inverter("Invert status")
                               .Wait("Increment counter 1", 3, tickProvider)
                               .End()
                               .Build();
            var metadata   = new BehaviourTreeMetadata <TestBlackboard>(root);
            var data       = metadata.CreateExecutionData();
            var executor   = new BehaviourTreeExecutor <TestBlackboard>(root);
            var blackboard = new TestBlackboard();

            executor.Start(data);

            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Running, data.Statuses[root.Id]);

            tickProvider.Tick();
            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Running, data.Statuses[root.Id]);

            tickProvider.Tick();
            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Running, data.Statuses[root.Id]);

            tickProvider.Tick();
            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Failure, data.Statuses[root.Id]);
            Assert.AreEqual(0, data.Stack.Count);
        }
        public void SelectorRunningTest()
        {
            var tickProvider = new TestTickSupplier();
            var root         = new BehaviourTreeBuilder <TestBlackboard>()
                               .Selector("Sequence")
                               .AlwaysFail("Fail increment")
                               .Do("Increment counter 1", TestActions.IncrementCounter1)
                               .End()
                               .Wait("Wait 1 tick", 1, tickProvider)
                               .Do("Increment counter 2", TestActions.IncrementCounter2)
                               .End()
                               .Build();
            var metadata   = new BehaviourTreeMetadata <TestBlackboard>(root);
            var data       = metadata.CreateExecutionData();
            var executor   = new BehaviourTreeExecutor <TestBlackboard>(root);
            var blackboard = new TestBlackboard();

            executor.Start(data);

            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Running, data.Statuses[root.Id]);
            Assert.AreEqual(1, blackboard.Counter1);
            Assert.AreEqual(0, blackboard.Counter2);

            tickProvider.Tick();
            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Success, data.Statuses[root.Id]);
            Assert.AreEqual(1, blackboard.Counter1);
            Assert.AreEqual(0, blackboard.Counter2);
            Assert.AreEqual(0, data.Stack.Count);
        }
        public override void Initialize(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> data, TBlackboard blackboard)
        {
            data.Variables[_failedCountId]    = 0;
            data.Variables[_succeededCountId] = 0;

            foreach (var node in Children)
            {
                executor.Start(data, node, this);
            }
        }
 public void OnComplete(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> execution, BehaviourTreeStatus status)
 {
     if (status != _status)
     {
         executor.Stop(execution, this, BehaviourTreeStatus.Success);
     }
     else
     {
         executor.Schedule(execution, Node, this);
     }
 }
 public void AbortRunningChildren(BehaviourTreeExecutor <TBlackboard> executor,
                                  BehaviourTreeExecutionData <TBlackboard> data)
 {
     foreach (var node in Children)
     {
         if (node.IsRunning(data))
         {
             executor.Abort(data, node);
         }
     }
 }
        protected override BehaviourTreeStatus Update(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> data, TBlackboard blackboard)
        {
            const int undefined = int.MinValue;
            var       tick      = _tickSupplier.Supply();

            if (data.Variables[_startTickId] == undefined)
            {
                data.Variables[_startTickId] = tick;
            }

            if (tick - data.Variables[_startTickId] < _ticksToWaitSupplier.Supply(blackboard))
            {
                return(BehaviourTreeStatus.Running);
            }

            data.Variables[_startTickId] = undefined;
            return(BehaviourTreeStatus.Success);
        }
        public void ActionTest()
        {
            var root = new BehaviourTreeBuilder <TestBlackboard>()
                       .Do("Increment counter 1", TestActions.IncrementCounter1)
                       .Build();
            var metadata   = new BehaviourTreeMetadata <TestBlackboard>(root);
            var data       = metadata.CreateExecutionData();
            var executor   = new BehaviourTreeExecutor <TestBlackboard>(root);
            var blackboard = new TestBlackboard();

            executor.Start(data);
            executor.Tick(data, blackboard);
            var status = data.Statuses[root.Id];

            Assert.AreEqual(BehaviourTreeStatus.Success, status);
            Assert.AreEqual(1, blackboard.Counter1);
            Assert.AreEqual(0, data.Stack.Count);
        }
Example #12
0
        public void IfNodeFalseTest()
        {
            var root = new SimpleBehaviourTreeBuilder <TestBlackboard>()
                       .If("If false", TestActions.False)
                       .Do("Increment counter 1", TestActions.IncrementCounter1)
                       .End()
                       .Build();
            var metadata   = new BehaviourTreeMetadata <TestBlackboard>(root);
            var data       = metadata.CreateExecutionData();
            var executor   = new BehaviourTreeExecutor <TestBlackboard>(root);
            var blackboard = new TestBlackboard();

            executor.Start(data);
            executor.Tick(data, blackboard);

            var status = data.Statuses[root.Id];

            Assert.AreEqual(BehaviourTreeStatus.Failure, status);
            Assert.AreEqual(0, blackboard.Counter1);
            Assert.AreEqual(0, data.Stack.Count);
        }
        public void OnComplete(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> execution, BehaviourTreeStatus status)
        {
            var node = GetCurrentChild(execution);

            if (execution.Statuses[node.Id] == BehaviourTreeStatus.Success)
            {
                executor.Stop(execution, this, BehaviourTreeStatus.Success);
                return;
            }

            var currentChild = ++execution.Variables[_currentChildId];

            if (currentChild == ChildrenCount)
            {
                executor.Stop(execution, this, BehaviourTreeStatus.Failure);
            }
            else
            {
                executor.Start(execution, GetCurrentChild(execution), this);
            }
        }
Example #14
0
        public void SelectorSuccessTest()
        {
            var root = new SimpleBehaviourTreeBuilder <TestBlackboard>()
                       .Selector("Sequence")
                       .Do("Increment counter 1", TestActions.IncrementCounter1)
                       .Do("Increment counter 2", TestActions.IncrementCounter2)
                       .End()
                       .Build();
            var metadata   = new BehaviourTreeMetadata <TestBlackboard>(root);
            var data       = metadata.CreateExecutionData();
            var executor   = new BehaviourTreeExecutor <TestBlackboard>(root);
            var blackboard = new TestBlackboard();

            executor.Start(data);

            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Success, data.Statuses[root.Id]);
            Assert.AreEqual(1, blackboard.Counter1);
            Assert.AreEqual(0, blackboard.Counter2);
            Assert.AreEqual(0, data.Stack.Count);
        }
        public void WaitNodeTest()
        {
            var tickProvider = new TestTickSupplier();
            var root         = new BehaviourTreeBuilder <TestBlackboard>()
                               .Wait("Increment counter 1", 3, tickProvider)
                               .Build();
            var metadata   = new BehaviourTreeMetadata <TestBlackboard>(root);
            var data       = metadata.CreateExecutionData();
            var executor   = new BehaviourTreeExecutor <TestBlackboard>(root);
            var blackboard = new TestBlackboard();

            executor.Start(data);

            for (var i = 0; i < 3; i++)
            {
                executor.Tick(data, blackboard);
                Assert.AreEqual(BehaviourTreeStatus.Running, data.Statuses[root.Id]);
                tickProvider.Tick();
            }

            executor.Tick(data, blackboard);
            Assert.AreEqual(BehaviourTreeStatus.Success, data.Statuses[root.Id]);
            Assert.AreEqual(0, data.Stack.Count);
        }
 protected override BehaviourTreeStatus Update(BehaviourTreeExecutor <TBlackboard> executor,
                                               BehaviourTreeExecutionData <TBlackboard> data,
                                               TBlackboard blackboard)
 {
     return(BehaviourTreeStatus.Running);
 }
 public override void Initialize(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> data, TBlackboard blackboard)
 {
     data.Variables[_currentChildId] = 0;
     executor.Start(data, GetCurrentChild(data), this);
 }
 public override void Initialize(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> data, TBlackboard blackboard)
 {
     executor.Start(data, Node, this);
 }
 public override void Initialize(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> data, TBlackboard blackboard)
 {
     data.Variables[_startTickId] = int.MinValue;
 }
 public BehaviourTree(BehaviourTreeNode <Entity <TScope> > root)
 {
     _root     = root;
     _metadata = new BehaviourTreeMetadata <Entity <TScope> >(root);
     _executor = new BehaviourTreeExecutor <Entity <TScope> >(root);
 }
 public void OnComplete(BehaviourTreeExecutor <TBlackboard> executor, BehaviourTreeExecutionData <TBlackboard> execution, BehaviourTreeStatus status)
 {
     executor.Stop(execution, this, BehaviourTreeStatus.Failure);
 }