Sow.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 net.sf.l2j.gameserver.handler.skillhandlers;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.ai.CtrlIntention;
  19. import net.sf.l2j.gameserver.handler.ISkillHandler;
  20. import net.sf.l2j.gameserver.model.L2Character;
  21. import net.sf.l2j.gameserver.model.L2ItemInstance;
  22. import net.sf.l2j.gameserver.model.L2Manor;
  23. import net.sf.l2j.gameserver.model.L2Object;
  24. import net.sf.l2j.gameserver.model.L2Skill;
  25. import net.sf.l2j.gameserver.model.actor.instance.L2MonsterInstance;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  27. import net.sf.l2j.gameserver.network.SystemMessageId;
  28. import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
  29. import net.sf.l2j.gameserver.network.serverpackets.PlaySound;
  30. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  31. import net.sf.l2j.gameserver.templates.L2SkillType;
  32. import net.sf.l2j.util.Rnd;
  33. /**
  34. * @author l3x
  35. */
  36. public class Sow implements ISkillHandler
  37. {
  38. private static Logger _log = Logger.getLogger(Sow.class.getName());
  39. private static final L2SkillType[] SKILL_IDS =
  40. {
  41. L2SkillType.SOW
  42. };
  43. private L2PcInstance _activeChar;
  44. private L2MonsterInstance _target;
  45. private int _seedId;
  46. /**
  47. *
  48. * @see net.sf.l2j.gameserver.handler.ISkillHandler#useSkill(net.sf.l2j.gameserver.model.L2Character, net.sf.l2j.gameserver.model.L2Skill, net.sf.l2j.gameserver.model.L2Object[])
  49. */
  50. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  51. {
  52. if (!(activeChar instanceof L2PcInstance))
  53. return;
  54. _activeChar = (L2PcInstance) activeChar;
  55. L2Object[] targetList = skill.getTargetList(activeChar);
  56. if (targetList == null)
  57. {
  58. return;
  59. }
  60. if (Config.DEBUG)
  61. {
  62. _log.info("Casting sow");
  63. }
  64. for (int index = 0; index < targetList.length; index++)
  65. {
  66. if (!(targetList[0] instanceof L2MonsterInstance))
  67. continue;
  68. _target = (L2MonsterInstance) targetList[0];
  69. if (_target.isSeeded())
  70. {
  71. _activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  72. continue;
  73. }
  74. if (_target.isDead())
  75. {
  76. _activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  77. continue;
  78. }
  79. if (_target.getSeeder() != _activeChar)
  80. {
  81. _activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  82. continue;
  83. }
  84. _seedId = _target.getSeedType();
  85. if (_seedId == 0)
  86. {
  87. _activeChar.sendPacket(ActionFailed.STATIC_PACKET);
  88. continue;
  89. }
  90. L2ItemInstance item = _activeChar.getInventory().getItemByItemId(_seedId);
  91. //Consuming used seed
  92. _activeChar.destroyItem("Consume", item.getObjectId(), 1, null, false);
  93. SystemMessage sm = null;
  94. if (calcSuccess())
  95. {
  96. _activeChar.sendPacket(new PlaySound("Itemsound.quest_itemget"));
  97. _target.setSeeded();
  98. sm = new SystemMessage(SystemMessageId.THE_SEED_WAS_SUCCESSFULLY_SOWN);
  99. }
  100. else
  101. {
  102. sm = new SystemMessage(SystemMessageId.THE_SEED_WAS_NOT_SOWN);
  103. }
  104. if (_activeChar.getParty() == null)
  105. {
  106. _activeChar.sendPacket(sm);
  107. }
  108. else
  109. {
  110. _activeChar.getParty().broadcastToPartyMembers(sm);
  111. }
  112. //TODO: Mob should not agro on player, this way doesn't work really nice
  113. _target.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  114. }
  115. }
  116. private boolean calcSuccess()
  117. {
  118. // TODO: check all the chances
  119. int basicSuccess = (L2Manor.getInstance().isAlternative(_seedId) ? 20 : 90);
  120. int minlevelSeed = 0;
  121. int maxlevelSeed = 0;
  122. minlevelSeed = L2Manor.getInstance().getSeedMinLevel(_seedId);
  123. maxlevelSeed = L2Manor.getInstance().getSeedMaxLevel(_seedId);
  124. int levelPlayer = _activeChar.getLevel(); // Attacker Level
  125. int levelTarget = _target.getLevel(); // target Level
  126. // seed level
  127. if (levelTarget < minlevelSeed)
  128. basicSuccess -= 5 * (minlevelSeed - levelTarget);
  129. if (levelTarget > maxlevelSeed)
  130. basicSuccess -= 5 * (levelTarget - maxlevelSeed);
  131. // 5% decrease in chance if player level
  132. // is more than +/- 5 levels to _target's_ level
  133. int diff = (levelPlayer - levelTarget);
  134. if (diff < 0)
  135. diff = -diff;
  136. if (diff > 5)
  137. basicSuccess -= 5 * (diff - 5);
  138. //chance can't be less than 1%
  139. if (basicSuccess < 1)
  140. basicSuccess = 1;
  141. int rate = Rnd.nextInt(99);
  142. return (rate < basicSuccess);
  143. }
  144. /**
  145. *
  146. * @see net.sf.l2j.gameserver.handler.ISkillHandler#getSkillIds()
  147. */
  148. public L2SkillType[] getSkillIds()
  149. {
  150. return SKILL_IDS;
  151. }
  152. }