diff api/Objects/UnitType.cs @ 12:ac232763858b

Re #9 - Make WarFoundry API use smaller methods * Obselete old Category and UnitType constructors and add minimal constructors * Add setters to properties where needed * Add value sanity checking to Category and UnitType * Set default values for percentages, choices and points for Category * Make XML factory use new constructor and setter properties * Add DuplicateItemException to project file
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Jan 2009 20:52:29 +0000
parents 613bc5eaac59
children 306558904c2a
line wrap: on
line diff
--- a/api/Objects/UnitType.cs	Sun Jan 18 16:24:03 2009 +0000
+++ b/api/Objects/UnitType.cs	Sun Jan 18 20:52:29 2009 +0000
@@ -12,54 +12,48 @@
 	/// </summary>
 	public class UnitType : WarFoundryObject
 	{
-		protected Category mainCat;
-		protected string mainCatID;
-		protected Category[] categories;
-		protected string[] categoryIDs;
+		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 UnitRequirement[] requirements;
+		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)
 		{
-			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);
+			mainCat = race.GetCategory(mainCategoryID);
+
+			foreach (string catID in allCategoryIDs)
+			{
+				categories.Add(race.GetCategory(catID));
 			}
 			
-			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);
+			MinNumber = minNum;			
+			MaxNumber = maxNum;
+			MinSize = minimumSize;			
+			MaxSize = maximumSize;
+			BaseUnitCost = unitCost;
+			CostPerTrooper = trooperCost;
+			UnitStats = unitStats;
+
+			foreach (UnitRequirement requirement in requirements)
+			{
+				AddRequirement(requirement);
 			}
-			else
-			{
-				minSize = (minimumSize >= 0 ? minimumSize : 0);
-			}
-			
-			maxSize = maximumSize;
-			baseUnitCost = unitCost;
-			costPerTrooper = trooperCost;
-			requirements = unitRequirements;
-			stats = unitStats;
 		}
 
 		public Race Race
@@ -70,42 +64,56 @@
 		public virtual Category MainCategory
 		{
 			get
-			{
-				if (mainCat == null)
-				{
-					mainCat = Race.GetCategory(mainCatID);
-				}
-					
+			{					
 				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 
-			{
-				if (categories == null)
-				{
-					categories = new Category[categoryIDs.Length];
-					
-					for (int i = 0; i<categoryIDs.Length; i++)
-					{
-						categories[i] = Race.GetCategory(categoryIDs[i]);
-					}
-				}
-				
-				return categories;
+			{				
+				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
@@ -116,21 +124,51 @@
 		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; }
+			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()
@@ -144,6 +182,13 @@
 			{
 				return stats;
 			}
+			set
+			{
+				if (value!=null)
+				{
+					stats = value;
+				}
+			}
 		}
 
 		public UnitEquipmentItem GetEquipmentItem(string id)
@@ -163,12 +208,17 @@
 
 			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.Length > 0)
+			if (requirements!=null && requirements.Count > 0)
 			{
 				foreach (UnitRequirement requirement in requirements)
 				{
@@ -188,7 +238,7 @@
 		{
 			List<FailedUnitRequirement> failures = new List<FailedUnitRequirement>();
 			
-			if (requirements!=null && requirements.Length > 0)
+			if (requirements!=null && requirements.Count > 0)
 			{
 				foreach (UnitRequirement requirement in requirements)
 				{