2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
9 * @file game_info.h Convert NetworkGameInfo to Packet and back.
12 #ifndef NETWORK_CORE_GAME_INFO_H
13 #define NETWORK_CORE_GAME_INFO_H
17 #include "../../newgrf_config.h"
18 #include "../../timer/timer_game_calendar.h"
19 #include "../../timer/timer_game_tick.h"
21 #include <unordered_map>
24 * NetworkGameInfo has several revisions which we still need to support on the
25 * wire. The table below shows the version and size for each field of the
26 * serialized NetworkGameInfo.
28 * Version: Bytes: Description:
29 * all 1 the version of this packet's structure
31 * 7+ 8 amount of ticks this game has been running unpaused.
33 * 6+ 1 type of storage for the NewGRFs below:
34 * 0 = NewGRF ID and MD5 checksum.
35 * Used as default for version 5 and below, and for
36 * later game updates to the Game Coordinator.
37 * 1 = NewGRF ID, MD5 checksum and name.
38 * Used for direct requests and the first game
39 * update to Game Coordinator.
40 * 2 = Index in NewGRF lookup table.
41 * Used for sending server listing from the Game
42 * Coordinator to the clients.
44 * 5+ 4 version number of the Game Script (-1 is case none is selected).
45 * 5+ var string with the name of the Game Script.
47 * 4+ 1 number of GRFs attached (n).
48 * 4+ n * var identifiers for GRF files. Consists of:
49 * Note: the 'vN' refers to packet version and 'type'
50 * refers to the v6+ type of storage for the NewGRFs.
51 * - 4 byte variable with the GRF ID.
52 * For v4, v5, and v6+ in case of type 0 and/or type 1.
53 * - 16 bytes with the MD5 checksum of the GRF.
54 * For v4, v5, and v6+ in case of type 0 and/or type 1.
55 * - string with name of NewGRF.
56 * For v6+ in case of type 1.
57 * - 4 byte lookup table index.
58 * For v6+ in case of type 2.
60 * 3+ 4 current calendar date in days since 1-1-0 (DMY)
61 * 3+ 4 calendar start date in days since 1-1-0 (DMY)
63 * 2+ 1 maximum number of companies allowed on the server
64 * 2+ 1 number of companies on the server
65 * 2+ 1 maximum number of spectators allowed on the server
67 * 1+ var string with the name of the server
68 * 1+ var string with the revision of the server
69 * 1 - 5 1 the language run on the server
70 * (0 = any, 1 = English, 2 = German, 3 = French)
71 * 1+ 1 whether the server uses a password (0 = no, 1 = yes)
72 * 1+ 1 maximum number of clients allowed on the server
73 * 1+ 1 number of clients on the server
74 * 1+ 1 number of spectators on the server
75 * 1 & 2 2 current game date in days since 1-1-1920 (DMY)
76 * 1 & 2 2 game introduction date in days since 1-1-1920 (DMY)
77 * 1 - 5 var string with the name of the map
78 * 1+ 2 width of the map in tiles
79 * 1+ 2 height of the map in tiles
81 * (0 = temperate, 1 = arctic, 2 = desert, 3 = toyland)
82 * 1+ 1 whether the server is dedicated (0 = no, 1 = yes)
85 /** The different types/ways a NewGRF can be serialized in the GameInfo since version 6. */
86 enum NewGRFSerializationType
{
87 NST_GRFID_MD5
= 0, ///< Unique GRF ID and MD5 checksum.
88 NST_GRFID_MD5_NAME
= 1, ///< Unique GRF ID, MD5 checksum and name.
89 NST_LOOKUP_ID
= 2, ///< Unique ID into a lookup table that is sent before.
90 NST_END
///< The end of the list (period).
94 * The game information that is sent from the server to the client.
96 struct NetworkServerGameInfo
{
97 GRFConfig
*grfconfig
; ///< List of NewGRF files used
98 TimerGameCalendar::Date calendar_start
; ///< When the game started.
99 TimerGameCalendar::Date calendar_date
; ///< Current calendar date.
100 TimerGameTick::TickCounter ticks_playing
; ///< Amount of ticks the game has been running unpaused.
101 uint16_t map_width
; ///< Map width
102 uint16_t map_height
; ///< Map height
103 std::string server_name
; ///< Server name
104 std::string server_revision
; ///< The version number the server is using (e.g.: 'r304' or 0.5.0)
105 bool dedicated
; ///< Is this a dedicated server?
106 bool use_password
; ///< Is this server passworded?
107 uint8_t clients_on
; ///< Current count of clients on server
108 uint8_t clients_max
; ///< Max clients allowed on server
109 uint8_t companies_on
; ///< How many started companies do we have
110 uint8_t companies_max
; ///< Max companies allowed on server
111 uint8_t spectators_on
; ///< How many spectators do we have?
112 uint8_t landscape
; ///< The used landscape
113 int gamescript_version
; ///< Version of the gamescript.
114 std::string gamescript_name
; ///< Name of the gamescript.
118 * The game information that is sent from the server to the clients
119 * with extra information only required at the client side.
121 struct NetworkGameInfo
: NetworkServerGameInfo
{
122 bool version_compatible
; ///< Can we connect to this server or not? (based on server_revision)
123 bool compatible
; ///< Can we connect to this server or not? (based on server_revision _and_ grf_match
127 * Container to hold the GRF identifier (GRF ID + MD5 checksum) and the name
128 * associated with that NewGRF.
130 struct NamedGRFIdentifier
{
131 GRFIdentifier ident
; ///< The unique identifier of the NewGRF.
132 std::string name
; ///< The name of the NewGRF.
134 /** Lookup table for the GameInfo in case of #NST_LOOKUP_ID. */
135 typedef std::unordered_map
<uint32_t, NamedGRFIdentifier
> GameInfoNewGRFLookupTable
;
137 extern NetworkServerGameInfo _network_game_info
;
139 std::string_view
GetNetworkRevisionString();
140 bool IsNetworkCompatibleVersion(std::string_view other
);
141 void CheckGameCompatibility(NetworkGameInfo
&ngi
);
143 void FillStaticNetworkServerGameInfo();
144 const NetworkServerGameInfo
&GetCurrentNetworkServerGameInfo();
146 void DeserializeGRFIdentifier(Packet
&p
, GRFIdentifier
&grf
);
147 void DeserializeGRFIdentifierWithName(Packet
&p
, NamedGRFIdentifier
&grf
);
148 void SerializeGRFIdentifier(Packet
&p
, const GRFIdentifier
&grf
);
150 void DeserializeNetworkGameInfo(Packet
&p
, NetworkGameInfo
&info
, const GameInfoNewGRFLookupTable
*newgrf_lookup_table
= nullptr);
151 void SerializeNetworkGameInfo(Packet
&p
, const NetworkServerGameInfo
&info
, bool send_newgrf_names
= true);
153 #endif /* NETWORK_CORE_GAME_INFO_H */