private void gameStateUpdateHelper(InteractionTreeNode node) { if ((node.eventType & Events.Quest) > 0) { Quest.addQuestState(node.questID, node.questState); } if ((node.eventType & Events.Reputation) > 0) { Hero.instance.shiftReputation(node.repClique, node.repChange); } if ((node.eventType & Events.Inventory) > 0) { if (node.itemChange > 0) Hero.instance.inventory.addItem(node.item, node.itemChange); else Hero.instance.inventory.removeItem(node.item, -1 * node.itemChange); } if ((node.eventType & Events.Special) > 0) { DialogueEventParser.parseEvent(node.specialEvent); } if ((node.eventType & Events.End) > 0 || (node.eventType & Events.NextLine) == 0) { end = true; } if ((node.eventType & Events.NextLine) > 0) { current = interaction.dialogue.ElementAtOrDefault(node.nextLine - 1) ?? defaultNode; } }
public Interaction(string interactionFile) { if (interactionFile.StartsWith(Directories.INTERACTIONS)) interactionFile = interactionFile.Substring(Directories.INTERACTIONS.Length); string[] lines = System.IO.File.ReadAllLines(Directories.INTERACTIONS + interactionFile); this.dialogue = new List<InteractionLinkedTreeNode>(); this.conditionalList = new List<InteractionConditional>(); this.name = interactionFile.Split(new char[] { '.' })[0]; this.defaultLine = 0; int lineCounter = 0; foreach (var line in lines) { // ignore empty lines if (line.Length == 0) continue; // conditional statements go here // If the statement is true, then the line of dialogue directly below // will be the first line of the conversation if (line.StartsWith("if ")) { string conditional = line.Substring("if ".Length); this.conditionalList.Add(new InteractionConditional(conditional, lineCounter)); } else if (line.StartsWith("default")) { this.defaultLine = lineCounter; } else // otherwise we assume it's a line of dialogue { var interactionNode = new InteractionLinkedTreeNode(); var matches = lineMatcher.Match(line); if (!matches.Success) throw new LineException(string.Format("Bad Line: {0}", line)); var groups = matches.Groups; interactionNode.line = groups["line"].Value; //debug //if (groups["eventargs"].Success) //{ // System.Diagnostics.Debug.WriteLine("Event args:"); // for (int i = 0; i < groups["eventargs"].Captures.Count; ++i) // { // System.Diagnostics.Debug.WriteLine(groups["eventargs"].Captures[i].Value); // } // System.Diagnostics.Debug.WriteLine(""); //} //end debug if (groups["noresponse"].Success) { var eventargline = groups["noresponse"].Captures[0].Value.Trim(); if (eventargline.EndsWith(",")) eventargline = eventargline.Substring(0, eventargline.Length - 1); //System.Diagnostics.Debug.WriteLine(eventargline); var eventargparts = eventargline.Split(','); for (int j = 0; j < eventargparts.Length; ++j) { eventargparts[j] = eventargparts[j].Trim(); if (eventargparts[j].StartsWith("(")) eventargparts[j] = eventargparts[j].Substring(1); if (eventargparts[j].EndsWith(")")) eventargparts[j] = eventargparts[j].Substring(0, eventargparts[j].Length - 1); string[] eventarguments = eventargparts[j].Split(' '); //System.Diagnostics.Debug.WriteLine(eventargparts[j]); switch (eventarguments[0]) { case "End": interactionNode.eventType |= Events.End; break; case "Fight": interactionNode.eventType |= Events.Fight; break; case "Quest": interactionNode.eventType |= Events.Quest; interactionNode.questID = SunsetUtils.parseEnum<QuestID>(eventarguments[1]); interactionNode.questState = SunsetUtils.parseEnum<QuestState>(eventarguments[2]); break; case "Reputation": interactionNode.eventType |= Events.Reputation; interactionNode.repClique = SunsetUtils.parseEnum<Clique>(eventarguments[1]); interactionNode.repChange = Int32.Parse(eventarguments[2]); break; case "Inventory": interactionNode.eventType |= Events.Inventory; interactionNode.item = SunsetUtils.parseEnum<Item>(eventarguments[1]); interactionNode.itemChange = Int32.Parse(eventarguments[2]); break; case "Event": interactionNode.eventType |= Events.Special; interactionNode.specialEvent = eventarguments[1]; break; default: interactionNode.eventType |= Events.NextLine; interactionNode.nextLine = parseNext(eventarguments[0]); break; } } } else if (groups["response"].Success) { interactionNode.eventType = Events.None; for (int i = 0; i < groups["response"].Captures.Count; ++i) { var responseline = groups["response"].Captures[i].Value.Trim(); if (responseline.EndsWith(",")) responseline = responseline.Substring(0, responseline.Length - 1); //System.Diagnostics.Debug.WriteLine(responseline); var responseparts = responseline.Split(new string[] { " -> " }, StringSplitOptions.None); InteractionTreeNode responseNode = new InteractionTreeNode(); responseNode.line = responseparts[0]; string[] responsearguments = responseparts[1].Split(','); for (int j = 0; j < responsearguments.Length; ++j) { responsearguments[j] = responsearguments[j].Trim(); if (responsearguments[j].StartsWith("(")) responsearguments[j] = responsearguments[j].Substring(1); if (responsearguments[j].EndsWith(")")) responsearguments[j] = responsearguments[j].Substring(0, responsearguments[j].Length - 1); string[] eventarguments = responsearguments[j].Split(' '); //System.Diagnostics.Debug.WriteLine(responsearguments[j]); switch (eventarguments[0]) { case "End": responseNode.eventType |= Events.End; responseNode.nextLine = 0; break; case "Fight": responseNode.eventType |= Events.Fight; responseNode.nextLine = 0; break; case "Quest": responseNode.eventType |= Events.Quest; responseNode.questID = SunsetUtils.parseEnum<QuestID>(eventarguments[1]); responseNode.questState = SunsetUtils.parseEnum<QuestState>(eventarguments[2]); break; case "Reputation": responseNode.eventType |= Events.Reputation; responseNode.repClique = SunsetUtils.parseEnum<Clique>(eventarguments[1]); responseNode.repChange = Int32.Parse(eventarguments[2]); break; case "Inventory": responseNode.eventType |= Events.Inventory; responseNode.item = SunsetUtils.parseEnum<Item>(eventarguments[1]); responseNode.itemChange = Int32.Parse(eventarguments[2]); break; case "Event": responseNode.eventType |= Events.Special; responseNode.specialEvent = eventarguments[1]; break; default: responseNode.eventType |= Events.NextLine; responseNode.nextLine = parseNext(eventarguments[0]); break; } } interactionNode.responses.Add(responseNode); } } else throw new LineException(string.Format("No valid continuation: {0}", line)); this.dialogue.Add(interactionNode); lineCounter++; } } }