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