StartInfo.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * StartInfo.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 "vstd/DateUtils.h"
  12. #include "GameConstants.h"
  13. #include "TurnTimerInfo.h"
  14. #include "ExtraOptionsInfo.h"
  15. #include "campaign/CampaignConstants.h"
  16. #include "serializer/GameConnectionID.h"
  17. #include "serializer/Serializeable.h"
  18. #include "serializer/PlayerConnectionID.h"
  19. #include "mapObjects/army/CStackBasicDescriptor.h"
  20. #include "ResourceSet.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. class CMapGenOptions;
  23. class CampaignState;
  24. class CMapInfo;
  25. struct PlayerInfo;
  26. class PlayerColor;
  27. struct DLL_LINKAGE SimturnsInfo
  28. {
  29. /// Minimal number of turns that must be played simultaneously even if contact has been detected
  30. int requiredTurns = 0;
  31. /// Maximum number of turns that might be played simultaneously unless contact is detected
  32. int optionalTurns = 0;
  33. /// If set to true, human and 1 AI can act at the same time
  34. bool allowHumanWithAI = false;
  35. /// If set to true, allied players can play simultaneously even after contacting each other
  36. bool ignoreAlliedContacts = true;
  37. bool operator == (const SimturnsInfo & other) const
  38. {
  39. return requiredTurns == other.requiredTurns &&
  40. optionalTurns == other.optionalTurns &&
  41. ignoreAlliedContacts == other.ignoreAlliedContacts &&
  42. allowHumanWithAI == other.allowHumanWithAI;
  43. }
  44. template <typename Handler>
  45. void serialize(Handler &h)
  46. {
  47. h & requiredTurns;
  48. h & optionalTurns;
  49. h & allowHumanWithAI;
  50. h & ignoreAlliedContacts;
  51. }
  52. };
  53. enum class PlayerStartingBonus : int8_t
  54. {
  55. RANDOM = -1,
  56. ARTIFACT = 0,
  57. GOLD = 1,
  58. RESOURCE = 2
  59. };
  60. struct DLL_LINKAGE Handicap {
  61. TResources startBonus = TResources();
  62. int percentIncome = 100;
  63. int percentGrowth = 100;
  64. template <typename Handler>
  65. void serialize(Handler &h)
  66. {
  67. h & startBonus;
  68. h & percentIncome;
  69. h & percentGrowth;
  70. }
  71. };
  72. /// Struct which describes the name, the color, the starting bonus of a player
  73. struct DLL_LINKAGE PlayerSettings
  74. {
  75. PlayerStartingBonus bonus;
  76. FactionID castle;
  77. HeroTypeID hero;
  78. HeroTypeID heroPortrait; //-1 if default, else ID
  79. std::string heroNameTextId;
  80. PlayerColor color; //from 0 -
  81. Handicap handicap;
  82. std::string name;
  83. std::set<PlayerConnectionID> connectedPlayerIDs; //Empty - AI, or connectrd player ids
  84. bool compOnly; //true if this player is a computer only player; required for RMG
  85. template <typename Handler>
  86. void serialize(Handler &h)
  87. {
  88. h & castle;
  89. h & hero;
  90. h & heroPortrait;
  91. h & heroNameTextId;
  92. h & bonus;
  93. h & color;
  94. h & handicap;
  95. h & name;
  96. h & connectedPlayerIDs;
  97. h & compOnly;
  98. }
  99. PlayerSettings();
  100. bool isControlledByAI() const;
  101. bool isControlledByHuman() const;
  102. FactionID getCastleValidated() const;
  103. HeroTypeID getHeroValidated() const;
  104. };
  105. enum class EStartMode : int32_t
  106. {
  107. NEW_GAME,
  108. LOAD_GAME,
  109. CAMPAIGN,
  110. INVALID = 255
  111. };
  112. /// Struct which describes the difficulty, the turn time,.. of a heroes match.
  113. struct DLL_LINKAGE StartInfo : public Serializeable
  114. {
  115. EStartMode mode;
  116. ui8 difficulty; //0=easy; 4=impossible
  117. using TPlayerInfos = std::map<PlayerColor, PlayerSettings>;
  118. TPlayerInfos playerInfos; //color indexed
  119. time_t startTime;
  120. std::string fileURI;
  121. SimturnsInfo simturnsInfo;
  122. TurnTimerInfo turnTimerInfo;
  123. ExtraOptionsInfo extraOptionsInfo;
  124. std::string mapname; // empty for random map, otherwise name of the map or savegame
  125. bool createRandomMap() const { return mapGenOptions != nullptr; }
  126. std::shared_ptr<CMapGenOptions> mapGenOptions;
  127. std::shared_ptr<CampaignState> campState;
  128. PlayerSettings & getIthPlayersSettings(const PlayerColor & no);
  129. const PlayerSettings & getIthPlayersSettings(const PlayerColor & no) const;
  130. PlayerSettings * getPlayersSettings(PlayerConnectionID connectedPlayerId);
  131. // TODO: Must be client-side
  132. std::string getCampaignName() const;
  133. /// Controls check for handling of garrisons by AI in Restoration of Erathia campaigns to match H3 behavior
  134. bool restrictedGarrisonsForAI() const;
  135. template <typename Handler>
  136. void serialize(Handler &h)
  137. {
  138. h & mode;
  139. h & difficulty;
  140. h & playerInfos;
  141. h & startTime;
  142. h & fileURI;
  143. h & simturnsInfo;
  144. h & turnTimerInfo;
  145. h & extraOptionsInfo;
  146. h & mapname;
  147. h & mapGenOptions;
  148. h & campState;
  149. }
  150. StartInfo()
  151. : mode(EStartMode::INVALID)
  152. , difficulty(1)
  153. , startTime(std::time(nullptr))
  154. {
  155. }
  156. };
  157. struct ClientPlayer
  158. {
  159. GameConnectionID connection;
  160. std::string name;
  161. template <typename Handler> void serialize(Handler &h)
  162. {
  163. h & connection;
  164. h & name;
  165. }
  166. };
  167. struct DLL_LINKAGE LobbyState
  168. {
  169. std::shared_ptr<StartInfo> si;
  170. std::shared_ptr<CMapInfo> mi;
  171. std::map<PlayerConnectionID, ClientPlayer> playerNames; // id of player <-> player name;
  172. GameConnectionID hostClientId = GameConnectionID::INVALID;
  173. // TODO: Campaign-only and we don't really need either of them.
  174. // Before start both go into CCampaignState that is part of StartInfo
  175. CampaignScenarioID campaignMap;
  176. int campaignBonus;
  177. LobbyState() : si(new StartInfo()), campaignMap(CampaignScenarioID::NONE), campaignBonus(-1) {}
  178. template <typename Handler> void serialize(Handler &h)
  179. {
  180. h & si;
  181. h & mi;
  182. h & playerNames;
  183. h & hostClientId;
  184. h & campaignMap;
  185. h & campaignBonus;
  186. }
  187. };
  188. struct DLL_LINKAGE LobbyInfo : public LobbyState
  189. {
  190. std::string uuid;
  191. LobbyInfo() {}
  192. void verifyStateBeforeStart(bool ignoreNoHuman = false) const;
  193. bool isClientHost(GameConnectionID clientId) const;
  194. bool isPlayerHost(const PlayerColor & color) const;
  195. std::set<PlayerColor> getAllClientPlayers(GameConnectionID clientId) const;
  196. std::vector<PlayerConnectionID> getConnectedPlayerIdsForClient(GameConnectionID clientId) const;
  197. // Helpers for lobby state access
  198. std::set<PlayerColor> clientHumanColors(GameConnectionID clientId);
  199. PlayerColor clientFirstColor(GameConnectionID clientId) const;
  200. bool isClientColor(GameConnectionID clientId, const PlayerColor & color) const;
  201. PlayerConnectionID clientFirstId(GameConnectionID clientId) const; // Used by chat only!
  202. PlayerInfo & getPlayerInfo(PlayerColor color);
  203. TeamID getPlayerTeamId(const PlayerColor & color);
  204. };
  205. class DLL_LINKAGE BattleOnlyModeStartInfo : public Serializeable
  206. {
  207. public:
  208. std::optional<TerrainId> selectedTerrain;
  209. std::optional<FactionID> selectedTown;
  210. std::array<std::optional<HeroTypeID>, 2> selectedHero;
  211. std::array<std::array<CStackBasicDescriptor, 7>, 2> selectedArmy;
  212. std::array<std::array<int, GameConstants::PRIMARY_SKILLS>, 2> primSkillLevel;
  213. BattleOnlyModeStartInfo();
  214. template <typename Handler> void serialize(Handler &h)
  215. {
  216. h & selectedTerrain;
  217. h & selectedTown;
  218. h & selectedHero;
  219. h & selectedArmy;
  220. h & primSkillLevel;
  221. }
  222. };
  223. VCMI_LIB_NAMESPACE_END