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