2
0

Timed.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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<const Bonus> peculiarBonus = nullptr;
  130. std::shared_ptr<const Bonus> addedValueBonus = nullptr;
  131. std::shared_ptr<const Bonus> fixedValueBonus = nullptr;
  132. auto casterHero = dynamic_cast<const CGHeroInstance *>(m->caster);
  133. if(casterHero)
  134. {
  135. peculiarBonus = casterHero->getBonusLocalFirst(Selector::typeSubtype(Bonus::SPECIAL_PECULIAR_ENCHANT, m->getSpellIndex()));
  136. addedValueBonus = casterHero->getBonusLocalFirst(Selector::typeSubtype(Bonus::SPECIAL_ADD_VALUE_ENCHANT, m->getSpellIndex()));
  137. fixedValueBonus = casterHero->getBonusLocalFirst(Selector::typeSubtype(Bonus::SPECIAL_FIXED_VALUE_ENCHANT, m->getSpellIndex()));
  138. }
  139. //TODO: does hero specialty should affects his stack casting spells?
  140. for(auto & t : target)
  141. {
  142. std::vector<Bonus> buffer;
  143. std::copy(converted.begin(), converted.end(), std::back_inserter(buffer));
  144. const battle::Unit * affected = t.unitValue;
  145. if(!affected)
  146. {
  147. logGlobal->error("[Internal error] Invalid target for timed effect");
  148. continue;
  149. }
  150. if(!affected->alive())
  151. continue;
  152. if(describe)
  153. describeEffect(sse.battleLog, m, converted, affected);
  154. const auto tier = std::max(affected->creatureLevel(), 1); //don't divide by 0 for certain creatures (commanders, war machines)
  155. //Apply hero specials - peculiar enchants
  156. if(peculiarBonus)
  157. {
  158. si32 power = 0;
  159. switch (peculiarBonus->additionalInfo[0])
  160. {
  161. case 0: //normal
  162. switch (tier)
  163. {
  164. case 1:
  165. case 2:
  166. power = 3;
  167. break;
  168. case 3:
  169. case 4:
  170. power = 2;
  171. break;
  172. case 5:
  173. case 6:
  174. power = 1;
  175. break;
  176. }
  177. break;
  178. case 1:
  179. //Coronius style specialty bonus.
  180. //Please note that actual Coronius isnt here, because Slayer is a spell that doesnt affect monster stats and is used only in calculateDmgRange
  181. power = std::max(5 - tier, 0);
  182. break;
  183. }
  184. if(m->isNegativeSpell())
  185. {
  186. //negative spells like weakness are defined in json with negative numbers, so we need do same here
  187. power = -1 * power;
  188. }
  189. for(Bonus& b : buffer)
  190. {
  191. b.val += power;
  192. }
  193. }
  194. if(addedValueBonus)
  195. {
  196. for(Bonus& b : buffer)
  197. {
  198. b.val += addedValueBonus->additionalInfo[0];
  199. }
  200. }
  201. if(fixedValueBonus)
  202. {
  203. for(Bonus& b : buffer)
  204. {
  205. b.val = fixedValueBonus->additionalInfo[0];
  206. }
  207. }
  208. if(casterHero && casterHero->hasBonusOfType(Bonus::SPECIAL_BLESS_DAMAGE, m->getSpellIndex())) //TODO: better handling of bonus percentages
  209. {
  210. int damagePercent = casterHero->valOfBonuses(Bonus::SPECIAL_BLESS_DAMAGE, m->getSpellIndex()) / tier;
  211. Bonus specialBonus(Bonus::N_TURNS, Bonus::CREATURE_DAMAGE, Bonus::SPELL_EFFECT, damagePercent, m->getSpellIndex(), 0, Bonus::PERCENT_TO_ALL);
  212. specialBonus.turnsRemain = duration;
  213. buffer.push_back(specialBonus);
  214. }
  215. if(cumulative)
  216. sse.toAdd.push_back(std::make_pair(affected->unitId(), buffer));
  217. else
  218. sse.toUpdate.push_back(std::make_pair(affected->unitId(), buffer));
  219. }
  220. }
  221. void Timed::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  222. {
  223. assert(!handler.saving);
  224. handler.serializeBool("cumulative", cumulative, false);
  225. {
  226. auto guard = handler.enterStruct("bonus");
  227. const JsonNode & data = handler.getCurrent();
  228. for(const auto & p : data.Struct())
  229. {
  230. //TODO: support JsonSerializeFormat in Bonus
  231. auto guard = handler.enterStruct(p.first);
  232. const JsonNode & bonusNode = handler.getCurrent();
  233. auto b = JsonUtils::parseBonus(bonusNode);
  234. bonus.push_back(b);
  235. }
  236. }
  237. }
  238. }
  239. }