1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef MULTI_UTIL_H
13 #define MULTI_UTIL_H
14 
15 #include "network/psnet2.h"
16 
17 // prototypes instead of headers :)
18 struct net_player;
19 struct net_addr;
20 class player;
21 struct button_info;
22 struct join_request;
23 struct physics_info;
24 class object;
25 struct active_game;
26 class ship;
27 struct server_item;
28 class ship_info;
29 class p_object;
30 
31 // two types of signatures that we can request,  permanent signatures are all below 5000.  non-permanent are above 5000
32 #define MULTI_SIG_SHIP					1
33 #define MULTI_SIG_ASTEROID				2
34 #define MULTI_SIG_NON_PERMANENT		3
35 #define MULTI_SIG_DEBRIS				4
36 #define MULTI_SIG_WAYPOINT				5				// Added for dynamic waypoints in multiplayer missions
37 
38 extern ushort multi_assign_network_signature( int what_kind );
39 extern ushort multi_get_next_network_signature( int what_kind );
40 extern void multi_set_network_signature( ushort signature, int what_kind );
41 
42 extern void stuff_netplayer_info( net_player *nplayer, net_addr *addr, int ship_class, player *pplayer );
43 extern int find_player(net_addr* addr);
44 extern int find_player_no_port(net_addr *addr);
45 extern int find_player_index(short player_id);
46 extern int find_player_socket(PSNET_SOCKET_RELIABLE sock);	// note this is only valid to do on a server!
47 extern int multi_find_player_by_object( object *obj );
48 extern int multi_find_player_by_signature( int signature );
49 extern int multi_find_player_by_callsign(const char *callsign);
50 extern int multi_find_player_by_net_signature(ushort net_signature);
51 extern int multi_find_player_by_parse_object(p_object *p_objp );
52 extern int multi_find_player_by_ship_name(const char *ship_name, bool inc_respawning = false);
53 extern void multi_create_player(int player_num, player *pl, const char* name, net_addr* addr, int ship_class, short id);
54 extern int multi_find_open_netplayer_slot();
55 extern int multi_find_open_player_slot();
56 extern void delete_player(int player_num, int kicked_reason = -1);
57 extern int multi_get_player_ship(int np_index);
58 
59 extern int multi_num_players();
60 extern int multi_num_observers();
61 extern int multi_num_connections();
62 
63 extern const char* multi_random_death_word();
64 extern const char* multi_random_chat_start();
65 
66 extern int multi_ship_class_lookup(const char* ship_name);
67 extern ushort netmisc_calc_checksum( void * vptr, int len );
68 extern void fill_net_addr(net_addr* addr, ubyte* address, ushort port);
69 extern char* get_text_address( char * text, ubyte * address );
70 
71 extern object *multi_get_network_object( ushort net_signature );		// find a network object
72 
73 void multi_find_ingame_join_pos(object *new_obj);
74 
75 // return size of packed matrix
76 void multi_pack_orient_matrix(ubyte *data,matrix *m);
77 
78 // return bytes processed
79 void multi_unpack_orient_matrix(ubyte *data,matrix *m);
80 
81 // catchall to do any necessary client-side simulation processing or master side process for menu pauses, etc.
82 void multi_do_client_warp(float frame_time);
83 
84 void multi_assign_player_ship( int net_player_num, object *objp, int ship_class );
85 
86 // -------------------------------------------------------------------
87 // ship status change functions (used both client and server side)
88 int lookup_ship_status(net_player *p, int unique_id, int remove=0);        // auto-remove if remove == 1
89 void remove_ship_status_item(net_player *p, int id);
90 void add_net_button_info(net_player *p, button_info *bi, int unique_id);
91 
92 // called client-side every frame
93 void multi_maybe_send_ship_status();
94 
95 // will be used server side _and_ client side.
96 void multi_apply_ship_status(net_player *p,button_info *bi, int locally);
97 
98 void multiplayer_match_target_speed(net_player *p);
99 
100 void server_verify_filesig(short player_id, ushort sum_sig, int length_sig);
101 int server_all_filesigs_ok();
102 
103 void multi_untag_player_ships();
104 
105 // broadcast alltime stats to everyone in the game
106 void multi_broadcast_stats(int stats_code);
107 
108 int multi_netplayer_state_check(int state, int ignore_standalone = 0);
109 int multi_netplayer_state_check2(int state, int state2, int ignore_standalone = 0);
110 int multi_netplayer_state_check3(int state, int state2, int state3, int ignore_standalone = 0);
111 int multi_netplayer_flag_check(int flags, int ignore_standalone = 0);
112 
113 void multi_eval_socket_error(PSNET_SOCKET sock, int error);
114 
115 void multi_maybe_send_repair_info(object *dest_obj, object *source_objp, int code);
116 
117 int multi_is_valid_unknown_packet(ubyte type);
118 
119 // create a bogus object for the standalone
120 void multi_create_standalone_object();
121 
122 // determine whether (as a server), you should be rebroadcasting certain messages to everyone in the game
123 int multi_message_should_broadcast(int type);
124 
125 // the active game list manager functions
126 active_game *multi_new_active_game( void );
127 active_game *multi_update_active_games(active_game *ag);
128 void multi_free_active_games();
129 
130 server_item *multi_new_server_item( void );
131 void multi_free_server_list();
132 
133 // netgame options evaluation stuff
134 int multi_can_message(net_player *p);
135 int multi_can_end_mission(net_player *p);
136 
137 int multi_eval_join_request(join_request *jr,net_addr *addr);
138 
139 // called by any machine (client, host, server, standalone, etc), to begin warping out all player objects
140 void multi_warpout_all_players();
141 
142 // determine the highest rank of any of the players in the game
143 int multi_get_highest_rank();
144 
145 // called on the machine of the player who hit alt+j
146 void multi_handle_end_mission_request();
147 
148 // called to handle any special cases where a player is in some submenu when he needs to get pushed into some other state
149 void multi_handle_state_special();
150 
151 // called by the file xfer subsytem when we start receiving a file
152 void multi_file_xfer_notify(int handle);
153 
154 // return the lag/disconnected status of the game
155 int multi_query_lag_status();
156 
157 // process a valid join request
158 void multi_process_valid_join_request(join_request *jr, net_addr *who_from, int ingame_join_team = -1);
159 
160 // if a player is trying to join a restricted game, evaluate the keypress (accept or not, etc)
161 int multi_process_restricted_keys(int k);
162 
163 // determine the status of available player ships (use team_0 for non team vs. team situations)
164 void multi_player_ships_available(int *team_0, int *team_1);
165 
166 // server should update the player's bank/link status with the data in the passed ship
167 void multi_server_update_player_weapons(net_player *pl, ship *shipp);
168 
169 // flush the multidata cache directory
170 void multi_flush_multidata_cache();
171 
172 // flush all data from a previous mission before starting the next
173 void multi_flush_mission_stuff();
174 
175 // should we ignore all controls and keypresses because of some multiplayer
176 int multi_ignore_controls(int key = -1);
177 
178 // if the kill limit has been reached by any given player
179 int multi_kill_limit_reached();
180 
181 // display a chat message (write to the correct spot - hud, standalone gui, chatbox, etc)
182 void multi_display_chat_msg(const char *msg, int player_index, int add_id);
183 
184 // fill in Current_file_checksum and Current_file_length
185 void multi_get_mission_checksum(const char *filename);
186 
187 // Packs/unpacks an object position.
188 // Returns number of bytes read or written.
189 int multi_pack_unpack_position(int write, ubyte *data, vec3d *pos);
190 
191 // Packs/unpacks an orientation matrix.
192 // Returns number of bytes read or written.
193 int multi_pack_unpack_orient(int write, ubyte *data, angles *angles_out);
194 
195 // Packs/unpacks velocity
196 // Returns number of bytes read or written.
197 int multi_pack_unpack_vel(int write, ubyte *data, matrix *orient, physics_info *pi);
198 
199 // Packs/unpacks rotational velocity
200 // Returns number of bytes read or written.
201 int multi_pack_unpack_rotvel(int write, ubyte *data, physics_info *pi);
202 
203 // Cyborg17 - Packs/unpacks desired velocity and rotational velocity.
204 int multi_pack_unpack_desired_vel_and_desired_rotvel(int write, bool full_physics, ubyte* data, physics_info* pi, vec3d* local_desired_vel);
205 
206 // Cyborg17 - Compresses the list of subsystems, so that we don't have to mark each one with a ubyte
207 int multi_pack_unpack_subsystem_list(bool write, ubyte* data, SCP_vector<ubyte>* flags, SCP_vector<float>* subsys_data);
208 
209 char multi_unit_to_char(float unit);
210 float multi_char_to_unit(float val);
211 
212 // if we should render our ping time to the server in a multiplayer game
213 int multi_show_ingame_ping();
214 
215 // if Game_current_mission_filename is a builtin multiplayer mission
216 int multi_is_builtin_mission();
217 
218 int multi_get_connection_speed();
219 
220 // if we're in tracker mode, do a validation update on all known missions
221 void multi_update_valid_missions();
222 
223 // get a new id# for a player
224 short multi_get_new_id();
225 
226 // Karajorma - sends the player to the correct debrief for this game type
227 void send_debrief_event();
228 
229 // Karajorma - Performs any cleanup needed by missions which don't end with a warpout.
230 void multi_handle_sudden_mission_end();
231 
232 // Send a ping to make sure the mission doesn't timeout during loading.
233 void multi_send_anti_timeout_ping();
234 
235 // make a bunch of fake players - don't rely on this to be very safe - its mostly used for interface testing
236 #ifndef NDEBUG
237 void multi_make_fake_players(int count);
238 #endif
239 
240 #endif
241