Advertisement
Guest User

Untitled

a guest
May 5th, 2021
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This file is part of the L2J Mobius project.
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12.  * General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17. package org.l2jmobius.gameserver.model.actor.instance;
  18.  
  19. import org.l2jmobius.Config;
  20. import org.l2jmobius.gameserver.ai.CtrlIntention;
  21. import org.l2jmobius.gameserver.enums.InstanceType;
  22. import org.l2jmobius.gameserver.model.World;
  23. import org.l2jmobius.gameserver.model.actor.Attackable;
  24. import org.l2jmobius.gameserver.model.actor.Creature;
  25. import org.l2jmobius.gameserver.model.actor.templates.NpcTemplate;
  26. import org.l2jmobius.gameserver.model.events.EventDispatcher;
  27. import org.l2jmobius.gameserver.model.events.EventType;
  28. import org.l2jmobius.gameserver.model.events.impl.creature.npc.OnNpcFirstTalk;
  29. import org.l2jmobius.gameserver.model.skills.Skill;
  30. import org.l2jmobius.gameserver.network.serverpackets.ActionFailed;
  31.  
  32. /**
  33.  * This class manages all Guards in the world. It inherits all methods from Attackable and adds some more such as tracking PK and aggressive MonsterInstance.
  34.  */
  35. public class GuardInstance extends Attackable
  36. {
  37.     /**
  38.      * Creates a guard.
  39.      * @param template the guard NPC template
  40.      */
  41.     public GuardInstance(NpcTemplate template)
  42.     {
  43.         super(template);
  44.         setInstanceType(InstanceType.GuardInstance);
  45.     }
  46.    
  47.     @Override
  48.     public boolean isAutoAttackable(Creature attacker)
  49.     {
  50.         if (attacker.isMonster() && !attacker.isFakePlayer())
  51.         {
  52.             return true;
  53.         }
  54.         if (Config.FACTION_SYSTEM_ENABLED && Config.FACTION_GUARDS_ENABLED && attacker.isPlayable())
  55.         {
  56.             final PlayerInstance player = attacker.getActingPlayer();
  57.             if ((player.isGood() && getTemplate().isClan(Config.FACTION_EVIL_TEAM_NAME)) || (player.isEvil() && getTemplate().isClan(Config.FACTION_GOOD_TEAM_NAME)))
  58.             {
  59.                 return true;
  60.             }
  61.         }
  62.         return super.isAutoAttackable(attacker);
  63.     }
  64.    
  65.     @Override
  66.     public void addDamage(Creature attacker, int damage, Skill skill)
  67.     {
  68.         super.addDamage(attacker, damage, skill);
  69.         getAI().startFollow(attacker);
  70.         addDamageHate(attacker, 0, 10);
  71.         World.getInstance().forEachVisibleObjectInRange(this, GuardInstance.class, 500, guard ->
  72.         {
  73.             guard.getAI().startFollow(attacker);
  74.             guard.addDamageHate(attacker, 0, 10);
  75.         });
  76.     }
  77.    
  78.     /**
  79.      * Set the home location of its GuardInstance.
  80.      */
  81.     @Override
  82.     public void onSpawn()
  83.     {
  84.         setRandomWalking(false);
  85.         super.onSpawn();
  86.        
  87.         getAI().setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
  88.         // check the region where this mob is, do not activate the AI if region is inactive.
  89.         // final WorldRegion region = World.getInstance().getRegion(this);
  90.         // if ((region != null) && (!region.isActive()))
  91.         // {
  92.         // getAI().stopAITask();
  93.         // }
  94.     }
  95.    
  96.     /**
  97.      * Return the pathfile of the selected HTML file in function of the GuardInstance Identifier and of the page number.<br>
  98.      * <br>
  99.      * <b><u>Format of the pathfile</u>:</b>
  100.      * <ul>
  101.      * <li>if page number = 0 : <b>data/html/guard/12006.htm</b> (npcId-page number)</li>
  102.      * <li>if page number > 0 : <b>data/html/guard/12006-1.htm</b> (npcId-page number)</li>
  103.      * </ul>
  104.      * @param npcId The Identifier of the NpcInstance whose text must be display
  105.      * @param value The number of the page to display
  106.      */
  107.     @Override
  108.     public String getHtmlPath(int npcId, int value)
  109.     {
  110.         String pom = "";
  111.         if (value == 0)
  112.         {
  113.             pom = Integer.toString(npcId);
  114.         }
  115.         else
  116.         {
  117.             pom = npcId + "-" + value;
  118.         }
  119.         return "data/html/guard/" + pom + ".htm";
  120.     }
  121.    
  122.     /**
  123.      * Manage actions when a player click on the GuardInstance.<br>
  124.      * <br>
  125.      * <b><u>Actions on first click on the GuardInstance (Select it)</u>:</b>
  126.      * <ul>
  127.      * <li>Set the GuardInstance as target of the PlayerInstance player (if necessary)</li>
  128.      * <li>Send a Server->Client packet MyTargetSelected to the PlayerInstance player (display the select window)</li>
  129.      * <li>Set the PlayerInstance Intention to AI_INTENTION_IDLE</li>
  130.      * <li>Send a Server->Client packet ValidateLocation to correct the GuardInstance position and heading on the client</li>
  131.      * </ul>
  132.      * <br>
  133.      * <b><u>Actions on second click on the GuardInstance (Attack it/Interact with it)</u>:</b>
  134.      * <ul>
  135.      * <li>If PlayerInstance is in the _aggroList of the GuardInstance, set the PlayerInstance Intention to AI_INTENTION_ATTACK</li>
  136.      * <li>If PlayerInstance is NOT in the _aggroList of the GuardInstance, set the PlayerInstance Intention to AI_INTENTION_INTERACT (after a distance verification) and show message</li>
  137.      * </ul>
  138.      * <br>
  139.      * <b><u>Example of use</u>:</b>
  140.      * <ul>
  141.      * <li>Client packet : Action, AttackRequest</li>
  142.      * </ul>
  143.      * @param player The PlayerInstance that start an action on the GuardInstance
  144.      */
  145.     @Override
  146.     public void onAction(PlayerInstance player, boolean interactValue)
  147.     {
  148.         if (!canTarget(player))
  149.         {
  150.             return;
  151.         }
  152.        
  153.         boolean interact = interactValue;
  154.         if (Config.FACTION_SYSTEM_ENABLED && Config.FACTION_GUARDS_ENABLED && ((player.isGood() && getTemplate().isClan(Config.FACTION_EVIL_TEAM_NAME)) || (player.isEvil() && getTemplate().isClan(Config.FACTION_GOOD_TEAM_NAME))))
  155.         {
  156.             interact = false;
  157.             // TODO: Fix normal targeting
  158.             player.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, this);
  159.         }
  160.        
  161.         if (isFakePlayer() && isInCombat())
  162.         {
  163.             interact = false;
  164.             // TODO: Fix normal targeting
  165.             player.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, this);
  166.         }
  167.        
  168.         // Check if the PlayerInstance already target the GuardInstance
  169.         if (getObjectId() != player.getTargetId())
  170.         {
  171.             // Set the target of the PlayerInstance player
  172.             player.setTarget(this);
  173.         }
  174.         else if (interact)
  175.         {
  176.             // Check if the PlayerInstance is in the _aggroList of the GuardInstance
  177.             if (isInAggroList(player))
  178.             {
  179.                 // Set the PlayerInstance Intention to AI_INTENTION_ATTACK
  180.                 player.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, this);
  181.             }
  182.             else
  183.             {
  184.                 // Calculate the distance between the PlayerInstance and the NpcInstance
  185.                 if (!canInteract(player))
  186.                 {
  187.                     // Set the PlayerInstance Intention to AI_INTENTION_INTERACT
  188.                     player.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, this);
  189.                 }
  190.                 else
  191.                 {
  192.                     player.setLastFolkNPC(this);
  193.                    
  194.                     // Open a chat window on client with the text of the GuardInstance
  195.                     if (hasListener(EventType.ON_NPC_QUEST_START))
  196.                     {
  197.                         player.setLastQuestNpcObject(getObjectId());
  198.                     }
  199.                    
  200.                     if (hasListener(EventType.ON_NPC_FIRST_TALK))
  201.                     {
  202.                         EventDispatcher.getInstance().notifyEventAsync(new OnNpcFirstTalk(this, player), this);
  203.                     }
  204.                     else
  205.                     {
  206.                         showChatWindow(player, 0);
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.        
  212.         // Send a Server->Client ActionFailed to the PlayerInstance in order to avoid that the client wait another packet
  213.         player.sendPacket(ActionFailed.STATIC_PACKET);
  214.     }
  215. }
  216.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement