AreaFriendly.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.targethandlers;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.Comparator;
  24. import java.util.List;
  25. import com.l2jserver.Config;
  26. import com.l2jserver.gameserver.GeoData;
  27. import com.l2jserver.gameserver.handler.ITargetTypeHandler;
  28. import com.l2jserver.gameserver.model.L2Object;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.actor.instance.L2SiegeFlagInstance;
  31. import com.l2jserver.gameserver.model.skills.L2Skill;
  32. import com.l2jserver.gameserver.model.skills.targets.L2TargetType;
  33. import com.l2jserver.gameserver.network.SystemMessageId;
  34. import com.l2jserver.util.Rnd;
  35. /**
  36. * @author Adry_85
  37. */
  38. public class AreaFriendly implements ITargetTypeHandler
  39. {
  40. @Override
  41. public L2Object[] getTargetList(L2Skill skill, L2Character activeChar, boolean onlyFirst, L2Character target)
  42. {
  43. List<L2Character> targetList = new ArrayList<>();
  44. if (!checkTarget(activeChar, target) && (skill.getCastRange() >= 0))
  45. {
  46. activeChar.sendPacket(SystemMessageId.TARGET_IS_INCORRECT);
  47. return _emptyTargetList;
  48. }
  49. if (skill.getCastRange() >= 0)
  50. {
  51. if (onlyFirst)
  52. {
  53. return new L2Character[]
  54. {
  55. target
  56. };
  57. }
  58. if (activeChar.getActingPlayer().isInOlympiadMode())
  59. {
  60. return new L2Character[]
  61. {
  62. activeChar
  63. };
  64. }
  65. targetList.add(target); // Add target to target list
  66. }
  67. if (target != null)
  68. {
  69. int[] affectLimit = skill.getAffectLimit();
  70. // calculate maximum affect limit between min and max values
  71. int randomMax = Rnd.get(affectLimit[0], affectLimit[1]);
  72. int curTargets = 0;
  73. final Collection<L2Character> objs = target.getKnownList().getKnownCharactersInRadius(skill.getAffectRange());
  74. // TODO: Chain Heal - The recovery amount decreases starting from the most injured person.
  75. Collections.sort(targetList, new CharComparator());
  76. for (L2Character obj : objs)
  77. {
  78. if (!checkTarget(activeChar, obj) || (obj == activeChar))
  79. {
  80. continue;
  81. }
  82. targetList.add(obj);
  83. curTargets++;
  84. if (curTargets >= randomMax)
  85. {
  86. break;
  87. }
  88. }
  89. }
  90. if (targetList.isEmpty())
  91. {
  92. return _emptyTargetList;
  93. }
  94. return targetList.toArray(new L2Character[targetList.size()]);
  95. }
  96. private boolean checkTarget(L2Character activeChar, L2Character target)
  97. {
  98. if ((Config.GEODATA > 0) && !GeoData.getInstance().canSeeTarget(activeChar, target))
  99. {
  100. return false;
  101. }
  102. if ((target == null) || target.isDead() || target.isAlikeDead() || target.isDoor() || (target instanceof L2SiegeFlagInstance) || target.isMonster())
  103. {
  104. return false;
  105. }
  106. if ((target.getActingPlayer() != null) && (target.getActingPlayer().inObserverMode() || target.getActingPlayer().isInOlympiadMode()))
  107. {
  108. return false;
  109. }
  110. if ((activeChar.getActingPlayer().getClan() != null) && (target.getActingPlayer().getClan() != null))
  111. {
  112. if (activeChar.getActingPlayer().getClanId() != target.getActingPlayer().getClanId())
  113. {
  114. return false;
  115. }
  116. }
  117. if ((activeChar.getActingPlayer().getAllyId() != 0) && (target.getActingPlayer().getAllyId() != 0))
  118. {
  119. if (activeChar.getActingPlayer().getAllyId() != target.getActingPlayer().getAllyId())
  120. {
  121. return false;
  122. }
  123. }
  124. if ((target != activeChar) && (target.getActingPlayer() != null) && (target.getActingPlayer().getPvpFlag() > 0))
  125. {
  126. return false;
  127. }
  128. return true;
  129. }
  130. public class CharComparator implements Comparator<L2Character>
  131. {
  132. @Override
  133. public int compare(L2Character char1, L2Character char2)
  134. {
  135. return Integer.compare((int) (char1.getCurrentHp() / char1.getMaxHp()), (int) (char2.getCurrentHp() / char2.getMaxHp()));
  136. }
  137. }
  138. @Override
  139. public Enum<L2TargetType> getTargetType()
  140. {
  141. return L2TargetType.AREA_FRIENDLY;
  142. }
  143. }