changeset 64:2094bd0ba652

Re #61 - Complete structure of WarFoundry API objects * Make Unit work with EquipmentItem objects as it doesn't need to know about the limits * Add methods to UnitType to check limits on an EquipmentItem Also: * Remove some unnecessary casting now that we're using genericed lists
author IBBoard <dev@ibboard.co.uk>
date Thu, 09 Apr 2009 15:36:22 +0000
parents a920b1bcb408
children aa2d05a9c635
files api/Objects/CompositeEquipmentItem.cs api/Objects/Unit.cs api/Objects/UnitType.cs
diffstat 3 files changed, 103 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/api/Objects/CompositeEquipmentItem.cs	Thu Apr 09 15:05:38 2009 +0000
+++ b/api/Objects/CompositeEquipmentItem.cs	Thu Apr 09 15:36:22 2009 +0000
@@ -29,7 +29,7 @@
 		public void RemoveItem(EquipmentItem item)
 		{
 			compositeItems.Remove(item);
-			cost-= item.Cost;
+			Cost-= item.Cost;
 		}
 		
 		public EquipmentItem[] Items
--- a/api/Objects/Unit.cs	Thu Apr 09 15:05:38 2009 +0000
+++ b/api/Objects/Unit.cs	Thu Apr 09 15:36:22 2009 +0000
@@ -21,7 +21,7 @@
 		private Race race;
 		private double points;
 		private ArmyCategory cat;
-		private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>();
+		private Dictionary<EquipmentItem, double> equipment = new Dictionary<EquipmentItem, double>();
 		private List<Unit> containedUnits = new List<Unit>();
 		public event DoubleValChangedDelegate PointsValueChanged;
 		public event IntValChangedDelegate UnitSizeChanged;
@@ -34,7 +34,7 @@
 			Army = parentArmy;
 			type = unitType;
 			Size = startSize;
-			setInitialEquipment();
+			SetInitialEquipment();
 			CalcCost();
 		}
 
@@ -57,15 +57,15 @@
 			}
 		}
 
-		private void setInitialEquipment()
+		private void SetInitialEquipment()
 		{
 			foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems())
 			{
 				if (unitEquip.IsRequired)
 				{
-					if (CanEquipWithItem(unitEquip.ID))
+					if (CanEquipWithItem(unitEquip))
 					{
-						equipment[unitEquip] = unitEquip.MinNumber;
+						equipment[unitEquip.EquipmentItem] = unitEquip.MinNumber;
 					}
 				}
 			}
@@ -77,10 +77,10 @@
 			double oldpoints = points;
 			points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost;
 
-			foreach (UnitEquipmentItem unitEquipItem in equipment.Keys)
+			foreach (EquipmentItem equipItem in equipment.Keys)
 			{
-				EquipmentItem equipItem = unitEquipItem.EquipmentItem;
-				double count = (double)equipment[unitEquipItem];
+				double count = equipment[equipItem];
+				UnitEquipmentItem unitEquipItem = UnitType.GetEquipmentItem(equipItem);
 				
 				if (unitEquipItem.IsRatioLimit)
 				{
@@ -235,42 +235,53 @@
 				}
 			}
 
-			return (UnitEquipmentItem[])list.ToArray();
+			return list.ToArray();
 		}
 
-		public UnitEquipmentItem[] GetEquipment()
+		public EquipmentItem[] GetEquipment()
 		{
-			UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count];
-			int i = 0;
-
-			foreach (UnitEquipmentItem item in equipment.Keys)
-			{
-				items[i++] = item;
-			}
-
-			return items;
+			return DictionaryUtils.ToKeyArray(equipment);
+		}
+		
+		private UnitEquipmentItem[] GetUnitEquipmentItemsForEquipment()
+		{
+			EquipmentItem[] items = GetEquipment();
+			int arrLength = items.Length;
+			UnitEquipmentItem[] unitItems = new UnitEquipmentItem[arrLength];
+			
+			for (int i = 0; i < arrLength; i++)
+			{
+				UnitType.GetEquipmentItem(items[i]);
+			}
+			
+			return unitItems;
 		}
 
-		public UnitEquipmentItem[] GetRequiredEquipment()
+		public EquipmentItem[] GetRequiredEquipment()
 		{
-			List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();
+			List<EquipmentItem> list = new List<EquipmentItem>();
 
-			foreach(UnitEquipmentItem item in equipment.Keys)
+			foreach(UnitEquipmentItem item in GetUnitEquipmentItemsForEquipment())
 			{
 				if (item.IsRequired)
 				{
-					list.Add(item);
+					list.Add(item.EquipmentItem);
 				}
 			}
 
-			return (UnitEquipmentItem[])list.ToArray();
+			return list.ToArray();
 		}
