public void RandomTest() { var blackboard = new TestBlackboard(); var node = new Random <TestBlackboard>(); var tree = BehaviorTreeBuilder <TestBlackboard> .Begin() .Sequence() .Decorator(node).Leaf(new TrackingLeaf()) .Build(blackboard); ushort[] result = new ushort[2]; for (var i = 0; i < 100; i++) { this.UpdateAndStep(tree, 0.1f); if (node.Status == BTTaskStatus.Succeeded) { result[1]++; } else { result[0]++; } } // We should get a decently even spread but we can't rely on it, 50% sway should be plenty enough to have a stable result Assert.Greater(result[0], 25); Assert.Greater(result[1], 25); Assert.AreEqual(100, blackboard.GetTracking(TestBlackboard.TrackingExecution)); }
public void InvertTest() { var blackboard = new TestBlackboard(); var node = new Invert <TestBlackboard>(); var tree = BehaviorTreeBuilder <TestBlackboard> .Begin() .Sequence() .Decorator(node).Leaf(new TrackingLeaf()) .Build(blackboard); this.UpdateAndStep(tree, 0.1f); Assert.AreEqual(BTTaskStatus.Failed, node.Status); Assert.AreEqual(1, blackboard.GetTracking(TestBlackboard.TrackingExecution)); }
// the same tree is here, once with LowerPriority aborts and once with Self aborts and ConditionalDecorators public void BuildLowerPriorityAbortTree() { var builder = BehaviorTreeBuilder <BehaviorTreeMiner> .Begin(this); builder.Selector(); // sleep is most important builder.Sequence(AbortTypes.LowerPriority) .Conditional(m => m.MinerState.Fatigue >= MinerState.MaxFatigue) .LogAction("--- tired! gotta go home") .Action(m => m.GoToLocation(MinerState.Location.Home)) .LogAction("--- prep me my bed!") .Action(m => m.Sleep()) .EndComposite(); // thirst is next most important builder.Sequence(AbortTypes.LowerPriority) .Conditional(m => m.MinerState.Thirst >= MinerState.MaxThirst) .LogAction("--- thirsty! time for a drink") .Action(m => m.GoToLocation(MinerState.Location.Saloon)) .LogAction("--- get me a drink!") .Action(m => m.Drink()) .EndComposite(); // dropping off gold is next builder.Sequence(AbortTypes.LowerPriority) .Conditional(m => m.MinerState.Gold >= MinerState.MaxGold) .LogAction("--- bags are full! gotta drop this off at the bank.") .Action(m => m.GoToLocation(MinerState.Location.Bank)) .LogAction("--- take me gold!") .Action(m => m.DepositGold()) .EndComposite(); // fetching gold is last builder.Sequence() .Action(m => m.GoToLocation(MinerState.Location.Mine)) .LogAction("--- time to get me some gold!") .Action(m => m.DigForGold()) .EndComposite(); builder.EndComposite(); _tree = builder.Build(); }
public static BehaviorTree <BehaviorTreeMinerState> BuildSelfAbortTree() { var builder = BehaviorTreeBuilder <BehaviorTreeMinerState> .Begin(new BehaviorTreeMinerState()); builder.Selector(AbortTypes.Self); // sleep is most important builder.ConditionalDecorator(m => m.MinerState.Fatigue >= MinerState.MAX_FATIGUE, false); builder.Sequence() .LogAction("--- Tired! Gotta go home") .Action(m => m.GoToLocation(MinerState.Location.Home)) .LogAction("--- Prep me my bed!") .Action(m => m.Sleep()) .EndComposite(); // thirst is next most important builder.ConditionalDecorator(m => m.MinerState.Thirst >= MinerState.MAX_THIRST, false); builder.Sequence() .LogAction("--- Thirsty! Time for a drink") .Action(m => m.GoToLocation(MinerState.Location.Saloon)) .LogAction("--- Get me a drink!") .Action(m => m.Drink()) .EndComposite(); // dropping off gold is next builder.ConditionalDecorator(m => m.MinerState.Gold >= MinerState.MAX_GOLD, false); builder.Sequence() .LogAction("--- Bags are full! Gotta drop this off at the bank.") .Action(m => m.GoToLocation(MinerState.Location.Bank)) .LogAction("--- Take me gold!") .Action(m => m.DepositGold()) .EndComposite(); // fetching gold is last builder.Sequence() .Action(m => m.GoToLocation(MinerState.Location.Mine)) .LogAction("--- Time to get me some gold!") .Action(m => m.DigForGold()) .EndComposite(); builder.EndComposite(); return(builder.Build()); }
public void IntervalTest() { var blackboard = new TestBlackboard(); var tree = BehaviorTreeBuilder <TestBlackboard> .Begin() .Sequence() .Interval().Leaf(new TrackingLeaf()) .Build(blackboard); for (var n = 0; n < 10; n++) { for (var i = 0; i < 9; i++) { this.UpdateAndStep(tree, 0.1f); } Assert.AreEqual(n, blackboard.GetTracking(TestBlackboard.TrackingExecution)); this.UpdateAndStep(tree, 0.1f); Assert.AreEqual(n + 1, blackboard.GetTracking(TestBlackboard.TrackingExecution)); } }