EntityIdentifiers.cpp 11 KB

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