changeset 202:37ad50823531

Re #208: equipmentslot limit issues * Move more code to a common method * Use new IBBMath.Percentage method to ensure consistent calculation of percentages * Move subtraction of amount already taken in slot inside IF and ELSE and tailor to situation Some tests still failing
author IBBoard <dev@ibboard.co.uk>
date Fri, 06 Nov 2009 20:11:30 +0000
parents 4d7ff70bb109
children df4e56e2ee71
files api/Util/UnitEquipmentUtil.cs
diffstat 1 files changed, 17 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/api/Util/UnitEquipmentUtil.cs	Thu Nov 05 21:09:03 2009 +0000
+++ b/api/Util/UnitEquipmentUtil.cs	Fri Nov 06 20:11:30 2009 +0000
@@ -5,6 +5,7 @@
 using System;
 using System.Collections.Generic;
 using System.Text;
+using IBBoard.CustomMath;
 using IBBoard.Limits;
 using IBBoard.WarFoundry.API.Objects;
 
@@ -142,31 +143,31 @@
 		
 		public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
 		{
+			return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MaxLimit, unit);
+		}
+		
+		private static double GetPercentageOfUnitSize(int number, Unit unit)
+		{
+			return IBBMath.Percentage(number, unit.Size);
+		}
+		
+		private static double GetMinOfSlotLimitAndEquipmentLimit(UnitEquipmentItem equip, AbstractLimit equipLimit, Unit unit)
+		{			
 			double limit = 0;
 			AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
 			
 			if (slotLimit is IPercentageLimit)
 			{
 				limit = ((IPercentageLimit)slotLimit).Percentage;
+				limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
 			}
 			else
 			{
-				limit =  GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit);
+				int remaining = slotLimit.GetLimit(unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip);
+				limit = GetPercentageOfUnitSize(remaining, 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)
+			if (equip.IsRatioLimit)
 			{
 				limit = Math.Min(limit, ((IPercentageLimit)equipLimit).Percentage);
 			}
@@ -179,22 +180,8 @@
 		}
 		
 		public static double GetMinEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
-		{
-			double limit = 0;
-			AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
-			
-			if (slotLimit is IPercentageLimit)
-			{
-				limit = ((IPercentageLimit)slotLimit).Percentage;
-			}
-			else
-			{
-				limit =  GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit);
-			}
-			
-			limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
-			
-			return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MinLimit, unit);
+		{	
+			return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MinLimit, unit);
 		}
 	}
 }