GameConstants.cpp 4.9 KB

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