File: Obstacle.cpp

package info (click to toggle)
vcmi 0.99%2Bdfsg%2Bgit20190113.f06c8a87-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 11,096 kB
  • sloc: cpp: 142,605; sh: 315; objc: 248; makefile: 32; ansic: 28; python: 13
file content (342 lines) | stat: -rw-r--r-- 8,641 bytes parent folder | download | duplicates (2)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Obstacle.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 "Obstacle.h"

#include "Registry.h"
#include "../ISpellMechanics.h"

#include "../../NetPacks.h"
#include "../../battle/IBattleState.h"
#include "../../battle/CBattleInfoCallback.h"
#include "../../serializer/JsonSerializeFormat.h"

static const std::string EFFECT_NAME = "core:obstacle";

namespace spells
{
namespace effects
{

VCMI_REGISTER_SPELL_EFFECT(Obstacle, EFFECT_NAME);

ObstacleSideOptions::ObstacleSideOptions()
	: shape(),
	range()
{
}

void ObstacleSideOptions::serializeJson(JsonSerializeFormat & handler)
{
	serializeRelativeShape(handler, "shape", shape);
	serializeRelativeShape(handler, "range", range);

	handler.serializeString("appearAnimation", appearAnimation);
	handler.serializeString("animation", animation);

	handler.serializeInt("offsetY", offsetY);
}

void ObstacleSideOptions::serializeRelativeShape(JsonSerializeFormat & handler, const std::string & fieldName, RelativeShape & value)
{
	static const std::vector<std::string> EDirMap =
	{
		"TL",
		"TR",
		"R",
		"BR",
		"BL",
		"L",
		""
	};

	{
		JsonArraySerializer outer = handler.enterArray(fieldName);
		outer.syncSize(value, JsonNode::JsonType::DATA_VECTOR);

		for(size_t outerIndex = 0; outerIndex < outer.size(); outerIndex++)
		{
			JsonArraySerializer inner = outer.enterArray(outerIndex);
			inner.syncSize(value.at(outerIndex), JsonNode::JsonType::DATA_STRING);

			for(size_t innerIndex = 0; innerIndex < inner.size(); innerIndex++)
			{
				std::string temp;

				if(handler.saving)
				{
					temp = EDirMap.at(value.at(outerIndex).at(innerIndex));
				}

				inner.serializeString(innerIndex, temp);

				if(!handler.saving)
				{
					value.at(outerIndex).at(innerIndex) = (BattleHex::EDir) vstd::find_pos(EDirMap, temp);
				}
			}
		}
	}

	if(!handler.saving)
	{
		if(value.empty())
			value.emplace_back();

		if(value.back().empty())
			value.back().emplace_back(BattleHex::EDir::NONE);
	}
}

Obstacle::Obstacle()
	: LocationEffect(),
	hidden(false),
	passable(false),
	trigger(false),
	trap(false),
	removeOnTrigger(false),
	patchCount(1),
	turnsRemaining(-1)
{
}

Obstacle::~Obstacle() = default;

void Obstacle::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
{
	EffectTarget effectTarget = transformTarget(m, spellTarget, spellTarget);

	const ObstacleSideOptions & options = sideOptions.at(m->casterSide);

	for(auto & destination : effectTarget)
	{
		for(auto & trasformation : options.shape)
		{
			BattleHex hex = destination.hexValue;

			for(auto direction : trasformation)
				hex.moveInDirection(direction, false);

			if(hex.isValid())
				hexes.insert(hex);
		}
	}
}

bool Obstacle::applicable(Problem & problem, const Mechanics * m) const
{
	return LocationEffect::applicable(problem, m);
}

bool Obstacle::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
{
	if(!m->isMassive())
	{
		const bool requiresClearTiles = m->requiresClearTiles();
		const ObstacleSideOptions & options = sideOptions.at(m->casterSide);

		if(target.empty())
			return noRoomToPlace(problem, m);

		for(const auto & destination : target)
		{
			for(auto & trasformation : options.shape)
			{
				BattleHex hex = destination.hexValue;
				for(auto direction : trasformation)
					hex.moveInDirection(direction, false);

				if(!isHexAvailable(m->cb, hex, requiresClearTiles))
					return noRoomToPlace(problem, m);
			}
		}
	}

	return LocationEffect::applicable(problem, m, target);
}

EffectTarget Obstacle::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
{
	const ObstacleSideOptions & options = sideOptions.at(m->casterSide);

	EffectTarget ret;

	if(!m->isMassive())
	{
		for(auto & spellDestination : spellTarget)
		{
			for(auto & rangeShape : options.range)
			{
				BattleHex hex = spellDestination.hexValue;

				for(auto direction : rangeShape)
					hex.moveInDirection(direction, false);

				ret.emplace_back(hex);
			}
		}
	}

	return ret;
}

void Obstacle::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
{
	if(m->isMassive())
	{
		std::vector<BattleHex> availableTiles;
		for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
		{
			BattleHex hex = i;
			if(isHexAvailable(m->cb, hex, true))
				availableTiles.push_back(hex);
		}
		RandomGeneratorUtil::randomShuffle(availableTiles, rng);

		const int patchesToPut = std::min<int>(patchCount, availableTiles.size());

		EffectTarget randomTarget;
		randomTarget.reserve(patchesToPut);
		for(int i = 0; i < patchesToPut; i++)
			randomTarget.emplace_back(availableTiles.at(i));

		placeObstacles(battleState, m, randomTarget);
	}
	else
	{
		placeObstacles(battleState, m, target);
	}
}

void Obstacle::serializeJsonEffect(JsonSerializeFormat & handler)
{
	handler.serializeBool("hidden", hidden);
	handler.serializeBool("passable", passable);
	handler.serializeBool("trigger", trigger);
	handler.serializeBool("trap", trap);
    handler.serializeBool("removeOnTrigger", removeOnTrigger);

	handler.serializeInt("patchCount", patchCount);
	handler.serializeInt("turnsRemaining", turnsRemaining, -1);

	handler.serializeStruct("attacker", sideOptions.at(BattleSide::ATTACKER));
	handler.serializeStruct("defender", sideOptions.at(BattleSide::DEFENDER));
}

bool Obstacle::isHexAvailable(const CBattleInfoCallback * cb, const BattleHex & hex, const bool mustBeClear)
{
	if(!hex.isAvailable())
		return false;

	if(!mustBeClear)
		return true;

	if(cb->battleGetUnitByPos(hex, true))
		return false;

	auto obst = cb->battleGetAllObstaclesOnPos(hex, false);

	for(auto & i : obst)
		if(i->obstacleType != CObstacleInstance::MOAT)
			return false;

	if(cb->battleGetSiegeLevel() != 0)
	{
		EWallPart::EWallPart part = cb->battleHexToWallPart(hex);

		if(part == EWallPart::INVALID || part == EWallPart::INDESTRUCTIBLE_PART_OF_GATE)
			return true;//no fortification here
		else if(static_cast<int>(part) < 0)
			return false;//indestructible part (cant be checked by battleGetWallState)
		else if(part == EWallPart::BOTTOM_TOWER || part == EWallPart::UPPER_TOWER)
			return false;//destructible, but should not be available
		else if(cb->battleGetWallState(part) != EWallState::DESTROYED && cb->battleGetWallState(part) != EWallState::NONE)
			return false;
	}

	return true;
}

bool Obstacle::noRoomToPlace(Problem & problem, const Mechanics * m)
{
	MetaString text;
	text.addTxt(MetaString::GENERAL_TXT, 181);//No room to place %s here
	text.addReplacement(m->getSpellName());
	problem.add(std::move(text));
	return false;
}

void Obstacle::placeObstacles(BattleStateProxy * battleState, const Mechanics * m, const EffectTarget & target) const
{
	const ObstacleSideOptions & options = sideOptions.at(m->casterSide);

	BattleObstaclesChanged pack;

	boost::optional<BattlePerspective::BattlePerspective> perspective;

	if(!m->cb->getPlayerID())
		perspective = boost::make_optional(BattlePerspective::ALL_KNOWING);

	auto all = m->cb->battleGetAllObstacles(perspective);

	int obstacleIdToGive = 1;
	for(auto & one : all)
		if(one->uniqueID >= obstacleIdToGive)
			obstacleIdToGive = one->uniqueID + 1;

	for(const Destination & destination : target)
	{
		SpellCreatedObstacle obstacle;
		obstacle.uniqueID = obstacleIdToGive++;
		obstacle.pos = destination.hexValue;
		obstacle.obstacleType = CObstacleInstance::USUAL;
		obstacle.ID = m->getSpellIndex();

		obstacle.turnsRemaining = turnsRemaining;
		obstacle.casterSpellPower = m->getEffectPower();
		obstacle.spellLevel = m->getEffectLevel();//todo: level of indirect effect should be also configurable
		obstacle.casterSide = m->casterSide;

		obstacle.hidden = hidden;
		obstacle.passable = passable;
		obstacle.trigger = trigger;
		obstacle.trap = trap;
		obstacle.removeOnTrigger = removeOnTrigger;

		obstacle.appearAnimation = options.appearAnimation;
		obstacle.animation = options.animation;

		obstacle.animationYOffset = options.offsetY;

		obstacle.customSize.clear();
		obstacle.customSize.reserve(options.shape.size());

		for(auto & shape : options.shape)
		{
			BattleHex hex = destination.hexValue;

			for(auto direction : shape)
				hex.moveInDirection(direction, false);

			obstacle.customSize.emplace_back(hex);
		}

		pack.changes.emplace_back();
		obstacle.toInfo(pack.changes.back());
	}

	if(!pack.changes.empty())
		battleState->apply(&pack);
}


}
}