Heal.cpp 4.1 KB

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