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