コード例 #1
0
        public void can_create_a_sequence()
        {
            Init();

            var invokeCount = 0;

            var sequence = testObject
                           .Sequence("some-sequence")
                           .Do("some-action-1", t =>
            {
                ++invokeCount;
                return(Status.Success);
            })
                           .Do("some-action-2", t =>
            {
                ++invokeCount;
                return(Status.Success);
            })
                           .End()
                           .Build();

            Assert.IsType <SequenceNode <TimeData> >(sequence);
            Assert.Equal(Status.Success, sequence.Tick(new TimeData(0)));
            Assert.Equal(2, invokeCount);
        }
コード例 #2
0
        void initTree1()
        {
            Console.WriteLine("Initializing Behavior Tree 1");

            btree1 = treeBuilder1
                     .Sequence("Sequence1")
                     .Do(seq1Action1, t =>
            {
                return(actionSuccess(t, seq1Action1));
            })
                     .Do(seq1Action2, t =>
            {
                return(actionSuccess(t, seq1Action2));
            })

                     .Selector("Selector-With-Condition")
                     .Condition(sel1Condition, t => { return(evalActionTrue(t, sel1Condition)); })
                     .Do(sel1Action1, t => { return(actionFail(t, sel1Action1)); })
                     .Do(sel1Action2, t => { return(actionSuccess(t, sel1Action2)); })
                     .Do(sel1Action3, t => { return(actionSuccess(t, sel1Action3)); })


                     .Parallel("Parallel1", 2, 2).
                     Do(par1Action1, t =>
            {
                return(actionSuccess(t, par1Action1));
            })
                     .Do(par1Action2, t =>
            {
                return(actionSuccess(t, par1Action2));
            })
                     .Do(par1Action3, t =>
            {
                return(actionSuccess(t, par1Action3));
            })

                     .Sequence("Sequence2")
                     .Do(seq2Action1, t =>
            {
                return(actionSuccess(t, seq2Action1));
            })
                     .Do(seq2Action2, t =>
            {
                return(actionSuccess(t, seq2Action2));
            })
                     .Do(seq2Action3, t =>
            {
                return(actionSuccess(t, seq2Action3));
            })
                     .End()
                     .Build();
            Console.WriteLine("Finished Buidling Behavior Tree 1 !");
        }
コード例 #3
0
    void Start()
    {
        behaviorController = GetComponent <BehaviorController>();
        var builder = new BehaviourTreeBuilder();

        tree = builder.Sequence("gatherResources").Splice(CreateGatherTreeSequence())
               .End()
               .Build();
    }
コード例 #4
0
        void initTree1()
        {
            Console.WriteLine("Initializing Behavior Tree 1");

            btree1 = treeBuilder2
                     .Sequence("Sequence1")
                     .Do(seq1Action1, t =>
            {
                return(actionSuccess(t, seq1Action1));
            })
                     .Do(seq1Action2, t =>
            {
                return(actionSuccess(t, seq1Action2));
            })
                     .End()
                     .RandomSelector("Random-Selector1-With-Condition")
                     .Condition(sel1Condition, t => { return(evalActionTrue(t, sel1Condition)); })
                     .Do(sel1Action1, t => { return(actionSuccess(t, sel1Action1)); })
                     .Do(sel1Action2, t => { return(actionSuccess(t, sel1Action2)); })
                     .Do(sel1Action3, t => { return(actionSuccess(t, sel1Action3)); })
                     .Do(sel1Action4, t => { return(actionSuccess(t, sel1Action4)); })
                     .Do(sel1Action5, t => { return(actionSuccess(t, sel1Action5)); })
                     .Do(sel1Action6, t => { return(actionSuccess(t, sel1Action6)); })
                     .Do(sel1Action7, t => { return(actionSuccess(t, sel1Action7)); })
                     .Do(sel1Action8, t => { return(actionSuccess(t, sel1Action8)); })
                     .Do(sel1Action9, t => { return(actionSuccess(t, sel1Action9)); })
                     .Do(sel1Action10, t => { return(actionSuccess(t, sel1Action10)); })
                     .Do(sel1Action11, t => { return(actionSuccess(t, sel1Action11)); })
                     .Do(sel1Action12, t => { return(actionSuccess(t, sel1Action12)); })
                     .End()

                     .Selector("Normal-Selector2-With-Condition")
                     .Condition(sel2Condition, t => { return(evalActionTrue(t, sel2Condition)); })
                     .Do(sel2Action1, t => { return(actionFail(t, sel2Action1)); })
                     .Do(sel2Action2, t => { return(actionFail(t, sel2Action2)); })
                     .Do(sel2Action3, t => { return(actionFail(t, sel2Action3)); })
                     .Do(sel2Action4, t => { return(actionFail(t, sel2Action4)); })
                     .Do(sel2Action5, t => { return(actionSuccess(t, sel2Action5)); })
                     .Do(sel2Action6, t => { return(actionSuccess(t, sel2Action6)); })
                     .Do(sel2Action7, t => { return(actionSuccess(t, sel2Action7)); })
                     .Do(sel2Action8, t => { return(actionSuccess(t, sel2Action8)); })
                     .Do(sel2Action9, t => { return(actionSuccess(t, sel2Action9)); })
                     .Do(sel2Action10, t => { return(actionSuccess(t, sel2Action10)); })
                     .Do(sel2Action11, t => { return(actionSuccess(t, sel2Action11)); })
                     .Do(sel2Action12, t => { return(actionSuccess(t, sel2Action12)); })
                     .End()

                     .Build();
            Console.WriteLine("Finished Buidling Behavior Tree 1 !");
        }
