Timed.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Timed.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 "Timed.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../battle/IBattleState.h"
  16. #include "../../battle/Unit.h"
  17. #include "../../serializer/JsonSerializeFormat.h"
  18. static const std::string EFFECT_NAME = "core:timed";
  19. namespace spells
  20. {
  21. namespace effects
  22. {
  23. VCMI_REGISTER_SPELL_EFFECT(Timed, EFFECT_NAME);
  24. Timed::Timed()
  25. : UnitEffect(),
  26. cumulative(false),
  27. bonus()
  28. {
  29. }
  30. Timed::~Timed() = default;
  31. void Timed::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  32. {
  33. SetStackEffect sse;
  34. prepareEffects(sse, m, target, battleState->describe);
  35. if(!(sse.toAdd.empty() && sse.toUpdate.empty()))
  36. battleState->apply(&sse);
  37. }
  38. void Timed::convertBonus(const Mechanics * m, int32_t & duration, std::vector<Bonus> & converted) const
  39. {
  40. int32_t maxDuration = 0;
  41. for(const std::shared_ptr<Bonus> & b : bonus)
  42. {
  43. Bonus nb(*b);
  44. //use configured duration if present
  45. if(nb.turnsRemain == 0)
  46. nb.turnsRemain = duration;
  47. vstd::amax(maxDuration, nb.turnsRemain);
  48. nb.sid = m->getSpellIndex(); //for all
  49. nb.source = Bonus::SPELL_EFFECT;//for all
  50. //fix to original config: shield should display damage reduction
  51. if((nb.sid == SpellID::SHIELD || nb.sid == SpellID::AIR_SHIELD) && (nb.type == Bonus::GENERAL_DAMAGE_REDUCTION))
  52. nb.val = 100 - nb.val;
  53. //we need to know who cast Bind
  54. else if(nb.sid == SpellID::BIND && nb.type == Bonus::BIND_EFFECT && m->caster->getCasterUnitId() >= 0)
  55. nb.additionalInfo = m->caster->getCasterUnitId();
  56. converted.push_back(nb);
  57. }
  58. //if all spell effects have special duration, use it later for special bonuses
  59. duration = maxDuration;
  60. }
  61. void Timed::describeEffect(std::vector<MetaString> & log, const Mechanics * m, const std::vector<Bonus> & bonuses, const battle::Unit * target) const
  62. {
  63. auto addLogLine = [&](const int32_t baseTextID, const boost::logic::tribool & plural)
  64. {
  65. MetaString line;
  66. target->addText(line, MetaString::GENERAL_TXT, baseTextID, plural);
  67. target->addNameReplacement(line, plural);
  68. log.push_back(std::move(line));
  69. };
  70. if(m->getSpellIndex() == SpellID::DISEASE)
  71. {
  72. addLogLine(553, boost::logic::indeterminate);
  73. return;
  74. }
  75. for(const auto & bonus : bonuses)
  76. {
  77. switch(bonus.type)
  78. {
  79. case Bonus::NOT_ACTIVE:
  80. {
  81. switch(bonus.subtype)
  82. {
  83. case SpellID::STONE_GAZE:
  84. addLogLine(558, boost::logic::indeterminate);
  85. return;
  86. case SpellID::PARALYZE:
  87. addLogLine(563, boost::logic::indeterminate);
  88. return;
  89. default:
  90. break;
  91. }
  92. }
  93. break;
  94. case Bonus::POISON:
  95. addLogLine(561, boost::logic::indeterminate);
  96. return;
  97. case Bonus::BIND_EFFECT:
  98. addLogLine(-560, true);
  99. return;
  100. case Bonus::STACK_HEALTH:
  101. {
  102. if(bonus.val < 0)
  103. {
  104. BonusList unitHealth = *target->getBonuses(Selector::type(Bonus::STACK_HEALTH));
  105. auto oldHealth = unitHealth.totalValue();
  106. unitHealth.push_back(std::make_shared<Bonus>(bonus));
  107. auto newHealth = unitHealth.totalValue();
  108. //"The %s shrivel with age, and lose %d hit points."
  109. MetaString line;
  110. target->addText(line, MetaString::GENERAL_TXT, 551);
  111. target->addNameReplacement(line);
  112. line.addReplacement(oldHealth - newHealth);
  113. log.push_back(std::move(line));
  114. return;
  115. }
  116. }
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. }
  123. void Timed::prepareEffects(SetStackEffect & sse, const Mechanics * m, const EffectTarget & target, bool describe) const
  124. {
  125. //get default spell duration (spell power with bonuses for heroes)
  126. int32_t duration = m->getEffectDuration();
  127. std::vector<Bonus> converted;
  128. convertBonus(m, duration, converted);
  129. std::shared_ptr<Bonus> bonus = nullptr;
  130. auto casterHero = dynamic_cast<const CGHeroInstance *>(m->caster);
  131. if(casterHero)
  132. bonus = casterHero->getBonusLocalFirst(Selector::typeSubtype(Bonus::SPECIAL_PECULIAR_ENCHANT, m->getSpellIndex()));
  133. //TODO: does hero specialty should affects his stack casting spells?
  134. for(auto & t : target)
  135. {
  136. std::vector<Bonus> buffer;
  137. std::copy(converted.begin(), converted.end(), std::back_inserter(buffer));
  138. const battle::Unit * affected = t.unitValue;
  139. if(!affected)
  140. {
  141. logGlobal->error("[Internal error] Invalid target for timed effect");
  142. continue;
  143. }
  144. if(!affected->alive())
  145. continue;
  146. if(describe)
  147. describeEffect(sse.battleLog, m, converted, affected);
  148. si32 power = 0;
  149. //Apply hero specials - peculiar enchants
  150. const auto tier = std::max(affected->creatureLevel(), 1); //don't divide by 0 for certain creatures (commanders, war machines)
  151. if(bonus)
  152. {
  153. switch(bonus->additionalInfo[0])
  154. {
  155. case 0: //normal
  156. switch(tier)
  157. {
  158. case 1:
  159. case 2:
  160. power = 3;
  161. break;
  162. case 3:
  163. case 4:
  164. power = 2;
  165. break;
  166. case 5:
  167. case 6:
  168. power = 1;
  169. break;
  170. }
  171. for(const Bonus & b : converted)
  172. {
  173. Bonus specialBonus(b);
  174. specialBonus.val = power; //it doesn't necessarily make sense for some spells, use it wisely
  175. specialBonus.turnsRemain = duration;
  176. //additional premy to given effect
  177. buffer.push_back(specialBonus);
  178. }
  179. break;
  180. case 1: //only Coronius as yet
  181. power = std::max(5 - tier, 0);
  182. Bonus specialBonus(Bonus::N_TURNS, Bonus::PRIMARY_SKILL, Bonus::SPELL_EFFECT, power, m->getSpellIndex(), PrimarySkill::ATTACK);
  183. specialBonus.turnsRemain = duration;
  184. buffer.push_back(specialBonus);
  185. break;
  186. }
  187. }
  188. if(casterHero && casterHero->hasBonusOfType(Bonus::SPECIAL_BLESS_DAMAGE, m->getSpellIndex())) //TODO: better handling of bonus percentages
  189. {
  190. int damagePercent = casterHero->valOfBonuses(Bonus::SPECIAL_BLESS_DAMAGE, m->getSpellIndex()) / tier;
  191. Bonus specialBonus(Bonus::N_TURNS, Bonus::CREATURE_DAMAGE, Bonus::SPELL_EFFECT, damagePercent, m->getSpellIndex(), 0, Bonus::PERCENT_TO_ALL);
  192. specialBonus.turnsRemain = duration;
  193. buffer.push_back(specialBonus);
  194. }
  195. if(cumulative)
  196. sse.toAdd.push_back(std::make_pair(affected->unitId(), buffer));
  197. else
  198. sse.toUpdate.push_back(std::make_pair(affected->unitId(), buffer));
  199. }
  200. }
  201. void Timed::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  202. {
  203. assert(!handler.saving);
  204. handler.serializeBool("cumulative", cumulative, false);
  205. {
  206. auto guard = handler.enterStruct("bonus");
  207. const JsonNode & data = handler.getCurrent();
  208. for(const auto & p : data.Struct())
  209. {
  210. //TODO: support JsonSerializeFormat in Bonus
  211. auto guard = handler.enterStruct(p.first);
  212. const JsonNode & bonusNode = handler.getCurrent();
  213. auto b = JsonUtils::parseBonus(bonusNode);
  214. bonus.push_back(b);
  215. }
  216. }
  217. }
  218. }
  219. }