File: EntityIdentifiers.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 (673 lines) | stat: -rw-r--r-- 15,346 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
 * EntityIdentifiers.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"

#ifndef VCMI_NO_EXTRA_VERSION
#include "../Version.h"
#endif
#include <vcmi/Artifact.h>
#include <vcmi/ArtifactService.h>
#include <vcmi/Faction.h>
#include <vcmi/FactionService.h>
#include <vcmi/HeroType.h>
#include <vcmi/HeroTypeService.h>
#include <vcmi/HeroClass.h>
#include <vcmi/HeroClassService.h>
#include <vcmi/Services.h>

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

#include "modding/IdentifierStorage.h"
#include "modding/ModScope.h"
#include "VCMI_Lib.h"
#include "CArtHandler.h"//todo: remove
#include "CCreatureHandler.h"//todo: remove
#include "spells/CSpellHandler.h" //todo: remove
#include "CSkillHandler.h"//todo: remove
#include "entities/faction/CFaction.h"
#include "entities/hero/CHero.h"
#include "entities/hero/CHeroClass.h"
#include "mapObjectConstructors/AObjectTypeHandler.h"
#include "constants/StringConstants.h"
#include "texts/CGeneralTextHandler.h"
#include "TerrainHandler.h" //TODO: remove
#include "RiverHandler.h"
#include "RoadHandler.h"
#include "BattleFieldHandler.h"
#include "ObstacleHandler.h"
#include "mapObjectConstructors/CObjectClassesHandler.h"

VCMI_LIB_NAMESPACE_BEGIN

const CampaignScenarioID CampaignScenarioID::NONE(-1);
const BattleID BattleID::NONE(-1);
const QueryID QueryID::NONE(-1);
const QueryID QueryID::CLIENT(-2);
const HeroTypeID HeroTypeID::NONE(-1);
const HeroTypeID HeroTypeID::RANDOM(-2);
const HeroTypeID HeroTypeID::GEM(27);
const HeroTypeID HeroTypeID::SOLMYR(45);

const ObjectInstanceID ObjectInstanceID::NONE(-1);

const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER(-2);
const SlotID SlotID::SUMMONED_SLOT_PLACEHOLDER(-3);
const SlotID SlotID::WAR_MACHINES_SLOT(-4);
const SlotID SlotID::ARROW_TOWERS_SLOT(-5);

const PlayerColor PlayerColor::SPECTATOR(-4);
const PlayerColor PlayerColor::CANNOT_DETERMINE(-3);
const PlayerColor PlayerColor::UNFLAGGABLE(-2);
const PlayerColor PlayerColor::NEUTRAL(-1);
const PlayerColor PlayerColor::PLAYER_LIMIT(PLAYER_LIMIT_I);
const TeamID TeamID::NO_TEAM(-1);

const SpellSchool SpellSchool::ANY(-1);
const SpellSchool SpellSchool::AIR(0);
const SpellSchool SpellSchool::FIRE(1);
const SpellSchool SpellSchool::WATER(2);
const SpellSchool SpellSchool::EARTH(3);

const FactionID FactionID::NONE(-2);
const FactionID FactionID::DEFAULT(-1);
const FactionID FactionID::RANDOM(-1);
const FactionID FactionID::ANY(-1);
const FactionID FactionID::CASTLE(0);
const FactionID FactionID::RAMPART(1);
const FactionID FactionID::TOWER(2);
const FactionID FactionID::INFERNO(3);
const FactionID FactionID::NECROPOLIS(4);
const FactionID FactionID::DUNGEON(5);
const FactionID FactionID::STRONGHOLD(6);
const FactionID FactionID::FORTRESS(7);
const FactionID FactionID::CONFLUX(8);
const FactionID FactionID::NEUTRAL(9);

const PrimarySkill PrimarySkill::NONE(-1);
const PrimarySkill PrimarySkill::ATTACK(0);
const PrimarySkill PrimarySkill::DEFENSE(1);
const PrimarySkill PrimarySkill::SPELL_POWER(2);
const PrimarySkill PrimarySkill::KNOWLEDGE(3);
const PrimarySkill PrimarySkill::EXPERIENCE(4);

const BoatId BoatId::NONE(-1);
const BoatId BoatId::NECROPOLIS(0);
const BoatId BoatId::CASTLE(1);
const BoatId BoatId::FORTRESS(2);

const RiverId RiverId::NO_RIVER(0);
const RiverId RiverId::WATER_RIVER(1);
const RiverId RiverId::ICY_RIVER(2);
const RiverId RiverId::MUD_RIVER(3);
const RiverId RiverId::LAVA_RIVER(4);

