GameConstants.cpp 9.0 KB

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