changeset 344:8585dfacac3b

Re #27: Unit Requirements * Take unit being added into account
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 10:47:58 +0000
parents acd390dba551
children 008537acf244
files API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs
diffstat 1 files changed, 36 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Wed Apr 06 20:05:46 2011 +0000
+++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirement.cs	Sat Apr 09 10:47:58 2011 +0000
@@ -42,7 +42,7 @@
 			
 			foreach (UnitCountRequirementData limit in limitedTypes)
 			{
-				if (toArmy.GetUnitTypeCount(limit.UnitType) > limit.Count)
+				if (GetUnitTypeCount(toArmy, limit.UnitType, wfObject) > limit.Count)
 				{
 					canAdd = Validation.Failed;
 					break;
@@ -52,6 +52,16 @@
 			return canAdd;
 		}
 
+		private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
+		{
+			return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
+		}
+
+		private int GetCountFromObject(WarFoundryObject wfObject, UnitType limitedType)
+		{
+			return (limitedType.Equals(wfObject) || (wfObject is Unit && ((Unit)wfObject).UnitType.Equals(limitedType))) ? 1 : 0;
+		}
+
 		/// <summary>
 		/// Adds a requirement for there to be no more than maxCount of a given UnitType
 		/// </summary>
@@ -76,6 +86,31 @@
 		{
 			AddUnitTypeRequirement(unitType, 0);
 		}
+
+		/// <summary>
+		/// Checks whether the supplied army is currently valid according to this requirement.
+		/// </summary>
+		/// <returns>
+		/// A <code>Validation</code> enum to show the result of the validation
+		/// </returns>
+		/// <param name='toValidate'>
+		/// The army to validate
+		/// </param>
+		public Validation ValidatesArmy(Army army)
+		{
+			Validation canAdd = Validation.Passed;
+			
+			foreach (UnitCountRequirementData limit in limitedTypes)
+			{
+				if (army.GetUnitTypeCount(limit.UnitType) > limit.Count)
+				{
+					canAdd = Validation.Failed;
+					break;
+				}
+			}
+			
+			return canAdd;
+		}
 	}
 }