File: AttackPossibility.cpp

package info (click to toggle)
vcmi 0.99%2Bdfsg%2Bgit20190113.f06c8a87-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 11,136 kB
  • sloc: cpp: 142,615; sh: 315; objc: 248; makefile: 32; ansic: 28; python: 13
file content (97 lines) | stat: -rw-r--r-- 2,918 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
/*
 * AttackPossibility.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 "AttackPossibility.h"

AttackPossibility::AttackPossibility(BattleHex tile_, const BattleAttackInfo & attack_)
	: tile(tile_),
	attack(attack_)
{
}


int64_t AttackPossibility::damageDiff() const
{
	//TODO: use target priority from HypotheticBattle
	const auto dealtDmgValue = damageDealt;
	const auto receivedDmgValue = damageReceived;

	int64_t diff = 0;

	//friendly fire or not
	if(attack.attacker->unitSide() == attack.defender->unitSide())
		diff = -dealtDmgValue - receivedDmgValue;
	else
		diff = dealtDmgValue - receivedDmgValue;

	//mind control
	auto actualSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attack.attacker));
	if(actualSide && actualSide.get() != attack.attacker->unitSide())
		diff = -diff;
	return diff;
}

int64_t AttackPossibility::attackValue() const
{
	return damageDiff() + tacticImpact;
}

AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex)
{
	const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
	static const auto selectorBlocksRetaliation = Selector::type(Bonus::BLOCKS_RETALIATION);

	const bool counterAttacksBlocked = attackInfo.attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);

	AttackPossibility ap(hex, attackInfo);

	ap.attackerState = attackInfo.attacker->acquireState();

	const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);

	if(!attackInfo.shooting)
		ap.attackerState->setPosition(hex);

	auto defenderState = attackInfo.defender->acquireState();
	ap.affectedUnits.push_back(defenderState);

	for(int i = 0; i < totalAttacks; i++)
	{
		TDmgRange retaliation(0,0);
		auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);

		vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
		vstd::amin(attackDmg.second, defenderState->getAvailableHealth());

		vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
		vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());

		ap.damageDealt += (attackDmg.first + attackDmg.second) / 2;

		ap.attackerState->afterAttack(attackInfo.shooting, false);

		//FIXME: use ranged retaliation
		if(!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
		{
			ap.damageReceived += (retaliation.first + retaliation.second) / 2;
			defenderState->afterAttack(attackInfo.shooting, true);
		}

		ap.attackerState->damage(ap.damageReceived);
		defenderState->damage(ap.damageDealt);

		if(!ap.attackerState->alive() || !defenderState->alive())
			break;
	}

	//TODO other damage related to attack (eg. fire shield and other abilities)

	return ap;
}