const RoadId RoadId::NO_ROAD(0);
const RoadId RoadId::DIRT_ROAD(1);
const RoadId RoadId::GRAVEL_ROAD(2);
const RoadId RoadId::COBBLESTONE_ROAD(3);

namespace GameConstants
{
#ifdef VCMI_NO_EXTRA_VERSION
	const std::string VCMI_VERSION = "VCMI " VCMI_VERSION_STRING;
#else
	const std::string VCMI_VERSION = "VCMI " VCMI_VERSION_STRING "." + std::string{GIT_SHA1};
#endif
}

BuildingTypeUniqueID::BuildingTypeUniqueID(FactionID factionID, BuildingID buildingID ):
	BuildingTypeUniqueID(factionID.getNum() * 0x10000 + buildingID.getNum())
{
	assert(factionID.getNum() >= 0);
	assert(factionID.getNum() < 0x10000);
	assert(buildingID.getNum() >= 0);
	assert(buildingID.getNum() < 0x10000);
}

BuildingID BuildingTypeUniqueID::getBuilding() const
{
	return BuildingID(getNum() % 0x10000);
}

FactionID BuildingTypeUniqueID::getFaction() const
{
	return FactionID(getNum() / 0x10000);
}

int32_t IdentifierBase::resolveIdentifier(const std::string & entityType, const std::string identifier)
{
	if (identifier.empty())
		return -1;

	auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), entityType, identifier);

	if (rawId)
		return rawId.value();
	throw IdentifierResolutionException(identifier);
}

si32 HeroClassID::decode(const std::string & identifier)
{
	return resolveIdentifier("heroClass", identifier);
}

std::string HeroClassID::encode(const si32 index)
{
	if (index == -1)
		return "";
	return VLC->heroClasses()->getByIndex(index)->getJsonKey();
}

std::string HeroClassID::entityType()
{
	return "heroClass";
}

const CHeroClass * HeroClassID::toHeroClass() const
{
	return dynamic_cast<const CHeroClass*>(toEntity(VLC));
}

const HeroClass * HeroClassID::toEntity(const Services * services) const
{
	return services->heroClasses()->getByIndex(num);
}

si32 ObjectInstanceID::decode(const std::string & identifier)
{
	return std::stoi(identifier);
}

std::string ObjectInstanceID::encode(const si32 index)
{
	return std::to_string(index);
}

si32 CampaignScenarioID::decode(const std::string & identifier)
{
	return std::stoi(identifier);
}

std::string CampaignScenarioID::encode(const si32 index)
{
	return std::to_string(index);
}

std::string MapObjectID::encode(int32_t index)
{
	if (index == -1)
		return "";
	return VLC->objtypeh->getJsonKey(MapObjectID(index));
}

si32 MapObjectID::decode(const std::string & identifier)
{
	return resolveIdentifier("object", identifier);
}

std::string MapObjectSubID::encode(MapObjectID primaryID, int32_t index)
{
	if (index == -1)
		return "";

	if(primaryID == Obj::PRISON || primaryID == Obj::HERO)
		return HeroTypeID::encode(index);

	if (primaryID == Obj::SPELL_SCROLL)
		return SpellID::encode(index);

	return VLC->objtypeh->getHandlerFor(primaryID, index)->getJsonKey();
}

si32 MapObjectSubID::decode(MapObjectID primaryID, const std::string & identifier)
{
	if(primaryID == Obj::PRISON || primaryID == Obj::HERO)
		return HeroTypeID::decode(identifier);

	if (primaryID == Obj::SPELL_SCROLL)
		return SpellID::decode(identifier);

	return resolveIdentifier(VLC->objtypeh->getJsonKey(primaryID), identifier);
}

std::string BoatId::encode(int32_t index)
{
	if (index == -1)
		return "";
	return VLC->objtypeh->getHandlerFor(MapObjectID::BOAT, index)->getJsonKey();
}

si32 BoatId::decode(const std::string & identifier)
{
	return resolveIdentifier("core:boat", identifier);
}

si32 HeroTypeID::decode(const std::string & identifier)
{
	if (identifier == "random")
		return -2;
	return resolveIdentifier("hero", identifier);
}

std::string HeroTypeID::encode(const si32 index)
{
	if (index == -1)
		return "";
	if (index == -2)
		return "random";
	return VLC->heroTypes()->getByIndex(index)->getJsonKey();
}

std::string HeroTypeID::entityType()
{
	return "hero";
}

