EntityIdentifiers.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * EntityIdentifiers.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #ifndef VCMI_NO_EXTRA_VERSION
  12. #include "../Version.h"
  13. #endif
  14. #include <vcmi/Artifact.h>
  15. #include <vcmi/ArtifactService.h>
  16. #include <vcmi/Faction.h>
  17. #include <vcmi/FactionService.h>
  18. #include <vcmi/HeroType.h>
  19. #include <vcmi/HeroTypeService.h>
  20. #include <vcmi/HeroClass.h>
  21. #include <vcmi/HeroClassService.h>
  22. #include <vcmi/Services.h>
  23. #include <vcmi/spells/Spell.h>
  24. #include <vcmi/spells/Service.h>
  25. #include "modding/IdentifierStorage.h"
  26. #include "modding/ModScope.h"
  27. #include "GameLibrary.h"
  28. #include "CCreatureHandler.h"
  29. #include "spells/CSpellHandler.h"
  30. #include "spells/SpellSchoolHandler.h"
  31. #include "CSkillHandler.h"
  32. #include "entities/artifact/CArtifact.h"
  33. #include "entities/faction/CFaction.h"
  34. #include "entities/hero/CHero.h"
  35. #include "entities/hero/CHeroClass.h"
  36. #include "mapObjectConstructors/AObjectTypeHandler.h"
  37. #include "constants/StringConstants.h"
  38. #include "texts/CGeneralTextHandler.h"
  39. #include "TerrainHandler.h"
  40. #include "RiverHandler.h"
  41. #include "RoadHandler.h"
  42. #include "BattleFieldHandler.h"
  43. #include "ObstacleHandler.h"
  44. #include "mapObjectConstructors/CObjectClassesHandler.h"
  45. VCMI_LIB_NAMESPACE_BEGIN
  46. const CampaignScenarioID CampaignScenarioID::NONE(-1);
  47. const BattleID BattleID::NONE(-1);
  48. const QueryID QueryID::NONE(-1);
  49. const QueryID QueryID::CLIENT(-2);
  50. const HeroTypeID HeroTypeID::NONE(-1);
  51. const HeroTypeID HeroTypeID::RANDOM(-2);
  52. const HeroTypeID HeroTypeID::GEM(27);
  53. const HeroTypeID HeroTypeID::SOLMYR(45);
  54. const HeroTypeID HeroTypeID::CAMP_STRONGEST(0xFFFD);
  55. const HeroTypeID HeroTypeID::CAMP_GENERATED(0xFFFE);
  56. const HeroTypeID HeroTypeID::CAMP_RANDOM(0xFFFF);
  57. const ObjectInstanceID ObjectInstanceID::NONE(-1);
  58. const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER(-2);
  59. const SlotID SlotID::SUMMONED_SLOT_PLACEHOLDER(-3);
  60. const SlotID SlotID::WAR_MACHINES_SLOT(-4);
  61. const SlotID SlotID::ARROW_TOWERS_SLOT(-5);
  62. const PlayerColor PlayerColor::SPECTATOR(-4);
  63. const PlayerColor PlayerColor::CANNOT_DETERMINE(-3);
  64. const PlayerColor PlayerColor::UNFLAGGABLE(-2);
  65. const PlayerColor PlayerColor::NEUTRAL(-1);
  66. const PlayerColor PlayerColor::PLAYER_LIMIT(PLAYER_LIMIT_I);
  67. const TeamID TeamID::NO_TEAM(-1);
  68. const SpellSchool SpellSchool::ANY(-1);
  69. const SpellSchool SpellSchool::AIR(0);
  70. const SpellSchool SpellSchool::FIRE(1);
  71. const SpellSchool SpellSchool::EARTH(2);
  72. const SpellSchool SpellSchool::WATER(3);
  73. const FactionID FactionID::NONE(-2);
  74. const FactionID FactionID::DEFAULT(-1);
  75. const FactionID FactionID::RANDOM(-1);
  76. const FactionID FactionID::ANY(-1);
  77. const FactionID FactionID::CASTLE(0);
  78. const FactionID FactionID::RAMPART(1);
  79. const FactionID FactionID::TOWER(2);
  80. const FactionID FactionID::INFERNO(3);
  81. const FactionID FactionID::NECROPOLIS(4);
  82. const FactionID FactionID::DUNGEON(5);
  83. const FactionID FactionID::STRONGHOLD(6);
  84. const FactionID FactionID::FORTRESS(7);
  85. const FactionID FactionID::CONFLUX(8);
  86. const FactionID FactionID::NEUTRAL(9);
  87. const PrimarySkill PrimarySkill::NONE(-1);
  88. const PrimarySkill PrimarySkill::ATTACK(0);
  89. const PrimarySkill PrimarySkill::DEFENSE(1);
  90. const PrimarySkill PrimarySkill::SPELL_POWER(2);
  91. const PrimarySkill PrimarySkill::KNOWLEDGE(3);
  92. const BoatId BoatId::NONE(-1);
  93. const BoatId BoatId::NECROPOLIS(0);
  94. const BoatId BoatId::CASTLE(1);
  95. const BoatId BoatId::FORTRESS(2);
  96. const RiverId RiverId::NO_RIVER(0);
  97. const RiverId RiverId::WATER_RIVER(1);
  98. const RiverId RiverId::ICY_RIVER(2);
  99. const RiverId RiverId::MUD_RIVER(3);
  100. const RiverId RiverId::LAVA_RIVER(4);
  101. const RoadId RoadId::NO_ROAD(0);
  102. const RoadId RoadId::DIRT_ROAD(1);
  103. const RoadId RoadId::GRAVEL_ROAD(2);
  104. const RoadId RoadId::COBBLESTONE_ROAD(3);
  105. namespace GameConstants
  106. {
  107. #ifdef VCMI_NO_EXTRA_VERSION
  108. const std::string VCMI_VERSION = "VCMI " VCMI_VERSION_STRING;
  109. #else
  110. const std::string VCMI_VERSION = "VCMI " VCMI_VERSION_STRING "." + std::string{GIT_SHA1};
  111. #endif
  112. }
  113. BuildingTypeUniqueID::BuildingTypeUniqueID(FactionID factionID, BuildingID buildingID ):
  114. BuildingTypeUniqueID(factionID.getNum() * 0x10000 + buildingID.getNum())
  115. {
  116. assert(factionID.getNum() >= 0);
  117. assert(factionID.getNum() < 0x10000);
  118. assert(buildingID.getNum() >= 0);
  119. assert(buildingID.getNum() < 0x10000);
  120. }
  121. BuildingID BuildingTypeUniqueID::getBuilding() const
  122. {
  123. return BuildingID(getNum() % 0x10000);
  124. }
  125. FactionID BuildingTypeUniqueID::getFaction() const
  126. {
  127. return FactionID(getNum() / 0x10000);
  128. }
  129. int32_t IdentifierBase::resolveIdentifier(const std::string & entityType, const std::string identifier)
  130. {
  131. if (identifier.empty())
  132. return -1;
  133. auto rawId = LIBRARY->identifiers()->getIdentifier(ModScope::scopeGame(), entityType, identifier);
  134. if (rawId)
  135. return rawId.value();
  136. throw IdentifierResolutionException(identifier);
  137. }
  138. si32 HeroClassID::decode(const std::string & identifier)
  139. {
  140. return resolveIdentifier("heroClass", identifier);
  141. }
  142. std::string HeroClassID::encode(const si32 index)
  143. {
  144. if (index == -1)
  145. return "";
  146. return LIBRARY->heroClasses()->getByIndex(index)->getJsonKey();
  147. }
  148. std::string HeroClassID::entityType()
  149. {
  150. return "heroClass";
  151. }
  152. const CHeroClass * HeroClassID::toHeroClass() const
  153. {
  154. return dynamic_cast<const CHeroClass*>(toEntity(LIBRARY));
  155. }
  156. const HeroClass * HeroClassID::toEntity(const Services * services) const
  157. {
  158. return services->heroClasses()->getByIndex(num);
  159. }
  160. si32 ObjectInstanceID::decode(const std::string & identifier)
  161. {
  162. return std::stoi(identifier);
  163. }
  164. std::string ObjectInstanceID::encode(const si32 index)
  165. {
  166. return std::to_string(index);
  167. }
  168. si32 CampaignScenarioID::decode(const std::string & identifier)
  169. {
  170. return std::stoi(identifier);
  171. }
  172. std::string CampaignScenarioID::encode(const si32 index)
  173. {
  174. return std::to_string(index);
  175. }
  176. std::string MapObjectID::encode(int32_t index)
  177. {
  178. if (index == -1)
  179. return "";
  180. return LIBRARY->objtypeh->getJsonKey(MapObjectID(index));
  181. }
  182. si32 MapObjectID::decode(const std::string & identifier)
  183. {
  184. return resolveIdentifier("object", identifier);
  185. }
  186. std::string MapObjectSubID::encode(MapObjectID primaryID, int32_t index)
  187. {
  188. if (index == -1)
  189. return "";
  190. if(primaryID == Obj::PRISON || primaryID == Obj::HERO)
  191. return HeroTypeID::encode(index);
  192. if (primaryID == Obj::SPELL_SCROLL)
  193. return SpellID::encode(index);
  194. return LIBRARY->objtypeh->getHandlerFor(primaryID, index)->getJsonKey();
  195. }
  196. si32 MapObjectSubID::decode(MapObjectID primaryID, const std::string & identifier)
  197. {
  198. if(primaryID == Obj::PRISON || primaryID == Obj::HERO)
  199. return HeroTypeID::decode(identifier);
  200. if (primaryID == Obj::SPELL_SCROLL)
  201. return SpellID::decode(identifier);
  202. return resolveIdentifier(LIBRARY->objtypeh->getJsonKey(primaryID), identifier);
  203. }
  204. std::string BoatId::encode(int32_t index)
  205. {
  206. if (index == -1)
  207. return "";
  208. return LIBRARY->objtypeh->getHandlerFor(MapObjectID::BOAT, index)->getJsonKey();
  209. }
  210. si32 BoatId::decode(const std::string & identifier)
  211. {
  212. return resolveIdentifier("core:boat", identifier);
  213. }
  214. si32 HeroTypeID::decode(const std::string & identifier)
  215. {
  216. if (identifier == "random")
  217. return -2;
  218. return resolveIdentifier("hero", identifier);
  219. }
  220. std::string HeroTypeID::encode(const si32 index)
  221. {
  222. if (index == -1)
  223. return "";
  224. if (index == -2)
  225. return "random";
  226. return LIBRARY->heroTypes()->getByIndex(index)->getJsonKey();
  227. }
  228. std::string HeroTypeID::entityType()
  229. {
  230. return "hero";
  231. }
  232. const CArtifact * ArtifactIDBase::toArtifact() const
  233. {
  234. return dynamic_cast<const CArtifact*>(toEntity(LIBRARY));
  235. }
  236. const Artifact * ArtifactIDBase::toEntity(const Services * services) const
  237. {
  238. return services->artifacts()->getByIndex(num);
  239. }
  240. si32 ArtifactID::decode(const std::string & identifier)
  241. {
  242. return resolveIdentifier("artifact", identifier);
  243. }
  244. std::string ArtifactID::encode(const si32 index)
  245. {
  246. if (index == -1)
  247. return "";
  248. return LIBRARY->artifacts()->getByIndex(index)->getJsonKey();
  249. }
  250. std::string ArtifactID::entityType()
  251. {
  252. return "artifact";
  253. }
  254. si32 SecondarySkill::decode(const std::string& identifier)
  255. {
  256. return resolveIdentifier("secondarySkill", identifier);
  257. }
  258. std::string SecondarySkill::encode(const si32 index)
  259. {
  260. if (index == -1)
  261. return "";
  262. return LIBRARY->skills()->getById(SecondarySkill(index))->getJsonKey();
  263. }
  264. const CSkill * SecondarySkill::toSkill() const
  265. {
  266. return dynamic_cast<const CSkill *>(toEntity(LIBRARY));
  267. }
  268. const Skill * SecondarySkill::toEntity(const Services * services) const
  269. {
  270. return services->skills()->getByIndex(num);
  271. }
  272. const CCreature * CreatureIDBase::toCreature() const
  273. {
  274. return (*LIBRARY->creh)[num];
  275. }
  276. const Creature * CreatureIDBase::toEntity(const Services * services) const
  277. {
  278. return toEntity(services->creatures());
  279. }
  280. const Creature * CreatureIDBase::toEntity(const CreatureService * creatures) const
  281. {
  282. return creatures->getByIndex(num);
  283. }
  284. si32 CreatureID::decode(const std::string & identifier)
  285. {
  286. return resolveIdentifier("creature", identifier);
  287. }
  288. std::string CreatureID::encode(const si32 index)
  289. {
  290. if (index == -1)
  291. return "";
  292. return LIBRARY->creatures()->getById(CreatureID(index))->getJsonKey();
  293. }
  294. std::string CreatureID::entityType()
  295. {
  296. return "creature";
  297. }
  298. const CSpell * SpellIDBase::toSpell() const
  299. {
  300. return dynamic_cast<const CSpell*>(toEntity(LIBRARY));
  301. }
  302. const spells::Spell * SpellIDBase::toEntity(const Services * services) const
  303. {
  304. return toEntity(services->spells());
  305. }
  306. const spells::Spell * SpellIDBase::toEntity(const spells::Service * service) const
  307. {
  308. return service->getByIndex(num);
  309. }
  310. const CHero * HeroTypeID::toHeroType() const
  311. {
  312. return dynamic_cast<const CHero*>(toEntity(LIBRARY));
  313. }
  314. const HeroType * HeroTypeID::toEntity(const Services * services) const
  315. {
  316. return services->heroTypes()->getByIndex(num);
  317. }
  318. si32 SpellID::decode(const std::string & identifier)
  319. {
  320. if (identifier == "preset")
  321. return SpellID::PRESET;
  322. if (identifier == "spellbook_preset")
  323. return SpellID::SPELLBOOK_PRESET;
  324. return resolveIdentifier("spell", identifier);
  325. }
  326. std::string SpellID::encode(const si32 index)
  327. {
  328. if (index == -1)
  329. return "";
  330. if (index == SpellID::PRESET)
  331. return "preset";
  332. if (index == SpellID::SPELLBOOK_PRESET)
  333. return "spellbook_preset";
  334. return LIBRARY->spells()->getByIndex(index)->getJsonKey();
  335. }
  336. si32 BattleField::decode(const std::string & identifier)
  337. {
  338. return resolveIdentifier("battlefield", identifier);
  339. }
  340. std::string BattleField::encode(const si32 index)
  341. {
  342. return LIBRARY->battlefields()->getByIndex(index)->getJsonKey();
  343. }
  344. std::string SpellID::entityType()
  345. {
  346. return "spell";
  347. }
  348. bool PlayerColor::isValidPlayer() const
  349. {
  350. return num >= 0 && num < PLAYER_LIMIT_I;
  351. }
  352. bool PlayerColor::isSpectator() const
  353. {
  354. return num == SPECTATOR.num;
  355. }
  356. std::string PlayerColor::toString() const
  357. {
  358. return encode(num);
  359. }
  360. si32 PlayerColor::decode(const std::string & identifier)
  361. {
  362. return vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, identifier);
  363. }
  364. std::string PlayerColor::encode(const si32 index)
  365. {
  366. if (index == -1)
  367. return "neutral";
  368. if (index < 0 || index >= std::size(GameConstants::PLAYER_COLOR_NAMES))
  369. {
  370. assert(0);
  371. return "invalid";
  372. }
  373. return GameConstants::PLAYER_COLOR_NAMES[index];
  374. }
  375. std::string PlayerColor::entityType()
  376. {
  377. return "playerColor";
  378. }
  379. si32 PrimarySkill::decode(const std::string& identifier)
  380. {
  381. return resolveIdentifier(entityType(), identifier);
  382. }
  383. std::string PrimarySkill::encode(const si32 index)
  384. {
  385. return NPrimarySkill::names[index];
  386. }
  387. std::string PrimarySkill::entityType()
  388. {
  389. return "primarySkill";
  390. }
  391. si32 FactionID::decode(const std::string & identifier)
  392. {
  393. return resolveIdentifier(entityType(), identifier);
  394. }
  395. std::string FactionID::encode(const si32 index)
  396. {
  397. if (index == -1)
  398. return "";
  399. return LIBRARY->factions()->getByIndex(index)->getJsonKey();
  400. }
  401. std::string FactionID::entityType()
  402. {
  403. return "faction";
  404. }
  405. const CFaction * FactionID::toFaction() const
  406. {
  407. return dynamic_cast<const CFaction*>(toEntity(LIBRARY));
  408. }
  409. const Faction * FactionID::toEntity(const Services * service) const
  410. {
  411. return service->factions()->getByIndex(num);
  412. }
  413. si32 TerrainId::decode(const std::string & identifier)
  414. {
  415. if (identifier == "native")
  416. return TerrainId::NATIVE_TERRAIN;
  417. return resolveIdentifier(entityType(), identifier);
  418. }
  419. std::string TerrainId::encode(const si32 index)
  420. {
  421. if (index == TerrainId::NONE)
  422. return "";
  423. if (index == TerrainId::NATIVE_TERRAIN)
  424. return "native";
  425. return LIBRARY->terrainTypeHandler->getByIndex(index)->getJsonKey();
  426. }
  427. std::string TerrainId::entityType()
  428. {
  429. return "terrain";
  430. }
  431. si32 RoadId::decode(const std::string & identifier)
  432. {
  433. if (identifier.empty())
  434. return RoadId::NO_ROAD.getNum();
  435. return resolveIdentifier(entityType(), identifier);
  436. }
  437. std::string RoadId::encode(const si32 index)
  438. {
  439. if (index == RoadId::NO_ROAD.getNum())
  440. return "";
  441. return LIBRARY->roadTypeHandler->getByIndex(index)->getJsonKey();
  442. }
  443. std::string RoadId::entityType()
  444. {
  445. return "road";
  446. }
  447. si32 RiverId::decode(const std::string & identifier)
  448. {
  449. if (identifier.empty())
  450. return RiverId::NO_RIVER.getNum();
  451. return resolveIdentifier(entityType(), identifier);
  452. }
  453. std::string RiverId::encode(const si32 index)
  454. {
  455. if (index == RiverId::NO_RIVER.getNum())
  456. return "";
  457. return LIBRARY->riverTypeHandler->getByIndex(index)->getJsonKey();
  458. }
  459. std::string RiverId::entityType()
  460. {
  461. return "river";
  462. }
  463. const TerrainType * TerrainId::toEntity(const Services * service) const
  464. {
  465. return LIBRARY->terrainTypeHandler->getByIndex(num);
  466. }
  467. const RoadType * RoadId::toEntity(const Services * service) const
  468. {
  469. return LIBRARY->roadTypeHandler->getByIndex(num);
  470. }
  471. const RiverType * RiverId::toEntity(const Services * service) const
  472. {
  473. return LIBRARY->riverTypeHandler->getByIndex(num);
  474. }
  475. const BattleField BattleField::NONE;
  476. const BattleFieldInfo * BattleField::getInfo() const
  477. {
  478. return LIBRARY->battlefields()->getById(*this);
  479. }
  480. const ObstacleInfo * Obstacle::getInfo() const
  481. {
  482. return LIBRARY->obstacles()->getById(*this);
  483. }
  484. si32 SpellSchool::decode(const std::string & identifier)
  485. {
  486. return resolveIdentifier(entityType(), identifier);
  487. }
  488. std::string SpellSchool::encode(const si32 index)
  489. {
  490. if (index == ANY.getNum())
  491. return "any";
  492. return LIBRARY->spellSchoolHandler->getById(SpellSchool(index))->getJsonKey();
  493. }
  494. std::string SpellSchool::entityType()
  495. {
  496. return "spellSchool";
  497. }
  498. si32 GameResID::decode(const std::string & identifier)
  499. {
  500. return resolveIdentifier(entityType(), identifier);
  501. }
  502. std::string GameResID::encode(const si32 index)
  503. {
  504. return GameConstants::RESOURCE_NAMES[index];
  505. }
  506. si32 BuildingTypeUniqueID::decode(const std::string & identifier)
  507. {
  508. assert(0); //TODO
  509. return -1;
  510. }
  511. std::string BuildingTypeUniqueID::encode(const si32 index)
  512. {
  513. assert(0); // TODO
  514. return "";
  515. }
  516. std::string GameResID::entityType()
  517. {
  518. return "resource";
  519. }
  520. const std::array<PlayerColor, PlayerColor::PLAYER_LIMIT_I> & PlayerColor::ALL_PLAYERS()
  521. {
  522. static const std::array allPlayers = {
  523. PlayerColor(0),
  524. PlayerColor(1),
  525. PlayerColor(2),
  526. PlayerColor(3),
  527. PlayerColor(4),
  528. PlayerColor(5),
  529. PlayerColor(6),
  530. PlayerColor(7)
  531. };
  532. return allPlayers;
  533. }
  534. const std::array<PrimarySkill, 4> & PrimarySkill::ALL_SKILLS()
  535. {
  536. static const std::array allSkills = {
  537. PrimarySkill(ATTACK),
  538. PrimarySkill(DEFENSE),
  539. PrimarySkill(SPELL_POWER),
  540. PrimarySkill(KNOWLEDGE)
  541. };
  542. return allSkills;
  543. }
  544. const std::array<GameResID, 7> & GameResID::ALL_RESOURCES()
  545. {
  546. static const std::array allResources = {
  547. GameResID(WOOD),
  548. GameResID(MERCURY),
  549. GameResID(ORE),
  550. GameResID(SULFUR),
  551. GameResID(CRYSTAL),
  552. GameResID(GEMS),
  553. GameResID(GOLD)
  554. };
  555. return allResources;
  556. }
  557. std::string SecondarySkill::entityType()
  558. {
  559. return "secondarySkill";
  560. }
  561. std::string BuildingID::encode(int32_t index)
  562. {
  563. return std::to_string(index);
  564. }
  565. si32 BuildingID::decode(const std::string & identifier)
  566. {
  567. return std::stoi(identifier);
  568. }
  569. VCMI_LIB_NAMESPACE_END