EnchantScroll.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2004-2013 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.enchant;
  20. import java.util.logging.Level;
  21. import com.l2jserver.gameserver.datatables.EnchantItemGroupsData;
  22. import com.l2jserver.gameserver.model.StatsSet;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  25. import com.l2jserver.gameserver.model.items.type.L2EtcItemType;
  26. import com.l2jserver.gameserver.model.items.type.L2ItemType;
  27. import com.l2jserver.gameserver.network.Debug;
  28. import com.l2jserver.gameserver.util.Util;
  29. import com.l2jserver.util.Rnd;
  30. /**
  31. * @author UnAfraid
  32. */
  33. public final class EnchantScroll extends EnchantItem
  34. {
  35. private final boolean _isBlessed;
  36. private final boolean _isSafe;
  37. private final int _scrollGroupId;
  38. public EnchantScroll(StatsSet set)
  39. {
  40. super(set);
  41. _scrollGroupId = set.getInteger("scrollGroupId", 0);
  42. final L2ItemType type = getItem().getItemType();
  43. _isWeapon = (type == L2EtcItemType.ANCIENT_CRYSTAL_ENCHANT_WP) || (type == L2EtcItemType.BLESS_SCRL_ENCHANT_WP) || (type == L2EtcItemType.SCRL_ENCHANT_WP);
  44. _isBlessed = (type == L2EtcItemType.BLESS_SCRL_ENCHANT_AM) || (type == L2EtcItemType.BLESS_SCRL_ENCHANT_WP);
  45. _isSafe = (type == L2EtcItemType.ANCIENT_CRYSTAL_ENCHANT_AM) || (type == L2EtcItemType.ANCIENT_CRYSTAL_ENCHANT_WP);
  46. }
  47. /**
  48. * @return {@code true} for blessed scrolls (enchanted item will remain on failure), {@code false} otherwise
  49. */
  50. public boolean isBlessed()
  51. {
  52. return _isBlessed;
  53. }
  54. /**
  55. * @return {@code true} for safe-enchant scrolls (enchant level will remain on failure), {@code false} otherwise
  56. */
  57. public boolean isSafe()
  58. {
  59. return _isSafe;
  60. }
  61. /**
  62. * @return id of scroll group that should be used
  63. */
  64. public int getScrollGroupId()
  65. {
  66. return _scrollGroupId;
  67. }
  68. /**
  69. * @param enchantItem
  70. * @param supportItem
  71. * @return {@code true} if current scroll is valid to be used with support item, {@code false} otherwise
  72. */
  73. public boolean isValid(L2ItemInstance enchantItem, EnchantItem supportItem)
  74. {
  75. if ((supportItem != null))
  76. {
  77. // blessed scrolls can't use support items
  78. if (isBlessed())
  79. {
  80. return false;
  81. }
  82. if (!supportItem.isValid(enchantItem))
  83. {
  84. return false;
  85. }
  86. else if (supportItem.isWeapon() != _isWeapon)
  87. {
  88. return false;
  89. }
  90. }
  91. return super.isValid(enchantItem);
  92. }
  93. /**
  94. * @param player
  95. * @param enchantItem
  96. * @return the chance of current scroll's group.
  97. */
  98. public double getChance(L2PcInstance player, L2ItemInstance enchantItem)
  99. {
  100. if (EnchantItemGroupsData.getInstance().getScrollGroup(_scrollGroupId) == null)
  101. {
  102. _log.log(Level.WARNING, getClass().getSimpleName() + ": Unexistent enchant scroll group specified for enchant scroll: " + getId());
  103. return -1;
  104. }
  105. final EnchantItemGroup group = EnchantItemGroupsData.getInstance().getItemGroup(enchantItem.getItem(), _scrollGroupId);
  106. if (group == null)
  107. {
  108. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't find enchant item group for scroll: " + getId() + " requested by: " + player);
  109. return -1;
  110. }
  111. return group.getChance(enchantItem.getEnchantLevel());
  112. }
  113. /**
  114. * @param player
  115. * @param enchantItem
  116. * @param supportItem
  117. * @return the total chance for success rate of this scroll
  118. */
  119. public EnchantResultType calculateSuccess(L2PcInstance player, L2ItemInstance enchantItem, EnchantItem supportItem)
  120. {
  121. if (!isValid(enchantItem, supportItem))
  122. {
  123. return EnchantResultType.ERROR;
  124. }
  125. final double chance = getChance(player, enchantItem);
  126. if (chance == -1)
  127. {
  128. return EnchantResultType.ERROR;
  129. }
  130. final double bonusRate = getBonusRate();
  131. final double supportBonusRate = (supportItem != null) ? supportItem.getBonusRate() : 0;
  132. final double finalChance = Math.min(chance + bonusRate + supportBonusRate, 100);
  133. final double random = 100 * Rnd.nextDouble();
  134. final boolean success = (random < finalChance);
  135. if (player.isDebug())
  136. {
  137. final EnchantItemGroup group = EnchantItemGroupsData.getInstance().getItemGroup(enchantItem.getItem(), _scrollGroupId);
  138. final StatsSet set = new StatsSet();
  139. if (isBlessed())
  140. {
  141. set.set("isBlessed", isBlessed());
  142. }
  143. if (isSafe())
  144. {
  145. set.set("isSafe", isSafe());
  146. }
  147. set.set("chance", Util.formatDouble(chance, "#.##"));
  148. if (bonusRate > 0)
  149. {
  150. set.set("bonusRate", Util.formatDouble(bonusRate, "#.##"));
  151. }
  152. if (supportBonusRate > 0)
  153. {
  154. set.set("supportBonusRate", Util.formatDouble(supportBonusRate, "#.##"));
  155. }
  156. set.set("finalChance", Util.formatDouble(finalChance, "#.##"));
  157. set.set("random", Util.formatDouble(random, "#.##"));
  158. set.set("success", success);
  159. set.set("item group", group.getName());
  160. set.set("scroll group", _scrollGroupId);
  161. Debug.sendItemDebug(player, enchantItem, set);
  162. }
  163. return success ? EnchantResultType.SUCCESS : EnchantResultType.FAILURE;
  164. }
  165. }