2
0

AbstractEnchantPacket.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.network.clientpackets;
  16. import gnu.trove.TIntObjectHashMap;
  17. import java.util.Arrays;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.model.L2ItemInstance;
  20. import com.l2jserver.gameserver.templates.item.L2Item;
  21. import com.l2jserver.gameserver.templates.item.L2WeaponType;
  22. public abstract class AbstractEnchantPacket extends L2GameClientPacket
  23. {
  24. public static final TIntObjectHashMap<EnchantScroll> _scrolls = new TIntObjectHashMap<EnchantScroll>();
  25. public static final TIntObjectHashMap<EnchantItem> _supports = new TIntObjectHashMap<EnchantItem>();
  26. public static class EnchantItem
  27. {
  28. protected final boolean _isWeapon;
  29. protected final int _grade;
  30. protected final int _maxEnchantLevel;
  31. protected final int _chanceAdd;
  32. protected final int[] _itemIds;
  33. public EnchantItem(boolean wep, int type, int level, int chance, int[] items)
  34. {
  35. _isWeapon = wep;
  36. _grade = type;
  37. _maxEnchantLevel = level;
  38. _chanceAdd = chance;
  39. _itemIds = items;
  40. }
  41. /*
  42. * Return true if support item can be used for this item
  43. */
  44. public final boolean isValid(L2ItemInstance enchantItem)
  45. {
  46. if (enchantItem == null)
  47. return false;
  48. int type2 = enchantItem.getItem().getType2();
  49. // checking scroll type and configured maximum enchant level
  50. switch (type2)
  51. {
  52. // weapon scrolls can enchant only weapons
  53. case L2Item.TYPE2_WEAPON:
  54. if (!_isWeapon
  55. || (Config.ENCHANT_MAX_WEAPON > 0 && enchantItem.getEnchantLevel() >= Config.ENCHANT_MAX_WEAPON))
  56. return false;
  57. break;
  58. // armor scrolls can enchant only accessory and armors
  59. case L2Item.TYPE2_SHIELD_ARMOR:
  60. if (_isWeapon
  61. || (Config.ENCHANT_MAX_ARMOR > 0 && enchantItem.getEnchantLevel() >= Config.ENCHANT_MAX_ARMOR))
  62. return false;
  63. break;
  64. case L2Item.TYPE2_ACCESSORY:
  65. if (_isWeapon
  66. || (Config.ENCHANT_MAX_JEWELRY > 0 && enchantItem.getEnchantLevel() >= Config.ENCHANT_MAX_JEWELRY))
  67. return false;
  68. break;
  69. default:
  70. return false;
  71. }
  72. // check for crystal types
  73. if (_grade != enchantItem.getItem().getItemGradeSPlus())
  74. return false;
  75. // check for maximum enchant level
  76. if (_maxEnchantLevel != 0 && enchantItem.getEnchantLevel() >= _maxEnchantLevel)
  77. return false;
  78. if(_itemIds != null && Arrays.binarySearch(_itemIds, enchantItem.getItemId()) < 0)
  79. return false;
  80. return true;
  81. }
  82. /*
  83. * return chance increase
  84. */
  85. public final int getChanceAdd()
  86. {
  87. return _chanceAdd;
  88. }
  89. }
  90. public static final class EnchantScroll extends EnchantItem
  91. {
  92. private final boolean _isBlessed;
  93. private final boolean _isCrystal;
  94. private final boolean _isSafe;
  95. public EnchantScroll(boolean wep, boolean bless, boolean crystal, boolean safe, int type, int level, int chance, int[] items)
  96. {
  97. super(wep, type, level, chance, items);
  98. _isBlessed = bless;
  99. _isCrystal = crystal;
  100. _isSafe = safe;
  101. }
  102. /*
  103. * Return true for blessed scrolls
  104. */
  105. public final boolean isBlessed()
  106. {
  107. return _isBlessed;
  108. }
  109. /*
  110. * Return true for crystal scrolls
  111. */
  112. public final boolean isCrystal()
  113. {
  114. return _isCrystal;
  115. }
  116. /*
  117. * Return true for safe-enchant scrolls (enchant level will remain on failure)
  118. */
  119. public final boolean isSafe()
  120. {
  121. return _isSafe;
  122. }
  123. public final boolean isValid(L2ItemInstance enchantItem, EnchantItem supportItem)
  124. {
  125. // blessed scrolls can't use support items
  126. if (supportItem != null && (!supportItem.isValid(enchantItem) || isBlessed()))
  127. return false;
  128. return isValid(enchantItem);
  129. }
  130. public final int getChance(L2ItemInstance enchantItem, EnchantItem supportItem)
  131. {
  132. if (!isValid(enchantItem, supportItem))
  133. return -1;
  134. boolean fullBody = enchantItem.getItem().getBodyPart() == L2Item.SLOT_FULL_ARMOR;
  135. if (enchantItem.getEnchantLevel() < Config.ENCHANT_SAFE_MAX
  136. || (fullBody && enchantItem.getEnchantLevel() < Config.ENCHANT_SAFE_MAX_FULL))
  137. return 100;
  138. boolean isAccessory = enchantItem.getItem().getType2() == L2Item.TYPE2_ACCESSORY;
  139. int chance = 0;
  140. if (_isBlessed)
  141. {
  142. // blessed scrolls does not use support items
  143. if (supportItem != null)
  144. return -1;
  145. if (_isWeapon)
  146. chance = Config.BLESSED_ENCHANT_CHANCE_WEAPON;
  147. else if (isAccessory)
  148. chance = Config.BLESSED_ENCHANT_CHANCE_JEWELRY;
  149. else
  150. chance = Config.BLESSED_ENCHANT_CHANCE_ARMOR;
  151. }
  152. else
  153. {
  154. if (_isWeapon)
  155. chance = Config.ENCHANT_CHANCE_WEAPON;
  156. else if (isAccessory)
  157. chance = Config.ENCHANT_CHANCE_JEWELRY;
  158. else
  159. chance = Config.ENCHANT_CHANCE_ARMOR;
  160. }
  161. chance += _chanceAdd;
  162. if (supportItem != null)
  163. chance += supportItem.getChanceAdd();
  164. return chance;
  165. }
  166. }
  167. static
  168. {
  169. // itemId, (isWeapon, isBlessed, isCrystal, isSafe, grade, max enchant level, chance increase, allowed item IDs)
  170. // allowed items list must be sorted by ascending order
  171. _scrolls.put(729, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_A, 0, 0, null));
  172. _scrolls.put(730, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_A, 0, 0, null));
  173. _scrolls.put(731, new EnchantScroll(true, false, true, false, L2Item.CRYSTAL_A, 0, 0, null));
  174. _scrolls.put(732, new EnchantScroll(false, false, true, false, L2Item.CRYSTAL_A, 0, 0, null));
  175. _scrolls.put(947, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_B, 0, 0, null));
  176. _scrolls.put(948, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_B, 0, 0, null));
  177. _scrolls.put(949, new EnchantScroll(true, false, true, false, L2Item.CRYSTAL_B, 0, 0, null));
  178. _scrolls.put(950, new EnchantScroll(false, false, true, false, L2Item.CRYSTAL_B, 0, 0, null));
  179. _scrolls.put(951, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_C, 0, 0, null));
  180. _scrolls.put(952, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_C, 0, 0, null));
  181. _scrolls.put(953, new EnchantScroll(true, false, true, false, L2Item.CRYSTAL_C, 0, 0, null));
  182. _scrolls.put(954, new EnchantScroll(false, false, true, false, L2Item.CRYSTAL_C, 0, 0, null));
  183. _scrolls.put(955, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_D, 0, 0, null));
  184. _scrolls.put(956, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_D, 0, 0, null));
  185. _scrolls.put(957, new EnchantScroll(true, false, true, false, L2Item.CRYSTAL_D, 0, 0, null));
  186. _scrolls.put(958, new EnchantScroll(false, false, true, false, L2Item.CRYSTAL_D, 0, 0, null));
  187. _scrolls.put(959, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_S, 0, 0, null));
  188. _scrolls.put(960, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_S, 0, 0, null));
  189. _scrolls.put(961, new EnchantScroll(true, false, true, false, L2Item.CRYSTAL_S, 0, 0, null));
  190. _scrolls.put(962, new EnchantScroll(false, false, true, false, L2Item.CRYSTAL_S, 0, 0, null));
  191. _scrolls.put(6569, new EnchantScroll(true, true, false, false, L2Item.CRYSTAL_A, 0, 0, null));
  192. _scrolls.put(6570, new EnchantScroll(false, true, false, false, L2Item.CRYSTAL_A, 0, 0, null));
  193. _scrolls.put(6571, new EnchantScroll(true, true, false, false, L2Item.CRYSTAL_B, 0, 0, null));
  194. _scrolls.put(6572, new EnchantScroll(false, true, false, false, L2Item.CRYSTAL_B, 0, 0, null));
  195. _scrolls.put(6573, new EnchantScroll(true, true, false, false, L2Item.CRYSTAL_C, 0, 0, null));
  196. _scrolls.put(6574, new EnchantScroll(false, true, false, false, L2Item.CRYSTAL_C, 0, 0, null));
  197. _scrolls.put(6575, new EnchantScroll(true, true, false, false, L2Item.CRYSTAL_D, 0, 0, null));
  198. _scrolls.put(6576, new EnchantScroll(false, true, false, false, L2Item.CRYSTAL_D, 0, 0, null));
  199. _scrolls.put(6577, new EnchantScroll(true, true, false, false, L2Item.CRYSTAL_S, 0, 0, null));
  200. _scrolls.put(6578, new EnchantScroll(false, true, false, false, L2Item.CRYSTAL_S, 0, 0, null));
  201. _scrolls.put(22006, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_D, 0, 10, null));
  202. _scrolls.put(22007, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_C, 0, 10, null));
  203. _scrolls.put(22008, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_B, 0, 10, null));
  204. _scrolls.put(22009, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_A, 0, 10, null));
  205. _scrolls.put(22010, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_D, 0, 10, null));
  206. _scrolls.put(22011, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_C, 0, 10, null));
  207. _scrolls.put(22012, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_B, 0, 10, null));
  208. _scrolls.put(22013, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_A, 0, 10, null));
  209. _scrolls.put(22014, new EnchantScroll(true, false, false, true, L2Item.CRYSTAL_B, 16, 10, null));
  210. _scrolls.put(22015, new EnchantScroll(true, false, false, true, L2Item.CRYSTAL_A, 16, 10, null));
  211. _scrolls.put(22016, new EnchantScroll(false, false, false, true, L2Item.CRYSTAL_B, 16, 10, null));
  212. _scrolls.put(22017, new EnchantScroll(false, false, false, true, L2Item.CRYSTAL_A, 16, 10, null));
  213. _scrolls.put(22018, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_B, 0, 100, null));
  214. _scrolls.put(22019, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_A, 0, 100, null));
  215. _scrolls.put(22020, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_B, 0, 100, null));
  216. _scrolls.put(22021, new EnchantScroll(false, false, false, false, L2Item.CRYSTAL_A, 0, 100, null));
  217. // Master Yogi's Scroll Enchant Weapon (event)
  218. _scrolls.put(13540, new EnchantScroll(true, false, false, false, L2Item.CRYSTAL_NONE, 0, 0, new int[]{ 13539 }));
  219. // itemId, (isWeapon, grade, max enchant level, chance increase)
  220. _supports.put(12362, new EnchantItem(true, L2Item.CRYSTAL_D, 9, 20, null));
  221. _supports.put(12363, new EnchantItem(true, L2Item.CRYSTAL_C, 9, 18, null));
  222. _supports.put(12364, new EnchantItem(true, L2Item.CRYSTAL_B, 9, 15, null));
  223. _supports.put(12365, new EnchantItem(true, L2Item.CRYSTAL_A, 9, 12, null));
  224. _supports.put(12366, new EnchantItem(true, L2Item.CRYSTAL_S, 9, 10, null));
  225. _supports.put(12367, new EnchantItem(false, L2Item.CRYSTAL_D, 9, 35, null));
  226. _supports.put(12368, new EnchantItem(false, L2Item.CRYSTAL_C, 9, 27, null));
  227. _supports.put(12369, new EnchantItem(false, L2Item.CRYSTAL_B, 9, 23, null));
  228. _supports.put(12370, new EnchantItem(false, L2Item.CRYSTAL_A, 9, 18, null));
  229. _supports.put(12371, new EnchantItem(false, L2Item.CRYSTAL_S, 9, 15, null));
  230. _supports.put(14702, new EnchantItem(true, L2Item.CRYSTAL_D, 9, 20, null));
  231. _supports.put(14703, new EnchantItem(true, L2Item.CRYSTAL_C, 9, 18, null));
  232. _supports.put(14704, new EnchantItem(true, L2Item.CRYSTAL_B, 9, 15, null));
  233. _supports.put(14705, new EnchantItem(true, L2Item.CRYSTAL_A, 9, 12, null));
  234. _supports.put(14706, new EnchantItem(true, L2Item.CRYSTAL_S, 9, 10, null));
  235. _supports.put(14707, new EnchantItem(false, L2Item.CRYSTAL_D, 9, 35, null));
  236. _supports.put(14708, new EnchantItem(false, L2Item.CRYSTAL_C, 9, 27, null));
  237. _supports.put(14709, new EnchantItem(false, L2Item.CRYSTAL_B, 9, 23, null));
  238. _supports.put(14710, new EnchantItem(false, L2Item.CRYSTAL_A, 9, 18, null));
  239. _supports.put(14711, new EnchantItem(false, L2Item.CRYSTAL_S, 9, 15, null));
  240. }
  241. /**
  242. * Return enchant template for scroll
  243. */
  244. protected static final EnchantScroll getEnchantScroll(L2ItemInstance scroll)
  245. {
  246. return _scrolls.get(scroll.getItemId());
  247. }
  248. /**
  249. * Return enchant template for support item
  250. */
  251. protected static final EnchantItem getSupportItem(L2ItemInstance item)
  252. {
  253. return _supports.get(item.getItemId());
  254. }
  255. /**
  256. * Return true if item can be enchanted
  257. */
  258. protected static final boolean isEnchantable(L2ItemInstance item)
  259. {
  260. if (item.isHeroItem())
  261. return false;
  262. if (item.isShadowItem())
  263. return false;
  264. if (item.isCommonItem())
  265. return false;
  266. if (item.isEtcItem())
  267. return false;
  268. if (item.isTimeLimitedItem())
  269. return false;
  270. if (item.isWear())
  271. return false;
  272. // rods
  273. if (item.getItem().getItemType() == L2WeaponType.ROD)
  274. return false;
  275. // apprentice and travelers weapons
  276. if (item.getItemId() >= 7816 && item.getItemId() <= 7831)
  277. return false;
  278. // bracelets
  279. if (item.getItem().getBodyPart() == L2Item.SLOT_L_BRACELET)
  280. return false;
  281. if (item.getItem().getBodyPart() == L2Item.SLOT_R_BRACELET)
  282. return false;
  283. if (item.getItem().getBodyPart() == L2Item.SLOT_BACK)
  284. return false;
  285. // only items in inventory and equipped can be enchanted
  286. if (item.getLocation() != L2ItemInstance.ItemLocation.INVENTORY
  287. && item.getLocation() != L2ItemInstance.ItemLocation.PAPERDOLL)
  288. return false;
  289. return true;
  290. }
  291. }