Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2004-2013 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.datatables;
  20.  
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.Statement;
  25. import java.util.ArrayList;
  26. import java.util.Collection;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;
  32.  
  33. import com.l2jserver.L2DatabaseFactory;
  34. import com.l2jserver.gameserver.model.actor.L2Summon;
  35.  
  36. public class SummonSkillsTable
  37. {
  38.     private static Logger _log = Logger.getLogger(SummonSkillsTable.class.getName());
  39.     private final Map<Integer, Map<Integer, L2PetSkillLearn>> _skillTrees = new HashMap<>();
  40.    
  41.     protected SummonSkillsTable()
  42.     {
  43.         load();
  44.     }
  45.    
  46.     public void load()
  47.     {
  48.         _skillTrees.clear();
  49.         int npcId = 0;
  50.         int count = 0;
  51.         try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  52.             Statement s = con.createStatement();
  53.             ResultSet rs = s.executeQuery("SELECT id FROM npc WHERE type IN ('L2Pet','L2BabyPet','L2SiegeSummon') ORDER BY id"))
  54.         {
  55.             Map<Integer, L2PetSkillLearn> map;
  56.             try (PreparedStatement ps2 = con.prepareStatement("SELECT minLvl, skillId, skillLvl FROM pets_skills where templateId=? ORDER BY skillId, skillLvl"))
  57.             {
  58.                 while (rs.next())
  59.                 {
  60.                     map = new HashMap<>();
  61.                     npcId = rs.getInt("id");
  62.                     ps2.setInt(1, npcId);
  63.                     try (ResultSet skilltree = ps2.executeQuery())
  64.                     {
  65.                         while (skilltree.next())
  66.                         {
  67.                             int id = skilltree.getInt("skillId");
  68.                             int lvl = skilltree.getInt("skillLvl");
  69.                             map.put(SkillTable.getSkillHashCode(id, lvl + 1), new L2PetSkillLearn(id, lvl, skilltree.getInt("minLvl")));
  70.                         }
  71.                         _skillTrees.put(npcId, map);
  72.                     }
  73.                     ps2.clearParameters();
  74.                     count += map.size();
  75.                     _log.fine(getClass().getSimpleName() + ": skill tree for pet " + npcId + " has " + map.size() + " skills");
  76.                 }
  77.             }
  78.         }
  79.         catch (Exception e)
  80.         {
  81.             _log.log(Level.SEVERE, getClass().getSimpleName() + ": Error while creating pet skill tree (Pet ID " + npcId + "): " + e.getMessage(), e);
  82.         }
  83.         _log.info(getClass().getSimpleName() + ": Loaded " + count + " skills.");
  84.     }
  85.    
  86.     public int getAvailableLevel(L2Summon cha, int skillId)
  87.     {
  88.         int lvl = 0;
  89.         if (!_skillTrees.containsKey(cha.getNpcId()))
  90.         {
  91.             _log.warning(getClass().getSimpleName() + ": 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.             {
  99.                 continue;
  100.             }
  101.             if (temp.getLevel() == 0)
  102.             {
  103.                 if (cha.getLevel() < 70)
  104.                 {
  105.                     lvl = (cha.getLevel() / 10);
  106.                     if (lvl <= 0)
  107.                     {
  108.                         lvl = 1;
  109.                     }
  110.                 }
  111.                 else
  112.                 {
  113.                     lvl = (7 + ((cha.getLevel() - 70) / 5));
  114.                 }
  115.                
  116.                 // formula usable for skill that have 10 or more skill levels
  117.                 int maxLvl = SkillTable.getInstance().getMaxLevel(temp.getId());
  118.                 if (lvl > maxLvl)
  119.                 {
  120.                     lvl = maxLvl;
  121.                 }
  122.                 break;
  123.             }
  124.             else if (temp.getMinLevel() <= cha.getLevel())
  125.             {
  126.                 if (temp.getLevel() > lvl)
  127.                 {
  128.                     lvl = temp.getLevel();
  129.                 }
  130.             }
  131.         }
  132.         return lvl;
  133.     }
  134.    
  135.     public List<Integer> getAvailableSkills(L2Summon cha)
  136.     {
  137.         List<Integer> skillIds = new ArrayList<>();
  138.         if (!_skillTrees.containsKey(cha.getNpcId()))
  139.         {
  140.             _log.warning(getClass().getSimpleName() + ": Pet id " + cha.getNpcId() + " does not have any skills assigned.");
  141.             return skillIds;
  142.         }
  143.         Collection<L2PetSkillLearn> skills = _skillTrees.get(cha.getNpcId()).values();
  144.         for (L2PetSkillLearn temp : skills)
  145.         {
  146.             if (skillIds.contains(temp.getId()))
  147.             {
  148.                 continue;
  149.             }
  150.             skillIds.add(temp.getId());
  151.         }
  152.         return skillIds;
  153.     }
  154.    
  155.     public static final class L2PetSkillLearn
  156.     {
  157.         private final int _id;
  158.         private final int _level;
  159.         private final int _minLevel;
  160.        
  161.         public L2PetSkillLearn(int id, int lvl, int minLvl)
  162.         {
  163.             _id = id;
  164.             _level = lvl;
  165.             _minLevel = minLvl;
  166.         }
  167.        
  168.         public int getId()
  169.         {
  170.             return _id;
  171.         }
  172.        
  173.         public int getLevel()
  174.         {
  175.             return _level;
  176.         }
  177.        
  178.         public int getMinLevel()
  179.         {
  180.             return _minLevel;
  181.         }
  182.     }
  183.    
  184.     public static SummonSkillsTable getInstance()
  185.     {
  186.         return SingletonHolder._instance;
  187.     }
  188.    
  189.     private static class SingletonHolder
  190.     {
  191.         protected static final SummonSkillsTable _instance = new SummonSkillsTable();
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement