File: LuaSpellEffect.cpp

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (190 lines) | stat: -rw-r--r-- 4,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * 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"
#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";

VCMI_LIB_NAMESPACE_BEGIN

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());
		logMod->debug(response.toJson(true));
		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;

	for(auto & dest : target)
	{
		JsonNode targetData;
		targetData.Vector().push_back(JsonUtils::intNode(dest.hexValue.hex));

		if(dest.unitValue)
			targetData.Vector().push_back(JsonUtils::intNode(dest.unitValue->unitId()));
		else
			targetData.Vector().push_back(JsonUtils::intNode(-1));

		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());
		logMod->debug(response.toJson(true));
		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;

	for(auto & dest : target)
	{
		JsonNode targetData;
		targetData.Vector().push_back(JsonUtils::intNode(dest.hexValue.hex));

		if(dest.unitValue)
			targetData.Vector().push_back(JsonUtils::intNode(dest.unitValue->unitId()));
		else
			targetData.Vector().push_back(JsonUtils::intNode(-1));

		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);
}

void LuaSpellEffect::setContextVariables(const Mechanics * m, std::shared_ptr<Context> context) const
{
	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()));
}
}
}

VCMI_LIB_NAMESPACE_END