diff api/Util/UnitEquipmentUtil.cs @ 187:104a683b5ef8

Re #198: Add equipment slots * Fix problem with shared "get limit" method not handling min and max properly * Add method to get percentage limit
author IBBoard <dev@ibboard.co.uk>
date Tue, 27 Oct 2009 20:53:27 +0000
parents c8682d206a73
children 354d51cc002b
line wrap: on
line diff
--- a/api/Util/UnitEquipmentUtil.cs	Tue Oct 27 20:26:19 2009 +0000
+++ b/api/Util/UnitEquipmentUtil.cs	Tue Oct 27 20:53:27 2009 +0000
@@ -101,28 +101,31 @@
 		}
 
 		public static int GetMaxEquipmentCount (Unit unit, UnitEquipmentItem equip)
-		{
-			return GetEquipmentCountLimit (unit, equip);
+		{			
+			int unitSize = unit.Size;
+			int max = Math.Max(equip.MinLimit.GetLimit(unitSize), equip.MaxLimit.GetLimit (unitSize));
+			return GetEquipmentCountLimit (unit, max, equip);
 		}
 
-		private static int GetEquipmentCountLimit (Unit unit, UnitEquipmentItem equip)
+		private static int GetEquipmentCountLimit (Unit unit, int currLimit, UnitEquipmentItem equip)
 		{
-			int unitSize = unit.Size;
-			int max = Math.Min (equip.MinLimit.GetLimit (unitSize), equip.MaxLimit.GetLimit (unitSize));
+			int newLimit = currLimit;
 			AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit (equip.SlotName);
 			
 			if (!(limit is UnlimitedLimit))
 			{
-				int slotMax = limit.GetLimit (unitSize) - unit.GetEquipmentAmountInSlot (equip.SlotName);
-				max = Math.Min (slotMax, max);
+				int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlot (equip.SlotName);
+				newLimit = Math.Min (slotMax, newLimit);
 			}
 			
-			return max;
+			return newLimit;
 		}
 		
 		public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip)
-		{
-			return GetEquipmentCountLimit (unit, equip);
+		{			
+			int unitSize = unit.Size;
+			int min = Math.Min (equip.MinLimit.GetLimit (unitSize), equip.MaxLimit.GetLimit (unitSize));
+			return GetEquipmentCountLimit (unit, min, equip);
 		}
 		
 		public static bool IsEquipmentRatioLimited(Unit unit, UnitEquipmentItem equip)
@@ -130,5 +133,17 @@
 			AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit(equip.SlotName);
 			return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit);
 		}
+		
+		public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
+		{
+			double limit = 0;
+			
+			if (equip.IsRatioLimit)
+			{
+				limit = Math.Max(((IPercentageLimit)equip.MinLimit).Percentage, ((IPercentageLimit)equip.MaxLimit).Percentage);
+			}
+			
+			return limit;
+		}
 	}
 }