Timed.cpp 6.5 KB

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