2
0

CMapHeader.h 8.4 KB

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