changeset 68:10d14a7051d5

Re #50 - Complete core loading of WarFoundry XML files * Start to restructure loading so that we can use pre-existing objects * Break unit loading in to methods Also: * Pad stats list with nulls because setting capacity doesn't let you set arbitrary indexes * Add GameSystem property to UnitType
author IBBoard <dev@ibboard.co.uk>
date Sat, 25 Apr 2009 19:18:11 +0000
parents e6200220ece3
children 91cf8efbea0b
files api/Factories/Xml/WarFoundryXmlRaceFactory.cs api/Objects/Stats.cs api/Objects/UnitType.cs
diffstat 3 files changed, 123 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Apr 25 14:59:23 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Apr 25 19:18:11 2009 +0000
@@ -59,38 +59,84 @@
 			race.SetAsLoading();			
 			XmlDocument extraData = GetExtraData(race);
 			
-			foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:units/race:unit"))
-			{
-				UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
-				race.AddUnitType(type);
-			}
-			
 			foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:categories/cat:cat"))
 			{
-				race.AddCategory(CreateCategoryFromElement(node));
+				CreateCategoryFromElement(node, race);
 			}
 							
 			foreach (XmlElement node  in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:equipment/cat:equipmentItem"))
 			{
-				EquipmentItem item = CreateEquipmentItemFromElement(node, race);
-				race.AddEquipmentItem(item);
+				CreateEquipmentItemFromElement(node, race);
 			}
 							
 			foreach (XmlElement node  in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:abilities/cat:ability"))
 			{
-				Ability ability = CreateAbilityFromElement(node, race);
-				race.AddAbility(ability);
+				CreateAbilityFromElement(node, race);
+			}
+			
+			foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:units/race:unit"))
+			{
+				CreateUnitTypeFromElement(node, race);
 			}
 			
 			race.SetAsFullyLoaded();
 			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID);
 		}
+
+		private Category CreateCategoryFromElement(XmlElement elem, Race parentRace)
+		{
+			Category cat = CreateCategoryFromElement(elem);
+			parentRace.AddCategory(cat);
+			return cat;
+		}
+
+		private UnitType GetUnitType(Race race, string id)
+		{
+			UnitType type = race.GetUnitType(id);
+
+			if (type == null)
+			{
+				type = CreateUnitTypeFromElement(GetExtraData(race).GetElementById(id), race);
+			}
+			
+			return type;
+		}
+
+		private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace)
+		{
+			return CreateUnitTypeFromElement(WarFoundryXmlFactoryUtils.SelectSingleElement(doc, "/race:race/race:units/race:unit[id="+id+"]"), parentRace);
+		}
 						
-		private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
+		private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace)
 		{
 			string id = elem.GetAttribute("id");
+			UnitType type = parentRace.GetUnitType(id);
+
+			if (type==null)
+			{
+				type = CreateUnitTypeFromElement(elem, id, parentRace);
+			}
+			
+			return type;
+		}
+
+		private UnitType CreateUnitTypeFromElement(XmlElement elem, string id, Race parentRace)
+		{
 			string name = elem.GetAttribute("typeName");
 			UnitType type = new UnitType(id, name, parentRace);
+			LoadCoreValuesForUnitType(elem, type);
+			LoadEquipmentForUnitType(elem, type);
+			LoadAbilitiesForUnitType(elem, type);
+			LoadContainedUnitsForUnitType(elem, type);
+			LoadRequirementsForUnitType(elem, type);
+			LoadExtraDataForUnitType(elem, type);
+			LoadNotesForUnitType(elem, type);
+			parentRace.AddUnitType(type);
+			return type;
+		}
+
+		private void LoadCoreValuesForUnitType(XmlElement elem, UnitType type)
+		{
 			type.MaxNumber = XmlTools.GetIntValueFromAttribute(elem, "maxNum");
 			type.MinNumber = XmlTools.GetIntValueFromAttribute(elem, "minNum");
 			type.MaxSize = XmlTools.GetIntValueFromAttribute(elem, "maxSize");
@@ -99,19 +145,53 @@
 			type.CostPerTrooper = XmlTools.GetIntValueFromAttribute(elem, "points");
 			type.BaseUnitCost = XmlTools.GetIntValueFromAttribute(elem, "unitPoints");
 			string mainCatID = elem.GetAttribute("cat");
-			Category cat = parentRace.GetCategory(mainCatID);
+			Category cat = type.Race.GetCategory(mainCatID);
 			
 			if (cat == null)
 			{
-				throw new InvalidDataException(String.Format("Attribute 'cat' of UnitType {0} (value: {1}) did not reference a valid category", id, mainCatID));
+				throw new InvalidDataException(String.Format("Attribute 'cat' of UnitType {0} (value: {1}) did not reference a valid category", type.ID, mainCatID));
 			}
 			
 			type.MainCategory = cat;
 			XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "/race:race/race:units/race:unit/race:stats");
