EntityIdentifiers.cpp 12 KB

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