GameConstants.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_Lib.h"
  16. #include "mapObjects/CObjectClassesHandler.h"
  17. #include "CArtHandler.h"
  18. #include "CCreatureHandler.h"
  19. #include "spells/CSpellHandler.h"
  20. #include "CSkillHandler.h"
  21. #include "StringConstants.h"
  22. #include "CGeneralTextHandler.h"
  23. #include "CModHandler.h"
  24. const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER = SlotID(-2);
  25. const SlotID SlotID::SUMMONED_SLOT_PLACEHOLDER = SlotID(-3);
  26. const SlotID SlotID::WAR_MACHINES_SLOT = SlotID(-4);
  27. const SlotID SlotID::ARROW_TOWERS_SLOT = SlotID(-5);
  28. const PlayerColor PlayerColor::SPECTATOR = PlayerColor(252);
  29. const PlayerColor PlayerColor::CANNOT_DETERMINE = PlayerColor(253);
  30. const PlayerColor PlayerColor::UNFLAGGABLE = PlayerColor(254);
  31. const PlayerColor PlayerColor::NEUTRAL = PlayerColor(255);
  32. const PlayerColor PlayerColor::PLAYER_LIMIT = PlayerColor(PLAYER_LIMIT_I);
  33. const TeamID TeamID::NO_TEAM = TeamID(255);
  34. namespace GameConstants
  35. {
  36. #ifdef VCMI_NO_EXTRA_VERSION
  37. const std::string VCMI_VERSION = std::string("VCMI 0.99");
  38. #else
  39. const std::string VCMI_VERSION = std::string("VCMI 0.99 ") + GIT_SHA1;
  40. #endif
  41. }
  42. const CArtifact * ArtifactID::toArtifact() const
  43. {
  44. return VLC->arth->artifacts.at(*this);
  45. }
  46. si32 ArtifactID::decode(const std::string & identifier)
  47. {
  48. auto rawId = VLC->modh->identifiers.getIdentifier("core", "artifact", identifier);
  49. if(rawId)
  50. return rawId.get();
  51. else
  52. return -1;
  53. }
  54. std::string ArtifactID::encode(const si32 index)
  55. {
  56. return VLC->arth->artifacts.at(index)->identifier;
  57. }
  58. const CCreature * CreatureID::toCreature() const
  59. {
  60. return VLC->creh->creatures.at(*this);
  61. }
  62. si32 CreatureID::decode(const std::string & identifier)
  63. {
  64. auto rawId = VLC->modh->identifiers.getIdentifier("core", "creature", identifier);
  65. if(rawId)
  66. return rawId.get();
  67. else
  68. return -1;
  69. }
  70. std::string CreatureID::encode(const si32 index)
  71. {
  72. return VLC->creh->creatures.at(index)->identifier;
  73. }
  74. const CSpell * SpellID::toSpell() const
  75. {
  76. if(num < 0 || num >= VLC->spellh->objects.size())
  77. {
  78. logGlobal->error("Unable to get spell of invalid ID %d", int(num));
  79. return nullptr;
  80. }
  81. return VLC->spellh->objects[*this];
  82. }
  83. si32 SpellID::decode(const std::string & identifier)
  84. {
  85. auto rawId = VLC->modh->identifiers.getIdentifier("core", "spell", identifier);
  86. if(rawId)
  87. return rawId.get();
  88. else
  89. return -1;
  90. }
  91. std::string SpellID::encode(const si32 index)
  92. {
  93. return VLC->spellh->objects.at(index)->identifier;
  94. }
  95. const CSkill * SecondarySkill::toSkill() const
  96. {
  97. return VLC->skillh->objects.at(*this);
  98. }
  99. //template std::ostream & operator << <ArtifactInstanceID>(std::ostream & os, BaseForID<ArtifactInstanceID> id);
  100. //template std::ostream & operator << <ObjectInstanceID>(std::ostream & os, BaseForID<ObjectInstanceID> id);
  101. bool PlayerColor::isValidPlayer() const
  102. {
  103. return num < PLAYER_LIMIT_I;
  104. }
  105. bool PlayerColor::isSpectator() const
  106. {
  107. return num == 252;
  108. }
  109. std::string PlayerColor::getStr(bool L10n) const
  110. {
  111. std::string ret = "unnamed";
  112. if(isValidPlayer())
  113. {
  114. if(L10n)
  115. ret = VLC->generaltexth->colors[num];
  116. else
  117. ret = GameConstants::PLAYER_COLOR_NAMES[num];
  118. }
  119. else if(L10n)
  120. {
  121. ret = VLC->generaltexth->allTexts[508];
  122. ret[0] = std::tolower(ret[0]);
  123. }
  124. return ret;
  125. }
  126. std::string PlayerColor::getStrCap(bool L10n) const
  127. {
  128. std::string ret = getStr(L10n);
  129. ret[0] = std::toupper(ret[0]);
  130. return ret;
  131. }
  132. std::ostream & operator<<(std::ostream & os, const EActionType actionType)
  133. {
  134. static const std::map<EActionType, std::string> actionTypeToString =
  135. {
  136. {EActionType::END_TACTIC_PHASE, "End tactic phase"},
  137. {EActionType::INVALID, "Invalid"},
  138. {EActionType::NO_ACTION, "No action"},
  139. {EActionType::HERO_SPELL, "Hero spell"},
  140. {EActionType::WALK, "Walk"},
  141. {EActionType::DEFEND, "Defend"},
  142. {EActionType::RETREAT, "Retreat"},
  143. {EActionType::SURRENDER, "Surrender"},
  144. {EActionType::WALK_AND_ATTACK, "Walk and attack"},
  145. {EActionType::SHOOT, "Shoot"},
  146. {EActionType::WAIT, "Wait"},
  147. {EActionType::CATAPULT, "Catapult"},
  148. {EActionType::MONSTER_SPELL, "Monster spell"},
  149. {EActionType::BAD_MORALE, "Bad morale"},
  150. {EActionType::STACK_HEAL, "Stack heal"},
  151. {EActionType::DAEMON_SUMMONING, "Daemon summoning"}
  152. };
  153. auto it = actionTypeToString.find(actionType);
  154. if (it == actionTypeToString.end()) return os << "<Unknown type>";
  155. else return os << it->second;
  156. }
  157. std::ostream & operator<<(std::ostream & os, const ETerrainType terrainType)
  158. {
  159. static const std::map<ETerrainType::EETerrainType, std::string> terrainTypeToString =
  160. {
  161. #define DEFINE_ELEMENT(element) {ETerrainType::element, #element}
  162. DEFINE_ELEMENT(WRONG),
  163. DEFINE_ELEMENT(BORDER),
  164. DEFINE_ELEMENT(DIRT),
  165. DEFINE_ELEMENT(SAND),
  166. DEFINE_ELEMENT(GRASS),
  167. DEFINE_ELEMENT(SNOW),
  168. DEFINE_ELEMENT(SWAMP),
  169. DEFINE_ELEMENT(ROUGH),
  170. DEFINE_ELEMENT(SUBTERRANEAN),
  171. DEFINE_ELEMENT(LAVA),
  172. DEFINE_ELEMENT(WATER),
  173. DEFINE_ELEMENT(ROCK)
  174. #undef DEFINE_ELEMENT
  175. };
  176. auto it = terrainTypeToString.find(terrainType.num);
  177. if (it == terrainTypeToString.end()) return os << "<Unknown type>";
  178. else return os << it->second;
  179. }
  180. std::string ETerrainType::toString() const
  181. {
  182. std::stringstream ss;
  183. ss << *this;
  184. return ss.str();
  185. }
  186. std::ostream & operator<<(std::ostream & os, const EPathfindingLayer pathfindingLayer)
  187. {
  188. static const std::map<EPathfindingLayer::EEPathfindingLayer, std::string> pathfinderLayerToString
  189. {
  190. #define DEFINE_ELEMENT(element) {EPathfindingLayer::element, #element}
  191. DEFINE_ELEMENT(WRONG),
  192. DEFINE_ELEMENT(AUTO),
  193. DEFINE_ELEMENT(LAND),
  194. DEFINE_ELEMENT(SAIL),
  195. DEFINE_ELEMENT(WATER),
  196. DEFINE_ELEMENT(AIR),
  197. DEFINE_ELEMENT(NUM_LAYERS)
  198. #undef DEFINE_ELEMENT
  199. };
  200. auto it = pathfinderLayerToString.find(pathfindingLayer.num);
  201. if (it == pathfinderLayerToString.end()) return os << "<Unknown type>";
  202. else return os << it->second;
  203. }