SkillTable.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.datatables;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;
  24. import com.l2jserver.Config;
  25. import com.l2jserver.gameserver.engines.DocumentEngine;
  26. import com.l2jserver.gameserver.model.holders.SkillHolder;
  27. import com.l2jserver.gameserver.model.skills.L2Skill;
  28. import gnu.trove.list.array.TIntArrayList;
  29. import gnu.trove.map.hash.TIntIntHashMap;
  30. /**
  31. * Skill table.
  32. */
  33. public final class SkillTable
  34. {
  35. private static Logger _log = Logger.getLogger(SkillTable.class.getName());
  36. private final Map<Integer, L2Skill> _skills = new HashMap<>();
  37. private final TIntIntHashMap _skillMaxLevel = new TIntIntHashMap();
  38. private final TIntArrayList _enchantable = new TIntArrayList();
  39. protected SkillTable()
  40. {
  41. load();
  42. }
  43. public void reload()
  44. {
  45. load();
  46. // Reload Skill Tree as well.
  47. SkillTreesData.getInstance().load();
  48. }
  49. private void load()
  50. {
  51. _skills.clear();
  52. DocumentEngine.getInstance().loadAllSkills(_skills);
  53. _skillMaxLevel.clear();
  54. for (L2Skill skill : _skills.values())
  55. {
  56. final int skillId = skill.getId();
  57. final int skillLvl = skill.getLevel();
  58. if (skillLvl > 99)
  59. {
  60. if (!_enchantable.contains(skillId))
  61. {
  62. _enchantable.add(skillId);
  63. }
  64. continue;
  65. }
  66. // only non-enchanted skills
  67. final int maxLvl = _skillMaxLevel.get(skillId);
  68. if (skillLvl > maxLvl)
  69. {
  70. _skillMaxLevel.put(skillId, skillLvl);
  71. }
  72. }
  73. // Sorting for binarySearch
  74. _enchantable.sort();
  75. }
  76. /**
  77. * Provides the skill hash
  78. * @param skill The L2Skill to be hashed
  79. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  80. */
  81. public static int getSkillHashCode(L2Skill skill)
  82. {
  83. return getSkillHashCode(skill.getId(), skill.getLevel());
  84. }
  85. /**
  86. * Centralized method for easier change of the hashing sys
  87. * @param skillId The Skill Id
  88. * @param skillLevel The Skill Level
  89. * @return The Skill hash number
  90. */
  91. public static int getSkillHashCode(int skillId, int skillLevel)
  92. {
  93. return (skillId * 1021) + skillLevel;
  94. }
  95. public L2Skill getInfo(int skillId, int level)
  96. {
  97. final L2Skill result = _skills.get(getSkillHashCode(skillId, level));
  98. if (result != null)
  99. {
  100. return result;
  101. }
  102. // skill/level not found, fix for transformation scripts
  103. final int maxLvl = _skillMaxLevel.get(skillId);
  104. // requested level too high
  105. if ((maxLvl > 0) && (level > maxLvl))
  106. {
  107. if (Config.DEBUG)
  108. {
  109. _log.log(Level.WARNING, getClass().getSimpleName() + ": call to unexisting skill level id: " + skillId + " requested level: " + level + " max level: " + maxLvl, new Throwable());
  110. }
  111. return _skills.get(getSkillHashCode(skillId, maxLvl));
  112. }
  113. _log.warning(getClass().getSimpleName() + ": No skill info found for skill id " + skillId + " and skill level " + level + ".");
  114. return null;
  115. }
  116. public int getMaxLevel(int skillId)
  117. {
  118. return _skillMaxLevel.get(skillId);
  119. }
  120. public boolean isEnchantable(int skillId)
  121. {
  122. return _enchantable.binarySearch(skillId) >= 0;
  123. }
  124. /**
  125. * @param addNoble
  126. * @param hasCastle
  127. * @return an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  128. */
  129. public L2Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  130. {
  131. L2Skill[] temp = new L2Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  132. int i = 0;
  133. temp[i++] = _skills.get(SkillTable.getSkillHashCode(246, 1));
  134. temp[i++] = _skills.get(SkillTable.getSkillHashCode(247, 1));
  135. if (addNoble)
  136. {
  137. temp[i++] = _skills.get(SkillTable.getSkillHashCode(326, 1));
  138. }
  139. if (hasCastle)
  140. {
  141. temp[i++] = _skills.get(SkillTable.getSkillHashCode(844, 1));
  142. temp[i++] = _skills.get(SkillTable.getSkillHashCode(845, 1));
  143. }
  144. return temp;
  145. }
  146. /**
  147. * Enum to hold some important references to frequently used (hardcoded) skills in core
  148. * @author DrHouse
  149. */
  150. public static enum FrequentSkill
  151. {
  152. RAID_CURSE(4215, 1),
  153. RAID_CURSE2(4515, 1),
  154. SEAL_OF_RULER(246, 1),
  155. BUILD_HEADQUARTERS(247, 1),
  156. WYVERN_BREATH(4289, 1),
  157. STRIDER_SIEGE_ASSAULT(325, 1),
  158. FIREWORK(5965, 1),
  159. LARGE_FIREWORK(2025, 1),
  160. BLESSING_OF_PROTECTION(5182, 1),
  161. VOID_BURST(3630, 1),
  162. VOID_FLOW(3631, 1),
  163. THE_VICTOR_OF_WAR(5074, 1),
  164. THE_VANQUISHED_OF_WAR(5075, 1),
  165. SPECIAL_TREE_RECOVERY_BONUS(2139, 1),
  166. WEAPON_GRADE_PENALTY(6209, 1),
  167. ARMOR_GRADE_PENALTY(6213, 1);
  168. private final SkillHolder _holder;
  169. private FrequentSkill(int id, int level)
  170. {
  171. _holder = new SkillHolder(id, level);
  172. }
  173. public int getId()
  174. {
  175. return _holder.getSkillId();
  176. }
  177. public int getLevel()
  178. {
  179. return _holder.getSkillLvl();
  180. }
  181. public L2Skill getSkill()
  182. {
  183. return _holder.getSkill();
  184. }
  185. }
  186. public static SkillTable getInstance()
  187. {
  188. return SingletonHolder._instance;
  189. }
  190. private static class SingletonHolder
  191. {
  192. protected static final SkillTable _instance = new SkillTable();
  193. }
  194. }