SkillTable.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 gnu.trove.list.array.TIntArrayList;
  15. import gnu.trove.map.hash.TIntIntHashMap;
  16. import gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.util.logging.Logger;
  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 Skill Tree as well.
  44. SkillTreesData.getInstance().load();
  45. }
  46. private void load()
  47. {
  48. _skills.clear();
  49. SkillsEngine.getInstance().loadAllSkills(_skills);
  50. _skillMaxLevel.clear();
  51. for (final L2Skill skill : _skills.values(new L2Skill[0]))
  52. {
  53. final int skillId = skill.getId();
  54. final int skillLvl = skill.getLevel();
  55. if (skillLvl > 99)
  56. {
  57. if (!_enchantable.contains(skillId))
  58. _enchantable.add(skillId);
  59. continue;
  60. }
  61. // only non-enchanted skills
  62. final int maxLvl = _skillMaxLevel.get(skillId);
  63. if (skillLvl > maxLvl)
  64. _skillMaxLevel.put(skillId, skillLvl);
  65. }
  66. // Sorting for binarySearch
  67. _enchantable.sort();
  68. // Reloading as well FrequentSkill enumeration values
  69. for (FrequentSkill sk : FrequentSkill.values())
  70. sk._skill = getInfo(sk._id, sk._level);
  71. }
  72. /**
  73. * Provides the skill hash
  74. *
  75. * @param skill
  76. * The L2Skill to be hashed
  77. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  78. */
  79. public static int getSkillHashCode(L2Skill skill)
  80. {
  81. return getSkillHashCode(skill.getId(), skill.getLevel());
  82. }
  83. /**
  84. * Centralized method for easier change of the hashing sys
  85. *
  86. * @param skillId
  87. * The Skill Id
  88. * @param skillLevel
  89. * The Skill Level
  90. * @return The Skill hash number
  91. */
  92. public static int getSkillHashCode(int skillId, int skillLevel)
  93. {
  94. return skillId * 1021 + skillLevel;
  95. }
  96. public final L2Skill getInfo(final int skillId, final int level)
  97. {
  98. final L2Skill result = _skills.get(getSkillHashCode(skillId, level));
  99. if (result != null)
  100. return result;
  101. // skill/level not found, fix for transformation scripts
  102. final int maxLvl = _skillMaxLevel.get(skillId);
  103. // requested level too high
  104. if (maxLvl > 0 && level > maxLvl)
  105. return _skills.get(getSkillHashCode(skillId, maxLvl));
  106. _log.warning("No skill info found for skill id " + skillId + " and skill level " + level + ".");
  107. return null;
  108. }
  109. public final int getMaxLevel(final int skillId)
  110. {
  111. return _skillMaxLevel.get(skillId);
  112. }
  113. public final boolean isEnchantable(final int skillId)
  114. {
  115. return _enchantable.binarySearch(skillId) >= 0;
  116. }
  117. /**
  118. * @param addNoble
  119. * @param hasCastle
  120. * @return 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. public static enum FrequentSkill
  148. {
  149. RAID_CURSE(4215, 1),
  150. RAID_CURSE2(4515, 1),
  151. SEAL_OF_RULER(246, 1),
  152. BUILD_HEADQUARTERS(247, 1),
  153. WYVERN_BREATH(4289, 1),
  154. STRIDER_SIEGE_ASSAULT(325, 1),
  155. FAKE_PETRIFICATION(4616, 1),
  156. FIREWORK(5965, 1),
  157. LARGE_FIREWORK(2025, 1),
  158. BLESSING_OF_PROTECTION(5182, 1),
  159. ARENA_CP_RECOVERY(4380, 1),
  160. VOID_BURST(3630, 1),
  161. VOID_FLOW(3631, 1),
  162. THE_VICTOR_OF_WAR(5074, 1),
  163. THE_VANQUISHED_OF_WAR(5075, 1),
  164. SPECIAL_TREE_RECOVERY_BONUS(2139, 1);
  165. private final int _id;
  166. private final int _level;
  167. private L2Skill _skill = null;
  168. private FrequentSkill(int id, int level)
  169. {
  170. _id = id;
  171. _level = level;
  172. }
  173. public L2Skill getSkill()
  174. {
  175. return _skill;
  176. }
  177. }
  178. }