changeset 97:95746083d037

Fixes #118: Allow equipment amounts of "ratio" equipment to be define as absolute or ratio amounts * Add extra "NumericSelection" type so that ratio selections check the absolute value is within their ratio range * Update structure of checking for valid values * Move cost calculation for equipment selection in to abstract class and add abstract method to get "number of items taken" * Handle numeric selection and numeric selection for ratio differently in Unit
author IBBoard <dev@ibboard.co.uk>
date Sun, 09 Aug 2009 12:02:35 +0000
parents ced5a18d9f52
children 4dd1c41c95b4
files api/Objects/AbstractUnitEquipmentItemSelection.cs api/Objects/Unit.cs api/Objects/UnitEquipmentNumericForRatioSelection.cs api/Objects/UnitEquipmentNumericSelection.cs api/Objects/UnitEquipmentRatioSelection.cs
diffstat 5 files changed, 94 insertions(+), 26 deletions(-) [+]
line diff
     1.1 --- a/api/Objects/AbstractUnitEquipmentItemSelection.cs	Sun Aug 09 11:09:12 2009 +0000
     1.2 +++ b/api/Objects/AbstractUnitEquipmentItemSelection.cs	Sun Aug 09 12:02:35 2009 +0000
     1.3 @@ -38,11 +38,6 @@
     1.4  			}
     1.5  		}
     1.6  		
     1.7 -		public abstract double TotalCost
     1.8 -		{
     1.9 -			get;
    1.10 -		}
    1.11 -		
    1.12  		public double AmountTaken
    1.13  		{
    1.14  			get 
    1.15 @@ -62,6 +57,24 @@
    1.16  			}
    1.17  		}
    1.18  		
    1.19 -		protected abstract bool IsValidValue(double newValue);
    1.20 +		protected bool IsValidValue(double newValue)
    1.21 +		{
    1.22 +			return IsInRange(newValue);
    1.23 +		}
    1.24 +		
    1.25 +		protected abstract bool IsInRange(double newValue);
    1.26 +		
    1.27 +		public double TotalCost
    1.28 +		{
    1.29 +			get
    1.30 +			{
    1.31 +				return NumberTaken * EquipmentItem.Cost;
    1.32 +			}
    1.33 +		}
    1.34 +		
    1.35 +		public abstract int NumberTaken
    1.36 +		{
    1.37 +			 get;
    1.38 +		}
    1.39  	}
    1.40  }
     2.1 --- a/api/Objects/Unit.cs	Sun Aug 09 11:09:12 2009 +0000
     2.2 +++ b/api/Objects/Unit.cs	Sun Aug 09 12:02:35 2009 +0000
     2.3 @@ -288,7 +288,18 @@
     2.4  				{
     2.5  					if (oldAmount == 0)
     2.6  					{
     2.7 -						equipment[equip] = new UnitEquipmentNumericSelection(this, equip, amount);
     2.8 +						AbstractUnitEquipmentItemSelection newItem = null;
     2.9 +						
    2.10 +						if (equip.IsRatioLimit)
    2.11 +						{
    2.12 +							newItem = new UnitEquipmentNumericForRatioSelection(this, equip, amount);
    2.13 +						}
    2.14 +						else
    2.15 +						{
    2.16 +							newItem = new UnitEquipmentNumericSelection(this, equip, amount);
    2.17 +						}
    2.18 +						
    2.19 +						equipment[equip] = newItem;
    2.20  					}
    2.21  					else
    2.22  					{
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/api/Objects/UnitEquipmentNumericForRatioSelection.cs	Sun Aug 09 12:02:35 2009 +0000
     3.3 @@ -0,0 +1,33 @@
     3.4 +// This file (UnitEquipmentNumericForRatioSelection.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     3.5 +// 
     3.6 +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
     3.7 +
     3.8 +using System;
     3.9 +using IBBoard.Lang;
    3.10 +
    3.11 +namespace IBBoard.WarFoundry.API.Objects
    3.12 +{
    3.13 +	/// <summary>
    3.14 +	/// An object to hold the selection of a unit's equipment where the selection was made as an absolute number and the
    3.15 +	/// equipment item has a ratio limit
    3.16 +	/// </summary>
    3.17 +	public class UnitEquipmentNumericForRatioSelection : UnitEquipmentNumericSelection
    3.18 +	{
    3.19 +		public UnitEquipmentNumericForRatioSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
    3.20 +		{
    3.21 +		}
    3.22 +		
    3.23 +		public UnitEquipmentNumericForRatioSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, IBBMath.Round(unit.Size * item.MinPercentage, item.RoundNumberUp))
    3.24 +		{
    3.25 +		}
    3.26 +		
    3.27 +		protected override bool IsInRange (double newValue)
    3.28 +		{
    3.29 +			int minLimit = (int) IBBMath.Round(EquipmentForUnit.Size * EquipmentItem.MinPercentage, EquipmentItem.RoundNumberUp);
    3.30 +			int maxLimit = (int) IBBMath.Round(EquipmentForUnit.Size * EquipmentItem.MaxPercentage, EquipmentItem.RoundNumberUp);
    3.31 +			bool isInRange = (minLimit <= newValue) && (newValue <= maxLimit);
    3.32 +			newValue = (newValue == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : newValue);
    3.33 +			return isInRange && IsWholeNumber(newValue);
    3.34 +		}
    3.35 +	}
    3.36 +}
     4.1 --- a/api/Objects/UnitEquipmentNumericSelection.cs	Sun Aug 09 11:09:12 2009 +0000
     4.2 +++ b/api/Objects/UnitEquipmentNumericSelection.cs	Sun Aug 09 12:02:35 2009 +0000
     4.3 @@ -6,6 +6,9 @@
     4.4  
     4.5  namespace IBBoard.WarFoundry.API.Objects
     4.6  {	
     4.7 +	/// <summary>
     4.8 +	/// An object to hold the selection of a unit's equipment where the selection was made as an absolute number
     4.9 +	/// </summary>
    4.10  	public class UnitEquipmentNumericSelection : AbstractUnitEquipmentItemSelection
    4.11  	{	
    4.12  		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
    4.13 @@ -15,23 +18,36 @@
    4.14  		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinNumber)
    4.15  		{
    4.16  		}
    4.17 -		
    4.18 -		public override double TotalCost
    4.19 +					
    4.20 +		public override int NumberTaken
    4.21  		{
    4.22  			get
    4.23  			{
    4.24 -				return CalculateAmount() * EquipmentItem.Cost;
    4.25 +				return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : (int) AmountTaken);
    4.26  			}
    4.27  		}
    4.28 -					
    4.29 -		private double CalculateAmount()
    4.30 -		{
    4.31 -			return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : AmountTaken);
    4.32 -		}
    4.33  		
    4.34 -		protected override bool IsValidValue (double newValue)
    4.35 +		protected bool IsWholeNumber(double newValue)
    4.36  		{
    4.37  			return newValue == Math.Round(newValue);
    4.38  		}
    4.39 +		
    4.40 +		protected override bool IsInRange(double newValue)
    4.41 +		{
    4.42 +			bool isInRange = IsWholeNumber(newValue);
    4.43 +			
    4.44 +			if (newValue == WarFoundryCore.INFINITY)
    4.45 +			{
    4.46 +				isInRange = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY);
    4.47 +			}
    4.48 +			else if (isInRange)
    4.49 +			{
    4.50 +				int minLimit = (EquipmentItem.MinNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MinNumber);
    4.51 +				int maxLimit = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MaxNumber);
    4.52 +				isInRange = (minLimit <= newValue) && (newValue <= maxLimit);
    4.53 +			}
    4.54 +			
    4.55 +			return isInRange;
    4.56 +		}
    4.57  	}
    4.58  }
     5.1 --- a/api/Objects/UnitEquipmentRatioSelection.cs	Sun Aug 09 11:09:12 2009 +0000
     5.2 +++ b/api/Objects/UnitEquipmentRatioSelection.cs	Sun Aug 09 12:02:35 2009 +0000
     5.3 @@ -19,22 +19,17 @@
     5.4  		public UnitEquipmentRatioSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinPercentage)
     5.5  		{
     5.6  		}
     5.7 -		
     5.8 -		public override double TotalCost
     5.9 +					
    5.10 +		public override int NumberTaken
    5.11  		{
    5.12  			get
    5.13  			{
    5.14 -				return CalculateAmount() * EquipmentItem.Cost;
    5.15 +				double numberTaken = AmountTaken * EquipmentForUnit.Size;
    5.16 +				return (int) (EquipmentItem.RoundNumberUp ? Math.Ceiling(numberTaken) : Math.Floor(numberTaken));
    5.17  			}
    5.18  		}
    5.19 -					
    5.20 -		private double CalculateAmount()
    5.21 -		{
    5.22 -			double numberTaken = AmountTaken * EquipmentForUnit.Size;
    5.23 -			return (EquipmentItem.RoundNumberUp ? Math.Ceiling(numberTaken) : Math.Floor(numberTaken));
    5.24 -		}
    5.25  		
    5.26 -		protected override bool IsValidValue (double newValue)
    5.27 +		protected override bool IsInRange (double newValue)
    5.28  		{
    5.29  			return (EquipmentItem.MinPercentage <= newValue) && (newValue <= EquipmentItem.MaxPercentage);
    5.30  		}