network.h
branchcustombridgeheads
changeset 5642 bfa6074e2833
parent 5641 d4d00a16ef26
child 5643 3778051e8095
equal deleted inserted replaced
5641:d4d00a16ef26 5642:bfa6074e2833
     1 /* $Id$ */
       
     2 
       
     3 #ifndef NETWORK_H
       
     4 #define NETWORK_H
       
     5 
       
     6 #define NOREV_STRING "norev000"
       
     7 
       
     8 #ifdef ENABLE_NETWORK
       
     9 
       
    10 #include "player.h"
       
    11 
       
    12 // If this line is enable, every frame will have a sync test
       
    13 //  this is not needed in normal games. Normal is like 1 sync in 100
       
    14 //  frames. You can enable this if you have a lot of desyncs on a certain
       
    15 //  game.
       
    16 // Remember: both client and server have to be compiled with this
       
    17 //  option enabled to make it to work. If one of the two has it disabled
       
    18 //  nothing will happen.
       
    19 //#define ENABLE_NETWORK_SYNC_EVERY_FRAME
       
    20 
       
    21 // In theory sending 1 of the 2 seeds is enough to check for desyncs
       
    22 //   so in theory, this next define can be left off.
       
    23 //#define NETWORK_SEND_DOUBLE_SEED
       
    24 
       
    25 // How many clients can we have? Like.. MAX_PLAYERS - 1 is the amount of
       
    26 //  players that can really play.. so.. a max of 4 spectators.. gives us..
       
    27 //  MAX_PLAYERS + 3
       
    28 #define MAX_CLIENTS (MAX_PLAYERS + 3)
       
    29 
       
    30 
       
    31 // Do not change this next line. It should _ALWAYS_ be MAX_CLIENTS + 1
       
    32 #define MAX_CLIENT_INFO (MAX_CLIENTS + 1)
       
    33 
       
    34 /* Stuff for the master-server */
       
    35 #define NETWORK_MASTER_SERVER_PORT 3978
       
    36 #define NETWORK_MASTER_SERVER_HOST "master.openttd.org"
       
    37 #define NETWORK_MASTER_SERVER_WELCOME_MESSAGE "OpenTTDRegister"
       
    38 
       
    39 #define NETWORK_DEFAULT_PORT 3979
       
    40 
       
    41 #define MAX_INTERFACES 9
       
    42 
       
    43 
       
    44 // How many vehicle/station types we put over the network
       
    45 #define NETWORK_VEHICLE_TYPES 5
       
    46 #define NETWORK_STATION_TYPES 5
       
    47 
       
    48 enum {
       
    49 	NETWORK_NAME_LENGTH        =  80,
       
    50 	NETWORK_HOSTNAME_LENGTH    =  80,
       
    51 	NETWORK_REVISION_LENGTH    =  15,
       
    52 	NETWORK_PASSWORD_LENGTH    =  20,
       
    53 	NETWORK_PLAYERS_LENGTH     = 200,
       
    54 	NETWORK_CLIENT_NAME_LENGTH =  25,
       
    55 	NETWORK_RCONCOMMAND_LENGTH = 500,
       
    56 
       
    57 	NETWORK_GRF_NAME_LENGTH    =  80, ///< Maximum length of the name of a GRF
       
    58 	/* Maximum number of GRFs that can be sent.
       
    59 	 * This value is related to number of handles (files) OpenTTD can open.
       
    60 	 * This is currently 64 and about 10 are currently used when OpenTTD loads
       
    61 	 * without any NewGRFs. Therefore one can only load about 55 NewGRFs, so
       
    62 	 * this is not a limit, but rather a way to easily check whether the limit
       
    63 	 * imposed by the handle count is reached. Secondly it isn't possible to
       
    64 	 * send much more GRF IDs + MD5sums in the PACKET_UDP_SERVER_RESPONSE, due
       
    65 	 * to the limited size of UDP packets. */
       
    66 	NETWORK_MAX_GRF_COUNT      =  55,
       
    67 
       
    68 	NETWORK_NUM_LANGUAGES      =   4,
       
    69 };
       
    70 
       
    71 // This is the struct used by both client and server
       
    72 //  some fields will be empty on the client (like game_password) by default
       
    73 //  and only filled with data a player enters.
       
    74 typedef struct NetworkGameInfo {
       
    75 	char server_name[NETWORK_NAME_LENGTH];          // Server name
       
    76 	char hostname[NETWORK_HOSTNAME_LENGTH];         // Hostname of the server (if any)
       
    77 	char server_revision[NETWORK_REVISION_LENGTH];  // The SVN version number the server is using (e.g.: 'r304')
       
    78 	                                                //  It even shows a SVN version in release-version, so
       
    79 	                                                //  it is easy to compare if a server is of the correct version
       
    80 	bool version_compatible;                        // Can we connect to this server or not? (based on server_revision)
       
    81 	bool compatible;                                // Can we connect to this server or not? (based on server_revision _and_ grf_match
       
    82 	byte server_lang;                               // Language of the server (we should make a nice table for this)
       
    83 	byte use_password;                              // Is set to != 0 if it uses a password
       
    84 	char server_password[NETWORK_PASSWORD_LENGTH];  // On the server: the game password, on the client: != "" if server has password
       
    85 	byte clients_max;                               // Max clients allowed on server
       
    86 	byte clients_on;                                // Current count of clients on server
       
    87 	byte companies_max;                             // Max companies allowed on server
       
    88 	byte companies_on;                              // How many started companies do we have (XXX - disabled for server atm, use ActivePlayerCount())
       
    89 	byte spectators_max;                            // Max spectators allowed on server
       
    90 	byte spectators_on;                             // How many spectators do we have? (XXX - disabled for server atm, use NetworkSpectatorCount())
       
    91 	Date game_date;                                 // Current date
       
    92 	Date start_date;                                // When the game started
       
    93 	char map_name[NETWORK_NAME_LENGTH];             // Map which is played ["random" for a randomized map]
       
    94 	uint16 map_width;                               // Map width
       
    95 	uint16 map_height;                              // Map height
       
    96 	byte map_set;                                   // Graphical set
       
    97 	bool dedicated;                                 // Is this a dedicated server?
       
    98 	char rcon_password[NETWORK_PASSWORD_LENGTH];    // RCon password for the server. "" if rcon is disabled
       
    99 	struct GRFConfig *grfconfig;                    // List of NewGRF files required
       
   100 } NetworkGameInfo;
       
   101 
       
   102 typedef struct NetworkPlayerInfo {
       
   103 	char company_name[NETWORK_NAME_LENGTH];         // Company name
       
   104 	char password[NETWORK_PASSWORD_LENGTH];         // The password for the player
       
   105 	Year inaugurated_year;                          // What year the company started in
       
   106 	int64 company_value;                            // The company value
       
   107 	int64 money;                                    // The amount of money the company has
       
   108 	int64 income;                                   // How much did the company earned last year
       
   109 	uint16 performance;                             // What was his performance last month?
       
   110 	byte use_password;                              // 0: No password 1: There is a password
       
   111 	uint16 num_vehicle[NETWORK_VEHICLE_TYPES];      // How many vehicles are there of this type?
       
   112 	uint16 num_station[NETWORK_STATION_TYPES];      // How many stations are there of this type?
       
   113 	char players[NETWORK_PLAYERS_LENGTH];           // The players that control this company (Name1, name2, ..)
       
   114 	uint16 months_empty;                            // How many months the company is empty
       
   115 } NetworkPlayerInfo;
       
   116 
       
   117 typedef struct NetworkClientInfo {
       
   118 	uint16 client_index;                            // Index of the client (same as ClientState->index)
       
   119 	char client_name[NETWORK_CLIENT_NAME_LENGTH];   // Name of the client
       
   120 	byte client_lang;                               // The language of the client
       
   121 	byte client_playas;                             // As which player is this client playing (PlayerID)
       
   122 	uint32 client_ip;                               // IP-address of the client (so he can be banned)
       
   123 	Date join_date;                                 // Gamedate the player has joined
       
   124 	char unique_id[NETWORK_NAME_LENGTH];            // Every play sends an unique id so we can indentify him
       
   125 } NetworkClientInfo;
       
   126 
       
   127 typedef struct NetworkGameList {
       
   128 	NetworkGameInfo info;
       
   129 	uint32 ip;
       
   130 	uint16 port;
       
   131 	bool online;                                    // False if the server did not respond (default status)
       
   132 	bool manually;                                  // True if the server was added manually
       
   133 	struct NetworkGameList *next;
       
   134 } NetworkGameList;
       
   135 
       
   136 typedef enum {
       
   137 	NETWORK_JOIN_STATUS_CONNECTING,
       
   138 	NETWORK_JOIN_STATUS_AUTHORIZING,
       
   139 	NETWORK_JOIN_STATUS_WAITING,
       
   140 	NETWORK_JOIN_STATUS_DOWNLOADING,
       
   141 	NETWORK_JOIN_STATUS_PROCESSING,
       
   142 	NETWORK_JOIN_STATUS_REGISTERING,
       
   143 
       
   144 	NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO,
       
   145 } NetworkJoinStatus;
       
   146 
       
   147 // language ids for server_lang and client_lang
       
   148 typedef enum {
       
   149 	NETLANG_ANY     = 0,
       
   150 	NETLANG_ENGLISH = 1,
       
   151 	NETLANG_GERMAN  = 2,
       
   152 	NETLANG_FRENCH  = 3,
       
   153 } NetworkLanguage;
       
   154 
       
   155 VARDEF NetworkGameList *_network_game_list;
       
   156 
       
   157 VARDEF NetworkGameInfo _network_game_info;
       
   158 VARDEF NetworkPlayerInfo _network_player_info[MAX_PLAYERS];
       
   159 VARDEF NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
       
   160 
       
   161 VARDEF char _network_player_name[NETWORK_CLIENT_NAME_LENGTH];
       
   162 VARDEF char _network_default_ip[NETWORK_HOSTNAME_LENGTH];
       
   163 
       
   164 VARDEF uint16 _network_own_client_index;
       
   165 VARDEF char _network_unique_id[NETWORK_NAME_LENGTH]; // Our own unique ID
       
   166 
       
   167 VARDEF uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
       
   168 VARDEF uint32 _frame_counter_max; // To where we may go with our clients
       
   169 
       
   170 VARDEF uint32 _last_sync_frame; // Used in the server to store the last time a sync packet was sent to clients.
       
   171 
       
   172 // networking settings
       
   173 VARDEF uint32 _broadcast_list[MAX_INTERFACES + 1];
       
   174 
       
   175 VARDEF uint16 _network_server_port;
       
   176 /* We use bind_ip and bind_ip_host, where bind_ip_host is the readable form of
       
   177     bind_ip_host, and bind_ip the numeric value, because we want a nice number
       
   178     in the openttd.cfg, but we wants to use the uint32 internally.. */
       
   179 VARDEF uint32 _network_server_bind_ip;
       
   180 VARDEF char _network_server_bind_ip_host[NETWORK_HOSTNAME_LENGTH];
       
   181 VARDEF bool _is_network_server; // Does this client wants to be a network-server?
       
   182 VARDEF char _network_server_name[NETWORK_NAME_LENGTH];
       
   183 VARDEF char _network_server_password[NETWORK_PASSWORD_LENGTH];
       
   184 VARDEF char _network_rcon_password[NETWORK_PASSWORD_LENGTH];
       
   185 
       
   186 VARDEF uint16 _network_max_join_time;             ///< Time a client can max take to join
       
   187 VARDEF bool _network_pause_on_join;               ///< Pause the game when a client tries to join (more chance of succeeding join)
       
   188 
       
   189 VARDEF uint16 _redirect_console_to_client;
       
   190 
       
   191 VARDEF uint16 _network_sync_freq;
       
   192 VARDEF uint8 _network_frame_freq;
       
   193 
       
   194 VARDEF uint32 _sync_seed_1, _sync_seed_2;
       
   195 VARDEF uint32 _sync_frame;
       
   196 VARDEF bool _network_first_time;
       
   197 // Vars needed for the join-GUI
       
   198 VARDEF NetworkJoinStatus _network_join_status;
       
   199 VARDEF uint8 _network_join_waiting;
       
   200 VARDEF uint16 _network_join_kbytes;
       
   201 VARDEF uint16 _network_join_kbytes_total;
       
   202 
       
   203 VARDEF char _network_last_host[NETWORK_HOSTNAME_LENGTH];
       
   204 VARDEF short _network_last_port;
       
   205 VARDEF uint32 _network_last_host_ip;
       
   206 VARDEF uint8 _network_reconnect;
       
   207 
       
   208 VARDEF bool _network_udp_server;
       
   209 VARDEF uint16 _network_udp_broadcast;
       
   210 
       
   211 VARDEF byte _network_lan_internet;
       
   212 
       
   213 VARDEF bool _network_need_advertise;
       
   214 VARDEF uint32 _network_last_advertise_frame;
       
   215 VARDEF uint8 _network_advertise_retries;
       
   216 
       
   217 VARDEF bool _network_autoclean_companies;
       
   218 VARDEF uint8 _network_autoclean_unprotected; // Remove a company after X months
       
   219 VARDEF uint8 _network_autoclean_protected;   // Unprotect a company after X months
       
   220 
       
   221 VARDEF Year _network_restart_game_year;      // If this year is reached, the server automaticly restarts
       
   222 VARDEF uint8 _network_min_players;           // Minimum number of players for game to unpause
       
   223 
       
   224 NetworkGameList *NetworkQueryServer(const char* host, unsigned short port, bool game_info);
       
   225 
       
   226 byte NetworkSpectatorCount(void);
       
   227 
       
   228 VARDEF char *_network_host_list[10];
       
   229 VARDEF char *_network_ban_list[25];
       
   230 
       
   231 void ParseConnectionString(const char **player, const char **port, char *connection_string);
       
   232 void NetworkUpdateClientInfo(uint16 client_index);
       
   233 void NetworkAddServer(const char *b);
       
   234 void NetworkRebuildHostList(void);
       
   235 bool NetworkChangeCompanyPassword(byte argc, char *argv[]);
       
   236 void NetworkPopulateCompanyInfo(void);
       
   237 void UpdateNetworkGameWindow(bool unselect);
       
   238 void CheckMinPlayers(void);
       
   239 
       
   240 void NetworkStartUp(void);
       
   241 void NetworkUDPClose(void);
       
   242 void NetworkShutDown(void);
       
   243 void NetworkGameLoop(void);
       
   244 void NetworkUDPGameLoop(void);
       
   245 bool NetworkServerStart(void);
       
   246 bool NetworkClientConnectGame(const char *host, uint16 port);
       
   247 void NetworkReboot(void);
       
   248 void NetworkDisconnect(void);
       
   249 
       
   250 VARDEF bool _networking;         ///< are we in networking mode?
       
   251 VARDEF bool _network_server;     ///< network-server is active
       
   252 VARDEF bool _network_available;  ///< is network mode available?
       
   253 
       
   254 #else /* ENABLE_NETWORK */
       
   255 /* Network function stubs when networking is disabled */
       
   256 
       
   257 static inline void NetworkStartUp(void) {}
       
   258 static inline void NetworkShutDown(void) {}
       
   259 
       
   260 #define _networking 0
       
   261 #define _network_server 0
       
   262 #define _network_available 0
       
   263 
       
   264 #endif /* ENABLE_NETWORK */
       
   265 
       
   266 /* These variables must always be registered! */
       
   267 VARDEF bool _network_dedicated;  ///< are we a dedicated server?
       
   268 VARDEF bool _network_advertise;  ///< is the server advertising to the master server?
       
   269 VARDEF PlayerID _network_playas; ///< an id to play as.. (see players.h:Players)
       
   270 
       
   271 #endif /* NETWORK_H */