Damage.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../CStack.h"
  16. #include "../../battle/IBattleState.h"
  17. #include "../../battle/CBattleInfoCallback.h"
  18. #include "../../CGeneralTextHandler.h"
  19. #include "../../serializer/JsonSerializeFormat.h"
  20. static const std::string EFFECT_NAME = "core:damage";
  21. namespace spells
  22. {
  23. namespace effects
  24. {
  25. VCMI_REGISTER_SPELL_EFFECT(Damage, EFFECT_NAME);
  26. Damage::Damage()
  27. : UnitEffect(),
  28. customEffectId(-1),
  29. killByPercentage(false),
  30. killByCount(false)
  31. {
  32. }
  33. Damage::~Damage() = default;
  34. void Damage::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  35. {
  36. StacksInjured stacksInjured;
  37. prepareEffects(stacksInjured, rng, m, target, battleState->describe);
  38. if(!stacksInjured.stacks.empty())
  39. battleState->apply(&stacksInjured);
  40. }
  41. bool Damage::isReceptive(const Mechanics * m, const battle::Unit * unit) const
  42. {
  43. if(!UnitEffect::isReceptive(m, unit))
  44. return false;
  45. //elemental immunity for damage
  46. auto filter = m->getElementalImmunity();
  47. for(auto element : filter)
  48. {
  49. if(!m->isPositiveSpell() && unit->hasBonusOfType(element, 2))
  50. return false;
  51. }
  52. return true;
  53. }
  54. void Damage::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  55. {
  56. handler.serializeInt("customEffectId", customEffectId, -1);
  57. handler.serializeBool("killByPercentage", killByPercentage);
  58. handler.serializeBool("killByCount", killByCount);
  59. }
  60. int64_t Damage::damageForTarget(size_t targetIndex, const Mechanics * m, const battle::Unit * target) const
  61. {
  62. int64_t baseDamage;
  63. if(killByPercentage)
  64. {
  65. int64_t amountToKill = target->getCount() * m->getEffectValue() / 100;
  66. baseDamage = amountToKill * target->MaxHealth();
  67. }
  68. else if(killByCount)
  69. {
  70. baseDamage = m->getEffectValue() * target->MaxHealth();
  71. }
  72. else
  73. {
  74. baseDamage = m->adjustEffectValue(target);
  75. }
  76. if(chainLength > 1 && targetIndex > 0)
  77. {
  78. double indexedFactor = std::pow(chainFactor, (double) targetIndex);
  79. return (int64_t) (indexedFactor * baseDamage);
  80. }
  81. return baseDamage;
  82. }
  83. void Damage::describeEffect(std::vector<MetaString> & log, const Mechanics * m, const battle::Unit * firstTarget, uint32_t kills, int64_t damage, bool multiple) const
  84. {
  85. if(m->getSpellIndex() == SpellID::DEATH_STARE && !multiple)
  86. {
  87. MetaString line;
  88. if(kills > 1)
  89. {
  90. line.addTxt(MetaString::GENERAL_TXT, 119); //%d %s die under the terrible gaze of the %s.
  91. line.addReplacement(kills);
  92. firstTarget->addNameReplacement(line, true);
  93. }
  94. else
  95. {
  96. line.addTxt(MetaString::GENERAL_TXT, 118); //One %s dies under the terrible gaze of the %s.
  97. firstTarget->addNameReplacement(line, false);
  98. }
  99. m->caster->getCasterName(line);
  100. log.push_back(line);
  101. }
  102. else if(m->getSpellIndex() == SpellID::THUNDERBOLT && !multiple)
  103. {
  104. {
  105. MetaString line;
  106. firstTarget->addText(line, MetaString::GENERAL_TXT, -367, true);
  107. firstTarget->addNameReplacement(line, true);
  108. log.push_back(line);
  109. }
  110. {
  111. MetaString line;
  112. //todo: handle newlines in metastring
  113. std::string text = VLC->generaltexth->allTexts.at(343); //Does %d points of damage.
  114. boost::algorithm::trim(text);
  115. line << text;
  116. line.addReplacement((int)damage); //no more text afterwards
  117. log.push_back(line);
  118. }
  119. }
  120. else
  121. {
  122. {
  123. MetaString line;
  124. line.addTxt(MetaString::GENERAL_TXT, 376);
  125. line.addReplacement(MetaString::SPELL_NAME, m->getSpellIndex());
  126. line.addReplacement((int)damage);
  127. log.push_back(line);
  128. }
  129. {
  130. MetaString line;
  131. const int textId = (kills > 1) ? 379 : 378;
  132. line.addTxt(MetaString::GENERAL_TXT, textId);
  133. if(kills > 1)
  134. line.addReplacement(kills);
  135. if(kills > 1)
  136. {
  137. if(multiple)
  138. line.addReplacement(MetaString::GENERAL_TXT, 43);
  139. else
  140. firstTarget->addNameReplacement(line, true);
  141. }
  142. else
  143. {
  144. if(multiple)
  145. line.addReplacement(MetaString::GENERAL_TXT, 42);
  146. else
  147. firstTarget->addNameReplacement(line, false);
  148. }
  149. log.push_back(line);
  150. }
  151. }
  152. }
  153. void Damage::prepareEffects(StacksInjured & stacksInjured, RNG & rng, const Mechanics * m, const EffectTarget & target, bool describe) const
  154. {
  155. size_t targetIndex = 0;
  156. const battle::Unit * firstTarget = nullptr;
  157. int64_t damageToDisplay = 0;
  158. uint32_t killed = 0;
  159. bool multiple = false;
  160. for(auto & t : target)
  161. {
  162. const battle::Unit * unit = t.unitValue;
  163. if(unit && unit->alive())
  164. {
  165. BattleStackAttacked bsa;
  166. bsa.damageAmount = damageForTarget(targetIndex, m, unit);
  167. bsa.stackAttacked = unit->unitId();
  168. bsa.attackerID = -1;
  169. auto newState = unit->acquireState();
  170. CStack::prepareAttacked(bsa, rng, newState);
  171. if(describe)
  172. {
  173. if(!firstTarget)
  174. firstTarget = unit;
  175. else
  176. multiple = true;
  177. damageToDisplay += bsa.damageAmount;
  178. killed += bsa.killedAmount;
  179. }
  180. if(customEffectId >= 0)
  181. {
  182. bsa.effect = 82;
  183. bsa.flags |= BattleStackAttacked::EFFECT;
  184. }
  185. stacksInjured.stacks.push_back(bsa);
  186. }
  187. targetIndex++;
  188. }
  189. if(describe && firstTarget && damageToDisplay > 0)
  190. describeEffect(stacksInjured.battleLog, m, firstTarget, killed, damageToDisplay, multiple);
  191. }
  192. }
  193. }