StartInfo.h 7.1 KB

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