PetSkillsTable.java 5.2 KB

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