コード例 #5
0
    private IBehaviourTreeNode CreateGatherTreeSequence()
    {
        var builder = new BehaviourTreeBuilder();

        return(builder.Sequence("gatherResources")
               .Do("changeColor", t => {
            behaviorController.ChangeColor(Color.magenta);
            return BehaviourTreeStatus.Success;
        })

               .Do("findNearestResource", t =>
        {
            if (behaviorController.needsResource)
            {
                target = behaviorController.GetNextResource();
                behaviorController.ai.target = target;
                behaviorController.hasTarget = true;
                behaviorController.needsResource = false;
                return BehaviourTreeStatus.Success;
            }
            else
            {
                return BehaviourTreeStatus.Success;
            }
        })
               .Do("goToResource", t =>
        {
            while (behaviorController.hasTarget && !behaviorController.ai.DestinationReached() && behaviorController.ai.target.tag == "resource")
            {
                return BehaviourTreeStatus.Failure;      //presumably we pause here
            }
            behaviorController.hasTarget = false;
            behaviorController.needsResource = true;
            return BehaviourTreeStatus.Success;
        })
               .Do("checkIfAllResourcesAreGathered", t => {
            if (behaviorController.allResourcesGathered && behaviorController.ai.DestinationReached())
            {
                return BehaviourTreeStatus.Success;
            }
            else
            {
                Debug.Log("All resouces found");
                return BehaviourTreeStatus.Failure;
            }
        })
               .End()
               .Build());
    }
コード例 #6
0
        public void disabled_parent_nodes_is_ignored()
        {
            TimeData data        = new TimeData(0);
            var      invokeCount = 0;

            var builder = new BehaviourTreeBuilder <TimeData>(new List <int>
            {
                0,
                2,
                4
            });

            builder
            .Sequence("seq1")
            .Do("do1", t =>
            {
                ++invokeCount;
                throw new Exception("do1 invoked.");
            })
            .End()
            .Build()
            .Tick(data);

            builder
            .Selector("sel1")
            .Do("do2", t =>
            {
                ++invokeCount;
                throw new Exception("do2 invoked.");
            })
            .End()
            .Build()
            .Tick(data);

            builder
            .Parallel("para1", 0, 0)
            .Do("do3", t =>
            {
                ++invokeCount;
                throw new Exception("do3 invoked.");
            })
            .End()
            .Build()
            .Tick(data);
        }
