Timed.cpp 7.1 KB

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