view api/Requirements/UnitRequiresAtLeastRequirement.cs @ 197:ec1cfe3ef94e

* Add extra method for getting the number of items in a slot without including the given item (required when editing equipment in slots) no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Fri, 30 Oct 2009 21:02:48 +0000
parents 2f3cafb69799
children 6083010a005c
line wrap: on
line source

// This file (UnitRequiresAtLeastRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
//
// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.

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;
		}
	}
}