File: CBank.cpp

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (357 lines) | stat: -rw-r--r-- 8,290 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * CBank.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 "CBank.h"

#include <vcmi/spells/Spell.h>
#include <vcmi/spells/Service.h>

#include "../NetPacks.h"
#include "../CGeneralTextHandler.h"
#include "../CSoundBase.h"
#include "CommonConstructors.h"
#include "../IGameCallback.h"
#include "../CGameState.h"

VCMI_LIB_NAMESPACE_BEGIN

///helpers
static std::string & visitedTxt(const bool visited)
{
	int id = visited ? 352 : 353;
	return VLC->generaltexth->allTexts[id];
}

CBank::CBank()
{
	daycounter = 0;
	resetDuration = 0;
}

CBank::~CBank()
{
}

void CBank::initObj(CRandomGenerator & rand)
{
	daycounter = 0;
	resetDuration = 0;
	VLC->objtypeh->getHandlerFor(ID, subID)->configureObject(this, rand);
}

std::string CBank::getHoverText(PlayerColor player) const
{
	// TODO: record visited players
	return getObjectName() + " " + visitedTxt(bc == nullptr);
}

void CBank::setConfig(const BankConfig & config)
{
	bc.reset(new BankConfig(config));
	clear(); // remove all stacks, if any

	for (auto & stack : config.guards)
		setCreature (SlotID(stacksCount()), stack.type->idNumber, stack.count);
}

void CBank::setPropertyDer (ui8 what, ui32 val)
{
	switch (what)
	{
		case ObjProperty::BANK_DAYCOUNTER: //daycounter
				daycounter+=val;
			break;
		case ObjProperty::BANK_RESET:
			// FIXME: Object reset must be done by separate netpack from server
			initObj(cb->gameState()->getRandomGenerator());
			daycounter = 1; //yes, 1 since "today" daycounter won't be incremented
			break;
		case ObjProperty::BANK_CLEAR:
			bc.reset();
			break;
	}
}

void CBank::newTurn(CRandomGenerator & rand) const
{
	if (bc == nullptr)
	{
		if (resetDuration != 0)
		{
			if (daycounter >= resetDuration)
				cb->setObjProperty (id, ObjProperty::BANK_RESET, 0); //daycounter 0
			else
				cb->setObjProperty (id, ObjProperty::BANK_DAYCOUNTER, 1); //daycounter++
		}
	}
}

bool CBank::wasVisited (PlayerColor player) const
{
	return !bc; //FIXME: player A should not know about visit done by player B
}

void CBank::onHeroVisit(const CGHeroInstance * h) const
{
	int banktext = 0;
	switch (ID)
	{
	case Obj::DERELICT_SHIP:
		banktext = 41;
		break;
	case Obj::DRAGON_UTOPIA:
		banktext = 47;
		break;
	case Obj::CRYPT:
		banktext = 119;
		break;
	case Obj::SHIPWRECK:
		banktext = 122;
		break;
	case Obj::PYRAMID:
		banktext = 105;
		break;
	case Obj::CREATURE_BANK:
	default:
		banktext = 32;
		break;
	}
	BlockingDialog bd(true, false);
	bd.player = h->getOwner();
	bd.soundID = soundBase::invalid; // Sound is handled in json files, else two sounds are played
	bd.text.addTxt(MetaString::ADVOB_TXT, banktext);
	if (banktext == 32)
		bd.text.addReplacement(getObjectName());
	cb->showBlockingDialog(&bd);
}

