comparison api/Objects/Unit.cs @ 80:aa66dd18cdae

Closes #81 - Resolve Unit Equipment problems in WinForms GUI * Take the easy solution (after changes in r123 that made EquipmentItem methods available through UnitEquipmentItem) and revert back to using UnitEquipmentItems in Unit
author IBBoard <dev@ibboard.co.uk>
date Tue, 19 May 2009 19:00:48 +0000
parents aa2d05a9c635
children 032b174fc17a
comparison
equal deleted inserted replaced
79:284ebe05158c 80:aa66dd18cdae
19 private int size; 19 private int size;
20 private Army army; 20 private Army army;
21 private Unit parentUnit; 21 private Unit parentUnit;
22 private double points; 22 private double points;
23 private ArmyCategory cat; 23 private ArmyCategory cat;
24 private Dictionary<EquipmentItem, double> equipment = new Dictionary<EquipmentItem, double>(); 24 private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, 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
63 { 63 {
64 if (unitEquip.IsRequired) 64 if (unitEquip.IsRequired)
65 { 65 {
66 if (CanEquipWithItem(unitEquip)) 66 if (CanEquipWithItem(unitEquip))
67 { 67 {
68 equipment[unitEquip.EquipmentItem] = unitEquip.MinNumber; 68 equipment[unitEquip] = 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 (EquipmentItem equipItem in equipment.Keys) 80 foreach (UnitEquipmentItem unitEquipItem in equipment.Keys)
81 { 81 {
82 double count = equipment[equipItem]; 82 double count = equipment[unitEquipItem];
83 UnitEquipmentItem unitEquipItem = UnitType.GetEquipmentItem(equipItem);
84 83
85 if (unitEquipItem.IsRatioLimit) 84 if (unitEquipItem.IsRatioLimit)
86 { 85 {
87 if (unitEquipItem.RoundNumberUp) 86 if (unitEquipItem.RoundNumberUp)
88 { 87 {
89 points+= Math.Ceiling(size * count) * equipItem.Cost; 88 points += Math.Ceiling(size * count) * unitEquipItem.Cost;
90 } 89 }
91 else 90 else
92 { 91 {
93 points+= Math.Floor(size * count) * equipItem.Cost; 92 points += Math.Floor(size * count) * unitEquipItem.Cost;
94 } 93 }
95 } 94 }
96 else 95 else
97 { 96 {
98 if (count == WarFoundryCore.INFINITY) 97 if (count == WarFoundryCore.INFINITY)
99 { 98 {
100 points+= size * equipItem.Cost; 99 points += size * unitEquipItem.Cost;
101 } 100 }
102 else 101 else
103 { 102 {
104 points+= count * equipItem.Cost; 103 points += count * unitEquipItem.Cost;
105 } 104 }
106 } 105 }
107 } 106 }
108 107
109 if (oldpoints!=points) 108 if (oldpoints!=points)
254 } 253 }
255 254
256 return list.ToArray(); 255 return list.ToArray();
257 } 256 }
258 257
259 public EquipmentItem[] GetEquipment() 258 public UnitEquipmentItem[] GetEquipment()
260 { 259 {
261 return DictionaryUtils.ToKeyArray(equipment); 260 return DictionaryUtils.ToKeyArray(equipment);
262 } 261 }
263
264 private UnitEquipmentItem[] GetUnitEquipmentItemsForEquipment()
265 {
266 EquipmentItem[] items = GetEquipment();
267 int arrLength = items.Length;
268 UnitEquipmentItem[] unitItems = new UnitEquipmentItem[arrLength];
269
270 for (int i = 0; i < arrLength; i++)
271 {
272 UnitType.GetEquipmentItem(items[i]);
273 }
274
275 return unitItems;
276 }
277 262
278 public EquipmentItem[] GetRequiredEquipment() 263 public EquipmentItem[] GetRequiredEquipment()
279 { 264 {
280 List<EquipmentItem> list = new List<EquipmentItem>(); 265 List<EquipmentItem> list = new List<EquipmentItem>();
281 266
282 foreach(UnitEquipmentItem item in GetUnitEquipmentItemsForEquipment()) 267 foreach(UnitEquipmentItem item in GetEquipment())
283 { 268 {
284 if (item.IsRequired) 269 if (item.IsRequired)
285 { 270 {
286 list.Add(item.EquipmentItem); 271 list.Add(item.EquipmentItem);
287 } 272 }
290 return list.ToArray(); 275 return list.ToArray();
291 } 276 }
292 277
293 public double GetEquipmentAmount(UnitEquipmentItem item) 278 public double GetEquipmentAmount(UnitEquipmentItem item)
294 { 279 {
295 return GetEquipmentAmount(item.EquipmentItem); 280 return GetEquipmentAmount(item.ID);
296 }
297
298 public double GetEquipmentAmount(EquipmentItem item)
299 {
300 if (equipment.ContainsKey(item))
301 {
302 return equipment[item];
303 }
304 else
305 {
306 return 0;
307 }
308 } 281 }
309 282
310 public double GetEquipmentAmount(string equipID) 283 public double GetEquipmentAmount(string equipID)
311 { 284 {
312 return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID)); 285 return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID));
313 } 286 }
314 287
315 public void SetEquipmentAmount(UnitEquipmentItem equip, int amount) 288 public void SetEquipmentAmount(UnitEquipmentItem equip, int amount)
316 { 289 {
317 SetEquipmentAmount(equip.EquipmentItem, amount); 290 if (equip.IsRatioLimit)
318 }
319
320 public void SetEquipmentAmount(EquipmentItem equip, int amount)
321 {
322 if (UnitType.IsRatioLimitedEquipmentItem(equip))
323 { 291 {
324 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit"); 292 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has a ratio limit, not an absolute limit");
325 } 293 }
326 294
327 if (amount <1 && amount != WarFoundryCore.INFINITY) 295 if (amount <1 && amount != WarFoundryCore.INFINITY)
332 SetEquipmentAmount(equip, amount); 300 SetEquipmentAmount(equip, amount);
333 } 301 }
334 302
335 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) 303 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
336 { 304 {
337 SetEquipmentRatio(equip.EquipmentItem, ratio); 305 if (!equip.IsRatioLimit)
338 }
339
340 public void SetEquipmentRatio(EquipmentItem equip, double ratio)
341 {
342 if (UnitType.IsAbsoluteLimitedEquipmentItem(equip))
343 { 306 {
344 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit"); 307 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit");
345 } 308 }
346 309
347 if (ratio > 1) 310 if (ratio > 1)
354 } 317 }
355 318
356 SetEquipmentAmount(equip, ratio); 319 SetEquipmentAmount(equip, ratio);
357 } 320 }
358 321
359 private void SetEquipmentAmount(EquipmentItem equip, double amount) 322 private void SetEquipmentAmount(UnitEquipmentItem equip, double amount)
360 { 323 {
361 double oldAmount = 0; 324 double oldAmount = 0;
362 325
363 if (equipment.ContainsKey(equip)) 326 if (equipment.ContainsKey(equip))
364 { 327 {
388 if (mutex == "") 351 if (mutex == "")
389 { 352 {
390 return true; 353 return true;
391 } 354 }
392 355
393 foreach (UnitEquipmentItem unitItem in GetUnitEquipmentItemsForEquipment()) 356 foreach (UnitEquipmentItem unitItem in GetEquipment())
394 { 357 {
395 if (unitItem.MutexGroup == mutex) 358 if (unitItem.MutexGroup == mutex)
396 { 359 {
397 return false; 360 return false;
398 } 361 }
420 { 383 {
421 UnitSizeChanged(this, oldValue, newValue); 384 UnitSizeChanged(this, oldValue, newValue);
422 } 385 }
423 } 386 }
424 387
425 private void OnUnitEquipmentAmountChanged(EquipmentItem equip, double oldValue, double newValue) 388 private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue)
426 { 389 {
427 if (UnitEquipmentAmountChanged!=null) 390 if (UnitEquipmentAmountChanged!=null)
428 { 391 {
429 UnitEquipmentAmountChanged(equip, oldValue, newValue); 392 UnitEquipmentAmountChanged(equip, oldValue, newValue);
430 } 393 }