d_client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2000 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION:
  30. * Network client. Passes information to/from server, staying
  31. * synchronised.
  32. * Contains the main wait loop, waiting for network input or
  33. * time before doing the next tic.
  34. * Rewritten for LxDoom, but based around bits of the old code.
  35. *
  36. *-----------------------------------------------------------------------------
  37. */
  38. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #include <sys/types.h>
  42. #ifdef HAVE_UNISTD_H
  43. #include <unistd.h>
  44. #endif
  45. #ifdef HAVE_SYS_WAIT_H
  46. #include <sys/wait.h>
  47. #endif
  48. #ifdef USE_SDL_NET
  49. #include "SDL.h"
  50. #endif
  51. #include "doomtype.h"
  52. #include "doomstat.h"
  53. #include "d_net.h"
  54. #include "z_zone.h"
  55. #include "d_main.h"
  56. #include "g_game.h"
  57. #include "m_menu.h"
  58. #include "p_checksum.h"
  59. #include "protocol.h"
  60. #include "i_network.h"
  61. #include "i_system.h"
  62. #include "i_main.h"
  63. #include "i_video.h"
  64. #include "m_argv.h"
  65. #include "r_fps.h"
  66. #include "lprintf.h"
  67. static boolean server;
  68. static int remotetic; // Tic expected from the remote
  69. static int remotesend; // Tic expected by the remote
  70. ticcmd_t netcmds[MAXPLAYERS][BACKUPTICS];
  71. static ticcmd_t* localcmds;
  72. static unsigned numqueuedpackets;
  73. static packet_header_t** queuedpacket;
  74. int maketic;
  75. int ticdup = 1;
  76. static int xtratics = 0;
  77. int wanted_player_number;
  78. static boolean isExtraDDisplay = false;
  79. static void D_QuitNetGame (void);
  80. #ifndef HAVE_NET
  81. doomcom_t* doomcom;
  82. #endif
  83. #ifdef HAVE_NET
  84. void D_InitNetGame (void)
  85. {
  86. int i;
  87. int numplayers = 1;
  88. i = M_CheckParm("-net");
  89. if (i && i < myargc-1) i++;
  90. if (!(netgame = server = !!i)) {
  91. playeringame[consoleplayer = 0] = true;
  92. // e6y
  93. // for play, recording or playback using "single-player coop" mode.
  94. // Equivalent to using prboom_server with -N 1
  95. netgame = M_CheckParm("-solo-net") || M_CheckParm("-net1");
  96. } else {
  97. // Get game info from server
  98. packet_header_t *packet = Z_Malloc(1000, PU_STATIC, NULL);
  99. struct setup_packet_s *sinfo = (void*)(packet+1);
  100. struct { packet_header_t head; short pn; } PACKEDATTR initpacket;
  101. I_InitNetwork();
  102. udp_socket = I_Socket(0);
  103. I_ConnectToServer(myargv[i]);
  104. do
  105. {
  106. do {
  107. // Send init packet
  108. initpacket.pn = doom_htons(wanted_player_number);
  109. packet_set(&initpacket.head, PKT_INIT, 0);
  110. I_SendPacket(&initpacket.head, sizeof(initpacket));
  111. I_WaitForPacket(5000);
  112. } while (!I_GetPacket(packet, 1000));
  113. if (packet->type == PKT_DOWN) I_Error("Server aborted the game");
  114. } while (packet->type != PKT_SETUP);
  115. // Once we have been accepted by the server, we should tell it when we leave
  116. atexit(D_QuitNetGame);
  117. // Get info from the setup packet
  118. consoleplayer = sinfo->yourplayer;
  119. compatibility_level = sinfo->complevel;
  120. G_Compatibility();
  121. startskill = sinfo->skill;
  122. deathmatch = sinfo->deathmatch;
  123. startmap = sinfo->level;
  124. startepisode = sinfo->episode;
  125. ticdup = sinfo->ticdup;
  126. xtratics = sinfo->extratic;
  127. G_ReadOptions(sinfo->game_options);
  128. lprintf(LO_INFO, "\tjoined game as player %d/%d; %d WADs specified\n",
  129. consoleplayer+1, numplayers = sinfo->players, sinfo->numwads);
  130. {
  131. char *p = sinfo->wadnames;
  132. int i = sinfo->numwads;
  133. while (i--) {
  134. D_AddFile(p, source_net);
  135. p += strlen(p) + 1;
  136. }
  137. }
  138. Z_Free(packet);
  139. }
  140. localcmds = netcmds[displayplayer = consoleplayer];
  141. for (i=0; i<numplayers; i++)
  142. playeringame[i] = true;
  143. for (; i<MAXPLAYERS; i++)
  144. playeringame[i] = false;
  145. if (!playeringame[consoleplayer]) I_Error("D_InitNetGame: consoleplayer not in game");
  146. }
  147. #else
  148. void D_InitNetGame (void)
  149. {
  150. int i;
  151. doomcom = Z_Malloc(sizeof *doomcom, PU_STATIC, NULL);
  152. doomcom->consoleplayer = 0;
  153. doomcom->numnodes = 0; doomcom->numplayers = 1;
  154. localcmds = netcmds[consoleplayer];
  155. netgame = (M_CheckParm("-solo-net") != 0) || (M_CheckParm("-net1") != 0);
  156. for (i=0; i<doomcom->numplayers; i++)
  157. playeringame[i] = true;
  158. for (; i<MAXPLAYERS; i++)
  159. playeringame[i] = false;
  160. consoleplayer = displayplayer = doomcom->consoleplayer;
  161. }
  162. #endif // HAVE_NET
  163. #ifdef HAVE_NET
  164. void D_CheckNetGame(void)
  165. {
  166. packet_header_t *packet = Z_Malloc(sizeof(packet_header_t)+1, PU_STATIC, NULL);
  167. if (server) {
  168. lprintf(LO_INFO, "D_CheckNetGame: waiting for server to signal game start\n");
  169. do {
  170. while (!I_GetPacket(packet, sizeof(packet_header_t)+1)) {
  171. packet_set(packet, PKT_GO, 0);
  172. *(byte*)(packet+1) = consoleplayer;
  173. I_SendPacket(packet, sizeof(packet_header_t)+1);
  174. I_uSleep(100000);
  175. }
  176. } while (packet->type != PKT_GO);
  177. }
  178. Z_Free(packet);
  179. }
  180. boolean D_NetGetWad(const char* name)
  181. {
  182. #if defined(HAVE_WAIT_H)
  183. size_t psize = sizeof(packet_header_t) + strlen(name) + 500;
  184. packet_header_t *packet;
  185. boolean done = false;
  186. if (!server || strchr(name, '/')) return false; // If it contains path info, reject
  187. do {
  188. // Send WAD request to remote
  189. packet = Z_Malloc(psize, PU_STATIC, NULL);
  190. packet_set(packet, PKT_WAD, 0);
  191. *(byte*)(packet+1) = consoleplayer;
  192. strcpy(1+(byte*)(packet+1), name);
  193. I_SendPacket(packet, sizeof(packet_header_t) + strlen(name) + 2);
  194. I_uSleep(10000);
  195. } while (!I_GetPacket(packet, psize) || (packet->type != PKT_WAD));
  196. Z_Free(packet);
  197. if (!strcasecmp((void*)(packet+1), name)) {
  198. pid_t pid;
  199. int rv;
  200. byte *p = (byte*)(packet+1) + strlen(name) + 1;
  201. /* Automatic wad file retrieval using wget (supports http and ftp, using URLs)
  202. * Unix systems have all these commands handy, this kind of thing is easy
  203. * Any windo$e port will have some awkward work replacing these.
  204. */
  205. /* cph - caution here. This is data from an untrusted source.
  206. * Don't pass it via a shell. */
  207. if ((pid = fork()) == -1)
  208. perror("fork");
  209. else if (!pid) {
  210. /* Child chains to wget, does the download */
  211. execlp("wget", "wget", p, NULL);
  212. }
  213. /* This is the parent, i.e. main LxDoom process */
  214. wait(&rv);
  215. if (!(done = !access(name, R_OK))) {
  216. if (!strcmp(p+strlen(p)-4, ".zip")) {
  217. p = strrchr(p, '/')+1;
  218. if ((pid = fork()) == -1)
  219. perror("fork");
  220. else if (!pid) {
  221. /* Child executes decompressor */
  222. execlp("unzip", "unzip", p, name, NULL);
  223. }
  224. /* Parent waits for the file */
  225. wait(&rv);
  226. done = !!access(name, R_OK);
  227. }
  228. /* Add more decompression protocols here as desired */
  229. }
  230. Z_Free(buffer);
  231. }
  232. return done;
  233. #else /* HAVE_WAIT_H */
  234. return false;
  235. #endif
  236. }
  237. void NetUpdate(void)
  238. {
  239. static int lastmadetic;
  240. if (isExtraDDisplay)
  241. return;
  242. if (server) { // Receive network packets
  243. size_t recvlen;
  244. packet_header_t *packet = Z_Malloc(10000, PU_STATIC, NULL);
  245. while ((recvlen = I_GetPacket(packet, 10000))) {
  246. switch(packet->type) {
  247. case PKT_TICS:
  248. {
  249. byte *p = (void*)(packet+1);
  250. int tics = *p++;
  251. unsigned long ptic = doom_ntohl(packet->tic);
  252. if (ptic > (unsigned)remotetic) { // Missed some
  253. packet_set(packet, PKT_RETRANS, remotetic);
  254. *(byte*)(packet+1) = consoleplayer;
  255. I_SendPacket(packet, sizeof(*packet)+1);
  256. } else {
  257. if (ptic + tics <= (unsigned)remotetic) break; // Will not improve things
  258. remotetic = ptic;
  259. while (tics--) {
  260. int players = *p++;
  261. while (players--) {
  262. int n = *p++;
  263. RawToTic(&netcmds[n][remotetic%BACKUPTICS], p);
  264. p += sizeof(ticcmd_t);
  265. }
  266. remotetic++;
  267. }
  268. }
  269. }
  270. break;
  271. case PKT_RETRANS: // Resend request
  272. remotesend = doom_ntohl(packet->tic);
  273. break;
  274. case PKT_DOWN: // Server downed
  275. {
  276. int j;
  277. for (j=0; j<MAXPLAYERS; j++)
  278. if (j != consoleplayer) playeringame[j] = false;
  279. server = false;
  280. doom_printf("Server is down\nAll other players are no longer in the game\n");
  281. }
  282. break;
  283. case PKT_EXTRA: // Misc stuff
  284. case PKT_QUIT: // Player quit
  285. // Queue packet to be processed when its tic time is reached
  286. queuedpacket = Z_Realloc(queuedpacket, ++numqueuedpackets * sizeof *queuedpacket,
  287. PU_STATIC, NULL);
  288. queuedpacket[numqueuedpackets-1] = Z_Malloc(recvlen, PU_STATIC, NULL);
  289. memcpy(queuedpacket[numqueuedpackets-1], packet, recvlen);
  290. break;
  291. case PKT_BACKOFF:
  292. /* cph 2003-09-18 -
  293. * The server sends this when we have got ahead of the other clients. We should
  294. * stall the input side on this client, to allow other clients to catch up.
  295. */
  296. lastmadetic++;
  297. break;
  298. default: // Other packet, unrecognised or redundant
  299. break;
  300. }
  301. }
  302. Z_Free(packet);
  303. }
  304. { // Build new ticcmds
  305. int newtics = I_GetTime() - lastmadetic;
  306. newtics = (newtics > 0 ? newtics : 0);
  307. lastmadetic += newtics;
  308. if (ffmap) newtics++;
  309. while (newtics--) {
  310. I_StartTic();
  311. if (maketic - gametic > BACKUPTICS/2) break;
  312. G_BuildTiccmd(&localcmds[maketic%BACKUPTICS]);
  313. maketic++;
  314. }
  315. if (server && maketic > remotesend) { // Send the tics to the server
  316. int sendtics;
  317. remotesend -= xtratics;
  318. if (remotesend < 0) remotesend = 0;
  319. sendtics = maketic - remotesend;
  320. {
  321. size_t pkt_size = sizeof(packet_header_t) + 2 + sendtics * sizeof(ticcmd_t);
  322. packet_header_t *packet = Z_Malloc(pkt_size, PU_STATIC, NULL);
  323. packet_set(packet, PKT_TICC, maketic - sendtics);
  324. *(byte*)(packet+1) = sendtics;
  325. *(((byte*)(packet+1))+1) = consoleplayer;
  326. {
  327. void *tic = ((byte*)(packet+1)) +2;
  328. while (sendtics--) {
  329. TicToRaw(tic, &localcmds[remotesend++%BACKUPTICS]);
  330. tic = (byte *)tic + sizeof(ticcmd_t);
  331. }
  332. }
  333. I_SendPacket(packet, pkt_size);
  334. Z_Free(packet);
  335. }
  336. }
  337. }
  338. }
  339. #else
  340. void D_BuildNewTiccmds(void)
  341. {
  342. static int lastmadetic;
  343. int newtics = I_GetTime() - lastmadetic;
  344. lastmadetic += newtics;
  345. while (newtics--)
  346. {
  347. I_StartTic();
  348. if (maketic - gametic > BACKUPTICS/2) break;
  349. G_BuildTiccmd(&localcmds[maketic%BACKUPTICS]);
  350. maketic++;
  351. }
  352. }
  353. #endif
  354. #ifdef HAVE_NET
  355. /* cph - data passed to this must be in the Doom (little-) endian */
  356. void D_NetSendMisc(netmisctype_t type, size_t len, void* data)
  357. {
  358. if (server) {
  359. size_t size = sizeof(packet_header_t) + 3*sizeof(int) + len;
  360. packet_header_t *packet = Z_Malloc(size, PU_STATIC, NULL);
  361. int *p = (void*)(packet+1);
  362. packet_set(packet, PKT_EXTRA, gametic);
  363. *p++ = LONG(type); *p++ = LONG(consoleplayer); *p++ = LONG(len);
  364. memcpy(p, data, len);
  365. I_SendPacket(packet, size);
  366. Z_Free(packet);
  367. }
  368. }
  369. static void CheckQueuedPackets(void)
  370. {
  371. int i;
  372. for (i=0; (unsigned)i<numqueuedpackets; i++)
  373. if (doom_ntohl(queuedpacket[i]->tic) <= gametic)
  374. switch (queuedpacket[i]->type) {
  375. case PKT_QUIT: // Player quit the game
  376. {
  377. int pn = *(byte*)(queuedpacket[i]+1);
  378. playeringame[pn] = false;
  379. doom_printf("Player %d left the game\n", pn);
  380. }
  381. break;
  382. case PKT_EXTRA:
  383. {
  384. int *p = (int*)(queuedpacket[i]+1);
  385. size_t len = LONG(*(p+2));
  386. switch (LONG(*p)) {
  387. case nm_plcolour:
  388. G_ChangedPlayerColour(LONG(*(p+1)), LONG(*(p+3)));
  389. break;
  390. case nm_savegamename:
  391. if (len < SAVEDESCLEN) {
  392. memcpy(savedescription, p+3, len);
  393. // Force terminating 0 in case
  394. savedescription[len] = 0;
  395. }
  396. break;
  397. }
  398. }
  399. break;
  400. default: // Should not be queued
  401. break;
  402. }
  403. { // Requeue remaining packets
  404. int newnum = 0;
  405. packet_header_t **newqueue = NULL;
  406. for (i=0; (unsigned)i<numqueuedpackets; i++)
  407. if (doom_ntohl(queuedpacket[i]->tic) > gametic) {
  408. newqueue = Z_Realloc(newqueue, ++newnum * sizeof *newqueue,
  409. PU_STATIC, NULL);
  410. newqueue[newnum-1] = queuedpacket[i];
  411. } else Z_Free(queuedpacket[i]);
  412. Z_Free(queuedpacket);
  413. numqueuedpackets = newnum; queuedpacket = newqueue;
  414. }
  415. }
  416. #endif // HAVE_NET
  417. void TryRunTics (void)
  418. {
  419. int runtics;
  420. int entertime = I_GetTime();
  421. // Wait for tics to run
  422. while (1) {
  423. #ifdef HAVE_NET
  424. NetUpdate();
  425. #else
  426. D_BuildNewTiccmds();
  427. #endif
  428. runtics = (server ? remotetic : maketic) - gametic;
  429. if (!runtics) {
  430. if (!movement_smooth) {
  431. #ifdef HAVE_NET
  432. if (server)
  433. I_WaitForPacket(ms_to_next_tick);
  434. else
  435. #endif
  436. I_uSleep(ms_to_next_tick*1000);
  437. }
  438. if (I_GetTime() - entertime > 10) {
  439. #ifdef HAVE_NET
  440. if (server) {
  441. char buf[sizeof(packet_header_t)+1];
  442. remotesend--;
  443. packet_set((packet_header_t *)buf, PKT_RETRANS, remotetic);
  444. buf[sizeof(buf)-1] = consoleplayer;
  445. I_SendPacket((packet_header_t *)buf, sizeof buf);
  446. }
  447. #endif
  448. M_Ticker(); return;
  449. }
  450. //if ((displaytime) < (tic_vars.next-SDL_GetTicks()))
  451. {
  452. WasRenderedInTryRunTics = true;
  453. if (V_GetMode() == VID_MODEGL ?
  454. movement_smooth :
  455. movement_smooth && gamestate==wipegamestate)
  456. {
  457. isExtraDDisplay = true;
  458. D_Display();
  459. isExtraDDisplay = false;
  460. }
  461. }
  462. } else break;
  463. }
  464. while (runtics--) {
  465. #ifdef HAVE_NET
  466. if (server) CheckQueuedPackets();
  467. #endif
  468. if (advancedemo)
  469. D_DoAdvanceDemo ();
  470. M_Ticker ();
  471. I_GetTime_SaveMS();
  472. G_Ticker ();
  473. P_Checksum(gametic);
  474. gametic++;
  475. #ifdef HAVE_NET
  476. NetUpdate(); // Keep sending our tics to avoid stalling remote nodes
  477. #endif
  478. }
  479. }
  480. #ifdef HAVE_NET
  481. static void D_QuitNetGame (void)
  482. {
  483. byte buf[1 + sizeof(packet_header_t)];
  484. packet_header_t *packet = (void*)buf;
  485. int i;
  486. if (!server) return;
  487. buf[sizeof(packet_header_t)] = consoleplayer;
  488. packet_set(packet, PKT_QUIT, gametic);
  489. for (i=0; i<4; i++) {
  490. I_SendPacket(packet, 1 + sizeof(packet_header_t));
  491. I_uSleep(10000);
  492. }
  493. }
  494. #endif