Timed.cpp 6.9 KB

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