GameConstants.cpp 7.8 KB

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