comparison API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs @ 457:8e01c3174cc3

Re #379: Fix validation of requirements to check for unit * Rename public accessor for unit types to ConstraintTypes so that "no more than" requirements don't work with "required types" * Remove class level version of ConstraintTypes to fall back to abstract class version * Make sure we cascade allowedObject up through RequiresNoMoreThanNOfUnitTypeRequirement * Rebuild UnitRequiresNoMoreThanNOfUnitTypeRequirement "IsApplicable" so that we don't get applicable for unrelated types, even if the requirement is currently in a failure state
author IBBoard <dev@ibboard.co.uk>
date Sun, 26 Feb 2012 15:14:01 +0000
parents 52baffdd2ab9
children 680db2462e34
comparison
equal deleted inserted replaced
456:52baffdd2ab9 457:8e01c3174cc3
16 16
17 public override int GetHashCode() 17 public override int GetHashCode()
18 { 18 {
19 int hash = 0; 19 int hash = 0;
20 20
21 foreach (UnitCountRequirementData req in RequiredTypes) 21 foreach (UnitCountRequirementData req in ConstraintTypes)
22 { 22 {
23 hash += req.UnitType.GetHashCode(); 23 hash += req.UnitType.GetHashCode();
24 } 24 }
25 25
26 return hash; 26 return hash;
27 } 27 }
28 28
29 protected override bool TypeEquals(object obj) 29 protected override bool TypeEquals(object obj)
30 { 30 {
31 RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE> otherReq = (RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE>)obj; 31 RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE> otherReq = (RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE>)obj;
32 if (!Collections.Collections.AreEqual(RequiredTypes, otherReq.RequiredTypes)) 32 if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes))
33 { 33 {
34 return false; 34 return false;
35 } 35 }
36 else 36 else
37 { 37 {
47 private List<string> GetFailedAddingRequirements(IWarFoundryObject obj, Army toArmy) 47 private List<string> GetFailedAddingRequirements(IWarFoundryObject obj, Army toArmy)
48 { 48 {
49 List<string> failures = new List<string>(); 49 List<string> failures = new List<string>();
50 int allowedObjectCount = GetAllowedObjectCount(toArmy, AllowedObject, obj); 50 int allowedObjectCount = GetAllowedObjectCount(toArmy, AllowedObject, obj);
51 51
52 foreach (UnitCountRequirementData limit in RequiredTypes) 52 foreach (UnitCountRequirementData limit in ConstraintTypes)
53 { 53 {
54 int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, obj); 54 int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, obj);
55 55
56 if (!IsValidByRequirement(obj, toArmy, limit, allowedObjectCount)) 56 if (!IsValidByRequirement(obj, toArmy, limit, allowedObjectCount))
57 { 57 {
94 Validation canAdd = Validation.NotApplicable; 94 Validation canAdd = Validation.NotApplicable;
95 UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType; 95 UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType;
96 bool typeFound = (wfObject == (IWarFoundryObject)AllowedObject || addedUnitType == (IWarFoundryObject)AllowedObject); 96 bool typeFound = (wfObject == (IWarFoundryObject)AllowedObject || addedUnitType == (IWarFoundryObject)AllowedObject);
97 int allowedTypeCount = GetAllowedObjectCount(toArmy, AllowedObject, wfObject); 97 int allowedTypeCount = GetAllowedObjectCount(toArmy, AllowedObject, wfObject);
98 98
99 foreach (UnitCountRequirementData limit in RequiredTypes) 99 foreach (UnitCountRequirementData limit in ConstraintTypes)
100 { 100 {
101 typeFound |= (addedUnitType == limit.UnitType); 101 typeFound |= (addedUnitType == limit.UnitType);
102 102
103 if (!IsValidByRequirement(wfObject, toArmy, limit, allowedTypeCount)) 103 if (!IsValidByRequirement(wfObject, toArmy, limit, allowedTypeCount))
104 { 104 {
146 146
147 if (allowedTypeCount > 0) 147 if (allowedTypeCount > 0)
148 { 148 {
149 canAdd = Validation.Passed; 149 canAdd = Validation.Passed;
150 150
151 foreach (UnitCountRequirementData limit in RequiredTypes) 151 foreach (UnitCountRequirementData limit in ConstraintTypes)
152 { 152 {
153 int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes); 153 int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes);
154 154
155 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) 155 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount))
156 { 156 {
175 private List<string> GetFailedValidationRequirements(Army army) 175 private List<string> GetFailedValidationRequirements(Army army)
176 { 176 {
177 List<string> failures = new List<string>(); 177 List<string> failures = new List<string>();
178 int allowedTypeCount = GetAllowedObjectCount(army); 178 int allowedTypeCount = GetAllowedObjectCount(army);
179 179
180 foreach (UnitCountRequirementData limit in RequiredTypes) 180 foreach (UnitCountRequirementData limit in ConstraintTypes)
181 { 181 {
182 int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes); 182 int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes);
183 183
184 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) 184 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount))
185 { 185 {
210 /// <param name='allowedCount'> 210 /// <param name='allowedCount'>
211 /// The number of units allowed for every minCount units of the supplied unit type. 211 /// The number of units allowed for every minCount units of the supplied unit type.
212 /// </param> 212 /// </param>
213 public void AddUnitTypeRequirement(UnitType unitType, int minCount, int allowedCount) 213 public void AddUnitTypeRequirement(UnitType unitType, int minCount, int allowedCount)
214 { 214 {
215 RequiredTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount)); 215 ConstraintTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount));
216 } 216 }
217 217
218 /// <summary> 218 /// <summary>
219 /// Adds a requirement for there to be at least one of a given UnitType, allowing allowedCount of this UnitType 219 /// Adds a requirement for there to be at least one of a given UnitType, allowing allowedCount of this UnitType
220 /// </summary> 220 /// </summary>
256 /// <param name='unitType'> 256 /// <param name='unitType'>
257 /// The unit type or types to require. 257 /// The unit type or types to require.
258 /// </param> 258 /// </param>
259 public void AddUnitTypeRequirement(int allowsAmount, params UnitType[] unitTypes) 259 public void AddUnitTypeRequirement(int allowsAmount, params UnitType[] unitTypes)
260 { 260 {
261 RequiredTypes.Add(new UnitCountRequirementData(unitTypes, 1, allowsAmount)); 261 ConstraintTypes.Add(new UnitCountRequirementData(unitTypes, 1, allowsAmount));
262 } 262 }
263 263
264 /// <summary> 264 /// <summary>
265 /// Adds a requirement for there to be minCount or more of the given UnitTypes, allowing allowsAmount of this UnitType. If multiple unit types 265 /// Adds a requirement for there to be minCount or more of the given UnitTypes, allowing allowsAmount of this UnitType. If multiple unit types
266 /// are supplied here then the number is additive (so 1 x unitType1 and 1 x unitType2 allows two of this UnitType). 266 /// are supplied here then the number is additive (so 1 x unitType1 and 1 x unitType2 allows two of this UnitType).
274 /// <param name='unitType'> 274 /// <param name='unitType'>
275 /// The unit type or types to require. 275 /// The unit type or types to require.
276 /// </param> 276 /// </param>
277 public void AddUnitTypeRequirement(int minCount, int allowsAmount, params UnitType[] unitTypes) 277 public void AddUnitTypeRequirement(int minCount, int allowsAmount, params UnitType[] unitTypes)
278 { 278 {
279 RequiredTypes.Add(new UnitCountRequirementData(unitTypes, minCount, allowsAmount)); 279 ConstraintTypes.Add(new UnitCountRequirementData(unitTypes, minCount, allowsAmount));
280 } 280 }
281 } 281 }
282 } 282 }
283 283