GameConstants.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER = SlotID(-2);
  18. const PlayerColor PlayerColor::CANNOT_DETERMINE = PlayerColor(253);
  19. const PlayerColor PlayerColor::UNFLAGGABLE = PlayerColor(254);
  20. const PlayerColor PlayerColor::NEUTRAL = PlayerColor(255);
  21. const PlayerColor PlayerColor::PLAYER_LIMIT = PlayerColor(PLAYER_LIMIT_I);
  22. const TeamID TeamID::NO_TEAM = TeamID(255);
  23. #define ID_LIKE_OPERATORS_INTERNAL(A, B, AN, BN) \
  24. bool operator==(const A & a, const B & b) \
  25. { \
  26. return AN == BN ; \
  27. } \
  28. bool operator!=(const A & a, const B & b) \
  29. { \
  30. return AN != BN ; \
  31. } \
  32. bool operator<(const A & a, const B & b) \
  33. { \
  34. return AN < BN ; \
  35. } \
  36. bool operator<=(const A & a, const B & b) \
  37. { \
  38. return AN <= BN ; \
  39. } \
  40. bool operator>(const A & a, const B & b) \
  41. { \
  42. return AN > BN ; \
  43. } \
  44. bool operator>=(const A & a, const B & b) \
  45. { \
  46. return AN >= BN ; \
  47. }
  48. #define ID_LIKE_OPERATORS(CLASS_NAME, ENUM_NAME) \
  49. ID_LIKE_OPERATORS_INTERNAL(CLASS_NAME, CLASS_NAME, a.num, b.num) \
  50. ID_LIKE_OPERATORS_INTERNAL(CLASS_NAME, ENUM_NAME, a.num, b) \
  51. ID_LIKE_OPERATORS_INTERNAL(ENUM_NAME, CLASS_NAME, a, b.num)
  52. ID_LIKE_OPERATORS(SecondarySkill, SecondarySkill::ESecondarySkill)
  53. ID_LIKE_OPERATORS(Obj, Obj::EObj)
  54. ID_LIKE_OPERATORS(ETerrainType, ETerrainType::EETerrainType)
  55. ID_LIKE_OPERATORS(ArtifactID, ArtifactID::EArtifactID)
  56. ID_LIKE_OPERATORS(ArtifactPosition, ArtifactPosition::EArtifactPosition)
  57. ID_LIKE_OPERATORS(CreatureID, CreatureID::ECreatureID)
  58. ID_LIKE_OPERATORS(SpellID, SpellID::ESpellID)
  59. ID_LIKE_OPERATORS(BuildingID, BuildingID::EBuildingID)
  60. ID_LIKE_OPERATORS(BFieldType, BFieldType::EBFieldType)
  61. CArtifact * ArtifactID::toArtifact() const
  62. {
  63. return VLC->arth->artifacts[*this];
  64. }
  65. CCreature * CreatureID::toCreature() const
  66. {
  67. return VLC->creh->creatures[*this];
  68. }
  69. CSpell * SpellID::toSpell() const
  70. {
  71. if(num < 0 || num >= VLC->spellh->objects.size())
  72. {
  73. logGlobal->errorStream() << "Unable to get spell of invalid ID " << int(num);
  74. return nullptr;
  75. }
  76. return VLC->spellh->objects[*this];
  77. }
  78. //template std::ostream & operator << <ArtifactInstanceID>(std::ostream & os, BaseForID<ArtifactInstanceID> id);
  79. //template std::ostream & operator << <ObjectInstanceID>(std::ostream & os, BaseForID<ObjectInstanceID> id);
  80. bool PlayerColor::isValidPlayer() const
  81. {
  82. return num < PLAYER_LIMIT_I;
  83. }
  84. std::ostream & operator<<(std::ostream & os, const Battle::ActionType actionType)
  85. {
  86. static const std::map<Battle::ActionType, std::string> actionTypeToString =
  87. {
  88. {Battle::END_TACTIC_PHASE, "End tactic phase"},
  89. {Battle::INVALID, "Invalid"},
  90. {Battle::NO_ACTION, "No action"},
  91. {Battle::HERO_SPELL, "Hero spell"},
  92. {Battle::WALK, "Walk"},
  93. {Battle::DEFEND, "Defend"},
  94. {Battle::RETREAT, "Retreat"},
  95. {Battle::SURRENDER, "Surrender"},
  96. {Battle::WALK_AND_ATTACK, "Walk and attack"},
  97. {Battle::SHOOT, "Shoot"},
  98. {Battle::WAIT, "Wait"},
  99. {Battle::CATAPULT, "Catapult"},
  100. {Battle::MONSTER_SPELL, "Monster spell"},
  101. {Battle::BAD_MORALE, "Bad morale"},
  102. {Battle::STACK_HEAL, "Stack heal"},
  103. {Battle::DAEMON_SUMMONING, "Daemon summoning"}
  104. };
  105. auto it = actionTypeToString.find(actionType);
  106. if (it == actionTypeToString.end()) return os << "<Unknown type>";
  107. else return os << it->second;
  108. }
  109. std::ostream & operator<<(std::ostream & os, const ETerrainType actionType)
  110. {
  111. static const std::map<ETerrainType::EETerrainType, std::string> terrainTypeToString =
  112. {
  113. #define DEFINE_ELEMENT(element) {ETerrainType::element, #element}
  114. DEFINE_ELEMENT(WRONG),
  115. DEFINE_ELEMENT(BORDER),
  116. DEFINE_ELEMENT(DIRT),
  117. DEFINE_ELEMENT(SAND),
  118. DEFINE_ELEMENT(GRASS),
  119. DEFINE_ELEMENT(SNOW),
  120. DEFINE_ELEMENT(SWAMP),
  121. DEFINE_ELEMENT(ROUGH),
  122. DEFINE_ELEMENT(SUBTERRANEAN),
  123. DEFINE_ELEMENT(LAVA),
  124. DEFINE_ELEMENT(WATER),
  125. DEFINE_ELEMENT(ROCK)
  126. };
  127. auto it = terrainTypeToString.find(actionType.num);
  128. if (it == terrainTypeToString.end()) return os << "<Unknown type>";
  129. else return os << it->second;
  130. }
  131. std::string ETerrainType::toString() const
  132. {
  133. std::stringstream ss;
  134. ss << *this;
  135. return ss.str();
  136. }