SkillTable.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.engines.DocumentEngine;
  22. import com.l2jserver.gameserver.model.holders.SkillHolder;
  23. import com.l2jserver.gameserver.model.skills.L2Skill;
  24. import gnu.trove.list.array.TIntArrayList;
  25. import gnu.trove.map.hash.TIntIntHashMap;
  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. {
  58. _enchantable.add(skillId);
  59. }
  60. continue;
  61. }
  62. // only non-enchanted skills
  63. final int maxLvl = _skillMaxLevel.get(skillId);
  64. if (skillLvl > maxLvl)
  65. {
  66. _skillMaxLevel.put(skillId, skillLvl);
  67. }
  68. }
  69. // Sorting for binarySearch
  70. _enchantable.sort();
  71. }
  72. /**
  73. * Provides the skill hash
  74. * @param skill The L2Skill to be hashed
  75. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  76. */
  77. public static int getSkillHashCode(L2Skill skill)
  78. {
  79. return getSkillHashCode(skill.getId(), skill.getLevel());
  80. }
  81. /**
  82. * Centralized method for easier change of the hashing sys
  83. * @param skillId The Skill Id
  84. * @param skillLevel The Skill Level
  85. * @return The Skill hash number
  86. */
  87. public static int getSkillHashCode(int skillId, int skillLevel)
  88. {
  89. return (skillId * 1021) + skillLevel;
  90. }
  91. public final L2Skill getInfo(final int skillId, final int level)
  92. {
  93. final L2Skill result = _skills.get(getSkillHashCode(skillId, level));
  94. if (result != null)
  95. {
  96. return result;
  97. }
  98. // skill/level not found, fix for transformation scripts
  99. final int maxLvl = _skillMaxLevel.get(skillId);
  100. // requested level too high
  101. if ((maxLvl > 0) && (level > maxLvl))
  102. {
  103. if (Config.DEBUG)
  104. {
  105. _log.log(Level.WARNING, getClass().getSimpleName() + ": call to unexisting skill level id: " + skillId + " requested level: " + level + " max level: " + maxLvl, new Throwable());
  106. }
  107. return _skills.get(getSkillHashCode(skillId, maxLvl));
  108. }
  109. _log.warning(getClass().getSimpleName() + ": No skill info found for skill id " + skillId + " and skill level " + level + ".");
  110. return null;
  111. }
  112. public final int getMaxLevel(final int skillId)
  113. {
  114. return _skillMaxLevel.get(skillId);
  115. }
  116. public final boolean isEnchantable(final int skillId)
  117. {
  118. return _enchantable.binarySearch(skillId) >= 0;
  119. }
  120. /**
  121. * @param addNoble
  122. * @param hasCastle
  123. * @return an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  124. */
  125. public L2Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  126. {
  127. L2Skill[] temp = new L2Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  128. int i = 0;
  129. temp[i++] = _skills.get(SkillTable.getSkillHashCode(246, 1));
  130. temp[i++] = _skills.get(SkillTable.getSkillHashCode(247, 1));
  131. if (addNoble)
  132. {
  133. temp[i++] = _skills.get(SkillTable.getSkillHashCode(326, 1));
  134. }
  135. if (hasCastle)
  136. {
  137. temp[i++] = _skills.get(SkillTable.getSkillHashCode(844, 1));
  138. temp[i++] = _skills.get(SkillTable.getSkillHashCode(845, 1));
  139. }
  140. return temp;
  141. }
  142. /**
  143. * Enum to hold some important references to frequently used (hardcoded) skills in core
  144. * @author DrHouse
  145. */
  146. public static enum FrequentSkill
  147. {
  148. RAID_CURSE(4215, 1),
  149. RAID_CURSE2(4515, 1),
  150. SEAL_OF_RULER(246, 1),
  151. BUILD_HEADQUARTERS(247, 1),
  152. WYVERN_BREATH(4289, 1),
  153. STRIDER_SIEGE_ASSAULT(325, 1),
  154. FAKE_PETRIFICATION(4616, 1),
  155. FIREWORK(5965, 1),
  156. LARGE_FIREWORK(2025, 1),
  157. BLESSING_OF_PROTECTION(5182, 1),
  158. ARENA_CP_RECOVERY(4380, 1),
  159. VOID_BURST(3630, 1),
  160. VOID_FLOW(3631, 1),
  161. THE_VICTOR_OF_WAR(5074, 1),
  162. THE_VANQUISHED_OF_WAR(5075, 1),
  163. SPECIAL_TREE_RECOVERY_BONUS(2139, 1),
  164. WEAPON_GRADE_PENALTY(6209, 1),
  165. ARMOR_GRADE_PENALTY(6213, 1);
  166. private final SkillHolder _holder;
  167. private FrequentSkill(int id, int level)
  168. {
  169. _holder = new SkillHolder(id, level);
  170. }
  171. public int getId()
  172. {
  173. return _holder.getSkillId();
  174. }
  175. public int getLevel()
  176. {
  177. return _holder.getSkillLvl();
  178. }
  179. public L2Skill getSkill()
  180. {
  181. return _holder.getSkill();
  182. }
  183. }
  184. public static SkillTable getInstance()
  185. {
  186. return SingletonHolder._instance;
  187. }
  188. private static class SingletonHolder
  189. {
  190. protected static final SkillTable _instance = new SkillTable();
  191. }
  192. }