changeset 444:206a45fdfa9e

Re #350: Add requirement to allow N of unit for specific other units * Extend N for M requirement to start handling multiple unit types in an additive method * Extend requirement data object to handle multiple unit types
author IBBoard <dev@ibboard.co.uk>
date Wed, 07 Dec 2011 21:01:33 +0000
parents 86725e88052e
children 2d1bdd679f82
files API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs API/Objects/Requirement/UnitCountRequirementData.cs
diffstat 2 files changed, 58 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Sun Dec 04 20:40:31 2011 +0000
+++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirement.cs	Wed Dec 07 21:01:33 2011 +0000
@@ -68,20 +68,34 @@
 
 			foreach (UnitCountRequirementData limit in requiredTypes)
 			{
-				int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, unitType);
+				int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, unitType);
 
 				if (!IsValidByRequirement(unitType, toArmy, limit, allowedTypeCount))
 				{
-					failures.Add(String.Format("{0} {1} for every {2} {3} (would have {4} for {5})", limit.Count, limit.UnitType.Name, limit.AllowsCount, allowedType.Name, limitedTypeCount, allowedTypeCount));
+					string unitTypeList = GetUnitTypeList(limit);
+					failures.Add(String.Format("{0} {1} for every {2} {3} (would have {4} for {5})", limit.Count, unitTypeList, limit.AllowsCount, allowedType.Name, limitedTypeCount, allowedTypeCount));
 				}
 			}
 
 			return failures;
 		}
 
+		private string GetUnitTypeList(UnitCountRequirementData limit)
+		{
+			List<String> namesList = new List<String>();
+
+			foreach (UnitType unitType in limit.UnitTypes)
+			{
+				namesList.Add(unitType.Name);
+			}
+
+			string[] names = namesList.ToArray();
+			return String.Join(" or ", names);
+		}
+
 		private bool IsValidByRequirement(WarFoundryObject wfObject, Army toArmy, UnitCountRequirementData limit, int allowedTypeCount)
 		{
-			int limitedTypeCount = GetUnitTypeCount(toArmy, limit.UnitType, wfObject);
+			int limitedTypeCount = GetUnitTypesCount(toArmy, limit.UnitTypes, wfObject);
 			return IsValidByRequirement(limit, allowedTypeCount, limitedTypeCount);
 		}
 
@@ -118,6 +132,18 @@
 			return canAdd;
 		}
 
+		private int GetUnitTypesCount(Army toArmy, UnitType[] unitTypes, WarFoundryObject wfObject)
+		{
+			int count = 0;
+
+			foreach (UnitType unitType in unitTypes)
+			{
+				count += GetUnitTypeCount(toArmy, unitType, wfObject);
+			}
+
+			return count;
+		}
+
 		private int GetUnitTypeCount(Army toArmy, UnitType unitType, WarFoundryObject wfObject)
 		{
 			return toArmy.GetUnitTypeCount(unitType) + GetCountFromObject(wfObject, unitType);
@@ -221,14 +247,20 @@
 		}
 
 		/// <summary>
-		/// Adds a requirement for there to be one or more of a given UnitType, allowing one of this UnitType
+		/// Adds a requirement for there to be one or more of a given UnitType, allowing one of this UnitType. If multiple unit types
+		/// are supplied here then the number is additive (so 1 x unitType1 and 1 x unitType2 allows two of this UnitType).
 		/// </summary>
 		/// <param name='unitType'>
-		/// The unit type to require.
+		/// The unit type or types to require.
 		/// </param>
-		public void AddUnitTypeRequirement(UnitType unitType)
+		public void AddUnitTypeRequirement(params UnitType[] unitTypes)
 		{
-			AddUnitTypeRequirement(unitType, 1);
+			AddUnitTypeRequirement(1, unitTypes);
+		}
+
+		private void AddUnitTypeRequirement(int minCount, params UnitType[] unitTypes)
+		{
+			requiredTypes.Add(new UnitCountRequirementData(unitTypes, minCount, 1));
 		}
 	}
 }
--- a/API/Objects/Requirement/UnitCountRequirementData.cs	Sun Dec 04 20:40:31 2011 +0000
+++ b/API/Objects/Requirement/UnitCountRequirementData.cs	Wed Dec 07 21:01:33 2011 +0000
@@ -8,7 +8,7 @@
 {
 	public class UnitCountRequirementData
 	{
-		private UnitType unitType;
+		private UnitType[] unitTypes;
 		private int count;
 		private int allows;
 
@@ -17,16 +17,31 @@
 			//Do nothing special
 		}
 
-		public UnitCountRequirementData(UnitType unitType, int count, int allows)
+		public UnitCountRequirementData(UnitType unitType, int count, int allows) : this(new UnitType[]{unitType}, count, allows)
+		{
+			//Do nothing special
+		}
+
+		public UnitCountRequirementData(UnitType[] unitTypes, int count) : this(unitTypes, count, WarFoundryCore.INFINITY)
 		{
-			this.unitType = unitType;
+			//Do nothing special
+		}
+
+		public UnitCountRequirementData(UnitType[] unitTypes, int count, int allows)
+		{
+			this.unitTypes = unitTypes ?? new UnitType[0];
 			this.count = count;
 			this.allows = allows;
 		}
 
 		public UnitType UnitType
 		{
-			get { return unitType; }
+			get { return unitTypes.Length > 0 ? unitTypes[0] : null; }
+		}
+
+		public UnitType[] UnitTypes
+		{
+			get { return unitTypes; }
 		}
 
 		public int Count