EntityIdentifiers.cpp 7.1 KB

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