changeset 56:9561ef46c6fb

Re #61 - Complete structure of WarFoundry API objects * Add adding of abilities and equipment to UnitType * Add getting of all, required and optional abilities * Add UnitAbility class to store UnitType's reference to Ability object Also: * Convert UnitType to using Genericed collections * Reduce visibility of properties to private
author IBBoard <dev@ibboard.co.uk>
date Sun, 05 Apr 2009 19:57:32 +0000
parents 9080366031c0
children e0ce5578e7c2
files IBBoard.WarFoundry.API.csproj api/Objects/UnitAbility.cs api/Objects/UnitType.cs
diffstat 3 files changed, 167 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.csproj	Sun Apr 05 19:21:51 2009 +0000
+++ b/IBBoard.WarFoundry.API.csproj	Sun Apr 05 19:57:32 2009 +0000
@@ -124,6 +124,7 @@
     <Compile Include="api\Factories\Xml\WarFoundryXmlArmyFactory.cs" />
     <Compile Include="api\Factories\Xml\WarFoundryXmlFactoryUtils.cs" />
     <Compile Include="api\Factories\Xml\AbstractStagedLoadedSubFactory.cs" />
+    <Compile Include="api\Objects\UnitAbility.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\ICSharpCode.SharpZipLib.dll" />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Objects/UnitAbility.cs	Sun Apr 05 19:57:32 2009 +0000
@@ -0,0 +1,67 @@
+//  This file (UnitAbility.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;
+
+namespace IBBoard.WarFoundry.API.Objects
+{
+	public class UnitAbility : WarFoundryObject
+	{		
+		private Ability ability;
+		private bool isRequired;
+		
+		public UnitAbility(Ability ability)
+		{
+			this.ability = ability;
+		}
+		
+		public Ability Ability
+		{
+			get { return ability; }
+		}
+		
+		public bool IsRequired
+		{
+			get { return isRequired; }
+			set { isRequired = value; }
+		}
+		
+		public override int GetHashCode ()
+		{
+			return typeof(UnitAbility).GetHashCode() + ability.GetHashCode();
+		}
+		
+		public override bool Equals (object obj)
+		{
+			bool equal = true;
+			
+			if (obj == this)
+			{
+				equal = true;
+			}
+			else if (obj != null && GetType().Equals(obj.GetType()))
+			{
+				UnitAbility other = (UnitAbility)obj;
+				
+				if (!Ability.Equals(other.Ability))
+				{
+					equal = false;
+				}
+				else if (IsRequired != other.IsRequired)
+				{
+					equal = false;
+				}
+			}
+			else
+			{
+				equal = false;
+			}
+			
+			return equal;
+		}
+
+
+	}
+}
--- a/api/Objects/UnitType.cs	Sun Apr 05 19:21:51 2009 +0000
+++ b/api/Objects/UnitType.cs	Sun Apr 05 19:57:32 2009 +0000
@@ -2,8 +2,7 @@
 //
 // 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;
 using System.Collections.Generic;
 using System.Xml;
 using IBBoard.Logging;
@@ -16,17 +15,18 @@
 	/// </summary>
 	public class UnitType : WarFoundryObject
 	{
-		protected Category mainCat;
-		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();
+		private Category mainCat;
+		private Race race;
+		private int min, max, baseSize = 0;
+		private int minSize, maxSize;
+		private double baseUnitCost;
+		private double costPerTrooper;
+		private Stats stats;
+		private List<UnitRequirement> requirements = new List<UnitRequirement>();
+		private Dictionary<string, UnitEquipmentItem> equipment = new Dictionary<string, UnitEquipmentItem>();
+		private Dictionary<string, List<UnitEquipmentItem>> equipmentExclusionGroups = new Dictionary<string, List<UnitEquipmentItem>>();
+		private List<string> equipmentKeyOrder = new List<string>();
+		private Dictionary<string, UnitAbility> abilities = new Dictionary<string, UnitAbility>();
 
 
 		public UnitType(string id, string typeName, Race parentRace) : base(id, typeName)
@@ -35,9 +35,8 @@
 		}
 
 		[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)
