changeset 458:680db2462e34

Re #379: * Move GetObjectCountFromArmy(Army, OBJECT_TYPE) to top level and implement * Fix army validation for appropriate "NA" returns * Make basic "Requires..." requirements abstract so that we always need to make specific versions (e.g. UnitRequires... that knows how to check amount of units)
author IBBoard <dev@ibboard.co.uk>
date Sun, 26 Feb 2012 20:16:33 +0000
parents 8e01c3174cc3
children 7a00aeba3d2f
files API/Objects/Requirement/AbstractUnitRequirement.cs API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs
diffstat 6 files changed, 76 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/Requirement/AbstractUnitRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
+++ b/API/Objects/Requirement/AbstractUnitRequirement.cs	Sun Feb 26 20:16:33 2012 +0000
@@ -153,6 +153,8 @@
 
 		protected abstract int GetObjectCountFromArmy(Army toArmy);
 
+		protected abstract int GetObjectCountFromArmy(Army toArmy, OBJECT_TYPE obj);
+
 		protected virtual int GetObjectCountFromObject(IWarFoundryObject wfObject)
 		{
 			return allowedObject.Equals(wfObject) ? 1 : 0;
--- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
+++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun Feb 26 20:16:33 2012 +0000
@@ -13,7 +13,7 @@
 	///
 	/// The definition for how this requirement is built from a data file is defined in the <see cref="UnitRequiresAtLeastNUnitsRequirementFactory"/> class.
 	/// </summary>
-	public class RequiresAtLeastNUnitsRequirement<OBJECT_TYPE> : AbstractUnitRequirement<OBJECT_TYPE> where OBJECT_TYPE : IWarFoundryObject
+	public abstract class RequiresAtLeastNUnitsRequirement<OBJECT_TYPE> : AbstractUnitRequirement<OBJECT_TYPE> where OBJECT_TYPE : IWarFoundryObject
 	{
 		public static readonly string REQUIREMENT_ID = "RequiresAtLeastNUnits";
 
@@ -156,16 +156,26 @@
 		/// </param>
 		public override Validation ValidatesArmy(Army toValidate)
 		{
-			Validation isValid = Validation.Passed;
+			Validation isValid = Validation.NotApplicable;
+			int allowedTypeCount = GetObjectCountFromArmy(toValidate, AllowedObject);
 
-			foreach (UnitCountRequirementData requirement in ConstraintTypes)
+			if (allowedTypeCount > 0)
 			{
-				if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count)
+				isValid = Validation.Passed;
+
+				foreach (UnitCountRequirementData requirement in ConstraintTypes)
 				{
-					isValid = Validation.Failed;
-					break;
+					if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count)
+					{
+						isValid = Validation.Failed;
+						break;
+					}
 				}
 			}
+			else
+			{
+				isValid = Validation.NotApplicable;
+			}
 
 			return isValid;
 		}
--- a/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
+++ b/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs	Sun Feb 26 20:16:33 2012 +0000
@@ -132,8 +132,6 @@
 			return GetObjectCountFromArmy(toArmy, obj) + GetCountFromObject(wfObject, obj);
 		}
 
-		protected abstract int GetObjectCountFromArmy(Army toArmy, OBJECT_TYPE obj);
-
 		private int GetCountFromObject(IWarFoundryObject wfObject, OBJECT_TYPE limitedType)
 		{
 			return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
--- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
+++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Feb 26 20:16:33 2012 +0000
@@ -11,7 +11,7 @@
 	/// <summary>
 	/// A requirement where a WarFoundryObject cannot be taken in an army if more than N of a UnitType will be in the army.
 	/// </summary>
-	public class RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE> : AbstractUnitRequirement<OBJECT_TYPE> where OBJECT_TYPE : IWarFoundryObject
+	public abstract class RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE> : AbstractUnitRequirement<OBJECT_TYPE> where OBJECT_TYPE : IWarFoundryObject
 	{
 		public static readonly string REQUIREMENT_ID = "RequiresNoMoreThanNUnits";
 
@@ -102,16 +102,26 @@
 		/// </param>
 		public override Validation ValidatesArmy(Army army)
 		{
-			Validation canAdd = Validation.Passed;
-			
-			foreach (UnitCountRequirementData limit in ConstraintTypes)
+			Validation canAdd = Validation.NotApplicable;
+			int allowedTypeCount = GetObjectCountFromArmy(army, AllowedObject);
+
+			if (allowedTypeCount > 0)
 			{
-				if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
+				canAdd = Validation.Passed;
+
+				foreach (UnitCountRequirementData limit in ConstraintTypes)
 				{
-					canAdd = Validation.Failed;
-					break;
+					if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
+					{
+						canAdd = Validation.Failed;
+						break;
+					}
 				}
 			}
+			else
+			{
+				canAdd = Validation.NotApplicable;
+			}
 			
 			return canAdd;
 		}
--- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
+++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirement.cs	Sun Feb 26 20:16:33 2012 +0000
@@ -16,24 +16,49 @@
 		{
 		}
 
-		protected override bool IsApplicable(IWarFoundryObject toObjectAdded)
+		public override Validation AllowsAdding(IWarFoundryObject wfObject, Army toArmy)
+		{
+			return IsApplicable(wfObject, toArmy) ? base.AllowsAdding(wfObject, toArmy) : Validation.NotApplicable;
+		}
+
+		protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy)
 		{
-			return base.IsApplicable(toObjectAdded) || IsRequirementOnType(toObjectAdded);
+			return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy);
+		}
+
+		protected override bool IsApplicable(IWarFoundryObject toObject)
+		{
+			return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType));
 		}
 
-		private bool IsRequirementOnType(IWarFoundryObject toObjectAdded)
+		private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy)
 		{
-			return AllowedObject.Equals(toObjectAdded) || (toObjectAdded is Unit && AllowedObject.Equals(((Unit)toObjectAdded).UnitType));
+			bool isApplicable = false;
+			UnitType addedType = toObject as UnitType;
+
+			if (addedType == null)
+			{
+				addedType = (toObject is Unit) ? ((Unit)toObject).UnitType : null;
+			}
+
+			if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0)
+			{
+				foreach (UnitCountRequirementData limit in ConstraintTypes)
+				{
+					if (Arrays.Contains(limit.UnitTypes, addedType))
+					{
+						isApplicable = true;
+						break;
+					}
+				}
+			}
+
+			return isApplicable;
 		}
 
-		protected override bool IsApplicable(Army toArmy)
+		protected override int GetObjectCountFromArmy(Army toArmy, UnitType obj)
 		{
-			return toArmy.GetUnitTypeCount(AllowedObject) > 0;
-		}
-
-		public override Validation ValidatesArmy(Army toArmy)
-		{
-			return IsApplicable(toArmy) ? base.ValidatesArmy(toArmy) : Validation.NotApplicable;
+			return toArmy.GetUnitTypeCount(obj);
 		}
 	}
 }
--- a/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
+++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Feb 26 20:16:33 2012 +0000
@@ -68,6 +68,11 @@
 
 			return isApplicable;
 		}
+
+		protected override int GetObjectCountFromArmy(Army toArmy, UnitType obj)
+		{
+			return toArmy.GetUnitTypeCount(obj);
+		}
 	}
 }