Heal.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. BattleLogMessage logMessage;
  41. BattleUnitsChanged pack;
  42. prepareHealEffect(value, pack, logMessage, *server->getRNG(), m, target);
  43. if(!pack.changedStacks.empty())
  44. server->apply(&pack);
  45. if(!logMessage.lines.empty())
  46. server->apply(&logMessage);
  47. }
  48. bool Heal::isValidTarget(const Mechanics * m, const battle::Unit * unit) const
  49. {
  50. const bool onlyAlive = healLevel == EHealLevel::HEAL;
  51. const bool validInGenaral = unit->isValidTarget(!onlyAlive);
  52. if(!validInGenaral)
  53. return false;
  54. auto injuries = unit->getTotalHealth() - unit->getAvailableHealth();
  55. if(injuries == 0)
  56. return false;
  57. if(minFullUnits > 0)
  58. {
  59. auto hpGained = std::min(m->getEffectValue(), injuries);
  60. if(hpGained < minFullUnits * unit->MaxHealth())
  61. return false;
  62. }
  63. if(unit->isDead())
  64. {
  65. //check if alive unit blocks resurrection
  66. for(const BattleHex & hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide()))
  67. {
  68. auto blocking = m->battle()->battleGetUnitsIf([hex, unit](const battle::Unit * other)
  69. {
  70. return other->isValidTarget(false) && other->coversPos(hex) && other != unit;
  71. });
  72. if(!blocking.empty())
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. void Heal::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  79. {
  80. static const std::vector<std::string> HEAL_LEVEL_MAP =
  81. {
  82. "heal",
  83. "resurrect",
  84. "overHeal"
  85. };
  86. static const std::vector<std::string> HEAL_POWER_MAP =
  87. {
  88. "oneBattle",
  89. "permanent"
  90. };
  91. handler.serializeEnum("healLevel", healLevel, EHealLevel::HEAL, HEAL_LEVEL_MAP);
  92. handler.serializeEnum("healPower", healPower, EHealPower::PERMANENT, HEAL_POWER_MAP);
  93. handler.serializeInt("minFullUnits", minFullUnits);
  94. }
  95. void Heal::prepareHealEffect(int64_t value, BattleUnitsChanged & pack, BattleLogMessage & logMessage, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  96. {
  97. for(auto & oneTarget : target)
  98. {
  99. const battle::Unit * unit = oneTarget.unitValue;
  100. if(unit)
  101. {
  102. auto unitHPgained = m->applySpellBonus(value, unit);
  103. auto state = unit->acquire();
  104. const auto countBeforeHeal = state->getCount();
  105. state->heal(unitHPgained, healLevel, healPower);
  106. if(const auto resurrectedCount = std::max(0, state->getCount() - countBeforeHeal))
  107. {
  108. // %d %s rise from the dead!
  109. // in the table first comes plural string, then the singular one
  110. MetaString resurrectText;
  111. state->addText(resurrectText, MetaString::GENERAL_TXT, 116, resurrectedCount == 1);
  112. state->addNameReplacement(resurrectText);
  113. resurrectText.addReplacement(resurrectedCount);
  114. logMessage.lines.push_back(std::move(resurrectText));
  115. }
  116. if(unitHPgained > 0)
  117. {
  118. UnitChanges info(state->unitId(), UnitChanges::EOperation::RESET_STATE);
  119. info.healthDelta = unitHPgained;
  120. state->save(info.data);
  121. pack.changedStacks.push_back(info);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. VCMI_LIB_NAMESPACE_END