Unlock.java 5.8 KB

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