Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Util/UnitEquipmentUtil.cs @ 185:3577bfb7702b
Re #198: Add equipment slots
* Add GetMinEquipmentAmount method
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 27 Oct 2009 20:17:40 +0000 |
parents | 36adabb1c3ea |
children | c8682d206a73 |
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) |
6fe336109128
Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents:
174
diff
changeset
|
104 { |
185 | 105 return GetEquipmentCountLimit (unit, equip); |
106 } | |
107 | |
108 private static int GetEquipmentCountLimit (Unit unit, UnitEquipmentItem equip) | |
109 { | |
110 int unitSize = unit.Size; | |
111 int max = Math.Min (equip.MinLimit.GetLimit (unitSize), equip.MaxLimit.GetLimit (unitSize)); | |
112 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
|
113 |
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
182
diff
changeset
|
114 if (!(limit is UnlimitedLimit)) |
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
182
diff
changeset
|
115 { |
185 | 116 int slotMax = limit.GetLimit (unitSize) - unit.GetEquipmentAmountInSlot (equip.SlotName); |
117 max = Math.Min (slotMax, max); | |
183
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
182
diff
changeset
|
118 } |
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 return max; |
182
6fe336109128
Re #198: Add slots with count to unit
IBBoard <dev@ibboard.co.uk>
parents:
174
diff
changeset
|
121 } |
185 | 122 |
123 | |
124 public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip) | |
125 { | |
126 return GetEquipmentCountLimit (unit, equip); | |
127 } | |
161
81abc04b3dbe
Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
128 } |
81abc04b3dbe
Re #192: Improve "clean coding" practice
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
129 } |