SkillData.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2004-2015 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.data.xml.impl.SkillTreesData;
  28. import com.l2jserver.gameserver.engines.DocumentEngine;
  29. import com.l2jserver.gameserver.model.skills.Skill;
  30. /**
  31. * Skill data.
  32. */
  33. public final class SkillData
  34. {
  35. private static Logger LOGGER = Logger.getLogger(SkillData.class.getName());
  36. private final Map<Integer, Skill> _skills = new HashMap<>();
  37. private final Map<Integer, Integer> _skillMaxLevel = new HashMap<>();
  38. private final Set<Integer> _enchantable = new HashSet<>();
  39. protected SkillData()
  40. {
  41. load();
  42. }
  43. public void reload()
  44. {
  45. load();
  46. // Reload Skill Tree as well.
  47. SkillTreesData.getInstance().load();
  48. }
  49. private void load()
  50. {
  51. final Map<Integer, Skill> _temp = new HashMap<>();
  52. DocumentEngine.getInstance().loadAllSkills(_temp);
  53. _skills.clear();
  54. _skills.putAll(_temp);
  55. _skillMaxLevel.clear();
  56. _enchantable.clear();
  57. for (Skill skill : _skills.values())
  58. {
  59. final int skillId = skill.getId();
  60. final int skillLvl = skill.getLevel();
  61. if (skillLvl > 99)
  62. {
  63. if (!_enchantable.contains(skillId))
  64. {
  65. _enchantable.add(skillId);
  66. }
  67. continue;
  68. }
  69. // only non-enchanted skills
  70. final int maxLvl = getMaxLevel(skillId);
  71. if (skillLvl > maxLvl)
  72. {
  73. _skillMaxLevel.put(skillId, skillLvl);
  74. }
  75. }
  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. LOGGER.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. LOGGER.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 _enchantable.contains(skillId);
  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. public static SkillData getInstance()
  154. {
  155. return SingletonHolder._instance;
  156. }
  157. private static class SingletonHolder
  158. {
  159. protected static final SkillData _instance = new SkillData();
  160. }
  161. }