Damage.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Damage.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 "Damage.h"
  12. #include "Registry.h"
  13. #include "../CSpellHandler.h"
  14. #include "../ISpellMechanics.h"
  15. #include "../../NetPacks.h"
  16. #include "../../CStack.h"
  17. #include "../../battle/IBattleState.h"
  18. #include "../../battle/CBattleInfoCallback.h"
  19. #include "../../CGeneralTextHandler.h"
  20. #include "../../serializer/JsonSerializeFormat.h"
  21. #include <vcmi/spells/Spell.h>
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. namespace spells
  24. {
  25. namespace effects
  26. {
  27. void Damage::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  28. {
  29. StacksInjured stacksInjured;
  30. BattleLogMessage blm;
  31. size_t targetIndex = 0;
  32. const battle::Unit * firstTarget = nullptr;
  33. const bool describe = server->describeChanges();
  34. int64_t damageToDisplay = 0;
  35. uint32_t killed = 0;
  36. bool multiple = false;
  37. for(const auto & t : target)
  38. {
  39. const battle::Unit * unit = t.unitValue;
  40. if(unit && unit->alive())
  41. {
  42. BattleStackAttacked bsa;
  43. bsa.damageAmount = damageForTarget(targetIndex, m, unit);
  44. bsa.stackAttacked = unit->unitId();
  45. bsa.attackerID = -1;
  46. auto newState = unit->acquireState();
  47. CStack::prepareAttacked(bsa, *server->getRNG(), newState);
  48. if(describe)
  49. {
  50. if(!firstTarget)
  51. firstTarget = unit;
  52. else
  53. multiple = true;
  54. damageToDisplay += bsa.damageAmount;
  55. killed += bsa.killedAmount;
  56. }
  57. stacksInjured.stacks.push_back(bsa);
  58. }
  59. targetIndex++;
  60. }
  61. if(describe && firstTarget && damageToDisplay > 0)
  62. {
  63. describeEffect(blm.lines, m, firstTarget, killed, damageToDisplay, multiple);
  64. }
  65. if(!stacksInjured.stacks.empty())
  66. server->apply(&stacksInjured);
  67. if(!blm.lines.empty())
  68. server->apply(&blm);
  69. }
  70. bool Damage::isReceptive(const Mechanics * m, const battle::Unit * unit) const
  71. {
  72. if(!UnitEffect::isReceptive(m, unit))
  73. return false;
  74. bool isImmune = m->getSpell()->isMagical() && (unit->getBonusBearer()->valOfBonuses(BonusType::SPELL_DAMAGE_REDUCTION, SpellSchool(ESpellSchool::ANY)) >= 100); //General spell damage immunity
  75. //elemental immunity for damage
  76. m->getSpell()->forEachSchool([&](const SchoolInfo & cnf, bool & stop)
  77. {
  78. isImmune |= (unit->getBonusBearer()->valOfBonuses(BonusType::SPELL_DAMAGE_REDUCTION, cnf.id) >= 100); //100% reduction is immunity
  79. });
  80. return !isImmune;
  81. }
  82. void Damage::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  83. {
  84. handler.serializeBool("killByPercentage", killByPercentage);
  85. handler.serializeBool("killByCount", killByCount);
  86. }
  87. int64_t Damage::damageForTarget(size_t targetIndex, const Mechanics * m, const battle::Unit * target) const
  88. {
  89. int64_t baseDamage;
  90. if(killByPercentage)
  91. {
  92. int64_t amountToKill = target->getCount() * m->getEffectValue() / 100;
  93. baseDamage = amountToKill * target->getMaxHealth();
  94. }
  95. else if(killByCount)
  96. {
  97. baseDamage = m->getEffectValue() * target->getMaxHealth();
  98. }
  99. else
  100. {
  101. baseDamage = m->adjustEffectValue(target);
  102. }
  103. if(chainLength > 1 && targetIndex > 0)
  104. {
  105. double indexedFactor = std::pow(chainFactor, static_cast<double>(targetIndex));
  106. return static_cast<int64_t>(indexedFactor * baseDamage);
  107. }
  108. return baseDamage;
  109. }
  110. void Damage::describeEffect(std::vector<MetaString> & log, const Mechanics * m, const battle::Unit * firstTarget, uint32_t kills, int64_t damage, bool multiple) const
  111. {
  112. if(m->getSpellIndex() == SpellID::DEATH_STARE && !multiple)
  113. {
  114. MetaString line;
  115. if(kills > 1)
  116. {
  117. line.addTxt(MetaString::GENERAL_TXT, 119); //%d %s die under the terrible gaze of the %s.
  118. line.addReplacement(kills);
  119. firstTarget->addNameReplacement(line, true);
  120. }
  121. else
  122. {
  123. line.addTxt(MetaString::GENERAL_TXT, 118); //One %s dies under the terrible gaze of the %s.
  124. firstTarget->addNameReplacement(line, false);
  125. }
  126. m->caster->getCasterName(line);
  127. log.push_back(line);
  128. }
  129. else if(m->getSpellIndex() == SpellID::THUNDERBOLT && !multiple)
  130. {
  131. {
  132. MetaString line;
  133. firstTarget->addText(line, MetaString::GENERAL_TXT, -367, true);
  134. firstTarget->addNameReplacement(line, true);
  135. log.push_back(line);
  136. }
  137. {
  138. MetaString line;
  139. //todo: handle newlines in metastring
  140. std::string text = VLC->generaltexth->allTexts[343]; //Does %d points of damage.
  141. boost::algorithm::trim(text);
  142. line << text;
  143. line.addReplacement(static_cast<int>(damage)); //no more text afterwards
  144. log.push_back(line);
  145. }
  146. }
  147. else
  148. {
  149. {
  150. MetaString line;
  151. line.addTxt(MetaString::GENERAL_TXT, 376); // Spell %s does %d damage
  152. line.addReplacement(MetaString::SPELL_NAME, m->getSpellIndex());
  153. line.addReplacement(static_cast<int>(damage));
  154. log.push_back(line);
  155. }
  156. if (kills > 0)
  157. {
  158. MetaString line;
  159. if(kills > 1)
  160. {
  161. line.addTxt(MetaString::GENERAL_TXT, 379); // %d %s perishes
  162. line.addReplacement(kills);
  163. if(multiple)
  164. line.addReplacement(MetaString::GENERAL_TXT, 43); // creatures
  165. else
  166. firstTarget->addNameReplacement(line, true);
  167. }
  168. else // single creature killed
  169. {
  170. line.addTxt(MetaString::GENERAL_TXT, 378); // one %s perishes
  171. if(multiple)
  172. line.addReplacement(MetaString::GENERAL_TXT, 42); // creature
  173. else
  174. firstTarget->addNameReplacement(line, false);
  175. }
  176. log.push_back(line);
  177. }
  178. }
  179. }
  180. }
  181. }
  182. VCMI_LIB_NAMESPACE_END