File: Catapult.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (285 lines) | stat: -rw-r--r-- 8,493 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
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
/*
 * Catapult.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 "Catapult.h"

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

#include "../../battle/IBattleState.h"
#include "../../battle/CBattleInfoCallback.h"
#include "../../battle/Unit.h"
#include "../../mapObjects/CGTownInstance.h"
#include "../../entities/building/TownFortifications.h"
#include "../../networkPacks/PacksForClientBattle.h"
#include "../../serializer/JsonSerializeFormat.h"

#include <vstd/RNG.h>

VCMI_LIB_NAMESPACE_BEGIN

namespace spells
{
namespace effects
{

bool Catapult::applicable(Problem & problem, const Mechanics * m) const
{
	const auto *town = m->battle()->battleGetDefendedTown();

	if(nullptr == town)
	{
		return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
	}

	if(town->fortificationsLevel().wallsHealth == 0)
	{
		return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
	}

	if(m->isSmart() && m->casterSide != BattleSide::ATTACKER)
	{
		//if spell targeting is smart, then only attacker can use it
		return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
	}

	const auto attackableBattleHexes = m->battle()->getAttackableBattleHexes();

	return !attackableBattleHexes.empty() || m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
}

void Catapult::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & eTarget) const
{
	if(m->isMassive())
		applyMassive(server, m); // Like earthquake
	else
		applyTargeted(server, m, eTarget); // Like catapult shots
}


void Catapult::applyMassive(ServerCallback * server, const Mechanics * m) const
{
	//start with all destructible parts
	std::vector<EWallPart> allowedTargets = getPotentialTargets(m, true, true);

	assert(!allowedTargets.empty());
	if (allowedTargets.empty())
		return;

	CatapultAttack ca;
	ca.battleID = m->battle()->getBattle()->getBattleID();
	ca.attacker = m->caster->getHeroCaster() ? -1 : m->caster->getCasterUnitId();

	for(int i = 0; i < targetsToAttack; i++)
	{
		// Hit on any existing, not destroyed targets are allowed
		// Multiple hit on same target are allowed.
		// Potential overshots (more hits on same targets than remaining HP) are allowed
		EWallPart target = *RandomGeneratorUtil::nextItem(allowedTargets, *server->getRNG());

		auto attackInfo = ca.attackedParts.begin();
		for ( ; attackInfo != ca.attackedParts.end(); ++attackInfo)
			if ( attackInfo->attackedPart == target )
				break;

		if (attackInfo == ca.attackedParts.end()) // new part
		{
			CatapultAttack::AttackInfo newInfo;
			newInfo.damageDealt = getRandomDamage(server);
			newInfo.attackedPart = target;
			newInfo.destinationTile = m->battle()->wallPartToBattleHex(target).toInt();
			ca.attackedParts.push_back(newInfo);
			attackInfo = ca.attackedParts.end() - 1;
		}
		else // already damaged before, update damage
		{
			attackInfo->damageDealt += getRandomDamage(server);
		}
	}
	server->apply(ca);

	removeTowerShooters(server, m);
}

void Catapult::applyTargeted(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
{
	assert(!target.empty());
	auto destination = target.at(0).hexValue;
	auto desiredTarget = m->battle()->battleHexToWallPart(destination);

	for(int i = 0; i < targetsToAttack; i++)
	{
		auto actualTarget = EWallPart::INVALID;

		if ( m->battle()->isWallPartAttackable(desiredTarget) &&
				server->getRNG()->nextInt(0, 99) < getCatapultHitChance(desiredTarget))
		{
			actualTarget = desiredTarget;
		}
		else
		{
			std::vector<EWallPart> potentialTargets = getPotentialTargets(m, false, false);

			if (potentialTargets.empty())
				break; // everything is gone, can't attack anymore

			actualTarget = *RandomGeneratorUtil::nextItem(potentialTargets, *server->getRNG());
		}
		assert(actualTarget != EWallPart::INVALID);

		CatapultAttack::AttackInfo attack;
		attack.attackedPart = actualTarget;
		attack.destinationTile = m->battle()->wallPartToBattleHex(actualTarget).toInt();
		attack.damageDealt = getRandomDamage(server);

		CatapultAttack ca; //package for clients
		ca.battleID = m->battle()->getBattle()->getBattleID();
		ca.attacker = m->caster->getHeroCaster() ? -1 : m->caster->getCasterUnitId();
		ca.attackedParts.push_back(attack);
		server->apply(ca);
		removeTowerShooters(server, m);
	}
}

int Catapult::getCatapultHitChance(EWallPart part) const
{
	switch(part)
	{
	case EWallPart::GATE:
		return gate;
	case EWallPart::KEEP:
		return keep;
	case EWallPart::BOTTOM_TOWER:
	case EWallPart::UPPER_TOWER:
		return tower;
	case EWallPart::BOTTOM_WALL:
	case EWallPart::BELOW_GATE:
	case EWallPart::OVER_GATE:
	case EWallPart::UPPER_WALL:
		return wall;
	default:
		return 0;
	}
}

int Catapult::getRandomDamage (ServerCallback * server) const
{
	std::array<int, 3> damageChances = { noDmg, hit, crit }; //dmgChance[i] - chance for doing i dmg when hit is successful
	int totalChance = std::accumulate(damageChances.begin(), damageChances.end(), 0);
	int damageRandom = server->getRNG()->nextInt(0, totalChance - 1);
	int dealtDamage = 0;

	//calculating dealt damage
	for (int damage = 0; damage < damageChances.size(); ++damage)
	{
		if (damageRandom <= damageChances[damage])
		{
			dealtDamage = damage;
			break;
		}
		damageRandom -= damageChances[damage];
	}
	return dealtDamage;
}

void Catapult::removeTowerShooters(ServerCallback * server, const Mechanics * m) const
{
	BattleUnitsChanged removeUnits;
	removeUnits.battleID = m->battle()->getBattle()->getBattleID();

	for (auto const wallPart : { EWallPart::KEEP, EWallPart::BOTTOM_TOWER, EWallPart::UPPER_TOWER })
	{
		//removing creatures in turrets / keep if one is destroyed
		BattleHex posRemove;
		auto state = m->battle()->battleGetWallState(wallPart);

		switch(wallPart)
		{
		case EWallPart::KEEP:
			posRemove = BattleHex::CASTLE_CENTRAL_TOWER;
			break;
		case EWallPart::BOTTOM_TOWER:
			posRemove = BattleHex::CASTLE_BOTTOM_TOWER;
			break;
		case EWallPart::UPPER_TOWER:
			posRemove = BattleHex::CASTLE_UPPER_TOWER;
			break;
		}

		if(state == EWallState::DESTROYED) //HP enum subtraction not intuitive, consider using SiegeInfo::applyDamage
		{
			auto all = m->battle()->battleGetUnitsIf([=](const battle::Unit * unit)
			{
				return !unit->isGhost() && unit->getPosition() == posRemove;
			});

			assert(all.size() == 0 || all.size() == 1);
			for(auto & elem : all)
				removeUnits.changedStacks.emplace_back(elem->unitId(), UnitChanges::EOperation::REMOVE);
		}
	}

	if(!removeUnits.changedStacks.empty())
		server->apply(removeUnits);
}

std::vector<EWallPart> Catapult::getPotentialTargets(const Mechanics * m, bool bypassGateCheck, bool bypassTowerCheck) const
{
	std::vector<EWallPart> potentialTargets;
	constexpr std::array<EWallPart, 4> walls = { EWallPart::BOTTOM_WALL, EWallPart::BELOW_GATE, EWallPart::OVER_GATE, EWallPart::UPPER_WALL };
	constexpr std::array<EWallPart, 3> towers= { EWallPart::BOTTOM_TOWER, EWallPart::KEEP, EWallPart::UPPER_TOWER };
	constexpr EWallPart gates = EWallPart::GATE;

	// in H3, catapult under automatic control will attack objects in following order:
	// walls, gates, towers
	for (auto & part : walls)
		if (m->battle()->isWallPartAttackable(part))
			potentialTargets.push_back(part);

	if ((potentialTargets.empty() || bypassGateCheck) && (m->battle()->isWallPartAttackable(gates)))
		potentialTargets.push_back(gates);

	if (potentialTargets.empty() || bypassTowerCheck)
		for (auto & part : towers)
			if (m->battle()->isWallPartAttackable(part))
				potentialTargets.push_back(part);

	return potentialTargets;
}

void Catapult::adjustHitChance()
{
	vstd::abetween(keep, 0, 100);
	vstd::abetween(tower, 0, 100);
	vstd::abetween(gate, 0, 100);
	vstd::abetween(wall, 0, 100);
	vstd::abetween(crit, 0, 100);
	vstd::abetween(hit, 0, 100 - crit);
	vstd::amin(noDmg, 100 - hit - crit);
}

void Catapult::serializeJsonEffect(JsonSerializeFormat & handler)
{
	handler.serializeInt("targetsToAttack", targetsToAttack);
	handler.serializeInt("chanceToHitKeep", keep);
	handler.serializeInt("chanceToHitGate", gate);
	handler.serializeInt("chanceToHitTower", tower);
	handler.serializeInt("chanceToHitWall", wall);
	handler.serializeInt("chanceToNormalHit", hit);
	handler.serializeInt("chanceToCrit", crit);
	adjustHitChance();
}


}
}

VCMI_LIB_NAMESPACE_END