changeset 189:0b413f41e6e3

Re #198: Add equipment slots * Refactor out common function * Add handling of slot limits and existing equipment to GetMaxEquipmentPercentage
author IBBoard <dev@ibboard.co.uk>
date Wed, 28 Oct 2009 20:42:13 +0000
parents 354d51cc002b
children 6f67f16a67fb
files api/Util/UnitEquipmentUtil.cs
diffstat 1 files changed, 27 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/api/Util/UnitEquipmentUtil.cs	Tue Oct 27 20:58:10 2009 +0000
+++ b/api/Util/UnitEquipmentUtil.cs	Wed Oct 28 20:42:13 2009 +0000
@@ -110,7 +110,7 @@
 		private static int GetEquipmentCountLimit (Unit unit, int currLimit, UnitEquipmentItem equip)
 		{
 			int newLimit = currLimit;
-			AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit (equip.SlotName);
+			AbstractLimit limit = GetSlotLimitForItem(unit, equip);
 			
 			if (!(limit is UnlimitedLimit))
 			{
@@ -120,6 +120,12 @@
 			
 			return newLimit;
 		}
+
+		private static AbstractLimit GetSlotLimitForItem(Unit unit, UnitEquipmentItem equip)
+		{
+			return unit.UnitType.GetEquipmentSlotLimit(equip.SlotName);
+		}
+
 		
 		public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip)
 		{			
@@ -130,23 +136,37 @@
 		
 		public static bool IsEquipmentRatioLimited(Unit unit, UnitEquipmentItem equip)
 		{
-			AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit(equip.SlotName);
+			AbstractLimit limit = GetSlotLimitForItem(unit, equip);
 			return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit);
 		}
 		
 		public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
 		{
 			double limit = 0;
+			AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
 			
-			if (equip.IsRatioLimit)
+			
+			if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || unit.GetEquipmentAmountInSlot (equip.SlotName) != 0)
 			{
-				limit = Math.Max(((IPercentageLimit)equip.MinLimit).Percentage, ((IPercentageLimit)equip.MaxLimit).Percentage);
+				limit = (GetMaxEquipmentCount(unit, equip) / (double)unit.Size) * 100.0;
 			}
 			else
 			{
-				int unitSize = unit.Size;
-				limit = (equip.MaxLimit.GetLimit(unitSize) / (double)unitSize) * 100.0;
-			}
+				if (equip.IsRatioLimit)
+				{
+					limit = Math.Max(((IPercentageLimit)equip.MinLimit).Percentage, ((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);
+				}
+			}	
 			
 			return limit;
 		}