2017-07-20 06:08:49 +02:00
|
|
|
/*
|
|
|
|
* Effects.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include "Effects.h"
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
#include <vcmi/spells/Caster.h>
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
#include "../ISpellMechanics.h"
|
|
|
|
|
|
|
|
#include "../../serializer/JsonSerializeFormat.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
|
|
|
|
namespace spells
|
|
|
|
{
|
|
|
|
namespace effects
|
|
|
|
{
|
|
|
|
|
|
|
|
Effects::Effects() = default;
|
|
|
|
|
|
|
|
Effects::~Effects() = default;
|
|
|
|
|
|
|
|
void Effects::add(const std::string & name, std::shared_ptr<Effect> effect, const int level)
|
|
|
|
{
|
|
|
|
effect->name = name;
|
|
|
|
data.at(level)[name] = effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Effects::applicable(Problem & problem, const Mechanics * m) const
|
|
|
|
{
|
|
|
|
//stop on first problem
|
|
|
|
//require all not optional effects to be applicable in general
|
|
|
|
//f.e. FireWall damage effect also need to have smart target
|
|
|
|
|
|
|
|
bool requiredEffectNotBlocked = true;
|
|
|
|
bool oneEffectApplicable = false;
|
|
|
|
|
|
|
|
auto callback = [&](const Effect * e, bool & stop)
|
|
|
|
{
|
|
|
|
if(e->applicable(problem, m))
|
|
|
|
{
|
|
|
|
oneEffectApplicable = true;
|
|
|
|
}
|
|
|
|
else if(!e->optional)
|
|
|
|
{
|
|
|
|
requiredEffectNotBlocked = false;
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
forEachEffect(m->getEffectLevel(), callback);
|
|
|
|
|
|
|
|
return requiredEffectNotBlocked && oneEffectApplicable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Effects::applicable(Problem & problem, const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
|
|
|
|
{
|
|
|
|
//stop on first problem
|
|
|
|
//require all direct and not optional effects to be applicable at this aimPoint
|
|
|
|
//f.e. FireWall do not need damage target here, only a place to put obstacle
|
|
|
|
|
|
|
|
bool requiredEffectNotBlocked = true;
|
|
|
|
bool oneEffectApplicable = false;
|
|
|
|
|
|
|
|
auto callback = [&](const Effect * e, bool & stop)
|
|
|
|
{
|
|
|
|
if(e->indirect)
|
|
|
|
return;
|
|
|
|
|
|
|
|
EffectTarget target = e->transformTarget(m, aimPoint, spellTarget);
|
|
|
|
|
|
|
|
if(e->applicable(problem, m, target))
|
|
|
|
{
|
|
|
|
oneEffectApplicable = true;
|
|
|
|
}
|
|
|
|
else if(!e->optional)
|
|
|
|
{
|
|
|
|
requiredEffectNotBlocked = false;
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
forEachEffect(m->getEffectLevel(), callback);
|
|
|
|
|
|
|
|
return requiredEffectNotBlocked && oneEffectApplicable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Effects::forEachEffect(const int level, const std::function<void(const Effect *, bool &)> & callback) const
|
|
|
|
{
|
|
|
|
bool stop = false;
|
|
|
|
for(auto one : data.at(level))
|
|
|
|
{
|
|
|
|
callback(one.second.get(), stop);
|
|
|
|
if(stop)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Effects::EffectsToApply Effects::prepare(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
|
|
|
|
{
|
|
|
|
EffectsToApply effectsToApply;
|
|
|
|
|
|
|
|
auto callback = [&](const Effect * e, bool & stop)
|
|
|
|
{
|
|
|
|
bool applyThis = false;
|
|
|
|
|
|
|
|
//todo: find a better way to handle such special cases
|
|
|
|
|
|
|
|
if(m->getSpellIndex() == SpellID::RESURRECTION && e->name == "cure")
|
2018-03-04 10:13:07 +02:00
|
|
|
applyThis = (m->caster->getCasterUnitId() >= 0);
|
2017-07-20 06:08:49 +02:00
|
|
|
else
|
|
|
|
applyThis = !e->indirect;
|
|
|
|
|
|
|
|
if(applyThis)
|
|
|
|
{
|
|
|
|
EffectTarget target = e->transformTarget(m, aimPoint, spellTarget);
|
|
|
|
effectsToApply.push_back(std::make_pair(e, target));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
forEachEffect(m->getEffectLevel(), callback);
|
|
|
|
|
|
|
|
return effectsToApply;
|
|
|
|
}
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
void Effects::serializeJson(const Registry * registry, JsonSerializeFormat & handler, const int level)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
assert(!handler.saving);
|
|
|
|
|
|
|
|
const JsonNode & effectMap = handler.getCurrent();
|
|
|
|
|
|
|
|
for(auto & p : effectMap.Struct())
|
|
|
|
{
|
|
|
|
const std::string & name = p.first;
|
|
|
|
|
|
|
|
auto guard = handler.enterStruct(name);
|
|
|
|
|
|
|
|
std::string type;
|
|
|
|
handler.serializeString("type", type);
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
auto effect = Effect::create(registry, type);
|
2017-07-20 06:08:49 +02:00
|
|
|
if(effect)
|
|
|
|
{
|
|
|
|
effect->serializeJson(handler);
|
|
|
|
add(name, effect, level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|