2
0

EntityIdentifiers.cpp 15 KB

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