SummonSkillsTable.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (C) 2004-2015 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.data.sql.impl;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  31. import com.l2jserver.gameserver.datatables.SkillData;
  32. import com.l2jserver.gameserver.model.actor.L2Summon;
  33. public class SummonSkillsTable
  34. {
  35. private static Logger LOGGER = 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 count = 0;
  45. try (Connection con = ConnectionFactory.getInstance().getConnection();
  46. Statement s = con.createStatement();
  47. ResultSet rs = s.executeQuery("SELECT templateId, minLvl, skillId, skillLvl FROM pets_skills"))
  48. {
  49. while (rs.next())
  50. {
  51. final int npcId = rs.getInt("templateId");
  52. Map<Integer, L2PetSkillLearn> skillTree = _skillTrees.get(npcId);
  53. if (skillTree == null)
  54. {
  55. skillTree = new HashMap<>();
  56. _skillTrees.put(npcId, skillTree);
  57. }
  58. int id = rs.getInt("skillId");
  59. int lvl = rs.getInt("skillLvl");
  60. skillTree.put(SkillData.getSkillHashCode(id, lvl + 1), new L2PetSkillLearn(id, lvl, rs.getInt("minLvl")));
  61. count++;
  62. }
  63. }
  64. catch (Exception e)
  65. {
  66. LOGGER.log(Level.SEVERE, getClass().getSimpleName() + ": Error while loading pet skill tree:", e);
  67. }
  68. LOGGER.info(getClass().getSimpleName() + ": Loaded " + count + " skills.");
  69. }
  70. public int getAvailableLevel(L2Summon cha, int skillId)
  71. {
  72. int lvl = 0;
  73. if (!_skillTrees.containsKey(cha.getId()))
  74. {
  75. LOGGER.warning(getClass().getSimpleName() + ": Pet id " + cha.getId() + " does not have any skills assigned.");
  76. return lvl;
  77. }
  78. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getId()).values();
  79. for (L2PetSkillLearn temp : skills)
  80. {
  81. if (temp.getId() != skillId)
  82. {
  83. continue;
  84. }
  85. if (temp.getLevel() == 0)
  86. {
  87. if (cha.getLevel() < 70)
  88. {
  89. lvl = (cha.getLevel() / 10);
  90. if (lvl <= 0)
  91. {
  92. lvl = 1;
  93. }
  94. }
  95. else
  96. {
  97. lvl = (7 + ((cha.getLevel() - 70) / 5));
  98. }
  99. // formula usable for skill that have 10 or more skill levels
  100. int maxLvl = SkillData.getInstance().getMaxLevel(temp.getId());
  101. if (lvl > maxLvl)
  102. {
  103. lvl = maxLvl;
  104. }
  105. break;
  106. }
  107. else if (temp.getMinLevel() <= cha.getLevel())
  108. {
  109. if (temp.getLevel() > lvl)
  110. {
  111. lvl = temp.getLevel();
  112. }
  113. }
  114. }
  115. return lvl;
  116. }
  117. public List<Integer> getAvailableSkills(L2Summon cha)
  118. {
  119. List<Integer> skillIds = new ArrayList<>();
  120. if (!_skillTrees.containsKey(cha.getId()))
  121. {
  122. LOGGER.warning(getClass().getSimpleName() + ": Pet id " + cha.getId() + " does not have any skills assigned.");
  123. return skillIds;
  124. }
  125. Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getId()).values();
  126. for (L2PetSkillLearn temp : skills)
  127. {
  128. if (skillIds.contains(temp.getId()))
  129. {
  130. continue;
  131. }
  132. skillIds.add(temp.getId());
  133. }
  134. return skillIds;
  135. }
  136. public static final class L2PetSkillLearn
  137. {
  138. private final int _id;
  139. private final int _level;
  140. private final int _minLevel;
  141. public L2PetSkillLearn(int id, int lvl, int minLvl)
  142. {
  143. _id = id;
  144. _level = lvl;
  145. _minLevel = minLvl;
  146. }
  147. public int getId()
  148. {
  149. return _id;
  150. }
  151. public int getLevel()
  152. {
  153. return _level;
  154. }
  155. public int getMinLevel()
  156. {
  157. return _minLevel;
  158. }
  159. }
  160. public static SummonSkillsTable getInstance()
  161. {
  162. return SingletonHolder._instance;
  163. }
  164. private static class SingletonHolder
  165. {
  166. protected static final SummonSkillsTable _instance = new SummonSkillsTable();
  167. }
  168. }