EntityIdentifiers.cpp 7.4 KB

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