GameConstants.cpp 8.5 KB

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