Unlock.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.skillhandlers;
  16. import com.l2jserver.gameserver.ai.CtrlIntention;
  17. import com.l2jserver.gameserver.handler.ISkillHandler;
  18. import com.l2jserver.gameserver.instancemanager.InstanceManager;
  19. import com.l2jserver.gameserver.model.L2Object;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.model.actor.instance.L2ChestInstance;
  22. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  23. import com.l2jserver.gameserver.model.entity.Instance;
  24. import com.l2jserver.gameserver.model.skills.L2Skill;
  25. import com.l2jserver.gameserver.model.skills.L2SkillType;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  28. import com.l2jserver.util.Rnd;
  29. public class Unlock implements ISkillHandler
  30. {
  31. private static final L2SkillType[] SKILL_IDS =
  32. {
  33. L2SkillType.UNLOCK,
  34. L2SkillType.UNLOCK_SPECIAL
  35. };
  36. @Override
  37. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  38. {
  39. L2Object[] targetList = skill.getTargetList(activeChar);
  40. if (targetList == null)
  41. return;
  42. for (L2Object target: targets)
  43. {
  44. if (target instanceof L2DoorInstance)
  45. {
  46. L2DoorInstance door = (L2DoorInstance) target;
  47. // Check if door in the different instance
  48. if (activeChar.getInstanceId() != door.getInstanceId())
  49. {
  50. // Search for the instance
  51. final Instance inst = InstanceManager.getInstance().getInstance(activeChar.getInstanceId());
  52. if (inst == null)
  53. {
  54. // Instance not found
  55. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  56. return;
  57. }
  58. for (L2DoorInstance instanceDoor : inst.getDoors())
  59. {
  60. if (instanceDoor.getDoorId() == door.getDoorId())
  61. {
  62. // Door found
  63. door = instanceDoor;
  64. break;
  65. }
  66. }
  67. // Checking instance again
  68. if (activeChar.getInstanceId() != door.getInstanceId())
  69. {
  70. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  71. return;
  72. }
  73. }
  74. if ((!door.isUnlockable() && skill.getSkillType() != L2SkillType.UNLOCK_SPECIAL)
  75. || door.getFort() != null)
  76. {
  77. activeChar.sendPacket(SystemMessageId.UNABLE_TO_UNLOCK_DOOR);
  78. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  79. return;
  80. }
  81. if (doorUnlock(skill) && (!door.getOpen()))
  82. {
  83. door.openMe();
  84. if(skill.getAfterEffectId() == 0)
  85. door.onOpen();
  86. }
  87. else
  88. activeChar.sendPacket(SystemMessageId.FAILED_TO_UNLOCK_DOOR);
  89. }
  90. else if (target instanceof L2ChestInstance)
  91. {
  92. L2ChestInstance chest = (L2ChestInstance) target;
  93. if ((chest.getCurrentHp() <= 0) || chest.isInteracted() || activeChar.getInstanceId() != chest.getInstanceId())
  94. {
  95. activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  96. return;
  97. }
  98. chest.setInteracted();
  99. if (chestUnlock(skill, chest))
  100. {
  101. activeChar.broadcastSocialAction(3);
  102. chest.setSpecialDrop();
  103. chest.setMustRewardExpSp(false);
  104. chest.reduceCurrentHp(99999999, activeChar, skill);
  105. }
  106. else
  107. {
  108. activeChar.broadcastSocialAction(13);
  109. chest.addDamageHate(activeChar, 0, 1);
  110. chest.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, activeChar);
  111. if (chestTrap(chest))
  112. chest.chestTrap(activeChar);
  113. }
  114. }
  115. }
  116. }
  117. private static final boolean doorUnlock(L2Skill skill)
  118. {
  119. if (skill.getSkillType() == L2SkillType.UNLOCK_SPECIAL)
  120. return Rnd.get(100) < skill.getPower();
  121. switch (skill.getLevel())
  122. {
  123. case 0:
  124. return false;
  125. case 1:
  126. return Rnd.get(120) < 30;
  127. case 2:
  128. return Rnd.get(120) < 50;
  129. case 3:
  130. return Rnd.get(120) < 75;
  131. default:
  132. return Rnd.get(120) < 100;
  133. }
  134. }
  135. private static final boolean chestUnlock(L2Skill skill, L2Character chest)
  136. {
  137. int chance = 0;
  138. if (chest.getLevel() > 60)
  139. {
  140. if (skill.getLevel() < 10)
  141. return false;
  142. chance = (skill.getLevel() - 10) * 5 + 30;
  143. }
  144. else if (chest.getLevel() > 40)
  145. {
  146. if (skill.getLevel() < 6)
  147. return false;
  148. chance = (skill.getLevel() - 6) * 5 + 10;
  149. }
  150. else if (chest.getLevel() > 30)
  151. {
  152. if (skill.getLevel() < 3)
  153. return false;
  154. if (skill.getLevel() > 12)
  155. return true;
  156. chance = (skill.getLevel() - 3) * 5 + 30;
  157. }
  158. else
  159. {
  160. if (skill.getLevel() > 10)
  161. return true;
  162. chance = skill.getLevel() * 5 + 35;
  163. }
  164. chance = Math.min(chance, 50);
  165. return Rnd.get(100) < chance;
  166. }
  167. private static final boolean chestTrap(L2Character chest)
  168. {
  169. if (chest.getLevel() > 60)
  170. return Rnd.get(100) < 80;
  171. if (chest.getLevel() > 40)
  172. return Rnd.get(100) < 50;
  173. if (chest.getLevel() > 30)
  174. return Rnd.get(100) < 30;
  175. return Rnd.get(100) < 10;
  176. }
  177. @Override
  178. public L2SkillType[] getSkillIds()
  179. {
  180. return SKILL_IDS;
  181. }
  182. }