Confusion.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.util.Rnd;
  32. /**
  33. * Confusion effect implementation.
  34. * @author littlecrow
  35. */
  36. public class Confusion extends L2Effect
  37. {
  38. public Confusion(Env env, EffectTemplate template)
  39. {
  40. super(env, template);
  41. }
  42. @Override
  43. public int getEffectFlags()
  44. {
  45. return EffectFlag.CONFUSED.getMask();
  46. }
  47. @Override
  48. public L2EffectType getEffectType()
  49. {
  50. return L2EffectType.CONFUSION;
  51. }
  52. @Override
  53. public boolean isInstant()
  54. {
  55. return true;
  56. }
  57. @Override
  58. public boolean onActionTime()
  59. {
  60. final List<L2Character> targetList = new ArrayList<>();
  61. // Getting the possible targets
  62. for (L2Object obj : getEffected().getKnownList().getKnownObjects().values())
  63. {
  64. if (((getEffected().isMonster() && obj.isL2Attackable()) || (obj instanceof L2Character)) && (obj != getEffected()))
  65. {
  66. targetList.add((L2Character) obj);
  67. }
  68. }
  69. // if there is no target, exit function
  70. if (!targetList.isEmpty())
  71. {
  72. // Choosing randomly a new target
  73. final L2Character target = targetList.get(Rnd.nextInt(targetList.size()));
  74. // Attacking the target
  75. getEffected().setTarget(target);
  76. getEffected().getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  77. return true;
  78. }
  79. return false;
  80. }
  81. @Override
  82. public void onExit()
  83. {
  84. if (!getEffected().isPlayer())
  85. {
  86. getEffected().getAI().notifyEvent(CtrlEvent.EVT_THINK);
  87. }
  88. }
  89. @Override
  90. public boolean onStart()
  91. {
  92. getEffected().getAI().notifyEvent(CtrlEvent.EVT_CONFUSED);
  93. return true;
  94. }
  95. }