Mercurial > repos > IBBoard.WarFoundry.API
changeset 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 |
files | API/Objects/Requirement/AbstractUnitRequirement.cs API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs |
diffstat | 4 files changed, 44 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- 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)
--- 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<OBJECT_TYPE> otherReq = (RequiresAtLeastNUnitsRequirement<OBJECT_TYPE>)obj; + RequiresAtLeastNUnitsRequirement<OBJECT_TYPE> otherReq = obj as RequiresAtLeastNUnitsRequirement<OBJECT_TYPE>; if (!Collections.Collections.AreEqual(RequiredTypes, otherReq.RequiredTypes)) { return false;
--- 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;
--- 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