CMapGenerator.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. namespace EWaterContent
  17. {
  18. enum EWaterContent
  19. {
  20. RANDOM = -1,
  21. NONE,
  22. NORMAL,
  23. ISLANDS
  24. };
  25. }
  26. namespace EMonsterStrength
  27. {
  28. enum EMonsterStrength
  29. {
  30. RANDOM = -1,
  31. WEAK,
  32. NORMAL,
  33. STRONG
  34. };
  35. }
  36. namespace EPlayerType
  37. {
  38. enum EPlayerType
  39. {
  40. HUMAN,
  41. AI,
  42. COMP_ONLY
  43. };
  44. }
  45. /// The map gen options class holds values about general map generation settings
  46. /// e.g. the size of the map, the count of players,...
  47. class DLL_LINKAGE CMapGenOptions
  48. {
  49. public:
  50. /// The player settings class maps the player color, starting town and human player flag.
  51. class DLL_LINKAGE CPlayerSettings
  52. {
  53. public:
  54. CPlayerSettings();
  55. /// The color of the player ranging from 0 to PlayerColor::PLAYER_LIMIT - 1.
  56. /// The default value is 0.
  57. PlayerColor getColor() const;
  58. void setColor(PlayerColor value);
  59. /// The starting town of the player ranging from 0 to town max count or RANDOM_TOWN.
  60. /// The default value is RANDOM_TOWN.
  61. si32 getStartingTown() const;
  62. void setStartingTown(si32 value);
  63. /// The default value is EPlayerType::AI.
  64. EPlayerType::EPlayerType getPlayerType() const;
  65. void setPlayerType(EPlayerType::EPlayerType value);
  66. /// Constant for a random town selection.
  67. static const si32 RANDOM_TOWN = -1;
  68. private:
  69. PlayerColor color;
  70. si32 startingTown;
  71. EPlayerType::EPlayerType playerType;
  72. public:
  73. template <typename Handler>
  74. void serialize(Handler & h, const int version)
  75. {
  76. h & color & startingTown & playerType;
  77. }
  78. };
  79. CMapGenOptions();
  80. si32 getWidth() const;
  81. void setWidth(si32 value);
  82. si32 getHeight() const;
  83. void setHeight(si32 value);
  84. bool getHasTwoLevels() const;
  85. void setHasTwoLevels(bool value);
  86. /// The count of the players ranging from 1 to PlayerColor::PLAYER_LIMIT or RANDOM_SIZE for random. If you call
  87. /// this method, all player settings are reset to default settings.
  88. si8 getPlayersCnt() const;
  89. void setPlayersCnt(si8 value);
  90. /// The count of the teams ranging from 0 to <players count - 1> or RANDOM_SIZE for random.
  91. si8 getTeamsCnt() const;
  92. void setTeamsCnt(si8 value);
  93. /// The count of the computer only players ranging from 0 to <PlayerColor::PLAYER_LIMIT - players count> or RANDOM_SIZE for random.
  94. /// If you call this method, all player settings are reset to default settings.
  95. si8 getCompOnlyPlayersCnt() const;
  96. void setCompOnlyPlayersCnt(si8 value);
  97. /// The count of the computer only teams ranging from 0 to <comp only players - 1> or RANDOM_SIZE for random.
  98. si8 getCompOnlyTeamsCnt() const;
  99. void setCompOnlyTeamsCnt(si8 value);
  100. EWaterContent::EWaterContent getWaterContent() const;
  101. void setWaterContent(EWaterContent::EWaterContent value);
  102. EMonsterStrength::EMonsterStrength getMonsterStrength() const;
  103. void setMonsterStrength(EMonsterStrength::EMonsterStrength value);
  104. /// The first player colors belong to standard players and the last player colors belong to comp only players.
  105. /// All standard players are by default of type EPlayerType::AI.
  106. const std::map<PlayerColor, CPlayerSettings> & getPlayersSettings() const;
  107. void setStartingTownForPlayer(PlayerColor color, si32 town);
  108. /// Sets a player type for a standard player. A standard player is the opposite of a computer only player. The
  109. /// values which can be chosen for the player type are EPlayerType::AI or EPlayerType::HUMAN. Calling this method
  110. /// has no effect for the map itself, but it adds some informational text for the map description.
  111. void setPlayerTypeForStandardPlayer(PlayerColor color, EPlayerType::EPlayerType playerType);
  112. /// Finalizes the options. All random sizes for various properties will be overwritten by numbers from
  113. /// a random number generator by keeping the options in a valid state.
  114. void finalize();
  115. void finalize(CRandomGenerator & gen);
  116. static const si8 RANDOM_SIZE = -1;
  117. private:
  118. void resetPlayersMap();
  119. int countHumanPlayers() const;
  120. PlayerColor getNextPlayerColor() const;
  121. si32 width, height;
  122. bool hasTwoLevels;
  123. si8 playersCnt, teamsCnt, compOnlyPlayersCnt, compOnlyTeamsCnt;
  124. EWaterContent::EWaterContent waterContent;
  125. EMonsterStrength::EMonsterStrength monsterStrength;
  126. std::map<PlayerColor, CPlayerSettings> players;
  127. public:
  128. template <typename Handler>
  129. void serialize(Handler & h, const int version)
  130. {
  131. //FIXME: Enum is not a fixed with data type. Add enum class to both enums
  132. // later. For now it is ok.
  133. h & width & height & hasTwoLevels & playersCnt & teamsCnt & compOnlyPlayersCnt;
  134. h & compOnlyTeamsCnt & waterContent & monsterStrength & players;
  135. }
  136. };
  137. /// The map generator creates a map randomly.
  138. class DLL_LINKAGE CMapGenerator
  139. {
  140. public:
  141. explicit CMapGenerator(const CMapGenOptions & mapGenOptions, int randomSeed = std::time(nullptr));
  142. ~CMapGenerator(); // required due to unique_ptr
  143. std::unique_ptr<CMap> generate();
  144. private:
  145. /// Generation methods
  146. std::string getMapDescription() const;
  147. void addPlayerInfo();
  148. void addHeaderInfo();
  149. void genTerrain();
  150. void genTowns();
  151. CMapGenOptions mapGenOptions;
  152. std::unique_ptr<CMap> map;
  153. CRandomGenerator gen;
  154. int randomSeed;
  155. CMapEditManager * editManager;
  156. };