L2NpcAction.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. public boolean action(L2PcInstance activeChar, L2Object target, boolean interact)
  57. {
  58. if (!((L2Npc)target).canTarget(activeChar))
  59. return false;
  60. activeChar.setLastFolkNPC((L2Npc)target);
  61. // Check if the L2PcInstance already target the L2Npc
  62. if (target != activeChar.getTarget())
  63. {
  64. // Set the target of the L2PcInstance activeChar
  65. activeChar.setTarget(target);
  66. // Check if the activeChar is attackable (without a forced attack)
  67. if (target.isAutoAttackable(activeChar))
  68. {
  69. ((L2Npc)target).getAI(); //wake up ai
  70. // Send a Server->Client packet MyTargetSelected to the L2PcInstance activeChar
  71. // The activeChar.getLevel() - getLevel() permit to display the correct color in the select window
  72. MyTargetSelected my = new MyTargetSelected(target.getObjectId(), activeChar.getLevel() - ((L2Character)target).getLevel());
  73. activeChar.sendPacket(my);
  74. // Send a Server->Client packet StatusUpdate of the L2Npc to the L2PcInstance to update its HP bar
  75. StatusUpdate su = new StatusUpdate(target);
  76. su.addAttribute(StatusUpdate.CUR_HP, (int) ((L2Character)target).getCurrentHp());
  77. su.addAttribute(StatusUpdate.MAX_HP, ((L2Character)target).getMaxHp());
  78. activeChar.sendPacket(su);
  79. }
  80. else
  81. {
  82. // Send a Server->Client packet MyTargetSelected to the L2PcInstance activeChar
  83. MyTargetSelected my = new MyTargetSelected(target.getObjectId(), 0);
  84. activeChar.sendPacket(my);
  85. }
  86. // Send a Server->Client packet ValidateLocation to correct the L2Npc position and heading on the client
  87. activeChar.sendPacket(new ValidateLocation((L2Character)target));
  88. }
  89. else if (interact)
  90. {
  91. activeChar.sendPacket(new ValidateLocation((L2Character)target));
  92. // Check if the activeChar is attackable (without a forced attack) and isn't dead
  93. if (target.isAutoAttackable(activeChar) && !((L2Character)target).isAlikeDead())
  94. {
  95. // Check the height difference
  96. if (Math.abs(activeChar.getZ() - target.getZ()) < 400) // this max heigth difference might need some tweaking
  97. {
  98. // Set the L2PcInstance Intention to AI_INTENTION_ATTACK
  99. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  100. // activeChar.startAttack(this);
  101. }
  102. else
  103. {
  104. // Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
  105. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  106. }
  107. }
  108. else if (!target.isAutoAttackable(activeChar))
  109. {
  110. // Calculate the distance between the L2PcInstance and the L2Npc
  111. if (!((L2Npc)target).canInteract(activeChar))
  112. {
  113. // Notify the L2PcInstance AI with AI_INTENTION_INTERACT
  114. activeChar.getAI().setIntention(CtrlIntention.AI_INTENTION_INTERACT, target);
  115. }
  116. else
  117. {
  118. if (((L2Npc)target).hasRandomAnimation())
  119. ((L2Npc)target).onRandomAnimation(Rnd.get(8));
  120. // Open a chat window on client with the text of the L2Npc
  121. if (((L2Npc)target).isEventMob)
  122. {
  123. L2Event.showEventHtml(activeChar, String.valueOf(target.getObjectId()));
  124. }
  125. else
  126. {
  127. Quest[] qlsa = ((L2Npc)target).getTemplate().getEventQuests(Quest.QuestEventType.QUEST_START);
  128. if ((qlsa != null) && qlsa.length > 0)
  129. activeChar.setLastQuestNpcObject(target.getObjectId());
  130. Quest[] qlst = ((L2Npc)target).getTemplate().getEventQuests(Quest.QuestEventType.ON_FIRST_TALK);
  131. if ((qlst != null) && qlst.length == 1)
  132. qlst[0].notifyFirstTalk((L2Npc)target, activeChar);
  133. else
  134. ((L2Npc)target).showChatWindow(activeChar);
  135. }
  136. if(Config.PLAYER_MOVEMENT_BLOCK_TIME > 0
  137. && !activeChar.isGM())
  138. activeChar.updateNotMoveUntil();
  139. }
  140. }
  141. }
  142. return true;
  143. }
  144. public InstanceType getInstanceType()
  145. {
  146. return InstanceType.L2Npc;
  147. }
  148. }