changeset 356:51cccccf3669

Re #27: Unit requirements * Initial work to try to make unit types return requirements for count limits * Add requirements when limits aren't defaults * Start adding equality/hash code methods
author IBBoard <dev@ibboard.co.uk>
date Mon, 25 Apr 2011 15:11:20 +0000
parents 47712a323cf2
children 50d0d3b39a0b
files API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs API/Objects/Requirement/UnitCountRequirementData.cs API/Objects/UnitType.cs
diffstat 3 files changed, 218 insertions(+), 140 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Mon Apr 25 10:32:51 2011 +0000
     1.2 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Mon Apr 25 15:11:20 2011 +0000
     1.3 @@ -1,116 +1,154 @@
     1.4 -// This file (RequiresNoMoreThanNOfUnitTypeRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 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.WarFoundry.API.Objects;
     1.9 -using System.Collections.Generic;
    1.10 +// This file (RequiresNoMoreThanNOfUnitTypeRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
    1.11 +// 
    1.12 +// 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.13 +using System;
    1.14 +using IBBoard.WarFoundry.API.Objects;
    1.15 +using System.Collections.Generic;
    1.16 +
    1.17 +namespace IBBoard.WarFoundry.API.Objects.Requirement
    1.18 +{
    1.19 +	/// <summary>
    1.20 +	/// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army.
    1.21 +	/// </summary>
    1.22 +	public class RequiresNoMoreThanNOfUnitTypeRequirement : IRequirement
    1.23 +	{
    1.24 +		private List<UnitCountRequirementData> limitedTypes;
    1.25 +
    1.26 +		public RequiresNoMoreThanNOfUnitTypeRequirement(params UnitType[] limitedUnitTypes)
    1.27 +		{
    1.28 +			limitedTypes = new List<UnitCountRequirementData>();
    1.29 +
    1.30 +			foreach (UnitType unitType in limitedUnitTypes)
    1.31 +			{
    1.32 +				AddUnitTypeRequirement(unitType);
    1.33 +			}
    1.34 +		}
    1.35 +
    1.36 +		/// <summary>
    1.37 +		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
    1.38 +		/// </summary>
    1.39 +		/// <returns>
    1.40 +		/// A <code>Validation</code> enum to show the result of the validation
    1.41 +		/// </returns>
    1.42 +		/// <param name='wfObject'>
    1.43 +		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
    1.44 +		/// </param>
    1.45 +		/// <param name='toArmy'>
    1.46 +		/// The army to add the object to.
    1.47 +		/// </param>
    1.48 +		public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
    1.49 +		{
    1.50 +			Validation canAdd = Validation.Passed;
    1.51 +			
    1.52 +			foreach (UnitCountRequirementData limit in limitedTypes)
    1.53 +			{
    1.54 +				if (GetUnitTypeCount(toArmy, limit.UnitType, wfObject) > limit.Count)
    1.55 +				{
    1.56 +					canAdd = Validation.Failed;
    1.57 +					break;
    1.58 +				}
    1.59 +			}
    1.60 +			
    1.61 +			return canAdd;
    1.62 +		}
    1.63 +
    1.64 +		private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
    1.65 +		{
    1.66 +			return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
    1.67 +		}
    1.68 +
    1.69 +		private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
    1.70 +		{
    1.71 +			return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
    1.72 +		}
    1.73 +
    1.74 +		/// <summary>
    1.75 +		/// Adds a requirement for there to be no more than maxCount of a given UnitType
    1.76 +		/// </summary>
    1.77 +		/// <param name='unitType'>
    1.78 +		/// The unit type to limit.
    1.79 +		/// </param>
    1.80 +		/// <param name='minCount'>
    1.81 +		/// The maximum number of that type that must exist.
    1.82 +		/// </param>
    1.83 +		public void AddUnitTypeRequirement(UnitType unitType, int maxCount)
    1.84 +		{
    1.85 +			limitedTypes.Add(new UnitCountRequirementData(unitType, maxCount));
    1.86 +		}
    1.87 +
    1.88 +		/// <summary>
    1.89 +		/// Adds a requirement for there to be none of a given UnitType
    1.90 +		/// </summary>
    1.91 +		/// <param name='unitType'>
    1.92 +		/// The unit type to limit.
    1.93 +		/// </param>
    1.94 +		public void AddUnitTypeRequirement(UnitType unitType)
    1.95 +		{
    1.96 +			AddUnitTypeRequirement(unitType, 0);
    1.97 +		}
    1.98 +
    1.99 +		/// <summary>
   1.100 +		/// Checks whether the supplied army is currently valid according to this requirement.
   1.101 +		/// </summary>
   1.102 +		/// <returns>
   1.103 +		/// A <code>Validation</code> enum to show the result of the validation
   1.104 +		/// </returns>
   1.105 +		/// <param name='toValidate'>
   1.106 +		/// The army to validate
   1.107 +		/// </param>
   1.108 +		public Validation ValidatesArmy(Army army)
   1.109 +		{
   1.110 +			Validation canAdd = Validation.Passed;
   1.111 +			
   1.112 +			foreach (UnitCountRequirementData limit in limitedTypes)
   1.113 +			{
   1.114 +				if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
   1.115 +				{
   1.116 +					canAdd = Validation.Failed;
   1.117 +					break;
   1.118 +				}
   1.119 +			}
   1.120 +			
   1.121 +			return canAdd;
   1.122 +		}
   1.123 +
   1.124 +		public override bool Equals(object obj)
   1.125 +		{
   1.126 +			if (obj == null)
   1.127 +			{
   1.128 +				return false;
   1.129 +			}
   1.130 +			else if (!(obj is RequiresNoMoreThanNOfUnitTypeRequirement))
   1.131 +			{
   1.132 +				return false;
   1.133 +			}
   1.134 +			else
   1.135 +			{
   1.136 +				RequiresNoMoreThanNOfUnitTypeRequirement other = (RequiresNoMoreThanNOfUnitTypeRequirement)obj;
   1.137  
   1.138 -namespace IBBoard.WarFoundry.API.Objects.Requirement
   1.139 -{
   1.140 -	/// <summary>
   1.141 -	/// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army.
   1.142 -	/// </summary>
   1.143 -	public class RequiresNoMoreThanNOfUnitTypeRequirement : IRequirement
   1.144 -	{
   1.145 -		private List<UnitCountRequirementData> limitedTypes;
   1.146 +				if (limitedTypes.Count != other.limitedTypes.Count)
   1.147 +				{
   1.148 +					return false;
   1.149 +				}
   1.150 +				else
   1.151 +				{
   1.152 +					foreach (UnitCountRequirementData req in limitedTypes)
   1.153 +					{
   1.154 +						if (!other.limitedTypes.Contains(req))
   1.155 +						{
   1.156  
   1.157 -		public RequiresNoMoreThanNOfUnitTypeRequirement(params UnitType[] limitedUnitTypes)
   1.158 -		{
   1.159 -			limitedTypes = new List<UnitCountRequirementData>();
   1.160 -
   1.161 -			foreach (UnitType unitType in limitedUnitTypes)
   1.162 -			{
   1.163 -				AddUnitTypeRequirement(unitType);
   1.164 -			}
   1.165 +						}
   1.166 +					}
   1.167 +					
   1.168 +					return true;
   1.169 +				}
   1.170 +			}
   1.171  		}
   1.172  
   1.173 -		/// <summary>
   1.174 -		/// Checks whether the supplied WarFoundryObject can be added to the supplied army.
   1.175 -		/// </summary>
   1.176 -		/// <returns>
   1.177 -		/// A <code>Validation</code> enum to show the result of the validation
   1.178 -		/// </returns>
   1.179 -		/// <param name='wfObject'>
   1.180 -		/// The object that we want to add. This may be involved in the check, or it may not affect the evaluation of the requirement
   1.181 -		/// </param>
   1.182 -		/// <param name='toArmy'>
   1.183 -		/// The army to add the object to.
   1.184 -		/// </param>
   1.185 -		public virtual Validation AllowsAdding(WarFoundryObject wfObject, Army toArmy)
   1.186 +		public override int GetHashCode()
   1.187  		{
   1.188 -			Validation canAdd = Validation.Passed;
   1.189 -			
   1.190 -			foreach (UnitCountRequirementData limit in limitedTypes)
   1.191 -			{
   1.192 -				if (GetUnitTypeCount(toArmy, limit.UnitType, wfObject) > limit.Count)
   1.193 -				{
   1.194 -					canAdd = Validation.Failed;
   1.195 -					break;
   1.196 -				}
   1.197 -			}
   1.198 -			
   1.199 -			return canAdd;
   1.200 -		}
   1.201 -
   1.202 -		private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
   1.203 -		{
   1.204 -			return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
   1.205 -		}
   1.206 -
   1.207 -		private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
   1.208 -		{
   1.209 -			return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
   1.210 -		}
   1.211 -
   1.212 -		/// <summary>
   1.213 -		/// Adds a requirement for there to be no more than maxCount of a given UnitType
   1.214 -		/// </summary>
   1.215 -		/// <param name='unitType'>
   1.216 -		/// The unit type to limit.
   1.217 -		/// </param>
   1.218 -		/// <param name='minCount'>
   1.219 -		/// The maximum number of that type that must exist.
   1.220 -		/// </param>
   1.221 -		public void AddUnitTypeRequirement(UnitType unitType, int maxCount)
   1.222 -		{
   1.223 -			limitedTypes.Add(new UnitCountRequirementData(unitType, maxCount));
   1.224 -		}
   1.225 -
   1.226 -		/// <summary>
   1.227 -		/// Adds a requirement for there to be none of a given UnitType
   1.228 -		/// </summary>
   1.229 -		/// <param name='unitType'>
   1.230 -		/// The unit type to limit.
   1.231 -		/// </param>
   1.232 -		public void AddUnitTypeRequirement(UnitType unitType)
   1.233 -		{
   1.234 -			AddUnitTypeRequirement(unitType, 0);
   1.235 -		}
   1.236 -
   1.237 -		/// <summary>
   1.238 -		/// Checks whether the supplied army is currently valid according to this requirement.
   1.239 -		/// </summary>
   1.240 -		/// <returns>
   1.241 -		/// A <code>Validation</code> enum to show the result of the validation
   1.242 -		/// </returns>
   1.243 -		/// <param name='toValidate'>
   1.244 -		/// The army to validate
   1.245 -		/// </param>
   1.246 -		public Validation ValidatesArmy(Army army)
   1.247 -		{
   1.248 -			Validation canAdd = Validation.Passed;
   1.249 -			
   1.250 -			foreach (UnitCountRequirementData limit in limitedTypes)
   1.251 -			{
   1.252 -				if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
   1.253 -				{
   1.254 -					canAdd = Validation.Failed;
   1.255 -					break;
   1.256 -				}
   1.257 -			}
   1.258 -			
   1.259 -			return canAdd;
   1.260 -		}
   1.261 -	}
   1.262 -}
   1.263 -
   1.264 +			return base.GetHashCode ();
   1.265 +		}
   1.266 +	}
   1.267 +}
   1.268 +
     2.1 --- a/API/Objects/Requirement/UnitCountRequirementData.cs	Mon Apr 25 10:32:51 2011 +0000
     2.2 +++ b/API/Objects/Requirement/UnitCountRequirementData.cs	Mon Apr 25 15:11:20 2011 +0000
     2.3 @@ -1,31 +1,53 @@
     2.4 -// This file (UnitCountRequirementData.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard
     2.5 -// 
     2.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.
     2.7 -using System;
     2.8 -using IBBoard.WarFoundry.API.Objects;
     2.9 -
    2.10 -namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.11 -{
    2.12 -	public class UnitCountRequirementData
    2.13 -	{
    2.14 -		private UnitType unitType;
    2.15 -		private int count;
    2.16 -
    2.17 -		public UnitCountRequirementData(UnitType unitType, int count)
    2.18 -		{
    2.19 -			this.unitType = unitType;
    2.20 -			this.count = count;
    2.21 +// This file (UnitCountRequirementData.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard
    2.22 +// 
    2.23 +// 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.
    2.24 +using System;
    2.25 +using IBBoard.WarFoundry.API.Objects;
    2.26 +
    2.27 +namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.28 +{
    2.29 +	public class UnitCountRequirementData
    2.30 +	{
    2.31 +		private UnitType unitType;
    2.32 +		private int count;
    2.33 +
    2.34 +		public UnitCountRequirementData(UnitType unitType, int count)
    2.35 +		{
    2.36 +			this.unitType = unitType;
    2.37 +			this.count = count;
    2.38 +		}
    2.39 +
    2.40 +		public UnitType UnitType
    2.41 +		{
    2.42 +			get { return unitType; }
    2.43 +		}
    2.44 +
    2.45 +		public int Count
    2.46 +		{
    2.47 +			get { return count; }
    2.48 +		}
    2.49 +
    2.50 +		public override bool Equals (object obj)
    2.51 +		{
    2.52 +			if (obj == null)
    2.53 +			{
    2.54 +				return false;
    2.55 +			}
    2.56 +			else if (!(obj is UnitCountRequirementData))
    2.57 +			{
    2.58 +				return false;
    2.59 +			}
    2.60 +			else
    2.61 +			{
    2.62 +				UnitCountRequirementData other = (UnitCountRequirementData)obj;
    2.63 +				return UnitType.Equals(other.UnitType) && Count == other.Count;
    2.64 +			}
    2.65  		}
    2.66  
    2.67 -		public UnitType UnitType
    2.68 +		public override int GetHashCode()
    2.69  		{
    2.70 -			get { return unitType; }
    2.71 +			return UnitType.GetHashCode() + Count;
    2.72  		}
    2.73 -
    2.74 -		public int Count
    2.75 -		{
    2.76 -			get { return count; }
    2.77 -		}
    2.78 -	}
    2.79 -}
    2.80 -
    2.81 +	}
    2.82 +}
    2.83 +
     3.1 --- a/API/Objects/UnitType.cs	Mon Apr 25 10:32:51 2011 +0000
     3.2 +++ b/API/Objects/UnitType.cs	Mon Apr 25 15:11:20 2011 +0000
     3.3 @@ -19,7 +19,9 @@
     3.4  	{
     3.5  		private Category mainCat;
     3.6  		private Race race;
     3.7 -		private int min, max, baseSize = 0;
     3.8 +		private int min = 0;
     3.9 +		private int max = WarFoundryCore.INFINITY;
    3.10 +		private int baseSize = 0;
    3.11  		private int minSize, maxSize;
    3.12  		private double baseUnitCost;
    3.13  		private double costPerTrooper;
    3.14 @@ -465,7 +467,23 @@
    3.15  
    3.16  		public IRequirement[] GetRequirements()
    3.17  		{
    3.18 -			return requirements.ToArray();
    3.19 +			List<IRequirement> reqs = new List<IRequirement>(requirements);
    3.20 +
    3.21 +			if (MinNumber != 0)
    3.22 +			{
    3.23 +				RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
    3.24 +				req.AddUnitTypeRequirement(this, MinNumber);
    3.25 +				reqs.Add(req);
    3.26 +			}
    3.27 +
    3.28 +			if (MaxNumber != WarFoundryCore.INFINITY)
    3.29 +			{
    3.30 +				RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
    3.31 +				req.AddUnitTypeRequirement(this, MaxNumber);
    3.32 +				reqs.Add(req);
    3.33 +			}
    3.34 +
    3.35 +			return reqs.ToArray();
    3.36  		}
    3.37  
    3.38  		[Obsolete]