EntityIdentifiers.cpp 15 KB

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