comparison API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.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 afc6410e4efc
children 680db2462e34
comparison
equal deleted inserted replaced
456:52baffdd2ab9 457:8e01c3174cc3
10 /// <summary> 10 /// <summary>
11 /// A requirement where a UnitType can only be taken if there are no more than N units of one or more unit in an army. 11 /// A requirement where a UnitType can only be taken if there are no more than N units of one or more unit in an army.
12 /// </summary> 12 /// </summary>
13 public class UnitRequiresNoMoreThanNOfUnitTypeRequirement : RequiresNoMoreThanNOfUnitTypeRequirement<UnitType> 13 public class UnitRequiresNoMoreThanNOfUnitTypeRequirement : RequiresNoMoreThanNOfUnitTypeRequirement<UnitType>
14 { 14 {
15 private UnitType requirementOnType; 15 public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType allowedObject, params UnitType[] unitTypes) : base(allowedObject, unitTypes)
16
17 public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType requirementOn) : base()
18 { 16 {
19 requirementOnType = requirementOn; 17 //Do nothing special
20 } 18 }
21 19
22 /// <summary> 20 /// <summary>
23 /// Checks whether the supplied WarFoundryObject can be added to the supplied army. 21 /// Checks whether the supplied WarFoundryObject can be added to the supplied army.
24 /// </summary> 22 /// </summary>
36 return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable; 34 return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
37 } 35 }
38 36
39 protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) 37 protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy)
40 { 38 {
41 return IsApplicable(toArmy) || IsApplicable(toObject); 39 return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy);
42 }
43
44 protected override bool IsApplicable(Army toArmy)
45 {
46 return toArmy.GetUnitTypeCount(requirementOnType) > 0;
47 } 40 }
48 41
49 protected override bool IsApplicable(IWarFoundryObject toObject) 42 protected override bool IsApplicable(IWarFoundryObject toObject)
50 { 43 {
51 return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType)); 44 return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType));
45 }
46
47 private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy)
48 {
49 bool isApplicable = false;
50 UnitType addedType = toObject as UnitType;
51
52 if (addedType == null)
53 {
54 addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null;
55 }
56
57 if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0)
58 {
59 foreach (UnitCountRequirementData limit in ConstraintTypes)
60 {
61 if (Arrays.Contains(limit.UnitTypes, addedType))
62 {
63 isApplicable = true;
64 break;
65 }
66 }
67 }
68
69 return isApplicable;
52 } 70 }
53 } 71 }
54 } 72 }
55 73