Mercurial > repos > snowblizz-super-API-ideas
annotate api/Objects/Unit.cs @ 54:3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
* Remove min/max from EquipmentItem and add description
* Add min/max numbers and percentages to UnitEquipmentItem
* Make Race schema define min/max number without the ratio (which is the percentage)
* Replace use of EquipmentItem with UnitEquipmentItem because of increased use of UnitEquipmentItem for unit-specific data
* Use doubles instead of floats for equipment amounts
* Distinguish between ratio and absolute limits
* Delete UnitEquipmentItemObj helper class that was purely used for UI
Re #9 - Use smaller methods
* Deprecate long Race and EquipmentItem constructors and ensure all getters/setters exist
Also:
* Migrate Unit to using genericed collections
* Always use GameSystem object for Race, not ID string
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 05 Apr 2009 13:45:23 +0000 |
parents | 548cfc776f54 |
children | 3c77722a02b5 |
rev | line source |
---|---|
15 | 1 // This file (Unit.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. |
2 // | |
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
4 | |
0 | 5 using System; |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
6 using System.Collections.Generic; |
0 | 7 using System.Text; |
8 using System.Xml; | |
9 using IBBoard.Lang; | |
10 | |
11 namespace IBBoard.WarFoundry.API.Objects | |
12 { | |
13 /// <summary> | |
14 /// Summary description for UnitInstance. | |
15 /// </summary> | |
16 public class Unit : WarFoundryObject | |
17 { | |
18 private UnitType type; | |
19 private int size; | |
20 private Army army; | |
21 private Race race; | |
22 private double points; | |
23 private ArmyCategory cat; | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
24 private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>(); |
0 | 25 public event DoubleValChangedDelegate PointsValueChanged; |
26 public event IntValChangedDelegate UnitSizeChanged; | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
27 public event DoubleValChangedDelegate UnitEquipmentAmountChanged; |
0 | 28 |
29 public Unit(UnitType unitType, Army parentArmy) : this(unitType, unitType.MinSize, parentArmy){} | |
30 | |
31 public Unit(UnitType unitType, int startSize, Army parentArmy) | |
32 { | |
33 Army = parentArmy; | |
34 type = unitType; | |
35 Size = startSize; | |
36 setInitialEquipment(); | |
37 CalcCost(); | |
38 } | |
39 | |
40 protected override string DefaultName() | |
41 { | |
42 if (type != null) | |
43 { | |
44 if (size == 1) | |
45 { | |
46 return type.Name; | |
47 } | |
48 else | |
49 { | |
50 return String.Format(Translation.GetTranslation("defaultUnitName"), size, type.Name); | |
51 } | |
52 } | |
53 else | |
54 { | |
55 return "Unknown Unit"; | |
56 } | |
57 } | |
58 | |
59 private void setInitialEquipment() | |
60 { | |
61 foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems()) | |
62 { | |
63 if (unitEquip.IsRequired) | |
64 { | |
65 if (CanEquipWithItem(unitEquip.ID)) | |
66 { | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
67 equipment[unitEquip] = unitEquip.MinNumber; |
0 | 68 } |
69 } | |
70 } | |
71 } | |
72 | |
73 private void CalcCost() | |
74 { | |
75 String oldName = HasDefaultName() ? Name : null; | |
76 double oldpoints = points; | |
77 points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost; | |
78 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
79 foreach (UnitEquipmentItem unitEquipItem in equipment.Keys) |
0 | 80 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
81 EquipmentItem equipItem = unitEquipItem.EquipmentItem; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
82 double count = (double)equipment[unitEquipItem]; |
0 | 83 |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
84 if (unitEquipItem.IsRatioLimit) |
0 | 85 { |
86 if (unitEquipItem.RoundNumberUp) | |
87 { | |
88 points+= Math.Ceiling(size * count) * equipItem.Cost; | |
89 } | |
90 else | |
91 { | |
92 points+= Math.Floor(size * count) * equipItem.Cost; | |
93 } | |
94 } | |
95 else | |
96 { | |
38
548cfc776f54
Fixes #34 - Remove "Choices" and "Base/Increment" from Category
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
97 if (count == WarFoundryCore.INFINITY) |
0 | 98 { |
99 points+= size * equipItem.Cost; | |
100 } | |
101 else | |
102 { | |
103 points+= count * equipItem.Cost; | |
104 } | |
105 } | |
106 } | |
107 | |
108 if (oldpoints!=points) | |
109 { | |
110 OnPointsValueChanged(oldpoints, points); | |
111 } | |
112 | |
113 if (oldName!=null) | |
114 { | |
115 OnNameChanged(oldName, Name); | |
116 } | |
117 } | |
118 | |
119 public int AdditionalTroopers | |
120 { | |
121 get { return Math.Max(Size - type.BaseSize, 0); } | |
122 } | |
123 | |
124 public int Size | |
125 { | |
126 get { return size; } | |
127 set | |
128 { | |
129 if (value!=size) | |
130 { | |
131 int oldValue = size; | |
132 size = (value>0 ? value : 1); | |
133 CalcCost(); | |
134 OnUnitSizeChanged(oldValue, size); | |
135 } | |
136 } | |
137 } | |
138 | |
139 public UnitType UnitType | |
140 { | |
141 get { return type; } | |
142 } | |
143 | |
144 public Army Army | |
145 { | |
146 get { return army; } | |
147 set | |
148 { | |
149 army = value; | |
150 | |
151 if (army == null) | |
152 { | |
153 Category = null; | |
154 } | |
155 } | |
156 } | |
157 | |
158 public Race Race | |
159 { | |
160 get { return race; } | |
161 set { race = value; } | |
162 } | |
163 | |
164 public ArmyCategory Category | |
165 { | |
166 get | |
167 { | |
168 if (cat==null) | |
169 { | |
170 if (Army!=null) | |
171 { | |
172 return Army.GetCategory(UnitType.MainCategory); | |
173 } | |
174 else | |
175 { | |
176 return null; | |
177 } | |
178 } | |
179 else | |
180 { | |
181 return cat; | |
182 } | |
183 } | |
184 set { cat = value; } | |
185 } | |
186 | |
187 public double PointsValue | |
188 { | |
189 get | |
190 { | |
191 if (points == 0) | |
192 { | |
193 CalcCost(); | |
194 } | |
195 | |
196 return points; | |
197 } | |
198 } | |
199 | |
200 public UnitEquipmentItem[] GetAllowedOptionalEquipment() | |
201 { | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
202 List<UnitEquipmentItem> list = new List<UnitEquipmentItem>(); |
0 | 203 |
204 foreach (UnitEquipmentItem item in UnitType.GetEquipmentItems()) | |
205 { | |
206 if (!item.IsRequired) | |
207 { | |
208 list.Add(item); | |
209 } | |
210 } | |
211 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
212 return (UnitEquipmentItem[])list.ToArray(); |
0 | 213 } |
214 | |
215 public UnitEquipmentItem[] GetEquipment() | |
216 { | |
217 UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count]; | |
218 int i = 0; | |
219 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
220 foreach (UnitEquipmentItem item in equipment.Keys) |
0 | 221 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
222 items[i++] = item; |
0 | 223 } |
224 | |
225 return items; | |
226 } | |
227 | |
228 public UnitEquipmentItem[] GetRequiredEquipment() | |
229 { | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
230 List<UnitEquipmentItem> list = new List<UnitEquipmentItem>(); |
0 | 231 |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
232 foreach(UnitEquipmentItem item in equipment.Keys) |
0 | 233 { |
234 if (item.IsRequired) | |
235 { | |
236 list.Add(item); | |
237 } | |
238 } | |
239 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
240 return (UnitEquipmentItem[])list.ToArray(); |
0 | 241 } |
242 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
243 public double GetEquipmentAmount(UnitEquipmentItem item) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
244 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
245 if (equipment.ContainsKey(item)) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
246 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
247 return (double)equipment[item]; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
248 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
249 else |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
250 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
251 return 0; |
0 | 252 } |
253 } | |
254 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
255 public double GetEquipmentAmount(string equipID) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
256 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
257 return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID)); |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
258 } |
0 | 259 |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
260 public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
261 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
262 if (equip.IsRatioLimit) |
0 | 263 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
264 throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has a ratio limit, not an absolute limit"); |
0 | 265 } |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
266 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
267 if (amount <1 && amount != WarFoundryCore.INFINITY) |
0 | 268 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
269 amount = 0; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
270 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
271 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
272 SetEquipmentAmount(equip, amount); |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
273 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
274 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
275 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
276 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
277 if (!equip.IsRatioLimit) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
278 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
279 throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has an absolute limit, not a ratio limit"); |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
280 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
281 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
282 if (ratio > 1) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
283 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
284 ratio = 1; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
285 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
286 else if (ratio < 0) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
287 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
288 ratio = 0; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
289 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
290 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
291 SetEquipmentAmount(equip, ratio); |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
292 } |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
293 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
294 private void SetEquipmentAmount(UnitEquipmentItem equip, double amount) |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
295 { |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
296 double oldAmount = 0; |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
297 |
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
298 if (equipment.ContainsKey(equip)) |
0 | 299 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
300 oldAmount = equipment[equip]; |
0 | 301 } |
302 | |
303 if (amount!=oldAmount) | |
304 { | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
305 if (amount != 0) |
0 | 306 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
307 equipment[equip] = amount; |
0 | 308 } |
309 else | |
310 { | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
311 equipment.Remove(equip); |
0 | 312 } |
313 | |
314 OnUnitEquipmentAmountChanged(equip, oldAmount, amount); | |
315 CalcCost(); | |
316 } | |
317 } | |
318 | |
319 public bool CanEquipWithItem(string equipID) | |
320 { | |
321 string mutex = UnitType.GetEquipmentItem(equipID).MutexGroup; | |
322 | |
323 if (mutex == "") | |
324 { | |
325 return true; | |
326 } | |
327 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
328 foreach (UnitEquipmentItem item in equipment.Keys) |
0 | 329 { |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
330 if (item.MutexGroup == mutex) |
0 | 331 { |
332 return false; | |
333 } | |
334 } | |
335 | |
336 return true; | |
337 } | |
338 | |
339 private void OnPointsValueChanged(double oldValue, double newValue) | |
340 { | |
341 if (PointsValueChanged!=null) | |
342 { | |
343 PointsValueChanged(this, oldValue, newValue); | |
344 } | |
345 } | |
346 | |
347 private void OnUnitSizeChanged(int oldValue, int newValue) | |
348 { | |
349 if (UnitSizeChanged!=null) | |
350 { | |
351 UnitSizeChanged(this, oldValue, newValue); | |
352 } | |
353 } | |
354 | |
54
3a90f70dac73
Re #61 - Complete structure of WarFoundry API objects
IBBoard <dev@ibboard.co.uk>
parents:
38
diff
changeset
|
355 private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue) |
0 | 356 { |
357 if (UnitEquipmentAmountChanged!=null) | |
358 { | |
359 UnitEquipmentAmountChanged(equip, oldValue, newValue); | |
360 } | |
361 } | |
362 | |
363 public Stats UnitStats | |
364 { | |
365 get { return UnitType.UnitStats; } | |
366 } | |
367 } | |
368 } |