diff API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirement.cs @ 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 afc6410e4efc
children 680db2462e34
line wrap: on
line diff
--- 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;
 		}
 	}
 }