2
0

SummonSkillsTable.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import com.l2jserver.L2DatabaseFactory;
  31. import com.l2jserver.gameserver.model.actor.L2Summon;
  32. public class SummonSkillsTable
  33. {
  34. private static Logger _log = Logger.getLogger(SummonSkillsTable.class.getName());
  35. private final Map<Integer, Map<Integer, L2PetSkillLearn>> _skillTrees = new HashMap<>();
  36. protected SummonSkillsTable()
  37. {
  38. load();
  39. }
  40. public void load()
  41. {
  42. _skillTrees.clear();
  43. int count = 0;
  44. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  45. Statement s = con.createStatement();
  46. ResultSet rs = s.executeQuery("SELECT templateId, minLvl, skillId, skillLvl FROM pets_skills"))
  47. {
  48. while (rs.next())
  49. {
  50. final int npcId = rs.getInt("templateId");
  51. Map<Integer, L2PetSkillLearn> skillTree = _skillTrees.get(npcId);
  52. if (skillTree == null)
  53. {
  54. skillTree = new HashMap<>();
  55. _skillTrees.put(npcId, skillTree);
  56. }
  57. int id = rs.getInt("skillId");
  58. int lvl = rs.getInt("skillLvl");
  59. skillTree.put(SkillData.getSkillHashCode(id, lvl + 1), new L2PetSkillLearn(id, lvl, rs.getInt("minLvl")));
  60. count++;
  61. }
  62. }
  63. catch (Exception e)
  64. {
  65. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while loading pet skill tree:", e);
  66. }
  67. _log.info(getClass().getSimpleName() + ": Loaded " + count + " skills.");
  68. }
  69. public int getAvailableLevel(L2Summon cha, int skillId)
  70. {
  71. int lvl = 0;
  72. if (!_skillTrees.containsKey(cha.getId()))
  73. {
  74. _log.warning(getClass().getSimpleName() + ": Pet id " + cha.getId() + " does not have any skills assigned.");
  75. return lvl;
  76. }
  77. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getId()).values();
  78. for (L2PetSkillLearn temp : skills)
  79. {
  80. if (temp.getId() != skillId)
  81. {
  82. continue;
  83. }
  84. if (temp.getLevel() == 0)
  85. {
  86. if (cha.getLevel() < 70)
  87. {
  88. lvl = (cha.getLevel() / 10);
  89. if (lvl <= 0)
  90. {
  91. lvl = 1;
  92. }
  93. }
  94. else
  95. {
  96. lvl = (7 + ((cha.getLevel() - 70) / 5));
  97. }
  98. // formula usable for skill that have 10 or more skill levels
  99. int maxLvl = SkillData.getInstance().getMaxLevel(temp.getId());
  100. if (lvl > maxLvl)
  101. {
  102. lvl = maxLvl;
  103. }
  104. break;
  105. }
  106. else if (temp.getMinLevel() <= cha.getLevel())
  107. {
  108. if (temp.getLevel() > lvl)
  109. {
  110. lvl = temp.getLevel();
  111. }
  112. }
  113. }
  114. return lvl;
  115. }
  116. public List<Integer> getAvailableSkills(L2Summon cha)
  117. {
  118. List<Integer> skillIds = new ArrayList<>();
  119. if (!_skillTrees.containsKey(cha.getId()))
  120. {
  121. _log.warning(getClass().getSimpleName() + ": Pet id " + cha.getId() + " does not have any skills assigned.");
  122. return skillIds;
  123. }
  124. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getId()).values();
  125. for (L2PetSkillLearn temp : skills)
  126. {
  127. if (skillIds.contains(temp.getId()))
  128. {
  129. continue;
  130. }
  131. skillIds.add(temp.getId());
  132. }
  133. return skillIds;
  134. }
  135. public static final class L2PetSkillLearn
  136. {
  137. private final int _id;
  138. private final int _level;
  139. private final int _minLevel;
  140. public L2PetSkillLearn(int id, int lvl, int minLvl)
  141. {
  142. _id = id;
  143. _level = lvl;
  144. _minLevel = minLvl;
  145. }
  146. public int getId()
  147. {
  148. return _id;
  149. }
  150. public int getLevel()
  151. {
  152. return _level;
  153. }
  154. public int getMinLevel()
  155. {
  156. return _minLevel;
  157. }
  158. }
  159. public static SummonSkillsTable getInstance()
  160. {
  161. return SingletonHolder._instance;
  162. }
  163. private static class SingletonHolder
  164. {
  165. protected static final SummonSkillsTable _instance = new SummonSkillsTable();
  166. }
  167. }