changeset 85:46ad6f478203

Re #50: Complete core loading of WarFoundry XML files * Start loading of UnitEquipmentItems * Fix XPath queries for equipment items and abilities * Allow UnitEquipmentItem to be created without a UnitType * Make adding UnitEquipmentItem to UnitType set UnitType of UnitEquipmentItem * Make loading of abilities and equipment items add the item to the race Also: * Code cleanup (line endings) * Make method to get equipment by ID return null instead of throwing "no such key" exception
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Aug 2009 16:06:25 +0000
parents 2b977962302a
children 129636305ad7
files api/Factories/Xml/WarFoundryXmlRaceFactory.cs api/Objects/Race.cs api/Objects/Unit.cs api/Objects/UnitEquipmentItem.cs api/Objects/UnitType.cs
diffstat 5 files changed, 55 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sun Jul 05 09:50:40 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Aug 01 16:06:25 2009 +0000
@@ -64,12 +64,12 @@
 				CreateCategoryFromElement(node, race);
 			}
 							
-			foreach (XmlElement node  in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:equipment/cat:equipmentItem"))
+			foreach (XmlElement node  in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:equipment/race:equipmentItem"))
 			{
 				CreateEquipmentItemFromElement(node, race);
 			}
 							
-			foreach (XmlElement node  in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:abilities/cat:ability"))
+			foreach (XmlElement node  in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:abilities/race:ability"))
 			{
 				CreateAbilityFromElement(node, race);
 			}
@@ -148,6 +148,32 @@
 
 		private void LoadEquipmentForUnitType(XmlElement elem, UnitType type)
 		{
+			foreach (XmlElement equip in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitEquipment/race:unitEquipmentItem"))
+			{
+													/*	<xs:attribute name="id" type="xs:IDREF" />
+														<xs:attribute name="required" type="xs:boolean" default="false"/>
+														<xs:attribute name="exclusivityGroup" type="xs:string" default=""/>
+														<xs:attribute name="minNum" type="core:nonNegativeOrInfiniteInteger" default="-1"/>
+														<xs:attribute name="maxNum" type="core:nonNegativeOrInfiniteInteger" default="-1"/>
+														<xs:attribute name="minPercentage" type="core:percentage" default="100"/>
+														<xs:attribute name="maxPercentage" type="core:percentage" default="100"/>
+														<xs:attribute name="roundDirection" type="updowntype" default="up"/>
+														<xs:attribute name="costMultiplier" type="core:nonNegativeDouble" default="1"/>
+														<xs:attribute name="costRounding" type="costroundingtype" default="UpToHalf"/>*/
+				string id = equip.GetAttribute("id");
+				EquipmentItem equipItem = type.Race.GetEquipmentItem(id);
+				
+				if (equipItem!=null)
+				{
+					UnitEquipmentItem unitEquipItem = new UnitEquipmentItem(equipItem);
+					type.AddEquipmentItem(unitEquipItem);
+				}
+				else
+				{
+					//TODO: Warn about missing item	
+				}
+			}
+				
 		}
 		
 		private void LoadAbilitiesForUnitType(XmlElement elem, UnitType type)
@@ -227,7 +253,7 @@
 			string id = elem.GetAttribute("id");
 			EquipmentItem item = race.GetEquipmentItem(id);
 
-			if (item==null)
+			if (item == null)
 			{
 				item = CreateEquipmentItemFromElement(elem, id, race);
 			}
@@ -263,7 +289,7 @@
 			//TODO: Parse equipment stats if there are any
 			item.Cost = cost;
 			item.ItemArmourType = armourType;
-			
+			race.AddEquipmentItem(item);			
 			return item;
 		}
 		
@@ -274,6 +300,7 @@
 			Ability ability = new Ability(id, name);
 			XmlNode node = elem.SelectSingleNode("description", WarFoundryXmlFactoryUtils.GetNamespaceManager());
 			ability.Description = (node == null) ? "" : node.InnerText;
+			race.AddAbility(ability);
 			return ability;
 		}
 	}
--- a/api/Objects/Race.cs	Sun Jul 05 09:50:40 2009 +0000
+++ b/api/Objects/Race.cs	Sat Aug 01 16:06:25 2009 +0000
@@ -131,10 +131,10 @@
 			}
 		}
 
