Unlock.java 6.1 KB

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