# HG changeset patch # User IBBoard # Date 1254657616 0 # Node ID 81abc04b3dbe950079b47198d7234c7b610d13d7 # Parent 41b927998a413f15c663615b5f1765f120ae6cf1 Re #192: Improve "clean coding" practice * Move some calculation methods for equipment out of the encapsulation objects diff -r 41b927998a41 -r 81abc04b3dbe IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Sun Oct 04 10:49:17 2009 +0000 +++ b/IBBoard.WarFoundry.API.csproj Sun Oct 04 12:00:16 2009 +0000 @@ -53,6 +53,8 @@ PreserveNewest + + @@ -117,6 +119,7 @@ + @@ -152,6 +155,7 @@ + diff -r 41b927998a41 -r 81abc04b3dbe api/Objects/Unit.cs --- a/api/Objects/Unit.cs Sun Oct 04 10:49:17 2009 +0000 +++ b/api/Objects/Unit.cs Sun Oct 04 12:00:16 2009 +0000 @@ -6,7 +6,8 @@ using System.Collections.Generic; using System.Text; using System.Xml; -using IBBoard.Lang; +using IBBoard.Lang; +using IBBoard.WarFoundry.API.Util; namespace IBBoard.WarFoundry.API.Objects { @@ -220,39 +221,10 @@ } } + [Obsolete("Use UnitEquipmentUtil.GetAllowedEquipmentItems(Unit) instead")] public UnitEquipmentItem[] GetAllowedAdditionalEquipment() - { - List list = new List(); - List existingMutexGroups = new List(); - - foreach (UnitEquipmentItem item in GetEquipment()) - { - foreach (string mutex in item.MutexGroups) - { - existingMutexGroups.Add(mutex); - } - } - - foreach (UnitEquipmentItem item in UnitType.GetEquipmentItems()) - { - bool mutexMatch = false; - - foreach (string mutex in item.MutexGroups) - { - if (existingMutexGroups.Contains(mutex)) - { - mutexMatch = true; - break; - } - } - - if (!mutexMatch) - { - list.Add(item); - } - } - - return list.ToArray(); + { + return UnitEquipmentUtil.GetAllowedEquipmentItems(this); } public UnitEquipmentItem[] GetEquipment() @@ -468,24 +440,10 @@ return canEquip; } - /// - /// Gets a list of all s that would stop the unit taking item because of mutex groups. - /// - /// The item to check blocking items for - /// a list of all s that would stop the unit taking item + [Obsolete("Use UnitEquipmentUtil.GetBlockingEquipmentItems(Unit, UnitEquipmentItem) instead")] public List GetBlockingEquipmentItems(UnitEquipmentItem item) - { - List items = new List(); - - foreach (UnitEquipmentItem unitItem in GetEquipment()) - { - if (unitItem.IsMutuallyExclusive(item)) - { - items.Add(unitItem); - } - } - - return items; + { + return UnitEquipmentUtil.GetBlockingEquipmentItems(this, item); } public bool CanEquipWithItem(string equipID) diff -r 41b927998a41 -r 81abc04b3dbe api/Objects/UnitEquipmentItem.cs --- a/api/Objects/UnitEquipmentItem.cs Sun Oct 04 10:49:17 2009 +0000 +++ b/api/Objects/UnitEquipmentItem.cs Sun Oct 04 12:00:16 2009 +0000 @@ -3,7 +3,8 @@ // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. using System; -using IBBoard.Lang; +using IBBoard.Lang; +using IBBoard.WarFoundry.API.Util; namespace IBBoard.WarFoundry.API.Objects { @@ -229,29 +230,10 @@ return EquipmentItem.CanBeUsedWithArmourType(otherItemType); } - /// - /// Checks the "mutex" (mutual exclusion) groups of the other item against its own and determines whether the two items are mutually exclusive (share at least one mutex group) - /// - /// the item to check against - /// true if the two items share at least one mutex group, else false + [Obsolete("Use UnitEquipmentUtil method instead")] public bool IsMutuallyExclusive(UnitEquipmentItem item) - { - bool areMutex = false; - - foreach (string mutex in MutexGroups) - { - foreach (string otherMutex in item.MutexGroups) - { - if (mutex.Equals(otherMutex)) - { - areMutex = true; - goto postLoop; - } - } - } - postLoop: - - return areMutex; + { + return UnitEquipmentUtil.ItemsAreMutuallyExclusive(this, item); } } } diff -r 41b927998a41 -r 81abc04b3dbe api/Util/UnitEquipmentUtil.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Util/UnitEquipmentUtil.cs Sun Oct 04 12:00:16 2009 +0000 @@ -0,0 +1,102 @@ +// This file (UnitEquipmentUtil.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 and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; +using System.Collections.Generic; +using System.Text; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Util +{ + class UnitEquipmentUtil + { + /// + /// Gets an array of allowed s based on the current selections of the unit, taking in to account Mutex groups and other limits. + /// + /// The to get equipment items for + /// The array of allowed s + public static UnitEquipmentItem[] GetAllowedEquipmentItems(Unit unit) + { + List list = new List(); + UnitEquipmentItem[] currItems = unit.GetEquipment(); + + foreach (UnitEquipmentItem item in GetAllEquipmentItems(unit)) + { + bool allowed = IsAllowedByMutex(item, currItems); + + if (allowed) + { + list.Add(item); + } + } + + return list.ToArray(); + } + + private static bool IsAllowedByMutex(UnitEquipmentItem item, UnitEquipmentItem[] currItems) + { + bool allowed = true; + + foreach (UnitEquipmentItem currItem in currItems) + { + if (ItemsAreMutuallyExclusive(currItem, item)) + { + allowed = false; + break; + } + } + + return allowed; + } + + /// + /// Gets a list of all s that would stop the unit taking item because of mutex groups. + /// + /// The unit that wants to take the equipment item + /// The item to check blocking items for + /// a list of all s that would stop the unit taking item + public static List GetBlockingEquipmentItems(Unit unit, UnitEquipmentItem item) + { + List items = new List(); + UnitEquipmentItem[] currItems = unit.GetEquipment(); + + foreach (UnitEquipmentItem unitItem in unit.GetEquipment()) + { + if (!ItemsAreMutuallyExclusive(unitItem, item)) + { + items.Add(unitItem); + } + } + + return items; + } + + public static UnitEquipmentItem[] GetAllEquipmentItems(Unit unit) + { + return unit.UnitType.GetEquipmentItems(); + } + + public static bool ItemsAreMutuallyExclusive(UnitEquipmentItem item1, UnitEquipmentItem item2) + { + bool areMutex = false; + string[] item1mutex = item1.MutexGroups; + string[] item2mutex = item2.MutexGroups; + + foreach (string mutex in item1mutex) + { + foreach (string otherMutex in item2mutex) + { + if (mutex.Equals(otherMutex)) + { + areMutex = true; + goto postLoop; + } + } + } + postLoop: + + return areMutex; + } + } +}