comparison API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 345:008537acf244

Re #27: Unit requirements * Make basic "Requires at least" requirement take into account the unit type being added * Make unit version of "requires at least" requirement pass execution up to the matching parent method
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 11:00:09 +0000
parents 7bd2a7cdbfbd
children 44a6539fadf9
comparison
equal deleted inserted replaced
344:8585dfacac3b 345:008537acf244
36 /// <param name='toArmy'> 36 /// <param name='toArmy'>
37 /// The army to add the object to. 37 /// The army to add the object to.
38 /// </param> 38 /// </param>
39 public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy) 39 public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
40 { 40 {
41 return this.ValidatesArmy(toArmy); 41 Validation isValid = Validation.Passed;
42
43 foreach (UnitCountRequirementData requirement in requiredTypes)
44 {
45 if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count)
46 {
47 isValid = Validation.Failed;
48 break;
49 }
50 }
51
52 return isValid;
53 }
54
55 private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
56 {
57 return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
58 }
59
60 private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
61 {
62 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
42 } 63 }
43 64
44 /// <summary> 65 /// <summary>
45 /// Adds a requirement for there to be at least minCount of a given UnitType 66 /// Adds a requirement for there to be at least minCount of a given UnitType
46 /// </summary> 67 /// </summary>