2
0

Effects.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Effects.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 "Effects.h"
  12. #include <vcmi/spells/Caster.h>
  13. #include "../ISpellMechanics.h"
  14. #include "../../serializer/JsonSerializeFormat.h"
  15. namespace spells
  16. {
  17. namespace effects
  18. {
  19. Effects::Effects() = default;
  20. Effects::~Effects() = default;
  21. void Effects::add(const std::string & name, std::shared_ptr<Effect> effect, const int level)
  22. {
  23. effect->name = name;
  24. data.at(level)[name] = effect;
  25. }
  26. bool Effects::applicable(Problem & problem, const Mechanics * m) const
  27. {
  28. //stop on first problem
  29. //require all not optional effects to be applicable in general
  30. //f.e. FireWall damage effect also need to have smart target
  31. bool requiredEffectNotBlocked = true;
  32. bool oneEffectApplicable = false;
  33. auto callback = [&](const Effect * e, bool & stop)
  34. {
  35. if(e->applicable(problem, m))
  36. {
  37. oneEffectApplicable = true;
  38. }
  39. else if(!e->optional)
  40. {
  41. requiredEffectNotBlocked = false;
  42. stop = true;
  43. }
  44. };
  45. forEachEffect(m->getEffectLevel(), callback);
  46. return requiredEffectNotBlocked && oneEffectApplicable;
  47. }
  48. bool Effects::applicable(Problem & problem, const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  49. {
  50. //stop on first problem
  51. //require all direct and not optional effects to be applicable at this aimPoint
  52. //f.e. FireWall do not need damage target here, only a place to put obstacle
  53. bool requiredEffectNotBlocked = true;
  54. bool oneEffectApplicable = false;
  55. auto callback = [&](const Effect * e, bool & stop)
  56. {
  57. if(e->indirect)
  58. return;
  59. EffectTarget target = e->transformTarget(m, aimPoint, spellTarget);
  60. if(e->applicable(problem, m, target))
  61. {
  62. oneEffectApplicable = true;
  63. }
  64. else if(!e->optional)
  65. {
  66. requiredEffectNotBlocked = false;
  67. stop = true;
  68. }
  69. };
  70. forEachEffect(m->getEffectLevel(), callback);
  71. return requiredEffectNotBlocked && oneEffectApplicable;
  72. }
  73. void Effects::forEachEffect(const int level, const std::function<void(const Effect *, bool &)> & callback) const
  74. {
  75. bool stop = false;
  76. for(auto one : data.at(level))
  77. {
  78. callback(one.second.get(), stop);
  79. if(stop)
  80. return;
  81. }
  82. }
  83. Effects::EffectsToApply Effects::prepare(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  84. {
  85. EffectsToApply effectsToApply;
  86. auto callback = [&](const Effect * e, bool & stop)
  87. {
  88. bool applyThis = false;
  89. //todo: find a better way to handle such special cases
  90. if(m->getSpellIndex() == SpellID::RESURRECTION && e->name == "cure")
  91. applyThis = (m->caster->getCasterUnitId() >= 0);
  92. else
  93. applyThis = !e->indirect;
  94. if(applyThis)
  95. {
  96. EffectTarget target = e->transformTarget(m, aimPoint, spellTarget);
  97. effectsToApply.push_back(std::make_pair(e, target));
  98. }
  99. };
  100. forEachEffect(m->getEffectLevel(), callback);
  101. return effectsToApply;
  102. }
  103. void Effects::serializeJson(const Registry * registry, JsonSerializeFormat & handler, const int level)
  104. {
  105. assert(!handler.saving);
  106. const JsonNode & effectMap = handler.getCurrent();
  107. for(auto & p : effectMap.Struct())
  108. {
  109. const std::string & name = p.first;
  110. auto guard = handler.enterStruct(name);
  111. std::string type;
  112. handler.serializeString("type", type);
  113. auto effect = Effect::create(registry, type);
  114. if(effect)
  115. {
  116. effect->serializeJson(handler);
  117. add(name, effect, level);
  118. }
  119. }
  120. }
  121. }
  122. }