2
0

PetSkillsTable.java 5.3 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 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. try
  97. {
  98. con.close();
  99. }
  100. catch (Exception e)
  101. {
  102. }
  103. }
  104. }
  105. public int getAvailableLevel(L2Summon cha, int skillId)
  106. {
  107. int lvl = 0;
  108. if (!_skillTrees.containsKey(cha.getNpcId()))
  109. return lvl;
  110. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getNpcId()).values();
  111. for (L2PetSkillLearn temp : skills)
  112. {
  113. if (temp.getId() != skillId)
  114. continue;
  115. if (temp.getLevel() == 0)
  116. {
  117. if (cha.getLevel() < 70)
  118. {
  119. lvl = (cha.getLevel() / 10);
  120. if (lvl <= 0)
  121. lvl = 1;
  122. }
  123. else
  124. lvl = (7 + ((cha.getLevel() - 70) / 5));
  125. // formula usable for skill that have 10 or more skill levels
  126. int maxLvl = SkillTable.getInstance().getMaxLevel(temp.getId());
  127. if (lvl > maxLvl)
  128. lvl = maxLvl;
  129. break;
  130. }
  131. else if (temp.getMinLevel() <= cha.getLevel())
  132. {
  133. if (temp.getLevel() > lvl)
  134. lvl = temp.getLevel();
  135. }
  136. }
  137. return lvl;
  138. }
  139. public FastList<Integer> getAvailableSkills(L2Summon cha)
  140. {
  141. FastList<Integer> skillIds = new FastList<Integer>();
  142. if (!_skillTrees.containsKey(cha.getNpcId()))
  143. return null;
  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 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 PetSkillsTable _instance = new PetSkillsTable();
  181. }
  182. }