comparison api/Objects/Unit.cs @ 64:2094bd0ba652

Re #61 - Complete structure of WarFoundry API objects * Make Unit work with EquipmentItem objects as it doesn't need to know about the limits * Add methods to UnitType to check limits on an EquipmentItem Also: * Remove some unnecessary casting now that we're using genericed lists
author IBBoard <dev@ibboard.co.uk>
date Thu, 09 Apr 2009 15:36:22 +0000
parents 3c77722a02b5
children aa2d05a9c635
comparison
equal deleted inserted replaced
63:a920b1bcb408 64:2094bd0ba652
19 private int size; 19 private int size;
20 private Army army; 20 private Army army;
21 private Race race; 21 private Race race;
22 private double points; 22 private double points;
23 private ArmyCategory cat; 23 private ArmyCategory cat;
24 private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>(); 24 private Dictionary<EquipmentItem, double> equipment = new Dictionary<EquipmentItem, double>();
25 private List<Unit> containedUnits = new List<Unit>(); 25 private List<Unit> containedUnits = new List<Unit>();
26 public event DoubleValChangedDelegate PointsValueChanged; 26 public event DoubleValChangedDelegate PointsValueChanged;
27 public event IntValChangedDelegate UnitSizeChanged; 27 public event IntValChangedDelegate UnitSizeChanged;
28 public event DoubleValChangedDelegate UnitEquipmentAmountChanged; 28 public event DoubleValChangedDelegate UnitEquipmentAmountChanged;
29 29
32 public Unit(UnitType unitType, int startSize, Army parentArmy) 32 public Unit(UnitType unitType, int startSize, Army parentArmy)
33 { 33 {
34 Army = parentArmy; 34 Army = parentArmy;
35 type = unitType; 35 type = unitType;
36 Size = startSize; 36 Size = startSize;
37 setInitialEquipment(); 37 SetInitialEquipment();
38 CalcCost(); 38 CalcCost();
39 } 39 }
40 40
41 protected override string DefaultName() 41 protected override string DefaultName()
42 { 42 {
55 { 55 {
56 return "Unknown Unit"; 56 return "Unknown Unit";
57 } 57 }
58 } 58 }
59 59
60 private void setInitialEquipment() 60 private void SetInitialEquipment()
61 { 61 {
62 foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems()) 62 foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems())
63 { 63 {
64 if (unitEquip.IsRequired) 64 if (unitEquip.IsRequired)
65 { 65 {
66 if (CanEquipWithItem(unitEquip.ID)) 66 if (CanEquipWithItem(unitEquip))
67 { 67 {
68 equipment[unitEquip] = unitEquip.MinNumber; 68 equipment[unitEquip.EquipmentItem] = unitEquip.MinNumber;
69 } 69 }
70 } 70 }
71 } 71 }
72 } 72 }
73 73
75 { 75 {
76 String oldName = HasDefaultName() ? Name : null; 76 String oldName = HasDefaultName() ? Name : null;
77 double oldpoints = points; 77 double oldpoints = points;
78 points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost; 78 points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost;
79 79
80 foreach (UnitEquipmentItem unitEquipItem in equipment.Keys) 80 foreach (EquipmentItem equipItem in equipment.Keys)
81 { 81 {
82 EquipmentItem equipItem = unitEquipItem.EquipmentItem; 82 double count = equipment[equipItem];
83 double count = (double)equipment[unitEquipItem]; 83 UnitEquipmentItem unitEquipItem = UnitType.GetEquipmentItem(equipItem);
84 84
85 if (unitEquipItem.IsRatioLimit) 85 if (unitEquipItem.IsRatioLimit)
86 { 86 {
87 if (unitEquipItem.RoundNumberUp) 87 if (unitEquipItem.RoundNumberUp)
88 { 88 {
233 { 233 {
234 list.Add(item); 234 list.Add(item);
235 } 235 }
236 } 236 }
237 237
238 return (UnitEquipmentItem[])list.ToArray(); 238 return list.ToArray();
239 } 239 }
240 240
241 public UnitEquipmentItem[] GetEquipment() 241 public EquipmentItem[] GetEquipment()
242 { 242 {
243 UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count]; 243 return DictionaryUtils.ToKeyArray(equipment);
244 int i = 0; 244 }
245 245
246 foreach (UnitEquipmentItem item in equipment.Keys) 246 private UnitEquipmentItem[] GetUnitEquipmentItemsForEquipment()
247 { 247 {
248 items[i++] = item; 248 EquipmentItem[] items = GetEquipment();
249 } 249 int arrLength = items.Length;
250 250 UnitEquipmentItem[] unitItems = new UnitEquipmentItem[arrLength];
251 return items; 251
252 } 252 for (int i = 0; i < arrLength; i++)
253 253 {
254 public UnitEquipmentItem[] GetRequiredEquipment() 254 UnitType.GetEquipmentItem(items[i]);
255 { 255 }
256 List<UnitEquipmentItem> list = new List<UnitEquipmentItem>(); 256
257 257 return unitItems;
258 foreach(UnitEquipmentItem item in equipment.Keys) 258 }
259
260 public EquipmentItem[] GetRequiredEquipment()
261 {
262 List<EquipmentItem> list = new List<EquipmentItem>();
263
264 foreach(UnitEquipmentItem item in GetUnitEquipmentItemsForEquipment())
259 { 265 {
260 if (item.IsRequired) 266 if (item.IsRequired)
261 { 267 {
262 list.Add(item); 268 list.Add(item.EquipmentItem);
263 } 269 }
264 } 270 }
265 271
266 return (UnitEquipmentItem[])list.ToArray(); 272 return list.ToArray();
267 } 273 }
268 274
269 public double GetEquipmentAmount(UnitEquipmentItem item) 275 public double GetEquipmentAmount(UnitEquipmentItem item)
270 { 276 {
277 return GetEquipmentAmount(item.EquipmentItem);
278 }
279
280 public double GetEquipmentAmount(EquipmentItem item)
281 {
271 if (equipment.ContainsKey(item)) 282 if (equipment.ContainsKey(item))
272 { 283 {
273 return (double)equipment[item]; 284 return equipment[item];
274 } 285 }
275 else 286 else
276 { 287 {
277 return 0; 288 return 0;
278 } 289 }
280 291
281 public double GetEquipmentAmount(string equipID) 292 public double GetEquipmentAmount(string equipID)
282 { 293 {
283 return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID)); 294 return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID));
284 } 295 }
285 296
286 public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) 297 public void SetEquipmentAmount(UnitEquipmentItem equip, int amount)
287 { 298 {
288 if (equip.IsRatioLimit) 299 SetEquipmentAmount(equip.EquipmentItem, amount);
289 { 300 }
290 throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has a ratio limit, not an absolute limit"); 301
302 public void SetEquipmentAmount(EquipmentItem equip, int amount)
303 {
304 if (UnitType.IsRatioLimitedEquipmentItem(equip))
305 {
306 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit");
291 } 307 }
292 308
293 if (amount <1 && amount != WarFoundryCore.INFINITY) 309 if (amount <1 && amount != WarFoundryCore.INFINITY)
294 { 310 {
295 amount = 0; 311 amount = 0;
298 SetEquipmentAmount(equip, amount); 314 SetEquipmentAmount(equip, amount);
299 } 315 }
300 316
301 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) 317 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
302 { 318 {
303 if (!equip.IsRatioLimit) 319 SetEquipmentRatio(equip.EquipmentItem, ratio);
304 { 320 }
305 throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has an absolute limit, not a ratio limit"); 321
322 public void SetEquipmentRatio(EquipmentItem equip, double ratio)
323 {
324 if (UnitType.IsAbsoluteLimitedEquipmentItem(equip))
325 {
326 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit");
306 } 327 }
307 328
308 if (ratio > 1) 329 if (ratio > 1)
309 { 330 {
310 ratio = 1; 331 ratio = 1;
315 } 336 }
316 337
317 SetEquipmentAmount(equip, ratio); 338 SetEquipmentAmount(equip, ratio);
318 } 339 }
319 340
320 private void SetEquipmentAmount(UnitEquipmentItem equip, double amount) 341 private void SetEquipmentAmount(EquipmentItem equip, double amount)
321 { 342 {
322 double oldAmount = 0; 343 double oldAmount = 0;
323 344
324 if (equipment.ContainsKey(equip)) 345 if (equipment.ContainsKey(equip))
325 { 346 {
339 360
340 OnUnitEquipmentAmountChanged(equip, oldAmount, amount); 361 OnUnitEquipmentAmountChanged(equip, oldAmount, amount);
341 CalcCost(); 362 CalcCost();
342 } 363 }
343 } 364 }
365
366 public bool CanEquipWithItem(UnitEquipmentItem item)
367 {
368 string mutex = item.MutexGroup;
369
370 if (mutex == "")
371 {
372 return true;
373 }
374
375 foreach (UnitEquipmentItem unitItem in GetUnitEquipmentItemsForEquipment())
376 {
377 if (unitItem.MutexGroup == mutex)
378 {
379 return false;
380 }
381 }
382
383 return true;
384 }
344 385
345 public bool CanEquipWithItem(string equipID) 386 public bool CanEquipWithItem(string equipID)
346 { 387 {
347 string mutex = UnitType.GetEquipmentItem(equipID).MutexGroup; 388 return CanEquipWithItem(UnitType.GetEquipmentItem(equipID));
348
349 if (mutex == "")
350 {
351 return true;
352 }
353
354 foreach (UnitEquipmentItem item in equipment.Keys)
355 {
356 if (item.MutexGroup == mutex)
357 {
358 return false;
359 }
360 }
361
362 return true;
363 } 389 }
364 390
365 private void OnPointsValueChanged(double oldValue, double newValue) 391 private void OnPointsValueChanged(double oldValue, double newValue)
366 { 392 {
367 if (PointsValueChanged!=null) 393 if (PointsValueChanged!=null)
376 { 402 {
377 UnitSizeChanged(this, oldValue, newValue); 403 UnitSizeChanged(this, oldValue, newValue);
378 } 404 }
379 } 405 }
380 406
381 private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue) 407 private void OnUnitEquipmentAmountChanged(EquipmentItem equip, double oldValue, double newValue)
382 { 408 {
383 if (UnitEquipmentAmountChanged!=null) 409 if (UnitEquipmentAmountChanged!=null)
384 { 410 {
385 UnitEquipmentAmountChanged(equip, oldValue, newValue); 411 UnitEquipmentAmountChanged(equip, oldValue, newValue);
386 } 412 }