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