diff API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs @ 456:52baffdd2ab9

Re #379: Fix validation of requirements to check for unit * Default to not being applicable - make requirements say when they are * Fix UnitRequiresNUnitsForMUnitsRequirement to not be applicable when adding unrelated units (even if army itself fails the rule) * Fix type casting issues in equality check
author IBBoard <dev@ibboard.co.uk>
date Sat, 25 Feb 2012 16:35:31 +0000
parents afc6410e4efc
children 8e01c3174cc3
line wrap: on
line diff
--- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs	Wed Feb 22 20:45:39 2012 +0000
+++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirement.cs	Sat Feb 25 16:35:31 2012 +0000
@@ -39,12 +39,7 @@
 
 		protected override bool IsApplicable(IWarFoundryObject toObject, Army toArmy)
 		{
-			return IsApplicable(toArmy) || IsApplicable(toObject);
-		}
-
-		protected override bool IsApplicable(Army toArmy)
-		{
-			return toArmy.GetUnitTypeCount(AllowedObject) > 0;
+			return IsApplicable(toObject) || IsApplicableForRequiredType(toObject, toArmy);
 		}
 
 		protected override bool IsApplicable(IWarFoundryObject toObject)
@@ -52,6 +47,31 @@
 			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 RequiredTypes)
+				{
+					if (Arrays.Contains(limit.UnitTypes, addedType))
+					{
+						isApplicable = true;
+						break;
+					}
+				}
+			}
+
+			return isApplicable;
+		}
+
 		public override string RequirementID
 		{
 			get