GameConstants.cpp 7.8 KB

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