L2NpcAction.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2004-2013 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 java.util.List;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.ai.CtrlIntention;
  23. import com.l2jserver.gameserver.handler.IActionHandler;
  24. import com.l2jserver.gameserver.model.L2Object;
  25. import com.l2jserver.gameserver.model.L2Object.InstanceType;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  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.quest.Quest;
  31. import com.l2jserver.gameserver.model.quest.Quest.QuestEventType;
  32. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  33. import com.l2jserver.gameserver.network.serverpackets.MyTargetSelected;
  34. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  35. import com.l2jserver.gameserver.network.serverpackets.ValidateLocation;
  36. import com.l2jserver.util.Rnd;
  37. public class L2NpcAction implements IActionHandler
  38. {
  39. /**
  40. * Manage actions when a player click on the L2Npc.<BR>
  41. * <BR>
  42. * <B><U> Actions on first click on the L2Npc (Select it)</U> :</B><BR>
  43. * <BR>
  44. * <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
  45. * update L2Npc HP bar</li> <li>Send a Server->Client packet ValidateLocation to correct the L2Npc position and heading on the client</li><BR>
  46. * <BR>
  47. * <B><U> Actions on second click on the L2Npc (Attack it/Intercat with it)</U> :</B><BR>
  48. * <BR>
  49. * <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
  50. * with AI_INTENTION_INTERACT (after a distance verification) and show message</li><BR>
  51. * <BR>
  52. * <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>
  53. * <BR>
  54. * <B><U> Example of use </U> :</B><BR>
  55. * <BR>
  56. * <li>Client packet : Action, AttackRequest</li><BR>
  57. * <BR>
  58. * @param activeChar The L2PcInstance that start an action on the L2Npc
  59. */
  60. @Override
  61. public boolean action(L2PcInstance activeChar, L2Object target, boolean interact)
  62. {
  63. if (!((L2Npc) target).canTarget(activeChar))
  64. {
  65. return false;
  66. }
  67. activeChar.setLastFolkNPC((L2Npc) target);
  68. // Check if the L2PcInstance already target the L2Npc
  69. if (target != activeChar.getTarget())
  70. {
  71. // Set the target of the L2PcInstance activeChar
  72. activeChar.setTarget(target);
  73. // Check if the activeChar is attackable (without a forced attack)
  74. if (target.isAutoAttackable(activeChar))
  75. {
  76. ((L2Npc) target).getAI(); // wake up ai
  77. // Send a Server->Client packet MyTargetSelected to the L2PcInstance activeChar
  78. // The activeChar.getLevel() - getLevel() permit to display the correct color in the select window
  79. MyTargetSelected my = new MyTargetSelected(target.getObjectId(), activeChar.getLevel() - ((L2Character) target).getLevel());
  80. activeChar.sendPacket(my);
  81. // Send a Server->Client packet StatusUpdate of the L2Npc to the L2PcInstance to update its HP bar
  82. StatusUpdate su = new StatusUpdate(target);
  83. su.addAttribute(StatusUpdate.CUR_HP, (int) ((L2Character) target).getCurrentHp());
  84. su.addAttribute(StatusUpdate.MAX_HP, ((L2Character) target).getMaxHp());
  85. activeChar.sendPacket(su);
  86. }
  87. else
  88. {
  89. // Send a Server->Client packet MyTargetSelected to the L2PcInstance activeChar
  90. MyTargetSelected my = new MyTargetSelected(target.getObjectId(), 0);
  91. activeChar.sendPacket(my);
  92. }
  93. // Send a Server->Client packet ValidateLocation to correct the L2Npc position and heading on the client
  94. activeChar.sendPacket(new ValidateLocation((L2Character) target));
  95. }
  96. else if (interact)
  97. {
  98. activeChar.sendPacket(new ValidateLocation((L2Character) target));
  99. // Check if the activeChar is attackable (without a forced attack) and isn't dead
  100. if (target.isAutoAttackable(activeChar) && !((L2Character) target).isAlikeDead())
  101. {
  102. // Check the height difference
  103. if (Math.abs(activeChar.getZ() - target.getZ()) < 400) // this max heigth difference might need some tweaking
  104. {
  105. // Set the L2PcInstance Intention to AI_INTENTION_ATTACK
  106. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  107. // activeChar.startAttack(this);
  108. }
  109. else
  110. {
  111. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  112. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  113. }
  114. }
  115. else if (!target.isAutoAttackable(activeChar))
  116. {
  117. // Calculate the distance between the L2PcInstance and the L2Npc
  118. if (!((L2Npc) target).canInteract(activeChar))
  119. {
  120. // Notify the L2PcInstance AI with AI_INTENTION_INTERACT
  121. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, target);
  122. }
  123. else
  124. {
  125. L2Npc npc = (L2Npc) target;
  126. if (npc.hasRandomAnimation())
  127. {
  128. npc.onRandomAnimation(Rnd.get(8));
  129. }
  130. // Open a chat window on client with the text of the L2Npc
  131. if (npc.isEventMob())
  132. {
  133. L2Event.showEventHtml(activeChar, String.valueOf(target.getObjectId()));
  134. }
  135. else
  136. {
  137. List<Quest> qlsa = npc.getTemplate().getEventQuests(QuestEventType.QUEST_START);
  138. List<Quest> qlst = npc.getTemplate().getEventQuests(QuestEventType.ON_FIRST_TALK);
  139. if ((qlsa != null) && !qlsa.isEmpty())
  140. {
  141. activeChar.setLastQuestNpcObject(target.getObjectId());
  142. }
  143. if ((qlst != null) && (qlst.size() == 1))
  144. {
  145. qlst.get(0).notifyFirstTalk(npc, activeChar);
  146. }
  147. else
  148. {
  149. npc.showChatWindow(activeChar);
  150. }
  151. }
  152. if ((Config.PLAYER_MOVEMENT_BLOCK_TIME > 0) && !activeChar.isGM())
  153. {
  154. activeChar.updateNotMoveUntil();
  155. }
  156. }
  157. }
  158. }
  159. return true;
  160. }
  161. @Override
  162. public InstanceType getInstanceType()
  163. {
  164. return InstanceType.L2Npc;
  165. }
  166. }