Timed.cpp 7.0 KB

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