changeset 457:8e01c3174cc3

Re #379: Fix validation of requirements to check for unit * Rename public accessor for unit types to ConstraintTypes so that "no more than" requirements don't work with "required types" * Remove class level version of ConstraintTypes to fall back to abstract class version * Make sure we cascade allowedObject up through RequiresNoMoreThanNOfUnitTypeRequirement * Rebuild UnitRequiresNoMoreThanNOfUnitTypeRequirement "IsApplicable" so that we don't get applicable for unrelated types, even if the requirement is currently in a failure state
author IBBoard <dev@ibboard.co.uk>
date Sun, 26 Feb 2012 15:14:01 +0000
parents 52baffdd2ab9
children 680db2462e34
files API/Objects/Requirement/AbstractUnitRequirement.cs API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs
diffstat 6 files changed, 57 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/Requirement/AbstractUnitRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
+++ b/API/Objects/Requirement/AbstractUnitRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
@@ -24,7 +24,7 @@
 
 		public abstract string RequirementID { get; }
 
-		protected List<UnitCountRequirementData> RequiredTypes { get { return requiredTypes; } }
+		protected List<UnitCountRequirementData> ConstraintTypes { get { return requiredTypes; } }
 
 		public abstract void AddUnitTypeRequirement(UnitType unitType);
 
--- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
+++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
@@ -33,7 +33,7 @@
 		protected override bool TypeEquals(object obj)
 		{
 			RequiresAtLeastNUnitsRequirement<OBJECT_TYPE> otherReq = obj as RequiresAtLeastNUnitsRequirement<OBJECT_TYPE>;
-			if (!Collections.Collections.AreEqual(RequiredTypes, otherReq.RequiredTypes))
+			if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes))
 			{
 				return false;
 			}
