Timed.cpp 6.5 KB

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