CMapHeader.h 8.1 KB

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