Heal.cpp 3.7 KB

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