Example #1
0
        public LargeBehavior()
        {
            int step = 0;

            var walk = new Behavior <AIContext>(
                context => CheckTarget(step = context.InitialStep, context.Target),
                context => CheckTarget(++step, context.Target));

            walk.Name = "walk";

            var unlockDoor = new Behavior <AIContext>(
                context => context.CanUnlock ? BehaviorResult.Success : BehaviorResult.Failure);

            unlockDoor.Name = "unlock door";

            var breakDoor = new Behavior <AIContext>(
                context => BehaviorResult.Success);

            breakDoor.Name = "break door";

            var closeDoor = new Behavior <AIContext>(
                context => BehaviorResult.Failure);

            closeDoor.Name = "close door";

            var checkIfDoorIsCLosed = new Question <AIContext>(
                context => false);

            checkIfDoorIsCLosed.Name = "is door closed?";

            var lockDoor = new Behavior <AIContext>(
                context => BehaviorResult.Success);

            lockDoor.Name = "lock door";

            var openDoor = new Selector <AIContext>(new[] { unlockDoor, breakDoor });

            openDoor.Name = "open door";

            Behavior      = new Sequence <AIContext>(new[] { walk, openDoor, DecorateFor.AlwaysSucceeding(closeDoor), checkIfDoorIsCLosed, lockDoor });
            Behavior.Name = "go to room";
        }
Example #2
0
        public void WalkAndUnlockDoorBehaviorTest()
        {
            using (var sw = File.CreateText("WalkAndUnlockDoorBehaviorTest.info"))
            {
                int step = 0;

                var walk = new Behavior <AIContext>(
                    context => CheckTarget(step = context.InitialStep, context.Target, sw.WriteLine),
                    context => CheckTarget(++step, context.Target, sw.WriteLine))
                           .TraceAs("Walk Action", sw.WriteLine);

                var unlockDoor = new Behavior <AIContext>(
                    context => context.CanUnlock ? BehaviorResult.Success : BehaviorResult.Failure)
                                 .TraceAs("Unlock Door Action", sw.WriteLine);

                var breakDoor = new Behavior <AIContext>(
                    context => BehaviorResult.Success)
                                .TraceAs("Break Door Action", sw.WriteLine);

                var closeDoor = new Behavior <AIContext>(
                    context => BehaviorResult.Failure)
                                .TraceAs("Close Door Action", sw.WriteLine);

                var openDoor = new Selector <AIContext>(new[] { unlockDoor, breakDoor.TraceAs("BreakDoor") })
                               .TraceAs("Open Door Composite", sw.WriteLine);

                var goToRoom = new Sequence <AIContext>(new[] { walk, openDoor, DecorateFor.AlwaysSucceeding(closeDoor) })
                               .TraceAs("Go To Room Composite", sw.WriteLine);

                var aiContext = new AIContext();

                for (int tick = 0; tick < 50; tick++)
                {
                    goToRoom.Behave(aiContext);
                }
            }
        }
