# HG changeset patch # User IBBoard # Date 1238961452 0 # Node ID 9561ef46c6fb47284b821236d035dabca0332e1a # Parent 9080366031c08446f8131fcd7445b32ce9c26369 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 diff -r 9080366031c0 -r 9561ef46c6fb IBBoard.WarFoundry.API.csproj --- 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 @@ + diff -r 9080366031c0 -r 9561ef46c6fb api/Objects/UnitAbility.cs --- /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; + } + + + } +} diff -r 9080366031c0 -r 9561ef46c6fb api/Objects/UnitType.cs --- 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 @@ /// 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 requirements = new List(); - 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 requirements = new List(); + private Dictionary equipment = new Dictionary(); + private Dictionary> equipmentExclusionGroups = new Dictionary>(); + private List equipmentKeyOrder = new List(); + private Dictionary abilities = new Dictionary(); 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 items = null; + equipmentExclusionGroups.TryGetValue(mutexGroup, out items); + + if (items == null) + { + items = new List(); + equipmentExclusionGroups.Add(mutexGroup, items); + } + + items.Add(item); + } } /// @@ -212,8 +237,10 @@ /// The for the given ID string, or null if nothing exists for that ID /// public UnitEquipmentItem GetEquipmentItem(string id) - { - return (UnitEquipmentItem)equipment[id]; + { + UnitEquipmentItem equipItem = null; + equipment.TryGetValue(id, out equipItem); + return equipItem; } /// @@ -229,10 +256,61 @@ foreach (string itemID in equipmentKeyOrder) { - items[i++] = (UnitEquipmentItem)equipment[itemID]; + items[i++] = equipment[itemID]; } return items; + } + + public ICollection GetAbilities() + { + ICollection abilitiesList = new List(); + + foreach (UnitAbility ability in abilities.Values) + { + abilitiesList.Add(ability.Ability); + } + + return abilitiesList; + } + + public ICollection GetRequiredAbilities() + { + ICollection requiredAbilities = new List(); + + foreach (UnitAbility ability in abilities.Values) + { + if (ability.IsRequired) + { + requiredAbilities.Add(ability.Ability); + } + } + + return requiredAbilities; + } + + public ICollection GetOptionalAbilities() + { + ICollection optionalAbilities = new List(); + + 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 list = null; + equipmentExclusionGroups.TryGetValue(group, out list); if (list == null) { @@ -290,7 +369,7 @@ } else { - return (UnitEquipmentItem[])list.ToArray(typeof(UnitEquipmentItem)); + return list.ToArray(); } } }