home *** CD-ROM | disk | FTP | other *** search
/ 1,001 Nights of Doom / 1001NightsOfDoom1995wickedSensations.iso / modem / ser6_src.zip / CONNECT.C next >
Text File  |  1994-11-12  |  2KB  |  109 lines

  1. void Connect (void)
  2. {
  3.     struct time     time;
  4.     int                     oldsec;
  5.     int                     localstage, remotestage;
  6.     char            str[20];
  7.     char            idstr[7];
  8.     char            remoteidstr[7];
  9.     unsigned long           idnum;
  10.     int                     i;
  11.     
  12. //
  13. // wait for a good packet
  14. //
  15.     printf (STR_ATTEMPT"\n");
  16.  
  17. //
  18. // build a (hopefully) unique id string by hashing up the current milliseconds
  19. // and the interrupt table
  20. //
  21.     if (CheckParm ("-player1"))
  22.         idnum = 0;
  23.     else if (CheckParm ("-player2"))
  24.         idnum = 999999;
  25.     else
  26.     {
  27.         gettime (&time);
  28.         idnum = time.ti_sec*100+time.ti_hund;
  29.         for (i=0 ; i<512 ; i++)
  30.             idnum += ((unsigned far *)0)[i];
  31.         idnum %= 1000000;
  32.     }
  33.     
  34.     idstr[0] = '0' + idnum/ 100000l;
  35.     idnum -= (idstr[0]-'0')*100000l;
  36.     idstr[1] = '0' + idnum/ 10000l;
  37.     idnum -= (idstr[1]-'0')*10000l;
  38.     idstr[2] = '0' + idnum/ 1000l;
  39.     idnum -= (idstr[2]-'0')*1000l;
  40.     idstr[3] = '0' + idnum/ 100l;
  41.     idnum -= (idstr[3]-'0')*100l;
  42.     idstr[4] = '0' + idnum/ 10l;
  43.     idnum -= (idstr[4]-'0')*10l;
  44.     idstr[5] = '0' + idnum;
  45.     idstr[6] = 0;
  46.     
  47. //
  48. // sit in a loop until things are worked out
  49. //
  50. // the packet is:  ID000000_0
  51. // the first field is the idnum, the second is the acknowledge stage
  52. // ack stage starts out 0, is bumped to 1 after the other computer's id
  53. // is known, and is bumped to 2 after the other computer has raised to 1
  54. //
  55.     oldsec = -1;
  56.     localstage = remotestage = 0;
  57.  
  58.     do
  59.     {
  60.         while ( bioskey(1) )
  61.         {
  62.             if ( (bioskey (0) & 0xff) == 27)
  63.                 Error ("\n\n"STR_NETABORT);
  64.         }
  65.  
  66.         if (ReadPacket ())
  67.         {
  68.             packet[packetlen] = 0;
  69.             printf ("read : %s\n",packet);
  70.             if (packetlen != 10)
  71.                 continue;
  72.             if (strncmp(packet,"ID",2) )
  73.                 continue;
  74.             if (!strncmp (packet+2,idstr,6))
  75.                 Error ("\n\n"STR_DUPLICATE);
  76.             strncpy (remoteidstr,packet+2,6);
  77.                 
  78.             remotestage = packet[9] - '0';
  79.             localstage = remotestage+1;
  80.             oldsec = -1;
  81.         }
  82.  
  83.         gettime (&time);
  84.         if (time.ti_sec != oldsec)
  85.         {
  86.             oldsec = time.ti_sec;
  87.             sprintf (str,"ID%s_%i",idstr,localstage);
  88.             WritePacket (str,strlen(str));
  89.             printf ("wrote: %s\n",str);
  90.         }
  91.  
  92.     } while (localstage < 2);
  93.  
  94. //
  95. // decide who is who
  96. //
  97.     if (strcmp(remoteidstr,idstr) > 0)
  98.         doomcom.consoleplayer = 0;
  99.     else
  100.         doomcom.consoleplayer = 1;
  101.     
  102.  
  103. //
  104. // flush out any extras
  105. //
  106.     while (ReadPacket ())
  107.     ;
  108. }
  109.