EntityIdentifiers.cpp 16 KB

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