annotate API/Objects/UnitEquipmentRatioSelection.cs @ 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 3c4a6403a88c
children c8002429ab45
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (UnitEquipmentRatioSelection.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using IBBoard.CustomMath;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using IBBoard.Limits;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using IBBoard.WarFoundry.API.Util;
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 namespace IBBoard.WarFoundry.API.Objects
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 /// <summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 /// An object to hold the selection of a unit's equipment where the selection was made as a percentage or ratio
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 /// of the total size of the unit
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 /// </summary>
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 public class UnitEquipmentRatioSelection : AbstractUnitEquipmentItemSelection
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 public UnitEquipmentRatioSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 public UnitEquipmentRatioSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, ((IPercentageLimit)item.MinLimit).Percentage)
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 public override int NumberTaken
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 get
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
429
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
30 return CalculateNumberTaken(EquipmentForUnit, EquipmentItem, AmountTaken);
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
31 }
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
32 }
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
33
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
34 public override int RawNumberTaken
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
35 {
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
36 get
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
37 {
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
38 return CalculateRawNumberTaken(EquipmentForUnit, EquipmentItem, AmountTaken);
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41
429
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
42 internal static int CalculateNumberTaken(Unit unit, UnitEquipmentItem item, double ratioTaken)
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
43 {
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
44 int wholeNumberTaken = CalculateRawNumberTaken(unit, item, ratioTaken);
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
45 int maxTaken = Int32.MaxValue;
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
46 int minTaken = 0;
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
47
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
48 if (wholeNumberTaken > 0)
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
49 {
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
50 maxTaken = UnitEquipmentUtil.GetMaxEquipmentCount(unit, item);
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
51 }
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
52
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
53 minTaken = UnitEquipmentUtil.GetMinEquipmentCount(unit, item);
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
54 return Math.Min(Math.Max(wholeNumberTaken, minTaken), maxTaken);
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
55 }
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
56
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
57 private static int CalculateRawNumberTaken (Unit unit, UnitEquipmentItem item, double ratioTaken)
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 {
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 double exactNumberTaken = (ratioTaken / 100) * unit.Size;
429
7179c585d31d Re #356: Stack overflow with some equipment limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
60 return (int)IBBMath.Round(exactNumberTaken, item.RoundNumberUp);
337
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 }
3c4a6403a88c * Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 }