const CArtifact * ArtifactIDBase::toArtifact() const
{
	return dynamic_cast<const CArtifact*>(toEntity(VLC));
}

const Artifact * ArtifactIDBase::toEntity(const Services * services) const
{
	return services->artifacts()->getByIndex(num);
}

si32 ArtifactID::decode(const std::string & identifier)
{
	return resolveIdentifier("artifact", identifier);
}

std::string ArtifactID::encode(const si32 index)
{
	if (index == -1)
		return "";
	return VLC->artifacts()->getByIndex(index)->getJsonKey();
}

std::string ArtifactID::entityType()
{
	return "artifact";
}

si32 SecondarySkill::decode(const std::string& identifier)
{
	return resolveIdentifier("secondarySkill", identifier);
}

std::string SecondarySkill::encode(const si32 index)
{
	if (index == -1)
		return "";
	return VLC->skills()->getById(SecondarySkill(index))->getJsonKey();
}

const CSkill * SecondarySkill::toSkill() const
{
	return dynamic_cast<const CSkill *>(toEntity(VLC));
}

const Skill * SecondarySkill::toEntity(const Services * services) const
{
	return services->skills()->getByIndex(num);
}

const CCreature * CreatureIDBase::toCreature() const
{
	return (*VLC->creh)[num];
}

const Creature * CreatureIDBase::toEntity(const Services * services) const
{
	return toEntity(services->creatures());
}

const Creature * CreatureIDBase::toEntity(const CreatureService * creatures) const
{
	return creatures->getByIndex(num);
}

si32 CreatureID::decode(const std::string & identifier)
{
	return resolveIdentifier("creature", identifier);
}

std::string CreatureID::encode(const si32 index)
{
	if (index == -1)
		return "";
	return VLC->creatures()->getById(CreatureID(index))->getJsonKey();
}

std::string CreatureID::entityType()
{
	return "creature";
}

const CSpell * SpellIDBase::toSpell() const
{
	return dynamic_cast<const CSpell*>(toEntity(VLC));
}

const spells::Spell * SpellIDBase::toEntity(const Services * services) const
{
	return toEntity(services->spells());
}

const spells::Spell * SpellIDBase::toEntity(const spells::Service * service) const
{
	return service->getByIndex(num);
}

const CHero * HeroTypeID::toHeroType() const
{
	return dynamic_cast<const CHero*>(toEntity(VLC));
}

const HeroType * HeroTypeID::toEntity(const Services * services) const
{
	return services->heroTypes()->getByIndex(num);
}

si32 SpellID::decode(const std::string & identifier)
{
	if (identifier == "preset")
		return SpellID::PRESET;
	if (identifier == "spellbook_preset")
		return SpellID::SPELLBOOK_PRESET;
	return resolveIdentifier("spell", identifier);
}

std::string SpellID::encode(const si32 index)
{
	if (index == -1)
		return "";
	if (index == SpellID::PRESET)
		return "preset";
	if (index == SpellID::SPELLBOOK_PRESET)
		return "spellbook_preset";
	return VLC->spells()->getByIndex(index)->getJsonKey();
}

si32 BattleField::decode(const std::string & identifier)
{
	return resolveIdentifier("battlefield", identifier);
}

std::string BattleField::encode(const si32 index)
{
	return VLC->battlefields()->getByIndex(index)->getJsonKey();
}

std::string SpellID::entityType()
{
	return "spell";
}

bool PlayerColor::isValidPlayer() const
{
	return num >= 0 && num < PLAYER_LIMIT_I;
}

bool PlayerColor::isSpectator() const
{
	return num == SPECTATOR.num;
}

std::string PlayerColor::toString() const
{
	return encode(num);
}

si32 PlayerColor::decode(const std::string & identifier)
{
	return vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, identifier);
}

std::string PlayerColor::encode(const si32 index)
{
	if (index == -1)
		return "neutral";

	if (index < 0 || index >= std::size(GameConstants::PLAYER_COLOR_NAMES))
	{
		assert(0);
		return "invalid";
	}

	return GameConstants::PLAYER_COLOR_NAMES[index];
}

std::string PlayerColor::entityType()
{
	return "playerColor";
}

si32 PrimarySkill::decode(const std::string& identifier)
{
	return resolveIdentifier(entityType(), identifier);
}

std::string PrimarySkill::encode(const si32 index)
{
	return NPrimarySkill::names[index];
}

std::string PrimarySkill::entityType()
{
	return "primarySkill";
}

si32 FactionID::decode(const std::string & identifier)
{
	return resolveIdentifier(entityType(), identifier);
}

