SkillTable.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under the terms of the
  3. * GNU General Public License as published by the Free Software Foundation, either version 3 of the
  4. * License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. * General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with this program. If
  11. * not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package com.l2jserver.gameserver.datatables;
  14. import java.util.logging.Logger;
  15. import gnu.trove.TIntArrayList;
  16. import gnu.trove.TIntIntHashMap;
  17. import gnu.trove.TIntObjectHashMap;
  18. import com.l2jserver.gameserver.model.L2Skill;
  19. import com.l2jserver.gameserver.skills.SkillsEngine;
  20. /**
  21. *
  22. */
  23. public class SkillTable
  24. {
  25. private static Logger _log = Logger.getLogger(SkillTable.class.getName());
  26. private final TIntObjectHashMap<L2Skill> _skills;
  27. private final TIntIntHashMap _skillMaxLevel;
  28. private final TIntArrayList _enchantable;
  29. public static SkillTable getInstance()
  30. {
  31. return SingletonHolder._instance;
  32. }
  33. private SkillTable()
  34. {
  35. _skills = new TIntObjectHashMap<L2Skill>();
  36. _skillMaxLevel = new TIntIntHashMap();
  37. _enchantable = new TIntArrayList();
  38. load();
  39. }
  40. public void reload()
  41. {
  42. load();
  43. //reload some related too
  44. SkillTreeTable.getInstance().reload();
  45. SubPledgeSkillTree.getInstance().reload();
  46. }
  47. private void load()
  48. {
  49. _skills.clear();
  50. SkillsEngine.getInstance().loadAllSkills(_skills);
  51. _skillMaxLevel.clear();
  52. for (final L2Skill skill : _skills.getValues(new L2Skill[_skills.size()]))
  53. {
  54. final int skillId = skill.getId();
  55. final int skillLvl = skill.getLevel();
  56. if (skillLvl > 99)
  57. {
  58. if (!_enchantable.contains(skillId))
  59. _enchantable.add(skillId);
  60. continue;
  61. }
  62. // only non-enchanted skills
  63. final int maxLvl = _skillMaxLevel.get(skillId);
  64. if (skillLvl > maxLvl)
  65. _skillMaxLevel.put(skillId, skillLvl);
  66. }
  67. //SkillTreeTable.getInstance().reload();
  68. // Sorting for binarySearch
  69. _enchantable.sort();
  70. // Reloading as well FrequentSkill enumeration values
  71. for (FrequentSkill sk : FrequentSkill.values())
  72. sk._skill = getInfo(sk._id, sk._level);
  73. }
  74. /**
  75. * Provides the skill hash
  76. *
  77. * @param skill
  78. * 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. *
  88. * @param skillId
  89. * The Skill Id
  90. * @param skillLevel
  91. * The Skill Level
  92. * @return The Skill hash number
  93. */
  94. public static int getSkillHashCode(int skillId, int skillLevel)
  95. {
  96. return skillId * 1021 + skillLevel;
  97. }
  98. public final L2Skill getInfo(final int skillId, final int level)
  99. {
  100. final L2Skill result = _skills.get(getSkillHashCode(skillId, level));
  101. if (result != null)
  102. return result;
  103. // skill/level not found, fix for transformation scripts
  104. final int maxLvl = _skillMaxLevel.get(skillId);
  105. // requested level too high
  106. if (maxLvl > 0 && level > maxLvl)
  107. return _skills.get(getSkillHashCode(skillId, maxLvl));
  108. _log.warning("No skill info found for skill id " + skillId + " and skill level " + level + ".");
  109. return null;
  110. }
  111. public final int getMaxLevel(final int skillId)
  112. {
  113. return _skillMaxLevel.get(skillId);
  114. }
  115. public final boolean isEnchantable(final int skillId)
  116. {
  117. return _enchantable.binarySearch(skillId) >= 0;
  118. }
  119. /**
  120. * Returns an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  121. */
  122. public L2Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  123. {
  124. L2Skill[] temp = new L2Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  125. int i = 0;
  126. temp[i++] = _skills.get(SkillTable.getSkillHashCode(246, 1));
  127. temp[i++] = _skills.get(SkillTable.getSkillHashCode(247, 1));
  128. if (addNoble)
  129. temp[i++] = _skills.get(SkillTable.getSkillHashCode(326, 1));
  130. if (hasCastle)
  131. {
  132. temp[i++] = _skills.get(SkillTable.getSkillHashCode(844, 1));
  133. temp[i++] = _skills.get(SkillTable.getSkillHashCode(845, 1));
  134. }
  135. return temp;
  136. }
  137. @SuppressWarnings("synthetic-access")
  138. private static class SingletonHolder
  139. {
  140. protected static final SkillTable _instance = new SkillTable();
  141. }
  142. /**
  143. * Enum to hold some important references to frequently used (hardcoded) skills in core
  144. *
  145. * @author DrHouse
  146. *
  147. */
  148. public static enum FrequentSkill
  149. {
  150. RAID_CURSE(4215, 1),
  151. RAID_CURSE2(4515, 1),
  152. SEAL_OF_RULER(246, 1),
  153. BUILD_HEADQUARTERS(247, 1),
  154. LUCKY(194, 1),
  155. DWARVEN_CRAFT(1321, 1),
  156. COMMON_CRAFT(1322, 1),
  157. WYVERN_BREATH(4289, 1),
  158. STRIDER_SIEGE_ASSAULT(325, 1),
  159. FAKE_PETRIFICATION(4616, 1),
  160. FIREWORK(5965, 1),
  161. LARGE_FIREWORK(2025, 1),
  162. BLESSING_OF_PROTECTION(5182, 1),
  163. ARENA_CP_RECOVERY(4380, 1),
  164. VOID_BURST(3630, 1),
  165. VOID_FLOW(3631, 1),
  166. THE_VICTOR_OF_WAR(5074, 1),
  167. THE_VANQUISHED_OF_WAR(5075, 1),
  168. SPECIAL_TREE_RECOVERY_BONUS(2139, 1);
  169. private final int _id;
  170. private final int _level;
  171. private L2Skill _skill = null;
  172. private FrequentSkill(int id, int level)
  173. {
  174. _id = id;
  175. _level = level;
  176. }
  177. public L2Skill getSkill()
  178. {
  179. return _skill;
  180. }
  181. }
  182. }