-
-		public double GetEquipmentAmount(UnitEquipmentItem item)
+
+		public double GetEquipmentAmount(UnitEquipmentItem item)
+		{
+			return GetEquipmentAmount(item.EquipmentItem);
+		}
+		
+		public double GetEquipmentAmount(EquipmentItem item)
 		{
 			if (equipment.ContainsKey(item))
 			{
-				return (double)equipment[item];
+				return equipment[item];
 			}
 			else
 			{
@@ -281,13 +292,18 @@
 		public double GetEquipmentAmount(string equipID)
 		{
 			return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID));
+		}
+		
+		public void SetEquipmentAmount(UnitEquipmentItem equip, int amount)
+		{
+			SetEquipmentAmount(equip.EquipmentItem, amount);
 		}
 
-		public void SetEquipmentAmount(UnitEquipmentItem equip, int amount)
+		public void SetEquipmentAmount(EquipmentItem equip, int amount)
 		{
-			if (equip.IsRatioLimit)
+			if (UnitType.IsRatioLimitedEquipmentItem(equip))
 			{
-				throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has a ratio limit, not an absolute limit");
+				throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit");
 			}
 			
 			if (amount <1 && amount != WarFoundryCore.INFINITY)
@@ -300,9 +316,14 @@
 		
 		public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
 		{
-			if (!equip.IsRatioLimit)
+			SetEquipmentRatio(equip.EquipmentItem, ratio);
+		}
+		
+		public void SetEquipmentRatio(EquipmentItem equip, double ratio)
+		{
+			if (UnitType.IsAbsoluteLimitedEquipmentItem(equip))
 			{
-				throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has an absolute limit, not a ratio limit");
+				throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit");
 			}
 			
 			if (ratio > 1)
@@ -317,7 +338,7 @@
 			SetEquipmentAmount(equip, ratio);
 		}
 		
-		private void SetEquipmentAmount(UnitEquipmentItem equip, double amount)
+		private void SetEquipmentAmount(EquipmentItem equip, double amount)
 		{
 			double oldAmount = 0;
 
@@ -340,26 +361,31 @@
 				OnUnitEquipmentAmountChanged(equip, oldAmount, amount);
 				CalcCost();
 			}
+		}
+		
+		public bool CanEquipWithItem(UnitEquipmentItem item)
+		{
+			string mutex = item.MutexGroup;
+
+			if (mutex == "")
+			{
+				return true;
+			}
+
+			foreach (UnitEquipmentItem unitItem in GetUnitEquipmentItemsForEquipment())
+			{
+				if (unitItem.MutexGroup == mutex)
+				{
+					return false;
+				}
+			}
+
+			return true;
 		}
 
 		public bool CanEquipWithItem(string equipID)
 		{
-			string mutex = UnitType.GetEquipmentItem(equipID).MutexGroup;
-
-			if (mutex == "")
-			{
-				return true;
-			}
-
-			foreach (UnitEquipmentItem item in equipment.Keys)
-			{
-				if (item.MutexGroup == mutex)
-				{
-					return false;
-				}
-			}
-
-			return true;
+			return CanEquipWithItem(UnitType.GetEquipmentItem(equipID));
 		}
 
 		private void OnPointsValueChanged(double oldValue, double newValue)
@@ -378,7 +404,7 @@
 			}
 		}
 
-		private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue)
+		private void OnUnitEquipmentAmountChanged(EquipmentItem equip, double oldValue, double newValue)
 		{
 			if (UnitEquipmentAmountChanged!=null)
 			{
--- a/api/Objects/UnitType.cs	Thu Apr 09 15:05:38 2009 +0000
+++ b/api/Objects/UnitType.cs	Thu Apr 09 15:36:22 2009 +0000
@@ -244,6 +244,20 @@
 			UnitEquipmentItem equipItem = null;
 			equipment.TryGetValue(id, out equipItem);
 			return equipItem;
+		}
+		
+		/// <summary>
+		/// Gets a <see cref=" UnitEquipmentItem"/> for the given <see cref=" EquipmentItem"/>, or <code>null</code> if the unit can't take that <code>EquipmentItem</code>
+		/// </summary>
+		/// <param name="item">
+		/// The <see cref="EquipmentItem"/> to get the <see cref=" UnitEquipmentItem"/>
+		/// </param>
+		/// <returns>
+		/// The <see cref="UnitEquipmentItem"/> that definies the UnitType's restrictions for taking the <see cref=" EquipmentItem"/>
+		/// </returns>
+		public UnitEquipmentItem GetEquipmentItem(EquipmentItem item)
+		{
+			return GetEquipmentItem(item.ID);
 		}
 
 		/// <summary>
@@ -272,6 +286,18 @@
 			}
 		}
 		
+		public bool IsRatioLimitedEquipmentItem(EquipmentItem item)
+		{
+			UnitEquipmentItem equip = GetEquipmentItem(item);
+			return equip != null && equip.IsRatioLimit;
+		}
+		
+		public bool IsAbsoluteLimitedEquipmentItem(EquipmentItem item)
+		{
+			UnitEquipmentItem equip = GetEquipmentItem(item);
+			return equip != null && !equip.IsRatioLimit;
+		}
+		
 		public ICollection<Ability> GetRequiredAbilities()
 		{
 			return requiredAbilities.Values;