+		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) : this (id, typeName, parentRace)
 		{
-			race = parentRace;
 			mainCat = race.GetCategory(mainCategoryID);			
 			MinNumber = minNum;			
 			MaxNumber = maxNum;
@@ -200,6 +199,32 @@
 					stats = value;
 				}
 			}
+		}
+		
+		public void AddEquipmentItem(UnitEquipmentItem item)
+		{
+			equipment.Add(item.ID, item);
+			equipmentKeyOrder.Add(item.ID);
+			AddToMutexGroup(item);
+		}
+		
+		private void AddToMutexGroup(UnitEquipmentItem item)
+		{
+			string mutexGroup = item.MutexGroup;
+			
+			if (mutexGroup!="" && mutexGroup!=null)
+			{
+				List<UnitEquipmentItem> items = null;
+				equipmentExclusionGroups.TryGetValue(mutexGroup, out items);
+				
+				if (items == null)
+				{
+					items = new List<UnitEquipmentItem>();
+					equipmentExclusionGroups.Add(mutexGroup, items);
+				}
+				
+				items.Add(item);
+			}
 		}
 
 		/// <summary>
@@ -212,8 +237,10 @@
 		/// The <see cref="UnitEquipmentItem"/> for the given ID string, or <code>null</code> if nothing exists for that ID
 		/// </returns>
 		public UnitEquipmentItem GetEquipmentItem(string id)
-		{
-			return (UnitEquipmentItem)equipment[id];
+		{
+			UnitEquipmentItem equipItem = null;
+			equipment.TryGetValue(id, out equipItem);
+			return equipItem;
 		}
 
 		/// <summary>
@@ -229,10 +256,61 @@
 
 			foreach (string itemID in equipmentKeyOrder)
 			{
-				items[i++] = (UnitEquipmentItem)equipment[itemID];
+				items[i++] = equipment[itemID];
 			}
 
 			return items;
+		}
+		
+		public ICollection<Ability> GetAbilities()
+		{
+			ICollection<Ability> abilitiesList = new List<Ability>();
+			
+			foreach (UnitAbility ability in abilities.Values)
+			{
+				abilitiesList.Add(ability.Ability);
+			}
+			
+			return abilitiesList;
+		}
+		
+		public ICollection<Ability> GetRequiredAbilities()
+		{
+			ICollection<Ability> requiredAbilities = new List<Ability>();
+			
+			foreach (UnitAbility ability in abilities.Values)
+			{
+				if (ability.IsRequired)
+				{
+					requiredAbilities.Add(ability.Ability);
+				}
+			}
+			
+			return requiredAbilities;
+		}
+		
+		public ICollection<Ability> GetOptionalAbilities()
+		{
+			ICollection<Ability> optionalAbilities = new List<Ability>();
+			
+			foreach (UnitAbility ability in abilities.Values)
+			{
+				if (!ability.IsRequired)
+				{
+					optionalAbilities.Add(ability.Ability);
+				}
+			}
+			
+			return optionalAbilities;
+		}
+		
+		public void AddAbility(Ability ability)
+		{			
+			if (!abilities.ContainsKey(ability.ID))
+			{
+				UnitAbility unitAbility = new UnitAbility(ability);
+				abilities[ability.ID] = unitAbility;
+			}
 		}
 
 		public void AddRequirement(UnitRequirement requirement)
@@ -282,7 +360,8 @@
 
 		public UnitEquipmentItem[] GetEquipmentItemsByExclusionGroup(string group)
 		{
-			ArrayList list = (ArrayList)equipmentExclusionGroups[group];
+			List<UnitEquipmentItem> list = null;
+			equipmentExclusionGroups.TryGetValue(group, out list);
 
 			if (list == null)
 			{
@@ -290,7 +369,7 @@
 			}
 			else
 			{
-				return (UnitEquipmentItem[])list.ToArray(typeof(UnitEquipmentItem));
+				return list.ToArray();
 			}
 		}
 	}