std::string FactionID::encode(const si32 index)
{
	if (index == -1)
		return "";
	return VLC->factions()->getByIndex(index)->getJsonKey();
}

std::string FactionID::entityType()
{
	return "faction";
}

const CFaction * FactionID::toFaction() const
{
	return dynamic_cast<const CFaction*>(toEntity(VLC));
}

const Faction * FactionID::toEntity(const Services * service) const
{
	return service->factions()->getByIndex(num);
}

si32 TerrainId::decode(const std::string & identifier)
{
	if (identifier == "native")
		return TerrainId::NATIVE_TERRAIN;

	return resolveIdentifier(entityType(), identifier);
}

std::string TerrainId::encode(const si32 index)
{
	if (index == TerrainId::NONE)
		return "";
	if (index == TerrainId::NATIVE_TERRAIN)
		return "native";
	return VLC->terrainTypeHandler->getByIndex(index)->getJsonKey();
}

std::string TerrainId::entityType()
{
	return "terrain";
}

si32 RoadId::decode(const std::string & identifier)
{
	if (identifier.empty())
		return RoadId::NO_ROAD.getNum();

	return resolveIdentifier(entityType(), identifier);
}

std::string RoadId::encode(const si32 index)
{
	if (index == RoadId::NO_ROAD.getNum())
		return "";
	return VLC->roadTypeHandler->getByIndex(index)->getJsonKey();
}

std::string RoadId::entityType()
{
	return "road";
}

si32 RiverId::decode(const std::string & identifier)
{
	if (identifier.empty())
		return RiverId::NO_RIVER.getNum();

	return resolveIdentifier(entityType(), identifier);
}

std::string RiverId::encode(const si32 index)
{
	if (index == RiverId::NO_RIVER.getNum())
		return "";
	return VLC->riverTypeHandler->getByIndex(index)->getJsonKey();
}

std::string RiverId::entityType()
{
	return "river";
}

const TerrainType * TerrainId::toEntity(const Services * service) const
{
	return VLC->terrainTypeHandler->getByIndex(num);
}

const RoadType * RoadId::toEntity(const Services * service) const
{
	return VLC->roadTypeHandler->getByIndex(num);
}

const RiverType * RiverId::toEntity(const Services * service) const
{
	return VLC->riverTypeHandler->getByIndex(num);
}

const BattleField BattleField::NONE;

const BattleFieldInfo * BattleField::getInfo() const
{
	return VLC->battlefields()->getById(*this);
}

const ObstacleInfo * Obstacle::getInfo() const
{
	return VLC->obstacles()->getById(*this);
}

si32 SpellSchool::decode(const std::string & identifier)
{
	return resolveIdentifier(entityType(), identifier);
}

std::string SpellSchool::encode(const si32 index)
{
	if (index == ANY.getNum())
		return "any";

	return SpellConfig::SCHOOL[index].jsonName;
}

std::string SpellSchool::entityType()
{
	return "spellSchool";
}

si32 GameResID::decode(const std::string & identifier)
{
	return resolveIdentifier(entityType(), identifier);
}

std::string GameResID::encode(const si32 index)
{
	return GameConstants::RESOURCE_NAMES[index];
}

si32 BuildingTypeUniqueID::decode(const std::string & identifier)
{
	assert(0); //TODO
	return -1;
}

std::string BuildingTypeUniqueID::encode(const si32 index)
{
	assert(0); // TODO
	return "";
}

std::string GameResID::entityType()
{
	return "resource";
}

const std::array<PrimarySkill, 4> & PrimarySkill::ALL_SKILLS()
{
	static const std::array allSkills = {
		PrimarySkill(ATTACK),
		PrimarySkill(DEFENSE),
		PrimarySkill(SPELL_POWER),
		PrimarySkill(KNOWLEDGE)
	};

	return allSkills;
}

const std::array<GameResID, 7> & GameResID::ALL_RESOURCES()
{
	static const std::array allResources = {
		GameResID(WOOD),
		GameResID(MERCURY),
		GameResID(ORE),
		GameResID(SULFUR),
		GameResID(CRYSTAL),
		GameResID(GEMS),
		GameResID(GOLD)
	};

	return allResources;
}

std::string SecondarySkill::entityType()
{
	return "secondarySkill";
}

std::string BuildingID::encode(int32_t index)
{
	return std::to_string(index);
}

si32 BuildingID::decode(const std::string & identifier)
{
	return std::stoi(identifier);
}

VCMI_LIB_NAMESPACE_END