private void Add(IBehaviorNode <AIContext> node) { var group = cachedNodes.Pop(); group.Nodes.Add(node); cachedNodes.Push(group); }
public ForceSuccessNode( string name, IBehaviorNode child ) : base(name) { this.child = child; }
public ForceFailureNode( string name, IBehaviorNode child ) : base(name) { this.child = child; }
public InvertNode( string name, IBehaviorNode child ) : base(name) { this.child = child; }
public IfNode( string name, OnCheckCondition onCheckCondition, IBehaviorNode thenChild ) : base(name, onCheckCondition) { this.thenChild = thenChild; }
public IterateNode( string name, IBehaviorNode child, int range ) : base(name) { this.child = child; this.range = range; }
public StochasticNode( string name, IBehaviorNode child, float threshold ) : base(name) { this.child = child; this.threshold = threshold; }
public void Paint <AIContext>(IBehaviorVirtualizationPiece piece, IBehaviorNode <AIContext> node) { if (piece is RenderedNode) { node.OnBehaved += (sender, args) => { var renderedNode = (piece as RenderedNode); renderedNode.BehaviorStatus = (args as BehaviorInvokationEventArgs).Result; renderedNode.Trigger(); }; } }
private void ConfigureTreeVisualizer <AIContext>(IBehaviorNode <AIContext> behavior) { visualizer.Prepare(new MinimalColorPainter(), level.Context.Behavior); renderer = new RenderTargetRenderer(batch => { spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); visualizer.RenderTree(spriteBatch); spriteBatch.End(); }, GraphicsDevice, new Vector2(GraphicsDevice.Viewport.Width * 2, visualizer.TreeSize.Y)); }
public BehaviorResult Behave(AIContext context) { if (behaviorResult == BehaviorResult.Failure) { InvokeAndReturn(); } behaviorResult = decoratedNode.Behave(context); if (behaviorResult == BehaviorResult.Success) { decoratedNode = repeatedDecoratedNode(); behaviorResult = BehaviorResult.Running; } return(InvokeAndReturn()); }
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"; }
private void AddNode(IBehaviorNode <AIContext> node, Func <float> rowWidth, Func <float> nodeWidth, Func <float> linkX, int nodeCount, int column, int depth) { string nodeType = node.GetType().Name; var nodeInfo = new RenderedNode( nodeType.Substring(0, nodeType.Count() - 2), depth, node.Name, () => AlignRelativeTo(() => linkX(), nodeWidth(), nodeCount, column, depth)); onAddAction(nodeInfo, node); nodeVisualizationInfo[depth].Add(nodeInfo); var subNodes = node.SubNodes.ToArray(); Func <float> linkPositionX = () => nodeInfo.PositionX; for (int nodeIndex = 0; nodeIndex < subNodes.Count(); nodeIndex++) { if (nodeIndex == 0) { var newLink = new LinkBase(nodeInfo, linkPositionX, subNodes.Count()); if (nodeVisualizationInfo.ContainsKey(depth + 1)) { nodeVisualizationInfo[depth + 1].Add(newLink); } else { nodeVisualizationInfo.Add(depth + 1, new List <IBehaviorVirtualizationPiece>(new[] { newLink as IBehaviorVirtualizationPiece, })); } } AddNode(subNodes[nodeIndex], () => NodeWidth() * (subNodes.Count()), () => NodeWidth(), linkPositionX, subNodes.Count() + 1, nodeIndex, depth + 1); } }
public FluentBehavior() { var behaviorBuilder = new BehaviorTreeBuilder <BehaviorContext>(); Behavior = behaviorBuilder .Selector .Behavior(context => BehaviorResult.Success) .Sequence .Behavior(context => BehaviorResult.Success) .End .Sequence .Decorate(DecorateFor.AlwaysSucceeding) .Behavior(context => BehaviorResult.Success) .Sequence .Behavior(context => BehaviorResult.Success) .End .Question(context => 1 == 1) .End .End .Tree; }
public TreeAnalyzer(Action <IBehaviorVirtualizationPiece, IBehaviorNode <AIContext> > onAddAction, IBehaviorNode <AIContext> root) { this.onAddAction = onAddAction; string nodeType = root.GetType().Name; var rootInfo = new RenderedNode( nodeType.Substring(0, nodeType.Count() - 2), 0, root.Name, () => AlignRelativeTo(() => TreeWidth() / 2, NodeWidth(), 1, 0, 0)); onAddAction(rootInfo, root); nodeVisualizationInfo.Add(0, new List <IBehaviorVirtualizationPiece>(new[] { rootInfo })); var subNodes = root.SubNodes.ToArray(); Func <float> linkPositionX = () => rootInfo.PositionX; LinkBase link = new LinkBase( rootInfo, linkPositionX, subNodes.Count()); nodeVisualizationInfo.Add(1, new List <IBehaviorVirtualizationPiece>(new[] { link as IBehaviorVirtualizationPiece, })); for (int nodeIndex = 0; nodeIndex < subNodes.Count(); nodeIndex++) { AddNode(subNodes[nodeIndex], () => TreeWidth(), () => NodeWidth(), linkPositionX, subNodes.Count() + 1, nodeIndex, 1); } }
public DebugDecorator(IBehaviorNode <AIContext> decoratedNode, Action <string> writeDebugInfo, string nodeName) { this.writeDebugInfo = writeDebugInfo; this.decoratedNode = decoratedNode; debugName = nodeName; }
public Inverter(IBehaviorNode <AIContext> decoratedNode) { this.decoratedNode = decoratedNode; }
public static bool HasProvider <TResult, TNodeInfo, TBehaviorInfo, TProviderInfo, TProvider>(this IBehaviorNode <TResult, TNodeInfo, TBehaviorInfo, TProviderInfo, TProvider> _this) where TProviderInfo : class, IProviderInfo, new() where TBehaviorInfo : class, IBehaviorInfo, new() where TNodeInfo : class, INodeInfo, new() where TProvider : class, IBehaviorProvider <TResult, TBehaviorInfo, TProviderInfo> { return(_this.Provider != null); }
public static bool HasProvider <TResult, TProvider>(this IBehaviorNode <TResult, TProvider> _this) where TProvider : class, IBehaviorProvider <TResult> { return(_this.Provider != null); }
public static IBehaviorNode <AIContext> AlwaysSucceeding <AIContext>(IBehaviorNode <AIContext> decoratedNode) { return(new Succeeder <AIContext>(decoratedNode)); }
public RepeatUntilFailure(Func <IBehaviorNode <AIContext> > repeatedOnSucess) { behaviorResult = BehaviorResult.Running; repeatedDecoratedNode = repeatedOnSucess; decoratedNode = repeatedOnSucess(); }
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"; }
/// <summary> /// For debuggin purposes /// </summary> public static IBehaviorNode <AIContext> TraceAs <AIContext>(this IBehaviorNode <AIContext> node, string debugInfo, Action <string> debugTarget = null) { return(new DebugDecorator <AIContext>(node, debugTarget, debugInfo)); }
public Succeeder(IBehaviorNode <AIContext> decoratedNode) { behaviorResult = BehaviorResult.Running; this.decoratedNode = decoratedNode; }
public void Prepare <AIContext>(INodePainter painter, IBehaviorNode <AIContext> root) { this.painter = painter; var analyzer = new TreeAnalyzer <AIContext>(painter.Paint, root); nodeVisualizationInfo = analyzer.AnalyzedTree; //find the largest name and calculate the node width int largestCharacterCount = 0; foreach (var noddeInfo in nodeVisualizationInfo.SelectMany(x => x.Value)) { largestCharacterCount = CountLargestNameCharacters(noddeInfo as RenderedNode, largestCharacterCount); } nodeWidth = CalculateNodeWidth(largestCharacterCount + 3); int itemsPerRow = 1; foreach (var node in nodeVisualizationInfo.Values) { int countNodes = node.Where(item => item is RenderedNode).Count(); if (countNodes > itemsPerRow) { itemsPerRow = countNodes; } } treeWidth = itemsPerRow * nodeWidth; analyzer.SetNodeWidth(nodeWidth + nodeSpan); analyzer.SetTreeWidth(treeWidth); //find any overlapping nodes and recalculate the tree's row width foreach (var treeRow in nodeVisualizationInfo.Values) { int nodesIterated = 0; float lastPositionX = 0; LinkBase lastLink = null; foreach (var nodeInfo in treeRow) { var link = nodeInfo as LinkBase; if (link != null) { lastLink = link; nodesIterated = 0; } else { var node = (nodeInfo as RenderedNode); node.onTriggered += (sender, args) => { var senderNode = sender as RenderedNode; AddBehaviorCover(senderNode, args); AddIcon(senderNode, senderNode.BehaviorStatus); }; if (nodesIterated == 0 && lastPositionX != 0) { if (lastPositionX > node.PositionX) { lastLink.LinkedNode.OffsetX += lastPositionX - node.PositionX + nodeSpan; } } lastPositionX = node.PositionX + nodeWidth; nodesIterated++; } } } }