changeset 344:8585dfacac3b

Re #27: Unit Requirements * Take unit being added into account
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 10:47:58 +0000
parents acd390dba551
children 008537acf244
files API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs
diffstat 1 files changed, 36 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Wed Apr 06 20:05:46 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sat Apr 09 10:47:58 2011 +0000
     1.3 @@ -42,7 +42,7 @@
     1.4  			
     1.5  			foreach (UnitCountRequirementData limit in limitedTypes)
     1.6  			{
     1.7 -				if (toArmy.GetUnitTypeCount(limit.UnitType) > limit.Count)
     1.8 +				if (GetUnitTypeCount(toArmy, limit.UnitType, wfObject) > limit.Count)
     1.9  				{
    1.10  					canAdd = Validation.Failed;
    1.11  					break;
    1.12 @@ -52,6 +52,16 @@
    1.13  			return canAdd;
    1.14  		}
    1.15  
    1.16 +		private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
    1.17 +		{
    1.18 +			return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
    1.19 +		}
    1.20 +
    1.21 +		private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
    1.22 +		{
    1.23 +			return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
    1.24 +		}
    1.25 +
    1.26  		/// <summary>
    1.27  		/// Adds a requirement for there to be no more than maxCount of a given UnitType
    1.28  		/// </summary>
    1.29 @@ -76,6 +86,31 @@
    1.30  		{
    1.31  			AddUnitTypeRequirement(unitType, 0);
    1.32  		}
    1.33 +
    1.34 +		/// <summary>
    1.35 +		/// Checks whether the supplied army is currently valid according to this requirement.
    1.36 +		/// </summary>
    1.37 +		/// <returns>
    1.38 +		/// A <code>Validation</code> enum to show the result of the validation
    1.39 +		/// </returns>
    1.40 +		/// <param name='toValidate'>
    1.41 +		/// The army to validate
    1.42 +		/// </param>
    1.43 +		public Validation ValidatesArmy(Army army)
    1.44 +		{
    1.45 +			Validation canAdd = Validation.Passed;
    1.46 +			
    1.47 +			foreach (UnitCountRequirementData limit in limitedTypes)
    1.48 +			{
    1.49 +				if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
    1.50 +				{
    1.51 +					canAdd = Validation.Failed;
    1.52 +					break;
    1.53 +				}
    1.54 +			}
    1.55 +			
    1.56 +			return canAdd;
    1.57 +		}
    1.58  	}
    1.59  }
    1.60