Heal.cpp 4.3 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 "../../battle/IBattleState.h"
  15. #include "../../battle/CUnitState.h"
  16. #include "../../battle/CBattleInfoCallback.h"
  17. #include "../../battle/Unit.h"
  18. #include "../../networkPacks/PacksForClientBattle.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. logMessage.battleID = m->battle()->getBattle()->getBattleID();
  33. BattleUnitsChanged pack;
  34. pack.battleID = m->battle()->getBattle()->getBattleID();
  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->getMaxHealth())
  54. return false;
  55. }
  56. if(unit->isDead())
  57. {
  58. //check if alive unit blocks resurrection
  59. for(const BattleHex & hex : unit->getHexes())
  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, EMetaText::GENERAL_TXT, 116, resurrectedCount == 1);
  105. state->addNameReplacement(resurrectText);
  106. resurrectText.replaceNumber(resurrectedCount);
  107. logMessage.lines.push_back(std::move(resurrectText));
  108. }
  109. else if (unitHPgained > 0 && m->caster->getHeroCaster() == nullptr) //Show text about healed HP if healed by unit
  110. {
  111. MetaString healText;
  112. auto casterUnitID = m->caster->getCasterUnitId();
  113. auto casterUnit = m->battle()->battleGetUnitByID(casterUnitID);
  114. healText.appendLocalString(EMetaText::GENERAL_TXT, 414);
  115. casterUnit->addNameReplacement(healText, false);
  116. state->addNameReplacement(healText, false);
  117. healText.replaceNumber((int)unitHPgained);
  118. logMessage.lines.push_back(std::move(healText));
  119. }
  120. if(unitHPgained > 0)
  121. {
  122. UnitChanges info(state->unitId(), UnitChanges::EOperation::RESET_STATE);
  123. info.healthDelta = unitHPgained;
  124. state->save(info.data);
  125. pack.changedStacks.push_back(info);
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. VCMI_LIB_NAMESPACE_END