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