L2NpcAction.java 6.9 KB

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