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 BinaryTree()
 {
     root = null;
     threads = new ArrayList(4);
 }
		///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 BehaviorTree(BTNode rootNode, MonoBehaviour parent)
 {
     this.rootNode = rootNode;
     this.parent = parent;
 }
 public void Clear()
 {
     root = null;
 }
Beispiel #11
0
    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);
    }
Beispiel #12
0
 public BTNode AddChild(BTNode child)
 {
     children.Add(child); return child;
 }
Beispiel #13
0
 // 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;
 }
            public bool MoveNext()
            {
                if (current == null)
                    current = btroot;
                else
                    current = current.next;

                if (current == null)
                    return false;
                return true;
            }
 public BTEnumerator(BTNode _btroot)
 {
     btroot = _btroot;
     current = null;
 }
 private BTNode findSmallestNode(BTNode base_node)
 {
     BTNode mover = base_node;
     while (base_node.left != null)
         mover = base_node.left;
     return mover;
 }
        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;
        }
 public void Reset()
 {
     current = null;
 }
 public BTNode(IComparable _data)
 {
     data = _data;
     left = null;
     right = null;
     parent = null;
     next = null;
 }
Beispiel #20
0
 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;
 }
Beispiel #21
0
 public BTNotNode(BTNode childNode)
 {
     this.childNode = childNode;
 }