-		public  EquipmentItem GetEquipmentItem(string id)
+		public EquipmentItem GetEquipmentItem(string id)
 		{
 			EnsureFullyLoaded();
-			return (EquipmentItem)equipment[id];
+			return DictionaryUtils.GetValue(equipment, id);
 		}
 		
 		public List<EquipmentItem> GetEquipmentList()
--- a/api/Objects/Unit.cs	Sun Jul 05 09:50:40 2009 +0000
+++ b/api/Objects/Unit.cs	Sat Aug 01 16:06:25 2009 +0000
@@ -24,12 +24,12 @@
 		private List<Unit> containedUnits = new List<Unit>();
 		public event DoubleValChangedDelegate PointsValueChanged;
 		public event IntValChangedDelegate UnitSizeChanged;
-		public event DoubleValChangedDelegate UnitEquipmentAmountChanged;
-
-		public Unit(UnitType unitType, ArmyCategory parentArmyCat) : this(unitType, unitType.MinSize, parentArmyCat) { }
-
+		public event DoubleValChangedDelegate UnitEquipmentAmountChanged;
+
+		public Unit(UnitType unitType, ArmyCategory parentArmyCat) : this(unitType, unitType.MinSize, parentArmyCat) { }
+
 		public Unit(UnitType unitType, int startSize, ArmyCategory parentArmyCat)
-		{
+		{
 			Category = parentArmyCat;
 			type = unitType;
 			Size = startSize;
@@ -151,10 +151,10 @@
 		}
 
 		public ArmyCategory Category
-		{
-			get
-			{
-				return cat;
+		{
+			get
+			{
+				return cat;
 			}
 			set { cat = value; }
 		}
--- a/api/Objects/UnitEquipmentItem.cs	Sun Jul 05 09:50:40 2009 +0000
+++ b/api/Objects/UnitEquipmentItem.cs	Sat Aug 01 16:06:25 2009 +0000
@@ -25,10 +25,9 @@
 		private string mutexGroup;
 		private UnitType unitType;
 
-		protected UnitEquipmentItem(EquipmentItem equipmentItem, UnitType equipForType)
+		public UnitEquipmentItem(EquipmentItem equipmentItem)
 		{
 			item = equipmentItem;
-			EquipmentForUnit = equipForType;
 		}
 		
 		public override string Name
@@ -98,6 +97,7 @@
 				if (value != null)
 				{
 					unitType = value;
+					unitType.AddEquipmentItem(this);
 				}
 			}
 		}
@@ -175,6 +175,10 @@
 			{
 				return false;
 			}
+			else if (EquipmentForUnit == null)
+			{
+				return false;
+			}
 			else
 			{
 				//If the number of items in the MutEx group is greater than one then it must be this item plus another
--- a/api/Objects/UnitType.cs	Sun Jul 05 09:50:40 2009 +0000
+++ b/api/Objects/UnitType.cs	Sat Aug 01 16:06:25 2009 +0000
@@ -229,9 +229,13 @@
 		
 		public void AddEquipmentItem(UnitEquipmentItem item)
 		{
-			equipment.Add(item.ID, item);
-			equipmentKeyOrder.Add(item.ID);
-			AddToMutexGroup(item);
+			if (!equipment.ContainsKey(item.ID))
+			{
+				equipment.Add(item.ID, item);
+				equipmentKeyOrder.Add(item.ID);
+				AddToMutexGroup(item);
+				item.EquipmentForUnit = this;
+			}
 		}
 		
 		private void AddToMutexGroup(UnitEquipmentItem item)