CMapHeader.h 9.6 KB

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