L2NpcAction.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 handlers.actionhandlers;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.GeoData;
  22. import com.l2jserver.gameserver.ai.CtrlIntention;
  23. import com.l2jserver.gameserver.enums.InstanceType;
  24. import com.l2jserver.gameserver.handler.IActionHandler;
  25. import com.l2jserver.gameserver.model.L2Object;
  26. import com.l2jserver.gameserver.model.Location;
  27. import com.l2jserver.gameserver.model.actor.L2Npc;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.entity.L2Event;
  30. import com.l2jserver.gameserver.model.events.EventDispatcher;
  31. import com.l2jserver.gameserver.model.events.EventType;
  32. import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcFirstTalk;
  33. import com.l2jserver.gameserver.network.serverpackets.MoveToPawn;
  34. import com.l2jserver.util.Rnd;
  35. public class L2NpcAction implements IActionHandler
  36. {
  37. /**
  38. * Manage actions when a player click on the L2Npc.<BR>
  39. * <BR>
  40. * <B><U> Actions on first click on the L2Npc (Select it)</U> :</B><BR>
  41. * <BR>
  42. * <li>Set the L2Npc as target of the L2PcInstance player (if necessary)</li> <li>Send a Server->Client packet MyTargetSelected to the L2PcInstance player (display the select window)</li> <li>If L2Npc is autoAttackable, send a Server->Client packet StatusUpdate to the L2PcInstance in order to
  43. * update L2Npc HP bar</li> <li>Send a Server->Client packet ValidateLocation to correct the L2Npc position and heading on the client</li><BR>
  44. * <BR>
  45. * <B><U> Actions on second click on the L2Npc (Attack it/Intercat with it)</U> :</B><BR>
  46. * <BR>
  47. * <li>Send a Server->Client packet MyTargetSelected to the L2PcInstance player (display the select window)</li> <li>If L2Npc is autoAttackable, notify the L2PcInstance AI with AI_INTENTION_ATTACK (after a height verification)</li> <li>If L2Npc is NOT autoAttackable, notify the L2PcInstance AI
  48. * with AI_INTENTION_INTERACT (after a distance verification) and show message</li><BR>
  49. * <BR>
  50. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Each group of Server->Client packet must be terminated by a ActionFailed packet in order to avoid that client wait an other packet</B></FONT><BR>
  51. * <BR>
  52. * <B><U> Example of use </U> :</B><BR>
  53. * <BR>
  54. * <li>Client packet : Action, AttackRequest</li><BR>
  55. * <BR>
  56. * @param activeChar The L2PcInstance that start an action on the L2Npc
  57. */
  58. @Override
  59. public boolean action(L2PcInstance activeChar, L2Object target, boolean interact)
  60. {
  61. final L2Npc npc = (L2Npc) target;
  62. if (!npc.canTarget(activeChar))
  63. {
  64. return false;
  65. }
  66. activeChar.setLastFolkNPC(npc);
  67. // Check if the L2PcInstance already target the L2Npc
  68. if (npc != activeChar.getTarget())
  69. {
  70. // Set the target of the L2PcInstance activeChar
  71. activeChar.setTarget(npc);
  72. // Check if the activeChar is attackable (without a forced attack)
  73. if (npc.isAutoAttackable(activeChar))
  74. {
  75. npc.getAI(); // wake up ai
  76. }
  77. }
  78. else if (interact)
  79. {
  80. // Check if the activeChar is attackable (without a forced attack) and isn't dead
  81. if (npc.isAutoAttackable(activeChar) && !npc.isAlikeDead())
  82. {
  83. if (GeoData.getInstance().canSeeTarget(activeChar, npc))
  84. {
  85. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, npc);
  86. }
  87. else
  88. {
  89. final Location destination = GeoData.getInstance().moveCheck(activeChar, npc);
  90. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, destination);
  91. }
  92. }
  93. else if (!npc.isAutoAttackable(activeChar))
  94. {
  95. if (!GeoData.getInstance().canSeeTarget(activeChar, npc))
  96. {
  97. final Location destination = GeoData.getInstance().moveCheck(activeChar, npc);
  98. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, destination);
  99. return true;
  100. }
  101. // Verifies if the NPC can interact with the player.
  102. if (!npc.canInteract(activeChar))
  103. {
  104. // Notify the L2PcInstance AI with AI_INTENTION_INTERACT
  105. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, npc);
  106. }
  107. else
  108. {
  109. // Turn NPC to the player.
  110. activeChar.sendPacket(new MoveToPawn(activeChar, npc, 100));
  111. if (npc.hasRandomAnimation())
  112. {
  113. npc.onRandomAnimation(Rnd.get(8));
  114. }
  115. // Open a chat window on client with the text of the L2Npc
  116. if (npc.isEventMob())
  117. {
  118. L2Event.showEventHtml(activeChar, String.valueOf(npc.getObjectId()));
  119. }
  120. else
  121. {
  122. if (npc.hasListener(EventType.ON_NPC_QUEST_START))
  123. {
  124. activeChar.setLastQuestNpcObject(npc.getObjectId());
  125. }
  126. if (npc.hasListener(EventType.ON_NPC_FIRST_TALK))
  127. {
  128. EventDispatcher.getInstance().notifyEventAsync(new OnNpcFirstTalk(npc, activeChar), npc);
  129. }
  130. else
  131. {
  132. npc.showChatWindow(activeChar);
  133. }
  134. }
  135. if ((Config.PLAYER_MOVEMENT_BLOCK_TIME > 0) && !activeChar.isGM())
  136. {
  137. activeChar.updateNotMoveUntil();
  138. }
  139. }
  140. }
  141. }
  142. return true;
  143. }
  144. @Override
  145. public InstanceType getInstanceType()
  146. {
  147. return InstanceType.L2Npc;
  148. }
  149. }