changeset 345:008537acf244

Re #27: Unit requirements * Make basic "Requires at least" requirement take into account the unit type being added * Make unit version of "requires at least" requirement pass execution up to the matching parent method
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 11:00:09 +0000
parents 8585dfacac3b
children b0ba1d25f51e
files API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs
diffstat 2 files changed, 23 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sat Apr 09 10:47:58 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sat Apr 09 11:00:09 2011 +0000
     1.3 @@ -38,7 +38,28 @@
     1.4  		/// </param>
     1.5  		public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
     1.6  		{
     1.7 -			return this.ValidatesArmy(toArmy);
     1.8 +			Validation isValid = Validation.Passed;
     1.9 +
    1.10 +			foreach (UnitCountRequirementData requirement in requiredTypes)
    1.11 +			{
    1.12 +				if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count)
    1.13 +				{
    1.14 +					isValid = Validation.Failed;
    1.15 +					break;
    1.16 +				}
    1.17 +			}
    1.18 +
    1.19 +			return isValid;
    1.20 +		}
    1.21 +
    1.22 +		private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
    1.23 +		{
    1.24 +			return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
    1.25 +		}
    1.26 +
    1.27 +		private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
    1.28 +		{
    1.29 +			return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
    1.30  		}
    1.31  
    1.32  		/// <summary>
     2.1 --- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs	Sat Apr 09 10:47:58 2011 +0000
     2.2 +++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs	Sat Apr 09 11:00:09 2011 +0000
     2.3 @@ -33,7 +33,7 @@
     2.4  		/// </param>
     2.5  		public override Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
     2.6  		{
     2.7 -			return IsApplicable(wfObject, toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable;
     2.8 +			return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
     2.9  		}
    2.10  
    2.11