changeset 201:4d7ff70bb109

Re #208: equipmentslot limit issues * Fix numeric slot issues by using "amount taken excluding this item" method * Rename "without this item" to "excluding this item" to clarify purpose * Restructure percentage limits and break some tests (some of which are only broken by rounding errors)
author IBBoard <dev@ibboard.co.uk>
date Thu, 05 Nov 2009 21:09:03 +0000
parents 3e287b6b5244
children 37ad50823531
files api/Objects/Unit.cs api/Util/UnitEquipmentUtil.cs
diffstat 2 files changed, 35 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/api/Objects/Unit.cs	Thu Nov 05 20:22:00 2009 +0000
+++ b/api/Objects/Unit.cs	Thu Nov 05 21:09:03 2009 +0000
@@ -525,7 +525,7 @@
 		/// </summary>
 		/// <param name="item">the item to exclude from the count</param>
 		/// <returns>the total number of items</returns>
-		public int GetEquipmentAmountInSlotWithoutItem(UnitEquipmentItem item)
+		public int GetEquipmentAmountInSlotExcludingItem(UnitEquipmentItem item)
 		{
 			int amount = 0;
 
--- a/api/Util/UnitEquipmentUtil.cs	Thu Nov 05 20:22:00 2009 +0000
+++ b/api/Util/UnitEquipmentUtil.cs	Thu Nov 05 21:09:03 2009 +0000
@@ -114,7 +114,7 @@
 			
 			if (!(limit is UnlimitedLimit))
 			{
-				int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlot (equip.SlotName);
+				int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip);
 				newLimit = Math.Min (slotMax, newLimit);
 			}
 			
@@ -144,29 +144,36 @@
 		{
 			double limit = 0;
 			AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
-
-
-			if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || (!(slotLimit is UnlimitedLimit) && unit.GetEquipmentAmountInSlotWithoutItem(equip) != 0))
+			
+			if (slotLimit is IPercentageLimit)
 			{
-				limit = (GetMaxEquipmentCount(unit, equip) / (double)unit.Size) * 100.0;
+				limit = ((IPercentageLimit)slotLimit).Percentage;
 			}
 			else
 			{
-				if (equip.IsRatioLimit)
-				{
-					limit = ((IPercentageLimit)equip.MaxLimit).Percentage;
-				}
-				else
-				{
-					int unitSize = unit.Size;
-					limit = (equip.MaxLimit.GetLimit(unitSize) / (double)unitSize) * 100.0;
-				}
-				
-				if (slotLimit is IPercentageLimit)
-				{
-					limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit);
-				}
-			}	
+				limit =  GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit);
+			}
+			
+			limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
+			
+			return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MaxLimit, unit);
+		}
+		
+		private static double GetPercentageOfUnitSize(int number, Unit unit)
+		{
+			return (number / (double)unit.Size) * 100;
+		}
+		
+		private static double GetMinOfSlotLimitAndEquipmentLimit(bool equipIsRatio, double limit, AbstractLimit equipLimit, Unit unit)
+		{
+			if (equipIsRatio)
+			{
+				limit = Math.Min(limit, ((IPercentageLimit)equipLimit).Percentage);
+			}
+			else
+			{
+				limit = Math.Min(limit, GetPercentageOfUnitSize(equipLimit.GetLimit(unit.Size), unit));
+			}
 			
 			return limit;
 		}
@@ -176,30 +183,18 @@
 			double limit = 0;
 			AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
 			
-			
-			if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || (!(slotLimit is UnlimitedLimit) && unit.GetEquipmentAmountInSlotWithoutItem(equip) != 0))
+			if (slotLimit is IPercentageLimit)
 			{
-				limit = (GetMinEquipmentCount(unit, equip) / (double)unit.Size) * 100.0;
+				limit = ((IPercentageLimit)slotLimit).Percentage;
 			}
 			else
 			{
-				if (equip.IsRatioLimit)
-				{
-					limit = ((IPercentageLimit)equip.MinLimit).Percentage;
-				}
-				else
-				{
-					int unitSize = unit.Size;
-					limit = (equip.MinLimit.GetLimit(unitSize) / (double)unitSize) * 100.0;
-				}
-				
-				if (slotLimit is IPercentageLimit)
-				{
-					limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit);
-				}
-			}	
+				limit =  GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit);
+			}
 			
-			return limit;
+			limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
+			
+			return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MinLimit, unit);
 		}
 	}
 }