AreaFriendly.java 4.4 KB

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