2
0

SkillData.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. _skills.clear();
  51. DocumentEngine.getInstance().loadAllSkills(_skills);
  52. _skillMaxLevel.clear();
  53. for (Skill skill : _skills.values())
  54. {
  55. final int skillId = skill.getId();
  56. final int skillLvl = skill.getLevel();
  57. if (skillLvl > 99)
  58. {
  59. if (!_enchantable.contains(skillId))
  60. {
  61. _enchantable.add(skillId);
  62. }
  63. continue;
  64. }
  65. // only non-enchanted skills
  66. final int maxLvl = getMaxLevel(skillId);
  67. if (skillLvl > maxLvl)
  68. {
  69. _skillMaxLevel.put(skillId, skillLvl);
  70. }
  71. }
  72. }
  73. /**
  74. * Provides the skill hash
  75. * @param skill The L2Skill to be hashed
  76. * @return getSkillHashCode(skill.getId(), skill.getLevel())
  77. */
  78. public static int getSkillHashCode(Skill skill)
  79. {
  80. return getSkillHashCode(skill.getId(), skill.getLevel());
  81. }
  82. /**
  83. * Centralized method for easier change of the hashing sys
  84. * @param skillId The Skill Id
  85. * @param skillLevel The Skill Level
  86. * @return The Skill hash number
  87. */
  88. public static int getSkillHashCode(int skillId, int skillLevel)
  89. {
  90. return (skillId * 1021) + skillLevel;
  91. }
  92. public Skill getSkill(int skillId, int level)
  93. {
  94. final Skill result = _skills.get(getSkillHashCode(skillId, level));
  95. if (result != null)
  96. {
  97. return result;
  98. }
  99. // skill/level not found, fix for transformation scripts
  100. final int maxLvl = getMaxLevel(skillId);
  101. // requested level too high
  102. if ((maxLvl > 0) && (level > maxLvl))
  103. {
  104. if (Config.DEBUG)
  105. {
  106. _log.log(Level.WARNING, getClass().getSimpleName() + ": call to unexisting skill level id: " + skillId + " requested level: " + level + " max level: " + maxLvl, new Throwable());
  107. }
  108. return _skills.get(getSkillHashCode(skillId, maxLvl));
  109. }
  110. _log.warning(getClass().getSimpleName() + ": No skill info found for skill id " + skillId + " and skill level " + level + ".");
  111. return null;
  112. }
  113. public int getMaxLevel(int skillId)
  114. {
  115. final Integer maxLevel = _skillMaxLevel.get(skillId);
  116. return maxLevel != null ? maxLevel : 0;
  117. }
  118. /**
  119. * Verifies if the given skill ID correspond to an enchantable skill.
  120. * @param skillId the skill ID
  121. * @return {@code true} if the skill is enchantable, {@code false} otherwise
  122. */
  123. public boolean isEnchantable(int skillId)
  124. {
  125. return _enchantable.contains(skillId);
  126. }
  127. /**
  128. * @param addNoble
  129. * @param hasCastle
  130. * @return an array with siege skills. If addNoble == true, will add also Advanced headquarters.
  131. */
  132. public Skill[] getSiegeSkills(boolean addNoble, boolean hasCastle)
  133. {
  134. Skill[] temp = new Skill[2 + (addNoble ? 1 : 0) + (hasCastle ? 2 : 0)];
  135. int i = 0;
  136. temp[i++] = _skills.get(SkillData.getSkillHashCode(246, 1));
  137. temp[i++] = _skills.get(SkillData.getSkillHashCode(247, 1));
  138. if (addNoble)
  139. {
  140. temp[i++] = _skills.get(SkillData.getSkillHashCode(326, 1));
  141. }
  142. if (hasCastle)
  143. {
  144. temp[i++] = _skills.get(SkillData.getSkillHashCode(844, 1));
  145. temp[i++] = _skills.get(SkillData.getSkillHashCode(845, 1));
  146. }
  147. return temp;
  148. }
  149. public static SkillData getInstance()
  150. {
  151. return SingletonHolder._instance;
  152. }
  153. private static class SingletonHolder
  154. {
  155. protected static final SkillData _instance = new SkillData();
  156. }
  157. }