Confuse.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2004-2013 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.actor.L2Character;
  26. import com.l2jserver.gameserver.model.effects.EffectFlag;
  27. import com.l2jserver.gameserver.model.effects.EffectTemplate;
  28. import com.l2jserver.gameserver.model.effects.L2Effect;
  29. import com.l2jserver.gameserver.model.effects.L2EffectType;
  30. import com.l2jserver.gameserver.model.stats.Env;
  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 class Confuse extends L2Effect
  38. {
  39. private final int _chance;
  40. public Confuse(Env env, EffectTemplate template)
  41. {
  42. super(env, template);
  43. _chance = template.hasParameters() ? template.getParameters().getInt("chance", 100) : 100;
  44. }
  45. @Override
  46. public boolean calcSuccess()
  47. {
  48. return Formulas.calcProbability(_chance, getEffector(), getEffected(), getSkill());
  49. }
  50. @Override
  51. public int getEffectFlags()
  52. {
  53. return EffectFlag.CONFUSED.getMask();
  54. }
  55. @Override
  56. public L2EffectType getEffectType()
  57. {
  58. return L2EffectType.NONE;
  59. }
  60. @Override
  61. public boolean isInstant()
  62. {
  63. return true;
  64. }
  65. @Override
  66. public boolean onActionTime()
  67. {
  68. final List<L2Character> targetList = new ArrayList<>();
  69. // Getting the possible targets
  70. for (L2Object obj : getEffected().getKnownList().getKnownObjects().values())
  71. {
  72. if (((getEffected().isMonster() && obj.isL2Attackable()) || (obj instanceof L2Character)) && (obj != getEffected()))
  73. {
  74. targetList.add((L2Character) obj);
  75. }
  76. }
  77. // if there is no target, exit function
  78. if (!targetList.isEmpty())
  79. {
  80. // Choosing randomly a new target
  81. final L2Character target = targetList.get(Rnd.nextInt(targetList.size()));
  82. // Attacking the target
  83. getEffected().setTarget(target);
  84. getEffected().getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  85. return true;
  86. }
  87. return false;
  88. }
  89. @Override
  90. public void onExit()
  91. {
  92. if (!getEffected().isPlayer())
  93. {
  94. getEffected().getAI().notifyEvent(CtrlEvent.EVT_THINK);
  95. }
  96. }
  97. @Override
  98. public boolean onStart()
  99. {
  100. getEffected().getAI().notifyEvent(CtrlEvent.EVT_CONFUSED);
  101. return true;
  102. }
  103. }