changeset 350:6c0404277cad

Re #27: Unit requirements * Rebuild base "at least" requirement to say "N/A" when added unit isn't constrained * Remove duplicate code from "unit requires at least" requirement and override where necessary
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 19:57:40 +0000
parents 12a56786120c
children d97016f2515b
files API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs
diffstat 2 files changed, 65 insertions(+), 28 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sat Apr 09 19:14:11 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sat Apr 09 19:57:40 2011 +0000
     1.3 @@ -38,8 +38,66 @@
     1.4  		/// </param>
     1.5  		public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
     1.6  		{
     1.7 +			return IsApplicable(wfObject, toArmy) ? CheckAllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
     1.8 +		}
     1.9 +
    1.10 +		private bool IsApplicable(WarFoundryObject toObjectAdded, Army toArmy)
    1.11 +		{
    1.12 +			return IsApplicable(toArmy) || IsApplicable(toObjectAdded);
    1.13 +		}
    1.14 +
    1.15 +		protected virtual bool IsApplicable(Army toArmy)
    1.16 +		{
    1.17 +			return false;
    1.18 +		}
    1.19 +
    1.20 +		protected virtual bool IsApplicable(WarFoundryObject toObject)
    1.21 +		{
    1.22 +			bool isApplicable = false;
    1.23 +			UnitType unitType = GetUnitTypeFromObject(toObject);
    1.24 +
    1.25 +			if (unitType != null)
    1.26 +			{
    1.27 +				isApplicable = IsApplicable(unitType);
    1.28 +			}
    1.29 +
    1.30 +			return isApplicable;
    1.31 +		}
    1.32 +
    1.33 +		protected UnitType GetUnitTypeFromObject (WarFoundryObject toObject)
    1.34 +		{
    1.35 +			UnitType unitType = null;
    1.36 +
    1.37 +			if (toObject is UnitType)
    1.38 +			{
    1.39 +				unitType = (UnitType)toObject;
    1.40 +			}
    1.41 +			else if (toObject is Unit)
    1.42 +			{
    1.43 +				unitType = ((Unit)toObject).UnitType;
    1.44 +			}
    1.45 +
    1.46 +			return unitType;
    1.47 +		}
    1.48 +
    1.49 +		private bool IsApplicable (UnitType unitType)
    1.50 +		{
    1.51 +			bool isApplicable = false;
    1.52 +			foreach (UnitCountRequirementData requirement in requiredTypes)
    1.53 +			{
    1.54 +				if (requirement.UnitType.Equals(unitType))
    1.55 +				{
    1.56 +					isApplicable = true;
    1.57 +					break;
    1.58 +				}
    1.59 +			}
    1.60 +			return isApplicable;
    1.61 +		}
    1.62 +
    1.63 +		private Validation CheckAllowsAdding(WarFoundryObject wfObject, Army toArmy)
    1.64 +		{
    1.65  			Validation isValid = Validation.Passed;
    1.66 -
    1.67 +			
    1.68  			foreach (UnitCountRequirementData requirement in requiredTypes)
    1.69  			{
    1.70  				if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count)
    1.71 @@ -48,7 +106,7 @@
    1.72  					break;
    1.73  				}
    1.74  			}
    1.75 -
    1.76 +			
    1.77  			return isValid;
    1.78  		}
    1.79  
     2.1 --- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs	Sat Apr 09 19:14:11 2011 +0000
     2.2 +++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs	Sat Apr 09 19:57:40 2011 +0000
     2.3 @@ -19,42 +19,21 @@
     2.4  			requirementOnType = requirementOn;
     2.5  		}
     2.6  
     2.7 -		/// <summary>
     2.8 -		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
     2.9 -		/// </summary>
    2.10 -		/// <returns>
    2.11 -		/// A <code>Validation</code> enum to show the result of the validation
    2.12 -		/// </returns>
    2.13 -		/// <param name='wfObject'>
    2.14 -		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
    2.15 -		/// </param>
    2.16 -		/// <param name='toArmy'>
    2.17 -		/// The army to add the object to.
    2.18 -		/// </param>
    2.19 -		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    2.20 +		protected override bool IsApplicable(WarFoundryObject toObjectAdded)
    2.21  		{
    2.22 -			return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
    2.23 +			return base.IsApplicable(toObjectAdded) || IsRequirementOnType(toObjectAdded);
    2.24  		}
    2.25  
    2.26 -
    2.27 -		private bool IsApplicable(WarFoundryObject toObject, Army toArmy)
    2.28 +		private bool IsRequirementOnType(WarFoundryObject toObjectAdded)
    2.29  		{
    2.30 -			return IsApplicable(toArmy) || IsApplicable(toObject);
    2.31 +			return requirementOnType.Equals(toObjectAdded) || (toObjectAdded is Unit && requirementOnType.Equals(((Unit)toObjectAdded).UnitType));
    2.32  		}
    2.33  
    2.34 -
    2.35 -		private bool IsApplicable(Army toArmy)
    2.36 +		protected override bool IsApplicable(Army toArmy)
    2.37  		{
    2.38  			return toArmy.GetUnitTypeCount(requirementOnType) > 0;
    2.39  		}
    2.40  
    2.41 -
    2.42 -		private bool IsApplicable(WarFoundryObject toObject)
    2.43 -		{
    2.44 -			return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType));
    2.45 -		}
    2.46 -
    2.47 -
    2.48  		public override Validation ValidatesArmy(Army toArmy)
    2.49  		{
    2.50  			return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable;