CMapHeader.h 8.2 KB

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