Unlock.java 5.5 KB

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