EntityIdentifiers.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/spells/Spell.h>
  21. #include <vcmi/spells/Service.h>
  22. #include "modding/IdentifierStorage.h"
  23. #include "modding/ModScope.h"
  24. #include "VCMI_Lib.h"
  25. #include "CArtHandler.h"//todo: remove
  26. #include "CCreatureHandler.h"//todo: remove
  27. #include "spells/CSpellHandler.h" //todo: remove
  28. #include "CSkillHandler.h"//todo: remove
  29. #include "constants/StringConstants.h"
  30. #include "CGeneralTextHandler.h"
  31. #include "TerrainHandler.h" //TODO: remove
  32. #include "BattleFieldHandler.h"
  33. #include "ObstacleHandler.h"
  34. VCMI_LIB_NAMESPACE_BEGIN
  35. const BattleID BattleID::NONE(-1);
  36. const QueryID QueryID::NONE(-1);
  37. const HeroTypeID HeroTypeID::NONE(-1);
  38. const HeroTypeID HeroTypeID::RANDOM(-2);
  39. const ObjectInstanceID ObjectInstanceID::NONE(-1);
  40. const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER(-2);
  41. const SlotID SlotID::SUMMONED_SLOT_PLACEHOLDER(-3);
  42. const SlotID SlotID::WAR_MACHINES_SLOT(-4);
  43. const SlotID SlotID::ARROW_TOWERS_SLOT(-5);
  44. const PlayerColor PlayerColor::SPECTATOR(-4);
  45. const PlayerColor PlayerColor::CANNOT_DETERMINE(-3);
  46. const PlayerColor PlayerColor::UNFLAGGABLE(-2);
  47. const PlayerColor PlayerColor::NEUTRAL(-1);
  48. const PlayerColor PlayerColor::PLAYER_LIMIT(PLAYER_LIMIT_I);
  49. const TeamID TeamID::NO_TEAM(-1);
  50. const SpellSchool SpellSchool::ANY(-1);
  51. const SpellSchool SpellSchool::AIR(0);
  52. const SpellSchool SpellSchool::FIRE(1);
  53. const SpellSchool SpellSchool::WATER(2);
  54. const SpellSchool SpellSchool::EARTH(3);
  55. const FactionID FactionID::NONE(-2);
  56. const FactionID FactionID::DEFAULT(-1);
  57. const FactionID FactionID::RANDOM(-1);
  58. const FactionID FactionID::ANY(-1);
  59. const FactionID FactionID::CASTLE(0);
  60. const FactionID FactionID::RAMPART(1);
  61. const FactionID FactionID::TOWER(2);
  62. const FactionID FactionID::INFERNO(3);
  63. const FactionID FactionID::NECROPOLIS(4);
  64. const FactionID FactionID::DUNGEON(5);
  65. const FactionID FactionID::STRONGHOLD(6);
  66. const FactionID FactionID::FORTRESS(7);
  67. const FactionID FactionID::CONFLUX(8);
  68. const FactionID FactionID::NEUTRAL(9);
  69. const BoatId BoatId::NONE(-1);
  70. const BoatId BoatId::NECROPOLIS(0);
  71. const BoatId BoatId::CASTLE(1);
  72. const BoatId BoatId::FORTRESS(2);
  73. const RiverId RiverId::NO_RIVER(0);
  74. const RiverId RiverId::WATER_RIVER(1);
  75. const RiverId RiverId::ICY_RIVER(2);
  76. const RiverId RiverId::MUD_RIVER(3);
  77. const RiverId RiverId::LAVA_RIVER(4);
  78. const RoadId RoadId::NO_ROAD(0);
  79. const RoadId RoadId::DIRT_ROAD(1);
  80. const RoadId RoadId::GRAVEL_ROAD(2);
  81. const RoadId RoadId::COBBLESTONE_ROAD(3);
  82. namespace GameConstants
  83. {
  84. #ifdef VCMI_NO_EXTRA_VERSION
  85. const std::string VCMI_VERSION = "VCMI " VCMI_VERSION_STRING;
  86. #else
  87. const std::string VCMI_VERSION = "VCMI " VCMI_VERSION_STRING "." + std::string{GIT_SHA1};
  88. #endif
  89. }
  90. si32 HeroTypeID::decode(const std::string & identifier)
  91. {
  92. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeMap(), "hero", identifier);
  93. if(rawId)
  94. return rawId.value();
  95. else
  96. return -1;
  97. }
  98. std::string HeroTypeID::encode(const si32 index)
  99. {
  100. return VLC->heroTypes()->getByIndex(index)->getJsonKey();
  101. }
  102. std::string HeroTypeID::entityType()
  103. {
  104. return "hero";
  105. }
  106. const CArtifact * ArtifactIDBase::toArtifact() const
  107. {
  108. return dynamic_cast<const CArtifact*>(toArtifact(VLC->artifacts()));
  109. }
  110. const Artifact * ArtifactIDBase::toArtifact(const ArtifactService * service) const
  111. {
  112. return service->getByIndex(num);
  113. }
  114. si32 ArtifactID::decode(const std::string & identifier)
  115. {
  116. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "artifact", identifier);
  117. if(rawId)
  118. return rawId.value();
  119. else
  120. return -1;
  121. }
  122. std::string ArtifactID::encode(const si32 index)
  123. {
  124. return VLC->artifacts()->getByIndex(index)->getJsonKey();
  125. }
  126. std::string ArtifactID::entityType()
  127. {
  128. return "artifact";
  129. }
  130. const CCreature * CreatureIDBase::toCreature() const
  131. {
  132. return VLC->creh->objects.at(num);
  133. }
  134. const Creature * CreatureIDBase::toCreature(const CreatureService * creatures) const
  135. {
  136. return creatures->getByIndex(num);
  137. }
  138. si32 CreatureID::decode(const std::string & identifier)
  139. {
  140. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "creature", identifier);
  141. if(rawId)
  142. return rawId.value();
  143. else
  144. return -1;
  145. }
  146. std::string CreatureID::encode(const si32 index)
  147. {
  148. return VLC->creatures()->getById(CreatureID(index))->getJsonKey();
  149. }
  150. std::string CreatureID::entityType()
  151. {
  152. return "creature";
  153. }
  154. const CSpell * SpellIDBase::toSpell() const
  155. {
  156. if(num < 0 || num >= VLC->spellh->objects.size())
  157. {
  158. logGlobal->error("Unable to get spell of invalid ID %d", static_cast<int>(num));
  159. return nullptr;
  160. }
  161. return VLC->spellh->objects[num];
  162. }
  163. const spells::Spell * SpellIDBase::toSpell(const spells::Service * service) const
  164. {
  165. return service->getByIndex(num);
  166. }
  167. si32 SpellID::decode(const std::string & identifier)
  168. {
  169. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), "spell", identifier);
  170. if(rawId)
  171. return rawId.value();
  172. else
  173. return -1;
  174. }
  175. std::string SpellID::encode(const si32 index)
  176. {
  177. return VLC->spells()->getByIndex(index)->getJsonKey();
  178. }
  179. std::string SpellID::entityType()
  180. {
  181. return "spell";
  182. }
  183. bool PlayerColor::isValidPlayer() const
  184. {
  185. return num >= 0 && num < PLAYER_LIMIT_I;
  186. }
  187. bool PlayerColor::isSpectator() const
  188. {
  189. return num == SPECTATOR.num;
  190. }
  191. std::string PlayerColor::toString() const
  192. {
  193. if (num == -1)
  194. return "neutral";
  195. return encode(num);
  196. }
  197. si32 PlayerColor::decode(const std::string & identifier)
  198. {
  199. return vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, identifier);
  200. }
  201. std::string PlayerColor::encode(const si32 index)
  202. {
  203. if (index < 0 || index >= std::size(GameConstants::PLAYER_COLOR_NAMES))
  204. {
  205. assert(0);
  206. return "invalid";
  207. }
  208. return GameConstants::PLAYER_COLOR_NAMES[index];
  209. }
  210. std::string PlayerColor::entityType()
  211. {
  212. return "playerColor";
  213. }
  214. si32 FactionID::decode(const std::string & identifier)
  215. {
  216. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), entityType(), identifier);
  217. if(rawId)
  218. return rawId.value();
  219. else
  220. return FactionID::DEFAULT;
  221. }
  222. std::string FactionID::encode(const si32 index)
  223. {
  224. return VLC->factions()->getByIndex(index)->getJsonKey();
  225. }
  226. std::string FactionID::entityType()
  227. {
  228. return "faction";
  229. }
  230. si32 TerrainId::decode(const std::string & identifier)
  231. {
  232. auto rawId = VLC->identifiers()->getIdentifier(ModScope::scopeGame(), entityType(), identifier);
  233. if(rawId)
  234. return rawId.value();
  235. else
  236. return static_cast<si32>(TerrainId::NONE);
  237. }
  238. std::string TerrainId::encode(const si32 index)
  239. {
  240. return VLC->terrainTypeHandler->getByIndex(index)->getJsonKey();
  241. }
  242. std::string TerrainId::entityType()
  243. {
  244. return "terrain";
  245. }
  246. const BattleField BattleField::NONE;
  247. const BattleFieldInfo * BattleField::getInfo() const
  248. {
  249. return VLC->battlefields()->getById(*this);
  250. }
  251. const ObstacleInfo * Obstacle::getInfo() const
  252. {
  253. return VLC->obstacles()->getById(*this);
  254. }
  255. VCMI_LIB_NAMESPACE_END