view api/Objects/UnitEquipmentItem.cs @ 183:36adabb1c3ea

Re #198: Add slots with counts to units * Remove old Min/MaxNumber/Percentage for equipment and replace with limits * Refactor equipment selections and remove "numeric for ratio" as the limits handle the upper/lower limit differences * Stop equipment selections taking an amount of 0 for out of range amounts * Add "IsValid" property for selections * Removed use of "-1" as an 'infinity' limit - now use 100% as a more correct value * Change "unlimitedSize" limit in schema to "unitSizeLimit"
author IBBoard <dev@ibboard.co.uk>
date Mon, 26 Oct 2009 20:55:04 +0000
parents e83fc7b493f4
children cedf8bba1d52
line wrap: on
line source

// This file (UnitEquipmentItem.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 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 IBBoard.CustomMath;
using IBBoard.Limits;
using IBBoard.WarFoundry.API.Util;

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 double costMultiplier;
		private RoundType roundType;
		private string[] mutexGroups;
		private UnitType unitType;
		private string slotName = "";
		private AbstractLimit minLimit;
		private AbstractLimit maxLimit;

		public UnitEquipmentItem(EquipmentItem equipmentItem, UnitType equipmentFor)
			: this(equipmentItem, equipmentFor, new string[0])
		{
			//Do nothing extra
		}

		public UnitEquipmentItem(EquipmentItem equipmentItem, UnitType equipmentFor, params string[] mutexGroups)
		{
			item = equipmentItem;
			unitType = equipmentFor;
			this.mutexGroups = mutexGroups;
			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; }
		}

		[Obsolete("Use MutexGroups instead for greater flexibility")]
		public string MutexGroup
		{
			get { return (mutexGroups.Length == 0 ? "" : mutexGroups[0]); }
		}

		public String[] MutexGroups
		{
			get { return (string[]) mutexGroups.Clone(); }
		}

		public UnitType EquipmentForUnit
		{
			get { return unitType; }
		}

		public bool IsRatioLimit
		{
			get { return MinLimit is IPercentageLimit && MaxLimit is IPercentageLimit; }
		}
		
		/// <summary>
		/// Gets the Limit object for the minimum number of items that can be taken
		/// </summary>
		public AbstractLimit MinLimit
		{
			get
			{
				return (minLimit == null ? new UnlimitedLimit() : minLimit);
			}
			set
			{
				if (value != null)
				{
					minLimit = value;			
					
					if (maxLimit == null)
					{
						maxLimit = minLimit;
					}
				}
			}
		}
		
		/// <summary>
		/// Gets the Limit object for the maximum number of items that can be taken
		/// </summary>
		public AbstractLimit MaxLimit
		{
			get
			{
				return (maxLimit == null ? new UnlimitedLimit() : maxLimit);
			}
			set
			{
				if (value != null)
				{
					maxLimit = value;					
					
					if (minLimit == null)
					{
						minLimit = maxLimit;
					}
				}
			}
		}

		public EquipmentItem EquipmentItem
		{
			get { return item; }
		}

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

		public bool HasAlternatives()
		{
			if (MutexGroups.Length == 0)
			{
				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.GetEquipmentItemsByExclusionGroups(MutexGroups).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);
		}

		[Obsolete("Use UnitEquipmentUtil method instead")]
		public bool IsMutuallyExclusive(UnitEquipmentItem item)
		{
			return UnitEquipmentUtil.ItemsAreMutuallyExclusive(this, item);
		}

		public string SlotName
		{
			get { return slotName; }
			set
			{
				if (value != null && value != "")
				{
					slotName = value;
				}
			}
		}
	}
}