annotate API/Util/UnitEquipmentUtil.cs @ 472:e6c93ceba119

Re #359: Add "only contained" attribute to unit types * Make sure we add and remove from the parent unit appropriately * Make sure that setting the unit's parent to null removes it from the parent
author IBBoard <dev@ibboard.co.uk>
date Mon, 16 Apr 2012 20:14:23 +0100
parents c8002429ab45
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
431
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
1 // This file (UnitEquipmentUtil.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
2 //
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
3 // 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.
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
4
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
5 using System;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
6 using System.Collections.Generic;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
7 using System.Text;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
8 using IBBoard.CustomMath;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
9 using IBBoard.Limits;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
10 using IBBoard.WarFoundry.API.Objects;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
11
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
12 namespace IBBoard.WarFoundry.API.Util
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
13 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
14 public class UnitEquipmentUtil
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
15 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
16 /// <summary>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
17 /// Gets an array of allowed <see cref="UnitEquipmentItem"/>s based on the current selections of the unit, taking in to account Mutex groups and other limits.
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
18 /// </summary>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
19 /// <param name="unit">The <see cref="Unit"/> to get equipment items for</param>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
20 /// <returns>The array of allowed <see cref="UnitEquipmentItem"/>s</returns>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
21 public static UnitEquipmentItem[] GetAllowedEquipmentItems(Unit unit)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
22 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
23 List<UnitEquipmentItem> list = new List<UnitEquipmentItem>();
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
24 UnitEquipmentItem[] currItems = unit.GetEquipment();
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
25
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
26 foreach (UnitEquipmentItem item in GetAllEquipmentItems(unit))
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
27 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
28 bool allowed = IsAllowedByMutex(item, currItems);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
29
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
30 if (allowed)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
31 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
32 list.Add(item);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
33 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
34 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
35
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
36 return list.ToArray();
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
37 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
38
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
39 private static bool IsAllowedByMutex(UnitEquipmentItem item, UnitEquipmentItem[] currItems)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
40 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
41 bool allowed = true;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
42
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
43 foreach (UnitEquipmentItem currItem in currItems)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
44 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
45 if (ItemsAreMutuallyExclusive(currItem, item))
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
46 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
47 allowed = false;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
48 break;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
49 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
50 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
51
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
52 return allowed;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
53 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
54
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
55 /// <summary>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
56 /// Gets a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code> because of mutex groups.
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
57 /// </summary>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
58 /// <param name="unit">The unit that wants to take the equipment item</param>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
59 /// <param name="item">The item to check blocking items for</param>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
60 /// <returns>a list of all <see cref="UnitEquipmentItem"/>s that would stop the unit taking <code>item</code></returns>
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
61 public static List<UnitEquipmentItem> GetBlockingEquipmentItems(Unit unit, UnitEquipmentItem item)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
62 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
63 List<UnitEquipmentItem> items = new List<UnitEquipmentItem>();
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
64 UnitEquipmentItem[] currItems = unit.GetEquipment();
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
65
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
66 foreach (UnitEquipmentItem unitItem in currItems)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
67 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
68 if (ItemsAreMutuallyExclusive(unitItem, item))
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
69 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
70 items.Add(unitItem);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
71 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
72 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
73
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
74 return items;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
75 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
76
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
77 public static UnitEquipmentItem[] GetAllEquipmentItems(Unit unit)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
78 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
79 return unit.UnitType.GetEquipmentItems();
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
80 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
81
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
82 public static bool ItemsAreMutuallyExclusive(UnitEquipmentItem item1, UnitEquipmentItem item2)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
83 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
84 bool areMutex = false;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
85 string[] item1mutex = item1.MutexGroups;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
86 string[] item2mutex = item2.MutexGroups;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
87
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
88 foreach (string mutex in item1mutex)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
89 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
90 foreach (string otherMutex in item2mutex)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
91 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
92 if (mutex.Equals(otherMutex))
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
93 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
94 areMutex = true;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
95 goto postLoop;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
96 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
97 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
98 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
99 postLoop:
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
100
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
101 return areMutex;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
102 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
103
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
104 public static int GetMaxEquipmentCount (Unit unit, UnitEquipmentItem equip)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
105 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
106 return GetEquipmentCountLimit (unit, equip.MaxLimit.GetLimit(unit.Size), equip);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
107 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
108
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
109 private static int GetEquipmentCountLimit (Unit unit, int currLimit, UnitEquipmentItem equip)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
110 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
111 int newLimit = currLimit;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
112 ILimit limit = GetSlotLimitForItem(unit, equip);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
113
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
114 if (!(limit is UnlimitedLimit))
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
115 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
116 int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
117 newLimit = Math.Min (slotMax, newLimit);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
118 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
119
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
120 return newLimit;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
121 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
122
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
123 private static ILimit GetSlotLimitForItem(Unit unit, UnitEquipmentItem equip)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
124 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
125 return unit.UnitType.GetEquipmentSlotLimit(equip.SlotName);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
126 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
127
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
128
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
129 public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
130 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
131 return GetEquipmentCountLimit (unit, equip.MinLimit.GetLimit(unit.Size), equip);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
132 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
133
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
134 public static bool IsEquipmentRatioLimited(Unit unit, UnitEquipmentItem equip)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
135 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
136 ILimit limit = GetSlotLimitForItem(unit, equip);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
137 return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
138 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
139
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
140 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
141 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
142 return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MaxLimit, unit);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
143 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
144
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
145 private static double GetPercentageOfUnitSize(int number, Unit unit)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
146 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
147 return IBBMath.Percentage(number, unit.Size);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
148 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
149
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
150 private static double GetMinOfSlotLimitAndEquipmentLimit(UnitEquipmentItem equip, ILimit equipLimit, Unit unit)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
151 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
152 double limit = 0;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
153 ILimit slotLimit = GetSlotLimitForItem(unit, equip);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
154
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
155 if (slotLimit is IPercentageLimit)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
156 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
157 limit = ((IPercentageLimit)slotLimit).Percentage - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
158 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
159 else
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
160 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
161 int remaining = slotLimit.GetLimit(unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
162 limit = GetPercentageOfUnitSize(remaining, unit);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
163 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
164
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
165 if (equip.IsRatioLimit)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
166 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
167 limit = Math.Min(limit, ((IPercentageLimit)equipLimit).Percentage);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
168 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
169 else
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
170 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
171 limit = Math.Min(limit, GetPercentageOfUnitSize(equipLimit.GetLimit(unit.Size), unit));
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
172 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
173
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
174 return limit;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
175 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
176
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
177 public static double GetMinEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
178 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
179 return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MinLimit, unit);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
180 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
181
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
182 public static double GetEquipmentAmount(Unit unit, UnitEquipmentItem item)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
183 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
184 double amount = 0;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
185 AbstractUnitEquipmentItemSelection selection = unit.GetEquipmentSelection(item);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
186
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
187 if (selection != null)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
188 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
189 amount = selection.AmountTaken;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
190 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
191
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
192 return amount;
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
193 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
194
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
195 public static bool GetEquipmentAmountIsRatio(Unit unit, UnitEquipmentItem item)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
196 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
197 return (unit.GetEquipmentSelection(item) is UnitEquipmentRatioSelection);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
198 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
199
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
200 public static int GetEquipmentAmountTaken(Unit unit, UnitEquipmentItem item)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
201 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
202 AbstractUnitEquipmentItemSelection selection = unit.GetEquipmentSelection(item);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
203 return (selection == null ? 0 : selection.NumberTaken);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
204 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
205
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
206 public static bool CanEditEquipmentAmount(Unit unit, UnitEquipmentItem item)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
207 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
208 return item !=null && (item.MaxLimit.GetLimit(unit.Size) != item.MinLimit.GetLimit(unit.Size));
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
209 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
210
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
211 public static int CalculateEquipmentAmountTakenFromRatio(Unit unit, UnitEquipmentItem equipItem, double newAmount)
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
212 {
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
213 return (int)IBBMath.Round(unit.Size * (newAmount / 100), equipItem.RoundNumberUp);
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
214 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
215 }
c8002429ab45 Re #366: Rewrite equipment handling and limits
IBBoard <dev@ibboard.co.uk>
parents: 337
diff changeset
216 }