# HG changeset patch # User IBBoard # Date 1249819355 0 # Node ID 95746083d0376afbc4047fd0bc008ed7a6041c90 # Parent ced5a18d9f52d4d4b620d84d6a1312f148f71cf1 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 diff -r ced5a18d9f52 -r 95746083d037 api/Objects/AbstractUnitEquipmentItemSelection.cs --- a/api/Objects/AbstractUnitEquipmentItemSelection.cs Sun Aug 09 11:09:12 2009 +0000 +++ b/api/Objects/AbstractUnitEquipmentItemSelection.cs Sun Aug 09 12:02:35 2009 +0000 @@ -38,11 +38,6 @@ } } - public abstract double TotalCost - { - get; - } - public double AmountTaken { get @@ -62,6 +57,24 @@ } } - protected abstract bool IsValidValue(double newValue); + protected bool IsValidValue(double newValue) + { + return IsInRange(newValue); + } + + protected abstract bool IsInRange(double newValue); + + public double TotalCost + { + get + { + return NumberTaken * EquipmentItem.Cost; + } + } + + public abstract int NumberTaken + { + get; + } } } diff -r ced5a18d9f52 -r 95746083d037 api/Objects/Unit.cs --- a/api/Objects/Unit.cs Sun Aug 09 11:09:12 2009 +0000 +++ b/api/Objects/Unit.cs Sun Aug 09 12:02:35 2009 +0000 @@ -288,7 +288,18 @@ { if (oldAmount == 0) { - equipment[equip] = new UnitEquipmentNumericSelection(this, equip, amount); + AbstractUnitEquipmentItemSelection newItem = null; + + if (equip.IsRatioLimit) + { + newItem = new UnitEquipmentNumericForRatioSelection(this, equip, amount); + } + else + { + newItem = new UnitEquipmentNumericSelection(this, equip, amount); + } + + equipment[equip] = newItem; } else { diff -r ced5a18d9f52 -r 95746083d037 api/Objects/UnitEquipmentNumericForRatioSelection.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Objects/UnitEquipmentNumericForRatioSelection.cs Sun Aug 09 12:02:35 2009 +0000 @@ -0,0 +1,33 @@ +// This file (UnitEquipmentNumericForRatioSelection.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard +// +// 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. + +using System; +using IBBoard.Lang; + +namespace IBBoard.WarFoundry.API.Objects +{ + /// + /// An object to hold the selection of a unit's equipment where the selection was made as an absolute number and the + /// equipment item has a ratio limit + /// + public class UnitEquipmentNumericForRatioSelection : UnitEquipmentNumericSelection + { + public UnitEquipmentNumericForRatioSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount) + { + } + + public UnitEquipmentNumericForRatioSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, IBBMath.Round(unit.Size * item.MinPercentage, item.RoundNumberUp)) + { + } + + protected override bool IsInRange (double newValue) + { + int minLimit = (int) IBBMath.Round(EquipmentForUnit.Size * EquipmentItem.MinPercentage, EquipmentItem.RoundNumberUp); + int maxLimit = (int) IBBMath.Round(EquipmentForUnit.Size * EquipmentItem.MaxPercentage, EquipmentItem.RoundNumberUp); + bool isInRange = (minLimit <= newValue) && (newValue <= maxLimit); + newValue = (newValue == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : newValue); + return isInRange && IsWholeNumber(newValue); + } + } +} diff -r ced5a18d9f52 -r 95746083d037 api/Objects/UnitEquipmentNumericSelection.cs --- a/api/Objects/UnitEquipmentNumericSelection.cs Sun Aug 09 11:09:12 2009 +0000 +++ b/api/Objects/UnitEquipmentNumericSelection.cs Sun Aug 09 12:02:35 2009 +0000 @@ -6,6 +6,9 @@ namespace IBBoard.WarFoundry.API.Objects { + /// + /// An object to hold the selection of a unit's equipment where the selection was made as an absolute number + /// public class UnitEquipmentNumericSelection : AbstractUnitEquipmentItemSelection { public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount) @@ -15,23 +18,36 @@ public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinNumber) { } - - public override double TotalCost + + public override int NumberTaken { get { - return CalculateAmount() * EquipmentItem.Cost; + return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : (int) AmountTaken); } } - - private double CalculateAmount() - { - return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : AmountTaken); - } - protected override bool IsValidValue (double newValue) + protected bool IsWholeNumber(double newValue) { return newValue == Math.Round(newValue); } + + protected override bool IsInRange(double newValue) + { + bool isInRange = IsWholeNumber(newValue); + + if (newValue == WarFoundryCore.INFINITY) + { + isInRange = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY); + } + else if (isInRange) + { + int minLimit = (EquipmentItem.MinNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MinNumber); + int maxLimit = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MaxNumber); + isInRange = (minLimit <= newValue) && (newValue <= maxLimit); + } + + return isInRange; + } } } diff -r ced5a18d9f52 -r 95746083d037 api/Objects/UnitEquipmentRatioSelection.cs --- a/api/Objects/UnitEquipmentRatioSelection.cs Sun Aug 09 11:09:12 2009 +0000 +++ b/api/Objects/UnitEquipmentRatioSelection.cs Sun Aug 09 12:02:35 2009 +0000 @@ -19,22 +19,17 @@ public UnitEquipmentRatioSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinPercentage) { } - - public override double TotalCost + + public override int NumberTaken { get { - return CalculateAmount() * EquipmentItem.Cost; + double numberTaken = AmountTaken * EquipmentForUnit.Size; + return (int) (EquipmentItem.RoundNumberUp ? Math.Ceiling(numberTaken) : Math.Floor(numberTaken)); } } - - private double CalculateAmount() - { - double numberTaken = AmountTaken * EquipmentForUnit.Size; - return (EquipmentItem.RoundNumberUp ? Math.Ceiling(numberTaken) : Math.Floor(numberTaken)); - } - protected override bool IsValidValue (double newValue) + protected override bool IsInRange (double newValue) { return (EquipmentItem.MinPercentage <= newValue) && (newValue <= EquipmentItem.MaxPercentage); }