annotate api/Util/UnitEquipmentUtil.cs @ 196:4bbf5624ced6

* Only check current amount of equipment when the slot isn't unlimited (needs unit test writing) no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Fri, 30 Oct 2009 20:58:19 +0000
parents 1412a42190a1
children 4d7ff70bb109
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;
183
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
8 using IBBoard.Limits;
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 using IBBoard.WarFoundry.API.Objects;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 namespace IBBoard.WarFoundry.API.Util
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 {
162
624422e91a1c Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents: 161
diff changeset
13 public class UnitEquipmentUtil
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 /// <summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 /// 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
17 /// </summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 /// <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
19 /// <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
20 public static UnitEquipmentItem[] GetAllowedEquipmentItems(Unit unit)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 UnitEquipmentItem[] currItems = unit.GetEquipment();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 foreach (UnitEquipmentItem item in GetAllEquipmentItems(unit))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 bool allowed = IsAllowedByMutex(item, currItems);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 if (allowed)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 list.Add(item);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 }
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 return list.ToArray();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 }
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 private static bool IsAllowedByMutex(UnitEquipmentItem item, UnitEquipmentItem[] currItems)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 bool allowed = true;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 foreach (UnitEquipmentItem currItem in currItems)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 if (ItemsAreMutuallyExclusive(currItem, item))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 allowed = false;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 break;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 }
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 return allowed;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 }
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 /// <summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 /// 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
56 /// </summary>
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 /// <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
58 /// <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
59 /// <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
60 public static List<UnitEquipmentItem> GetBlockingEquipmentItems(Unit unit, UnitEquipmentItem item)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 List<UnitEquipmentItem> items = new List<UnitEquipmentItem>();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 UnitEquipmentItem[] currItems = unit.GetEquipment();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64
174
33433862467f * Make use of local variable
IBBoard <dev@ibboard.co.uk>
parents: 162
diff changeset
65 foreach (UnitEquipmentItem unitItem in currItems)
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 {
194
1412a42190a1 * Fix mutex group clash checking
IBBoard <dev@ibboard.co.uk>
parents: 190
diff changeset
67 if (ItemsAreMutuallyExclusive(unitItem, item))
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 items.Add(unitItem);
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 }
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 return items;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 }
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 public static UnitEquipmentItem[] GetAllEquipmentItems(Unit unit)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 return unit.UnitType.GetEquipmentItems();
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 }
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 public static bool ItemsAreMutuallyExclusive(UnitEquipmentItem item1, UnitEquipmentItem item2)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 bool areMutex = false;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 string[] item1mutex = item1.MutexGroups;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 string[] item2mutex = item2.MutexGroups;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 foreach (string mutex in item1mutex)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 foreach (string otherMutex in item2mutex)
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 if (mutex.Equals(otherMutex))
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 {
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 areMutex = true;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 goto postLoop;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 }
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 postLoop:
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 return areMutex;
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 }
182
6fe336109128 Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents: 174
diff changeset
102
6fe336109128 Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents: 174
diff changeset
103 public static int GetMaxEquipmentCount (Unit unit, UnitEquipmentItem equip)
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
104 {
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
105 int unitSize = unit.Size;
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
106 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
107 return GetEquipmentCountLimit (unit, max, equip);
185
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
108 }
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
109
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
110 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
111 {
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
112 int newLimit = currLimit;
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
113 AbstractLimit limit = GetSlotLimitForItem(unit, equip);
183
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
114
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
115 if (!(limit is UnlimitedLimit))
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
116 {
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
117 int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlot (equip.SlotName);
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
118 newLimit = Math.Min (slotMax, newLimit);
183
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
119 }
36adabb1c3ea Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents: 182
diff changeset
120
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
121 return newLimit;
182
6fe336109128 Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents: 174
diff changeset
122 }
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
123
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
124 private static AbstractLimit GetSlotLimitForItem(Unit unit, UnitEquipmentItem equip)
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
125 {
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
126 return unit.UnitType.GetEquipmentSlotLimit(equip.SlotName);
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
127 }
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
128
186
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
129
185
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
130 public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip)
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
131 {
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
132 int unitSize = unit.Size;
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
133 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
134 return GetEquipmentCountLimit (unit, min, equip);
185
3577bfb7702b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 183
diff changeset
135 }
186
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
136
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
137 public static bool IsEquipmentRatioLimited(Unit unit, UnitEquipmentItem equip)
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
138 {
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
139 AbstractLimit limit = GetSlotLimitForItem(unit, equip);
186
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
140 return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit);
c8682d206a73 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 185
diff changeset
141 }
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
142
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
143 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
144 {
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
145 double limit = 0;
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
146 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
196
4bbf5624ced6 * Only check current amount of equipment when the slot isn't unlimited (needs unit test writing)
IBBoard <dev@ibboard.co.uk>
parents: 194
diff changeset
147
4bbf5624ced6 * Only check current amount of equipment when the slot isn't unlimited (needs unit test writing)
IBBoard <dev@ibboard.co.uk>
parents: 194
diff changeset
148
4bbf5624ced6 * Only check current amount of equipment when the slot isn't unlimited (needs unit test writing)
IBBoard <dev@ibboard.co.uk>
parents: 194
diff changeset
149 if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || (!(slotLimit is UnlimitedLimit) && unit.GetEquipmentAmountInSlotWithoutItem(equip) != 0))
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
150 {
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
151 limit = (GetMaxEquipmentCount(unit, equip) / (double)unit.Size) * 100.0;
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
152 }
188
354d51cc002b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 187
diff changeset
153 else
354d51cc002b Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 187
diff changeset
154 {
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
155 if (equip.IsRatioLimit)
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
156 {
190
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
157 limit = ((IPercentageLimit)equip.MaxLimit).Percentage;
189
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
158 }
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
159 else
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
160 {
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
161 int unitSize = unit.Size;
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
162 limit = (equip.MaxLimit.GetLimit(unitSize) / (double)unitSize) * 100.0;
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
163 }
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
164
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
165 if (slotLimit is IPercentageLimit)
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
166 {
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
167 limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit);
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
168 }
0b413f41e6e3 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 188
diff changeset
169 }
187
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
170
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
171 return limit;
104a683b5ef8 Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 186
diff changeset
172 }
190
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
173
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
174 public static double GetMinEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
175 {
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
176 double limit = 0;
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
177 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
178
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
179
196
4bbf5624ced6 * Only check current amount of equipment when the slot isn't unlimited (needs unit test writing)
IBBoard <dev@ibboard.co.uk>
parents: 194
diff changeset
180 if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || (!(slotLimit is UnlimitedLimit) && unit.GetEquipmentAmountInSlotWithoutItem(equip) != 0))
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 limit = (GetMinEquipmentCount(unit, equip) / (double)unit.Size) * 100.0;
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
183 }
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
184 else
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
185 {
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
186 if (equip.IsRatioLimit)
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
187 {
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
188 limit = ((IPercentageLimit)equip.MinLimit).Percentage;
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
189 }
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
190 else
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
191 {
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
192 int unitSize = unit.Size;
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
193 limit = (equip.MinLimit.GetLimit(unitSize) / (double)unitSize) * 100.0;
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
194 }
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
195
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
196 if (slotLimit is IPercentageLimit)
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
197 {
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
198 limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit);
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
199 }
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
200 }
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
201
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
202 return limit;
6f67f16a67fb Re #198: Add equipment slots
IBBoard <dev@ibboard.co.uk>
parents: 189
diff changeset
203 }
161
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
204 }
81abc04b3dbe Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
205 }