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, 43 insertions(+), 21 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/AbstractUnitRequirement.cs	Wed Feb 22 20:45:39 2012 +0000
     1.2 +++ b/API/Objects/Requirement/AbstractUnitRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
     1.3 @@ -64,12 +64,12 @@
     1.4  
     1.5  		protected virtual bool IsApplicable(Army toArmy)
     1.6  		{
     1.7 -			return true;
     1.8 +			return false;
     1.9  		}
    1.10  
    1.11  		protected virtual bool IsApplicable(IWarFoundryObject toObject)
    1.12  		{
    1.13 -			return true;
    1.14 +			return false;
    1.15  		}
    1.16  
    1.17  		public string GetValidationMessage(Army army)
     2.1 --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Wed Feb 22 20:45:39 2012 +0000
     2.2 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
     2.3 @@ -32,7 +32,7 @@
     2.4  
     2.5  		protected override bool TypeEquals(object obj)
     2.6  		{
     2.7 -			RequiresAtLeastNUnitsRequirement<OBJECT_TYPE> otherReq = (RequiresAtLeastNUnitsRequirement<OBJECT_TYPE>)obj;
     2.8 +			RequiresAtLeastNUnitsRequirement<OBJECT_TYPE> otherReq = obj as RequiresAtLeastNUnitsRequirement<OBJECT_TYPE>;
     2.9  			if (!Collections.Collections.AreEqual(RequiredTypes, otherReq.RequiredTypes))
    2.10  			{
    2.11  				return false;
     3.1 --- a/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs	Wed Feb 22 20:45:39 2012 +0000
     3.2 +++ b/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
     3.3 @@ -93,7 +93,7 @@
     3.4  		{
     3.5  			Validation canAdd = Validation.NotApplicable;
     3.6  			UnitType addedUnitType = (wfObject is Unit) ? ((Unit)wfObject).UnitType : wfObject as UnitType;
     3.7 -			bool typeFound = (wfObject == (IWarFoundryObject)AllowedObject);
     3.8 +			bool typeFound = (wfObject == (IWarFoundryObject)AllowedObject || addedUnitType == (IWarFoundryObject)AllowedObject);
     3.9  			int allowedTypeCount = GetAllowedObjectCount(toArmy, AllowedObject, wfObject);
    3.10  
    3.11  			foreach (UnitCountRequirementData limit in RequiredTypes)
    3.12 @@ -141,25 +141,27 @@
    3.13  
    3.14  		public override Validation ValidatesArmy(Army army)
    3.15  		{
    3.16 -			Validation canAdd = Validation.NotApplicable;
    3.17 +			Validation canAdd;
    3.18  			int allowedTypeCount = GetObjectCountFromArmy(army, AllowedObject);
    3.19 -			bool typeFound = (allowedTypeCount > 0);
    3.20  
    3.21 -			foreach (UnitCountRequirementData limit in RequiredTypes)
    3.22 +			if (allowedTypeCount > 0)
    3.23  			{
    3.24 -				int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes);
    3.25 -				typeFound |= (limitedTypeCount > 0);
    3.26 +				canAdd = Validation.Passed;
    3.27  
    3.28 -				if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount))
    3.29 +				foreach (UnitCountRequirementData limit in RequiredTypes)
    3.30  				{
    3.31 -					canAdd = Validation.Failed;
    3.32 -					break;
    3.33 +					int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes);
    3.34 +
    3.35 +					if (!IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount))
    3.36 +					{
    3.37 +						canAdd = Validation.Failed;
    3.38 +						break;
    3.39 +					}
    3.40  				}
    3.41  			}
    3.42 -
    3.43 -			if (typeFound && canAdd == Validation.NotApplicable)
    3.44 +			else
    3.45  			{
    3.46 -				canAdd = Validation.Passed;
    3.47 +				canAdd = Validation.NotApplicable;
    3.48  			}
    3.49  
    3.50  			return canAdd;
     4.1 --- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs	Wed Feb 22 20:45:39 2012 +0000
     4.2 +++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
     4.3 @@ -39,12 +39,7 @@
     4.4  
     4.5  		protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy)
     4.6  		{
     4.7 -			return IsApplicable(toArmy) || IsApplicable(toObject);
     4.8 -		}
     4.9 -
    4.10 -		protected override bool IsApplicable(Army toArmy)
    4.11 -		{
    4.12 -			return toArmy.GetUnitTypeCount(AllowedObject) > 0;
    4.13 +			return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy);
    4.14  		}
    4.15  
    4.16  		protected override bool IsApplicable(IWarFoundryObject toObject)
    4.17 @@ -52,6 +47,31 @@
    4.18  			return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType));
    4.19  		}
    4.20  
    4.21 +		private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy)
    4.22 +		{
    4.23 +			bool isApplicable = false;
    4.24 +			UnitType addedType = toObject as UnitType;
    4.25 +
    4.26 +			if (addedType == null)
    4.27 +			{
    4.28 +				addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null;
    4.29 +			}
    4.30 +
    4.31 +			if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0)
    4.32 +			{
    4.33 +				foreach (UnitCountRequirementData limit in RequiredTypes)
    4.34 +				{
    4.35 +					if (Arrays.Contains(limit.UnitTypes, addedType))
    4.36 +					{
    4.37 +						isApplicable = true;
    4.38 +						break;
    4.39 +					}
    4.40 +				}
    4.41 +			}
    4.42 +
    4.43 +			return isApplicable;
    4.44 +		}
    4.45 +
    4.46  		public override string RequirementID
    4.47  		{
    4.48  			get