D_NET.C 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // d_net.c
  2. // This version has the fixed ticdup code
  3. #include "DoomDef.h"
  4. #define NCMD_EXIT 0x80000000
  5. #define NCMD_RETRANSMIT 0x40000000
  6. #define NCMD_SETUP 0x20000000
  7. #define NCMD_KILL 0x10000000 // kill game
  8. #define NCMD_CHECKSUM 0x0fffffff
  9. doomcom_t *doomcom;
  10. doomdata_t *netbuffer; // points inside doomcom
  11. /*
  12. ==============================================================================
  13. NETWORKING
  14. gametic is the tic about to (or currently being) run
  15. maketic is the tick that hasn't had control made for it yet
  16. nettics[] has the maketics for all players
  17. a gametic cannot be run until nettics[] > gametic for all players
  18. ==============================================================================
  19. */
  20. #define RESENDCOUNT 10
  21. #define PL_DRONE 0x80 // bit flag in doomdata->player
  22. ticcmd_t localcmds[BACKUPTICS];
  23. ticcmd_t netcmds[MAXPLAYERS][BACKUPTICS];
  24. int nettics[MAXNETNODES];
  25. boolean nodeingame[MAXNETNODES]; // set false as nodes leave game
  26. boolean remoteresend[MAXNETNODES]; // set when local needs tics
  27. int resendto[MAXNETNODES]; // set when remote needs tics
  28. int resendcount[MAXNETNODES];
  29. int nodeforplayer[MAXPLAYERS];
  30. int maketic;
  31. int lastnettic, skiptics;
  32. int ticdup;
  33. int maxsend; // BACKUPTICS/(2*ticdup)-1
  34. void D_ProcessEvents (void);
  35. void G_BuildTiccmd (ticcmd_t *cmd);
  36. void D_DoAdvanceDemo (void);
  37. boolean reboundpacket;
  38. doomdata_t reboundstore;
  39. int NetbufferSize (void)
  40. {
  41. return (int)&(((doomdata_t *)0)->cmds[netbuffer->numtics]);
  42. }
  43. unsigned NetbufferChecksum (void)
  44. {
  45. unsigned c;
  46. int i,l;
  47. c = 0x1234567;
  48. #if defined(NeXT) || defined(NORMALUNIX)
  49. return 0; // byte order problems
  50. #endif
  51. l = (NetbufferSize () - (int)&(((doomdata_t *)0)->retransmitfrom))/4;
  52. for (i=0 ; i<l ; i++)
  53. c += ((unsigned *)&netbuffer->retransmitfrom)[i] * (i+1);
  54. return c & NCMD_CHECKSUM;
  55. }
  56. int ExpandTics (int low)
  57. {
  58. int delta;
  59. delta = low - (maketic&0xff);
  60. if (delta >= -64 && delta <= 64)
  61. return (maketic&~0xff) + low;
  62. if (delta > 64)
  63. return (maketic&~0xff) - 256 + low;
  64. if (delta < -64)
  65. return (maketic&~0xff) + 256 + low;
  66. I_Error ("ExpandTics: strange value %i at maketic %i",low,maketic);
  67. return 0;
  68. }
  69. //============================================================================
  70. /*
  71. ==============
  72. =
  73. = HSendPacket
  74. =
  75. ==============
  76. */
  77. void HSendPacket (int node, int flags)
  78. {
  79. netbuffer->checksum = NetbufferChecksum () | flags;
  80. if (!node)
  81. {
  82. reboundstore = *netbuffer;
  83. reboundpacket = true;
  84. return;
  85. }
  86. if (demoplayback)
  87. return;
  88. if (!netgame)
  89. I_Error ("Tried to transmit to another node");
  90. doomcom->command = CMD_SEND;
  91. doomcom->remotenode = node;
  92. doomcom->datalength = NetbufferSize ();
  93. if (debugfile)
  94. {
  95. int i;
  96. int realretrans;
  97. if (netbuffer->checksum & NCMD_RETRANSMIT)
  98. realretrans = ExpandTics (netbuffer->retransmitfrom);
  99. else
  100. realretrans = -1;
  101. fprintf (debugfile,"send (%i + %i, R %i) [%i] "
  102. ,ExpandTics(netbuffer->starttic),netbuffer->numtics, realretrans, doomcom->datalength);
  103. for (i=0 ; i<doomcom->datalength ; i++)
  104. fprintf (debugfile,"%i ",((byte *)netbuffer)[i]);
  105. fprintf (debugfile,"\n");
  106. }
  107. I_NetCmd ();
  108. }
  109. /*
  110. ==============
  111. =
  112. = HGetPacket
  113. =
  114. = Returns false if no packet is waiting
  115. =
  116. ==============
  117. */
  118. boolean HGetPacket (void)
  119. {
  120. if (reboundpacket)
  121. {
  122. *netbuffer = reboundstore;
  123. doomcom->remotenode = 0;
  124. reboundpacket = false;
  125. return true;
  126. }
  127. if (!netgame)
  128. return false;
  129. if (demoplayback)
  130. return false;
  131. doomcom->command = CMD_GET;
  132. I_NetCmd ();
  133. if (doomcom->remotenode == -1)
  134. return false;
  135. if (doomcom->datalength != NetbufferSize ())
  136. {
  137. if (debugfile)
  138. fprintf (debugfile,"bad packet length %i\n",doomcom->datalength);
  139. return false;
  140. }
  141. if (NetbufferChecksum () != (netbuffer->checksum&NCMD_CHECKSUM) )
  142. {
  143. if (debugfile)
  144. fprintf (debugfile,"bad packet checksum\n");
  145. return false;
  146. }
  147. if (debugfile)
  148. {
  149. int realretrans;
  150. int i;
  151. if (netbuffer->checksum & NCMD_SETUP)
  152. fprintf (debugfile,"setup packet\n");
  153. else
  154. {
  155. if (netbuffer->checksum & NCMD_RETRANSMIT)
  156. realretrans = ExpandTics (netbuffer->retransmitfrom);
  157. else
  158. realretrans = -1;
  159. fprintf (debugfile,"get %i = (%i + %i, R %i)[%i] ",doomcom->remotenode,
  160. ExpandTics(netbuffer->starttic),netbuffer->numtics, realretrans, doomcom->datalength);
  161. for (i=0 ; i<doomcom->datalength ; i++)
  162. fprintf (debugfile,"%i ",((byte *)netbuffer)[i]);
  163. fprintf (debugfile,"\n");
  164. }
  165. }
  166. return true;
  167. }
  168. /*
  169. ===================
  170. =
  171. = GetPackets
  172. =
  173. ===================
  174. */
  175. char exitmsg[80];
  176. void GetPackets (void)
  177. {
  178. int netconsole;
  179. int netnode;
  180. ticcmd_t *src, *dest;
  181. int realend;
  182. int realstart;
  183. while (HGetPacket ())
  184. {
  185. if (netbuffer->checksum & NCMD_SETUP)
  186. continue; // extra setup packet
  187. netconsole = netbuffer->player & ~PL_DRONE;
  188. netnode = doomcom->remotenode;
  189. //
  190. // to save bytes, only the low byte of tic numbers are sent
  191. // Figure out what the rest of the bytes are
  192. //
  193. realstart = ExpandTics (netbuffer->starttic);
  194. realend = (realstart+netbuffer->numtics);
  195. //
  196. // check for exiting the game
  197. //
  198. if (netbuffer->checksum & NCMD_EXIT)
  199. {
  200. if (!nodeingame[netnode])
  201. continue;
  202. nodeingame[netnode] = false;
  203. playeringame[netconsole] = false;
  204. strcpy (exitmsg, "PLAYER 1 LEFT THE GAME");
  205. exitmsg[7] += netconsole;
  206. players[consoleplayer].message = exitmsg;
  207. // if (demorecording)
  208. // G_CheckDemoStatus ();
  209. continue;
  210. }
  211. //
  212. // check for a remote game kill
  213. //
  214. if (netbuffer->checksum & NCMD_KILL)
  215. I_Error ("Killed by network driver");
  216. nodeforplayer[netconsole] = netnode;
  217. //
  218. // check for retransmit request
  219. //
  220. if ( resendcount[netnode] <= 0
  221. && (netbuffer->checksum & NCMD_RETRANSMIT) )
  222. {
  223. resendto[netnode] = ExpandTics(netbuffer->retransmitfrom);
  224. if (debugfile)
  225. fprintf (debugfile,"retransmit from %i\n", resendto[netnode]);
  226. resendcount[netnode] = RESENDCOUNT;
  227. }
  228. else
  229. resendcount[netnode]--;
  230. //
  231. // check for out of order / duplicated packet
  232. //
  233. if (realend == nettics[netnode])
  234. continue;
  235. if (realend < nettics[netnode])
  236. {
  237. if (debugfile)
  238. fprintf (debugfile,"out of order packet (%i + %i)\n" ,realstart,netbuffer->numtics);
  239. continue;
  240. }
  241. //
  242. // check for a missed packet
  243. //
  244. if (realstart > nettics[netnode])
  245. {
  246. // stop processing until the other system resends the missed tics
  247. if (debugfile)
  248. fprintf (debugfile,"missed tics from %i (%i - %i)\n", netnode, realstart, nettics[netnode]);
  249. remoteresend[netnode] = true;
  250. continue;
  251. }
  252. //
  253. // update command store from the packet
  254. //
  255. {
  256. int start;
  257. remoteresend[netnode] = false;
  258. start = nettics[netnode] - realstart;
  259. src = &netbuffer->cmds[start];
  260. while (nettics[netnode] < realend)
  261. {
  262. dest = &netcmds[netconsole][nettics[netnode]%BACKUPTICS];
  263. nettics[netnode]++;
  264. *dest = *src;
  265. src++;
  266. }
  267. }
  268. }
  269. }
  270. /*
  271. =============
  272. =
  273. = NetUpdate
  274. =
  275. = Builds ticcmds for console player
  276. = sends out a packet
  277. =============
  278. */
  279. int gametime;
  280. void NetUpdate (void)
  281. {
  282. int nowtime;
  283. int newtics;
  284. int i,j;
  285. int realstart;
  286. int gameticdiv;
  287. //
  288. // check time
  289. //
  290. nowtime = I_GetTime ()/ticdup;
  291. newtics = nowtime - gametime;
  292. gametime = nowtime;
  293. if (newtics <= 0) // nothing new to update
  294. goto listen;
  295. if (skiptics <= newtics)
  296. {
  297. newtics -= skiptics;
  298. skiptics = 0;
  299. }
  300. else
  301. {
  302. skiptics -= newtics;
  303. newtics = 0;
  304. }
  305. netbuffer->player = consoleplayer;
  306. //
  307. // build new ticcmds for console player
  308. //
  309. gameticdiv = gametic/ticdup;
  310. for (i=0 ; i<newtics ; i++)
  311. {
  312. I_StartTic ();
  313. D_ProcessEvents ();
  314. if (maketic - gameticdiv >= BACKUPTICS/2-1)
  315. break; // can't hold any more
  316. //printf ("mk:%i ",maketic);
  317. G_BuildTiccmd (&localcmds[maketic%BACKUPTICS]);
  318. maketic++;
  319. }
  320. if (singletics)
  321. return; // singletic update is syncronous
  322. //
  323. // send the packet to the other nodes
  324. //
  325. for (i=0 ; i<doomcom->numnodes ; i++)
  326. if (nodeingame[i])
  327. {
  328. netbuffer->starttic = realstart = resendto[i];
  329. netbuffer->numtics = maketic - realstart;
  330. if (netbuffer->numtics > BACKUPTICS)
  331. I_Error ("NetUpdate: netbuffer->numtics > BACKUPTICS");
  332. resendto[i] = maketic - doomcom->extratics;
  333. for (j=0 ; j< netbuffer->numtics ; j++)
  334. netbuffer->cmds[j] =
  335. localcmds[(realstart+j)%BACKUPTICS];
  336. if (remoteresend[i])
  337. {
  338. netbuffer->retransmitfrom = nettics[i];
  339. HSendPacket (i, NCMD_RETRANSMIT);
  340. }
  341. else
  342. {
  343. netbuffer->retransmitfrom = 0;
  344. HSendPacket (i, 0);
  345. }
  346. }
  347. //
  348. // listen for other packets
  349. //
  350. listen:
  351. GetPackets ();
  352. }
  353. /*
  354. =====================
  355. =
  356. = CheckAbort
  357. =
  358. =====================
  359. */
  360. void CheckAbort (void)
  361. {
  362. event_t *ev;
  363. int stoptic;
  364. stoptic = I_GetTime () + 2;
  365. while (I_GetTime() < stoptic)
  366. I_StartTic ();
  367. I_StartTic ();
  368. for ( ; eventtail != eventhead
  369. ; eventtail = (++eventtail)&(MAXEVENTS-1) )
  370. {
  371. ev = &events[eventtail];
  372. if (ev->type == ev_keydown && ev->data1 == KEY_ESCAPE)
  373. I_Error ("Network game synchronization aborted.");
  374. }
  375. }
  376. /*
  377. =====================
  378. =
  379. = D_ArbitrateNetStart
  380. =
  381. =====================
  382. */
  383. void D_ArbitrateNetStart (void)
  384. {
  385. int i;
  386. boolean gotinfo[MAXNETNODES];
  387. autostart = true;
  388. memset (gotinfo,0,sizeof(gotinfo));
  389. if (doomcom->consoleplayer)
  390. { // listen for setup info from key player
  391. // mprintf ("listening for network start info...\n");
  392. while (1)
  393. {
  394. CheckAbort ();
  395. if (!HGetPacket ())
  396. continue;
  397. if (netbuffer->checksum & NCMD_SETUP)
  398. {
  399. if (netbuffer->player != VERSION)
  400. I_Error ("Different DOOM versions cannot play a net game!");
  401. startskill = netbuffer->retransmitfrom & 15;
  402. deathmatch = (netbuffer->retransmitfrom & 0xc0) >> 6;
  403. nomonsters = (netbuffer->retransmitfrom & 0x20) > 0;
  404. respawnparm = (netbuffer->retransmitfrom & 0x10) > 0;
  405. //startmap = netbuffer->starttic & 0x3f;
  406. //startepisode = netbuffer->starttic >> 6;
  407. startmap = netbuffer->starttic&15;
  408. startepisode = netbuffer->starttic>>4;
  409. return;
  410. }
  411. }
  412. }
  413. else
  414. { // key player, send the setup info
  415. // mprintf ("sending network start info...\n");
  416. do
  417. {
  418. CheckAbort ();
  419. for (i=0 ; i<doomcom->numnodes ; i++)
  420. {
  421. netbuffer->retransmitfrom = startskill;
  422. if (deathmatch)
  423. netbuffer->retransmitfrom |= (deathmatch<<6);
  424. if (nomonsters)
  425. netbuffer->retransmitfrom |= 0x20;
  426. if (respawnparm)
  427. netbuffer->retransmitfrom |= 0x10;
  428. //netbuffer->starttic = startepisode * 64 + startmap;
  429. netbuffer->starttic = (startepisode<<4)+startmap;
  430. netbuffer->player = VERSION;
  431. netbuffer->numtics = 0;
  432. HSendPacket (i, NCMD_SETUP);
  433. }
  434. #if 1
  435. for(i = 10 ; i && HGetPacket(); --i)
  436. {
  437. if((netbuffer->player&0x7f) < MAXNETNODES)
  438. gotinfo[netbuffer->player&0x7f] = true;
  439. }
  440. #else
  441. while (HGetPacket ())
  442. {
  443. gotinfo[netbuffer->player&0x7f] = true;
  444. }
  445. #endif
  446. for (i=1 ; i<doomcom->numnodes ; i++)
  447. if (!gotinfo[i])
  448. break;
  449. } while (i < doomcom->numnodes);
  450. }
  451. }
  452. /*
  453. ===================
  454. =
  455. = D_CheckNetGame
  456. =
  457. = Works out player numbers among the net participants
  458. ===================
  459. */
  460. extern int viewangleoffset;
  461. void D_CheckNetGame (void)
  462. {
  463. int i;
  464. for (i=0 ; i<MAXNETNODES ; i++)
  465. {
  466. nodeingame[i] = false;
  467. nettics[i] = 0;
  468. remoteresend[i] = false; // set when local needs tics
  469. resendto[i] = 0; // which tic to start sending
  470. }
  471. // I_InitNetwork sets doomcom and netgame
  472. I_InitNetwork ();
  473. if (doomcom->id != DOOMCOM_ID)
  474. I_Error ("Doomcom buffer invalid!");
  475. netbuffer = &doomcom->data;
  476. consoleplayer = displayplayer = doomcom->consoleplayer;
  477. if (netgame)
  478. D_ArbitrateNetStart ();
  479. //printf ("startskill %i deathmatch: %i startmap: %i startepisode: %i\n", startskill, deathmatch, startmap, startepisode);
  480. // read values out of doomcom
  481. ticdup = doomcom->ticdup;
  482. maxsend = BACKUPTICS/(2*ticdup)-1;
  483. if (maxsend<1)
  484. maxsend = 1;
  485. for (i=0 ; i<doomcom->numplayers ; i++)
  486. playeringame[i] = true;
  487. for (i=0 ; i<doomcom->numnodes ; i++)
  488. nodeingame[i] = true;
  489. //printf ("player %i of %i (%i nodes)\n", consoleplayer+1, doomcom->numplayers, doomcom->numnodes);
  490. }
  491. /*
  492. ==================
  493. =
  494. = D_QuitNetGame
  495. =
  496. = Called before quitting to leave a net game without hanging the
  497. = other players
  498. =
  499. ==================
  500. */
  501. void D_QuitNetGame (void)
  502. {
  503. int i, j;
  504. if (debugfile)
  505. fclose (debugfile);
  506. if (!netgame || !usergame || consoleplayer == -1 || demoplayback)
  507. return;
  508. // send a bunch of packets for security
  509. netbuffer->player = consoleplayer;
  510. netbuffer->numtics = 0;
  511. for (i=0 ; i<4 ; i++)
  512. {
  513. for (j=1 ; j<doomcom->numnodes ; j++)
  514. if (nodeingame[j])
  515. HSendPacket (j, NCMD_EXIT);
  516. I_WaitVBL (1);
  517. }
  518. }
  519. /*
  520. ===============
  521. =
  522. = TryRunTics
  523. =
  524. ===============
  525. */
  526. int frametics[4], frameon;
  527. int frameskip[4];
  528. int oldnettics;
  529. extern boolean advancedemo;
  530. void TryRunTics (void)
  531. {
  532. int i;
  533. int lowtic;
  534. int entertic;
  535. static int oldentertics;
  536. int realtics, availabletics;
  537. int counts;
  538. int numplaying;
  539. //
  540. // get real tics
  541. //
  542. entertic = I_GetTime ()/ticdup;
  543. realtics = entertic - oldentertics;
  544. oldentertics = entertic;
  545. //
  546. // get available tics
  547. //
  548. NetUpdate ();
  549. lowtic = MAXINT;
  550. numplaying = 0;
  551. for (i=0 ; i<doomcom->numnodes ; i++)
  552. if (nodeingame[i])
  553. {
  554. numplaying++;
  555. if (nettics[i] < lowtic)
  556. lowtic = nettics[i];
  557. }
  558. availabletics = lowtic - gametic/ticdup;
  559. //
  560. // decide how many tics to run
  561. //
  562. if (realtics < availabletics-1)
  563. counts = realtics+1;
  564. else if (realtics < availabletics)
  565. counts = realtics;
  566. else
  567. counts = availabletics;
  568. if (counts < 1)
  569. counts = 1;
  570. frameon++;
  571. if (debugfile)
  572. fprintf (debugfile,"=======real: %i avail: %i game: %i\n",realtics, availabletics,counts);
  573. if (!demoplayback)
  574. {
  575. //=============================================================================
  576. //
  577. // ideally nettics[0] should be 1 - 3 tics above lowtic
  578. // if we are consistantly slower, speed up time
  579. //
  580. for (i=0 ; i<MAXPLAYERS ; i++)
  581. if (playeringame[i])
  582. break;
  583. if (consoleplayer == i)
  584. { // the key player does not adapt
  585. }
  586. else
  587. {
  588. if (nettics[0] <= nettics[nodeforplayer[i]])
  589. {
  590. gametime--;
  591. // printf ("-");
  592. }
  593. frameskip[frameon&3] = (oldnettics > nettics[nodeforplayer[i]]);
  594. oldnettics = nettics[0];
  595. if (frameskip[0] && frameskip[1] && frameskip[2] && frameskip[3])
  596. {
  597. skiptics = 1;
  598. // printf ("+");
  599. }
  600. }
  601. //=============================================================================
  602. } // demoplayback
  603. //
  604. // wait for new tics if needed
  605. //
  606. while (lowtic < gametic/ticdup + counts)
  607. {
  608. NetUpdate ();
  609. lowtic = MAXINT;
  610. for (i=0 ; i<doomcom->numnodes ; i++)
  611. if (nodeingame[i] && nettics[i] < lowtic)
  612. lowtic = nettics[i];
  613. if (lowtic < gametic/ticdup)
  614. I_Error ("TryRunTics: lowtic < gametic");
  615. // don't stay in here forever -- give the menu a chance to work
  616. if (I_GetTime ()/ticdup - entertic >= 20)
  617. {
  618. MN_Ticker ();
  619. return;
  620. }
  621. }
  622. //
  623. // run the count * ticdup dics
  624. //
  625. while (counts--)
  626. {
  627. for (i=0 ; i<ticdup ; i++)
  628. {
  629. if (gametic/ticdup > lowtic)
  630. I_Error ("gametic>lowtic");
  631. if (advancedemo)
  632. D_DoAdvanceDemo ();
  633. MN_Ticker ();
  634. G_Ticker ();
  635. gametic++;
  636. //
  637. // modify command for duplicated tics
  638. //
  639. if (i != ticdup-1)
  640. {
  641. ticcmd_t *cmd;
  642. int buf;
  643. int j;
  644. buf = (gametic/ticdup)%BACKUPTICS;
  645. for (j=0 ; j<MAXPLAYERS ; j++)
  646. {
  647. cmd = &netcmds[j][buf];
  648. cmd->chatchar = 0;
  649. if (cmd->buttons & BT_SPECIAL)
  650. cmd->buttons = 0;
  651. }
  652. }
  653. }
  654. NetUpdate (); // check for new console commands
  655. }
  656. }