Ejemplo n.º 1
0
    public BehaviourTree()
    {
        finished = false;

        // Hard coding of the specific behaviour tree for a student
        // outlined in more detail in the provide diagram

        // intermediate nodes take a parent, action leaf nodes take in the type of action they are

        BNode root = new RepeaterNode(null);

        BNode getadvising = new SequenceNode(root);

        BNode branch = new SelectorNode(getadvising);

        BNode checkdata = new ActionNode(branch, Astar.Actiontype.CHECKDATA);

        BNode randomplaque = new SequenceNode(branch);
        BNode pathplaque   = new ActionNode(randomplaque, Astar.Actiontype.PATHPLAQUE);
        BNode checkplaque  = new ActionNode(randomplaque, Astar.Actiontype.CHECKPLAQUE);

        BNode pathprof3 = new ActionNode(getadvising, Astar.Actiontype.PATHPROF);
        BNode pickprof3 = new ActionNode(getadvising, Astar.Actiontype.PICKPROF);

        BNode randbranch = new RandSelectorNode(getadvising);
        BNode finish     = new ActionNode(randbranch, Astar.Actiontype.FINISH);
        BNode pathrand   = new ActionNode(randbranch, Astar.Actiontype.PATHRAND);

        // current is the node being looked at in the tree at any time
        current = root;
    }
Ejemplo n.º 2
0
        public void Tick_AlwaysReturnsTrue(NodeStatus status)
        {
            var blackboard = this.GetBlackboard();
            var action     = new ActionNode <TestBlackboard>((TestBlackboard bb) =>
            {
                return(status);
            });
            var repeaterNode = new RepeaterNode <TestBlackboard>(action);

            Assert.AreEqual(NodeStatus.Running, repeaterNode.Tick(blackboard));
        }
Ejemplo n.º 3
0
        public async Task RunAsync_ShouldReturnRunningAndTriggerEventWhenCountIsDone()
        {
            // Arrange
            var child = new TestNode();
            var node  = new RepeaterNode <object>("", child, 1);

            // Act
            await node.BeforeRunAsync();

            var result = await node.RunAsync();

            child.TriggerFinishedEvent(ResultType.Succeeded);

            // Assert
            Assert.Equal(ResultType.Running, result);
            Assert.Equal(1, node.RanCount);
        }
Ejemplo n.º 4
0
        public void Repeat()
        {
            var ctx  = new SimpleContext();
            var init = 0;            //DateTime.UtcNow.Ticks % 0xFFFFFF;

            for (int i = 1; i < 128; i++)
            {
                ctx.SetInteger("A" + i, init);
                var chg = new IntegerChangeCmd("A" + i, 1);
                var rep = new RepeaterNode("Repeat", chg, i);
                while (Result.Processing == rep.Tick(ctx))
                {
                }

                Assert.AreEqual(init + i, ctx.GetInteger("A" + i));
            }
        }
Ejemplo n.º 5
0
        public void Until()
        {
            var ctx   = new SimpleContext();
            var items = new Result [] { Result.Error, Result.Failure, Result.Processing, Result.Success, Result.Unknown };

            foreach (var i in items)
            {
                var res = new HashSet <Result> ();
                res.Add(i);
                var rep = new RepeaterNode("Repeat", new AlwaysCmd(i), 10, res);
                var x   = rep.Tick(ctx);
                while (Result.Processing == x)
                {
                    x = rep.Tick(ctx);
                }
                Assert.AreEqual(Result.Success, x);
            }
        }
Ejemplo n.º 6
0
        public async Task RunAsync_ShouldRunChildCountTimes(int count)
        {
            // Arrange
            var child = new Mock <Node <object> >();

            child.Setup(c => c.RunAsync())
            .Returns(new ValueTask <ResultType>(ResultType.Succeeded));

            var node = new RepeaterNode <object>("", child.Object, count);

            // Act
            await node.BeforeRunAsync();

            var result = await node.RunAsync();

            // Assert
            Assert.Equal(ResultType.Succeeded, result);
            Assert.Equal(count, node.RanCount);
        }
        Node AddRepeater(Random r)
        {
            var a = r.Next() % 5;
            var b = new HashSet <Result> ();

            if (r.Next() % 2 == 0)
            {
                var c = r.Next() % 4;
                for (int i = 0; i < c; i++)
                {
                    b.Add(RandomEnum <Result> (r));
                }
            }
            else
            {
                b = null;
            }

            var xx = new RepeaterNode("Repeater", null, a, b);

            AddLeaf(xx, r);
            return(xx);
        }