Example #3
0
        public WalkBehavior()
        {
            IBehaviorNode <BehaviorContext> checkNextTile
                = new Question <BehaviorContext>(
                      context => context.Level.NextTile is EmptyTile);

            checkNextTile.Name = "next tile empty?";

            IBehaviorNode <BehaviorContext> foundKey
                = new Question <BehaviorContext>(
                      context => context.Level.Map[context.Level.PlayerPosition + 1] is Key);

            foundKey.Name = "found a key?";

            IBehaviorNode <BehaviorContext> foundDoor
                = new Question <BehaviorContext>(
                      context => context.Level.Map[context.Level.PlayerPosition + 1] is Door);

            foundDoor.Name = "found door?";

            IBehaviorNode <BehaviorContext> doIHaveKey
                = new Question <BehaviorContext>(context => context.Level.HaveKey);

            doIHaveKey.Name = "have a key?";

            IBehaviorNode <BehaviorContext> doIHaveEmptySpace
                = new Question <BehaviorContext>(context => !context.Level.HaveKey);

            doIHaveEmptySpace.Name = "inventory empty?";

            IBehaviorNode <BehaviorContext> pickKeyUp
                = new Behavior <BehaviorContext>(
                      context =>
            {
                context.Level.HaveKey = true;
                context.Level.Map[context.Level.PlayerPosition + 1] = new EmptyTile();
                return(BehaviorResult.Success);
            });

            pickKeyUp.Name = "pick key up";

            IBehaviorNode <BehaviorContext> walk
                = new Behavior <BehaviorContext>(context =>
                                                 context.Level.MovePlayer(1) ?
                                                 BehaviorResult.Success : BehaviorResult.Failure);

            walk.Name = "go 1 step forward";

            IBehaviorNode <BehaviorContext> skipKey
                = new Behavior <BehaviorContext>(context =>
                                                 context.Level.MovePlayer(1) ?
                                                 BehaviorResult.Success : BehaviorResult.Failure);

            skipKey.Name = "walk over key";

            IBehaviorNode <BehaviorContext> unlockDoor
                = new Behavior <BehaviorContext>(context =>
            {
                context.Level.Map[context.Level.PlayerPosition + 1] = new EmptyTile();
                context.Level.HaveKey = false;
                return(BehaviorResult.Success);
            });

            unlockDoor.Name = "unlock door";

            IBehaviorNode <BehaviorContext> goBack
                = new Behavior <BehaviorContext>(context =>
            {
                context.Level.MovePlayer(-1);
                return(BehaviorResult.Success);
            });

            goBack.Name = "go 1 step back";

            IBehaviorNode <BehaviorContext> isPreviousNodeKey
                = new Question <BehaviorContext>(
                      context => context.Level.Map[context.Level.PlayerPosition - 1] is Key);

            isPreviousNodeKey.Name = "found a key?";

            IBehaviorNode <BehaviorContext> pickFoundKeyUp
                = new Behavior <BehaviorContext>(
                      context =>
            {
                context.Level.HaveKey = true;
                context.Level.Map[context.Level.PlayerPosition - 1] = new EmptyTile();
                return(BehaviorResult.Success);
            });

            pickFoundKeyUp.Name = "pick key up";

            var findKeySequence = new Sequence <BehaviorContext>(new[] { goBack, isPreviousNodeKey, pickFoundKeyUp });

            findKeySequence.Name = "look for a key";

            var repeatUntiFoundAKey = DecorateFor.RepeatingUntilSuccess(() =>
            {
                findKeySequence.Reset();
                return(findKeySequence);
            });

            var pickupKeySequence = new Sequence <BehaviorContext>(new[] { doIHaveEmptySpace, pickKeyUp });

            pickupKeySequence.Name = "try pick up";

            var whatToDoWithKey = new Selector <BehaviorContext>(new[] { pickupKeySequence, skipKey });

            whatToDoWithKey.Name = "handle key";

            var keySequence = new Sequence <BehaviorContext>(new[] { foundKey, whatToDoWithKey });

            keySequence.Name = "key obstacle";

            var unlockDoorSequence = new Sequence <BehaviorContext>(new[] { doIHaveKey, unlockDoor });

            unlockDoorSequence.Name = "try unlock";

            var goThroughDoorSelector = new Selector <BehaviorContext>(new[] { unlockDoorSequence, repeatUntiFoundAKey });

            goThroughDoorSelector.Name = "go through";

            var doorSequence = new Sequence <BehaviorContext>(new[] { foundDoor, goThroughDoorSelector });

            doorSequence.Name = "door obstacle";

            var move = new Sequence <BehaviorContext>(new[] { checkNextTile, walk });

            move.Name = "try walk";

            var repeater = DecorateFor.RepeatingUntilFailure(() =>
            {
                move.Reset();
                return(move);
            });

            Behavior      = new Selector <BehaviorContext>(new[] { repeater, keySequence, doorSequence });
            Behavior.Name = "move to end";
        }