EntityIdentifiers.cpp 15 KB

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