コード例 #7
0
        internal void BuildBehaviorTree()
        {
            var builder = new BehaviourTreeBuilder();

            _behavior = builder.Sequence("Transport")
                        .Selector("Transport")
                        .Do("WalkToEndpoint", data =>
            {
                if (!_leftTransport)
                {
                    return(BehaviourTreeStatus.Failure);
                }
                var player    = ObjectManager.Instance.Player;
                var playerPos = player.Position;
                var transport = player.CurrentTransport;
                var loc       = transport == null ? TransportEnd : TransportEnd.GetRelativeToPlayerTransport();
                if (playerPos.GetDistanceTo(loc) >= 1.3f)
                {
                    player.CtmTo(loc);
                    return(BehaviourTreeStatus.Running);
                }
                _arrivedAtEndPoint = true;
                return(BehaviourTreeStatus.Success);
            })
                        .Do("WaitForArrivalAndWalk", data =>
            {
                if (!_onTransport)
                {
                    return(BehaviourTreeStatus.Failure);
                }
                var player    = ObjectManager.Instance.Player;
                var transport = player.CurrentTransport;

                var transportArrived = ObjectManager.Instance.GameObjects.FirstOrDefault(x => x.Position.GetDistanceTo(TransportArrivedState) <= 0.1f) != null;
                if (transport == null)
                {
                    if (transportArrived)
                    {
                        _leftTransport = true;
                    }
                    return(BehaviourTreeStatus.Success);
                }
                if (transportArrived)
                {
                    player.CtmTo(TransportEnd.GetRelativeToPlayerTransport());
                    return(BehaviourTreeStatus.Running);
                }
                player.CtmStopMovement();
                return(BehaviourTreeStatus.Running);
            })
                        .Do("WaitAndWalkOnTransport", data =>
            {
                if (!_arrivedAtWaitPoint)
                {
                    return(BehaviourTreeStatus.Failure);
                }
                var player      = ObjectManager.Instance.Player;
                var myTransport =
                    ObjectManager.Instance.GameObjects.FirstOrDefault(
                        x => x.Position.GetDistanceTo(TransportRestState) <= 0.1f);
                Location loc;
                if (player.TransportGuid == 0)
                {
                    if (myTransport != null)
                    {
                        loc          = TransportMovingPoints[0].GetAbsoluteFromRelativeTransportPosition(myTransport);
                        _movingIndex = 0;
                        player.CtmTo(loc);
                    }
                    else
                    {
                        player.CtmStopMovement();
                    }
                    return(BehaviourTreeStatus.Running);
                }
                var playerPos = player.Position;
                loc           = TransportMovingPoints[_movingIndex];
                if (playerPos.GetDistanceTo(loc) < 1.3)
                {
                    if (_movingIndex >= TransportMovingPoints.Count - 1)
                    {
                        _onTransport = true;
                        return(BehaviourTreeStatus.Success);
                    }
                    _movingIndex++;
                    loc = TransportMovingPoints[_movingIndex];
                }
                player.CtmTo(loc);
                return(BehaviourTreeStatus.Running);
            })
                        .Do("GoToWaitingPoint", data =>
            {
                var player = ObjectManager.Instance.Player;
                if (player.CurrentTransport == null && !_onTransport && !_leftTransport)
                {
                    var pos = player.Position;

                    if (pos.GetDistanceTo(TransportWait) >= 1.3f)
                    {
                        player.CtmTo(TransportWait);
                        return(BehaviourTreeStatus.Running);
                    }
                    _arrivedAtWaitPoint = true;
                    return(BehaviourTreeStatus.Success);
                }
                return(BehaviourTreeStatus.Running);
            })
                        .End()
                        .Build();
        }
コード例 #8
0
        // Constructor
        public FishingLogic(Fisher fisher)
        {
            this.fisher = fisher;
            obj         = new object();
            // Creating an updater instace which will execute LogicPulse each 250ms from the mainthread
            pulse = new MainThread.Updater(LogicPulse, 250);

            // Build the BehaviorTree which executes the logic
            // Sequence: Will jump to the next Do if the current Do returns success
            var builder = new BehaviourTreeBuilder();

            tree = builder.Sequence("Fishing")
                   .Sequence("Fishing")
                   .Do("CheckIngame", data =>
            {
                if (!ObjectManager.Instance.IsIngame)
                {
                    return(BehaviourTreeStatus.Failure);
                }
                var player = ObjectManager.Instance.Player;
                // We are ingame and we find the player = success
                return(player == null ? BehaviourTreeStatus.Failure : BehaviourTreeStatus.Success);
            })
                   .Do("AntiTP", data =>
            {
                var player = ObjectManager.Instance.Player;
                if (player.RealZoneText != startLocation)
                {
                    Console.Beep(800, 2000);
                    Stop();
                    return(BehaviourTreeStatus.Failure);
                }
                return(BehaviourTreeStatus.Success);
            })
                   .Do("CheckLootWindow", data =>
            {
                var player = ObjectManager.Instance.Player;
                // Loot window closed? Continue
                if (player.CurrentLootGuid == 0 || !LootFrame.IsOpen)
                {
                    return(BehaviourTreeStatus.Success);
                }
                // Otherwise try to loot, blacklist the current bobber and keep repeating this Do
                oldBobber = player.CurrentLootGuid;
                if (Wait.For("FishingLootWait2", 500))
                {
                    LootFrame.Instance.LootAll();
                }
                return(BehaviourTreeStatus.Running);
            })
                   .Do("ApplyLure", data =>
            {
                var player = ObjectManager.Instance.Player;
                var lure   = Inventory.Instance.ExistingItems(lures).FirstOrDefault();
                var bobber = ObjectManager.Instance.GameObjects.FirstOrDefault(x => x.OwnedBy == player.Guid && x.Guid != oldBobber);
                if ((player.ChannelingId != 0 && bobber != null) || player.IsMainhandEnchanted() || lure.Name == "")
                {
                    return(BehaviourTreeStatus.Success);
                }
                if (!player.IsMainhandEnchanted() && Wait.For("ApplyingLure", 5500))
                {
                    player.EnchantMainhandItem(lure.Name);
                }
                return(BehaviourTreeStatus.Running);
            })
                   .Do("Cast", data =>
            {
                var player = ObjectManager.Instance.Player;
                // We are channeling something (fishing)? Success!
                var bobber = ObjectManager.Instance.GameObjects.FirstOrDefault(x => x.OwnedBy == player.Guid && x.Guid != oldBobber);
                if (player.ChannelingId != 0 && bobber != null)
                {
                    return(BehaviourTreeStatus.Success);
                }
                // Cast fishing (only each 1000ms tho)
                if (Wait.For("FishingWait", 1000))
                {
                    Spell.Instance.Cast("Fishing");
                }
                ObjectManager.Instance.Player.AntiAfk();
                // Stil running. Only success if we are casting fishing
                return(BehaviourTreeStatus.Running);
            })
                   .Do("WaitForFish", data =>
            {
                var player = ObjectManager.Instance.Player;
                var bobber = ObjectManager.Instance.GameObjects.FirstOrDefault(x => x.OwnedBy == player.Guid && x.Guid != oldBobber);
                // No bobber? Fail
                if (bobber == null)
                {
                    return(BehaviourTreeStatus.Failure);
                }
                // Bobber got no fish? Waiting!
                if (!bobber.IsBobbing)
                {
                    return(BehaviourTreeStatus.Running);
                }
                // Got a fish on bobber? Loot it and add the guid of the bobber to the blacklist
                if (Wait.For("FishingLootWait", 500))
                {
                    bobber.Interact(false);
                }
                return(BehaviourTreeStatus.Success);
            })
                   .End()
                   .Build();
        }
