view api/Objects/UnitEquipmentNumericSelection.cs @ 155:df61d26c23fb

Re #178: "crash" when loading old .army file with equipment tags that are "old" * Try to tidy up the exception messages - ideally they'll end up translated and localised
author IBBoard <dev@ibboard.co.uk>
date Mon, 28 Sep 2009 19:48:37 +0000
parents 2f3cafb69799
children 36adabb1c3ea
line wrap: on
line source

// This file (UnitEquipmentNumericSelection.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 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;

namespace IBBoard.WarFoundry.API.Objects
{	
	/// <summary>
	/// An object to hold the selection of a unit's equipment where the selection was made as an absolute number
	/// </summary>
	public class UnitEquipmentNumericSelection : AbstractUnitEquipmentItemSelection
	{	
		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item, double amount) : base(unit, item, amount)
		{
		}
		
		public UnitEquipmentNumericSelection(Unit unit, UnitEquipmentItem item) : base(unit, item, item.MinNumber)
		{
		}
					
		public override int NumberTaken
		{
			get
			{
				return (AmountTaken == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : (int) AmountTaken);
			}
		}
		
		protected bool IsWholeNumber(double newValue)
		{
			return newValue == Math.Round(newValue);
		}
		
		protected override bool IsInRange(double newValue)
		{
			bool isInRange = IsWholeNumber(newValue);
			
			if (newValue == WarFoundryCore.INFINITY)
			{
				isInRange = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY);
			}
			else if (isInRange)
			{
				int minLimit = (EquipmentItem.MinNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MinNumber);
				int maxLimit = (EquipmentItem.MaxNumber == WarFoundryCore.INFINITY ? EquipmentForUnit.Size : EquipmentItem.MaxNumber);
				isInRange = (minLimit <= newValue) && (newValue <= maxLimit);
			}
			
			return isInRange;
		}

		public override string GetEquipmentAmountString ()
		{
			return GetEquipmentAmountString(AmountTaken);
		}

		/// <summary>
		/// Gets a string representing the amount of equipment taken when the amount is a numeric selection
		/// </summary>
		/// <param name="amount">
		/// A <see cref="System.Double"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		public static string GetEquipmentAmountString(double amount)
		{
			string amountString;
			
			if (amount == WarFoundryCore.INFINITY)
			{
				amountString = "all"; //TODO: Translate
			}
			else
			{
				amountString = amount.ToString();
			}

			return amountString;
		}
	}
}