Damage.cpp 5.1 KB

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