2
0

Damage.cpp 5.1 KB

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