SkillData.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 = _skillMaxLevel.get(skillId);
  69. if (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 = _skillMaxLevel.get(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. return _skillMaxLevel.get(skillId);
  120. }
  121. /**
  122. * Verifies if the given skill ID correspond to an enchantable skill.
  123. * @param skillId the skill ID
  124. * @return {@code true} if the skill is enchantable, {@code false} otherwise
  125. */
  126. public boolean isEnchantable(int skillId)
  127. {
  128. return Collections.binarySearch(_enchantable, skillId) >= 0;
  129. }
  130. /**
  131. * @param addNoble
  132. * @param hasCastle
  133. * @return an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  134. */
  135. public Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  136. {
  137. Skill[] temp = new Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  138. int i = 0;
  139. temp[i++] = _skills.get(SkillData.getSkillHashCode(246, 1));
  140. temp[i++] = _skills.get(SkillData.getSkillHashCode(247, 1));
  141. if (addNoble)
  142. {
  143. temp[i++] = _skills.get(SkillData.getSkillHashCode(326, 1));
  144. }
  145. if (hasCastle)
  146. {
  147. temp[i++] = _skills.get(SkillData.getSkillHashCode(844, 1));
  148. temp[i++] = _skills.get(SkillData.getSkillHashCode(845, 1));
  149. }
  150. return temp;
  151. }
  152. /**
  153. * Enum to hold some important references to frequently used (hardcoded) skills in core
  154. * @author DrHouse
  155. */
  156. public static enum FrequentSkill
  157. {
  158. RAID_CURSE(4215, 1),
  159. RAID_CURSE2(4515, 1),
  160. SEAL_OF_RULER(246, 1),
  161. BUILD_HEADQUARTERS(247, 1),
  162. WYVERN_BREATH(4289, 1),
  163. STRIDER_SIEGE_ASSAULT(325, 1),
  164. FIREWORK(5965, 1),
  165. LARGE_FIREWORK(2025, 1),
  166. BLESSING_OF_PROTECTION(5182, 1),
  167. VOID_BURST(3630, 1),
  168. VOID_FLOW(3631, 1),
  169. THE_VICTOR_OF_WAR(5074, 1),
  170. THE_VANQUISHED_OF_WAR(5075, 1),
  171. SPECIAL_TREE_RECOVERY_BONUS(2139, 1),
  172. WEAPON_GRADE_PENALTY(6209, 1),
  173. ARMOR_GRADE_PENALTY(6213, 1);
  174. private final SkillHolder _holder;
  175. private FrequentSkill(int id, int level)
  176. {
  177. _holder = new SkillHolder(id, level);
  178. }
  179. public int getId()
  180. {
  181. return _holder.getSkillId();
  182. }
  183. public int getLevel()
  184. {
  185. return _holder.getSkillLvl();
  186. }
  187. public Skill getSkill()
  188. {
  189. return _holder.getSkill();
  190. }
  191. }
  192. public static SkillData getInstance()
  193. {
  194. return SingletonHolder._instance;
  195. }
  196. private static class SingletonHolder
  197. {
  198. protected static final SkillData _instance = new SkillData();
  199. }
  200. }