changeset 460:025319b6fa7a

Re #379: Fix validation of requirements to check for unit * Create max count requirement object * Re-add code for min/max counts as requirements
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Mar 2012 21:13:52 +0000
parents 7a00aeba3d2f
children f0594621e4a0
files API/Objects/Requirement/RaceRequiresNoMoreThanNUnitsRequirement.cs API/Objects/UnitType.cs IBBoard.WarFoundry.API.csproj
diffstat 3 files changed, 64 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Objects/Requirement/RaceRequiresNoMoreThanNUnitsRequirement.cs	Sun Mar 04 21:13:52 2012 +0000
     1.3 @@ -0,0 +1,57 @@
     1.4 +// This file (RaceRequiresNoMoreThanNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2012 IBBoard
     1.5 +// 
     1.6 +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
     1.7 +using System;
     1.8 +using IBBoard.Collections;
     1.9 +
    1.10 +namespace IBBoard.WarFoundry.API.Objects.Requirement
    1.11 +{
    1.12 +	public class RaceRequiresNoMoreThanNUnitsRequirement : RequiresNoMoreThanNOfUnitTypeRequirement<Race>
    1.13 +	{
    1.14 +		private static readonly string REQUIREMENT_ID_RACE = "RaceRequiresNoMoreThanNUnits";
    1.15 +
    1.16 +		public RaceRequiresNoMoreThanNUnitsRequirement(Race requirementOn, params UnitType[] requiredUnitTypes) : base(requirementOn, requiredUnitTypes)
    1.17 +		{
    1.18 +			//Do nothing special
    1.19 +		}
    1.20 +
    1.21 +		protected override int GetObjectCountFromArmy(Army toArmy, Race obj)
    1.22 +		{
    1.23 +			return toArmy.Race == AllowedObject ? 1 : 0;
    1.24 +		}
    1.25 +
    1.26 +		protected override bool IsApplicable(Army toArmy)
    1.27 +		{
    1.28 +			bool isApplicable = false;
    1.29 +			SimpleSet<UnitType> checkedTypes = new SimpleSet<UnitType>();
    1.30 +
    1.31 +			foreach (UnitCountRequirementData limit in ConstraintTypes)
    1.32 +			{
    1.33 +				foreach (UnitType unitType in limit.UnitTypes)
    1.34 +				{
    1.35 +					if (!checkedTypes.Contains(unitType))
    1.36 +					{
    1.37 +						if (toArmy.GetUnitTypeCount(unitType) > 0)
    1.38 +						{
    1.39 +							isApplicable = true;
    1.40 +							break;
    1.41 +						}
    1.42 +
    1.43 +						checkedTypes.Add(unitType);
    1.44 +					}
    1.45 +				}
    1.46 +			}
    1.47 +
    1.48 +			return isApplicable;
    1.49 +		}
    1.50 +
    1.51 +		public override string RequirementID
    1.52 +		{
    1.53 +			get
    1.54 +			{
    1.55 +				return REQUIREMENT_ID_RACE;
    1.56 +			}
    1.57 +		}
    1.58 +	}
    1.59 +}
    1.60 +
     2.1 --- a/API/Objects/UnitType.cs	Sun Mar 04 20:27:33 2012 +0000
     2.2 +++ b/API/Objects/UnitType.cs	Sun Mar 04 21:13:52 2012 +0000
     2.3 @@ -498,16 +498,16 @@
     2.4  
     2.5  			if (MinNumber != 0)
     2.6  			{
     2.7 -				//RequiresAtLeastNUnitsRequirement<Army> req = new RequiresAtLeastNUnitsRequirement<Army>();
     2.8 -				//req.AddUnitTypeRequirement(this, MinNumber);
     2.9 -				//reqs.Add(req);
    2.10 +				RaceRequiresAtLeastNUnitsRequirement req = new RaceRequiresAtLeastNUnitsRequirement(Race);
    2.11 +				req.AddUnitTypeRequirement(this, MinNumber);
    2.12 +				reqs.Add(req);
    2.13  			}
    2.14  
    2.15  			if (MaxNumber != WarFoundryCore.INFINITY)
    2.16  			{
    2.17 -				//RequiresNoMoreThanNOfUnitTypeRequirement<IWarFoundryObject> req = new RequiresNoMoreThanNOfUnitTypeRequirement<IWarFoundryObject>();
    2.18 -				//req.AddUnitTypeRequirement(this, MaxNumber);
    2.19 -				//reqs.Add(req);
    2.20 +				RaceRequiresNoMoreThanNUnitsRequirement req = new RaceRequiresNoMoreThanNUnitsRequirement(Race);
    2.21 +				req.AddUnitTypeRequirement(this, MaxNumber);
    2.22 +				reqs.Add(req);
    2.23  			}
    2.24  
    2.25  			return reqs.ToArray();
     3.1 --- a/IBBoard.WarFoundry.API.csproj	Sun Mar 04 20:27:33 2012 +0000
     3.2 +++ b/IBBoard.WarFoundry.API.csproj	Sun Mar 04 21:13:52 2012 +0000
     3.3 @@ -195,6 +195,7 @@
     3.4      <Compile Include="API\Objects\Requirement\RequiresNUnitsForMObjectsRequirement.cs" />
     3.5      <Compile Include="API\Objects\Requirement\AbstractUnitRequirement.cs" />
     3.6      <Compile Include="API\Objects\Requirement\RaceRequiresAtLeastNUnitsRequirement.cs" />
     3.7 +    <Compile Include="API\Objects\Requirement\RaceRequiresNoMoreThanNUnitsRequirement.cs" />
     3.8    </ItemGroup>
     3.9    <ItemGroup>
    3.10      <Reference Include="System.Xml" />