ChainHeal.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package handlers.skillhandlers;
  16. import java.util.List;
  17. import java.util.Map;
  18. import javolution.util.FastList;
  19. import javolution.util.FastMap;
  20. import com.l2jserver.gameserver.handler.ISkillHandler;
  21. import com.l2jserver.gameserver.handler.SkillHandler;
  22. import com.l2jserver.gameserver.model.L2Object;
  23. import com.l2jserver.gameserver.model.actor.L2Character;
  24. import com.l2jserver.gameserver.model.skills.L2Skill;
  25. import com.l2jserver.gameserver.model.skills.L2SkillType;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  28. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  29. import com.l2jserver.util.ValueSortMap;
  30. /**
  31. * @author Nik, UnAfraid
  32. */
  33. public class ChainHeal implements ISkillHandler
  34. {
  35. private static final L2SkillType[] SKILL_IDS =
  36. {
  37. L2SkillType.CHAIN_HEAL
  38. };
  39. @Override
  40. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  41. {
  42. //check for other effects
  43. ISkillHandler handler = SkillHandler.getInstance().getHandler(L2SkillType.BUFF);
  44. if (handler != null)
  45. handler.useSkill(activeChar, skill, targets);
  46. SystemMessage sm;
  47. double amount = 0;
  48. L2Character[] characters = getTargetsToHeal((L2Character[]) targets);
  49. double power = skill.getPower();
  50. // Get top 10 most damaged and iterate the heal over them
  51. for (L2Character character : characters)
  52. {
  53. //1505 - sublime self sacrifice
  54. if ((character.isDead() || character.isInvul()) && skill.getId() != 1505)
  55. continue;
  56. // Cursed weapon owner can't heal or be healed
  57. if (character != activeChar)
  58. {
  59. if (character.isPlayer() && character.getActingPlayer().isCursedWeaponEquipped())
  60. continue;
  61. }
  62. if (power == 100.)
  63. amount = character.getMaxHp();
  64. else
  65. amount = character.getMaxHp() * power / 100.0;
  66. amount = Math.min(amount, character.getMaxRecoverableHp() - character.getCurrentHp());
  67. if (amount < 0)
  68. amount = 0;
  69. character.setCurrentHp(amount + character.getCurrentHp());
  70. if (activeChar != character)
  71. {
  72. sm = SystemMessage.getSystemMessage(SystemMessageId.S2_HP_RESTORED_BY_C1);
  73. sm.addCharName(activeChar);
  74. }
  75. else
  76. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_HP_RESTORED);
  77. sm.addNumber((int) amount);
  78. character.sendPacket(sm);
  79. StatusUpdate su = new StatusUpdate(character);
  80. su.addAttribute(StatusUpdate.CUR_HP, (int) character.getCurrentHp());
  81. character.sendPacket(su);
  82. power -= 3;
  83. }
  84. }
  85. private L2Character[] getTargetsToHeal(L2Character[] targets)
  86. {
  87. Map<L2Character, Double> tmpTargets = new FastMap<>();
  88. List<L2Character> sortedListToReturn = new FastList<>();
  89. int curTargets = 0;
  90. for (L2Character target : targets)
  91. {
  92. //1505 - sublime self sacrifice
  93. if ((target == null || target.isDead() || target.isInvul()))
  94. continue;
  95. if (target.getMaxHp() == target.getCurrentHp()) // Full hp ..
  96. continue;
  97. double hpPercent = target.getCurrentHp() / target.getMaxHp();
  98. tmpTargets.put(target, hpPercent);
  99. curTargets++;
  100. if (curTargets >= 10) // Unhardcode?
  101. break;
  102. }
  103. // Sort in ascending order then add the values to the list
  104. ValueSortMap.sortMapByValue(tmpTargets, true);
  105. sortedListToReturn.addAll(tmpTargets.keySet());
  106. return sortedListToReturn.toArray(new L2Character[sortedListToReturn.size()]);
  107. }
  108. @Override
  109. public L2SkillType[] getSkillIds()
  110. {
  111. return SKILL_IDS;
  112. }
  113. }