StartInfo.h 6.0 KB

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