annotate api/Util/UnitEquipmentUtil.cs @ 202:37ad50823531

Re #208: equipmentslot limit issues * Move more code to a common method * Use new IBBMath.Percentage method to ensure consistent calculation of percentages * Move subtraction of amount already taken in slot inside IF and ELSE and tailor to situation Some tests still failing
author IBBoard <dev@ibboard.co.uk>
date Fri, 06 Nov 2009 20:11:30 +0000
parents 4d7ff70bb109
children df4e56e2ee71
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (UnitEquipmentUtil.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
81abc04b3dbe Re #192: Improve "clean coding" practice
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.
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Text;
202
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
8 using IBBoard.CustomMath;
183
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
9 using IBBoard.Limits;
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using IBBoard.WarFoundry.API.Objects;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 namespace IBBoard.WarFoundry.API.Util
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 {
162
624422e91a1c Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents: 161
diff changeset
14 public class UnitEquipmentUtil
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 /// <summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 /// Gets an array of allowed <see cref="UnitEquipmentItem"/>s based on the current selections of the unit, taking in to account Mutex groups and other limits.
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 /// </summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 /// <param name="unit">The <see cref="Unit"/> to get equipment items for</param>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 /// <returns>The array of allowed <see cref="UnitEquipmentItem"/>s</returns>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 public static UnitEquipmentItem[] GetAllowedEquipmentItems(Unit unit)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 UnitEquipmentItem[] currItems = unit.GetEquipment();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 foreach (UnitEquipmentItem item in GetAllEquipmentItems(unit))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 bool allowed = IsAllowedByMutex(item, currItems);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 if (allowed)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 list.Add(item);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 return list.ToArray();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 private static bool IsAllowedByMutex(UnitEquipmentItem item, UnitEquipmentItem[] currItems)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 bool allowed = true;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 foreach (UnitEquipmentItem currItem in currItems)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 if (ItemsAreMutuallyExclusive(currItem, item))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 allowed = false;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 break;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 return allowed;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 /// <summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 /// Gets a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code> because of mutex groups.
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 /// </summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 /// <param name="unit">The unit that wants to take the equipment item</param>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 /// <param name="item">The item to check blocking items for</param>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 /// <returns>a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code></returns>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 public static List<UnitEquipmentItem> GetBlockingEquipmentItems(Unit unit, UnitEquipmentItem item)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 List<UnitEquipmentItem> items = new List<UnitEquipmentItem>();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 UnitEquipmentItem[] currItems = unit.GetEquipment();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65
174
33433862467f * Make use of local variable
IBBoard <dev@ibboard.co.uk>
parents: 162
diff changeset
66 foreach (UnitEquipmentItem unitItem in currItems)
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 {
194
1412a42190a1 * Fix mutex group clash checking
IBBoard <dev@ibboard.co.uk>
parents: 190
diff changeset
68 if (ItemsAreMutuallyExclusive(unitItem, item))
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 items.Add(unitItem);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 return items;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 public static UnitEquipmentItem[] GetAllEquipmentItems(Unit unit)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 return unit.UnitType.GetEquipmentItems();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 public static bool ItemsAreMutuallyExclusive(UnitEquipmentItem item1, UnitEquipmentItem item2)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 bool areMutex = false;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 string[] item1mutex = item1.MutexGroups;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 string[] item2mutex = item2.MutexGroups;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 foreach (string mutex in item1mutex)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 foreach (string otherMutex in item2mutex)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 if (mutex.Equals(otherMutex))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 areMutex = true;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 goto postLoop;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 postLoop:
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 return areMutex;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102 }
182
6fe336109128 Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents: 174
diff changeset
103
6fe336109128 Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents: 174
diff changeset
104 public static int GetMaxEquipmentCount (Unit unit, UnitEquipmentItem equip)
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
105 {
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
106 int unitSize = unit.Size;
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
107 int max = Math.Max(equip.MinLimit.GetLimit(unitSize), equip.MaxLimit.GetLimit (unitSize));
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
108 return GetEquipmentCountLimit (unit, max, equip);
185
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
109 }
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
110
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
111 private static int GetEquipmentCountLimit (Unit unit, int currLimit, UnitEquipmentItem equip)
185
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
112 {
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
113 int newLimit = currLimit;
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
114 AbstractLimit limit = GetSlotLimitForItem(unit, equip);
183
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
115
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
116 if (!(limit is UnlimitedLimit))
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
117 {
201
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
118 int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip);
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
119 newLimit = Math.Min (slotMax, newLimit);
183
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
120 }
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
121
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
122 return newLimit;
182
6fe336109128 Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents: 174
diff changeset
123 }
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
124
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
125 private static AbstractLimit GetSlotLimitForItem(Unit unit, UnitEquipmentItem equip)
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
126 {
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
127 return unit.UnitType.GetEquipmentSlotLimit(equip.SlotName);
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
128 }
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
129
186
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
130
185
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
131 public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip)
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
132 {
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
133 int unitSize = unit.Size;
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
134 int min = Math.Min (equip.MinLimit.GetLimit (unitSize), equip.MaxLimit.GetLimit (unitSize));
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
135 return GetEquipmentCountLimit (unit, min, equip);
185
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
136 }
186
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
137
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
138 public static bool IsEquipmentRatioLimited(Unit unit, UnitEquipmentItem equip)
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
139 {
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
140 AbstractLimit limit = GetSlotLimitForItem(unit, equip);
186
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
141 return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit);
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
142 }
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
143
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
144 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
145 {
202
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
146 return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MaxLimit, unit);
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
147 }
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
148
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
149 private static double GetPercentageOfUnitSize(int number, Unit unit)
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
150 {
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
151 return IBBMath.Percentage(number, unit.Size);
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
152 }
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
153
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
154 private static double GetMinOfSlotLimitAndEquipmentLimit(UnitEquipmentItem equip, AbstractLimit equipLimit, Unit unit)
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
155 {
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
156 double limit = 0;
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
157 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
201
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
158
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
159 if (slotLimit is IPercentageLimit)
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
160 {
201
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
161 limit = ((IPercentageLimit)slotLimit).Percentage;
202
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
162 limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
163 }
188
354d51cc002b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 187
diff changeset
164 else
354d51cc002b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 187
diff changeset
165 {
202
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
166 int remaining = slotLimit.GetLimit(unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip);
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
167 limit = GetPercentageOfUnitSize(remaining, unit);
201
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
168 }
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
169
202
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
170 if (equip.IsRatioLimit)
201
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
171 {
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
172 limit = Math.Min(limit, ((IPercentageLimit)equipLimit).Percentage);
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
173 }
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
174 else
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
175 {
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
176 limit = Math.Min(limit, GetPercentageOfUnitSize(equipLimit.GetLimit(unit.Size), unit));
4d7ff70bb109 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 196
diff changeset
177 }
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
178
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
179 return limit;
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
180 }
190
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
181
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
182 public static double GetMinEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
202
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
183 {
37ad50823531 Re #208: equipmentslot limit issues
IBBoard <dev@ibboard.co.uk>
parents: 201
diff changeset
184 return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MinLimit, unit);
190
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
185 }
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
186 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
187 }