2
0

GameConstants.cpp 7.8 KB

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