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 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 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 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 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);
        }
        public void Execute(Entity <TScope> entity)
        {
            var data          = entity.Get <BehaviourTreeData <TScope> >();
            var executionData = data.ExecutionData;

            if (executionData == null)
            {
                executionData = _metadata.CreateExecutionData();
                entity.Replace(new BehaviourTreeData <TScope>(data.BehaviourTree, executionData));
            }

            if (executionData.Stack.Count == 0)
            {
                _executor.Start(executionData, _root);
            }

            _executor.Tick(executionData, entity);
        }
Example #8
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);
        }
Example #9
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 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);
            }
        }
        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);
        }
 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[_currentChildId] = 0;
     executor.Start(data, GetCurrentChild(data), this);
 }