EnchantScroll.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.network.Debug;
  26. import com.l2jserver.gameserver.util.Util;
  27. import com.l2jserver.util.Rnd;
  28. /**
  29. * @author UnAfraid
  30. */
  31. public final class EnchantScroll extends EnchantItem
  32. {
  33. private final boolean _isBlessed;
  34. private final boolean _isSafe;
  35. private final int _scrollGroupId;
  36. public EnchantScroll(StatsSet set)
  37. {
  38. super(set);
  39. _isBlessed = set.getBool("isBlessed", false);
  40. _isSafe = set.getBool("isSafe", false);
  41. _scrollGroupId = set.getInteger("scrollGroupId", 0);
  42. }
  43. /**
  44. * @return {@code true} for blessed scrolls (enchanted item will remain on failure), {@code false} otherwise.
  45. */
  46. public boolean isBlessed()
  47. {
  48. return _isBlessed;
  49. }
  50. /**
  51. * @return {@code true} for safe-enchant scrolls (enchant level will remain on failure), {@code false} otherwise.
  52. */
  53. public boolean isSafe()
  54. {
  55. return _isSafe;
  56. }
  57. /**
  58. * @return id of scroll group that should be used.
  59. */
  60. public int getScrollGroupId()
  61. {
  62. return _scrollGroupId;
  63. }
  64. /**
  65. * @param enchantItem
  66. * @param supportItem
  67. * @return {@code true} if current scroll is valid to be used with support item, {@code false} otherwise.
  68. */
  69. public boolean isValid(L2ItemInstance enchantItem, EnchantItem supportItem)
  70. {
  71. // blessed scrolls can't use support items
  72. if ((supportItem != null) && (!supportItem.isValid(enchantItem) || isBlessed()))
  73. {
  74. return false;
  75. }
  76. return super.isValid(enchantItem);
  77. }
  78. /**
  79. * @param player
  80. * @param enchantItem
  81. * @param supportItem
  82. * @return the total chance for success rate of this scroll.
  83. */
  84. public EnchantResultType calculateSuccess(L2PcInstance player, L2ItemInstance enchantItem, EnchantItem supportItem)
  85. {
  86. if (!isValid(enchantItem, supportItem))
  87. {
  88. return EnchantResultType.FAILURE;
  89. }
  90. if (EnchantItemGroupsData.getInstance().getScrollGroup(_scrollGroupId) == null)
  91. {
  92. _log.log(Level.WARNING, getClass().getSimpleName() + ": Unexistent enchant scroll group specified for enchant scroll: " + getId());
  93. return EnchantResultType.FAILURE;
  94. }
  95. final EnchantItemGroup group = EnchantItemGroupsData.getInstance().getItemGroup(enchantItem.getItem(), _scrollGroupId);
  96. if (group == null)
  97. {
  98. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't find enchant item group for scroll: " + getId() + " requested by: " + player);
  99. return EnchantResultType.FAILURE;
  100. }
  101. final double chance = group.getChance(enchantItem.getEnchantLevel());
  102. final double bonusRate = getBonusRate();
  103. final double supportBonusRate = ((supportItem != null) && !_isBlessed) ? supportItem.getBonusRate() : 0;
  104. final double finalChance = chance + bonusRate + supportBonusRate;
  105. final double random = 100 * Rnd.nextDouble();
  106. final boolean success = (random < finalChance);
  107. if (player.isDebug())
  108. {
  109. final StatsSet set = new StatsSet();
  110. set.set("chance", Util.formatDouble(chance, "#.##"));
  111. set.set("bonusRate", Util.formatDouble(bonusRate, "#.##"));
  112. set.set("supportBonusRate", Util.formatDouble(supportBonusRate, "#.##"));
  113. set.set("finalChance", Util.formatDouble(finalChance, "#.##"));
  114. set.set("random", Util.formatDouble(random, "#.##"));
  115. set.set("success", success);
  116. set.set("item group", group.getName());
  117. set.set("scroll group", _scrollGroupId);
  118. Debug.sendItemDebug(player, enchantItem, set);
  119. }
  120. return success ? EnchantResultType.SUCCESS : EnchantResultType.FAILURE;
  121. }
  122. }