Guest User

L2Occuration Crystal Mode

a guest
Oct 28th, 2011
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 30.40 KB | None | 0 0
  1. package com.l2jarchid.gameserver.model.actor.instance;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.StringTokenizer;
  9.  
  10. import javolution.text.TextBuilder;
  11. import javolution.util.FastSet;
  12.  
  13. import com.l2jarchid.L2DatabaseFactory;
  14. import com.l2jarchid.gameserver.ThreadPoolManager;
  15. import com.l2jarchid.gameserver.ai.CtrlIntention;
  16. import com.l2jarchid.gameserver.cache.HtmCache;
  17. import com.l2jarchid.gameserver.datatables.ClanTable;
  18. import com.l2jarchid.gameserver.datatables.NpcTable;
  19. import com.l2jarchid.gameserver.model.L2Clan;
  20. import com.l2jarchid.gameserver.model.L2ClanMember;
  21. import com.l2jarchid.gameserver.model.L2Spawn;
  22. import com.l2jarchid.gameserver.model.L2Attackable;
  23. import com.l2jarchid.gameserver.model.L2Character;
  24. import com.l2jarchid.gameserver.model.actor.instance.L2NpcInstance;
  25. import com.l2jarchid.gameserver.network.clientpackets.Say2;
  26. import com.l2jarchid.gameserver.network.serverpackets.ActionFailed;
  27. import com.l2jarchid.gameserver.network.serverpackets.CreatureSay;
  28. import com.l2jarchid.gameserver.network.serverpackets.ExShowScreenMessage;
  29. import com.l2jarchid.gameserver.network.serverpackets.MyTargetSelected;
  30. import com.l2jarchid.gameserver.network.serverpackets.NpcHtmlMessage;
  31. import com.l2jarchid.gameserver.network.serverpackets.StatusUpdate;
  32. import com.l2jarchid.gameserver.network.serverpackets.ValidateLocation;
  33. import com.l2jarchid.gameserver.templates.L2NpcTemplate;
  34. import com.l2jarchid.util.Rnd;
  35.  
  36. /**
  37.  * @author Matim
  38.  * @version 2.1
  39.  * <br><br>
  40.  * L2Occupation Crystal Instance<br><br>
  41.  * <li>will not move anywhere.
  42.  * <li>will not attack attackers.
  43.  * <li>it has hp bar status.
  44.  * <br><br>
  45.  * Owner of the crystal may manage crystal functions, such as:
  46.  * <li>defence abilitiy
  47.  * <li>warning ability
  48.  * <li>stats upgrades
  49.  * <li>special items/buffs/teleports (TODO)
  50.  */
  51. public class L2CrystalInstance extends L2FolkInstance
  52. {
  53.     private final int DEFENDERS_COUNT = 5; 
  54.     private int _ownerClanId = 1;
  55.     private String _locationName = "";
  56.     private boolean _defenceAbilityEnabled = false;
  57.     private boolean _warningAbilityEnabled = false;
  58.     private boolean _statsUpgradeEnabled = false;
  59.     private boolean _canWarn = true;
  60.     private boolean _canSpawnDefenders = true;
  61.    
  62.     private int _warningCount = 0;
  63.     private int _defenceSpawnCount = 0;
  64.    
  65.     public static FastSet<L2NpcInstance> _defenders = new FastSet<L2NpcInstance>();
  66.    
  67.     public L2CrystalInstance(int objectId, L2NpcTemplate template)
  68.     {
  69.         super(objectId, template);
  70.         setIsInvul(false);
  71.     }
  72.    
  73.     @Override
  74.     public void onSpawn()
  75.     {
  76.         super.onSpawn();
  77.         loadData(this.getNpcId());
  78.     }
  79.    
  80.     /**
  81.      * Load Data about crystal from database.
  82.      * Clan owner Id, location name, enabled avilities etc.
  83.      * @param crystalId
  84.      */
  85.     private void loadData(int crystalId)
  86.     {
  87.         Connection con = null;
  88.         try
  89.         {
  90.             PreparedStatement statement;
  91.             ResultSet rs;
  92.            
  93.             con = L2DatabaseFactory.getInstance().getConnection();
  94.            
  95.             statement = con.prepareStatement("Select * from crystal_list where mapId =" + this.getNpcId());
  96.             rs = statement.executeQuery();
  97.            
  98.             while (rs.next())
  99.             {
  100.                 _ownerClanId = rs.getInt("clanId");
  101.                 _locationName = rs.getString("locationName");
  102.                 _defenceAbilityEnabled = rs.getBoolean("defenceEnabled");
  103.                 _warningAbilityEnabled = rs.getBoolean("warningEnabled");
  104.                 _statsUpgradeEnabled = rs.getBoolean("statsEnabled");
  105.                 _warningCount = rs.getInt("warningCount");
  106.                 _defenceSpawnCount = rs.getInt("defenceCount");
  107.             }
  108.             rs.close();
  109.             statement.close();
  110.  
  111.         }
  112.         catch (Exception e)
  113.         {
  114.             _log.info("Crystal Manager [Load Data]: " + e);
  115.         }
  116.         finally
  117.         {
  118.             L2DatabaseFactory.close(con);
  119.         }
  120.     }
  121.    
  122.     /**
  123.      * Save Data about crystal into database.
  124.      * @param crystalId
  125.      */
  126.     public void saveData(int crystalId)
  127.     {
  128.         Connection con = null;
  129.         try
  130.         {
  131.             con = L2DatabaseFactory.getInstance().getConnection();
  132.             Statement statement = con.createStatement();
  133.             statement.executeUpdate("UPDATE crystal_list SET clanId ='" + _ownerClanId + "', defenceEnabled ='" + _defenceAbilityEnabled + "', warningEnabled ='" + _warningAbilityEnabled + "', statsEnabled ='" + _statsUpgradeEnabled  + "', warningCount ='" + _warningCount + "', defenceCount ='" + _defenceSpawnCount + "' WHERE mapId ='" + crystalId + "'");
  134.             statement.close();
  135.         }
  136.         catch (SQLException e)
  137.         {
  138.             _log.info("Crystal Manager [Save Data]: " + e);
  139.         }
  140.         finally
  141.         {
  142.             L2DatabaseFactory.close(con);
  143.         }
  144.     }
  145.    
  146.     @Override
  147.     public void onBypassFeedback(L2PcInstance player, String command)
  148.     {
  149.         if (player == null || player.getLastFolkNPC() == null || player.getLastFolkNPC().getObjectId() != this.getObjectId())
  150.         {
  151.             return;
  152.         }
  153.         if (command.startsWith("manageCrystal"))
  154.         {          
  155.             if (player.isClanLeader())
  156.             {
  157.                 try
  158.                 {
  159.                     showManageCrystalWindow(player);
  160.                 }
  161.                 catch (Exception e)
  162.                 {
  163.                     System.out.println("MANAGE CRYSTAL ERROR: " + e);
  164.                 }
  165.             }
  166.         }
  167.         else if (command.startsWith("disableDefence"))
  168.         {
  169.             setDefenceAbilityEnabled(false);
  170.             saveDataAndShowManageWindow(player);
  171.         }
  172.         else if (command.startsWith("enableDefence"))
  173.         {
  174.             setDefenceAbilityEnabled(true);
  175.             saveDataAndShowManageWindow(player);
  176.         }
  177.         else if (command.startsWith("disableWarning"))
  178.         {
  179.             setWarningAbilityEnabled(false);
  180.             saveDataAndShowManageWindow(player);
  181.         }
  182.         else if (command.startsWith("enableWarning"))
  183.         {
  184.             setWarningAbilityEnabled(true);
  185.             saveDataAndShowManageWindow(player);
  186.         }
  187.         else if (command.startsWith("disableStats"))
  188.         {
  189.             setStatsEnabled(false);
  190.             saveDataAndShowManageWindow(player);
  191.         }
  192.         else if (command.startsWith("enableStats"))
  193.         {
  194.             setStatsEnabled(true);
  195.             saveDataAndShowManageWindow(player);
  196.         }
  197.         else if (command.startsWith("defenceInfo"))
  198.         {
  199.             showCrystalWindow(player, "defence.htm");
  200.         }
  201.         else if (command.startsWith("warningInfo"))
  202.         {
  203.             showCrystalWindow(player, "warning.htm");
  204.         }
  205.         else if (command.startsWith("statsInfo"))
  206.         {
  207.             showCrystalWindow(player, "stats.htm");
  208.         }
  209.         else if (command.startsWith("mainWindow"))
  210.         {
  211.             showChatWindow(player, 0);
  212.         }
  213.         else if (command.startsWith("ownerInfo"))
  214.         {
  215.             showOwnerInfoWindow(player);
  216.         }
  217.         else if (command.startsWith("crystalGuide"))
  218.         {
  219.             showCrystalWindow(player, "guide.htm");
  220.         }
  221.         else if (command.startsWith("abilitiesWindow"))
  222.         {
  223.             showAbilitiesShopWindow(player);
  224.         }
  225.         else if (command.startsWith("buyWarning"))
  226.         {
  227.             try
  228.             {
  229.                 String val = command.substring(10);
  230.                 StringTokenizer st = new StringTokenizer(val);
  231.  
  232.                 String id = st.nextToken();
  233.                 int idval = Integer.parseInt(id);
  234.                 buyAbility(player, idval, 1);
  235.                 showAbilitiesShopWindow(player);
  236.                 saveData(this.getNpcId());
  237.             }
  238.             catch (StringIndexOutOfBoundsException e)
  239.             {
  240.                 player.sendMessage("Error!");
  241.             }
  242.             catch (NumberFormatException nfe)
  243.             {
  244.                 player.sendMessage("Specify a valid number.");
  245.             }
  246.         }
  247.         else if (command.startsWith("buyDefence"))
  248.         {
  249.             try
  250.             {
  251.                 String val = command.substring(10);
  252.                 StringTokenizer st = new StringTokenizer(val);
  253.  
  254.                 String id = st.nextToken();
  255.                 int idval = Integer.parseInt(id);
  256.                 buyAbility(player, idval, 2);
  257.                 showAbilitiesShopWindow(player);
  258.                 saveData(this.getNpcId());
  259.             }
  260.             catch (StringIndexOutOfBoundsException e)
  261.             {
  262.                 player.sendMessage("Error!");
  263.             }
  264.             catch (NumberFormatException nfe)
  265.             {
  266.                 player.sendMessage("Specify a valid number.");
  267.             }
  268.         }
  269.         else if (command.startsWith("locStatus"))
  270.         {
  271.             showLocationsStatusWindow(player);
  272.         }
  273.         else if (command.startsWith("crystalFunctions"))
  274.         {
  275.             showCrystalFunctionsWindow(player);
  276.         }
  277.     }
  278.    
  279.     @Override
  280.     public void showChatWindow(L2PcInstance player, int val)
  281.     {
  282.         TextBuilder tb = new TextBuilder();
  283.         tb.append("<html><title>Crystal</title><body><center><br>");
  284.         tb.append("Conqerable <font color=LEVEL>" + getLocationName() + "</font> Location:<br>");
  285.         tb.append("Welcome <font color=LEVEL>" + player.getName() + "</font>, Im Occupation Crystal!<br>");
  286.        
  287.         if (player.getClanId() == getOwnerClanId())
  288.         {
  289.             if (player.isClanLeader())
  290.             {
  291.                 tb.append("How may I help you my owner?<br>");
  292.                 tb.append("<button value=\"Manage Functions\" action=\"bypass -h npc_%objectId%_manageCrystal\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  293.             }
  294.             else
  295.             {
  296.                 tb.append("Glad to see you!<br>");
  297.                 tb.append("Only clan leader may manage crystal!<br>");
  298.             }
  299.            
  300.             tb.append("<button value=\"Crystal Functions\" action=\"bypass -h npc_%objectId%_crystalFunctions\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  301.         }
  302.         else
  303.         {
  304.             tb.append("Go away, I will not help you!<br>");
  305.             tb.append("You are not my owner!<br>");
  306.         }  
  307.        
  308.         tb.append("<button value=\"Owner Info\" action=\"bypass -h npc_%objectId%_ownerInfo\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  309.         tb.append("<button value=\"Locations Status\" action=\"bypass -h npc_%objectId%_locStatus\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  310.         tb.append("<button value=\"Crystal Guide\" action=\"bypass -h npc_%objectId%_crystalGuide\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  311.         tb.append("<br><br><br>By Matim");
  312.        
  313.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  314.         msg.setHtml(tb.toString());
  315.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  316.    
  317.         player.sendPacket(msg);
  318.     }
  319.    
  320.     /**
  321.      * Show info about crystal functions.
  322.      * Are they enabled or disabled.
  323.      * Also possibility to use functions is there.
  324.      * Such as Teleports and additional buffs.
  325.      * @param player
  326.      */
  327.     public void showCrystalFunctionsWindow(L2PcInstance player)
  328.     {  
  329.         TextBuilder tb = new TextBuilder();
  330.         tb.append("<html><title>Crystal</title><body><center><br>");
  331.         tb.append("Crystal Functions:<br>");
  332.         tb.append("<img src=\"L2UI_CH3.onscrmsg_pattern01_1\" width=300 height=32 align=left><br><br>");
  333.        
  334.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  335.         tb.append("<td width=270 align=\"center\">Defence Ability: <font color=\"LEVEL\">" + isEnabled(defenceAbilityEnabled()) + "</font></td></tr></table><br>");
  336.        
  337.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  338.         tb.append("<td width=270 align=\"center\">Warning Ability: <font color=\"LEVEL\">" + isEnabled(warningAbilityEnabled()) + "</font></td></tr></table><br>");
  339.        
  340.         tb.append("<br><br><img src=\"L2UI_CH3.onscrmsg_pattern01_2\" width=300 height=32 align=left><br>");
  341.         tb.append("<button value=\"Main Window\" action=\"bypass -h npc_%objectId%_mainWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  342.        
  343.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  344.         msg.setHtml(tb.toString());
  345.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  346.    
  347.         player.sendPacket(msg);
  348.     }
  349.    
  350.     /**
  351.      * Show basic info about clan which is owner of the location.
  352.      * Info like leader name, clan level, member count etc.
  353.      * <br><br>
  354.      * @param player
  355.      */
  356.     public void showOwnerInfoWindow(L2PcInstance player)
  357.     {
  358.         TextBuilder tb = new TextBuilder();
  359.         tb.append("<html><title>Crystal</title><body><center><br>");
  360.         tb.append("Info About Owner:<br>");
  361.         tb.append("<img src=\"L2UI_CH3.onscrmsg_pattern01_1\" width=300 height=32 align=left><br><br>");
  362.        
  363.         if (_ownerClanId == 0)
  364.         {
  365.             tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  366.             tb.append("<td width=270 align=\"center\">Location Owned by: <font color=\"LEVEL\">Nobody</font>!</td></tr></table>");
  367.         }
  368.         else
  369.         {
  370.             if (ClanTable.getInstance().getClan(_ownerClanId) != null)
  371.             {
  372.                 L2Clan clan = ClanTable.getInstance().getClan(getOwnerClanId());
  373.                 String ownerClanName = clan.getName();
  374.                
  375.                 tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  376.                 tb.append("<td width=270 align=\"center\">Location Owned by: <font color=\"LEVEL\">" + ownerClanName + "</font> clan!</td></tr></table><br>");
  377.                
  378.                 tb.append("<table width=270 bgcolor=\"144499\"><tr>");
  379.                 tb.append("<td width=270 align=\"center\">Clan Leader: <font color=\"LEVEL\">" + clan.getLeaderName() + "</font></td></tr></table><br>");
  380.                
  381.                 tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  382.                 tb.append("<td width=270 align=\"center\">Members Count: <font color=\"LEVEL\">" + clan.getMembersCount() + "</font></td></tr></table><br>");
  383.                
  384.                 tb.append("<table width=270 bgcolor=\"144499\"><tr>");
  385.                 tb.append("<td width=270 align=\"center\">Clan Level: <font color=\"LEVEL\">" + clan.getLevel() + "</font></td></tr></table><br>");
  386.                
  387.                 tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  388.                 tb.append("<td width=270 align=\"center\">Online Members: <font color=\"LEVEL\">" + clan.getOnlineMembers(null).length + "</font></td></tr></table><br>");
  389.             }
  390.             else
  391.             {
  392.                 tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  393.                 tb.append("<td width=270 align=\"center\">Location Owned by: <font color=\"LEVEL\">Nobody</font>!</td></tr></table>");
  394.             }
  395.         }
  396.        
  397.         tb.append("<br><br><img src=\"L2UI_CH3.onscrmsg_pattern01_2\" width=300 height=32 align=left><br>");
  398.         tb.append("<button value=\"Main Window\" action=\"bypass -h npc_%objectId%_mainWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  399.        
  400.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  401.         msg.setHtml(tb.toString());
  402.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  403.    
  404.         player.sendPacket(msg);
  405.     }
  406.    
  407.     /**
  408.      * Main window, where clan leader which is owner of the crystal,
  409.      * may manage crystal functions, till now there are 3 available:
  410.      * <li>Stats Upgrade - additional HP/Defence etc.
  411.      * <li>Defence Ability - spawn defenders while crystal is beeing attacked.
  412.      * <li>Warning Ability - warn each clan member while crystal is attacket.
  413.      * <br><br>
  414.      * @param player
  415.      */
  416.     public void showManageCrystalWindow(L2PcInstance player)
  417.     {
  418.         TextBuilder tb = new TextBuilder();
  419.         tb.append("<html><title>Crystal Manage</title><body><center><br>");
  420.         tb.append("<font color=LEVEL>" + player.getName() + "</font>, you are my Leader!<br>");
  421.        
  422.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr><td>Defence Ability</td>");
  423.         tb.append("<td><button value=\"Info\" action=\"bypass -h npc_%objectId%_defenceInfo\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  424.        
  425.         if (defenceAbilityEnabled())
  426.         {
  427.             tb.append("<td><button value=\"Disable\" action=\"bypass -h npc_%objectId%_disableDefence\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  428.         }
  429.         else
  430.         {
  431.             tb.append("<td><button value=\"Enable\" action=\"bypass -h npc_%objectId%_enableDefence\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  432.         }
  433.         tb.append("</tr></table>");
  434.         tb.append("<table width=270 border=0><tr><td>Warning Ability</td>");
  435.         tb.append("<td><button value=\"Info\" action=\"bypass -h npc_%objectId%_warningInfo\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  436.        
  437.         if (warningAbilityEnabled())
  438.         {
  439.             tb.append("<td><button value=\"Disable\" action=\"bypass -h npc_%objectId%_disableWarning\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  440.         }
  441.         else
  442.         {
  443.             tb.append("<td><button value=\"Enable\" action=\"bypass -h npc_%objectId%_enableWarning\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  444.         }
  445.         tb.append("</tr></table>");
  446.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr><td>Stats Upgrades</td>");
  447.         tb.append("<td><button value=\"Info\" action=\"bypass -h npc_%objectId%_statsInfo\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  448.        
  449.         if (statsUpgradeEnabled())
  450.         {
  451.             tb.append("<td><button value=\"Disable\" action=\"bypass -h npc_%objectId%_disableStats\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  452.         }
  453.         else
  454.         {
  455.             tb.append("<td><button value=\"Enable\" action=\"bypass -h npc_%objectId%_enableStats\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  456.         }
  457.         tb.append("</tr></table>");
  458.         tb.append("<br><br><button value=\"Buy Abilities\" action=\"bypass -h npc_%objectId%_abilitiesWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  459.         tb.append("<button value=\"Buy Functions\" action=\"bypass -h npc_%objectId%_functionsWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  460.         tb.append("<button value=\"Main Window\" action=\"bypass -h npc_%objectId%_mainWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  461.            
  462.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  463.         msg.setHtml(tb.toString());
  464.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  465.    
  466.         player.sendPacket(msg);
  467.     }
  468.    
  469.     /**
  470.      * Basic window where owner of the crystal (Clan Leader) may buy additional
  471.      * ammount of the abilities, each time when crystal will use his ability, for example
  472.      * will warn clan members or spawn his defenders (defence ability) defence ability or
  473.      * warning ability count will be decrested, each ability cos 100 Clan Reputation Points.
  474.      * To prevent spamming with warning etc, there is delay - one minute.                  
  475.      * @param player
  476.      */
  477.     public void showAbilitiesShopWindow(L2PcInstance player)
  478.     {
  479.         L2Clan clan = player.getClan();
  480.        
  481.         TextBuilder tb = new TextBuilder();
  482.         tb.append("<html><title>Crystal</title><body><center><br>");
  483.         tb.append("<font color=\"LEVEL\">Abilities Shop:</font><br>");
  484.         tb.append("Each time your clan will be warned or crystal will use Defence Ability (will spawn defenders) - your bought abilities count will be decrested. Here you can buy additional ammount of the abilities!<br>");
  485.         tb.append("You need <font color=\"LEVEL\">100</font> Clan Repotation Points to buy one.<br>");
  486.         tb.append("Your clan CRP ammount: <font color=\"LEVEL\">" + clan.getReputationScore() + "</font><br>");
  487.        
  488.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr><td>Warning Count:</td><td><edit var=\"count\" width=50></td>");
  489.         tb.append("<td><button value=\"Buy\" action=\"bypass -h npc_%objectId%_buyWarning $count\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table><br>");
  490.        
  491.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr><td>Defence Count:</td><td><edit var=\"count2\" width=50></td>");
  492.         tb.append("<td><button value=\"Buy\" action=\"bypass -h npc_%objectId%_buyDefence $count2\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table><br>");
  493.        
  494.         tb.append("<br><br>Held Warnings Left: <font color=\"LEVEL\">" + _warningCount + "</font><br>");
  495.         tb.append("Held Defenders Spawn Left: <font color=\"LEVEL\">" + _defenceSpawnCount + "</font><br>");
  496.         tb.append("<button value=\"Back\" action=\"bypass -h npc_%objectId%_manageCrystal\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  497.        
  498.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  499.         msg.setHtml(tb.toString());
  500.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  501.    
  502.         player.sendPacket(msg);
  503.     }
  504.    
  505.     /**
  506.      * Easier way to show Crystal Htm Window.
  507.      * @param player
  508.      * @param htm
  509.      */
  510.     public void showCrystalWindow(L2PcInstance player, String htm)
  511.     {
  512.         String html = null;
  513.         html = HtmCache.getInstance().getHtm("data/html/ephion/crystal/" + htm);
  514.    
  515.         NpcHtmlMessage msg = new NpcHtmlMessage(getObjectId());
  516.         msg.setHtml(html);
  517.         msg.replace("%objectId%", String.valueOf(getObjectId()));
  518.         player.sendPacket(msg);
  519.     }
  520.    
  521.     /**
  522.      * Show list with all conquerable locations status.
  523.      * With info about owner (only clan names)
  524.      * TODO
  525.      * @param player
  526.      */
  527.     public void showLocationsStatusWindow(L2PcInstance player)
  528.     {      
  529.         TextBuilder tb = new TextBuilder();
  530.         tb.append("<html><title>Crystal</title><body><center><br>");
  531.         tb.append("<font color=\"LEVEL\">Conquerable Locations Status: </font><br>");
  532.         tb.append("<font color=\"LEVEL\">Not implemented yet, sorry! </font><br>");
  533.         tb.append("<button value=\"Back\" action=\"bypass -h npc_%objectId%_mainWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  534.        
  535.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  536.         msg.setHtml(tb.toString());
  537.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  538.    
  539.         player.sendPacket(msg);
  540.     }
  541.    
  542.     @Override
  543.     public boolean doDie(L2Character killer)
  544.     {
  545.         if (!super.doDie(killer))
  546.             return false;
  547.        
  548.         L2PcInstance player = null;
  549.         int _clanId = 0;
  550.        
  551.         if (killer instanceof L2PcInstance)
  552.             player = (L2PcInstance) killer;
  553.         else if (killer instanceof L2SummonInstance)
  554.             player = ((L2SummonInstance) killer).getOwner();
  555.        
  556.         if (player.getClan() != null)
  557.         {
  558.             if (_ownerClanId != 0)
  559.             {
  560.                 L2Clan oldOwner = ClanTable.getInstance().getClan(_ownerClanId);
  561.                 sendMessageToClan(oldOwner, "Your clan lost your " + getLocationName() + " Conquerable Location");
  562.             }
  563.            
  564.             _clanId = player.getClanId();
  565.             _ownerClanId = _clanId;
  566.            
  567.             L2Clan newOwner = ClanTable.getInstance().getClan(_clanId);
  568.            
  569.             cleanCrystalFunctions();
  570.             saveData(this.getNpcId());
  571.             sendMessageToClan(newOwner, "Your clan Conquered " + getLocationName() + " Location!");
  572.         }
  573.         else
  574.         {
  575.             player.sendPacket(new ExShowScreenMessage("You need clan to conquer a location!", 5000, false));
  576.         }
  577.        
  578.         return true;
  579.     }
  580.    
  581.     @Override
  582.     public void onAction(L2PcInstance player)
  583.     {
  584.         if (!this.canTarget(player))
  585.             return;
  586.    
  587.         player.setLastFolkNPC(this);
  588.    
  589.         if(player.getTarget() != this)
  590.         {
  591.             player.setTarget(this);
  592.             getAI();
  593.             MyTargetSelected my = new MyTargetSelected(getObjectId(), player.getLevel() - getLevel());
  594.             player.sendPacket(my);
  595.  
  596.             StatusUpdate su = new StatusUpdate(getObjectId());
  597.             su.addAttribute(StatusUpdate.CUR_HP, (int) getCurrentHp());
  598.             su.addAttribute(StatusUpdate.MAX_HP, getMaxHp());
  599.             player.sendPacket(su);
  600.             player.sendPacket(new ValidateLocation(this));
  601.         }
  602.         else
  603.         {
  604.             if(!isAutoAttackable(player))
  605.                 showChatWindow(player, 0);
  606.             else if(!player.isAlikeDead())
  607.                 player.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, this);
  608.            
  609.             player.sendPacket(new ValidateLocation(this));
  610.             player.sendPacket(ActionFailed.STATIC_PACKET);
  611.         }
  612.     }
  613.    
  614.     @Override
  615.     public void reduceCurrentHp(double damage, L2Character attacker, boolean awake)
  616.     {
  617.         L2PcInstance player = null;
  618.         boolean canAttack = false;
  619.         if (attacker instanceof L2PcInstance)
  620.             player = (L2PcInstance) attacker;
  621.         else if (attacker instanceof L2SummonInstance)
  622.             player = ((L2SummonInstance) attacker).getOwner();
  623.    
  624.         if (player.getClanId() != _ownerClanId)
  625.             canAttack = true;          
  626.         if (canAttack)
  627.         {
  628.             super.reduceCurrentHp(damage, attacker, awake);
  629.             warnClanOwnerMembers();
  630.             spawnDefenders(attacker);
  631.         }
  632.         else if (player.getClan() == null)
  633.         {
  634.             attacker.sendPacket(new ExShowScreenMessage("You need clan to conquer a location!", 5000,false));
  635.         }
  636.         else
  637.         {
  638.             attacker.sendPacket(new ExShowScreenMessage("You cant kill your own Crystal!", 5000,false));
  639.         }
  640.     }
  641.    
  642.     /**
  643.      * Send Message to each online clan member.
  644.      * <br>
  645.      * @param clan
  646.      * @param msg
  647.      */
  648.     private static void sendMessageToClan(L2Clan clan, String msg)
  649.     {
  650.         if (clan != null)
  651.         {
  652.             for (L2ClanMember member: clan.getMembers())
  653.             {              
  654.                 if (member.isOnline())
  655.                 {
  656.                     L2PcInstance player = member.getPlayerInstance();
  657.                     player.sendPacket(new ExShowScreenMessage(msg, 5000,false));
  658.                 }
  659.             }
  660.         }
  661.     }
  662.    
  663.     /**
  664.      * Save data into Database and show Managment window again.
  665.      * Just to reduce same code all the time.
  666.      * @param player
  667.      */
  668.     private void saveDataAndShowManageWindow(L2PcInstance player)
  669.     {
  670.         saveData(this.getNpcId());
  671.         showManageCrystalWindow(player);
  672.     }
  673.        
  674.     /**
  675.      * A way to sell owner Crystal Abilities such as Warning and Defence ones.
  676.      * If abilitiId is 1, it means that it is Warning ability, if 2 its Defence.
  677.      * Owner need Clan Reputation Points to purchase abilities.
  678.      * Each cost 100 Clan Reputation Points.
  679.      * @param player
  680.      * @param count
  681.      * @param abilitiId
  682.      */
  683.     private void buyAbility(L2PcInstance player, int count, int abilitiId)
  684.     {
  685.         int crp = player.getClan().getReputationScore();
  686.         int price = count * 100;
  687.        
  688.         if (crp >= price)
  689.         {
  690.             if (abilitiId == 1)
  691.             {
  692.                 int current = _warningCount;
  693.                 _warningCount = current + count;
  694.                 player.getClan().setReputationScore(player.getClan().getReputationScore() - price, true);  
  695.                 player.sendPacket(new CreatureSay(0, Say2.PARTY, "Crystal", "Successfully purchased " + count + " warnings!"));
  696.             }
  697.             else if (abilitiId == 2)
  698.             {
  699.                 int current = _defenceSpawnCount;
  700.                 _defenceSpawnCount = current + count;
  701.                 player.getClan().setReputationScore(player.getClan().getReputationScore() - price, true);  
  702.                 player.sendPacket(new CreatureSay(0, Say2.PARTY, "Crystal", "Successfully purchased " + count + " Defence Abilities!"));
  703.             }
  704.         }
  705.         else
  706.         {
  707.             player.sendPacket(new CreatureSay(0, Say2.PARTY, "Crystal", "You don't have enough Clan Reputation Points!"));
  708.         }
  709.     }
  710.    
  711.     /**
  712.      * Warn each online clan member, if their crystal is under attack.
  713.      * To prevent spam after each hit from the attacker, there is delay.
  714.      * Default delay between next warning: 60 second.
  715.      * Also Crystal Warning Ability should be enabled.
  716.      */
  717.     private void warnClanOwnerMembers()
  718.     {
  719.         if (warningAbilityEnabled())
  720.         {
  721.             if (canWarn())
  722.             {
  723.                 if (_warningCount > 0)
  724.                 {
  725.                     L2Clan clan = ClanTable.getInstance().getClan(getOwnerClanId());
  726.                     if (clan != null)
  727.                     {
  728.                         sendMessageToClan(clan, "Your " + getLocationName() + " location is under attack!");
  729.                         _warningCount --;
  730.                         setCanWarn(false);
  731.                         ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleWarningTask(), 60000);
  732.                     }
  733.                 }
  734.             }
  735.         }
  736.     }
  737.    
  738.     /**
  739.      * Spawn 5 new Defenders (NPC's)
  740.      * Provoke them to attack character which attacked crystal.
  741.      * After 60 seconds, defenders should disappear.
  742.      * @param attacker
  743.      */
  744.     private void spawnDefenders(L2Character attacker)
  745.     {      
  746.         if (defenceAbilityEnabled())
  747.         {
  748.             if (canSpawnDefenders())
  749.             {
  750.                 if (_defenceSpawnCount > 0)
  751.                 {
  752.                     L2Clan clan = ClanTable.getInstance().getClan(getOwnerClanId());
  753.                     if (clan != null)
  754.                     {
  755.                         L2NpcInstance defender1 = null;
  756.                         L2NpcInstance defender2 = null;
  757.                        
  758.                         for (int i=1; i<= DEFENDERS_COUNT; i++)
  759.                         {
  760.                             defender1 = addSpawn(50050, this.getX() - Rnd.get(100), this.getY() - Rnd.get(100), this.getZ());
  761.                             defender1.getKnownList().addKnownObject(attacker);
  762.                             _defenders.add(defender1);
  763.                             defender2 = addSpawn(50051, this.getX() - Rnd.get(100), this.getY() - Rnd.get(100), this.getZ());
  764.                             defender2.getKnownList().addKnownObject(attacker);
  765.                             _defenders.add(defender2);
  766.                         }
  767.                        
  768.                         for (L2NpcInstance npc: _defenders)
  769.                         {
  770.                             ((L2Attackable) npc).addDamageHate(attacker, 9000, 9000);
  771.                             npc.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
  772.                             npc.setRunning();
  773.                         }
  774.                        
  775.                         setCanSpawnDefenders(false);
  776.                         _defenceSpawnCount --;
  777.                         ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleDefenceTast(), 60000);
  778.                     }
  779.                 }
  780.             }
  781.         }
  782.     }
  783.    
  784.     private static L2NpcInstance addSpawn(int npcId, int x, int y, int z)
  785.     {
  786.         L2NpcInstance result = null;
  787.         try
  788.         {
  789.             L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  790.             if (template != null)
  791.             {
  792.                 L2Spawn spawn = new L2Spawn(template);
  793.                 spawn.setInstanceId(0);
  794.                 spawn.setHeading(1);
  795.                 spawn.setLocx(x);
  796.                 spawn.setLocy(y);
  797.                 spawn.setLocz(z);
  798.                 spawn.stopRespawn();
  799.                 result = spawn.spawnOne();
  800.    
  801.                 return result;
  802.             }
  803.         }
  804.         catch (Exception e1)
  805.         {
  806.            
  807.         }  
  808.         return null;
  809.     }
  810.    
  811.     /**
  812.      * Prepare crystal functions for new owner.
  813.      * At begining each functions should be disabled.
  814.      * <li>Clean Crystal Functions
  815.      * <li>Save Crystal Data
  816.      */
  817.     private void cleanCrystalFunctions()
  818.     {
  819.         setCanWarn(true);
  820.         setCanSpawnDefenders(true);
  821.         setDefenceAbilityEnabled(false);
  822.         setWarningAbilityEnabled(false);
  823.         setStatsEnabled(false);
  824.         _warningCount = 0;
  825.         _defenceSpawnCount = 0;
  826.     }
  827.    
  828.     /**
  829.      * Delay for warning ability, to prevent spam.
  830.      */
  831.     private class ScheduleWarningTask implements Runnable
  832.     {
  833.         public ScheduleWarningTask()
  834.         {
  835.             // Nothing
  836.         }
  837.        
  838.         public void run()
  839.         {
  840.             setCanWarn(true);
  841.         }
  842.     }
  843.    
  844.     /**
  845.      * Delay for defence spawn, to prevent spam.
  846.      */
  847.     private class ScheduleDefenceTast implements Runnable
  848.     {
  849.         public ScheduleDefenceTast()
  850.         {
  851.             // Nothing
  852.         }
  853.        
  854.         public void run()
  855.         {
  856.             setCanSpawnDefenders(true);
  857.            
  858.             for (L2NpcInstance npc: _defenders)
  859.             {
  860.                 npc.deleteMe();
  861.             }
  862.            
  863.             _defenders.clear();
  864.         }
  865.     }
  866.    
  867.     /**
  868.      * @param b
  869.      * @return Enabled/Disabled
  870.      */
  871.     private String isEnabled(boolean b)
  872.     {
  873.         if (b)
  874.             return "Enabled";
  875.         else
  876.             return "Disabled";
  877.     }
  878.    
  879.     public void setOwnerClanId(int clanId)
  880.     {
  881.         _ownerClanId = clanId;
  882.     }
  883.  
  884.     public int getOwnerClanId()
  885.     {
  886.         return _ownerClanId;
  887.     }
  888.    
  889.     public void setLocationName(String name)
  890.     {
  891.         _locationName = name;
  892.     }
  893.  
  894.     public String getLocationName()
  895.     {
  896.         return _locationName;
  897.     }
  898.    
  899.     public boolean defenceAbilityEnabled()
  900.     {
  901.         return _defenceAbilityEnabled;
  902.     }
  903.    
  904.     public void setDefenceAbilityEnabled(boolean b)
  905.     {
  906.         _defenceAbilityEnabled = b;
  907.     }
  908.    
  909.     public boolean warningAbilityEnabled()
  910.     {
  911.         return _warningAbilityEnabled;
  912.     }
  913.    
  914.     public void setWarningAbilityEnabled(boolean b)
  915.     {
  916.         _warningAbilityEnabled = b;
  917.     }
  918.    
  919.     public boolean statsUpgradeEnabled()
  920.     {
  921.         return _statsUpgradeEnabled;
  922.     }
  923.    
  924.     public void setStatsEnabled(boolean b)
  925.     {
  926.         _statsUpgradeEnabled = b;
  927.     }
  928.    
  929.     public boolean canWarn()
  930.     {
  931.         return _canWarn;
  932.     }
  933.    
  934.     public void setCanWarn(boolean b)
  935.     {
  936.         _canWarn = b;
  937.     }
  938.    
  939.     public boolean canSpawnDefenders()
  940.     {
  941.         return _canSpawnDefenders;
  942.     }
  943.    
  944.     public void setCanSpawnDefenders(boolean b)
  945.     {
  946.         _canSpawnDefenders = b;
  947.     }
  948. }
  949.  
Advertisement
Add Comment
Please, Sign In to add comment