# HG changeset patch # User IBBoard # Date 1257455343 0 # Node ID 4d7ff70bb109c44e56ecb53f8d0517f040860885 # Parent 3e287b6b5244fcdf4021762864d286e5b29e8c1a 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) diff -r 3e287b6b5244 -r 4d7ff70bb109 api/Objects/Unit.cs --- a/api/Objects/Unit.cs Thu Nov 05 20:22:00 2009 +0000 +++ b/api/Objects/Unit.cs Thu Nov 05 21:09:03 2009 +0000 @@ -525,7 +525,7 @@ /// /// the item to exclude from the count /// the total number of items - public int GetEquipmentAmountInSlotWithoutItem(UnitEquipmentItem item) + public int GetEquipmentAmountInSlotExcludingItem(UnitEquipmentItem item) { int amount = 0; diff -r 3e287b6b5244 -r 4d7ff70bb109 api/Util/UnitEquipmentUtil.cs --- a/api/Util/UnitEquipmentUtil.cs Thu Nov 05 20:22:00 2009 +0000 +++ b/api/Util/UnitEquipmentUtil.cs Thu Nov 05 21:09:03 2009 +0000 @@ -114,7 +114,7 @@ if (!(limit is UnlimitedLimit)) { - int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlot (equip.SlotName); + int slotMax = limit.GetLimit (unit.Size) - unit.GetEquipmentAmountInSlotExcludingItem(equip); newLimit = Math.Min (slotMax, newLimit); } @@ -144,29 +144,36 @@ { double limit = 0; AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip); - - - if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || (!(slotLimit is UnlimitedLimit) && unit.GetEquipmentAmountInSlotWithoutItem(equip) != 0)) + + if (slotLimit is IPercentageLimit) { - limit = (GetMaxEquipmentCount(unit, equip) / (double)unit.Size) * 100.0; + limit = ((IPercentageLimit)slotLimit).Percentage; } else { - if (equip.IsRatioLimit) - { - limit = ((IPercentageLimit)equip.MaxLimit).Percentage; - } - else - { - int unitSize = unit.Size; - limit = (equip.MaxLimit.GetLimit(unitSize) / (double)unitSize) * 100.0; - } - - if (slotLimit is IPercentageLimit) - { - limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit); - } - } + limit = GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit); + } + + limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit); + + return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MaxLimit, unit); + } + + private static double GetPercentageOfUnitSize(int number, Unit unit) + { + return (number / (double)unit.Size) * 100; + } + + private static double GetMinOfSlotLimitAndEquipmentLimit(bool equipIsRatio, double limit, AbstractLimit equipLimit, Unit unit) + { + if (equipIsRatio) + { + limit = Math.Min(limit, ((IPercentageLimit)equipLimit).Percentage); + } + else + { + limit = Math.Min(limit, GetPercentageOfUnitSize(equipLimit.GetLimit(unit.Size), unit)); + } return limit; } @@ -176,30 +183,18 @@ double limit = 0; AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip); - - if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || (!(slotLimit is UnlimitedLimit) && unit.GetEquipmentAmountInSlotWithoutItem(equip) != 0)) + if (slotLimit is IPercentageLimit) { - limit = (GetMinEquipmentCount(unit, equip) / (double)unit.Size) * 100.0; + limit = ((IPercentageLimit)slotLimit).Percentage; } else { - if (equip.IsRatioLimit) - { - limit = ((IPercentageLimit)equip.MinLimit).Percentage; - } - else - { - int unitSize = unit.Size; - limit = (equip.MinLimit.GetLimit(unitSize) / (double)unitSize) * 100.0; - } - - if (slotLimit is IPercentageLimit) - { - limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit); - } - } + limit = GetPercentageOfUnitSize(slotLimit.GetLimit(unit.Size), unit); + } - return limit; + limit = limit - GetPercentageOfUnitSize(unit.GetEquipmentAmountInSlotExcludingItem(equip), unit); + + return GetMinOfSlotLimitAndEquipmentLimit(equip.IsRatioLimit, limit, equip.MinLimit, unit); } } }