Heal.cpp 4.4 KB

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