CMapHeader.h 7.3 KB

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