view api/Objects/UnitType.cs @ 26:67db7f97b4ab

Reference new version of SharpZipLib from WarFoundry API .csproj no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sat, 14 Mar 2009 16:43:38 +0000
parents 306558904c2a
children b28be912adab
line wrap: on
line source

// This file (UnitType.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;
using System.Collections.Generic;
using System.Xml;
using IBBoard.Logging;
using IBBoard.WarFoundry.API.Requirements;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Summary description for Unit.
	/// </summary>
	public class UnitType : WarFoundryObject
	{
		protected Category mainCat;
		protected List<Category> categories = new List<Category>();
		protected Race race;
		protected int min, max, baseSize = 0;
		protected int minSize, maxSize;
		protected double baseUnitCost;
		protected double costPerTrooper;
		protected Stats stats;
		protected List<UnitRequirement> requirements = new List<UnitRequirement>();
		protected Hashtable equipment = new Hashtable();
		protected Hashtable equipmentExclusionGroups = new Hashtable();
		protected ArrayList equipmentKeyOrder = new ArrayList();


		public UnitType(string id, string typeName, Race parentRace) : base(id, typeName)
		{
			race = parentRace;
		}

		[Obsolete("Use three parameter constructor and setters")]
		public UnitType(string id, string typeName, string mainCategoryID, string[] allCategoryIDs, int minNum, int maxNum, int minimumSize, int maximumSize, double unitCost, double trooperCost, Stats unitStats, UnitRequirement[] unitRequirements, Race parentRace) : base(id, typeName)
		{
			race = parentRace;
			mainCat = race.GetCategory(mainCategoryID);

			foreach (string catID in allCategoryIDs)
			{
				categories.Add(race.GetCategory(catID));
			}
			
			MinNumber = minNum;			
			MaxNumber = maxNum;
			MinSize = minimumSize;			
			MaxSize = maximumSize;
			BaseUnitCost = unitCost;
			CostPerTrooper = trooperCost;
			UnitStats = unitStats;

			foreach (UnitRequirement requirement in requirements)
			{
				AddRequirement(requirement);
			}
		}

		public Race Race
		{
			get { return race; }
		}

		public virtual Category MainCategory
		{
			get
			{					
				return mainCat;
			}
			set
			{
				if (value!=null && categories.Contains(value))
				{
					mainCat = value;
				}
				else
				{
					throw new ArgumentException("MainCategory must exist in list of Categories");
				}	
			}
		}
		
		public virtual Category[] Categories
		{
			get 
			{				
				return categories.ToArray();
			}
		}

		public void AddCategory(Category cat)
		{
			if (!categories.Contains(cat))
			{
				categories.Add(cat);
			}
		}

		public int MinSize
		{
			get { return minSize; }
			set
			{
				minSize = (value >= 0 ? value : 0);
				CheckMinimumSize();
			}
		}

		public int MaxSize
		{
			get { return maxSize; }
			set
			{
				maxSize = (value >= -1 ? value : -1);
				CheckMinimumSize();
			}
		}
		
		public int BaseSize
		{
			get { return baseSize; }
		}

		public int MinNumber
		{
			get { return min; }
			set
			{
				min = (value >= 0 ? value : 0);
				CheckMinimumNumber();
			}
		}

		public int MaxNumber
		{
			get { return max; }
			set
			{
				max = (value >= -1 ? value : -1);
				CheckMinimumNumber();
			}
		}

		private void CheckMinimumNumber()
		{
			if (MinNumber > MaxNumber && MaxNumber!=-1)
			{
				MinNumber = MaxNumber;
				LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum number greater than their maximum number.", Name, ID);
			}
		}

		private void CheckMinimumSize()
		{
			if (MinSize > MaxSize && MaxSize!=-1)
			{
				MinSize = MaxNumber;
				LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum size greater than their maximum size.", Name, ID);
			}
		}
		
		public double BaseUnitCost
		{
			get { return baseUnitCost; }
			set { baseUnitCost = (value >= 0 ? value : 0); }
		}

		public double CostPerTrooper
		{
			get { return costPerTrooper; }
			set { costPerTrooper = (value >= 0 ? value : 0); }
		}

		protected override string DefaultName()
		{
			throw new InvalidOperationException("Unit type with id "+id+" did not have a name specified");
		}

		public Stats UnitStats
		{
			get 
			{
				return stats;
			}
			set
			{
				if (value!=null)
				{
					stats = value;
				}
			}
		}

		public UnitEquipmentItem GetEquipmentItem(string id)
		{
			return (UnitEquipmentItem)equipment[id];
		}

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

			foreach (string itemID in equipmentKeyOrder)
			{
				items[i++] = (UnitEquipmentItem)equipment[itemID];
			}

			return items;
		}

		public void AddRequirement(UnitRequirement requirement)
		{
			requirements.Add(requirement);
		}
		
		public List<FailedUnitRequirement> CanAddToArmy(Army army)
		{
			List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>();
			
			if (requirements!=null && requirements.Count > 0)
			{
				foreach (UnitRequirement requirement in requirements)
				{
					FailedUnitRequirement failure = (FailedUnitRequirement)requirement.CanAddToWarFoundryObject(army);
					
					if (failure!=null)
					{
						failures.Add(failure);
					}
				}
			}
			
			return failures;
		}
		
		public List<FailedUnitRequirement> CanRemoveFromArmy(Army army)
		{
			List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>();
			
			if (requirements!=null && requirements.Count > 0)
			{
				foreach (UnitRequirement requirement in requirements)
				{
					FailedUnitRequirement failure = (FailedUnitRequirement)requirement.CanRemoveFromWarFoundryObject(army);
					
					if (failure!=null)
					{
						failures.Add(failure);
					}
				}
			}
			
			return failures;
		}

		public UnitEquipmentItem[] GetEquipmentItemsByExclusionGroup(string group)
		{
			ArrayList list = (ArrayList)equipmentExclusionGroups[group];

			if (list == null)
			{
				return new UnitEquipmentItem[0];
			}
			else
			{
				return (UnitEquipmentItem[])list.ToArray(typeof(UnitEquipmentItem));
			}
		}
	}
}