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
|
|
|
/*
|
|
|
|
* LuaSpellEffect.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 "LuaSpellEffect.h"
|
|
|
|
|
|
|
|
#include <vcmi/scripting/Service.h>
|
|
|
|
|
|
|
|
#include "../../lib/spells/effects/Registry.h"
|
|
|
|
#include "../../lib/spells/ISpellMechanics.h"
|
|
|
|
|
|
|
|
#include "../../lib/battle/Unit.h"
|
|
|
|
#include "../../lib/battle/CBattleInfoCallback.h"
|
2024-02-11 23:09:01 +02:00
|
|
|
#include "../../lib/json/JsonUtils.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 "../../lib/serializer/JsonSerializeFormat.h"
|
|
|
|
|
|
|
|
static const std::string APPLICABLE_GENERAL = "applicable";
|
|
|
|
static const std::string APPLICABLE_TARGET = "applicableTarget";
|
|
|
|
static const std::string APPLY = "apply";
|
|
|
|
|
2022-08-15 13:38:17 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
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
|
|
|
namespace spells
|
|
|
|
{
|
|
|
|
namespace effects
|
|
|
|
{
|
|
|
|
|
|
|
|
LuaSpellEffectFactory::LuaSpellEffectFactory(const Script * script_)
|
|
|
|
: script(script_)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaSpellEffectFactory::~LuaSpellEffectFactory() = default;
|
|
|
|
|
|
|
|
Effect * LuaSpellEffectFactory::create() const
|
|
|
|
{
|
|
|
|
return new LuaSpellEffect(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaSpellEffect::LuaSpellEffect(const Script * script_)
|
|
|
|
: script(script_)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaSpellEffect::~LuaSpellEffect() = default;
|
|
|
|
|
|
|
|
void LuaSpellEffect::adjustTargetTypes(std::vector<TargetType> & types) const
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaSpellEffect::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaSpellEffect::applicable(Problem & problem, const Mechanics * m) const
|
|
|
|
{
|
|
|
|
std::shared_ptr<scripting::Context> context = resolveScript(m);
|
|
|
|
if(!context)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
setContextVariables(m, context);
|
|
|
|
|
|
|
|
JsonNode response = context->callGlobal(APPLICABLE_GENERAL, JsonNode());
|
|
|
|
|
|
|
|
if(response.getType() != JsonNode::JsonType::DATA_BOOL)
|
|
|
|
{
|
|
|
|
logMod->error("Invalid API response from script %s.", script->getName());
|
2024-02-12 01:22:16 +02:00
|
|
|
logMod->debug(response.toCompactString());
|
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
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return response.Bool();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaSpellEffect::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
|
|
|
std::shared_ptr<scripting::Context> context = resolveScript(m);
|
|
|
|
if(!context)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
setContextVariables(m, context);
|
|
|
|
|
|
|
|
JsonNode requestP;
|
|
|
|
|
|
|
|
if(target.empty())
|
|
|
|
return false;
|
|
|
|
|
2023-02-05 18:24:34 +02:00
|
|
|
for(const auto & dest : target)
|
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
|
|
|
{
|
|
|
|
JsonNode targetData;
|
2024-02-13 14:34:16 +02:00
|
|
|
targetData.Vector().emplace_back(dest.hexValue.hex);
|
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
|
|
|
|
|
|
|
if(dest.unitValue)
|
2024-02-13 14:34:16 +02:00
|
|
|
targetData.Vector().emplace_back(dest.unitValue->unitId());
|
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
|
|
|
else
|
2024-02-13 14:34:16 +02:00
|
|
|
targetData.Vector().emplace_back(-1);
|
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
|
|
|
|
|
|
|
requestP.Vector().push_back(targetData);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode request;
|
|
|
|
request.Vector().push_back(requestP);
|
|
|
|
|
|
|
|
JsonNode response = context->callGlobal(APPLICABLE_TARGET, request);
|
|
|
|
|
|
|
|
if(response.getType() != JsonNode::JsonType::DATA_BOOL)
|
|
|
|
{
|
|
|
|
logMod->error("Invalid API response from script %s.", script->getName());
|
2024-02-12 01:22:16 +02:00
|
|
|
logMod->debug(response.toCompactString());
|
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
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return response.Bool();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaSpellEffect::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
|
|
|
if(target.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::shared_ptr<scripting::Context> context = resolveScript(m);
|
|
|
|
if(!context)
|
|
|
|
{
|
|
|
|
server->complain("Unable to create scripting context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setContextVariables(m, context);
|
|
|
|
|
|
|
|
JsonNode requestP;
|
|
|
|
|
2023-02-05 18:24:34 +02:00
|
|
|
for(const auto & dest : target)
|
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
|
|
|
{
|
|
|
|
JsonNode targetData;
|
2024-02-13 14:34:16 +02:00
|
|
|
targetData.Vector().emplace_back(dest.hexValue.hex);
|
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
|
|
|
|
|
|
|
if(dest.unitValue)
|
2024-02-13 14:34:16 +02:00
|
|
|
targetData.Vector().emplace_back(dest.unitValue->unitId());
|
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
|
|
|
else
|
2024-02-13 14:34:16 +02:00
|
|
|
targetData.Vector().emplace_back(-1);
|
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
|
|
|
|
|
|
|
requestP.Vector().push_back(targetData);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonNode request;
|
|
|
|
request.Vector().push_back(requestP);
|
|
|
|
|
|
|
|
context->callGlobal(server, APPLY, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget LuaSpellEffect::filterTarget(const Mechanics * m, const EffectTarget & target) const
|
|
|
|
{
|
|
|
|
return EffectTarget(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
EffectTarget LuaSpellEffect::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
|
|
|
|
{
|
|
|
|
return EffectTarget(spellTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaSpellEffect::serializeJsonEffect(JsonSerializeFormat & handler)
|
|
|
|
{
|
|
|
|
//TODO: load everything and provide to script
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Context> LuaSpellEffect::resolveScript(const Mechanics * m) const
|
|
|
|
{
|
|
|
|
return m->battle()->getContextPool()->getContext(script);
|
|
|
|
}
|
|
|
|
|
2023-02-05 18:24:34 +02:00
|
|
|
void LuaSpellEffect::setContextVariables(const Mechanics * m, const std::shared_ptr<Context>& context)
|
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
|
|
|
{
|
|
|
|
context->setGlobal("effectLevel", m->getEffectLevel());
|
|
|
|
context->setGlobal("effectRangeLevel", m->getRangeLevel());
|
|
|
|
context->setGlobal("effectPower", m->getEffectPower());
|
|
|
|
context->setGlobal("effectDuration", m->getEffectDuration());
|
|
|
|
context->setGlobal("effectValue", static_cast<int>(m->getEffectValue()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 13:38:17 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|