Mercurial > repos > snowblizz-super-API-ideas
annotate api/Util/UnitEquipmentUtil.cs @ 187:104a683b5ef8
Re #198: Add equipment slots
* Fix problem with shared "get limit" method not handling min and max properly
* Add method to get percentage limit
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 27 Oct 2009 20:53:27 +0000 |
parents | c8682d206a73 |
children | 354d51cc002b |
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 | 65 foreach (UnitEquipmentItem unitItem in currItems) |
161
81abc04b3dbe
Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 { |
81abc04b3dbe
Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 if (!ItemsAreMutuallyExclusive(unitItem, item)) |
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 | 104 { |
105 int unitSize = unit.Size; | |
106 int max = Math.Max(equip.MinLimit.GetLimit(unitSize), equip.MaxLimit.GetLimit (unitSize)); | |
107 return GetEquipmentCountLimit (unit, max, equip); | |
185 | 108 } |
109 | |
187 | 110 private static int GetEquipmentCountLimit (Unit unit, int currLimit, UnitEquipmentItem equip) |
185 | 111 { |
187 | 112 int newLimit = currLimit; |
185 | 113 AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit (equip.SlotName); |
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 | 117 int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlot (equip.SlotName); |
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 | 121 return newLimit; |
182
6fe336109128
Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents:
174
diff
changeset
|
122 } |
186 | 123 |
185 | 124 public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip) |
187 | 125 { |
126 int unitSize = unit.Size; | |
127 int min = Math.Min (equip.MinLimit.GetLimit (unitSize), equip.MaxLimit.GetLimit (unitSize)); | |
128 return GetEquipmentCountLimit (unit, min, equip); | |
185 | 129 } |
186 | 130 |
131 public static bool IsEquipmentRatioLimited(Unit unit, UnitEquipmentItem equip) | |
132 { | |
133 AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit(equip.SlotName); | |
134 return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit); | |
135 } | |
187 | 136 |
137 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip) | |
138 { | |
139 double limit = 0; | |
140 | |
141 if (equip.IsRatioLimit) | |
142 { | |
143 limit = Math.Max(((IPercentageLimit)equip.MinLimit).Percentage, ((IPercentageLimit)equip.MaxLimit).Percentage); | |
144 } | |
145 | |
146 return limit; | |
147 } | |
161
81abc04b3dbe
Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
148 } |
81abc04b3dbe
Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
149 } |