Timed.cpp 6.8 KB

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