Timed.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  32. {
  33. const bool describe = server->describeChanges();
  34. int32_t duration = m->getEffectDuration();
  35. std::vector<Bonus> converted;
  36. convertBonus(m, duration, converted);
  37. std::shared_ptr<const Bonus> peculiarBonus = nullptr;
  38. std::shared_ptr<const Bonus> addedValueBonus = nullptr;
  39. std::shared_ptr<const Bonus> fixedValueBonus = nullptr;
  40. auto casterHero = dynamic_cast<const CGHeroInstance *>(m->caster);
  41. if(casterHero)
  42. {
  43. peculiarBonus = casterHero->getBonusLocalFirst(Selector::typeSubtype(Bonus::SPECIAL_PECULIAR_ENCHANT, m->getSpellIndex()));
  44. addedValueBonus = casterHero->getBonusLocalFirst(Selector::typeSubtype(Bonus::SPECIAL_ADD_VALUE_ENCHANT, m->getSpellIndex()));
  45. fixedValueBonus = casterHero->getBonusLocalFirst(Selector::typeSubtype(Bonus::SPECIAL_FIXED_VALUE_ENCHANT, m->getSpellIndex()));
  46. }
  47. //TODO: does hero specialty should affects his stack casting spells?
  48. SetStackEffect sse;
  49. BattleLogMessage blm;
  50. for(auto & t : target)
  51. {
  52. std::vector<Bonus> buffer;
  53. std::copy(converted.begin(), converted.end(), std::back_inserter(buffer));
  54. const battle::Unit * affected = t.unitValue;
  55. if(!affected)
  56. {
  57. logGlobal->error("[Internal error] Invalid target for timed effect");
  58. continue;
  59. }
  60. if(!affected->alive())
  61. continue;
  62. if(describe)
  63. describeEffect(blm.lines, m, converted, affected);
  64. //Apply hero specials - peculiar enchants
  65. const auto tier = std::max(affected->creatureLevel(), 1); //don't divide by 0 for certain creatures (commanders, war machines)
  66. if(peculiarBonus)
  67. {
  68. si32 power = 0;
  69. switch (peculiarBonus->additionalInfo[0])
  70. {
  71. case 0: //normal
  72. switch (tier)
  73. {
  74. case 1:
  75. case 2:
  76. power = 3;
  77. break;
  78. case 3:
  79. case 4:
  80. power = 2;
  81. break;
  82. case 5:
  83. case 6:
  84. power = 1;
  85. break;
  86. }
  87. break;
  88. case 1:
  89. //Coronius style specialty bonus.
  90. //Please note that actual Coronius isnt here, because Slayer is a spell that doesnt affect monster stats and is used only in calculateDmgRange
  91. power = std::max(5 - tier, 0);
  92. break;
  93. }
  94. if(m->isNegativeSpell())
  95. {
  96. //negative spells like weakness are defined in json with negative numbers, so we need do same here
  97. power = -1 * power;
  98. }
  99. for(Bonus& b : buffer)
  100. {
  101. b.val += power;
  102. }
  103. }
  104. if(addedValueBonus)
  105. {
  106. for(Bonus & b : buffer)
  107. {
  108. b.val += addedValueBonus->additionalInfo[0];
  109. }
  110. }
  111. if(fixedValueBonus)
  112. {
  113. for(Bonus & b : buffer)
  114. {
  115. b.val = fixedValueBonus->additionalInfo[0];
  116. }
  117. }
  118. if(casterHero && casterHero->hasBonusOfType(Bonus::SPECIAL_BLESS_DAMAGE, m->getSpellIndex())) //TODO: better handling of bonus percentages
  119. {
  120. int damagePercent = casterHero->valOfBonuses(Bonus::SPECIAL_BLESS_DAMAGE, m->getSpellIndex()) / tier;
  121. Bonus specialBonus(Bonus::N_TURNS, Bonus::CREATURE_DAMAGE, Bonus::SPELL_EFFECT, damagePercent, m->getSpellIndex(), 0, Bonus::PERCENT_TO_ALL);
  122. specialBonus.turnsRemain = duration;
  123. buffer.push_back(specialBonus);
  124. }
  125. if(cumulative)
  126. sse.toAdd.push_back(std::make_pair(affected->unitId(), buffer));
  127. else
  128. sse.toUpdate.push_back(std::make_pair(affected->unitId(), buffer));
  129. }
  130. if(!(sse.toAdd.empty() && sse.toUpdate.empty()))
  131. server->apply(&sse);
  132. if(describe && !blm.lines.empty())
  133. server->apply(&blm);
  134. }
  135. void Timed::convertBonus(const Mechanics * m, int32_t & duration, std::vector<Bonus> & converted) const
  136. {
  137. int32_t maxDuration = 0;
  138. for(const std::shared_ptr<Bonus> & b : bonus)
  139. {
  140. Bonus nb(*b);
  141. //use configured duration if present
  142. if(nb.turnsRemain == 0)
  143. nb.turnsRemain = duration;
  144. vstd::amax(maxDuration, nb.turnsRemain);
  145. nb.sid = m->getSpellIndex(); //for all
  146. nb.source = Bonus::SPELL_EFFECT;//for all
  147. //fix to original config: shield should display damage reduction
  148. if((nb.sid == SpellID::SHIELD || nb.sid == SpellID::AIR_SHIELD) && (nb.type == Bonus::GENERAL_DAMAGE_REDUCTION))
  149. nb.val = 100 - nb.val;
  150. //we need to know who cast Bind
  151. else if(nb.sid == SpellID::BIND && nb.type == Bonus::BIND_EFFECT && m->caster->getCasterUnitId() >= 0)
  152. nb.additionalInfo = m->caster->getCasterUnitId();
  153. converted.push_back(nb);
  154. }
  155. //if all spell effects have special duration, use it later for special bonuses
  156. duration = maxDuration;
  157. }
  158. void Timed::describeEffect(std::vector<MetaString> & log, const Mechanics * m, const std::vector<Bonus> & bonuses, const battle::Unit * target) const
  159. {
  160. auto addLogLine = [&](const int32_t baseTextID, const boost::logic::tribool & plural)
  161. {
  162. MetaString line;
  163. target->addText(line, MetaString::GENERAL_TXT, baseTextID, plural);
  164. target->addNameReplacement(line, plural);
  165. log.push_back(std::move(line));
  166. };
  167. if(m->getSpellIndex() == SpellID::DISEASE)
  168. {
  169. addLogLine(553, boost::logic::indeterminate);
  170. return;
  171. }
  172. for(const auto & bonus : bonuses)
  173. {
  174. switch(bonus.type)
  175. {
  176. case Bonus::NOT_ACTIVE:
  177. {
  178. switch(bonus.subtype)
  179. {
  180. case SpellID::STONE_GAZE:
  181. addLogLine(558, boost::logic::indeterminate);
  182. return;
  183. case SpellID::PARALYZE:
  184. addLogLine(563, boost::logic::indeterminate);
  185. return;
  186. default:
  187. break;
  188. }
  189. }
  190. break;
  191. case Bonus::POISON:
  192. addLogLine(561, boost::logic::indeterminate);
  193. return;
  194. case Bonus::BIND_EFFECT:
  195. addLogLine(-560, true);
  196. return;
  197. case Bonus::STACK_HEALTH:
  198. {
  199. if(bonus.val < 0)
  200. {
  201. BonusList unitHealth = *target->getBonuses(Selector::type()(Bonus::STACK_HEALTH));
  202. auto oldHealth = unitHealth.totalValue();
  203. unitHealth.push_back(std::make_shared<Bonus>(bonus));
  204. auto newHealth = unitHealth.totalValue();
  205. //"The %s shrivel with age, and lose %d hit points."
  206. MetaString line;
  207. target->addText(line, MetaString::GENERAL_TXT, 551);
  208. target->addNameReplacement(line);
  209. line.addReplacement(oldHealth - newHealth);
  210. log.push_back(std::move(line));
  211. return;
  212. }
  213. }
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. }
  220. void Timed::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  221. {
  222. assert(!handler.saving);
  223. handler.serializeBool("cumulative", cumulative, false);
  224. {
  225. auto guard = handler.enterStruct("bonus");
  226. const JsonNode & data = handler.getCurrent();
  227. for(const auto & p : data.Struct())
  228. {
  229. //TODO: support JsonSerializeFormat in Bonus
  230. auto guard = handler.enterStruct(p.first);
  231. const JsonNode & bonusNode = handler.getCurrent();
  232. auto b = JsonUtils::parseBonus(bonusNode);
  233. bonus.push_back(b);
  234. }
  235. }
  236. }
  237. }
  238. }