Beispiel #1
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 #2
0
    private BTNode LoadBTNode(XmlNode currentNode, AI owner, MacroAIParty party)
    {
        XmlNodeList nodeContent = currentNode.ChildNodes;

        XmlAttributeCollection nodeAttributes = currentNode.Attributes;
        BTNode node = null;

        if (currentNode.Name == "behavior")
        {
            node = LoadBTNode(nodeContent[0], owner, party);
        }
        else if (currentNode.Name == "composite")
        {
            BTComposite compNode = null;
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "Sequence")
            {
                BTSequence seqNode = new BTSequence();
                seqNode.CompNodeType = BTCompType.Sequence;
                compNode             = seqNode;
            }
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "Selector")
            {
                BTSelector selNode = new BTSelector();
                selNode.CompNodeType = BTCompType.Selector;
                compNode             = selNode;
            }
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "ParallelAnd")
            {
                BTParallelAnd parNode = new BTParallelAnd();
                parNode.CompNodeType = BTCompType.ParallelAnd;
                compNode             = parNode;
            }
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "ParallelMain")
            {
                BTParallelMain parNode = new BTParallelMain();
                parNode.CompNodeType = BTCompType.ParallelMain;
                compNode             = parNode;
            }
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "Random")
            {
                BTRandom randNode = new BTRandom();
                randNode.CompNodeType = BTCompType.Random;
                compNode = randNode;
            }
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "Switch")
            {
                BTSwitch swNode = new BTSwitch();
                swNode.CompNodeType = BTCompType.Switch;
                compNode            = swNode;
            }

            compNode.Children = new List <BTNode>();

            foreach (XmlNode nodeItem in nodeContent)
            {
                compNode.Children.Add(LoadBTNode(nodeItem, owner, party));
            }

            node = compNode;
        }
        else if (currentNode.Name == "decorator")
        {
            BTDecorator decNode = null;
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "Repeat")
            {
                BTRepeat repNode = new BTRepeat();
                decNode = repNode;
            }
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "Invert")
            {
                BTInvert invNode = new BTInvert();
                decNode = invNode;
            }
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "UntilFail")
            {
                BTUntilFail repNode = new BTUntilFail();
                decNode = repNode;
            }
            foreach (XmlNode nodeItem in nodeContent)
            {
                decNode.Child = LoadBTNode(nodeItem, owner, party);
            }

            node = decNode;
        }
        else if (currentNode.Name == "leaf")
        {
            XmlNodeList   parameterList = currentNode.ChildNodes;
            List <string> parameters    = new List <string>();
            foreach (XmlNode paramNode in parameterList)
            {
                parameters.Add(paramNode.InnerText);
            }

            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "Check")
            {
                if (nodeAttributes["name"] != null)
                {
                    string  actionName = nodeAttributes["name"].Value;
                    BTCheck checkNode  = new BTCheck();
                    checkNode.Action     = actionName;
                    checkNode.Parameters = parameters;
                    checkNode.MyAI       = owner;
                    checkNode.MyParty    = party;
                    node = checkNode;
                }
            }
            if (nodeAttributes["type"] != null && nodeAttributes["type"].Value == "Action")
            {
                if (nodeAttributes["name"] != null)
                {
                    BTLeaf leafNode = (BTLeaf)System.Activator.CreateInstance(System.Type.GetType("BT" + nodeAttributes["name"].Value));
                    leafNode.Parameters = parameters;
                    leafNode.MyAI       = owner;
                    leafNode.MyParty    = party;
                    node = leafNode;
                }
            }
        }

        return(node);
    }