comparison API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs @ 344:8585dfacac3b

Re #27: Unit Requirements * Take unit being added into account
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 10:47:58 +0000
parents acd390dba551
children 44a6539fadf9
comparison
equal deleted inserted replaced
343:acd390dba551 344:8585dfacac3b
40 { 40 {
41 Validation canAdd = Validation.Passed; 41 Validation canAdd = Validation.Passed;
42 42
43 foreach (UnitCountRequirementData limit in limitedTypes) 43 foreach (UnitCountRequirementData limit in limitedTypes)
44 { 44 {
45 if (toArmy.GetUnitTypeCount(limit.UnitType) > limit.Count) 45 if (GetUnitTypeCount(toArmy, limit.UnitType, wfObject) > limit.Count)
46 { 46 {
47 canAdd = Validation.Failed; 47 canAdd = Validation.Failed;
48 break; 48 break;
49 } 49 }
50 } 50 }
51 51
52 return canAdd; 52 return canAdd;
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;
53 } 63 }
54 64
55 /// <summary> 65 /// <summary>
56 /// Adds a requirement for there to be no more than maxCount of a given UnitType 66 /// Adds a requirement for there to be no more than maxCount of a given UnitType
57 /// </summary> 67 /// </summary>
74 /// </param> 84 /// </param>
75 public void AddUnitTypeRequirement(UnitType unitType) 85 public void AddUnitTypeRequirement(UnitType unitType)
76 { 86 {
77 AddUnitTypeRequirement(unitType, 0); 87 AddUnitTypeRequirement(unitType, 0);
78 } 88 }
89
90 /// <summary>
91 /// Checks whether the supplied army is currently valid according to this requirement.
92 /// </summary>
93 /// <returns>
94 /// A <code>Validation</code> enum to show the result of the validation
95 /// </returns>
96 /// <param name='toValidate'>
97 /// The army to validate
98 /// </param>
99 public Validation ValidatesArmy(Army army)
100 {
101 Validation canAdd = Validation.Passed;
102
103 foreach (UnitCountRequirementData limit in limitedTypes)
104 {
105 if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
106 {
107 canAdd = Validation.Failed;
108 break;
109 }
110 }
111
112 return canAdd;
113 }
79 } 114 }
80 } 115 }
81 116