view api/Objects/Unit.cs @ 57:e0ce5578e7c2

Re #61 - Complete structure of WarFoundry API objects * Add methods for adding and getting Notes and contained UnitTypes of a UnitType * Add method to get requirements of a unit * Separate out required abilities from optional abilities * Remove UnitAbility and handle by passing a boolean to the AddAbility method
author IBBoard <dev@ibboard.co.uk>
date Tue, 07 Apr 2009 14:43:04 +0000
parents 3a90f70dac73
children 3c77722a02b5
line wrap: on
line source

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

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Summary description for UnitInstance.
	/// </summary>
	public class Unit : WarFoundryObject
	{
		private UnitType type;
		private int size;
		private Army army;
		private Race race;
		private double points;
		private ArmyCategory cat;
		private Dictionary<UnitEquipmentItem, double> equipment = new Dictionary<UnitEquipmentItem, double>();
		public event DoubleValChangedDelegate PointsValueChanged;
		public event IntValChangedDelegate UnitSizeChanged;
		public event DoubleValChangedDelegate UnitEquipmentAmountChanged;

		public Unit(UnitType unitType, Army parentArmy) : this(unitType, unitType.MinSize, parentArmy){}

		public Unit(UnitType unitType, int startSize, Army parentArmy)
		{
			Army = parentArmy;
			type = unitType;
			Size = startSize;
			setInitialEquipment();
			CalcCost();
		}

		protected override string DefaultName()
		{
			if (type != null)
			{
				if (size == 1)
				{
					return type.Name;
				}
				else
				{
					return String.Format(Translation.GetTranslation("defaultUnitName"), size, type.Name);
				}
			}
			else
			{
				return "Unknown Unit";
			}
		}

		private void setInitialEquipment()
		{
			foreach (UnitEquipmentItem unitEquip in UnitType.GetEquipmentItems())
			{
				if (unitEquip.IsRequired)
				{
					if (CanEquipWithItem(unitEquip.ID))
					{
						equipment[unitEquip] = unitEquip.MinNumber;
					}
				}
			}
		}

		private void CalcCost()
		{
			String oldName = HasDefaultName() ? Name : null;
			double oldpoints = points;
			points = type.CostPerTrooper * AdditionalTroopers + type.BaseUnitCost;

			foreach (UnitEquipmentItem unitEquipItem in equipment.Keys)
			{
				EquipmentItem equipItem = unitEquipItem.EquipmentItem;
				double count = (double)equipment[unitEquipItem];
				
				if (unitEquipItem.IsRatioLimit)
				{
					if (unitEquipItem.RoundNumberUp)
					{
						points+= Math.Ceiling(size * count) * equipItem.Cost;
					}
					else
					{
						points+= Math.Floor(size * count) * equipItem.Cost;
					}
				}
				else
				{
					if (count == WarFoundryCore.INFINITY)
					{
						points+= size * equipItem.Cost;
					}
					else
					{
						points+= count * equipItem.Cost;
					}
				}
			}

			if (oldpoints!=points)
			{
				OnPointsValueChanged(oldpoints, points);
			}

			if (oldName!=null)
			{
				OnNameChanged(oldName, Name);
			}
		}
		
		public int AdditionalTroopers
		{
			get { return Math.Max(Size - type.BaseSize, 0); }
		}

		public int Size
		{
			get { return size; }
			set 
			{
				if (value!=size)
				{
					int oldValue = size;
					size = (value>0 ? value : 1);
					CalcCost();
					OnUnitSizeChanged(oldValue, size);
				}
			}
		}

		public UnitType UnitType
		{
			get { return type; }
		}

		public Army Army
		{
			get { return army; }
			set 
			{
				army = value;

				if (army == null)
				{
					Category = null;
				}
			}
		}

		public Race Race
		{
			get { return race; }
			set { race = value; }
		}

		public ArmyCategory Category
		{
			get 
			{ 
				if (cat==null)
				{
					if (Army!=null)
					{
						return Army.GetCategory(UnitType.MainCategory);
					}
					else
					{
						return null;
					}
				}
				else
				{
					return cat; 
				}
			}
			set { cat = value; }
		}

		public double PointsValue
		{
			get 
			{
				if (points == 0)
				{
					CalcCost();
				}

				return points;
			}
		}

		public UnitEquipmentItem[] GetAllowedOptionalEquipment()
		{
			List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();

			foreach (UnitEquipmentItem item in UnitType.GetEquipmentItems())
			{
				if (!item.IsRequired)
				{
					list.Add(item);
				}
			}

			return (UnitEquipmentItem[])list.ToArray();
		}

		public UnitEquipmentItem[] GetEquipment()
		{
			UnitEquipmentItem[] items = new UnitEquipmentItem[equipment.Count];
			int i = 0;

			foreach (UnitEquipmentItem item in equipment.Keys)
			{
				items[i++] = item;
			}

			return items;
		}

		public UnitEquipmentItem[] GetRequiredEquipment()
		{
			List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();

			foreach(UnitEquipmentItem item in equipment.Keys)
			{
				if (item.IsRequired)
				{
					list.Add(item);
				}
			}

			return (UnitEquipmentItem[])list.ToArray();
		}

		public double GetEquipmentAmount(UnitEquipmentItem item)
		{
			if (equipment.ContainsKey(item))
			{
				return (double)equipment[item];
			}
			else
			{
				return 0;
			}
		}

		public double GetEquipmentAmount(string equipID)
		{
			return GetEquipmentAmount(UnitType.GetEquipmentItem(equipID));
		}

		public void SetEquipmentAmount(UnitEquipmentItem equip, int amount)
		{
			if (equip.IsRatioLimit)
			{
				throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has a ratio limit, not an absolute limit");
			}
			
			if (amount <1 && amount != WarFoundryCore.INFINITY)
			{
				amount = 0;
			}
			
			SetEquipmentAmount(equip, amount);
		}
		
		public void SetEquipmentRatio(UnitEquipmentItem equip, double ratio)
		{
			if (!equip.IsRatioLimit)
			{
				throw new InvalidOperationException("Equipment with ID "+equip.EquipmentItemID+" for unit "+ID+" has an absolute limit, not a ratio limit");
			}
			
			if (ratio > 1)
			{
				ratio = 1;
			}
			else if (ratio < 0)
			{
				ratio = 0;
			}
			
			SetEquipmentAmount(equip, ratio);
		}
		
		private void SetEquipmentAmount(UnitEquipmentItem equip, double amount)
		{
			double oldAmount = 0;

			if (equipment.ContainsKey(equip))
			{
				oldAmount = equipment[equip];
			}

			if (amount!=oldAmount)
			{
				if (amount != 0)
				{
					equipment[equip] = amount;
				}
				else
				{
					equipment.Remove(equip);
				}

				OnUnitEquipmentAmountChanged(equip, oldAmount, amount);
				CalcCost();
			}
		}

		public bool CanEquipWithItem(string equipID)
		{
			string mutex = UnitType.GetEquipmentItem(equipID).MutexGroup;

			if (mutex == "")
			{
				return true;
			}

			foreach (UnitEquipmentItem item in equipment.Keys)
			{
				if (item.MutexGroup == mutex)
				{
					return false;
				}
			}

			return true;
		}

		private void OnPointsValueChanged(double oldValue, double newValue)
		{
			if (PointsValueChanged!=null)
			{
				PointsValueChanged(this, oldValue, newValue);
			}
		}

		private void OnUnitSizeChanged(int oldValue, int newValue)
		{
			if (UnitSizeChanged!=null)
			{
				UnitSizeChanged(this, oldValue, newValue);
			}
		}

		private void OnUnitEquipmentAmountChanged(UnitEquipmentItem equip, double oldValue, double newValue)
		{
			if (UnitEquipmentAmountChanged!=null)
			{
				UnitEquipmentAmountChanged(equip, oldValue, newValue);
			}
		}

		public Stats UnitStats
		{
			get { return UnitType.UnitStats; }
		}
	}
}