view api/Objects/UnitEquipmentItem.cs @ 101:f7b9423c2a5a

Big mess of updates, breaking our rules on "commit little and often" because the code was so ugly. This revision will be broken for the WinForms UI, but as MonoDevelop/eSVN don't have a way of committing multiple projects in one go it can't be helped (Eclipse's Team Sync view could handle it) Fixes #122: Make usage of percentage or ratio common * All usage of ratio amounts for equipment items should now assume percentage * Properly calculate number taken for ratio selection (divide by 0 now we're using percentages) Fixes #118: Allow equipment amounts of "ratio" equipment to be define as absolute or ratio amounts * Added extra commands that differentiate between ratio and absolute amounts Fixes #120: Numeric limit equipment items show large percentages * Now made formatting treat ratios as percentages (don't multiply by 100) * Move string formatting to UnitEquipmentItem...Selection classes * Add method to Unit to say whether an equipment item is a numeric or ratio amount
author IBBoard <dev@ibboard.co.uk>
date Thu, 13 Aug 2009 21:09:20 +0000
parents ced5a18d9f52
children 2f3cafb69799
line wrap: on
line source

// This file (UnitEquipmentItem.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.Xml;
using IBBoard.Lang;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Summary description for UnitEquipmentItem.
	/// </summary>
	public class UnitEquipmentItem : WarFoundryObject
	{
		private EquipmentItem item;
		private bool required;
		private bool roundUp;
		private int minNum;
		private int maxNum;
		private double minPercentage;
		private double maxPercentage;
		private double costMultiplier;
		private RoundType roundType;
		private string mutexGroup;
		private UnitType unitType;

		public UnitEquipmentItem(EquipmentItem equipmentItem, UnitType equipmentFor)
		{
			item = equipmentItem;
			unitType = equipmentFor;
			unitType.AddEquipmentItem(this);
		}
		
		public override string Name
		{
			get 
			{
				return item.Name;
			}
			set
			{
				base.Name = value;
			}
		}

		public override string ID {
			get
			{
				return (EquipmentForUnit == null ? base.ID : EquipmentForUnit.ID) + EquipmentItemID;
			}
			set
			{
				base.ID = value;
			}
		}

		
		public string EquipmentItemID
		{
			get { return item.ID; }
		}

        public double Cost
        {
            get
			{
				return IBBMath.Round(EquipmentItem.Cost * CostMultiplier, CostRoundType);
			}
        }

		public double CostMultiplier
		{
			get { return costMultiplier; }
			set
			{
				costMultiplier = value;
			}
		}

		public RoundType CostRoundType
		{
			get { return roundType; }
			set
			{
				roundType = value;
			}	
		}

		public bool IsRequired
		{
			get { return required; }
			set { required = value; }
		}

		public bool RoundNumberUp
		{
			get { return roundUp; }
			set { roundUp = value; }
		}

		public string MutexGroup
		{
			get { return mutexGroup; }
			set { mutexGroup = (value == null ? "" : value.Trim()); }
		}

		public UnitType EquipmentForUnit
		{
			get { return unitType; }
		}
		
		public bool IsRatioLimit
		{
			get { return minPercentage!=100 || maxPercentage!=100; }
		}
		
		public int MinNumber
		{
			get { return minNum; }
			set
			{
				if (value >=0 || value == WarFoundryCore.INFINITY)
				{
					minNum = value;
				}
				//TODO: Check Min<Max
			}
		}
		
		public int MaxNumber
		{
			get { return maxNum; }
			set
			{
				if (value >=0 || value == WarFoundryCore.INFINITY)
				{
					maxNum = value;
				}
				//TODO: Check Min<Max
			}
		}
		
		public double MinPercentage
		{
			get { return minPercentage; }
			set
			{
				if (value >=0 && value <= 100)
				{
					minPercentage = value;
				}
				//TODO: Check Min<Max
			}
		}
		
		public double MaxPercentage
		{
			get { return maxPercentage; }
			set
			{
				if (value >=0 && value <= 100)
				{
					maxPercentage = value;
				}
				//TODO: Check Min<Max
			}
		}

		public EquipmentItem EquipmentItem
		{
			get { return item; }
		}

		public override string ToString()
		{
			return EquipmentItem.Name+ " ("+EquipmentItem.Cost+"pts each)";
		}

		public bool HasAlternatives()
		{
			if (MutexGroup=="")
			{
				return false;
			}
			else if (EquipmentForUnit == null)
			{
				return false;
			}
			else
			{
				//If the number of items in the MutEx group is greater than one then it must be this item plus another
				return EquipmentForUnit.GetEquipmentItemsByExclusionGroup(MutexGroup).Length > 1;
			}
		}

		public ArmourType ItemArmourType
		{
			get { return EquipmentItem.ItemArmourType; }
		}
		
		public string Description
		{
			get { return EquipmentItem.Description; }
		}
		
		public Race EquipmentForRace
		{
			get { return EquipmentItem.EquipmentForRace; }
		}

		public bool CanBeUsedWithItem(EquipmentItem item)
		{
			return EquipmentItem.CanBeUsedWithItem(item);
		}

		public bool CanBeUsedWithArmourType(ArmourType otherItemType)
		{
			return EquipmentItem.CanBeUsedWithArmourType(otherItemType);
		}
	}
}