SkillData.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.HashMap;
  21. import java.util.HashSet;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.Config;
  27. import com.l2jserver.gameserver.engines.DocumentEngine;
  28. import com.l2jserver.gameserver.model.skills.Skill;
  29. /**
  30. * Skill data.
  31. */
  32. public final class SkillData
  33. {
  34. private static Logger _log = Logger.getLogger(SkillData.class.getName());
  35. private final Map<Integer, Skill> _skills = new HashMap<>();
  36. private final Map<Integer, Integer> _skillMaxLevel = new HashMap<>();
  37. private final Set<Integer> _enchantable = new HashSet<>();
  38. protected SkillData()
  39. {
  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. final Map<Integer, Skill> _temp = new HashMap<>();
  51. DocumentEngine.getInstance().loadAllSkills(_temp);
  52. _skills.clear();
  53. _skills.putAll(_temp);
  54. _skillMaxLevel.clear();
  55. _enchantable.clear();
  56. for (Skill skill : _skills.values())
  57. {
  58. final int skillId = skill.getId();
  59. final int skillLvl = skill.getLevel();
  60. if (skillLvl > 99)
  61. {
  62. if (!_enchantable.contains(skillId))
  63. {
  64. _enchantable.add(skillId);
  65. }
  66. continue;
  67. }
  68. // only non-enchanted skills
  69. final int maxLvl = getMaxLevel(skillId);
  70. if (skillLvl > maxLvl)
  71. {
  72. _skillMaxLevel.put(skillId, skillLvl);
  73. }
  74. }
  75. }
  76. /**
  77. * Provides the skill hash
  78. * @param skill The L2Skill to be hashed
  79. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  80. */
  81. public static int getSkillHashCode(Skill skill)
  82. {
  83. return getSkillHashCode(skill.getId(), skill.getLevel());
  84. }
  85. /**
  86. * Centralized method for easier change of the hashing sys
  87. * @param skillId The Skill Id
  88. * @param skillLevel The Skill Level
  89. * @return The Skill hash number
  90. */
  91. public static int getSkillHashCode(int skillId, int skillLevel)
  92. {
  93. return (skillId * 1021) + skillLevel;
  94. }
  95. public Skill getSkill(int skillId, int level)
  96. {
  97. final Skill result = _skills.get(getSkillHashCode(skillId, level));
  98. if (result != null)
  99. {
  100. return result;
  101. }
  102. // skill/level not found, fix for transformation scripts
  103. final int maxLvl = getMaxLevel(skillId);
  104. // requested level too high
  105. if ((maxLvl > 0) && (level > maxLvl))
  106. {
  107. if (Config.DEBUG)
  108. {
  109. _log.log(Level.WARNING, getClass().getSimpleName() + ": call to unexisting skill level id: " + skillId + " requested level: " + level + " max level: " + maxLvl, new Throwable());
  110. }
  111. return _skills.get(getSkillHashCode(skillId, maxLvl));
  112. }
  113. _log.warning(getClass().getSimpleName() + ": No skill info found for skill id " + skillId + " and skill level " + level + ".");
  114. return null;
  115. }
  116. public int getMaxLevel(int skillId)
  117. {
  118. final Integer maxLevel = _skillMaxLevel.get(skillId);
  119. return maxLevel != null ? maxLevel : 0;
  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 _enchantable.contains(skillId);
  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. public static SkillData getInstance()
  153. {
  154. return SingletonHolder._instance;
  155. }
  156. private static class SingletonHolder
  157. {
  158. protected static final SkillData _instance = new SkillData();
  159. }
  160. }