@@ -47,7 +47,7 @@
 		{
 			int hash = 0;
 
-			foreach (UnitCountRequirementData req in RequiredTypes)
+			foreach (UnitCountRequirementData req in ConstraintTypes)
 			{
 				hash += req.UnitType.GetHashCode();
 			}
@@ -93,7 +93,7 @@
 		private bool IsApplicable(UnitType unitType)
 		{
 			bool isApplicable = false;
-			foreach (UnitCountRequirementData requirement in RequiredTypes)
+			foreach (UnitCountRequirementData requirement in ConstraintTypes)
 			{
 				if (requirement.UnitType.Equals(unitType))
 				{
@@ -108,7 +108,7 @@
 		{
 			Validation isValid = Validation.Passed;
 			
-			foreach (UnitCountRequirementData requirement in RequiredTypes)
+			foreach (UnitCountRequirementData requirement in ConstraintTypes)
 			{
 				if (GetUnitTypeCount(toArmy, requirement.UnitType, wfObject) < requirement.Count)
 				{
@@ -131,7 +131,7 @@
 		/// </param>
 		public void AddUnitTypeRequirement(UnitType unitType, int minCount)
 		{
-			RequiredTypes.Add(new UnitCountRequirementData(unitType, minCount));
+			ConstraintTypes.Add(new UnitCountRequirementData(unitType, minCount));
 		}
 
 		/// <summary>
@@ -158,7 +158,7 @@
 		{
 			Validation isValid = Validation.Passed;
 
-			foreach (UnitCountRequirementData requirement in RequiredTypes)
+			foreach (UnitCountRequirementData requirement in ConstraintTypes)
 			{
 				if (toValidate.GetUnitTypeCount(requirement.UnitType) < requirement.Count)
 				{
@@ -179,7 +179,7 @@
 		{
 			List<string> failures = new List<string>();
 
-			foreach (UnitCountRequirementData requirement in RequiredTypes)
+			foreach (UnitCountRequirementData requirement in ConstraintTypes)
 			{
 				int unitCount = army.GetUnitTypeCount(requirement.UnitType);
 
@@ -201,7 +201,7 @@
 		{
 			List<string> failures = new List<string>();
 
-			foreach (UnitCountRequirementData requirement in RequiredTypes)
+			foreach (UnitCountRequirementData requirement in ConstraintTypes)
 			{
 				int unitCount = GetUnitTypeCount(army, requirement.UnitType, toAdd);
 
--- a/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
+++ b/API/Objects/Requirement/RequiresNUnitsForMObjectsRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
@@ -18,7 +18,7 @@
 		{
 			int hash = 0;
 
-			foreach (UnitCountRequirementData req in RequiredTypes)
+			foreach (UnitCountRequirementData req in ConstraintTypes)
 			{
 				hash += req.UnitType.GetHashCode();
 			}
@@ -29,7 +29,7 @@
 		protected override bool TypeEquals(object obj)
 		{
 			RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE> otherReq = (RequiresNUnitsForMObjectsRequirement<OBJECT_TYPE>)obj;
-			if (!Collections.Collections.AreEqual(RequiredTypes, otherReq.RequiredTypes))
+			if (!Collections.Collections.AreEqual(ConstraintTypes, otherReq.ConstraintTypes))
 			{
 				return false;
 			}
@@ -49,7 +49,7 @@
 			List<string> failures = new List<string>();
 			int allowedObjectCount = GetAllowedObjectCount(toArmy, AllowedObject, obj);
 
-			foreach (UnitCountRequirementData limit in RequiredTypes)
+			foreach (UnitCountRequirementData limit in ConstraintTypes)
 			{
 				int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, obj);
 
@@ -96,7 +96,7 @@
 			bool typeFound = (wfObject == (IWarFoundryObject)AllowedObject || addedUnitType == (IWarFoundryObject)AllowedObject);
 			int allowedTypeCount = GetAllowedObjectCount(toArmy, AllowedObject, wfObject);
 
-			foreach (UnitCountRequirementData limit in RequiredTypes)
+			foreach (UnitCountRequirementData limit in ConstraintTypes)
 			{
 				typeFound |= (addedUnitType == limit.UnitType);
 
@@ -148,7 +148,7 @@
 			{
 				canAdd = Validation.Passed;
 
-				foreach (UnitCountRequirementData limit in RequiredTypes)
+				foreach (UnitCountRequirementData limit in ConstraintTypes)
 				{
 					int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes);
 
@@ -177,7 +177,7 @@
 			List<string> failures = new List<string>();
 			int allowedTypeCount = GetAllowedObjectCount(army);
 
-			foreach (UnitCountRequirementData limit in RequiredTypes)
+			foreach (UnitCountRequirementData limit in ConstraintTypes)
 			{
 				int limitedTypeCount = GetUnitTypesCount(army, limit.UnitTypes);
 
@@ -212,7 +212,7 @@
 		/// </param>
 		public void AddUnitTypeRequirement(UnitType unitType, int minCount, int allowedCount)
 		{
-			RequiredTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount));
+			ConstraintTypes.Add(new UnitCountRequirementData(unitType, minCount, allowedCount));
 		}
 
 		/// <summary>
@@ -258,7 +258,7 @@
 		/// </param>
 		public void AddUnitTypeRequirement(int allowsAmount, params UnitType[] unitTypes)
 		{
-			RequiredTypes.Add(new UnitCountRequirementData(unitTypes, 1, allowsAmount));
+			ConstraintTypes.Add(new UnitCountRequirementData(unitTypes, 1, allowsAmount));
 		}
 
 		/// <summary>
@@ -276,7 +276,7 @@
 		/// </param>
 		public void AddUnitTypeRequirement(int minCount, int allowsAmount, params UnitType[] unitTypes)
 		{
-			RequiredTypes.Add(new UnitCountRequirementData(unitTypes, minCount, allowsAmount));
+			ConstraintTypes.Add(new UnitCountRequirementData(unitTypes, minCount, allowsAmount));
 		}
 	}
 }
--- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
+++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
@@ -14,11 +14,10 @@
 	public class RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE> : AbstractUnitRequirement<OBJECT_TYPE> where OBJECT_TYPE : IWarFoundryObject
 	{
 		public static readonly string REQUIREMENT_ID = "RequiresNoMoreThanNUnits";
-		private List<UnitCountRequirementData> limitedTypes;
 
-		public RequiresNoMoreThanNOfUnitTypeRequirement(params UnitType[] limitedUnitTypes) : base(default(OBJECT_TYPE), limitedUnitTypes)
+		public RequiresNoMoreThanNOfUnitTypeRequirement(OBJECT_TYPE allowedObject, params UnitType[] limitedUnitTypes) : base(allowedObject, limitedUnitTypes)
 		{
-			limitedTypes = new List<UnitCountRequirementData>();
+			//Do nothing special
 		}
 
 		public override string RequirementID
@@ -45,7 +44,7 @@
 		{
 			Validation canAdd = Validation.Passed;
 			
-			foreach (UnitCountRequirementData limit in limitedTypes)
+			foreach (UnitCountRequirementData limit in ConstraintTypes)
 			{
 				if (GetUnitTypeCount(toArmy, limit.UnitType, wfObject) > limit.Count)
 				{
@@ -78,7 +77,7 @@
 		/// </param>
 		public void AddUnitTypeRequirement(UnitType unitType, int maxCount)
 		{
-			limitedTypes.Add(new UnitCountRequirementData(unitType, maxCount));
+			ConstraintTypes.Add(new UnitCountRequirementData(unitType, maxCount));
 		}
 
 		/// <summary>
@@ -105,7 +104,7 @@
 		{
 			Validation canAdd = Validation.Passed;
 			
-			foreach (UnitCountRequirementData limit in limitedTypes)
+			foreach (UnitCountRequirementData limit in ConstraintTypes)
 			{
 				if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
 				{
@@ -120,14 +119,14 @@
 		protected override bool TypeEquals(object obj)
 		{
 			RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE> other = (RequiresNoMoreThanNOfUnitTypeRequirement<OBJECT_TYPE>)obj;
-			return Collections.Collections.AreEqual(limitedTypes, other.limitedTypes);
+			return Collections.Collections.AreEqual(ConstraintTypes, other.ConstraintTypes);
 		}
 
 		public override int GetHashCode()
 		{
 			int hash = 0;
 
-			foreach (UnitCountRequirementData limit in limitedTypes)
+			foreach (UnitCountRequirementData limit in ConstraintTypes)
 			{
 				hash += limit.UnitType.GetHashCode();
 			}
@@ -152,7 +151,7 @@
 		{
 			List<string> failures = new List<string>();
 
-			foreach (UnitCountRequirementData requirement in limitedTypes)
+			foreach (UnitCountRequirementData requirement in ConstraintTypes)
 			{
 				int unitCount = army.GetUnitTypeCount(requirement.UnitType);
 
@@ -174,7 +173,7 @@
 		{
 			List<string> failures = new List<string>();
 
-			foreach (UnitCountRequirementData requirement in limitedTypes)
+			foreach (UnitCountRequirementData requirement in ConstraintTypes)
 			{
 				int unitCount = GetUnitTypeCount(army, requirement.UnitType, toAdd);
 
--- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
+++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
@@ -59,7 +59,7 @@
 
 			if (addedType != null && toArmy.GetUnitTypeCount(AllowedObject) > 0)
 			{
-				foreach (UnitCountRequirementData limit in RequiredTypes)
+				foreach (UnitCountRequirementData limit in ConstraintTypes)
 				{
 					if (Arrays.Contains(limit.UnitTypes, addedType))
 					{
--- a/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
+++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs	Sun Feb 26 15:14:01 2012 +0000
@@ -12,11 +12,9 @@
 	/// </summary>
 	public class UnitRequiresNoMoreThanNOfUnitTypeRequirement : RequiresNoMoreThanNOfUnitTypeRequirement<UnitType>
 	{
-		private UnitType requirementOnType;
-
-		public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType requirementOn) : base()
+		public UnitRequiresNoMoreThanNOfUnitTypeRequirement(UnitType allowedObject, params UnitType[] unitTypes) : base(allowedObject, unitTypes)
 		{
-			requirementOnType = requirementOn;
+			//Do nothing special
 		}
 
 		/// <summary>
@@ -38,17 +36,37 @@
 
 		protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy)
 		{
-			return IsApplicable(toArmy) || IsApplicable(toObject);
-		}
-
-		protected override bool IsApplicable(Army toArmy)
-		{
-			return toArmy.GetUnitTypeCount(requirementOnType) > 0;
+			return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy);
 		}
 
 		protected override bool IsApplicable(IWarFoundryObject toObject)
 		{
-			return requirementOnType.Equals(toObject) || (toObject is Unit && requirementOnType.Equals(((Unit)toObject).UnitType));
+			return AllowedObject.Equals(toObject) || (toObject is Unit && AllowedObject.Equals(((Unit)toObject).UnitType));
+		}
+
+		private bool IsApplicableForRequiredType(IWarFoundryObject toObject, Army toArmy)
+		{
+			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;
 		}
 	}
 }