Mercurial > repos > IBBoard.WarFoundry.API
comparison API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs @ 456:52baffdd2ab9
Re #379: Fix validation of requirements to check for unit
* Default to not being applicable - make requirements say when they are
* Fix UnitRequiresNUnitsForMUnitsRequirement to not be applicable when adding unrelated units (even if army itself fails the rule)
* Fix type casting issues in equality check
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 25 Feb 2012 16:35:31 +0000 |
parents | afc6410e4efc |
children | 8e01c3174cc3 |
comparison
equal
deleted
inserted
replaced
455:afc6410e4efc | 456:52baffdd2ab9 |
---|---|
91 | 91 |
92 public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) | 92 public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy) |
93 { | 93 { |
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); | 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 RequiredTypes) |
100 { | 100 { |
101 typeFound |= (addedUnitType == limit.UnitType); | 101 typeFound |= (addedUnitType == limit.UnitType); |
139 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; | 139 return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0; |
140 } | 140 } |
141 | 141 |
142 public override Validation ValidatesArmy(Army army) | 142 public override Validation ValidatesArmy(Army army) |
143 { | 143 { |
144 Validation canAdd = Validation.NotApplicable; | 144 Validation canAdd; |
145 int allowedTypeCount = GetObjectCountFromArmy(army, AllowedObject); | 145 int allowedTypeCount = GetObjectCountFromArmy(army, AllowedObject); |
146 bool typeFound = (allowedTypeCount > 0); | 146 |
147 | 147 if (allowedTypeCount > 0) |
148 foreach (UnitCountRequirementData limit in RequiredTypes) | |
149 { | |
150 int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes); | |
151 typeFound |= (limitedTypeCount > 0); | |
152 | |
153 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) | |
154 { | |
155 canAdd = Validation.Failed; | |
156 break; | |
157 } | |
158 } | |
159 | |
160 if (typeFound && canAdd == Validation.NotApplicable) | |
161 { | 148 { |
162 canAdd = Validation.Passed; | 149 canAdd = Validation.Passed; |
150 | |
151 foreach (UnitCountRequirementData limit in RequiredTypes) | |
152 { | |
153 int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes); | |
154 | |
155 if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) | |
156 { | |
157 canAdd = Validation.Failed; | |
158 break; | |
159 } | |
160 } | |
161 } | |
162 else | |
163 { | |
164 canAdd = Validation.NotApplicable; | |
163 } | 165 } |
164 | 166 |
165 return canAdd; | 167 return canAdd; |
166 } | 168 } |
167 | 169 |