comparison api/Util/UnitEquipmentUtil.cs @ 202:37ad50823531

Re #208: equipmentslot limit issues * Move more code to a common method * Use new IBBMath.Percentage method to ensure consistent calculation of percentages * Move subtraction of amount already taken in slot inside IF and ELSE and tailor to situation Some tests still failing
author IBBoard <dev@ibboard.co.uk>
date Fri, 06 Nov 2009 20:11:30 +0000
parents 4d7ff70bb109
children df4e56e2ee71
comparison
equal deleted inserted replaced
201:4d7ff70bb109 202:37ad50823531
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. 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.
4 4
5 using System; 5 using System;
6 using System.Collections.Generic; 6 using System.Collections.Generic;
7 using System.Text; 7 using System.Text;
8 using IBBoard.CustomMath;
8 using IBBoard.Limits; 9 using IBBoard.Limits;
9 using IBBoard.WarFoundry.API.Objects; 10 using IBBoard.WarFoundry.API.Objects;
10 11
11 namespace IBBoard.WarFoundry.API.Util 12 namespace IBBoard.WarFoundry.API.Util
12 { 13 {
140 return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit); 141 return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit);
141 } 142 }
142 143
143 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip) 144 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
144 { 145 {
146 return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MaxLimit, unit);
147 }
148
149 private static double GetPercentageOfUnitSize(int number, Unit unit)
150 {
151 return IBBMath.Percentage(number, unit.Size);
152 }
153
154 private static double GetMinOfSlotLimitAndEquipmentLimit(UnitEquipmentItem equip, AbstractLimit equipLimit, Unit unit)
155 {
145 double limit = 0; 156 double limit = 0;
146 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip); 157 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
147 158
148 if (slotLimit is IPercentageLimit) 159 if (slotLimit is IPercentageLimit)
149 { 160 {
150 limit = ((IPercentageLimit)slotLimit).Percentage; 161 limit = ((IPercentageLimit)slotLimit).Percentage;
162 limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
151 } 163 }
152 else 164 else
153 { 165 {
154 limit = GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit); 166 int remaining = slotLimit.GetLimit(unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip);
167 limit = GetPercentageOfUnitSize(remaining, unit);
155 } 168 }
156 169
157 limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit); 170 if (equip.IsRatioLimit)
158
159 return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MaxLimit, unit);
160 }
161
162 private static double GetPercentageOfUnitSize(int number, Unit unit)
163 {
164 return (number / (double)unit.Size) * 100;
165 }
166
167 private static double GetMinOfSlotLimitAndEquipmentLimit(bool equipIsRatio, double limit, AbstractLimit equipLimit, Unit unit)
168 {
169 if (equipIsRatio)
170 { 171 {
171 limit = Math.Min(limit, ((IPercentageLimit)equipLimit).Percentage); 172 limit = Math.Min(limit, ((IPercentageLimit)equipLimit).Percentage);
172 } 173 }
173 else 174 else
174 { 175 {
177 178
178 return limit; 179 return limit;
179 } 180 }
180 181
181 public static double GetMinEquipmentPercentage(Unit unit, UnitEquipmentItem equip) 182 public static double GetMinEquipmentPercentage(Unit unit, UnitEquipmentItem equip)
182 { 183 {
183 double limit = 0; 184 return GetMinOfSlotLimitAndEquipmentLimit(equip, equip.MinLimit, unit);
184 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip);
185
186 if (slotLimit is IPercentageLimit)
187 {
188 limit = ((IPercentageLimit)slotLimit).Percentage;
189 }
190 else
191 {
192 limit = GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit);
193 }
194
195 limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit);
196
197 return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MinLimit, unit);
198 } 185 }
199 } 186 }
200 } 187 }