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