/// <summary> /// Takes a path to a quest file and parses it to a quest object /// </summary> /// <param name="filename">the file path</param> /// <returns>A quest</returns> public static Quest ParseQuest(string filename) { Quest quest = null; Console.WriteLine("Quest file name: \n\t" + filename); using (StreamReader input = new StreamReader(filename)) { string data = input.ReadToEnd(); if (data.Substring(5, 12).Contains("Storyline")) { /* //load individual quests int index, end; index = data.IndexOf("Folder") + 8; end = data.IndexOf('"', index); string folder = data.Substring(index, end - index); storylineFolder = QUEST_DIRECTORY + folder; Storyline newStoryline; string[] files = Directory.GetFiles(storylineFolder); Quest loaded; //loop through all of the files in the directory foreach (string path in files) { //try and parse the quest from each file loaded = ParseQuest(path); //if the parse was successfull, add it to the log if (loaded != null) { newStoryline.Add(loaded); } } */ } else { int index, end; string attribute; //get name index = data.IndexOf("Name:") + 6; end = data.IndexOf('"', index); string name = data.Substring(index, end - index); //get world key index = data.IndexOf("World:") + 7; end = data.IndexOf('"', index); string key = data.Substring(index, end - index); //get the description index = data.IndexOf("Description:") + 13; end = data.IndexOf('"', index); string description = data.Substring(index, end - index); //get objective index = data.IndexOf("Objective:") + 11; end = data.IndexOf('"', index); string objective = data.Substring(index, end - index); //get reward index = data.IndexOf("Reward:"); index = data.IndexOf("Cash:", index) + 5; end = data.IndexOf("\n", index); attribute = data.Substring(index, end - index); int cash; if (!int.TryParse(attribute, out cash)) cash = 0; index = data.IndexOf("Q-Points:", index) + 9; end = data.IndexOf("\n", index); attribute = data.Substring(index, end - index); int qPoints; if (!int.TryParse(attribute, out qPoints)) qPoints = 0; //get start point index = data.IndexOf("Start:"); index = data.IndexOf("X:", index) + 2; end = data.IndexOf("\n", index); attribute = data.Substring(index, end - index); int X; if (!int.TryParse(attribute, out X)) X = 10; index = data.IndexOf("Y:", index) + 2; end = data.IndexOf("\r", index); attribute = data.Substring(index, end - index); int Y; if (!int.TryParse(attribute, out Y)) Y = 10; Vector2 start = new Vector2(X, Y); //get the win condition index = data.IndexOf("Condition:") + 10; end = data.IndexOf("\r", index); attribute = data.Substring(index, end - index); WinCondition condition; //win state switch (attribute) { case "EnemyDies": condition = WinCondition.EnemyDies; break; case "ObtainItem": condition = WinCondition.ObtainItem; break; case "DeliverItem": condition = WinCondition.DeliverItem; break; case "AllEnemiesDead": default: condition = WinCondition.AllEnemiesDead; break; } //get the id of the entity or item that satisfies the quest condition string target = ""; string recipient = ""; switch (condition) { case WinCondition.EnemyDies: case WinCondition.ObtainItem: //get the name of the entity index = data.IndexOf("Target:") + 7; string test = data.Substring(index); end = data.IndexOf('\n', index) - 1; target = data.Substring(index, end - index); break; case WinCondition.DeliverItem: //get the target item index = data.IndexOf("Target:") + 7; target = data.Substring(index); //get destination index = data.IndexOf("Recipient:") + 10; end = data.IndexOf('\n'); recipient = data.Substring(index, index - end); break; } #region Read Enemies //read all of the living entities Dictionary<string, Entity.LivingEntity> livingEntities = new Dictionary<string, Entity.LivingEntity>(); int endEntity = data.Length; FloatRectangle EntityRect; AI.AI ai; //string test = data.Substring(index, 10); index = 1; while((index = data.IndexOf("Living Entity:", index)) > 0) { ai = null; endEntity = data.IndexOf("End Living Enity", index); //get the id index = data.IndexOf("ID:", index) + 3; end = data.IndexOf('\r', index); string id = data.Substring(index, end - index); //get the start position //get start point index = data.IndexOf("Start:", index); index = data.IndexOf("X:", index) + 2; end = data.IndexOf("\n", index); attribute = data.Substring(index, end - index); int sX; if (!int.TryParse(attribute, out sX)) sX = 10; index = data.IndexOf("Y:", index) + 2; end = data.IndexOf("\r", index); attribute = data.Substring(index, end - index); int sY; if (!int.TryParse(attribute, out sY)) sY = 10; //get texture index = data.IndexOf("Texture:", index) + 9; end = data.IndexOf('"', index); string texturePath = data.Substring(index, end - index); GUI.Sprite sprite = GUI.Sprites.spritesDictionary[texturePath]; //create rectangle EntityRect = new FloatRectangle(sX, sY, sprite.Width, sprite.Height); //get the health index = data.IndexOf("Health:", index) + 7; end = data.IndexOf('\n', index); attribute = data.Substring(index, end - index); int health; if(!int.TryParse(attribute, out health)) { health = 0; } livingEntities[id] = new Entity.LivingEntity(EntityRect, sprite, health, null); livingEntities[id].Name = id; //get the ai index = data.IndexOf("AI:", index) + 3; end = data.IndexOf('\n', index) - 1; if (index >= 0) { attribute = data.Substring(index, end - index); switch (attribute) { case "Low": ai = new AI.LowAI(livingEntities[id]); break; case "Mid": ai = new AI.HighAI(livingEntities[id]); break; case "High": ai = new AI.HighAI(livingEntities[id]); break; default: ai = null; break; } livingEntities[id].ai = ai; livingEntities[id].inventory.Add(new Item.Weapon(50, 1, 10, "Bam", 100, 0.5)); livingEntities[id].inventory.ActiveWeapon = 0; } } #endregion #region Read Items Dictionary<string, QuestItem> items = new Dictionary<string,QuestItem>(); index = 0; int endItem = data.Length; while((index = data.IndexOf("Item:", index)) > 0) { endItem = data.IndexOf("End Item", index); //get the id index = data.IndexOf("ID:", index) + 3; end = data.IndexOf('\n', index); string id = data.Substring(index, end - index); //get the texture index = data.IndexOf("Texture:", index) + 9; end = data.IndexOf('"', index); string texturePath = data.Substring(index, end - index); GUI.Sprite sprite = GUI.Sprites.spritesDictionary[texturePath]; //create item items[id] = new QuestItem(id); items[id].Item = new Item.Item(); items[id].Item.previewSprite = sprite; } #endregion //Create the quest quest = new Quest(name, objective, description, start, null, condition, qPoints, cash); quest.worldKey = key; quest.Status = 1; //add the entities to the quests entitiy list foreach (Entity.LivingEntity entity in livingEntities.Values.ToList()) { quest.entitites.Add(entity); } //items too /* Quest needs to be updated foreach(Item.Item item in items.Values.ToArray()) { quest. } */ switch (condition) { case WinCondition.EnemyDies: quest.EnemyToKill = livingEntities[target]; break; case WinCondition.AllEnemiesDead: break; case WinCondition.ObtainItem: quest.FindThis = items[target].Item; break; case WinCondition.DeliverItem: quest.Delivery = items[target].Item; quest.Recipient = livingEntities[recipient]; break; default: break; } } } return quest; }
/// <summary> /// Takes a path to a quest file and parses it to a quest object /// </summary> /// <param name="filename">the file path</param> /// <returns>A quest</returns> public static Quest ParseQuest(string filename) { Quest quest = null; Console.WriteLine("Quest file name: \n\t" + filename); using (StreamReader input = new StreamReader(filename)) { string data = input.ReadToEnd(); if (data.Substring(5, 12).Contains("Storyline")) { /* * //load individual quests * int index, end; * index = data.IndexOf("Folder") + 8; * end = data.IndexOf('"', index); * string folder = data.Substring(index, end - index); * storylineFolder = QUEST_DIRECTORY + folder; * * Storyline newStoryline; * * * string[] files = Directory.GetFiles(storylineFolder); * Quest loaded; * * //loop through all of the files in the directory * foreach (string path in files) * { * //try and parse the quest from each file * loaded = ParseQuest(path); * * //if the parse was successfull, add it to the log * if (loaded != null) * { * newStoryline.Add(loaded); * } * } */ } else { int index, end; string attribute; //get name index = data.IndexOf("Name:") + 6; end = data.IndexOf('"', index); string name = data.Substring(index, end - index); //get world key index = data.IndexOf("World:") + 7; end = data.IndexOf('"', index); string key = data.Substring(index, end - index); //get the description index = data.IndexOf("Description:") + 13; end = data.IndexOf('"', index); string description = data.Substring(index, end - index); //get objective index = data.IndexOf("Objective:") + 11; end = data.IndexOf('"', index); string objective = data.Substring(index, end - index); //get reward index = data.IndexOf("Reward:"); index = data.IndexOf("Cash:", index) + 5; end = data.IndexOf("\n", index); attribute = data.Substring(index, end - index); int cash; if (!int.TryParse(attribute, out cash)) { cash = 0; } index = data.IndexOf("Q-Points:", index) + 9; end = data.IndexOf("\n", index); attribute = data.Substring(index, end - index); int qPoints; if (!int.TryParse(attribute, out qPoints)) { qPoints = 0; } //get start point index = data.IndexOf("Start:"); index = data.IndexOf("X:", index) + 2; end = data.IndexOf("\n", index); attribute = data.Substring(index, end - index); int X; if (!int.TryParse(attribute, out X)) { X = 10; } index = data.IndexOf("Y:", index) + 2; end = data.IndexOf("\r", index); attribute = data.Substring(index, end - index); int Y; if (!int.TryParse(attribute, out Y)) { Y = 10; } Vector2 start = new Vector2(X, Y); //get the win condition index = data.IndexOf("Condition:") + 10; end = data.IndexOf("\r", index); attribute = data.Substring(index, end - index); WinCondition condition; //win state switch (attribute) { case "EnemyDies": condition = WinCondition.EnemyDies; break; case "ObtainItem": condition = WinCondition.ObtainItem; break; case "DeliverItem": condition = WinCondition.DeliverItem; break; case "AllEnemiesDead": default: condition = WinCondition.AllEnemiesDead; break; } //get the id of the entity or item that satisfies the quest condition string target = ""; string recipient = ""; switch (condition) { case WinCondition.EnemyDies: case WinCondition.ObtainItem: //get the name of the entity index = data.IndexOf("Target:") + 7; string test = data.Substring(index); end = data.IndexOf('\n', index) - 1; target = data.Substring(index, end - index); break; case WinCondition.DeliverItem: //get the target item index = data.IndexOf("Target:") + 7; target = data.Substring(index); //get destination index = data.IndexOf("Recipient:") + 10; end = data.IndexOf('\n'); recipient = data.Substring(index, index - end); break; } #region Read Enemies //read all of the living entities Dictionary <string, Entity.LivingEntity> livingEntities = new Dictionary <string, Entity.LivingEntity>(); int endEntity = data.Length; FloatRectangle EntityRect; AI.AI ai; //string test = data.Substring(index, 10); index = 1; while ((index = data.IndexOf("Living Entity:", index)) > 0) { ai = null; endEntity = data.IndexOf("End Living Enity", index); //get the id index = data.IndexOf("ID:", index) + 3; end = data.IndexOf('\r', index); string id = data.Substring(index, end - index); //get the start position //get start point index = data.IndexOf("Start:", index); index = data.IndexOf("X:", index) + 2; end = data.IndexOf("\n", index); attribute = data.Substring(index, end - index); int sX; if (!int.TryParse(attribute, out sX)) { sX = 10; } index = data.IndexOf("Y:", index) + 2; end = data.IndexOf("\r", index); attribute = data.Substring(index, end - index); int sY; if (!int.TryParse(attribute, out sY)) { sY = 10; } //get texture index = data.IndexOf("Texture:", index) + 9; end = data.IndexOf('"', index); string texturePath = data.Substring(index, end - index); GUI.Sprite sprite = GUI.Sprites.spritesDictionary[texturePath]; //create rectangle EntityRect = new FloatRectangle(sX, sY, sprite.Width, sprite.Height); //get the health index = data.IndexOf("Health:", index) + 7; end = data.IndexOf('\n', index); attribute = data.Substring(index, end - index); int health; if (!int.TryParse(attribute, out health)) { health = 0; } livingEntities[id] = new Entity.LivingEntity(EntityRect, sprite, health, null); livingEntities[id].Name = id; //get the ai index = data.IndexOf("AI:", index) + 3; end = data.IndexOf('\n', index) - 1; if (index >= 0) { attribute = data.Substring(index, end - index); switch (attribute) { case "Low": ai = new AI.LowAI(livingEntities[id]); break; case "Mid": ai = new AI.HighAI(livingEntities[id]); break; case "High": ai = new AI.HighAI(livingEntities[id]); break; default: ai = null; break; } livingEntities[id].ai = ai; livingEntities[id].inventory.Add(new Item.Weapon(50, 1, 10, "Bam", 100, 0.5)); livingEntities[id].inventory.ActiveWeapon = 0; } } #endregion #region Read Items Dictionary <string, QuestItem> items = new Dictionary <string, QuestItem>(); index = 0; int endItem = data.Length; while ((index = data.IndexOf("Item:", index)) > 0) { endItem = data.IndexOf("End Item", index); //get the id index = data.IndexOf("ID:", index) + 3; end = data.IndexOf('\n', index); string id = data.Substring(index, end - index); //get the texture index = data.IndexOf("Texture:", index) + 9; end = data.IndexOf('"', index); string texturePath = data.Substring(index, end - index); GUI.Sprite sprite = GUI.Sprites.spritesDictionary[texturePath]; //create item items[id] = new QuestItem(id); items[id].Item = new Item.Item(); items[id].Item.previewSprite = sprite; } #endregion //Create the quest quest = new Quest(name, objective, description, start, null, condition, qPoints, cash); quest.worldKey = key; quest.Status = 1; //add the entities to the quests entitiy list foreach (Entity.LivingEntity entity in livingEntities.Values.ToList()) { quest.entitites.Add(entity); } //items too /* Quest needs to be updated * foreach(Item.Item item in items.Values.ToArray()) * { * quest. * } */ switch (condition) { case WinCondition.EnemyDies: quest.EnemyToKill = livingEntities[target]; break; case WinCondition.AllEnemiesDead: break; case WinCondition.ObtainItem: quest.FindThis = items[target].Item; break; case WinCondition.DeliverItem: quest.Delivery = items[target].Item; quest.Recipient = livingEntities[recipient]; break; default: break; } } } return(quest); }