2
0

SummonSkillsTable.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.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. }
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while creating pet skill tree (Pet ID " + npcId + "): " + e.getMessage(), e);
  76. }
  77. _log.info(getClass().getSimpleName() + ": Loaded " + count + " skills.");
  78. }
  79. public int getAvailableLevel(L2Summon cha, int skillId)
  80. {
  81. int lvl = 0;
  82. if (!_skillTrees.containsKey(cha.getId()))
  83. {
  84. _log.warning(getClass().getSimpleName() + ": Pet id " + cha.getId() + " does not have any skills assigned.");
  85. return lvl;
  86. }
  87. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getId()).values();
  88. for (L2PetSkillLearn temp : skills)
  89. {
  90. if (temp.getId() != skillId)
  91. {
  92. continue;
  93. }
  94. if (temp.getLevel() == 0)
  95. {
  96. if (cha.getLevel() < 70)
  97. {
  98. lvl = (cha.getLevel() / 10);
  99. if (lvl <= 0)
  100. {
  101. lvl = 1;
  102. }
  103. }
  104. else
  105. {
  106. lvl = (7 + ((cha.getLevel() - 70) / 5));
  107. }
  108. // formula usable for skill that have 10 or more skill levels
  109. int maxLvl = SkillTable.getInstance().getMaxLevel(temp.getId());
  110. if (lvl > maxLvl)
  111. {
  112. lvl = maxLvl;
  113. }
  114. break;
  115. }
  116. else if (temp.getMinLevel() <= cha.getLevel())
  117. {
  118. if (temp.getLevel() > lvl)
  119. {
  120. lvl = temp.getLevel();
  121. }
  122. }
  123. }
  124. return lvl;
  125. }
  126. public List<Integer> getAvailableSkills(L2Summon cha)
  127. {
  128. List<Integer> skillIds = new ArrayList<>();
  129. if (!_skillTrees.containsKey(cha.getId()))
  130. {
  131. _log.warning(getClass().getSimpleName() + ": Pet id " + cha.getId() + " does not have any skills assigned.");
  132. return skillIds;
  133. }
  134. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getId()).values();
  135. for (L2PetSkillLearn temp : skills)
  136. {
  137. if (skillIds.contains(temp.getId()))
  138. {
  139. continue;
  140. }
  141. skillIds.add(temp.getId());
  142. }
  143. return skillIds;
  144. }
  145. public static final class L2PetSkillLearn
  146. {
  147. private final int _id;
  148. private final int _level;
  149. private final int _minLevel;
  150. public L2PetSkillLearn(int id, int lvl, int minLvl)
  151. {
  152. _id = id;
  153. _level = lvl;
  154. _minLevel = minLvl;
  155. }
  156. public int getId()
  157. {
  158. return _id;
  159. }
  160. public int getLevel()
  161. {
  162. return _level;
  163. }
  164. public int getMinLevel()
  165. {
  166. return _minLevel;
  167. }
  168. }
  169. public static SummonSkillsTable getInstance()
  170. {
  171. return SingletonHolder._instance;
  172. }
  173. private static class SingletonHolder
  174. {
  175. protected static final SummonSkillsTable _instance = new SummonSkillsTable();
  176. }
  177. }