ChainHeal.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.L2Skill;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  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.gameserver.templates.skills.L2SkillType;
  30. import com.l2jserver.util.ValueSortMap;
  31. /**
  32. *
  33. * @author Nik
  34. * @author UnAfraid
  35. *
  36. */
  37. public class ChainHeal implements ISkillHandler
  38. {
  39. private static final L2SkillType[] SKILL_IDS =
  40. {
  41. L2SkillType.CHAIN_HEAL
  42. };
  43. /**
  44. *
  45. * @see com.l2jserver.gameserver.handler.ISkillHandler#useSkill(com.l2jserver.gameserver.model.actor.L2Character, com.l2jserver.gameserver.model.L2Skill, com.l2jserver.gameserver.model.L2Object[])
  46. */
  47. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  48. {
  49. //check for other effects
  50. ISkillHandler handler = SkillHandler.getInstance().getSkillHandler(L2SkillType.BUFF);
  51. if (handler != null)
  52. handler.useSkill(activeChar, skill, targets);
  53. SystemMessage sm;
  54. double amount = 0;
  55. L2Character[] characters = getTargetsToHeal((L2Character[]) targets);
  56. double power = skill.getPower();
  57. // Get top 10 most damaged and iterate the heal over them
  58. for (L2Character character : characters)
  59. {
  60. //1505 - sublime self sacrifice
  61. if ((character == null || character.isDead() || character.isInvul()) && skill.getId() != 1505)
  62. continue;
  63. // Cursed weapon owner can't heal or be healed
  64. if (character != activeChar)
  65. {
  66. if (character instanceof L2PcInstance && ((L2PcInstance) character).isCursedWeaponEquipped())
  67. continue;
  68. }
  69. if (power == 100.)
  70. amount = character.getMaxHp();
  71. else
  72. amount = character.getMaxHp() * power / 100.0;
  73. amount = Math.min(amount, character.getMaxRecoverableHp() - character.getCurrentHp());
  74. if (amount < 0)
  75. amount = 0;
  76. character.setCurrentHp(amount + character.getCurrentHp());
  77. if (activeChar != character)
  78. {
  79. sm = SystemMessage.getSystemMessage(SystemMessageId.S2_HP_RESTORED_BY_C1);
  80. sm.addCharName(activeChar);
  81. }
  82. else
  83. sm = SystemMessage.getSystemMessage(SystemMessageId.S1_HP_RESTORED);
  84. sm.addNumber((int) amount);
  85. character.sendPacket(sm);
  86. StatusUpdate su = new StatusUpdate(character);
  87. su.addAttribute(StatusUpdate.CUR_HP, (int) character.getCurrentHp());
  88. character.sendPacket(su);
  89. power -= 3;
  90. }
  91. }
  92. private L2Character[] getTargetsToHeal(L2Character[] targets)
  93. {
  94. Map<L2Character, Double> tmpTargets = new FastMap<L2Character, Double>();
  95. List<L2Character> sortedListToReturn = new FastList<L2Character>();
  96. int curTargets = 0;
  97. for (L2Character target : targets)
  98. {
  99. //1505 - sublime self sacrifice
  100. if ((target == null || target.isDead() || target.isInvul()))
  101. continue;
  102. if (target.getMaxHp() == target.getCurrentHp()) // Full hp ..
  103. continue;
  104. double hpPercent = target.getCurrentHp() / target.getMaxHp();
  105. tmpTargets.put(target, hpPercent);
  106. curTargets++;
  107. if (curTargets >= 10) // Unhardcode?
  108. break;
  109. }
  110. // Sort in ascending order then add the values to the list
  111. ValueSortMap.sortMapByValue(tmpTargets, true);
  112. sortedListToReturn.addAll(tmpTargets.keySet());
  113. return sortedListToReturn.toArray(new L2Character[sortedListToReturn.size()]);
  114. }
  115. /**
  116. *
  117. * @see com.l2jserver.gameserver.handler.ISkillHandler#getSkillIds()
  118. */
  119. public L2SkillType[] getSkillIds()
  120. {
  121. return SKILL_IDS;
  122. }
  123. }