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