2
0

SkillTable.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 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.getValues(new L2Skill[_skills.size()]))
  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. * Returns an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  119. */
  120. public L2Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  121. {
  122. L2Skill[] temp = new L2Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  123. int i = 0;
  124. temp[i++] = _skills.get(SkillTable.getSkillHashCode(246, 1));
  125. temp[i++] = _skills.get(SkillTable.getSkillHashCode(247, 1));
  126. if (addNoble)
  127. temp[i++] = _skills.get(SkillTable.getSkillHashCode(326, 1));
  128. if (hasCastle)
  129. {
  130. temp[i++] = _skills.get(SkillTable.getSkillHashCode(844, 1));
  131. temp[i++] = _skills.get(SkillTable.getSkillHashCode(845, 1));
  132. }
  133. return temp;
  134. }
  135. @SuppressWarnings("synthetic-access")
  136. private static class SingletonHolder
  137. {
  138. protected static final SkillTable _instance = new SkillTable();
  139. }
  140. /**
  141. * Enum to hold some important references to frequently used (hardcoded) skills in core
  142. *
  143. * @author DrHouse
  144. */
  145. public static enum FrequentSkill
  146. {
  147. RAID_CURSE(4215, 1),
  148. RAID_CURSE2(4515, 1),
  149. SEAL_OF_RULER(246, 1),
  150. BUILD_HEADQUARTERS(247, 1),
  151. WYVERN_BREATH(4289, 1),
  152. STRIDER_SIEGE_ASSAULT(325, 1),
  153. FAKE_PETRIFICATION(4616, 1),
  154. FIREWORK(5965, 1),
  155. LARGE_FIREWORK(2025, 1),
  156. BLESSING_OF_PROTECTION(5182, 1),
  157. ARENA_CP_RECOVERY(4380, 1),
  158. VOID_BURST(3630, 1),
  159. VOID_FLOW(3631, 1),
  160. THE_VICTOR_OF_WAR(5074, 1),
  161. THE_VANQUISHED_OF_WAR(5075, 1),
  162. SPECIAL_TREE_RECOVERY_BONUS(2139, 1);
  163. private final int _id;
  164. private final int _level;
  165. private L2Skill _skill = null;
  166. private FrequentSkill(int id, int level)
  167. {
  168. _id = id;
  169. _level = level;
  170. }
  171. public L2Skill getSkill()
  172. {
  173. return _skill;
  174. }
  175. }
  176. }