Ejemplo n.º 8
0
    void Start()
    {
        _behaviorTreeController = new BehaviorTreeController();

        // rootとなるSequencer
        SequencerNode rootNode = new SequencerNode();

        rootNode.name = "rootノード";

        // 出発
        ActionNode departure = new ActionNode();

        departure.name = "出発する";
        departure.SetRunningFunc(() => {
            Debug.LogError("出発");
            return(NodeStatus.SUCCESS);
        });

        // HP確認のDecorator
        DecoratorNode confirmationHp = new DecoratorNode();

        confirmationHp.name = "HP確認するのDecorator";
        confirmationHp.SetConditionFunc(() => {
            return(_myHp >= 100 ? NodeStatus.SUCCESS : NodeStatus.FAILURE);
        });

        // 敵に寄る
        ActionNode enemyApproach = new ActionNode();

        enemyApproach.name = "敵に寄るアクションノード";
        enemyApproach.SetRunningFunc(() => {
            Debug.LogError("敵に寄る");
            return(NodeStatus.SUCCESS);
        });

        // HP確認のDecoratorの子供登録
        confirmationHp.AddChild(enemyApproach);

        // 友達2人を呼ぶParallelNode
        ParallelNode callFriendAB = new ParallelNode();

        callFriendAB.name = "友達2人を呼ぶParallelNode";

        // 友達A
        ActionNode friendA = new ActionNode();

        friendA.name = "友達Aを呼ぶ";
        friendA.SetRunningFunc(() => {
            Debug.LogError("友達A");
            return(NodeStatus.SUCCESS);
        });

        // 友達B
        ActionNode friendB = new ActionNode();

        friendB.name = "友達Bを呼ぶ";
        friendB.SetRunningFunc(() => {
            Debug.LogError("友達B");
            return(NodeStatus.SUCCESS);
        });

        // 友達2人を呼ぶParallelNodeの子供登録
        callFriendAB.AddChild(friendA);
        callFriendAB.AddChild(friendB);

        // スキルを繰り返し行うRepeaterNode
        RepeaterNode skillRepeater = new RepeaterNode();

        skillRepeater.name = "スキルを繰り返し行うRepeaterNode";

        // スキルを選択するSelector
        SelectorNode selectSkill = new SelectorNode();

        selectSkill.name = "スキルを選択するSelector";

        // スキルAの発動を確認するDecorator
        DecoratorNode triggerSkillA = new DecoratorNode();

        triggerSkillA.name = "スキルAの発動を確認するDecorator";
        triggerSkillA.SetConditionFunc(() => {
            int probability = Mathf.Clamp(probabilitySkillA, 0, 100);
            int random      = Random.Range(0, 100);
            return(probability > random ? NodeStatus.SUCCESS : NodeStatus.FAILURE);
        });

        // スキルA
        ActionNode skillA = new ActionNode();

        skillA.name = "skillA";
        skillA.SetRunningFunc(() => {
            Debug.LogError("skillA");
            _enemyHp -= 50;
            return(NodeStatus.SUCCESS);
        });

        // スキルAの発動を確認するDecoratorの子供登録
        triggerSkillA.AddChild(skillA);


        // スキルB
        ActionNode skillB = new ActionNode();

        skillB.name = "skillB";
        skillB.SetRunningFunc(() => {
            Debug.LogError("skillB");
            _enemyHp -= 60;
            return(NodeStatus.SUCCESS);
        });

        // スキルを選択するSelectorの子供登録
        selectSkill.AddChild(triggerSkillA);
        selectSkill.AddChild(skillB);

        // スキルを繰り返し行うRepeaterNodeの子供登録
        skillRepeater._repeatNum = 2;
        skillRepeater.AddChild(selectSkill);

        // 敵の生存を確認するSelector
        SelectorNode enemySurvial = new SelectorNode();

        enemySurvial.name = "敵の生存を確認するSelector";

        // 敵が死んでいるか確認するDecorator
        DecoratorNode enemyDied = new DecoratorNode();

        enemyDied.name = "敵が死んでいるか確認するDecorator";
        enemyDied.SetConditionFunc(() => {
            return(_enemyHp <= 0 ? NodeStatus.SUCCESS : NodeStatus.FAILURE);
        });

        // 敵が死んでいる
        ActionNode died = new ActionNode();

        died.name = "敵が死んでいる";
        died.SetRunningFunc(() => {
            Debug.LogError("End1");
            Debug.LogError("EnemyHp : " + _enemyHp);
            return(NodeStatus.SUCCESS);
        });

        // 敵が死んでいるか確認するDecoratorの子供登録
        enemyDied.AddChild(died);

        // 敵が生きているか確認するDecorator
        DecoratorNode enemyAlive = new DecoratorNode();

        enemyAlive.name = "敵が生きているか確認するDecorator";
        enemyAlive.SetConditionFunc(() => {
            return(_enemyHp > 0 ? NodeStatus.SUCCESS : NodeStatus.FAILURE);
        });

        // 敵が生きている
        ActionNode alive = new ActionNode();

        alive.name = "敵が生きている";
        alive.SetRunningFunc(() => {
            Debug.LogError("End2");
            Debug.LogError("EnemyHp : " + _enemyHp);
            return(NodeStatus.SUCCESS);
        });

        // 敵が生きているか確認するDecoratorの子供登録
        enemyAlive.AddChild(alive);

        // 敵の生存を確認するSelectorの子供登録
        enemySurvial.AddChild(enemyDied);
        enemySurvial.AddChild(enemyAlive);


        // rootノードの子供登録
        rootNode.AddChild(departure);
        rootNode.AddChild(confirmationHp);
        rootNode.AddChild(callFriendAB);
        rootNode.AddChild(skillRepeater);
        rootNode.AddChild(enemySurvial);

        // ツリー実行
        _behaviorTreeController.Initialize(rootNode);
        _behaviorTreeController.OnStart();
    }