view api/Objects/UnitType.cs @ 8:613bc5eaac59

Re #9 - Make WarFoundry loading granular * Remove specific staged loading classes * Rework category loading for GameSystem and Race to make it use AddCategory(Category) method * Promote staged loading from Native Factory to all Factories level * Refactor XML Factory to use smaller methods Also removed some commented code that isn't used any more
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Jan 2009 19:24:13 +0000
parents 520818033bb6
children ac232763858b
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)
				{
					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));
			}
		}
	}
}