SkillTable.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 java.util.HashMap;
  19. import java.util.Map;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.engines.DocumentEngine;
  24. import com.l2jserver.gameserver.model.holders.SkillHolder;
  25. import com.l2jserver.gameserver.model.skills.L2Skill;
  26. /**
  27. *
  28. */
  29. public class SkillTable
  30. {
  31. private static Logger _log = Logger.getLogger(SkillTable.class.getName());
  32. private final Map<Integer, L2Skill> _skills = new HashMap<>();
  33. private final TIntIntHashMap _skillMaxLevel = new TIntIntHashMap();
  34. private final TIntArrayList _enchantable = new TIntArrayList();
  35. protected SkillTable()
  36. {
  37. load();
  38. }
  39. public void reload()
  40. {
  41. load();
  42. //Reload Skill Tree as well.
  43. SkillTreesData.getInstance().load();
  44. }
  45. private void load()
  46. {
  47. _skills.clear();
  48. DocumentEngine.getInstance().loadAllSkills(_skills);
  49. _skillMaxLevel.clear();
  50. for (final L2Skill skill : _skills.values())
  51. {
  52. final int skillId = skill.getId();
  53. final int skillLvl = skill.getLevel();
  54. if (skillLvl > 99)
  55. {
  56. if (!_enchantable.contains(skillId))
  57. _enchantable.add(skillId);
  58. continue;
  59. }
  60. // only non-enchanted skills
  61. final int maxLvl = _skillMaxLevel.get(skillId);
  62. if (skillLvl > maxLvl)
  63. _skillMaxLevel.put(skillId, skillLvl);
  64. }
  65. // Sorting for binarySearch
  66. _enchantable.sort();
  67. }
  68. /**
  69. * Provides the skill hash
  70. *
  71. * @param skill
  72. * The L2Skill to be hashed
  73. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  74. */
  75. public static int getSkillHashCode(L2Skill skill)
  76. {
  77. return getSkillHashCode(skill.getId(), skill.getLevel());
  78. }
  79. /**
  80. * Centralized method for easier change of the hashing sys
  81. *
  82. * @param skillId
  83. * The Skill Id
  84. * @param skillLevel
  85. * The Skill Level
  86. * @return The Skill hash number
  87. */
  88. public static int getSkillHashCode(int skillId, int skillLevel)
  89. {
  90. return skillId * 1021 + skillLevel;
  91. }
  92. public final L2Skill getInfo(final int skillId, final int level)
  93. {
  94. final L2Skill result = _skills.get(getSkillHashCode(skillId, level));
  95. if (result != null)
  96. return result;
  97. // skill/level not found, fix for transformation scripts
  98. final int maxLvl = _skillMaxLevel.get(skillId);
  99. // requested level too high
  100. if (maxLvl > 0 && level > maxLvl)
  101. {
  102. if (Config.DEBUG)
  103. {
  104. _log.log(Level.WARNING, getClass().getSimpleName() + ": call to unexisting skill level id: " + skillId + " requested level: " + level + " max level: " + maxLvl, new Throwable());
  105. }
  106. return _skills.get(getSkillHashCode(skillId, maxLvl));
  107. }
  108. _log.warning(getClass().getSimpleName() + ": 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. /**
  140. * Enum to hold some important references to frequently used (hardcoded) skills in core
  141. *
  142. * @author DrHouse
  143. */
  144. public static enum FrequentSkill
  145. {
  146. RAID_CURSE(4215, 1),
  147. RAID_CURSE2(4515, 1),
  148. SEAL_OF_RULER(246, 1),
  149. BUILD_HEADQUARTERS(247, 1),
  150. WYVERN_BREATH(4289, 1),
  151. STRIDER_SIEGE_ASSAULT(325, 1),
  152. FAKE_PETRIFICATION(4616, 1),
  153. FIREWORK(5965, 1),
  154. LARGE_FIREWORK(2025, 1),
  155. BLESSING_OF_PROTECTION(5182, 1),
  156. ARENA_CP_RECOVERY(4380, 1),
  157. VOID_BURST(3630, 1),
  158. VOID_FLOW(3631, 1),
  159. THE_VICTOR_OF_WAR(5074, 1),
  160. THE_VANQUISHED_OF_WAR(5075, 1),
  161. SPECIAL_TREE_RECOVERY_BONUS(2139, 1);
  162. private final SkillHolder _holder;
  163. private FrequentSkill(int id, int level)
  164. {
  165. _holder = new SkillHolder(id, level);
  166. }
  167. public L2Skill getSkill()
  168. {
  169. return _holder.getSkill();
  170. }
  171. }
  172. public static SkillTable getInstance()
  173. {
  174. return SingletonHolder._instance;
  175. }
  176. private static class SingletonHolder
  177. {
  178. protected static final SkillTable _instance = new SkillTable();
  179. }
  180. }