File: Summon.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 (210 lines) | stat: -rw-r--r-- 5,502 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
/*
 * Summon.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 "Summon.h"
#include "Registry.h"

#include "../ISpellMechanics.h"
#include "../../battle/CBattleInfoCallback.h"
#include "../../battle/BattleInfo.h"
#include "../../battle/Unit.h"
#include "../../serializer/JsonSerializeFormat.h"
#include "../../CCreatureHandler.h"
#include "../../mapObjects/CGHeroInstance.h"
#include "../../networkPacks/PacksForClientBattle.h"

VCMI_LIB_NAMESPACE_BEGIN

namespace spells
{
namespace effects
{

void Summon::adjustAffectedHexes(BattleHexArray & hexes, const Mechanics * m, const Target & spellTarget) const
{
	//no hexes affected
}

void Summon::adjustTargetTypes(std::vector<TargetType> & types) const
{
	//any target type allowed
}

bool Summon::applicable(Problem & problem, const Mechanics * m) const
{
	if (creature == CreatureID::NONE)
	{
		logMod->error("Attempt to summon non-existing creature!");
		return m->adaptGenericProblem(problem);
	}

	if (summonedCreatureAmount(m) == 0)
	{
		logMod->debug("Attempt to summon zero creatures!");
		return m->adaptGenericProblem(problem);
	}

	if(exclusive)
	{
		//check if there are summoned creatures of other type

		auto otherSummoned = m->battle()->battleGetUnitsIf([m, this](const battle::Unit * unit)
		{
			return (unit->unitOwner() == m->getCasterColor())
				&& (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
				&& (!unit->isClone())
				&& (unit->creatureId() != creature);
		});

		if(!otherSummoned.empty())
		{
			const auto *elemental = otherSummoned.front();

			MetaString text;
			text.appendLocalString(EMetaText::GENERAL_TXT, 538);

			const auto *caster = dynamic_cast<const CGHeroInstance *>(m->caster);
			if(caster)
			{
				text.replaceRawString(caster->getNameTranslated());

				text.replaceNamePlural(elemental->creatureId());

				if(caster->gender == EHeroGender::FEMALE)
					text.replaceLocalString(EMetaText::GENERAL_TXT, 540);
				else
					text.replaceLocalString(EMetaText::GENERAL_TXT, 539);

			}
			problem.add(std::move(text), Problem::NORMAL);
			return false;
		}
	}

	return true;
}

int32_t Summon::summonedCreatureHealth(const Mechanics * m, const battle::Unit * unit) const
{
	auto valueWithBonus = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));

	if(summonByHealth)
		return valueWithBonus;
	else
		return valueWithBonus * unit->getMaxHealth();
}

int32_t Summon::summonedCreatureAmount(const Mechanics * m) const
{
	auto valueWithBonus = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));

	if(summonByHealth)
	{
		const auto *creatureType = creature.toEntity(m->creatures());
		auto creatureMaxHealth = creatureType->getMaxHealth();
		return valueWithBonus / creatureMaxHealth;
	}
	else
	{
		return valueWithBonus;
	}
}

void Summon::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
{
	BattleUnitsChanged pack;
	pack.battleID = m->battle()->getBattle()->getBattleID();

	for(const auto & dest : target)
	{
		if(dest.unitValue)
		{
			const battle::Unit * summoned = dest.unitValue;
			std::shared_ptr<battle::Unit> state = summoned->acquire();
			int64_t healthValue = summonedCreatureHealth(m, summoned);
			state->heal(healthValue, EHealLevel::OVERHEAL, (permanent ? EHealPower::PERMANENT : EHealPower::ONE_BATTLE));
			pack.changedStacks.emplace_back(summoned->unitId(), UnitChanges::EOperation::RESET_STATE);
			state->save(pack.changedStacks.back().data);
		}
		else
		{
			int32_t amount = summonedCreatureAmount(m);

			if(amount < 1)
			{
				server->complain("Summoning didn't summon any!");
				continue;
			}

			battle::UnitInfo info;
			info.id = m->battle()->battleNextUnitId();
			info.count = amount;
			info.type = creature;
			info.side = m->casterSide;
			info.position = dest.hexValue;
			info.summoned = !permanent;

			pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
			info.save(pack.changedStacks.back().data);
		}
	}

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

EffectTarget Summon::filterTarget(const Mechanics * m, const EffectTarget & target) const
{
	return target;
}

void Summon::serializeJsonEffect(JsonSerializeFormat & handler)
{
	handler.serializeId("id", creature, CreatureID());
	handler.serializeBool("permanent", permanent, false);
	handler.serializeBool("exclusive", exclusive, true);
	handler.serializeBool("summonByHealth", summonByHealth, false);
	handler.serializeBool("summonSameUnit", summonSameUnit, false);
}

EffectTarget Summon::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
{
	auto sameSummoned = m->battle()->battleGetUnitsIf([m, this](const battle::Unit * unit)
	{
		return (unit->unitOwner() == m->getCasterColor())
			&& (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
			&& (!unit->isClone())
			&& (unit->creatureId() == creature)
			&& (unit->alive());
	});

	EffectTarget effectTarget;

	if(sameSummoned.empty() || !summonSameUnit)
	{
		BattleHex hex = m->battle()->getAvailableHex(creature, m->casterSide);
		if(!hex.isValid())
			logGlobal->error("No free space to summon creature!");
		else
			effectTarget.emplace_back(hex);
	}
	else
	{
		effectTarget.emplace_back(sameSummoned.front());
	}

	return effectTarget;
}

}
}

VCMI_LIB_NAMESPACE_END