home *** CD-ROM | disk | FTP | other *** search
/ Doom I/II Collection / DM12.ISO / serial / ipxsrc / ipxsetup.c < prev    next >
C/C++ Source or Header  |  1994-02-13  |  5KB  |  284 lines

  1. // ipxsetup.c
  2.  
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <dos.h>
  7. #include <string.h>
  8. #include <process.h>
  9. #include <stdarg.h>
  10. #include <bios.h>
  11.  
  12. #include "ipxnet.h"
  13.  
  14. int            gameid;
  15. int            numnetnodes;
  16.  
  17. setupdata_t    nodesetup[MAXNETNODES];
  18.  
  19.  
  20. /*
  21. =================
  22. =
  23. = Error
  24. =
  25. = For abnormal program terminations
  26. =
  27. =================
  28. */
  29.  
  30. void Error (char *error, ...)
  31. {
  32.     va_list argptr;
  33.  
  34.     if (vectorishooked)
  35.         setvect (doomcom.intnum,olddoomvect);
  36.  
  37.     va_start (argptr,error);
  38.     vprintf (error,argptr);
  39.     va_end (argptr);
  40.     printf ("\n");
  41.     exit (1);
  42. }
  43.  
  44.  
  45. /*
  46. =============
  47. =
  48. = NetISR
  49. =
  50. =============
  51. */
  52.  
  53. void interrupt NetISR (void)
  54. {
  55.     if (doomcom.command == CMD_SEND)
  56.     {
  57. //I_ColorBlack (63,0,0);
  58.         localtime++;
  59.         SendPacket (doomcom.remotenode);
  60. //I_ColorBlack (0,0,0);
  61.     }
  62.     else if (doomcom.command == CMD_GET)
  63.     {
  64. //I_ColorBlack (0,63,0);
  65.         GetPacket ();
  66. //I_ColorBlack (0,0,0);
  67.     }
  68. }
  69.  
  70.  
  71.  
  72. /*
  73. ===================
  74. =
  75. = LookForNodes
  76. =
  77. = Finds all the nodes for the game and works out player numbers among them
  78. =
  79. = Exits with nodesetup[0..numnodes] and nodeadr[0..numnodes] filled in
  80. ===================
  81. */
  82.  
  83. void LookForNodes (void)
  84. {
  85.     int             i,j,k;
  86.     int             netids[MAXNETNODES];
  87.     int             netplayer[MAXNETNODES];
  88.     struct time        time;
  89.     int                oldsec;
  90.     setupdata_t        *setup, *dest;
  91.     char            str[80];
  92.     int            total, console;
  93.  
  94. //
  95. // wait until we get [numnetnodes] packets, then start playing
  96. // the playernumbers are assigned by netid
  97. //
  98.     printf("Attempting to find all players for %i player net play. "
  99.         "Press ESC to exit.\n", numnetnodes);
  100.  
  101.     printf ("Looking for a node");
  102.  
  103.     oldsec = -1;
  104.     setup = (setupdata_t *)&doomcom.data;
  105.     localtime = -1;        // in setup time, not game time
  106.  
  107. //
  108. // build local setup info
  109. //
  110.     nodesetup[0].nodesfound = 1;
  111.     nodesetup[0].nodeswanted = numnetnodes;
  112.     doomcom.numnodes = 1;
  113.  
  114.     do
  115.     {
  116. //
  117. // check for aborting
  118. //
  119.         while ( bioskey(1) )
  120.         {
  121.             if ( (bioskey (0) & 0xff) == 27)
  122.                 Error ("\n\nNetwork game synchronization aborted.");
  123.         }
  124.  
  125. //
  126. // listen to the network
  127. //
  128.         while (GetPacket ())
  129.         {
  130.             if (doomcom.remotenode == -1)
  131.                 dest = &nodesetup[doomcom.numnodes];
  132.             else
  133.                 dest = &nodesetup[doomcom.remotenode];
  134.  
  135.             if (remotetime != -1)
  136.             {    // an early game packet, not a setup packet
  137.                 if (doomcom.remotenode == -1)
  138.                     Error ("Got an unknown game packet during setup");
  139.                 // if it allready started, it must have found all nodes
  140.                 dest->nodesfound = dest->nodeswanted;
  141.                 continue;
  142.             }
  143.  
  144.             // update setup ingo
  145.             memcpy (dest, setup, sizeof(*dest) );
  146.  
  147.             if (doomcom.remotenode != -1)
  148.                 continue;            // allready know that node address
  149.  
  150.             //
  151.             // this is a new node
  152.             //
  153.             memcpy (&nodeadr[doomcom.numnodes], &remoteadr
  154.             , sizeof(nodeadr[doomcom.numnodes]) );
  155.  
  156.             //
  157.             // if this node has a lower address, take all startup info
  158.             //
  159.             if ( memcmp (&remoteadr, &nodeadr[0], sizeof(&remoteadr) ) < 0 )
  160.             {
  161.             }
  162.  
  163.             doomcom.numnodes++;
  164.  
  165.             printf ("\nFound a node!\n");
  166.  
  167.             if (doomcom.numnodes < numnetnodes)
  168.                 printf ("Looking for node");
  169.         }
  170. //
  171. // we are done if all nodes have found all other nodes
  172. //
  173.         for (i=0 ; i<doomcom.numnodes ; i++)
  174.             if (nodesetup[i].nodesfound != nodesetup[i].nodeswanted)
  175.                 break;
  176.  
  177.         if (i == nodesetup[0].nodeswanted)
  178.             break;        // got them all
  179.  
  180. //
  181. // send out a broadcast packet every second
  182. //
  183.         gettime (&time);
  184.         if (time.ti_sec == oldsec)
  185.             continue;
  186.         oldsec = time.ti_sec;
  187.  
  188.         printf (".");
  189.         doomcom.datalength = sizeof(*setup);
  190.  
  191.         nodesetup[0].nodesfound = doomcom.numnodes;
  192.  
  193.         memcpy (&doomcom.data, &nodesetup[0], sizeof(*setup));
  194.  
  195.         SendPacket (MAXNETNODES);    // send to all
  196.  
  197.     } while (1);
  198.  
  199. //
  200. // count players
  201. //
  202.     total = 0;
  203.     console = 0;
  204.  
  205.     for (i=0 ; i<numnetnodes ; i++)
  206.     {
  207.         if (nodesetup[i].drone)
  208.             continue;
  209.         total++;
  210.         if (total > MAXPLAYERS)
  211.             Error ("More than %i players specified!",MAXPLAYERS);
  212.         if (memcmp (&nodeadr[i], &nodeadr[0], sizeof(nodeadr[0])) < 0)
  213.             console++;
  214.     }
  215.  
  216.  
  217.     if (!total)
  218.         Error ("No players specified for game!");
  219.  
  220.     doomcom.consoleplayer = console;
  221.     doomcom.numplayers = total;
  222.  
  223.     printf ("Console is player %i of %i\n", console+1, total);
  224. }
  225.  
  226.  
  227.  
  228. /*
  229. =============
  230. =
  231. = main
  232. =
  233. =============
  234. */
  235.  
  236. void main (void)
  237. {
  238.     int    i;
  239.  
  240. //
  241. // determine game parameters
  242. //
  243.     gameid = 0;
  244.     numnetnodes = 2;
  245.     doomcom.ticdup = 1;
  246.     doomcom.extratics = 1;
  247.     doomcom.episode = 1;
  248.     doomcom.map = 1;
  249.     doomcom.skill = 2;
  250.     doomcom.deathmatch = 0;
  251.  
  252.  
  253.     printf("\n"
  254.            "--------------------------\n"
  255.            "DOOM NETWORK DEVICE DRIVER\n"
  256.            "--------------------------\n");
  257.  
  258.     i = CheckParm ("-nodes");
  259.     if (i && i < _argc-1)
  260.     {
  261.         numnetnodes = atoi(_argv[i+1]);
  262.     }
  263.  
  264. // make sure the network exists and create a bunch of buffers
  265.     InitNetwork ();
  266.  
  267. // get addresses of all nodes
  268.     LookForNodes ();
  269.  
  270.     localtime = 0;            // no longer in setup
  271.  
  272. //
  273. // launch DOOM
  274. //
  275.     LaunchDOOM ();
  276.  
  277. // cleanup
  278.     ShutdownNetwork ();
  279.  
  280.     Error (NULL);
  281. }
  282.  
  283.  
  284.