CMapGenerator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * CMapGenerator.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 "../GameConstants.h"
  12. #include "../CRandomGenerator.h"
  13. class CMap;
  14. class CTerrainViewPatternConfig;
  15. class CMapEditManager;
  16. class JsonNode;
  17. namespace EWaterContent
  18. {
  19. enum EWaterContent
  20. {
  21. RANDOM = -1,
  22. NONE,
  23. NORMAL,
  24. ISLANDS
  25. };
  26. }
  27. namespace EMonsterStrength
  28. {
  29. enum EMonsterStrength
  30. {
  31. RANDOM = -1,
  32. WEAK,
  33. NORMAL,
  34. STRONG
  35. };
  36. }
  37. namespace EPlayerType
  38. {
  39. enum EPlayerType
  40. {
  41. HUMAN,
  42. AI,
  43. COMP_ONLY
  44. };
  45. }
  46. /// The map gen options class holds values about general map generation settings
  47. /// e.g. the size of the map, the count of players,...
  48. class DLL_LINKAGE CMapGenOptions
  49. {
  50. public:
  51. /// The player settings class maps the player color, starting town and human player flag.
  52. class DLL_LINKAGE CPlayerSettings
  53. {
  54. public:
  55. CPlayerSettings();
  56. /// The color of the player ranging from 0 to PlayerColor::PLAYER_LIMIT - 1.
  57. /// The default value is 0.
  58. PlayerColor getColor() const;
  59. void setColor(PlayerColor value);
  60. /// The starting town of the player ranging from 0 to town max count or RANDOM_TOWN.
  61. /// The default value is RANDOM_TOWN.
  62. si32 getStartingTown() const;
  63. void setStartingTown(si32 value);
  64. /// The default value is EPlayerType::AI.
  65. EPlayerType::EPlayerType getPlayerType() const;
  66. void setPlayerType(EPlayerType::EPlayerType value);
  67. /// Constant for a random town selection.
  68. static const si32 RANDOM_TOWN = -1;
  69. private:
  70. PlayerColor color;
  71. si32 startingTown;
  72. EPlayerType::EPlayerType playerType;
  73. public:
  74. template <typename Handler>
  75. void serialize(Handler & h, const int version)
  76. {
  77. h & color & startingTown & playerType;
  78. }
  79. };
  80. CMapGenOptions();
  81. si32 getWidth() const;
  82. void setWidth(si32 value);
  83. si32 getHeight() const;
  84. void setHeight(si32 value);
  85. bool getHasTwoLevels() const;
  86. void setHasTwoLevels(bool value);
  87. /// The count of the players ranging from 1 to PlayerColor::PLAYER_LIMIT or RANDOM_SIZE for random. If you call
  88. /// this method, all player settings are reset to default settings.
  89. si8 getPlayersCnt() const;
  90. void setPlayersCnt(si8 value);
  91. /// The count of the teams ranging from 0 to <players count - 1> or RANDOM_SIZE for random.
  92. si8 getTeamsCnt() const;
  93. void setTeamsCnt(si8 value);
  94. /// The count of the computer only players ranging from 0 to <PlayerColor::PLAYER_LIMIT - players count> or RANDOM_SIZE for random.
  95. /// If you call this method, all player settings are reset to default settings.
  96. si8 getCompOnlyPlayersCnt() const;
  97. void setCompOnlyPlayersCnt(si8 value);
  98. /// The count of the computer only teams ranging from 0 to <comp only players - 1> or RANDOM_SIZE for random.
  99. si8 getCompOnlyTeamsCnt() const;
  100. void setCompOnlyTeamsCnt(si8 value);
  101. EWaterContent::EWaterContent getWaterContent() const;
  102. void setWaterContent(EWaterContent::EWaterContent value);
  103. EMonsterStrength::EMonsterStrength getMonsterStrength() const;
  104. void setMonsterStrength(EMonsterStrength::EMonsterStrength value);
  105. /// The first player colors belong to standard players and the last player colors belong to comp only players.
  106. /// All standard players are by default of type EPlayerType::AI.
  107. const std::map<PlayerColor, CPlayerSettings> & getPlayersSettings() const;
  108. void setStartingTownForPlayer(PlayerColor color, si32 town);
  109. /// Sets a player type for a standard player. A standard player is the opposite of a computer only player. The
  110. /// values which can be chosen for the player type are EPlayerType::AI or EPlayerType::HUMAN. Calling this method
  111. /// has no effect for the map itself, but it adds some informational text for the map description.
  112. void setPlayerTypeForStandardPlayer(PlayerColor color, EPlayerType::EPlayerType playerType);
  113. /// Finalizes the options. All random sizes for various properties will be overwritten by numbers from
  114. /// a random number generator by keeping the options in a valid state.
  115. void finalize();
  116. void finalize(CRandomGenerator & gen);
  117. static const si8 RANDOM_SIZE = -1;
  118. private:
  119. void resetPlayersMap();
  120. int countHumanPlayers() const;
  121. PlayerColor getNextPlayerColor() const;
  122. si32 width, height;
  123. bool hasTwoLevels;
  124. si8 playersCnt, teamsCnt, compOnlyPlayersCnt, compOnlyTeamsCnt;
  125. EWaterContent::EWaterContent waterContent;
  126. EMonsterStrength::EMonsterStrength monsterStrength;
  127. std::map<PlayerColor, CPlayerSettings> players;
  128. public:
  129. template <typename Handler>
  130. void serialize(Handler & h, const int version)
  131. {
  132. //FIXME: Enum is not a fixed with data type. Add enum class to both enums
  133. // later. For now it is ok.
  134. h & width & height & hasTwoLevels & playersCnt & teamsCnt & compOnlyPlayersCnt;
  135. h & compOnlyTeamsCnt & waterContent & monsterStrength & players;
  136. }
  137. };
  138. /// The map generator creates a map randomly.
  139. class DLL_LINKAGE CMapGenerator
  140. {
  141. public:
  142. explicit CMapGenerator(const CMapGenOptions & mapGenOptions, int randomSeed = std::time(nullptr));
  143. ~CMapGenerator(); // required due to unique_ptr
  144. std::unique_ptr<CMap> generate();
  145. private:
  146. /// Generation methods
  147. std::string getMapDescription() const;
  148. void addPlayerInfo();
  149. void addHeaderInfo();
  150. void genTerrain();
  151. void genTowns();
  152. CMapGenOptions mapGenOptions;
  153. std::unique_ptr<CMap> map;
  154. CRandomGenerator gen;
  155. int randomSeed;
  156. CMapEditManager * editManager;
  157. };
  158. /* ---------------------------------------------------------------------------- */
  159. /* Implementation/Detail classes, Private API */
  160. /* ---------------------------------------------------------------------------- */
  161. namespace ETemplateZoneType
  162. {
  163. enum ETemplateZoneType
  164. {
  165. HUMAN_START,
  166. COMPUTER_START,
  167. TREASURE,
  168. JUNCTION
  169. };
  170. }
  171. /// The CTemplateZoneTowns holds info about towns in a template zone.
  172. class DLL_LINKAGE CTemplateZoneTowns
  173. {
  174. public:
  175. CTemplateZoneTowns();
  176. int getMinTowns() const; /// Default: 0
  177. void setMinTowns(int value);
  178. int getMinCastles() const; /// Default: 0
  179. void setMinCastles(int value);
  180. int getTownDensity() const; /// Default: 0
  181. void setTownDensity(int value);
  182. int getCastleDensity() const; /// Default: 0
  183. void setCastleDensity(int value);
  184. private:
  185. int minTowns, minCastles, townDensity, castleDensity;
  186. };
  187. typedef int TTemplateZoneId;
  188. /// The CTemplateZone describes a zone in a template.
  189. class DLL_LINKAGE CTemplateZone
  190. {
  191. public:
  192. CTemplateZone();
  193. TTemplateZoneId getId() const; /// Default: 0 = not set;
  194. void setId(TTemplateZoneId value);
  195. ETemplateZoneType::ETemplateZoneType getType() const; /// Default: ETemplateZoneType::HUMAN_START
  196. void setType(ETemplateZoneType::ETemplateZoneType value);
  197. int getBaseSize() const; /// Default: 0 = not set;
  198. void setBaseSize(int value);
  199. int getOwner() const; /// Default: 0 = not set;
  200. void setOwner(int value);
  201. const CTemplateZoneTowns & getPlayerTowns() const;
  202. void setPlayerTowns(const CTemplateZoneTowns & value);
  203. const CTemplateZoneTowns & getNeutralTowns() const;
  204. void setNeutralTowns(const CTemplateZoneTowns & value);
  205. bool getNeutralTownsAreSameType() const; /// Default: false
  206. void setNeutralTownsAreSameType(bool value);
  207. const std::set<TFaction> & getAllowedTownTypes() const;
  208. void setAllowedTownTypes(const std::set<TFaction> & value);
  209. bool getMatchTerrainToTown() const; /// Default: false
  210. void setMatchTerrainToTown(bool value);
  211. const std::set<ETerrainType> & getTerrainTypes() const;
  212. void setTerrainTypes(const std::set<ETerrainType> & value);
  213. private:
  214. TTemplateZoneId id;
  215. ETemplateZoneType::ETemplateZoneType type;
  216. int baseSize;
  217. int owner;
  218. CTemplateZoneTowns playerTowns, neutralTowns;
  219. bool neutralTownsAreSameType;
  220. std::set<TFaction> allowedTownTypes;
  221. bool matchTerrainToTown;
  222. std::set<ETerrainType> terrainTypes;
  223. };
  224. /// The CTemplateZoneConnection describes the connection between two zones.
  225. class DLL_LINKAGE CTemplateZoneConnection
  226. {
  227. public:
  228. CTemplateZoneConnection();
  229. TTemplateZoneId getZoneA() const; /// Default: 0 = not set;
  230. void setZoneA(TTemplateZoneId value);
  231. TTemplateZoneId getZoneB() const; /// Default: 0 = not set;
  232. void setZoneB(TTemplateZoneId value);
  233. int getGuardStrength() const; /// Default: 0
  234. void setGuardStrength(int value);
  235. private:
  236. TTemplateZoneId zoneA, zoneB;
  237. int guardStrength;
  238. };
  239. /// The CRandomMapTemplateSize describes the dimensions of the template.
  240. class CRandomMapTemplateSize
  241. {
  242. public:
  243. CRandomMapTemplateSize();
  244. CRandomMapTemplateSize(int width, int height, bool under);
  245. int getWidth() const; /// Default: CMapHeader::MAP_SIZE_MIDDLE
  246. void setWidth(int value);
  247. int getHeight() const; /// Default: CMapHeader::MAP_SIZE_MIDDLE
  248. void setHeight(int value);
  249. bool getUnder() const; /// Default: true
  250. void setUnder(bool value);
  251. private:
  252. int width, height;
  253. bool under;
  254. };
  255. /// The CRandomMapTemplate describes a random map template.
  256. class DLL_LINKAGE CRandomMapTemplate
  257. {
  258. public:
  259. CRandomMapTemplate();
  260. const std::string & getName() const;
  261. void setName(const std::string & value);
  262. const CRandomMapTemplateSize & getMinSize() const; /// Default: CMapHeader::MAP_SIZE_SMALL x CMapHeader::MAP_SIZE_SMALL x wo under
  263. void setMinSize(const CRandomMapTemplateSize & value);
  264. const CRandomMapTemplateSize & getMaxSize() const; /// Default: CMapHeader::MAP_SIZE_XLARGE x CMapHeader::MAP_SIZE_XLARGE x under
  265. void setMaxSize(const CRandomMapTemplateSize & value);
  266. int getMinHumanCnt() const; /// Default: 1
  267. void setMinHumanCnt(int value);
  268. int getMaxHumanCnt() const; /// Default: PlayerColor::PLAYER_LIMIT_I
  269. void setMaxHumanCnt(int value);
  270. int getMinTotalCnt() const; /// Default: 2
  271. void setMinTotalCnt(int value);
  272. int getMaxTotalCnt() const; /// Default: PlayerColor::PLAYER_LIMIT_I
  273. void setMaxTotalCnt(int value);
  274. const std::map<TTemplateZoneId, CTemplateZone> & getZones() const;
  275. void setZones(const std::map<TTemplateZoneId, CTemplateZone> & value);
  276. const std::list<CTemplateZoneConnection> & getConnections() const;
  277. void setConnections(const std::list<CTemplateZoneConnection> & value);
  278. private:
  279. std::string name;
  280. CRandomMapTemplateSize minSize, maxSize;
  281. int minHumanCnt, maxHumanCnt, minTotalCnt, maxTotalCnt;
  282. std::map<TTemplateZoneId, CTemplateZone> zones;
  283. std::list<CTemplateZoneConnection> connections;
  284. };
  285. /// The CRmTemplateLoader is a abstract base class for loading templates.
  286. class DLL_LINKAGE CRmTemplateLoader
  287. {
  288. public:
  289. virtual ~CRmTemplateLoader() { };
  290. virtual void loadTemplates() = 0;
  291. const std::map<std::string, CRandomMapTemplate> & getTemplates() const;
  292. protected:
  293. std::map<std::string, CRandomMapTemplate> templates;
  294. };
  295. /// The CJsonRmTemplateLoader loads templates from a JSON file.
  296. class DLL_LINKAGE CJsonRmTemplateLoader : public CRmTemplateLoader
  297. {
  298. public:
  299. void loadTemplates() override;
  300. private:
  301. CRandomMapTemplateSize parseMapTemplateSize(const std::string & text) const;
  302. CTemplateZoneTowns parseTemplateZoneTowns(const JsonNode & node) const;
  303. ETemplateZoneType::ETemplateZoneType getZoneType(const std::string & type) const;
  304. std::set<TFaction> getFactions(const std::vector<JsonNode> factionStrings) const;
  305. std::set<ETerrainType> parseTerrainTypes(const std::vector<JsonNode> terTypeStrings) const;
  306. };
  307. /// The CRandomMapTemplateStorage is a singleton object where templates are stored and which can be accessed from anywhere.
  308. class DLL_LINKAGE CRandomMapTemplateStorage
  309. {
  310. public:
  311. static CRandomMapTemplateStorage & get();
  312. const std::map<std::string, CRandomMapTemplate> & getTemplates() const;
  313. private:
  314. CRandomMapTemplateStorage();
  315. ~CRandomMapTemplateStorage();
  316. static boost::mutex smx;
  317. std::map<std::string, CRandomMapTemplate> templates; /// Key: Template name
  318. };