diff api/Objects/Unit.cs @ 80:aa66dd18cdae

Closes #81 - Resolve Unit Equipment problems in WinForms GUI * Take the easy solution (after changes in r123 that made EquipmentItem methods available through UnitEquipmentItem) and revert back to using UnitEquipmentItems in Unit
author IBBoard <dev@ibboard.co.uk>
date Tue, 19 May 2009 19:00:48 +0000
parents aa2d05a9c635
children 032b174fc17a
line wrap: on
line diff
--- a/api/Objects/Unit.cs	Tue May 19 14:40:10 2009 +0000
+++ b/api/Objects/Unit.cs	Tue May 19 19:00:48 2009 +0000
@@ -21,7 +21,7 @@
 		private Unit parentUnit;
 		private double points;
 		private ArmyCategory cat;
-		private Dictionary<EquipmentItem, double> equipment = new Dictionary<EquipmentItem, double>();
+		private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>();
 		private List<Unit> containedUnits = new List<Unit>();
 		public event DoubleValChangedDelegate PointsValueChanged;
 		public event IntValChangedDelegate UnitSizeChanged;
@@ -65,7 +65,7 @@
 				{
 					if (CanEquipWithItem(unitEquip))
 					{
-						equipment[unitEquip.EquipmentItem] = unitEquip.MinNumber;
+						equipment[unitEquip] = unitEquip.MinNumber;
 					}
 				}
 			}
@@ -77,31 +77,30 @@
 			double oldpoints = points;
 			points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost;
 
-			foreach (EquipmentItem equipItem in equipment.Keys)
+			foreach (UnitEquipmentItem unitEquipItem in equipment.Keys)
 			{
-				double count = equipment[equipItem];
-				UnitEquipmentItem unitEquipItem = UnitType.GetEquipmentItem(equipItem);
+				double count = equipment[unitEquipItem];
 				
 				if (unitEquipItem.IsRatioLimit)
 				{
 					if (unitEquipItem.RoundNumberUp)
 					{
-						points+= Math.Ceiling(size * count) * equipItem.Cost;
+						points += Math.Ceiling(size * count) * unitEquipItem.Cost;
 					}
 					else
 					{
-						points+= Math.Floor(size * count) * equipItem.Cost;
+						points += Math.Floor(size * count) * unitEquipItem.Cost;
 					}
 				}
 				else
 				{
 					if (count == WarFoundryCore.INFINITY)
 					{
-						points+= size * equipItem.Cost;
+						points += size * unitEquipItem.Cost;
 					}
 					else
 					{
-						points+= count * equipItem.Cost;
+						points += count * unitEquipItem.Cost;
 					}
 				}
 			}
@@ -256,30 +255,16 @@
 			return list.ToArray();
 		}
 
-		public EquipmentItem[] GetEquipment()
+		public UnitEquipmentItem[] GetEquipment()
 		{
 			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 EquipmentItem[] GetRequiredEquipment()
 		{
 			List<EquipmentItem> list = new List<EquipmentItem>();
 
-			foreach(UnitEquipmentItem item in GetUnitEquipmentItemsForEquipment())
+			foreach(UnitEquipmentItem item in GetEquipment())
 			{
 				if (item.IsRequired)
 				{
@@ -292,19 +277,7 @@
 
 		public double GetEquipmentAmount(UnitEquipmentItem item)
 		{
-			return GetEquipmentAmount(item.EquipmentItem);
-		}
-		
-		public double GetEquipmentAmount(EquipmentItem item)
-		{
-			if (equipment.ContainsKey(item))
-			{
-				return equipment[item];
-			}
-			else
-			{
-				return 0;
-			}
+			return GetEquipmentAmount(item.ID);
 		}
 
 		public double GetEquipmentAmount(string equipID)
@@ -314,12 +287,7 @@
 		
 		public void SetEquipmentAmount(UnitEquipmentItem equip, int amount)
 		{
-			SetEquipmentAmount(equip.EquipmentItem, amount);
-		}
-
-		public void SetEquipmentAmount(EquipmentItem equip, int amount)
-		{
-			if (UnitType.IsRatioLimitedEquipmentItem(equip))
+			if (equip.IsRatioLimit)
 			{
 				throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit");
 			}
@@ -334,12 +302,7 @@
 		
 		public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
 		{
-			SetEquipmentRatio(equip.EquipmentItem, ratio);
-		}
-		
-		public void SetEquipmentRatio(EquipmentItem equip, double ratio)
-		{
-			if (UnitType.IsAbsoluteLimitedEquipmentItem(equip))
+			if (!equip.IsRatioLimit)
 			{
 				throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit");
 			}
@@ -356,7 +319,7 @@
 			SetEquipmentAmount(equip, ratio);
 		}
 		
-		private void SetEquipmentAmount(EquipmentItem equip, double amount)
+		private void SetEquipmentAmount(UnitEquipmentItem equip, double amount)
 		{
 			double oldAmount = 0;
 
@@ -390,7 +353,7 @@
 				return true;
 			}
 
-			foreach (UnitEquipmentItem unitItem in GetUnitEquipmentItemsForEquipment())
+			foreach (UnitEquipmentItem unitItem in GetEquipment())
 			{
 				if (unitItem.MutexGroup == mutex)
 				{
@@ -422,7 +385,7 @@
 			}
 		}
 
-		private void OnUnitEquipmentAmountChanged(EquipmentItem equip, double oldValue, double newValue)
+		private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue)
 		{
 			if (UnitEquipmentAmountChanged!=null)
 			{