SkillTable.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 com.l2jserver.gameserver.model.L2Skill;
  15. import com.l2jserver.gameserver.skills.SkillsEngine;
  16. import gnu.trove.TIntArrayList;
  17. import gnu.trove.TIntIntHashMap;
  18. import gnu.trove.TIntObjectHashMap;
  19. /**
  20. *
  21. */
  22. public class SkillTable
  23. {
  24. private final TIntObjectHashMap<L2Skill> _skills;
  25. private final TIntIntHashMap _skillMaxLevel;
  26. private final TIntArrayList _enchantable;
  27. public static SkillTable getInstance()
  28. {
  29. return SingletonHolder._instance;
  30. }
  31. private SkillTable()
  32. {
  33. _skills = new TIntObjectHashMap<L2Skill>();
  34. _skillMaxLevel = new TIntIntHashMap();
  35. _enchantable = new TIntArrayList();
  36. reload();
  37. }
  38. public void reload()
  39. {
  40. _skills.clear();
  41. SkillsEngine.getInstance().loadAllSkills(_skills);
  42. _skillMaxLevel.clear();
  43. for (final L2Skill skill : _skills.getValues(new L2Skill[_skills.size()]))
  44. {
  45. final int skillId = skill.getId();
  46. final int skillLvl = skill.getLevel();
  47. if (skillLvl > 99)
  48. {
  49. if (!_enchantable.contains(skillId))
  50. _enchantable.add(skillId);
  51. continue;
  52. }
  53. // only non-enchanted skills
  54. final int maxLvl = _skillMaxLevel.get(skillId);
  55. if (skillLvl > maxLvl)
  56. _skillMaxLevel.put(skillId, skillLvl);
  57. }
  58. //SkillTreeTable.getInstance().reload();
  59. // Sorting for binarySearch
  60. _enchantable.sort();
  61. // Reloading as well FrequentSkill enumeration values
  62. for (FrequentSkill sk : FrequentSkill.values())
  63. sk._skill = getInfo(sk._id, sk._level);
  64. }
  65. /**
  66. * Provides the skill hash
  67. *
  68. * @param skill
  69. * The L2Skill to be hashed
  70. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  71. */
  72. public static int getSkillHashCode(L2Skill skill)
  73. {
  74. return getSkillHashCode(skill.getId(), skill.getLevel());
  75. }
  76. /**
  77. * Centralized method for easier change of the hashing sys
  78. *
  79. * @param skillId
  80. * The Skill Id
  81. * @param skillLevel
  82. * The Skill Level
  83. * @return The Skill hash number
  84. */
  85. public static int getSkillHashCode(int skillId, int skillLevel)
  86. {
  87. return skillId * 1021 + skillLevel;
  88. }
  89. public final L2Skill getInfo(final int skillId, final int level)
  90. {
  91. final L2Skill result = _skills.get(getSkillHashCode(skillId, level));
  92. if (result != null)
  93. return result;
  94. // skill/level not found, fix for transformation scripts
  95. final int maxLvl = _skillMaxLevel.get(skillId);
  96. // requested level too high
  97. if (maxLvl > 0 && level > maxLvl)
  98. return _skills.get(getSkillHashCode(skillId, maxLvl));
  99. return null;
  100. }
  101. public final int getMaxLevel(final int skillId)
  102. {
  103. return _skillMaxLevel.get(skillId);
  104. }
  105. public final boolean isEnchantable(final int skillId)
  106. {
  107. return _enchantable.binarySearch(skillId) >= 0;
  108. }
  109. /**
  110. * Returns an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  111. */
  112. public L2Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  113. {
  114. L2Skill[] temp = new L2Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  115. int i = 0;
  116. temp[i++] = _skills.get(SkillTable.getSkillHashCode(246, 1));
  117. temp[i++] = _skills.get(SkillTable.getSkillHashCode(247, 1));
  118. if (addNoble)
  119. temp[i++] = _skills.get(SkillTable.getSkillHashCode(326, 1));
  120. if (hasCastle)
  121. {
  122. temp[i++] = _skills.get(SkillTable.getSkillHashCode(844, 1));
  123. temp[i++] = _skills.get(SkillTable.getSkillHashCode(845, 1));
  124. }
  125. return temp;
  126. }
  127. @SuppressWarnings("synthetic-access")
  128. private static class SingletonHolder
  129. {
  130. protected static final SkillTable _instance = new SkillTable();
  131. }
  132. /**
  133. * Enum to hold some important references to frequently used (hardcoded) skills in core
  134. *
  135. * @author DrHouse
  136. *
  137. */
  138. public static enum FrequentSkill
  139. {
  140. RAID_CURSE(4215, 1),
  141. RAID_CURSE2(4515, 1),
  142. SEAL_OF_RULER(246, 1),
  143. BUILD_HEADQUARTERS(247, 1),
  144. LUCKY(194, 1),
  145. DWARVEN_CRAFT(1321, 1),
  146. COMMON_CRAFT(1322, 1),
  147. WYVERN_BREATH(4289, 1),
  148. STRIDER_SIEGE_ASSAULT(325, 1),
  149. FAKE_PETRIFICATION(4616, 1),
  150. FIREWORK(5965, 1),
  151. LARGE_FIREWORK(2025, 1),
  152. BLESSING_OF_PROTECTION(5182, 1),
  153. ARENA_CP_RECOVERY(4380, 1),
  154. VOID_BURST(3630, 1),
  155. VOID_FLOW(3631, 1),
  156. THE_VICTOR_OF_WAR(5074, 1),
  157. THE_VANQUISHED_OF_WAR(5075, 1),
  158. SPECIAL_TREE_RECOVERY_BONUS(2139, 1);
  159. private final int _id;
  160. private final int _level;
  161. private L2Skill _skill = null;
  162. private FrequentSkill(int id, int level)
  163. {
  164. _id = id;
  165. _level = level;
  166. }
  167. public L2Skill getSkill()
  168. {
  169. return _skill;
  170. }
  171. }
  172. }