File: CHeroHandler.h

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 (335 lines) | stat: -rw-r--r-- 9,388 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
/*
 * CHeroHandler.h, 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
 *
 */
#pragma once

#include "../lib/ConstTransitivePtr.h"
#include "GameConstants.h"
#include "HeroBonus.h"
#include "IHandlerBase.h"

class CHeroClass;
class CGameInfo;
class CGHeroInstance;
struct BattleHex;
class JsonNode;
class CRandomGenerator;

struct SSpecialtyInfo
{	si32 type;
	si32 val;
	si32 subtype;
	si32 additionalinfo;
	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & type;
		h & val;
		h & subtype;
		h & additionalinfo;
	}
};

struct SSpecialtyBonus
/// temporary hold
{
	ui8 growsWithLevel;
	BonusList bonuses;
	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & growsWithLevel;
		h & bonuses;
	}
};

class DLL_LINKAGE CHero
{
public:
	struct InitialArmyStack
	{
		ui32 minAmount;
		ui32 maxAmount;
		CreatureID creature;

		template <typename Handler> void serialize(Handler &h, const int version)
		{
			h & minAmount;
			h & maxAmount;
			h & creature;
		}
	};
	std::string identifier;
	HeroTypeID ID;
	si32 imageIndex;

	std::vector<InitialArmyStack> initialArmy;

	CHeroClass * heroClass;
	std::vector<std::pair<SecondarySkill, ui8> > secSkillsInit; //initial secondary skills; first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert)
	std::vector<SSpecialtyInfo> specDeprecated;
	std::vector<SSpecialtyBonus> specialtyDeprecated;
	BonusList specialty;
	std::set<SpellID> spells;
	bool haveSpellBook;
	bool special; // hero is special and won't be placed in game (unless preset on map), e.g. campaign heroes
	ui8 sex; // default sex: 0=male, 1=female

	/// Localized texts
	std::string name; //name of hero
	std::string biography;
	std::string specName;
	std::string specDescr;
	std::string specTooltip;

	/// Graphics
	std::string iconSpecSmall;
	std::string iconSpecLarge;
	std::string portraitSmall;
	std::string portraitLarge;
	std::string battleImage;

	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & ID;
		h & imageIndex;
		h & initialArmy;
		h & heroClass;
		h & secSkillsInit;
		if(version >= 781)
		{
			h & specialty;
		}
		else
		{
			h & specDeprecated;
			h & specialtyDeprecated;
		}
		h & spells;
		h & haveSpellBook;
		h & sex;
		h & special;
		h & name;
		h & biography;
		h & specName;
		h & specDescr;
		h & specTooltip;
		h & iconSpecSmall;
		h & iconSpecLarge;
		h & portraitSmall;
		h & portraitLarge;
		if(version >= 759)
		{
			h & identifier;
		}
		if(version >= 790)
		{
			h & battleImage;
		}
	}
};

// convert deprecated format
std::vector<std::shared_ptr<Bonus>> SpecialtyInfoToBonuses(const SSpecialtyInfo & spec, int sid = 0);
std::vector<std::shared_ptr<Bonus>> SpecialtyBonusToBonuses(const SSpecialtyBonus & spec, int sid = 0);

class DLL_LINKAGE CHeroClass
{
public:
	enum EClassAffinity
	{
		MIGHT,
		MAGIC
	};

	std::string identifier;
	std::string name; // translatable
	//double aggression; // not used in vcmi.
	TFaction faction;
	ui8 id;
	ui8 affinity; // affility, using EClassAffinity enum

	// default chance for hero of specific class to appear in tavern, if field "tavern" was not set
	// resulting chance = sqrt(town.chance * heroClass.chance)
	ui32 defaultTavernChance;

	CCreature * commander;

	std::vector<int> primarySkillInitial;  // initial primary skills
	std::vector<int> primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level
	std::vector<int> primarySkillHighLevel;// same for high levels (> 10)

	std::vector<int> secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order

	std::map<TFaction, int> selectionProbability; //probability of selection in towns

	std::string imageBattleMale;
	std::string imageBattleFemale;
	std::string imageMapMale;
	std::string imageMapFemale;

	CHeroClass();

