Heal.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. VCMI_LIB_NAMESPACE_BEGIN
  20. static const std::string EFFECT_NAME = "core:heal";
  21. namespace spells
  22. {
  23. namespace effects
  24. {
  25. VCMI_REGISTER_SPELL_EFFECT(Heal, EFFECT_NAME);
  26. Heal::Heal()
  27. : UnitEffect(),
  28. healLevel(EHealLevel::HEAL),
  29. healPower(EHealPower::PERMANENT),
  30. minFullUnits(0)
  31. {
  32. }
  33. Heal::~Heal() = default;
  34. void Heal::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  35. {
  36. apply(m->getEffectValue(), server, m, target);
  37. }
  38. void Heal::apply(int64_t value, ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  39. {
  40. BattleUnitsChanged pack;
  41. prepareHealEffect(value, pack, *server->getRNG(), m, target);
  42. if(!pack.changedStacks.empty())
  43. server->apply(&pack);
  44. }
  45. bool Heal::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
  46. {
  47. const bool onlyAlive = healLevel == EHealLevel::HEAL;
  48. const bool validInGenaral = unit->isValidTarget(!onlyAlive);
  49. if(!validInGenaral)
  50. return false;
  51. auto insuries = unit->getTotalHealth() - unit->getAvailableHealth();
  52. if(insuries == 0)
  53. return false;
  54. if(minFullUnits > 0)
  55. {
  56. auto hpGained = std::min(m->getEffectValue(), insuries);
  57. if(hpGained < minFullUnits * unit->MaxHealth())
  58. return false;
  59. }
  60. if(unit->isDead())
  61. {
  62. //check if alive unit blocks resurrection
  63. for(const BattleHex & hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  64. {
  65. auto blocking = m->battle()->battleGetUnitsIf([hex, unit](const battle::Unit * other)
  66. {
  67. return other->isValidTarget(false) && other->coversPos(hex) && other != unit;
  68. });
  69. if(!blocking.empty())
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. void Heal::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  76. {
  77. static const std::vector<std::string> HEAL_LEVEL_MAP =
  78. {
  79. "heal",
  80. "resurrect",
  81. "overHeal"
  82. };
  83. static const std::vector<std::string> HEAL_POWER_MAP =
  84. {
  85. "oneBattle",
  86. "permanent"
  87. };
  88. handler.serializeEnum("healLevel", healLevel, EHealLevel::HEAL, HEAL_LEVEL_MAP);
  89. handler.serializeEnum("healPower", healPower, EHealPower::PERMANENT, HEAL_POWER_MAP);
  90. handler.serializeInt("minFullUnits", minFullUnits);
  91. }
  92. void Heal::prepareHealEffect(int64_t value, BattleUnitsChanged & pack, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  93. {
  94. for(auto & oneTarget : target)
  95. {
  96. const battle::Unit * unit = oneTarget.unitValue;
  97. if(unit)
  98. {
  99. auto unitHPgained = m->applySpellBonus(value, unit);
  100. auto state = unit->acquire();
  101. state->heal(unitHPgained, healLevel, healPower);
  102. if(unitHPgained > 0)
  103. {
  104. UnitChanges info(state->unitId(), UnitChanges::EOperation::RESET_STATE);
  105. info.healthDelta = unitHPgained;
  106. state->save(info.data);
  107. pack.changedStacks.push_back(info);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. VCMI_LIB_NAMESPACE_END