Effects.cpp 3.5 KB

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