Confuse.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 java.util.ArrayList;
  21. import java.util.List;
  22. import com.l2jserver.gameserver.ai.CtrlEvent;
  23. import com.l2jserver.gameserver.ai.CtrlIntention;
  24. import com.l2jserver.gameserver.model.L2Object;
  25. import com.l2jserver.gameserver.model.StatsSet;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.conditions.Condition;
  28. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  29. import com.l2jserver.gameserver.model.effects.EffectFlag;
  30. import com.l2jserver.gameserver.model.skills.BuffInfo;
  31. import com.l2jserver.gameserver.model.stats.Formulas;
  32. import com.l2jserver.util.Rnd;
  33. /**
  34. * Confuse effect implementation.
  35. * @author littlecrow
  36. */
  37. public final class Confuse extends AbstractEffect
  38. {
  39. private final int _chance;
  40. public Confuse(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  41. {
  42. super(attachCond, applyCond, set, params);
  43. _chance = params.getInt("chance", 100);
  44. }
  45. @Override
  46. public boolean calcSuccess(BuffInfo info)
  47. {
  48. return Formulas.calcProbability(_chance, info.getEffector(), info.getEffected(), info.getSkill());
  49. }
  50. @Override
  51. public int getEffectFlags()
  52. {
  53. return EffectFlag.CONFUSED.getMask();
  54. }
  55. @Override
  56. public boolean isInstant()
  57. {
  58. return true;
  59. }
  60. @Override
  61. public void onExit(BuffInfo info)
  62. {
  63. if (!info.getEffected().isPlayer())
  64. {
  65. info.getEffected().getAI().notifyEvent(CtrlEvent.EVT_THINK);
  66. }
  67. }
  68. @Override
  69. public void onStart(BuffInfo info)
  70. {
  71. info.getEffected().getAI().notifyEvent(CtrlEvent.EVT_CONFUSED);
  72. final List<L2Character> targetList = new ArrayList<>();
  73. // Getting the possible targets
  74. for (L2Object obj : info.getEffected().getKnownList().getKnownObjects().values())
  75. {
  76. if (((info.getEffected().isMonster() && obj.isAttackable()) || (obj instanceof L2Character)) && (obj != info.getEffected()))
  77. {
  78. targetList.add((L2Character) obj);
  79. }
  80. }
  81. // if there is no target, exit function
  82. if (!targetList.isEmpty())
  83. {
  84. // Choosing randomly a new target
  85. final L2Character target = targetList.get(Rnd.nextInt(targetList.size()));
  86. // Attacking the target
  87. info.getEffected().setTarget(target);
  88. info.getEffected().getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  89. }
  90. }
  91. }