Damage.cpp 5.2 KB

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