GameConstants.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * GameConstants.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. #define INSTANTIATE_BASE_FOR_ID_HERE
  11. #include "StdInc.h"
  12. #ifndef VCMI_NO_EXTRA_VERSION
  13. #include "../Version.h"
  14. #endif
  15. #include <vcmi/Artifact.h>
  16. #include <vcmi/ArtifactService.h>
  17. #include <vcmi/Faction.h>
  18. #include <vcmi/FactionService.h>
  19. #include <vcmi/HeroType.h>
  20. #include <vcmi/HeroTypeService.h>
  21. #include <vcmi/spells/Spell.h>
  22. #include <vcmi/spells/Service.h>
  23. #include "VCMI_Lib.h"
  24. #include "CArtHandler.h"//todo: remove
  25. #include "CCreatureHandler.h"//todo: remove
  26. #include "spells/CSpellHandler.h" //todo: remove
  27. #include "CSkillHandler.h"//todo: remove
  28. #include "StringConstants.h"
  29. #include "CGeneralTextHandler.h"
  30. #include "CModHandler.h"//todo: remove
  31. #include "TerrainHandler.h" //TODO: remove
  32. #include "BattleFieldHandler.h"
  33. #include "ObstacleHandler.h"
  34. VCMI_LIB_NAMESPACE_BEGIN
  35. const HeroTypeID HeroTypeID::NONE = HeroTypeID(-1);
  36. const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER = SlotID(-2);
  37. const SlotID SlotID::SUMMONED_SLOT_PLACEHOLDER = SlotID(-3);
  38. const SlotID SlotID::WAR_MACHINES_SLOT = SlotID(-4);
  39. const SlotID SlotID::ARROW_TOWERS_SLOT = SlotID(-5);
  40. const PlayerColor PlayerColor::SPECTATOR = PlayerColor(252);
  41. const PlayerColor PlayerColor::CANNOT_DETERMINE = PlayerColor(253);
  42. const PlayerColor PlayerColor::UNFLAGGABLE = PlayerColor(254);
  43. const PlayerColor PlayerColor::NEUTRAL = PlayerColor(255);
  44. const PlayerColor PlayerColor::PLAYER_LIMIT = PlayerColor(PLAYER_LIMIT_I);
  45. const TeamID TeamID::NO_TEAM = TeamID(255);
  46. namespace GameConstants
  47. {
  48. #ifdef VCMI_NO_EXTRA_VERSION
  49. const std::string VCMI_VERSION = "VCMI " VCMI_VERSION_STRING;
  50. #else
  51. const std::string VCMI_VERSION = "VCMI " VCMI_VERSION_STRING "." + std::string{GIT_SHA1};
  52. #endif
  53. }
  54. si32 HeroTypeID::decode(const std::string & identifier)
  55. {
  56. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "hero", identifier);
  57. if(rawId)
  58. return rawId.value();
  59. else
  60. return -1;
  61. }
  62. std::string HeroTypeID::encode(const si32 index)
  63. {
  64. return VLC->heroTypes()->getByIndex(index)->getJsonKey();
  65. }
  66. const CArtifact * ArtifactID::toArtifact() const
  67. {
  68. return dynamic_cast<const CArtifact*>(toArtifact(VLC->artifacts()));
  69. }
  70. const Artifact * ArtifactID::toArtifact(const ArtifactService * service) const
  71. {
  72. return service->getById(*this);
  73. }
  74. si32 ArtifactID::decode(const std::string & identifier)
  75. {
  76. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), "artifact", identifier);
  77. if(rawId)
  78. return rawId.value();
  79. else
  80. return -1;
  81. }
  82. std::string ArtifactID::encode(const si32 index)
  83. {
  84. return VLC->artifacts()->getByIndex(index)->getJsonKey();
  85. }
  86. const CCreature * CreatureID::toCreature() const
  87. {
  88. return VLC->creh->objects.at(*this);
  89. }
  90. const Creature * CreatureID::toCreature(const CreatureService * creatures) const
  91. {
  92. return creatures->getById(*this);
  93. }
  94. si32 CreatureID::decode(const std::string & identifier)
  95. {
  96. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), "creature", identifier);
  97. if(rawId)
  98. return rawId.value();
  99. else
  100. return -1;
  101. }
  102. std::string CreatureID::encode(const si32 index)
  103. {
  104. return VLC->creatures()->getById(CreatureID(index))->getJsonKey();
  105. }
  106. const CSpell * SpellID::toSpell() const
  107. {
  108. if(num < 0 || num >= VLC->spellh->objects.size())
  109. {
  110. logGlobal->error("Unable to get spell of invalid ID %d", static_cast<int>(num));
  111. return nullptr;
  112. }
  113. return VLC->spellh->objects[*this];
  114. }
  115. const spells::Spell * SpellID::toSpell(const spells::Service * service) const
  116. {
  117. return service->getById(*this);
  118. }
  119. si32 SpellID::decode(const std::string & identifier)
  120. {
  121. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), "spell", identifier);
  122. if(rawId)
  123. return rawId.value();
  124. else
  125. return -1;
  126. }
  127. std::string SpellID::encode(const si32 index)
  128. {
  129. return VLC->spells()->getByIndex(index)->getJsonKey();
  130. }
  131. bool PlayerColor::isValidPlayer() const
  132. {
  133. return num < PLAYER_LIMIT_I;
  134. }
  135. bool PlayerColor::isSpectator() const
  136. {
  137. return num == 252;
  138. }
  139. std::string PlayerColor::getStr(bool L10n) const
  140. {
  141. std::string ret = "unnamed";
  142. if(isValidPlayer())
  143. {
  144. if(L10n)
  145. ret = VLC->generaltexth->colors[num];
  146. else
  147. ret = GameConstants::PLAYER_COLOR_NAMES[num];
  148. }
  149. else if(L10n)
  150. {
  151. ret = VLC->generaltexth->allTexts[508];
  152. ret[0] = std::tolower(ret[0]);
  153. }
  154. return ret;
  155. }
  156. std::string PlayerColor::getStrCap(bool L10n) const
  157. {
  158. std::string ret = getStr(L10n);
  159. ret[0] = std::toupper(ret[0]);
  160. return ret;
  161. }
  162. const FactionID FactionID::NONE = FactionID(-2);
  163. const FactionID FactionID::DEFAULT = FactionID(-1);
  164. const FactionID FactionID::CASTLE = FactionID(0);
  165. const FactionID FactionID::RAMPART = FactionID(1);
  166. const FactionID FactionID::TOWER = FactionID(2);
  167. const FactionID FactionID::INFERNO = FactionID(3);
  168. const FactionID FactionID::NECROPOLIS = FactionID(4);
  169. const FactionID FactionID::DUNGEON = FactionID(5);
  170. const FactionID FactionID::STRONGHOLD = FactionID(6);
  171. const FactionID FactionID::FORTRESS = FactionID(7);
  172. const FactionID FactionID::CONFLUX = FactionID(8);
  173. const FactionID FactionID::NEUTRAL = FactionID(9);
  174. si32 FactionID::decode(const std::string & identifier)
  175. {
  176. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), entityType(), identifier);
  177. if(rawId)
  178. return rawId.value();
  179. else
  180. return FactionID::DEFAULT;
  181. }
  182. std::string FactionID::encode(const si32 index)
  183. {
  184. return VLC->factions()->getByIndex(index)->getJsonKey();
  185. }
  186. std::string FactionID::entityType()
  187. {
  188. return "faction";
  189. }
  190. si32 TerrainID::decode(const std::string & identifier)
  191. {
  192. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeGame(), entityType(), identifier);
  193. if(rawId)
  194. return rawId.value();
  195. else
  196. return static_cast<si32>(ETerrainId::NONE);
  197. }
  198. std::string TerrainID::encode(const si32 index)
  199. {
  200. return VLC->terrainTypeHandler->getByIndex(index)->getJsonKey();
  201. }
  202. std::string TerrainID::entityType()
  203. {
  204. return "terrain";
  205. }
  206. std::ostream & operator<<(std::ostream & os, const EActionType actionType)
  207. {
  208. static const std::map<EActionType, std::string> actionTypeToString =
  209. {
  210. {EActionType::END_TACTIC_PHASE, "End tactic phase"},
  211. {EActionType::INVALID, "Invalid"},
  212. {EActionType::NO_ACTION, "No action"},
  213. {EActionType::HERO_SPELL, "Hero spell"},
  214. {EActionType::WALK, "Walk"},
  215. {EActionType::DEFEND, "Defend"},
  216. {EActionType::RETREAT, "Retreat"},
  217. {EActionType::SURRENDER, "Surrender"},
  218. {EActionType::WALK_AND_ATTACK, "Walk and attack"},
  219. {EActionType::SHOOT, "Shoot"},
  220. {EActionType::WAIT, "Wait"},
  221. {EActionType::CATAPULT, "Catapult"},
  222. {EActionType::MONSTER_SPELL, "Monster spell"},
  223. {EActionType::BAD_MORALE, "Bad morale"},
  224. {EActionType::STACK_HEAL, "Stack heal"},
  225. };
  226. auto it = actionTypeToString.find(actionType);
  227. if (it == actionTypeToString.end()) return os << "<Unknown type>";
  228. else return os << it->second;
  229. }
  230. std::ostream & operator<<(std::ostream & os, const EPathfindingLayer & pathfindingLayer)
  231. {
  232. static const std::map<EPathfindingLayer::EEPathfindingLayer, std::string> pathfinderLayerToString
  233. {
  234. #define DEFINE_ELEMENT(element) {EPathfindingLayer::element, #element}
  235. DEFINE_ELEMENT(WRONG),
  236. DEFINE_ELEMENT(AUTO),
  237. DEFINE_ELEMENT(LAND),
  238. DEFINE_ELEMENT(SAIL),
  239. DEFINE_ELEMENT(WATER),
  240. DEFINE_ELEMENT(AIR),
  241. DEFINE_ELEMENT(NUM_LAYERS)
  242. #undef DEFINE_ELEMENT
  243. };
  244. auto it = pathfinderLayerToString.find(pathfindingLayer.num);
  245. if (it == pathfinderLayerToString.end()) return os << "<Unknown type>";
  246. else return os << it->second;
  247. }
  248. const BattleField BattleField::NONE;
  249. bool operator==(const BattleField & l, const BattleField & r)
  250. {
  251. return l.num == r.num;
  252. }
  253. bool operator!=(const BattleField & l, const BattleField & r)
  254. {
  255. return l.num != r.num;
  256. }
  257. bool operator<(const BattleField & l, const BattleField & r)
  258. {
  259. return l.num < r.num;
  260. }
  261. BattleField::operator std::string() const
  262. {
  263. return getInfo()->identifier;
  264. }
  265. const BattleFieldInfo * BattleField::getInfo() const
  266. {
  267. return VLC->battlefields()->getById(*this);
  268. }
  269. BattleField BattleField::fromString(const std::string & identifier)
  270. {
  271. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeBuiltin(), "battlefield", identifier);
  272. if(rawId)
  273. return BattleField(rawId.value());
  274. else
  275. return BattleField::NONE;
  276. }
  277. const ObstacleInfo * Obstacle::getInfo() const
  278. {
  279. return VLC->obstacles()->getById(*this);
  280. }
  281. Obstacle::operator std::string() const
  282. {
  283. return getInfo()->identifier;
  284. }
  285. Obstacle Obstacle::fromString(const std::string & identifier)
  286. {
  287. auto rawId = VLC->modh->identifiers.getIdentifier(CModHandler::scopeBuiltin(), "obstacle", identifier);
  288. if(rawId)
  289. return Obstacle(rawId.value());
  290. else
  291. return Obstacle(-1);
  292. }
  293. VCMI_LIB_NAMESPACE_END