L2NpcAction.java 6.7 KB

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