Sow.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2004-2015 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.effecthandlers;
  20. import com.l2jserver.gameserver.ai.CtrlIntention;
  21. import com.l2jserver.gameserver.enums.QuestSound;
  22. import com.l2jserver.gameserver.model.L2Seed;
  23. import com.l2jserver.gameserver.model.StatsSet;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.model.conditions.Condition;
  28. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  29. import com.l2jserver.gameserver.model.skills.BuffInfo;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  32. import com.l2jserver.util.Rnd;
  33. /**
  34. * Sow effect implementation.
  35. * @author Adry_85, l3x
  36. */
  37. public final class Sow extends AbstractEffect
  38. {
  39. public Sow(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  40. {
  41. super(attachCond, applyCond, set, params);
  42. }
  43. @Override
  44. public boolean isInstant()
  45. {
  46. return true;
  47. }
  48. @Override
  49. public void onStart(BuffInfo info)
  50. {
  51. if (!info.getEffector().isPlayer() || !info.getEffected().isMonster())
  52. {
  53. return;
  54. }
  55. final L2PcInstance player = info.getEffector().getActingPlayer();
  56. final L2MonsterInstance target = (L2MonsterInstance) info.getEffected();
  57. if (target.isDead() || (!target.getTemplate().canBeSown()) || target.isSeeded() || (target.getSeederId() != player.getObjectId()))
  58. {
  59. return;
  60. }
  61. // Consuming used seed
  62. final L2Seed seed = target.getSeed();
  63. if (!player.destroyItemByItemId("Consume", seed.getSeedId(), 1, target, false))
  64. {
  65. return;
  66. }
  67. final SystemMessage sm;
  68. if (calcSuccess(player, target, seed))
  69. {
  70. player.sendPacket(QuestSound.ITEMSOUND_QUEST_ITEMGET.getPacket());
  71. target.setSeeded(player.getActingPlayer());
  72. sm = SystemMessage.getSystemMessage(SystemMessageId.THE_SEED_WAS_SUCCESSFULLY_SOWN);
  73. }
  74. else
  75. {
  76. sm = SystemMessage.getSystemMessage(SystemMessageId.THE_SEED_WAS_NOT_SOWN);
  77. }
  78. if (player.isInParty())
  79. {
  80. player.getParty().broadcastPacket(sm);
  81. }
  82. else
  83. {
  84. player.sendPacket(sm);
  85. }
  86. // TODO: Mob should not aggro on player, this way doesn't work really nice
  87. target.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  88. }
  89. private static boolean calcSuccess(L2Character activeChar, L2Character target, L2Seed seed)
  90. {
  91. // TODO: check all the chances
  92. final int minlevelSeed = seed.getLevel() - 5;
  93. final int maxlevelSeed = seed.getLevel() + 5;
  94. final int levelPlayer = activeChar.getLevel(); // Attacker Level
  95. final int levelTarget = target.getLevel(); // target Level
  96. int basicSuccess = seed.isAlternative() ? 20 : 90;
  97. // seed level
  98. if (levelTarget < minlevelSeed)
  99. {
  100. basicSuccess -= 5 * (minlevelSeed - levelTarget);
  101. }
  102. if (levelTarget > maxlevelSeed)
  103. {
  104. basicSuccess -= 5 * (levelTarget - maxlevelSeed);
  105. }
  106. // 5% decrease in chance if player level
  107. // is more than +/- 5 levels to _target's_ level
  108. int diff = (levelPlayer - levelTarget);
  109. if (diff < 0)
  110. {
  111. diff = -diff;
  112. }
  113. if (diff > 5)
  114. {
  115. basicSuccess -= 5 * (diff - 5);
  116. }
  117. // chance can't be less than 1%
  118. Math.max(basicSuccess, 1);
  119. return Rnd.nextInt(99) < basicSuccess;
  120. }
  121. }