GameConstants.cpp 5.2 KB

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