public BehaviorTree(string xmlPath, MonoBehaviour parent) { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.ConformanceLevel = ConformanceLevel.Fragment; settings.ValidationType = ValidationType.None; this.parent = parent; using(XmlReader reader = XmlReader.Create(xmlPath, settings)) { XDocument doc = XDocument.Load(reader); if (doc.Root.Name.ToString().ToLowerInvariant() != "behavior-tree") throw new XmlException("Behavior tree root node not found"); if (doc.Root.Elements().Count() == 0) throw new XmlException("The behavior tree is empty!"); if (doc.Root.Elements().Count() > 1) throw new XmlException("The behavior tree must have only one node at the top level"); Dictionary<string, MethodInfo> leaves, conditions; LoadParentLeafFunctions(out leaves, out conditions); this.rootNode = ParseXmlElement(doc.Root.Elements().First(), leaves, conditions); } }
public void Add(object data) { if (!(data is IComparable)) return; root = add_internal(root, (IComparable)data); count++; }
private void InitTree(BTNode source,AIDebugerTreeNode desc) { desc.m_NodeData = source; for (int i = 0; i < source.GetChildList().Count; ++i) { InitTree(source.GetChildList()[i], desc.m_ChildList[i]); } }
//Duplicate a node along with all children hierarchy static Node DuplicateBranch(BTNode root, Graph targetGraph){ if (targetGraph == null) return null; var newNode = root.Duplicate(targetGraph); var dupConnections = new List<Connection>(); for (var i = 0; i < root.outConnections.Count; i++) dupConnections.Add( root.outConnections[i].Duplicate(newNode, DuplicateBranch( (BTNode)root.outConnections[i].targetNode, targetGraph) )); newNode.outConnections.Clear(); foreach (var c in dupConnections) newNode.outConnections.Add(c); return newNode; }
private BTNode add_internal(BTNode base_node, IComparable data) { if (base_node == null) return(new BTNode(data)); BTNode node = base_node; bool inserted = false; //do it iteratively. Recursion is performance killer while (!inserted) { if (node.data.CompareTo(data) < 0) { if (node.left == null) { node.left = new BTNode(data); node.left.parent = node; node.left.next = node; inserted = true; } else node = node.left; } else if (node.data.CompareTo(data) > 0) { if (node.right == null) { node.right = new BTNode(data); node.right.parent = node; node.right.next = findNextLargestNode(node); inserted = true; } else node = node.right; } else //equal to insert as smallest in the right subtree { node.right = add_internal(node.right, data); // for the case when right node is null if (node.right.next == null) { node.right.parent = node; node.right.next = findNextLargestNode(node.right); } inserted = true; } } return base_node; }
///Create a new SubTree out of the branch of the provided root node static void MakeNestedSubTree(BTNode root){ if (!UnityEditor.EditorUtility.DisplayDialog("Convert to SubTree", "This will create a new SubTree out of this branch.\nThe SubTree can NOT be unpacked later on.\nAre you sure?", "Yes", "No!")) return; var newNestedNode = root.graph.AddNode<SubTree>(); var newBT = EditorUtils.CreateAsset<BehaviourTree>(true); if (newBT == null) return; newNestedNode.nestedGraph = newBT; newNestedNode.nodePosition = root.nodePosition; for (var i = 0; i < root.inConnections.Count; i++) root.inConnections[i].SetTarget(newNestedNode); root.inConnections.Clear(); newBT.primeNode = DuplicateBranch(root, newBT);; DeleteBranch(root); UnityEditor.AssetDatabase.SaveAssets(); }
public void OnBtnUtilSucClick() { BTNode node = this._instantiate("utlsuccess"); this._scrollView.AddChild(node); }
public BTNode AddChild(BTNode child) { children.Add(child); return child; }
protected Decorator(BTNode child) { this.child = child; }
public DecoratorNode(BehaviourTree T, BTNode c) : base(T) { child = c; }
public void OnBtnSubTreeClick() { BTNode node = this._instantiate("subtree"); this._scrollView.AddChild(node); }
public void Reset() { current = null; }
public BTNode(T value) { this.Value = value; this.Left = null; this.Right = null; }
public void OnBtnRetrunningClick() { BTNode node = this._instantiate("retrunning"); this._scrollView.AddChild(node); }
public void OnBtnCountLimitClick() { BTNode node = this._instantiate("countlimit"); this._scrollView.AddChild(node); }
public void OnBtnPersistWhileClick() { BTNode node = this._instantiate("persistwhile"); this._scrollView.AddChild(node); }
public void OnBtnLoopUntilClick() { BTNode node = this._instantiate("loopuntil"); this._scrollView.AddChild(node); }
public void OnBtnRandomSequenceClick() { BTNode node = this._instantiate("randomsequence"); this._scrollView.AddChild(node); }
private BTNode findSmallestNode(BTNode base_node) { BTNode mover = base_node; while (base_node.left != null) mover = base_node.left; return mover; }
public bool MoveNext() { if (current == null) current = btroot; else current = current.next; if (current == null) return false; return true; }
public BTInverseDecoractor(BTNode node) { childNode = node; }
public void OnBtnUtilFailClick() { BTNode node = this._instantiate("utlfail"); this._scrollView.AddChild(node); }
public ActionDie(BTNode _parent) : base(_parent) { }
///Delete the whole branch of provided root node along with the root node static void DeleteBranch(BTNode root){ var graph = root.graph; foreach ( var node in root.GetAllChildNodesRecursively(true).ToArray() ) graph.RemoveNode(node); }
public FleeDecorator(BTNode WrappedNode, Blackboard bb) : base(WrappedNode, bb) { zBB = (ZombieBB)bb; }
public ActionMoveToPoint(BTNode _parent) : base(_parent) { }
public BTDecoratorNode(BehaviorTree t, BTNode c) : base(t) { Child = c; }
protected override bool IsThisType(BTNode _btNode) { return(_btNode.GetType().IsSubclassOf(typeof(BTCompositeNode))); }
public BTDecorator(BehaviourTree tree, BTNode child) : base(tree) { Child = child; }
public override bool ContainsChild(BTNode child) { throw new System.InvalidOperationException(string.Format("{0} cannot have child connections", this)); }
private void DivideArea(BTNode node) { int w = node.area.width; int h = node.area.height; int division; //1 means vertical, 0 means horizontal if (w < minAreaWidth) { if (h < minAreaHeight) { CreateRoom(node); return; } else { division = 0; } } else if (h < minAreaHeight) { division = 1; } else { division = pseudoRandom.Next(0, 2); } BTNode newLeaf1, newLeaf2; BTArea newArea1, newArea2; if (division == 0) // horizontal bisection { int lowend = node.area.btmLeft.tileY + offset; int highend = node.area.btmLeft.tileY + node.area.height - offset; int divisionHeight = pseudoRandom.Next(lowend, highend); newArea1 = new BTArea(node.area.btmLeft, node.area.width, divisionHeight - node.area.btmLeft.tileY); Tile newTile = new Tile(node.area.btmLeft.tileX, divisionHeight + 1); newArea2 = new BTArea(newTile, node.area.width, node.area.btmLeft.tileY + node.area.height - 1 - divisionHeight); newLeaf1 = new BTNode(newArea1, node); newLeaf2 = new BTNode(newArea2, node); } else // vertical bisection { int lowend = node.area.btmLeft.tileX + offset; int highend = node.area.btmLeft.tileX + node.area.width - offset; int divisionWidth = pseudoRandom.Next(lowend, highend); newArea1 = new BTArea(node.area.btmLeft, divisionWidth - node.area.btmLeft.tileX, node.area.height); Tile newTile = new Tile(divisionWidth + 1, node.area.btmLeft.tileY); newArea2 = new BTArea(newTile, node.area.btmLeft.tileX + node.area.width - 1 - divisionWidth, node.area.height); newLeaf1 = new BTNode(newArea1, node); newLeaf2 = new BTNode(newArea2, node); } node.child1 = newLeaf1; node.child2 = newLeaf2; DivideArea(newLeaf1); DivideArea(newLeaf2); }
public BehaviorTree(BTNode rootNode, MonoBehaviour parent) { this.rootNode = rootNode; this.parent = parent; }
private void CreateRoom(BTNode node) { int roomWidth; int roomHeight; int maxWidth, maxHeight; if (alternativeMinCheck) { if (node.area.width < minRoomWidth && node.area.height < minRoomHeight) { node = null; return; } } else { if (node.area.width < minRoomWidth || node.area.height < minRoomHeight) { node = null; return; } } roomCount++; if (maxRoomWidth > node.area.width) { maxWidth = node.area.width; } else { maxWidth = maxRoomWidth; } if (maxRoomHeight > node.area.height) { maxHeight = node.area.height; } else { maxHeight = maxRoomHeight; } if (node.area.width < minRoomWidth) { roomWidth = node.area.width; } else { roomWidth = pseudoRandom.Next(minRoomWidth, maxWidth + 1); } if (node.area.height < minRoomHeight) { roomHeight = node.area.height; } else { roomHeight = pseudoRandom.Next(minRoomHeight, maxHeight + 1); } int coordX = node.area.btmLeft.tileX + pseudoRandom.Next(0, node.area.width + 1 - roomWidth); int coordY = node.area.btmLeft.tileY + pseudoRandom.Next(0, node.area.height + 1 - roomHeight); BTRoom newRoom = new BTRoom(new Tile(coordX, coordY), roomWidth, roomHeight); node.area.rooms.Add(newRoom); DrawRooms(node.area.rooms); }
private BTNode findNextLargestNode(BTNode base_node) { BTNode mover = base_node; while (mover.parent != null && mover.parent.right == mover) mover = mover.parent; // reached root, base_node must be largest in the tree if (mover.parent == null) return null; if (mover.parent.left == mover) return mover; //should never get here return null; }
private List <BTRoom> ConnectRooms(BTNode node) { if (node.child1 == null && node.child2 == null) { return(node.area.rooms); } if (node.child1 == null) { List <BTRoom> roomComplex = ConnectRooms(node.child2); node.area.rooms.AddRange(roomComplex); return(node.area.rooms); } if (node.child2 == null) { List <BTRoom> roomComplex = ConnectRooms(node.child1); node.area.rooms.AddRange(roomComplex); return(node.area.rooms); } List <BTRoom> roomComplex1 = ConnectRooms(node.child1); List <BTRoom> roomComplex2 = ConnectRooms(node.child2); node.area.rooms.AddRange(roomComplex1); node.area.rooms.AddRange(roomComplex2); List <BTConnection> connectionsList = new List <BTConnection>(); int bestDistance = -1; foreach (BTRoom room1 in roomComplex1) { BTConnection connectionBetweenRooms = null; foreach (BTRoom room2 in roomComplex2) { int tmpBest; BTConnection tmpConnection = GetBestConnection(room1, room2, out tmpBest); if (tmpBest != -1 && (tmpBest < bestDistance || bestDistance == -1)) { bestDistance = tmpBest; connectionBetweenRooms = tmpConnection; } } if (connectionBetweenRooms != null) { connectionsList.Add(connectionBetweenRooms); } bestDistance = -1; } int depth = GetTreeDepth(node); int connectionsAmount = pseudoRandom.Next(1 + depth / 3, 1 + depth / 2); for (int i = 0; i < connectionsAmount; i++) { if (connectionsList.Count == 0) { break; } int index = pseudoRandom.Next(0, connectionsList.Count); BTConnection connection = connectionsList[index]; List <Tile> corridor = CreateCorridorRoom(connection); node.area.rooms.Add(new BTRoom(corridor)); connectionsList.Remove(connection); } return(node.area.rooms); }
public BTEnumerator(BTNode _btroot) { btroot = _btroot; current = null; }
public ActionIdle(BTNode _parent) : base(_parent) { }
public BinaryTree() { root = null; threads = new ArrayList(4); }
public NodeCounter(BTNode child, float maxCount) { this.child = child; this.maxCount = maxCount; count = int.MaxValue; }
public BTNode(IComparable _data) { data = _data; left = null; right = null; parent = null; next = null; }
public RepeaterNode(BehaviourTree T, BTNode child) : base(T, child) { }
public void Clear() { root = null; }
// If you're new to this, take a look at http://www.altdevblogaday.com/2011/02/24/introduction-to-behavior-trees/ public BehaviorTree(BTNode rootNode) { mp_root = rootNode; }
private BTNodeInfo ParseNodeInfo(BTNode node, int level, int indexInParent) { int maxNodeSize = 1; List<BTNodeInfo> infos = new List<BTNodeInfo>(); if (node is BTSimpleParallel) { // simple parallel has a primary child BTSimpleParallel simpleParallel = (BTSimpleParallel) node; List<BTNode> children = simpleParallel.children; children.Insert(0, simpleParallel.primaryChild); if (children.Count > 0) { maxNodeSize = 0; } int i=0; foreach (BTNode child in children) { BTNodeInfo info = ParseNodeInfo(child, level+1, i++); maxNodeSize += info.maxNodeSize; infos.Add(info); } } else if (node is BTComposite) { BTComposite composite = (BTComposite) node; List<BTNode> children = composite.children; if (children.Count > 0) { maxNodeSize = 0; } int i=0; foreach (BTNode child in children) { BTNodeInfo info = ParseNodeInfo(child, level+1, i++); maxNodeSize += info.maxNodeSize; infos.Add(info); } } else if (node is BTDecorator) { BTDecorator decorator = (BTDecorator) node; if (decorator.child != null) { BTNodeInfo info = ParseNodeInfo(decorator.child, level+1, 0); if (info.maxNodeSize > maxNodeSize) { maxNodeSize = info.maxNodeSize; } infos.Add(info); } } int countInLevel; _levelToCount.TryGetValue(level, out countInLevel); _levelToCount[level] = countInLevel + maxNodeSize; return new BTNodeInfo(node, infos, maxNodeSize, level, countInLevel, indexInParent); }
public void OnBtnSuccessClick() { BTNode node = this._instantiate("retsuccess"); this._scrollView.AddChild(node); }
public BinaryTree() { root = null; }
public void OnBtnTaskClick() { BTNode node = this._instantiate("task"); this._scrollView.AddChild(node); }
/// <summary> /// This method is used to add a new numeric value to a referenced instance /// </summary> /// <param name="data"></param> public void Insert(int data) { this.root = this.Insert(data, this.root); }
public ExecuteOnce(BTNode child) : base(child) { }
public BTNodeInfo(BTNode node, List<BTNodeInfo> childrenInfo, int maxNodeSize, int level, int countInLevel, int indexInParent) { this.node = node; this.childrenInfo = childrenInfo; this.maxNodeSize = maxNodeSize; this.level = level; this.countInLevel = countInLevel; this.indexInParent = indexInParent; }
public BTNotNode(BTNode childNode) { this.childNode = childNode; }