Sow.java 5.1 KB

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