SummonSkillsTable.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.datatables;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.Statement;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  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.L2DatabaseFactory;
  28. import com.l2jserver.gameserver.model.actor.L2Summon;
  29. public class SummonSkillsTable
  30. {
  31. private static Logger _log = Logger.getLogger(SummonSkillsTable.class.getName());
  32. private final Map<Integer, Map<Integer, L2PetSkillLearn>> _skillTrees = new HashMap<>();
  33. protected SummonSkillsTable()
  34. {
  35. load();
  36. }
  37. public void load()
  38. {
  39. _skillTrees.clear();
  40. int npcId = 0;
  41. int count = 0;
  42. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  43. Statement s = con.createStatement();
  44. ResultSet rs = s.executeQuery("SELECT id FROM npc WHERE type IN ('L2Pet','L2BabyPet','L2SiegeSummon') ORDER BY id"))
  45. {
  46. Map<Integer, L2PetSkillLearn> map;
  47. try (PreparedStatement ps2 = con.prepareStatement("SELECT minLvl, skillId, skillLvl FROM pets_skills where templateId=? ORDER BY skillId, skillLvl"))
  48. {
  49. while (rs.next())
  50. {
  51. map = new HashMap<>();
  52. npcId = rs.getInt("id");
  53. ps2.setInt(1, npcId);
  54. try (ResultSet skilltree = ps2.executeQuery())
  55. {
  56. while (skilltree.next())
  57. {
  58. int id = skilltree.getInt("skillId");
  59. int lvl = skilltree.getInt("skillLvl");
  60. map.put(SkillTable.getSkillHashCode(id, lvl + 1), new L2PetSkillLearn(id, lvl, skilltree.getInt("minLvl")));
  61. }
  62. _skillTrees.put(npcId, map);
  63. }
  64. ps2.clearParameters();
  65. count += map.size();
  66. _log.fine(getClass().getSimpleName() + ": skill tree for pet " + npcId + " has " + map.size() + " skills");
  67. }
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while creating pet skill tree (Pet ID " + npcId + "): " + e.getMessage(), e);
  73. }
  74. _log.info(getClass().getSimpleName() + ": Loaded " + count + " skills.");
  75. }
  76. public int getAvailableLevel(L2Summon cha, int skillId)
  77. {
  78. int lvl = 0;
  79. if (!_skillTrees.containsKey(cha.getNpcId()))
  80. {
  81. _log.warning(getClass().getSimpleName() + ": Pet id " + cha.getNpcId() + " does not have any skills assigned.");
  82. return lvl;
  83. }
  84. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getNpcId()).values();
  85. for (L2PetSkillLearn temp : skills)
  86. {
  87. if (temp.getId() != skillId)
  88. {
  89. continue;
  90. }
  91. if (temp.getLevel() == 0)
  92. {
  93. if (cha.getLevel() < 70)
  94. {
  95. lvl = (cha.getLevel() / 10);
  96. if (lvl <= 0)
  97. {
  98. lvl = 1;
  99. }
  100. }
  101. else
  102. {
  103. lvl = (7 + ((cha.getLevel() - 70) / 5));
  104. }
  105. // formula usable for skill that have 10 or more skill levels
  106. int maxLvl = SkillTable.getInstance().getMaxLevel(temp.getId());
  107. if (lvl > maxLvl)
  108. {
  109. lvl = maxLvl;
  110. }
  111. break;
  112. }
  113. else if (temp.getMinLevel() <= cha.getLevel())
  114. {
  115. if (temp.getLevel() > lvl)
  116. {
  117. lvl = temp.getLevel();
  118. }
  119. }
  120. }
  121. return lvl;
  122. }
  123. public List<Integer> getAvailableSkills(L2Summon cha)
  124. {
  125. List<Integer> skillIds = new ArrayList<>();
  126. if (!_skillTrees.containsKey(cha.getNpcId()))
  127. {
  128. _log.warning(getClass().getSimpleName() + ": Pet id " + cha.getNpcId() + " does not have any skills assigned.");
  129. return skillIds;
  130. }
  131. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getNpcId()).values();
  132. for (L2PetSkillLearn temp : skills)
  133. {
  134. if (skillIds.contains(temp.getId()))
  135. {
  136. continue;
  137. }
  138. skillIds.add(temp.getId());
  139. }
  140. return skillIds;
  141. }
  142. public static final class L2PetSkillLearn
  143. {
  144. private final int _id;
  145. private final int _level;
  146. private final int _minLevel;
  147. public L2PetSkillLearn(int id, int lvl, int minLvl)
  148. {
  149. _id = id;
  150. _level = lvl;
  151. _minLevel = minLvl;
  152. }
  153. public int getId()
  154. {
  155. return _id;
  156. }
  157. public int getLevel()
  158. {
  159. return _level;
  160. }
  161. public int getMinLevel()
  162. {
  163. return _minLevel;
  164. }
  165. }
  166. public static SummonSkillsTable getInstance()
  167. {
  168. return SingletonHolder._instance;
  169. }
  170. private static class SingletonHolder
  171. {
  172. protected static final SummonSkillsTable _instance = new SummonSkillsTable();
  173. }
  174. }