SkillData.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.Config;
  28. import com.l2jserver.gameserver.engines.DocumentEngine;
  29. import com.l2jserver.gameserver.model.holders.SkillHolder;
  30. import com.l2jserver.gameserver.model.skills.Skill;
  31. /**
  32. * Skill data.
  33. */
  34. public final class SkillData
  35. {
  36. private static Logger _log = Logger.getLogger(SkillData.class.getName());
  37. private final Map<Integer, Skill> _skills = new HashMap<>();
  38. private final Map<Integer, Integer> _skillMaxLevel = new HashMap<>();
  39. private final List<Integer> _enchantable = new ArrayList<>();
  40. protected SkillData()
  41. {
  42. load();
  43. }
  44. public void reload()
  45. {
  46. load();
  47. // Reload Skill Tree as well.
  48. SkillTreesData.getInstance().load();
  49. }
  50. private void load()
  51. {
  52. _skills.clear();
  53. DocumentEngine.getInstance().loadAllSkills(_skills);
  54. _skillMaxLevel.clear();
  55. for (Skill skill : _skills.values())
  56. {
  57. final int skillId = skill.getId();
  58. final int skillLvl = skill.getLevel();
  59. if (skillLvl > 99)
  60. {
  61. if (!_enchantable.contains(skillId))
  62. {
  63. _enchantable.add(skillId);
  64. }
  65. continue;
  66. }
  67. // only non-enchanted skills
  68. final int maxLvl = getMaxLevel(skillId);
  69. if ((maxLvl > 0) || (skillLvl > maxLvl))
  70. {
  71. _skillMaxLevel.put(skillId, skillLvl);
  72. }
  73. }
  74. // Sorting for binary-search.
  75. Collections.sort(_enchantable);
  76. }
  77. /**
  78. * Provides the skill hash
  79. * @param skill The L2Skill to be hashed
  80. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  81. */
  82. public static int getSkillHashCode(Skill skill)
  83. {
  84. return getSkillHashCode(skill.getId(), skill.getLevel());
  85. }
  86. /**
  87. * Centralized method for easier change of the hashing sys
  88. * @param skillId The Skill Id
  89. * @param skillLevel 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 Skill getSkill(int skillId, int level)
  97. {
  98. final Skill result = _skills.get(getSkillHashCode(skillId, level));
  99. if (result != null)
  100. {
  101. return result;
  102. }
  103. // skill/level not found, fix for transformation scripts
  104. final int maxLvl = getMaxLevel(skillId);
  105. // requested level too high
  106. if ((maxLvl > 0) && (level > maxLvl))
  107. {
  108. if (Config.DEBUG)
  109. {
  110. _log.log(Level.WARNING, getClass().getSimpleName() + ": call to unexisting skill level id: " + skillId + " requested level: " + level + " max level: " + maxLvl, new Throwable());
  111. }
  112. return _skills.get(getSkillHashCode(skillId, maxLvl));
  113. }
  114. _log.warning(getClass().getSimpleName() + ": No skill info found for skill id " + skillId + " and skill level " + level + ".");
  115. return null;
  116. }
  117. public int getMaxLevel(int skillId)
  118. {
  119. final Integer maxLevel = _skillMaxLevel.get(skillId);
  120. return maxLevel != null ? maxLevel : 0;
  121. }
  122. /**
  123. * Verifies if the given skill ID correspond to an enchantable skill.
  124. * @param skillId the skill ID
  125. * @return {@code true} if the skill is enchantable, {@code false} otherwise
  126. */
  127. public boolean isEnchantable(int skillId)
  128. {
  129. return Collections.binarySearch(_enchantable, skillId) >= 0;
  130. }
  131. /**
  132. * @param addNoble
  133. * @param hasCastle
  134. * @return an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  135. */
  136. public Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  137. {
  138. Skill[] temp = new Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  139. int i = 0;
  140. temp[i++] = _skills.get(SkillData.getSkillHashCode(246, 1));
  141. temp[i++] = _skills.get(SkillData.getSkillHashCode(247, 1));
  142. if (addNoble)
  143. {
  144. temp[i++] = _skills.get(SkillData.getSkillHashCode(326, 1));
  145. }
  146. if (hasCastle)
  147. {
  148. temp[i++] = _skills.get(SkillData.getSkillHashCode(844, 1));
  149. temp[i++] = _skills.get(SkillData.getSkillHashCode(845, 1));
  150. }
  151. return temp;
  152. }
  153. /**
  154. * Enum to hold some important references to frequently used (hardcoded) skills in core
  155. * @author DrHouse
  156. */
  157. public static enum FrequentSkill
  158. {
  159. RAID_CURSE(4215, 1),
  160. RAID_CURSE2(4515, 1),
  161. SEAL_OF_RULER(246, 1),
  162. BUILD_HEADQUARTERS(247, 1),
  163. WYVERN_BREATH(4289, 1),
  164. STRIDER_SIEGE_ASSAULT(325, 1),
  165. FIREWORK(5965, 1),
  166. LARGE_FIREWORK(2025, 1),
  167. BLESSING_OF_PROTECTION(5182, 1),
  168. VOID_BURST(3630, 1),
  169. VOID_FLOW(3631, 1),
  170. THE_VICTOR_OF_WAR(5074, 1),
  171. THE_VANQUISHED_OF_WAR(5075, 1),
  172. SPECIAL_TREE_RECOVERY_BONUS(2139, 1),
  173. WEAPON_GRADE_PENALTY(6209, 1),
  174. ARMOR_GRADE_PENALTY(6213, 1);
  175. private final SkillHolder _holder;
  176. private FrequentSkill(int id, int level)
  177. {
  178. _holder = new SkillHolder(id, level);
  179. }
  180. public int getId()
  181. {
  182. return _holder.getSkillId();
  183. }
  184. public int getLevel()
  185. {
  186. return _holder.getSkillLvl();
  187. }
  188. public Skill getSkill()
  189. {
  190. return _holder.getSkill();
  191. }
  192. }
  193. public static SkillData getInstance()
  194. {
  195. return SingletonHolder._instance;
  196. }
  197. private static class SingletonHolder
  198. {
  199. protected static final SkillData _instance = new SkillData();
  200. }
  201. }