File: CBonusTypeHandler.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 (328 lines) | stat: -rw-r--r-- 6,905 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
/*
 * CBonusTypeHandler.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"

#define INSTANTIATE_CBonusTypeHandler_HERE

#include "CBonusTypeHandler.h"

#include "JsonNode.h"
#include "filesystem/Filesystem.h"

#include "GameConstants.h"
#include "CCreatureHandler.h"
#include "spells/CSpellHandler.h"

template class std::vector<CBonusType>;

///MacroString

MacroString::MacroString(const std::string & format)
{
	static const std::string MACRO_START = "${";
	static const std::string MACRO_END = "}";
	static const size_t MACRO_START_L = 2;
	static const size_t MACRO_END_L = 1;

	size_t end_pos = 0;
	size_t start_pos = std::string::npos;
	do
	{
		start_pos = format.find(MACRO_START, end_pos);

		if(!(start_pos == std::string::npos))
		{
			//chunk before macro
			items.push_back(Item(Item::STRING, format.substr(end_pos, start_pos - end_pos)));

			start_pos += MACRO_START_L;
			end_pos = format.find(MACRO_END, start_pos);

			if(end_pos == std::string::npos)
			{
				logBonus->warn("Format error in: %s", format);
				end_pos = start_pos;
				break;
			}
			else
			{
				items.push_back(Item(Item::MACRO, format.substr(start_pos, end_pos - start_pos)));
				end_pos += MACRO_END_L;
			}
		}
	}
	while(start_pos != std::string::npos);

	//no more macros
	items.push_back(Item(Item::STRING, format.substr(end_pos)));
}

std::string MacroString::build(const GetValue & getValue) const
{
	std::string result;

	for(const Item & i : items)
	{
		switch(i.type)
		{
		case Item::MACRO:
		{
			result += getValue(i.value);
			break;
		}
		case Item::STRING:
		{
			result += i.value;
			break;
		}
		}
	}
	return result;
}

///CBonusType

CBonusType::CBonusType()
{
	hidden = true;
	icon = nameTemplate = descriptionTemplate = "";
}

CBonusType::~CBonusType()
{

}

void CBonusType::buildMacros()
{
	name = MacroString(nameTemplate);
	description = MacroString(descriptionTemplate);
}


///CBonusTypeHandler

CBonusTypeHandler::CBonusTypeHandler()
{
	//register predefined bonus types

	#define BONUS_NAME(x) \
	do { \
		bonusTypes.push_back(CBonusType()); \
	} while(0);


	BONUS_LIST;
	#undef BONUS_NAME

	load();
}

CBonusTypeHandler::~CBonusTypeHandler()
{
	//dtor
}

std::string CBonusTypeHandler::bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer, bool description) const
{
	auto getValue = [=](const std::string & name) -> std::string
	{
		if(name == "val")
		{
			 return boost::lexical_cast<std::string>(bearer->valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype)));
		}
		else if(name == "subtype.creature")
		{
			 const CreatureID cre(bonus->subtype);
			 return cre.toCreature()->namePl;
		}
		else if(name == "subtype.spell")
		{
			 const SpellID sp(bonus->subtype);
			 return sp.toSpell()->name;
		}
		else if(name == "MR")
		{
			 return boost::lexical_cast<std::string>(bearer->magicResistance());
		}
		else
		{
			 logBonus->warn("Unknown macro in bonus config: %s", name);
			 return "[error]";
		}
	};

	const CBonusType & bt = bonusTypes[bonus->type];
	if(bt.hidden)
		return "";
	const MacroString & macro = description ? bt.description : bt.name;

	return macro.build(getValue);
}

std::string CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
{
	std::string fileName;
	bool fullPath = false;

	switch(bonus->type)
	{
	case Bonus::SECONDARY_SKILL_PREMY:
		if(bonus->subtype == SecondarySkill::RESISTANCE)
		{
			fileName = "E_DWARF.bmp";
		}
		break;
	case Bonus::SPELL_IMMUNITY:
	{
		fullPath = true;
		const CSpell * sp = SpellID(bonus->subtype).toSpell();
		fileName = sp->getIconImmune();
		break;
	}
	case Bonus::FIRE_IMMUNITY:
		switch(bonus->subtype)
		{
		case 0:
			fileName = "E_SPFIRE.bmp";
			break;//all
		case 1:
			fileName = "E_SPFIRE1.bmp";
			break;//not positive
		case 2:
			fileName = "E_FIRE.bmp";
			break;//direct damage
		}
		break;
	case Bonus::WATER_IMMUNITY:
		switch(bonus->subtype)
		{
		case 0:
			fileName = "E_SPWATER.bmp";
			break;//all
		case 1:
			fileName = "E_SPWATER1.bmp";
			break;//not positive
		case 2:
			fileName = "E_SPCOLD.bmp";
			break;//direct damage
		}
		break;
	case Bonus::AIR_IMMUNITY:
		switch(bonus->subtype)
		{
		case 0:
			fileName = "E_SPAIR.bmp";
			break;//all
		case 1:
			fileName = "E_SPAIR1.bmp";
			break;//not positive
		case 2:
			fileName = "E_LIGHT.bmp";
			break;//direct damage
		}
		break;
	case Bonus::EARTH_IMMUNITY:
		switch(bonus->subtype)
		{
		case 0:
			fileName = "E_SPEATH.bmp";
			break;//all
		case 1:
		case 2://no specific icon for direct damage immunity
			fileName = "E_SPEATH1.bmp";
			break;//not positive
		}
		break;
	case Bonus::LEVEL_SPELL_IMMUNITY:
	{
		if(vstd::iswithin(bonus->val, 1, 5))
		{
			fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
		}
		break;
	}
	case Bonus::GENERAL_DAMAGE_REDUCTION:
	{
		switch(bonus->subtype)
		{
		case 0:
			fileName = "DamageReductionMelee.bmp";
			break;
		case 1:
			fileName = "DamageReductionRanged.bmp";
			break;
		}
		break;
	}

	default:
		{
			const CBonusType & bt = bonusTypes[bonus->type];
			fileName = bt.icon;
			fullPath = true;
		}
		break;
	}

	if(!fileName.empty() && !fullPath)
		fileName = "zvs/Lib1.res/" + fileName;
	return fileName;
}

void CBonusTypeHandler::load()
{
	const JsonNode gameConf(ResourceID("config/gameConfig.json"));
	const JsonNode config(JsonUtils::assembleFromFiles(gameConf["bonuses"].convertTo<std::vector<std::string>>()));
	load(config);
}

void CBonusTypeHandler::load(const JsonNode & config)
{
	for(auto & node : config.Struct())
	{
		auto it = bonusNameMap.find(node.first);

		if(it == bonusNameMap.end())
		{
			//TODO: new bonus
//			CBonusType bt;
//			loadItem(node.second, bt);
//
//			auto new_id = bonusTypes.size();
//
//			bonusTypes.push_back(bt);

			logBonus->warn("Adding new bonuses not implemented (%s)", node.first);
		}
		else
		{
			CBonusType & bt = bonusTypes[it->second];

			loadItem(node.second, bt);
			logBonus->trace("Loaded bonus type %s", node.first);
		}
	}
}

void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest)
{
	dest.nameTemplate = source["name"].String();
	dest.descriptionTemplate = source["description"].String();
	dest.hidden = source["hidden"].Bool(); //Null -> false

	const JsonNode & graphics = source["graphics"];

	if(!graphics.isNull())
	{
		dest.icon = graphics["icon"].String();
	}
	dest.buildMacros();
}