Ejemplo n.º 1
0
        public static Quest[] ReadQuestFromGraphml(XmlDocument doc, string filename)
        {
            if (doc == null)
                throw new NullReferenceException("Cannot load graph from NULL reference!");

            XmlNodeList conNodeList = doc.GetElementsByTagName("node");
            XmlNodeList conEdgeList = doc.GetElementsByTagName("edge");
            Dictionary<string, QuestNode> questNodes = new Dictionary<string, QuestNode>();
            List<QuestNode> conStartNodes = new List<QuestNode>(); // store conversation starts

            /* load nodes aka dialogue */
            QuestNode latestNode;
            foreach (XmlNode node in conNodeList)
            {
                latestNode = new QuestNode();
                latestNode.xmlID = node.Attributes["id"].Value;
                Debug.Assert(!questNodes.ContainsKey(latestNode.xmlID), "Duplicated nodes entries found in XML!");
                questNodes[latestNode.xmlID] = latestNode;

                // get important ATTRIBUTES for this node
                Dictionary<string, string> nodeDic = XmlNodesToDic(node.ChildNodes, "key");
                nodeDic.TryGetValue("label", out latestNode.GetMainGoal().label);

                if (nodeDic.ContainsKey("alignment"))
                    int.TryParse(nodeDic["alignment"], out latestNode.GetMainGoal().alignment);
                if (nodeDic.ContainsKey("isstart"))
                    conStartNodes.Add(latestNode);

                if (nodeDic.ContainsKey("activationevent"))
                {
                    Debug.Log(nodeDic["activationevent"] + ": Node Activation Event found!");
                }

                if (nodeDic.ContainsKey("requirement"))
                {
                    Debug.Log(nodeDic["requirement"] + ": Node requirement found!");
                }

                if (nodeDic.ContainsKey("goaldescs") && nodeDic.ContainsKey("goaltypes") && nodeDic.ContainsKey("goaldatas"))
                    latestNode.AddGoals(ParseGoals(nodeDic["goaldescs"], nodeDic["goaltypes"], nodeDic["goaldatas"]));
            }

            /* load edges aka responses */
            foreach (XmlNode edge in conEdgeList)
            {
                QuestOption option = new QuestOption();
                Dictionary<string, string> edgeDic = XmlNodesToDic(edge.ChildNodes, "key");
                option.nextNode = questNodes[edge.Attributes["target"].Value];   // set target node link
                questNodes[edge.Attributes["source"].Value].AddOption(option, edgeDic.ContainsKey("isside") && edgeDic["isside"].Equals("true"));   // set source node link

                // get important ATTRIBUTES for this edge
                edgeDic.TryGetValue("edgelabel", out option.label);

                if (edgeDic.ContainsKey("alignment"))
                    int.TryParse(edgeDic["alignment"], out option.alignment);

                if (edgeDic.ContainsKey("requirement"))
                {
                    Debug.Log(edgeDic["requirement"] + ": Node requirement found!");
                }

                if (edgeDic.ContainsKey("goaldescs") && edgeDic.ContainsKey("goaltypes") && edgeDic.ContainsKey("goaldatas"))
                    option.AddGoals(ParseGoals(edgeDic["goaldescs"], edgeDic["goaltypes"], edgeDic["goaldatas"]));
            }

            // separate nodes into multiple conversations, if necessary
            Quest[] newConArr = new Quest[(conStartNodes.Count > 0) ? conStartNodes.Count : 1];
            QuestNode[] questNodesArr = new QuestNode[questNodes.Count];
            questNodes.Values.CopyTo(questNodesArr, 0);

            if (conStartNodes.Count == 0)
            {
                Debug.LogError("A valid conversion file should ALWAYS have at least 1 conversation start!");
            }
            else
            {
                for (int i = 0; i < conStartNodes.Count; ++i)
                    newConArr[i] = new Quest(conStartNodes[i], questNodesArr, filename);
            }

            return newConArr;
        }