GameConstants.cpp 8.3 KB

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