view api/Util/UnitEquipmentUtil.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 6fe336109128
children 3577bfb7702b
line wrap: on
line source

// This file (UnitEquipmentUtil.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;
using System.Collections.Generic;
using System.Text;
using IBBoard.Limits;
using IBBoard.WarFoundry.API.Objects;

namespace IBBoard.WarFoundry.API.Util
{
	public class UnitEquipmentUtil
	{
		/// <summary>
		/// Gets an array of allowed <see cref="UnitEquipmentItem"/>s based on the current selections of the unit, taking in to account Mutex groups and other limits.
		/// </summary>
		/// <param name="unit">The <see cref="Unit"/> to get equipment items for</param>
		/// <returns>The array of allowed <see cref="UnitEquipmentItem"/>s</returns>
		public static UnitEquipmentItem[] GetAllowedEquipmentItems(Unit unit)
		{
			List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();
			UnitEquipmentItem[] currItems = unit.GetEquipment();

			foreach (UnitEquipmentItem item in GetAllEquipmentItems(unit))
			{
				bool allowed = IsAllowedByMutex(item, currItems);

				if (allowed)
				{
					list.Add(item);
				}
			}

			return list.ToArray();
		}

		private static bool IsAllowedByMutex(UnitEquipmentItem item, UnitEquipmentItem[] currItems)
		{
			bool allowed = true;

			foreach (UnitEquipmentItem currItem in currItems)
			{
				if (ItemsAreMutuallyExclusive(currItem, item))
				{
					allowed = false;
					break;
				}
			}

			return allowed;
		}

		/// <summary>
		/// Gets a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code> because of mutex groups.
		/// </summary>
		/// <param name="unit">The unit that wants to take the equipment item</param>
		/// <param name="item">The item to check blocking items for</param>
		/// <returns>a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code></returns>
		public static List<UnitEquipmentItem> GetBlockingEquipmentItems(Unit unit, UnitEquipmentItem item)
		{
			List<UnitEquipmentItem> items = new List<UnitEquipmentItem>();
			UnitEquipmentItem[] currItems = unit.GetEquipment();

			foreach (UnitEquipmentItem unitItem in currItems)
			{
				if (!ItemsAreMutuallyExclusive(unitItem, item))
				{
					items.Add(unitItem);
				}
			}

			return items;
		}

		public static UnitEquipmentItem[] GetAllEquipmentItems(Unit unit)
		{
			return unit.UnitType.GetEquipmentItems();
		}

		public static bool ItemsAreMutuallyExclusive(UnitEquipmentItem item1, UnitEquipmentItem item2)
		{
			bool areMutex = false;
			string[] item1mutex = item1.MutexGroups;
			string[] item2mutex = item2.MutexGroups;

			foreach (string mutex in item1mutex)
			{
				foreach (string otherMutex in item2mutex)
				{
					if (mutex.Equals(otherMutex))
					{
						areMutex = true;
						goto postLoop;
					}
				}
			}
			postLoop:

			return areMutex;
		}

		public static int GetMaxEquipmentCount (Unit unit, UnitEquipmentItem equip)
		{
			int max = equip.MaxLimit.GetLimit(unit.Size);
			AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit(equip.SlotName);
			
			if (!(limit is UnlimitedLimit))
			{
				int slotMax = limit.GetLimit(unit.Size) - unit.GetEquipmentAmountInSlot(equip.SlotName);
				max = Math.Min(slotMax, max);
			}
			
			return max;
		}
	}
}