comparison api/Util/UnitEquipmentUtil.cs @ 201:4d7ff70bb109

Re #208: equipmentslot limit issues * Fix numeric slot issues by using "amount taken excluding this item" method * Rename "without this item" to "excluding this item" to clarify purpose * Restructure percentage limits and break some tests (some of which are only broken by rounding errors)
author IBBoard <dev@ibboard.co.uk>
date Thu, 05 Nov 2009 21:09:03 +0000
parents 4bbf5624ced6
children 37ad50823531
comparison
equal deleted inserted replaced
200:3e287b6b5244 201:4d7ff70bb109
112 int newLimit = currLimit; 112 int newLimit = currLimit;
113 AbstractLimit limit = GetSlotLimitForItem(unit, equip); 113 AbstractLimit limit = GetSlotLimitForItem(unit, equip);
114 114
115 if (!(limit is UnlimitedLimit)) 115 if (!(limit is UnlimitedLimit))
116 { 116 {
117 int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlot (equip.SlotName); 117 int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip);
118 newLimit = Math.Min (slotMax, newLimit); 118 newLimit = Math.Min (slotMax, newLimit);
119 } 119 }
120 120
121 return newLimit; 121 return newLimit;
122 } 122 }
142 142
143 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip) 143 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
144 { 144 {
145 double limit = 0; 145 double limit = 0;
146 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip); 146 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
147 147
148 148 if (slotLimit is IPercentageLimit)
149 if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || (!(slotLimit is UnlimitedLimit) && unit.GetEquipmentAmountInSlotWithoutItem(equip) != 0)) 149 {
150 { 150 limit = ((IPercentageLimit)slotLimit).Percentage;
151 limit = (GetMaxEquipmentCount(unit, equip) / (double)unit.Size) * 100.0;
152 } 151 }
153 else 152 else
154 { 153 {
155 if (equip.IsRatioLimit) 154 limit = GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit);
156 { 155 }
157 limit = ((IPercentageLimit)equip.MaxLimit).Percentage; 156
158 } 157 limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
159 else 158
160 { 159 return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MaxLimit, unit);
161 int unitSize = unit.Size; 160 }
162 limit = (equip.MaxLimit.GetLimit(unitSize) / (double)unitSize) * 100.0; 161
163 } 162 private static double GetPercentageOfUnitSize(int number, Unit unit)
164 163 {
165 if (slotLimit is IPercentageLimit) 164 return (number / (double)unit.Size) * 100;
166 { 165 }
167 limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit); 166
168 } 167 private static double GetMinOfSlotLimitAndEquipmentLimit(bool equipIsRatio, double limit, AbstractLimit equipLimit, Unit unit)
169 } 168 {
169 if (equipIsRatio)
170 {
171 limit = Math.Min(limit, ((IPercentageLimit)equipLimit).Percentage);
172 }
173 else
174 {
175 limit = Math.Min(limit, GetPercentageOfUnitSize(equipLimit.GetLimit(unit.Size), unit));
176 }
170 177
171 return limit; 178 return limit;
172 } 179 }
173 180
174 public static double GetMinEquipmentPercentage(Unit unit, UnitEquipmentItem equip) 181 public static double GetMinEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
175 { 182 {
176 double limit = 0; 183 double limit = 0;
177 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip); 184 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
178 185
179 186 if (slotLimit is IPercentageLimit)
180 if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || (!(slotLimit is UnlimitedLimit) && unit.GetEquipmentAmountInSlotWithoutItem(equip) != 0)) 187 {
181 { 188 limit = ((IPercentageLimit)slotLimit).Percentage;
182 limit = (GetMinEquipmentCount(unit, equip) / (double)unit.Size) * 100.0;
183 } 189 }
184 else 190 else
185 { 191 {
186 if (equip.IsRatioLimit) 192 limit = GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit);
187 { 193 }
188 limit = ((IPercentageLimit)equip.MinLimit).Percentage; 194
189 } 195 limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
190 else 196
191 { 197 return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MinLimit, unit);
192 int unitSize = unit.Size;
193 limit = (equip.MinLimit.GetLimit(unitSize) / (double)unitSize) * 100.0;
194 }
195
196 if (slotLimit is IPercentageLimit)
197 {
198 limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit);
199 }
200 }
201
202 return limit;
203 } 198 }
204 } 199 }
205 } 200 }