GameConstants.cpp 8.1 KB

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