Unlock.java 5.7 KB

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