home *** CD-ROM | disk | FTP | other *** search
/ Launch & Play / spustahrej2.iso / Egoboo / code / network.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-03  |  54.4 KB  |  1,633 lines

  1. // network.c
  2.  
  3. // Egoboo, Copyright (C) 2000 Aaron Bishop
  4.  
  5. #include "egoboo.h"
  6.  
  7. //--------------------------------------------------------------------------------------------
  8. void close_session()
  9. {
  10.     // ZZ> This function gets the computer out of a network game
  11. /*PORT
  12.     if(networkon)
  13.     {
  14.         lpDirectPlay3A->Close();
  15.     }
  16. */
  17. }
  18.  
  19. //--------------------------------------------------------------------------------------------
  20. int add_player(unsigned short character, unsigned short player, unsigned char device)
  21. {
  22.     // ZZ> This function adds a player, returning FALSE if it fails, TRUE otherwise
  23.     int cnt;
  24.  
  25.     if(plavalid[player] == FALSE)
  26.     {
  27.         chrisplayer[character] = TRUE;
  28.         plaindex[player] = character;
  29.         plavalid[player] = TRUE;
  30.         pladevice[player] = device;
  31.         if(device != INPUTNONE)  nolocalplayers = FALSE;
  32.         plalatchx[player] = 0;
  33.         plalatchy[player] = 0;
  34.         plalatchbutton[player] = 0;
  35.         cnt = 0;
  36.         while(cnt < MAXLAG)
  37.         {
  38.             platimelatchx[player][cnt] = 0;
  39.             platimelatchy[player][cnt] = 0;
  40.             platimelatchbutton[player][cnt] = 0;
  41.             cnt++;
  42.         }
  43.         if(device != INPUTNONE)
  44.         {
  45.             chrislocalplayer[character] = TRUE;
  46.             numlocalpla++;
  47.         }
  48.         numpla++;
  49.         return TRUE;
  50.     }
  51.     return FALSE;
  52. }
  53.  
  54. //--------------------------------------------------------------------------------------------
  55. void clear_messages()
  56. {
  57.     // ZZ> This function empties the message buffer
  58.     int cnt;
  59.  
  60.     cnt = 0;
  61.     while(cnt < MAXMESSAGE)
  62.     {
  63.         msgtime[cnt] = 0;
  64.         cnt++;
  65.     }
  66. }
  67.  
  68. //--------------------------------------------------------------------------------------------
  69. void clear_select()
  70. {
  71.     // ZZ> This function clears the RTS select list
  72.     numrtsselect = 0;
  73. }
  74.  
  75. //--------------------------------------------------------------------------------------------
  76. void add_select(unsigned short character)
  77. {
  78.     // ZZ> This function selects a character
  79.     if(numrtsselect < MAXSELECT)
  80.     {
  81.         rtsselect[numrtsselect] = character;
  82.         numrtsselect++;
  83.     }
  84. }
  85.  
  86. //--------------------------------------------------------------------------------------------
  87. void check_add(unsigned char key, char bigletter, char littleletter)
  88. {
  89.     // ZZ> This function adds letters to the net message
  90. /*PORT
  91.     if(KEYDOWN(key))
  92.     {
  93.         if(keypress[key]==FALSE)
  94.         {
  95.             keypress[key] = TRUE;
  96.             if(netmessagewrite < MESSAGESIZE-2)
  97.             {
  98.                 if(KEYDOWN(DIK_LSHIFT) || KEYDOWN(DIK_RSHIFT))
  99.                 {
  100.                     netmessage[netmessagewrite] = bigletter;
  101.                 }
  102.                 else
  103.                 {
  104.                     netmessage[netmessagewrite] = littleletter;
  105.                 }
  106.                 netmessagewrite++;
  107.                 netmessage[netmessagewrite] = '?'; // The flashing input cursor
  108.                 netmessage[netmessagewrite+1] = 0;
  109.             }
  110.         }
  111.     }
  112.     else
  113.     {
  114.         keypress[key] = FALSE;
  115.     }
  116. */
  117. }
  118.  
  119. //--------------------------------------------------------------------------------------------
  120. void start_building_packet()
  121. {
  122.     // ZZ> This function starts building a network packet
  123.     packethead = 0;
  124.     packetsize = 0;
  125. }
  126.  
  127. //--------------------------------------------------------------------------------------------
  128. void start_reading_packet()
  129. {
  130.     // ZZ> This function starts reading a network packet
  131.     packethead = 0;
  132. }
  133.  
  134. //--------------------------------------------------------------------------------------------
  135. void add_packet_uc(unsigned char uc)
  136. {
  137.     // ZZ> This function appends an unsigned char to the packet
  138.     unsigned char* ucp;
  139.     ucp = (unsigned char*) (&packetbuffer[packethead]);
  140.     *ucp = uc;
  141.     packethead+=1;
  142.     packetsize+=1;
  143. }
  144.  
  145. //--------------------------------------------------------------------------------------------
  146. void add_packet_sc(signed char sc)
  147. {
  148.     // ZZ> This function appends a signed char to the packet
  149.     signed char* scp;
  150.     scp = (signed char*) (&packetbuffer[packethead]);
  151.     *scp = sc;
  152.     packethead+=1;
  153.     packetsize+=1;
  154. }
  155.  
  156. //--------------------------------------------------------------------------------------------
  157. void add_packet_us(unsigned short us)
  158. {
  159.     // ZZ> This function appends an unsigned short to the packet
  160.     unsigned short* usp;
  161.     usp = (unsigned short*) (&packetbuffer[packethead]);
  162.     *usp = us;
  163.     packethead+=2;
  164.     packetsize+=2;
  165. }
  166.  
  167. //--------------------------------------------------------------------------------------------
  168. void add_packet_ss(signed short ss)
  169. {
  170.     // ZZ> This function appends a signed short to the packet
  171.     signed short* ssp;
  172.     ssp = (signed short*) (&packetbuffer[packethead]);
  173.     *ssp = ss;
  174.     packethead+=2;
  175.     packetsize+=2;
  176. }
  177.  
  178. //--------------------------------------------------------------------------------------------
  179. void add_packet_ui(unsigned int ui)
  180. {
  181.     // ZZ> This function appends an unsigned int to the packet
  182.     unsigned int* uip;
  183.     uip = (unsigned int*) (&packetbuffer[packethead]);
  184.     *uip = ui;
  185.     packethead+=4;
  186.     packetsize+=4;
  187. }
  188.  
  189. //--------------------------------------------------------------------------------------------
  190. void add_packet_si(signed int si)
  191. {
  192.     // ZZ> This function appends a signed int to the packet
  193.     signed int* sip;
  194.     sip = (signed int*) (&packetbuffer[packethead]);
  195.     *sip = si;
  196.     packethead+=4;
  197.     packetsize+=4;
  198. }
  199.  
  200. //--------------------------------------------------------------------------------------------
  201. void add_packet_sz(char *string)
  202. {
  203.     // ZZ> This function appends a null terminated string to the packet
  204.     char* cp;
  205.     char cTmp;
  206.     int cnt;
  207.  
  208.     cnt = 0;
  209.     cTmp = 1;
  210.     cp = (char*) (&packetbuffer[packethead]);
  211.     while(cTmp != 0)
  212.     {
  213.         cTmp = string[cnt];
  214.         *cp = cTmp;
  215.         cp+=1;
  216.         packethead+=1;
  217.         packetsize+=1;
  218.         cnt++;
  219.     }
  220. }
  221.  
  222. //--------------------------------------------------------------------------------------------
  223. void read_packet_sz(char *text)
  224. {
  225.     // ZZ> This function reads a null terminated string from the packet
  226.     unsigned char uc;
  227.     unsigned short outindex;
  228.  
  229.  
  230.     outindex = 0;
  231.     uc = packetbuffer[packethead];
  232.     packethead++;
  233.     while(uc != 0 && outindex < 255)
  234.     {
  235.         text[outindex] = uc;
  236.         outindex++;
  237.         uc = packetbuffer[packethead];
  238.         packethead++;
  239.     }
  240.     text[outindex] = 0;
  241. }
  242.  
  243. //--------------------------------------------------------------------------------------------
  244. unsigned char read_packet_uc()
  245. {
  246.     // ZZ> This function reads an unsigned char from the packet
  247.     unsigned char uc;
  248.     unsigned char* ucp;
  249.     ucp = (unsigned char*) (&packetbuffer[packethead]);
  250.     uc = *ucp;
  251.     packethead+=1;
  252.     return uc;
  253. }
  254.  
  255. //--------------------------------------------------------------------------------------------
  256. signed char read_packet_sc()
  257. {
  258.     // ZZ> This function reads a signed char from the packet
  259.     signed char sc;
  260.     signed char* scp;
  261.     scp = (signed char*) (&packetbuffer[packethead]);
  262.     sc = *scp;
  263.     packethead+=1;
  264.     return sc;
  265. }
  266.  
  267. //--------------------------------------------------------------------------------------------
  268. unsigned short read_packet_us()
  269. {
  270.     // ZZ> This function reads an unsigned short from the packet
  271.     unsigned short us;
  272.     unsigned short* usp;
  273.     usp = (unsigned short*) (&packetbuffer[packethead]);
  274.     us = *usp;
  275.     packethead+=2;
  276.     return us;
  277. }
  278.  
  279. //--------------------------------------------------------------------------------------------
  280. signed short read_packet_ss()
  281. {
  282.     // ZZ> This function reads a signed short from the packet
  283.     signed short ss;
  284.     signed short* ssp;
  285.     ssp = (signed short*) (&packetbuffer[packethead]);
  286.     ss = *ssp;
  287.     packethead+=2;
  288.     return ss;
  289. }
  290.  
  291. //--------------------------------------------------------------------------------------------
  292. unsigned int read_packet_ui()
  293. {
  294.     // ZZ> This function reads an unsigned int from the packet
  295.     unsigned int ui;
  296.     unsigned int* uip;
  297.     uip = (unsigned int*) (&packetbuffer[packethead]);
  298.     ui = *uip;
  299.     packethead+=4;
  300.     return ui;
  301. }
  302.  
  303. //--------------------------------------------------------------------------------------------
  304. signed int read_packet_si()
  305. {
  306.     // ZZ> This function reads a signed int from the packet
  307.     signed int si;
  308.     signed int* sip;
  309.     sip = (signed int*) (&packetbuffer[packethead]);
  310.     si = *sip;
  311.     packethead+=4;
  312.     return si;
  313. }
  314.  
  315. //--------------------------------------------------------------------------------------------
  316. int still_reading_packet()
  317. {
  318.     // ZZ> This function tells if there's still data left in the packet
  319.     return (packethead < packetsize);
  320. }
  321.  
  322. //--------------------------------------------------------------------------------------------
  323. void send_packet_to_host()
  324. {
  325. /*PORT
  326.     // ZZ> This function sends a packet to the host
  327.     lpDirectPlay3A->Send(selfid, DPID_SERVERPLAYER, 0, packetbuffer, packetsize);
  328. */
  329. }
  330.  
  331. //--------------------------------------------------------------------------------------------
  332. void send_packet_to_all_players()
  333. {
  334.     // ZZ> This function sends a packet to all the players
  335. /*PORT
  336.     lpDirectPlay3A->Send(selfid, DPID_ALLPLAYERS, 0, packetbuffer, packetsize);
  337. */
  338. }
  339.  
  340. //--------------------------------------------------------------------------------------------
  341. void send_packet_to_host_guaranteed()
  342. {
  343.     // ZZ> This function sends a packet to the host
  344. /*PORT
  345.     lpDirectPlay3A->Send(selfid, DPID_SERVERPLAYER, DPSEND_GUARANTEED, packetbuffer, packetsize);
  346. */
  347. }
  348.  
  349. //--------------------------------------------------------------------------------------------
  350. void send_packet_to_all_players_guaranteed()
  351. {
  352.     // ZZ> This function sends a packet to all the players
  353. /*PORT
  354.     lpDirectPlay3A->Send(selfid, DPID_ALLPLAYERS, DPSEND_GUARANTEED, packetbuffer, packetsize);
  355. */
  356. }
  357.  
  358. //--------------------------------------------------------------------------------------------
  359. void send_packet_to_one_player_guaranteed(int player)
  360. {
  361.     // ZZ> This function sends a packet to one of the players
  362. /*PORT
  363.     if(player < numplayer)
  364.     {
  365.         lpDirectPlay3A->Send(selfid, netplayerid[player], DPSEND_GUARANTEED, packetbuffer, packetsize);
  366.     }
  367. */
  368. }
  369.  
  370. //--------------------------------------------------------------------------------------------
  371. void input_net_message()
  372. {
  373.     // ZZ> This function lets players communicate over network by hitting return, then
  374.     //     typing text, then return again
  375. /*PORT
  376.     int cnt;
  377.     char cTmp;
  378.  
  379.  
  380.     if(netmessagemode)
  381.     {
  382.         // Add new letters
  383.         check_add(DIK_A, 'A', 'a');
  384.         check_add(DIK_B, 'B', 'b');
  385.         check_add(DIK_C, 'C', 'c');
  386.         check_add(DIK_D, 'D', 'd');
  387.         check_add(DIK_E, 'E', 'e');
  388.         check_add(DIK_F, 'F', 'f');
  389.         check_add(DIK_G, 'G', 'g');
  390.         check_add(DIK_H, 'H', 'h');
  391.         check_add(DIK_I, 'I', 'i');
  392.         check_add(DIK_J, 'J', 'j');
  393.         check_add(DIK_K, 'K', 'k');
  394.         check_add(DIK_L, 'L', 'l');
  395.         check_add(DIK_M, 'M', 'm');
  396.         check_add(DIK_N, 'N', 'n');
  397.         check_add(DIK_O, 'O', 'o');
  398.         check_add(DIK_P, 'P', 'p');
  399.         check_add(DIK_Q, 'Q', 'q');
  400.         check_add(DIK_R, 'R', 'r');
  401.         check_add(DIK_S, 'S', 's');
  402.         check_add(DIK_T, 'T', 't');
  403.         check_add(DIK_U, 'U', 'u');
  404.         check_add(DIK_V, 'V', 'v');
  405.         check_add(DIK_W, 'W', 'w');
  406.         check_add(DIK_X, 'X', 'x');
  407.         check_add(DIK_Y, 'Y', 'y');
  408.         check_add(DIK_Z, 'Z', 'z');
  409.  
  410.  
  411.         check_add(DIK_1, '!', '1');
  412.         check_add(DIK_2, '@', '2');
  413.         check_add(DIK_3, '#', '3');
  414.         check_add(DIK_4, '$', '4');
  415.         check_add(DIK_5, '%', '5');
  416.         check_add(DIK_6, '^', '6');
  417.         check_add(DIK_7, '&', '7');
  418.         check_add(DIK_8, '*', '8');
  419.         check_add(DIK_9, '(', '9');
  420.         check_add(DIK_0, ')', '0');
  421.  
  422.  
  423.         check_add(DIK_APOSTROPHE, 34, 39);
  424.         check_add(DIK_SPACE,      ' ', ' ');
  425.         check_add(DIK_SEMICOLON,  ':', ';');
  426.         check_add(DIK_PERIOD,     '>', '.');
  427.         check_add(DIK_COMMA,      '<', ',');
  428.         check_add(DIK_GRAVE,      '`', '`');
  429.         check_add(DIK_MINUS,      '_', '-');
  430.         check_add(DIK_EQUALS,     '+', '=');
  431.         check_add(DIK_LBRACKET,   '{', '[');
  432.         check_add(DIK_RBRACKET,   '}', ']');
  433.         check_add(DIK_BACKSLASH,  '|', '\\');
  434.         check_add(DIK_SLASH,      '?', '/');
  435.  
  436.  
  437.  
  438.         // Make cursor flash
  439.         if(netmessagewrite < MESSAGESIZE-1)
  440.         {
  441.             if((wldframe & 8) == 0)
  442.             {
  443.                 netmessage[netmessagewrite] = '#';
  444.             }
  445.             else
  446.             {
  447.                 netmessage[netmessagewrite] = '+';
  448.             }
  449.         }
  450.  
  451.  
  452.         // Check backspace and return
  453.         if(netmessagedelay == 0)
  454.         {
  455.             if(KEYDOWN(DIK_BACK))
  456.             {
  457.                 if(netmessagewrite < MESSAGESIZE)  netmessage[netmessagewrite] = 0;
  458.                 if(netmessagewrite > netmessagewritemin) netmessagewrite--;
  459.                 netmessagedelay = 3;
  460.             }
  461.  
  462.  
  463.             // Ship out the message
  464.             if(KEYDOWN(DIK_RETURN))
  465.             {
  466.                 // Is it long enough to bother?
  467.                 if(netmessagewrite > 0)
  468.                 {
  469.                     // Yes, so send it
  470.                     netmessage[netmessagewrite] = 0;
  471.                     if(networkon)
  472.                     {
  473.                         start_building_packet();
  474.                         add_packet_us(TO_ANY_TEXT);
  475.                         add_packet_sz(netmessage);
  476.                         send_packet_to_all_players();
  477.                     }
  478.                 }
  479.                 netmessagemode = FALSE;
  480.                 netmessagedelay = 20;
  481.             }
  482.         }
  483.         else
  484.         {
  485.             netmessagedelay--;
  486.         }
  487.     }
  488.     else
  489.     {
  490.         // Input a new message?
  491.         if(netmessagedelay == 0)
  492.         {
  493.             if(KEYDOWN(DIK_RETURN))
  494.             {
  495.                 // Copy the name
  496.                 cnt = 0;
  497.                 cTmp = netmessagename[cnt];
  498.                 while(cTmp != 0 && cnt < 64)
  499.                 {
  500.                     netmessage[cnt] = cTmp;
  501.                     cnt++;
  502.                     cTmp = netmessagename[cnt];
  503.                 }
  504.                 netmessage[cnt] = '>';  cnt++;
  505.                 netmessage[cnt] = ' ';  cnt++;
  506.                 netmessage[cnt] = '?';
  507.                 netmessage[cnt+1] = 0;
  508.                 netmessagewrite = cnt;
  509.                 netmessagewritemin = cnt;
  510.  
  511.                 netmessagemode = TRUE;
  512.                 netmessagedelay = 20;
  513.             }
  514.         }
  515.         else
  516.         {
  517.             netmessagedelay--;
  518.         }
  519.     }
  520. */
  521. }
  522.  
  523. //------------------------------------------------------------------------------
  524. void copy_file_to_all_players(char *source, char *dest)
  525. {
  526.     // ZZ> This function copies a file on the host to every remote computer.
  527.     //     Packets are sent in chunks of COPYSIZE bytes.  The max file size
  528.     //     that can be sent is 2 Megs ( TOTALSIZE ).
  529. /*PORT
  530.     FILE* fileread;
  531.     int packetsize, packetstart;
  532.     int filesize;
  533.     int fileisdir;
  534.     char cTmp;
  535.  
  536.  
  537.     if(networkon && hostactive)
  538.     {
  539.         fileisdir = GetFileAttributes(source);
  540.         fileisdir = ((fileisdir&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY);
  541.         if(fileisdir)
  542.         {
  543.             start_building_packet();
  544.             add_packet_us(TO_REMOTE_DIR);
  545.             add_packet_sz(dest);
  546.             send_packet_to_all_players_guaranteed();
  547.         }
  548.         else
  549.         {
  550.             fileread = fopen(source, "rb");
  551.             if(fileread)
  552.             {
  553.                 fseek(fileread, 0, SEEK_END);
  554.                 filesize = ftell(fileread);
  555.                 fseek(fileread, 0, SEEK_SET);
  556.                 if(filesize > 0 && filesize < TOTALSIZE)
  557.                 {
  558.                     packetsize = 0;
  559.                     packetstart = 0;
  560.                     start_building_packet();
  561.                     numfilesent++;
  562.                     add_packet_us(TO_REMOTE_FILE);
  563.                     add_packet_sz(dest);
  564.                     add_packet_ui(filesize);
  565.                     add_packet_ui(packetstart);
  566.                     while(packetstart < filesize)
  567.                     {
  568.                         fscanf(fileread, "%c", &cTmp);
  569.                         add_packet_uc(cTmp);
  570.                         packetsize++;
  571.                         packetstart++;
  572.                         if(packetsize >= COPYSIZE)
  573.                         {
  574.                             // Send off the packet
  575.                             send_packet_to_all_players_guaranteed();
  576.  
  577.  
  578.                             // Start on the next 4K
  579.                             packetsize = 0;
  580.                             start_building_packet();
  581.                             add_packet_us(TO_REMOTE_FILE);
  582.                             add_packet_sz(dest);
  583.                             add_packet_ui(filesize);
  584.                             add_packet_ui(packetstart);
  585.                         }
  586.                     }
  587.                     // Send off the packet
  588.                     send_packet_to_all_players_guaranteed();
  589.                 }
  590.                 fclose(fileread);
  591.             }
  592.         }
  593.     }
  594. */
  595. }
  596.  
  597. //------------------------------------------------------------------------------
  598. void copy_file_to_host(char *source, char *dest)
  599. {
  600.     // ZZ> This function copies a file on the remote to the host computer.
  601.     //     Packets are sent in chunks of COPYSIZE bytes.  The max file size
  602.     //     that can be sent is 2 Megs ( TOTALSIZE ).
  603. /*PORT
  604.     FILE* fileread;
  605.     int packetsize, packetstart;
  606.     int filesize;
  607.     int fileisdir;
  608.     char cTmp;
  609.  
  610.  
  611.     if(hostactive)
  612.     {
  613.         // Simulate a network transfer
  614.         fileisdir = GetFileAttributes(source);
  615.         fileisdir = ((fileisdir&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY);
  616.         if(fileisdir)
  617.         {
  618.             make_directory(dest);
  619.         }
  620.         else
  621.         {
  622.             copy_file(source, dest);
  623.         }
  624.     }
  625.     else
  626.     {
  627.         fileisdir = GetFileAttributes(source);
  628.         fileisdir = ((fileisdir&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY);
  629.         if(fileisdir)
  630.         {
  631.             start_building_packet();
  632.             add_packet_us(TO_HOST_DIR);
  633.             add_packet_sz(dest);
  634.             send_packet_to_host_guaranteed();
  635.         }
  636.         else
  637.         {
  638.             fileread = fopen(source, "rb");
  639.             if(fileread)
  640.             {
  641.                 fseek(fileread, 0, SEEK_END);
  642.                 filesize = ftell(fileread);
  643.                 fseek(fileread, 0, SEEK_SET);
  644.                 if(filesize > 0 && filesize < TOTALSIZE)
  645.                 {
  646.                     numfilesent++;
  647.                     packetsize = 0;
  648.                     packetstart = 0;
  649.                     start_building_packet();
  650.                     add_packet_us(TO_HOST_FILE);
  651.                     add_packet_sz(dest);
  652.                     add_packet_ui(filesize);
  653.                     add_packet_ui(packetstart);
  654.                     while(packetstart < filesize)
  655.                     {
  656.                         fscanf(fileread, "%c", &cTmp);
  657.                         add_packet_uc(cTmp);
  658.                         packetsize++;
  659.                         packetstart++;
  660.                         if(packetsize >= COPYSIZE)
  661.                         {
  662.                             // Send off the packet
  663.                             send_packet_to_host_guaranteed();
  664.  
  665.  
  666.                             // Start on the next 4K
  667.                             packetsize = 0;
  668.                             start_building_packet();
  669.                             add_packet_us(TO_HOST_FILE);
  670.                             add_packet_sz(dest);
  671.                             add_packet_ui(filesize);
  672.                             add_packet_ui(packetstart);
  673.                         }
  674.                     }
  675.                     // Send off the packet
  676.                     send_packet_to_host_guaranteed();
  677.                 }
  678.                 fclose(fileread);
  679.             }
  680.         }
  681.     }
  682. */
  683. }
  684.  
  685. //--------------------------------------------------------------------------------------------
  686. void copy_directory_to_host(char *dirname, char *todirname)
  687. {
  688.     // ZZ> This function copies all files in a directory
  689. /*PORT
  690.     char searchname[128];
  691.     char fromname[128];
  692.     char toname[128];
  693.     WIN32_FIND_DATA wfdData;
  694.     HANDLE hFind;
  695.     BOOL keeplooking;
  696.  
  697.  
  698.     // Search for all files
  699.     sprintf(searchname, "%s\\*.*", dirname);
  700.     hFind = FindFirstFile(searchname, &wfdData);
  701.     keeplooking = 1;
  702.     if(hFind != INVALID_HANDLE_VALUE)
  703.     {
  704.         // Make the new directory
  705.         copy_file_to_host(dirname, todirname);
  706.         // Copy each file
  707.         while(keeplooking)
  708.         {
  709.             sprintf(fromname, "%s\\%s", dirname, wfdData.cFileName);
  710.             sprintf(toname, "%s\\%s", todirname, wfdData.cFileName);
  711.             copy_file_to_host(fromname, toname);
  712.             keeplooking = FindNextFile(hFind, &wfdData);
  713.         }
  714.     }
  715. */
  716. }
  717.  
  718. //--------------------------------------------------------------------------------------------
  719. void copy_directory_to_all_players(char *dirname, char *todirname)
  720. {
  721.     // ZZ> This function copies all files in a directory
  722. /*PORT
  723.     char searchname[128];
  724.     char fromname[128];
  725.     char toname[128];
  726.     WIN32_FIND_DATA wfdData;
  727.     HANDLE hFind;
  728.     BOOL keeplooking;
  729.  
  730.  
  731.     // Search for all files
  732.     sprintf(searchname, "%s\\*.*", dirname);
  733.     hFind = FindFirstFile(searchname, &wfdData);
  734.     keeplooking = 1;
  735.     if(hFind != INVALID_HANDLE_VALUE)
  736.     {
  737.         // Make the new directory
  738.         copy_file_to_all_players(dirname, todirname);
  739.         // Copy each file
  740.         while(keeplooking)
  741.         {
  742.             sprintf(fromname, "%s\\%s", dirname, wfdData.cFileName);
  743.             sprintf(toname, "%s\\%s", todirname, wfdData.cFileName);
  744.             copy_file_to_all_players(fromname, toname);
  745.             keeplooking = FindNextFile(hFind, &wfdData);
  746.         }
  747.     }
  748. */
  749. }
  750.  
  751. //--------------------------------------------------------------------------------------------
  752. void say_hello()
  753. {
  754.     // ZZ> This function lets everyone know we're here
  755.     waitingforplayers = TRUE;
  756.     playersloaded = 0;
  757.     if(networkon)
  758.     {
  759.         if(hostactive)
  760.         {
  761.             playersloaded++;
  762.             if(playersloaded >= numplayer)
  763.             {
  764.                 waitingforplayers = FALSE;
  765.             }
  766.         }
  767.         else
  768.         {
  769. /*PORT
  770.             start_building_packet();
  771.             add_packet_us(TO_HOST_IM_LOADED);
  772.             send_packet_to_host_guaranteed(); */
  773.         }
  774.     }
  775.     else
  776.     {
  777.         waitingforplayers = FALSE;
  778.     }
  779. }
  780.  
  781. //--------------------------------------------------------------------------------------------
  782. void talk_to_host()
  783. {
  784.     // ZZ> This function sends the latch packets to the host machine
  785. /*PORT    int player;
  786.  
  787.     // Let the players respawn
  788.     if(KEYDOWN(DIK_SPACE)
  789.        && ( alllocalpladead || respawnanytime )
  790.        && respawnvalid
  791.        && rtscontrol == FALSE
  792.        && netmessagemode == FALSE)
  793.     {
  794.         player = 0;
  795.         while(player < MAXPLAYER)
  796.         {
  797.             if(plavalid[player] && pladevice[player] != INPUTNONE)
  798.             {
  799.                 plalatchbutton[player]|=LATCHBUTTONRESPAWN;  // Press the respawn button...
  800.             }
  801.             player++;
  802.         }
  803.     }
  804.  
  805.   // Start talkin'
  806.     if(networkon && hostactive==FALSE && rtscontrol == FALSE)
  807.     {
  808.         start_building_packet();
  809.         add_packet_us(TO_HOST_LATCH);                           // The message header
  810.         player = 0;
  811.         while(player < MAXPLAYER)
  812.         {
  813.             // Find the local players
  814.             if(plavalid[player] && pladevice[player] != INPUTNONE)
  815.             {
  816.                 add_packet_uc(player);                          // The player index
  817.                 add_packet_uc(plalatchbutton[player]);          // Player button states
  818.                 add_packet_ss(plalatchx[player]*SHORTLATCH);    // Player motion
  819.                 add_packet_ss(plalatchy[player]*SHORTLATCH);    // Player motion
  820.             }
  821.             player++;
  822.         }
  823.  
  824.         // Send it to the host        
  825.         send_packet_to_host();
  826.     }
  827. */
  828. }
  829.  
  830.  
  831. //--------------------------------------------------------------------------------------------
  832. void talk_to_remotes()
  833. {
  834.     // ZZ> This function sends the character data to all the remote machines
  835.     int player, time;
  836.     signed short sTmp;
  837.  
  838.     if(wldframe > STARTTALK)
  839.     {
  840.         if(hostactive && rtscontrol == FALSE)
  841.         {
  842.             time = wldframe+lag;
  843.             
  844.             if (networkon)
  845.             {
  846.                 // Send a message to all players
  847.                 start_building_packet();
  848.                 add_packet_us(TO_REMOTE_LATCH);                         // The message header
  849.                 add_packet_ui(time);                                    // The stamp
  850.  
  851.  
  852.                 // Send all player latches...
  853.                 player = 0;
  854.                 while(player < MAXPLAYER)
  855.                 {
  856.                     if(plavalid[player])
  857.                     {
  858.                         add_packet_uc(player);                          // The player index
  859.                         add_packet_uc(plalatchbutton[player]);          // Player button states
  860.                         add_packet_ss(plalatchx[player]*SHORTLATCH);    // Player motion
  861.                         add_packet_ss(plalatchy[player]*SHORTLATCH);    // Player motion
  862.                     }
  863.                     player++;
  864.                 }
  865.  
  866.  
  867.                 // Send the packet
  868.                 send_packet_to_all_players();
  869.             }
  870.             else
  871.             {
  872.                 time = wldframe+1;
  873.             }
  874.  
  875.  
  876.             // Now pretend the host got the packet...
  877.             time = time&LAGAND;
  878.             player = 0;
  879.             while(player < MAXPLAYER)
  880.             {
  881.                 if(plavalid[player])
  882.                 {
  883.                     platimelatchbutton[player][time] = plalatchbutton[player];
  884.                     sTmp = plalatchx[player]*SHORTLATCH;
  885.                     platimelatchx[player][time] = sTmp/SHORTLATCH;
  886.                     sTmp = plalatchy[player]*SHORTLATCH;
  887.                     platimelatchy[player][time] = sTmp/SHORTLATCH;
  888.                 }
  889.                 player++;
  890.             }
  891.             numplatimes++;
  892.         }
  893.     }
  894. }
  895.  
  896. //--------------------------------------------------------------------------------------------
  897. void listen_for_packets()
  898. {
  899. /*PORT
  900.     // ZZ> This function reads any new messages and sets the player latch and matrix needed
  901.     //     lists...
  902.     DWORD readnumber;
  903.     DPID  sender, holder;
  904.     HRESULT hr;
  905.     FILE* filewrite;
  906.     int filesize;
  907.     int newfilesize;
  908.     int fileposition;
  909.     char filename[256];
  910.     unsigned short header, player;
  911.     unsigned char  who;
  912.     unsigned int stamp;
  913.     unsigned short whichorder;
  914.     unsigned int what, when;
  915.     int cnt, time;
  916.  
  917.  
  918.     if(networkon)
  919.     {
  920.         // Listen for new messages
  921.         hr = lpDirectPlay3A->GetMessageCount(selfid, &readnumber);
  922.         if(hr == DP_OK)
  923.         {
  924.             while(readnumber > 0)
  925.             {
  926.                 packetsize = MAXSENDSIZE;
  927.                 hr = lpDirectPlay3A->Receive(&sender, &holder, DPRECEIVE_ALL, packetbuffer, &packetsize);
  928.                 if(hr == DP_OK)
  929.                 {
  930.                     // Read the message
  931.                     start_reading_packet();
  932.                     header = read_packet_us();
  933.                     if(header == TO_ANY_TEXT)
  934.                     {
  935.                         debug_message((char *) (&packetbuffer[packethead]));
  936.                     }
  937.                     if(header == TO_HOST_MODULEOK && hostactive)
  938.                     {
  939.                         playersready++;
  940.                         if(playersready >= numplayer)
  941.                         {
  942.                             readytostart = TRUE;
  943.                         }
  944.                     }
  945.                     if(header == TO_HOST_LATCH && hostactive)
  946.                     {
  947.                         // Read latches for each player sent
  948.                         while(still_reading_packet())
  949.                         {
  950.                             player = read_packet_uc();
  951.                             plalatchbutton[player] = read_packet_uc();
  952.                             plalatchx[player] = read_packet_ss()/SHORTLATCH;
  953.                             plalatchy[player] = read_packet_ss()/SHORTLATCH;
  954.                         }
  955.                     }
  956.                     if(header == TO_HOST_IM_LOADED && hostactive)
  957.                     {
  958.                         playersloaded++;
  959.                         if(playersloaded >= numplayer)
  960.                         {
  961.                             // Let the games begin...
  962.                             waitingforplayers = FALSE;
  963.                             start_building_packet();
  964.                             add_packet_us(TO_REMOTE_START);
  965.                             send_packet_to_all_players_guaranteed();
  966.                         }
  967.                     }
  968.                     if(header == TO_HOST_RTS && hostactive)
  969.                     {
  970.                         whichorder = get_empty_order();
  971.                         if(whichorder < MAXORDER)
  972.                         {
  973.                             // Add the order on the host machine
  974.                             cnt = 0;
  975.                             while(cnt < MAXSELECT)
  976.                             {
  977.                                 who = read_packet_uc();
  978.                                 orderwho[whichorder][cnt] = who;
  979.                                 cnt++;
  980.                             }
  981.                             what = read_packet_ui();
  982.                             when = wldframe + orderlag;
  983.                             orderwhat[whichorder] = what;
  984.                             orderwhen[whichorder] = when;
  985.  
  986.  
  987.                             // Send the order off to everyone else
  988.                             start_building_packet();
  989.                             add_packet_us(TO_REMOTE_RTS);
  990.                             cnt = 0;
  991.                             while(cnt < MAXSELECT)
  992.                             {
  993.                                 add_packet_uc(orderwho[whichorder][cnt]);
  994.                                 cnt++;
  995.                             }
  996.                             add_packet_ui(what);
  997.                             add_packet_ui(when);
  998.                             send_packet_to_all_players_guaranteed();
  999.                         }
  1000.                     }
  1001.                     if(header == TO_HOST_FILE)
  1002.                     {
  1003.                         read_packet_sz(filename);
  1004.                         newfilesize = read_packet_ui();
  1005.  
  1006.  
  1007.                         // Change the size of the file if need be
  1008.                         filewrite = fopen(filename, "rb");
  1009.                         if(filewrite)
  1010.                         {
  1011.                             fseek(filewrite, 0, SEEK_END);
  1012.                             filesize = ftell(filewrite);
  1013.                             fseek(filewrite, 0, SEEK_SET);
  1014.                             fclose(filewrite);
  1015.                             if(filesize != newfilesize)
  1016.                             {
  1017.                                 // Destroy the old file
  1018.                                 numfile++;
  1019.                                 filewrite = fopen(filename, "wb");
  1020.                                 if(filewrite)
  1021.                                 {
  1022.                                     filesize = 0;
  1023.                                     while(filesize < newfilesize)
  1024.                                     {
  1025.                                         fprintf(filewrite, "%c", 0);
  1026.                                         filesize++;
  1027.                                     }
  1028.                                     fclose(filewrite);
  1029.                                 }
  1030.                             }
  1031.                         }
  1032.                         else
  1033.                         {
  1034.                             // File must be created
  1035.                             numfile++;
  1036.                             filewrite = fopen(filename, "wb");
  1037.                             if(filewrite)
  1038.                             {
  1039.                                 filesize = 0;
  1040.                                 while(filesize < newfilesize)
  1041.                                 {
  1042.                                     fprintf(filewrite, "%c", 0);
  1043.                                     filesize++;
  1044.                                 }
  1045.                                 fclose(filewrite);
  1046.                             }
  1047.                         }
  1048.  
  1049.  
  1050.  
  1051.  
  1052.                         // Go to the position in the file and copy data
  1053.                         fileposition = read_packet_ui();
  1054.                         filewrite = fopen(filename, "r+b");
  1055.                         if(filewrite)
  1056.                         {
  1057.                             if(fseek(filewrite, fileposition, SEEK_SET) == 0)
  1058.                             {
  1059.                                 while(still_reading_packet())
  1060.                                 {
  1061.                                     who = read_packet_uc();
  1062.                                     fprintf(filewrite, "%c", who);
  1063.                                 }
  1064.                             }
  1065.                             fclose(filewrite);
  1066.                         }
  1067.                     }
  1068.                     if(header == TO_HOST_DIR && hostactive)
  1069.                     {
  1070.                         read_packet_sz(filename);
  1071.                         make_directory(filename);
  1072.                     }
  1073.                     if(header == TO_HOST_FILESENT && hostactive)
  1074.                     {
  1075.                         cnt = read_packet_ui();
  1076.                         numfileexpected += cnt;
  1077.                         numplayerrespond++;
  1078.                     }
  1079.                     if(header == TO_REMOTE_FILESENT && hostactive==FALSE)
  1080.                     {
  1081.                         cnt = read_packet_ui();
  1082.                         numfileexpected += cnt;
  1083.                         numplayerrespond++;
  1084.                     }
  1085.                     if(header == TO_REMOTE_MODULE && hostactive==FALSE && readytostart==FALSE)
  1086.                     {
  1087.                         seed = read_packet_ui();
  1088.                         rtslocalteam = read_packet_uc();
  1089.                         localmachine = rtslocalteam;
  1090.                         sprintf(pickedmodule, "%s", (char *) (&packetbuffer[packethead]));
  1091.  
  1092.  
  1093.                         // Check to see if the module exists
  1094.                         pickedindex = find_module(pickedmodule);
  1095.                         if(pickedindex == -1)
  1096.                         {
  1097.                             // The module doesn't exist locally
  1098.                             // !!!BAD!!!  Copy the data from the host
  1099.                             pickedindex = 0;
  1100.                         }
  1101.  
  1102.                         // Make ourselves ready
  1103.                         readytostart = TRUE;
  1104.  
  1105.                         // Tell the host we're ready
  1106.                         start_building_packet();
  1107.                         add_packet_us(TO_HOST_MODULEOK);
  1108.                         send_packet_to_host_guaranteed();
  1109.                     }
  1110.                     if(header == TO_REMOTE_START && hostactive==FALSE)
  1111.                     {
  1112.                         waitingforplayers = FALSE;
  1113.                     }
  1114.                     if(header == TO_REMOTE_RTS && hostactive==FALSE)
  1115.                     {
  1116.                         whichorder = get_empty_order();
  1117.                         if(whichorder < MAXORDER)
  1118.                         {
  1119.                             // Add the order on the remote machine
  1120.                             cnt = 0;
  1121.                             while(cnt < MAXSELECT)
  1122.                             {
  1123.                                 who = read_packet_uc();
  1124.                                 orderwho[whichorder][cnt] = who;
  1125.                                 cnt++;
  1126.                             }
  1127.                             what = read_packet_ui();
  1128.                             when = read_packet_ui();
  1129.                             orderwhat[whichorder] = what;
  1130.                             orderwhen[whichorder] = when;
  1131.                         }
  1132.                     }
  1133.                     if(header == TO_REMOTE_FILE && hostactive==FALSE)
  1134.                     {
  1135.                         read_packet_sz(filename);
  1136.                         newfilesize = read_packet_ui();
  1137.  
  1138.  
  1139.                         // Change the size of the file if need be
  1140.                         filewrite = fopen(filename, "rb");
  1141.                         if(filewrite)
  1142.                         {
  1143.                             fseek(filewrite, 0, SEEK_END);
  1144.                             filesize = ftell(filewrite);
  1145.                             fseek(filewrite, 0, SEEK_SET);
  1146.                             fclose(filewrite);
  1147.                             if(filesize != newfilesize)
  1148.                             {
  1149.                                 // Destroy the old file
  1150.                                 numfile++;
  1151.                                 filewrite = fopen(filename, "wb");
  1152.                                 if(filewrite)
  1153.                                 {
  1154.                                     filesize = 0;
  1155.                                     while(filesize < newfilesize)
  1156.                                     {
  1157.                                         fprintf(filewrite, "%c", 0);
  1158.                                         filesize++;
  1159.                                     }
  1160.                                     fclose(filewrite);
  1161.                                 }
  1162.                             }
  1163.                         }
  1164.                         else
  1165.                         {
  1166.                             // File must be created
  1167.                             numfile++;
  1168.                             filewrite = fopen(filename, "wb");
  1169.                             if(filewrite)
  1170.                             {
  1171.                                 filesize = 0;
  1172.                                 while(filesize < newfilesize)
  1173.                                 {
  1174.                                     fprintf(filewrite, "%c", 0);
  1175.                                     filesize++;
  1176.                                 }
  1177.                                 fclose(filewrite);
  1178.                             }
  1179.                         }
  1180.  
  1181.  
  1182.  
  1183.  
  1184.                         // Go to the position in the file and copy data
  1185.                         fileposition = read_packet_ui();
  1186.                         filewrite = fopen(filename, "r+b");
  1187.                         if(filewrite)
  1188.                         {
  1189.                             if(fseek(filewrite, fileposition, SEEK_SET) == 0)
  1190.                             {
  1191.                                 while(still_reading_packet())
  1192.                                 {
  1193.                                     who = read_packet_uc();
  1194.                                     fprintf(filewrite, "%c", who);
  1195.                                 }
  1196.                             }
  1197.                             fclose(filewrite);
  1198.                         }
  1199.                     }
  1200.                     if(header == TO_REMOTE_DIR && hostactive==FALSE)
  1201.                     {
  1202.                         read_packet_sz(filename);
  1203.                         make_directory(filename);
  1204.                     }
  1205.                     if(header == TO_REMOTE_LATCH && hostactive==FALSE)
  1206.                     {
  1207.                         stamp = read_packet_ui();
  1208.                         time = stamp&LAGAND;
  1209.                         if(nexttimestamp == -1)
  1210.                         {
  1211.                             nexttimestamp = stamp;
  1212.                         }
  1213.                         if(stamp < nexttimestamp)
  1214.                         {
  1215.                             debug_message("OUT OF ORDER PACKET");
  1216.                             outofsync = TRUE;
  1217.                         }
  1218.                         if(stamp <= wldframe)
  1219.                         {
  1220.                             debug_message("LATE PACKET");
  1221.                             outofsync = TRUE;
  1222.                         }
  1223.                         if(stamp > nexttimestamp)
  1224.                         {
  1225.                             debug_message("MISSED PACKET");
  1226.                             nexttimestamp = stamp;  // Still use it
  1227.                             outofsync = TRUE;
  1228.                         }
  1229.                         if(stamp == nexttimestamp)
  1230.                         {
  1231.                             // Remember that we got it
  1232.                             numplatimes++;
  1233.  
  1234.  
  1235.                             // Read latches for each player sent
  1236.                             while(still_reading_packet())
  1237.                             {
  1238.                                 player = read_packet_uc();
  1239.                                 platimelatchbutton[player][time] = read_packet_uc();
  1240.                                 platimelatchx[player][time] = read_packet_ss()/SHORTLATCH;
  1241.                                 platimelatchy[player][time] = read_packet_ss()/SHORTLATCH;
  1242.                             }
  1243.                             nexttimestamp = stamp+1;
  1244.                         }
  1245.                     }
  1246.                 }
  1247.                 readnumber--;
  1248.             }
  1249.         }
  1250.     }
  1251. */
  1252. }
  1253.  
  1254. //--------------------------------------------------------------------------------------------
  1255. void unbuffer_player_latches()
  1256. {
  1257.     // ZZ> This function sets character latches based on player input to the host
  1258.     int cnt, time, character;
  1259.  
  1260.  
  1261.     // Copy the latches
  1262.     time = wldframe&LAGAND;
  1263.     cnt = 0;
  1264.     while(cnt < MAXPLAYER)
  1265.     {
  1266.         if(plavalid[cnt] && !rtscontrol)
  1267.         {
  1268.             character = plaindex[cnt];
  1269.  
  1270.             chrlatchx[character] = platimelatchx[cnt][time];
  1271.             chrlatchy[character] = platimelatchy[cnt][time];
  1272.             chrlatchbutton[character] = platimelatchbutton[cnt][time];
  1273.             
  1274.             // Let players respawn
  1275.             if((chrlatchbutton[character] & LATCHBUTTONRESPAWN) && respawnvalid)
  1276.             {
  1277.                 if(chralive[character] == FALSE)
  1278.                 {
  1279.                     respawn_character(character);
  1280.                     teamleader[chrteam[character]] = character;
  1281.                     chralert[character] |= ALERTIFCLEANEDUP;
  1282.                     // Cost some experience for doing this...  Never lose a level
  1283.                     chrexperience[character] = chrexperience[character] * EXPKEEP;
  1284.                 }
  1285.                 chrlatchbutton[character] &= 127;
  1286.             }
  1287.         }
  1288.         cnt++;
  1289.     }
  1290.     numplatimes--;
  1291. }
  1292.  
  1293. //--------------------------------------------------------------------------------------------
  1294. void chug_orders()
  1295. {
  1296.     // ZZ> This function takes care of lag in orders, issuing at the proper wldframe
  1297.     int cnt, character, tnc;
  1298.  
  1299.     cnt = 0;
  1300.     while(cnt < MAXORDER)
  1301.     {
  1302.         if(ordervalid[cnt] && orderwhen[cnt] <= wldframe)
  1303.         {
  1304.             if(orderwhen[cnt] < wldframe)
  1305.             {
  1306.                 debug_message("MISSED AN ORDER");
  1307.             }
  1308.             tnc = 0;
  1309.             while(tnc < MAXSELECT)
  1310.             {
  1311.                 character = orderwho[cnt][tnc];
  1312.                 if(character < MAXCHR)
  1313.                 {
  1314.                     chrorder[character] = orderwhat[cnt];
  1315.                     chrcounter[character] = tnc;
  1316.                     chralert[character]|=ALERTIFORDERED;
  1317.                 }
  1318.                 tnc++;
  1319.             }
  1320.             ordervalid[cnt] = FALSE;
  1321.         }
  1322.         cnt++;
  1323.     }
  1324. }
  1325.  
  1326. //--------------------------------------------------------------------------------------------
  1327. void send_rts_order(int x, int y, unsigned char order, unsigned char target)
  1328. {
  1329.     // ZZ> This function asks the host to order the selected characters
  1330.     unsigned int what, when, whichorder, cnt;
  1331.  
  1332.     if(numrtsselect > 0)
  1333.     {
  1334.         x = (x >> 6) & 1023;
  1335.         y = (y >> 6) & 1023;
  1336.         what = (target << 24) | (x << 14) | (y << 4) | (order&15); 
  1337.         if(hostactive)
  1338.         {
  1339.             when = wldframe + orderlag;
  1340.             whichorder = get_empty_order();
  1341.             if(whichorder != MAXORDER)
  1342.             {
  1343.                 // Add a new order on own machine
  1344.                 orderwhen[whichorder] = when;
  1345.                 orderwhat[whichorder] = what;
  1346.                 cnt = 0;
  1347.                 while(cnt < numrtsselect)
  1348.                 {
  1349.                     orderwho[whichorder][cnt] = rtsselect[cnt];
  1350.                     cnt++;
  1351.                 }
  1352.                 while(cnt < MAXSELECT)
  1353.                 {
  1354.                     orderwho[whichorder][cnt] = MAXCHR;
  1355.                     cnt++;
  1356.                 }
  1357.  
  1358.  
  1359.                 // Send the order off to everyone else
  1360.                 if(networkon)
  1361.                 {
  1362.                     start_building_packet();
  1363.                     add_packet_us(TO_REMOTE_RTS);
  1364.                     cnt = 0;
  1365.                     while(cnt < MAXSELECT)
  1366.                     {
  1367.                         add_packet_uc(orderwho[whichorder][cnt]);
  1368.                         cnt++;
  1369.                     }
  1370.                     add_packet_ui(what);
  1371.                     add_packet_ui(when);
  1372.                     send_packet_to_all_players_guaranteed();
  1373.                 }
  1374.             }
  1375.         }
  1376.         else
  1377.         {
  1378.             // Send the order off to the host
  1379.             start_building_packet();
  1380.             add_packet_us(TO_HOST_RTS);
  1381.             cnt = 0;
  1382.             while(cnt < numrtsselect)
  1383.             {
  1384.                 add_packet_uc(rtsselect[cnt]);
  1385.                 cnt++;
  1386.             }
  1387.             while(cnt < MAXSELECT)
  1388.             {
  1389.                 add_packet_uc(MAXCHR);
  1390.                 cnt++;
  1391.             }
  1392.             add_packet_ui(what);
  1393.             send_packet_to_host_guaranteed();
  1394.         }
  1395.     }
  1396. }
  1397.  
  1398. //--------------------------------------------------------------------------------------------
  1399. void setup_network()
  1400. {
  1401.     // ZZ> This starts up the network and logs whatever goes on
  1402. /*PORT
  1403.     HRESULT             hr;
  1404.  
  1405.  
  1406.     serviceon = FALSE;
  1407.     numsession = 0;
  1408.     numservice = 0;
  1409.     if(globalnetworkerr)  fprintf(globalnetworkerr, "This file helps debug networking...\n");
  1410.     if(networkon)
  1411.     {
  1412.         // Create the direct play interface
  1413.         hr = CoCreateInstance( CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER, 
  1414.                                IID_IDirectPlay3A, (LPVOID*)&lpDirectPlay3A);
  1415.         serviceon = TRUE;
  1416.         if(hr != DP_OK) { networkon = 0; serviceon = 0; }
  1417.         if(globalnetworkerr)
  1418.         {
  1419.             if(networkon)  fprintf(globalnetworkerr, "  Interface OK\n");
  1420.             else           fprintf(globalnetworkerr, "  ERROR:  Interface broken\n");
  1421.         }
  1422.     }
  1423.     else
  1424.     {
  1425.         // Network wasn't turned on
  1426.         if(globalnetworkerr)  fprintf(globalnetworkerr, "  ERROR:  Network not turned on\n");
  1427.     }
  1428.     if(networkon)
  1429.     {
  1430.         // Get the different service providers
  1431.         if(globalnetworkerr)  fprintf(globalnetworkerr, "  Looking for services...\n");
  1432.         lpDirectPlay3A->EnumConnections(NULL, ConnectionsCallback, hGlobalWindow, 0);
  1433.         if(numservice == 0 && globalnetworkerr)  fprintf(globalnetworkerr, "    ERROR:  None found...\n");
  1434.     }
  1435. */
  1436. }
  1437.  
  1438. //--------------------------------------------------------------------------------------------
  1439. void find_open_sessions()
  1440. {
  1441. /*PORT
  1442.     // ZZ> This function finds some open games to join
  1443.     DPSESSIONDESC2      sessionDesc;
  1444.     HRESULT             hr;
  1445.  
  1446.     if(networkon)
  1447.     {
  1448.         numsession = 0;
  1449.         if(globalnetworkerr)  fprintf(globalnetworkerr, "  Looking for open games...\n");
  1450.         ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  1451.         sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  1452.         sessionDesc.guidApplication = NETWORKID;
  1453.         hr = lpDirectPlay3A->EnumSessions(&sessionDesc, 0, SessionsCallback, hGlobalWindow, DPENUMSESSIONS_AVAILABLE);
  1454.         if(globalnetworkerr)  fprintf(globalnetworkerr, "    %d sessions found\n", numsession);
  1455.     }
  1456. */
  1457. }
  1458.  
  1459. //--------------------------------------------------------------------------------------------
  1460. void find_all_players()
  1461. {
  1462.     // ZZ> This function finds all the players in the game
  1463. /*PORT
  1464.     HRESULT             hr;
  1465.  
  1466.     if(networkon)
  1467.     {
  1468.         numplayer = 0;
  1469. //        if(globalnetworkerr)  fprintf(globalnetworkerr, "  Looking for players...\n");
  1470.         hr = lpDirectPlay3A->EnumPlayers(NULL, PlayersCallback, hGlobalWindow, DPENUMPLAYERS_ALL);
  1471. //        if(globalnetworkerr)  fprintf(globalnetworkerr, "    %d players found\n", numplayer);
  1472.     }
  1473. */
  1474. }
  1475.  
  1476. //--------------------------------------------------------------------------------------------
  1477. int create_player(int host)
  1478. {
  1479.     // ZZ> This function tries creating a player
  1480. /*PORT
  1481.     DPNAME              dpName;
  1482.     HRESULT             hr;
  1483.  
  1484.     if(networkon)
  1485.     {
  1486.         ZeroMemory(&dpName, sizeof(DPNAME));
  1487.         dpName.dwSize = sizeof(DPNAME);
  1488.         dpName.lpszShortNameA = nethostname;
  1489.         dpName.lpszLongNameA = NULL;
  1490.         if(host)
  1491.         {
  1492. //            hr = lpDirectPlay3A->CreatePlayer(&selfid, &dpName, lpDPInfo->hPlayerEvent, NULL, 0, DPPLAYER_SERVERPLAYER);
  1493.             hr = lpDirectPlay3A->CreatePlayer(&selfid, &dpName, NULL, NULL, 0, DPPLAYER_SERVERPLAYER);
  1494.         }
  1495.         else
  1496.         {
  1497. //            hr = lpDirectPlay3A->CreatePlayer(&selfid, &dpName, lpDPInfo->hPlayerEvent, NULL, 0, 0);
  1498.             hr = lpDirectPlay3A->CreatePlayer(&selfid, &dpName, NULL, NULL, 0, 0);
  1499.         }
  1500.         if(hr == DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  Player created\n");
  1501.         if(hr != DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  ERROR:  Player not created\n");
  1502.         if(hr == DP_OK)  return TRUE;
  1503.     }
  1504.     return FALSE;
  1505. */
  1506. return FALSE;
  1507. }
  1508.  
  1509. //--------------------------------------------------------------------------------------------
  1510. int join_session(int session)
  1511. {
  1512.     // ZZ> This function tries to join one of the sessions we found
  1513. /*PORT
  1514.     DPSESSIONDESC2      sessionDesc;
  1515.     HRESULT             hr;
  1516.  
  1517.     if(networkon)
  1518.     {
  1519.         if(session < numsession)
  1520.         {
  1521.             // Try joining the game
  1522.             ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  1523.             sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  1524.             sessionDesc.guidInstance = *netlpsessionguid[session];
  1525.  
  1526.             hr = lpDirectPlay3A->Open(&sessionDesc, DPOPEN_JOIN);
  1527.             if(hr == DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  Joined session %d\n", session);
  1528.             if(hr != DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  ERROR:  Could not join session %d\n", session);
  1529.             if(hr != DP_OK)  return FALSE;
  1530.  
  1531.  
  1532.             // Try creating a player
  1533.             return create_player(FALSE);
  1534.         }
  1535.         else
  1536.         {
  1537.             if(globalnetworkerr)  fprintf(globalnetworkerr, "  ERROR:  Session %d not available\n", session);
  1538.         }
  1539.     }
  1540.     return FALSE;
  1541. */
  1542. return FALSE;
  1543. }
  1544.  
  1545. //--------------------------------------------------------------------------------------------
  1546. void stop_players_from_joining()
  1547. {
  1548.     // ZZ> This function stops players from joining a game
  1549. /*PORT
  1550.     DPSESSIONDESC2      sessionDesc;
  1551.     HRESULT             hr;
  1552.  
  1553.     if(hostactive)
  1554.     {
  1555.         ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  1556.         sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  1557.         sessionDesc.dwFlags = DPSESSION_JOINDISABLED | DPSESSION_NOMESSAGEID | DPSESSION_NODATAMESSAGES | DPSESSION_CLIENTSERVER;
  1558.         sessionDesc.guidApplication = NETWORKID;
  1559.         sessionDesc.dwMaxPlayers = MAXNETPLAYER;
  1560.         sessionDesc.dwCurrentPlayers = numplayer;
  1561.         sessionDesc.lpszSessionNameA = nethostname;
  1562.         hr = lpDirectPlay3A->SetSessionDesc(&sessionDesc, 0);
  1563.         if(hr == DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  Stopped players from joining\n");
  1564.         if(hr != DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  ERROR:  Players can still join\n");
  1565.     }
  1566. */
  1567. }
  1568.  
  1569. //--------------------------------------------------------------------------------------------
  1570. int host_session()
  1571. {
  1572.     // ZZ> This function tries to host a new session
  1573. /*PORT
  1574.     DPSESSIONDESC2      sessionDesc;
  1575.     HRESULT             hr;
  1576.  
  1577.     if(networkon)
  1578.     {
  1579.         // Try to create a new session
  1580.         ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  1581.         sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  1582.         sessionDesc.dwFlags = DPSESSION_NOMESSAGEID | DPSESSION_NODATAMESSAGES | DPSESSION_CLIENTSERVER;
  1583.         sessionDesc.guidApplication = NETWORKID;
  1584.         sessionDesc.dwMaxPlayers = MAXNETPLAYER;
  1585.         sessionDesc.dwCurrentPlayers = 1;
  1586.         sessionDesc.lpszSessionNameA = nethostname;
  1587.         hr = lpDirectPlay3A->Open(&sessionDesc, DPOPEN_CREATE);
  1588.         if(hr == DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  Hosted a new session\n");
  1589.         if(hr != DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  ERROR:  New session not created\n");
  1590.         if(hr != DP_OK)  return FALSE;
  1591.  
  1592.  
  1593.         // Try to create a host player
  1594.         return create_player(TRUE);
  1595.     }
  1596.     else
  1597.     {
  1598.         // Run in solo mode
  1599.         return TRUE;
  1600.     }
  1601. */
  1602. return FALSE;
  1603. }
  1604.  
  1605. //--------------------------------------------------------------------------------------------
  1606. void turn_on_service(int service)
  1607. {
  1608.     // This function turns on a network service ( IPX, TCP, serial, modem )
  1609. /*PORT
  1610.     HRESULT             hr;
  1611.     if(networkon && service < numservice)
  1612.     {
  1613.         hr = lpDirectPlay3A->InitializeConnection(netlpconnectionbuffer[service], 0);
  1614.         if(hr == DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  Service %d ( %s ) OK\n", service, netservicename[service]);
  1615.         if(hr != DP_OK && globalnetworkerr)  fprintf(globalnetworkerr, "  ERROR:  Service %d ( %s ) broken\n", service, netservicename[service]);
  1616.         if(hr != DP_OK) networkon = FALSE;
  1617.     }
  1618.     else
  1619.     {
  1620.         if(service == numservice)
  1621.         {
  1622.             if(globalnetworkerr)  fprintf(globalnetworkerr, "  Working in No Network mode\n");
  1623.         }
  1624.         else
  1625.         {
  1626.             if(globalnetworkerr)  fprintf(globalnetworkerr, "  ERROR:  Service %d not available\n", service);
  1627.         }
  1628.         networkon = FALSE;
  1629.     }
  1630. */
  1631. }
  1632.  
  1633.