| 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_connect.cpp $ |
| 21 | * $Revision: 82 $ |
| 22 | * $Date: 10/10/01 11:33a $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/multi_connect.cpp $ |
| 28 | * |
| 29 | * 82 10/10/01 11:33a Matt |
| 30 | * Do checksums on all ships, not just the client's ship. (This fixes a |
| 31 | * problem where the server could cheat because his ship was never |
| 32 | * checked.) |
| 33 | * |
| 34 | * 81 10/08/01 3:08p Matt |
| 35 | * Added physics flags & model radius to ship checksum. |
| 36 | * |
| 37 | * 80 8/29/01 2:51p Matt |
| 38 | * Improved ship weapon checksum calculation to fix cheating by modifiying |
| 39 | * weapon values. |
| 40 | * |
| 41 | * 79 2/01/00 3:47a Jason |
| 42 | * made usesmoothing be a client side option only |
| 43 | * |
| 44 | * 78 10/21/99 4:56p Matt |
| 45 | * Mac merge |
| 46 | * |
| 47 | * 77 5/23/99 4:17a Jason |
| 48 | * fixed potential ranking bug |
| 49 | * |
| 50 | * 76 5/23/99 3:05a Jason |
| 51 | * fixed bug with player rankings not being updated correctly |
| 52 | * |
| 53 | * 75 5/21/99 7:08p Jason |
| 54 | * fixed some weird multiplayer rejoin anomalies |
| 55 | * |
| 56 | * 74 5/21/99 11:38a Jason |
| 57 | * fixed bug with server heartbeats |
| 58 | * |
| 59 | * 73 5/20/99 4:55p Jason |
| 60 | * added heartbeats to server |
| 61 | * |
| 62 | * 72 5/20/99 4:10p Jason |
| 63 | * added heartbeat to multiplayer so clients wouldn't time out, also |
| 64 | * various multiplayer fixes |
| 65 | * |
| 66 | * 71 5/13/99 6:53p Jason |
| 67 | * upped retry time for clients connecting to servers |
| 68 | * |
| 69 | * 70 5/10/99 10:22p Ardussi |
| 70 | * changes to compile on Mac |
| 71 | * |
| 72 | * 69 5/09/99 1:34p Kevin |
| 73 | * Added diffuculty level system to multiplayer |
| 74 | * |
| 75 | * 68 5/07/99 3:16p Jason |
| 76 | * fixed crash bug with Player_object being null |
| 77 | * |
| 78 | * 67 5/07/99 2:50p Jason |
| 79 | * fixed a bunch of endlevel multiplayer issues |
| 80 | * |
| 81 | * 66 5/05/99 5:41p Jason |
| 82 | * fixed various multiplayer issues, including sequencing bugs and cheat |
| 83 | * prevention |
| 84 | * |
| 85 | * 65 5/05/99 12:24p Jason |
| 86 | * fixed some problems with ending levels |
| 87 | * |
| 88 | * 64 5/03/99 3:35p Kevin |
| 89 | * bug fixes |
| 90 | * |
| 91 | * 63 4/29/99 7:50p Jason |
| 92 | * fixed level ending problems |
| 93 | * |
| 94 | * 62 4/26/99 4:16p Jason |
| 95 | * added check for cheaters |
| 96 | * |
| 97 | * 61 4/16/99 11:56a Matt |
| 98 | * Changed directplay code to be "ifdef _WIN32" instead of "ifndef |
| 99 | * __LINUX__" so it will work on the Mac. |
| 100 | * |
| 101 | * 60 4/15/99 1:41a Jeff |
| 102 | * changes for linux compile |
| 103 | * |
| 104 | * 59 4/14/99 2:51a Jeff |
| 105 | * fixed some case mismatched #includes |
| 106 | * |
| 107 | * 58 3/22/99 6:21p Jeff |
| 108 | * added 2 more audio taunts. a mulitplayer event when someone plays an |
| 109 | * audio taunt. option to disable audio taunts. |
| 110 | * |
| 111 | * 57 3/17/99 4:08p Kevin |
| 112 | * Changed the way games appear and timeout in the game list. |
| 113 | * |
| 114 | * 56 2/15/99 7:49p Jeff |
| 115 | * new pilot file class and read/write system checked in...should be more |
| 116 | * robust than old |
| 117 | * |
| 118 | * 55 2/01/99 4:17p Jason |
| 119 | * more changes for multisafe |
| 120 | * |
| 121 | * 54 1/29/99 5:22p Jeff |
| 122 | * localization |
| 123 | * |
| 124 | * 53 1/27/99 5:47p Jeff |
| 125 | * audio taunts implemented! |
| 126 | * |
| 127 | * 52 1/07/99 11:51a Kevin |
| 128 | * Added support for joining servers on alternate ports and hosting behind |
| 129 | * a proxy/firewall |
| 130 | * |
| 131 | * 51 11/19/98 11:27a Jason |
| 132 | * added Multi_accept_state for Jeff |
| 133 | * |
| 134 | * 50 11/10/98 11:16a Jason |
| 135 | * fixed some multiplayer bugs with powerups disappearing |
| 136 | * |
| 137 | * |
| 138 | * 49 11/02/98 6:53p Jason |
| 139 | * made different game versions not show up in game list |
| 140 | * |
| 141 | * 48 10/28/98 5:07p Jason |
| 142 | * more changes for cleaner multiplayer |
| 143 | * |
| 144 | * 47 10/28/98 11:51a Jason |
| 145 | * fixed some multiplayer problems, plus redid coop a bit to make it |
| 146 | * cleaner |
| 147 | * |
| 148 | * 46 10/08/98 7:30p Samir |
| 149 | * game sequencing changes. |
| 150 | * |
| 151 | * 45 10/07/98 11:34a Kevin |
| 152 | * Added connecting progress screen |
| 153 | * |
| 154 | * 44 10/05/98 5:44p Jason |
| 155 | * added netgame flags to connection accepted packet |
| 156 | * |
| 157 | * 43 9/28/98 12:56p Jason |
| 158 | * multiplayer bug fixes |
| 159 | * |
| 160 | * 42 9/02/98 6:54p Kevin |
| 161 | * Fixed general directplay support up, and got modem-modem working |
| 162 | * |
| 163 | * 41 8/27/98 5:03p Kevin |
| 164 | * Prettied up multiplayer screens and fixed some bugs. |
| 165 | * |
| 166 | * 40 8/19/98 11:49a Kevin |
| 167 | * Got DirectPlay IPX working, and localized connection DLLs |
| 168 | * |
| 169 | * 39 8/14/98 4:54p Kevin |
| 170 | * More directplay stuff |
| 171 | * |
| 172 | * 38 8/05/98 2:25p Kevin |
| 173 | * improved network address reporting |
| 174 | * |
| 175 | * 37 8/04/98 6:21p Jeff |
| 176 | * send over Game name on connect |
| 177 | * |
| 178 | * 36 8/04/98 10:26a Kevin |
| 179 | * Custom sound and bmp exchange system |
| 180 | * |
| 181 | * 35 7/29/98 5:39p Kevin |
| 182 | * sound/bitmap exchange |
| 183 | * |
| 184 | * 34 7/27/98 5:31p Kevin |
| 185 | * Sound/Bitmap exchange system |
| 186 | * |
| 187 | * 33 7/21/98 1:49p Kevin |
| 188 | * IPX support and peer-peer option for multi |
| 189 | * |
| 190 | * 32 7/20/98 6:20p Kevin |
| 191 | * peer-peer stuff |
| 192 | * |
| 193 | * 31 7/17/98 1:22p Kevin |
| 194 | * Dynamic retransmission of reliable packets and stuff |
| 195 | * |
| 196 | * 30 7/13/98 11:52a Kevin |
| 197 | * Added Callscriptwithparms |
| 198 | * |
| 199 | * 29 6/30/98 3:22p Kevin |
| 200 | * Improved game ping time |
| 201 | * |
| 202 | * 28 6/25/98 5:22p Kevin |
| 203 | * Req/Send gametime to clients |
| 204 | * |
| 205 | * 27 6/24/98 3:24p Kevin |
| 206 | * Updated PXO screens with chat, etc. |
| 207 | * |
| 208 | * 26 6/18/98 4:49p Kevin |
| 209 | * Updated multiplayer menus |
| 210 | * |
| 211 | * 25 6/16/98 10:54a Jeff |
| 212 | * |
| 213 | * 24 6/01/98 10:09a Kevin |
| 214 | * Added DLL connection interface and auto update DLL |
| 215 | * |
| 216 | * 23 5/14/98 5:27p Jason |
| 217 | * fixed some more sequencing bugs |
| 218 | * |
| 219 | * 22 5/14/98 3:18p Jason |
| 220 | * finished multiplayer sequencing issues |
| 221 | * |
| 222 | * 21 5/14/98 11:07a Kevin |
| 223 | * Made gameover packet to the tracker reliable |
| 224 | * |
| 225 | * 20 5/12/98 12:33p Jason |
| 226 | * got level sequencing working in multiplayer |
| 227 | * |
| 228 | * 19 5/11/98 4:40p Chris |
| 229 | * AI info is now a malloc'ed pointer |
| 230 | * |
| 231 | * 18 5/01/98 5:34p Jason |
| 232 | * added debug mprintf |
| 233 | * |
| 234 | * 17 4/30/98 3:50p Kevin |
| 235 | * Mastertracker pilot stats |
| 236 | * |
| 237 | * 16 4/24/98 3:50p Kevin |
| 238 | * Added mastertracker game tracking support |
| 239 | * |
| 240 | * 15 4/24/98 11:06a Kevin |
| 241 | * Fixed problem where Ok_to_join was set to -1 when the user tried to |
| 242 | * join a server that wasn't running. |
| 243 | * |
| 244 | * 14 4/20/98 12:46p Jason |
| 245 | * added level checksumming for multiplayer games |
| 246 | * |
| 247 | * 13 4/15/98 8:38p Jason |
| 248 | * fixed some multiplayer issues |
| 249 | * |
| 250 | * 12 4/14/98 7:56p Matt |
| 251 | * Moved MSN_NAMELEN from mission.h to descent,h, so multi.h didn't need |
| 252 | * to include mission.h. |
| 253 | * |
| 254 | */ |
| 255 | |
| 256 | #include "multi.h" |
| 257 | #include "multi_server.h" |
| 258 | #include "player.h" |
| 259 | #include "game.h" |
| 260 | #include "ddio.h" |
| 261 | #include "Mission.h" |
| 262 | // #include "gametrack.h" |
| 263 | #include "stringtable.h" |
| 264 | #include "pilot.h" |
| 265 | #include "ship.h" |
| 266 | #include "args.h" |
| 267 | |
| 268 | #include "ui.h" |
| 269 | #include "newui.h" |
| 270 | #include "multi_dll_mgr.h" |
| 271 | |
| 272 | #include "LoadLevel.h" |
| 273 | // #define USE_DIRECTPLAY |
| 274 | |
| 275 | #ifdef USE_DIRECTPLAY |
| 276 | #include "directplay.h" |
| 277 | #endif |
| 278 | |
| 279 | SOCKET ReliableSocket; |
| 280 | int Ok_to_join = -1; |
| 281 | |
| 282 | int Got_level_info = 0; |
| 283 | |
| 284 | // Whether or not the server will answer |
| 285 | bool Multi_accept_state = true; |
| 286 | |
| 287 | // Points the server_address variable at a new location |
| 288 | void MultiSetServerAddress(network_address *addr) { memcpy(&Netgame.server_address, addr, sizeof(network_address)); } |
| 289 | |
| 290 | // How often to inquire about joining a game |
| 291 | #define ASK_POLL_TIME 2 |
| 292 | |
| 293 | // Returns true if the server says we can join |
| 294 | int AskToJoin(network_address *addr) { |
| 295 | int count = 0; |
| 296 | int size; |
| 297 | int tries = 0; |
| 298 | uint8_t data[MAX_GAME_DATA_SIZE]; |
| 299 | float start_time; |
| 300 | network_address from_addr; |
| 301 | |
| 302 | size = START_DATA(MP_ASK_TO_JOIN, data, &count); |
| 303 | END_DATA(count, data, size); |
| 304 | |
| 305 | Ok_to_join = -1; |
| 306 | |
| 307 | while (tries < 5 && Ok_to_join == -1) { |
| 308 | // Send of our question and then wait! |
| 309 | ASSERT(size > 0); |
| 310 | nw_Send(addr, data, count, 0); |
| 311 | tries++; |
| 312 | |
| 313 | start_time = timer_GetTime(); |
| 314 | while ((timer_GetTime() - start_time < ASK_POLL_TIME) && Ok_to_join == -1) { |
| 315 | int packsize; |
| 316 | while (((packsize = nw_Receive(Multi_receive_buffer, &from_addr)) > 0) && Ok_to_join == -1) { |
| 317 | MultiProcessBigData(Multi_receive_buffer, packsize, &from_addr); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (tries >= 5 || Ok_to_join != JOIN_ANSWER_OK) |
| 323 | return 0; |
| 324 | |
| 325 | // Ok to join! |
| 326 | return 1; |
| 327 | } |
| 328 | |
| 329 | // Trys to join this client with the server address passed in |
| 330 | // Returns 1 on success, 0 on fail |
| 331 | // Client only |
| 332 | // For DirectPlay games, use the addr pointer as a pointer to the DPSESSIONDESC2 |
| 333 | int TryToJoinServer(network_address *addr) { |
| 334 | SOCKET sock; |
| 335 | |
| 336 | ShowProgressScreen(TXT_MULTI_CONNECTING); |
| 337 | // Store the server's address in the netplayer struct |
| 338 | memcpy(&NetPlayers[0].addr, addr, sizeof(network_address)); |
| 339 | |
| 340 | for (int i = 0; i < MAX_NET_PLAYERS; i++) { |
| 341 | NetPlayers[i].file_xfer_flags = NETFILE_NONE; |
| 342 | NetPlayers[i].ship_logo[0] = 0; |
| 343 | NetPlayers[i].voice_taunt1[0] = 0; |
| 344 | NetPlayers[i].voice_taunt2[0] = 0; |
| 345 | NetPlayers[i].voice_taunt3[0] = 0; |
| 346 | NetPlayers[i].voice_taunt4[0] = 0; |
| 347 | NetPlayers[i].custom_file_seq = NETFILE_ID_NOFILE; |
| 348 | // Set all custom textures to NULL |
| 349 | PlayerSetCustomTexture(i, NULL); |
| 350 | } |
| 351 | Current_pilot.get_multiplayer_data(NetPlayers[Player_num].ship_logo, NetPlayers[Player_num].voice_taunt1, |
| 352 | NetPlayers[Player_num].voice_taunt2, NULL, NetPlayers[Player_num].voice_taunt3, |
| 353 | NetPlayers[Player_num].voice_taunt4); |
| 354 | if (NetPlayers[Player_num].ship_logo[0]) |
| 355 | PlayerSetCustomTexture(Player_num, NetPlayers[Player_num].ship_logo); |
| 356 | |
| 357 | bool connected = false; |
| 358 | #ifdef USE_DIRECTPLAY |
| 359 | if (Use_DirectPlay) { |
| 360 | LPDPSESSIONDESC2 session = (LPDPSESSIONDESC2)addr; |
| 361 | DPID server_id = DPID_SERVERPLAYER; |
| 362 | Netgame.server_address.connection_type = NP_DIRECTPLAY; |
| 363 | memcpy(&Netgame.server_address.address, &server_id, sizeof(DPID)); |
| 364 | if (dp_DirectPlayJoinGame(session)) { |
| 365 | connected = true; |
| 366 | NetPlayers[Player_num].reliable_socket = DPID_SERVERPLAYER; |
| 367 | } else { |
| 368 | mprintf(0, "Unable to join DirectPlay session!\n"); |
| 369 | } |
| 370 | } else |
| 371 | #endif |
| 372 | { |
| 373 | if (!AskToJoin(addr)) { |
| 374 | if (Ok_to_join != -1) { |
| 375 | char str[255]; |
| 376 | snprintf(str, sizeof(str), "%s", TXT(Join_response_strings[Ok_to_join])); |
| 377 | ShowProgressScreen(str); |
| 378 | Sleep(2000); |
| 379 | } else { |
| 380 | ShowProgressScreen(TXT_MLTNORESPONSE); |
| 381 | Sleep(2000); |
| 382 | } |
| 383 | return 0; |
| 384 | } else |
| 385 | mprintf(0, "Server says its ok to join!\n"); |
| 386 | |
| 387 | nw_ConnectToServer(&sock, addr); |
| 388 | if (sock != INVALID_SOCKET && sock != 0) { |
| 389 | connected = true; |
| 390 | NetPlayers[Player_num].reliable_socket = sock; |
| 391 | } |
| 392 | } |
| 393 | if (connected) { |
| 394 | #ifdef USE_DIRECTPLAY |
| 395 | if (!Use_DirectPlay) |
| 396 | #endif |
| 397 | { |
| 398 | MultiSetServerAddress(addr); |
| 399 | } |
| 400 | mprintf(0, "Client mode set! Polling...\n"); |
| 401 | |
| 402 | MultiPollForConnectionAccepted(); |
| 403 | |
| 404 | if (NetPlayers[Player_num].flags & NPF_CONNECTED) |
| 405 | return 1; |
| 406 | else { |
| 407 | mprintf(0, "Couldn't join game for some reason!\n"); |
| 408 | #ifdef USE_DIRECTPLAY |
| 409 | if (!Use_DirectPlay) |
| 410 | #endif |
| 411 | { |
| 412 | nw_CloseSocket(&sock); |
| 413 | } |
| 414 | } |
| 415 | } else { |
| 416 | mprintf(0, "nw_ConnectToServer says it can't find a good socket!\n"); |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | // The server says we can join! |
| 423 | // Client only |
| 424 | void MultiDoConnectionAccepted(uint8_t *data) { |
| 425 | int count = 0; |
| 426 | uint16_t server_version; |
| 427 | |
| 428 | // Skip header |
| 429 | SKIP_HEADER(data, &count); |
| 430 | |
| 431 | server_version = MultiGetShort(data, &count); |
| 432 | |
| 433 | if (server_version != MULTI_VERSION) { |
| 434 | mprintf(0, "Client and server code versions don't match. Do an update!\n"); |
| 435 | return; |
| 436 | } else { |
| 437 | // Versions match, get info about the game and then connect! |
| 438 | mprintf(0, "Client/server versions match.\n"); |
| 439 | |
| 440 | // Check if we have the mission needed |
| 441 | uint8_t len = MultiGetByte(data, &count); |
| 442 | memcpy(Netgame.mission, &data[count], len); |
| 443 | count += len; |
| 444 | |
| 445 | if (!LoadMission(Netgame.mission)) { |
| 446 | mprintf(0, "We don't have this mission: %s!\n", Netgame.mission); |
| 447 | } else |
| 448 | mprintf(0, "Using mission %s...\n", Netgame.mission); |
| 449 | |
| 450 | len = MultiGetByte(data, &count); |
| 451 | memcpy(Netgame.name, &data[count], len); |
| 452 | count += len; |
| 453 | |
| 454 | // Do script name stuff |
| 455 | len = MultiGetByte(data, &count); |
| 456 | memcpy(Netgame.scriptname, &data[count], len); |
| 457 | count += len; |
| 458 | |
| 459 | SetGamemodeScript(Netgame.scriptname); |
| 460 | |
| 461 | // Get my player num and copy my name over |
| 462 | uint8_t player_num = MultiGetByte(data, &count); |
| 463 | char name[CALLSIGN_LEN]; |
| 464 | netplayer tempplayer; |
| 465 | strcpy(name, Players[Player_num].callsign); |
| 466 | memcpy(&tempplayer, &NetPlayers[Player_num], sizeof(netplayer)); |
| 467 | |
| 468 | Player_num = player_num; |
| 469 | mprintf(0, "Server tells me that my player num is %d!\n", Player_num); |
| 470 | strcpy(Players[Player_num].callsign, name); |
| 471 | memcpy(&NetPlayers[Player_num], &tempplayer, sizeof(netplayer)); |
| 472 | NetPlayers[Player_num].flags = NPF_CONNECTED; // Hurray! We're connected |
| 473 | |
| 474 | // Get packets per second |
| 475 | Netgame.packets_per_second = MultiGetByte(data, &count); |
| 476 | mprintf(0, "Server is sending %d packets per second\n", Netgame.packets_per_second); |
| 477 | |
| 478 | // Get the secret code we will use to identify ourselves to the server |
| 479 | uint32_t secret_code = MultiGetUint(data, &count); |
| 480 | |
| 481 | // Get the peer-peer flag |
| 482 | int flags = MultiGetInt(data, &count); |
| 483 | if (flags & NF_PEER_PEER) |
| 484 | mprintf(0, "Using Peer/Peer model\n"); |
| 485 | else |
| 486 | mprintf(0, "Using Client/Server model\n"); |
| 487 | |
| 488 | Netgame.flags = flags; |
| 489 | |
| 490 | // Do Client smoothing hack |
| 491 | if (FindArg("-usesmoothing")) |
| 492 | Netgame.flags |= NF_USE_SMOOTHING; |
| 493 | |
| 494 | MultiSendGreetings(secret_code); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | #define JOIN_POLL_TIME 10 // Wait 4 seconds before bailing |
| 499 | // Polls for a connection message so we can finally join this game |
| 500 | // Client only |
| 501 | void MultiPollForConnectionAccepted() { |
| 502 | float start_time; |
| 503 | int connected = 0; |
| 504 | uint8_t data[MAX_RECEIVE_SIZE]; |
| 505 | |
| 506 | start_time = timer_GetTime(); |
| 507 | |
| 508 | while ((timer_GetTime() - start_time < JOIN_POLL_TIME) && !connected) { |
| 509 | int size; |
| 510 | while ((size = nw_ReceiveReliable(NetPlayers[Player_num].reliable_socket, data, MAX_RECEIVE_SIZE)) > 0) { |
| 511 | MultiProcessBigData(data, size, &Netgame.server_address); |
| 512 | |
| 513 | if (NetPlayers[Player_num].flags & NPF_CONNECTED) { |
| 514 | connected = 1; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (!connected) { |
| 520 | mprintf(0, "Couldn't get a connection_accepted packet for some reason!\n"); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Gets a new connection to a new client set up |
| 525 | // This does not mean the client will enter the game, just that the server |
| 526 | // is successfully talking to the client |
| 527 | // Server only |
| 528 | void MultiSendConnectionAccepted(int slotnum, SOCKET sock, network_address *addr) { |
| 529 | uint8_t data[MAX_GAME_DATA_SIZE]; |
| 530 | int count = 0; |
| 531 | int size_offset; |
| 532 | |
| 533 | mprintf(0, "Sending connection accepted packet to slot %d!\n", slotnum); |
| 534 | |
| 535 | NetPlayers[slotnum].reliable_socket = sock; |
| 536 | memcpy(&NetPlayers[slotnum].addr, addr, sizeof(network_address)); |
| 537 | NetPlayers[slotnum].flags = NPF_CONNECTED; |
| 538 | NetPlayers[slotnum].sequence = NETSEQ_WAITING_FOR_LEVEL; |
| 539 | Players[slotnum].flags = 0; |
| 540 | Players[slotnum].rank = -1; |
| 541 | |
| 542 | // Adjust port |
| 543 | NetPlayers[slotnum].addr.port--; |
| 544 | |
| 545 | NetPlayers[slotnum].last_packet_time = timer_GetTime(); |
| 546 | |
| 547 | // Tell the new guy about the game in progress |
| 548 | size_offset = START_DATA(MP_CONNECTION_ACCEPTED, data, &count); |
| 549 | |
| 550 | // Send server version |
| 551 | MultiAddShort(Netgame.server_version, data, &count); |
| 552 | |
| 553 | // Do mission name |
| 554 | int len = strlen(Netgame.mission) + 1; |
| 555 | MultiAddByte(len, data, &count); |
| 556 | memcpy(&data[count], Netgame.mission, len); |
| 557 | count += len; |
| 558 | |
| 559 | mprintf(0, "Sending netgame mission %s with length of %d!\n", Netgame.mission, len); |
| 560 | |
| 561 | len = strlen(Netgame.name) + 1; |
| 562 | MultiAddByte(len, data, &count); |
| 563 | memcpy(&data[count], Netgame.name, len); |
| 564 | count += len; |
| 565 | |
| 566 | // Do script name |
| 567 | len = strlen(Netgame.scriptname) + 1; |
| 568 | MultiAddByte(len, data, &count); |
| 569 | memcpy(&data[count], Netgame.scriptname, len); |
| 570 | count += len; |
| 571 | |
| 572 | // Set Player number |
| 573 | MultiAddByte(slotnum, data, &count); |
| 574 | |
| 575 | // Send packets per second |
| 576 | MultiAddByte(Netgame.packets_per_second, data, &count); |
| 577 | |
| 578 | // Send secret code which the client will use to send a non reliable packet |
| 579 | NetPlayers[slotnum].secret_net_id = Secret_net_id++; |
| 580 | MultiAddUint(NetPlayers[slotnum].secret_net_id, data, &count); |
| 581 | |
| 582 | // Send netplayer flags (obsoletes the above line of code, left for a while for compatability) |
| 583 | MultiAddInt(Netgame.flags, data, &count); |
| 584 | |
| 585 | END_DATA(count, data, size_offset); |
| 586 | |
| 587 | // Send it back to the player |
| 588 | nw_SendReliable(sock, data, count); |
| 589 | |
| 590 | ASSERT(count < MAX_GAME_DATA_SIZE); |
| 591 | } |
| 592 | |
| 593 | #define LEVEL_POLL_TIME 60 |
| 594 | // Polls for a connection message so we can finally join this game |
| 595 | // Returns true if we got the level info |
| 596 | // Client only |
| 597 | |
| 598 | int MultiPollForLevelInfo() { |
| 599 | float start_time, ask_time, initial_start_time; |
| 600 | int connected = 0; |
| 601 | uint8_t data[MAX_RECEIVE_SIZE]; |
| 602 | network_address from_addr; |
| 603 | |
| 604 | ShowProgressScreen(TXT_MLTWAITSERVER); |
| 605 | |
| 606 | NetPlayers[Player_num].sequence = NETSEQ_WAITING_FOR_LEVEL; |
| 607 | Got_level_info = 0; |
| 608 | Got_heartbeat = false; |
| 609 | |
| 610 | MultiSendReadyForLevel(); |
| 611 | |
| 612 | start_time = timer_GetTime(); |
| 613 | ask_time = timer_GetTime(); |
| 614 | initial_start_time = timer_GetTime(); |
| 615 | |
| 616 | // Go through this waiting period until the server times out or we get level info |
| 617 | while ((timer_GetTime() - start_time < LEVEL_POLL_TIME) && !Got_level_info && |
| 618 | (timer_GetTime() - initial_start_time < (LEVEL_POLL_TIME * 5))) { |
| 619 | if (timer_GetTime() - ask_time > 5.0) { |
| 620 | ask_time = timer_GetTime(); |
| 621 | MultiSendReadyForLevel(); |
| 622 | } |
| 623 | |
| 624 | int size; |
| 625 | while ((size = nw_ReceiveReliable(NetPlayers[Player_num].reliable_socket, data, MAX_RECEIVE_SIZE)) > 0) { |
| 626 | MultiProcessBigData(data, size, &Netgame.server_address); |
| 627 | } |
| 628 | |
| 629 | while ((size = nw_Receive(data, &from_addr)) > 0) { |
| 630 | if (data[0] == MP_HEARTBEAT) { |
| 631 | mprintf(0, "Got a heart beat from the server.\n"); |
| 632 | Got_heartbeat = true; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | if (Got_heartbeat) { |
| 637 | // Reset timer because we got a heartbeat |
| 638 | start_time = timer_GetTime(); |
| 639 | Got_heartbeat = false; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | if (!Got_level_info) { |
| 644 | ShowProgressScreen(TXT_MLTNOLEVELINFO); |
| 645 | MultiLeaveGame(); |
| 646 | Sleep(2000); |
| 647 | } else if (Got_level_info < 0) { |
| 648 | ShowProgressScreen(TXT(Join_response_strings[-Got_level_info])); |
| 649 | MultiLeaveGame(); |
| 650 | Sleep(2000); |
| 651 | } else { |
| 652 | Got_level_info = 0; |
| 653 | return 1; |
| 654 | } |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | #include "polymodel.h" |
| 660 | |
| 661 | // Returns a unique value for this ship |
| 662 | int MultiGetShipChecksum(int ship_index) { |
| 663 | ship *s = &Ships[ship_index]; |
| 664 | |
| 665 | int num = 0; |
| 666 | |
| 667 | AppendToLevelChecksum(s->phys_info.full_thrust); |
| 668 | AppendToLevelChecksum(s->phys_info.full_rotthrust); |
| 669 | |
| 670 | AppendToLevelChecksum(s->phys_info.mass); |
| 671 | AppendToLevelChecksum(s->phys_info.drag); |
| 672 | AppendToLevelChecksum(s->phys_info.rotdrag); |
| 673 | |
| 674 | AppendToLevelChecksum(s->phys_info.flags); |
| 675 | poly_model *pm = GetPolymodelPointer(s->model_handle); |
| 676 | AppendToLevelChecksum(pm->rad); |
| 677 | |
| 678 | for (int w = 0; w < MAX_PLAYER_WEAPONS; w++) { |
| 679 | |
| 680 | for (int i = 0; i < MAX_WB_FIRING_MASKS; i++) { |
| 681 | AppendToLevelChecksum(s->static_wb[w].gp_fire_wait[i]); |
| 682 | AppendToLevelChecksum(s->static_wb[w].gp_fire_masks[i]); |
| 683 | } |
| 684 | |
| 685 | AppendToLevelChecksum(s->static_wb[w].energy_usage); |
| 686 | AppendToLevelChecksum(s->static_wb[w].ammo_usage); |
| 687 | AppendToLevelChecksum(s->fire_flags[w]); |
| 688 | } |
| 689 | return num; |
| 690 | } |
| 691 | |
| 692 | // Returns a unique value for this ship |
| 693 | void MultiProcessShipChecksum(MD5 *md5, int ship_index) { |
| 694 | ship *s = &Ships[ship_index]; |
| 695 | |
| 696 | md5->update(s->phys_info.full_thrust); |
| 697 | md5->update(s->phys_info.full_rotthrust); |
| 698 | |
| 699 | md5->update(s->phys_info.mass); |
| 700 | md5->update(s->phys_info.drag); |
| 701 | md5->update(s->phys_info.rotdrag); |
| 702 | |
| 703 | md5->update(s->phys_info.flags); |
| 704 | poly_model *pm = GetPolymodelPointer(s->model_handle); |
| 705 | md5->update(pm->rad); |
| 706 | |
| 707 | for (int w = 0; w < MAX_PLAYER_WEAPONS; w++) { |
| 708 | |
| 709 | for (int i = 0; i < MAX_WB_FIRING_MASKS; i++) { |
| 710 | md5->update(s->static_wb[w].gp_fire_wait[i]); |
| 711 | md5->update(s->static_wb[w].gp_fire_masks[i]); |
| 712 | } |
| 713 | |
| 714 | md5->update(s->static_wb[w].energy_usage); |
| 715 | md5->update(s->static_wb[w].ammo_usage); |
| 716 | md5->update(s->fire_flags[w]); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // Server is telling us about the level |
| 721 | void MultiDoLevelInfo(uint8_t *data) { |
| 722 | int count = 0; |
| 723 | char pshipmodel[PAGENAME_LEN]; |
| 724 | Current_pilot.get_ship(pshipmodel); |
| 725 | |
| 726 | int ship_index = FindShipName(pshipmodel); |
| 727 | if (ship_index < 0) |
| 728 | ship_index = 0; |
| 729 | |
| 730 | // Skip header stuff |
| 731 | SKIP_HEADER(data, &count); |
| 732 | |
| 733 | int join_response = MultiGetByte(data, &count); |
| 734 | |
| 735 | // Get level number |
| 736 | Current_mission.cur_level = MultiGetByte(data, &count); |
| 737 | |
| 738 | Netgame.difficulty = MultiGetByte(data, &count); |
| 739 | |
| 740 | if ((Netgame.difficulty > 4) || (Netgame.difficulty < 0)) |
| 741 | Netgame.difficulty = 2; |
| 742 | |
| 743 | if (join_response != JOIN_ANSWER_OK) |
| 744 | Got_level_info = -join_response; |
| 745 | else |
| 746 | Got_level_info = 1; |
| 747 | } |
| 748 | |
| 749 | // Server is telling the client about the level currently playing |
| 750 | // Server only |
| 751 | void MultiSendLevelInfo(int slot) { |
| 752 | ASSERT(Netgame.local_role == LR_SERVER); |
| 753 | uint8_t data[MAX_GAME_DATA_SIZE]; |
| 754 | int count = 0; |
| 755 | int size_offset; |
| 756 | |
| 757 | size_offset = START_DATA(MP_LEVEL_INFO, data, &count); |
| 758 | |
| 759 | if (Netgame.local_role == LR_SERVER) { |
| 760 | if (Players[slot].start_roomnum != -1) |
| 761 | MultiAddByte(JOIN_ANSWER_OK, data, &count); |
| 762 | else |
| 763 | MultiAddByte(JOIN_ANSWER_NO_ROOM, data, &count); |
| 764 | } else |
| 765 | MultiAddByte(JOIN_ANSWER_NOT_SERVER, data, &count); |
| 766 | |
| 767 | // Do level number (of the mission) |
| 768 | mprintf(0, "Sending current mission level %d!\n", Current_mission.cur_level); |
| 769 | MultiAddByte(Current_mission.cur_level, data, &count); |
| 770 | |
| 771 | // Send the difficulty |
| 772 | MultiAddByte(Netgame.difficulty, data, &count); |
| 773 | |
| 774 | END_DATA(count, data, size_offset); |
| 775 | |
| 776 | // Send it out |
| 777 | nw_SendReliable(NetPlayers[slot].reliable_socket, data, count); |
| 778 | } |
| 779 | |
| 780 | // Clients says he's ready for level info |
| 781 | // so send it to him |
| 782 | void MultiDoReadyForLevel(uint8_t *data) { |
| 783 | int count = 0; |
| 784 | uint8_t slot; |
| 785 | char ship_name[PAGENAME_LEN]; |
| 786 | |
| 787 | // Skip header stuff |
| 788 | SKIP_HEADER(data, &count); |
| 789 | |
| 790 | // Get slot number |
| 791 | slot = MultiGetByte(data, &count); |
| 792 | |
| 793 | // Copy the ship name out |
| 794 | int len = MultiGetByte(data, &count); |
| 795 | memcpy(ship_name, &data[count], len); |
| 796 | count += len; |
| 797 | |
| 798 | int ship_index = FindShipName(ship_name); |
| 799 | if (ship_index < 0) |
| 800 | ship_index = 0; |
| 801 | |
| 802 | PlayerChangeShip(slot, ship_index); |
| 803 | |
| 804 | MultiSendLevelInfo(slot); |
| 805 | NetPlayers[slot].sequence = NETSEQ_LEVEL_START; |
| 806 | } |
| 807 | |
| 808 | // Client is telling the server that he is ready for a level |
| 809 | // Client only |
| 810 | void MultiSendReadyForLevel() { |
| 811 | ASSERT(Netgame.local_role != LR_SERVER); |
| 812 | |
| 813 | uint8_t data[MAX_GAME_DATA_SIZE]; |
| 814 | int count = 0; |
| 815 | int size_offset; |
| 816 | |
| 817 | mprintf(0, "Sending ready for level!\n"); |
| 818 | |
| 819 | char pshipmodel[PAGENAME_LEN]; |
| 820 | Current_pilot.get_ship(pshipmodel); |
| 821 | |
| 822 | int ship_index = FindShipName(pshipmodel); |
| 823 | if (ship_index < 0) |
| 824 | ship_index = 0; |
| 825 | |
| 826 | size_offset = START_DATA(MP_READY_FOR_LEVEL, data, &count); |
| 827 | |
| 828 | MultiAddByte(Player_num, data, &count); |
| 829 | |
| 830 | // Send ship for anti-cheating purposes (CHEAP and EASILY HACKABLE!) |
| 831 | int len = strlen(Ships[ship_index].name) + 1; |
| 832 | MultiAddByte(len, data, &count); |
| 833 | memcpy(&data[count], Ships[ship_index].name, len); |
| 834 | count += len; |
| 835 | |
| 836 | END_DATA(count, data, size_offset); |
| 837 | |
| 838 | // Send it out |
| 839 | nw_SendReliable(NetPlayers[Player_num].reliable_socket, data, count); |
| 840 | } |
| 841 | |
| 842 | extern int Multi_packet_tracking[]; |
| 843 | // Clears all connections |
| 844 | // Server and Client |
| 845 | void MultiCloseGame() { |
| 846 | mprintf(0, "Multi close game!\n"); |
| 847 | |
| 848 | if (!(Game_mode & GM_NETWORK)) { |
| 849 | mprintf(0, "Not network game!\n"); |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | #if !defined(RELEASE) |
| 854 | // Write our packet tracking |
| 855 | CFILE *outfile; |
| 856 | outfile = cfopen("PacketTracking", "wt"); |
| 857 | if (!outfile) { |
| 858 | mprintf(0, "Couldn't open packet tracking file!\n"); |
| 859 | } else { |
| 860 | for (int i = 0; i < 255; i++) { |
| 861 | cfprintf(outfile, "Packet %d = %d\n", i, Multi_packet_tracking[i]); |
| 862 | } |
| 863 | cfclose(outfile); |
| 864 | } |
| 865 | #endif |
| 866 | |
| 867 | Sleep(1000); // Sleep for one second |
| 868 | |
| 869 | if (Netgame.local_role == LR_SERVER) { |
| 870 | int i; |
| 871 | |
| 872 | for (i = 0; i < MAX_NET_PLAYERS; i++) { |
| 873 | if (i == Player_num) |
| 874 | continue; |
| 875 | |
| 876 | if (NetPlayers[i].flags & NPF_CONNECTED) { |
| 877 | MultiDisconnectPlayer(i); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | for (i = 0; i < 100; i++) { |
| 882 | nw_DoNetworkIdle(); |
| 883 | Sleep(10); |
| 884 | } |
| 885 | |
| 886 | NetPlayers[Player_num].flags &= ~NPF_CONNECTED; |
| 887 | MultiFlushAllIncomingBuffers(); |
| 888 | } else { |
| 889 | if (NetPlayers[Player_num].flags & NPF_CONNECTED) { |
| 890 | MultiFlushAllIncomingBuffers(); |
| 891 | nw_CloseSocket(&NetPlayers[Player_num].reliable_socket); |
| 892 | NetPlayers[Player_num].flags &= ~NPF_CONNECTED; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | Game_mode &= ~GM_MULTI; |
| 897 | if (Player_num != 0 && Player_object) |
| 898 | SetObjectControlType(Player_object, CT_NONE); |
| 899 | Player_num = 0; |
| 900 | Player_object = Viewer_object = &Objects[Players[0].objnum]; |
| 901 | } |
| 902 | |
| 903 | #define GET_GAME_INFO_TIME 2 |
| 904 | // Fills in the passed in buffers with the info of the games that are on this subnet |
| 905 | int SearchForLocalGamesTCP(uint32_t ask, uint16_t port) { |
| 906 | int count = 0; |
| 907 | int size; |
| 908 | int tries = 0; |
| 909 | uint8_t data[MAX_GAME_DATA_SIZE]; |
| 910 | network_address check_addr, from_addr; |
| 911 | check_addr.connection_type = NP_TCP; |
| 912 | if (ask) { |
| 913 | memcpy(&check_addr.address, &ask, 4); |
| 914 | check_addr.port = htons(port); |
| 915 | |
| 916 | size = START_DATA(MP_GET_GAME_INFO, data, &count); |
| 917 | MultiAddFloat(timer_GetTime(), data, &count); |
| 918 | MultiAddInt(MULTI_VERSION, data, &count); |
| 919 | END_DATA(count, data, size); |
| 920 | |
| 921 | nw_Send(&check_addr, data, count, 0); |
| 922 | |
| 923 | // Num_network_games_known=0; |
| 924 | } |
| 925 | |
| 926 | int packsize; |
| 927 | while (((packsize = nw_Receive(Multi_receive_buffer, &from_addr)) > 0)) { |
| 928 | MultiProcessBigData(Multi_receive_buffer, packsize, &from_addr); |
| 929 | } |
| 930 | return Num_network_games_known; |
| 931 | } |
| 932 | |
| 933 | // Fills in the passed in buffers with the info of the games that are on this subnet |
| 934 | int SearchForGamesPXO(uint32_t ask, uint16_t port) { |
| 935 | int count = 0; |
| 936 | int size; |
| 937 | int tries = 0; |
| 938 | uint8_t data[MAX_GAME_DATA_SIZE]; |
| 939 | network_address check_addr, from_addr; |
| 940 | check_addr.connection_type = NP_TCP; |
| 941 | if (ask) { |
| 942 | memcpy(&check_addr.address, &ask, 4); |
| 943 | check_addr.port = htons(port); |
| 944 | |
| 945 | size = START_DATA(MP_GET_PXO_GAME_INFO, data, &count); |
| 946 | MultiAddFloat(timer_GetTime(), data, &count); |
| 947 | MultiAddInt(MULTI_VERSION, data, &count); |
| 948 | END_DATA(count, data, size); |
| 949 | |
| 950 | nw_Send(&check_addr, data, count, 0); |
| 951 | |
| 952 | // Num_network_games_known=0; |
| 953 | } |
| 954 | |
| 955 | int packsize; |
| 956 | while (((packsize = nw_Receive(Multi_receive_buffer, &from_addr)) > 0)) { |
| 957 | MultiProcessBigData(Multi_receive_buffer, packsize, &from_addr); |
| 958 | } |
| 959 | return Num_network_games_known; |
| 960 | } |
| 961 | |
| 962 | #define MULTI_SERVER_TIMEOUT_TIME 40 |
| 963 | extern bool Multi_Gamelist_changed; |
| 964 | |
| 965 | void UpdateAndPackGameList(void) { |
| 966 | float curtime = timer_GetTime(); |
| 967 | for (int i = 0; i < Num_network_games_known; i++) { |
| 968 | // Network_games[i] |
| 969 | if ((curtime - Network_games[i].last_update) > MULTI_SERVER_TIMEOUT_TIME) { |
| 970 | memcpy(&Network_games[i], &Network_games[Num_network_games_known], sizeof(network_game)); |
| 971 | Num_network_games_known--; |
| 972 | i = 0; |
| 973 | Multi_Gamelist_changed = true; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | // Sets whether or not the server answsers to a connection request |
| 979 | void MultiSetAcceptState(bool state) { |
| 980 | mprintf(0, "Setting multi_accept_state to %s.\n", state ? "true" : "false"); |
| 981 | Multi_accept_state = state; |
| 982 | } |