-			type.UnitStats = ParseUnitStats(statsElement, system);
-			//TODO: Add unit requirements
-			LogNotifier.Debug(GetType(), "Loaded "+type.Name);
-			return type;
+			type.UnitStats = ParseUnitStats(statsElement, type.GameSystem);
+		}
+
+		private void LoadEquipmentForUnitType(XmlElement elem, UnitType type)
+		{
+		}
+		
+		private void LoadAbilitiesForUnitType(XmlElement elem, UnitType type)
+		{
+			foreach (XmlElement ability in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitAbilities/race:unitAbility"))
+			{
+				string id = ability.GetAttribute("abilityID");
+				bool required = XmlTools.GetBoolValueFromAttribute(ability, "required");
+				type.AddAbility(type.Race.GetAbility(id), required);
+			}
+		}
+		
+		private void LoadContainedUnitsForUnitType(XmlElement elem, UnitType type)
+		{
+		}
+
+		private void LoadRequirementsForUnitType(XmlElement elem, UnitType type)
+		{
+			//TODO: Load requirements
+		}
+		
+		private void LoadExtraDataForUnitType(XmlElement elem, UnitType type)
+		{
+		}
+		
+		private void LoadNotesForUnitType(XmlElement elem, UnitType type)
+		{
+			XmlNode node = WarFoundryXmlFactoryUtils.SelectSingleNode(elem, "race:notes");
+
+			if (node!=null)
+			{
+				type.Notes = node.InnerText;
+			}
 		}
 		
 		private Stats ParseUnitStats(XmlElement elem, GameSystem system)
@@ -142,7 +222,20 @@
 		private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race)
 		{
 			string id = elem.GetAttribute("id");
+			EquipmentItem item = race.GetEquipmentItem(id);
+
+			if (item==null)
+			{
+				item = CreateEquipmentItemFromElement(elem, id, race);
+			}
+			
+			return item;
+		}
+
+		private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, string id, Race race)
+		{
 			string name = elem.GetAttribute("name");
+			EquipmentItem item = new EquipmentItem(id, name, race);
 			double cost = 0;
 			ArmourType armourType;
 			
@@ -165,7 +258,6 @@
 			}
 			
 			//TODO: Parse equipment stats if there are any
-			EquipmentItem item = new EquipmentItem(id, name, race);
 			item.Cost = cost;
 			item.ItemArmourType = armourType;
 			
--- a/api/Objects/Stats.cs	Sat Apr 25 14:59:23 2009 +0000
+++ b/api/Objects/Stats.cs	Sat Apr 25 19:18:11 2009 +0000
@@ -19,7 +19,13 @@
 		public Stats(SystemStats systemStats)
 		{
 			sysStats = systemStats;
-			stats = new List<Stat>(sysStats.SlotCount);
+			int statCount = sysStats.SlotCount;
+			stats = new List<Stat>(statCount);
+
+			for (int i = 0; i < statCount; i++)
+			{
+				stats.Add(null);
+			}
 		}
 		
 		public Stat[] StatsArray
--- a/api/Objects/UnitType.cs	Sat Apr 25 14:59:23 2009 +0000
+++ b/api/Objects/UnitType.cs	Sat Apr 25 19:18:11 2009 +0000
@@ -56,6 +56,11 @@
 			}
 		}
 
+		public GameSystem GameSystem
+		{
+			get { return Race.GameSystem; }
+		}
+		
 		/// <value>
 		/// Gets the <see cref=" Race"/> that this unit belongs to.
 		/// </value>