2
0

EntityIdentifiers.cpp 15 KB

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