GameConstants.cpp 4.6 KB

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