Debian Codegraph @

Descent3/multi_client.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/multi_client.cpp $
21 * $Revision: 57 $
22 * $Date: 9/20/01 1:10p $
23 * $Author: Matt $
24 *
25 * Multiplayer client code
26 *
27 * $Log: /DescentIII/Main/multi_client.cpp $
28 *
29 * 57 9/20/01 1:10p Matt
30 * Fix for client not taking fusion overchage damage in a peer-to-peer
31 * game.
32 *
33 * 56 9/02/99 3:34p Jason
34 * send secondary fire reliable in a C/S game
35 *
36 * 55 5/20/99 4:10p Jason
37 * added heartbeat to multiplayer so clients wouldn't time out, also
38 * various multiplayer fixes
39 *
40 * 54 5/17/99 6:26p Jason
41 * added message for when the reliable buffer overruns
42 *
43 * 53 5/13/99 6:20p Kevin
44 * fixed bug with client who's reliable connection was broken to the
45 * server not disconnecting
46 *
47 * 52 5/10/99 10:22p Ardussi
48 * changes to compile on Mac
49 *
50 * 51 5/05/99 5:41p Jason
51 * fixed various multiplayer issues, including sequencing bugs and cheat
52 * prevention
53 *
54 * 50 4/30/99 5:06p Kevin
55 * misc dedicated server, networking and low memory enhancements
56 *
57 * 49 4/27/99 4:42a Jeff
58 * pass guidebot name on MyInfo
59 *
60 * 48 4/14/99 2:51a Jeff
61 * fixed some case mismatched #includes
62 *
63 * 47 2/16/99 3:47p Jason
64 * added marker updates to multiplayer server stream
65 *
66 * 46 2/15/99 7:49p Jeff
67 * new pilot file class and read/write system checked in...should be more
68 * robust than old
69 *
70 * 45 2/15/99 12:24p Jason
71 * set default pps setting if permissable netgame
72 *
73 * 44 2/15/99 11:21a Jason
74 * took out client side interpolation
75 *
76 * 43 2/12/99 5:58p Jason
77 * more permissable server stuff
78 *
79 * 42 2/12/99 3:38p Jason
80 * added client side interpolation
81 *
82 * 41 2/04/99 11:01a Jason
83 * added anti cheating to multiplayer
84 *
85 * 40 12/21/98 10:22a Jason
86 * added auto pps
87 *
88 * 39 12/08/98 4:52p Jeff
89 * changed the pilot pics packing to use Ushort instead of int16_t just for
90 * my conscience...removed some annoying mprintf's too
91 *
92 * 38 12/01/98 5:48p Jeff
93 * added pilot picture id to netplayer struct
94 *
95 * 37 11/18/98 10:50a Jason
96 * fixed peer to peer with new architecture
97 *
98 * 36 11/16/98 4:47p Jason
99 * changes for multiplayer (weapons load sent and deleting destroyed
100 * lightmapped objects)
101 *
102 * 35 10/28/98 11:51a Jason
103 * fixed some multiplayer problems, plus redid coop a bit to make it
104 * cleaner
105 *
106 * 34 10/27/98 10:19a Jason
107 * changes for new arch
108 *
109 * 33 10/22/98 12:28p Kevin
110 * Fixed pings in peer-peer
111 *
112 * 32 10/19/98 7:18p Matt
113 * Added system to support different types of damage to the player and
114 * have these different types make different sounds.
115 *
116 * 31 10/01/98 4:03p Jason
117 * took out sequencing bug
118 *
119 * 30 10/01/98 11:36a Kevin
120 * UI fixes and stuff
121 *
122 * 29 10/01/98 11:29a Jason
123 * changes for world states in multiplayer games
124 *
125 * 28 9/11/98 12:26p Jason
126 * got energy to shield and fusion damage working in multiplayer
127 *
128 * 27 9/04/98 3:45p Kevin
129 * Added ping_time to Netplayers struct. It's updated in peer-peer and
130 * client server
131 *
132 * 26 8/05/98 10:46a Kevin
133 * sound & bmp exchange fixes
134 *
135 */
136
137 #include "multi.h"
138 #include "multi_client.h"
139 #include "game.h"
140 #include "player.h"
141 #include "ddio.h"
142 #include "pilot.h"
143 #include "Mission.h"
144 #include "stringtable.h"
145 #include "d3serial.h"
146 #include "ship.h"
147
148 #define WEAPONS_LOAD_UPDATE_INTERVAL 2.0
149
150 float Last_weapons_load_update_time = 0;
151
152 // Setup saved moves
153 saved_move SavedMoves[MAX_SAVED_MOVES];
154 int Current_saved_move = 0;
155
156 extern int Use_file_xfer;
157
158 // Tell the server about my info, such as name, ship type, etc
159 void MultiSendMyInfo() {
160 int size;
161 uint8_t data[MAX_GAME_DATA_SIZE];
162 int count = 0;
163
164 mprintf(0, "Sending my info\n");
165 char pshipmodel[PAGENAME_LEN];
166 Current_pilot.get_ship(pshipmodel);
167
168 int ship_index = FindShipName(pshipmodel);
169 if (ship_index < 0)
170 ship_index = 0;
171
172 size = START_DATA(MP_MY_INFO, data, &count);
173
174 // Do callsign name
175 MultiAddByte(Player_num, data, &count);
176 int len = strlen(Players[Player_num].callsign) + 1;
177 MultiAddByte(len, data, &count);
178 memcpy(&data[count], Players[Player_num].callsign, len);
179 count += len;
180
181 // Do ship name
182 len = strlen(Ships[ship_index].name) + 1;
183 MultiAddByte(len, data, &count);
184 memcpy(&data[count], Ships[ship_index].name, len);
185 count += len;
186
187 if (Game_is_master_tracker_game) {
188 MultiAddUint(MASTER_TRACKER_SIG, data, &count);
189 strcpy(Players[Player_num].tracker_id, Tracker_id);
190 len = strlen(Players[Player_num].tracker_id) + 1;
191 MultiAddByte(len, data, &count);
192 memcpy(&data[count], Players[Player_num].tracker_id, len);
193 count += len;
194 }
195 int ser = 0;
196 GetInternalSerializationNumber(&ser);
197 MultiAddInt(ser, data, &count);
198
199 // Send packets per second
200 int pps = nw_ReccomendPPS();
201 if ((Netgame.flags & NF_PERMISSABLE) && pps < 8)
202 pps = 8; // If permissable game, can't be lower than 8
203
204 MultiAddByte(pps, data, &count);
205
206 // pilot picture id
207 uint16_t ppic_id;
208 Current_pilot.get_multiplayer_data(NULL, NULL, NULL, &ppic_id);
209 MultiAddUshort(ppic_id, data, &count);
210
211 // Copy the guidebot name out
212 char guidebot_name[32];
213 Current_pilot.get_guidebot_name(guidebot_name);
214 len = strlen(guidebot_name) + 1;
215 MultiAddByte(len, data, &count);
216 memcpy(&data[count], guidebot_name, len);
217 count += len;
218
219 END_DATA(count, data, size);
220
221 nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count);
222 }
223
224 // Ask the server to tell me about the players
225 void MultiSendRequestForPlayers() {
226 int size;
227 uint8_t data[MAX_GAME_DATA_SIZE];
228 int count = 0;
229
230 mprintf(0, "Sending request for players\n");
231
232 size = START_DATA(MP_REQUEST_PLAYERS, data, &count);
233 MultiAddByte(Player_num, data, &count);
234 END_DATA(count, data, size);
235
236 nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count);
237 }
238
239 // Ask the server to tell me about the buildings
240 void MultiSendRequestForBuildings() {
241 int size;
242 uint8_t data[MAX_GAME_DATA_SIZE];
243 int count = 0;
244
245 mprintf(0, "Sending request for buildings\n");
246
247 size = START_DATA(MP_REQUEST_BUILDINGS, data, &count);
248 MultiAddByte(Player_num, data, &count);
249 END_DATA(count, data, size);
250
251 nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count);
252 }
253
254 // Ask the server to tell me about the world
255 void MultiSendRequestForWorldStates() {
256 int size;
257 uint8_t data[MAX_GAME_DATA_SIZE];
258 int count = 0;
259
260 mprintf(0, "Sending request for world states\n");
261
262 size = START_DATA(MP_REQUEST_WORLD_STATES, data, &count);
263 MultiAddByte(Player_num, data, &count);
264 // Send the digest (checksum) for this level + our salt + the ships
265 // Do level checksum
266 for (int i = 0; i < 16; i++) {
267 MultiAddByte(NetPlayers[Player_num].digest[i], data, &count);
268 }
269
270 END_DATA(count, data, size);
271
272 nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count);
273 }
274
275 // Ask the server to tell me about the objects
276 void MultiSendRequestForObjects() {
277 int size;
278 uint8_t data[MAX_GAME_DATA_SIZE];
279 int count = 0;
280
281 mprintf(0, "Sending request for objects\n");
282
283 size = START_DATA(MP_REQUEST_OBJECTS, data, &count);
284 MultiAddByte(Player_num, data, &count);
285 END_DATA(count, data, size);
286
287 nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count);
288 }
289
290 #define SERVER_DISCONNECT_TIME 8.0
291
292 // Returns 1 if the server is gone!
293 int ServerTimedOut() {
294 // Don't check for a non-connected player
295 if (!(NetPlayers[Player_num].flags & NPF_CONNECTED))
296 return 0;
297
298 if (!nw_CheckReliableSocket(NetPlayers[Player_num].reliable_socket)) {
299 mprintf(0, "Reliable connection to the server was broken. Disconnecting.\n");
300 ShowProgressScreen(TXT_RELIABLE_OVERRUN);
301 Sleep(1000);
302 return 1;
303 }
304 if ((timer_GetTime() - Netgame.last_server_time) > SERVER_DISCONNECT_TIME)
305 return 1;
306
307 return 0;
308 }
309 #define NET_CLIENT_GAMETIME_REQ_TIMEOUT 10
310 #define NET_CLIENT_GAMETIME_REQ_RETRY 1
311 float First_gametime_req = 0;
312 float Last_gametime_req = 0;
313
314 // Do client stuff for this frame
315 void MultiDoClientFrame() {
316 int i;
317 Multi_last_sent_time[Player_num][0] += Frametime;
318
319 // Get data from the server
320 MultiProcessIncoming();
321
322 if (Netgame.flags & NF_EXIT_NOW) {
323 // DLL says we should bail!
324 Netgame.flags &= ~NF_EXIT_NOW;
325 MultiLeaveGame();
326 SetFunctionMode(MENU_MODE);
327 return;
328 }
329
330 if (NetPlayers[Player_num].sequence == NETSEQ_LEVEL_START) {
331 MultiSendMyInfo();
332 NetPlayers[Player_num].sequence = NETSEQ_NEED_GAMETIME;
333 First_gametime_req = timer_GetTime();
334 } else if (NetPlayers[Player_num].sequence == NETSEQ_NEED_GAMETIME) {
335 // Ask for gametime
336 GetServerGameTime();
337 Last_gametime_req = timer_GetTime();
338 NetPlayers[Player_num].sequence = NETSEQ_WAIT_GAMETIME;
339 } else if (NetPlayers[Player_num].sequence == NETSEQ_WAIT_GAMETIME) {
340 if (Got_new_game_time) {
341 NetPlayers[Player_num].sequence = NETSEQ_REQUEST_PLAYERS;
342 } else {
343 // Wait for server response
344 if ((timer_GetTime() - First_gametime_req) > NET_CLIENT_GAMETIME_REQ_TIMEOUT) {
345 // Giving up, we waited too long.
346 mprintf(0, "Server disconnected while waiting for gametime!\n");
347 MultiLeaveGame();
348 ShowProgressScreen(TXT_MLTDISCFRMSERV);
349 Sleep(2000);
350 } else if ((timer_GetTime() - Last_gametime_req) > NET_CLIENT_GAMETIME_REQ_RETRY) {
351 NetPlayers[Player_num].sequence = NETSEQ_NEED_GAMETIME;
352 }
353 }
354
355 } else if (NetPlayers[Player_num].sequence == NETSEQ_REQUEST_PLAYERS) {
356 MultiSendRequestForPlayers();
357 NetPlayers[Player_num].sequence = NETSEQ_PLAYERS;
358 } else if (NetPlayers[Player_num].sequence == NETSEQ_REQUEST_BUILDINGS) {
359 MultiSendRequestForBuildings();
360 NetPlayers[Player_num].sequence = NETSEQ_BUILDINGS;
361 } else if (NetPlayers[Player_num].sequence == NETSEQ_REQUEST_OBJECTS) {
362 MultiSendRequestForObjects();
363 NetPlayers[Player_num].sequence = NETSEQ_OBJECTS;
364 } else if (NetPlayers[Player_num].sequence == NETSEQ_REQUEST_WORLD) {
365 MultiSendRequestForWorldStates();
366 NetPlayers[Player_num].sequence = NETSEQ_WORLD;
367 NetPlayers[Player_num].custom_file_seq = 0;
368 } else if (NetPlayers[Player_num].sequence == NETSEQ_PLAYING) {
369 if (NetPlayers[Player_num].custom_file_seq == 0) {
370 // Tell the server about our custom data (once only)
371 if (NetPlayers[Player_num].ship_logo[0])
372 PlayerSetCustomTexture(Player_num, NetPlayers[Player_num].ship_logo);
373 NetPlayers[Player_num].custom_file_seq = 0xff;
374
375 // Make sure we as the client actually want to send our custom data
376 if (Use_file_xfer)
377 MultiSendClientCustomData(Player_num);
378 }
379
380 bool client_file_xfering = false;
381 for (i = 0; i < MAX_NET_PLAYERS; i++) {
382 if (NetPlayers[i].file_xfer_flags != NETFILE_NONE)
383 client_file_xfering = true;
384 }
385 // See if we have room for a transfer
386 if (!client_file_xfering) {
387 // Check to see if we need to request any files from this bozo
388 for (i = 0; i < MAX_NET_PLAYERS; i++) {
389 if (i == Player_num)
390 continue;
391 if ((NetPlayers[i].custom_file_seq != NETFILE_ID_NOFILE) &&
392 (NetPlayers[i].custom_file_seq != NETFILE_ID_DONE)) {
393 // See if we are already sending or receiving a file from this player
394 if (NetPlayers[i].file_xfer_flags == NETFILE_NONE) {
395 // Check to see if we have this file in our cache
396 // Time to ask for the file specified in the sequence
397 MultiAskForFile(NetPlayers[i].custom_file_seq, i, 0);
398 break;
399 }
400 }
401 }
402 }
403
404 Last_weapons_load_update_time += Frametime;
405 if (Last_weapons_load_update_time > WEAPONS_LOAD_UPDATE_INTERVAL) {
406 Last_weapons_load_update_time = 0;
407 MultiSendWeaponsLoad();
408 }
409
410 // Figure out if we need to send our movement
411 bool send_it = false;
412
413 if (Player_fire_packet[Player_num].fired_on_this_frame != PFP_NO_FIRED)
414 send_it = true;
415
416 if (Multi_last_sent_time[Player_num][0] > (1.0 / (float)NetPlayers[Player_num].pps))
417 send_it = true;
418
419 if (send_it) {
420 // Request damage if need be
421 if (Multi_requested_damage_amount != 0) {
422 if ((Netgame.local_role == LR_CLIENT) && (Netgame.flags & NF_PEER_PEER)) {
423 MultiSendRequestPeerDamage(&Objects[Players[Player_num].objnum], -1, Multi_requested_damage_type,
424 Multi_requested_damage_amount);
425 } else {
426 MultiSendRequestDamage(Multi_requested_damage_type, Multi_requested_damage_amount);
427 }
428 Multi_requested_damage_amount = 0;
429 }
430
431 // Request shields if need be
432 for (i = 0; i < MAX_SHIELD_REQUEST_TYPES; i++) {
433 if (Multi_additional_shields[i] != 0) {
434 MultiSendRequestShields(i, Multi_additional_shields[i]);
435 Multi_additional_shields[i] = 0;
436 }
437 }
438
439 uint8_t data[MAX_GAME_DATA_SIZE], count = 0, add_count = 0;
440
441 count = MultiStuffPosition(Player_num, data);
442
443 // Send firing if needed
444 if (Player_fire_packet[Player_num].fired_on_this_frame == PFP_FIRED)
445 add_count = MultiStuffPlayerFire(Player_num, &data[count]);
446
447 count += add_count;
448
449 // Add in guided stuff
450 if (Players[Player_num].guided_obj != NULL) {
451 add_count = MultiStuffGuidedInfo(Player_num, &data[count]);
452 count += add_count;
453 }
454
455 ASSERT(count < MAX_GAME_DATA_SIZE);
456
457 if (Netgame.flags & NF_PEER_PEER) {
458 for (i = 0; i < MAX_NET_PLAYERS; i++) {
459 if (!(NetPlayers[i].flags & NPF_CONNECTED))
460 continue;
461 if (i == Player_num)
462 continue;
463
464 // Now get the ping time if needed
465 if ((timer_GetTime() - NetPlayers[i].last_ping_time) > MULTI_PING_INTERVAL)
466 MultiSendPing(i);
467
468 nw_Send(&NetPlayers[i].addr, data, count, 0);
469 }
470 } else
471 nw_Send(&Netgame.server_address, data, count, 0);
472
473 // TODO: SEND RELIABLE WEAPON FIRE
474 if (Player_fire_packet[Player_num].fired_on_this_frame == PFP_FIRED_RELIABLE) {
475 // mprintf(0,"I NEED TO SEND RELIABLE FIRE\n");
476 count = MultiStuffPlayerFire(Player_num, data);
477 nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count, true);
478 }
479
480 // Clear our data
481 Player_fire_packet[Player_num].fired_on_this_frame = PFP_NO_FIRED;
482 Multi_last_sent_time[Player_num][0] = 0;
483 Players[Player_num].flags &= ~PLAYER_FLAGS_SEND_MOVEMENT;
484 }
485
486 if (ServerTimedOut()) {
487 mprintf(0, "Server disconnected!\n");
488 MultiLeaveGame();
489 ShowProgressScreen(TXT_MLTDISCFRMSERV);
490 Sleep(2000);
491 }
492 }
493 }
494
495 // Initializes some fields for a network client to play
496 // Client only
497 void MultiStartClient(char *scriptname) {
498 int i;
499
500 for (i = 0; i < MAX_NET_PLAYERS; i++) {
501 memset(&NetPlayers[i], 0, sizeof(netplayer));
502 }
503
504 for (i = 0; i < MAX_SAVED_MOVES; i++)
505 SavedMoves[i].timestamp = 0;
506
507 Current_saved_move = 0;
508
509 // Temporary name fix
510 Current_pilot.get_name(Players[Player_num].callsign);
511
512 Multi_send_size[Player_num] = 0;
513 Multi_last_sent_time[Player_num][0] = 0;
514 Last_weapons_load_update_time = 0;
515
516 Netgame.local_role = LR_CLIENT;
517 Game_mode = GM_NETWORK;
518 SetGamemodeScript(scriptname);
519 }