Mercurial > repos > IBBoard.WarFoundry.API
comparison api/Util/UnitEquipmentUtil.cs @ 189:0b413f41e6e3
Re #198: Add equipment slots
* Refactor out common function
* Add handling of slot limits and existing equipment to GetMaxEquipmentPercentage
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 28 Oct 2009 20:42:13 +0000 |
parents | 354d51cc002b |
children | 6f67f16a67fb |
comparison
equal
deleted
inserted
replaced
188:354d51cc002b | 189:0b413f41e6e3 |
---|---|
108 } | 108 } |
109 | 109 |
110 private static int GetEquipmentCountLimit (Unit unit, int currLimit, UnitEquipmentItem equip) | 110 private static int GetEquipmentCountLimit (Unit unit, int currLimit, UnitEquipmentItem equip) |
111 { | 111 { |
112 int newLimit = currLimit; | 112 int newLimit = currLimit; |
113 AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit (equip.SlotName); | 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.GetEquipmentAmountInSlot (equip.SlotName); |
118 newLimit = Math.Min (slotMax, newLimit); | 118 newLimit = Math.Min (slotMax, newLimit); |
119 } | 119 } |
120 | 120 |
121 return newLimit; | 121 return newLimit; |
122 } | 122 } |
123 | |
124 private static AbstractLimit GetSlotLimitForItem(Unit unit, UnitEquipmentItem equip) | |
125 { | |
126 return unit.UnitType.GetEquipmentSlotLimit(equip.SlotName); | |
127 } | |
128 | |
123 | 129 |
124 public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip) | 130 public static int GetMinEquipmentCount (Unit unit, UnitEquipmentItem equip) |
125 { | 131 { |
126 int unitSize = unit.Size; | 132 int unitSize = unit.Size; |
127 int min = Math.Min (equip.MinLimit.GetLimit (unitSize), equip.MaxLimit.GetLimit (unitSize)); | 133 int min = Math.Min (equip.MinLimit.GetLimit (unitSize), equip.MaxLimit.GetLimit (unitSize)); |
128 return GetEquipmentCountLimit (unit, min, equip); | 134 return GetEquipmentCountLimit (unit, min, equip); |
129 } | 135 } |
130 | 136 |
131 public static bool IsEquipmentRatioLimited(Unit unit, UnitEquipmentItem equip) | 137 public static bool IsEquipmentRatioLimited(Unit unit, UnitEquipmentItem equip) |
132 { | 138 { |
133 AbstractLimit limit = unit.UnitType.GetEquipmentSlotLimit(equip.SlotName); | 139 AbstractLimit limit = GetSlotLimitForItem(unit, equip); |
134 return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit); | 140 return equip.IsRatioLimit && (limit is IPercentageLimit || limit is UnlimitedLimit); |
135 } | 141 } |
136 | 142 |
137 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip) | 143 public static double GetMaxEquipmentPercentage(Unit unit, UnitEquipmentItem equip) |
138 { | 144 { |
139 double limit = 0; | 145 double limit = 0; |
146 AbstractLimit slotLimit = GetSlotLimitForItem(unit, equip); | |
140 | 147 |
141 if (equip.IsRatioLimit) | 148 |
149 if (!(slotLimit is IPercentageLimit || slotLimit is UnlimitedLimit) || unit.GetEquipmentAmountInSlot (equip.SlotName) != 0) | |
142 { | 150 { |
143 limit = Math.Max(((IPercentageLimit)equip.MinLimit).Percentage, ((IPercentageLimit)equip.MaxLimit).Percentage); | 151 limit = (GetMaxEquipmentCount(unit, equip) / (double)unit.Size) * 100.0; |
144 } | 152 } |
145 else | 153 else |
146 { | 154 { |
147 int unitSize = unit.Size; | 155 if (equip.IsRatioLimit) |
148 limit = (equip.MaxLimit.GetLimit(unitSize) / (double)unitSize) * 100.0; | 156 { |
149 } | 157 limit = Math.Max(((IPercentageLimit)equip.MinLimit).Percentage, ((IPercentageLimit)equip.MaxLimit).Percentage); |
158 } | |
159 else | |
160 { | |
161 int unitSize = unit.Size; | |
162 limit = (equip.MaxLimit.GetLimit(unitSize) / (double)unitSize) * 100.0; | |
163 } | |
164 | |
165 if (slotLimit is IPercentageLimit) | |
166 { | |
167 limit = Math.Min(((IPercentageLimit)slotLimit).Percentage, limit); | |
168 } | |
169 } | |
150 | 170 |
151 return limit; | 171 return limit; |
152 } | 172 } |
153 } | 173 } |
154 } | 174 } |