Guest User

Untitled

a guest
Aug 31st, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.80 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2004-2015 L2J DataPack
  3.  *
  4.  * This file is part of L2J DataPack.
  5.  *
  6.  * L2J DataPack 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 DataPack 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 hellbound;
  20.  
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.datatables.DoorTable;
  23. import com.l2jserver.gameserver.instancemanager.GlobalVariablesManager;
  24. import com.l2jserver.gameserver.model.L2Spawn;
  25. import com.l2jserver.gameserver.model.actor.L2Npc;
  26. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.model.quest.Quest;
  29. import com.l2jserver.gameserver.util.Broadcast;
  30.  
  31. /**
  32.  * Hellbound Engine.
  33.  * @author -
  34.  */
  35. public final class HellboundEngine extends Quest
  36. {
  37.     private static final int[][] DOOR_LIST =
  38.     {
  39.         { 19250001, 5 },
  40.         { 19250002, 5 },
  41.         { 20250001, 9 },
  42.         { 20250002, 7 }
  43.     };
  44.     private static final int[] MAX_TRUST =
  45.     {
  46.         0, 300000, 600000, 1000000, 1010000, 1400000, 1490000, 2000000, 2000001, 2500000, 4000000, 0
  47.     };
  48.  
  49.     private static final int DEREK = 18465;
  50.  
  51.     private static final String ANNOUNCEMENT = "Hellbound has reached level: %lvl%";
  52.     private static final int UPDATE_INTERVAL = 60000;
  53.     private static final String UPDATE_EVENT = "UPDATE";
  54.     private int _cachedLevel = -1;
  55.     private int _maxTrust = 0;
  56.     private int _minTrust = 0;
  57.    
  58.     public HellboundEngine()
  59.     {
  60.         super(-1, HellboundEngine.class.getSimpleName(), "hellbound");
  61.        
  62.         for (int id : HellboundPointData.getInstance().getPointsInfo().keySet())
  63.         {
  64.             addKillId(id);
  65.         }
  66.        
  67.         startQuestTimer(UPDATE_EVENT, 1000, null, null);
  68.        
  69.         _log.info(HellboundEngine.class.getSimpleName() + ": Level: " + getLevel());
  70.         _log.info(HellboundEngine.class.getSimpleName() + ": Trust: " + getTrust());
  71.         _log.info(HellboundEngine.class.getSimpleName() + ": Status: " + (isLocked() ? "locked." : "unlocked."));
  72.     }
  73.    
  74.     @Override
  75.     public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
  76.     {
  77.         if (event.equals(UPDATE_EVENT))
  78.         {
  79.             int level = getLevel();
  80.             if ((level > 0) && (level == getCachedLevel()))
  81.             {
  82.                 if ((getTrust() == getMaxTrust()) && (level != 4)) // Only exclusion is kill of Derek.
  83.                 {
  84.                     level++;
  85.                     setLevel(level);
  86.                     onLevelChange(level);
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 onLevelChange(level); // First run or changed by administrator.
  92.             }
  93.             startQuestTimer(UPDATE_EVENT, UPDATE_INTERVAL, null, null);
  94.         }
  95.         return super.onAdvEvent(event, npc, player);
  96.     }
  97.    
  98.     /**
  99.      * Adds and removes spawns for Hellbound given the conditions for spawn.
  100.      */
  101.     private void doSpawn()
  102.     {
  103.         int added = 0;
  104.         int deleted = 0;
  105.         final HellboundSpawns hellboundSpawns = HellboundSpawns.getInstance();
  106.         for (L2Spawn spawn : hellboundSpawns.getSpawns())
  107.         {
  108.             final L2Npc npc = spawn.getLastSpawn();
  109.             if ((getLevel() < hellboundSpawns.getSpawnMinLevel(spawn.getNpcid())) || (getLevel() > hellboundSpawns.getSpawnMaxLevel(spawn.getNpcid())))
  110.             {
  111.                 spawn.stopRespawn();
  112.                
  113.                 if ((npc != null) && npc.isVisible())
  114.                 {
  115.                     npc.deleteMe();
  116.                     deleted++;
  117.                 }
  118.             }
  119.             else
  120.             {
  121.                 spawn.startRespawn();
  122.                 if (npc == null)
  123.                 {
  124.                     spawn.doSpawn();
  125.                     added++;
  126.                 }
  127.                 else
  128.                 {
  129.                     if (npc.isDecayed())
  130.                     {
  131.                         npc.setDecayed(false);
  132.                     }
  133.                     if (npc.isDead())
  134.                     {
  135.                         npc.doRevive();
  136.                     }
  137.                     if (!npc.isVisible())
  138.                     {
  139.                         npc.setIsVisible(true);
  140.                         added++;
  141.                     }
  142.                    
  143.                     npc.setCurrentHp(npc.getMaxHp());
  144.                     npc.setCurrentMp(npc.getMaxMp());
  145.                 }
  146.             }
  147.         }
  148.        
  149.         if (added > 0)
  150.         {
  151.             _log.info(getClass().getSimpleName() + ": Spawned " + added + " NPCs.");
  152.         }
  153.         if (deleted > 0)
  154.         {
  155.             _log.info(getClass().getSimpleName() + ": Removed " + deleted + " NPCs.");
  156.         }
  157.     }
  158.    
  159.     /**
  160.      * Gets the Hellbound level.
  161.      * @return the level
  162.      */
  163.     public int getLevel()
  164.     {
  165.         return Integer.parseInt(GlobalVariablesManager.getInstance().getStoredVariable("HBLevel"));
  166.     }
  167.    
  168.     /**
  169.      * Sets the Hellbound level.
  170.      * @param lvl the level to set
  171.      */
  172.     public void setLevel(int lvl)
  173.     {
  174.         if (lvl == getLevel())
  175.         {
  176.             return;
  177.         }
  178.        
  179.         _log.info(HellboundEngine.class.getSimpleName() + ": Changing level from " + getLevel() + " to " + lvl + ".");
  180.        
  181.         GlobalVariablesManager.getInstance().storeVariable("HBLevel", String.valueOf(lvl));
  182.     }
  183.    
  184.     public int getCachedLevel()
  185.     {
  186.         return _cachedLevel;
  187.     }
  188.    
  189.     public int getMaxTrust()
  190.     {
  191.         return _maxTrust;
  192.     }
  193.    
  194.     public int getMinTrust()
  195.     {
  196.         return _minTrust;
  197.     }
  198.    
  199.     /**
  200.      * Gets the trust.
  201.      * @return the trust
  202.      */
  203.     public int getTrust()
  204.     {
  205.         return Integer.parseInt(GlobalVariablesManager.getInstance().getStoredVariable("HBTrust"));
  206.     }
  207.    
  208.     /**
  209.      * Sets the truest.
  210.      * @param trust the trust to set
  211.      */
  212.     private void setTrust(int trust)
  213.     {
  214.         GlobalVariablesManager.getInstance().storeVariable("HBTrust", String.valueOf(trust));
  215.     }
  216.    
  217.     /**
  218.      * Verifies if Hellbound is locked.
  219.      * @return {@code true} if Hellbound is locked, {@code false} otherwise
  220.      */
  221.     public boolean isLocked()
  222.     {
  223.         return getLevel() <= 0;
  224.     }
  225.    
  226.     @Override
  227.     public String onKill(L2Npc npc, L2PcInstance killer, boolean isSummon)
  228.     {
  229.         final int npcId = npc.getNpcId();
  230.         final HellboundPointData hellboundPointData = HellboundPointData.getInstance();
  231.         if (hellboundPointData.getPointsInfo().containsKey(npcId))
  232.         {
  233.             if ((getLevel() >= hellboundPointData.getMinHbLvl(npcId)) && (getLevel() <= hellboundPointData.getMaxHbLvl(npcId)) && ((hellboundPointData.getLowestTrustLimit(npcId) == 0) || (getTrust() > hellboundPointData.getLowestTrustLimit(npcId))))
  234.             {
  235.                 updateTrust(hellboundPointData.getPointsAmount(npcId), true);
  236.             }
  237.            
  238.             if ((npcId == DEREK) && (getLevel() == 4))
  239.             {
  240.                 setLevel(5);
  241.             }
  242.         }
  243.         return super.onKill(npc, killer, isSummon);
  244.     }
  245.    
  246.     /**
  247.      * Called on every level change.
  248.      * @param newLevel the new level
  249.      */
  250.     public void onLevelChange(int newLevel)
  251.     {
  252.         try
  253.         {
  254.             setMaxTrust(MAX_TRUST[newLevel]);
  255.             setMinTrust(MAX_TRUST[newLevel - 1]);
  256.         }
  257.         catch (Exception e)
  258.         {
  259.             setMaxTrust(0);
  260.             setMinTrust(0);
  261.         }
  262.        
  263.         updateTrust(0, false);
  264.        
  265.         doSpawn();
  266.        
  267.         for (int[] doorData : DOOR_LIST)
  268.         {
  269.             try
  270.             {
  271.                 L2DoorInstance door = DoorTable.getInstance().getDoor(doorData[0]);
  272.                 if (door.getOpen())
  273.                 {
  274.                     if (newLevel < doorData[1])
  275.                     {
  276.                         door.closeMe();
  277.                     }
  278.                 }
  279.                 else
  280.                 {
  281.                     if (newLevel >= doorData[1])
  282.                     {
  283.                         door.openMe();
  284.                     }
  285.                 }
  286.             }
  287.             catch (Exception e)
  288.             {
  289.                 _log.warning(getClass().getSimpleName() + " : Doors problem!" + e.getMessage());
  290.             }
  291.         }
  292.        
  293.         if (_cachedLevel > 0)
  294.         {
  295.             Broadcast.announceToOnlinePlayers(ANNOUNCEMENT.replace("%lvl%", String.valueOf(newLevel)));
  296.             _log.info(HellboundEngine.class.getSimpleName() + ": New level: " + newLevel);
  297.         }
  298.         _cachedLevel = newLevel;
  299.     }
  300.    
  301.     /**
  302.      * Sets the maximum trust for the current level.
  303.      * @param trust the maximum trust
  304.      */
  305.     private void setMaxTrust(int trust)
  306.     {
  307.         _maxTrust = trust;
  308.         if ((_maxTrust > 0) && (getTrust() > _maxTrust))
  309.         {
  310.             setTrust(_maxTrust);
  311.         }
  312.     }
  313.    
  314.     /**
  315.      * Sets the minimum trust for the current level.
  316.      * @param trust the minimum trust
  317.      */
  318.     private void setMinTrust(int trust)
  319.     {
  320.         _minTrust = trust;
  321.        
  322.         if (getTrust() >= _maxTrust)
  323.         {
  324.             setTrust(_minTrust);
  325.         }
  326.     }
  327.    
  328.     @Override
  329.     public boolean unload()
  330.     {
  331.         cancelQuestTimers(UPDATE_EVENT);
  332.         return true;
  333.     }
  334.    
  335.     /**
  336.      * Updates the trust.
  337.      * @param trust the trust
  338.      * @param useRates if {@code true} it will use Hellbound trust rates
  339.      */
  340.     public synchronized void updateTrust(int trust, boolean useRates)
  341.     {
  342.         if (isLocked())
  343.         {
  344.             return;
  345.         }
  346.        
  347.         int reward = trust;
  348.         if (useRates)
  349.         {
  350.             reward = (int) (trust * (trust > 0 ? Config.RATE_HB_TRUST_INCREASE : Config.RATE_HB_TRUST_DECREASE));
  351.         }
  352.        
  353.         final int finalTrust = Math.max(getTrust() + reward, _minTrust);
  354.         if (_maxTrust > 0)
  355.         {
  356.             setTrust(Math.min(finalTrust, _maxTrust));
  357.         }
  358.         else
  359.         {
  360.             setTrust(finalTrust);
  361.         }
  362.     }
  363.    
  364.     public static HellboundEngine getInstance()
  365.     {
  366.         return SingletonHolder.INSTANCE;
  367.     }
  368.    
  369.     private static class SingletonHolder
  370.     {
  371.         protected static final HellboundEngine INSTANCE = new HellboundEngine();
  372.     }
  373. }
Advertisement
Add Comment
Please, Sign In to add comment