diff api/Objects/Unit.cs @ 152:0c0e14f03785

Re #180: Add multiple mutex groups * Add multiple mutex groups to UnitEquipmentItem * Add new attribute to Race schema * Make everywhere handle multiple groups instead of assuming one * Make factory load new attribute then fall back to old (deprecated) attribute * Add method to Unit to get the blocking items (useful for future UI work to say "replace X and Y with Z") * Add IsMutuallyExclusive method to UnitEquipmentItem to determine whether mutex groups overlap
author IBBoard <dev@ibboard.co.uk>
date Sat, 26 Sep 2009 19:51:11 +0000
parents 1d13820b3d96
children 4a02c07278e7
line wrap: on
line diff
--- a/api/Objects/Unit.cs	Sat Sep 26 18:48:36 2009 +0000
+++ b/api/Objects/Unit.cs	Sat Sep 26 19:51:11 2009 +0000
@@ -433,22 +433,39 @@
 		
 		public bool CanEquipWithItem(UnitEquipmentItem item)
 		{
-			string mutex = item.MutexGroup;
-
-			if (mutex == "")
-			{
-				return true;
+			string[] mutexes = item.MutexGroups;
+			bool canEquip = false;
+
+			if (mutexes.Length == 0)
+			{
+				canEquip = true;
+			}
+			else
+			{
+				canEquip = GetBlockingEquipmentItems(item).Count == 0;
 			}
 
-			foreach (UnitEquipmentItem unitItem in GetEquipment())
-			{
-				if (unitItem.MutexGroup == mutex)
-				{
-					return false;
-				}
-			}
-
-			return true;
+			return canEquip;
+		}
+
+		/// <summary>
+		/// Gets a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code> because of mutex groups.
+		/// </summary>
+		/// <param name="item">The item to check blocking items for</param>
+		/// <returns>a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code></returns>
+		public List<UnitEquipmentItem> GetBlockingEquipmentItems(UnitEquipmentItem item)
+		{
+			List<UnitEquipmentItem> items = new List<UnitEquipmentItem>();
+
+			foreach (UnitEquipmentItem unitItem in GetEquipment())
+			{
+				if (unitItem.IsMutuallyExclusive(item))
+				{
+					items.Add(unitItem);
+				}
+			}
+
+			return items;
 		}
 
 		public bool CanEquipWithItem(string equipID)