view api/Objects/UnitEquipmentNumericForRatioSelection.cs @ 136:413a7a049e41

Fixes #146: Incorrect logic in Unit.AddEquipmentAmount() * Logic error was in UnitEquipmentNumericForRatioSelection where we didn't treat percentages as decimal numbers and got ranges of "125 to 500" for a unit of 5!
author IBBoard <dev@ibboard.co.uk>
date Tue, 08 Sep 2009 19:27:51 +0000
parents 2f3cafb69799
children 70808ff799e6
line source
1 // This file (UnitEquipmentNumericForRatioSelection.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
5 using System;
6 using IBBoard.Lang;
8 namespace IBBoard.WarFoundry.API.Objects
9 {
10 /// <summary>
11 /// An object to hold the selection of a unit's equipment where the selection was made as an absolute number and the
12 /// equipment item has a ratio limit
13 /// </summary>
14 public class UnitEquipmentNumericForRatioSelection : UnitEquipmentNumericSelection
15 {
16 public UnitEquipmentNumericForRatioSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
17 {
18 }
20 public UnitEquipmentNumericForRatioSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, IBBMath.Round(unit.Size * item.MinPercentage, item.RoundNumberUp))
21 {
22 }
24 protected override bool IsInRange (double newValue)
25 {
26 int minLimit = (int) IBBMath.Round(EquipmentForUnit.Size * EquipmentItem.MinPercentage / 100, EquipmentItem.RoundNumberUp);
27 int maxLimit = (int)IBBMath.Round(EquipmentForUnit.Size * EquipmentItem.MaxPercentage / 100, EquipmentItem.RoundNumberUp);
28 newValue = (newValue == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : newValue);
29 bool isInRange = (minLimit <= newValue) && (newValue <= maxLimit);
30 return isInRange && IsWholeNumber(newValue);
31 }
32 }
33 }