comparison API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs @ 465:7b9ff7b1df24

Re #394: Make requirements (or factory) more closely match Rollcall methods * Make UnitRequiresAtLeastNUnitsRequirementFactory handle "alternatives" (Rollcall's "-1,X|Y" notation) * Make RequiresAtLeastNUnitsRequirement handle alternatives/multiple unit types in one limit * Move some useful code up the classes
author IBBoard <dev@ibboard.co.uk>
date Sat, 24 Mar 2012 20:33:11 +0000
parents 159dc9be36c2
children f48c49b53738
comparison
equal deleted inserted replaced
464:59e1fb8a476a 465:7b9ff7b1df24
83 { 83 {
84 Validation isValid = Validation.Passed; 84 Validation isValid = Validation.Passed;
85 85
86 foreach (UnitCountRequirementData requirement in ConstraintTypes) 86 foreach (UnitCountRequirementData requirement in ConstraintTypes)
87 { 87 {
88 if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count) 88 if (GetUnitTypesCount(toArmy, requirement.UnitTypes, wfObject) < requirement.Count)
89 { 89 {
90 isValid = Validation.Failed; 90 isValid = Validation.Failed;
91 break; 91 break;
92 } 92 }
93 } 93 }
94 94
95 return isValid; 95 return isValid;
96 } 96 }
97 97
98 /// <summary> 98 /// <summary>
99 /// Adds a requirement for there to be at least minCount of a given UnitType 99 /// Adds a requirement for there to be at least minCount of a given UnitType, allowing any number of of this UnitType.
100 /// </summary> 100 /// </summary>
101 /// <param name='unitType'> 101 /// <param name='unitType'>
102 /// The unit type to require. 102 /// The unit type to require.
103 /// </param> 103 /// </param>
104 /// <param name='minCount'> 104 /// <param name='minCount'>
105 /// The minimum number of that type that must exist. 105 /// The minimum number of that type that must exist.
106 /// </param> 106 /// </param>
107 public void AddUnitTypeRequirement(UnitType unitType, int minCount) 107 public void AddUnitTypeRequirement(UnitType unitType, int minCount)
108 { 108 {
109 ConstraintTypes.Add(new UnitCountRequirementData(unitType, minCount)); 109 AddUnitTypeRequirement(minCount, unitType);
110 } 110 }
111 111
112 /// <summary> 112 /// <summary>
113 /// Adds a requirement for there to be one or more of a given UnitType 113 /// Adds a requirement for there to be one or more of a given UnitType, allowing any number of of this UnitType.
114 /// </summary> 114 /// </summary>
115 /// <param name='unitType'> 115 /// <param name='unitType'>
116 /// The unit type to require. 116 /// The unit type to require.
117 /// </param> 117 /// </param>
118 public override void AddUnitTypeRequirement(UnitType unitType) 118 public override void AddUnitTypeRequirement(UnitType unitType)
119 { 119 {
120 AddUnitTypeRequirement(unitType, 1); 120 AddUnitTypeRequirement(unitType, 1);
121 } 121 }
122
123 /// <summary>
124 /// Adds a requirement for there to be one or more of the given UnitTypes, allowing any number of of this UnitType. If multiple unit types
125 /// are supplied here then they are treated as alternatives (so "unitType1, unitType2" requires one of either type).
126 /// </summary>
127 /// <param name='unitType'>
128 /// The unit type or types to require.
129 /// </param>
130 public void AddUnitTypeRequirement(params UnitType[] unitTypes)
131 {
132 AddUnitTypeRequirement(1, unitTypes);
133 }
134
135 /// <summary>
136 /// Adds a requirement for there to be minCount or more of the given UnitTypes, allowing any number of of this UnitType. If multiple unit types
137 /// are supplied here then they are treated as alternatives (so "unitType1, unitType2" requires one of either type).
138 /// </summary>
139 /// <param name="minCount">
140 /// The number of units to be allowed for each 1 of unitType
141 /// </param>
142 /// <param name='unitType'>
143 /// The unit type or types to require.
144 /// </param>
145 public void AddUnitTypeRequirement(int minCount, params UnitType[] unitTypes)
146 {
147 ConstraintTypes.Add(new UnitCountRequirementData(unitTypes, minCount));
148 }
149
122 150
123 /// <summary> 151 /// <summary>
124 /// Checks whether the supplied army is currently valid according to this requirement. 152 /// Checks whether the supplied army is currently valid according to this requirement.
125 /// </summary> 153 /// </summary>
126 /// <returns> 154 /// <returns>
138 { 166 {
139 isValid = Validation.Passed; 167 isValid = Validation.Passed;
140 168
141 foreach (UnitCountRequirementData requirement in ConstraintTypes) 169 foreach (UnitCountRequirementData requirement in ConstraintTypes)
142 { 170 {
143 if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count) 171 if (GetUnitTypesCount(toValidate, requirement.UnitTypes) < requirement.Count)
144 { 172 {
145 isValid = Validation.Failed; 173 isValid = Validation.Failed;
146 break; 174 break;
147 } 175 }
148 } 176 }
164 { 192 {
165 List<string> failures = new List<string>(); 193 List<string> failures = new List<string>();
166 194
167 foreach (UnitCountRequirementData requirement in ConstraintTypes) 195 foreach (UnitCountRequirementData requirement in ConstraintTypes)
168 { 196 {
169 int unitCount = army.GetUnitTypeCount(requirement.UnitType); 197 int unitCount = GetUnitTypesCount(army, requirement.UnitTypes);
170 198
171 if (unitCount < requirement.Count) 199 if (unitCount < requirement.Count)
172 { 200 {
173 failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (have " + unitCount + ")"); 201 failures.Add(requirement.Count + " × " + GetUnitTypeList(requirement) + " (have " + unitCount + ")");
174 } 202 }
175 } 203 }
176 204
177 return failures; 205 return failures;
178 } 206 }
186 { 214 {
187 List<string> failures = new List<string>(); 215 List<string> failures = new List<string>();
188 216
189 foreach (UnitCountRequirementData requirement in ConstraintTypes) 217 foreach (UnitCountRequirementData requirement in ConstraintTypes)
190 { 218 {
191 int unitCount = GetUnitTypeCount(army, requirement.UnitType, toAdd); 219 int unitCount = GetUnitTypesCount(army, requirement.UnitTypes, toAdd);
192 220
193 if (unitCount < requirement.Count) 221 if (unitCount < requirement.Count)
194 { 222 {
195 failures.Add(requirement.Count + " × " + requirement.UnitType.Name + " (would have " + unitCount + ")"); 223 failures.Add(requirement.Count + " × " + GetUnitTypeList(requirement) + " (would have " + unitCount + ")");
196 } 224 }
197 } 225 }
198 226
199 return failures; 227 return failures;
200 } 228 }