private void TableRollWriteXml(TableRoll obj) { writer.WriteStartElement("TableRoll"); writer.WriteAttributeString("TableName", obj.Table.TableName); Utility.WriteAttribute(writer, "Rolls", obj.Rolls, 1); Utility.WriteAttribute(writer, "IgnoreBelow", obj.IgnoreBelow, 0); Utility.WriteAttribute(writer, "IgnoreAbove", obj.IgnoreAbove, int.MaxValue); writer.WriteEndElement(); }
private TableRoll TableRollFromXml(XmlNode node) { TableRoll obj = new TableRoll(); obj.Table = Repository.GetTable(node.Attributes["TableName"].Value); obj.Rolls = Utility.ParseAttribute(node, "Rolls", 1); obj.IgnoreBelow = Utility.ParseAttribute(node, "IgnoreBelow", 0); obj.IgnoreAbove = Utility.ParseAttribute(node, "IgnoreAbove", int.MaxValue); return(obj); }
/// <summary> /// Performs a die roll to generate one or more IResolvers from Item /// </summary> /// <returns></returns> public IResolver Roll() { //GameObjectInstance result = new GameObjectInstance(Item, Dice.Roll() * Multiplier); if (Item is GameObject) { GameObjectInstance item = new GameObjectInstance((GameObject)Item, Dice.Roll() * Multiplier); Logger.Write("... rolled " + item.ToString()); return(item); } else if (Item is TableRoll && IsGrouped) { // Number of dice in Dice determines number of TableRolls performed. // Dice rolled with a single die determines number of occurrences of each TableRoll results. // e.g. if Dice = 4d10+2, 4 TableRolls are performed, and d10+2 is rolled to determine number of occurrences of each result. TableRoll t = (TableRoll)Item; Logger.Write("... performing " + Dice.Dice.ToString() + " rolls on " + t.Table.TableName); ItemList list = new ItemList(); DieRoll singleRoll = new DieRoll(1, Dice.Sides, Dice.Modifier); for (int i = 0; i < Dice.Dice; i++) { IResolver item = Item.Resolve(); if (item != null) { int count = singleRoll.Roll(); Logger.Write("... occurring " + count + " times"); for (int j = 0; j < count; j++) { list.Add(item); } } } return(list); } else { ItemList list = new ItemList(); int count = Dice.Roll() * Multiplier; Logger.Write("... rolled " + Dice.ToString() + ": " + count.ToString()); for (int i = 0; i < count; i++) { list.Add(Item.Resolve()); } return(list); } }
/// <summary> /// Convert a text string to an IResolver /// </summary> /// <param name="itemText"></param> /// <returns></returns> public static IResolver ParseItem(string itemText) { char[] comma = { ',' }; char[] blank = { ' ' }; // if the item is a comma-separated list, parse it as an ItemList string[] items = itemText.Split(comma, StringSplitOptions.RemoveEmptyEntries); if (items.Length > 1) { ItemList itemList = new ItemList(); foreach (string item in items) { IResolver obj = ParseItem(item); if (obj != null) { itemList.Add(obj); } } return(itemList); } // parse an individual item else if (items.Length == 1) { DieRoll dieRoll; GameObject gameObject; string item = items[0]; // ItemRoll string[] tokens = item.Split(blank, StringSplitOptions.RemoveEmptyEntries); if (DieRoll.TryParse(tokens[0], out dieRoll)) { string theRest = item.Substring(tokens[0].Length).Trim(); if (GameObject.TryParse(theRest, out gameObject)) { return(new ItemRoll(gameObject, dieRoll)); } } // GameObjectInstance GameObjectInstance gameObjectInstance; if (GameObjectInstance.TryParse(item, out gameObjectInstance)) { return(gameObjectInstance); } // TableRoll if (item.ToLower().StartsWith("roll")) { TableRoll tableRoll; if (TableRoll.TryParse(item.Remove(0, 5), out tableRoll)) { return(tableRoll); } } // GameObject if (GameObject.TryParse(item, out gameObject)) { return(gameObject); } } return(null); }
public static bool TryParse(string st, out TableRoll outTableRoll) { outTableRoll = new TableRoll(); char[] blank = { ' ' }; string[] words = st.ToLower().Split(blank, StringSplitOptions.RemoveEmptyEntries); int i = 0; int rolls = 1; DieRoll dieRoll = null; if (words[i] == "once") { i++; } else if (words[i] == "twice") { i++; rolls = 2; } else if (int.TryParse(words[i], out rolls)) { i++; } else if (DieRoll.TryParse(words[i], out dieRoll)) { i++; } if (words[i] == "times") { i++; } if (words[i] == "on") { i++; } // get the RollableTable name enclosed in double quotes string remainder = String.Join(" ", words, words.Length - i); Regex regex = new Regex("\"([^\"]*)\""); Match match = regex.Match(remainder); if (match.Success) { int ignoreBelow = 0; int ignoreAbove = int.MaxValue; // scan for ignore options remainder.Remove(0, match.Length); words = remainder.Split(blank, StringSplitOptions.RemoveEmptyEntries); i = 0; if (words[i] == "ignoring" && words[i] == "ignore") { i++; if (words[i] == "results") { i++; } switch (words[i]) { case "above": case ">": case "greater": i++; if (words[i] == "than") { i++; } if (!int.TryParse(words[i], out ignoreAbove)) { return(false); } break; case "below": case "<": case "less": i++; if (words[i] == "than") { i++; } if (!int.TryParse(words[i], out ignoreBelow)) { return(false); } break; } } outTableRoll.Table = Repository.GetTable(match.Value); outTableRoll.Rolls = rolls; outTableRoll.IgnoreAbove = ignoreAbove; outTableRoll.IgnoreBelow = ignoreBelow; } return(true); }