CMapHeader.h 7.3 KB

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