# HG changeset patch # User IBBoard # Date 1330187731 0 # Node ID 52baffdd2ab9beb829f441b4ec928d61a070ea90 # Parent afc6410e4efc75696abcb1f4e755712443bda60b 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 diff -r afc6410e4efc -r 52baffdd2ab9 API/Objects/Requirement/AbstractUnitRequirement.cs --- a/API/Objects/Requirement/AbstractUnitRequirement.cs Wed Feb 22 20:45:39 2012 +0000 +++ b/API/Objects/Requirement/AbstractUnitRequirement.cs Sat Feb 25 16:35:31 2012 +0000 @@ -64,12 +64,12 @@ protected virtual bool IsApplicable(Army toArmy) { - return true; + return false; } protected virtual bool IsApplicable(IWarFoundryObject toObject) { - return true; + return false; } public string GetValidationMessage(Army army) diff -r afc6410e4efc -r 52baffdd2ab9 API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Wed Feb 22 20:45:39 2012 +0000 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs Sat Feb 25 16:35:31 2012 +0000 @@ -32,7 +32,7 @@ protected override bool TypeEquals(object obj) { - RequiresAtLeastNUnitsRequirement otherReq = (RequiresAtLeastNUnitsRequirement)obj; + RequiresAtLeastNUnitsRequirement otherReq = obj as RequiresAtLeastNUnitsRequirement; if (!Collections.Collections.AreEqual(RequiredTypes, otherReq.RequiredTypes)) { return false; diff -r afc6410e4efc -r 52baffdd2ab9 API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs --- a/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs Wed Feb 22 20:45:39 2012 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs Sat Feb 25 16:35:31 2012 +0000 @@ -93,7 +93,7 @@ { Validation canAdd = Validation.NotApplicable; UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType; - bool typeFound = (wfObject == (IWarFoundryObject)AllowedObject); + bool typeFound = (wfObject == (IWarFoundryObject)AllowedObject || addedUnitType == (IWarFoundryObject)AllowedObject); int allowedTypeCount = GetAllowedObjectCount(toArmy, AllowedObject, wfObject); foreach (UnitCountRequirementData limit in RequiredTypes) @@ -141,25 +141,27 @@ public override Validation ValidatesArmy(Army army) { - Validation canAdd = Validation.NotApplicable; + Validation canAdd; int allowedTypeCount = GetObjectCountFromArmy(army, AllowedObject); - bool typeFound = (allowedTypeCount > 0); - foreach (UnitCountRequirementData limit in RequiredTypes) + if (allowedTypeCount > 0) { - int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes); - typeFound |= (limitedTypeCount > 0); + canAdd = Validation.Passed; + + foreach (UnitCountRequirementData limit in RequiredTypes) + { + int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes); - if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) - { - canAdd = Validation.Failed; - break; + if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount)) + { + canAdd = Validation.Failed; + break; + } } } - - if (typeFound && canAdd == Validation.NotApplicable) + else { - canAdd = Validation.Passed; + canAdd = Validation.NotApplicable; } return canAdd; diff -r afc6410e4efc -r 52baffdd2ab9 API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs --- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs Wed Feb 22 20:45:39 2012 +0000 +++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs Sat Feb 25 16:35:31 2012 +0000 @@ -39,12 +39,7 @@ protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy) { - return IsApplicable(toArmy) || IsApplicable(toObject); - } - - protected override bool IsApplicable(Army toArmy) - { - return toArmy.GetUnitTypeCount(AllowedObject) > 0; + return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy); } protected override bool IsApplicable(IWarFoundryObject toObject) @@ -52,6 +47,31 @@ return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType)); } + private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy) + { + bool isApplicable = false; + UnitType addedType = toObject as UnitType; + + if (addedType == null) + { + addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null; + } + + if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0) + { + foreach (UnitCountRequirementData limit in RequiredTypes) + { + if (Arrays.Contains(limit.UnitTypes, addedType)) + { + isApplicable = true; + break; + } + } + } + + return isApplicable; + } + public override string RequirementID { get