SkillTable.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.datatables;
  16. import gnu.trove.list.array.TIntArrayList;
  17. import gnu.trove.map.hash.TIntIntHashMap;
  18. import gnu.trove.map.hash.TIntObjectHashMap;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.gameserver.engines.DocumentEngine;
  21. import com.l2jserver.gameserver.model.skills.L2Skill;
  22. /**
  23. *
  24. */
  25. public class SkillTable
  26. {
  27. private static Logger _log = Logger.getLogger(SkillTable.class.getName());
  28. private final TIntObjectHashMap<L2Skill> _skills;
  29. private final TIntIntHashMap _skillMaxLevel;
  30. private final TIntArrayList _enchantable;
  31. public static SkillTable getInstance()
  32. {
  33. return SingletonHolder._instance;
  34. }
  35. private SkillTable()
  36. {
  37. _skills = new TIntObjectHashMap<L2Skill>();
  38. _skillMaxLevel = new TIntIntHashMap();
  39. _enchantable = new TIntArrayList();
  40. load();
  41. }
  42. public void reload()
  43. {
  44. load();
  45. //Reload Skill Tree as well.
  46. SkillTreesData.getInstance().load();
  47. }
  48. private void load()
  49. {
  50. _skills.clear();
  51. DocumentEngine.getInstance().loadAllSkills(_skills);
  52. _skillMaxLevel.clear();
  53. for (final L2Skill skill : _skills.values(new L2Skill[0]))
  54. {
  55. final int skillId = skill.getId();
  56. final int skillLvl = skill.getLevel();
  57. if (skillLvl > 99)
  58. {
  59. if (!_enchantable.contains(skillId))
  60. _enchantable.add(skillId);
  61. continue;
  62. }
  63. // only non-enchanted skills
  64. final int maxLvl = _skillMaxLevel.get(skillId);
  65. if (skillLvl > maxLvl)
  66. _skillMaxLevel.put(skillId, skillLvl);
  67. }
  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. * @param addNoble
  121. * @param hasCastle
  122. * @return an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  123. */
  124. public L2Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  125. {
  126. L2Skill[] temp = new L2Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  127. int i = 0;
  128. temp[i++] = _skills.get(SkillTable.getSkillHashCode(246, 1));
  129. temp[i++] = _skills.get(SkillTable.getSkillHashCode(247, 1));
  130. if (addNoble)
  131. temp[i++] = _skills.get(SkillTable.getSkillHashCode(326, 1));
  132. if (hasCastle)
  133. {
  134. temp[i++] = _skills.get(SkillTable.getSkillHashCode(844, 1));
  135. temp[i++] = _skills.get(SkillTable.getSkillHashCode(845, 1));
  136. }
  137. return temp;
  138. }
  139. @SuppressWarnings("synthetic-access")
  140. private static class SingletonHolder
  141. {
  142. protected static final SkillTable _instance = new SkillTable();
  143. }
  144. /**
  145. * Enum to hold some important references to frequently used (hardcoded) skills in core
  146. *
  147. * @author DrHouse
  148. */
  149. public static enum FrequentSkill
  150. {
  151. RAID_CURSE(4215, 1),
  152. RAID_CURSE2(4515, 1),
  153. SEAL_OF_RULER(246, 1),
  154. BUILD_HEADQUARTERS(247, 1),
  155. WYVERN_BREATH(4289, 1),
  156. STRIDER_SIEGE_ASSAULT(325, 1),
  157. FAKE_PETRIFICATION(4616, 1),
  158. FIREWORK(5965, 1),
  159. LARGE_FIREWORK(2025, 1),
  160. BLESSING_OF_PROTECTION(5182, 1),
  161. ARENA_CP_RECOVERY(4380, 1),
  162. VOID_BURST(3630, 1),
  163. VOID_FLOW(3631, 1),
  164. THE_VICTOR_OF_WAR(5074, 1),
  165. THE_VANQUISHED_OF_WAR(5075, 1),
  166. SPECIAL_TREE_RECOVERY_BONUS(2139, 1);
  167. private final int _id;
  168. private final int _level;
  169. private L2Skill _skill = null;
  170. private FrequentSkill(int id, int level)
  171. {
  172. _id = id;
  173. _level = level;
  174. }
  175. public L2Skill getSkill()
  176. {
  177. return _skill;
  178. }
  179. }
  180. }