changeset 429:7179c585d31d

Re #356: Stack overflow with some equipment limits * Add RawNumberTaken to be used in some locations to avoid indirect recursion * Use new RawNumberTaken within Unit equipment counting method Overflow fixed, but results not correct
author IBBoard <dev@ibboard.co.uk>
date Sun, 06 Nov 2011 20:21:57 +0000
parents 8f5125740316
children 7e95b880f9cc
files API/Objects/AbstractUnitEquipmentItemSelection.cs API/Objects/Unit.cs API/Objects/UnitEquipmentNumericSelection.cs API/Objects/UnitEquipmentRatioSelection.cs
diffstat 4 files changed, 53 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/AbstractUnitEquipmentItemSelection.cs	Sun Oct 30 20:33:05 2011 +0000
+++ b/API/Objects/AbstractUnitEquipmentItemSelection.cs	Sun Nov 06 20:21:57 2011 +0000
@@ -40,7 +40,7 @@
 		
 		public double AmountTaken
 		{
-			get 
+			get
 			{
 				return amountTaken;
 			}
@@ -83,10 +83,25 @@
 				return NumberTaken * EquipmentItem.Cost;
 			}
 		}
-		
+
+		/// <summary>
+		/// Gets the number taken as a whole number of times the item was taken. This number may be altered by the limits
+		/// placed on the equipment item (e.g. reduced if there is a lower maximum limit)
+		/// </summary>
+		/// <value>
+		/// The number of times the item was taken, modified by any limits.
+		/// </value>
 		public abstract int NumberTaken
 		{
 			 get;
 		}
+
+		/// <summary>
+		/// Gets the raw amount taken as a whole number of times the item was taken.
+		/// </summary>
+		/// <value>
+		/// The unmodified number of times the item was taken.
+		/// </value>
+		public abstract int RawNumberTaken { get; }
 	}
 }
--- a/API/Objects/Unit.cs	Sun Oct 30 20:33:05 2011 +0000
+++ b/API/Objects/Unit.cs	Sun Nov 06 20:21:57 2011 +0000
@@ -511,7 +511,7 @@
 			
 			foreach (AbstractUnitEquipmentItemSelection selection in selections)
 			{
-				amount += selection.NumberTaken;
+				amount += selection.RawNumberTaken;
 			}
 			
 			return amount;
--- a/API/Objects/UnitEquipmentNumericSelection.cs	Sun Oct 30 20:33:05 2011 +0000
+++ b/API/Objects/UnitEquipmentNumericSelection.cs	Sun Nov 06 20:21:57 2011 +0000
@@ -23,7 +23,15 @@
 		{
 			get
 			{
-				return (int) AmountTaken;
+				return RawNumberTaken;
+			}
+		}
+
+		public override int RawNumberTaken
+		{
+			get
+			{
+				return (int)AmountTaken;
 			}
 		}
 		
--- a/API/Objects/UnitEquipmentRatioSelection.cs	Sun Oct 30 20:33:05 2011 +0000
+++ b/API/Objects/UnitEquipmentRatioSelection.cs	Sun Nov 06 20:21:57 2011 +0000
@@ -27,17 +27,37 @@
 		{
 			get
 			{
-				return CalculateNumberTaken (EquipmentForUnit, EquipmentItem, AmountTaken);
+				return CalculateNumberTaken(EquipmentForUnit, EquipmentItem, AmountTaken);
+			}
+		}
+
+		public override int RawNumberTaken
+		{
+			get
+			{
+				return CalculateRawNumberTaken(EquipmentForUnit, EquipmentItem, AmountTaken);
 			}
 		}
 
-		internal static int CalculateNumberTaken (Unit unit, UnitEquipmentItem item, double ratioTaken)
+		internal static int CalculateNumberTaken(Unit unit, UnitEquipmentItem item, double ratioTaken)
+		{
+			int wholeNumberTaken = CalculateRawNumberTaken(unit, item, ratioTaken);
+			int maxTaken = Int32.MaxValue;
+			int minTaken = 0;
+
+			if (wholeNumberTaken > 0)
+			{
+				maxTaken = UnitEquipmentUtil.GetMaxEquipmentCount(unit, item);
+			}
+
+			minTaken = UnitEquipmentUtil.GetMinEquipmentCount(unit, item);
+			return Math.Min(Math.Max(wholeNumberTaken, minTaken), maxTaken);
+		}
+
+		private static int CalculateRawNumberTaken (Unit unit, UnitEquipmentItem item, double ratioTaken)
 		{
 			double exactNumberTaken = (ratioTaken / 100) * unit.Size;
-			int wholeNumberTaken = (int)IBBMath.Round (exactNumberTaken, item.RoundNumberUp);
-			int maxTaken = UnitEquipmentUtil.GetMaxEquipmentCount (unit, item);
-			int minTaken = UnitEquipmentUtil.GetMinEquipmentCount (unit, item);
-			return Math.Min (Math.Max (wholeNumberTaken, minTaken), maxTaken);
+			return (int)IBBMath.Round(exactNumberTaken, item.RoundNumberUp);
 		}
 	}
 }