コード例 #9
0
    IBehaviourTreeNode BuildSimpleAI()
    {
        BehaviourTreeBuilder treeBuilder = new BehaviourTreeBuilder();
        IBehaviourTreeNode   node        = treeBuilder
                                           .Sequence("simpleAI", true)
                                           .Do("DoWeHaveUnits", t => {
            if (this.aiData != null && this.aiData.myScoredUnits != null && this.aiData.myScoredUnits.Count > 0)
            {
                return(BehaviourTreeStatus.Success);
            }
            return(BehaviourTreeStatus.Failure);
        })
                                           .Do("SendCommandTo", t => {
            LogicalGrid grid = this.gameManager.GetGrid();

            List <Direction> posibleMoves     = new List <Direction> ();
            SoldierController selectedSoldier = null;
            int tryCount = 0;
            while (selectedSoldier == null && tryCount < 25)
            {
                tryCount++;
                foreach (ScoredSoldier scored in this.aiData.myScoredUnits)
                {
                    posibleMoves.Clear();
                    int rnd = Random.Range(0, 10);
                    if (rnd % 2 == 0)
                    {
                        continue;
                    }

                    List <string> posibleMovesStr = new List <string> ();
                    if (grid.CanMakeMove(scored.Soldier, Direction.UP))
                    {
                        posibleMoves.Add(Direction.UP);
                        posibleMovesStr.Add("Up");
                    }
                    if (grid.CanMakeMove(scored.Soldier, Direction.DOWN))
                    {
                        posibleMoves.Add(Direction.DOWN);
                        posibleMovesStr.Add("Down");
                    }
                    if (grid.CanMakeMove(scored.Soldier, Direction.LEFT))
                    {
                        posibleMoves.Add(Direction.LEFT);
                        posibleMovesStr.Add("Left");
                    }
                    if (grid.CanMakeMove(scored.Soldier, Direction.RIGHT))
                    {
                        posibleMoves.Add(Direction.RIGHT);
                        posibleMovesStr.Add("Right");
                    }

                    Debug.LogFormat("{0}:: posibleMoves: {1}", scored.Soldier.name, string.Join(", ", posibleMovesStr.ToArray()));
                    if (posibleMoves.Count == 0)
                    {
                        //Dam!
                        Debug.LogWarningFormat(scored.Soldier, "{0}[{1}] Has no possible moves", scored.Soldier.name, scored.Soldier.Position);
                        continue;
                    }
                    selectedSoldier = scored.Soldier;
                    break;
                }
                if (selectedSoldier != null)
                {
                    Debug.LogFormat(selectedSoldier, "{0} SelectedSoldier: {1}; moves: {2}", this.name, selectedSoldier.name, posibleMoves.Count);
                    int selectedMove = Random.Range(0, posibleMoves.Count);
                    Debug.LogFormat(this, "{0}[t:{1}].selected: {2} Move: {3}", this.name, this.turnN, selectedSoldier.name, selectedMove);
                    this.gameManager.IssueCommandTo(this.playerId, selectedSoldier, posibleMoves [selectedMove]);
                    return(BehaviourTreeStatus.Success);
                }
                Debug.LogWarningFormat(this, "{0} Cant find soldier with moves", this.name);
            }
            Debug.LogWarningFormat(this, "{0} return Failure", this.name);
            return(BehaviourTreeStatus.Failure);
        })
                                           .End()
                                           .Build();

        return(node);
    }