GameConstants.cpp 8.3 KB

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