OpenTTD Source  20241108-master-g80f628063a
network_game_info.h
1 /*
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/>.
6  */
7 
12 #ifndef NETWORK_CORE_GAME_INFO_H
13 #define NETWORK_CORE_GAME_INFO_H
14 
15 #include "config.h"
16 #include "core.h"
17 #include "../../newgrf_config.h"
18 #include "../../timer/timer_game_calendar.h"
19 #include "../../timer/timer_game_tick.h"
20 
21 #include <unordered_map>
22 
23 /*
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.
27  *
28  * Version: Bytes: Description:
29  * all 1 the version of this packet's structure
30  *
31  * 7+ 8 amount of ticks this game has been running unpaused.
32  *
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.
43  *
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.
46  *
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.
59  *
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)
62  *
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
66  *
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
80  * 1+ 1 type of map:
81  * (0 = temperate, 1 = arctic, 2 = desert, 3 = toyland)
82  * 1+ 1 whether the server is dedicated (0 = no, 1 = yes)
83  */
84 
86 enum NewGRFSerializationType {
87  NST_GRFID_MD5 = 0,
88  NST_GRFID_MD5_NAME = 1,
89  NST_LOOKUP_ID = 2,
90  NST_END
91 };
92 
101  uint16_t map_width;
102  uint16_t map_height;
103  std::string server_name;
104  std::string server_revision;
105  bool dedicated;
107  uint8_t clients_on;
108  uint8_t clients_max;
109  uint8_t companies_on;
110  uint8_t companies_max;
111  uint8_t spectators_on;
112  uint8_t landscape;
114  std::string gamescript_name;
115 };
116 
123  bool compatible;
124 };
125 
132  std::string name;
133 };
135 typedef std::unordered_map<uint32_t, NamedGRFIdentifier> GameInfoNewGRFLookupTable;
136 
137 extern NetworkServerGameInfo _network_game_info;
138 
139 std::string_view GetNetworkRevisionString();
140 bool IsNetworkCompatibleVersion(std::string_view other);
141 void CheckGameCompatibility(NetworkGameInfo &ngi);
142 
143 void FillStaticNetworkServerGameInfo();
144 const NetworkServerGameInfo &GetCurrentNetworkServerGameInfo();
145 
146 void DeserializeGRFIdentifier(Packet &p, GRFIdentifier &grf);
147 void DeserializeGRFIdentifierWithName(Packet &p, NamedGRFIdentifier &grf);
148 void SerializeGRFIdentifier(Packet &p, const GRFIdentifier &grf);
149 
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);
152 
153 #endif /* NETWORK_CORE_GAME_INFO_H */
uint64_t TickCounter
The type that the tick counter is stored in.
Configuration options of the network stuff.
Base for all network types (UDP and TCP)
Information about GRF, used in the game and (part of it) in savegames.
Basic data to distinguish a GRF.
Definition: newgrf_config.h:82
Container to hold the GRF identifier (GRF ID + MD5 checksum) and the name associated with that NewGRF...
std::string name
The name of the NewGRF.
GRFIdentifier ident
The unique identifier of the NewGRF.
The game information that is sent from the server to the clients with extra information only required...
bool version_compatible
Can we connect to this server or not? (based on server_revision)
bool compatible
Can we connect to this server or not? (based on server_revision and grf_match.
The game information that is sent from the server to the client.
TimerGameCalendar::Date calendar_start
When the game started.
bool dedicated
Is this a dedicated server?
std::string server_revision
The version number the server is using (e.g.: 'r304' or 0.5.0)
bool use_password
Is this server passworded?
uint8_t clients_max
Max clients allowed on server.
uint8_t spectators_on
How many spectators do we have?
uint16_t map_height
Map height.
std::string server_name
Server name.
uint16_t map_width
Map width.
TimerGameTick::TickCounter ticks_playing
Amount of ticks the game has been running unpaused.
GRFConfig * grfconfig
List of NewGRF files used.
uint8_t landscape
The used landscape.
uint8_t companies_max
Max companies allowed on server.
std::string gamescript_name
Name of the gamescript.
TimerGameCalendar::Date calendar_date
Current calendar date.
int gamescript_version
Version of the gamescript.
uint8_t companies_on
How many started companies do we have.
uint8_t clients_on
Current count of clients on server.
Internal entity of a packet.
Definition: packet.h:42
Templated helper to make a type-safe 'typedef' representing a single POD value.