2
0

GameConstants.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/Artifact.h>
  16. #include <vcmi/ArtifactService.h>
  17. #include <vcmi/Faction.h>
  18. #include <vcmi/FactionService.h>
  19. #include <vcmi/HeroType.h>
  20. #include <vcmi/HeroTypeService.h>
  21. #include <vcmi/spells/Spell.h>
  22. #include <vcmi/spells/Service.h>
  23. #include "VCMI_Lib.h"
  24. #include "mapObjects/CObjectClassesHandler.h"//todo: remove
  25. #include "CArtHandler.h"//todo: remove
  26. #include "CCreatureHandler.h"//todo: remove
  27. #include "spells/CSpellHandler.h" //todo: remove
  28. #include "CSkillHandler.h"//todo: remove
  29. #include "StringConstants.h"
  30. #include "CGeneralTextHandler.h"
  31. #include "CModHandler.h"//todo: remove
  32. #include "BattleFieldHandler.h"
  33. const SlotID SlotID::COMMANDER_SLOT_PLACEHOLDER = SlotID(-2);
  34. const SlotID SlotID::SUMMONED_SLOT_PLACEHOLDER = SlotID(-3);
  35. const SlotID SlotID::WAR_MACHINES_SLOT = SlotID(-4);
  36. const SlotID SlotID::ARROW_TOWERS_SLOT = SlotID(-5);
  37. const PlayerColor PlayerColor::SPECTATOR = PlayerColor(252);
  38. const PlayerColor PlayerColor::CANNOT_DETERMINE = PlayerColor(253);
  39. const PlayerColor PlayerColor::UNFLAGGABLE = PlayerColor(254);
  40. const PlayerColor PlayerColor::NEUTRAL = PlayerColor(255);
  41. const PlayerColor PlayerColor::PLAYER_LIMIT = PlayerColor(PLAYER_LIMIT_I);
  42. const TeamID TeamID::NO_TEAM = TeamID(255);
  43. namespace GameConstants
  44. {
  45. #ifdef VCMI_NO_EXTRA_VERSION
  46. const std::string VCMI_VERSION = std::string("VCMI 1.0.0");
  47. #else
  48. const std::string VCMI_VERSION = std::string("VCMI 1.0.0.") + GIT_SHA1;
  49. #endif
  50. }
  51. si32 HeroTypeID::decode(const std::string & identifier)
  52. {
  53. auto rawId = VLC->modh->identifiers.getIdentifier("core", "hero", identifier);
  54. if(rawId)
  55. return rawId.get();
  56. else
  57. return -1;
  58. }
  59. std::string HeroTypeID::encode(const si32 index)
  60. {
  61. return VLC->heroTypes()->getByIndex(index)->getJsonKey();
  62. }
  63. const CArtifact * ArtifactID::toArtifact() const
  64. {
  65. return VLC->arth->objects.at(*this);
  66. }
  67. const Artifact * ArtifactID::toArtifact(const ArtifactService * service) const
  68. {
  69. return service->getById(*this);
  70. }
  71. si32 ArtifactID::decode(const std::string & identifier)
  72. {
  73. auto rawId = VLC->modh->identifiers.getIdentifier("core", "artifact", identifier);
  74. if(rawId)
  75. return rawId.get();
  76. else
  77. return -1;
  78. }
  79. std::string ArtifactID::encode(const si32 index)
  80. {
  81. return VLC->artifacts()->getByIndex(index)->getJsonKey();
  82. }
  83. const CCreature * CreatureID::toCreature() const
  84. {
  85. return VLC->creh->objects.at(*this);
  86. }
  87. const Creature * CreatureID::toCreature(const CreatureService * creatures) const
  88. {
  89. return creatures->getById(*this);
  90. }
  91. si32 CreatureID::decode(const std::string & identifier)
  92. {
  93. auto rawId = VLC->modh->identifiers.getIdentifier("core", "creature", identifier);
  94. if(rawId)
  95. return rawId.get();
  96. else
  97. return -1;
  98. }
  99. std::string CreatureID::encode(const si32 index)
  100. {
  101. return VLC->creatures()->getById(CreatureID(index))->getJsonKey();
  102. }
  103. const CSpell * SpellID::toSpell() const
  104. {
  105. if(num < 0 || num >= VLC->spellh->objects.size())
  106. {
  107. logGlobal->error("Unable to get spell of invalid ID %d", int(num));
  108. return nullptr;
  109. }
  110. return VLC->spellh->objects[*this];
  111. }
  112. const spells::Spell * SpellID::toSpell(const spells::Service * service) const
  113. {
  114. return service->getById(*this);
  115. }
  116. si32 SpellID::decode(const std::string & identifier)
  117. {
  118. auto rawId = VLC->modh->identifiers.getIdentifier("core", "spell", identifier);
  119. if(rawId)
  120. return rawId.get();
  121. else
  122. return -1;
  123. }
  124. std::string SpellID::encode(const si32 index)
  125. {
  126. return VLC->spells()->getByIndex(index)->getJsonKey();
  127. }
  128. bool PlayerColor::isValidPlayer() const
  129. {
  130. return num < PLAYER_LIMIT_I;
  131. }
  132. bool PlayerColor::isSpectator() const
  133. {
  134. return num == 252;
  135. }
  136. std::string PlayerColor::getStr(bool L10n) const
  137. {
  138. std::string ret = "unnamed";
  139. if(isValidPlayer())
  140. {
  141. if(L10n)
  142. ret = VLC->generaltexth->colors[num];
  143. else
  144. ret = GameConstants::PLAYER_COLOR_NAMES[num];
  145. }
  146. else if(L10n)
  147. {
  148. ret = VLC->generaltexth->allTexts[508];
  149. ret[0] = std::tolower(ret[0]);
  150. }
  151. return ret;
  152. }
  153. std::string PlayerColor::getStrCap(bool L10n) const
  154. {
  155. std::string ret = getStr(L10n);
  156. ret[0] = std::toupper(ret[0]);
  157. return ret;
  158. }
  159. const FactionID FactionID::ANY = FactionID(-1);
  160. const FactionID FactionID::CASTLE = FactionID(0);
  161. const FactionID FactionID::RAMPART = FactionID(1);
  162. const FactionID FactionID::TOWER = FactionID(2);
  163. const FactionID FactionID::INFERNO = FactionID(3);
  164. const FactionID FactionID::NECROPOLIS = FactionID(4);
  165. const FactionID FactionID::DUNGEON = FactionID(5);
  166. const FactionID FactionID::STRONGHOLD = FactionID(6);
  167. const FactionID FactionID::FORTRESS = FactionID(7);
  168. const FactionID FactionID::CONFLUX = FactionID(8);
  169. const FactionID FactionID::NEUTRAL = FactionID(9);
  170. si32 FactionID::decode(const std::string & identifier)
  171. {
  172. auto rawId = VLC->modh->identifiers.getIdentifier("core", "faction", identifier);
  173. if(rawId)
  174. return rawId.get();
  175. else
  176. return -1;
  177. }
  178. std::string FactionID::encode(const si32 index)
  179. {
  180. return VLC->factions()->getByIndex(index)->getJsonKey();
  181. }
  182. std::ostream & operator<<(std::ostream & os, const EActionType actionType)
  183. {
  184. static const std::map<EActionType, std::string> actionTypeToString =
  185. {
  186. {EActionType::END_TACTIC_PHASE, "End tactic phase"},
  187. {EActionType::INVALID, "Invalid"},
  188. {EActionType::NO_ACTION, "No action"},
  189. {EActionType::HERO_SPELL, "Hero spell"},
  190. {EActionType::WALK, "Walk"},
  191. {EActionType::DEFEND, "Defend"},
  192. {EActionType::RETREAT, "Retreat"},
  193. {EActionType::SURRENDER, "Surrender"},
  194. {EActionType::WALK_AND_ATTACK, "Walk and attack"},
  195. {EActionType::SHOOT, "Shoot"},
  196. {EActionType::WAIT, "Wait"},
  197. {EActionType::CATAPULT, "Catapult"},
  198. {EActionType::MONSTER_SPELL, "Monster spell"},
  199. {EActionType::BAD_MORALE, "Bad morale"},
  200. {EActionType::STACK_HEAL, "Stack heal"},
  201. {EActionType::DAEMON_SUMMONING, "Daemon summoning"}
  202. };
  203. auto it = actionTypeToString.find(actionType);
  204. if (it == actionTypeToString.end()) return os << "<Unknown type>";
  205. else return os << it->second;
  206. }
  207. std::ostream & operator<<(std::ostream & os, const EPathfindingLayer pathfindingLayer)
  208. {
  209. static const std::map<EPathfindingLayer::EEPathfindingLayer, std::string> pathfinderLayerToString
  210. {
  211. #define DEFINE_ELEMENT(element) {EPathfindingLayer::element, #element}
  212. DEFINE_ELEMENT(WRONG),
  213. DEFINE_ELEMENT(AUTO),
  214. DEFINE_ELEMENT(LAND),
  215. DEFINE_ELEMENT(SAIL),
  216. DEFINE_ELEMENT(WATER),
  217. DEFINE_ELEMENT(AIR),
  218. DEFINE_ELEMENT(NUM_LAYERS)
  219. #undef DEFINE_ELEMENT
  220. };
  221. auto it = pathfinderLayerToString.find(pathfindingLayer.num);
  222. if (it == pathfinderLayerToString.end()) return os << "<Unknown type>";
  223. else return os << it->second;
  224. }
  225. const BattleField BattleField::NONE;
  226. bool operator==(const BattleField & l, const BattleField & r)
  227. {
  228. return l.num == r.num;
  229. }
  230. bool operator!=(const BattleField & l, const BattleField & r)
  231. {
  232. return l.num != r.num;
  233. }
  234. bool operator<(const BattleField & l, const BattleField & r)
  235. {
  236. return l.num < r.num;
  237. }
  238. BattleField::operator std::string() const
  239. {
  240. return getInfo()->identifier;
  241. }
  242. const BattleFieldInfo * BattleField::getInfo() const
  243. {
  244. return VLC->battlefields()->getById(*this);
  245. }
  246. BattleField BattleField::fromString(std::string identifier)
  247. {
  248. auto rawId = VLC->modh->identifiers.getIdentifier("core", "battlefield", identifier);
  249. if(rawId)
  250. return BattleField(rawId.get());
  251. else
  252. return BattleField::NONE;
  253. }