SummonSkillsTable.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. private SummonSkillsTable()
  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. {
  104. _log.warning("Pet id " + cha.getNpcId() + " does not have any skills assigned.");
  105. return lvl;
  106. }
  107. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getNpcId()).values();
  108. for (L2PetSkillLearn temp : skills)
  109. {
  110. if (temp.getId() != skillId)
  111. continue;
  112. if (temp.getLevel() == 0)
  113. {
  114. if (cha.getLevel() < 70)
  115. {
  116. lvl = (cha.getLevel() / 10);
  117. if (lvl <= 0)
  118. lvl = 1;
  119. }
  120. else
  121. lvl = (7 + ((cha.getLevel() - 70) / 5));
  122. // formula usable for skill that have 10 or more skill levels
  123. int maxLvl = SkillTable.getInstance().getMaxLevel(temp.getId());
  124. if (lvl > maxLvl)
  125. lvl = maxLvl;
  126. break;
  127. }
  128. else if (temp.getMinLevel() <= cha.getLevel())
  129. {
  130. if (temp.getLevel() > lvl)
  131. lvl = temp.getLevel();
  132. }
  133. }
  134. return lvl;
  135. }
  136. public FastList<Integer> getAvailableSkills(L2Summon cha)
  137. {
  138. FastList<Integer> skillIds = new FastList<Integer>();
  139. if (!_skillTrees.containsKey(cha.getNpcId()))
  140. {
  141. _log.warning("Pet id " + cha.getNpcId() + " does not have any skills assigned.");
  142. return skillIds;
  143. }
  144. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getNpcId()).values();
  145. for (L2PetSkillLearn temp : skills)
  146. {
  147. if (skillIds.contains(temp.getId()))
  148. continue;
  149. skillIds.add(temp.getId());
  150. }
  151. return skillIds;
  152. }
  153. public static final class L2PetSkillLearn
  154. {
  155. private final int _id;
  156. private final int _level;
  157. private final int _minLevel;
  158. public L2PetSkillLearn(int id, int lvl, int minLvl)
  159. {
  160. _id = id;
  161. _level = lvl;
  162. _minLevel = minLvl;
  163. }
  164. public int getId()
  165. {
  166. return _id;
  167. }
  168. public int getLevel()
  169. {
  170. return _level;
  171. }
  172. public int getMinLevel()
  173. {
  174. return _minLevel;
  175. }
  176. }
  177. @SuppressWarnings("synthetic-access")
  178. private static class SingletonHolder
  179. {
  180. protected static final SummonSkillsTable _instance = new SummonSkillsTable();
  181. }
  182. }