void CBank::doVisit(const CGHeroInstance * hero) const
{
	int textID = -1;
	InfoWindow iw;
	iw.player = hero->getOwner();
	MetaString loot;

	if (bc)
	{
		switch (ID)
		{
		case Obj::DERELICT_SHIP:
			textID = 43;
			break;
		case Obj::CRYPT:
			textID = 121;
			break;
		case Obj::SHIPWRECK:
			textID = 124;
			break;
		case Obj::PYRAMID:
			textID = 106;
			break;
		case Obj::CREATURE_BANK:
		case Obj::DRAGON_UTOPIA:
		default:
			textID = 34;
			break;
		}
	}
	else
	{
		switch (ID)
		{
		case Obj::SHIPWRECK:
		case Obj::DERELICT_SHIP:
		case Obj::CRYPT:
		{
			GiveBonus gbonus;
			gbonus.id = hero->id.getNum();
			gbonus.bonus.duration = Bonus::ONE_BATTLE;
			gbonus.bonus.source = Bonus::OBJECT;
			gbonus.bonus.sid = ID;
			gbonus.bonus.type = Bonus::MORALE;
			gbonus.bonus.val = -1;
			switch (ID)
			{
			case Obj::SHIPWRECK:
				textID = 123;
				gbonus.bdescr << VLC->generaltexth->arraytxt[99];
				break;
			case Obj::DERELICT_SHIP:
				textID = 42;
				gbonus.bdescr << VLC->generaltexth->arraytxt[101];
				break;
			case Obj::CRYPT:
				textID = 120;
				gbonus.bdescr << VLC->generaltexth->arraytxt[98];
				break;
			}
			cb->giveHeroBonus(&gbonus);
			iw.components.push_back(Component(Component::MORALE, 0, -1, 0));
			iw.soundID = soundBase::GRAVEYARD;
			break;
		}
		case Obj::PYRAMID:
		{
			GiveBonus gb;
			gb.bonus = Bonus(Bonus::ONE_BATTLE, Bonus::LUCK, Bonus::OBJECT, -2, id.getNum(), VLC->generaltexth->arraytxt[70]);
			gb.id = hero->id.getNum();
			cb->giveHeroBonus(&gb);
			textID = 107;
			iw.components.push_back(Component(Component::LUCK, 0, -2, 0));
			break;
		}
		case Obj::CREATURE_BANK:
		case Obj::DRAGON_UTOPIA:
		default:
			iw.text << VLC->generaltexth->advobtxt[33];// This was X, now is completely empty
			iw.text.addReplacement(getObjectName());
		}
		if(textID != -1)
		{
			iw.text.addTxt(MetaString::ADVOB_TXT, textID);
		}
		cb->showInfoDialog(&iw);
	}


	//grant resources
	if (bc)
	{
		for (int it = 0; it < bc->resources.size(); it++)
		{
			if (bc->resources[it] != 0)
			{
				iw.components.push_back(Component(Component::RESOURCE, it, bc->resources[it], 0));
				loot << "%d %s";
				loot.addReplacement(iw.components.back().val);
				loot.addReplacement(MetaString::RES_NAMES, iw.components.back().subtype);
				cb->giveResource(hero->getOwner(), static_cast<Res::ERes>(it), bc->resources[it]);
			}
		}
		//grant artifacts
		for (auto & elem : bc->artifacts)
		{
			iw.components.push_back(Component(Component::ARTIFACT, elem, 0, 0));
			loot << "%s";
			loot.addReplacement(MetaString::ART_NAMES, elem);
			cb->giveHeroNewArtifact(hero, VLC->arth->objects[elem], ArtifactPosition::FIRST_AVAILABLE);
		}
		//display loot
		if (!iw.components.empty())
		{
			iw.text.addTxt(MetaString::ADVOB_TXT, textID);
			if (textID == 34)
			{
				const CCreature * strongest = boost::range::max_element(bc->guards, [](const CStackBasicDescriptor & a, const CStackBasicDescriptor & b)
				{
					return a.type->fightValue < b.type->fightValue;
				})->type;

				iw.text.addReplacement(MetaString::CRE_PL_NAMES, strongest->idNumber);
				iw.text.addReplacement(loot.buildList());
			}
			cb->showInfoDialog(&iw);
		}

		loot.clear();
		iw.components.clear();
		iw.text.clear();

		if (!bc->spells.empty())
		{
			std::set<SpellID> spells;

			bool noWisdom = false;
			if(textID == 106)
			{
				iw.text.addTxt(MetaString::ADVOB_TXT, textID); //pyramid
			}
			for(const SpellID & spellId : bc->spells)
			{
				auto spell = spellId.toSpell(VLC->spells());
				iw.text.addTxt(MetaString::SPELL_NAME, spellId);
				if(spell->getLevel() <= hero->maxSpellLevel())
				{
					if(hero->canLearnSpell(spell))
					{
						spells.insert(spellId);
						iw.components.push_back(Component(Component::SPELL, spellId, 0, 0));
					}
				}
				else
					noWisdom = true;
			}

			if (!hero->getArt(ArtifactPosition::SPELLBOOK))
				iw.text.addTxt(MetaString::ADVOB_TXT, 109); //no spellbook
			else if(noWisdom)
				iw.text.addTxt(MetaString::ADVOB_TXT, 108); //no expert Wisdom

			if(!iw.components.empty() || !iw.text.toString().empty())
				cb->showInfoDialog(&iw);

			if(!spells.empty())
				cb->changeSpells(hero, true, spells);
		}

		iw.components.clear();
		iw.text.clear();

		//grant creatures
		CCreatureSet ourArmy;
		for (auto slot : bc->creatures)
		{
			ourArmy.addToSlot(ourArmy.getSlotFor(slot.type->idNumber), slot.type->idNumber, slot.count);
		}

		for (auto & elem : ourArmy.Slots())
		{
			iw.components.push_back(Component(*elem.second));
			loot << "%s";
			loot.addReplacement(*elem.second);
		}

		if(ourArmy.stacksCount())
		{
			if(ourArmy.stacksCount() == 1 && ourArmy.Slots().begin()->second->count == 1)
				iw.text.addTxt(MetaString::ADVOB_TXT, 185);
			else
				iw.text.addTxt(MetaString::ADVOB_TXT, 186);

			iw.text.addReplacement(loot.buildList());
			iw.text.addReplacement(hero->name);
			cb->showInfoDialog(&iw);
			cb->giveCreatures(this, hero, ourArmy, false);
		}
		cb->setObjProperty(id, ObjProperty::BANK_CLEAR, 0); //bc = nullptr
	}
}

void CBank::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
{
	if (result.winner == 0)
	{
		doVisit(hero);
	}
}

void CBank::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
{
	if (answer)
	{
		if (bc) // not looted bank
			cb->startBattleI(hero, this, true);
		else
			doVisit(hero);
	}
}

VCMI_LIB_NAMESPACE_END