	bool isMagicHero() const;
	SecondarySkill chooseSecSkill(const std::set<SecondarySkill> & possibles, CRandomGenerator & rand) const; //picks secondary skill out from given possibilities

	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & identifier;
		h & name;
		h & faction;
		h & id;
		h & defaultTavernChance;
		h & primarySkillInitial;
		h & primarySkillLowLevel;
		h & primarySkillHighLevel;
		h & secSkillProbability;
		h & selectionProbability;
		h & affinity;
		h & commander;
		h & imageBattleMale;
		h & imageBattleFemale;
		h & imageMapMale;
		h & imageMapFemale;
	}
	EAlignment::EAlignment getAlignment() const;
};

struct DLL_LINKAGE CObstacleInfo
{
	si32 ID;
	std::string defName;
	std::vector<ETerrainType> allowedTerrains;
	std::vector<BFieldType> allowedSpecialBfields;

	ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same
	si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm)
	std::vector<si16> blockedTiles; //offsets relative to obstacle position (that is its left bottom corner)

	std::vector<BattleHex> getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex'

	bool isAppropriate(ETerrainType terrainType, int specialBattlefield = -1) const;

	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & ID;
		h & defName;
		h & allowedTerrains;
		h & allowedSpecialBfields;
		h & isAbsoluteObstacle;
		h & width;
		h & height;
		h & blockedTiles;
	}
};

class DLL_LINKAGE CHeroClassHandler : public IHandlerBase
{
	CHeroClass *loadFromJson(const JsonNode & node, const std::string & identifier);
public:
	std::vector< ConstTransitivePtr<CHeroClass> > heroClasses;

	std::vector<JsonNode> loadLegacyData(size_t dataSize) override;

	void loadObject(std::string scope, std::string name, const JsonNode & data) override;
	void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;

	void afterLoadFinalization() override;

	std::vector<bool> getDefaultAllowed() const override;

	~CHeroClassHandler();

	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & heroClasses;
	}
};

class DLL_LINKAGE CHeroHandler : public IHandlerBase
{
	/// expPerLEvel[i] is amount of exp needed to reach level i;
	/// consists of 201 values. Any higher levels require experience larger that ui64 can hold
	std::vector<ui64> expPerLevel;

	/// helpers for loading to avoid huge load functions
	void loadHeroArmy(CHero * hero, const JsonNode & node);
	void loadHeroSkills(CHero * hero, const JsonNode & node);
	void loadHeroSpecialty(CHero * hero, const JsonNode & node);

	void loadExperience();
	void loadBallistics();
	void loadTerrains();
	void loadObstacles();

	/// Load single hero from json
	CHero * loadFromJson(const JsonNode & node, const std::string & identifier);

public:
	CHeroClassHandler classes;

	std::vector< ConstTransitivePtr<CHero> > heroes;

	//default costs of going through terrains. -1 means terrain is impassable
	std::vector<int> terrCosts;

	struct SBallisticsLevelInfo
	{
		ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%)
		ui8 shots; //how many shots we have
		ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100
		ui8 sum; //I don't know if it is useful for anything, but it's in config file
		template <typename Handler> void serialize(Handler &h, const int version)
		{
			h & keep;
			h & tower;
			h & gate;
			h & wall;
			h & shots;
			h & noDmg;
			h & oneDmg;
			h & twoDmg;
			h & sum;
		}
	};
	std::vector<SBallisticsLevelInfo> ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert

	std::map<int, CObstacleInfo> obstacles; //info about obstacles that may be placed on battlefield
	std::map<int, CObstacleInfo> absoluteObstacles; //info about obstacles that may be placed on battlefield

	ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount
	ui64 reqExp(ui32 level) const; //calculates experience required for given level

	std::vector<JsonNode> loadLegacyData(size_t dataSize) override;

	void beforeValidate(JsonNode & object) override;
	void loadObject(std::string scope, std::string name, const JsonNode & data) override;
	void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override;
	void afterLoadFinalization() override;

	CHeroHandler();
	~CHeroHandler();

	std::vector<bool> getDefaultAllowed() const override;

	///json serialization helper
	static si32 decodeHero(const std::string & identifier);

	///json serialization helper
	static std::string encodeHero(const si32 index);

	template <typename Handler> void serialize(Handler &h, const int version)
	{
		h & classes;
		h & heroes;
		h & expPerLevel;
		h & ballistics;
		h & terrCosts;
		h & obstacles;
		h & absoluteObstacles;
	}
};