GroupedGeneralDropItem.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2004-2015 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.Collection;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.datatables.ItemTable;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.actor.instance.L2RaidBossInstance;
  28. import com.l2jserver.gameserver.model.holders.ItemHolder;
  29. import com.l2jserver.gameserver.model.items.L2Item;
  30. import com.l2jserver.gameserver.util.Util;
  31. import com.l2jserver.util.Rnd;
  32. /**
  33. * @author NosBit
  34. */
  35. public class GroupedGeneralDropItem implements IDropItem
  36. {
  37. private final double _chance;
  38. private List<GeneralDropItem> _items;
  39. /**
  40. * @param chance the chance of this drop item.
  41. */
  42. public GroupedGeneralDropItem(double chance)
  43. {
  44. _chance = chance;
  45. }
  46. protected double getGlobalChanceMultiplier()
  47. {
  48. return 1.;
  49. }
  50. /**
  51. * Gets the chance of this drop item.
  52. * @return the chance
  53. */
  54. public double getChance()
  55. {
  56. return _chance;
  57. }
  58. /**
  59. * Gets the chance of this drop item.
  60. * @param victim the victim
  61. * @param killer the killer
  62. * @return the chance modified by any rates.
  63. */
  64. public double getChance(L2Character victim, L2Character killer)
  65. {
  66. for (final GeneralDropItem gdi : getItems())
  67. {
  68. final L2Item item = ItemTable.getInstance().getTemplate(gdi.getItemId());
  69. if ((item == null) || !item.hasExImmediateEffect())
  70. {
  71. return getChance() * getGlobalChanceMultiplier();
  72. }
  73. }
  74. return getChance() * Config.RATE_HERB_DROP_CHANCE_MULTIPLIER;
  75. }
  76. /**
  77. * Gets the items.
  78. * @return the items
  79. */
  80. public List<GeneralDropItem> getItems()
  81. {
  82. return _items;
  83. }
  84. /**
  85. * Sets an item list to this drop item.
  86. * @param items the item list
  87. */
  88. public void setItems(List<GeneralDropItem> items)
  89. {
  90. _items = Collections.unmodifiableList(items);
  91. }
  92. /*
  93. * (non-Javadoc)
  94. * @see com.l2jserver.gameserver.model.drop.IDropItem#calculateDrops(com.l2jserver.gameserver.model.actor.L2Character, com.l2jserver.gameserver.model.actor.L2Character)
  95. */
  96. @Override
  97. public Collection<ItemHolder> calculateDrops(L2Character victim, L2Character killer)
  98. {
  99. final int levelDifference = victim.getLevel() - killer.getLevel();
  100. double chanceModifier;
  101. if (victim instanceof L2RaidBossInstance)
  102. {
  103. chanceModifier = Math.max(0, Math.min(1, (levelDifference * 0.15) + 1));
  104. }
  105. else
  106. {
  107. chanceModifier = 1;
  108. double levelGapChanceToDrop = Util.map(levelDifference, -Config.DROP_ITEM_MAX_LEVEL_DIFFERENCE, -Config.DROP_ITEM_MIN_LEVEL_DIFFERENCE, Config.DROP_ITEM_MIN_LEVEL_GAP_CHANCE, 100.0);
  109. // There is a chance of level gap that it wont drop this item
  110. if (levelGapChanceToDrop < (Rnd.nextDouble() * 100))
  111. {
  112. return null;
  113. }
  114. }
  115. final double chance = getChance(victim, killer) * chanceModifier;
  116. int successes;
  117. if (!Config.L2JMOD_OLD_DROP_BEHAVIOR)
  118. {
  119. successes = chance > (Rnd.nextDouble() * 100) ? 1 : 0;
  120. }
  121. else
  122. {
  123. successes = (int) (chance / 100);
  124. successes += (chance % 100) > (Rnd.nextDouble() * 100) ? 1 : 0;
  125. }
  126. double totalChance = 0;
  127. final double random = (Rnd.nextDouble() * 100);
  128. for (GeneralDropItem item : getItems())
  129. {
  130. // Grouped item chance rates should not be modified.
  131. totalChance += item.getChance();
  132. if (totalChance > random)
  133. {
  134. final Collection<ItemHolder> items = new ArrayList<>(1);
  135. items.add(new ItemHolder(item.getItemId(), Rnd.get(item.getMin(victim, killer), item.getMax(victim, killer)) * successes));
  136. return items;
  137. }
  138. }
  139. return null;
  140. }
  141. }