Heal.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Heal.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Heal.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../battle/IBattleState.h"
  16. #include "../../battle/CBattleInfoCallback.h"
  17. #include "../../battle/Unit.h"
  18. #include "../../serializer/JsonSerializeFormat.h"
  19. static const std::string EFFECT_NAME = "core:heal";
  20. namespace spells
  21. {
  22. namespace effects
  23. {
  24. VCMI_REGISTER_SPELL_EFFECT(Heal, EFFECT_NAME);
  25. Heal::Heal()
  26. : UnitEffect(),
  27. healLevel(EHealLevel::HEAL),
  28. healPower(EHealPower::PERMANENT),
  29. minFullUnits(0)
  30. {
  31. }
  32. Heal::~Heal() = default;
  33. void Heal::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  34. {
  35. apply(m->getEffectValue(), battleState, rng, m, target);
  36. }
  37. void Heal::apply(int64_t value, BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  38. {
  39. BattleUnitsChanged pack;
  40. prepareHealEffect(value, pack, rng, m, target);
  41. if(!pack.changedStacks.empty())
  42. battleState->apply(&pack);
  43. }
  44. bool Heal::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
  45. {
  46. const bool onlyAlive = healLevel == EHealLevel::HEAL;
  47. const bool validInGenaral = unit->isValidTarget(!onlyAlive);
  48. if(!validInGenaral)
  49. return false;
  50. auto insuries = unit->getTotalHealth() - unit->getAvailableHealth();
  51. if(insuries == 0)
  52. return false;
  53. if(minFullUnits > 0)
  54. {
  55. auto hpGained = std::min(m->getEffectValue(), insuries);
  56. if(hpGained < minFullUnits * unit->MaxHealth())
  57. return false;
  58. }
  59. if(unit->isDead())
  60. {
  61. //check if alive unit blocks resurrection
  62. for(const BattleHex & hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  63. {
  64. auto blocking = m->cb->battleGetUnitsIf([hex, unit](const battle::Unit * other)
  65. {
  66. return other->isValidTarget(false) && other->coversPos(hex) && other != unit;
  67. });
  68. if(!blocking.empty())
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. void Heal::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  75. {
  76. static const std::vector<std::string> HEAL_LEVEL_MAP =
  77. {
  78. "heal",
  79. "resurrect",
  80. "overHeal"
  81. };
  82. static const std::vector<std::string> HEAL_POWER_MAP =
  83. {
  84. "oneBattle",
  85. "permanent"
  86. };
  87. handler.serializeEnum("healLevel", healLevel, EHealLevel::HEAL, HEAL_LEVEL_MAP);
  88. handler.serializeEnum("healPower", healPower, EHealPower::PERMANENT, HEAL_POWER_MAP);
  89. handler.serializeInt("minFullUnits", minFullUnits);
  90. }
  91. void Heal::prepareHealEffect(int64_t value, BattleUnitsChanged & pack, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  92. {
  93. for(auto & oneTarget : target)
  94. {
  95. const battle::Unit * unit = oneTarget.unitValue;
  96. if(unit)
  97. {
  98. auto unitHPgained = m->applySpellBonus(value, unit);
  99. auto state = unit->acquire();
  100. state->heal(unitHPgained, healLevel, healPower);
  101. if(unitHPgained > 0)
  102. {
  103. UnitChanges info(state->unitId(), UnitChanges::EOperation::RESET_STATE);
  104. info.healthDelta = unitHPgained;
  105. state->save(info.data);
  106. pack.changedStacks.push_back(info);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }