EntityIdentifiers.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 Obj::encode(int32_t index)
  141. {
  142. return VLC->objtypeh->getObjectHandlerName(index);
  143. }
  144. si32 Obj::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 CCreature * CreatureIDBase::toCreature() const
  205. {
  206. return VLC->creh->objects.at(num);
  207. }
  208. const Creature * CreatureIDBase::toEntity(const Services * services) const
  209. {
  210. return toEntity(services->creatures());
  211. }
  212. const Creature * CreatureIDBase::toEntity(const CreatureService * creatures) const
  213. {
  214. return creatures->getByIndex(num);
  215. }
  216. si32 CreatureID::decode(const std::string & identifier)
  217. {
  218. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "creature", identifier);
  219. if(rawId)
  220. return rawId.value();
  221. else
  222. return -1;
  223. }
  224. std::string CreatureID::encode(const si32 index)
  225. {
  226. return VLC->creatures()->getById(CreatureID(index))->getJsonKey();
  227. }
  228. std::string CreatureID::entityType()
  229. {
  230. return "creature";
  231. }
  232. const CSpell * SpellIDBase::toSpell() const
  233. {
  234. if(num < 0 || num >= VLC->spellh->objects.size())
  235. {
  236. logGlobal->error("Unable to get spell of invalid ID %d", static_cast<int>(num));
  237. return nullptr;
  238. }
  239. return VLC->spellh->objects[num];
  240. }
  241. const spells::Spell * SpellIDBase::toEntity(const Services * services) const
  242. {
  243. return toEntity(services->spells());
  244. }
  245. const spells::Spell * SpellIDBase::toEntity(const spells::Service * service) const
  246. {
  247. return service->getByIndex(num);
  248. }
  249. const CHero * HeroTypeID::toHeroType() const
  250. {
  251. return dynamic_cast<const CHero*>(toEntity(VLC));
  252. }
  253. const HeroType * HeroTypeID::toEntity(const Services * services) const
  254. {
  255. return services->heroTypes()->getByIndex(num);
  256. }
  257. si32 SpellID::decode(const std::string & identifier)
  258. {
  259. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "spell", identifier);
  260. if(rawId)
  261. return rawId.value();
  262. else
  263. return -1;
  264. }
  265. std::string SpellID::encode(const si32 index)
  266. {
  267. return VLC->spells()->getByIndex(index)->getJsonKey();
  268. }
  269. si32 BattleField::decode(const std::string & identifier)
  270. {
  271. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "spell", identifier);
  272. if(rawId)
  273. return rawId.value();
  274. else
  275. return -1;
  276. }
  277. std::string BattleField::encode(const si32 index)
  278. {
  279. return VLC->spells()->getByIndex(index)->getJsonKey();
  280. }
  281. std::string SpellID::entityType()
  282. {
  283. return "spell";
  284. }
  285. bool PlayerColor::isValidPlayer() const
  286. {
  287. return num >= 0 && num < PLAYER_LIMIT_I;
  288. }
  289. bool PlayerColor::isSpectator() const
  290. {
  291. return num == SPECTATOR.num;
  292. }
  293. std::string PlayerColor::toString() const
  294. {
  295. return encode(num);
  296. }
  297. si32 PlayerColor::decode(const std::string & identifier)
  298. {
  299. return vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, identifier);
  300. }
  301. std::string PlayerColor::encode(const si32 index)
  302. {
  303. if (index == -1)
  304. return "neutral";
  305. if (index < 0 || index >= std::size(GameConstants::PLAYER_COLOR_NAMES))
  306. {
  307. assert(0);
  308. return "invalid";
  309. }
  310. return GameConstants::PLAYER_COLOR_NAMES[index];
  311. }
  312. std::string PlayerColor::entityType()
  313. {
  314. return "playerColor";
  315. }
  316. si32 PrimarySkill::decode(const std::string& identifier)
  317. {
  318. return *VLC->identifiers()->getIdentifier(ModScope::scopeGame(), entityType(), identifier);
  319. }
  320. std::string PrimarySkill::encode(const si32 index)
  321. {
  322. return NPrimarySkill::names[index];
  323. }
  324. std::string PrimarySkill::entityType()
  325. {
  326. return "primarySkill";
  327. }
  328. si32 FactionID::decode(const std::string & identifier)
  329. {
  330. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), entityType(), identifier);
  331. if(rawId)
  332. return rawId.value();
  333. else
  334. return FactionID::DEFAULT.getNum();
  335. }
  336. std::string FactionID::encode(const si32 index)
  337. {
  338. return VLC->factions()->getByIndex(index)->getJsonKey();
  339. }
  340. std::string FactionID::entityType()
  341. {
  342. return "faction";
  343. }
  344. const Faction * FactionID::toEntity(const Services * service) const
  345. {
  346. return service->factions()->getByIndex(num);
  347. }
  348. si32 TerrainId::decode(const std::string & identifier)
  349. {
  350. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), entityType(), identifier);
  351. if(rawId)
  352. return rawId.value();
  353. else
  354. return static_cast<si32>(TerrainId::NONE);
  355. }
  356. std::string TerrainId::encode(const si32 index)
  357. {
  358. return VLC->terrainTypeHandler->getByIndex(index)->getJsonKey();
  359. }
  360. std::string TerrainId::entityType()
  361. {
  362. return "terrain";
  363. }
  364. const TerrainType * TerrainId::toEntity(const Services * service) const
  365. {
  366. return VLC->terrainTypeHandler->getByIndex(num);
  367. }
  368. const RoadType * RoadId::toEntity(const Services * service) const
  369. {
  370. return VLC->roadTypeHandler->getByIndex(num);
  371. }
  372. const RiverType * RiverId::toEntity(const Services * service) const
  373. {
  374. return VLC->riverTypeHandler->getByIndex(num);
  375. }
  376. const BattleField BattleField::NONE;
  377. const BattleFieldInfo * BattleField::getInfo() const
  378. {
  379. return VLC->battlefields()->getById(*this);
  380. }
  381. const ObstacleInfo * Obstacle::getInfo() const
  382. {
  383. return VLC->obstacles()->getById(*this);
  384. }
  385. si32 SpellSchool::decode(const std::string & identifier)
  386. {
  387. return *VLC->identifiers()->getIdentifier(ModScope::scopeGame(), entityType(), identifier);
  388. }
  389. std::string SpellSchool::encode(const si32 index)
  390. {
  391. if (index == ANY.getNum())
  392. return "any";
  393. return SpellConfig::SCHOOL[index].jsonName;
  394. }
  395. std::string SpellSchool::entityType()
  396. {
  397. return "spellSchool";
  398. }
  399. si32 GameResID::decode(const std::string & identifier)
  400. {
  401. return *VLC->identifiers()->getIdentifier(ModScope::scopeGame(), entityType(), identifier);
  402. }
  403. std::string GameResID::encode(const si32 index)
  404. {
  405. return GameConstants::RESOURCE_NAMES[index];
  406. }
  407. si32 BuildingTypeUniqueID::decode(const std::string & identifier)
  408. {
  409. assert(0); //TODO
  410. return -1;
  411. }
  412. std::string BuildingTypeUniqueID::encode(const si32 index)
  413. {
  414. assert(0); // TODO
  415. return "";
  416. }
  417. std::string GameResID::entityType()
  418. {
  419. return "resource";
  420. }
  421. const std::array<GameResID, 7> & GameResID::ALL_RESOURCES()
  422. {
  423. static const std::array allResources = {
  424. GameResID(WOOD),
  425. GameResID(MERCURY),
  426. GameResID(ORE),
  427. GameResID(SULFUR),
  428. GameResID(CRYSTAL),
  429. GameResID(GEMS),
  430. GameResID(GOLD)
  431. };
  432. return allResources;
  433. }
  434. std::string SecondarySkill::entityType()
  435. {
  436. return "secondarySkill";
  437. }
  438. VCMI_LIB_NAMESPACE_END