GroupedGeneralDropItem.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.drops;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import com.l2jserver.gameserver.model.actor.L2Character;
  24. import com.l2jserver.gameserver.model.drops.strategy.IAmountMultiplierStrategy;
  25. import com.l2jserver.gameserver.model.drops.strategy.IChanceMultiplierStrategy;
  26. import com.l2jserver.gameserver.model.drops.strategy.IGroupedItemDropCalculationStrategy;
  27. import com.l2jserver.gameserver.model.drops.strategy.IKillerChanceModifierStrategy;
  28. import com.l2jserver.gameserver.model.drops.strategy.IPreciseDeterminationStrategy;
  29. import com.l2jserver.gameserver.model.holders.ItemHolder;
  30. /**
  31. * @author Nos
  32. */
  33. public final class GroupedGeneralDropItem implements IDropItem
  34. {
  35. private final double _chance;
  36. private List<GeneralDropItem> _items;
  37. protected final IGroupedItemDropCalculationStrategy _dropCalculationStrategy;
  38. protected final IKillerChanceModifierStrategy _killerChanceModifierStrategy;
  39. protected final IPreciseDeterminationStrategy _preciseStrategy;
  40. /**
  41. * @param chance the chance of this drop item.
  42. */
  43. public GroupedGeneralDropItem(double chance)
  44. {
  45. this(chance, IGroupedItemDropCalculationStrategy.DEFAULT_STRATEGY, IKillerChanceModifierStrategy.DEFAULT_STRATEGY, IPreciseDeterminationStrategy.DEFAULT);
  46. }
  47. /**
  48. * @param chance the chance of this drop item.
  49. * @param dropStrategy to calculate drops.
  50. * @param killerStrategy
  51. * @param preciseStrategy
  52. */
  53. public GroupedGeneralDropItem(double chance, IGroupedItemDropCalculationStrategy dropStrategy, IKillerChanceModifierStrategy killerStrategy, IPreciseDeterminationStrategy preciseStrategy)
  54. {
  55. _chance = chance;
  56. _dropCalculationStrategy = dropStrategy;
  57. _killerChanceModifierStrategy = killerStrategy;
  58. _preciseStrategy = preciseStrategy;
  59. }
  60. /**
  61. * Gets the chance of this drop item.
  62. * @return the chance
  63. */
  64. public final double getChance()
  65. {
  66. return _chance;
  67. }
  68. /**
  69. * Gets the items.
  70. * @return the items
  71. */
  72. public final List<GeneralDropItem> getItems()
  73. {
  74. return _items;
  75. }
  76. /**
  77. * @return the strategy
  78. */
  79. public final IGroupedItemDropCalculationStrategy getDropCalculationStrategy()
  80. {
  81. return _dropCalculationStrategy;
  82. }
  83. /**
  84. * @return the _killerChanceModifierStrategy
  85. */
  86. public IKillerChanceModifierStrategy getKillerChanceModifierStrategy()
  87. {
  88. return _killerChanceModifierStrategy;
  89. }
  90. /**
  91. * @return the _preciseStrategy
  92. */
  93. public final IPreciseDeterminationStrategy getPreciseStrategy()
  94. {
  95. return _preciseStrategy;
  96. }
  97. /**
  98. * Sets an item list to this drop item.
  99. * @param items the item list
  100. */
  101. public final void setItems(List<GeneralDropItem> items)
  102. {
  103. _items = Collections.unmodifiableList(items);
  104. }
  105. /**
  106. * Returns a list of items in the group with chance multiplied by chance of the group
  107. * @return the list of items with modified chances
  108. */
  109. public final List<GeneralDropItem> extractMe()
  110. {
  111. List<GeneralDropItem> items = new ArrayList<>();
  112. for (final GeneralDropItem item : getItems())
  113. {
  114. // precise and killer strategies of the group
  115. items.add(new GeneralDropItem(item.getItemId(), item.getMin(), item.getMax(), (item.getChance() * getChance()) / 100, item.getAmountStrategy(), item.getChanceStrategy(), getPreciseStrategy(), getKillerChanceModifierStrategy(), item.getDropCalculationStrategy()));
  116. }
  117. return items;
  118. }
  119. /**
  120. * statically normalizes a group, useful when need to convert legacy SQL data
  121. * @return a new group with items, which have a sum of getChance() of 100%
  122. */
  123. public final GroupedGeneralDropItem normalizeMe()
  124. {
  125. double sumchance = 0;
  126. for (GeneralDropItem item : getItems())
  127. {
  128. sumchance += (item.getChance() * getChance()) / 100;
  129. }
  130. final double sumchance1 = sumchance;
  131. GroupedGeneralDropItem group = new GroupedGeneralDropItem(sumchance1, getDropCalculationStrategy(), IKillerChanceModifierStrategy.NO_RULES, getPreciseStrategy());
  132. List<GeneralDropItem> items = new ArrayList<>();
  133. for (final GeneralDropItem item : getItems())
  134. {
  135. // modify only the chance, leave all other rules intact
  136. items.add(new GeneralDropItem(item.getItemId(), item.getMin(), item.getMax(), (item.getChance() * getChance()) / sumchance1, item.getAmountStrategy(), item.getChanceStrategy(), item.getPreciseStrategy(), item.getKillerChanceModifierStrategy(), item.getDropCalculationStrategy()));
  137. }
  138. group.setItems(items);
  139. return group;
  140. }
  141. /**
  142. * Creates a normalized group taking into account all drop modifiers, needed when handling a group which has items with different chance rates
  143. * @param victim
  144. * @param killer
  145. * @return a new normalized group with all drop modifiers applied
  146. */
  147. public final GroupedGeneralDropItem normalizeMe(L2Character victim, L2Character killer)
  148. {
  149. return normalizeMe(victim, killer, true, 1);
  150. }
  151. /**
  152. * Creates a normalized group taking into account all drop modifiers, needed when handling a group which has items with different chance rates
  153. * @param victim
  154. * @param killer
  155. * @param chanceModifier an additional chance modifier
  156. * @return a new normalized group with all drop modifiers applied
  157. */
  158. public final GroupedGeneralDropItem normalizeMe(L2Character victim, L2Character killer, double chanceModifier)
  159. {
  160. return normalizeMe(victim, killer, true, chanceModifier);
  161. }
  162. /**
  163. * Creates a normalized group taking into account all drop modifiers, needed when handling a group which has items with different chance rates
  164. * @param victim
  165. * @return a new normalized group with all victim modifiers applied
  166. */
  167. public final GroupedGeneralDropItem normalizeMe(L2Character victim)
  168. {
  169. return normalizeMe(victim, null, false, 1);
  170. }
  171. /**
  172. * Creates a normalized group taking into account all drop modifiers, needed when handling a group which has items with different chance rates
  173. * @param victim
  174. * @param chanceModifier an additional chance modifier
  175. * @return a new normalized group with all victim modifiers applied
  176. */
  177. public final GroupedGeneralDropItem normalizeMe(L2Character victim, double chanceModifier)
  178. {
  179. return normalizeMe(victim, null, false, chanceModifier);
  180. }
  181. /**
  182. * Creates a normalized group taking into account all drop modifiers, needed when handling a group which has items with different chance rates
  183. * @param victim
  184. * @param killer
  185. * @param applyKillerModifier if to modify chance by {@link GroupedGeneralDropItem#getKillerChanceModifier(L2Character, L2Character)}
  186. * @param chanceModifier an additional chance modifier
  187. * @return a new normalized group with all drop modifiers applied
  188. */
  189. private final GroupedGeneralDropItem normalizeMe(L2Character victim, L2Character killer, boolean applyKillerModifier, double chanceModifier)
  190. {
  191. if (applyKillerModifier)
  192. {
  193. chanceModifier *= (getKillerChanceModifier(victim, killer));
  194. }
  195. double sumchance = 0;
  196. for (GeneralDropItem item : getItems())
  197. {
  198. sumchance += (item.getChance(victim) * getChance() * chanceModifier) / 100;
  199. }
  200. GroupedGeneralDropItem group = new GroupedGeneralDropItem(sumchance, getDropCalculationStrategy(), IKillerChanceModifierStrategy.NO_RULES, getPreciseStrategy()); // to discard further deep blue calculations
  201. List<GeneralDropItem> items = new ArrayList<>();
  202. for (GeneralDropItem item : getItems())
  203. {
  204. // the item is made almost "static"
  205. items.add(new GeneralDropItem(item.getItemId(), item.getMin(victim), item.getMax(victim), (item.getChance(victim) * getChance() * chanceModifier) / sumchance, IAmountMultiplierStrategy.STATIC, IChanceMultiplierStrategy.STATIC, getPreciseStrategy(), IKillerChanceModifierStrategy.NO_RULES, item.getDropCalculationStrategy()));
  206. }
  207. group.setItems(items);
  208. return group;
  209. }
  210. /*
  211. * (non-Javadoc)
  212. * @see com.l2jserver.gameserver.model.drop.IDropItem#calculateDrops(com.l2jserver.gameserver.model.actor.L2Character, com.l2jserver.gameserver.model.actor.L2Character)
  213. */
  214. @Override
  215. public final List<ItemHolder> calculateDrops(L2Character victim, L2Character killer)
  216. {
  217. return _dropCalculationStrategy.calculateDrops(this, victim, killer);
  218. }
  219. /**
  220. * This handles by default deep blue drop rules. It may also be used to handle another drop chance rules based on killer
  221. * @param victim the victim who drops the item
  222. * @param killer who kills the victim
  223. * @return a number between 0 and 1 (usually)
  224. */
  225. public final double getKillerChanceModifier(L2Character victim, L2Character killer)
  226. {
  227. return _killerChanceModifierStrategy.getKillerChanceModifier(this, victim, killer);
  228. }
  229. public boolean isPreciseCalculated()
  230. {
  231. return _preciseStrategy.isPreciseCalculated(this);
  232. }
  233. }