view api/Requirements/UnitRequiresAtLeastRequirement.cs @ 82:3ea0ab04352b

* Fix line terminators no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sat, 27 Jun 2009 18:59:49 +0000
parents 306558904c2a
children 2f3cafb69799
line wrap: on
line source

// This file (UnitRequiresAtLeastRequirement.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
//
// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL 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;
		}
	}
}