CMapGenOptions.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * CMapGenOptions.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 "../serializer/Serializeable.h"
  13. #include "CRmgTemplate.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. namespace vstd
  16. {
  17. class RNG;
  18. }
  19. enum class EPlayerType
  20. {
  21. HUMAN,
  22. AI,
  23. COMP_ONLY
  24. };
  25. /// The map gen options class holds values about general map generation settings
  26. /// e.g. the size of the map, the count of players,...
  27. class DLL_LINKAGE CMapGenOptions : public Serializeable
  28. {
  29. public:
  30. /// The player settings class maps the player color, starting town and human player flag.
  31. class DLL_LINKAGE CPlayerSettings
  32. {
  33. public:
  34. CPlayerSettings();
  35. /// The color of the player ranging from 0 to PlayerColor::PLAYER_LIMIT - 1.
  36. /// The default value is 0.
  37. PlayerColor getColor() const;
  38. void setColor(const PlayerColor & value);
  39. /// The starting town of the player ranging from 0 to town max count or RANDOM_TOWN.
  40. /// The default value is RANDOM_TOWN.
  41. FactionID getStartingTown() const;
  42. void setStartingTown(FactionID value);
  43. /// The starting hero of the player ranging from 0 to hero max count or RANDOM_HERO.
  44. /// The default value is RANDOM_HERO
  45. HeroTypeID getStartingHero() const;
  46. void setStartingHero(HeroTypeID value);
  47. /// The default value is EPlayerType::AI.
  48. EPlayerType getPlayerType() const;
  49. void setPlayerType(EPlayerType value);
  50. /// Team id for this player. TeamID::NO_TEAM by default - team will be randomly assigned
  51. TeamID getTeam() const;
  52. void setTeam(const TeamID & value);
  53. private:
  54. PlayerColor color;
  55. FactionID startingTown;
  56. HeroTypeID startingHero;
  57. EPlayerType playerType;
  58. TeamID team;
  59. public:
  60. template <typename Handler>
  61. void serialize(Handler & h)
  62. {
  63. h & color;
  64. h & startingTown;
  65. h & playerType;
  66. h & team;
  67. h & startingHero;
  68. }
  69. };
  70. CMapGenOptions();
  71. CMapGenOptions(const CMapGenOptions&) = delete;
  72. si32 getWidth() const;
  73. void setWidth(si32 value);
  74. si32 getHeight() const;
  75. void setHeight(si32 value);
  76. int getLevels() const;
  77. void setLevels(int value);
  78. /// The count of all (human or computer) players ranging from 1 to PlayerColor::PLAYER_LIMIT or RANDOM_SIZE for random. If you call
  79. /// this method, all player settings are reset to default settings.
  80. si8 getHumanOrCpuPlayerCount() const;
  81. void setHumanOrCpuPlayerCount(si8 value);
  82. si8 getMinPlayersCount(bool withTemplateLimit = true) const;
  83. si8 getMaxPlayersCount(bool withTemplateLimit = true) const;
  84. si8 getPlayerLimit() const;
  85. /// The count of the teams ranging from 0 to <players count - 1> or RANDOM_SIZE for random.
  86. si8 getTeamCount() const;
  87. void setTeamCount(si8 value);
  88. /// The count of the computer only players ranging from 0 to <PlayerColor::PLAYER_LIMIT - players count> or RANDOM_SIZE for random.
  89. /// If you call this method, all player settings are reset to default settings.
  90. si8 getCompOnlyPlayerCount() const;
  91. void setCompOnlyPlayerCount(si8 value);
  92. /// The count of the computer only teams ranging from 0 to <comp only players - 1> or RANDOM_SIZE for random.
  93. si8 getCompOnlyTeamCount() const;
  94. void setCompOnlyTeamCount(si8 value);
  95. EWaterContent::EWaterContent getWaterContent() const;
  96. void setWaterContent(EWaterContent::EWaterContent value);
  97. EMonsterStrength::EMonsterStrength getMonsterStrength() const;
  98. void setMonsterStrength(EMonsterStrength::EMonsterStrength value);
  99. bool isRoadEnabled(const RoadId & roadType) const;
  100. bool isRoadEnabled() const;
  101. void setRoadEnabled(const RoadId & roadType, bool enable);
  102. /// The first player colors belong to standard players and the last player colors belong to comp only players.
  103. /// All standard players are by default of type EPlayerType::AI.
  104. const std::map<PlayerColor, CPlayerSettings> & getPlayersSettings() const;
  105. const std::map<PlayerColor, CPlayerSettings> & getSavedPlayersMap() const;
  106. void setStartingTownForPlayer(const PlayerColor & color, FactionID town);
  107. void setStartingHeroForPlayer(const PlayerColor & color, HeroTypeID hero);
  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.
  110. void setPlayerTypeForStandardPlayer(const PlayerColor & color, EPlayerType playerType);
  111. void setPlayerTeam(const PlayerColor & color, const TeamID & team = TeamID::NO_TEAM);
  112. /// The random map template to generate the map with or empty/not set if the template should be chosen randomly.
  113. /// Default: Not set/random.
  114. const CRmgTemplate * getMapTemplate() const;
  115. void setMapTemplate(const CRmgTemplate * value);
  116. void setMapTemplate(const std::string & name);
  117. std::vector<const CRmgTemplate *> getPossibleTemplates() const;
  118. /// Finalizes the options. All random sizes for various properties will be overwritten by numbers from
  119. /// a random number generator by keeping the options in a valid state. Check options should return true, otherwise
  120. /// this function fails.
  121. void finalize(vstd::RNG & rand);
  122. /// Returns false if there is no template available which fits to the currently selected options.
  123. bool checkOptions() const;
  124. /// Returns true if player colors or teams were set in game GUI
  125. bool arePlayersCustomized() const;
  126. static const si8 RANDOM_SIZE = -1;
  127. private:
  128. void initPlayersMap();
  129. void resetPlayersMap();
  130. void savePlayersMap();
  131. int countHumanPlayers() const;
  132. int countCompOnlyPlayers() const;
  133. PlayerColor getNextPlayerColor() const;
  134. void updateCompOnlyPlayers();
  135. void updatePlayers();
  136. const CRmgTemplate * getPossibleTemplate(vstd::RNG & rand) const;
  137. si32 width;
  138. si32 height;
  139. si32 levels;
  140. si8 humanOrCpuPlayerCount;
  141. si8 teamCount;
  142. si8 compOnlyPlayerCount;
  143. si8 compOnlyTeamCount;
  144. EWaterContent::EWaterContent waterContent;
  145. EMonsterStrength::EMonsterStrength monsterStrength;
  146. std::map<PlayerColor, CPlayerSettings> players;
  147. std::map<PlayerColor, CPlayerSettings> savedPlayerSettings;
  148. std::set<RoadId> enabledRoads;
  149. bool customizedPlayers;
  150. const CRmgTemplate * mapTemplate;
  151. public:
  152. template <typename Handler>
  153. void serialize(Handler & h)
  154. {
  155. h & width;
  156. h & height;
  157. if (h.version >= Handler::Version::MORE_MAP_LAYERS)
  158. h & levels;
  159. else
  160. {
  161. if (h.saving)
  162. {
  163. bool hasTwoLevels = levels == 2;
  164. h & hasTwoLevels;
  165. }
  166. else
  167. {
  168. bool hasTwoLevels;
  169. h & hasTwoLevels;
  170. levels = hasTwoLevels ? 2 : 1;
  171. }
  172. }
  173. h & humanOrCpuPlayerCount;
  174. h & teamCount;
  175. h & compOnlyPlayerCount;
  176. h & compOnlyTeamCount;
  177. h & waterContent;
  178. h & monsterStrength;
  179. h & players;
  180. std::string templateName;
  181. if(mapTemplate && h.saving)
  182. {
  183. templateName = mapTemplate->getId();
  184. }
  185. h & templateName;
  186. if(!h.saving)
  187. {
  188. setMapTemplate(templateName);
  189. }
  190. h & enabledRoads;
  191. }
  192. void serializeJson(JsonSerializeFormat & handler);
  193. };
  194. VCMI_LIB_NAMESPACE_END