comparison api/Objects/Unit.cs @ 98:4dd1c41c95b4

Fixes #119: Handle changing of equipment between absolute and ratio amounts * Check the type of the existing unit and replace if necessary
author IBBoard <dev@ibboard.co.uk>
date Sun, 09 Aug 2009 13:12:38 +0000
parents 95746083d037
children f7b9423c2a5a
comparison
equal deleted inserted replaced
97:95746083d037 98:4dd1c41c95b4
280 { 280 {
281 RemoveEquipmentItem(equip); 281 RemoveEquipmentItem(equip);
282 } 282 }
283 else 283 else
284 { 284 {
285 double oldAmount = GetEquipmentAmount(equip); 285 AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip);
286 double oldAmount = (currSelection == null ? 0 : currSelection.AmountTaken);
286 287
287 if (amount!=oldAmount) 288 if (amount != oldAmount)
288 { 289 {
289 if (oldAmount == 0) 290 if (oldAmount == 0)
290 { 291 {
291 AbstractUnitEquipmentItemSelection newItem = null; 292 AddEquipmentAmount(equip, amount);
292 293 }
293 if (equip.IsRatioLimit) 294 else if (currSelection is UnitEquipmentNumericSelection)
294 { 295 {
295 newItem = new UnitEquipmentNumericForRatioSelection(this, equip, amount); 296 //A UnitEquipmentItem shouldn't change its IsRatio value, so assume we already have the right sub-type
296 } 297 currSelection.AmountTaken = amount;
297 else
298 {
299 newItem = new UnitEquipmentNumericSelection(this, equip, amount);
300 }
301
302 equipment[equip] = newItem;
303 } 298 }
304 else 299 else
305 { 300 {
306 AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip); 301 equipment.Remove(equip);
307 currSelection.AmountTaken = amount; 302 AddEquipmentAmount(equip, amount);
308 } 303 }
309 304
310 OnUnitEquipmentAmountChanged(equip, oldAmount, amount); 305 OnUnitEquipmentAmountChanged(equip, oldAmount, amount);
311 } 306 }
312 } 307 }
313 } 308 }
314 309
310 private void AddEquipmentAmount(UnitEquipmentItem equip, int amount)
311 {
312 AbstractUnitEquipmentItemSelection newItem = null;
313
314 if (equip.IsRatioLimit)
315 {
316 newItem = new UnitEquipmentNumericForRatioSelection(this, equip, amount);
317 }
318 else
319 {
320 newItem = new UnitEquipmentNumericSelection(this, equip, amount);
321 }
322
323 equipment[equip] = newItem;
324 }
325
315 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio) 326 public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
316 { 327 {
317 if (!equip.IsRatioLimit) 328 if (!equip.IsRatioLimit)
318 { 329 {
319 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit"); 330 throw new InvalidOperationException("Equipment with ID "+equip.ID+" for unit of type "+UnitType.ID+" has an absolute limit, not a ratio limit");
332 { 343 {
333 RemoveEquipmentItem(equip); 344 RemoveEquipmentItem(equip);
334 } 345 }
335 else 346 else
336 { 347 {
337 double oldRatio = GetEquipmentAmount(equip); 348 AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip);
349 double oldRatio = (currSelection == null ? 0 : currSelection.AmountTaken);
338 350
339 if (ratio != oldRatio) 351 if (ratio != oldRatio)
340 { 352 {
341 if (oldRatio == 0) 353 if (oldRatio == 0)
342 { 354 {
343 equipment[equip] = new UnitEquipmentRatioSelection(this, equip, ratio); 355 AddEquipmentRatio(equip, ratio);
356 }
357 else if (currSelection is UnitEquipmentRatioSelection)
358 {
359 currSelection.AmountTaken = ratio;
344 } 360 }
345 else 361 else
346 { 362 {
347 AbstractUnitEquipmentItemSelection currSelection = DictionaryUtils.GetValue(equipment, equip); 363 equipment.Remove(equip);
348 currSelection.AmountTaken = ratio; 364 AddEquipmentRatio(equip, ratio);
349 } 365 }
350 366
351 OnUnitEquipmentAmountChanged(equip, oldRatio, ratio); 367 OnUnitEquipmentAmountChanged(equip, oldRatio, ratio);
352 } 368 }
353 } 369 }
354 } 370 }
355 371
372 private void AddEquipmentRatio(UnitEquipmentItem equip, double ratio)
373 {
374 equipment[equip] = new UnitEquipmentRatioSelection(this, equip, ratio);
375 }
376
356 private void RemoveEquipmentItem(UnitEquipmentItem equip) 377 private void RemoveEquipmentItem(UnitEquipmentItem equip)
357 { 378 {
358 double oldAmount = GetEquipmentAmount(equip); 379 double oldAmount = GetEquipmentAmount(equip);
359 380
360 if (oldAmount != 0) 381 if (oldAmount != 0)