diff api/Requirements/UnitRequiresAtLeastRequirement.cs @ 0:520818033bb6

Initial commit of WarFoundry code
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 15:57:51 +0000
parents
children 306558904c2a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Requirements/UnitRequiresAtLeastRequirement.cs	Fri Dec 19 15:57:51 2008 +0000
@@ -0,0 +1,80 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using IBBoard.WarFoundry.API;
+using IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.API.Requirements
+{
+	/// <summary>
+	/// Summary description for UnitRequiresRequirement.
+	/// </summary>
+	public class UnitRequiresAtLeastRequirement : UnitRequirement
+	{
+		private UnitType[] requiredTypes;
+		private int[] requiredCounts;
+		private String unitList;
+
+		public UnitRequiresAtLeastRequirement(UnitType type, UnitType requiredUnitType) : this(type, new UnitType[]{requiredUnitType}, new int[]{1})
+		{
+		}
+		
+		public UnitRequiresAtLeastRequirement(UnitType type, UnitType[] requiredUnitTypes, int[] minNumsRequired) : base(type)
+		{
+			if (requiredUnitTypes.Length != minNumsRequired.Length)
+			{
+				throw new ArgumentException("List of required unit types and list of number of units required must be equal");
+			}
+			else if (requiredUnitTypes.Length == 1)
+			{
+				throw new ArgumentException("List of required unit types must not be empty");
+			}
+			
+			requiredTypes = requiredUnitTypes;
+			requiredCounts = minNumsRequired;
+
+			if (requiredTypes.Length > 1)
+			{
+				StringBuilder sb = new StringBuilder(requiredCounts[0]+" x "+requiredTypes[0].Name);
+
+				for (int i = 1; i<requiredTypes.Length; i++)
+				{
+					sb.Append(", "+requiredCounts[i]+" x "+requiredTypes[i].Name);
+				}
+
+				unitList = sb.ToString();
+			}
+			else
+			{
+				unitList = requiredTypes[0].Name;
+			}
+		}
+
+		public override string Description
+		{
+			get { return "The army must include at least the following units to include a unit of type "+unitType.Name+": "+unitList; }
+		}
+
+		protected override AbstractFailedRequirement CanRemoveFromArmy(Army army, UnitType type)
+		{
+			return null;
+		}
+
+		protected override AbstractFailedRequirement CanAddToArmy(Army army, UnitType type)
+		{
+			FailedRequirement failure = null;
+			int count = requiredTypes.Length;
+			
+			for (int i = 0; i < count; i++)
+			{
+				if (army.GetUnitTypeCount(requiredTypes[i]) < requiredCounts[i])
+				{				
+					failure = new FailedRequirement(this);
+					break;
+				}
+			}
+						
+			return failure;
+		}
+	}
+}