]> icculus.org git repositories - btb/d2x.git/blob - main/network.c
Partial application of linux/alpha patch. Courtesy of Falk Hueffner <falk.hueffner...
[btb/d2x.git] / main / network.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #ifdef RCS
19 static char rcsid[] = "$Id: network.c,v 1.5 2001-10-19 00:31:51 bradleyb Exp $";
20 #endif
21
22 #ifdef NETWORK
23
24 #define PATCH12
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "pstypes.h"
31 #include "strutil.h"
32 #include "args.h"
33 #include "timer.h"
34 #include "mono.h"
35 #include "ipx.h"
36 #include "newmenu.h"
37 #include "key.h"
38 #include "gauges.h"
39 #include "object.h"
40 #include "error.h"
41 #include "laser.h"
42 #include "gamesave.h"
43 #include "gamemine.h"
44 #include "player.h"
45 #include "gameseq.h"
46 #include "fireball.h"
47 #include "network.h"
48 #include "game.h"
49 #include "multi.h"
50 #include "endlevel.h"
51 #include "palette.h"
52 #include "cntrlcen.h"
53 #include "powerup.h"
54 #include "menu.h"
55 #include "sounds.h"
56 #include "text.h"
57 #include "kmatrix.h"
58 #include "newdemo.h"
59 #include "multibot.h"
60 #include "wall.h"
61 #include "bm.h"
62 #include "effects.h"
63 #include "physics.h"
64 #include "switch.h"
65 #include "automap.h"
66 #include "byteswap.h"
67 #include "netmisc.h"
68 #include "kconfig.h"
69 #include "playsave.h"
70
71 #ifdef MACINTOSH
72 #include <Events.h>
73 #include <Errors.h>             // for appletalk networking errors
74 #include "appltalk.h"
75 #endif
76
77 void network_send_rejoin_sync(int player_num);
78 void network_update_netgame(void);
79 void network_read_endlevel_packet( ubyte *data );
80 void network_read_object_packet( ubyte *data );
81 void network_read_sync_packet( netgame_info * sp, int d1x );
82 void network_flush();
83 void network_listen();
84 void network_read_pdata_packet(frame_info *pd );
85
86 void network_check_for_old_version (char pnum);
87 void network_send_endlevel_short_sub(int from_player_num,int to_player);
88 int network_wait_for_playerinfo();
89 void network_process_pdata (char *data);
90 void network_read_endlevel_short_packet( ubyte *data );
91 void network_ping (ubyte flag,int pnum);
92 void network_handle_ping_return (ubyte pnum);
93 void network_process_names_return (char *data);
94 void network_send_player_names (sequence_packet *their);
95 int network_choose_connect ();
96 void network_more_game_options ();
97 void network_count_powerups_in_mine(void);
98 int network_wait_for_all_info (int choice);
99 void network_do_big_wait(int choice);
100 void network_send_extras ();
101 void network_read_pdata_short_packet(short_frame_info *pd );
102
103 void network_AdjustMaxDataSize ();
104
105 void ClipRank (signed char *rank);
106 void DoRefuseStuff (sequence_packet *their);
107 void SetAllAllowablesTo (int on);
108 int GetNewPlayerNumber (sequence_packet *their);
109
110 #define LHX(x)          ((x)*(MenuHires?2:1))
111 #define LHY(y)          ((y)*(MenuHires?2.4:1))
112
113 #define PID_LITE_INFO                           43
114 #define PID_SEND_ALL_GAMEINFO      44
115 #define PID_PLAYERSINFO                         45
116 #define PID_REQUEST                                     46
117 #define PID_SYNC                                                47
118 #define PID_PDATA                                               48
119 #define PID_ADDPLAYER                           49
120 #define PID_DUMP                                                51
121 #define PID_ENDLEVEL                                    52
122 #define PID_QUIT_JOINING                        54
123 #define PID_OBJECT_DATA                         55
124 #define PID_GAME_LIST                           56
125 #define PID_GAME_INFO                           57
126 #define PID_PING_SEND                           58
127 #define PID_PING_RETURN                         59
128 #define PID_GAME_UPDATE                         60
129 #define PID_ENDLEVEL_SHORT                      61
130 #define PID_NAKED_PDATA                         62
131 #define PID_GAME_PLAYERS                                                                63
132 #define PID_NAMES_RETURN                                                                64
133
134 #define NETGAME_ANARCHY                         0
135 #define NETGAME_TEAM_ANARCHY            1
136 #define NETGAME_ROBOT_ANARCHY           2
137 #define NETGAME_COOPERATIVE             3
138 #define NETGAME_CAPTURE_FLAG            4
139 #define NETGAME_HOARD                            5
140 #define NETGAME_TEAM_HOARD                                       6
141
142 #define NETSECURITY_OFF 0
143 #define NETSECURITY_WAIT_FOR_PLAYERS 1
144 #define NETSECURITY_WAIT_FOR_GAMEINFO 2
145 #define NETSECURITY_WAIT_FOR_SYNC 3
146
147 // MWA -- these structures are aligned -- please save me sanity and
148 // headaches by keeping alignment if these are changed!!!!  Contact
149 // me for info.
150
151 typedef struct endlevel_info {
152         ubyte                                   type;
153         ubyte                                   player_num;
154         byte                                    connected;
155         ubyte                                   seconds_left;
156         short                                   kill_matrix[MAX_PLAYERS][MAX_PLAYERS];
157         short                                   kills;
158         short                                   killed;
159 } endlevel_info;
160
161 typedef struct endlevel_info_short {
162         ubyte                                   type;
163         ubyte                                   player_num;
164         byte                                    connected;
165         ubyte                                   seconds_left;
166 } endlevel_info_short;
167
168 // WARNING!!! This is the top part of netgame_info...if that struct changes,
169 //      this struct much change as well.  ie...they are aligned and the join system will
170 // not work without it.
171
172 // MWA  if this structure changes -- please make appropriate changes to receive_netgame_info
173 // code for macintosh in netmisc.c
174
175 typedef struct lite_info {
176         ubyte                           type;
177         int                             Security;
178         char                            game_name[NETGAME_NAME_LEN+1];
179         char                            mission_title[MISSION_NAME_LEN+1];
180         char                            mission_name[9];
181         int                             levelnum;
182         ubyte                           gamemode;
183         ubyte                           RefusePlayers;
184         ubyte                           difficulty;
185         ubyte                           game_status;
186         ubyte                           numplayers;
187         ubyte                           max_numplayers;
188         ubyte                           numconnected;
189         ubyte                           game_flags;
190         ubyte                           protocol_version;
191         ubyte                           version_major;
192         ubyte                           version_minor;
193         ubyte                           team_vector;
194  } lite_info;
195
196 // IF YOU CHANGE THE SIZE OF ANY OF THE FOLLOWING STRUCTURES
197 // MAKE THE MACINTOSH DEFINES BE THE SAME SIZE AS THE PC OR
198 // I WILL HAVE TO HURT YOU VERY BADLY!!!  -- MWA
199
200 #ifdef MACINTOSH
201 #define NETGAME_INFO_SIZE               ( Network_game_type == IPX_GAME?355:sizeof(netgame_info) )
202 #define ALLNETPLAYERSINFO_SIZE  ( Network_game_type == IPX_GAME?317:sizeof(AllNetPlayers_info) )
203 #define LITE_INFO_SIZE                  ( Network_game_type == IPX_GAME?72:sizeof(lite_info) )
204 #define SEQUENCE_PACKET_SIZE    ( Network_game_type == IPX_GAME?34:sizeof(sequence_packet) )
205 #define FRAME_INFO_SIZE                 ( Network_game_type == IPX_GAME?541:sizeof(frame_info) )
206 #define IPX_SHORT_INFO_SIZE             ( 490 )
207 #else
208 #define NETGAME_INFO_SIZE               sizeof(netgame_info)
209 #define ALLNETPLAYERSINFO_SIZE  sizeof(AllNetPlayers_info)
210 #define LITE_INFO_SIZE                  sizeof(lite_info)
211 #define SEQUENCE_PACKET_SIZE    sizeof(sequence_packet)
212 #define FRAME_INFO_SIZE                 sizeof(frame_info)
213 #endif
214
215 #define MAX_ACTIVE_NETGAMES     12
216
217 netgame_info Active_games[MAX_ACTIVE_NETGAMES];
218 AllNetPlayers_info ActiveNetPlayers[MAX_ACTIVE_NETGAMES];
219 AllNetPlayers_info *TempPlayersInfo,TempPlayersBase;
220 int NamesInfoSecurity=-1;
221
222 // MWAnetgame_info *TempNetInfo; 
223 netgame_info TempNetInfo;
224
225 extern void multi_send_drop_marker (int player,vms_vector position,char messagenum,char text[]);
226 extern void multi_send_kill_goal_counts();
227 extern int newmenu_dotiny( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) );
228
229 void network_process_naked_pdata (char *,int);
230 extern void multi_send_robot_controls(char);
231
232 int num_active_games = 0;
233 int PacketsPerSec=10;
234 int MaxXDataSize=NET_XDATA_SIZE;
235
236 int       Netlife_kills=0, Netlife_killed=0;
237
238 int     Network_debug=0;
239 int     Network_active=0;
240
241 int     Network_status = 0;
242 int     Network_games_changed = 0;
243
244 int     Network_socket = 0;
245 int     Network_allow_socket_changes = 1;
246
247 int     Network_initial_pps = 10;
248 int     Network_initial_shortpackets = 0;
249
250 int     NetSecurityFlag=NETSECURITY_OFF;
251 int     NetSecurityNum=0;
252 int       Network_sending_extras=0;
253 int      VerifyPlayerJoined=-1;
254 int       Player_joining_extras=-1;  // This is so we know who to send 'latecomer' packets to.
255                                                                                           // We could just send updates to everyone but that kills the sender!   
256
257 // For rejoin object syncing
258
259 int     Network_rejoined = 0;       // Did WE rejoin this game?
260 int     Network_new_game = 0;            // Is this the first level of a new game?
261 int     Network_send_objects = 0;  // Are we in the process of sending objects to a player?
262 int     Network_send_objnum = -1;   // What object are we sending next?
263 int     Network_player_added = 0;   // Is this a new player or a returning player?
264 int     Network_send_object_mode = 0; // What type of objects are we sending, static or dynamic?
265 sequence_packet Network_player_rejoining; // Who is rejoining now?
266
267 fix     LastPacketTime[MAX_PLAYERS]; // For timeouts of idle/crashed players
268
269 int     PacketUrgent = 0;
270 int   NetGameType=0;
271 int     TotalMissedPackets=0,TotalPacketsGot=0;
272
273 frame_info      MySyncPack,UrgentSyncPack;
274 ubyte           MySyncPackInitialized = 0;              // Set to 1 if the MySyncPack is zeroed.
275 ushort          my_segments_checksum = 0;
276
277 sequence_packet My_Seq;
278 char WantPlayersInfo=0;
279 char WaitingForPlayerInfo=0;
280
281 char *RankStrings[]={"(unpatched) ","Cadet ","Ensign ","Lieutenant ","Lt.Commander ",
282                                                                 "Commander ","Captain ","Vice Admiral ","Admiral ","Demigod "};
283
284 extern obj_position Player_init[MAX_PLAYERS];
285
286 extern int force_cockpit_redraw;
287
288 #define DUMP_CLOSED 0
289 #define DUMP_FULL 1
290 #define DUMP_ENDLEVEL 2
291 #define DUMP_DORK 3
292 #define DUMP_ABORTED 4
293 #define DUMP_CONNECTED 5
294 #define DUMP_LEVEL 6
295 #define DUMP_KICKED 7
296
297 extern ubyte Version_major,Version_minor;
298 extern ubyte SurfingNet;
299 extern char MaxPowerupsAllowed[MAX_POWERUP_TYPES];
300 extern char PowerupsInMine[MAX_POWERUP_TYPES];
301
302 extern void multi_send_stolen_items();
303
304 int network_wait_for_snyc();
305 extern void multi_send_wall_status (int,ubyte,ubyte,ubyte);
306 extern void multi_send_wall_status_specific (int,int,ubyte,ubyte,ubyte);
307
308 extern void game_disable_cheats();
309
310 char IWasKicked=0;
311 FILE *SendLogFile,*RecieveLogFile;
312 int TTSent[100],TTRecv[100];
313
314 extern int Final_boss_is_dead;
315
316 // following is network stuff for appletalk
317
318 void network_dump_appletalk_player(ubyte node, ushort net, ubyte socket, int why);
319
320 #define NETWORK_OEM 0x10
321
322 #ifdef MACINTOSH
323
324 int Network_game_type;                                  // used to tell IPX vs. appletalk games
325
326 #define MAX_ZONES                       255
327 #define MAX_ZONE_LENGTH         33
328 #define DEFAULT_ZONE_NAME       "\p*"
329 #define MAX_REGISTER_TRIES      5                                       // maximum time we will try and register a netgame
330 char Network_zone_name[MAX_ZONE_LENGTH];                // zone name game being played in for appletalk
331 char Zone_names[MAX_ZONES][MAX_ZONE_LENGTH];    // total list of zones that we can see
332 ubyte appletalk_use_broadcast = 0;
333 ubyte Network_game_validated = 0;               // validates a game before being able to join
334 ubyte Network_game_validate_choice = 0;
335 ubyte Network_appletalk_type = 0;               // type of appletalk network game is being played on
336
337 #define ETHERTALK_TYPE  0
338 #define LOCALTALK_TYPE  1
339 #define OTHERTALK_TYPE  2
340
341
342 void network_release_registered_game(void);
343
344 #endif
345
346 void
347 network_init(void)
348 {
349   
350         // So you want to play a netgame, eh?  Let's a get a few things
351         // straight
352
353    int t;
354         int save_pnum = Player_num;
355
356         game_disable_cheats();
357    IWasKicked=0;
358    Final_boss_is_dead=0;
359    NamesInfoSecurity=-1;
360
361
362    #ifdef NETPROFILING
363            OpenSendLog();
364                 OpenRecieveLog(); 
365         #endif
366         
367         for (t=0;t<MAX_POWERUP_TYPES;t++)
368                 {
369                         MaxPowerupsAllowed[t]=0;
370                         PowerupsInMine[t]=0;
371                 }
372
373    TotalMissedPackets=0; TotalPacketsGot=0;
374
375         memset(&Netgame, 0, sizeof(netgame_info));
376         memset(&NetPlayers,0,sizeof(AllNetPlayers_info));
377         memset(&My_Seq, 0, sizeof(sequence_packet));
378         My_Seq.type = PID_REQUEST;
379         memcpy(My_Seq.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1);
380
381    #if defined (D2_OEM)
382            Version_minor|=NETWORK_OEM;
383         #endif
384  
385         My_Seq.player.version_major=Version_major;
386         My_Seq.player.version_minor=Version_minor;
387    My_Seq.player.rank=GetMyNetRanking();        
388  
389         if (Network_game_type == IPX_GAME) {
390                 memcpy(My_Seq.player.network.ipx.node, ipx_get_my_local_address(), 6);
391                 memcpy(My_Seq.player.network.ipx.server, ipx_get_my_server_address(), 4 );
392         #ifdef MACINTOSH
393         } else {
394                 My_Seq.player.network.appletalk.node = appletalk_get_my_node();
395                 My_Seq.player.network.appletalk.net = appletalk_get_my_net();
396                 My_Seq.player.network.appletalk.socket = appletalk_get_my_socket();
397         #endif
398         }
399 #ifdef MACINTOSH
400         My_Seq.player.computer_type = MAC;
401 #else
402         My_Seq.player.computer_type = DOS;
403 #endif
404
405         for (Player_num = 0; Player_num < MAX_NUM_NET_PLAYERS; Player_num++)
406                 init_player_stats_game();
407
408         Player_num = save_pnum;         
409         multi_new_game();
410         Network_new_game = 1;
411         Control_center_destroyed = 0;
412         network_flush();
413
414         Netgame.PacketsPerSec=10;
415
416    if ((t=FindArg("-packets")))
417     {
418      Netgame.PacketsPerSec=atoi(Args[t+1]);
419      if (Netgame.PacketsPerSec<1)
420       Netgame.PacketsPerSec=1;
421      else if (Netgame.PacketsPerSec>20)
422       Netgame.PacketsPerSec=20;
423      mprintf ((0,"Will send %d packets per second",Netgame.PacketsPerSec));
424     }
425    if (FindArg("-shortpackets"))
426     {
427      Netgame.ShortPackets=1;
428      mprintf ((0,"Will send short packets.\n"));
429     }
430 }
431
432 int
433 network_i_am_master(void)
434 {
435         // I am the lowest numbered player in this game?
436
437         int i;
438
439         if (!(Game_mode & GM_NETWORK))
440                 return (Player_num == 0);
441
442         for (i = 0; i < Player_num; i++)
443                 if (Players[i].connected)
444                         return 0;
445         return 1;
446 }
447 int
448 network_who_is_master(void)
449 {
450         // Who is the master of this game?
451
452         int i;
453
454         if (!(Game_mode & GM_NETWORK))
455                 return (Player_num == 0);
456
457         for (i = 0; i < N_players; i++)
458                 if (Players[i].connected)
459                         return i;
460         return Player_num;
461 }
462 int network_how_many_connected()
463  {
464   int num=0,i;
465  
466         for (i = 0; i < N_players; i++)
467                 if (Players[i].connected)
468                         num++;
469    return (num);
470  }
471
472 #define ENDLEVEL_SEND_INTERVAL (F1_0*2)
473 #define ENDLEVEL_IDLE_TIME      (F1_0*20)
474 /*      
475 void 
476 network_endlevel_poll( int nitems, newmenu_item * menus, int * key, int citem )
477 {
478         // Polling loop for End-of-level menu
479
480         static fix t1 = 0;
481         int i = 0;
482         int num_ready = 0;
483         int num_escaped = 0;
484         int goto_secret = 0;
485
486         int previous_state[MAX_NUM_NET_PLAYERS];
487         int previous_seconds_left;
488
489         menus = menus;
490         citem = citem;
491         nitems = nitems;
492         key = key;
493
494         // Send our endlevel packet at regular intervals
495
496         if (timer_get_approx_seconds() > (t1+ENDLEVEL_SEND_INTERVAL))
497         {
498                 network_send_endlevel_packet();
499                 t1 = timer_get_approx_seconds();
500         }
501
502         for (i = 0; i < N_players; i++)
503                 previous_state[i] = Players[i].connected;
504
505         previous_seconds_left = Countdown_seconds_left;
506
507         network_listen();
508
509         for (i = 0; i < N_players; i++)
510         {
511                 if (Players[i].connected == 1)
512                 {
513                         // Check timeout for idle players
514                         if (timer_get_approx_seconds() > LastPacketTime[i]+ENDLEVEL_IDLE_TIME)
515                         {
516                                 mprintf((0, "idle timeout for player %d.\n", i));
517                                 Players[i].connected = 0;
518                                 network_send_endlevel_sub(i);
519                         }                               
520                 }
521
522                 if ((Players[i].connected != 1) && (Players[i].connected != 5) && (Players[i].connected != 6))
523                         num_ready++;
524                 if (Players[i].connected != 1)
525                         num_escaped++;
526                 if (Players[i].connected == 4)
527                         goto_secret = 1;
528         }
529
530         if (num_escaped == N_players) // All players are out of the mine
531         {
532                 Countdown_seconds_left = -1;
533         }
534
535
536         if (num_ready == N_players) // All players have checked in or are disconnected
537         {
538                 if (goto_secret)
539                         *key = -3;
540                 else
541                         *key = -2;
542         }
543 } */
544   
545 void 
546 network_endlevel_poll2( int nitems, newmenu_item * menus, int * key, int citem )
547 {
548         // Polling loop for End-of-level menu
549
550         static fix t1 = 0;
551         int i = 0;
552         int num_ready = 0;
553         int goto_secret = 0;
554
555         menus = menus;
556         citem = citem;
557         nitems = nitems;
558         key = key;
559
560         // Send our endlevel packet at regular intervals
561
562         if (timer_get_approx_seconds() > (t1+ENDLEVEL_SEND_INTERVAL))
563         {
564                 network_send_endlevel_packet();
565                 t1 = timer_get_approx_seconds();
566         }
567
568 //   mprintf ((0,"Trying to listen!\n"));
569         network_listen();
570
571         for (i = 0; i < N_players; i++)
572         {
573                 if ((Players[i].connected != 1) && (Players[i].connected != 5) && (Players[i].connected != 6))
574                         num_ready++;
575                 if (Players[i].connected == 4)
576                         goto_secret = 1;                                        
577         }
578
579         if (num_ready == N_players) // All players have checked in or are disconnected
580         {
581                 if (goto_secret)
582                         *key = -3;
583                 else
584                         *key = -2;
585         }
586 }
587
588
589 extern fix StartAbortMenuTime;
590
591 void 
592 network_endlevel_poll3( int nitems, newmenu_item * menus, int * key, int citem )
593 {
594         // Polling loop for End-of-level menu
595
596    int num_ready=0,i;
597  
598         menus = menus;
599         citem = citem;
600         nitems = nitems;
601         key = key;
602
603         if (timer_get_approx_seconds() > (StartAbortMenuTime+(F1_0 * 8)))
604     *key=-2;
605
606
607         network_listen();
608
609
610         for (i = 0; i < N_players; i++)
611                 if ((Players[i].connected != 1) && (Players[i].connected != 5) && (Players[i].connected != 6))
612                         num_ready++;
613
614         if (num_ready == N_players) // All players have checked in or are disconnected
615                         *key = -2;
616
617 }
618
619
620 int
621 network_endlevel(int *secret)
622 {
623         // Do whatever needs to be done between levels
624
625    int i;
626
627    *secret=0;
628
629         //network_flush();
630
631         Network_status = NETSTAT_ENDLEVEL; // We are between levels
632
633         network_listen();
634
635         network_send_endlevel_packet();
636
637         for (i=0; i<N_players; i++) 
638         {
639                 LastPacketTime[i] = timer_get_approx_seconds();
640         }
641    
642         network_send_endlevel_packet();
643         network_send_endlevel_packet();
644         MySyncPackInitialized = 0;
645
646         network_update_netgame();
647
648         return(0);
649 }
650
651 int 
652 can_join_netgame(netgame_info *game,AllNetPlayers_info *people)
653 {
654         // Can this player rejoin a netgame in progress?
655
656         int i, num_players;
657
658         if (game->game_status == NETSTAT_STARTING)
659      return 1;
660
661         if (game->game_status != NETSTAT_PLAYING)
662     {
663       mprintf ((0,"Error: Can't join because game_status !=NETSTAT_PLAYING\n"));
664                 return 0;
665     }
666
667    if (game->version_major==0 && Version_major>0)
668     {
669            mprintf ((0,"Error:Can't join because version majors don't match!\n"));
670                 return (0);
671     }
672
673         if (game->version_major>0 && Version_major==0)
674     {
675            mprintf ((0,"Error:Can't join because version majors2 don't match!\n"));
676                 return (0);
677     }
678
679         // Game is in progress, figure out if this guy can re-join it
680
681         num_players = game->numplayers;
682
683         if (!(game->game_flags & NETGAME_FLAG_CLOSED)) {
684                 // Look for player that is not connected
685                 
686                 if (game->numconnected==game->max_numplayers)
687                  return (2);
688
689 //      mprintf ((0,"Refuse = %d\n",game->RefusePlayers));
690                 
691                 if (game->RefusePlayers)
692                  return (3);
693                 
694                 if (game->numplayers < game->max_numplayers)
695                         return 1;
696
697                 if (game->numconnected<num_players)
698                         return 1;
699                      
700         }
701
702         if (people==NULL)
703     {
704       mprintf ((0,"Error! Can't join because people==NULL!\n"));
705                 return 0;
706     }
707         
708         // Search to see if we were already in this closed netgame in progress
709
710         for (i = 0; i < num_players; i++) {
711                 if (Network_game_type == IPX_GAME) {
712                         if ( (!stricmp(Players[Player_num].callsign, people->players[i].callsign)) &&
713                                   (!memcmp(My_Seq.player.network.ipx.node, people->players[i].network.ipx.node, 6)) &&
714                                   (!memcmp(My_Seq.player.network.ipx.server, people->players[i].network.ipx.server, 4)) )
715                                 break;
716                 } else {
717                         if ( (!stricmp(Players[Player_num].callsign, people->players[i].callsign)) &&
718                                   (My_Seq.player.network.appletalk.node == people->players[i].network.appletalk.node) &&
719                                   (My_Seq.player.network.appletalk.net == people->players[i].network.appletalk.net) )
720                                 break;
721                 }
722         }
723
724         if (i != num_players)
725                 return 1;
726  
727    mprintf ((0,"Error: Can't join because at end of list!\n"));
728         return 0;
729 }
730
731 void
732 network_disconnect_player(int playernum)
733 {
734         // A player has disconnected from the net game, take whatever steps are
735         // necessary 
736
737         if (playernum == Player_num) 
738         {
739                 Int3(); // Weird, see Rob
740                 return;
741         }
742
743         Players[playernum].connected = 0;
744    NetPlayers.players[playernum].connected = 0;
745    if (VerifyPlayerJoined==playernum)
746           VerifyPlayerJoined=-1;
747
748 //      create_player_appearance_effect(&Objects[Players[playernum].objnum]);
749         multi_make_player_ghost(playernum);
750
751         if (Newdemo_state == ND_STATE_RECORDING)
752                 newdemo_record_multi_disconnect(playernum);
753
754         multi_strip_robots(playernum);
755 }
756                 
757 void
758 network_new_player(sequence_packet *their)
759 {
760         int objnum;
761         int pnum;
762
763         pnum = their->player.connected;
764
765         Assert(pnum >= 0);
766         Assert(pnum < MaxNumNetPlayers);        
767         
768         objnum = Players[pnum].objnum;
769
770         if (Newdemo_state == ND_STATE_RECORDING) {
771                 int new_player;
772
773                 if (pnum == N_players)
774                         new_player = 1;
775                 else
776                         new_player = 0;
777                 newdemo_record_multi_connect(pnum, new_player, their->player.callsign);
778         }
779
780         memcpy(Players[pnum].callsign, their->player.callsign, CALLSIGN_LEN+1);
781         memcpy(NetPlayers.players[pnum].callsign, their->player.callsign, CALLSIGN_LEN+1);
782         
783
784    ClipRank (&their->player.rank);
785    NetPlayers.players[pnum].rank=their->player.rank;
786         NetPlayers.players[pnum].version_major=their->player.version_major;
787         NetPlayers.players[pnum].version_minor=their->player.version_minor;
788    network_check_for_old_version(pnum);
789
790         if (Network_game_type == IPX_GAME) {
791                 if ( (*(uint *)their->player.network.ipx.server) != 0 )
792                         ipx_get_local_target( their->player.network.ipx.server, their->player.network.ipx.node, Players[pnum].net_address );
793                 else
794                         memcpy(Players[pnum].net_address, their->player.network.ipx.node, 6);
795         
796                 memcpy(NetPlayers.players[pnum].network.ipx.node, their->player.network.ipx.node, 6);
797                 memcpy(NetPlayers.players[pnum].network.ipx.server, their->player.network.ipx.server, 4);
798         } else {
799                 NetPlayers.players[pnum].network.appletalk.node = their->player.network.appletalk.node;
800                 NetPlayers.players[pnum].network.appletalk.net = their->player.network.appletalk.net;
801                 NetPlayers.players[pnum].network.appletalk.socket = their->player.network.appletalk.socket;
802         }
803
804         Players[pnum].n_packets_got = 0;
805         Players[pnum].connected = 1;
806         Players[pnum].net_kills_total = 0;
807         Players[pnum].net_killed_total = 0;
808         memset(kill_matrix[pnum], 0, MAX_PLAYERS*sizeof(short)); 
809         Players[pnum].score = 0;
810         Players[pnum].flags = 0;
811         Players[pnum].KillGoalCount=0;
812
813         if (pnum == N_players)
814         {
815                 N_players++;
816                 Netgame.numplayers = N_players;
817         }
818
819         digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
820
821    ClipRank (&their->player.rank);
822    
823    if (FindArg("-norankings"))
824           HUD_init_message("'%s' %s\n",their->player.callsign, TXT_JOINING);
825    else   
826      HUD_init_message("%s'%s' %s\n",RankStrings[their->player.rank],their->player.callsign, TXT_JOINING);
827         
828         multi_make_ghost_player(pnum);
829
830         multi_send_score();
831         multi_sort_kill_list();
832
833 //      create_player_appearance_effect(&Objects[objnum]);
834 }
835
836 char RefuseThisPlayer=0,WaitForRefuseAnswer=0,RefuseTeam;
837 char RefusePlayerName[12];
838 fix RefuseTimeLimit=0;
839
840 void network_welcome_player(sequence_packet *their)
841 {
842         // Add a player to a game already in progress
843         ubyte local_address[6];
844         int player_num;
845         int i;
846
847    WaitForRefuseAnswer=0;
848
849         if (FindArg("-NoMatrixCheat"))
850         {
851                 if (their->player.version_minor & (0x0F<3))
852                 {
853                                         network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_DORK);
854                                         return;
855                 }
856         }
857
858         if (HoardEquipped())
859         {
860    // If hoard game, and this guy isn't D2 Christmas (v1.2), dump him
861
862            if ((Game_mode & GM_HOARD) && ((their->player.version_minor & 0x0F)<2))
863                 {
864                         if (Network_game_type == IPX_GAME)
865                                 network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_DORK);
866                         #ifdef MACINTOSH
867                         else
868                                 network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_DORK);
869                         #endif
870                         return;
871                 }
872         }
873
874         // Don't accept new players if we're ending this level.  Its safe to
875         // ignore since they'll request again later
876
877         if ((Endlevel_sequence) || (Control_center_destroyed))
878         {
879                 mprintf((0, "Ignored request from new player to join during endgame.\n"));
880                 if (Network_game_type == IPX_GAME)
881                         network_dump_player(their->player.network.ipx.server,their->player.network.ipx.node, DUMP_ENDLEVEL);
882                 #ifdef MACINTOSH
883                 else
884                         network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_ENDLEVEL);
885                 #endif
886                 return; 
887         }
888
889         if (Network_send_objects || Network_sending_extras)
890         {
891                 // Ignore silently, we're already responding to someone and we can't
892                 // do more than one person at a time.  If we don't dump them they will
893                 // re-request in a few seconds.
894                 return;
895         }
896
897         if (their->player.connected != Current_level_num)
898         {
899                 mprintf((0, "Dumping player due to old level number.\n"));
900                 if (Network_game_type == IPX_GAME)
901                         network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_LEVEL);
902                 #ifdef MACINTOSH
903                 else
904                         network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_LEVEL);
905                 #endif
906                 return;
907         }
908
909         player_num = -1;
910         memset(&Network_player_rejoining, 0, sizeof(sequence_packet));
911         Network_player_added = 0;
912
913         if (Network_game_type == IPX_GAME) {
914                 if ( (*(uint *)their->player.network.ipx.server) != 0 )
915                         ipx_get_local_target( their->player.network.ipx.server, their->player.network.ipx.node, local_address );
916                 else
917                         memcpy(local_address, their->player.network.ipx.node, 6);
918         }
919
920         for (i = 0; i < N_players; i++)
921         {
922                 if ( (Network_game_type == IPX_GAME) && (!stricmp(Players[i].callsign, their->player.callsign )) && (!memcmp(Players[i].net_address,local_address, 6)) ) 
923                 {
924                         player_num = i;
925                         break;
926                 }
927 #ifdef MACINTOSH                // note link to above if
928                         else if ( (!stricmp(Players[i].callsign, their->player.callsign)) &&
929                                     (NetPlayers.players[i].network.appletalk.node == their->player.network.appletalk.node) &&
930                                         (NetPlayers.players[i].network.appletalk.net == their->player.network.appletalk.net)) {
931                         player_num = i;
932                         break;
933                 }
934 #endif
935         }
936
937         if (player_num == -1)
938         {
939                 // Player is new to this game
940
941                 if ( !(Netgame.game_flags & NETGAME_FLAG_CLOSED) && (N_players < MaxNumNetPlayers))
942                 {
943                         // Add player in an open slot, game not full yet
944
945                         player_num = N_players;
946                         Network_player_added = 1;
947                 }
948                 else if (Netgame.game_flags & NETGAME_FLAG_CLOSED)
949                 {
950                         // Slots are open but game is closed
951
952                         if (Network_game_type == IPX_GAME)
953                                 network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_CLOSED);
954                         #ifdef MACINTOSH
955                         else
956                                 network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_CLOSED);
957                         #endif
958                         return;
959                 }
960                 else
961                 {
962                         // Slots are full but game is open, see if anyone is
963                         // disconnected and replace the oldest player with this new one
964                 
965                         int oldest_player = -1;
966                         fix oldest_time = timer_get_approx_seconds();
967
968                         Assert(N_players == MaxNumNetPlayers);
969
970                         for (i = 0; i < N_players; i++)
971                         {
972                                 if ( (!Players[i].connected) && (LastPacketTime[i] < oldest_time))
973                                 {
974                                         oldest_time = LastPacketTime[i];
975                                         oldest_player = i;
976                                 }
977                         }
978
979                         if (oldest_player == -1)
980                         {
981                                 // Everyone is still connected 
982
983                                 if (Network_game_type == IPX_GAME)
984                                         network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_FULL);
985                                 #ifdef MACINTOSH
986                                 else
987                                         network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_FULL);
988                                 #endif
989                                 return;
990                         }
991                         else
992                         {       
993                                 // Found a slot!
994
995                                 player_num = oldest_player;
996                                 Network_player_added = 1;
997                         }
998                 }
999         }
1000         else 
1001         {
1002                 // Player is reconnecting
1003                 
1004                 if (Players[player_num].connected)
1005                 {
1006                         mprintf((0, "Extra REQUEST from player ignored.\n"));
1007                         return;
1008                 }
1009
1010                 if (Newdemo_state == ND_STATE_RECORDING)
1011                         newdemo_record_multi_reconnect(player_num);
1012
1013                 Network_player_added = 0;
1014
1015                 digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
1016                 
1017                 if (FindArg("-norankings"))
1018                         HUD_init_message("'%s' %s", Players[player_num].callsign, TXT_REJOIN);
1019                 else
1020                         HUD_init_message("%s'%s' %s", RankStrings[NetPlayers.players[player_num].rank],Players[player_num].callsign, TXT_REJOIN);
1021         }
1022
1023         Players[player_num].KillGoalCount=0;
1024
1025         // Send updated Objects data to the new/returning player
1026
1027         
1028         Network_player_rejoining = *their;
1029         Network_player_rejoining.player.connected = player_num;
1030         Network_send_objects = 1;
1031         Network_send_objnum = -1;
1032
1033         network_send_objects();
1034 }
1035
1036 int network_objnum_is_past(int objnum)
1037 {
1038         // determine whether or not a given object number has already been sent
1039         // to a re-joining player.
1040         
1041         int player_num = Network_player_rejoining.player.connected;
1042         int obj_mode = !((object_owner[objnum] == -1) || (object_owner[objnum] == player_num));
1043
1044         if (!Network_send_objects)
1045                 return 0; // We're not sending objects to a new player
1046
1047         if (obj_mode > Network_send_object_mode)
1048                 return 0;
1049         else if (obj_mode < Network_send_object_mode)
1050                 return 1;
1051         else if (objnum < Network_send_objnum)
1052                 return 1;
1053         else
1054                 return 0;
1055 }
1056
1057 #define OBJ_PACKETS_PER_FRAME 1
1058 extern void multi_send_active_door(char);
1059 extern void multi_send_door_open_specific(int,int,int,ubyte);
1060
1061
1062 void network_send_door_updates(int pnum)
1063 {
1064         // Send door status when new player joins
1065         
1066         int i;
1067    
1068    pnum=pnum;
1069
1070 //   Assert (pnum>-1 && pnum<N_players);
1071
1072         for (i = 0; i < Num_walls; i++)
1073         {
1074       if ((Walls[i].type == WALL_DOOR) && ((Walls[i].state == WALL_DOOR_OPENING) || (Walls[i].state == WALL_DOOR_WAITING) || (Walls[i].state == WALL_DOOR_OPEN)))
1075                         multi_send_door_open_specific(pnum,Walls[i].segnum, Walls[i].sidenum,Walls[i].flags);
1076                 else if ((Walls[i].type == WALL_BLASTABLE) && (Walls[i].flags & WALL_BLASTED))
1077                         multi_send_door_open_specific(pnum,Walls[i].segnum, Walls[i].sidenum,Walls[i].flags);
1078                 else if ((Walls[i].type == WALL_BLASTABLE) && (Walls[i].hps != WALL_HPS))
1079                         multi_send_hostage_door_status(i);
1080                 else
1081                         multi_send_wall_status_specific(pnum,i,Walls[i].type,Walls[i].flags,Walls[i].state);
1082         }
1083 }
1084
1085 extern vms_vector MarkerPoint[];
1086 void network_send_markers()
1087  {
1088   // send marker positions/text to new player
1089
1090   
1091   int i;
1092   
1093   for (i=0;i<8;i++)
1094    {
1095     if (MarkerObject[(i*2)]!=-1)
1096      multi_send_drop_marker (i,MarkerPoint[(i*2)],0,MarkerMessage[i*2]);
1097     if (MarkerObject[(i*2)+1]!=-1)
1098      multi_send_drop_marker (i,MarkerPoint[(i*2)+1],1,MarkerMessage[(i*2)+1]);
1099    }
1100  }
1101
1102 void network_process_monitor_vector(int vector)
1103 {
1104         int i, j;
1105         int count = 0;
1106         segment *seg;
1107         
1108         for (i=0; i <= Highest_segment_index; i++)
1109         {
1110                 int tm, ec, bm;
1111                 seg = &Segments[i];
1112                 for (j = 0; j < 6; j++)
1113                 {
1114                         if ( ((tm = seg->sides[j].tmap_num2) != 0) &&
1115                                   ((ec = TmapInfo[tm&0x3fff].eclip_num) != -1) &&
1116                                   ((bm = Effects[ec].dest_bm_num) != -1) )
1117                         {
1118                                 if (vector & (1 << count))
1119                                 {
1120                                         seg->sides[j].tmap_num2 = bm | (tm&0xc000);
1121                                 //      mprintf((0, "Monitor %d blown up.\n", count));
1122                                 }
1123                                 //else
1124                                   //    mprintf((0, "Monitor %d intact.\n", count));
1125                                 count++;
1126                                 Assert(count < 32);
1127                         }
1128                 }
1129         }
1130 }
1131
1132 int network_create_monitor_vector(void)
1133 {
1134         int i, j, k;
1135         int num_blown_bitmaps = 0;
1136         int monitor_num = 0;
1137         #define NUM_BLOWN_BITMAPS 20
1138         int blown_bitmaps[NUM_BLOWN_BITMAPS];
1139         int vector = 0;
1140         segment *seg;
1141
1142         for (i=0; i < Num_effects; i++)
1143         {
1144                 if (Effects[i].dest_bm_num > 0) {
1145                         for (j = 0; j < num_blown_bitmaps; j++)
1146                                 if (blown_bitmaps[j] == Effects[i].dest_bm_num)
1147                                         break;
1148                         if (j == num_blown_bitmaps) {
1149                                 blown_bitmaps[num_blown_bitmaps++] = Effects[i].dest_bm_num;
1150                                 Assert(num_blown_bitmaps < NUM_BLOWN_BITMAPS);
1151                         }
1152                 }
1153         }               
1154                 
1155 //      for (i = 0; i < num_blown_bitmaps; i++)
1156 //              mprintf((0, "Blown bitmap #%d = %d.\n", i, blown_bitmaps[i]));
1157
1158         for (i=0; i <= Highest_segment_index; i++)
1159         {
1160                 int tm, ec;
1161                 seg = &Segments[i];
1162                 for (j = 0; j < 6; j++)
1163                 {
1164                         if ((tm = seg->sides[j].tmap_num2) != 0) 
1165                         {
1166                                 if ( ((ec = TmapInfo[tm&0x3fff].eclip_num) != -1) &&
1167                                           (Effects[ec].dest_bm_num != -1) )
1168                                 {
1169                                 //      mprintf((0, "Monitor %d intact.\n", monitor_num));
1170                                         monitor_num++;
1171                                         Assert(monitor_num < 32);
1172                                 }
1173                                 else
1174                                 {
1175                                         for (k = 0; k < num_blown_bitmaps; k++)
1176                                         {
1177                                                 if ((tm&0x3fff) == blown_bitmaps[k])
1178                                                 {
1179                                                         //mprintf((0, "Monitor %d destroyed.\n", monitor_num));
1180                                                         vector |= (1 << monitor_num);
1181                                                         monitor_num++;
1182                                                         Assert(monitor_num < 32);
1183                                                         break;
1184                                                 }
1185                                         }
1186                                 }
1187                         }
1188                 }
1189         }
1190   //    mprintf((0, "Final monitor vector %x.\n", vector));
1191         return(vector);
1192 }
1193
1194 void network_stop_resync(sequence_packet *their)
1195 {
1196         if (Network_game_type == IPX_GAME) {
1197                 if ( (!memcmp(Network_player_rejoining.player.network.ipx.node, their->player.network.ipx.node, 6)) &&
1198                           (!memcmp(Network_player_rejoining.player.network.ipx.server, their->player.network.ipx.server, 4)) &&
1199                      (!stricmp(Network_player_rejoining.player.callsign, their->player.callsign)) )
1200                 {
1201                         mprintf((0, "Aborting resync for player %s.\n", their->player.callsign));
1202                         Network_send_objects = 0;
1203                         Network_sending_extras=0;
1204                         Network_rejoined=0;
1205                         Player_joining_extras=-1;
1206                         Network_send_objnum = -1;
1207                 }
1208         } else {
1209                 if ( (Network_player_rejoining.player.network.appletalk.node == their->player.network.appletalk.node) &&
1210                          (Network_player_rejoining.player.network.appletalk.net == their->player.network.appletalk.net) &&
1211                          (!stricmp(Network_player_rejoining.player.callsign, their->player.callsign)) )
1212                 {
1213                         mprintf((0, "Aborting resync for player %s.\n", their->player.callsign));
1214                         Network_send_objects = 0;
1215                         Network_sending_extras=0;
1216                         Network_rejoined=0;
1217                         Player_joining_extras=-1;
1218                         Network_send_objnum = -1;
1219                 }
1220         }
1221 }
1222
1223 byte object_buffer[IPX_MAX_DATA_SIZE];
1224
1225 void network_send_objects(void)
1226 {
1227         short remote_objnum;
1228         byte owner;
1229         int loc, i, h;
1230
1231         static int obj_count = 0;
1232         static int frame_num = 0;
1233
1234         int obj_count_frame = 0;
1235         int player_num = Network_player_rejoining.player.connected;
1236
1237         // Send clear objects array trigger and send player num
1238
1239         Assert(Network_send_objects != 0);
1240         Assert(player_num >= 0);
1241         Assert(player_num < MaxNumNetPlayers);
1242
1243         if (Endlevel_sequence || Control_center_destroyed)
1244         {
1245                 // Endlevel started before we finished sending the goods, we'll
1246                 // have to stop and try again after the level.
1247
1248                 if (Network_game_type == IPX_GAME)
1249                         network_dump_player(Network_player_rejoining.player.network.ipx.server,Network_player_rejoining.player.network.ipx.node, DUMP_ENDLEVEL);
1250                 #ifdef MACINTOSH
1251                 else
1252                         network_dump_appletalk_player(Network_player_rejoining.player.network.appletalk.node,Network_player_rejoining.player.network.appletalk.net, Network_player_rejoining.player.network.appletalk.socket, DUMP_ENDLEVEL);
1253                 #endif
1254                 Network_send_objects = 0; 
1255                 return;
1256         }
1257
1258         for (h = 0; h < OBJ_PACKETS_PER_FRAME; h++) // Do more than 1 per frame, try to speed it up without
1259                                                                                                                           // over-stressing the receiver.
1260         {
1261                 obj_count_frame = 0;
1262                 memset(object_buffer, 0, IPX_MAX_DATA_SIZE);
1263                 object_buffer[0] = PID_OBJECT_DATA;
1264                 loc = 3;
1265         
1266                 if (Network_send_objnum == -1)
1267                 {
1268                         obj_count = 0;
1269                         Network_send_object_mode = 0;
1270 //       mprintf((0, "Sending object array to player %d.\n", player_num));
1271                         *(short *)(object_buffer+loc) = INTEL_SHORT(-1);        loc += 2;
1272                         object_buffer[loc] = player_num;                loc += 1;
1273                                                                                                         loc += 2; // Placeholder for remote_objnum, not used here
1274                         Network_send_objnum = 0;
1275                         obj_count_frame = 1;
1276                         frame_num = 0;
1277                 }
1278                 
1279                 for (i = Network_send_objnum; i <= Highest_object_index; i++)
1280                 {
1281                         if ((Objects[i].type != OBJ_POWERUP) && (Objects[i].type != OBJ_PLAYER) &&
1282                                  (Objects[i].type != OBJ_CNTRLCEN) && (Objects[i].type != OBJ_GHOST) &&
1283                                  (Objects[i].type != OBJ_ROBOT) && (Objects[i].type != OBJ_HOSTAGE) &&
1284                                  !(Objects[i].type==OBJ_WEAPON && Objects[i].id==PMINE_ID))
1285                                 continue;
1286                         if ((Network_send_object_mode == 0) && ((object_owner[i] != -1) && (object_owner[i] != player_num)))
1287                                 continue;
1288                         if ((Network_send_object_mode == 1) && ((object_owner[i] == -1) || (object_owner[i] == player_num)))
1289                                 continue;
1290
1291                         if ( ((IPX_MAX_DATA_SIZE-1) - loc) < (sizeof(object)+5) )
1292                                 break; // Not enough room for another object
1293
1294                         obj_count_frame++;
1295                         obj_count++;
1296         
1297                         remote_objnum = objnum_local_to_remote((short)i, &owner);
1298                         Assert(owner == object_owner[i]);
1299
1300                         *(short *)(object_buffer+loc) = INTEL_SHORT((short)i);                          loc += 2;
1301                         object_buffer[loc] = owner;                                                                                     loc += 1;
1302                         *(short *)(object_buffer+loc) = INTEL_SHORT(remote_objnum);             loc += 2;
1303 #ifndef MACINTOSH
1304                         memcpy(object_buffer+loc, &Objects[i], sizeof(object)); loc += sizeof(object);
1305 #else
1306                         if (Network_game_type == IPX_GAME) {
1307                                 object tmpobj;
1308
1309                                 memcpy(&tmpobj, &(Objects[i]), sizeof(object));
1310                                 swap_object(&tmpobj);
1311                                 memcpy(&(object_buffer[loc]), &tmpobj, sizeof(object));                 loc += sizeof(object);
1312                         }
1313                         else
1314                                 memcpy(object_buffer+loc, &Objects[i], sizeof(object)); loc += sizeof(object);
1315 #endif
1316 //                      mprintf((0, "..packing object %d, remote %d\n", i, remote_objnum));
1317                 }
1318
1319                 if (obj_count_frame) // Send any objects we've buffered
1320                 {
1321                         frame_num++;
1322         
1323                         Network_send_objnum = i;
1324                         object_buffer[1] = obj_count_frame;  object_buffer[2] = frame_num;
1325 //       mprintf((0, "Object packet %d contains %d objects.\n", frame_num, obj_count_frame));
1326
1327                         Assert(loc <= IPX_MAX_DATA_SIZE);
1328
1329                         if (Network_game_type == IPX_GAME)
1330                                 ipx_send_internetwork_packet_data( object_buffer, loc, Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1331                         #ifdef MACINTOSH
1332                         else
1333                                 appletalk_send_packet_data( object_buffer, loc, Network_player_rejoining.player.network.appletalk.node,
1334                                         Network_player_rejoining.player.network.appletalk.net,
1335                                         Network_player_rejoining.player.network.appletalk.socket);
1336                         #endif
1337
1338                         // OLD ipx_send_packet_data(object_buffer, loc, &Network_player_rejoining.player.node);
1339                 }
1340
1341                 if (i > Highest_object_index)
1342                 {
1343                         if (Network_send_object_mode == 0)
1344                         {
1345                                 Network_send_objnum = 0;
1346                                 Network_send_object_mode = 1; // go to next mode
1347                         }
1348                         else 
1349                         {
1350                                 Assert(Network_send_object_mode == 1); 
1351
1352                                 frame_num++;
1353                                 // Send count so other side can make sure he got them all
1354 //                              mprintf((0, "Sending end marker in packet #%d.\n", frame_num));
1355                                 mprintf((0, "Sent %d objects.\n", obj_count));
1356                                 object_buffer[0] = PID_OBJECT_DATA;
1357                                 object_buffer[1] = 1;
1358                                 object_buffer[2] = frame_num;
1359                                 *(short *)(object_buffer+3) = INTEL_SHORT(-2);
1360                                 *(short *)(object_buffer+6) = INTEL_SHORT(obj_count);
1361                                 //OLD ipx_send_packet_data(object_buffer, 8, &Network_player_rejoining.player.node);
1362                                 if (Network_game_type == IPX_GAME)
1363                                         ipx_send_internetwork_packet_data(object_buffer, 8, Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node);
1364                                 #ifdef MACINTOSH
1365                                 else
1366                                         appletalk_send_packet_data( object_buffer, 8, Network_player_rejoining.player.network.appletalk.node,
1367                                                                                                 Network_player_rejoining.player.network.appletalk.net,
1368                                                                                                 Network_player_rejoining.player.network.appletalk.socket);
1369                                 #endif
1370                                 
1371                         
1372                                 // Send sync packet which tells the player who he is and to start!
1373                                 network_send_rejoin_sync(player_num);
1374                                 mprintf ((0,"VerfiyPlayerJoined is now set to %d\n",player_num));
1375                                 VerifyPlayerJoined=player_num;
1376
1377                                 // Turn off send object mode
1378                                 Network_send_objnum = -1;
1379                                 Network_send_objects = 0;
1380                                 obj_count = 0;
1381
1382                                 //if (!network_i_am_master ())
1383                                 // Int3();  // Bad!! Get Jason.  Someone goofy is trying to get ahold of the game!
1384
1385                                 Network_sending_extras=40; // start to send extras
1386                            Player_joining_extras=player_num;
1387
1388                                 return;
1389                         } // mode == 1;
1390                 } // i > Highest_object_index
1391         } // For PACKETS_PER_FRAME
1392 }
1393
1394 extern void multi_send_powerup_update();
1395
1396 void network_send_rejoin_sync(int player_num)
1397 {
1398         int i, j;
1399
1400         Players[player_num].connected = 1; // connect the new guy
1401         LastPacketTime[player_num] = timer_get_approx_seconds();
1402
1403         if (Endlevel_sequence || Control_center_destroyed)
1404         {
1405                 // Endlevel started before we finished sending the goods, we'll
1406                 // have to stop and try again after the level.
1407
1408                 if (Network_game_type == IPX_GAME)
1409                         network_dump_player(Network_player_rejoining.player.network.ipx.server,Network_player_rejoining.player.network.ipx.node, DUMP_ENDLEVEL);
1410                 #ifdef MACINTOSH
1411                 else
1412                         network_dump_appletalk_player(Network_player_rejoining.player.network.appletalk.node,Network_player_rejoining.player.network.appletalk.net, Network_player_rejoining.player.network.appletalk.socket, DUMP_ENDLEVEL);
1413                 #endif
1414                 Network_send_objects = 0; 
1415                 Network_sending_extras=0;
1416                 return;
1417         }
1418
1419         if (Network_player_added)
1420         {
1421                 Network_player_rejoining.type = PID_ADDPLAYER;
1422                 Network_player_rejoining.player.connected = player_num;
1423                 network_new_player(&Network_player_rejoining);
1424
1425                 for (i = 0; i < N_players; i++)
1426                 {
1427                         if ((i != player_num) && (i != Player_num) && (Players[i].connected))
1428                                 if (Network_game_type == IPX_GAME) {
1429                                         #ifndef MACINTOSH
1430                                         ipx_send_packet_data( (ubyte *)&Network_player_rejoining, sizeof(sequence_packet), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, Players[i].net_address);
1431                                         #else
1432                                         send_sequence_packet( Network_player_rejoining, NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, Players[i].net_address);
1433                                         #endif
1434                                 #ifdef MACINTOSH
1435                                 } else {
1436                                         appletalk_send_packet_data( (ubyte *)&Network_player_rejoining, sizeof(sequence_packet), NetPlayers.players[i].network.appletalk.node,
1437                                                                                                 NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket);
1438                                 #endif
1439                                 }
1440                 }
1441         }       
1442
1443         // Send sync packet to the new guy
1444
1445         network_update_netgame();
1446
1447         // Fill in the kill list
1448         for (j=0; j<MAX_PLAYERS; j++)
1449         {
1450                 for (i=0; i<MAX_PLAYERS;i++)
1451                         Netgame.kills[j][i] = kill_matrix[j][i];
1452                 Netgame.killed[j] = Players[j].net_killed_total;
1453                 Netgame.player_kills[j] = Players[j].net_kills_total;
1454                 Netgame.player_score[j] = Players[j].score;
1455         }       
1456
1457         Netgame.level_time = Players[Player_num].time_level;
1458         Netgame.monitor_vector = network_create_monitor_vector();
1459
1460         mprintf((0, "Sending rejoin sync packet!!!\n"));
1461
1462         if (Network_game_type == IPX_GAME) {
1463                 #ifndef MACINTOSH       
1464                 ipx_send_internetwork_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1465                 ipx_send_internetwork_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1466                 #else
1467                 send_netgame_packet(Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node, NULL, 0);
1468                 send_netplayers_packet(Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node);
1469                 #endif
1470         #ifdef MACINTOSH
1471         } else {
1472                 appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), Network_player_rejoining.player.network.appletalk.node,
1473                                                                         Network_player_rejoining.player.network.appletalk.net,
1474                                                                         Network_player_rejoining.player.network.appletalk.socket);
1475                 appletalk_send_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), Network_player_rejoining.player.network.appletalk.node,
1476                                                                         Network_player_rejoining.player.network.appletalk.net,
1477                                                                         Network_player_rejoining.player.network.appletalk.socket);
1478         #endif
1479         }
1480         return;
1481 }
1482 void resend_sync_due_to_packet_loss_for_allender ()
1483 {
1484    int i,j;
1485
1486    mprintf ((0,"I'm resending a sync packet! VPJ=%d\n",VerifyPlayerJoined));
1487   
1488         network_update_netgame();
1489
1490         // Fill in the kill list
1491         for (j=0; j<MAX_PLAYERS; j++)
1492         {
1493                 for (i=0; i<MAX_PLAYERS;i++)
1494                         Netgame.kills[j][i] = kill_matrix[j][i];
1495                 Netgame.killed[j] = Players[j].net_killed_total;
1496                 Netgame.player_kills[j] = Players[j].net_kills_total;
1497                 Netgame.player_score[j] = Players[j].score;
1498         }       
1499
1500         Netgame.level_time = Players[Player_num].time_level;
1501         Netgame.monitor_vector = network_create_monitor_vector();
1502
1503         if (Network_game_type == IPX_GAME) {
1504                 #ifndef MACINTOSH       
1505                 ipx_send_internetwork_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1506                 ipx_send_internetwork_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1507                 #else
1508                 send_netgame_packet(Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node, NULL, 0);
1509                 send_netplayers_packet(Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node);
1510                 #endif
1511         #ifdef MACINTOSH
1512         } else {
1513                 appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), Network_player_rejoining.player.network.appletalk.node,
1514                                                                         Network_player_rejoining.player.network.appletalk.net,
1515                                                                         Network_player_rejoining.player.network.appletalk.socket);
1516                 appletalk_send_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), Network_player_rejoining.player.network.appletalk.node,
1517                                                                         Network_player_rejoining.player.network.appletalk.net,
1518                                                                         Network_player_rejoining.player.network.appletalk.socket);
1519         #endif
1520         }
1521 }
1522
1523
1524 char * network_get_player_name( int objnum )
1525 {
1526         if ( objnum < 0 ) return NULL; 
1527         if ( Objects[objnum].type != OBJ_PLAYER ) return NULL;
1528         if ( Objects[objnum].id >= MAX_PLAYERS ) return NULL;
1529         if ( Objects[objnum].id >= N_players ) return NULL;
1530         
1531         return Players[Objects[objnum].id].callsign;
1532 }
1533
1534
1535 void network_add_player(sequence_packet *p)
1536 {
1537         int i;
1538         
1539         mprintf((0, "Got add player request!\n"));
1540
1541         for (i=0; i<N_players; i++ )    {
1542                 if (Network_game_type == IPX_GAME) {
1543                         if ( !memcmp( NetPlayers.players[i].network.ipx.node, p->player.network.ipx.node, 6) && !memcmp(NetPlayers.players[i].network.ipx.server, p->player.network.ipx.server, 4)) 
1544                                 return;         // already got them
1545                 } else {
1546                         if ( (NetPlayers.players[i].network.appletalk.node == p->player.network.appletalk.node) &&
1547                                  (NetPlayers.players[i].network.appletalk.net == p->player.network.appletalk.net))
1548                                         return;
1549                 }
1550         }
1551
1552         if (Network_game_type == IPX_GAME) {
1553                 memcpy( NetPlayers.players[N_players].network.ipx.node, p->player.network.ipx.node, 6 );
1554                 memcpy( NetPlayers.players[N_players].network.ipx.server, p->player.network.ipx.server, 4 );
1555         } else {
1556                 NetPlayers.players[N_players].network.appletalk.node = p->player.network.appletalk.node;
1557                 NetPlayers.players[N_players].network.appletalk.net = p->player.network.appletalk.net;
1558                 NetPlayers.players[N_players].network.appletalk.socket = p->player.network.appletalk.socket;
1559         }
1560    
1561    ClipRank (&p->player.rank);
1562   
1563         memcpy( NetPlayers.players[N_players].callsign, p->player.callsign, CALLSIGN_LEN+1 );
1564         NetPlayers.players[N_players].version_major=p->player.version_major;
1565         NetPlayers.players[N_players].version_minor=p->player.version_minor;
1566    NetPlayers.players[N_players].rank=p->player.rank;
1567         NetPlayers.players[N_players].connected = 1;
1568    network_check_for_old_version (N_players);
1569
1570         Players[N_players].KillGoalCount=0;
1571         Players[N_players].connected = 1;
1572         LastPacketTime[N_players] = timer_get_approx_seconds();
1573         N_players++;
1574         Netgame.numplayers = N_players;
1575
1576         // Broadcast updated info
1577
1578    mprintf ((0,"sending_game_info!\n"));
1579         network_send_game_info(NULL);
1580 }
1581
1582 // One of the players decided not to join the game
1583
1584 void network_remove_player(sequence_packet *p)
1585 {
1586         int i,pn;
1587         
1588         pn = -1;
1589         for (i=0; i<N_players; i++ )    {
1590                 if (Network_game_type == IPX_GAME) {
1591                         if (!memcmp(NetPlayers.players[i].network.ipx.node, p->player.network.ipx.node, 6) && !memcmp(NetPlayers.players[i].network.ipx.server, p->player.network.ipx.server, 4)) {
1592                                 pn = i;
1593                                 break;
1594                         }
1595                 } else {
1596                         if ( (NetPlayers.players[i].network.appletalk.node == p->player.network.appletalk.node) && (NetPlayers.players[i].network.appletalk.net == p->player.network.appletalk.net) ) {
1597                                 pn = i;
1598                                 break;
1599                         }
1600                 }
1601         }
1602         
1603         if (pn < 0 ) return;
1604
1605         for (i=pn; i<N_players-1; i++ ) {
1606                 if (Network_game_type == IPX_GAME) {
1607                         memcpy( NetPlayers.players[i].network.ipx.node, NetPlayers.players[i+1].network.ipx.node, 6 );
1608                         memcpy( NetPlayers.players[i].network.ipx.server, NetPlayers.players[i+1].network.ipx.server, 4 );
1609                 } else {
1610                         NetPlayers.players[i].network.appletalk.node = NetPlayers.players[i+1].network.appletalk.node;
1611                         NetPlayers.players[i].network.appletalk.net = NetPlayers.players[i+1].network.appletalk.net;
1612                         NetPlayers.players[i].network.appletalk.socket = NetPlayers.players[i+1].network.appletalk.socket;
1613                 }
1614                 memcpy( NetPlayers.players[i].callsign, NetPlayers.players[i+1].callsign, CALLSIGN_LEN+1 );
1615                 NetPlayers.players[i].version_major=NetPlayers.players[i+1].version_major;
1616                 NetPlayers.players[i].version_minor=NetPlayers.players[i+1].version_minor;
1617
1618            NetPlayers.players[i].rank=NetPlayers.players[i+1].rank;
1619                 ClipRank (&NetPlayers.players[i].rank);
1620            network_check_for_old_version(i);    
1621         }
1622                 
1623         N_players--;
1624         Netgame.numplayers = N_players;
1625
1626         // Broadcast new info
1627
1628         network_send_game_info(NULL);
1629
1630 }
1631
1632 void
1633 network_dump_player(ubyte * server, ubyte *node, int why)
1634 {
1635         // Inform player that he was not chosen for the netgame
1636
1637         sequence_packet temp;
1638
1639         temp.type = PID_DUMP;
1640         memcpy(temp.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1);
1641         temp.player.connected = why;
1642         if (Network_game_type == IPX_GAME) {
1643                 #ifndef MACINTOSH
1644                 ipx_send_internetwork_packet_data( (ubyte *)&temp, sizeof(sequence_packet), server, node);
1645                 #else
1646                 send_sequence_packet( temp, server, node, NULL);
1647                 #endif
1648         } else {
1649                 Int3();
1650         }
1651 }
1652
1653 #ifdef MACINTOSH
1654 void network_dump_appletalk_player(ubyte node, ushort net, ubyte socket, int why)
1655 {
1656         sequence_packet temp;
1657
1658         temp.type = PID_DUMP;
1659         memcpy(temp.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1);
1660         temp.player.connected = why;
1661         if (Network_game_type == APPLETALK_GAME) {
1662                 appletalk_send_packet_data( (ubyte *)&temp, sizeof(sequence_packet), node, net, socket );
1663         } else {
1664                 Int3();
1665         }
1666 }
1667 #endif
1668
1669 void
1670 network_send_game_list_request()
1671 {
1672         // Send a broadcast request for game info
1673
1674         sequence_packet me;
1675
1676         mprintf((0, "Sending game_list request.\n"));
1677         me.type = PID_GAME_LIST;
1678         memcpy( me.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1 );
1679
1680         if (Network_game_type == IPX_GAME) {
1681                 memcpy( me.player.network.ipx.node, ipx_get_my_local_address(), 6 );
1682                 memcpy( me.player.network.ipx.server, ipx_get_my_server_address(), 4 );
1683
1684                 #ifndef MACINTOSH       
1685                 ipx_send_broadcast_packet_data( (ubyte *)&me, sizeof(sequence_packet) );
1686                 #else
1687                 send_sequence_packet( me, NULL, NULL, NULL);
1688                 #endif
1689         #ifdef MACINTOSH
1690         } else {
1691                 me.player.network.appletalk.node = appletalk_get_my_node();
1692                 me.player.network.appletalk.net = appletalk_get_my_net();
1693                 me.player.network.appletalk.socket = appletalk_get_my_socket();
1694                 appletalk_send_game_info( (ubyte *)&me, sizeof(sequence_packet), Network_zone_name );
1695         #endif
1696         }
1697 }
1698
1699 void network_send_all_info_request(char type,int which_security)
1700 {
1701         // Send a broadcast request for game info
1702
1703         sequence_packet me;
1704
1705         mprintf((0, "Sending all_info request.\n"));
1706    
1707         me.Security=which_security;
1708         me.type = type;
1709         memcpy( me.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1 );
1710         
1711         if (Network_game_type == IPX_GAME) {
1712                 memcpy( me.player.network.ipx.node, ipx_get_my_local_address(), 6 );
1713                 memcpy( me.player.network.ipx.server, ipx_get_my_server_address(), 4 );
1714
1715                 #ifndef MACINTOSH       
1716                 ipx_send_broadcast_packet_data( (ubyte *)&me, sizeof(sequence_packet) );
1717                 #else
1718                 send_sequence_packet(me, NULL, NULL, NULL);
1719                 #endif
1720         #ifdef MACINTOSH
1721         } else {
1722                 me.player.network.appletalk.node = appletalk_get_my_node();
1723                 me.player.network.appletalk.net = appletalk_get_my_net();
1724                 me.player.network.appletalk.socket = appletalk_get_my_socket();
1725                 appletalk_send_game_info( (ubyte *)&me, sizeof(sequence_packet), Network_zone_name );
1726         #endif
1727         }
1728 }
1729
1730
1731 void
1732 network_update_netgame(void)
1733 {
1734         // Update the netgame struct with current game variables
1735
1736         int i, j;
1737
1738    Netgame.numconnected=0;
1739    for (i=0;i<N_players;i++)
1740          if (Players[i].connected)
1741                 Netgame.numconnected++;
1742
1743 // This is great: D2 1.0 and 1.1 ignore upper part of the game_flags field of
1744 //      the lite_info struct when you're sitting on the join netgame screen.  We can
1745 //      "sneak" Hoard information into this field.  This is better than sending 
1746 //      another packet that could be lost in transit.
1747
1748
1749         if (HoardEquipped())
1750         {
1751                 if (Game_mode & GM_HOARD)
1752                 {
1753                         Netgame.game_flags |=NETGAME_FLAG_HOARD;
1754                         if (Game_mode & GM_TEAM)
1755                                 Netgame.game_flags |=NETGAME_FLAG_TEAM_HOARD;
1756                 }
1757         }
1758  
1759         if (Network_status == NETSTAT_STARTING)
1760                 return;
1761
1762         Netgame.numplayers = N_players;
1763         Netgame.game_status = Network_status;
1764         Netgame.max_numplayers = MaxNumNetPlayers;
1765
1766         for (i = 0; i < MAX_NUM_NET_PLAYERS; i++) 
1767         {
1768                 NetPlayers.players[i].connected = Players[i].connected;
1769                 for(j = 0; j < MAX_NUM_NET_PLAYERS; j++)
1770                         Netgame.kills[i][j] = kill_matrix[i][j];
1771
1772                 Netgame.killed[i] = Players[i].net_killed_total;
1773                 Netgame.player_kills[i] = Players[i].net_kills_total;
1774                 Netgame.player_score[i] = Players[i].score;
1775                 Netgame.player_flags[i] = (Players[i].flags & (PLAYER_FLAGS_BLUE_KEY | PLAYER_FLAGS_RED_KEY | PLAYER_FLAGS_GOLD_KEY));
1776         }
1777         Netgame.team_kills[0] = team_kills[0];
1778         Netgame.team_kills[1] = team_kills[1];
1779         Netgame.levelnum = Current_level_num;
1780
1781  
1782 }
1783
1784 void
1785 network_send_endlevel_sub(int player_num)
1786 {
1787         endlevel_info end;
1788         int i, j;
1789
1790         // Send an endlevel packet for a player
1791         end.type                = PID_ENDLEVEL;
1792         end.player_num = player_num;
1793         end.connected   = Players[player_num].connected;
1794         end.kills               = INTEL_SHORT(Players[player_num].net_kills_total);
1795         end.killed              = INTEL_SHORT(Players[player_num].net_killed_total);
1796         memcpy(end.kill_matrix, kill_matrix[player_num], MAX_PLAYERS*sizeof(short));
1797 #ifdef MACINTOSH
1798         for (i = 0; i < MAX_PLAYERS; i++)
1799                 for (j = 0; j < MAX_PLAYERS; j++)
1800                         end.kill_matrix[i][j] = INTEL_SHORT(end.kill_matrix[i][j]);
1801 #else
1802         j = j;          // to satisfy compiler
1803 #endif
1804
1805         if (Players[player_num].connected == 1) // Still playing
1806         {
1807                 Assert(Control_center_destroyed);
1808                 end.seconds_left = Countdown_seconds_left;
1809         }
1810
1811 //      mprintf((0, "Sending endlevel packet.\n"));
1812
1813         for (i = 0; i < N_players; i++)
1814         {       
1815                 if ((i != Player_num) && (i!=player_num) && (Players[i].connected)) {
1816                   if (Players[i].connected==1)
1817                          network_send_endlevel_short_sub(player_num,i);
1818                   else {
1819                         if (Network_game_type == IPX_GAME)
1820                                 ipx_send_packet_data((ubyte *)&end, sizeof(endlevel_info), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node,Players[i].net_address);
1821                         #ifdef MACINTOSH
1822                         else
1823                                 appletalk_send_packet_data( (ubyte *)&end, sizeof(endlevel_info), NetPlayers.players[i].network.appletalk.node,
1824                                                                                         NetPlayers.players[i].network.appletalk.net,
1825                                                                                         NetPlayers.players[i].network.appletalk.socket);
1826                         #endif
1827                         }       
1828                 }
1829         }
1830 }
1831
1832 void
1833 network_send_endlevel_packet(void)
1834 {
1835         // Send an updated endlevel status to other hosts
1836
1837         network_send_endlevel_sub(Player_num);
1838 }
1839
1840
1841 void
1842 network_send_endlevel_short_sub(int from_player_num,int to_player)
1843 {
1844         endlevel_info_short end;
1845
1846         // Send an endlevel packet for a player
1847         end.type                = PID_ENDLEVEL_SHORT;
1848         end.player_num = from_player_num;
1849         end.connected   = Players[from_player_num].connected;
1850         end.seconds_left = Countdown_seconds_left;
1851
1852
1853         if (Players[from_player_num].connected == 1) // Still playing
1854         {
1855                 Assert(Control_center_destroyed);
1856         }
1857
1858         if ((to_player != Player_num) && (to_player!=from_player_num) && (Players[to_player].connected))
1859         {
1860                 mprintf((0, "Sending short endlevel packet to %s.\n",Players[to_player].callsign));
1861                 if (Network_game_type == IPX_GAME)
1862                         ipx_send_packet_data((ubyte *)&end, sizeof(endlevel_info_short), NetPlayers.players[to_player].network.ipx.server, NetPlayers.players[to_player].network.ipx.node,Players[to_player].net_address);
1863                 #ifdef MACINTOSH
1864                 else
1865                         appletalk_send_packet_data( (ubyte *)&end, sizeof(endlevel_info_short), NetPlayers.players[to_player].network.appletalk.node,
1866                                                                                 NetPlayers.players[to_player].network.appletalk.net,
1867                                                                                 NetPlayers.players[to_player].network.appletalk.socket);
1868                 #endif
1869         }
1870 }
1871
1872 extern fix ThisLevelTime;
1873
1874 void
1875 network_send_game_info(sequence_packet *their)
1876 {
1877         // Send game info to someone who requested it
1878
1879         char old_type, old_status;
1880    fix timevar;
1881    int i;
1882
1883         mprintf((0, "Sending game info.\n"));
1884
1885         network_update_netgame(); // Update the values in the netgame struct
1886
1887         old_type = Netgame.type;
1888         old_status = Netgame.game_status;
1889
1890         Netgame.type = PID_GAME_INFO;
1891    NetPlayers.type = PID_PLAYERSINFO;
1892
1893    NetPlayers.Security=Netgame.Security;
1894         Netgame.version_major=Version_major;
1895         Netgame.version_minor=Version_minor;
1896
1897         if (Endlevel_sequence || Control_center_destroyed)
1898                 Netgame.game_status = NETSTAT_ENDLEVEL;
1899
1900         if (Netgame.PlayTimeAllowed)
1901     {
1902                 timevar=i2f (Netgame.PlayTimeAllowed*5*60);
1903                 i=f2i(timevar-ThisLevelTime);
1904                 if (i<30)
1905                         Netgame.game_status=NETSTAT_ENDLEVEL;
1906         }       
1907
1908         if (!their) {
1909                 if (Network_game_type == IPX_GAME) {
1910                         #ifndef MACINTOSH
1911                         ipx_send_broadcast_packet_data((ubyte *)&Netgame, sizeof(netgame_info));
1912                         ipx_send_broadcast_packet_data((ubyte *)&NetPlayers,sizeof(AllNetPlayers_info));
1913                         #else
1914                         send_netgame_packet(NULL, NULL, NULL, 0);               // server == NULL says to broadcast packet
1915                         send_netplayers_packet(NULL, NULL);
1916                         #endif
1917                 } // nothing to do for appletalk games I think....
1918         } else {
1919                 if (Network_game_type == IPX_GAME) {
1920                         #ifndef MACINTOSH        
1921                     ipx_send_internetwork_packet_data((ubyte *)&Netgame, sizeof(netgame_info), their->player.network.ipx.server, their->player.network.ipx.node);
1922                         ipx_send_internetwork_packet_data((ubyte *)&NetPlayers,sizeof(AllNetPlayers_info),their->player.network.ipx.server,their->player.network.ipx.node);
1923                         #else
1924                         send_netgame_packet(their->player.network.ipx.server, their->player.network.ipx.node, NULL, 0);
1925                         send_netplayers_packet(their->player.network.ipx.server, their->player.network.ipx.node);
1926                         #endif
1927                 #ifdef MACINTOSH
1928                 } else {
1929                         appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), their->player.network.appletalk.node,
1930                                                                                 their->player.network.appletalk.net, their->player.network.appletalk.socket );
1931                         appletalk_send_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), their->player.network.appletalk.node,
1932                                                                                 their->player.network.appletalk.net, their->player.network.appletalk.socket );
1933                 #endif
1934                 }
1935         }  
1936
1937         Netgame.type = old_type;
1938         Netgame.game_status = old_status;
1939 }       
1940
1941 void network_send_lite_info(sequence_packet *their)
1942 {
1943         // Send game info to someone who requested it
1944
1945         char old_type, old_status,oldstatus;
1946
1947         mprintf((0, "Sending lite game info.\n"));
1948
1949         network_update_netgame(); // Update the values in the netgame struct
1950
1951         old_type = Netgame.type;
1952         old_status = Netgame.game_status;
1953
1954         Netgame.type = PID_LITE_INFO;
1955
1956         if (Endlevel_sequence || Control_center_destroyed)
1957                 Netgame.game_status = NETSTAT_ENDLEVEL;
1958
1959 // If hoard mode, make this game look closed even if it isn't
1960    if (HoardEquipped())
1961         {
1962                 if (Game_mode & GM_HOARD)
1963                 {
1964                         oldstatus=Netgame.game_status;
1965                         Netgame.game_status=NETSTAT_ENDLEVEL;
1966                         Netgame.gamemode=NETGAME_CAPTURE_FLAG;
1967                         if (oldstatus==NETSTAT_ENDLEVEL)
1968                                 Netgame.game_flags|=NETGAME_FLAG_REALLY_ENDLEVEL;
1969                         if (oldstatus==NETSTAT_STARTING)
1970                                 Netgame.game_flags|=NETGAME_FLAG_REALLY_FORMING;
1971                 }
1972         }
1973
1974         if (!their) {
1975                 if (Network_game_type == IPX_GAME) {
1976                         #ifndef MACINTOSH
1977                         ipx_send_broadcast_packet_data((ubyte *)&Netgame, sizeof(lite_info));
1978                         #else
1979                         send_netgame_packet(NULL, NULL, NULL, 1);                       // server == NULL says broadcast
1980                         #endif
1981                 }               // nothing to do for appletalk I think....
1982         } else {
1983                 if (Network_game_type == IPX_GAME) {
1984                         #ifndef MACINTOSH
1985                     ipx_send_internetwork_packet_data((ubyte *)&Netgame, sizeof(lite_info), their->player.network.ipx.server, their->player.network.ipx.node);
1986                         #else
1987                         send_netgame_packet(their->player.network.ipx.server, their->player.network.ipx.node, NULL, 1);
1988                         #endif
1989                 #ifdef MACINTOSH
1990                 } else {
1991                         appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(lite_info), their->player.network.appletalk.node,
1992                                                                                 their->player.network.appletalk.net, their->player.network.appletalk.socket );
1993                 #endif
1994                 }
1995         }  
1996
1997         //  Restore the pre-hoard mode
1998         if (HoardEquipped())
1999         {
2000                 if (Game_mode & GM_HOARD)
2001                 {
2002                         if (!(Game_mode & GM_TEAM))
2003                            Netgame.gamemode=NETGAME_HOARD;
2004                         else
2005                            Netgame.gamemode=NETGAME_TEAM_HOARD;
2006                         Netgame.game_flags&=~NETGAME_FLAG_REALLY_ENDLEVEL;
2007                         Netgame.game_flags&=~NETGAME_FLAG_REALLY_FORMING;
2008                         Netgame.game_flags&=~NETGAME_FLAG_TEAM_HOARD;
2009                 }
2010         }
2011
2012         Netgame.type = old_type;
2013         Netgame.game_status = old_status;
2014
2015 }       
2016
2017 void network_send_netgame_update()
2018 {
2019         // Send game info to someone who requested it
2020
2021         char old_type, old_status;
2022         int i;
2023
2024         mprintf((0, "Sending updated game info.\n"));
2025
2026         network_update_netgame(); // Update the values in the netgame struct
2027
2028         old_type = Netgame.type;
2029         old_status = Netgame.game_status;
2030
2031         Netgame.type = PID_GAME_UPDATE;
2032
2033         if (Endlevel_sequence || Control_center_destroyed)
2034                 Netgame.game_status = NETSTAT_ENDLEVEL;
2035  
2036         for (i=0; i<N_players; i++ )    {
2037                 if ( (Players[i].connected) && (i!=Player_num ) )       {
2038                         if (Network_game_type == IPX_GAME) {
2039                                 #ifndef MACINTOSH
2040                                 ipx_send_packet_data( (ubyte *)&Netgame, sizeof(lite_info), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node,Players[i].net_address );
2041                                 #else
2042                                 send_netgame_packet(NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, Players[i].net_address, 0);
2043                                 #endif
2044                         #ifdef MACINTOSH
2045                         } else {
2046                                 appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(lite_info), NetPlayers.players[i].network.appletalk.node,
2047                                                                                         NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket );
2048                         #endif
2049                         }
2050                 }
2051         }
2052
2053         Netgame.type = old_type;
2054         Netgame.game_status = old_status;
2055 }       
2056                           
2057 int network_send_request(void)
2058 {
2059         // Send a request to join a game 'Netgame'.  Returns 0 if we can join this
2060         // game, non-zero if there is some problem.
2061         int i;
2062
2063         if (Netgame.numplayers < 1)
2064          return 1;
2065
2066         for (i = 0; i < MAX_NUM_NET_PLAYERS; i++)
2067           if (NetPlayers.players[i].connected)
2068               break;
2069
2070         Assert(i < MAX_NUM_NET_PLAYERS);
2071
2072         mprintf((0, "Sending game enroll request to player %d (%s). Serv=%x Node=%x Level=%d\n", i, Players[i].callsign,NetPlayers.players[i].network.ipx.server,NetPlayers.players[i].network.ipx.node,Netgame.levelnum));
2073
2074 //      segments_checksum = netmisc_calc_checksum( Segments, sizeof(segment)*(Highest_segment_index+1) );       
2075
2076         My_Seq.type = PID_REQUEST;
2077         My_Seq.player.connected = Current_level_num;
2078
2079         if (Network_game_type == IPX_GAME) {
2080                 #ifndef MACINTOSH
2081                 ipx_send_internetwork_packet_data((ubyte *)&My_Seq, sizeof(sequence_packet), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node);
2082                 #else
2083                 send_sequence_packet(My_Seq, NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, NULL);
2084                 #endif
2085         #ifdef MACINTOSH
2086         } else {
2087                 appletalk_send_packet_data( (ubyte *)&My_Seq, sizeof(sequence_packet), NetPlayers.players[i].network.appletalk.node,
2088                                                                         NetPlayers.players[i].network.appletalk.net,
2089                                                                         NetPlayers.players[i].network.appletalk.socket);
2090         #endif
2091         }
2092
2093         return i;
2094 }
2095
2096 int SecurityCheck=0;
2097         
2098 void network_process_gameinfo(ubyte *data)
2099 {
2100 #ifdef MACINTOSH
2101         netgame_info tmp_info;
2102 #endif
2103         int i, j;
2104         netgame_info *new = (netgame_info *)data;
2105
2106 #ifdef MACINTOSH
2107         if (Network_game_type == IPX_GAME)  {
2108                 receive_netgame_packet(data, &tmp_info, 0);                     // get correctly aligned structure
2109                 new = &tmp_info;
2110         }
2111 #endif
2112         
2113
2114    WaitingForPlayerInfo=0;
2115
2116    if (new->Security !=TempPlayersInfo->Security)
2117     {
2118      Int3();     // Get Jason
2119      return;     // If this first half doesn't go with the second half
2120     }
2121
2122         Network_games_changed = 1;
2123
2124    //mprintf((0, "Got game data for game %s.\n", new->game_name));
2125
2126    Assert (TempPlayersInfo!=NULL);
2127
2128         for (i = 0; i < num_active_games; i++)
2129          {
2130           //mprintf ((0,"GAMEINFO: Game %d is %s!\n",i,Active_games[i].game_name));
2131           
2132           if (!stricmp(Active_games[i].game_name, new->game_name) && 
2133                                   Active_games[i].Security==new->Security)
2134                break;
2135          }
2136
2137         if (i == MAX_ACTIVE_NETGAMES)
2138         {
2139                 mprintf((0, "Too many netgames.\n"));
2140                 return;
2141         }
2142         
2143 // MWA  memcpy(&Active_games[i], data, sizeof(netgame_info));
2144         memcpy(&Active_games[i], (ubyte *)new, sizeof(netgame_info));
2145         memcpy (&ActiveNetPlayers[i],TempPlayersInfo,sizeof(AllNetPlayers_info));
2146
2147    if (SecurityCheck)
2148          if (Active_games[i].Security==SecurityCheck)
2149                 SecurityCheck=-1;
2150
2151         //mprintf ((0,"Recieved %d unique games...\n",i));
2152
2153         if (i == num_active_games)
2154                 num_active_games++;
2155
2156         if (Active_games[i].numplayers == 0)
2157         {
2158          mprintf ((0,"DELETING THIS GAME!!!\n"));       
2159                 // Delete this game
2160                 for (j = i; j < num_active_games-1; j++)
2161         {
2162              memcpy(&Active_games[j], &Active_games[j+1], sizeof(netgame_info));
2163            memcpy (&ActiveNetPlayers[j],&ActiveNetPlayers[j+1],sizeof(AllNetPlayers_info));
2164         }
2165                 num_active_games--;
2166                 SecurityCheck=0;
2167         }
2168 }
2169
2170 void network_process_lite_info(ubyte *data)
2171 {
2172 #ifdef MACINTOSH
2173         lite_info tmp_info;
2174 #endif
2175         int i, j;
2176         lite_info *new = (lite_info *)data;
2177
2178 #ifdef MACINTOSH
2179         if (Network_game_type == IPX_GAME) {
2180                 receive_netgame_packet(data, (netgame_info *)&tmp_info, 1);
2181                 new = &tmp_info;
2182         }
2183 #endif
2184
2185         Network_games_changed = 1;
2186
2187    //mprintf((0, "Got game data for game %s.\n", new->game_name));
2188
2189         for (i = 0; i < num_active_games; i++)
2190     {
2191       //mprintf ((0,"GAMEINFO: Game %d is %s!\n",i,Active_games[i].game_name));
2192       
2193       if ((!stricmp(Active_games[i].game_name, new->game_name)) && 
2194                          Active_games[i].Security==new->Security)
2195                                 break;
2196     }
2197
2198         if (i == MAX_ACTIVE_NETGAMES)
2199         {
2200                 mprintf((0, "Too many netgames.\n"));
2201                 return;
2202         }
2203         
2204         memcpy(&Active_games[i], (ubyte *)new, sizeof(lite_info));
2205
2206 // See if this is really a Hoard game
2207 // If so, adjust all the data accordingly
2208
2209         if (HoardEquipped())
2210         {
2211                 if (Active_games[i].game_flags & NETGAME_FLAG_HOARD)
2212                 {
2213                         Active_games[i].gamemode=NETGAME_HOARD;                                   
2214                         Active_games[i].game_status=NETSTAT_PLAYING;
2215                         
2216                         if (Active_games[i].game_flags & NETGAME_FLAG_TEAM_HOARD)
2217                                 Active_games[i].gamemode=NETGAME_TEAM_HOARD;                                      
2218                         if (Active_games[i].game_flags & NETGAME_FLAG_REALLY_ENDLEVEL)
2219                                 Active_games[i].game_status=NETSTAT_ENDLEVEL;
2220                         if (Active_games[i].game_flags & NETGAME_FLAG_REALLY_FORMING)
2221                                 Active_games[i].game_status=NETSTAT_STARTING;
2222                 }
2223         }
2224
2225    //mprintf ((0,"Recieved %d unique games...\n",i));
2226
2227         if (i == num_active_games)
2228                 num_active_games++;
2229
2230         if (Active_games[i].numplayers == 0)
2231     {
2232            mprintf ((0,"DELETING THIS GAME!!!\n"));     
2233                 // Delete this game
2234                 for (j = i; j < num_active_games-1; j++)
2235         {
2236              memcpy(&Active_games[j], &Active_games[j+1], sizeof(netgame_info));
2237         }
2238                 num_active_games--;
2239         }
2240 }
2241
2242 void network_process_dump(sequence_packet *their)
2243 {
2244         // Our request for join was denied.  Tell the user why.
2245
2246    char temp[40];
2247    int i;
2248  
2249         mprintf((0, "Dumped by player %s, type %d.\n", their->player.callsign, their->player.connected));
2250
2251    if (their->player.connected!=7)
2252                 nm_messagebox(NULL, 1, TXT_OK, NET_DUMP_STRINGS(their->player.connected));
2253         else
2254                 {
2255                  for (i=0;i<N_players;i++)
2256                         if (!stricmp (their->player.callsign,Players[i].callsign))
2257                         {
2258                                 if (i!=network_who_is_master())
2259                                 {
2260                                         HUD_init_message ("%s attempted to kick you out.",their->player.callsign);
2261                                 }
2262                                 else
2263                                 {
2264                                   sprintf (temp,"%s has kicked you out!",their->player.callsign);
2265                                   nm_messagebox(NULL, 1, TXT_OK, &temp);
2266                             if (Network_status==NETSTAT_PLAYING)
2267                                   {
2268                                         IWasKicked=1;
2269                                         multi_leave_game();     
2270                                   }
2271                                  else
2272                                         Network_status = NETSTAT_MENU;
2273                       }
2274                    }
2275                 }
2276 }       
2277 void network_process_request(sequence_packet *their)
2278 {
2279         // Player is ready to receieve a sync packet
2280         int i;
2281
2282         mprintf((0, "Player %s ready for sync.\n", their->player.callsign));
2283
2284         for (i = 0; i < N_players; i++) {
2285                 if (Network_game_type == IPX_GAME) {
2286                         if (!memcmp(their->player.network.ipx.server, NetPlayers.players[i].network.ipx.server, 4) && !memcmp(their->player.network.ipx.node, NetPlayers.players[i].network.ipx.node, 6) && (!stricmp(their->player.callsign, NetPlayers.players[i].callsign))) {
2287                                 Players[i].connected = 1;
2288                                 break;
2289                         }
2290                 } else {
2291                         if ( (their->player.network.appletalk.node == NetPlayers.players[i].network.appletalk.node) && (their->player.network.appletalk.net == NetPlayers.players[i].network.appletalk.net) && (!stricmp(their->player.callsign, NetPlayers.players[i].callsign)) ) {
2292                                 Players[i].connected = 1;
2293                                 break;
2294                         }
2295                 }
2296         }                       
2297 }
2298
2299 #define REFUSE_INTERVAL F1_0 * 8
2300 extern void multi_reset_object_texture (object *);
2301
2302 void network_process_packet(ubyte *data, int length )
2303 {
2304 #ifdef MACINTOSH
2305         sequence_packet tmp_packet;
2306 #endif
2307         sequence_packet *their = (sequence_packet *)data;
2308
2309 #ifdef MACINTOSH
2310         if (Network_game_type == IPX_GAME) {
2311                 receive_sequence_packet(data, &tmp_packet);
2312                 their = &tmp_packet;                                            // reassign their to point to correctly alinged structure
2313         }
2314 #endif
2315
2316    //mprintf( (0, "Got packet of length %d, type %d\n", length, their->type ));
2317         
2318 //      if ( length < sizeof(sequence_packet) ) return;
2319
2320         length = length;
2321
2322         switch( data[0] )       {
2323         
2324          case PID_GAME_INFO:            // Jason L. says we can safely ignore this type.
2325                 break;
2326         
2327      case PID_PLAYERSINFO:
2328
2329                 mprintf ((0,"Got a PID_PLAYERSINFO!\n"));
2330
2331                 if (Network_status==NETSTAT_WAITING)
2332                 {
2333 #ifndef MACINTOSH                        
2334                         memcpy (&TempPlayersBase,data,sizeof(AllNetPlayers_info));
2335 #else
2336                         if (Network_game_type == IPX_GAME)
2337                                 receive_netplayers_packet(data, &TempPlayersBase);
2338                         else
2339                                 memcpy (&TempPlayersBase,data,sizeof(AllNetPlayers_info));
2340 #endif
2341
2342                         if (TempPlayersBase.Security!=Netgame.Security)
2343                          {
2344                           mprintf ((0,"Bad security for PLAYERSINFO\n"));
2345                           break;
2346                          }      
2347                 
2348                         mprintf ((0,"Got a waiting PID_PLAYERSINFO!\n"));
2349                         if (length!=ALLNETPLAYERSINFO_SIZE)
2350                         {
2351                                 mprintf ((0,"Invalid size for netplayers packet!\n"));
2352                                 return;
2353                         }
2354
2355                         TempPlayersInfo=&TempPlayersBase;
2356                         WaitingForPlayerInfo=0;
2357                         NetSecurityNum=TempPlayersInfo->Security;
2358                         NetSecurityFlag=NETSECURITY_WAIT_FOR_SYNC;
2359            }
2360
2361      break;
2362
2363    case PID_LITE_INFO:
2364
2365          if (length != LITE_INFO_SIZE)
2366                  {
2367                   mprintf ((0,"WARNING! Recieved invalid size for PID_LITE_INFO\n"));
2368                   return;
2369                  }
2370  
2371          if (Network_status == NETSTAT_BROWSING)
2372                 network_process_lite_info (data);
2373     break;
2374
2375         case PID_GAME_LIST:
2376                 // Someone wants a list of games
2377
2378            if (length != SEQUENCE_PACKET_SIZE)
2379                  {
2380                   mprintf ((0,"WARNING! Recieved invalid size for PID_GAME_LIST\n"));
2381                   return;
2382                  }
2383                         
2384                 mprintf((0, "Got a PID_GAME_LIST!\n"));
2385                 if ((Network_status == NETSTAT_PLAYING) || (Network_status == NETSTAT_STARTING) || (Network_status == NETSTAT_ENDLEVEL))
2386                         if (network_i_am_master())
2387                                 network_send_lite_info(their);
2388                 break;
2389
2390
2391         case PID_SEND_ALL_GAMEINFO:
2392
2393            if (length != SEQUENCE_PACKET_SIZE)
2394                  {
2395                   mprintf ((0,"WARNING! Recieved invalid size for PID_SEND_ALL_GAMEINFO\n"));
2396                   return;
2397                  }
2398
2399                 if ((Network_status == NETSTAT_PLAYING) || (Network_status == NETSTAT_STARTING) || (Network_status == NETSTAT_ENDLEVEL))
2400                         if (network_i_am_master() && their->Security==Netgame.Security)
2401                                 network_send_game_info(their);
2402                 break;
2403         
2404         case PID_ADDPLAYER:
2405
2406                 mprintf( (0, "Got NEWPLAYER message from %s.\n", their->player.callsign));
2407                 
2408            if (length != SEQUENCE_PACKET_SIZE)
2409                  {
2410                   mprintf ((0,"WARNING! Recieved invalid size for PID_ADDPLAYER\n"));
2411                   return;
2412                  }
2413                 mprintf( (0, "Got NEWPLAYER message from %s.\n", their->player.callsign));
2414                 network_new_player(their);
2415                 break;                  
2416         case PID_REQUEST:
2417            if (length != SEQUENCE_PACKET_SIZE)
2418                  {
2419                   mprintf ((0,"WARNING! Recieved invalid size for PID_REQUEST\n"));
2420                   return;
2421                  }
2422
2423                 mprintf( (0, "Got REQUEST from '%s'\n", their->player.callsign ));
2424                 if (Network_status == NETSTAT_STARTING) 
2425                 {
2426                         // Someone wants to join our game!
2427                         network_add_player(their);
2428                 }
2429                 else if (Network_status == NETSTAT_WAITING)
2430                 {
2431                         // Someone is ready to recieve a sync packet
2432                         network_process_request(their);
2433                 }
2434                 else if (Network_status == NETSTAT_PLAYING)
2435                 {
2436                         // Someone wants to join a game in progress!
2437                         if (Netgame.RefusePlayers)
2438                 DoRefuseStuff (their);
2439                    else 
2440                                 network_welcome_player(their);
2441                 }
2442                 break;
2443         case PID_DUMP:  
2444
2445            if (length != SEQUENCE_PACKET_SIZE)
2446                  {
2447                   mprintf ((0,"WARNING! Recieved invalid size for PID_DUMP\n"));
2448                   return;
2449                  }
2450   
2451                 if (Network_status == NETSTAT_WAITING || Network_status==NETSTAT_PLAYING )
2452                         network_process_dump(their);
2453                 break;
2454         case PID_QUIT_JOINING:
2455
2456            if (length != SEQUENCE_PACKET_SIZE)
2457                  {
2458                   mprintf ((0,"WARNING! Recieved invalid size for PID_QUIT_JOINING\n"));
2459                   return;
2460                  }
2461                 if (Network_status == NETSTAT_STARTING)
2462                         network_remove_player( their );
2463                 else if ((Network_status == NETSTAT_PLAYING) && (Network_send_objects))
2464                         network_stop_resync( their );
2465                 break;
2466         case PID_SYNC:  
2467
2468                 mprintf ((0,"Got a sync packet! Network_status=%d\n",NETSTAT_WAITING));
2469
2470                 if (Network_status == NETSTAT_WAITING)  {
2471
2472 #ifndef MACINTOSH
2473                         memcpy((ubyte *)&(TempNetInfo), data, sizeof(netgame_info));
2474 #else
2475                         if (Network_game_type == IPX_GAME)
2476                                 receive_netgame_packet(data, &TempNetInfo, 0);
2477                         else
2478                                 memcpy((ubyte *)&(TempNetInfo), data, sizeof(netgame_info));
2479 #endif
2480
2481                         if (TempNetInfo.Security!=Netgame.Security)
2482                          {
2483                                 mprintf ((0,"Bad security on sync packet.\n"));
2484                                 break;
2485                          }
2486
2487                         if (NetSecurityFlag==NETSECURITY_WAIT_FOR_SYNC)
2488                          {
2489                           if (TempNetInfo.Security==TempPlayersInfo->Security)
2490                           {
2491                                 network_read_sync_packet (&TempNetInfo,0);
2492                                 NetSecurityFlag=0;
2493                                 NetSecurityNum=0;
2494                           }     
2495                          }
2496                         else
2497                          {      
2498                         NetSecurityFlag=NETSECURITY_WAIT_FOR_PLAYERS;
2499                         NetSecurityNum=TempNetInfo.Security;
2500
2501                         if ( network_wait_for_playerinfo())
2502                                 network_read_sync_packet((netgame_info *)data,0);
2503  
2504                         NetSecurityFlag=0;
2505                         NetSecurityNum=0;
2506                          }
2507                 }
2508                 break;
2509
2510         case PID_PDATA: 
2511                 if ((Game_mode&GM_NETWORK) && ((Network_status == NETSTAT_PLAYING)||(Network_status == NETSTAT_ENDLEVEL) || Network_status==NETSTAT_WAITING)) { 
2512                         network_process_pdata((char *)data);
2513                 }
2514                 break;
2515    case PID_NAKED_PDATA:
2516                 if ((Game_mode&GM_NETWORK) && ((Network_status == NETSTAT_PLAYING)||(Network_status == NETSTAT_ENDLEVEL) || Network_status==NETSTAT_WAITING)) 
2517                         network_process_naked_pdata((char *)data,length);
2518            break;
2519
2520         case PID_OBJECT_DATA:
2521                 if (Network_status == NETSTAT_WAITING) 
2522                         network_read_object_packet(data);
2523                 break;
2524         case PID_ENDLEVEL:
2525                 if ((Network_status == NETSTAT_ENDLEVEL) || (Network_status == NETSTAT_PLAYING))
2526                         network_read_endlevel_packet(data);
2527                 else
2528                         mprintf((0, "Junked endlevel packet.\n"));
2529                 break;
2530         case PID_ENDLEVEL_SHORT:
2531                 if ((Network_status == NETSTAT_ENDLEVEL) || (Network_status == NETSTAT_PLAYING))
2532                         network_read_endlevel_short_packet(data);
2533                 else
2534                         mprintf((0, "Junked short endlevel packet!\n"));
2535                 break;
2536
2537         case PID_GAME_UPDATE:
2538                 mprintf ((0,"Got a GAME_UPDATE!\n"));
2539                         
2540                 if (Network_status==NETSTAT_PLAYING)
2541                  {
2542 #ifndef MACINTOSH
2543                         memcpy((ubyte *)&TempNetInfo, data, sizeof(lite_info) );
2544 #else
2545                         if (Network_game_type == IPX_GAME)
2546                                 receive_netgame_packet(data, &TempNetInfo, 1);
2547                         else
2548                                 memcpy((ubyte *)&TempNetInfo, data, sizeof(lite_info) );
2549 #endif
2550                   if (TempNetInfo.Security==Netgame.Security)
2551                          memcpy (&Netgame,(ubyte *)&TempNetInfo,sizeof(lite_info));
2552                  }
2553                 if (Game_mode & GM_TEAM)
2554                  {
2555                         int i;
2556
2557                         for (i=0;i<N_players;i++)
2558                          if (Players[i].connected)
2559                         multi_reset_object_texture (&Objects[Players[i].objnum]);
2560                     reset_cockpit();
2561                  }
2562            break;
2563    case PID_PING_SEND:
2564                 network_ping (PID_PING_RETURN,data[1]);
2565                 break;
2566    case PID_PING_RETURN:
2567                 network_handle_ping_return(data[1]);  // data[1] is player who told us of their ping time
2568                 break;
2569    case PID_NAMES_RETURN:
2570                 if (Network_status==NETSTAT_BROWSING && NamesInfoSecurity!=-1)
2571                   network_process_names_return (data);
2572                 break;
2573         case PID_GAME_PLAYERS:
2574                 // Someone wants a list of players in this game
2575
2576            if (length != SEQUENCE_PACKET_SIZE)
2577                  {
2578                   mprintf ((0,"WARNING! Recieved invalid size for PID_GAME_PLAYERS\n"));
2579                   return;
2580                  }
2581                         
2582                 mprintf((0, "Got a PID_GAME_PLAYERS!\n"));
2583                 if ((Network_status == NETSTAT_PLAYING) || (Network_status == NETSTAT_STARTING) || (Network_status == NETSTAT_ENDLEVEL))
2584                         if (network_i_am_master() && their->Security==Netgame.Security)
2585                                 network_send_player_names(their);
2586                 break;
2587
2588         default:
2589                 mprintf((0, "Ignoring invalid packet type.\n"));
2590                 Int3(); // Invalid network packet type, see ROB
2591            break;
2592         }
2593 }
2594
2595 #ifndef NDEBUG
2596 void dump_segments()
2597 {
2598         FILE * fp;
2599
2600         fp = fopen( "TEST.DMP", "wb" );
2601         fwrite( Segments, sizeof(segment)*(Highest_segment_index+1),1, fp );    
2602         fclose(fp);
2603         mprintf( (0, "SS=%d\n", sizeof(segment) ));
2604 }
2605 #endif
2606
2607
2608 void
2609 network_read_endlevel_packet( ubyte *data )
2610 {
2611         // Special packet for end of level syncing
2612 #ifdef MACINTOSH
2613         int i, j;
2614 #endif
2615         int playernum;
2616         endlevel_info *end;     
2617
2618         end = (endlevel_info *)data;
2619 #ifdef MACINTOSH
2620         for (i = 0; i < MAX_PLAYERS; i++)
2621                 for (j = 0; j < MAX_PLAYERS; j++)
2622                         end->kill_matrix[i][j] = INTEL_SHORT(end->kill_matrix[i][j]);
2623         end->kills = INTEL_SHORT(end->kills);
2624         end->killed = INTEL_SHORT(end->killed);
2625 #endif
2626
2627         playernum = end->player_num;
2628         
2629         Assert(playernum != Player_num);
2630     
2631         if (playernum>=N_players)
2632          {              
2633                 Int3(); // weird, but it an happen in a coop restore game
2634                 return; // if it happens in a coop restore, don't worry about it
2635          }
2636
2637         if ((Network_status == NETSTAT_PLAYING) && (end->connected != 0))
2638                 return; // Only accept disconnect packets if we're not out of the level yet
2639
2640         Players[playernum].connected = end->connected;
2641         memcpy(&kill_matrix[playernum][0], end->kill_matrix, MAX_PLAYERS*sizeof(short));
2642         Players[playernum].net_kills_total = end->kills;
2643         Players[playernum].net_killed_total = end->killed;
2644
2645         if ((Players[playernum].connected == 1) && (end->seconds_left < Countdown_seconds_left))
2646                 Countdown_seconds_left = end->seconds_left;
2647
2648         LastPacketTime[playernum] = timer_get_approx_seconds();
2649
2650 //      mprintf((0, "Got endlevel packet from player %d.\n", playernum));
2651 }
2652
2653 void
2654 network_read_endlevel_short_packet( ubyte *data )
2655 {
2656         // Special packet for end of level syncing
2657
2658         int playernum;
2659         endlevel_info_short *end;       
2660
2661         end = (endlevel_info_short *)data;
2662
2663         playernum = end->player_num;
2664         
2665         Assert(playernum != Player_num);
2666     
2667         if (playernum>=N_players)
2668          {              
2669                 Int3(); // weird, but it can happen in a coop restore game
2670                 return; // if it happens in a coop restore, don't worry about it
2671          }
2672
2673         if ((Network_status == NETSTAT_PLAYING) && (end->connected != 0))
2674          {
2675                 //mprintf ((0,"Returning early for short_endlevel\n"));
2676                 return; // Only accept disconnect packets if we're not out of the level yet
2677          }
2678
2679         Players[playernum].connected = end->connected;
2680
2681         if ((Players[playernum].connected == 1) && (end->seconds_left < Countdown_seconds_left))
2682                 Countdown_seconds_left = end->seconds_left;
2683
2684         LastPacketTime[playernum] = timer_get_approx_seconds();
2685 }
2686
2687
2688 void
2689 network_pack_objects(void)
2690 {
2691         // Switching modes, pack the object array
2692
2693         special_reset_objects();
2694 }                               
2695
2696 int
2697 network_verify_objects(int remote, int local)
2698 {
2699         int i;
2700         int nplayers, got_controlcen=0;
2701
2702    mprintf ((0,"NETWORK:remote=%d local=%d\n",remote,local));
2703
2704         if ((remote-local) > 10)
2705                 return(-1);
2706
2707         if (Game_mode & GM_MULTI_ROBOTS)
2708                 got_controlcen = 1;
2709
2710         nplayers = 0;
2711
2712         for (i = 0; i <= Highest_object_index; i++)
2713         {
2714                 if ((Objects[i].type == OBJ_PLAYER) || (Objects[i].type == OBJ_GHOST))
2715                         nplayers++;
2716                 if (Objects[i].type == OBJ_CNTRLCEN)
2717                         got_controlcen=1;
2718         }
2719
2720     if (got_controlcen && (MaxNumNetPlayers<=nplayers))
2721                 return(0);
2722
2723         return(1);
2724 }
2725         
2726 void
2727 network_read_object_packet( ubyte *data )
2728 {
2729         // Object from another net player we need to sync with
2730
2731         short objnum, remote_objnum;
2732         byte obj_owner;
2733         int segnum, i;
2734         object *obj;
2735
2736         static int my_pnum = 0;
2737         static int mode = 0;
2738         static int object_count = 0;
2739         static int frame_num = 0;
2740         int nobj = data[1];
2741         int loc = 3;
2742         int remote_frame_num = data[2];
2743         
2744         frame_num++;
2745
2746 //      mprintf((0, "Object packet %d (remote #%d) contains %d objects.\n", frame_num, remote_frame_num, nobj));
2747
2748         for (i = 0; i < nobj; i++)
2749         {
2750                 objnum = INTEL_SHORT( *(short *)(data+loc) );                   loc += 2;
2751                 obj_owner = data[loc];                                                                  loc += 1;
2752                 remote_objnum = INTEL_SHORT( *(short *)(data+loc) );    loc += 2;
2753
2754                 if (objnum == -1) 
2755                 {
2756                         // Clear object array
2757                         mprintf((0, "Clearing object array.\n"));
2758
2759                         init_objects();
2760                         Network_rejoined = 1;
2761                         my_pnum = obj_owner;
2762                         change_playernum_to(my_pnum);
2763                         mode = 1;
2764                         object_count = 0;
2765                         frame_num = 1;
2766                 }
2767                 else if (objnum == -2)
2768                 {
2769                         // Special debug checksum marker for entire send
2770                         if (mode == 1)
2771                         {
2772                                 network_pack_objects();
2773                                 mode = 0;
2774                         }
2775                         mprintf((0, "Objnum -2 found in frame local %d remote %d.\n", frame_num, remote_frame_num));
2776                         mprintf((0, "Got %d objects, expected %d.\n", object_count, remote_objnum));
2777                         if (remote_objnum != object_count) {
2778                                 Int3();
2779                         }
2780                         if (network_verify_objects(remote_objnum, object_count))
2781                         {
2782                                 // Failed to sync up 
2783                                 nm_messagebox(NULL, 1, TXT_OK, TXT_NET_SYNC_FAILED);
2784                                 Network_status = NETSTAT_MENU;                          
2785                                 return;
2786                         }
2787                         frame_num = 0;
2788                 }
2789                 else 
2790                 {
2791                         if (frame_num != remote_frame_num)
2792                                 Int3();
2793                         mprintf ((0,"Got a type 3 object packet!\n"));
2794                         object_count++;
2795                         if ((obj_owner == my_pnum) || (obj_owner == -1)) 
2796                         {
2797                                 if (mode != 1)
2798                                         Int3(); // SEE ROB
2799                                 objnum = remote_objnum;
2800                                 //if (objnum > Highest_object_index)
2801                                 //{
2802                                 //      Highest_object_index = objnum;
2803                                 //      num_objects = Highest_object_index+1;
2804                                 //}
2805                         }
2806                         else {
2807                                 if (mode == 1)
2808                                 {
2809                                         network_pack_objects();
2810                                         mode = 0;
2811                                 }
2812                                 objnum = obj_allocate();
2813                         }
2814                         if (objnum != -1) {
2815                                 obj = &Objects[objnum];
2816                                 if (obj->segnum != -1)
2817                                         obj_unlink(objnum);
2818                                 Assert(obj->segnum == -1);
2819                                 Assert(objnum < MAX_OBJECTS);
2820                                 memcpy(obj,data+loc,sizeof(object));            loc += sizeof(object);
2821 #ifdef MACINTOSH
2822                                 if (Network_game_type == IPX_GAME)
2823                                         swap_object(obj);
2824 #endif
2825                                 segnum = obj->segnum;
2826                                 obj->next = obj->prev = obj->segnum = -1;
2827                                 obj->attached_obj = -1;
2828                                 if (segnum > -1)
2829                                         obj_link(obj-Objects,segnum);
2830                                 if (obj_owner == my_pnum) 
2831                                         map_objnum_local_to_local(objnum);
2832                                 else if (obj_owner != -1)
2833                                         map_objnum_local_to_remote(objnum, remote_objnum, obj_owner);
2834                                 else
2835                                         object_owner[objnum] = -1;
2836                         }
2837                 } // For a standard onbject
2838         } // For each object in packet
2839 }
2840         
2841 void network_sync_poll( int nitems, newmenu_item * menus, int * key, int citem )
2842 {
2843         // Polling loop waiting for sync packet to start game
2844
2845         static fix t1 = 0;
2846
2847         menus = menus;
2848         citem = citem;
2849         nitems = nitems;
2850
2851         network_listen();
2852
2853         if (Network_status != NETSTAT_WAITING)  // Status changed to playing, exit the menu
2854                 *key = -2;
2855
2856         if (!Network_rejoined && (timer_get_approx_seconds() > t1+F1_0*2))
2857         {
2858                 int i;
2859
2860                 // Poll time expired, re-send request
2861                 
2862                 t1 = timer_get_approx_seconds();
2863
2864                 mprintf((0, "Re-sending join request.\n"));
2865                 i = network_send_request();
2866                 if (i < 0)
2867                         *key = -2;
2868         }
2869 }
2870
2871 void network_start_poll( int nitems, newmenu_item * menus, int * key, int citem )
2872 {
2873         int i,n,nm;
2874
2875         key=key;
2876         citem=citem;
2877
2878         Assert(Network_status == NETSTAT_STARTING);
2879
2880         if (!menus[0].value) {
2881                         menus[0].value = 1;
2882                         menus[0].redraw = 1;
2883         }
2884
2885         for (i=1; i<nitems; i++ )       {
2886                 if ( (i>= N_players) && (menus[i].value) )      {
2887                         menus[i].value = 0;
2888                         menus[i].redraw = 1;
2889                 }
2890         }
2891
2892         nm = 0;
2893         for (i=0; i<nitems; i++ )       {
2894                 if ( menus[i].value )   {
2895                         nm++;
2896                         if ( nm > N_players )   {
2897                                 menus[i].value = 0;
2898                                 menus[i].redraw = 1;
2899                         }
2900                 }
2901         }
2902
2903         if ( nm > MaxNumNetPlayers )    {
2904                 nm_messagebox( TXT_ERROR, 1, TXT_OK, "%s %d %s", TXT_SORRY_ONLY, MaxNumNetPlayers, TXT_NETPLAYERS_IN );
2905                 // Turn off the last player highlighted
2906                 for (i = N_players; i > 0; i--)
2907                         if (menus[i].value == 1) 
2908                         {
2909                                 menus[i].value = 0;
2910                                 menus[i].redraw = 1;
2911                                 break;
2912                         }
2913         }
2914
2915 //       if (nitems > MAX_PLAYERS ) return; 
2916         
2917         n = Netgame.numplayers;
2918         network_listen();
2919
2920         if (n < Netgame.numplayers )    
2921         {
2922                 digi_play_sample (SOUND_HUD_MESSAGE,F1_0);
2923
2924       mprintf ((0,"More players are printed!"));
2925                 if (FindArg("-norankings"))
2926               sprintf( menus[N_players-1].text, "%d. %-20s", N_players,NetPlayers.players[N_players-1].callsign );
2927                 else
2928               sprintf( menus[N_players-1].text, "%d. %s%-20s", N_players, RankStrings[NetPlayers.players[N_players-1].rank],NetPlayers.players[N_players-1].callsign );
2929
2930                 menus[N_players-1].redraw = 1;
2931                 if (N_players <= MaxNumNetPlayers)
2932                 {
2933                         menus[N_players-1].value = 1;
2934                 }
2935         } 
2936         else if ( n > Netgame.numplayers )      
2937         {
2938                 // One got removed...
2939
2940       digi_play_sample (SOUND_HUD_KILL,F1_0);
2941   
2942                 for (i=0; i<N_players; i++ )    
2943                 {
2944          
2945          if (FindArg("-norankings"))    
2946                  sprintf( menus[i].text, "%d. %-20s", i+1, NetPlayers.players[i].callsign );
2947          else
2948                  sprintf( menus[i].text, "%d. %s%-20s", i+1, RankStrings[NetPlayers.players[i].rank],NetPlayers.players[i].callsign );
2949                         if (i < MaxNumNetPlayers)
2950                                 menus[i].value = 1;
2951                         else
2952                                 menus[i].value = 0;
2953                         menus[i].redraw = 1;
2954                 }
2955                 for (i=N_players; i<n; i++ )    
2956                 {
2957                         sprintf( menus[i].text, "%d. ", i+1 );          // Clear out the deleted entries...
2958                         menus[i].value = 0;
2959                         menus[i].redraw = 1;
2960                 }
2961    }
2962 }
2963
2964 int opt_cinvul, opt_team_anarchy, opt_coop, opt_show_on_map, opt_closed,opt_maxnet;
2965 int last_cinvul=0,last_maxnet,opt_team_hoard;
2966 int opt_refuse,opt_capture;
2967
2968 void network_game_param_poll( int nitems, newmenu_item * menus, int * key, int citem )
2969 {
2970         static int oldmaxnet=0;
2971
2972         if (((HoardEquipped() && menus[opt_team_hoard].value) || (menus[opt_team_anarchy].value || menus[opt_capture].value)) && !menus[opt_closed].value && !menus[opt_refuse].value) { 
2973                 menus[opt_refuse].value = 1;
2974                 menus[opt_refuse].redraw = 1;
2975                 menus[opt_refuse-1].value = 0;
2976                 menus[opt_refuse-1].redraw = 1;
2977                 menus[opt_refuse-2].value = 0;
2978                 menus[opt_refuse-2].redraw = 1;
2979         }
2980
2981         #ifndef MACINTOSH
2982         if (menus[opt_coop].value)
2983         #else
2984         if ( (menus[opt_coop].value) || ((Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) && (menus[opt_coop-1].value)) )
2985         #endif
2986         {
2987                 oldmaxnet=1;
2988
2989                 if (menus[opt_maxnet].value>2) 
2990                 {
2991                     menus[opt_maxnet].value=2;
2992                     menus[opt_maxnet].redraw=1;
2993                 }
2994
2995                 if (menus[opt_maxnet].max_value>2)
2996                 {
2997                     menus[opt_maxnet].max_value=2;
2998                     menus[opt_maxnet].redraw=1;
2999                 }
3000
3001                 #ifdef MACINTOSH
3002                 if ( (Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) ) {
3003                         if (menus[opt_maxnet].value > 0) {
3004                             menus[opt_maxnet].value=0;
3005                             menus[opt_maxnet].redraw=1;
3006                         }
3007                         if (menus[opt_maxnet].max_value > 0) {
3008                                 menus[opt_maxnet].max_value=0;
3009                                 menus[opt_maxnet].redraw=1;
3010                         }
3011                 }
3012                 #endif
3013
3014              if (!Netgame.game_flags & NETGAME_FLAG_SHOW_MAP) 
3015                         Netgame.game_flags |= NETGAME_FLAG_SHOW_MAP;
3016
3017                 if (Netgame.PlayTimeAllowed || Netgame.KillGoal)
3018                 {
3019                     Netgame.PlayTimeAllowed=0;
3020                     Netgame.KillGoal=0;
3021                 }
3022
3023         }
3024         else // if !Coop game
3025     {
3026                 if (oldmaxnet)
3027                 {
3028                     oldmaxnet=0;
3029                         menus[opt_maxnet].value=6;
3030                         menus[opt_maxnet].max_value=6;
3031
3032                     #ifdef MACINTOSH
3033                         if ( (Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) ) {
3034                                 menus[opt_maxnet].value=1;
3035                                 menus[opt_maxnet].max_value=1;
3036                         }
3037                         #endif
3038                           
3039                 }
3040         }         
3041
3042     if ( last_maxnet != menus[opt_maxnet].value )   
3043         {
3044                 sprintf( menus[opt_maxnet].text, "Maximum players: %d", menus[opt_maxnet].value+2 );
3045                 last_maxnet = menus[opt_maxnet].value;
3046                 menus[opt_maxnet].redraw = 1;
3047         }               
3048  }
3049
3050 int netgame_difficulty;
3051
3052 int network_get_game_params( char * game_name, int *mode, int *game_flags, int *level )
3053 {
3054         int i;
3055    int opt, opt_name, opt_level, opt_mode,opt_moreopts;
3056         newmenu_item m[20];
3057         char name[NETGAME_NAME_LEN+1];
3058         char slevel[5];
3059         char level_text[32];
3060    char srmaxnet[50];
3061
3062         int new_mission_num;
3063         int anarchy_only;
3064             
3065    *game_flags=*game_flags;
3066
3067    SetAllAllowablesTo (1);
3068    NamesInfoSecurity=-1;
3069
3070         for (i=0;i<MAX_PLAYERS;i++)
3071          if (i!=Player_num)     
3072                 Players[i].callsign[0]=0;    
3073
3074         MaxNumNetPlayers=8;
3075    Netgame.KillGoal=0;
3076    Netgame.PlayTimeAllowed=0;
3077         Netgame.Allow_marker_view=1;
3078         netgame_difficulty=Player_default_difficulty;
3079
3080         new_mission_num = multi_choose_mission(&anarchy_only);
3081
3082         if (new_mission_num < 0)
3083                 return -1;
3084
3085    if (!(FindArg ("-packets") && FindArg ("-shortpackets")))
3086            if (!network_choose_connect ())
3087                         return -1;
3088
3089         #ifdef MACINTOSH
3090         if ( (Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) )
3091                 MaxNumNetPlayers = 3;
3092         #endif
3093         
3094         strcpy(Netgame.mission_name, Mission_list[new_mission_num].filename);
3095         strcpy(Netgame.mission_title, Mission_list[new_mission_num].mission_name);
3096         Netgame.control_invul_time = control_invul_time;
3097         
3098         sprintf( name, "%s%s", Players[Player_num].callsign, TXT_S_GAME );
3099         sprintf( slevel, "1" );
3100
3101         opt = 0;
3102         m[opt].type = NM_TYPE_TEXT; m[opt].text = TXT_DESCRIPTION; opt++;
3103
3104         opt_name = opt;
3105         m[opt].type = NM_TYPE_INPUT; m[opt].text = name; m[opt].text_len = NETGAME_NAME_LEN; opt++;
3106
3107         sprintf(level_text, "%s (1-%d)", TXT_LEVEL_, Last_level);
3108
3109 //      if (Last_secret_level < -1)
3110 //              sprintf(level_text+strlen(level_text)-1, ", S1-S%d)", -Last_secret_level);
3111 //      else if (Last_secret_level == -1)
3112 //              sprintf(level_text+strlen(level_text)-1, ", S1)");
3113
3114         Assert(strlen(level_text) < 32);
3115
3116         m[opt].type = NM_TYPE_TEXT; m[opt].text = level_text; opt++;
3117
3118         opt_level = opt;
3119         m[opt].type = NM_TYPE_INPUT; m[opt].text = slevel; m[opt].text_len=4; opt++;
3120 //      m[opt].type = NM_TYPE_TEXT; m[opt].text = TXT_OPTIONS; opt++;
3121         
3122         opt_mode = opt;
3123         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_ANARCHY; m[opt].value=1; m[opt].group=0; opt++;
3124         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_TEAM_ANARCHY; m[opt].value=0; m[opt].group=0; opt_team_anarchy=opt; opt++;
3125         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_ANARCHY_W_ROBOTS; m[opt].value=0; m[opt].group=0; opt++;
3126         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_COOPERATIVE; m[opt].value=0; m[opt].group=0; opt_coop=opt; opt++;
3127         m[opt].type = NM_TYPE_RADIO; m[opt].text = "Capture the flag"; m[opt].value=0; m[opt].group=0; opt_capture=opt; opt++;
3128    
3129         if (HoardEquipped())
3130         {
3131                 m[opt].type = NM_TYPE_RADIO; m[opt].text = "Hoard"; m[opt].value=0; m[opt].group=0; opt++;
3132                 m[opt].type = NM_TYPE_RADIO; m[opt].text = "Team Hoard"; m[opt].value=0; m[opt].group=0; opt_team_hoard=opt; opt++;
3133            m[opt].type = NM_TYPE_TEXT; m[opt].text = ""; opt++;
3134         } 
3135         else
3136          {  m[opt].type = NM_TYPE_TEXT; m[opt].text = ""; opt++; }
3137
3138         m[opt].type = NM_TYPE_RADIO; m[opt].text = "Open game"; m[opt].group=1; m[opt].value=0; opt++;
3139         opt_closed = opt;
3140         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_CLOSED_GAME; m[opt].group=1; m[opt].value=0; opt++;
3141    opt_refuse = opt;
3142    m[opt].type = NM_TYPE_RADIO; m[opt].text = "Restricted Game              "; m[opt].group=1; m[opt].value=Netgame.RefusePlayers; opt++;
3143
3144 //      m[opt].type = NM_TYPE_CHECK; m[opt].text = TXT_SHOW_IDS; m[opt].value=0; opt++;
3145
3146    opt_maxnet = opt;
3147    sprintf( srmaxnet, "Maximum players: %d", MaxNumNetPlayers);
3148    m[opt].type = NM_TYPE_SLIDER; m[opt].value=MaxNumNetPlayers-2; m[opt].text= srmaxnet; m[opt].min_value=0; 
3149    m[opt].max_value=MaxNumNetPlayers-2; opt++;
3150    last_maxnet=MaxNumNetPlayers-2;
3151
3152    opt_moreopts=opt;
3153    m[opt].type = NM_TYPE_MENU;  m[opt].text = "More options..."; opt++;
3154
3155         Assert(opt <= 20);
3156
3157 menu:
3158    ExtGameStatus=GAMESTAT_NETGAME_OPTIONS; 
3159         i = newmenu_do1( NULL, NULL, opt, m, network_game_param_poll, 1 );
3160                                                                         //TXT_NETGAME_SETUP
3161    if (i==opt_moreopts)
3162     {
3163      if ( m[opt_mode+3].value )
3164       Game_mode=GM_MULTI_COOP;
3165      network_more_game_options();
3166      Game_mode=0;
3167      goto menu;
3168     }
3169   Netgame.RefusePlayers=m[opt_refuse].value;
3170
3171
3172         if ( i > -1 )   {
3173                 int j;
3174                       
3175    MaxNumNetPlayers = m[opt_maxnet].value+2;
3176    Netgame.max_numplayers=MaxNumNetPlayers;
3177                                 
3178                 for (j = 0; j < num_active_games; j++)
3179                         if (!stricmp(Active_games[j].game_name, name))
3180                         {
3181                                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_DUPLICATE_NAME);
3182                                 goto menu;
3183                         }
3184
3185                 strcpy( game_name, name );
3186                 
3187
3188                 *level = atoi(slevel);
3189
3190                 if ((*level < 1) || (*level > Last_level))
3191                 {
3192                         nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_LEVEL_OUT_RANGE );
3193                         sprintf(slevel, "1");
3194                         goto menu;
3195                 }
3196                 if ( m[opt_mode].value )
3197                         *mode = NETGAME_ANARCHY;
3198
3199 #ifdef SHAREWARE
3200                 else 
3201                 {
3202                         nm_messagebox(TXT_SORRY, 1, TXT_OK, TXT_REGISTERED_ONLY );
3203                         m[opt_mode+1].value = 0;
3204                         m[opt_mode+2].value = 0;
3205                         m[opt_mode+3].value = 0;
3206                    if (HoardEquipped())
3207                                 m[opt_mode+4].value = 0;
3208
3209                         m[opt_mode].value = 1;
3210                         goto menu;
3211                 }
3212 #else
3213                 else if (m[opt_mode+1].value) {
3214                         *mode = NETGAME_TEAM_ANARCHY;
3215                 }
3216                 else if (m[opt_capture].value)
3217                         *mode = NETGAME_CAPTURE_FLAG;
3218                 else if (HoardEquipped() && m[opt_capture+1].value)
3219                                 *mode = NETGAME_HOARD;
3220                 else if (HoardEquipped() && m[opt_capture+2].value)
3221                                 *mode = NETGAME_TEAM_HOARD;
3222                 else if (anarchy_only) {
3223                         nm_messagebox(NULL, 1, TXT_OK, TXT_ANARCHY_ONLY_MISSION);
3224                         m[opt_mode+2].value = 0;
3225                         m[opt_mode+3].value = 0;
3226                         m[opt_mode].value = 1;
3227                         goto menu;
3228                 }               
3229                 else if ( m[opt_mode+2].value ) 
3230                         *mode = NETGAME_ROBOT_ANARCHY;
3231                 else if ( m[opt_mode+3].value ) 
3232                         *mode = NETGAME_COOPERATIVE;
3233                 else Int3(); // Invalid mode -- see Rob
3234 #endif
3235                 if (m[opt_closed].value)
3236                         Netgame.game_flags |= NETGAME_FLAG_CLOSED;
3237       
3238         }
3239
3240         return i;
3241 }
3242
3243 void
3244 network_set_game_mode(int gamemode)
3245 {
3246         Show_kill_list = 1;
3247
3248         if ( gamemode == NETGAME_ANARCHY )
3249                 Game_mode = GM_NETWORK;
3250         else if ( gamemode == NETGAME_ROBOT_ANARCHY )
3251                 Game_mode = GM_NETWORK | GM_MULTI_ROBOTS;
3252         else if ( gamemode == NETGAME_COOPERATIVE ) 
3253                 Game_mode = GM_NETWORK | GM_MULTI_COOP | GM_MULTI_ROBOTS;
3254         else if (gamemode == NETGAME_CAPTURE_FLAG)
3255                 {
3256                  Game_mode = GM_NETWORK | GM_TEAM | GM_CAPTURE;
3257                  Show_kill_list=3;
3258                 }
3259
3260         else if (HoardEquipped() && gamemode == NETGAME_HOARD)
3261                  Game_mode = GM_NETWORK | GM_HOARD;
3262         else if (HoardEquipped() && gamemode == NETGAME_TEAM_HOARD)
3263                  {
3264                   Game_mode = GM_NETWORK | GM_HOARD | GM_TEAM;
3265                   Show_kill_list=3;
3266                  }
3267         else if ( gamemode == NETGAME_TEAM_ANARCHY )
3268         {
3269                 Game_mode = GM_NETWORK | GM_TEAM;
3270                 Show_kill_list = 3;
3271         }
3272         else
3273                 Int3();
3274
3275 #if 0
3276 #ifdef MACINTOSH                        // minimize players on localtalk games
3277         if ( (Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) ) {
3278                 if (Game_mode & GM_MULTI_ROBOTS)
3279                         MaxNumNetPlayers = 2;
3280                 else
3281                         MaxNumNetPlayers = 3;
3282         } else {
3283                 if (Game_mode & GM_MULTI_COOP)
3284                         MaxNumNetPlayers = 4;
3285                 else
3286                         MaxNumNetPlayers = 8;
3287         }
3288 #endif
3289 #endif
3290
3291 }
3292
3293 int
3294 network_find_game(void)
3295 {
3296         // Find out whether or not there is space left on this socket
3297
3298         fix t1;
3299
3300         Network_status = NETSTAT_BROWSING;
3301
3302         num_active_games = 0;
3303
3304         show_boxed_message(TXT_WAIT);
3305
3306         network_send_game_list_request();
3307         t1 = timer_get_approx_seconds() + F1_0*3;
3308
3309         while (timer_get_approx_seconds() < t1) // Wait 3 seconds for replies
3310                 network_listen();
3311
3312         clear_boxed_message();
3313
3314 //      mprintf((0, "%s %d %s\n", TXT_FOUND, num_active_games, TXT_ACTIVE_GAMES));
3315
3316         if (num_active_games < MAX_ACTIVE_NETGAMES)
3317                 return 0;
3318         return 1;
3319 }
3320         
3321 void network_read_sync_packet( netgame_info * sp, int rsinit)
3322 {       
3323 #ifdef MACINTOSH
3324         netgame_info tmp_info;
3325 #endif
3326
3327         int i, j;
3328         char temp_callsign[CALLSIGN_LEN+1];
3329
3330 #ifdef MACINTOSH
3331         if ( (Network_game_type == IPX_GAME) && (sp != &Netgame) ) {                            // for macintosh -- get the values unpacked to our structure format
3332                 receive_netgame_packet((ubyte *)sp, &tmp_info, 0);
3333                 sp = &tmp_info;
3334         }
3335 #endif
3336
3337    if (rsinit)
3338      TempPlayersInfo=&NetPlayers;
3339         
3340         // This function is now called by all people entering the netgame.
3341
3342         // mprintf( (0, "%s %d\n", TXT_STARTING_NETGAME, sp->levelnum ));
3343
3344         if (sp != &Netgame)
3345          {
3346           memcpy( &Netgame, sp, sizeof(netgame_info) );
3347           memcpy (&NetPlayers,TempPlayersInfo,sizeof (AllNetPlayers_info));
3348          }
3349
3350         N_players = sp->numplayers;
3351         Difficulty_level = sp->difficulty;
3352         Network_status = sp->game_status;
3353
3354    //Assert(Function_mode != FMODE_GAME);
3355
3356         // New code, 11/27
3357
3358         mprintf((1, "Netgame.checksum = %d, calculated checksum = %d.\n", Netgame.segments_checksum, my_segments_checksum));
3359
3360         if (Netgame.segments_checksum != my_segments_checksum)
3361         {
3362                 Network_status = NETSTAT_MENU;
3363                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_NETLEVEL_NMATCH);
3364 #ifdef RELEASE
3365                 return;
3366 #endif
3367         }
3368
3369         // Discover my player number
3370
3371         memcpy(temp_callsign, Players[Player_num].callsign, CALLSIGN_LEN+1);
3372         
3373         Player_num = -1;
3374
3375         for (i=0; i<MAX_NUM_NET_PLAYERS; i++ )  {
3376                 Players[i].net_kills_total = 0;
3377 //              Players[i].net_killed_total = 0;
3378         }
3379
3380         for (i=0; i<N_players; i++ )    {
3381                 if (Network_game_type == IPX_GAME) {
3382                         if ( (!memcmp( TempPlayersInfo->players[i].network.ipx.node, My_Seq.player.network.ipx.node, 6 )) && (!stricmp( TempPlayersInfo->players[i].callsign, temp_callsign)) ) {
3383                                 if (Player_num!=-1) {
3384                                         Int3(); // Hey, we've found ourselves twice
3385                                         mprintf ((0,"Hey, we've found ourselves twice!\n"));
3386                                         Network_status = NETSTAT_MENU;
3387                                         return; 
3388                                 }
3389                                 change_playernum_to(i);
3390                         }
3391                 } else {
3392                         if ( (TempPlayersInfo->players[i].network.appletalk.node == My_Seq.player.network.appletalk.node) && (!stricmp( TempPlayersInfo->players[i].callsign, temp_callsign)) ) {
3393                                 if (Player_num!=-1) {
3394                                         Int3(); // Hey, we've found ourselves twice
3395                                         Network_status = NETSTAT_MENU;
3396                                         return; 
3397                                 }
3398                                 change_playernum_to(i);
3399                         }
3400                 }
3401                 memcpy( Players[i].callsign, TempPlayersInfo->players[i].callsign, CALLSIGN_LEN+1 );
3402
3403                 if (Network_game_type == IPX_GAME) {
3404                         if ( (*(uint *)TempPlayersInfo->players[i].network.ipx.server) != 0 )
3405                                 ipx_get_local_target( TempPlayersInfo->players[i].network.ipx.server, TempPlayersInfo->players[i].network.ipx.node, Players[i].net_address );
3406                         else
3407                                 memcpy( Players[i].net_address, TempPlayersInfo->players[i].network.ipx.node, 6 );
3408                 }
3409                                 
3410                 Players[i].n_packets_got=0;                             // How many packets we got from them
3411                 Players[i].n_packets_sent=0;                            // How many packets we sent to them
3412                 Players[i].connected = TempPlayersInfo->players[i].connected;
3413                 Players[i].net_kills_total = sp->player_kills[i];
3414                 Players[i].net_killed_total = sp->killed[i];
3415                 if ((Network_rejoined) || (i != Player_num))
3416                         Players[i].score = sp->player_score[i];
3417                 for (j = 0; j < MAX_NUM_NET_PLAYERS; j++)
3418                 {
3419                         kill_matrix[i][j] = sp->kills[i][j];
3420                 }
3421         }
3422
3423         if ( Player_num < 0 )   {
3424                 mprintf ((0,"Bad Player_num, resetting to NETSTAT_MENU!\n"));
3425                 Network_status = NETSTAT_MENU;
3426                 return;
3427         }
3428
3429         if (Network_rejoined) 
3430                 for (i=0; i<N_players;i++)
3431                         Players[i].net_killed_total = sp->killed[i];
3432
3433         if (Network_rejoined) {
3434                 network_process_monitor_vector(sp->monitor_vector);
3435                 Players[Player_num].time_level = sp->level_time;
3436         }
3437
3438         team_kills[0] = sp->team_kills[0];
3439         team_kills[1] = sp->team_kills[1];
3440         
3441         Players[Player_num].connected = 1;
3442    NetPlayers.players[Player_num].connected = 1;
3443    NetPlayers.players[Player_num].rank=GetMyNetRanking();
3444
3445         if (!Network_rejoined)
3446                 for (i=0; i<NumNetPlayerPositions; i++) {
3447                         Objects[Players[i].objnum].pos = Player_init[Netgame.locations[i]].pos;
3448                         Objects[Players[i].objnum].orient = Player_init[Netgame.locations[i]].orient;
3449                         obj_relink(Players[i].objnum,Player_init[Netgame.locations[i]].segnum);
3450                 }
3451
3452         Objects[Players[Player_num].objnum].type = OBJ_PLAYER;
3453
3454    mprintf ((0,"Changing to NETSTAT_PLAYING!\n"));
3455         Network_status = NETSTAT_PLAYING;
3456         Function_mode = FMODE_GAME;
3457         multi_sort_kill_list();
3458
3459 }
3460
3461 void
3462 network_send_sync(void)
3463 {
3464         int i, j, np;
3465
3466         // Randomize their starting locations...
3467
3468         d_srand( timer_get_fixed_seconds() );
3469         for (i=0; i<NumNetPlayerPositions; i++ )        
3470         {
3471                 if (Players[i].connected)
3472                         Players[i].connected = 1; // Get rid of endlevel connect statuses
3473                 if (Game_mode & GM_MULTI_COOP)
3474                         Netgame.locations[i] = i;
3475                 else {
3476                         do 
3477                         {
3478                                 np = d_rand() % NumNetPlayerPositions;
3479                                 for (j=0; j<i; j++ )    
3480                                 {
3481                                         if (Netgame.locations[j]==np)   
3482                                         {
3483                                                 np =-1;
3484                                                 break;
3485                                         }
3486                                 }
3487                         } while (np<0);
3488                         // np is a location that is not used anywhere else..
3489                         Netgame.locations[i]=np;
3490 //                      mprintf((0, "Player %d starting in location %d\n" ,i ,np ));
3491                 }
3492         }
3493
3494         // Push current data into the sync packet
3495
3496         network_update_netgame();
3497         Netgame.game_status = NETSTAT_PLAYING;
3498         Netgame.type = PID_SYNC;
3499         Netgame.segments_checksum = my_segments_checksum;
3500
3501         for (i=0; i<N_players; i++ )    {
3502                 if ((!Players[i].connected) || (i == Player_num))
3503                         continue;
3504
3505                 if (Network_game_type == IPX_GAME) {
3506                 // Send several times, extras will be ignored
3507                         #ifndef MACINTOSH
3508                         ipx_send_internetwork_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node);
3509                         ipx_send_internetwork_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node);
3510                         #else
3511                         send_netgame_packet(NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, NULL, 0);
3512                         send_netplayers_packet(NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node);
3513                         #endif
3514                 #ifdef MACINTOSH
3515                 } else {
3516                                 appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), NetPlayers.players[i].network.appletalk.node,
3517                                         NetPlayers.players[i].network.appletalk.net,
3518                                         NetPlayers.players[i].network.appletalk.socket);
3519                                 appletalk_send_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), NetPlayers.players[i].network.appletalk.node,
3520                                         NetPlayers.players[i].network.appletalk.net,
3521                                         NetPlayers.players[i].network.appletalk.socket);
3522                 #endif
3523                 }
3524
3525         }       
3526         network_read_sync_packet(&Netgame,1); // Read it myself, as if I had sent it
3527 }
3528
3529 int
3530 network_select_teams(void)
3531 {
3532 #ifndef SHAREWARE
3533         newmenu_item m[MAX_PLAYERS+4];
3534         int choice, opt, opt_team_b;
3535         ubyte team_vector = 0;
3536         char team_names[2][CALLSIGN_LEN+1];
3537         int i;
3538         int pnums[MAX_PLAYERS+2];
3539
3540         // One-time initialization
3541
3542         for (i = N_players/2; i < N_players; i++) // Put first half of players on team A
3543         {
3544                 team_vector |= (1 << i);
3545         }
3546
3547         sprintf(team_names[0], "%s", TXT_BLUE);
3548         sprintf(team_names[1], "%s", TXT_RED);
3549
3550         // Here comes da menu
3551 menu:
3552         m[0].type = NM_TYPE_INPUT; m[0].text = team_names[0]; m[0].text_len = CALLSIGN_LEN; 
3553
3554         opt = 1;
3555         for (i = 0; i < N_players; i++)
3556         {
3557                 if (!(team_vector & (1 << i)))
3558                 {
3559                         m[opt].type = NM_TYPE_MENU; m[opt].text = NetPlayers.players[i].callsign; pnums[opt] = i; opt++;
3560                 }
3561         }
3562         opt_team_b = opt;
3563         m[opt].type = NM_TYPE_INPUT; m[opt].text = team_names[1]; m[opt].text_len = CALLSIGN_LEN; opt++;
3564         for (i = 0; i < N_players; i++)
3565         {
3566                 if (team_vector & (1 << i))
3567                 {
3568                         m[opt].type = NM_TYPE_MENU; m[opt].text = NetPlayers.players[i].callsign; pnums[opt] = i; opt++;
3569                 }
3570         }
3571         m[opt].type = NM_TYPE_TEXT; m[opt].text = ""; opt++;
3572         m[opt].type = NM_TYPE_MENU; m[opt].text = TXT_ACCEPT; opt++;
3573
3574         Assert(opt <= MAX_PLAYERS+4);
3575         
3576         choice = newmenu_do(NULL, TXT_TEAM_SELECTION, opt, m, NULL);
3577
3578         if (choice == opt-1)
3579         {
3580                 if ((opt-2-opt_team_b < 2) || (opt_team_b == 1)) 
3581                 {
3582                         nm_messagebox(NULL, 1, TXT_OK, TXT_TEAM_MUST_ONE);
3583                         #ifdef RELEASE
3584                                 goto menu;
3585                         #endif
3586                 }
3587                 
3588                 Netgame.team_vector = team_vector;
3589                 strcpy(Netgame.team_name[0], team_names[0]);
3590                 strcpy(Netgame.team_name[1], team_names[1]);
3591                 return 1;
3592         }
3593
3594         else if ((choice > 0) && (choice < opt_team_b)) {
3595                 team_vector |= (1 << pnums[choice]);
3596         }
3597         else if ((choice > opt_team_b) && (choice < opt-2)) {
3598                 team_vector &= ~(1 << pnums[choice]);
3599         }
3600         else if (choice == -1)
3601                 return 0;
3602         goto menu;
3603 #else
3604         return 0;
3605 #endif
3606 }
3607
3608 int
3609 network_select_players(void)
3610 {
3611         int i, j;
3612    newmenu_item m[MAX_PLAYERS+4];
3613    char text[MAX_PLAYERS+4][45];
3614         char title[50];
3615         int save_nplayers;              //how may people would like to join
3616
3617         network_add_player( &My_Seq );
3618                 
3619         for (i=0; i< MAX_PLAYERS+4; i++ ) {
3620                 sprintf( text[i], "%d.  %-20s", i+1, "" );
3621                 m[i].type = NM_TYPE_CHECK; m[i].text = text[i]; m[i].value = 0;
3622         }
3623
3624         m[0].value = 1;                         // Assume server will play...
3625
3626    if (FindArg("-norankings"))
3627                 sprintf( text[0], "%d. %-20s", 1, Players[Player_num].callsign );
3628         else
3629                 sprintf( text[0], "%d. %s%-20s", 1, RankStrings[NetPlayers.players[Player_num].rank],Players[Player_num].callsign );
3630
3631         sprintf( title, "%s %d %s", TXT_TEAM_SELECT, MaxNumNetPlayers, TXT_TEAM_PRESS_ENTER );
3632
3633 GetPlayersAgain:
3634    ExtGameStatus=GAMESTAT_NETGAME_PLAYER_SELECT;
3635         j=newmenu_do1( NULL, title, MAX_PLAYERS+4, m, network_start_poll, 1 );
3636
3637         save_nplayers = N_players;
3638
3639         if (j<0) 
3640         {
3641                 // Aborted!                                      
3642                 // Dump all players and go back to menu mode
3643
3644 abort:
3645                 for (i=1; i<save_nplayers; i++) {
3646                         if (Network_game_type == IPX_GAME)
3647                                 network_dump_player(NetPlayers.players[i].network.ipx.server,NetPlayers.players[i].network.ipx.node, DUMP_ABORTED);
3648                         #ifdef MACINTOSH
3649                         else
3650                                 network_dump_appletalk_player(NetPlayers.players[i].network.appletalk.node,NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket, DUMP_ABORTED);
3651                         #endif
3652                 }
3653                         
3654
3655                 Netgame.numplayers = 0;
3656                 network_send_game_info(0); // Tell everyone we're bailing
3657
3658                 Network_status = NETSTAT_MENU;
3659                 return(0);
3660         }
3661        
3662         // Count number of players chosen
3663
3664         N_players = 0;
3665         for (i=0; i<save_nplayers; i++ )
3666         {
3667                 if (m[i].value) 
3668                         N_players++;
3669         }
3670         
3671         if ( N_players > Netgame.max_numplayers) {
3672                 #ifndef MACINTOSH
3673                 nm_messagebox( TXT_ERROR, 1, TXT_OK, "%s %d %s", TXT_SORRY_ONLY, MaxNumNetPlayers, TXT_NETPLAYERS_IN );
3674                 #else
3675                 nm_messagebox( TXT_ERROR, 1, TXT_OK, "%s %d netplayers for this game.", TXT_SORRY_ONLY, MaxNumNetPlayers );
3676                 #endif
3677                 N_players = save_nplayers;
3678                 goto GetPlayersAgain;
3679         }
3680
3681 #ifdef RELEASE
3682         if ( N_players < 2 )    {
3683                 nm_messagebox( TXT_ERROR, 1, TXT_OK, TXT_TEAM_ATLEAST_TWO );
3684                 N_players = save_nplayers;
3685                 goto GetPlayersAgain;
3686         }
3687 #endif
3688
3689 #ifdef RELEASE
3690         if ( (Netgame.gamemode == NETGAME_TEAM_ANARCHY ||
3691                    Netgame.gamemode == NETGAME_CAPTURE_FLAG || Netgame.gamemode == NETGAME_TEAM_HOARD) && (N_players < 2) ) {
3692                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "You must select at least two\nplayers to start a team game" );
3693                 N_players = save_nplayers;
3694                 goto GetPlayersAgain;
3695         }
3696 #endif
3697
3698         // Remove players that aren't marked.
3699         N_players = 0;
3700         for (i=0; i<save_nplayers; i++ )        {
3701                 if (m[i].value) 
3702                 {
3703                         if (i > N_players)
3704                         {
3705                                 if (Network_game_type == IPX_GAME) {
3706                                         memcpy(NetPlayers.players[N_players].network.ipx.node, NetPlayers.players[i].network.ipx.node, 6);
3707                                         memcpy(NetPlayers.players[N_players].network.ipx.server, NetPlayers.players[i].network.ipx.server, 4);
3708                                 } else {
3709                                         NetPlayers.players[N_players].network.appletalk.node = NetPlayers.players[i].network.appletalk.node;
3710                                         NetPlayers.players[N_players].network.appletalk.net = NetPlayers.players[i].network.appletalk.net;
3711                                         NetPlayers.players[N_players].network.appletalk.socket = NetPlayers.players[i].network.appletalk.socket;
3712                                 }
3713                                 memcpy(NetPlayers.players[N_players].callsign, NetPlayers.players[i].callsign, CALLSIGN_LEN+1);
3714                                 NetPlayers.players[N_players].version_major=NetPlayers.players[i].version_major;
3715                                 NetPlayers.players[N_players].version_minor=NetPlayers.players[i].version_minor;
3716                                 NetPlayers.players[N_players].rank=NetPlayers.players[i].rank;
3717                                 ClipRank (&NetPlayers.players[N_players].rank);
3718                                 network_check_for_old_version(i);
3719                         }
3720                         Players[N_players].connected = 1;
3721                         N_players++;
3722                 }
3723                 else
3724                 {
3725                         if (Network_game_type == IPX_GAME)
3726                                 network_dump_player(NetPlayers.players[i].network.ipx.server,NetPlayers.players[i].network.ipx.node, DUMP_DORK);
3727                         #ifdef MACINTOSH
3728                         else
3729                                 network_dump_appletalk_player(NetPlayers.players[i].network.appletalk.node,NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket, DUMP_DORK);
3730                         #endif
3731                 }
3732         }
3733
3734         for (i = N_players; i < MAX_NUM_NET_PLAYERS; i++) {
3735                 if (Network_game_type == IPX_GAME) {
3736                 memset(NetPlayers.players[i].network.ipx.node, 0, 6);
3737                 memset(NetPlayers.players[i].network.ipx.server, 0, 4);
3738             } else {
3739                 NetPlayers.players[i].network.appletalk.node = 0;
3740                 NetPlayers.players[i].network.appletalk.net = 0;
3741                 NetPlayers.players[i].network.appletalk.socket = 0;
3742             }
3743         memset(NetPlayers.players[i].callsign, 0, CALLSIGN_LEN+1);
3744                 NetPlayers.players[i].version_major=0;
3745                 NetPlayers.players[i].version_minor=0;
3746                 NetPlayers.players[i].rank=0;
3747         }
3748
3749    mprintf ((0,"Select teams: Game mode is %d\n",Netgame.gamemode));
3750
3751         if (Netgame.gamemode == NETGAME_TEAM_ANARCHY ||
3752             Netgame.gamemode == NETGAME_CAPTURE_FLAG ||
3753                  Netgame.gamemode == NETGAME_TEAM_HOARD)
3754                  if (!network_select_teams())
3755                         goto abort;
3756
3757         return(1); 
3758 }
3759
3760 void 
3761 network_start_game()
3762 {
3763         int i;
3764         char game_name[NETGAME_NAME_LEN+1];
3765         int chosen_game_mode, game_flags, level;
3766
3767         if (Network_game_type == IPX_GAME) {
3768
3769                 Assert( FRAME_INFO_SIZE < IPX_MAX_DATA_SIZE );
3770                 mprintf((0, "Using frame_info len %d, max %d.\n", FRAME_INFO_SIZE, IPX_MAX_DATA_SIZE));
3771                 
3772                 if ( !Network_active )
3773                 {
3774                         nm_messagebox(NULL, 1, TXT_OK, TXT_IPX_NOT_FOUND );
3775                         return;
3776                 }
3777         #ifdef MACINTOSH
3778         } else {
3779                 int err;
3780                 char buf[256];
3781
3782                 Assert( FRAME_INFO_SIZE < APPLETALK_MAX_DATA_SIZE );            
3783                 mprintf((0, "Using frame_info len %d, max %d.\n", sizeof(frame_info), APPLETALK_MAX_DATA_SIZE));
3784                 if (Appletalk_active <= 0) {
3785                         switch (Appletalk_active) {
3786                         case APPLETALK_NOT_OPEN:
3787                                 sprintf(buf, "Appletalk is not currently active.\nPlease enable AppleTalk from the\nChooser and restart Descent.");
3788                                 break;
3789                         case APPLETALK_BAD_LISTENER:
3790                                 sprintf(buf, "The Resource Fork of Descent appears damaged.\nPlease re-install Descent or contact\nMacPlay technical support.");
3791                                 break;
3792                         case APPLETALK_NO_LOCAL_ADDR:
3793                                 sprintf(buf, "Wow! Strange!\n\nNo Local Address.");
3794                                 break;
3795                         case APPLETALK_NO_SOCKET:
3796                                 sprintf(buf, "All AppleTalk sockets are in use.\nTry shutting down other network\napplications and restarting Descent.\n");
3797                                 break;
3798                         }
3799                         nm_messagebox(NULL, 1, TXT_OK, buf);
3800                         return;
3801                 }
3802                 strcpy(Network_zone_name, DEFAULT_ZONE_NAME);
3803         #endif
3804         }
3805
3806         network_init();
3807         change_playernum_to(0);
3808
3809         if (network_find_game())
3810         {
3811                 nm_messagebox(NULL, 1, TXT_OK, TXT_NET_FULL);
3812                 return;
3813         }
3814
3815         game_flags = 0;
3816         i = network_get_game_params( game_name, &chosen_game_mode, &game_flags, &level );
3817
3818         if (i<0) return;
3819
3820         N_players = 0;
3821
3822 // LoadLevel(level); Old, no longer used.
3823
3824         Netgame.difficulty = Difficulty_level;
3825         Netgame.gamemode = chosen_game_mode;
3826         Netgame.game_status = NETSTAT_STARTING;
3827         Netgame.numplayers = 0;
3828         Netgame.max_numplayers = MaxNumNetPlayers;
3829         Netgame.levelnum = level;
3830         Netgame.protocol_version = MULTI_PROTO_VERSION;
3831
3832         strcpy(Netgame.game_name, game_name);
3833         
3834         Network_status = NETSTAT_STARTING;
3835
3836         #ifdef MACINTOSH
3837         if (Network_game_type == APPLETALK_GAME) {
3838                 OSErr err;
3839                 fix t1;
3840                 int count = 0;
3841                 
3842                 show_boxed_message("Registering Netgame");
3843                 do {
3844                         err = appletalk_register_netgame( game_name, TickCount() );
3845                         t1 = timer_get_fixed_seconds() + F1_0;
3846                         while (timer_get_fixed_seconds() < t1) ;
3847                         count++;
3848                 } while ( (err == nbpDuplicate) && (count != MAX_REGISTER_TRIES) );
3849                 clear_boxed_message();
3850                 if ( (err == tooManyReqs) || (count == MAX_REGISTER_TRIES) ) {
3851                         nm_messagebox(NULL, 1, TXT_OK, "AppleTalk Network is too busy.\nPlease try again shortly.");
3852                         Game_mode = GM_GAME_OVER;
3853                         return;
3854                 }
3855         }
3856         #endif
3857
3858         network_set_game_mode(Netgame.gamemode);
3859
3860    d_srand( timer_get_fixed_seconds() );
3861    Netgame.Security=d_rand();  // For syncing Netgames with player packets
3862
3863         if(network_select_players())
3864         {
3865                 StartNewLevel(Netgame.levelnum, 0);
3866         }
3867         else
3868                 Game_mode = GM_GAME_OVER;
3869         
3870 }
3871
3872 void restart_net_searching(newmenu_item * m)
3873 {
3874         int i;
3875         N_players = 0;
3876         num_active_games = 0;
3877
3878         memset(Active_games, 0, sizeof(netgame_info)*MAX_ACTIVE_NETGAMES);
3879
3880         for (i = 0; i < MAX_ACTIVE_NETGAMES; i++)
3881         {
3882                 sprintf(m[i+2].text, "%d.                                                     ",i+1);
3883                 m[i+2].redraw = 1;
3884         }
3885   
3886    NamesInfoSecurity=-1;
3887         Network_games_changed = 1;      
3888 }
3889
3890 char *ModeLetters[]={"ANRCHY","TEAM","ROBO","COOP","FLAG","HOARD","TMHOARD"};
3891
3892 int NumActiveNetgames=0;
3893
3894 void network_join_poll( int nitems, newmenu_item * menus, int * key, int citem )
3895 {
3896         // Polling loop for Join Game menu
3897         static fix t1 = 0;
3898         int i, osocket,join_status,temp;
3899
3900         menus = menus;
3901         citem = citem;
3902         nitems = nitems;
3903         key = key;
3904
3905         if ( (Network_game_type == IPX_GAME) && Network_allow_socket_changes )      {
3906                 osocket = Network_socket;
3907         
3908                 if ( *key==KEY_PAGEDOWN )       { Network_socket--; *key = 0; }
3909                 if ( *key==KEY_PAGEUP )         { Network_socket++; *key = 0; }
3910         
3911                 if (Network_socket>99)
3912                  Network_socket=99;
3913            if (Network_socket<-99)
3914                  Network_socket=-99;    
3915         
3916                 if ( Network_socket+IPX_DEFAULT_SOCKET > 0x8000 )
3917                         Network_socket  = 0x8000 - IPX_DEFAULT_SOCKET;
3918         
3919                 if ( Network_socket+IPX_DEFAULT_SOCKET < 0 )
3920                         Network_socket  = IPX_DEFAULT_SOCKET;
3921         
3922                 if (Network_socket != osocket )         {
3923                         sprintf( menus[0].text, "\t%s %+d (PgUp/PgDn to change)", TXT_CURRENT_IPX_SOCKET, Network_socket );
3924                         menus[0].redraw = 1;
3925                         mprintf(( 0, "Changing to socket %d\n", Network_socket ));
3926                         network_listen();
3927                         mprintf ((0,"netgood 1!\n"));
3928                         ipx_change_default_socket( IPX_DEFAULT_SOCKET + Network_socket );
3929                         mprintf ((0,"netgood 2!\n"));
3930                         restart_net_searching(menus);
3931                         mprintf ((0,"netgood 3!\n"));
3932                         network_send_game_list_request();
3933                         mprintf ((0,"netgood 4!\n"));
3934                         return;
3935                 }
3936         }
3937
3938    // send a request for game info every 3 seconds
3939   
3940         if (Network_game_type == IPX_GAME) {  
3941                 if (timer_get_approx_seconds() > t1+F1_0*3) {
3942                         t1 = timer_get_approx_seconds();
3943                         network_send_game_list_request();
3944                 }
3945         #ifdef MACINTOSH
3946         } else if (timer_get_approx_seconds() > t1+F1_0*20) {
3947                 hide_cursor();
3948                 t1 = timer_get_approx_seconds();
3949                 restart_net_searching(menus);
3950                 show_boxed_message("Requesting list of Netgames");
3951                 network_send_game_list_request();
3952                 clear_boxed_message();
3953                 show_cursor();
3954         #endif
3955         }
3956    
3957    temp=num_active_games;
3958         
3959         network_listen();
3960
3961    NumActiveNetgames=num_active_games;
3962
3963    if (!Network_games_changed)
3964      return;
3965
3966    if (temp!=num_active_games)
3967            digi_play_sample (SOUND_HUD_MESSAGE,F1_0);
3968  
3969         Network_games_changed = 0;
3970    mprintf ((0,"JOIN POLL: I'm looking at %d games!\n",num_active_games));
3971
3972         // Copy the active games data into the menu options
3973         for (i = 0; i < num_active_games; i++)
3974         {
3975                 int game_status = Active_games[i].game_status;
3976       int j,x, k,tx,ty,ta,nplayers = 0;
3977       char levelname[8],MissName[25],GameName[25],thold[2];
3978       thold[1]=0;
3979
3980       // These next two loops protect against menu skewing
3981       // if missiontitle or gamename contain a tab
3982                  
3983       for (x=0,tx=0,k=0,j=0;j<15;j++)
3984         {
3985           if (Active_games[i].mission_title[j]=='\t')
3986             continue;
3987           thold[0]=Active_games[i].mission_title[j];
3988           gr_get_string_size (thold,&tx,&ty,&ta);
3989  
3990           if ((x+=tx)>=LHX(55))
3991             {
3992               MissName[k]=MissName[k+1]=MissName[k+2]='.';
3993               k+=3;
3994               break;
3995             }
3996           
3997           MissName[k++]=Active_games[i].mission_title[j];
3998         }
3999       MissName[k]=0;
4000
4001       for (x=0,tx=0,k=0,j=0;j<15;j++)
4002         {
4003           if (Active_games[i].game_name[j]=='\t')
4004             continue;
4005           thold[0]=Active_games[i].game_name[j];
4006           gr_get_string_size (thold,&tx,&ty,&ta);
4007
4008           if ((x+=tx)>=LHX(55))
4009             {
4010               GameName[k]=GameName[k+1]=GameName[k+2]='.';
4011               k+=3;
4012               break;
4013             }
4014           GameName[k++]=Active_games[i].game_name[j];
4015         }
4016       GameName[k]=0;
4017
4018            
4019       nplayers=Active_games[i].numconnected;
4020
4021                 if (Active_games[i].levelnum < 0)
4022                         sprintf(levelname, "S%d", -Active_games[i].levelnum);
4023                 else 
4024                         sprintf(levelname, "%d", Active_games[i].levelnum);
4025           
4026                 if (game_status == NETSTAT_STARTING) 
4027                 {
4028         sprintf (menus[i+2].text,"%d.\t%s \t%s \t  %d/%d \t%s \t %s \t%s",
4029                  i+1,GameName,ModeLetters[Active_games[i].gamemode],nplayers,
4030                  Active_games[i].max_numplayers,MissName,levelname,"Forming");
4031                 }
4032                 else if (game_status == NETSTAT_PLAYING)
4033                 {
4034                  join_status=can_join_netgame(&Active_games[i],NULL);   
4035 //               mprintf ((0,"Joinstatus=%d\n",join_status));
4036                 
4037         if (join_status==1)
4038             sprintf (menus[i+2].text,"%d.\t%s \t%s \t  %d/%d \t%s \t %s \t%s",
4039                      i+1,GameName,ModeLetters[Active_games[i].gamemode],nplayers,
4040                      Active_games[i].max_numplayers,MissName,levelname,"Open");
4041                         else if (join_status==2)
4042             sprintf (menus[i+2].text,"%d.\t%s \t%s \t  %d/%d \t%s \t %s \t%s",
4043                      i+1,GameName,ModeLetters[Active_games[i].gamemode],nplayers,
4044                      Active_games[i].max_numplayers,MissName,levelname,"Full");
4045                         else if (join_status==3)
4046             sprintf (menus[i+2].text,"%d.\t%s \t%s \t  %d/%d \t%s \t %s \t%s",
4047                      i+1,GameName,ModeLetters[Active_games[i].gamemode],nplayers,
4048                      Active_games[i].max_numplayers,MissName,levelname,"Restrict");
4049                         else
4050             sprintf (menus[i+2].text,"%d.\t%s \t%s \t  %d/%d \t%s \t %s \t%s",
4051                      i+1,GameName,ModeLetters[Active_games[i].gamemode],nplayers,
4052                      Active_games[i].max_numplayers,MissName,levelname,"Closed");
4053
4054                 }
4055                 else
4056          sprintf (menus[i+2].text,"%d.\t%s \t%s \t  %d/%d \t%s \t %s \t%s",
4057                   i+1,GameName,ModeLetters[Active_games[i].gamemode],nplayers,
4058                   Active_games[i].max_numplayers,MissName,levelname,"Between");
4059                  
4060
4061       Assert(strlen(menus[i+2].text) < 100);
4062       menus[i+2].redraw = 1;
4063         }
4064
4065         for (i = num_active_games; i < MAX_ACTIVE_NETGAMES; i++)
4066         {
4067                 sprintf(menus[i+2].text, "%d.                                                     ",i+1);
4068                 menus[i+2].redraw = 1;
4069         }
4070 }
4071
4072 int
4073 network_wait_for_sync(void)
4074 {
4075         char text[60];
4076         newmenu_item m[2];
4077         int i, choice;
4078         
4079         Network_status = NETSTAT_WAITING;
4080
4081         m[0].type=NM_TYPE_TEXT; m[0].text = text;
4082         m[1].type=NM_TYPE_TEXT; m[1].text = TXT_NET_LEAVE;
4083         
4084         i = network_send_request();
4085
4086         if (i < 0)
4087                 return(-1);
4088
4089         sprintf( m[0].text, "%s\n'%s' %s", TXT_NET_WAITING, NetPlayers.players[i].callsign, TXT_NET_TO_ENTER );
4090
4091 menu:   
4092         choice=newmenu_do( NULL, TXT_WAIT, 2, m, network_sync_poll );
4093
4094         if (choice > -1)
4095                 goto menu;
4096
4097         if (Network_status != NETSTAT_PLAYING)  
4098         {
4099                 sequence_packet me;
4100
4101 //              if (Network_status == NETSTAT_ENDLEVEL)
4102 //              {
4103 //                      network_send_endlevel_packet(0);
4104 //                      longjmp(LeaveGame, 0);
4105 //              }               
4106
4107                 mprintf((0, "Aborting join.\n"));
4108                 me.type = PID_QUIT_JOINING;
4109                 memcpy( me.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1 );
4110                 if (Network_game_type == IPX_GAME) {
4111                         memcpy( me.player.network.ipx.node, ipx_get_my_local_address(), 6 );
4112                         memcpy( me.player.network.ipx.server, ipx_get_my_server_address(), 4 );
4113                         #ifndef MACINTOSH
4114                         ipx_send_internetwork_packet_data( (ubyte *)&me, sizeof(sequence_packet), NetPlayers.players[0].network.ipx.server, NetPlayers.players[0].network.ipx.node );
4115                         #else
4116                         send_sequence_packet(me, NetPlayers.players[0].network.ipx.server, NetPlayers.players[0].network.ipx.node, NULL);
4117                         #endif
4118                 #ifdef MACINTOSH
4119                 } else {
4120                         me.player.network.appletalk.node = appletalk_get_my_node();
4121                         me.player.network.appletalk.net = appletalk_get_my_net();
4122                         me.player.network.appletalk.socket = appletalk_get_my_socket();
4123                 #endif
4124                 }
4125                 N_players = 0;
4126                 Function_mode = FMODE_MENU;
4127                 Game_mode = GM_GAME_OVER;
4128                 return(-1);     // they cancelled               
4129         }
4130         return(0);
4131 }
4132
4133 void 
4134 network_request_poll( int nitems, newmenu_item * menus, int * key, int citem )
4135 {
4136         // Polling loop for waiting-for-requests menu
4137
4138         int i = 0;
4139         int num_ready = 0;
4140
4141         menus = menus;
4142         citem = citem;
4143         nitems = nitems;
4144         key = key;
4145
4146         // Send our endlevel packet at regular intervals
4147
4148 //      if (timer_get_approx_seconds() > t1+ENDLEVEL_SEND_INTERVAL)
4149 //      {
4150 //              network_send_endlevel_packet();
4151 //              t1 = timer_get_approx_seconds();
4152 //      }
4153
4154         network_listen();
4155
4156         for (i = 0; i < N_players; i++)
4157         {
4158                 if ((Players[i].connected == 1) || (Players[i].connected == 0))
4159                         num_ready++;
4160         }
4161
4162         if (num_ready == N_players) // All players have checked in or are disconnected
4163         {
4164                 *key = -2;
4165         }
4166 }
4167
4168 void
4169 network_wait_for_requests(void)
4170 {
4171         // Wait for other players to load the level before we send the sync
4172         int choice, i;
4173         newmenu_item m[1];
4174         
4175         Network_status = NETSTAT_WAITING;
4176
4177         m[0].type=NM_TYPE_TEXT; m[0].text = TXT_NET_LEAVE;
4178
4179         mprintf((0, "Entered wait_for_requests : N_players = %d.\n", N_players));
4180
4181         for (choice = 0; choice < N_players; choice++)
4182                 mprintf((0, "Players[%d].connected = %d.\n", choice, Players[choice].connected));
4183
4184         Network_status = NETSTAT_WAITING;
4185         network_flush();
4186
4187         Players[Player_num].connected = 1;
4188
4189 menu:
4190         choice = newmenu_do(NULL, TXT_WAIT, 1, m, network_request_poll);        
4191
4192         if (choice == -1)
4193         {
4194                 // User aborted
4195                 choice = nm_messagebox(NULL, 3, TXT_YES, TXT_NO, TXT_START_NOWAIT, TXT_QUITTING_NOW);
4196                 if (choice == 2)
4197                         return;
4198                 if (choice != 0)
4199                         goto menu;
4200                 
4201                 // User confirmed abort
4202                 
4203                 for (i=0; i < N_players; i++) {
4204                         if ((Players[i].connected != 0) && (i != Player_num)) {
4205                                 if (Network_game_type == IPX_GAME)
4206                                         network_dump_player(NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, DUMP_ABORTED);
4207                                 #ifdef MACINTOSH
4208                                 else
4209                                         network_dump_appletalk_player(NetPlayers.players[i].network.appletalk.node, NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket, DUMP_ABORTED);
4210                                 #endif
4211                         }
4212                 }
4213
4214                 #ifdef MACINTOSH
4215                 if (Network_game_type == APPLETALK_GAME)
4216                         network_release_registered_game();
4217                 #endif
4218                 longjmp(LeaveGame, 0);  
4219         }
4220         else if (choice != -2)
4221                 goto menu;
4222 }
4223
4224 int
4225 network_level_sync(void)
4226 {
4227         // Do required syncing between (before) levels
4228
4229         int result;
4230
4231         mprintf((0, "Player %d entering network_level_sync.\n", Player_num));
4232         
4233         MySyncPackInitialized = 0;
4234
4235 //      my_segments_checksum = netmisc_calc_checksum(Segments, sizeof(segment)*(Highest_segment_index+1));
4236
4237         network_flush(); // Flush any old packets
4238
4239         if (N_players == 0)
4240                 result = network_wait_for_sync();
4241         else if (network_i_am_master())
4242         {
4243                 network_wait_for_requests();
4244                 network_send_sync();
4245                 result = 0;
4246         }
4247         else
4248                 result = network_wait_for_sync();
4249
4250    network_count_powerups_in_mine();
4251
4252         if (result)
4253         {
4254                 Players[Player_num].connected = 0;
4255                 network_send_endlevel_packet();
4256                 #ifdef MACINTOSH
4257                 if (Network_game_type == APPLETALK_GAME)
4258                         network_release_registered_game();
4259                 #endif
4260                 longjmp(LeaveGame, 0);
4261         }
4262         return(0);
4263 }
4264
4265 void network_count_powerups_in_mine(void)
4266  {
4267   int i;
4268
4269   for (i=0;i<MAX_POWERUP_TYPES;i++)
4270         PowerupsInMine[i]=0;
4271         
4272   for (i=0;i<=Highest_object_index;i++) 
4273         {
4274          if (Objects[i].type==OBJ_POWERUP)
4275           {
4276                 PowerupsInMine[Objects[i].id]++;
4277                 if (multi_powerup_is_4pack(Objects[i].id))
4278                    PowerupsInMine[Objects[i].id-1]+=4;
4279           }
4280         }
4281                   
4282  }
4283
4284 #ifdef MACINTOSH
4285
4286 // code to release the NBP binding of an appletalk game
4287
4288 void network_release_registered_game()
4289 {
4290         if (Network_game_type == APPLETALK_GAME)
4291                 appletalk_remove_netgame();
4292 }
4293
4294 // code to sort zone lists....
4295
4296 int zone_sort_func(const char **e0, const char **e1)
4297 {
4298         return strcmp(*e0, *e1);
4299 }
4300
4301 void network_get_appletalk_zone()
4302 {
4303         int num_zones, i, item, default_item;
4304         char **zone_list;
4305         char default_zone[MAX_ZONE_LENGTH];                     // my zone
4306
4307         Network_zone_name[0] = '\0';
4308         
4309         show_boxed_message("Looking for AppleTalk Zones");
4310         num_zones = appletalk_get_zone_names(&zone_list);
4311         clear_boxed_message();
4312
4313         if (num_zones < 0)      {               // error in getting zone list...maybe no router available....
4314                 if ( (num_zones == tooManyReqs) || (num_zones == noDataArea) ){
4315                         nm_messagebox(NULL, 1, TXT_OK, "AppleTalk Network is too busy.\nPlease try again shortly.");
4316                         longjmp(LeaveGame,0);
4317                 }
4318                 num_zones = 0;
4319         }
4320         
4321         if (num_zones == 0) {
4322                 strcpy(Network_zone_name, DEFAULT_ZONE_NAME);
4323                 return;
4324         }
4325         
4326         if (num_zones == 1) {
4327                 Network_zone_name[0] = (char)(strlen(zone_list[0]));
4328                 memcpy( &(Network_zone_name[1]), zone_list[0], strlen(zone_list[0]) );
4329                 goto zone_done;
4330         }
4331         
4332 // sort the zone names
4333
4334         for (i = 0; i < num_zones; i++)
4335                 strlwr(zone_list[i]);
4336
4337         qsort(zone_list, num_zones, sizeof(char *), zone_sort_func);
4338
4339 // get my current zone so we can highlight that one first
4340
4341         if (appletalk_get_my_zone(default_zone))
4342                 default_item = 0;
4343         else {
4344                 for (i = 0; i < num_zones; i++) {
4345                         if ( !stricmp(zone_list[i], default_zone) ) {
4346                                 default_item = i;
4347                                 break;
4348                         }
4349                 }
4350         }
4351
4352 rezone:         
4353         item = newmenu_listbox1("AppleTalk Zones", num_zones, zone_list, 0, default_item, NULL);
4354         
4355         if (item == -1)
4356                 goto rezone;
4357                 
4358         Network_zone_name[0] = (char)(strlen(zone_list[item]));
4359         memcpy( &(Network_zone_name[1]), zone_list[item], strlen(zone_list[item]) );
4360         
4361 zone_done:
4362         for (i = 0; i < num_zones; i++)
4363                 d_free(zone_list[i]);
4364         d_free(zone_list);
4365 }
4366 #endif
4367
4368 void nm_draw_background1(char * filename);
4369
4370 void network_join_game()
4371 {
4372         int choice, i;
4373         char menu_text[MAX_ACTIVE_NETGAMES+2][200];
4374         
4375         newmenu_item m[MAX_ACTIVE_NETGAMES+2];
4376
4377         if (Network_game_type == IPX_GAME) {
4378                 if ( !Network_active )
4379                 {
4380                         nm_messagebox(NULL, 1, TXT_OK, TXT_IPX_NOT_FOUND);
4381                         return;
4382                 }
4383         #ifdef MACINTOSH
4384         } else if (Appletalk_active <= 0) {
4385                 char buf[256];
4386                 
4387                 switch (Appletalk_active) {
4388                         case APPLETALK_NOT_OPEN:
4389                                 sprintf(buf, "Appletalk is not currently active.\nPlease enable AppleTalk from the\nChooser and restart Descent.");
4390                                 break;
4391                         case APPLETALK_BAD_LISTENER:
4392                                 sprintf(buf, "The Resource Fork of Descent appears damaged.\nPlease re-install Descent or contact\nMacPlay technical support.");
4393                                 break;
4394                         case APPLETALK_NO_LOCAL_ADDR:
4395                                 sprintf(buf, "Wow! Strange!\n\nNo Local Address.");
4396                                 break;
4397                         case APPLETALK_NO_SOCKET:
4398                                 sprintf(buf, "All AppleTalk sockets are in use.\nTry shutting down other network\napplications and restarting Descent.\n");
4399                                 break;
4400                 }
4401                 nm_messagebox(NULL, 1, TXT_OK, buf);
4402                 return;
4403         #endif
4404         }
4405
4406         network_init();
4407
4408         N_players = 0;
4409
4410         setjmp(LeaveGame);
4411         
4412         #ifdef MACINTOSH
4413         if (Network_game_type == APPLETALK_GAME)
4414                 network_get_appletalk_zone();
4415         #endif
4416         
4417         Network_send_objects = 0; 
4418         Network_sending_extras=0;
4419         Network_rejoined=0;
4420           
4421         Network_status = NETSTAT_BROWSING; // We are looking at a game menu
4422         
4423    network_flush();
4424         network_listen();  // Throw out old info
4425
4426         #ifdef MACINTOSH
4427         if (Network_game_type == IPX_GAME)
4428         #endif          // note link to if
4429                 network_send_game_list_request(); // broadcast a request for lists
4430
4431         num_active_games = 0;
4432
4433    memset(m, 0, sizeof(newmenu_item)*MAX_ACTIVE_NETGAMES);
4434    memset(Active_games, 0, sizeof(netgame_info)*MAX_ACTIVE_NETGAMES);
4435    memset(ActiveNetPlayers,0,sizeof(AllNetPlayers_info)*MAX_ACTIVE_NETGAMES);
4436         
4437         gr_set_fontcolor(BM_XRGB(15,15,23),-1);
4438
4439         m[0].text = menu_text[0];
4440         m[0].type = NM_TYPE_TEXT;
4441         if (Network_game_type == IPX_GAME) {
4442                 if (Network_allow_socket_changes)
4443                         sprintf( m[0].text, "\tCurrent IPX Socket is default %+d (PgUp/PgDn to change)", Network_socket );
4444                 else
4445                         strcpy( m[0].text, "" );
4446         #ifdef MACINTOSH
4447         } else {
4448                 p2cstr(Network_zone_name);
4449                 if (strcmp(Network_zone_name, "*"))             // only print if there is a zone name
4450                         sprintf(m[0].text, "\tCurrent Zone is %s", Network_zone_name);          // is Network_zone_name a pascal string????
4451                 c2pstr(Network_zone_name);
4452         #endif
4453         }
4454
4455         m[1].text=menu_text[1];
4456         m[1].type=NM_TYPE_TEXT;
4457         sprintf (m[1].text,"\tGAME \tMODE \t#PLYRS \tMISSION \tLEV \tSTATUS");
4458
4459         for (i = 0; i < MAX_ACTIVE_NETGAMES; i++) {
4460                 m[i+2].text = menu_text[i+2];
4461                 m[i+2].type = NM_TYPE_MENU;
4462                 sprintf(m[i+2].text, "%d.                                                               ", i+1);
4463                 m[i+2].redraw = 1;
4464         }
4465
4466         Network_games_changed = 1;      
4467 remenu:
4468         SurfingNet=1;
4469         nm_draw_background1(Menu_pcx_name);             //load this here so if we abort after loading level, we restore the palette
4470         gr_palette_load(gr_palette);
4471    ExtGameStatus=GAMESTAT_JOIN_NETGAME;
4472         choice=newmenu_dotiny("NETGAMES", NULL,MAX_ACTIVE_NETGAMES+2, m, network_join_poll);
4473         SurfingNet=0;
4474
4475         if (choice==-1) {
4476                 Network_status = NETSTAT_MENU;
4477                 return; // they cancelled               
4478         }               
4479    choice-=2;
4480
4481         if (choice >=num_active_games)
4482         {
4483                 nm_messagebox(TXT_SORRY, 1, TXT_OK, TXT_INVALID_CHOICE);
4484                 goto remenu;
4485         }
4486
4487         // Choice has been made and looks legit
4488         if (Active_games[choice].game_status == NETSTAT_ENDLEVEL)
4489         {
4490                 nm_messagebox(TXT_SORRY, 1, TXT_OK, TXT_NET_GAME_BETWEEN2);
4491                 goto remenu;
4492         }
4493
4494         if (Active_games[choice].protocol_version != MULTI_PROTO_VERSION)
4495         {
4496                 if (Active_games[choice].protocol_version == 3) {
4497                         #ifndef SHAREWARE
4498                                 nm_messagebox(TXT_SORRY, 1, TXT_OK, "Your version of Descent 2\nis incompatible with the\nDemo version");
4499                         #endif
4500                 }
4501                 else if (Active_games[choice].protocol_version == 4) {
4502                         #ifdef SHAREWARE
4503                                 nm_messagebox(TXT_SORRY, 1, TXT_OK, "This Demo version of\nDescent 2 is incompatible\nwith the full commercial version");
4504                         #endif
4505                 }
4506                 else
4507                         nm_messagebox(TXT_SORRY, 1, TXT_OK, TXT_VERSION_MISMATCH);
4508
4509                 goto remenu;
4510         }
4511
4512 #ifndef SHAREWARE
4513         {       
4514                 // Check for valid mission name
4515                         mprintf((0, "Loading mission:%s.\n", Active_games[choice].mission_name));
4516                         if (!load_mission_by_name(Active_games[choice].mission_name))
4517                         {
4518                                 nm_messagebox(NULL, 1, TXT_OK, TXT_MISSION_NOT_FOUND);
4519                                 goto remenu;
4520                         }
4521         }
4522 #endif
4523
4524 #if defined (D2_OEM)
4525         {
4526                 if (Active_games[choice].levelnum>8)
4527                  {
4528                                 nm_messagebox(NULL, 1, TXT_OK, "This OEM version only supports\nthe first 8 levels!");
4529                                 goto remenu;
4530                  }
4531         }
4532 #endif                  
4533
4534      if (!network_wait_for_all_info (choice))
4535                 {
4536                   nm_messagebox (TXT_SORRY,1,TXT_OK,"There was a join error!");
4537                   Network_status = NETSTAT_BROWSING; // We are looking at a game menu
4538                   goto remenu;
4539                 }       
4540           
4541           Network_status = NETSTAT_BROWSING; // We are looking at a game menu
4542  
4543      if (!can_join_netgame(&Active_games[choice],&ActiveNetPlayers[choice]))
4544                         {
4545                                 if (Active_games[choice].numplayers == Active_games[choice].max_numplayers)
4546                                         nm_messagebox(TXT_SORRY, 1, TXT_OK, TXT_GAME_FULL);
4547                                 else
4548                                         nm_messagebox(TXT_SORRY, 1, TXT_OK, TXT_IN_PROGRESS);
4549                                 goto remenu;
4550                         }
4551
4552         // Choice is valid, prepare to join in
4553
4554         memcpy(&Netgame, &Active_games[choice], sizeof(netgame_info));
4555    memcpy (&NetPlayers,&ActiveNetPlayers[choice],sizeof(AllNetPlayers_info));
4556
4557         Difficulty_level = Netgame.difficulty;
4558         MaxNumNetPlayers = Netgame.max_numplayers;
4559         change_playernum_to(1);
4560
4561         #ifdef MACINTOSH
4562
4563 // register the joining player with NBP.  This will have the nice effect of a player wanting to
4564 // join to be able to see the netgame if this player were to become the master.
4565
4566         if (Network_game_type == APPLETALK_GAME) {
4567                 OSErr err;
4568                 int count = 0;
4569                 fix t1;
4570                 
4571                 show_boxed_message("Registering Netgame");
4572                 do {
4573                         err = appletalk_register_netgame( Active_games[choice].game_name, TickCount() );
4574                         t1 = timer_get_fixed_seconds() + F1_0;
4575                         while ( timer_get_fixed_seconds() < t1 ) ;
4576                         count++;
4577                 } while ( (err == nbpDuplicate) && (count != MAX_REGISTER_TRIES) );
4578                 clear_boxed_message();
4579                 if ( (err == tooManyReqs) || (count == MAX_REGISTER_TRIES) ) {
4580                         nm_messagebox(NULL, 1, TXT_OK, "AppleTalk Network is too busy.\nPlease try again shortly.");
4581                         goto remenu;
4582                 }
4583         }
4584         #endif
4585
4586         network_set_game_mode(Netgame.gamemode);
4587
4588         network_AdjustMaxDataSize ();
4589
4590         StartNewLevel(Netgame.levelnum, 0);
4591
4592         return;         // look ma, we're in a game!!!
4593 }
4594
4595 void network_AdjustMaxDataSize ()
4596  {
4597           
4598    if (Netgame.ShortPackets)
4599         MaxXDataSize=NET_XDATA_SIZE;
4600    else
4601       MaxXDataSize=NET_XDATA_SIZE;
4602  }
4603   
4604
4605 fix StartWaitAllTime=0;
4606 int WaitAllChoice=0;
4607 #define ALL_INFO_REQUEST_INTERVAL F1_0*3
4608
4609 void network_wait_all_poll( int nitems, newmenu_item * menus, int * key, int citem )
4610  {
4611   static fix t1=0;
4612
4613   menus=menus;
4614   nitems=nitems;
4615   citem=citem;
4616
4617   if (timer_get_approx_seconds() > t1+ALL_INFO_REQUEST_INTERVAL)
4618         {
4619                 network_send_all_info_request(PID_SEND_ALL_GAMEINFO,SecurityCheck);
4620                 t1 = timer_get_approx_seconds();
4621         }
4622
4623   network_do_big_wait(WaitAllChoice);  
4624    
4625   if(SecurityCheck==-1)
4626    *key=-2;
4627  }
4628  
4629 int network_wait_for_all_info (int choice)
4630  {
4631   int pick;
4632   
4633   newmenu_item m[2];
4634
4635   m[0].type=NM_TYPE_TEXT; m[0].text = "Press Escape to cancel";
4636
4637   WaitAllChoice=choice;
4638   StartWaitAllTime=timer_get_approx_seconds();
4639   SecurityCheck=Active_games[choice].Security;
4640   NetSecurityFlag=0;
4641
4642   GetMenu:
4643   pick=newmenu_do( NULL, "Connecting...", 1, m, network_wait_all_poll );
4644
4645   if (pick>-1 && SecurityCheck!=-1)
4646         goto GetMenu;
4647
4648   if (SecurityCheck==-1)
4649     {   
4650            SecurityCheck=0;     
4651            return (1);
4652          }
4653   SecurityCheck=0;      
4654   return (0);
4655  }
4656
4657 void network_do_big_wait(int choice)
4658 {
4659         int size;
4660         ubyte packet[IPX_MAX_DATA_SIZE],*data;
4661         AllNetPlayers_info *temp_info;
4662 #ifdef MACINTOSH
4663         AllNetPlayers_info info_struct;
4664         ubyte apacket[APPLETALK_MAX_DATA_SIZE];
4665 #endif
4666   
4667         #ifdef MACINTOSH
4668         if (Network_game_type == APPLETALK_GAME)
4669                 size=appletalk_get_packet_data( apacket );
4670         else
4671         #endif
4672                 size=ipx_get_packet_data( packet );
4673         
4674   if (size>0)
4675         {
4676                 #ifdef MACINTOSH
4677                 if (Network_game_type == APPLETALK_GAME)
4678                         data = apacket;
4679                 else
4680                 #endif
4681                         data = packet;
4682   
4683          switch (data[0])
4684      {  
4685                 case PID_GAME_INFO:
4686
4687                 if (Network_game_type == IPX_GAME) {
4688                         #ifndef MACINTOSH
4689                         memcpy((ubyte *)&TempNetInfo, data, sizeof(netgame_info));
4690                         #else
4691                         receive_netgame_packet(data, &TempNetInfo, 0);
4692                         #endif
4693                 } else {
4694                         memcpy((ubyte *)&TempNetInfo, data, sizeof(netgame_info));
4695                 }
4696                 mprintf ((0,"This is %s game with a security of %d\n",TempNetInfo.game_name,TempNetInfo.Security));
4697
4698       if (TempNetInfo.Security !=SecurityCheck)
4699                  {
4700                   mprintf ((0,"Bad security on big_wait...rejecting.\n"));      
4701                   break;
4702                  }
4703                       
4704       if (NetSecurityFlag==NETSECURITY_WAIT_FOR_GAMEINFO)
4705            {
4706                    if (TempPlayersInfo->Security==TempNetInfo.Security)
4707                         {
4708                           mprintf ((0,"EQUAL !: Game=%d Players=%d ",TempPlayersInfo->Security,TempNetInfo.Security));
4709                 if (TempPlayersInfo->Security==SecurityCheck)
4710                           {
4711                                         memcpy (&Active_games[choice],(ubyte *)&TempNetInfo,sizeof(netgame_info));
4712                                         memcpy (&ActiveNetPlayers[choice],TempPlayersInfo,sizeof(AllNetPlayers_info));
4713                                         SecurityCheck=-1;
4714                           }
4715                         }
4716            }
4717                 else
4718            {
4719               NetSecurityFlag=NETSECURITY_WAIT_FOR_PLAYERS;
4720               NetSecurityNum=TempNetInfo.Security;
4721                                     
4722               if (network_wait_for_playerinfo())
4723                         {
4724                            mprintf ((0,"HUH? Game=%d Player=%d\n",NetSecurityNum,TempPlayersInfo->Security));
4725                                 memcpy (&Active_games[choice],(ubyte *)&TempNetInfo,sizeof(netgame_info));
4726                                 memcpy (&ActiveNetPlayers[choice],TempPlayersInfo,sizeof(AllNetPlayers_info));
4727                                 SecurityCheck=-1;
4728                         }
4729               NetSecurityFlag=0;
4730               NetSecurityNum=0;
4731            }
4732         break;
4733       case PID_PLAYERSINFO:
4734                mprintf ((0,"Got a PID_PLAYERSINFO!\n"));
4735
4736                         if (Network_game_type == IPX_GAME) {
4737                                 #ifndef MACINTOSH
4738                                 temp_info=(AllNetPlayers_info *)data;
4739                                 #else
4740                                 receive_netplayers_packet(data, &info_struct);
4741                                 temp_info = &info_struct;
4742                                 #endif
4743                         } else {
4744                                 temp_info = (AllNetPlayers_info *)data;
4745                         }
4746                         if (temp_info->Security!=SecurityCheck) 
4747                                 break;     // If this isn't the guy we're looking for, move on
4748
4749                         memcpy (&TempPlayersBase,(ubyte *)&temp_info,sizeof(AllNetPlayers_info));
4750                         TempPlayersInfo=&TempPlayersBase;
4751                         WaitingForPlayerInfo=0;
4752                         NetSecurityNum=TempPlayersInfo->Security;
4753                         NetSecurityFlag=NETSECURITY_WAIT_FOR_GAMEINFO;
4754                         break;
4755            }
4756         }
4757 }
4758
4759 void network_leave_game()
4760
4761    int nsave;
4762                 
4763         network_do_frame(1, 1);
4764    
4765    #ifdef NETPROFILING
4766         fclose (SendLogFile);
4767            fclose (RecieveLogFile);
4768         #endif
4769
4770         if ((network_i_am_master()))
4771         {
4772                 while (Network_sending_extras>1 && Player_joining_extras!=-1)
4773                   network_send_extras();
4774    
4775                 Netgame.numplayers = 0;
4776            nsave=N_players;
4777            N_players=0;
4778                 network_send_game_info(NULL);
4779                 N_players=nsave;
4780                 
4781                 mprintf ((0,"HEY! I'm master and I've left.\n"));
4782         }
4783         
4784         Players[Player_num].connected = 0;
4785         network_send_endlevel_packet();
4786         change_playernum_to(0);
4787         Game_mode = GM_GAME_OVER;
4788    write_player_file();
4789
4790 //      WIN(ipx_destroy_read_thread());
4791
4792         network_flush();
4793 }
4794
4795 void network_flush()
4796 {
4797         #ifdef MACINTOSH
4798         ubyte apacket[APPLETALK_MAX_DATA_SIZE];
4799         #endif
4800         ubyte packet[IPX_MAX_DATA_SIZE];
4801
4802         #ifdef MACINTOSH
4803         if ( (Network_game_type == APPLETALK_GAME) && (Appletalk_active <= 0) )
4804                 return;
4805         #endif
4806         
4807         if ( Network_game_type == IPX_GAME )
4808                 if (!Network_active) return;
4809
4810         #ifdef MACINTOSH
4811         if (Network_game_type == APPLETALK_GAME)
4812                 while (appletalk_get_packet_data(apacket) > 0) ;
4813         else
4814         #endif
4815                 while (ipx_get_packet_data(packet) > 0) ;
4816 }
4817
4818 void network_listen()
4819 {
4820         #ifdef MACINTOSH
4821         ubyte apacket[APPLETALK_MAX_DATA_SIZE];
4822         #endif
4823         int size;
4824         ubyte packet[IPX_MAX_DATA_SIZE];
4825         int i,loopmax=999;
4826
4827         if (Network_status==NETSTAT_PLAYING && Netgame.ShortPackets && !Network_send_objects)
4828          {
4829                 loopmax=N_players*Netgame.PacketsPerSec;
4830          }
4831         
4832         #ifdef MACINTOSH
4833         if ( (Network_game_type == APPLETALK_GAME) && (Appletalk_active <= 0) )
4834                 return;
4835         #endif
4836
4837         if (Network_game_type == IPX_GAME)      
4838                 if (!Network_active) return;
4839
4840         if (!(Game_mode & GM_NETWORK) && (Function_mode == FMODE_GAME))
4841                 mprintf((0, "Calling network_listen() when not in net game.\n"));
4842
4843         WaitingForPlayerInfo=1;
4844         NetSecurityFlag=NETSECURITY_OFF;
4845
4846         i=1;
4847         if (Network_game_type == IPX_GAME) {
4848                 size = ipx_get_packet_data( packet );
4849                 while ( size > 0)       {
4850                         network_process_packet( packet, size );
4851                         if (++i>loopmax)
4852                          break;
4853                         size = ipx_get_packet_data( packet );
4854                 }
4855         #ifdef MACINTOSH
4856         } else {
4857                 size = appletalk_get_packet_data( apacket );
4858                 while ( size > 0)       {
4859                         network_process_packet( apacket, size );
4860                         if (++i>loopmax)
4861                          break;
4862                         size = appletalk_get_packet_data( apacket );
4863                 }
4864         #endif
4865         }
4866 }
4867
4868 int network_wait_for_playerinfo()
4869 {
4870         int size,retries=0;
4871         ubyte packet[IPX_MAX_DATA_SIZE];
4872         struct AllNetPlayers_info *TempInfo;
4873         fix basetime;
4874         ubyte id;
4875 #ifdef MACINTOSH
4876         AllNetPlayers_info info_struct;
4877         ubyte apacket[APPLETALK_MAX_DATA_SIZE];
4878 #endif
4879
4880         #ifdef MACINTOSH
4881         if ( (Network_game_type == APPLETALK_GAME) && (Appletalk_active <= 0) )
4882                 return;
4883         #endif
4884
4885         if ( Network_game_type == IPX_GAME)
4886                 if (!Network_active) return(0);
4887                 
4888       //  if (!WaitingForPlayerInfo)
4889         // return (1);
4890
4891         if (!(Game_mode & GM_NETWORK) && (Function_mode == FMODE_GAME))
4892                 {
4893         mprintf((0, "Calling network_wait_for_playerinfo() when not in net game.\n"));
4894                 }       
4895         if (Network_status==NETSTAT_PLAYING)
4896          {
4897                   Int3(); //MY GOD! Get Jason...this is the source of many problems
4898              return (0);
4899          }
4900    basetime=timer_get_approx_seconds();
4901
4902    while (WaitingForPlayerInfo && retries<50 && (timer_get_approx_seconds()<(basetime+(F1_0*5))))
4903       {
4904                 if (Network_game_type == IPX_GAME) {
4905                         size = ipx_get_packet_data( packet );
4906                         id = packet[0];
4907                 } else {
4908                         #ifdef MACINTOSH
4909                         size = appletalk_get_packet_data( apacket );
4910                         id = apacket[0];
4911                         #endif
4912                 }
4913
4914         if (size>0 && id==PID_PLAYERSINFO)
4915         {
4916                 #ifdef MACINTOSH
4917                 if (Network_game_type == APPLETALK_GAME) {
4918                         TempInfo = (AllNetPlayers_info *)apacket;
4919                 } else {
4920                         receive_netplayers_packet(packet, &info_struct);
4921                         TempInfo = &info_struct;
4922                 }
4923                 #else
4924                 TempInfo=(AllNetPlayers_info *)packet;
4925                 #endif
4926     
4927                 retries++;
4928
4929             if (NetSecurityFlag==NETSECURITY_WAIT_FOR_PLAYERS)
4930              {
4931               if (NetSecurityNum==TempInfo->Security)
4932                {
4933                 mprintf ((0,"HEYEQUAL: Player=%d Game=%d\n",TempInfo->Security,NetSecurityNum));
4934                 memcpy (&TempPlayersBase,(ubyte *)TempInfo,sizeof(AllNetPlayers_info));
4935                 TempPlayersInfo=&TempPlayersBase;
4936                 NetSecurityFlag=NETSECURITY_OFF;
4937                 NetSecurityNum=0;
4938                 WaitingForPlayerInfo=0;
4939                 return (1);
4940                }
4941               else
4942                continue;
4943              }
4944             else
4945              {
4946               mprintf ((0,"I'm original!\n"));
4947
4948               NetSecurityNum=TempInfo->Security;
4949               NetSecurityFlag=NETSECURITY_WAIT_FOR_GAMEINFO;
4950            
4951               memcpy (&TempPlayersBase,(ubyte *)TempInfo,sizeof(AllNetPlayers_info));
4952               TempPlayersInfo=&TempPlayersBase;
4953               WaitingForPlayerInfo=0;
4954               return (1);
4955              }
4956            }
4957          }
4958    return (0);
4959   }
4960
4961
4962 void network_send_data( ubyte * ptr, int len, int urgent )
4963 {
4964         char check;
4965
4966    #ifdef NETPROFILING
4967            TTSent[ptr[0]]++;  
4968            fprintf (SendLogFile,"Packet type: %d Len:%d Urgent:%d TT=%d\n",ptr[0],len,urgent,TTSent[ptr[0]]);
4969            fflush (SendLogFile);
4970         #endif
4971     
4972         if (Endlevel_sequence)
4973                 return;
4974
4975         if (!MySyncPackInitialized)     {
4976                 MySyncPackInitialized = 1;
4977                 memset( &MySyncPack, 0, sizeof(frame_info) );
4978         }
4979         
4980         if (urgent)
4981                 PacketUrgent = 1;
4982
4983         if ((MySyncPack.data_size+len) > MaxXDataSize ) {
4984                 check = ptr[0];
4985                 network_do_frame(1, 0);
4986                 if (MySyncPack.data_size != 0) {
4987                         mprintf((0, "%d bytes were added to data by network_do_frame!\n", MySyncPack.data_size));
4988                         Int3();
4989                 }
4990 //              Int3();         // Trying to send too much!
4991 //              return;
4992                 mprintf((0, "Packet overflow, sending additional packet, type %d len %d.\n", ptr[0], len));
4993                 Assert(check == ptr[0]);
4994         }
4995
4996         Assert(MySyncPack.data_size+len <= MaxXDataSize);
4997
4998         memcpy( &MySyncPack.data[MySyncPack.data_size], ptr, len );
4999         MySyncPack.data_size += len;
5000 }
5001
5002 void network_timeout_player(int playernum)
5003 {
5004         // Remove a player from the game if we haven't heard from them in 
5005         // a long time.
5006         int i, n = 0;
5007
5008         Assert(playernum < N_players);
5009         Assert(playernum > -1);
5010
5011         network_disconnect_player(playernum);
5012         create_player_appearance_effect(&Objects[Players[playernum].objnum]);
5013
5014         digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
5015
5016         HUD_init_message("%s %s", Players[playernum].callsign, TXT_DISCONNECTING);
5017         for (i = 0; i < N_players; i++)
5018                 if (Players[i].connected) 
5019                         n++;
5020
5021         if (n == 1)
5022         {
5023                 nm_messagebox(NULL, 1, TXT_OK, TXT_YOU_ARE_ONLY);
5024         }
5025 }
5026
5027 fix last_send_time = 0;
5028 fix last_timeout_check = 0;
5029
5030 #ifdef MACINTOSH
5031 void squish_short_frame_info(short_frame_info old_info, ubyte *data)
5032 {
5033         int loc = 0;
5034         int tmpi;
5035         short tmps;
5036         
5037         data[0] = old_info.type;                              loc++;  loc += 3;               // skip three for pad byte
5038         tmpi = swapint(old_info.numpackets);
5039         memcpy(&(data[loc]), &tmpi, 4);                                                 loc += 4;
5040
5041         memcpy(&(data[loc]), old_info.thepos.bytemat, 9);               loc += 9;
5042         tmps = INTEL_SHORT(old_info.thepos.xo);
5043         memcpy(&(data[loc]), &tmps, 2);                 loc += 2;
5044         tmps = INTEL_SHORT(old_info.thepos.yo);
5045         memcpy(&(data[loc]), &tmps, 2);                 loc += 2;
5046         tmps = INTEL_SHORT(old_info.thepos.zo);
5047         memcpy(&(data[loc]), &tmps, 2);                 loc += 2;
5048         tmps = INTEL_SHORT(old_info.thepos.segment);
5049         memcpy(&(data[loc]), &tmps, 2);    loc += 2;
5050         tmps = INTEL_SHORT(old_info.thepos.velx);
5051         memcpy(&(data[loc]), &tmps, 2);               loc += 2;
5052         tmps = INTEL_SHORT(old_info.thepos.vely);
5053         memcpy(&(data[loc]), &tmps, 2);               loc += 2;
5054         tmps = INTEL_SHORT(old_info.thepos.velz);
5055         memcpy(&(data[loc]), &tmps, 2);               loc += 2;
5056
5057         tmps = swapshort(old_info.data_size);
5058         memcpy(&(data[loc]), &tmps, 2);                                                 loc += 2;
5059
5060         data[loc] = old_info.playernum; loc++;
5061         data[loc] = old_info.obj_render_type; loc++;
5062         data[loc] = old_info.level_num; loc++;
5063         memcpy(&(data[loc]), old_info.data, old_info.data_size);
5064 }
5065 #endif
5066
5067 char NakedBuf[NET_XDATA_SIZE+4];
5068 int NakedPacketLen=0;
5069 int NakedPacketDestPlayer=-1;
5070
5071 void network_do_frame(int force, int listen)
5072 {
5073         int i;
5074         short_frame_info ShortSyncPack;
5075         static fix LastEndlevel=0;
5076
5077         if (!(Game_mode&GM_NETWORK)) return;
5078
5079         if ((Network_status != NETSTAT_PLAYING) || (Endlevel_sequence)) // Don't send postion during escape sequence...
5080                 goto listen;
5081
5082   if (NakedPacketLen)
5083         {
5084                 Assert (NakedPacketDestPlayer>-1);
5085 //         mprintf ((0,"Sending a naked packet to %s (%d bytes)!\n",Players[NakedPacketDestPlayer].callsign,NakedPacketLen));
5086                 if (Network_game_type == IPX_GAME) 
5087                         ipx_send_packet_data( (ubyte *)NakedBuf, NakedPacketLen, NetPlayers.players[NakedPacketDestPlayer].network.ipx.server, NetPlayers.players[NakedPacketDestPlayer].network.ipx.node,Players[NakedPacketDestPlayer].net_address );
5088                 #ifdef MACINTOSH
5089                 else
5090                         appletalk_send_packet_data( (ubyte *)NakedBuf, NakedPacketLen, NetPlayers.players[NakedPacketDestPlayer].network.appletalk.node, NetPlayers.players[NakedPacketDestPlayer].network.appletalk.net,NetPlayers.players[NakedPacketDestPlayer].network.appletalk.socket );
5091                 #endif
5092                 NakedPacketLen=0;
5093                 NakedPacketDestPlayer=-1;
5094    }
5095   
5096    if (WaitForRefuseAnswer && timer_get_approx_seconds()>(RefuseTimeLimit+(F1_0*12)))
5097                 WaitForRefuseAnswer=0;
5098                         
5099         last_send_time += FrameTime;
5100         last_timeout_check += FrameTime;
5101
5102    // Send out packet PacksPerSec times per second maximum... unless they fire, then send more often...
5103    if ( (last_send_time>F1_0/Netgame.PacketsPerSec) || (Network_laser_fired) || force || PacketUrgent )       {        
5104                 if ( Players[Player_num].connected )    {
5105                         int objnum = Players[Player_num].objnum;
5106                         PacketUrgent = 0;
5107
5108                         if (listen) {
5109                                 multi_send_robot_frame(0);
5110                                 multi_send_fire();              // Do firing if needed..
5111                         }
5112
5113                         last_send_time = 0;
5114
5115                         if (Netgame.ShortPackets)
5116                         {
5117 #ifdef MACINTOSH
5118                                 ubyte send_data[IPX_MAX_DATA_SIZE];
5119                                 int squished_size;
5120 #endif
5121                                 create_shortpos(&ShortSyncPack.thepos, Objects+objnum, 0);
5122                                 ShortSyncPack.type                                      = PID_PDATA;
5123                                 ShortSyncPack.playernum                         = Player_num;
5124                                 ShortSyncPack.obj_render_type           = Objects[objnum].render_type;
5125                                 ShortSyncPack.level_num                         = Current_level_num;
5126                                 ShortSyncPack.data_size                         = MySyncPack.data_size;
5127                                 memcpy (&ShortSyncPack.data[0],&MySyncPack.data[0],MySyncPack.data_size);
5128
5129                                 for (i=0; i<N_players; i++ )    {
5130                                         if ( (Players[i].connected) && (i!=Player_num ) )       {
5131                                                 MySyncPack.numpackets = Players[i].n_packets_sent++;
5132                                                 ShortSyncPack.numpackets=MySyncPack.numpackets;
5133                                                 if (Network_game_type == IPX_GAME) {
5134                                                         #ifndef MACINTOSH
5135                                                         ipx_send_packet_data( (ubyte *)&ShortSyncPack, sizeof(short_frame_info)-MaxXDataSize+MySyncPack.data_size, NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node,Players[i].net_address );
5136                                                         #else
5137                                                         squish_short_frame_info(ShortSyncPack, send_data);
5138                                                         ipx_send_packet_data( (ubyte *)send_data, IPX_SHORT_INFO_SIZE-MaxXDataSize+MySyncPack.data_size, NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node,Players[i].net_address );
5139                                                         #endif
5140                                                 #ifdef MACINTOSH
5141                                                 } else {
5142                                                         appletalk_send_packet_data( (ubyte *)&ShortSyncPack, sizeof(short_frame_info)-MaxXDataSize+MySyncPack.data_size, NetPlayers.players[i].network.appletalk.node, NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket );
5143                                                 #endif
5144                                                 }
5145                                         }
5146                                 }
5147                         }
5148                         else  // If long packets
5149                         {
5150                                 int send_data_size;
5151                                 
5152                                 MySyncPack.type                                 = PID_PDATA;
5153                                 MySyncPack.playernum                    = Player_num;
5154                                 MySyncPack.obj_render_type              = Objects[objnum].render_type;
5155                                 MySyncPack.level_num                    = Current_level_num;
5156                                 MySyncPack.obj_segnum                   = Objects[objnum].segnum;
5157                                 MySyncPack.obj_pos                              = Objects[objnum].pos;
5158                                 MySyncPack.obj_orient                   = Objects[objnum].orient;
5159                                 MySyncPack.phys_velocity                = Objects[objnum].mtype.phys_info.velocity;
5160                                 MySyncPack.phys_rotvel                  = Objects[objnum].mtype.phys_info.rotvel;
5161                                 
5162                                 send_data_size = MySyncPack.data_size;                  // do this so correct size data is sent
5163
5164                                 #ifdef MACINTOSH                        // do the swap stuff
5165                                 if (Network_game_type == IPX_GAME) {
5166                                         MySyncPack.obj_segnum = INTEL_SHORT(MySyncPack.obj_segnum);
5167                                         MySyncPack.obj_pos.x = INTEL_INT((int)MySyncPack.obj_pos.x);
5168                                         MySyncPack.obj_pos.y = INTEL_INT((int)MySyncPack.obj_pos.y);
5169                                         MySyncPack.obj_pos.z = INTEL_INT((int)MySyncPack.obj_pos.z);
5170                                         
5171                                         MySyncPack.obj_orient.rvec.x = INTEL_INT((int)MySyncPack.obj_orient.rvec.x);
5172                                         MySyncPack.obj_orient.rvec.y = INTEL_INT((int)MySyncPack.obj_orient.rvec.y);
5173                                         MySyncPack.obj_orient.rvec.z = INTEL_INT((int)MySyncPack.obj_orient.rvec.z);
5174                                         MySyncPack.obj_orient.uvec.x = INTEL_INT((int)MySyncPack.obj_orient.uvec.x);
5175                                         MySyncPack.obj_orient.uvec.y = INTEL_INT((int)MySyncPack.obj_orient.uvec.y);
5176                                         MySyncPack.obj_orient.uvec.z = INTEL_INT((int)MySyncPack.obj_orient.uvec.z);
5177                                         MySyncPack.obj_orient.fvec.x = INTEL_INT((int)MySyncPack.obj_orient.fvec.x);
5178                                         MySyncPack.obj_orient.fvec.y = INTEL_INT((int)MySyncPack.obj_orient.fvec.y);
5179                                         MySyncPack.obj_orient.fvec.z = INTEL_INT((int)MySyncPack.obj_orient.fvec.z);
5180                                         
5181                                         MySyncPack.phys_velocity.x = INTEL_INT((int)MySyncPack.phys_velocity.x);
5182                                         MySyncPack.phys_velocity.y = INTEL_INT((int)MySyncPack.phys_velocity.y);
5183                                         MySyncPack.phys_velocity.z = INTEL_INT((int)MySyncPack.phys_velocity.z);
5184                                 
5185                                         MySyncPack.phys_rotvel.x = INTEL_INT((int)MySyncPack.phys_rotvel.x);
5186                                         MySyncPack.phys_rotvel.y = INTEL_INT((int)MySyncPack.phys_rotvel.y);
5187                                         MySyncPack.phys_rotvel.z = INTEL_INT((int)MySyncPack.phys_rotvel.z);
5188                                         
5189                                         MySyncPack.data_size = INTEL_SHORT(MySyncPack.data_size);
5190                                 }
5191                                 #endif
5192
5193                                 for (i=0; i<N_players; i++ )    {
5194                                         if ( (Players[i].connected) && (i!=Player_num ) )       {
5195                                                 if (Network_game_type == IPX_GAME)
5196                                                         MySyncPack.numpackets = INTEL_INT(Players[i].n_packets_sent);
5197                                                 #ifdef MACINTOSH
5198                                                 else
5199                                                         MySyncPack.numpackets = Players[i].n_packets_sent;
5200                                                 #endif
5201
5202                                                 Players[i].n_packets_sent++;
5203                                                 if (Network_game_type == IPX_GAME)
5204                                                         ipx_send_packet_data( (ubyte *)&MySyncPack, sizeof(frame_info)-MaxXDataSize+send_data_size, NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node,Players[i].net_address );
5205                                                 #ifdef MACINTOSH
5206                                                 else
5207                                                         appletalk_send_packet_data( (ubyte *)&MySyncPack, sizeof(frame_info)-MaxXDataSize+send_data_size, NetPlayers.players[i].network.appletalk.node, NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket);
5208                                                 #endif
5209                                         }
5210                                 }
5211                         }
5212                                 
5213                         MySyncPack.data_size = 0;               // Start data over at 0 length.
5214                         if (Control_center_destroyed)
5215                         {
5216                                 if (Player_is_dead)
5217                                         Players[Player_num].connected=3;
5218                                 if (timer_get_approx_seconds() > (LastEndlevel+(F1_0/2)))
5219                                 {
5220                                         network_send_endlevel_packet();
5221                                         LastEndlevel=timer_get_approx_seconds();
5222                                 }
5223                         }
5224                         //mprintf( (0, "Packet has %d bytes appended (TS=%d)\n", MySyncPack.data_size, sizeof(frame_info)-MaxXDataSize+MySyncPack.data_size ));
5225                 }
5226         }
5227    
5228         if (!listen)
5229                 return;
5230
5231         if ((last_timeout_check > F1_0) && !(Control_center_destroyed))
5232         {
5233                 fix approx_time = timer_get_approx_seconds();
5234                 // Check for player timeouts
5235                 for (i = 0; i < N_players; i++)
5236                 {
5237                         if ((i != Player_num) && (Players[i].connected == 1))
5238                         {
5239                                 if ((LastPacketTime[i] == 0) || (LastPacketTime[i] > approx_time))
5240                                 {
5241                                         LastPacketTime[i] = approx_time;
5242                                         continue;
5243                                 }
5244                                 if ((approx_time - LastPacketTime[i]) > (15*F1_0))
5245                                         network_timeout_player(i);
5246                         }
5247                 }
5248                 last_timeout_check = 0;
5249         }
5250
5251 listen:
5252         if (!listen)
5253         {
5254                 MySyncPack.data_size = 0;
5255                 return;
5256         }
5257         network_listen();
5258
5259    if (VerifyPlayerJoined!=-1 && !(FrameCount & 63))
5260           resend_sync_due_to_packet_loss_for_allender(); // This will resend to network_player_rejoining
5261  
5262         if (Network_send_objects)
5263                 network_send_objects();
5264
5265         if (Network_sending_extras && VerifyPlayerJoined==-1)
5266         {
5267           network_send_extras();
5268           return;
5269     }
5270 }
5271
5272 int missed_packets = 0;
5273
5274 int ConsistencyCount = 0;
5275
5276 void network_consistency_error(void)
5277 {
5278         if (++ConsistencyCount < 10)
5279                 return;
5280
5281         Function_mode = FMODE_MENU;
5282         #ifndef MACINTOSH
5283         nm_messagebox(NULL, 1, TXT_OK, TXT_CONSISTENCY_ERROR);
5284         #else
5285         nm_messagebox(NULL, 1, TXT_OK, "Failed to join the netgame.\nYou are missing packets.  Check\nyour network connection and\ntry again.");
5286         #endif
5287         Function_mode = FMODE_GAME;
5288         ConsistencyCount = 0;
5289         multi_quit_game = 1;
5290         multi_leave_menu = 1;
5291         multi_reset_stuff();
5292         Function_mode = FMODE_MENU;
5293 }
5294
5295 void network_process_pdata (char *data)
5296  {
5297   Assert (Game_mode & GM_NETWORK);
5298  
5299   if (Netgame.ShortPackets)
5300         network_read_pdata_short_packet ((short_frame_info *)data);
5301   else
5302         network_read_pdata_packet ((frame_info *)data);
5303  }
5304
5305 void network_read_pdata_packet(frame_info *pd )
5306 {
5307         int TheirPlayernum;
5308         int TheirObjnum;
5309         object * TheirObj = NULL;
5310         
5311 // frame_info should be aligned...for mac, make the necessary adjustments
5312 #ifdef MACINTOSH
5313         if (Network_game_type == IPX_GAME) {
5314                 pd->numpackets = INTEL_INT(pd->numpackets);
5315                 pd->obj_pos.x = INTEL_INT(pd->obj_pos.x);
5316                 pd->obj_pos.y = INTEL_INT(pd->obj_pos.y);
5317                 pd->obj_pos.z = INTEL_INT(pd->obj_pos.z);
5318         
5319                 pd->obj_orient.rvec.x = (fix)INTEL_INT((int)pd->obj_orient.rvec.x);
5320                 pd->obj_orient.rvec.y = (fix)INTEL_INT((int)pd->obj_orient.rvec.y);
5321                 pd->obj_orient.rvec.z = (fix)INTEL_INT((int)pd->obj_orient.rvec.z);
5322                 pd->obj_orient.uvec.x = (fix)INTEL_INT((int)pd->obj_orient.uvec.x);
5323                 pd->obj_orient.uvec.y = (fix)INTEL_INT((int)pd->obj_orient.uvec.y);
5324                 pd->obj_orient.uvec.z = (fix)INTEL_INT((int)pd->obj_orient.uvec.z);
5325                 pd->obj_orient.fvec.x = (fix)INTEL_INT((int)pd->obj_orient.fvec.x);
5326                 pd->obj_orient.fvec.y = (fix)INTEL_INT((int)pd->obj_orient.fvec.y);
5327                 pd->obj_orient.fvec.z = (fix)INTEL_INT((int)pd->obj_orient.fvec.z);
5328         
5329                 pd->phys_velocity.x = (fix)INTEL_INT((int)pd->phys_velocity.x);
5330                 pd->phys_velocity.y = (fix)INTEL_INT((int)pd->phys_velocity.y);
5331                 pd->phys_velocity.z = (fix)INTEL_INT((int)pd->phys_velocity.z);
5332         
5333                 pd->phys_rotvel.x = (fix)INTEL_INT((int)pd->phys_rotvel.x);
5334                 pd->phys_rotvel.y = (fix)INTEL_INT((int)pd->phys_rotvel.y);
5335                 pd->phys_rotvel.z = (fix)INTEL_INT((int)pd->phys_rotvel.z);
5336                 
5337                 pd->obj_segnum = INTEL_SHORT(pd->obj_segnum);
5338                 pd->data_size = INTEL_SHORT(pd->data_size);
5339         }
5340 #endif
5341
5342         TheirPlayernum = pd->playernum;
5343         TheirObjnum = Players[pd->playernum].objnum;
5344         
5345         if (TheirPlayernum < 0) {
5346                 Int3(); // This packet is bogus!!
5347                 return;
5348         }
5349
5350    if (VerifyPlayerJoined!=-1 && TheirPlayernum==VerifyPlayerJoined)
5351          {
5352           // Hurray! Someone really really got in the game (I think).
5353      mprintf ((0,"Hurray! VPJ (%d) reset!\n",VerifyPlayerJoined));
5354      VerifyPlayerJoined=-1;
5355          }
5356  
5357         if (!multi_quit_game && (TheirPlayernum >= N_players)) {
5358                 if (Network_status!=NETSTAT_WAITING)
5359                  {
5360                         Int3(); // We missed an important packet!
5361                         network_consistency_error();
5362                         return;
5363                  }
5364                 else
5365                  return;
5366         }
5367         if (Endlevel_sequence || (Network_status == NETSTAT_ENDLEVEL) ) {
5368                 int old_Endlevel_sequence = Endlevel_sequence;
5369                 Endlevel_sequence = 1;
5370                 if ( pd->data_size>0 )  {
5371                         // pass pd->data to some parser function....
5372                         multi_process_bigdata( pd->data, pd->data_size );
5373                 }
5374                 Endlevel_sequence = old_Endlevel_sequence;
5375                 return;
5376         }
5377 //      mprintf((0, "Gametime = %d, Frametime = %d.\n", GameTime, FrameTime));
5378
5379         if ((byte)pd->level_num != Current_level_num)
5380         {
5381                 mprintf((0, "Got frame packet from player %d wrong level %d!\n", pd->playernum, pd->level_num));
5382                 return;
5383         }
5384
5385         TheirObj = &Objects[TheirObjnum];
5386
5387         //------------- Keep track of missed packets -----------------
5388         Players[TheirPlayernum].n_packets_got++;
5389         TotalPacketsGot++;
5390         LastPacketTime[TheirPlayernum] = timer_get_approx_seconds();
5391
5392         if  ( pd->numpackets != Players[TheirPlayernum].n_packets_got ) {
5393                 int missed_packets;
5394                 
5395                 missed_packets = pd->numpackets-Players[TheirPlayernum].n_packets_got;
5396                 if ((pd->numpackets-Players[TheirPlayernum].n_packets_got)>0)
5397                         TotalMissedPackets += pd->numpackets-Players[TheirPlayernum].n_packets_got;
5398
5399                         if ( missed_packets > 0 )       
5400                                 mprintf(( 0, "Missed %d packets from player #%d (%d total)\n", pd->numpackets-Players[TheirPlayernum].n_packets_got, TheirPlayernum, missed_packets ));
5401                         else
5402                                 mprintf( (0, "Got %d late packets from player #%d (%d total)\n", Players[TheirPlayernum].n_packets_got-pd->numpackets, TheirPlayernum, missed_packets ));
5403
5404                 #ifdef MACINTOSH
5405                 #ifdef APPLETALK_DEBUG
5406                 if (Network_game_type == APPLETALK_GAME) {
5407                         if ( missed_packets > 0 )       
5408                                 fprintf( at_fp, "Missed %d packets from player #%d (%d total)\n", pd->numpackets-Players[TheirPlayernum].n_packets_got, TheirPlayernum, missed_packets );
5409                         else
5410                                 fprintf( at_fp, "Got %d late packets from player #%d (%d total)\n", Players[TheirPlayernum].n_packets_got-pd->numpackets, TheirPlayernum, missed_packets );
5411                 }
5412                 #endif
5413                 
5414                 #ifdef IPX_DEBUG
5415                 if (Network_game_type == IPX_GAME) {
5416                         if ( missed_packets > 0 )       
5417                                 fprintf( ipx_fp, "Missed %d packets from player #%d (%d total)\n", pd->numpackets-Players[TheirPlayernum].n_packets_got, TheirPlayernum, missed_packets );
5418                         else
5419                                 fprintf( ipx_fp, "Got %d late packets from player #%d (%d total)\n", Players[TheirPlayernum].n_packets_got-pd->numpackets, TheirPlayernum, missed_packets );
5420                 }
5421                 #endif
5422                 #endif
5423
5424                 Players[TheirPlayernum].n_packets_got = pd->numpackets;
5425         }
5426
5427         //------------ Read the player's ship's object info ----------------------
5428         TheirObj->pos                           = pd->obj_pos;
5429         TheirObj->orient                        = pd->obj_orient;
5430         TheirObj->mtype.phys_info.velocity = pd->phys_velocity;
5431         TheirObj->mtype.phys_info.rotvel = pd->phys_rotvel;
5432
5433         if ((TheirObj->render_type != pd->obj_render_type) && (pd->obj_render_type == RT_POLYOBJ))
5434                 multi_make_ghost_player(TheirPlayernum);
5435
5436         obj_relink(TheirObjnum,pd->obj_segnum);
5437
5438         if (TheirObj->movement_type == MT_PHYSICS)
5439                 set_thrust_from_velocity(TheirObj);
5440
5441         //------------ Welcome them back if reconnecting --------------
5442         if (!Players[TheirPlayernum].connected) {
5443                 Players[TheirPlayernum].connected = 1;
5444
5445                 if (Newdemo_state == ND_STATE_RECORDING)
5446                         newdemo_record_multi_reconnect(TheirPlayernum);
5447
5448                 multi_make_ghost_player(TheirPlayernum);
5449
5450                 create_player_appearance_effect(&Objects[TheirObjnum]);
5451
5452                 digi_play_sample( SOUND_HUD_MESSAGE, F1_0);
5453                 
5454                 ClipRank (&NetPlayers.players[TheirPlayernum].rank);
5455
5456                 if (FindArg("-norankings"))      
5457                         HUD_init_message( "'%s' %s", Players[TheirPlayernum].callsign, TXT_REJOIN );
5458                 else
5459                         HUD_init_message( "%s'%s' %s", RankStrings[NetPlayers.players[TheirPlayernum].rank],Players[TheirPlayernum].callsign, TXT_REJOIN );
5460
5461
5462                 multi_send_score();
5463         }
5464
5465         //------------ Parse the extra data at the end ---------------
5466
5467         if ( pd->data_size>0 )  {
5468                 // pass pd->data to some parser function....
5469                 multi_process_bigdata( pd->data, pd->data_size );
5470         }
5471
5472 }
5473
5474 #ifdef MACINTOSH
5475 void get_short_frame_info(ubyte *old_info, short_frame_info *new_info)
5476 {
5477         int loc = 0;
5478         
5479         new_info->type = old_info[loc];                                                                 loc++;  loc += 3;               // skip three for pad byte
5480         memcpy(&(new_info->numpackets), &(old_info[loc]), 4);                   loc += 4;
5481         new_info->numpackets = INTEL_INT(new_info->numpackets);
5482         memcpy(new_info->thepos.bytemat, &(old_info[loc]), 9);                  loc += 9;
5483         memcpy(&(new_info->thepos.xo), &(old_info[loc]), 2);                    loc += 2;
5484         memcpy(&(new_info->thepos.yo), &(old_info[loc]), 2);                    loc += 2;
5485         memcpy(&(new_info->thepos.zo), &(old_info[loc]), 2);                    loc += 2;
5486         memcpy(&(new_info->thepos.segment), &(old_info[loc]), 2);               loc += 2;
5487         memcpy(&(new_info->thepos.velx), &(old_info[loc]), 2);                  loc += 2;
5488         memcpy(&(new_info->thepos.vely), &(old_info[loc]), 2);                  loc += 2;
5489         memcpy(&(new_info->thepos.velz), &(old_info[loc]), 2);                  loc += 2;
5490         new_info->thepos.xo = INTEL_SHORT(new_info->thepos.xo);
5491         new_info->thepos.yo = INTEL_SHORT(new_info->thepos.yo);
5492         new_info->thepos.zo = INTEL_SHORT(new_info->thepos.zo);
5493         new_info->thepos.segment = INTEL_SHORT(new_info->thepos.segment);
5494         new_info->thepos.velx = INTEL_SHORT(new_info->thepos.velx);
5495         new_info->thepos.vely = INTEL_SHORT(new_info->thepos.vely);
5496         new_info->thepos.velz = INTEL_SHORT(new_info->thepos.velz);
5497
5498         memcpy(&(new_info->data_size), &(old_info[loc]), 2);            loc += 2;
5499         new_info->data_size = INTEL_SHORT(new_info->data_size);
5500         new_info->playernum = old_info[loc];                                            loc++;
5501         new_info->obj_render_type = old_info[loc];                                      loc++;
5502         new_info->level_num = old_info[loc];                                            loc++;
5503         memcpy(new_info->data, &(old_info[loc]), new_info->data_size);
5504 }
5505 #endif
5506
5507 void network_read_pdata_short_packet(short_frame_info *pd )
5508 {
5509         int TheirPlayernum;
5510         int TheirObjnum;
5511         object * TheirObj = NULL;
5512         short_frame_info new_pd;
5513
5514 // short frame info is not aligned because of shortpos.  The mac
5515 // will call totally hacked and gross function to fix this up.
5516
5517         if (Network_game_type == IPX_GAME) {
5518                 #ifndef MACINTOSH
5519                 memcpy(&new_pd, (ubyte *)pd, sizeof(short_frame_info));
5520                 #else
5521                 get_short_frame_info((ubyte *)pd, &new_pd);
5522                 #endif
5523         } else {
5524                 memcpy(&new_pd, (ubyte *)pd, sizeof(short_frame_info));
5525         }
5526
5527         TheirPlayernum = new_pd.playernum;
5528         TheirObjnum = Players[new_pd.playernum].objnum;
5529
5530         if (TheirPlayernum < 0) {
5531                 Int3(); // This packet is bogus!!
5532                 return;
5533         }
5534         if (!multi_quit_game && (TheirPlayernum >= N_players)) {
5535                 if (Network_status!=NETSTAT_WAITING)
5536                  {
5537                         Int3(); // We missed an important packet!
5538                         network_consistency_error();
5539                         return;
5540                  }
5541                 else
5542                  return;
5543         }
5544
5545    if (VerifyPlayerJoined!=-1 && TheirPlayernum==VerifyPlayerJoined)
5546          {
5547           // Hurray! Someone really really got in the game (I think).
5548       mprintf ((0,"Hurray! VPJ (%d) reset!\n",VerifyPlayerJoined));
5549       VerifyPlayerJoined=-1;
5550          }
5551
5552         if (Endlevel_sequence || (Network_status == NETSTAT_ENDLEVEL) ) {
5553                 int old_Endlevel_sequence = Endlevel_sequence;
5554                 Endlevel_sequence = 1;
5555                 if ( new_pd.data_size>0 )       {
5556                         // pass pd->data to some parser function....
5557                         multi_process_bigdata( new_pd.data, new_pd.data_size );
5558                 }
5559                 Endlevel_sequence = old_Endlevel_sequence;
5560                 return;
5561         }
5562 //      mprintf((0, "Gametime = %d, Frametime = %d.\n", GameTime, FrameTime));
5563
5564         if ((byte)new_pd.level_num != Current_level_num)
5565         {
5566                 mprintf((0, "Got frame packet from player %d wrong level %d!\n", new_pd.playernum, new_pd.level_num));
5567                 return;
5568         }
5569
5570         TheirObj = &Objects[TheirObjnum];
5571
5572         //------------- Keep track of missed packets -----------------
5573         Players[TheirPlayernum].n_packets_got++;
5574         TotalPacketsGot++;
5575         LastPacketTime[TheirPlayernum] = timer_get_approx_seconds();
5576
5577         if  ( new_pd.numpackets != Players[TheirPlayernum].n_packets_got )      {
5578                 int missed_packets;
5579         
5580                 missed_packets = new_pd.numpackets-Players[TheirPlayernum].n_packets_got;
5581                 if ((new_pd.numpackets-Players[TheirPlayernum].n_packets_got)>0)
5582                         TotalMissedPackets += new_pd.numpackets-Players[TheirPlayernum].n_packets_got;
5583
5584                 if ( missed_packets > 0 )       
5585                         mprintf( (0, "Missed %d packets from player #%d (%d total)\n", new_pd.numpackets-Players[TheirPlayernum].n_packets_got, TheirPlayernum, missed_packets ));
5586                 else
5587                         mprintf( (0, "Got %d late packets from player #%d (%d total)\n", Players[TheirPlayernum].n_packets_got-new_pd.numpackets, TheirPlayernum, missed_packets ));
5588
5589                 Players[TheirPlayernum].n_packets_got = new_pd.numpackets;
5590         }
5591
5592         //------------ Read the player's ship's object info ----------------------
5593
5594         extract_shortpos(TheirObj, &new_pd.thepos, 0);
5595
5596         if ((TheirObj->render_type != new_pd.obj_render_type) && (new_pd.obj_render_type == RT_POLYOBJ))
5597                 multi_make_ghost_player(TheirPlayernum);
5598
5599         if (TheirObj->movement_type == MT_PHYSICS)
5600                 set_thrust_from_velocity(TheirObj);
5601
5602         //------------ Welcome them back if reconnecting --------------
5603         if (!Players[TheirPlayernum].connected) {
5604                 Players[TheirPlayernum].connected = 1;
5605
5606                 if (Newdemo_state == ND_STATE_RECORDING)
5607                         newdemo_record_multi_reconnect(TheirPlayernum);
5608
5609                 multi_make_ghost_player(TheirPlayernum);
5610
5611                 create_player_appearance_effect(&Objects[TheirObjnum]);
5612
5613                 digi_play_sample( SOUND_HUD_MESSAGE, F1_0);
5614                 ClipRank (&NetPlayers.players[TheirPlayernum].rank);
5615                 
5616                 if (FindArg("-norankings"))
5617                         HUD_init_message( "'%s' %s", Players[TheirPlayernum].callsign, TXT_REJOIN );
5618                 else
5619                         HUD_init_message( "%s'%s' %s", RankStrings[NetPlayers.players[TheirPlayernum].rank],Players[TheirPlayernum].callsign, TXT_REJOIN );
5620
5621
5622                 multi_send_score();
5623         }
5624
5625         //------------ Parse the extra data at the end ---------------
5626
5627         if ( new_pd.data_size>0 )       {
5628                 // pass pd->data to some parser function....
5629                 multi_process_bigdata( new_pd.data, new_pd.data_size );
5630         }
5631
5632 }
5633
5634   
5635 void network_set_power (void)
5636  {
5637   int opt=0,choice,opt_primary,opt_second,opt_power;
5638   newmenu_item m[40];
5639   
5640   opt_primary=opt;
5641   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Laser upgrade"; m[opt].value=Netgame.DoLaserUpgrade; opt++;
5642   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Super lasers"; m[opt].value=Netgame.DoSuperLaser; opt++;
5643   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Quad Lasers"; m[opt].value=Netgame.DoQuadLasers; opt++;
5644   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Vulcan cannon"; m[opt].value=Netgame.DoVulcan; opt++;
5645   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Spreadfire cannon"; m[opt].value=Netgame.DoSpread; opt++;
5646   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Plasma cannon"; m[opt].value=Netgame.DoPlasma; opt++;
5647   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Fusion cannon"; m[opt].value=Netgame.DoFusions; opt++;
5648   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Gauss cannon"; m[opt].value=Netgame.DoGauss; opt++;
5649   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Helix cannon"; m[opt].value=Netgame.DoHelix; opt++;
5650   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Phoenix cannon"; m[opt].value=Netgame.DoPhoenix; opt++;
5651   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Omega cannon"; m[opt].value=Netgame.DoOmega; opt++;
5652   
5653   opt_second=opt;   
5654   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Homing Missiles"; m[opt].value=Netgame.DoHoming; opt++;
5655   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Proximity Bombs"; m[opt].value=Netgame.DoProximity; opt++;
5656   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Smart Missiles"; m[opt].value=Netgame.DoSmarts; opt++;
5657   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Mega Missiles"; m[opt].value=Netgame.DoMegas; opt++;
5658   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Flash Missiles"; m[opt].value=Netgame.DoFlash; opt++;
5659   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Guided Missiles"; m[opt].value=Netgame.DoGuided; opt++;
5660   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Smart Mines"; m[opt].value=Netgame.DoSmartMine; opt++;
5661   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Mercury Missiles"; m[opt].value=Netgame.DoMercury; opt++;
5662   m[opt].type = NM_TYPE_CHECK; m[opt].text = "EarthShaker Missiles"; m[opt].value=Netgame.DoEarthShaker; opt++;
5663
5664   opt_power=opt;
5665   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Invulnerability"; m[opt].value=Netgame.DoInvulnerability; opt++;
5666   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Cloaking"; m[opt].value=Netgame.DoCloak; opt++;
5667   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Afterburners"; m[opt].value=Netgame.DoAfterburner; opt++;
5668   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Ammo rack"; m[opt].value=Netgame.DoAmmoRack; opt++;
5669   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Energy Converter"; m[opt].value=Netgame.DoConverter; opt++;
5670   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Headlight"; m[opt].value=Netgame.DoHeadlight; opt++;
5671   
5672   choice = newmenu_do(NULL, "Objects to allow", opt, m, NULL);
5673
5674   Netgame.DoLaserUpgrade=m[opt_primary].value; 
5675   Netgame.DoSuperLaser=m[opt_primary+1].value;
5676   Netgame.DoQuadLasers=m[opt_primary+2].value;  
5677   Netgame.DoVulcan=m[opt_primary+3].value;
5678   Netgame.DoSpread=m[opt_primary+4].value;
5679   Netgame.DoPlasma=m[opt_primary+5].value;
5680   Netgame.DoFusions=m[opt_primary+6].value;
5681   Netgame.DoGauss=m[opt_primary+7].value;
5682   Netgame.DoHelix=m[opt_primary+8].value;
5683   Netgame.DoPhoenix=m[opt_primary+9].value;
5684   Netgame.DoOmega=m[opt_primary+10].value;
5685   
5686   Netgame.DoHoming=m[opt_second].value;
5687   Netgame.DoProximity=m[opt_second+1].value;
5688   Netgame.DoSmarts=m[opt_second+2].value;
5689   Netgame.DoMegas=m[opt_second+3].value;
5690   Netgame.DoFlash=m[opt_second+4].value;
5691   Netgame.DoGuided=m[opt_second+5].value;
5692   Netgame.DoSmartMine=m[opt_second+6].value;
5693   Netgame.DoMercury=m[opt_second+7].value;
5694   Netgame.DoEarthShaker=m[opt_second+8].value;
5695
5696   Netgame.DoInvulnerability=m[opt_power].value;
5697   Netgame.DoCloak=m[opt_power+1].value;
5698   Netgame.DoAfterburner=m[opt_power+2].value;
5699   Netgame.DoAmmoRack=m[opt_power+3].value;
5700   Netgame.DoConverter=m[opt_power+4].value;     
5701   Netgame.DoHeadlight=m[opt_power+5].value;     
5702   
5703  }
5704
5705 void SetAllAllowablesTo (int on)
5706  {
5707   last_cinvul = control_invul_time = 0;   //default to zero
5708    
5709   Netgame.DoMegas=Netgame.DoSmarts=Netgame.DoFusions=Netgame.DoHelix=\
5710   Netgame.DoPhoenix=Netgame.DoCloak=Netgame.DoInvulnerability=\
5711   Netgame.DoAfterburner=Netgame.DoGauss=Netgame.DoVulcan=Netgame.DoPlasma=on;
5712
5713   Netgame.DoOmega=Netgame.DoSuperLaser=Netgame.DoProximity=\
5714   Netgame.DoSpread=Netgame.DoMercury=Netgame.DoSmartMine=Netgame.DoFlash=\
5715   Netgame.DoGuided=Netgame.DoEarthShaker=on;
5716   
5717   Netgame.DoConverter=Netgame.DoAmmoRack=Netgame.DoHeadlight=on;
5718   Netgame.DoHoming=Netgame.DoLaserUpgrade=Netgame.DoQuadLasers=on;
5719   Netgame.BrightPlayers=Netgame.invul=on;
5720  }
5721
5722 fix LastPTA;
5723 int LastKillGoal;
5724
5725 // Jeez -- mac compiler can't handle all of these on the same decl line.
5726 int opt_setpower,opt_playtime,opt_killgoal,opt_socket,opt_marker_view,opt_light,opt_show_on_map;
5727 int opt_difficulty,opt_packets,opt_short_packets, opt_bright,opt_start_invul;
5728 int opt_show_names;
5729
5730 void network_more_options_poll( int nitems, newmenu_item * menus, int * key, int citem );
5731   
5732 void network_more_game_options ()
5733  {
5734   int opt=0,i;
5735   char PlayText[80],KillText[80],srinvul[50],socket_string[5],packstring[5];
5736   newmenu_item m[21];
5737
5738   sprintf (socket_string,"%d",Network_socket);
5739   sprintf (packstring,"%d",Netgame.PacketsPerSec);
5740
5741   opt_difficulty = opt;
5742   m[opt].type = NM_TYPE_SLIDER; m[opt].value=netgame_difficulty; m[opt].text=TXT_DIFFICULTY; m[opt].min_value=0; m[opt].max_value=(NDL-1); opt++;
5743
5744   opt_cinvul = opt;
5745   sprintf( srinvul, "%s: %d %s", TXT_REACTOR_LIFE, control_invul_time*5, TXT_MINUTES_ABBREV );
5746   m[opt].type = NM_TYPE_SLIDER; m[opt].value=control_invul_time; m[opt].text= srinvul; m[opt].min_value=0; m[opt].max_value=10; opt++;
5747
5748   opt_playtime=opt;
5749   sprintf( PlayText, "Max time: %d %s", Netgame.PlayTimeAllowed*5, TXT_MINUTES_ABBREV );
5750   m[opt].type = NM_TYPE_SLIDER; m[opt].value=Netgame.PlayTimeAllowed; m[opt].text= PlayText; m[opt].min_value=0; m[opt].max_value=10; opt++;
5751
5752   opt_killgoal=opt;
5753   sprintf( KillText, "Kill Goal: %d kills", Netgame.KillGoal*5);
5754   m[opt].type = NM_TYPE_SLIDER; m[opt].value=Netgame.KillGoal; m[opt].text= KillText; m[opt].min_value=0; m[opt].max_value=10; opt++;
5755
5756   opt_start_invul=opt;
5757   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Invulnerable when reappearing"; m[opt].value=Netgame.invul; opt++;
5758         
5759   opt_marker_view = opt;
5760   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Allow camera views from Markers"; m[opt].value=Netgame.Allow_marker_view; opt++;
5761   opt_light = opt;
5762   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Indestructible lights"; m[opt].value=Netgame.AlwaysLighting; opt++;
5763
5764   opt_bright = opt;
5765   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Bright player ships"; m[opt].value=Netgame.BrightPlayers; opt++;
5766   
5767   opt_show_names=opt;
5768   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Show enemy names on HUD"; m[opt].value=Netgame.ShowAllNames; opt++;
5769
5770   opt_show_on_map=opt;
5771   m[opt].type = NM_TYPE_CHECK; m[opt].text = TXT_SHOW_ON_MAP; m[opt].value=(Netgame.game_flags & NETGAME_FLAG_SHOW_MAP); opt_show_on_map=opt; opt++;
5772
5773   opt_short_packets=opt;
5774   m[opt].type = NM_TYPE_CHECK; m[opt].text = "Short packets"; m[opt].value=Netgame.ShortPackets; opt++;
5775
5776   opt_setpower = opt;
5777   m[opt].type = NM_TYPE_MENU;  m[opt].text = "Set Objects allowed..."; opt++;
5778
5779   if (Network_game_type == IPX_GAME) {
5780           m[opt].type = NM_TYPE_TEXT; m[opt].text = "Network socket"; opt++;
5781           opt_socket = opt;
5782           m[opt].type = NM_TYPE_INPUT; m[opt].text = socket_string; m[opt].text_len=4; opt++;
5783   }
5784
5785   m[opt].type = NM_TYPE_TEXT; m[opt].text = "Packets per second (2 - 20)"; opt++;
5786   opt_packets=opt;
5787   m[opt].type = NM_TYPE_INPUT; m[opt].text=packstring; m[opt].text_len=2; opt++;
5788
5789   LastKillGoal=Netgame.KillGoal;
5790   LastPTA=Netgame.PlayTimeAllowed;
5791
5792   menu:
5793
5794   ExtGameStatus=GAMESTAT_MORE_NETGAME_OPTIONS; 
5795   i = newmenu_do1( NULL, "Additional netgame options", opt, m, network_more_options_poll, 0 );
5796
5797    //control_invul_time = atoi( srinvul )*60*F1_0;
5798     control_invul_time = m[opt_cinvul].value;
5799     Netgame.control_invul_time = control_invul_time*5*F1_0*60;
5800
5801   if (i==opt_setpower)
5802    {
5803     network_set_power ();
5804     goto menu;
5805    }
5806
5807   Netgame.PacketsPerSec=atoi(packstring);
5808
5809   if (Netgame.PacketsPerSec>20)
5810         {
5811          Netgame.PacketsPerSec=20;
5812          nm_messagebox(TXT_ERROR, 1, TXT_OK, "Packet value out of range\nSetting value to 20");
5813         }
5814   if (Netgame.PacketsPerSec<2)
5815         {
5816           nm_messagebox(TXT_ERROR, 1, TXT_OK, "Packet value out of range\nSetting value to 2");
5817           Netgame.PacketsPerSec=2;      
5818         }
5819
5820   mprintf ((0,"Hey! Sending out %d packets per second\n",Netgame.PacketsPerSec));
5821
5822         if (Network_game_type == IPX_GAME) { 
5823                 if ((atoi(socket_string))!=Network_socket) {
5824                         Network_socket=atoi(socket_string);
5825                         ipx_change_default_socket( IPX_DEFAULT_SOCKET + Network_socket );
5826                 }
5827         }
5828
5829   Netgame.invul=m[opt_start_invul].value;       
5830   Netgame.BrightPlayers=m[opt_bright].value;
5831   Netgame.ShortPackets=m[opt_short_packets].value;
5832   Netgame.ShowAllNames=m[opt_show_names].value;
5833   network_AdjustMaxDataSize();
5834
5835   Netgame.Allow_marker_view=m[opt_marker_view].value;   
5836   Netgame.AlwaysLighting=m[opt_light].value;     
5837   netgame_difficulty=Difficulty_level = m[opt_difficulty].value;
5838   if (m[opt_show_on_map].value)
5839         Netgame.game_flags |= NETGAME_FLAG_SHOW_MAP;
5840         
5841         
5842  }
5843
5844 void network_more_options_poll( int nitems, newmenu_item * menus, int * key, int citem )
5845  {
5846    menus = menus;
5847    citem = citem;      // kills compile warnings
5848    nitems = nitems;
5849    key = key;
5850
5851    if ( last_cinvul != menus[opt_cinvul].value )   {
5852         sprintf( menus[opt_cinvul].text, "%s: %d %s", TXT_REACTOR_LIFE, menus[opt_cinvul].value*5, TXT_MINUTES_ABBREV );
5853         last_cinvul = menus[opt_cinvul].value;
5854         menus[opt_cinvul].redraw = 1;
5855    }
5856   
5857   if (menus[opt_playtime].value!=LastPTA)
5858    {
5859     #ifdef SHAREWARE
5860       LastPTA=0;
5861       nm_messagebox ("Sorry",1,TXT_OK,"Registered version only!");
5862       menus[opt_playtime].value=0;
5863       menus[opt_playtime].redraw=1;
5864       return;
5865     #endif  
5866
5867     if (Game_mode & GM_MULTI_COOP)
5868      {
5869       LastPTA=0;
5870       nm_messagebox ("Sorry",1,TXT_OK,"You can't change those for coop!");
5871       menus[opt_playtime].value=0;
5872       menus[opt_playtime].redraw=1;
5873       return;
5874      }
5875
5876     Netgame.PlayTimeAllowed=menus[opt_playtime].value;
5877     sprintf( menus[opt_playtime].text, "Max Time: %d %s", Netgame.PlayTimeAllowed*5, TXT_MINUTES_ABBREV );
5878     LastPTA=Netgame.PlayTimeAllowed;
5879     menus[opt_playtime].redraw=1;
5880    }
5881   if (menus[opt_killgoal].value!=LastKillGoal)
5882    {
5883     #ifdef SHAREWARE
5884       nm_messagebox ("Sorry",1,TXT_OK,"Registered version only!");
5885       menus[opt_killgoal].value=0;
5886       menus[opt_killgoal].redraw=1;
5887       LastKillGoal=0;
5888       return;
5889          #endif         
5890
5891
5892     if (Game_mode & GM_MULTI_COOP)
5893      {
5894       nm_messagebox ("Sorry",1,TXT_OK,"You can't change those for coop!");
5895       menus[opt_killgoal].value=0;
5896       menus[opt_killgoal].redraw=1;
5897       LastKillGoal=0;
5898       return;
5899      }
5900
5901     
5902     Netgame.KillGoal=menus[opt_killgoal].value;
5903     sprintf( menus[opt_killgoal].text, "Kill Goal: %d kills", Netgame.KillGoal*5);
5904     LastKillGoal=Netgame.KillGoal;
5905     menus[opt_killgoal].redraw=1;
5906    }
5907  }
5908
5909 extern void multi_send_light (int,ubyte);
5910 extern void multi_send_light_specific (int,int,ubyte);
5911
5912 void network_send_smash_lights (int pnum) 
5913  {
5914   // send the lights that have been blown out
5915
5916   int i;
5917
5918   pnum=pnum;
5919   
5920   for (i=0;i<=Highest_segment_index;i++)
5921    if (Light_subtracted[i])
5922     multi_send_light_specific(pnum,i,Light_subtracted[i]);
5923  }
5924
5925 extern int Num_triggers;
5926 extern void multi_send_trigger_specific (char pnum,char);
5927
5928 void network_send_fly_thru_triggers (int pnum) 
5929  {
5930   // send the fly thru triggers that have been disabled
5931
5932   int i;
5933
5934   for (i=0;i<Num_triggers;i++)
5935    if (Triggers[i].flags & TF_DISABLED)
5936     multi_send_trigger_specific(pnum,i);
5937  }
5938
5939 void network_send_player_flags()
5940  {
5941   int i;
5942
5943   for (i=0;i<N_players;i++)
5944         multi_send_flags(i);
5945  }
5946
5947 void network_ping (ubyte flag,int pnum)
5948 {
5949         ubyte mybuf[2];
5950
5951         mybuf[0]=flag;
5952         mybuf[1]=Player_num;
5953
5954         if (Network_game_type == IPX_GAME)
5955                 ipx_send_packet_data( (ubyte *)mybuf, 2, NetPlayers.players[pnum].network.ipx.server, NetPlayers.players[pnum].network.ipx.node,Players[pnum].net_address );
5956         #ifdef MACINTOSH
5957         else
5958                 appletalk_send_packet_data( (ubyte *)mybuf, 2, NetPlayers.players[pnum].network.appletalk.node, NetPlayers.players[pnum].network.appletalk.net, NetPlayers.players[pnum].network.appletalk.socket);
5959         #endif
5960 }
5961
5962 extern fix PingLaunchTime,PingReturnTime;
5963
5964 void network_handle_ping_return (ubyte pnum)
5965  {
5966   if (PingLaunchTime==0 || pnum>=N_players)
5967         {
5968          mprintf ((0,"Got invalid PING RETURN from %s!\n",Players[pnum].callsign));
5969     return;
5970    }
5971   
5972   PingReturnTime=timer_get_fixed_seconds();
5973
5974   HUD_init_message ("Ping time for %s is %d ms!",Players[pnum].callsign,
5975         f2i(fixmul(PingReturnTime-PingLaunchTime,i2f(1000))));
5976   PingLaunchTime=0;
5977  }
5978         
5979 void network_send_ping (ubyte pnum)
5980  {
5981   network_ping (PID_PING_SEND,pnum);
5982  }  
5983
5984 void DoRefuseStuff (sequence_packet *their)
5985  {
5986   int i,new_player_num;
5987
5988   ClipRank (&their->player.rank);
5989         
5990   for (i=0;i<MAX_PLAYERS;i++)
5991          if (!strcmp (their->player.callsign,Players[i].callsign))
5992         {
5993                 network_welcome_player(their);
5994                         return;
5995                 }
5996   
5997    if (!WaitForRefuseAnswer)    
5998     {
5999         for (i=0;i<MAX_PLAYERS;i++)
6000                  if (!strcmp (their->player.callsign,Players[i].callsign))
6001                 {
6002                         network_welcome_player(their);
6003                                 return;
6004                         }
6005    
6006       digi_play_sample (SOUND_HUD_JOIN_REQUEST,F1_0*2);           
6007   
6008       if (Game_mode & GM_TEAM)
6009                  {
6010         
6011                                         
6012       if (!FindArg("-norankings"))
6013               HUD_init_message ("%s %s wants to join",RankStrings[their->player.rank],their->player.callsign);
6014         #ifndef MACINTOSH
6015                 HUD_init_message ("%s joining. Alt-1 assigns to team %s. Alt-2 to team %s",their->player.callsign,Netgame.team_name[0],Netgame.team_name[1]);
6016                 #else
6017                 HUD_init_message ("%s joining. Opt-1 assigns to team %s. Opt-2 to team %s",their->player.callsign,Netgame.team_name[0],Netgame.team_name[1]);
6018                 #endif
6019 //                      HUD_init_message ("Alt-1 to place on team %s!",Netgame.team_name[0]);
6020 //                      HUD_init_message ("Alt-2 to place on team %s!",Netgame.team_name[1]);
6021                  }               
6022                 else    
6023                 HUD_init_message ("%s wants to join...press F6 to connect",their->player.callsign);
6024
6025            strcpy (RefusePlayerName,their->player.callsign);
6026            RefuseTimeLimit=timer_get_approx_seconds();   
6027                 RefuseThisPlayer=0;
6028                 WaitForRefuseAnswer=1;
6029          }
6030         else
6031          {      
6032         for (i=0;i<MAX_PLAYERS;i++)
6033                  if (!strcmp (their->player.callsign,Players[i].callsign))
6034                 {
6035                         network_welcome_player(their);
6036                                 return;
6037                         }
6038       
6039                 if (strcmp(their->player.callsign,RefusePlayerName))
6040                         return;
6041
6042                 if (RefuseThisPlayer)
6043                  {
6044                                 RefuseTimeLimit=0;
6045                                 RefuseThisPlayer=0;
6046                                 WaitForRefuseAnswer=0;
6047                                 if (Game_mode & GM_TEAM)
6048                                  {
6049                                         new_player_num=GetNewPlayerNumber (their);
6050                                         mprintf ((0,"Newplayernum=%d\n",new_player_num));
6051                 
6052                                         Assert (RefuseTeam==1 || RefuseTeam==2);        
6053                                 
6054                                         if (RefuseTeam==1)      
6055                                         Netgame.team_vector &=(~(1<<new_player_num));
6056                                         else
6057                                         Netgame.team_vector |=(1<<new_player_num);
6058                                         network_welcome_player(their);
6059                                         network_send_netgame_update (); 
6060                                  }
6061                                 else
6062                                         network_welcome_player(their);
6063                            return;
6064                  }
6065                                         
6066           if ((timer_get_approx_seconds()) > RefuseTimeLimit+REFUSE_INTERVAL)
6067                 {
6068                         RefuseTimeLimit=0;
6069                         RefuseThisPlayer=0;
6070                         WaitForRefuseAnswer=0;
6071                         if (!strcmp (their->player.callsign,RefusePlayerName)) {
6072                                 if (Network_game_type == IPX_GAME)
6073                                         network_dump_player(their->player.network.ipx.server,their->player.network.ipx.node, DUMP_DORK);
6074                                 #ifdef MACINTOSH
6075                                 else
6076                                         network_dump_appletalk_player(their->player.network.appletalk.node,their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_DORK);
6077                                 #endif
6078                         }
6079                         return;
6080                 }
6081                                                         
6082     }
6083  }
6084
6085 int GetNewPlayerNumber (sequence_packet *their)
6086   {
6087          int i;
6088         
6089          their=their;
6090         
6091                 if ( N_players < MaxNumNetPlayers)
6092                         return (N_players);
6093                 
6094                 else
6095                 {
6096                         // Slots are full but game is open, see if anyone is
6097                         // disconnected and replace the oldest player with this new one
6098                 
6099                         int oldest_player = -1;
6100                         fix oldest_time = timer_get_approx_seconds();
6101
6102                         Assert(N_players == MaxNumNetPlayers);
6103
6104                         for (i = 0; i < N_players; i++)
6105                         {
6106                                 if ( (!Players[i].connected) && (LastPacketTime[i] < oldest_time))
6107                                 {
6108                                         oldest_time = LastPacketTime[i];
6109                                         oldest_player = i;
6110                                 }
6111                         }
6112             return (oldest_player);
6113           }
6114   }
6115
6116 void network_send_extras ()
6117  {
6118         Assert (Player_joining_extras>-1);
6119
6120    if (!network_i_am_master())
6121          {
6122           mprintf ((0,"Hey! I'm not the master and I was gonna send info!\n"));
6123          // Int3();     
6124          // Network_sending_extras=0;
6125          // return;
6126     }
6127
6128
6129    if (Network_sending_extras==40)
6130                 network_send_fly_thru_triggers(Player_joining_extras);
6131    if (Network_sending_extras==38)
6132         network_send_door_updates(Player_joining_extras);
6133    if (Network_sending_extras==35)
6134                 network_send_markers();
6135    if (Network_sending_extras==30 && (Game_mode & GM_MULTI_ROBOTS))
6136                 multi_send_stolen_items();
6137         if (Network_sending_extras==25 && (Netgame.PlayTimeAllowed || Netgame.KillGoal))
6138                 multi_send_kill_goal_counts();
6139    if (Network_sending_extras==20)
6140                 network_send_smash_lights(Player_joining_extras);
6141    if (Network_sending_extras==15)
6142                 network_send_player_flags();    
6143    if (Network_sending_extras==10)
6144                 multi_send_powerup_update();  
6145  //  if (Network_sending_extras==5)
6146 //              network_send_door_updates(Player_joining_extras); // twice!
6147
6148         Network_sending_extras--;
6149    if (!Network_sending_extras)
6150          Player_joining_extras=-1;
6151  }
6152
6153
6154 void network_send_naked_packet(char *buf, short len, int who)
6155 {
6156         if (!(Game_mode&GM_NETWORK)) return;
6157
6158    if (NakedPacketLen==0)
6159          {
6160            NakedBuf[0]=PID_NAKED_PDATA;
6161            NakedBuf[1]=Player_num;
6162                 NakedPacketLen=2;
6163          }
6164
6165    if (len+NakedPacketLen>MaxXDataSize)
6166     {
6167 //         mprintf ((0,"Sending a naked packet of %d bytes.\n",NakedPacketLen));
6168                 if (Network_game_type == IPX_GAME)
6169                         ipx_send_packet_data( (ubyte *)NakedBuf, NakedPacketLen, NetPlayers.players[who].network.ipx.server, NetPlayers.players[who].network.ipx.node,Players[who].net_address );
6170                 #ifdef MACINTOSH
6171                 else
6172                         appletalk_send_packet_data( (ubyte *)NakedBuf, NakedPacketLen, NetPlayers.players[who].network.appletalk.node, NetPlayers.players[who].network.appletalk.net, NetPlayers.players[who].network.appletalk.socket );
6173                 #endif
6174                 NakedPacketLen=2;
6175                 memcpy (&NakedBuf[NakedPacketLen],buf,len);     
6176                 NakedPacketLen+=len;
6177                 NakedPacketDestPlayer=who;
6178          }
6179    else
6180          {
6181                 memcpy (&NakedBuf[NakedPacketLen],buf,len);     
6182                 NakedPacketLen+=len;
6183                 NakedPacketDestPlayer=who;
6184          }
6185  }
6186
6187
6188 void network_process_naked_pdata (char *data,int len)
6189  {
6190    int pnum=data[1]; 
6191    Assert (data[0]=PID_NAKED_PDATA);
6192
6193 //   mprintf ((0,"Processing a naked packet of %d length.\n",len));
6194
6195         if (pnum < 0) {
6196            mprintf ((0,"Naked packet is bad!\n"));
6197                 Int3(); // This packet is bogus!!
6198                 return;
6199         }
6200
6201         if (!multi_quit_game && (pnum >= N_players)) {
6202                 if (Network_status!=NETSTAT_WAITING)
6203                  {
6204                         Int3(); // We missed an important packet!
6205                         network_consistency_error();
6206                         return;
6207                  }
6208                 else
6209                  return;
6210         }
6211
6212         if (Endlevel_sequence || (Network_status == NETSTAT_ENDLEVEL) ) {
6213                 int old_Endlevel_sequence = Endlevel_sequence;
6214                 Endlevel_sequence = 1;
6215                 multi_process_bigdata( data+2, len-2);
6216                 Endlevel_sequence = old_Endlevel_sequence;
6217                 return;
6218         }
6219
6220         multi_process_bigdata( data+2, len-2 );
6221  }
6222
6223 int ConnectionPacketLevel[]={0,1,1,1};
6224 int ConnectionSecLevel[]={12,3,5,7};
6225
6226 int AppletalkConnectionPacketLevel[]={0,1,0};
6227 int AppletalkConnectionSecLevel[]={10,3,8};
6228
6229 #if defined(RELEASE) && !defined(MACINTOSH)             // use the code below for mac appletalk games
6230 int network_choose_connect ()
6231  {
6232   return (1);
6233  }
6234 #else
6235 int network_choose_connect ()
6236  {
6237 #if 0
6238         newmenu_item m[16];
6239         int choice,opt=0;
6240 #endif
6241
6242         if (Network_game_type == IPX_GAME) {  
6243                 #if 0
6244            m[opt].type = NM_TYPE_MENU;  m[opt].text = "Local Subnet"; opt++;
6245            m[opt].type = NM_TYPE_MENU;  m[opt].text = "14.4 modem over Internet"; opt++;
6246            m[opt].type = NM_TYPE_MENU;  m[opt].text = "28.8 modem over Internet"; opt++;
6247            m[opt].type = NM_TYPE_MENU;  m[opt].text = "ISDN or T1 over Internet"; opt++;
6248
6249            choice = newmenu_do1( NULL, "Choose connection type", opt, m, NULL, 0 );
6250
6251                 if (choice<0)
6252                  return (NULL);
6253
6254            Assert (choice>=0 && choice<=3);
6255    
6256                 Netgame.ShortPackets=ConnectionPacketLevel[choice];
6257                 Netgame.PacketsPerSec=ConnectionSecLevel[choice];
6258                 #endif
6259            return (1);
6260         #ifdef MACINTOSH
6261         } else {
6262            m[opt].type = NM_TYPE_MENU;  m[opt].text = "EtherTalk"; opt++;
6263            m[opt].type = NM_TYPE_MENU;  m[opt].text = "LocalTalk"; opt++;
6264            m[opt].type = NM_TYPE_MENU;  m[opt].text = "Other"; opt++;
6265         
6266            choice = newmenu_do1( NULL, "Choose connection type", opt, m, NULL, 0 );
6267
6268                 if (choice<0)
6269                  return (NULL);
6270
6271                 Network_appletalk_type = choice;
6272                 
6273                 Assert (choice>=0 && choice<=2);
6274    
6275                 Netgame.ShortPackets=AppletalkConnectionPacketLevel[choice];
6276                 Netgame.PacketsPerSec=AppletalkConnectionSecLevel[choice];
6277            return (1);
6278         #endif
6279         }
6280   return (0);   
6281  }
6282 #endif
6283
6284
6285
6286 #ifdef NETPROFILING
6287 void OpenSendLog ()
6288  {
6289   int i;
6290   SendLogFile=(FILE *)fopen ("sendlog.net","w");
6291   for (i=0;i<100;i++)
6292         TTSent[i]=0;
6293  }
6294 void OpenRecieveLog ()
6295  {
6296   int i;
6297   RecieveLogFile=(FILE *)fopen ("recvlog.net","w");
6298   for (i=0;i<100;i++)
6299         TTRecv[i]=0;
6300  }
6301 #endif
6302
6303 int GetMyNetRanking ()
6304  {
6305   int rank;
6306   int eff;
6307
6308   if (Netlife_kills+Netlife_killed==0)
6309          return (1);
6310  
6311   rank=(int) (((float)Netlife_kills/3000.0)*8.0);
6312  
6313   eff=(int)((float)((float)Netlife_kills/((float)Netlife_killed+(float)Netlife_kills))*100.0);
6314
6315   if (rank>8)
6316    rank=8;
6317   
6318   if (eff<0)
6319          eff=0;
6320  
6321   if (eff<60)
6322         rank-=((59-eff)/10);
6323         
6324   if (rank<0)
6325         rank=0;
6326   if (rank>8)
6327         rank=8;
6328  
6329   mprintf ((0,"Rank is %d (%s)\n",rank+1,RankStrings[rank+1]));
6330   return (rank+1);
6331  }
6332
6333 void ClipRank (signed char *rank)
6334  {
6335   // This function insures no crashes when dealing with D2 1.0
6336
6337  
6338   if (*rank<0 || *rank>9)
6339         *rank=0;
6340  }
6341 void network_check_for_old_version (char pnum)
6342  {  
6343   if (NetPlayers.players[(int)pnum].version_major==1 && (NetPlayers.players[(int)pnum].version_minor & 0x0F)==0)
6344         NetPlayers.players[(int)pnum].rank=0;
6345  }
6346
6347 void network_request_player_names (int n)
6348  {
6349   network_send_all_info_request (PID_GAME_PLAYERS,Active_games[n].Security);
6350   NamesInfoSecurity=Active_games[n].Security;
6351  }
6352
6353 extern char already_showing_info;
6354 extern int newmenu_dotiny2( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem));
6355
6356 void network_process_names_return (char *data)
6357  {
6358         newmenu_item m[15];
6359    char mtext[15][50],temp[50];
6360         int i,t,l,gnum,num=0,count=5,numplayers;
6361    
6362    if (NamesInfoSecurity!=(*(int *)(data+1)))
6363          {
6364           mprintf ((0,"Bad security on names return!\n"));
6365           mprintf ((0,"NIS=%d data=%d\n",NamesInfoSecurity,(*(int *)(data+1))));
6366           return;
6367          }
6368
6369    numplayers=data[count]; count++;
6370
6371    if (numplayers==255)
6372          {
6373                  SurfingNet=0;  
6374                  NamesInfoSecurity=-1;
6375                  nm_messagebox(NULL, 1, "OK", "That game is refusing\nname requests.\n");
6376                  SurfingNet=1;
6377                  return;
6378          }
6379
6380    Assert (numplayers>0 && numplayers<MAX_NUM_NET_PLAYERS);
6381         
6382    for (i=0;i<12;i++)
6383         {
6384          m[i].text=(char *)&mtext[i];
6385     m[i].type=NM_TYPE_TEXT;             
6386         }
6387
6388    for (gnum=-1,i=0;i<num_active_games;i++)
6389          {
6390           if (NamesInfoSecurity==Active_games[i].Security)
6391                 {
6392                  gnum=i;
6393                  break;
6394                 }
6395          }
6396         
6397         if (gnum==-1)
6398     {
6399        SurfingNet=0;
6400                  NamesInfoSecurity=-1;
6401                  nm_messagebox(NULL, 1, "OK", "The game you have requested\nnames from is gone.\n");
6402                  SurfingNet=1;
6403                  return;
6404          }
6405  
6406    sprintf (mtext[num],"Players of game '%s':",Active_games[gnum].game_name); num++;
6407    for (i=0;i<numplayers;i++)
6408          {
6409           l=data[count++];
6410
6411      mprintf ((0,"%s\n",data+count));
6412
6413           for (t=0;t<CALLSIGN_LEN+1;t++)
6414                  temp[t]=data[count++];   
6415      if (FindArg("-norankings"))        
6416              sprintf (mtext[num],"%s",temp);
6417           else
6418              sprintf (mtext[num],"%s%s",RankStrings[l],temp);
6419         
6420           num++;        
6421          }
6422
6423         if (data[count]==99)
6424         {
6425          sprintf (mtext[num++]," ");
6426          sprintf (mtext[num++],"Short packets: %s",data[count+1]?"On":"Off");
6427          sprintf (mtext[num++],"Packets Per Second: %d",data[count+2]);
6428    }
6429
6430         already_showing_info=1; 
6431         newmenu_dotiny2( NULL, NULL, num, m, NULL);
6432         already_showing_info=0; 
6433  }
6434
6435 char NameReturning=1;
6436
6437 void network_send_player_names (sequence_packet *their)
6438  {
6439   int numconnected=0,count=0,i,t;
6440   char buf[80];
6441
6442   if (!their)
6443    {
6444     mprintf ((0,"Got a player name without a return address! Get Jason\n"));
6445          return;
6446         }
6447
6448    buf[0]=PID_NAMES_RETURN; count++;
6449    (*(int *)(buf+1))=Netgame.Security; count+=4;
6450
6451    if (!NameReturning)
6452          {
6453      buf[count]=255; count++; 
6454           goto sendit;
6455          }
6456  
6457    mprintf ((0,"RealSec=%d DS=%d\n",Netgame.Security,*(int *)(buf+1)));
6458   
6459    for (i=0;i<N_players;i++)
6460          if (Players[i].connected)
6461                 numconnected++;
6462
6463    buf[count]=numconnected; count++; 
6464    for (i=0;i<N_players;i++)
6465          if (Players[i].connected)
6466           {
6467                 buf[count++]=NetPlayers.players[i].rank; 
6468                 
6469                 for (t=0;t<CALLSIGN_LEN+1;t++)
6470                  {
6471                   buf[count]=NetPlayers.players[i].callsign[t];
6472                   count++;
6473                  }
6474           }
6475
6476    buf[count++]=99;
6477         buf[count++]=Netgame.ShortPackets;              
6478         buf[count++]=Netgame.PacketsPerSec;
6479   
6480    sendit:
6481                                 ;               // empty statement allows compilation on mac which does not have the
6482                                                 // statement below and kills the compiler when there is no
6483                                                 // statement following the label "sendit"
6484            
6485    #ifndef MACINTOSH
6486            ipx_send_internetwork_packet_data((ubyte *)buf, count, their->player.network.ipx.server, their->player.network.ipx.node);
6487         #endif
6488  }
6489  
6490
6491 extern int file_exists (char *);
6492
6493 int HoardEquipped ()
6494 {
6495         static int checked=-1;
6496         
6497         #ifdef WINDOWS
6498                 return 0;
6499         #endif
6500
6501         if (checked==-1)
6502         {
6503                 if (file_exists ("hoard.ham"))
6504                         checked=1;
6505                 else
6506                         checked=0;
6507         }
6508         return (checked);
6509 }
6510         
6511 #endif
6512
6513
6514
6515
6516
6517
6518
6519