CMapHeader.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * CMapHeader.h, 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. #pragma once
  11. #include "../constants/EntityIdentifiers.h"
  12. #include "../constants/Enumerations.h"
  13. #include "../constants/VariantIdentifier.h"
  14. #include "../modding/ModVerificationInfo.h"
  15. #include "../serializer/Serializeable.h"
  16. #include "../LogicalExpression.h"
  17. #include "../int3.h"
  18. #include "../MetaString.h"
  19. #include "../CGeneralTextHandler.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. class CGObjectInstance;
  22. enum class EMapFormat : uint8_t;
  23. /// The hero name struct consists of the hero id and the hero name.
  24. struct DLL_LINKAGE SHeroName
  25. {
  26. SHeroName();
  27. HeroTypeID heroId;
  28. std::string heroName;
  29. template <typename Handler>
  30. void serialize(Handler & h)
  31. {
  32. h & heroId;
  33. h & heroName;
  34. }
  35. };
  36. /// The player info constains data about which factions are allowed, AI tactical settings,
  37. /// the main hero name, where to generate the hero, whether the faction should be selected randomly,...
  38. struct DLL_LINKAGE PlayerInfo
  39. {
  40. PlayerInfo();
  41. /// Gets the default faction id or -1 for a random faction.
  42. FactionID defaultCastle() const;
  43. /// Gets the default hero id or -1 for a random hero.
  44. HeroTypeID defaultHero() const;
  45. bool canAnyonePlay() const;
  46. bool hasCustomMainHero() const;
  47. bool canHumanPlay;
  48. bool canComputerPlay;
  49. EAiTactic aiTactic; /// The default value is EAiTactic::RANDOM.
  50. std::set<FactionID> allowedFactions;
  51. bool isFactionRandom;
  52. ///main hero instance (VCMI maps only)
  53. std::string mainHeroInstance;
  54. /// Player has a random main hero
  55. bool hasRandomHero;
  56. /// The default value is -1.
  57. HeroTypeID mainCustomHeroPortrait;
  58. std::string mainCustomHeroNameTextId;
  59. /// ID of custom hero (only if portrait and hero name are set, otherwise unpredicted value), -1 if none (not always -1)
  60. HeroTypeID mainCustomHeroId;
  61. std::vector<SHeroName> heroesNames; /// list of placed heroes on the map
  62. bool hasMainTown; /// The default value is false.
  63. bool generateHeroAtMainTown; /// The default value is false.
  64. int3 posOfMainTown;
  65. TeamID team; /// The default value NO_TEAM
  66. template <typename Handler>
  67. void serialize(Handler & h)
  68. {
  69. h & hasRandomHero;
  70. h & mainCustomHeroId;
  71. h & canHumanPlay;
  72. h & canComputerPlay;
  73. h & aiTactic;
  74. h & allowedFactions;
  75. h & isFactionRandom;
  76. h & mainCustomHeroPortrait;
  77. h & mainCustomHeroNameTextId;
  78. h & heroesNames;
  79. h & hasMainTown;
  80. h & generateHeroAtMainTown;
  81. h & posOfMainTown;
  82. h & team;
  83. h & mainHeroInstance;
  84. }
  85. };
  86. /// The loss condition describes the condition to lose the game. (e.g. lose all own heroes/castles)
  87. struct DLL_LINKAGE EventCondition
  88. {
  89. enum EWinLoseType {
  90. HAVE_ARTIFACT, // type - required artifact
  91. HAVE_CREATURES, // type - creatures to collect, value - amount to collect
  92. HAVE_RESOURCES, // type - resource ID, value - amount to collect
  93. HAVE_BUILDING, // position - town, optional, type - building to build
  94. CONTROL, // position - position of object, optional, type - type of object
  95. DESTROY, // position - position of object, optional, type - type of object
  96. TRANSPORT, // position - where artifact should be transported, type - type of artifact
  97. DAYS_PASSED, // value - number of days from start of the game
  98. IS_HUMAN, // value - 0 = player is AI, 1 = player is human
  99. DAYS_WITHOUT_TOWN, // value - how long player can live without town, 0=instakill
  100. STANDARD_WIN, // normal defeat all enemies condition
  101. CONST_VALUE, // condition that always evaluates to "value" (0 = false, 1 = true)
  102. };
  103. using TargetTypeID = VariantIdentifier<ArtifactID, CreatureID, GameResID, BuildingID, MapObjectID>;
  104. EventCondition(EWinLoseType condition = STANDARD_WIN);
  105. EventCondition(EWinLoseType condition, si32 value, TargetTypeID objectType, const int3 & position = int3(-1, -1, -1));
  106. ObjectInstanceID objectID; // object that was at specified position or with instance name on start
  107. si32 value;
  108. TargetTypeID objectType;
  109. std::string objectInstanceName;
  110. int3 position;
  111. EWinLoseType condition;
  112. template <typename Handler>
  113. void serialize(Handler & h)
  114. {
  115. h & objectID;
  116. h & value;
  117. h & objectType;
  118. h & position;
  119. h & condition;
  120. h & objectInstanceName;
  121. }
  122. };
  123. using EventExpression = LogicalExpression<EventCondition>;
  124. struct DLL_LINKAGE EventEffect
  125. {
  126. enum EType
  127. {
  128. VICTORY,
  129. DEFEAT
  130. };
  131. /// effect type, using EType enum
  132. si8 type;
  133. /// message that will be sent to other players
  134. MetaString toOtherMessage;
  135. template <typename Handler>
  136. void serialize(Handler & h)
  137. {
  138. h & type;
  139. h & toOtherMessage;
  140. }
  141. };
  142. struct DLL_LINKAGE TriggeredEvent
  143. {
  144. /// base condition that must be evaluated
  145. EventExpression trigger;
  146. /// string identifier read from config file (e.g. captureKreelah)
  147. std::string identifier;
  148. /// string-description, for use in UI (capture town to win)
  149. MetaString description;
  150. /// Message that will be displayed when this event is triggered (You captured town. You won!)
  151. MetaString onFulfill;
  152. /// Effect of this event. TODO: refactor into something more flexible
  153. EventEffect effect;
  154. template <typename Handler>
  155. void serialize(Handler & h)
  156. {
  157. h & identifier;
  158. h & trigger;
  159. h & description;
  160. h & onFulfill;
  161. h & effect;
  162. }
  163. };
  164. enum class EMapDifficulty : uint8_t
  165. {
  166. EASY = 0,
  167. NORMAL = 1,
  168. HARD = 2,
  169. EXPERT = 3,
  170. IMPOSSIBLE = 4
  171. };
  172. /// The map header holds information about loss/victory condition,map format, version, players, height, width,...
  173. class DLL_LINKAGE CMapHeader: public Serializeable
  174. {
  175. void setupEvents();
  176. public:
  177. static constexpr int MAP_SIZE_SMALL = 36;
  178. static constexpr int MAP_SIZE_MIDDLE = 72;
  179. static constexpr int MAP_SIZE_LARGE = 108;
  180. static constexpr int MAP_SIZE_XLARGE = 144;
  181. static constexpr int MAP_SIZE_HUGE = 180;
  182. static constexpr int MAP_SIZE_XHUGE = 216;
  183. static constexpr int MAP_SIZE_GIANT = 252;
  184. CMapHeader();
  185. virtual ~CMapHeader();
  186. ui8 levels() const;
  187. EMapFormat version; /// The default value is EMapFormat::SOD.
  188. ModCompatibilityInfo mods; /// set of mods required to play a map
  189. si32 height; /// The default value is 72.
  190. si32 width; /// The default value is 72.
  191. bool twoLevel; /// The default value is true.
  192. MetaString name;
  193. MetaString description;
  194. EMapDifficulty difficulty;
  195. MetaString author;
  196. MetaString authorContact;
  197. MetaString mapVersion;
  198. std::time_t creationDateTime;
  199. /// Specifies the maximum level to reach for a hero. A value of 0 states that there is no
  200. /// maximum level for heroes. This is the default value.
  201. ui8 levelLimit;
  202. MetaString victoryMessage;
  203. MetaString defeatMessage;
  204. ui16 victoryIconIndex;
  205. ui16 defeatIconIndex;
  206. std::vector<PlayerInfo> players; /// The default size of the vector is PlayerColor::PLAYER_LIMIT.
  207. ui8 howManyTeams;
  208. std::set<HeroTypeID> allowedHeroes;
  209. std::set<HeroTypeID> reservedCampaignHeroes; /// Heroes that have placeholders in this map and are reserved for campaign
  210. bool areAnyPlayers; /// Unused. True if there are any playable players on the map.
  211. /// "main quests" of the map that describe victory and loss conditions
  212. std::vector<TriggeredEvent> triggeredEvents;
  213. /// translations for map to be transferred over network
  214. JsonNode translations;
  215. TextContainerRegistrable texts;
  216. void registerMapStrings();
  217. template <typename Handler>
  218. void serialize(Handler & h)
  219. {
  220. h & texts;
  221. h & version;
  222. h & mods;
  223. h & name;
  224. h & description;
  225. if (h.version >= Handler::Version::MAP_FORMAT_ADDITIONAL_INFOS)
  226. {
  227. h & author;
  228. h & authorContact;
  229. h & mapVersion;
  230. h & creationDateTime;
  231. }
  232. h & width;
  233. h & height;
  234. h & twoLevel;
  235. // FIXME: we should serialize enum's according to their underlying type
  236. // should be fixed when we are making breaking change to save compatibility
  237. static_assert(Handler::Version::MINIMAL < Handler::Version::RELEASE_143);
  238. uint8_t difficultyInteger = static_cast<uint8_t>(difficulty);
  239. h & difficultyInteger;
  240. difficulty = static_cast<EMapDifficulty>(difficultyInteger);
  241. h & levelLimit;
  242. h & areAnyPlayers;
  243. h & players;
  244. h & howManyTeams;
  245. h & allowedHeroes;
  246. h & reservedCampaignHeroes;
  247. //Do not serialize triggeredEvents in header as they can contain information about heroes and armies
  248. h & victoryMessage;
  249. h & victoryIconIndex;
  250. h & defeatMessage;
  251. h & defeatIconIndex;
  252. h & translations;
  253. if(!h.saving)
  254. registerMapStrings();
  255. }
  256. };
  257. /// wrapper functions to register string into the map and stores its translation
  258. std::string DLL_LINKAGE mapRegisterLocalizedString(const std::string & modContext, CMapHeader & mapHeader, const TextIdentifier & UID, const std::string & localized);
  259. std::string DLL_LINKAGE mapRegisterLocalizedString(const std::string & modContext, CMapHeader & mapHeader, const TextIdentifier & UID, const std::string & localized, const std::string & language);
  260. VCMI_LIB_NAMESPACE_END