2
0

SummonSkillsTable.java 5.1 KB

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