Damage.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "../CSpellHandler.h"
  14. #include "../ISpellMechanics.h"
  15. #include "../../CStack.h"
  16. #include "../../battle/IBattleState.h"
  17. #include "../../battle/CBattleInfoCallback.h"
  18. #include "../../networkPacks/PacksForClientBattle.h"
  19. #include "../../texts/CGeneralTextHandler.h"
  20. #include "../../texts/Languages.h"
  21. #include "../../serializer/JsonSerializeFormat.h"
  22. #include "../lib/CRandomGenerator.h"
  23. #include <vcmi/spells/Spell.h>
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. namespace spells
  26. {
  27. namespace effects
  28. {
  29. void Damage::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  30. {
  31. StacksInjured stacksInjured;
  32. BattleLogMessage blm;
  33. stacksInjured.battleID = m->battle()->getBattle()->getBattleID();
  34. blm.battleID = m->battle()->getBattle()->getBattleID();
  35. size_t targetIndex = 0;
  36. const battle::Unit * firstTarget = nullptr;
  37. const bool describe = server->describeChanges();
  38. int64_t damageToDisplay = 0;
  39. uint32_t killed = 0;
  40. bool multiple = false;
  41. for(const auto & t : target)
  42. {
  43. const battle::Unit * unit = t.unitValue;
  44. if(unit && unit->alive())
  45. {
  46. BattleStackAttacked bsa;
  47. bsa.damageAmount = damageForTarget(targetIndex, m, unit);
  48. bsa.stackAttacked = unit->unitId();
  49. bsa.attackerID = -1;
  50. auto newState = unit->acquireState();
  51. CStack::prepareAttacked(bsa, *server->getRNG(), newState);
  52. if(describe)
  53. {
  54. if(!firstTarget)
  55. firstTarget = unit;
  56. else
  57. multiple = true;
  58. damageToDisplay += bsa.damageAmount;
  59. killed += bsa.killedAmount;
  60. }
  61. stacksInjured.stacks.push_back(bsa);
  62. }
  63. targetIndex++;
  64. }
  65. if(describe && firstTarget && damageToDisplay > 0)
  66. {
  67. describeEffect(blm.lines, m, firstTarget, killed, damageToDisplay, multiple);
  68. }
  69. if(!stacksInjured.stacks.empty())
  70. server->apply(stacksInjured);
  71. if(!blm.lines.empty())
  72. server->apply(blm);
  73. }
  74. SpellEffectValue Damage::getHealthChange(const Mechanics * m, const EffectTarget & spellTarget) const
  75. {
  76. SpellEffectValue result {};
  77. size_t targetIndex = 0;
  78. CRandomGenerator fakeRng;
  79. for(auto const & t : spellTarget)
  80. {
  81. const battle::Unit * unit = t.unitValue;
  82. if(unit && unit->alive())
  83. {
  84. BattleStackAttacked bsa;
  85. bsa.damageAmount = damageForTarget(targetIndex, m, unit);
  86. bsa.stackAttacked = unit->unitId();
  87. bsa.attackerID = -1;
  88. auto state = unit->acquireState();
  89. int64_t before = state->getAvailableHealth();
  90. CStack::prepareAttacked(bsa, fakeRng, state);
  91. int64_t after = state->getAvailableHealth();
  92. int64_t applied = before - after;
  93. result.hpDelta -= applied;
  94. result.unitsDelta -= static_cast<int32_t>(bsa.killedAmount);
  95. }
  96. targetIndex++;
  97. }
  98. return result;
  99. }
  100. bool Damage::isReceptive(const Mechanics * m, const battle::Unit * unit) const
  101. {
  102. if(!UnitEffect::isReceptive(m, unit))
  103. return false;
  104. bool isImmune = m->getSpell()->isMagical() && (unit->getBonusBearer()->valOfBonuses(BonusType::SPELL_DAMAGE_REDUCTION, BonusSubtypeID(SpellSchool::ANY)) >= 100); //General spell damage immunity
  105. //elemental immunity for damage
  106. m->getSpell()->forEachSchool([&](const SpellSchool & cnf, bool & stop)
  107. {
  108. isImmune |= (unit->getBonusBearer()->valOfBonuses(BonusType::SPELL_DAMAGE_REDUCTION, BonusSubtypeID(cnf)) >= 100); //100% reduction is immunity
  109. });
  110. return !isImmune;
  111. }
  112. void Damage::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  113. {
  114. handler.serializeBool("killByPercentage", killByPercentage);
  115. handler.serializeBool("killByCount", killByCount);
  116. }
  117. int64_t Damage::damageForTarget(size_t targetIndex, const Mechanics * m, const battle::Unit * target) const
  118. {
  119. int64_t baseDamage;
  120. if(killByPercentage)
  121. {
  122. int64_t amountToKill = target->getCount() * m->getEffectValue() / 100;
  123. baseDamage = amountToKill * target->getMaxHealth();
  124. }
  125. else if(killByCount)
  126. {
  127. baseDamage = m->getEffectValue() * target->getMaxHealth();
  128. }
  129. else
  130. {
  131. baseDamage = m->adjustEffectValue(target);
  132. }
  133. if(chainLength > 1 && targetIndex > 0)
  134. {
  135. double indexedFactor = std::pow(chainFactor, static_cast<double>(targetIndex));
  136. return static_cast<int64_t>(indexedFactor * baseDamage);
  137. }
  138. return baseDamage;
  139. }
  140. void Damage::describeEffect(std::vector<MetaString> & log, const Mechanics * m, const battle::Unit * firstTarget, uint32_t kills, int64_t damage, bool multiple) const
  141. {
  142. if(m->getSpellIndex() == SpellID::DEATH_STARE && !multiple)
  143. {
  144. MetaString line;
  145. if(kills > 1)
  146. {
  147. line.appendLocalString(EMetaText::GENERAL_TXT, 119); //%d %s die under the terrible gaze of the %s.
  148. line.replaceNumber(kills);
  149. firstTarget->addNameReplacement(line, true);
  150. }
  151. else
  152. {
  153. line.appendLocalString(EMetaText::GENERAL_TXT, 118); //One %s dies under the terrible gaze of the %s.
  154. firstTarget->addNameReplacement(line, false);
  155. }
  156. m->caster->getCasterName(line);
  157. log.push_back(line);
  158. }
  159. else if(m->getSpell()->getJsonKey().find("accurateShot") != std::string::npos && !multiple)
  160. {
  161. MetaString line;
  162. std::string preferredLanguage = LIBRARY->generaltexth->getPreferredLanguage();
  163. std::string textID = "vcmi.battleWindow.accurateShot.resultDescription";
  164. line.appendTextID(Languages::getPluralFormTextID( preferredLanguage, kills, textID));
  165. line.replaceNumber(kills);
  166. firstTarget->addNameReplacement(line, kills != 1);
  167. log.push_back(line);
  168. }
  169. else if(m->getSpellIndex() == SpellID::THUNDERBOLT && !multiple)
  170. {
  171. {
  172. MetaString line;
  173. firstTarget->addText(line, EMetaText::GENERAL_TXT, -367, true);
  174. firstTarget->addNameReplacement(line, true);
  175. log.push_back(line);
  176. }
  177. {
  178. MetaString line;
  179. //todo: handle newlines in metastring
  180. std::string text = LIBRARY->generaltexth->allTexts[343]; //Does %d points of damage.
  181. boost::algorithm::trim(text);
  182. line.appendRawString(text);
  183. line.replaceNumber(static_cast<int>(damage)); //no more text afterwards
  184. log.push_back(line);
  185. }
  186. }
  187. else
  188. {
  189. {
  190. MetaString line;
  191. line.appendLocalString(EMetaText::GENERAL_TXT, 376); // Spell %s does %d damage
  192. line.replaceName(m->getSpellId());
  193. line.replaceNumber(static_cast<int>(damage));
  194. log.push_back(line);
  195. }
  196. if (kills > 0)
  197. {
  198. MetaString line;
  199. if(kills > 1)
  200. {
  201. line.appendLocalString(EMetaText::GENERAL_TXT, 379); // %d %s perishes
  202. line.replaceNumber(kills);
  203. if(multiple)
  204. line.replaceLocalString(EMetaText::GENERAL_TXT, 43); // creatures
  205. else
  206. firstTarget->addNameReplacement(line, true);
  207. }
  208. else // single creature killed
  209. {
  210. line.appendLocalString(EMetaText::GENERAL_TXT, 378); // one %s perishes
  211. if(multiple)
  212. line.replaceLocalString(EMetaText::GENERAL_TXT, 42); // creature
  213. else
  214. firstTarget->addNameReplacement(line, false);
  215. }
  216. log.push_back(line);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. VCMI_LIB_NAMESPACE_END