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