EntityIdentifiers.cpp 11 KB

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