EntityIdentifiers.cpp 16 KB

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