GameConstants.cpp 8.5 KB

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