view api/Objects/UnitType.cs @ 0:520818033bb6

Initial commit of WarFoundry code
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 15:57:51 +0000
parents
children 613bc5eaac59
line wrap: on
line source

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 string mainCatID;
		protected Category[] categories;
		protected string[] categoryIDs;
		protected Race race;
		protected int min, max, baseSize = 0;
		protected int minSize, maxSize;
		protected double baseUnitCost;
		protected double costPerTrooper;
		protected Stats stats;
		protected UnitRequirement[] requirements;
		protected Hashtable equipment = new Hashtable();
		protected Hashtable equipmentExclusionGroups = new Hashtable();
		protected ArrayList equipmentKeyOrder = new ArrayList();

		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)
		{
			mainCatID = mainCategoryID;
			categoryIDs = allCategoryIDs;
			race = parentRace;
			
			if (minNum > maxNum && maxNum!=-1)
			{
				min = maxNum;
				LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum number greater than their maximum number.", typeName, id);
			}
			else
			{
				min = (minNum >= 0 ? minNum : 0);
			}
			
			max = maxNum;
			
			if (minimumSize > maximumSize && maximumSize!=-1)
			{
				minSize = maximumSize;
				LogNotifier.WarnFormat(GetType(), "Unit type {0} ({1}) had a minimum size greater than their maximum size.", typeName, id);
			}
			else
			{
				minSize = (minimumSize >= 0 ? minimumSize : 0);
			}
			
			maxSize = maximumSize;
			baseUnitCost = unitCost;
			costPerTrooper = trooperCost;
			requirements = unitRequirements;
			stats = unitStats;
		}

		public Race Race
		{
			get { return race; }
		}

		public virtual Category MainCategory
		{
			get
			{
				if (mainCat == null)
				{
					LogNotifier.Debug(GetType(), Name+" main category: "+mainCatID);
					mainCat = Race.GetCategory(mainCatID);
				}
					
				return mainCat;
			}
		}
		
		public virtual Category[] Categories
		{
			get 
			{
				if (categories == null)
				{
					categories = new Category[categoryIDs.Length];
					
					for (int i = 0; i<categoryIDs.Length; i++)
					{
						categories[i] = Race.GetCategory(categoryIDs[i]);
					}
				}
				
				return categories;
			}
		}

		public int MinSize
		{
			get { return minSize; }
		}

		public int MaxSize
		{
			get { return maxSize; }
		}
		
		public int BaseSize
		{
			get { return baseSize; }
		}

		public int MinNumber
		{
			get { return min; }
		}

		public int MaxNumber
		{
			get { return max; }
		}
		
		public double BaseUnitCost
		{
			get { return baseUnitCost; }
		}

		public double CostPerTrooper
		{
			get { return costPerTrooper; }
		}

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

		public Stats UnitStats
		{
			get 
			{
				return stats;
			}
		}

		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 List<FailedUnitRequirement> CanAddToArmy(Army army)
		{
			List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>();
			
			if (requirements!=null && requirements.Length > 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.Length > 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));
			}
		}
	}
}