Damage.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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(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. if(customEffectId >= 0)
  65. {
  66. bsa.effect = 82;
  67. bsa.flags |= BattleStackAttacked::EFFECT;
  68. }
  69. stacksInjured.stacks.push_back(bsa);
  70. }
  71. targetIndex++;
  72. }
  73. if(describe && firstTarget && damageToDisplay > 0)
  74. {
  75. describeEffect(blm.lines, m, firstTarget, killed, damageToDisplay, multiple);
  76. }
  77. if(!stacksInjured.stacks.empty())
  78. server->apply(&stacksInjured);
  79. if(!blm.lines.empty())
  80. server->apply(&blm);
  81. }
  82. bool Damage::isReceptive(const Mechanics * m, const battle::Unit * unit) const
  83. {
  84. if(!UnitEffect::isReceptive(m, unit))
  85. return false;
  86. //elemental immunity for damage
  87. auto filter = m->getElementalImmunity();
  88. for(auto element : filter)
  89. {
  90. if(!m->isPositiveSpell() && unit->hasBonusOfType(element, 2))
  91. return false;
  92. }
  93. return true;
  94. }
  95. void Damage::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  96. {
  97. handler.serializeInt("customEffectId", customEffectId, -1);
  98. handler.serializeBool("killByPercentage", killByPercentage);
  99. handler.serializeBool("killByCount", killByCount);
  100. }
  101. int64_t Damage::damageForTarget(size_t targetIndex, const Mechanics * m, const battle::Unit * target) const
  102. {
  103. int64_t baseDamage;
  104. if(killByPercentage)
  105. {
  106. int64_t amountToKill = target->getCount() * m->getEffectValue() / 100;
  107. baseDamage = amountToKill * target->MaxHealth();
  108. }
  109. else if(killByCount)
  110. {
  111. baseDamage = m->getEffectValue() * target->MaxHealth();
  112. }
  113. else
  114. {
  115. baseDamage = m->adjustEffectValue(target);
  116. }
  117. if(chainLength > 1 && targetIndex > 0)
  118. {
  119. double indexedFactor = std::pow(chainFactor, (double) targetIndex);
  120. return (int64_t) (indexedFactor * baseDamage);
  121. }
  122. return baseDamage;
  123. }
  124. void Damage::describeEffect(std::vector<MetaString> & log, const Mechanics * m, const battle::Unit * firstTarget, uint32_t kills, int64_t damage, bool multiple) const
  125. {
  126. if(m->getSpellIndex() == SpellID::DEATH_STARE && !multiple)
  127. {
  128. MetaString line;
  129. if(kills > 1)
  130. {
  131. line.addTxt(MetaString::GENERAL_TXT, 119); //%d %s die under the terrible gaze of the %s.
  132. line.addReplacement(kills);
  133. firstTarget->addNameReplacement(line, true);
  134. }
  135. else
  136. {
  137. line.addTxt(MetaString::GENERAL_TXT, 118); //One %s dies under the terrible gaze of the %s.
  138. firstTarget->addNameReplacement(line, false);
  139. }
  140. m->caster->getCasterName(line);
  141. log.push_back(line);
  142. }
  143. else if(m->getSpellIndex() == SpellID::THUNDERBOLT && !multiple)
  144. {
  145. {
  146. MetaString line;
  147. firstTarget->addText(line, MetaString::GENERAL_TXT, -367, true);
  148. firstTarget->addNameReplacement(line, true);
  149. log.push_back(line);
  150. }
  151. {
  152. MetaString line;
  153. //todo: handle newlines in metastring
  154. std::string text = VLC->generaltexth->allTexts.at(343); //Does %d points of damage.
  155. boost::algorithm::trim(text);
  156. line << text;
  157. line.addReplacement((int)damage); //no more text afterwards
  158. log.push_back(line);
  159. }
  160. }
  161. else
  162. {
  163. {
  164. MetaString line;
  165. line.addTxt(MetaString::GENERAL_TXT, 376);
  166. line.addReplacement(MetaString::SPELL_NAME, m->getSpellIndex());
  167. line.addReplacement((int)damage);
  168. log.push_back(line);
  169. }
  170. {
  171. MetaString line;
  172. const int textId = (kills > 1) ? 379 : 378;
  173. line.addTxt(MetaString::GENERAL_TXT, textId);
  174. if(kills > 1)
  175. line.addReplacement(kills);
  176. if(kills > 1)
  177. {
  178. if(multiple)
  179. line.addReplacement(MetaString::GENERAL_TXT, 43);
  180. else
  181. firstTarget->addNameReplacement(line, true);
  182. }
  183. else
  184. {
  185. if(multiple)
  186. line.addReplacement(MetaString::GENERAL_TXT, 42);
  187. else
  188. firstTarget->addNameReplacement(line, false);
  189. }
  190. log.push_back(line);
  191. }
  192. }
  193. }
  194. }
  195. }