L2NpcAction.java 5.7 KB

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