AreaFriendly.java 4.4 KB

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