L2NpcAction.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package handlers.actionhandlers;
  16. import java.util.List;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.ai.CtrlIntention;
  19. import com.l2jserver.gameserver.handler.IActionHandler;
  20. import com.l2jserver.gameserver.model.L2Object;
  21. import com.l2jserver.gameserver.model.L2Object.InstanceType;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.actor.L2Npc;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.entity.L2Event;
  26. import com.l2jserver.gameserver.model.quest.Quest;
  27. import com.l2jserver.gameserver.model.quest.Quest.QuestEventType;
  28. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  29. import com.l2jserver.gameserver.network.serverpackets.MyTargetSelected;
  30. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  31. import com.l2jserver.gameserver.network.serverpackets.ValidateLocation;
  32. import com.l2jserver.util.Rnd;
  33. public class L2NpcAction implements IActionHandler
  34. {
  35. /**
  36. * Manage actions when a player click on the L2Npc.<BR><BR>
  37. *
  38. * <B><U> Actions on first click on the L2Npc (Select it)</U> :</B><BR><BR>
  39. * <li>Set the L2Npc as target of the L2PcInstance player (if necessary)</li>
  40. * <li>Send a Server->Client packet MyTargetSelected to the L2PcInstance player (display the select window)</li>
  41. * <li>If L2Npc is autoAttackable, send a Server->Client packet StatusUpdate to the L2PcInstance in order to update L2Npc HP bar </li>
  42. * <li>Send a Server->Client packet ValidateLocation to correct the L2Npc position and heading on the client </li><BR><BR>
  43. *
  44. * <B><U> Actions on second click on the L2Npc (Attack it/Intercat with it)</U> :</B><BR><BR>
  45. * <li>Send a Server->Client packet MyTargetSelected to the L2PcInstance player (display the select window)</li>
  46. * <li>If L2Npc is autoAttackable, notify the L2PcInstance AI with AI_INTENTION_ATTACK (after a height verification)</li>
  47. * <li>If L2Npc is NOT autoAttackable, notify the L2PcInstance AI with AI_INTENTION_INTERACT (after a distance verification) and show message</li><BR><BR>
  48. *
  49. * <FONT COLOR=#FF0000><B> <U>Caution</U> : Each group of Server->Client packet must be terminated by a ActionFailed packet in order to avoid
  50. * that client wait an other packet</B></FONT><BR><BR>
  51. *
  52. * <B><U> Example of use </U> :</B><BR><BR>
  53. * <li> Client packet : Action, AttackRequest</li><BR><BR>
  54. *
  55. * @param activeChar The L2PcInstance that start an action on the L2Npc
  56. *
  57. */
  58. @Override
  59. public boolean action(L2PcInstance activeChar, L2Object target, boolean interact)
  60. {
  61. if (!((L2Npc)target).canTarget(activeChar))
  62. return false;
  63. activeChar.setLastFolkNPC((L2Npc)target);
  64. // Check if the L2PcInstance already target the L2Npc
  65. if (target != activeChar.getTarget())
  66. {
  67. // Set the target of the L2PcInstance activeChar
  68. activeChar.setTarget(target);
  69. // Check if the activeChar is attackable (without a forced attack)
  70. if (target.isAutoAttackable(activeChar))
  71. {
  72. ((L2Npc)target).getAI(); //wake up ai
  73. // Send a Server->Client packet MyTargetSelected to the L2PcInstance activeChar
  74. // The activeChar.getLevel() - getLevel() permit to display the correct color in the select window
  75. MyTargetSelected my = new MyTargetSelected(target.getObjectId(), activeChar.getLevel() - ((L2Character)target).getLevel());
  76. activeChar.sendPacket(my);
  77. // Send a Server->Client packet StatusUpdate of the L2Npc to the L2PcInstance to update its HP bar
  78. StatusUpdate su = new StatusUpdate(target);
  79. su.addAttribute(StatusUpdate.CUR_HP, (int) ((L2Character)target).getCurrentHp());
  80. su.addAttribute(StatusUpdate.MAX_HP, ((L2Character)target).getMaxHp());
  81. activeChar.sendPacket(su);
  82. }
  83. else
  84. {
  85. // Send a Server->Client packet MyTargetSelected to the L2PcInstance activeChar
  86. MyTargetSelected my = new MyTargetSelected(target.getObjectId(), 0);
  87. activeChar.sendPacket(my);
  88. }
  89. // Send a Server->Client packet ValidateLocation to correct the L2Npc position and heading on the client
  90. activeChar.sendPacket(new ValidateLocation((L2Character)target));
  91. }
  92. else if (interact)
  93. {
  94. activeChar.sendPacket(new ValidateLocation((L2Character)target));
  95. // Check if the activeChar is attackable (without a forced attack) and isn't dead
  96. if (target.isAutoAttackable(activeChar) && !((L2Character)target).isAlikeDead())
  97. {
  98. // Check the height difference
  99. if (Math.abs(activeChar.getZ() - target.getZ()) < 400) // this max heigth difference might need some tweaking
  100. {
  101. // Set the L2PcInstance Intention to AI_INTENTION_ATTACK
  102. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  103. // activeChar.startAttack(this);
  104. }
  105. else
  106. {
  107. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  108. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  109. }
  110. }
  111. else if (!target.isAutoAttackable(activeChar))
  112. {
  113. // Calculate the distance between the L2PcInstance and the L2Npc
  114. if (!((L2Npc)target).canInteract(activeChar))
  115. {
  116. // Notify the L2PcInstance AI with AI_INTENTION_INTERACT
  117. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, target);
  118. }
  119. else
  120. {
  121. L2Npc npc = (L2Npc) target;
  122. if (npc.hasRandomAnimation())
  123. npc.onRandomAnimation(Rnd.get(8));
  124. // Open a chat window on client with the text of the L2Npc
  125. if (npc.isEventMob)
  126. {
  127. L2Event.showEventHtml(activeChar, String.valueOf(target.getObjectId()));
  128. }
  129. else
  130. {
  131. List<Quest> qlsa = npc.getTemplate().getEventQuests(QuestEventType.QUEST_START);
  132. List<Quest> qlst = npc.getTemplate().getEventQuests(QuestEventType.ON_FIRST_TALK);
  133. if ((qlsa != null) && !qlsa.isEmpty())
  134. {
  135. activeChar.setLastQuestNpcObject(target.getObjectId());
  136. }
  137. if ((qlst != null) && qlst.size() == 1)
  138. {
  139. qlst.get(0).notifyFirstTalk(npc, activeChar);
  140. }
  141. else
  142. {
  143. npc.showChatWindow(activeChar);
  144. }
  145. }
  146. if (Config.PLAYER_MOVEMENT_BLOCK_TIME > 0 && !activeChar.isGM())
  147. activeChar.updateNotMoveUntil();
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. @Override
  154. public InstanceType getInstanceType()
  155. {
  156. return InstanceType.L2Npc;
  157. }
  158. }