changeset 98:4dd1c41c95b4

Fixes #119: Handle changing of equipment between absolute and ratio amounts * Check the type of the existing unit and replace if necessary
author IBBoard <dev@ibboard.co.uk>
date Sun, 09 Aug 2009 13:12:38 +0000
parents 95746083d037
children 05a78408ae8f
files api/Objects/Unit.cs
diffstat 1 files changed, 41 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/api/Objects/Unit.cs	Sun Aug 09 12:02:35 2009 +0000
+++ b/api/Objects/Unit.cs	Sun Aug 09 13:12:38 2009 +0000
@@ -282,29 +282,24 @@
 			}
 			else
 			{
-				double oldAmount = GetEquipmentAmount(equip);
+				AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip);
+				double oldAmount = (currSelection == null ? 0 : currSelection.AmountTaken);
 	
-				if (amount!=oldAmount)
+				if (amount != oldAmount)
 				{
 					if (oldAmount == 0)
 					{
-						AbstractUnitEquipmentItemSelection newItem = null;
-						
-						if (equip.IsRatioLimit)
-						{
-							newItem = new UnitEquipmentNumericForRatioSelection(this, equip, amount);
-						}
-						else
-						{
-							newItem = new UnitEquipmentNumericSelection(this, equip, amount);
-						}
-						
-						equipment[equip] = newItem;
+						AddEquipmentAmount(equip, amount);
+					}
+					else if (currSelection is UnitEquipmentNumericSelection)
+					{
+						//A UnitEquipmentItem shouldn't change its IsRatio value, so assume we already have the right sub-type
+						currSelection.AmountTaken = amount;
 					}
 					else
 					{
-						AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip);
-						currSelection.AmountTaken = amount;
+						equipment.Remove(equip);
+						AddEquipmentAmount(equip, amount);
 					}
 	
 					OnUnitEquipmentAmountChanged(equip, oldAmount, amount);
@@ -312,6 +307,22 @@
 			}
 		}
 		
+		private void AddEquipmentAmount(UnitEquipmentItem equip, int amount)
+		{
+			AbstractUnitEquipmentItemSelection newItem = null;
+			
+			if (equip.IsRatioLimit)
+			{
+				newItem = new UnitEquipmentNumericForRatioSelection(this, equip, amount);
+			}
+			else
+			{
+				newItem = new UnitEquipmentNumericSelection(this, equip, amount);
+			}
+			
+			equipment[equip] = newItem;
+		}
+		
 		public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
 		{
 			if (!equip.IsRatioLimit)
@@ -334,18 +345,23 @@
 			}
 			else
 			{
-				double oldRatio = GetEquipmentAmount(equip);
+				AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip);
+				double oldRatio = (currSelection == null ? 0 : currSelection.AmountTaken);
 	
 				if (ratio != oldRatio)
 				{
 					if (oldRatio == 0)
 					{
-						equipment[equip] = new UnitEquipmentRatioSelection(this, equip, ratio);
+						AddEquipmentRatio(equip, ratio);
+					}
+					else if (currSelection is UnitEquipmentRatioSelection)
+					{
+						currSelection.AmountTaken = ratio;
 					}
 					else
 					{
-						AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip);
-						currSelection.AmountTaken = ratio;
+						equipment.Remove(equip);
+						AddEquipmentRatio(equip, ratio);
 					}
 	
 					OnUnitEquipmentAmountChanged(equip, oldRatio, ratio);
@@ -353,6 +369,11 @@
 			}
 		}
 		
+		private void AddEquipmentRatio(UnitEquipmentItem equip, double ratio)
+		{
+			equipment[equip] = new UnitEquipmentRatioSelection(this, equip, ratio);
+		}
+		
 		private void RemoveEquipmentItem(UnitEquipmentItem equip)
 		{
 			double oldAmount = GetEquipmentAmount(equip);