Trying to get network working on win32
[btb/d2x.git] / main / network.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #ifdef RCS
19 static char rcsid[] = "$Id: network.c,v 1.5 2001-10-19 00:31:51 bradleyb Exp $";
20 #endif
21
22 #ifdef NETWORK
23
24 #define PATCH12
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "pstypes.h"
31 #include "strutil.h"
32 #include "args.h"
33 #include "timer.h"
34 #include "mono.h"
35 #include "ipx.h"
36 #include "newmenu.h"
37 #include "key.h"
38 #include "gauges.h"
39 #include "object.h"
40 #include "error.h"
41 #include "laser.h"
42 #include "gamesave.h"
43 #include "gamemine.h"
44 #include "player.h"
45 #include "gameseq.h"
46 #include "fireball.h"
47 #include "network.h"
48 #include "game.h"
49 #include "multi.h"
50 #include "endlevel.h"
51 #include "palette.h"
52 #include "cntrlcen.h"
53 #include "powerup.h"
54 #include "menu.h"
55 #include "sounds.h"
56 #include "text.h"
57 #include "kmatrix.h"
58 #include "newdemo.h"
59 #include "multibot.h"
60 #include "wall.h"
61 #include "bm.h"
62 #include "effects.h"
63 #include "physics.h"
64 #include "switch.h"
65 #include "automap.h"
66 #include "byteswap.h"
67 #include "netmisc.h"
68 #include "kconfig.h"
69 #include "playsave.h"
70
71 #ifdef MACINTOSH
72 #include <Events.h>
73 #include <Errors.h>             // for appletalk networking errors
74 #include "appltalk.h"
75 #endif
76
77 void network_send_rejoin_sync(int player_num);
78 void network_update_netgame(void);
79 void network_read_endlevel_packet( ubyte *data );
80 void network_read_object_packet( ubyte *data );
81 void network_read_sync_packet( netgame_info * sp, int d1x );
82 void network_flush();
83 void network_listen();
84 void network_read_pdata_packet(frame_info *pd );
85
86 void network_check_for_old_version (char pnum);
87 void network_send_endlevel_short_sub(int from_player_num,int to_player);
88 int network_wait_for_playerinfo();
89 void network_process_pdata (char *data);
90 void network_read_endlevel_short_packet( ubyte *data );
91 void network_ping (ubyte flag,int pnum);
92 void network_handle_ping_return (ubyte pnum);
93 void network_process_names_return (char *data);
94 void network_send_player_names (sequence_packet *their);
95 int network_choose_connect ();
96 void network_more_game_options ();
97 void network_count_powerups_in_mine(void);
98 int network_wait_for_all_info (int choice);
99 void network_do_big_wait(int choice);
100 void network_send_extras ();
101 void network_read_pdata_short_packet(short_frame_info *pd );
102
103 void network_AdjustMaxDataSize ();
104
105 void ClipRank (signed char *rank);
106 void DoRefuseStuff (sequence_packet *their);
107 void SetAllAllowablesTo (int on);
108 int GetNewPlayerNumber (sequence_packet *their);
109
110 #define LHX(x)          ((x)*(MenuHires?2:1))
111 #define LHY(y)          ((y)*(MenuHires?2.4:1))
112
113 #define PID_LITE_INFO                           43
114 #define PID_SEND_ALL_GAMEINFO      44
115 #define PID_PLAYERSINFO                         45
116 #define PID_REQUEST                                     46
117 #define PID_SYNC                                                47
118 #define PID_PDATA                                               48
119 #define PID_ADDPLAYER                           49
120 #define PID_DUMP                                                51
121 #define PID_ENDLEVEL                                    52
122 #define PID_QUIT_JOINING                        54
123 #define PID_OBJECT_DATA                         55
124 #define PID_GAME_LIST                           56
125 #define PID_GAME_INFO                           57
126 #define PID_PING_SEND                           58
127 #define PID_PING_RETURN                         59
128 #define PID_GAME_UPDATE                         60
129 #define PID_ENDLEVEL_SHORT                      61
130 #define PID_NAKED_PDATA                         62
131 #define PID_GAME_PLAYERS                                                                63
132 #define PID_NAMES_RETURN                                                                64
133
134 #define NETGAME_ANARCHY                         0
135 #define NETGAME_TEAM_ANARCHY            1
136 #define NETGAME_ROBOT_ANARCHY           2
137 #define NETGAME_COOPERATIVE             3
138 #define NETGAME_CAPTURE_FLAG            4
139 #define NETGAME_HOARD                            5
140 #define NETGAME_TEAM_HOARD                                       6
141
142 #define NETSECURITY_OFF 0
143 #define NETSECURITY_WAIT_FOR_PLAYERS 1
144 #define NETSECURITY_WAIT_FOR_GAMEINFO 2
145 #define NETSECURITY_WAIT_FOR_SYNC 3
146
147 // MWA -- these structures are aligned -- please save me sanity and
148 // headaches by keeping alignment if these are changed!!!!  Contact
149 // me for info.
150
151 typedef struct endlevel_info {
152         ubyte                                   type;
153         ubyte                                   player_num;
154         byte                                    connected;
155         ubyte                                   seconds_left;
156         short                                   kill_matrix[MAX_PLAYERS][MAX_PLAYERS];
157         short                                   kills;
158         short                                   killed;
159 } endlevel_info;
160
161 typedef struct endlevel_info_short {
162         ubyte                                   type;
163         ubyte                                   player_num;
164         byte                                    connected;
165         ubyte                                   seconds_left;
166 } endlevel_info_short;
167
168 // WARNING!!! This is the top part of netgame_info...if that struct changes,
169 //      this struct much change as well.  ie...they are aligned and the join system will
170 // not work without it.
171
172 // MWA  if this structure changes -- please make appropriate changes to receive_netgame_info
173 // code for macintosh in netmisc.c
174
175 typedef struct lite_info {
176         ubyte                           type;
177         int                             Security;
178         char                            game_name[NETGAME_NAME_LEN+1];
179         char                            mission_title[MISSION_NAME_LEN+1];
180         char                            mission_name[9];
181         int                             levelnum;
182         ubyte                           gamemode;
183         ubyte                           RefusePlayers;
184         ubyte                           difficulty;
185         ubyte                           game_status;
186         ubyte                           numplayers;
187         ubyte                           max_numplayers;
188         ubyte                           numconnected;
189         ubyte                           game_flags;
190         ubyte                           protocol_version;
191         ubyte                           version_major;
192         ubyte                           version_minor;
193         ubyte                           team_vector;
194  } lite_info;
195
196 // IF YOU CHANGE THE SIZE OF ANY OF THE FOLLOWING STRUCTURES
197 // MAKE THE MACINTOSH DEFINES BE THE SAME SIZE AS THE PC OR
198 // I WILL HAVE TO HURT YOU VERY BADLY!!!  -- MWA
199
200 #ifdef MACINTOSH
201 #define NETGAME_INFO_SIZE               ( Network_game_type == IPX_GAME?355:sizeof(netgame_info) )
202 #define ALLNETPLAYERSINFO_SIZE  ( Network_game_type == IPX_GAME?317:sizeof(AllNetPlayers_info) )
203 #define LITE_INFO_SIZE                  ( Network_game_type == IPX_GAME?72:sizeof(lite_info) )
204 #define SEQUENCE_PACKET_SIZE    ( Network_game_type == IPX_GAME?34:sizeof(sequence_packet) )
205 #define FRAME_INFO_SIZE                 ( Network_game_type == IPX_GAME?541:sizeof(frame_info) )
206 #define IPX_SHORT_INFO_SIZE             ( 490 )
207 #else
208 #define NETGAME_INFO_SIZE               sizeof(netgame_info)
209 #define ALLNETPLAYERSINFO_SIZE  sizeof(AllNetPlayers_info)
210 #define LITE_INFO_SIZE                  sizeof(lite_info)
211 #define SEQUENCE_PACKET_SIZE    sizeof(sequence_packet)
212 #define FRAME_INFO_SIZE                 sizeof(frame_info)
213 #endif
214
215 #define MAX_ACTIVE_NETGAMES     12
216
217 netgame_info Active_games[MAX_ACTIVE_NETGAMES];
218 AllNetPlayers_info ActiveNetPlayers[MAX_ACTIVE_NETGAMES];
219 AllNetPlayers_info *TempPlayersInfo,TempPlayersBase;
220 int NamesInfoSecurity=-1;
221
222 // MWAnetgame_info *TempNetInfo; 
223 netgame_info TempNetInfo;
224
225 extern void multi_send_drop_marker (int player,vms_vector position,char messagenum,char text[]);
226 extern void multi_send_kill_goal_counts();
227 extern int newmenu_dotiny( char * title, char * subtitle, int nitems, newmenu_item * item, void (*subfunction)(int nitems,newmenu_item * items, int * last_key, int citem) );
228
229 void network_process_naked_pdata (char *,int);
230 extern void multi_send_robot_controls(char);
231
232 int num_active_games = 0;
233 int PacketsPerSec=10;
234 int MaxXDataSize=NET_XDATA_SIZE;
235
236 int       Netlife_kills=0, Netlife_killed=0;
237
238 int     Network_debug=0;
239 int     Network_active=0;
240
241 int     Network_status = 0;
242 int     Network_games_changed = 0;
243
244 int     Network_socket = 0;
245 int     Network_allow_socket_changes = 1;
246
247 int     Network_initial_pps = 10;
248 int     Network_initial_shortpackets = 0;
249
250 int     NetSecurityFlag=NETSECURITY_OFF;
251 int     NetSecurityNum=0;
252 int       Network_sending_extras=0;
253 int      VerifyPlayerJoined=-1;
254 int       Player_joining_extras=-1;  // This is so we know who to send 'latecomer' packets to.
255                                                                                           // We could just send updates to everyone but that kills the sender!   
256
257 // For rejoin object syncing
258
259 int     Network_rejoined = 0;       // Did WE rejoin this game?
260 int     Network_new_game = 0;            // Is this the first level of a new game?
261 int     Network_send_objects = 0;  // Are we in the process of sending objects to a player?
262 int     Network_send_objnum = -1;   // What object are we sending next?
263 int     Network_player_added = 0;   // Is this a new player or a returning player?
264 int     Network_send_object_mode = 0; // What type of objects are we sending, static or dynamic?
265 sequence_packet Network_player_rejoining; // Who is rejoining now?
266
267 fix     LastPacketTime[MAX_PLAYERS]; // For timeouts of idle/crashed players
268
269 int     PacketUrgent = 0;
270 int   NetGameType=0;
271 int     TotalMissedPackets=0,TotalPacketsGot=0;
272
273 frame_info      MySyncPack,UrgentSyncPack;
274 ubyte           MySyncPackInitialized = 0;              // Set to 1 if the MySyncPack is zeroed.
275 ushort          my_segments_checksum = 0;
276
277 sequence_packet My_Seq;
278 char WantPlayersInfo=0;
279 char WaitingForPlayerInfo=0;
280
281 char *RankStrings[]={"(unpatched) ","Cadet ","Ensign ","Lieutenant ","Lt.Commander ",
282                                                                 "Commander ","Captain ","Vice Admiral ","Admiral ","Demigod "};
283
284 extern obj_position Player_init[MAX_PLAYERS];
285
286 extern int force_cockpit_redraw;
287
288 #define DUMP_CLOSED 0
289 #define DUMP_FULL 1
290 #define DUMP_ENDLEVEL 2
291 #define DUMP_DORK 3
292 #define DUMP_ABORTED 4
293 #define DUMP_CONNECTED 5
294 #define DUMP_LEVEL 6
295 #define DUMP_KICKED 7
296
297 extern ubyte Version_major,Version_minor;
298 extern ubyte SurfingNet;
299 extern char MaxPowerupsAllowed[MAX_POWERUP_TYPES];
300 extern char PowerupsInMine[MAX_POWERUP_TYPES];
301
302 extern void multi_send_stolen_items();
303
304 int network_wait_for_snyc();
305 extern void multi_send_wall_status (int,ubyte,ubyte,ubyte);
306 extern void multi_send_wall_status_specific (int,int,ubyte,ubyte,ubyte);
307
308 extern void game_disable_cheats();
309
310 char IWasKicked=0;
311 FILE *SendLogFile,*RecieveLogFile;
312 int TTSent[100],TTRecv[100];
313
314 extern int Final_boss_is_dead;
315
316 // following is network stuff for appletalk
317
318 void network_dump_appletalk_player(ubyte node, ushort net, ubyte socket, int why);
319
320 #define NETWORK_OEM 0x10
321
322 #ifdef MACINTOSH
323
324 int Network_game_type;                                  // used to tell IPX vs. appletalk games
325
326 #define MAX_ZONES                       255
327 #define MAX_ZONE_LENGTH         33
328 #define DEFAULT_ZONE_NAME       "\p*"
329 #define MAX_REGISTER_TRIES      5                                       // maximum time we will try and register a netgame
330 char Network_zone_name[MAX_ZONE_LENGTH];                // zone name game being played in for appletalk
331 char Zone_names[MAX_ZONES][MAX_ZONE_LENGTH];    // total list of zones that we can see
332 ubyte appletalk_use_broadcast = 0;
333 ubyte Network_game_validated = 0;               // validates a game before being able to join
334 ubyte Network_game_validate_choice = 0;
335 ubyte Network_appletalk_type = 0;               // type of appletalk network game is being played on
336
337 #define ETHERTALK_TYPE  0
338 #define LOCALTALK_TYPE  1
339 #define OTHERTALK_TYPE  2
340
341
342 void network_release_registered_game(void);
343
344 #endif
345
346 void
347 network_init(void)
348 {
349   
350         // So you want to play a netgame, eh?  Let's a get a few things
351         // straight
352
353    int t;
354         int save_pnum = Player_num;
355
356         game_disable_cheats();
357    IWasKicked=0;
358    Final_boss_is_dead=0;
359    NamesInfoSecurity=-1;
360
361
362    #ifdef NETPROFILING
363            OpenSendLog();
364                 OpenRecieveLog(); 
365         #endif
366         
367         for (t=0;t<MAX_POWERUP_TYPES;t++)
368                 {
369                         MaxPowerupsAllowed[t]=0;
370                         PowerupsInMine[t]=0;
371                 }
372
373    TotalMissedPackets=0; TotalPacketsGot=0;
374
375         memset(&Netgame, 0, sizeof(netgame_info));
376         memset(&NetPlayers,0,sizeof(AllNetPlayers_info));
377         memset(&My_Seq, 0, sizeof(sequence_packet));
378         My_Seq.type = PID_REQUEST;
379         memcpy(My_Seq.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1);
380
381    #if defined (D2_OEM)
382            Version_minor|=NETWORK_OEM;
383         #endif
384  
385         My_Seq.player.version_major=Version_major;
386         My_Seq.player.version_minor=Version_minor;
387    My_Seq.player.rank=GetMyNetRanking();        
388  
389         if (Network_game_type == IPX_GAME) {
390                 memcpy(My_Seq.player.network.ipx.node, ipx_get_my_local_address(), 6);
391                 memcpy(My_Seq.player.network.ipx.server, ipx_get_my_server_address(), 4 );
392         #ifdef MACINTOSH
393         } else {
394                 My_Seq.player.network.appletalk.node = appletalk_get_my_node();
395                 My_Seq.player.network.appletalk.net = appletalk_get_my_net();
396                 My_Seq.player.network.appletalk.socket = appletalk_get_my_socket();
397         #endif
398         }
399 #ifdef MACINTOSH
400         My_Seq.player.computer_type = MAC;
401 #else
402         My_Seq.player.computer_type = DOS;
403 #endif
404
405         for (Player_num = 0; Player_num < MAX_NUM_NET_PLAYERS; Player_num++)
406                 init_player_stats_game();
407
408         Player_num = save_pnum;         
409         multi_new_game();
410         Network_new_game = 1;
411         Control_center_destroyed = 0;
412         network_flush();
413
414         Netgame.PacketsPerSec=10;
415
416    if ((t=FindArg("-packets")))
417     {
418      Netgame.PacketsPerSec=atoi(Args[t+1]);
419      if (Netgame.PacketsPerSec<1)
420       Netgame.PacketsPerSec=1;
421      else if (Netgame.PacketsPerSec>20)
422       Netgame.PacketsPerSec=20;
423      mprintf ((0,"Will send %d packets per second",Netgame.PacketsPerSec));
424     }
425    if (FindArg("-shortpackets"))
426     {
427      Netgame.ShortPackets=1;
428      mprintf ((0,"Will send short packets.\n"));
429     }
430 }
431
432 int
433 network_i_am_master(void)
434 {
435         // I am the lowest numbered player in this game?
436
437         int i;
438
439         if (!(Game_mode & GM_NETWORK))
440                 return (Player_num == 0);
441
442         for (i = 0; i < Player_num; i++)
443                 if (Players[i].connected)
444                         return 0;
445         return 1;
446 }
447 int
448 network_who_is_master(void)
449 {
450         // Who is the master of this game?
451
452         int i;
453
454         if (!(Game_mode & GM_NETWORK))
455                 return (Player_num == 0);
456
457         for (i = 0; i < N_players; i++)
458                 if (Players[i].connected)
459                         return i;
460         return Player_num;
461 }
462 int network_how_many_connected()
463  {
464   int num=0,i;
465  
466         for (i = 0; i < N_players; i++)
467                 if (Players[i].connected)
468                         num++;
469    return (num);
470  }
471
472 #define ENDLEVEL_SEND_INTERVAL (F1_0*2)
473 #define ENDLEVEL_IDLE_TIME      (F1_0*20)
474 /*      
475 void 
476 network_endlevel_poll( int nitems, newmenu_item * menus, int * key, int citem )
477 {
478         // Polling loop for End-of-level menu
479
480         static fix t1 = 0;
481         int i = 0;
482         int num_ready = 0;
483         int num_escaped = 0;
484         int goto_secret = 0;
485
486         int previous_state[MAX_NUM_NET_PLAYERS];
487         int previous_seconds_left;
488
489         menus = menus;
490         citem = citem;
491         nitems = nitems;
492         key = key;
493
494         // Send our endlevel packet at regular intervals
495
496         if (timer_get_approx_seconds() > (t1+ENDLEVEL_SEND_INTERVAL))
497         {
498                 network_send_endlevel_packet();
499                 t1 = timer_get_approx_seconds();
500         }
501
502         for (i = 0; i < N_players; i++)
503                 previous_state[i] = Players[i].connected;
504
505         previous_seconds_left = Countdown_seconds_left;
506
507         network_listen();
508
509         for (i = 0; i < N_players; i++)
510         {
511                 if (Players[i].connected == 1)
512                 {
513                         // Check timeout for idle players
514                         if (timer_get_approx_seconds() > LastPacketTime[i]+ENDLEVEL_IDLE_TIME)
515                         {
516                                 mprintf((0, "idle timeout for player %d.\n", i));
517                                 Players[i].connected = 0;
518                                 network_send_endlevel_sub(i);
519                         }                               
520                 }
521
522                 if ((Players[i].connected != 1) && (Players[i].connected != 5) && (Players[i].connected != 6))
523                         num_ready++;
524                 if (Players[i].connected != 1)
525                         num_escaped++;
526                 if (Players[i].connected == 4)
527                         goto_secret = 1;
528         }
529
530         if (num_escaped == N_players) // All players are out of the mine
531         {
532                 Countdown_seconds_left = -1;
533         }
534
535
536         if (num_ready == N_players) // All players have checked in or are disconnected
537         {
538                 if (goto_secret)
539                         *key = -3;
540                 else
541                         *key = -2;
542         }
543 } */
544   
545 void 
546 network_endlevel_poll2( int nitems, newmenu_item * menus, int * key, int citem )
547 {
548         // Polling loop for End-of-level menu
549
550         static fix t1 = 0;
551         int i = 0;
552         int num_ready = 0;
553         int goto_secret = 0;
554
555         menus = menus;
556         citem = citem;
557         nitems = nitems;
558         key = key;
559
560         // Send our endlevel packet at regular intervals
561
562         if (timer_get_approx_seconds() > (t1+ENDLEVEL_SEND_INTERVAL))
563         {
564                 network_send_endlevel_packet();
565                 t1 = timer_get_approx_seconds();
566         }
567
568 //   mprintf ((0,"Trying to listen!\n"));
569         network_listen();
570
571         for (i = 0; i < N_players; i++)
572         {
573                 if ((Players[i].connected != 1) && (Players[i].connected != 5) && (Players[i].connected != 6))
574                         num_ready++;
575                 if (Players[i].connected == 4)
576                         goto_secret = 1;                                        
577         }
578
579         if (num_ready == N_players) // All players have checked in or are disconnected
580         {
581                 if (goto_secret)
582                         *key = -3;
583                 else
584                         *key = -2;
585         }
586 }
587
588
589 extern fix StartAbortMenuTime;
590
591 void 
592 network_endlevel_poll3( int nitems, newmenu_item * menus, int * key, int citem )
593 {
594         // Polling loop for End-of-level menu
595
596    int num_ready=0,i;
597  
598         menus = menus;
599         citem = citem;
600         nitems = nitems;
601         key = key;
602
603         if (timer_get_approx_seconds() > (StartAbortMenuTime+(F1_0 * 8)))
604     *key=-2;
605
606
607         network_listen();
608
609
610         for (i = 0; i < N_players; i++)
611                 if ((Players[i].connected != 1) && (Players[i].connected != 5) && (Players[i].connected != 6))
612                         num_ready++;
613
614         if (num_ready == N_players) // All players have checked in or are disconnected
615                         *key = -2;
616
617 }
618
619
620 int
621 network_endlevel(int *secret)
622 {
623         // Do whatever needs to be done between levels
624
625    int i;
626
627    *secret=0;
628
629         //network_flush();
630
631         Network_status = NETSTAT_ENDLEVEL; // We are between levels
632
633         network_listen();
634
635         network_send_endlevel_packet();
636
637         for (i=0; i<N_players; i++) 
638         {
639                 LastPacketTime[i] = timer_get_approx_seconds();
640         }
641    
642         network_send_endlevel_packet();
643         network_send_endlevel_packet();
644         MySyncPackInitialized = 0;
645
646         network_update_netgame();
647
648         return(0);
649 }
650
651 int 
652 can_join_netgame(netgame_info *game,AllNetPlayers_info *people)
653 {
654         // Can this player rejoin a netgame in progress?
655
656         int i, num_players;
657
658         if (game->game_status == NETSTAT_STARTING)
659      return 1;
660
661         if (game->game_status != NETSTAT_PLAYING)
662     {
663       mprintf ((0,"Error: Can't join because game_status !=NETSTAT_PLAYING\n"));
664                 return 0;
665     }
666
667    if (game->version_major==0 && Version_major>0)
668     {
669            mprintf ((0,"Error:Can't join because version majors don't match!\n"));
670                 return (0);
671     }
672
673         if (game->version_major>0 && Version_major==0)
674     {
675            mprintf ((0,"Error:Can't join because version majors2 don't match!\n"));
676                 return (0);
677     }
678
679         // Game is in progress, figure out if this guy can re-join it
680
681         num_players = game->numplayers;
682
683         if (!(game->game_flags & NETGAME_FLAG_CLOSED)) {
684                 // Look for player that is not connected
685                 
686                 if (game->numconnected==game->max_numplayers)
687                  return (2);
688
689 //      mprintf ((0,"Refuse = %d\n",game->RefusePlayers));
690                 
691                 if (game->RefusePlayers)
692                  return (3);
693                 
694                 if (game->numplayers < game->max_numplayers)
695                         return 1;
696
697                 if (game->numconnected<num_players)
698                         return 1;
699                      
700         }
701
702         if (people==NULL)
703     {
704       mprintf ((0,"Error! Can't join because people==NULL!\n"));
705                 return 0;
706     }
707         
708         // Search to see if we were already in this closed netgame in progress
709
710         for (i = 0; i < num_players; i++) {
711                 if (Network_game_type == IPX_GAME) {
712                         if ( (!stricmp(Players[Player_num].callsign, people->players[i].callsign)) &&
713                                   (!memcmp(My_Seq.player.network.ipx.node, people->players[i].network.ipx.node, 6)) &&
714                                   (!memcmp(My_Seq.player.network.ipx.server, people->players[i].network.ipx.server, 4)) )
715                                 break;
716                 } else {
717                         if ( (!stricmp(Players[Player_num].callsign, people->players[i].callsign)) &&
718                                   (My_Seq.player.network.appletalk.node == people->players[i].network.appletalk.node) &&
719                                   (My_Seq.player.network.appletalk.net == people->players[i].network.appletalk.net) )
720                                 break;
721                 }
722         }
723
724         if (i != num_players)
725                 return 1;
726  
727    mprintf ((0,"Error: Can't join because at end of list!\n"));
728         return 0;
729 }
730
731 void
732 network_disconnect_player(int playernum)
733 {
734         // A player has disconnected from the net game, take whatever steps are
735         // necessary 
736
737         if (playernum == Player_num) 
738         {
739                 Int3(); // Weird, see Rob
740                 return;
741         }
742
743         Players[playernum].connected = 0;
744    NetPlayers.players[playernum].connected = 0;
745    if (VerifyPlayerJoined==playernum)
746           VerifyPlayerJoined=-1;
747
748 //      create_player_appearance_effect(&Objects[Players[playernum].objnum]);
749         multi_make_player_ghost(playernum);
750
751         if (Newdemo_state == ND_STATE_RECORDING)
752                 newdemo_record_multi_disconnect(playernum);
753
754         multi_strip_robots(playernum);
755 }
756                 
757 void
758 network_new_player(sequence_packet *their)
759 {
760         int objnum;
761         int pnum;
762
763         pnum = their->player.connected;
764
765         Assert(pnum >= 0);
766         Assert(pnum < MaxNumNetPlayers);        
767         
768         objnum = Players[pnum].objnum;
769
770         if (Newdemo_state == ND_STATE_RECORDING) {
771                 int new_player;
772
773                 if (pnum == N_players)
774                         new_player = 1;
775                 else
776                         new_player = 0;
777                 newdemo_record_multi_connect(pnum, new_player, their->player.callsign);
778         }
779
780         memcpy(Players[pnum].callsign, their->player.callsign, CALLSIGN_LEN+1);
781         memcpy(NetPlayers.players[pnum].callsign, their->player.callsign, CALLSIGN_LEN+1);
782         
783
784    ClipRank (&their->player.rank);
785    NetPlayers.players[pnum].rank=their->player.rank;
786         NetPlayers.players[pnum].version_major=their->player.version_major;
787         NetPlayers.players[pnum].version_minor=their->player.version_minor;
788    network_check_for_old_version(pnum);
789
790         if (Network_game_type == IPX_GAME) {
791                 if ( (*(uint *)their->player.network.ipx.server) != 0 )
792                         ipx_get_local_target( their->player.network.ipx.server, their->player.network.ipx.node, Players[pnum].net_address );
793                 else
794                         memcpy(Players[pnum].net_address, their->player.network.ipx.node, 6);
795         
796                 memcpy(NetPlayers.players[pnum].network.ipx.node, their->player.network.ipx.node, 6);
797                 memcpy(NetPlayers.players[pnum].network.ipx.server, their->player.network.ipx.server, 4);
798         } else {
799                 NetPlayers.players[pnum].network.appletalk.node = their->player.network.appletalk.node;
800                 NetPlayers.players[pnum].network.appletalk.net = their->player.network.appletalk.net;
801                 NetPlayers.players[pnum].network.appletalk.socket = their->player.network.appletalk.socket;
802         }
803
804         Players[pnum].n_packets_got = 0;
805         Players[pnum].connected = 1;
806         Players[pnum].net_kills_total = 0;
807         Players[pnum].net_killed_total = 0;
808         memset(kill_matrix[pnum], 0, MAX_PLAYERS*sizeof(short)); 
809         Players[pnum].score = 0;
810         Players[pnum].flags = 0;
811         Players[pnum].KillGoalCount=0;
812
813         if (pnum == N_players)
814         {
815                 N_players++;
816                 Netgame.numplayers = N_players;
817         }
818
819         digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
820
821    ClipRank (&their->player.rank);
822    
823    if (FindArg("-norankings"))
824           HUD_init_message("'%s' %s\n",their->player.callsign, TXT_JOINING);
825    else   
826      HUD_init_message("%s'%s' %s\n",RankStrings[their->player.rank],their->player.callsign, TXT_JOINING);
827         
828         multi_make_ghost_player(pnum);
829
830         multi_send_score();
831         multi_sort_kill_list();
832
833 //      create_player_appearance_effect(&Objects[objnum]);
834 }
835
836 char RefuseThisPlayer=0,WaitForRefuseAnswer=0,RefuseTeam;
837 char RefusePlayerName[12];
838 fix RefuseTimeLimit=0;
839
840 void network_welcome_player(sequence_packet *their)
841 {
842         // Add a player to a game already in progress
843         ubyte local_address[6];
844         int player_num;
845         int i;
846
847    WaitForRefuseAnswer=0;
848
849         if (FindArg("-NoMatrixCheat"))
850         {
851                 if (their->player.version_minor & (0x0F<3))
852                 {
853                                         network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_DORK);
854                                         return;
855                 }
856         }
857
858         if (HoardEquipped())
859         {
860    // If hoard game, and this guy isn't D2 Christmas (v1.2), dump him
861
862            if ((Game_mode & GM_HOARD) && ((their->player.version_minor & 0x0F)<2))
863                 {
864                         if (Network_game_type == IPX_GAME)
865                                 network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_DORK);
866                         #ifdef MACINTOSH
867                         else
868                                 network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_DORK);
869                         #endif
870                         return;
871                 }
872         }
873
874         // Don't accept new players if we're ending this level.  Its safe to
875         // ignore since they'll request again later
876
877         if ((Endlevel_sequence) || (Control_center_destroyed))
878         {
879                 mprintf((0, "Ignored request from new player to join during endgame.\n"));
880                 if (Network_game_type == IPX_GAME)
881                         network_dump_player(their->player.network.ipx.server,their->player.network.ipx.node, DUMP_ENDLEVEL);
882                 #ifdef MACINTOSH
883                 else
884                         network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_ENDLEVEL);
885                 #endif
886                 return; 
887         }
888
889         if (Network_send_objects || Network_sending_extras)
890         {
891                 // Ignore silently, we're already responding to someone and we can't
892                 // do more than one person at a time.  If we don't dump them they will
893                 // re-request in a few seconds.
894                 return;
895         }
896
897         if (their->player.connected != Current_level_num)
898         {
899                 mprintf((0, "Dumping player due to old level number.\n"));
900                 if (Network_game_type == IPX_GAME)
901                         network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_LEVEL);
902                 #ifdef MACINTOSH
903                 else
904                         network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_LEVEL);
905                 #endif
906                 return;
907         }
908
909         player_num = -1;
910         memset(&Network_player_rejoining, 0, sizeof(sequence_packet));
911         Network_player_added = 0;
912
913         if (Network_game_type == IPX_GAME) {
914                 if ( (*(uint *)their->player.network.ipx.server) != 0 )
915                         ipx_get_local_target( their->player.network.ipx.server, their->player.network.ipx.node, local_address );
916                 else
917                         memcpy(local_address, their->player.network.ipx.node, 6);
918         }
919
920         for (i = 0; i < N_players; i++)
921         {
922                 if ( (Network_game_type == IPX_GAME) && (!stricmp(Players[i].callsign, their->player.callsign )) && (!memcmp(Players[i].net_address,local_address, 6)) ) 
923                 {
924                         player_num = i;
925                         break;
926                 }
927 #ifdef MACINTOSH                // note link to above if
928                         else if ( (!stricmp(Players[i].callsign, their->player.callsign)) &&
929                                     (NetPlayers.players[i].network.appletalk.node == their->player.network.appletalk.node) &&
930                                         (NetPlayers.players[i].network.appletalk.net == their->player.network.appletalk.net)) {
931                         player_num = i;
932                         break;
933                 }
934 #endif
935         }
936
937         if (player_num == -1)
938         {
939                 // Player is new to this game
940
941                 if ( !(Netgame.game_flags & NETGAME_FLAG_CLOSED) && (N_players < MaxNumNetPlayers))
942                 {
943                         // Add player in an open slot, game not full yet
944
945                         player_num = N_players;
946                         Network_player_added = 1;
947                 }
948                 else if (Netgame.game_flags & NETGAME_FLAG_CLOSED)
949                 {
950                         // Slots are open but game is closed
951
952                         if (Network_game_type == IPX_GAME)
953                                 network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_CLOSED);
954                         #ifdef MACINTOSH
955                         else
956                                 network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_CLOSED);
957                         #endif
958                         return;
959                 }
960                 else
961                 {
962                         // Slots are full but game is open, see if anyone is
963                         // disconnected and replace the oldest player with this new one
964                 
965                         int oldest_player = -1;
966                         fix oldest_time = timer_get_approx_seconds();
967
968                         Assert(N_players == MaxNumNetPlayers);
969
970                         for (i = 0; i < N_players; i++)
971                         {
972                                 if ( (!Players[i].connected) && (LastPacketTime[i] < oldest_time))
973                                 {
974                                         oldest_time = LastPacketTime[i];
975                                         oldest_player = i;
976                                 }
977                         }
978
979                         if (oldest_player == -1)
980                         {
981                                 // Everyone is still connected 
982
983                                 if (Network_game_type == IPX_GAME)
984                                         network_dump_player(their->player.network.ipx.server, their->player.network.ipx.node, DUMP_FULL);
985                                 #ifdef MACINTOSH
986                                 else
987                                         network_dump_appletalk_player(their->player.network.appletalk.node, their->player.network.appletalk.net, their->player.network.appletalk.socket, DUMP_FULL);
988                                 #endif
989                                 return;
990                         }
991                         else
992                         {       
993                                 // Found a slot!
994
995                                 player_num = oldest_player;
996                                 Network_player_added = 1;
997                         }
998                 }
999         }
1000         else 
1001         {
1002                 // Player is reconnecting
1003                 
1004                 if (Players[player_num].connected)
1005                 {
1006                         mprintf((0, "Extra REQUEST from player ignored.\n"));
1007                         return;
1008                 }
1009
1010                 if (Newdemo_state == ND_STATE_RECORDING)
1011                         newdemo_record_multi_reconnect(player_num);
1012
1013                 Network_player_added = 0;
1014
1015                 digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
1016                 
1017                 if (FindArg("-norankings"))
1018                         HUD_init_message("'%s' %s", Players[player_num].callsign, TXT_REJOIN);
1019                 else
1020                         HUD_init_message("%s'%s' %s", RankStrings[NetPlayers.players[player_num].rank],Players[player_num].callsign, TXT_REJOIN);
1021         }
1022
1023         Players[player_num].KillGoalCount=0;
1024
1025         // Send updated Objects data to the new/returning player
1026
1027         
1028         Network_player_rejoining = *their;
1029         Network_player_rejoining.player.connected = player_num;
1030         Network_send_objects = 1;
1031         Network_send_objnum = -1;
1032
1033         network_send_objects();
1034 }
1035
1036 int network_objnum_is_past(int objnum)
1037 {
1038         // determine whether or not a given object number has already been sent
1039         // to a re-joining player.
1040         
1041         int player_num = Network_player_rejoining.player.connected;
1042         int obj_mode = !((object_owner[objnum] == -1) || (object_owner[objnum] == player_num));
1043
1044         if (!Network_send_objects)
1045                 return 0; // We're not sending objects to a new player
1046
1047         if (obj_mode > Network_send_object_mode)
1048                 return 0;
1049         else if (obj_mode < Network_send_object_mode)
1050                 return 1;
1051         else if (objnum < Network_send_objnum)
1052                 return 1;
1053         else
1054                 return 0;
1055 }
1056
1057 #define OBJ_PACKETS_PER_FRAME 1
1058 extern void multi_send_active_door(char);
1059 extern void multi_send_door_open_specific(int,int,int,ubyte);
1060
1061
1062 void network_send_door_updates(int pnum)
1063 {
1064         // Send door status when new player joins
1065         
1066         int i;
1067    
1068    pnum=pnum;
1069
1070 //   Assert (pnum>-1 && pnum<N_players);
1071
1072         for (i = 0; i < Num_walls; i++)
1073         {
1074       if ((Walls[i].type == WALL_DOOR) && ((Walls[i].state == WALL_DOOR_OPENING) || (Walls[i].state == WALL_DOOR_WAITING) || (Walls[i].state == WALL_DOOR_OPEN)))
1075                         multi_send_door_open_specific(pnum,Walls[i].segnum, Walls[i].sidenum,Walls[i].flags);
1076                 else if ((Walls[i].type == WALL_BLASTABLE) && (Walls[i].flags & WALL_BLASTED))
1077                         multi_send_door_open_specific(pnum,Walls[i].segnum, Walls[i].sidenum,Walls[i].flags);
1078                 else if ((Walls[i].type == WALL_BLASTABLE) && (Walls[i].hps != WALL_HPS))
1079                         multi_send_hostage_door_status(i);
1080                 else
1081                         multi_send_wall_status_specific(pnum,i,Walls[i].type,Walls[i].flags,Walls[i].state);
1082         }
1083 }
1084
1085 extern vms_vector MarkerPoint[];
1086 void network_send_markers()
1087  {
1088   // send marker positions/text to new player
1089
1090   
1091   int i;
1092   
1093   for (i=0;i<8;i++)
1094    {
1095     if (MarkerObject[(i*2)]!=-1)
1096      multi_send_drop_marker (i,MarkerPoint[(i*2)],0,MarkerMessage[i*2]);
1097     if (MarkerObject[(i*2)+1]!=-1)
1098      multi_send_drop_marker (i,MarkerPoint[(i*2)+1],1,MarkerMessage[(i*2)+1]);
1099    }
1100  }
1101
1102 void network_process_monitor_vector(int vector)
1103 {
1104         int i, j;
1105         int count = 0;
1106         segment *seg;
1107         
1108         for (i=0; i <= Highest_segment_index; i++)
1109         {
1110                 int tm, ec, bm;
1111                 seg = &Segments[i];
1112                 for (j = 0; j < 6; j++)
1113                 {
1114                         if ( ((tm = seg->sides[j].tmap_num2) != 0) &&
1115                                   ((ec = TmapInfo[tm&0x3fff].eclip_num) != -1) &&
1116                                   ((bm = Effects[ec].dest_bm_num) != -1) )
1117                         {
1118                                 if (vector & (1 << count))
1119                                 {
1120                                         seg->sides[j].tmap_num2 = bm | (tm&0xc000);
1121                                 //      mprintf((0, "Monitor %d blown up.\n", count));
1122                                 }
1123                                 //else
1124                                   //    mprintf((0, "Monitor %d intact.\n", count));
1125                                 count++;
1126                                 Assert(count < 32);
1127                         }
1128                 }
1129         }
1130 }
1131
1132 int network_create_monitor_vector(void)
1133 {
1134         int i, j, k;
1135         int num_blown_bitmaps = 0;
1136         int monitor_num = 0;
1137         #define NUM_BLOWN_BITMAPS 20
1138         int blown_bitmaps[NUM_BLOWN_BITMAPS];
1139         int vector = 0;
1140         segment *seg;
1141
1142         for (i=0; i < Num_effects; i++)
1143         {
1144                 if (Effects[i].dest_bm_num > 0) {
1145                         for (j = 0; j < num_blown_bitmaps; j++)
1146                                 if (blown_bitmaps[j] == Effects[i].dest_bm_num)
1147                                         break;
1148                         if (j == num_blown_bitmaps) {
1149                                 blown_bitmaps[num_blown_bitmaps++] = Effects[i].dest_bm_num;
1150                                 Assert(num_blown_bitmaps < NUM_BLOWN_BITMAPS);
1151                         }
1152                 }
1153         }               
1154                 
1155 //      for (i = 0; i < num_blown_bitmaps; i++)
1156 //              mprintf((0, "Blown bitmap #%d = %d.\n", i, blown_bitmaps[i]));
1157
1158         for (i=0; i <= Highest_segment_index; i++)
1159         {
1160                 int tm, ec;
1161                 seg = &Segments[i];
1162                 for (j = 0; j < 6; j++)
1163                 {
1164                         if ((tm = seg->sides[j].tmap_num2) != 0) 
1165                         {
1166                                 if ( ((ec = TmapInfo[tm&0x3fff].eclip_num) != -1) &&
1167                                           (Effects[ec].dest_bm_num != -1) )
1168                                 {
1169                                 //      mprintf((0, "Monitor %d intact.\n", monitor_num));
1170                                         monitor_num++;
1171                                         Assert(monitor_num < 32);
1172                                 }
1173                                 else
1174                                 {
1175                                         for (k = 0; k < num_blown_bitmaps; k++)
1176                                         {
1177                                                 if ((tm&0x3fff) == blown_bitmaps[k])
1178                                                 {
1179                                                         //mprintf((0, "Monitor %d destroyed.\n", monitor_num));
1180                                                         vector |= (1 << monitor_num);
1181                                                         monitor_num++;
1182                                                         Assert(monitor_num < 32);
1183                                                         break;
1184                                                 }
1185                                         }
1186                                 }
1187                         }
1188                 }
1189         }
1190   //    mprintf((0, "Final monitor vector %x.\n", vector));
1191         return(vector);
1192 }
1193
1194 void network_stop_resync(sequence_packet *their)
1195 {
1196         if (Network_game_type == IPX_GAME) {
1197                 if ( (!memcmp(Network_player_rejoining.player.network.ipx.node, their->player.network.ipx.node, 6)) &&
1198                           (!memcmp(Network_player_rejoining.player.network.ipx.server, their->player.network.ipx.server, 4)) &&
1199                      (!stricmp(Network_player_rejoining.player.callsign, their->player.callsign)) )
1200                 {
1201                         mprintf((0, "Aborting resync for player %s.\n", their->player.callsign));
1202                         Network_send_objects = 0;
1203                         Network_sending_extras=0;
1204                         Network_rejoined=0;
1205                         Player_joining_extras=-1;
1206                         Network_send_objnum = -1;
1207                 }
1208         } else {
1209                 if ( (Network_player_rejoining.player.network.appletalk.node == their->player.network.appletalk.node) &&
1210                          (Network_player_rejoining.player.network.appletalk.net == their->player.network.appletalk.net) &&
1211                          (!stricmp(Network_player_rejoining.player.callsign, their->player.callsign)) )
1212                 {
1213                         mprintf((0, "Aborting resync for player %s.\n", their->player.callsign));
1214                         Network_send_objects = 0;
1215                         Network_sending_extras=0;
1216                         Network_rejoined=0;
1217                         Player_joining_extras=-1;
1218                         Network_send_objnum = -1;
1219                 }
1220         }
1221 }
1222
1223 byte object_buffer[IPX_MAX_DATA_SIZE];
1224
1225 void network_send_objects(void)
1226 {
1227         short remote_objnum;
1228         byte owner;
1229         int loc, i, h;
1230
1231         static int obj_count = 0;
1232         static int frame_num = 0;
1233
1234         int obj_count_frame = 0;
1235         int player_num = Network_player_rejoining.player.connected;
1236
1237         // Send clear objects array trigger and send player num
1238
1239         Assert(Network_send_objects != 0);
1240         Assert(player_num >= 0);
1241         Assert(player_num < MaxNumNetPlayers);
1242
1243         if (Endlevel_sequence || Control_center_destroyed)
1244         {
1245                 // Endlevel started before we finished sending the goods, we'll
1246                 // have to stop and try again after the level.
1247
1248                 if (Network_game_type == IPX_GAME)
1249                         network_dump_player(Network_player_rejoining.player.network.ipx.server,Network_player_rejoining.player.network.ipx.node, DUMP_ENDLEVEL);
1250                 #ifdef MACINTOSH
1251                 else
1252                         network_dump_appletalk_player(Network_player_rejoining.player.network.appletalk.node,Network_player_rejoining.player.network.appletalk.net, Network_player_rejoining.player.network.appletalk.socket, DUMP_ENDLEVEL);
1253                 #endif
1254                 Network_send_objects = 0; 
1255                 return;
1256         }
1257
1258         for (h = 0; h < OBJ_PACKETS_PER_FRAME; h++) // Do more than 1 per frame, try to speed it up without
1259                                                                                                                           // over-stressing the receiver.
1260         {
1261                 obj_count_frame = 0;
1262                 memset(object_buffer, 0, IPX_MAX_DATA_SIZE);
1263                 object_buffer[0] = PID_OBJECT_DATA;
1264                 loc = 3;
1265         
1266                 if (Network_send_objnum == -1)
1267                 {
1268                         obj_count = 0;
1269                         Network_send_object_mode = 0;
1270 //       mprintf((0, "Sending object array to player %d.\n", player_num));
1271                         *(short *)(object_buffer+loc) = INTEL_SHORT(-1);        loc += 2;
1272                         object_buffer[loc] = player_num;                loc += 1;
1273                                                                                                         loc += 2; // Placeholder for remote_objnum, not used here
1274                         Network_send_objnum = 0;
1275                         obj_count_frame = 1;
1276                         frame_num = 0;
1277                 }
1278                 
1279                 for (i = Network_send_objnum; i <= Highest_object_index; i++)
1280                 {
1281                         if ((Objects[i].type != OBJ_POWERUP) && (Objects[i].type != OBJ_PLAYER) &&
1282                                  (Objects[i].type != OBJ_CNTRLCEN) && (Objects[i].type != OBJ_GHOST) &&
1283                                  (Objects[i].type != OBJ_ROBOT) && (Objects[i].type != OBJ_HOSTAGE) &&
1284                                  !(Objects[i].type==OBJ_WEAPON && Objects[i].id==PMINE_ID))
1285                                 continue;
1286                         if ((Network_send_object_mode == 0) && ((object_owner[i] != -1) && (object_owner[i] != player_num)))
1287                                 continue;
1288                         if ((Network_send_object_mode == 1) && ((object_owner[i] == -1) || (object_owner[i] == player_num)))
1289                                 continue;
1290
1291                         if ( ((IPX_MAX_DATA_SIZE-1) - loc) < (sizeof(object)+5) )
1292                                 break; // Not enough room for another object
1293
1294                         obj_count_frame++;
1295                         obj_count++;
1296         
1297                         remote_objnum = objnum_local_to_remote((short)i, &owner);
1298                         Assert(owner == object_owner[i]);
1299
1300                         *(short *)(object_buffer+loc) = INTEL_SHORT((short)i);                          loc += 2;
1301                         object_buffer[loc] = owner;                                                                                     loc += 1;
1302                         *(short *)(object_buffer+loc) = INTEL_SHORT(remote_objnum);             loc += 2;
1303 #ifndef MACINTOSH
1304                         memcpy(object_buffer+loc, &Objects[i], sizeof(object)); loc += sizeof(object);
1305 #else
1306                         if (Network_game_type == IPX_GAME) {
1307                                 object tmpobj;
1308
1309                                 memcpy(&tmpobj, &(Objects[i]), sizeof(object));
1310                                 swap_object(&tmpobj);
1311                                 memcpy(&(object_buffer[loc]), &tmpobj, sizeof(object));                 loc += sizeof(object);
1312                         }
1313                         else
1314                                 memcpy(object_buffer+loc, &Objects[i], sizeof(object)); loc += sizeof(object);
1315 #endif
1316 //                      mprintf((0, "..packing object %d, remote %d\n", i, remote_objnum));
1317                 }
1318
1319                 if (obj_count_frame) // Send any objects we've buffered
1320                 {
1321                         frame_num++;
1322         
1323                         Network_send_objnum = i;
1324                         object_buffer[1] = obj_count_frame;  object_buffer[2] = frame_num;
1325 //       mprintf((0, "Object packet %d contains %d objects.\n", frame_num, obj_count_frame));
1326
1327                         Assert(loc <= IPX_MAX_DATA_SIZE);
1328
1329                         if (Network_game_type == IPX_GAME)
1330                                 ipx_send_internetwork_packet_data( object_buffer, loc, Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1331                         #ifdef MACINTOSH
1332                         else
1333                                 appletalk_send_packet_data( object_buffer, loc, Network_player_rejoining.player.network.appletalk.node,
1334                                         Network_player_rejoining.player.network.appletalk.net,
1335                                         Network_player_rejoining.player.network.appletalk.socket);
1336                         #endif
1337
1338                         // OLD ipx_send_packet_data(object_buffer, loc, &Network_player_rejoining.player.node);
1339                 }
1340
1341                 if (i > Highest_object_index)
1342                 {
1343                         if (Network_send_object_mode == 0)
1344                         {
1345                                 Network_send_objnum = 0;
1346                                 Network_send_object_mode = 1; // go to next mode
1347                         }
1348                         else 
1349                         {
1350                                 Assert(Network_send_object_mode == 1); 
1351
1352                                 frame_num++;
1353                                 // Send count so other side can make sure he got them all
1354 //                              mprintf((0, "Sending end marker in packet #%d.\n", frame_num));
1355                                 mprintf((0, "Sent %d objects.\n", obj_count));
1356                                 object_buffer[0] = PID_OBJECT_DATA;
1357                                 object_buffer[1] = 1;
1358                                 object_buffer[2] = frame_num;
1359                                 *(short *)(object_buffer+3) = INTEL_SHORT(-2);
1360                                 *(short *)(object_buffer+6) = INTEL_SHORT(obj_count);
1361                                 //OLD ipx_send_packet_data(object_buffer, 8, &Network_player_rejoining.player.node);
1362                                 if (Network_game_type == IPX_GAME)
1363                                         ipx_send_internetwork_packet_data(object_buffer, 8, Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node);
1364                                 #ifdef MACINTOSH
1365                                 else
1366                                         appletalk_send_packet_data( object_buffer, 8, Network_player_rejoining.player.network.appletalk.node,
1367                                                                                                 Network_player_rejoining.player.network.appletalk.net,
1368                                                                                                 Network_player_rejoining.player.network.appletalk.socket);
1369                                 #endif
1370                                 
1371                         
1372                                 // Send sync packet which tells the player who he is and to start!
1373                                 network_send_rejoin_sync(player_num);
1374                                 mprintf ((0,"VerfiyPlayerJoined is now set to %d\n",player_num));
1375                                 VerifyPlayerJoined=player_num;
1376
1377                                 // Turn off send object mode
1378                                 Network_send_objnum = -1;
1379                                 Network_send_objects = 0;
1380                                 obj_count = 0;
1381
1382                                 //if (!network_i_am_master ())
1383                                 // Int3();  // Bad!! Get Jason.  Someone goofy is trying to get ahold of the game!
1384
1385                                 Network_sending_extras=40; // start to send extras
1386                            Player_joining_extras=player_num;
1387
1388                                 return;
1389                         } // mode == 1;
1390                 } // i > Highest_object_index
1391         } // For PACKETS_PER_FRAME
1392 }
1393
1394 extern void multi_send_powerup_update();
1395
1396 void network_send_rejoin_sync(int player_num)
1397 {
1398         int i, j;
1399
1400         Players[player_num].connected = 1; // connect the new guy
1401         LastPacketTime[player_num] = timer_get_approx_seconds();
1402
1403         if (Endlevel_sequence || Control_center_destroyed)
1404         {
1405                 // Endlevel started before we finished sending the goods, we'll
1406                 // have to stop and try again after the level.
1407
1408                 if (Network_game_type == IPX_GAME)
1409                         network_dump_player(Network_player_rejoining.player.network.ipx.server,Network_player_rejoining.player.network.ipx.node, DUMP_ENDLEVEL);
1410                 #ifdef MACINTOSH
1411                 else
1412                         network_dump_appletalk_player(Network_player_rejoining.player.network.appletalk.node,Network_player_rejoining.player.network.appletalk.net, Network_player_rejoining.player.network.appletalk.socket, DUMP_ENDLEVEL);
1413                 #endif
1414                 Network_send_objects = 0; 
1415                 Network_sending_extras=0;
1416                 return;
1417         }
1418
1419         if (Network_player_added)
1420         {
1421                 Network_player_rejoining.type = PID_ADDPLAYER;
1422                 Network_player_rejoining.player.connected = player_num;
1423                 network_new_player(&Network_player_rejoining);
1424
1425                 for (i = 0; i < N_players; i++)
1426                 {
1427                         if ((i != player_num) && (i != Player_num) && (Players[i].connected))
1428                                 if (Network_game_type == IPX_GAME) {
1429                                         #ifndef MACINTOSH
1430                                         ipx_send_packet_data( (ubyte *)&Network_player_rejoining, sizeof(sequence_packet), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, Players[i].net_address);
1431                                         #else
1432                                         send_sequence_packet( Network_player_rejoining, NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, Players[i].net_address);
1433                                         #endif
1434                                 #ifdef MACINTOSH
1435                                 } else {
1436                                         appletalk_send_packet_data( (ubyte *)&Network_player_rejoining, sizeof(sequence_packet), NetPlayers.players[i].network.appletalk.node,
1437                                                                                                 NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket);
1438                                 #endif
1439                                 }
1440                 }
1441         }       
1442
1443         // Send sync packet to the new guy
1444
1445         network_update_netgame();
1446
1447         // Fill in the kill list
1448         for (j=0; j<MAX_PLAYERS; j++)
1449         {
1450                 for (i=0; i<MAX_PLAYERS;i++)
1451                         Netgame.kills[j][i] = kill_matrix[j][i];
1452                 Netgame.killed[j] = Players[j].net_killed_total;
1453                 Netgame.player_kills[j] = Players[j].net_kills_total;
1454                 Netgame.player_score[j] = Players[j].score;
1455         }       
1456
1457         Netgame.level_time = Players[Player_num].time_level;
1458         Netgame.monitor_vector = network_create_monitor_vector();
1459
1460         mprintf((0, "Sending rejoin sync packet!!!\n"));
1461
1462         if (Network_game_type == IPX_GAME) {
1463                 #ifndef MACINTOSH       
1464                 ipx_send_internetwork_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1465                 ipx_send_internetwork_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1466                 #else
1467                 send_netgame_packet(Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node, NULL, 0);
1468                 send_netplayers_packet(Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node);
1469                 #endif
1470         #ifdef MACINTOSH
1471         } else {
1472                 appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), Network_player_rejoining.player.network.appletalk.node,
1473                                                                         Network_player_rejoining.player.network.appletalk.net,
1474                                                                         Network_player_rejoining.player.network.appletalk.socket);
1475                 appletalk_send_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), Network_player_rejoining.player.network.appletalk.node,
1476                                                                         Network_player_rejoining.player.network.appletalk.net,
1477                                                                         Network_player_rejoining.player.network.appletalk.socket);
1478         #endif
1479         }
1480         return;
1481 }
1482 void resend_sync_due_to_packet_loss_for_allender ()
1483 {
1484    int i,j;
1485
1486    mprintf ((0,"I'm resending a sync packet! VPJ=%d\n",VerifyPlayerJoined));
1487   
1488         network_update_netgame();
1489
1490         // Fill in the kill list
1491         for (j=0; j<MAX_PLAYERS; j++)
1492         {
1493                 for (i=0; i<MAX_PLAYERS;i++)
1494                         Netgame.kills[j][i] = kill_matrix[j][i];
1495                 Netgame.killed[j] = Players[j].net_killed_total;
1496                 Netgame.player_kills[j] = Players[j].net_kills_total;
1497                 Netgame.player_score[j] = Players[j].score;
1498         }       
1499
1500         Netgame.level_time = Players[Player_num].time_level;
1501         Netgame.monitor_vector = network_create_monitor_vector();
1502
1503         if (Network_game_type == IPX_GAME) {
1504                 #ifndef MACINTOSH       
1505                 ipx_send_internetwork_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1506                 ipx_send_internetwork_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node );
1507                 #else
1508                 send_netgame_packet(Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node, NULL, 0);
1509                 send_netplayers_packet(Network_player_rejoining.player.network.ipx.server, Network_player_rejoining.player.network.ipx.node);
1510                 #endif
1511         #ifdef MACINTOSH
1512         } else {
1513                 appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), Network_player_rejoining.player.network.appletalk.node,
1514                                                                         Network_player_rejoining.player.network.appletalk.net,
1515                                                                         Network_player_rejoining.player.network.appletalk.socket);
1516                 appletalk_send_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), Network_player_rejoining.player.network.appletalk.node,
1517                                                                         Network_player_rejoining.player.network.appletalk.net,
1518                                                                         Network_player_rejoining.player.network.appletalk.socket);
1519         #endif
1520         }
1521 }
1522
1523
1524 char * network_get_player_name( int objnum )
1525 {
1526         if ( objnum < 0 ) return NULL; 
1527         if ( Objects[objnum].type != OBJ_PLAYER ) return NULL;
1528         if ( Objects[objnum].id >= MAX_PLAYERS ) return NULL;
1529         if ( Objects[objnum].id >= N_players ) return NULL;
1530         
1531         return Players[Objects[objnum].id].callsign;
1532 }
1533
1534
1535 void network_add_player(sequence_packet *p)
1536 {
1537         int i;
1538         
1539         mprintf((0, "Got add player request!\n"));
1540
1541         for (i=0; i<N_players; i++ )    {
1542                 if (Network_game_type == IPX_GAME) {
1543                         if ( !memcmp( NetPlayers.players[i].network.ipx.node, p->player.network.ipx.node, 6) && !memcmp(NetPlayers.players[i].network.ipx.server, p->player.network.ipx.server, 4)) 
1544                                 return;         // already got them
1545                 } else {
1546                         if ( (NetPlayers.players[i].network.appletalk.node == p->player.network.appletalk.node) &&
1547                                  (NetPlayers.players[i].network.appletalk.net == p->player.network.appletalk.net))
1548                                         return;
1549                 }
1550         }
1551
1552         if (Network_game_type == IPX_GAME) {
1553                 memcpy( NetPlayers.players[N_players].network.ipx.node, p->player.network.ipx.node, 6 );
1554                 memcpy( NetPlayers.players[N_players].network.ipx.server, p->player.network.ipx.server, 4 );
1555         } else {
1556                 NetPlayers.players[N_players].network.appletalk.node = p->player.network.appletalk.node;
1557                 NetPlayers.players[N_players].network.appletalk.net = p->player.network.appletalk.net;
1558                 NetPlayers.players[N_players].network.appletalk.socket = p->player.network.appletalk.socket;
1559         }
1560    
1561    ClipRank (&p->player.rank);
1562   
1563         memcpy( NetPlayers.players[N_players].callsign, p->player.callsign, CALLSIGN_LEN+1 );
1564         NetPlayers.players[N_players].version_major=p->player.version_major;
1565         NetPlayers.players[N_players].version_minor=p->player.version_minor;
1566    NetPlayers.players[N_players].rank=p->player.rank;
1567         NetPlayers.players[N_players].connected = 1;
1568    network_check_for_old_version (N_players);
1569
1570         Players[N_players].KillGoalCount=0;
1571         Players[N_players].connected = 1;
1572         LastPacketTime[N_players] = timer_get_approx_seconds();
1573         N_players++;
1574         Netgame.numplayers = N_players;
1575
1576         // Broadcast updated info
1577
1578    mprintf ((0,"sending_game_info!\n"));
1579         network_send_game_info(NULL);
1580 }
1581
1582 // One of the players decided not to join the game
1583
1584 void network_remove_player(sequence_packet *p)
1585 {
1586         int i,pn;
1587         
1588         pn = -1;
1589         for (i=0; i<N_players; i++ )    {
1590                 if (Network_game_type == IPX_GAME) {
1591                         if (!memcmp(NetPlayers.players[i].network.ipx.node, p->player.network.ipx.node, 6) && !memcmp(NetPlayers.players[i].network.ipx.server, p->player.network.ipx.server, 4)) {
1592                                 pn = i;
1593                                 break;
1594                         }
1595                 } else {
1596                         if ( (NetPlayers.players[i].network.appletalk.node == p->player.network.appletalk.node) && (NetPlayers.players[i].network.appletalk.net == p->player.network.appletalk.net) ) {
1597                                 pn = i;
1598                                 break;
1599                         }
1600                 }
1601         }
1602         
1603         if (pn < 0 ) return;
1604
1605         for (i=pn; i<N_players-1; i++ ) {
1606                 if (Network_game_type == IPX_GAME) {
1607                         memcpy( NetPlayers.players[i].network.ipx.node, NetPlayers.players[i+1].network.ipx.node, 6 );
1608                         memcpy( NetPlayers.players[i].network.ipx.server, NetPlayers.players[i+1].network.ipx.server, 4 );
1609                 } else {
1610                         NetPlayers.players[i].network.appletalk.node = NetPlayers.players[i+1].network.appletalk.node;
1611                         NetPlayers.players[i].network.appletalk.net = NetPlayers.players[i+1].network.appletalk.net;
1612                         NetPlayers.players[i].network.appletalk.socket = NetPlayers.players[i+1].network.appletalk.socket;
1613                 }
1614                 memcpy( NetPlayers.players[i].callsign, NetPlayers.players[i+1].callsign, CALLSIGN_LEN+1 );
1615                 NetPlayers.players[i].version_major=NetPlayers.players[i+1].version_major;
1616                 NetPlayers.players[i].version_minor=NetPlayers.players[i+1].version_minor;
1617
1618            NetPlayers.players[i].rank=NetPlayers.players[i+1].rank;
1619                 ClipRank (&NetPlayers.players[i].rank);
1620            network_check_for_old_version(i);    
1621         }
1622                 
1623         N_players--;
1624         Netgame.numplayers = N_players;
1625
1626         // Broadcast new info
1627
1628         network_send_game_info(NULL);
1629
1630 }
1631
1632 void
1633 network_dump_player(ubyte * server, ubyte *node, int why)
1634 {
1635         // Inform player that he was not chosen for the netgame
1636
1637         sequence_packet temp;
1638
1639         temp.type = PID_DUMP;
1640         memcpy(temp.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1);
1641         temp.player.connected = why;
1642         if (Network_game_type == IPX_GAME) {
1643                 #ifndef MACINTOSH
1644                 ipx_send_internetwork_packet_data( (ubyte *)&temp, sizeof(sequence_packet), server, node);
1645                 #else
1646                 send_sequence_packet( temp, server, node, NULL);
1647                 #endif
1648         } else {
1649                 Int3();
1650         }
1651 }
1652
1653 #ifdef MACINTOSH
1654 void network_dump_appletalk_player(ubyte node, ushort net, ubyte socket, int why)
1655 {
1656         sequence_packet temp;
1657
1658         temp.type = PID_DUMP;
1659         memcpy(temp.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1);
1660         temp.player.connected = why;
1661         if (Network_game_type == APPLETALK_GAME) {
1662                 appletalk_send_packet_data( (ubyte *)&temp, sizeof(sequence_packet), node, net, socket );
1663         } else {
1664                 Int3();
1665         }
1666 }
1667 #endif
1668
1669 void
1670 network_send_game_list_request()
1671 {
1672         // Send a broadcast request for game info
1673
1674         sequence_packet me;
1675
1676         mprintf((0, "Sending game_list request.\n"));
1677         me.type = PID_GAME_LIST;
1678         memcpy( me.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1 );
1679
1680         if (Network_game_type == IPX_GAME) {
1681                 memcpy( me.player.network.ipx.node, ipx_get_my_local_address(), 6 );
1682                 memcpy( me.player.network.ipx.server, ipx_get_my_server_address(), 4 );
1683
1684                 #ifndef MACINTOSH       
1685                 ipx_send_broadcast_packet_data( (ubyte *)&me, sizeof(sequence_packet) );
1686                 #else
1687                 send_sequence_packet( me, NULL, NULL, NULL);
1688                 #endif
1689         #ifdef MACINTOSH
1690         } else {
1691                 me.player.network.appletalk.node = appletalk_get_my_node();
1692                 me.player.network.appletalk.net = appletalk_get_my_net();
1693                 me.player.network.appletalk.socket = appletalk_get_my_socket();
1694                 appletalk_send_game_info( (ubyte *)&me, sizeof(sequence_packet), Network_zone_name );
1695         #endif
1696         }
1697 }
1698
1699 void network_send_all_info_request(char type,int which_security)
1700 {
1701         // Send a broadcast request for game info
1702
1703         sequence_packet me;
1704
1705         mprintf((0, "Sending all_info request.\n"));
1706    
1707         me.Security=which_security;
1708         me.type = type;
1709         memcpy( me.player.callsign, Players[Player_num].callsign, CALLSIGN_LEN+1 );
1710         
1711         if (Network_game_type == IPX_GAME) {
1712                 memcpy( me.player.network.ipx.node, ipx_get_my_local_address(), 6 );
1713                 memcpy( me.player.network.ipx.server, ipx_get_my_server_address(), 4 );
1714
1715                 #ifndef MACINTOSH       
1716                 ipx_send_broadcast_packet_data( (ubyte *)&me, sizeof(sequence_packet) );
1717                 #else
1718                 send_sequence_packet(me, NULL, NULL, NULL);
1719                 #endif
1720         #ifdef MACINTOSH
1721         } else {
1722                 me.player.network.appletalk.node = appletalk_get_my_node();
1723                 me.player.network.appletalk.net = appletalk_get_my_net();
1724                 me.player.network.appletalk.socket = appletalk_get_my_socket();
1725                 appletalk_send_game_info( (ubyte *)&me, sizeof(sequence_packet), Network_zone_name );
1726         #endif
1727         }
1728 }
1729
1730
1731 void
1732 network_update_netgame(void)
1733 {
1734         // Update the netgame struct with current game variables
1735
1736         int i, j;
1737
1738    Netgame.numconnected=0;
1739    for (i=0;i<N_players;i++)
1740          if (Players[i].connected)
1741                 Netgame.numconnected++;
1742
1743 // This is great: D2 1.0 and 1.1 ignore upper part of the game_flags field of
1744 //      the lite_info struct when you're sitting on the join netgame screen.  We can
1745 //      "sneak" Hoard information into this field.  This is better than sending 
1746 //      another packet that could be lost in transit.
1747
1748
1749         if (HoardEquipped())
1750         {
1751                 if (Game_mode & GM_HOARD)
1752                 {
1753                         Netgame.game_flags |=NETGAME_FLAG_HOARD;
1754                         if (Game_mode & GM_TEAM)
1755                                 Netgame.game_flags |=NETGAME_FLAG_TEAM_HOARD;
1756                 }
1757         }
1758  
1759         if (Network_status == NETSTAT_STARTING)
1760                 return;
1761
1762         Netgame.numplayers = N_players;
1763         Netgame.game_status = Network_status;
1764         Netgame.max_numplayers = MaxNumNetPlayers;
1765
1766         for (i = 0; i < MAX_NUM_NET_PLAYERS; i++) 
1767         {
1768                 NetPlayers.players[i].connected = Players[i].connected;
1769                 for(j = 0; j < MAX_NUM_NET_PLAYERS; j++)
1770                         Netgame.kills[i][j] = kill_matrix[i][j];
1771
1772                 Netgame.killed[i] = Players[i].net_killed_total;
1773                 Netgame.player_kills[i] = Players[i].net_kills_total;
1774                 Netgame.player_score[i] = Players[i].score;
1775                 Netgame.player_flags[i] = (Players[i].flags & (PLAYER_FLAGS_BLUE_KEY | PLAYER_FLAGS_RED_KEY | PLAYER_FLAGS_GOLD_KEY));
1776         }
1777         Netgame.team_kills[0] = team_kills[0];
1778         Netgame.team_kills[1] = team_kills[1];
1779         Netgame.levelnum = Current_level_num;
1780
1781  
1782 }
1783
1784 void
1785 network_send_endlevel_sub(int player_num)
1786 {
1787         endlevel_info end;
1788         int i, j;
1789
1790         // Send an endlevel packet for a player
1791         end.type                = PID_ENDLEVEL;
1792         end.player_num = player_num;
1793         end.connected   = Players[player_num].connected;
1794         end.kills               = INTEL_SHORT(Players[player_num].net_kills_total);
1795         end.killed              = INTEL_SHORT(Players[player_num].net_killed_total);
1796         memcpy(end.kill_matrix, kill_matrix[player_num], MAX_PLAYERS*sizeof(short));
1797 #ifdef MACINTOSH
1798         for (i = 0; i < MAX_PLAYERS; i++)
1799                 for (j = 0; j < MAX_PLAYERS; j++)
1800                         end.kill_matrix[i][j] = INTEL_SHORT(end.kill_matrix[i][j]);
1801 #else
1802         j = j;          // to satisfy compiler
1803 #endif
1804
1805         if (Players[player_num].connected == 1) // Still playing
1806         {
1807                 Assert(Control_center_destroyed);
1808                 end.seconds_left = Countdown_seconds_left;
1809         }
1810
1811 //      mprintf((0, "Sending endlevel packet.\n"));
1812
1813         for (i = 0; i < N_players; i++)
1814         {       
1815                 if ((i != Player_num) && (i!=player_num) && (Players[i].connected)) {
1816                   if (Players[i].connected==1)
1817                          network_send_endlevel_short_sub(player_num,i);
1818                   else {
1819                         if (Network_game_type == IPX_GAME)
1820                                 ipx_send_packet_data((ubyte *)&end, sizeof(endlevel_info), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node,Players[i].net_address);
1821                         #ifdef MACINTOSH
1822                         else
1823                                 appletalk_send_packet_data( (ubyte *)&end, sizeof(endlevel_info), NetPlayers.players[i].network.appletalk.node,
1824                                                                                         NetPlayers.players[i].network.appletalk.net,
1825                                                                                         NetPlayers.players[i].network.appletalk.socket);
1826                         #endif
1827                         }       
1828                 }
1829         }
1830 }
1831
1832 void
1833 network_send_endlevel_packet(void)
1834 {
1835         // Send an updated endlevel status to other hosts
1836
1837         network_send_endlevel_sub(Player_num);
1838 }
1839
1840
1841 void
1842 network_send_endlevel_short_sub(int from_player_num,int to_player)
1843 {
1844         endlevel_info_short end;
1845
1846         // Send an endlevel packet for a player
1847         end.type                = PID_ENDLEVEL_SHORT;
1848         end.player_num = from_player_num;
1849         end.connected   = Players[from_player_num].connected;
1850         end.seconds_left = Countdown_seconds_left;
1851
1852
1853         if (Players[from_player_num].connected == 1) // Still playing
1854         {
1855                 Assert(Control_center_destroyed);
1856         }
1857
1858         if ((to_player != Player_num) && (to_player!=from_player_num) && (Players[to_player].connected))
1859         {
1860                 mprintf((0, "Sending short endlevel packet to %s.\n",Players[to_player].callsign));
1861                 if (Network_game_type == IPX_GAME)
1862                         ipx_send_packet_data((ubyte *)&end, sizeof(endlevel_info_short), NetPlayers.players[to_player].network.ipx.server, NetPlayers.players[to_player].network.ipx.node,Players[to_player].net_address);
1863                 #ifdef MACINTOSH
1864                 else
1865                         appletalk_send_packet_data( (ubyte *)&end, sizeof(endlevel_info_short), NetPlayers.players[to_player].network.appletalk.node,
1866                                                                                 NetPlayers.players[to_player].network.appletalk.net,
1867                                                                                 NetPlayers.players[to_player].network.appletalk.socket);
1868                 #endif
1869         }
1870 }
1871
1872 extern fix ThisLevelTime;
1873
1874 void
1875 network_send_game_info(sequence_packet *their)
1876 {
1877         // Send game info to someone who requested it
1878
1879         char old_type, old_status;
1880    fix timevar;
1881    int i;
1882
1883         mprintf((0, "Sending game info.\n"));
1884
1885         network_update_netgame(); // Update the values in the netgame struct
1886
1887         old_type = Netgame.type;
1888         old_status = Netgame.game_status;
1889
1890         Netgame.type = PID_GAME_INFO;
1891    NetPlayers.type = PID_PLAYERSINFO;
1892
1893    NetPlayers.Security=Netgame.Security;
1894         Netgame.version_major=Version_major;
1895         Netgame.version_minor=Version_minor;
1896
1897         if (Endlevel_sequence || Control_center_destroyed)
1898                 Netgame.game_status = NETSTAT_ENDLEVEL;
1899
1900         if (Netgame.PlayTimeAllowed)
1901     {
1902                 timevar=i2f (Netgame.PlayTimeAllowed*5*60);
1903                 i=f2i(timevar-ThisLevelTime);
1904                 if (i<30)
1905                         Netgame.game_status=NETSTAT_ENDLEVEL;
1906         }       
1907
1908         if (!their) {
1909                 if (Network_game_type == IPX_GAME) {
1910                         #ifndef MACINTOSH
1911                         ipx_send_broadcast_packet_data((ubyte *)&Netgame, sizeof(netgame_info));
1912                         ipx_send_broadcast_packet_data((ubyte *)&NetPlayers,sizeof(AllNetPlayers_info));
1913                         #else
1914                         send_netgame_packet(NULL, NULL, NULL, 0);               // server == NULL says to broadcast packet
1915                         send_netplayers_packet(NULL, NULL);
1916                         #endif
1917                 } // nothing to do for appletalk games I think....
1918         } else {
1919                 if (Network_game_type == IPX_GAME) {
1920                         #ifndef MACINTOSH        
1921                     ipx_send_internetwork_packet_data((ubyte *)&Netgame, sizeof(netgame_info), their->player.network.ipx.server, their->player.network.ipx.node);
1922                         ipx_send_internetwork_packet_data((ubyte *)&NetPlayers,sizeof(AllNetPlayers_info),their->player.network.ipx.server,their->player.network.ipx.node);
1923                         #else
1924                         send_netgame_packet(their->player.network.ipx.server, their->player.network.ipx.node, NULL, 0);
1925                         send_netplayers_packet(their->player.network.ipx.server, their->player.network.ipx.node);
1926                         #endif
1927                 #ifdef MACINTOSH
1928                 } else {
1929                         appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), their->player.network.appletalk.node,
1930                                                                                 their->player.network.appletalk.net, their->player.network.appletalk.socket );
1931                         appletalk_send_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), their->player.network.appletalk.node,
1932                                                                                 their->player.network.appletalk.net, their->player.network.appletalk.socket );
1933                 #endif
1934                 }
1935         }  
1936
1937         Netgame.type = old_type;
1938         Netgame.game_status = old_status;
1939 }       
1940
1941 void network_send_lite_info(sequence_packet *their)
1942 {
1943         // Send game info to someone who requested it
1944
1945         char old_type, old_status,oldstatus;
1946
1947         mprintf((0, "Sending lite game info.\n"));
1948
1949         network_update_netgame(); // Update the values in the netgame struct
1950
1951         old_type = Netgame.type;
1952         old_status = Netgame.game_status;
1953
1954         Netgame.type = PID_LITE_INFO;
1955
1956         if (Endlevel_sequence || Control_center_destroyed)
1957                 Netgame.game_status = NETSTAT_ENDLEVEL;
1958
1959 // If hoard mode, make this game look closed even if it isn't
1960    if (HoardEquipped())
1961         {
1962                 if (Game_mode & GM_HOARD)
1963                 {
1964                         oldstatus=Netgame.game_status;
1965                         Netgame.game_status=NETSTAT_ENDLEVEL;
1966                         Netgame.gamemode=NETGAME_CAPTURE_FLAG;
1967                         if (oldstatus==NETSTAT_ENDLEVEL)
1968                                 Netgame.game_flags|=NETGAME_FLAG_REALLY_ENDLEVEL;
1969                         if (oldstatus==NETSTAT_STARTING)
1970                                 Netgame.game_flags|=NETGAME_FLAG_REALLY_FORMING;
1971                 }
1972         }
1973
1974         if (!their) {
1975                 if (Network_game_type == IPX_GAME) {
1976                         #ifndef MACINTOSH
1977                         ipx_send_broadcast_packet_data((ubyte *)&Netgame, sizeof(lite_info));
1978                         #else
1979                         send_netgame_packet(NULL, NULL, NULL, 1);                       // server == NULL says broadcast
1980                         #endif
1981                 }               // nothing to do for appletalk I think....
1982         } else {
1983                 if (Network_game_type == IPX_GAME) {
1984                         #ifndef MACINTOSH
1985                     ipx_send_internetwork_packet_data((ubyte *)&Netgame, sizeof(lite_info), their->player.network.ipx.server, their->player.network.ipx.node);
1986                         #else
1987                         send_netgame_packet(their->player.network.ipx.server, their->player.network.ipx.node, NULL, 1);
1988                         #endif
1989                 #ifdef MACINTOSH
1990                 } else {
1991                         appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(lite_info), their->player.network.appletalk.node,
1992                                                                                 their->player.network.appletalk.net, their->player.network.appletalk.socket );
1993                 #endif
1994                 }
1995         }  
1996
1997         //  Restore the pre-hoard mode
1998         if (HoardEquipped())
1999         {
2000                 if (Game_mode & GM_HOARD)
2001                 {
2002                         if (!(Game_mode & GM_TEAM))
2003                            Netgame.gamemode=NETGAME_HOARD;
2004                         else
2005                            Netgame.gamemode=NETGAME_TEAM_HOARD;
2006                         Netgame.game_flags&=~NETGAME_FLAG_REALLY_ENDLEVEL;
2007                         Netgame.game_flags&=~NETGAME_FLAG_REALLY_FORMING;
2008                         Netgame.game_flags&=~NETGAME_FLAG_TEAM_HOARD;
2009                 }
2010         }
2011
2012         Netgame.type = old_type;
2013         Netgame.game_status = old_status;
2014
2015 }       
2016
2017 void network_send_netgame_update()
2018 {
2019         // Send game info to someone who requested it
2020
2021         char old_type, old_status;
2022         int i;
2023
2024         mprintf((0, "Sending updated game info.\n"));
2025
2026         network_update_netgame(); // Update the values in the netgame struct
2027
2028         old_type = Netgame.type;
2029         old_status = Netgame.game_status;
2030
2031         Netgame.type = PID_GAME_UPDATE;
2032
2033         if (Endlevel_sequence || Control_center_destroyed)
2034                 Netgame.game_status = NETSTAT_ENDLEVEL;
2035  
2036         for (i=0; i<N_players; i++ )    {
2037                 if ( (Players[i].connected) && (i!=Player_num ) )       {
2038                         if (Network_game_type == IPX_GAME) {
2039                                 #ifndef MACINTOSH
2040                                 ipx_send_packet_data( (ubyte *)&Netgame, sizeof(lite_info), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node,Players[i].net_address );
2041                                 #else
2042                                 send_netgame_packet(NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, Players[i].net_address, 0);
2043                                 #endif
2044                         #ifdef MACINTOSH
2045                         } else {
2046                                 appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(lite_info), NetPlayers.players[i].network.appletalk.node,
2047                                                                                         NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket );
2048                         #endif
2049                         }
2050                 }
2051         }
2052
2053         Netgame.type = old_type;
2054         Netgame.game_status = old_status;
2055 }       
2056                           
2057 int network_send_request(void)
2058 {
2059         // Send a request to join a game 'Netgame'.  Returns 0 if we can join this
2060         // game, non-zero if there is some problem.
2061         int i;
2062
2063         if (Netgame.numplayers < 1)
2064          return 1;
2065
2066         for (i = 0; i < MAX_NUM_NET_PLAYERS; i++)
2067           if (NetPlayers.players[i].connected)
2068               break;
2069
2070         Assert(i < MAX_NUM_NET_PLAYERS);
2071
2072         mprintf((0, "Sending game enroll request to player %d (%s). Serv=%x Node=%x Level=%d\n", i, Players[i].callsign,NetPlayers.players[i].network.ipx.server,NetPlayers.players[i].network.ipx.node,Netgame.levelnum));
2073
2074 //      segments_checksum = netmisc_calc_checksum( Segments, sizeof(segment)*(Highest_segment_index+1) );       
2075
2076         My_Seq.type = PID_REQUEST;
2077         My_Seq.player.connected = Current_level_num;
2078
2079         if (Network_game_type == IPX_GAME) {
2080                 #ifndef MACINTOSH
2081                 ipx_send_internetwork_packet_data((ubyte *)&My_Seq, sizeof(sequence_packet), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node);
2082                 #else
2083                 send_sequence_packet(My_Seq, NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, NULL);
2084                 #endif
2085         #ifdef MACINTOSH
2086         } else {
2087                 appletalk_send_packet_data( (ubyte *)&My_Seq, sizeof(sequence_packet), NetPlayers.players[i].network.appletalk.node,
2088                                                                         NetPlayers.players[i].network.appletalk.net,
2089                                                                         NetPlayers.players[i].network.appletalk.socket);
2090         #endif
2091         }
2092
2093         return i;
2094 }
2095
2096 int SecurityCheck=0;
2097         
2098 void network_process_gameinfo(ubyte *data)
2099 {
2100 #ifdef MACINTOSH
2101         netgame_info tmp_info;
2102 #endif
2103         int i, j;
2104         netgame_info *new = (netgame_info *)data;
2105
2106 #ifdef MACINTOSH
2107         if (Network_game_type == IPX_GAME)  {
2108                 receive_netgame_packet(data, &tmp_info, 0);                     // get correctly aligned structure
2109                 new = &tmp_info;
2110         }
2111 #endif
2112         
2113
2114    WaitingForPlayerInfo=0;
2115
2116    if (new->Security !=TempPlayersInfo->Security)
2117     {
2118      Int3();     // Get Jason
2119      return;     // If this first half doesn't go with the second half
2120     }
2121
2122         Network_games_changed = 1;
2123
2124    //mprintf((0, "Got game data for game %s.\n", new->game_name));
2125
2126    Assert (TempPlayersInfo!=NULL);
2127
2128         for (i = 0; i < num_active_games; i++)
2129          {
2130           //mprintf ((0,"GAMEINFO: Game %d is %s!\n",i,Active_games[i].game_name));
2131           
2132           if (!stricmp(Active_games[i].game_name, new->game_name) && 
2133                                   Active_games[i].Security==new->Security)
2134                break;
2135          }
2136
2137         if (i == MAX_ACTIVE_NETGAMES)
2138         {
2139                 mprintf((0, "Too many netgames.\n"));
2140                 return;
2141         }
2142         
2143 // MWA  memcpy(&Active_games[i], data, sizeof(netgame_info));
2144         memcpy(&Active_games[i], (ubyte *)new, sizeof(netgame_info));
2145         memcpy (&ActiveNetPlayers[i],TempPlayersInfo,sizeof(AllNetPlayers_info));
2146
2147    if (SecurityCheck)
2148          if (Active_games[i].Security==SecurityCheck)
2149                 SecurityCheck=-1;
2150
2151         //mprintf ((0,"Recieved %d unique games...\n",i));
2152
2153         if (i == num_active_games)
2154                 num_active_games++;
2155
2156         if (Active_games[i].numplayers == 0)
2157         {
2158          mprintf ((0,"DELETING THIS GAME!!!\n"));       
2159                 // Delete this game
2160                 for (j = i; j < num_active_games-1; j++)
2161         {
2162              memcpy(&Active_games[j], &Active_games[j+1], sizeof(netgame_info));
2163            memcpy (&ActiveNetPlayers[j],&ActiveNetPlayers[j+1],sizeof(AllNetPlayers_info));
2164         }
2165                 num_active_games--;
2166                 SecurityCheck=0;
2167         }
2168 }
2169
2170 void network_process_lite_info(ubyte *data)
2171 {
2172 #ifdef MACINTOSH
2173         lite_info tmp_info;
2174 #endif
2175         int i, j;
2176         lite_info *new = (lite_info *)data;
2177
2178 #ifdef MACINTOSH
2179         if (Network_game_type == IPX_GAME) {
2180                 receive_netgame_packet(data, (netgame_info *)&tmp_info, 1);
2181                 new = &tmp_info;
2182         }
2183 #endif
2184
2185         Network_games_changed = 1;
2186
2187    //mprintf((0, "Got game data for game %s.\n", new->game_name));
2188
2189         for (i = 0; i < num_active_games; i++)
2190     {
2191       //mprintf ((0,"GAMEINFO: Game %d is %s!\n",i,Active_games[i].game_name));
2192       
2193       if ((!stricmp(Active_games[i].game_name, new->game_name)) && 
2194                          Active_games[i].Security==new->Security)
2195                                 break;
2196     }
2197
2198         if (i == MAX_ACTIVE_NETGAMES)
2199         {
2200                 mprintf((0, "Too many netgames.\n"));
2201                 return;
2202         }
2203         
2204         memcpy(&Active_games[i], (ubyte *)new, sizeof(lite_info));
2205
2206 // See if this is really a Hoard game
2207 // If so, adjust all the data accordingly
2208
2209         if (HoardEquipped())
2210         {
2211                 if (Active_games[i].game_flags & NETGAME_FLAG_HOARD)
2212                 {
2213                         Active_games[i].gamemode=NETGAME_HOARD;                                   
2214                         Active_games[i].game_status=NETSTAT_PLAYING;
2215                         
2216                         if (Active_games[i].game_flags & NETGAME_FLAG_TEAM_HOARD)
2217                                 Active_games[i].gamemode=NETGAME_TEAM_HOARD;                                      
2218                         if (Active_games[i].game_flags & NETGAME_FLAG_REALLY_ENDLEVEL)
2219                                 Active_games[i].game_status=NETSTAT_ENDLEVEL;
2220                         if (Active_games[i].game_flags & NETGAME_FLAG_REALLY_FORMING)
2221                                 Active_games[i].game_status=NETSTAT_STARTING;
2222                 }
2223         }
2224
2225    //mprintf ((0,"Recieved %d unique games...\n",i));
2226
2227         if (i == num_active_games)
2228                 num_active_games++;
2229
2230         if (Active_games[i].numplayers == 0)
2231     {
2232            mprintf ((0,"DELETING THIS GAME!!!\n"));     
2233                 // Delete this game
2234                 for (j = i; j < num_active_games-1; j++)
2235         {
2236              memcpy(&Active_games[j], &Active_games[j+1], sizeof(netgame_info));
2237         }
2238                 num_active_games--;
2239         }
2240 }
2241
2242 void network_process_dump(sequence_packet *their)
2243 {
2244         // Our request for join was denied.  Tell the user why.
2245
2246    char temp[40];
2247    int i;
2248  
2249         mprintf((0, "Dumped by player %s, type %d.\n", their->player.callsign, their->player.connected));
2250
2251    if (their->player.connected!=7)
2252                 nm_messagebox(NULL, 1, TXT_OK, NET_DUMP_STRINGS(their->player.connected));
2253         else
2254                 {
2255                  for (i=0;i<N_players;i++)
2256                         if (!stricmp (their->player.callsign,Players[i].callsign))
2257                         {
2258                                 if (i!=network_who_is_master())
2259                                 {
2260                                         HUD_init_message ("%s attempted to kick you out.",their->player.callsign);
2261                                 }
2262                                 else
2263                                 {
2264                                   sprintf (temp,"%s has kicked you out!",their->player.callsign);
2265                                   nm_messagebox(NULL, 1, TXT_OK, &temp);
2266                             if (Network_status==NETSTAT_PLAYING)
2267                                   {
2268                                         IWasKicked=1;
2269                                         multi_leave_game();     
2270                                   }
2271                                  else
2272                                         Network_status = NETSTAT_MENU;
2273                       }
2274                    }
2275                 }
2276 }       
2277 void network_process_request(sequence_packet *their)
2278 {
2279         // Player is ready to receieve a sync packet
2280         int i;
2281
2282         mprintf((0, "Player %s ready for sync.\n", their->player.callsign));
2283
2284         for (i = 0; i < N_players; i++) {
2285                 if (Network_game_type == IPX_GAME) {
2286                         if (!memcmp(their->player.network.ipx.server, NetPlayers.players[i].network.ipx.server, 4) && !memcmp(their->player.network.ipx.node, NetPlayers.players[i].network.ipx.node, 6) && (!stricmp(their->player.callsign, NetPlayers.players[i].callsign))) {
2287                                 Players[i].connected = 1;
2288                                 break;
2289                         }
2290                 } else {
2291                         if ( (their->player.network.appletalk.node == NetPlayers.players[i].network.appletalk.node) && (their->player.network.appletalk.net == NetPlayers.players[i].network.appletalk.net) && (!stricmp(their->player.callsign, NetPlayers.players[i].callsign)) ) {
2292                                 Players[i].connected = 1;
2293                                 break;
2294                         }
2295                 }
2296         }                       
2297 }
2298
2299 #define REFUSE_INTERVAL F1_0 * 8
2300 extern void multi_reset_object_texture (object *);
2301
2302 void network_process_packet(ubyte *data, int length )
2303 {
2304 #ifdef MACINTOSH
2305         sequence_packet tmp_packet;
2306 #endif
2307         sequence_packet *their = (sequence_packet *)data;
2308
2309 #ifdef MACINTOSH
2310         if (Network_game_type == IPX_GAME) {
2311                 receive_sequence_packet(data, &tmp_packet);
2312                 their = &tmp_packet;                                            // reassign their to point to correctly alinged structure
2313         }
2314 #endif
2315
2316    //mprintf( (0, "Got packet of length %d, type %d\n", length, their->type ));
2317         
2318 //      if ( length < sizeof(sequence_packet) ) return;
2319
2320         length = length;
2321
2322         switch( data[0] )       {
2323         
2324          case PID_GAME_INFO:            // Jason L. says we can safely ignore this type.
2325                 break;
2326         
2327      case PID_PLAYERSINFO:
2328
2329                 mprintf ((0,"Got a PID_PLAYERSINFO!\n"));
2330
2331                 if (Network_status==NETSTAT_WAITING)
2332                 {
2333 #ifndef MACINTOSH                        
2334                         memcpy (&TempPlayersBase,data,sizeof(AllNetPlayers_info));
2335 #else
2336                         if (Network_game_type == IPX_GAME)
2337                                 receive_netplayers_packet(data, &TempPlayersBase);
2338                         else
2339                                 memcpy (&TempPlayersBase,data,sizeof(AllNetPlayers_info));
2340 #endif
2341
2342                         if (TempPlayersBase.Security!=Netgame.Security)
2343                          {
2344                           mprintf ((0,"Bad security for PLAYERSINFO\n"));
2345                           break;
2346                          }      
2347                 
2348                         mprintf ((0,"Got a waiting PID_PLAYERSINFO!\n"));
2349                         if (length!=ALLNETPLAYERSINFO_SIZE)
2350                         {
2351                                 mprintf ((0,"Invalid size for netplayers packet!\n"));
2352                                 return;
2353                         }
2354
2355                         TempPlayersInfo=&TempPlayersBase;
2356                         WaitingForPlayerInfo=0;
2357                         NetSecurityNum=TempPlayersInfo->Security;
2358                         NetSecurityFlag=NETSECURITY_WAIT_FOR_SYNC;
2359            }
2360
2361      break;
2362
2363    case PID_LITE_INFO:
2364
2365          if (length != LITE_INFO_SIZE)
2366                  {
2367                   mprintf ((0,"WARNING! Recieved invalid size for PID_LITE_INFO\n"));
2368                   return;
2369                  }
2370  
2371          if (Network_status == NETSTAT_BROWSING)
2372                 network_process_lite_info (data);
2373     break;
2374
2375         case PID_GAME_LIST:
2376                 // Someone wants a list of games
2377
2378            if (length != SEQUENCE_PACKET_SIZE)
2379                  {
2380                   mprintf ((0,"WARNING! Recieved invalid size for PID_GAME_LIST\n"));
2381                   return;
2382                  }
2383                         
2384                 mprintf((0, "Got a PID_GAME_LIST!\n"));
2385                 if ((Network_status == NETSTAT_PLAYING) || (Network_status == NETSTAT_STARTING) || (Network_status == NETSTAT_ENDLEVEL))
2386                         if (network_i_am_master())
2387                                 network_send_lite_info(their);
2388                 break;
2389
2390
2391         case PID_SEND_ALL_GAMEINFO:
2392
2393            if (length != SEQUENCE_PACKET_SIZE)
2394                  {
2395                   mprintf ((0,"WARNING! Recieved invalid size for PID_SEND_ALL_GAMEINFO\n"));
2396                   return;
2397                  }
2398
2399                 if ((Network_status == NETSTAT_PLAYING) || (Network_status == NETSTAT_STARTING) || (Network_status == NETSTAT_ENDLEVEL))
2400                         if (network_i_am_master() && their->Security==Netgame.Security)
2401                                 network_send_game_info(their);
2402                 break;
2403         
2404         case PID_ADDPLAYER:
2405
2406                 mprintf( (0, "Got NEWPLAYER message from %s.\n", their->player.callsign));
2407                 
2408            if (length != SEQUENCE_PACKET_SIZE)
2409                  {
2410                   mprintf ((0,"WARNING! Recieved invalid size for PID_ADDPLAYER\n"));
2411                   return;
2412                  }
2413                 mprintf( (0, "Got NEWPLAYER message from %s.\n", their->player.callsign));
2414                 network_new_player(their);
2415                 break;                  
2416         case PID_REQUEST:
2417            if (length != SEQUENCE_PACKET_SIZE)
2418                  {
2419                   mprintf ((0,"WARNING! Recieved invalid size for PID_REQUEST\n"));
2420                   return;
2421                  }
2422
2423                 mprintf( (0, "Got REQUEST from '%s'\n", their->player.callsign ));
2424                 if (Network_status == NETSTAT_STARTING) 
2425                 {
2426                         // Someone wants to join our game!
2427                         network_add_player(their);
2428                 }
2429                 else if (Network_status == NETSTAT_WAITING)
2430                 {
2431                         // Someone is ready to recieve a sync packet
2432                         network_process_request(their);
2433                 }
2434                 else if (Network_status == NETSTAT_PLAYING)
2435                 {
2436                         // Someone wants to join a game in progress!
2437                         if (Netgame.RefusePlayers)
2438                 DoRefuseStuff (their);
2439                    else 
2440                                 network_welcome_player(their);
2441                 }
2442                 break;
2443         case PID_DUMP:  
2444
2445            if (length != SEQUENCE_PACKET_SIZE)
2446                  {
2447                   mprintf ((0,"WARNING! Recieved invalid size for PID_DUMP\n"));
2448                   return;
2449                  }
2450   
2451                 if (Network_status == NETSTAT_WAITING || Network_status==NETSTAT_PLAYING )
2452                         network_process_dump(their);
2453                 break;
2454         case PID_QUIT_JOINING:
2455
2456            if (length != SEQUENCE_PACKET_SIZE)
2457                  {
2458                   mprintf ((0,"WARNING! Recieved invalid size for PID_QUIT_JOINING\n"));
2459                   return;
2460                  }
2461                 if (Network_status == NETSTAT_STARTING)
2462                         network_remove_player( their );
2463                 else if ((Network_status == NETSTAT_PLAYING) && (Network_send_objects))
2464                         network_stop_resync( their );
2465                 break;
2466         case PID_SYNC:  
2467
2468                 mprintf ((0,"Got a sync packet! Network_status=%d\n",NETSTAT_WAITING));
2469
2470                 if (Network_status == NETSTAT_WAITING)  {
2471
2472 #ifndef MACINTOSH
2473                         memcpy((ubyte *)&(TempNetInfo), data, sizeof(netgame_info));
2474 #else
2475                         if (Network_game_type == IPX_GAME)
2476                                 receive_netgame_packet(data, &TempNetInfo, 0);
2477                         else
2478                                 memcpy((ubyte *)&(TempNetInfo), data, sizeof(netgame_info));
2479 #endif
2480
2481                         if (TempNetInfo.Security!=Netgame.Security)
2482                          {
2483                                 mprintf ((0,"Bad security on sync packet.\n"));
2484                                 break;
2485                          }
2486
2487                         if (NetSecurityFlag==NETSECURITY_WAIT_FOR_SYNC)
2488                          {
2489                           if (TempNetInfo.Security==TempPlayersInfo->Security)
2490                           {
2491                                 network_read_sync_packet (&TempNetInfo,0);
2492                                 NetSecurityFlag=0;
2493                                 NetSecurityNum=0;
2494                           }     
2495                          }
2496                         else
2497                          {      
2498                         NetSecurityFlag=NETSECURITY_WAIT_FOR_PLAYERS;
2499                         NetSecurityNum=TempNetInfo.Security;
2500
2501                         if ( network_wait_for_playerinfo())
2502                                 network_read_sync_packet((netgame_info *)data,0);
2503  
2504                         NetSecurityFlag=0;
2505                         NetSecurityNum=0;
2506                          }
2507                 }
2508                 break;
2509
2510         case PID_PDATA: 
2511                 if ((Game_mode&GM_NETWORK) && ((Network_status == NETSTAT_PLAYING)||(Network_status == NETSTAT_ENDLEVEL) || Network_status==NETSTAT_WAITING)) { 
2512                         network_process_pdata((char *)data);
2513                 }
2514                 break;
2515    case PID_NAKED_PDATA:
2516                 if ((Game_mode&GM_NETWORK) && ((Network_status == NETSTAT_PLAYING)||(Network_status == NETSTAT_ENDLEVEL) || Network_status==NETSTAT_WAITING)) 
2517                         network_process_naked_pdata((char *)data,length);
2518            break;
2519
2520         case PID_OBJECT_DATA:
2521                 if (Network_status == NETSTAT_WAITING) 
2522                         network_read_object_packet(data);
2523                 break;
2524         case PID_ENDLEVEL:
2525                 if ((Network_status == NETSTAT_ENDLEVEL) || (Network_status == NETSTAT_PLAYING))
2526                         network_read_endlevel_packet(data);
2527                 else
2528                         mprintf((0, "Junked endlevel packet.\n"));
2529                 break;
2530         case PID_ENDLEVEL_SHORT:
2531                 if ((Network_status == NETSTAT_ENDLEVEL) || (Network_status == NETSTAT_PLAYING))
2532                         network_read_endlevel_short_packet(data);
2533                 else
2534                         mprintf((0, "Junked short endlevel packet!\n"));
2535                 break;
2536
2537         case PID_GAME_UPDATE:
2538                 mprintf ((0,"Got a GAME_UPDATE!\n"));
2539                         
2540                 if (Network_status==NETSTAT_PLAYING)
2541                  {
2542 #ifndef MACINTOSH
2543                         memcpy((ubyte *)&TempNetInfo, data, sizeof(lite_info) );
2544 #else
2545                         if (Network_game_type == IPX_GAME)
2546                                 receive_netgame_packet(data, &TempNetInfo, 1);
2547                         else
2548                                 memcpy((ubyte *)&TempNetInfo, data, sizeof(lite_info) );
2549 #endif
2550                   if (TempNetInfo.Security==Netgame.Security)
2551                          memcpy (&Netgame,(ubyte *)&TempNetInfo,sizeof(lite_info));
2552                  }
2553                 if (Game_mode & GM_TEAM)
2554                  {
2555                         int i;
2556
2557                         for (i=0;i<N_players;i++)
2558                          if (Players[i].connected)
2559                         multi_reset_object_texture (&Objects[Players[i].objnum]);
2560                     reset_cockpit();
2561                  }
2562            break;
2563    case PID_PING_SEND:
2564                 network_ping (PID_PING_RETURN,data[1]);
2565                 break;
2566    case PID_PING_RETURN:
2567                 network_handle_ping_return(data[1]);  // data[1] is player who told us of their ping time
2568                 break;
2569    case PID_NAMES_RETURN:
2570                 if (Network_status==NETSTAT_BROWSING && NamesInfoSecurity!=-1)
2571                   network_process_names_return (data);
2572                 break;
2573         case PID_GAME_PLAYERS:
2574                 // Someone wants a list of players in this game
2575
2576            if (length != SEQUENCE_PACKET_SIZE)
2577                  {
2578                   mprintf ((0,"WARNING! Recieved invalid size for PID_GAME_PLAYERS\n"));
2579                   return;
2580                  }
2581                         
2582                 mprintf((0, "Got a PID_GAME_PLAYERS!\n"));
2583                 if ((Network_status == NETSTAT_PLAYING) || (Network_status == NETSTAT_STARTING) || (Network_status == NETSTAT_ENDLEVEL))
2584                         if (network_i_am_master() && their->Security==Netgame.Security)
2585                                 network_send_player_names(their);
2586                 break;
2587
2588         default:
2589                 mprintf((0, "Ignoring invalid packet type.\n"));
2590                 Int3(); // Invalid network packet type, see ROB
2591            break;
2592         }
2593 }
2594
2595 #ifndef NDEBUG
2596 void dump_segments()
2597 {
2598         FILE * fp;
2599
2600         fp = fopen( "TEST.DMP", "wb" );
2601         fwrite( Segments, sizeof(segment)*(Highest_segment_index+1),1, fp );    
2602         fclose(fp);
2603         mprintf( (0, "SS=%d\n", sizeof(segment) ));
2604 }
2605 #endif
2606
2607
2608 void
2609 network_read_endlevel_packet( ubyte *data )
2610 {
2611         // Special packet for end of level syncing
2612 #ifdef MACINTOSH
2613         int i, j;
2614 #endif
2615         int playernum;
2616         endlevel_info *end;     
2617
2618         end = (endlevel_info *)data;
2619 #ifdef MACINTOSH
2620         for (i = 0; i < MAX_PLAYERS; i++)
2621                 for (j = 0; j < MAX_PLAYERS; j++)
2622                         end->kill_matrix[i][j] = INTEL_SHORT(end->kill_matrix[i][j]);
2623         end->kills = INTEL_SHORT(end->kills);
2624         end->killed = INTEL_SHORT(end->killed);
2625 #endif
2626
2627         playernum = end->player_num;
2628         
2629         Assert(playernum != Player_num);
2630     
2631         if (playernum>=N_players)
2632          {              
2633                 Int3(); // weird, but it an happen in a coop restore game
2634                 return; // if it happens in a coop restore, don't worry about it
2635          }
2636
2637         if ((Network_status == NETSTAT_PLAYING) && (end->connected != 0))
2638                 return; // Only accept disconnect packets if we're not out of the level yet
2639
2640         Players[playernum].connected = end->connected;
2641         memcpy(&kill_matrix[playernum][0], end->kill_matrix, MAX_PLAYERS*sizeof(short));
2642         Players[playernum].net_kills_total = end->kills;
2643         Players[playernum].net_killed_total = end->killed;
2644
2645         if ((Players[playernum].connected == 1) && (end->seconds_left < Countdown_seconds_left))
2646                 Countdown_seconds_left = end->seconds_left;
2647
2648         LastPacketTime[playernum] = timer_get_approx_seconds();
2649
2650 //      mprintf((0, "Got endlevel packet from player %d.\n", playernum));
2651 }
2652
2653 void
2654 network_read_endlevel_short_packet( ubyte *data )
2655 {
2656         // Special packet for end of level syncing
2657
2658         int playernum;
2659         endlevel_info_short *end;       
2660
2661         end = (endlevel_info_short *)data;
2662
2663         playernum = end->player_num;
2664         
2665         Assert(playernum != Player_num);
2666     
2667         if (playernum>=N_players)
2668          {              
2669                 Int3(); // weird, but it can happen in a coop restore game
2670                 return; // if it happens in a coop restore, don't worry about it
2671          }
2672
2673         if ((Network_status == NETSTAT_PLAYING) && (end->connected != 0))
2674          {
2675                 //mprintf ((0,"Returning early for short_endlevel\n"));
2676                 return; // Only accept disconnect packets if we're not out of the level yet
2677          }
2678
2679         Players[playernum].connected = end->connected;
2680
2681         if ((Players[playernum].connected == 1) && (end->seconds_left < Countdown_seconds_left))
2682                 Countdown_seconds_left = end->seconds_left;
2683
2684         LastPacketTime[playernum] = timer_get_approx_seconds();
2685 }
2686
2687
2688 void
2689 network_pack_objects(void)
2690 {
2691         // Switching modes, pack the object array
2692
2693         special_reset_objects();
2694 }                               
2695
2696 int
2697 network_verify_objects(int remote, int local)
2698 {
2699         int i;
2700         int nplayers, got_controlcen=0;
2701
2702    mprintf ((0,"NETWORK:remote=%d local=%d\n",remote,local));
2703
2704         if ((remote-local) > 10)
2705                 return(-1);
2706
2707         if (Game_mode & GM_MULTI_ROBOTS)
2708                 got_controlcen = 1;
2709
2710         nplayers = 0;
2711
2712         for (i = 0; i <= Highest_object_index; i++)
2713         {
2714                 if ((Objects[i].type == OBJ_PLAYER) || (Objects[i].type == OBJ_GHOST))
2715                         nplayers++;
2716                 if (Objects[i].type == OBJ_CNTRLCEN)
2717                         got_controlcen=1;
2718         }
2719
2720     if (got_controlcen && (MaxNumNetPlayers<=nplayers))
2721                 return(0);
2722
2723         return(1);
2724 }
2725         
2726 void
2727 network_read_object_packet( ubyte *data )
2728 {
2729         // Object from another net player we need to sync with
2730
2731         short objnum, remote_objnum;
2732         byte obj_owner;
2733         int segnum, i;
2734         object *obj;
2735
2736         static int my_pnum = 0;
2737         static int mode = 0;
2738         static int object_count = 0;
2739         static int frame_num = 0;
2740         int nobj = data[1];
2741         int loc = 3;
2742         int remote_frame_num = data[2];
2743         
2744         frame_num++;
2745
2746 //      mprintf((0, "Object packet %d (remote #%d) contains %d objects.\n", frame_num, remote_frame_num, nobj));
2747
2748         for (i = 0; i < nobj; i++)
2749         {
2750                 objnum = INTEL_SHORT( *(short *)(data+loc) );                   loc += 2;
2751                 obj_owner = data[loc];                                                                  loc += 1;
2752                 remote_objnum = INTEL_SHORT( *(short *)(data+loc) );    loc += 2;
2753
2754                 if (objnum == -1) 
2755                 {
2756                         // Clear object array
2757                         mprintf((0, "Clearing object array.\n"));
2758
2759                         init_objects();
2760                         Network_rejoined = 1;
2761                         my_pnum = obj_owner;
2762                         change_playernum_to(my_pnum);
2763                         mode = 1;
2764                         object_count = 0;
2765                         frame_num = 1;
2766                 }
2767                 else if (objnum == -2)
2768                 {
2769                         // Special debug checksum marker for entire send
2770                         if (mode == 1)
2771                         {
2772                                 network_pack_objects();
2773                                 mode = 0;
2774                         }
2775                         mprintf((0, "Objnum -2 found in frame local %d remote %d.\n", frame_num, remote_frame_num));
2776                         mprintf((0, "Got %d objects, expected %d.\n", object_count, remote_objnum));
2777                         if (remote_objnum != object_count) {
2778                                 Int3();
2779                         }
2780                         if (network_verify_objects(remote_objnum, object_count))
2781                         {
2782                                 // Failed to sync up 
2783                                 nm_messagebox(NULL, 1, TXT_OK, TXT_NET_SYNC_FAILED);
2784                                 Network_status = NETSTAT_MENU;                          
2785                                 return;
2786                         }
2787                         frame_num = 0;
2788                 }
2789                 else 
2790                 {
2791                         if (frame_num != remote_frame_num)
2792                                 Int3();
2793                         mprintf ((0,"Got a type 3 object packet!\n"));
2794                         object_count++;
2795                         if ((obj_owner == my_pnum) || (obj_owner == -1)) 
2796                         {
2797                                 if (mode != 1)
2798                                         Int3(); // SEE ROB
2799                                 objnum = remote_objnum;
2800                                 //if (objnum > Highest_object_index)
2801                                 //{
2802                                 //      Highest_object_index = objnum;
2803                                 //      num_objects = Highest_object_index+1;
2804                                 //}
2805                         }
2806                         else {
2807                                 if (mode == 1)
2808                                 {
2809                                         network_pack_objects();
2810                                         mode = 0;
2811                                 }
2812                                 objnum = obj_allocate();
2813                         }
2814                         if (objnum != -1) {
2815                                 obj = &Objects[objnum];
2816                                 if (obj->segnum != -1)
2817                                         obj_unlink(objnum);
2818                                 Assert(obj->segnum == -1);
2819                                 Assert(objnum < MAX_OBJECTS);
2820                                 memcpy(obj,data+loc,sizeof(object));            loc += sizeof(object);
2821 #ifdef MACINTOSH
2822                                 if (Network_game_type == IPX_GAME)
2823                                         swap_object(obj);
2824 #endif
2825                                 segnum = obj->segnum;
2826                                 obj->next = obj->prev = obj->segnum = -1;
2827                                 obj->attached_obj = -1;
2828                                 if (segnum > -1)
2829                                         obj_link(obj-Objects,segnum);
2830                                 if (obj_owner == my_pnum) 
2831                                         map_objnum_local_to_local(objnum);
2832                                 else if (obj_owner != -1)
2833                                         map_objnum_local_to_remote(objnum, remote_objnum, obj_owner);
2834                                 else
2835                                         object_owner[objnum] = -1;
2836                         }
2837                 } // For a standard onbject
2838         } // For each object in packet
2839 }
2840         
2841 void network_sync_poll( int nitems, newmenu_item * menus, int * key, int citem )
2842 {
2843         // Polling loop waiting for sync packet to start game
2844
2845         static fix t1 = 0;
2846
2847         menus = menus;
2848         citem = citem;
2849         nitems = nitems;
2850
2851         network_listen();
2852
2853         if (Network_status != NETSTAT_WAITING)  // Status changed to playing, exit the menu
2854                 *key = -2;
2855
2856         if (!Network_rejoined && (timer_get_approx_seconds() > t1+F1_0*2))
2857         {
2858                 int i;
2859
2860                 // Poll time expired, re-send request
2861                 
2862                 t1 = timer_get_approx_seconds();
2863
2864                 mprintf((0, "Re-sending join request.\n"));
2865                 i = network_send_request();
2866                 if (i < 0)
2867                         *key = -2;
2868         }
2869 }
2870
2871 void network_start_poll( int nitems, newmenu_item * menus, int * key, int citem )
2872 {
2873         int i,n,nm;
2874
2875         key=key;
2876         citem=citem;
2877
2878         Assert(Network_status == NETSTAT_STARTING);
2879
2880         if (!menus[0].value) {
2881                         menus[0].value = 1;
2882                         menus[0].redraw = 1;
2883         }
2884
2885         for (i=1; i<nitems; i++ )       {
2886                 if ( (i>= N_players) && (menus[i].value) )      {
2887                         menus[i].value = 0;
2888                         menus[i].redraw = 1;
2889                 }
2890         }
2891
2892         nm = 0;
2893         for (i=0; i<nitems; i++ )       {
2894                 if ( menus[i].value )   {
2895                         nm++;
2896                         if ( nm > N_players )   {
2897                                 menus[i].value = 0;
2898                                 menus[i].redraw = 1;
2899                         }
2900                 }
2901         }
2902
2903         if ( nm > MaxNumNetPlayers )    {
2904                 nm_messagebox( TXT_ERROR, 1, TXT_OK, "%s %d %s", TXT_SORRY_ONLY, MaxNumNetPlayers, TXT_NETPLAYERS_IN );
2905                 // Turn off the last player highlighted
2906                 for (i = N_players; i > 0; i--)
2907                         if (menus[i].value == 1) 
2908                         {
2909                                 menus[i].value = 0;
2910                                 menus[i].redraw = 1;
2911                                 break;
2912                         }
2913         }
2914
2915 //       if (nitems > MAX_PLAYERS ) return; 
2916         
2917         n = Netgame.numplayers;
2918         network_listen();
2919
2920         if (n < Netgame.numplayers )    
2921         {
2922                 digi_play_sample (SOUND_HUD_MESSAGE,F1_0);
2923
2924       mprintf ((0,"More players are printed!"));
2925                 if (FindArg("-norankings"))
2926               sprintf( menus[N_players-1].text, "%d. %-20s", N_players,NetPlayers.players[N_players-1].callsign );
2927                 else
2928               sprintf( menus[N_players-1].text, "%d. %s%-20s", N_players, RankStrings[NetPlayers.players[N_players-1].rank],NetPlayers.players[N_players-1].callsign );
2929
2930                 menus[N_players-1].redraw = 1;
2931                 if (N_players <= MaxNumNetPlayers)
2932                 {
2933                         menus[N_players-1].value = 1;
2934                 }
2935         } 
2936         else if ( n > Netgame.numplayers )      
2937         {
2938                 // One got removed...
2939
2940       digi_play_sample (SOUND_HUD_KILL,F1_0);
2941   
2942                 for (i=0; i<N_players; i++ )    
2943                 {
2944          
2945          if (FindArg("-norankings"))    
2946                  sprintf( menus[i].text, "%d. %-20s", i+1, NetPlayers.players[i].callsign );
2947          else
2948                  sprintf( menus[i].text, "%d. %s%-20s", i+1, RankStrings[NetPlayers.players[i].rank],NetPlayers.players[i].callsign );
2949                         if (i < MaxNumNetPlayers)
2950                                 menus[i].value = 1;
2951                         else
2952                                 menus[i].value = 0;
2953                         menus[i].redraw = 1;
2954                 }
2955                 for (i=N_players; i<n; i++ )    
2956                 {
2957                         sprintf( menus[i].text, "%d. ", i+1 );          // Clear out the deleted entries...
2958                         menus[i].value = 0;
2959                         menus[i].redraw = 1;
2960                 }
2961    }
2962 }
2963
2964 int opt_cinvul, opt_team_anarchy, opt_coop, opt_show_on_map, opt_closed,opt_maxnet;
2965 int last_cinvul=0,last_maxnet,opt_team_hoard;
2966 int opt_refuse,opt_capture;
2967
2968 void network_game_param_poll( int nitems, newmenu_item * menus, int * key, int citem )
2969 {
2970         static int oldmaxnet=0;
2971
2972         if (((HoardEquipped() && menus[opt_team_hoard].value) || (menus[opt_team_anarchy].value || menus[opt_capture].value)) && !menus[opt_closed].value && !menus[opt_refuse].value) { 
2973                 menus[opt_refuse].value = 1;
2974                 menus[opt_refuse].redraw = 1;
2975                 menus[opt_refuse-1].value = 0;
2976                 menus[opt_refuse-1].redraw = 1;
2977                 menus[opt_refuse-2].value = 0;
2978                 menus[opt_refuse-2].redraw = 1;
2979         }
2980
2981         #ifndef MACINTOSH
2982         if (menus[opt_coop].value)
2983         #else
2984         if ( (menus[opt_coop].value) || ((Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) && (menus[opt_coop-1].value)) )
2985         #endif
2986         {
2987                 oldmaxnet=1;
2988
2989                 if (menus[opt_maxnet].value>2) 
2990                 {
2991                     menus[opt_maxnet].value=2;
2992                     menus[opt_maxnet].redraw=1;
2993                 }
2994
2995                 if (menus[opt_maxnet].max_value>2)
2996                 {
2997                     menus[opt_maxnet].max_value=2;
2998                     menus[opt_maxnet].redraw=1;
2999                 }
3000
3001                 #ifdef MACINTOSH
3002                 if ( (Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) ) {
3003                         if (menus[opt_maxnet].value > 0) {
3004                             menus[opt_maxnet].value=0;
3005                             menus[opt_maxnet].redraw=1;
3006                         }
3007                         if (menus[opt_maxnet].max_value > 0) {
3008                                 menus[opt_maxnet].max_value=0;
3009                                 menus[opt_maxnet].redraw=1;
3010                         }
3011                 }
3012                 #endif
3013
3014              if (!Netgame.game_flags & NETGAME_FLAG_SHOW_MAP) 
3015                         Netgame.game_flags |= NETGAME_FLAG_SHOW_MAP;
3016
3017                 if (Netgame.PlayTimeAllowed || Netgame.KillGoal)
3018                 {
3019                     Netgame.PlayTimeAllowed=0;
3020                     Netgame.KillGoal=0;
3021                 }
3022
3023         }
3024         else // if !Coop game
3025     {
3026                 if (oldmaxnet)
3027                 {
3028                     oldmaxnet=0;
3029                         menus[opt_maxnet].value=6;
3030                         menus[opt_maxnet].max_value=6;
3031
3032                     #ifdef MACINTOSH
3033                         if ( (Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) ) {
3034                                 menus[opt_maxnet].value=1;
3035                                 menus[opt_maxnet].max_value=1;
3036                         }
3037                         #endif
3038                           
3039                 }
3040         }         
3041
3042     if ( last_maxnet != menus[opt_maxnet].value )   
3043         {
3044                 sprintf( menus[opt_maxnet].text, "Maximum players: %d", menus[opt_maxnet].value+2 );
3045                 last_maxnet = menus[opt_maxnet].value;
3046                 menus[opt_maxnet].redraw = 1;
3047         }               
3048  }
3049
3050 int netgame_difficulty;
3051
3052 int network_get_game_params( char * game_name, int *mode, int *game_flags, int *level )
3053 {
3054         int i;
3055    int opt, opt_name, opt_level, opt_mode,opt_moreopts;
3056         newmenu_item m[20];
3057         char name[NETGAME_NAME_LEN+1];
3058         char slevel[5];
3059         char level_text[32];
3060    char srmaxnet[50];
3061
3062         int new_mission_num;
3063         int anarchy_only;
3064             
3065    *game_flags=*game_flags;
3066
3067    SetAllAllowablesTo (1);
3068    NamesInfoSecurity=-1;
3069
3070         for (i=0;i<MAX_PLAYERS;i++)
3071          if (i!=Player_num)     
3072                 Players[i].callsign[0]=0;    
3073
3074         MaxNumNetPlayers=8;
3075    Netgame.KillGoal=0;
3076    Netgame.PlayTimeAllowed=0;
3077         Netgame.Allow_marker_view=1;
3078         netgame_difficulty=Player_default_difficulty;
3079
3080         new_mission_num = multi_choose_mission(&anarchy_only);
3081
3082         if (new_mission_num < 0)
3083                 return -1;
3084
3085    if (!(FindArg ("-packets") && FindArg ("-shortpackets")))
3086            if (!network_choose_connect ())
3087                         return -1;
3088
3089         #ifdef MACINTOSH
3090         if ( (Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) )
3091                 MaxNumNetPlayers = 3;
3092         #endif
3093         
3094         strcpy(Netgame.mission_name, Mission_list[new_mission_num].filename);
3095         strcpy(Netgame.mission_title, Mission_list[new_mission_num].mission_name);
3096         Netgame.control_invul_time = control_invul_time;
3097         
3098         sprintf( name, "%s%s", Players[Player_num].callsign, TXT_S_GAME );
3099         sprintf( slevel, "1" );
3100
3101         opt = 0;
3102         m[opt].type = NM_TYPE_TEXT; m[opt].text = TXT_DESCRIPTION; opt++;
3103
3104         opt_name = opt;
3105         m[opt].type = NM_TYPE_INPUT; m[opt].text = name; m[opt].text_len = NETGAME_NAME_LEN; opt++;
3106
3107         sprintf(level_text, "%s (1-%d)", TXT_LEVEL_, Last_level);
3108
3109 //      if (Last_secret_level < -1)
3110 //              sprintf(level_text+strlen(level_text)-1, ", S1-S%d)", -Last_secret_level);
3111 //      else if (Last_secret_level == -1)
3112 //              sprintf(level_text+strlen(level_text)-1, ", S1)");
3113
3114         Assert(strlen(level_text) < 32);
3115
3116         m[opt].type = NM_TYPE_TEXT; m[opt].text = level_text; opt++;
3117
3118         opt_level = opt;
3119         m[opt].type = NM_TYPE_INPUT; m[opt].text = slevel; m[opt].text_len=4; opt++;
3120 //      m[opt].type = NM_TYPE_TEXT; m[opt].text = TXT_OPTIONS; opt++;
3121         
3122         opt_mode = opt;
3123         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_ANARCHY; m[opt].value=1; m[opt].group=0; opt++;
3124         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_TEAM_ANARCHY; m[opt].value=0; m[opt].group=0; opt_team_anarchy=opt; opt++;
3125         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_ANARCHY_W_ROBOTS; m[opt].value=0; m[opt].group=0; opt++;
3126         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_COOPERATIVE; m[opt].value=0; m[opt].group=0; opt_coop=opt; opt++;
3127         m[opt].type = NM_TYPE_RADIO; m[opt].text = "Capture the flag"; m[opt].value=0; m[opt].group=0; opt_capture=opt; opt++;
3128    
3129         if (HoardEquipped())
3130         {
3131                 m[opt].type = NM_TYPE_RADIO; m[opt].text = "Hoard"; m[opt].value=0; m[opt].group=0; opt++;
3132                 m[opt].type = NM_TYPE_RADIO; m[opt].text = "Team Hoard"; m[opt].value=0; m[opt].group=0; opt_team_hoard=opt; opt++;
3133            m[opt].type = NM_TYPE_TEXT; m[opt].text = ""; opt++;
3134         } 
3135         else
3136          {  m[opt].type = NM_TYPE_TEXT; m[opt].text = ""; opt++; }
3137
3138         m[opt].type = NM_TYPE_RADIO; m[opt].text = "Open game"; m[opt].group=1; m[opt].value=0; opt++;
3139         opt_closed = opt;
3140         m[opt].type = NM_TYPE_RADIO; m[opt].text = TXT_CLOSED_GAME; m[opt].group=1; m[opt].value=0; opt++;
3141    opt_refuse = opt;
3142    m[opt].type = NM_TYPE_RADIO; m[opt].text = "Restricted Game              "; m[opt].group=1; m[opt].value=Netgame.RefusePlayers; opt++;
3143
3144 //      m[opt].type = NM_TYPE_CHECK; m[opt].text = TXT_SHOW_IDS; m[opt].value=0; opt++;
3145
3146    opt_maxnet = opt;
3147    sprintf( srmaxnet, "Maximum players: %d", MaxNumNetPlayers);
3148    m[opt].type = NM_TYPE_SLIDER; m[opt].value=MaxNumNetPlayers-2; m[opt].text= srmaxnet; m[opt].min_value=0; 
3149    m[opt].max_value=MaxNumNetPlayers-2; opt++;
3150    last_maxnet=MaxNumNetPlayers-2;
3151
3152    opt_moreopts=opt;
3153    m[opt].type = NM_TYPE_MENU;  m[opt].text = "More options..."; opt++;
3154
3155         Assert(opt <= 20);
3156
3157 menu:
3158    ExtGameStatus=GAMESTAT_NETGAME_OPTIONS; 
3159         i = newmenu_do1( NULL, NULL, opt, m, network_game_param_poll, 1 );
3160                                                                         //TXT_NETGAME_SETUP
3161    if (i==opt_moreopts)
3162     {
3163      if ( m[opt_mode+3].value )
3164       Game_mode=GM_MULTI_COOP;
3165      network_more_game_options();
3166      Game_mode=0;
3167      goto menu;
3168     }
3169   Netgame.RefusePlayers=m[opt_refuse].value;
3170
3171
3172         if ( i > -1 )   {
3173                 int j;
3174                       
3175    MaxNumNetPlayers = m[opt_maxnet].value+2;
3176    Netgame.max_numplayers=MaxNumNetPlayers;
3177                                 
3178                 for (j = 0; j < num_active_games; j++)
3179                         if (!stricmp(Active_games[j].game_name, name))
3180                         {
3181                                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_DUPLICATE_NAME);
3182                                 goto menu;
3183                         }
3184
3185                 strcpy( game_name, name );
3186                 
3187
3188                 *level = atoi(slevel);
3189
3190                 if ((*level < 1) || (*level > Last_level))
3191                 {
3192                         nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_LEVEL_OUT_RANGE );
3193                         sprintf(slevel, "1");
3194                         goto menu;
3195                 }
3196                 if ( m[opt_mode].value )
3197                         *mode = NETGAME_ANARCHY;
3198
3199 #ifdef SHAREWARE
3200                 else 
3201                 {
3202                         nm_messagebox(TXT_SORRY, 1, TXT_OK, TXT_REGISTERED_ONLY );
3203                         m[opt_mode+1].value = 0;
3204                         m[opt_mode+2].value = 0;
3205                         m[opt_mode+3].value = 0;
3206                    if (HoardEquipped())
3207                                 m[opt_mode+4].value = 0;
3208
3209                         m[opt_mode].value = 1;
3210                         goto menu;
3211                 }
3212 #else
3213                 else if (m[opt_mode+1].value) {
3214                         *mode = NETGAME_TEAM_ANARCHY;
3215                 }
3216                 else if (m[opt_capture].value)
3217                         *mode = NETGAME_CAPTURE_FLAG;
3218                 else if (HoardEquipped() && m[opt_capture+1].value)
3219                                 *mode = NETGAME_HOARD;
3220                 else if (HoardEquipped() && m[opt_capture+2].value)
3221                                 *mode = NETGAME_TEAM_HOARD;
3222                 else if (anarchy_only) {
3223                         nm_messagebox(NULL, 1, TXT_OK, TXT_ANARCHY_ONLY_MISSION);
3224                         m[opt_mode+2].value = 0;
3225                         m[opt_mode+3].value = 0;
3226                         m[opt_mode].value = 1;
3227                         goto menu;
3228                 }               
3229                 else if ( m[opt_mode+2].value ) 
3230                         *mode = NETGAME_ROBOT_ANARCHY;
3231                 else if ( m[opt_mode+3].value ) 
3232                         *mode = NETGAME_COOPERATIVE;
3233                 else Int3(); // Invalid mode -- see Rob
3234 #endif
3235                 if (m[opt_closed].value)
3236                         Netgame.game_flags |= NETGAME_FLAG_CLOSED;
3237       
3238         }
3239
3240         return i;
3241 }
3242
3243 void
3244 network_set_game_mode(int gamemode)
3245 {
3246         Show_kill_list = 1;
3247
3248         if ( gamemode == NETGAME_ANARCHY )
3249                 Game_mode = GM_NETWORK;
3250         else if ( gamemode == NETGAME_ROBOT_ANARCHY )
3251                 Game_mode = GM_NETWORK | GM_MULTI_ROBOTS;
3252         else if ( gamemode == NETGAME_COOPERATIVE ) 
3253                 Game_mode = GM_NETWORK | GM_MULTI_COOP | GM_MULTI_ROBOTS;
3254         else if (gamemode == NETGAME_CAPTURE_FLAG)
3255                 {
3256                  Game_mode = GM_NETWORK | GM_TEAM | GM_CAPTURE;
3257                  Show_kill_list=3;
3258                 }
3259
3260         else if (HoardEquipped() && gamemode == NETGAME_HOARD)
3261                  Game_mode = GM_NETWORK | GM_HOARD;
3262         else if (HoardEquipped() && gamemode == NETGAME_TEAM_HOARD)
3263                  {
3264                   Game_mode = GM_NETWORK | GM_HOARD | GM_TEAM;
3265                   Show_kill_list=3;
3266                  }
3267         else if ( gamemode == NETGAME_TEAM_ANARCHY )
3268         {
3269                 Game_mode = GM_NETWORK | GM_TEAM;
3270                 Show_kill_list = 3;
3271         }
3272         else
3273                 Int3();
3274
3275 #if 0
3276 #ifdef MACINTOSH                        // minimize players on localtalk games
3277         if ( (Network_game_type == APPLETALK_GAME) && (Network_appletalk_type == LOCALTALK_TYPE) ) {
3278                 if (Game_mode & GM_MULTI_ROBOTS)
3279                         MaxNumNetPlayers = 2;
3280                 else
3281                         MaxNumNetPlayers = 3;
3282         } else {
3283                 if (Game_mode & GM_MULTI_COOP)
3284                         MaxNumNetPlayers = 4;
3285                 else
3286                         MaxNumNetPlayers = 8;
3287         }
3288 #endif
3289 #endif
3290
3291 }
3292
3293 int
3294 network_find_game(void)
3295 {
3296         // Find out whether or not there is space left on this socket
3297
3298         fix t1;
3299
3300         Network_status = NETSTAT_BROWSING;
3301
3302         num_active_games = 0;
3303
3304         show_boxed_message(TXT_WAIT);
3305
3306         network_send_game_list_request();
3307         t1 = timer_get_approx_seconds() + F1_0*3;
3308
3309         while (timer_get_approx_seconds() < t1) // Wait 3 seconds for replies
3310                 network_listen();
3311
3312         clear_boxed_message();
3313
3314 //      mprintf((0, "%s %d %s\n", TXT_FOUND, num_active_games, TXT_ACTIVE_GAMES));
3315
3316         if (num_active_games < MAX_ACTIVE_NETGAMES)
3317                 return 0;
3318         return 1;
3319 }
3320         
3321 void network_read_sync_packet( netgame_info * sp, int rsinit)
3322 {       
3323 #ifdef MACINTOSH
3324         netgame_info tmp_info;
3325 #endif
3326
3327         int i, j;
3328         char temp_callsign[CALLSIGN_LEN+1];
3329
3330 #ifdef MACINTOSH
3331         if ( (Network_game_type == IPX_GAME) && (sp != &Netgame) ) {                            // for macintosh -- get the values unpacked to our structure format
3332                 receive_netgame_packet((ubyte *)sp, &tmp_info, 0);
3333                 sp = &tmp_info;
3334         }
3335 #endif
3336
3337    if (rsinit)
3338      TempPlayersInfo=&NetPlayers;
3339         
3340         // This function is now called by all people entering the netgame.
3341
3342         // mprintf( (0, "%s %d\n", TXT_STARTING_NETGAME, sp->levelnum ));
3343
3344         if (sp != &Netgame)
3345          {
3346           memcpy( &Netgame, sp, sizeof(netgame_info) );
3347           memcpy (&NetPlayers,TempPlayersInfo,sizeof (AllNetPlayers_info));
3348          }
3349
3350         N_players = sp->numplayers;
3351         Difficulty_level = sp->difficulty;
3352         Network_status = sp->game_status;
3353
3354    //Assert(Function_mode != FMODE_GAME);
3355
3356         // New code, 11/27
3357
3358         mprintf((1, "Netgame.checksum = %d, calculated checksum = %d.\n", Netgame.segments_checksum, my_segments_checksum));
3359
3360         if (Netgame.segments_checksum != my_segments_checksum)
3361         {
3362                 Network_status = NETSTAT_MENU;
3363                 nm_messagebox(TXT_ERROR, 1, TXT_OK, TXT_NETLEVEL_NMATCH);
3364 #ifdef RELEASE
3365                 return;
3366 #endif
3367         }
3368
3369         // Discover my player number
3370
3371         memcpy(temp_callsign, Players[Player_num].callsign, CALLSIGN_LEN+1);
3372         
3373         Player_num = -1;
3374
3375         for (i=0; i<MAX_NUM_NET_PLAYERS; i++ )  {
3376                 Players[i].net_kills_total = 0;
3377 //              Players[i].net_killed_total = 0;
3378         }
3379
3380         for (i=0; i<N_players; i++ )    {
3381                 if (Network_game_type == IPX_GAME) {
3382                         if ( (!memcmp( TempPlayersInfo->players[i].network.ipx.node, My_Seq.player.network.ipx.node, 6 )) && (!stricmp( TempPlayersInfo->players[i].callsign, temp_callsign)) ) {
3383                                 if (Player_num!=-1) {
3384                                         Int3(); // Hey, we've found ourselves twice
3385                                         mprintf ((0,"Hey, we've found ourselves twice!\n"));
3386                                         Network_status = NETSTAT_MENU;
3387                                         return; 
3388                                 }
3389                                 change_playernum_to(i);
3390                         }
3391                 } else {
3392                         if ( (TempPlayersInfo->players[i].network.appletalk.node == My_Seq.player.network.appletalk.node) && (!stricmp( TempPlayersInfo->players[i].callsign, temp_callsign)) ) {
3393                                 if (Player_num!=-1) {
3394                                         Int3(); // Hey, we've found ourselves twice
3395                                         Network_status = NETSTAT_MENU;
3396                                         return; 
3397                                 }
3398                                 change_playernum_to(i);
3399                         }
3400                 }
3401                 memcpy( Players[i].callsign, TempPlayersInfo->players[i].callsign, CALLSIGN_LEN+1 );
3402
3403                 if (Network_game_type == IPX_GAME) {
3404                         if ( (*(uint *)TempPlayersInfo->players[i].network.ipx.server) != 0 )
3405                                 ipx_get_local_target( TempPlayersInfo->players[i].network.ipx.server, TempPlayersInfo->players[i].network.ipx.node, Players[i].net_address );
3406                         else
3407                                 memcpy( Players[i].net_address, TempPlayersInfo->players[i].network.ipx.node, 6 );
3408                 }
3409                                 
3410                 Players[i].n_packets_got=0;                             // How many packets we got from them
3411                 Players[i].n_packets_sent=0;                            // How many packets we sent to them
3412                 Players[i].connected = TempPlayersInfo->players[i].connected;
3413                 Players[i].net_kills_total = sp->player_kills[i];
3414                 Players[i].net_killed_total = sp->killed[i];
3415                 if ((Network_rejoined) || (i != Player_num))
3416                         Players[i].score = sp->player_score[i];
3417                 for (j = 0; j < MAX_NUM_NET_PLAYERS; j++)
3418                 {
3419                         kill_matrix[i][j] = sp->kills[i][j];
3420                 }
3421         }
3422
3423         if ( Player_num < 0 )   {
3424                 mprintf ((0,"Bad Player_num, resetting to NETSTAT_MENU!\n"));
3425                 Network_status = NETSTAT_MENU;
3426                 return;
3427         }
3428
3429         if (Network_rejoined) 
3430                 for (i=0; i<N_players;i++)
3431                         Players[i].net_killed_total = sp->killed[i];
3432
3433         if (Network_rejoined) {
3434                 network_process_monitor_vector(sp->monitor_vector);
3435                 Players[Player_num].time_level = sp->level_time;
3436         }
3437
3438         team_kills[0] = sp->team_kills[0];
3439         team_kills[1] = sp->team_kills[1];
3440         
3441         Players[Player_num].connected = 1;
3442    NetPlayers.players[Player_num].connected = 1;
3443    NetPlayers.players[Player_num].rank=GetMyNetRanking();
3444
3445         if (!Network_rejoined)
3446                 for (i=0; i<NumNetPlayerPositions; i++) {
3447                         Objects[Players[i].objnum].pos = Player_init[Netgame.locations[i]].pos;
3448                         Objects[Players[i].objnum].orient = Player_init[Netgame.locations[i]].orient;
3449                         obj_relink(Players[i].objnum,Player_init[Netgame.locations[i]].segnum);
3450                 }
3451
3452         Objects[Players[Player_num].objnum].type = OBJ_PLAYER;
3453
3454    mprintf ((0,"Changing to NETSTAT_PLAYING!\n"));
3455         Network_status = NETSTAT_PLAYING;
3456         Function_mode = FMODE_GAME;
3457         multi_sort_kill_list();
3458
3459 }
3460
3461 void
3462 network_send_sync(void)
3463 {
3464         int i, j, np;
3465
3466         // Randomize their starting locations...
3467
3468         d_srand( timer_get_fixed_seconds() );
3469         for (i=0; i<NumNetPlayerPositions; i++ )        
3470         {
3471                 if (Players[i].connected)
3472                         Players[i].connected = 1; // Get rid of endlevel connect statuses
3473                 if (Game_mode & GM_MULTI_COOP)
3474                         Netgame.locations[i] = i;
3475                 else {
3476                         do 
3477                         {
3478                                 np = d_rand() % NumNetPlayerPositions;
3479                                 for (j=0; j<i; j++ )    
3480                                 {
3481                                         if (Netgame.locations[j]==np)   
3482                                         {
3483                                                 np =-1;
3484                                                 break;
3485                                         }
3486                                 }
3487                         } while (np<0);
3488                         // np is a location that is not used anywhere else..
3489                         Netgame.locations[i]=np;
3490 //                      mprintf((0, "Player %d starting in location %d\n" ,i ,np ));
3491                 }
3492         }
3493
3494         // Push current data into the sync packet
3495
3496         network_update_netgame();
3497         Netgame.game_status = NETSTAT_PLAYING;
3498         Netgame.type = PID_SYNC;
3499         Netgame.segments_checksum = my_segments_checksum;
3500
3501         for (i=0; i<N_players; i++ )    {
3502                 if ((!Players[i].connected) || (i == Player_num))
3503                         continue;
3504
3505                 if (Network_game_type == IPX_GAME) {
3506                 // Send several times, extras will be ignored
3507                         #ifndef MACINTOSH
3508                         ipx_send_internetwork_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node);
3509                         ipx_send_internetwork_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node);
3510                         #else
3511                         send_netgame_packet(NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node, NULL, 0);
3512                         send_netplayers_packet(NetPlayers.players[i].network.ipx.server, NetPlayers.players[i].network.ipx.node);
3513                         #endif
3514                 #ifdef MACINTOSH
3515                 } else {
3516                                 appletalk_send_packet_data( (ubyte *)&Netgame, sizeof(netgame_info), NetPlayers.players[i].network.appletalk.node,
3517                                         NetPlayers.players[i].network.appletalk.net,
3518                                         NetPlayers.players[i].network.appletalk.socket);
3519                                 appletalk_send_packet_data( (ubyte *)&NetPlayers, sizeof(AllNetPlayers_info), NetPlayers.players[i].network.appletalk.node,
3520                                         NetPlayers.players[i].network.appletalk.net,
3521                                         NetPlayers.players[i].network.appletalk.socket);
3522                 #endif
3523                 }
3524
3525         }       
3526         network_read_sync_packet(&Netgame,1); // Read it myself, as if I had sent it
3527 }
3528
3529 int
3530 network_select_teams(void)
3531 {
3532 #ifndef SHAREWARE
3533         newmenu_item m[MAX_PLAYERS+4];
3534         int choice, opt, opt_team_b;
3535         ubyte team_vector = 0;
3536         char team_names[2][CALLSIGN_LEN+1];
3537         int i;
3538         int pnums[MAX_PLAYERS+2];
3539
3540         // One-time initialization
3541
3542         for (i = N_players/2; i < N_players; i++) // Put first half of players on team A
3543         {
3544                 team_vector |= (1 << i);
3545         }
3546
3547         sprintf(team_names[0], "%s", TXT_BLUE);
3548         sprintf(team_names[1], "%s", TXT_RED);
3549
3550         // Here comes da menu
3551 menu:
3552         m[0].type = NM_TYPE_INPUT; m[0].text = team_names[0]; m[0].text_len = CALLSIGN_LEN; 
3553
3554         opt = 1;
3555         for (i = 0; i < N_players; i++)
3556         {
3557                 if (!(team_vector & (1 << i)))
3558                 {
3559                         m[opt].type = NM_TYPE_MENU; m[opt].text = NetPlayers.players[i].callsign; pnums[opt] = i; opt++;
3560                 }
3561         }
3562         opt_team_b = opt;
3563         m[opt].type = NM_TYPE_INPUT; m[opt].text = team_names[1]; m[opt].text_len = CALLSIGN_LEN; opt++;
3564         for (i = 0; i < N_players; i++)
3565         {
3566                 if (team_vector & (1 << i))
3567                 {
3568                         m[opt].type = NM_TYPE_MENU; m[opt].text = NetPlayers.players[i].callsign; pnums[opt] = i; opt++;
3569                 }
3570         }
3571         m[opt].type = NM_TYPE_TEXT; m[opt].text = ""; opt++;
3572         m[opt].type = NM_TYPE_MENU; m[opt].text = TXT_ACCEPT; opt++;
3573
3574         Assert(opt <= MAX_PLAYERS+4);
3575         
3576         choice = newmenu_do(NULL, TXT_TEAM_SELECTION, opt, m, NULL);
3577
3578         if (choice == opt-1)
3579         {
3580                 if ((opt-2-opt_team_b < 2) || (opt_team_b == 1)) 
3581                 {
3582                         nm_messagebox(NULL, 1, TXT_OK, TXT_TEAM_MUST_ONE);
3583                         #ifdef RELEASE
3584                                 goto menu;
3585                         #endif
3586                 }
3587                 
3588                 Netgame.team_vector = team_vector;
3589                 strcpy(Netgame.team_name[0], team_names[0]);
3590                 strcpy(Netgame.team_name[1], team_names[1]);
3591                 return 1;
3592         }
3593
3594         else if ((choice > 0) && (choice < opt_team_b)) {
3595                 team_vector |= (1 << pnums[choice]);
3596         }
3597         else if ((choice > opt_team_b) && (choice < opt-2)) {
3598                 team_vector &= ~(1 << pnums[choice]);
3599         }
3600         else if (choice == -1)
3601                 return 0;
3602         goto menu;
3603 #else
3604         return 0;
3605 #endif
3606 }
3607
3608 int
3609 network_select_players(void)
3610 {
3611         int i, j;
3612    newmenu_item m[MAX_PLAYERS+4];
3613    char text[MAX_PLAYERS+4][45];
3614         char title[50];
3615         int save_nplayers;              //how may people would like to join
3616
3617         network_add_player( &My_Seq );
3618                 
3619         for (i=0; i< MAX_PLAYERS+4; i++ ) {
3620                 sprintf( text[i], "%d.  %-20s", i+1, "" );
3621                 m[i].type = NM_TYPE_CHECK; m[i].text = text[i]; m[i].value = 0;
3622         }
3623
3624         m[0].value = 1;                         // Assume server will play...
3625
3626    if (FindArg("-norankings"))
3627                 sprintf( text[0], "%d. %-20s", 1, Players[Player_num].callsign );
3628         else
3629                 sprintf( text[0], "%d. %s%-20s", 1, RankStrings[NetPlayers.players[Player_num].rank],Players[Player_num].callsign );
3630
3631         sprintf( title, "%s %d %s", TXT_TEAM_SELECT, MaxNumNetPlayers, TXT_TEAM_PRESS_ENTER );
3632
3633 GetPlayersAgain:
3634    ExtGameStatus=GAMESTAT_NETGAME_PLAYER_SELECT;
3635         j=newmenu_do1( NULL, title, MAX_PLAYERS+4, m, network_start_poll, 1 );
3636
3637         save_nplayers = N_players;
3638
3639         if (j<0) 
3640         {
3641                 // Aborted!                                      
3642                 // Dump all players and go back to menu mode
3643
3644 abort:
3645                 for (i=1; i<save_nplayers; i++) {
3646                         if (Network_game_type == IPX_GAME)
3647                                 network_dump_player(NetPlayers.players[i].network.ipx.server,NetPlayers.players[i].network.ipx.node, DUMP_ABORTED);
3648                         #ifdef MACINTOSH
3649                         else
3650                                 network_dump_appletalk_player(NetPlayers.players[i].network.appletalk.node,NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket, DUMP_ABORTED);
3651                         #endif
3652                 }
3653                         
3654
3655                 Netgame.numplayers = 0;
3656                 network_send_game_info(0); // Tell everyone we're bailing
3657
3658                 Network_status = NETSTAT_MENU;
3659                 return(0);
3660         }
3661        
3662         // Count number of players chosen
3663
3664         N_players = 0;
3665         for (i=0; i<save_nplayers; i++ )
3666         {
3667                 if (m[i].value) 
3668                         N_players++;
3669         }
3670         
3671         if ( N_players > Netgame.max_numplayers) {
3672                 #ifndef MACINTOSH
3673                 nm_messagebox( TXT_ERROR, 1, TXT_OK, "%s %d %s", TXT_SORRY_ONLY, MaxNumNetPlayers, TXT_NETPLAYERS_IN );
3674                 #else
3675                 nm_messagebox( TXT_ERROR, 1, TXT_OK, "%s %d netplayers for this game.", TXT_SORRY_ONLY, MaxNumNetPlayers );
3676                 #endif
3677                 N_players = save_nplayers;
3678                 goto GetPlayersAgain;
3679         }
3680
3681 #ifdef RELEASE
3682         if ( N_players < 2 )    {
3683                 nm_messagebox( TXT_ERROR, 1, TXT_OK, TXT_TEAM_ATLEAST_TWO );
3684                 N_players = save_nplayers;
3685                 goto GetPlayersAgain;
3686         }
3687 #endif
3688
3689 #ifdef RELEASE
3690         if ( (Netgame.gamemode == NETGAME_TEAM_ANARCHY ||
3691                    Netgame.gamemode == NETGAME_CAPTURE_FLAG || Netgame.gamemode == NETGAME_TEAM_HOARD) && (N_players < 2) ) {
3692                 nm_messagebox(TXT_ERROR, 1, TXT_OK, "You must select at least two\nplayers to start a team game" );
3693                 N_players = save_nplayers;
3694                 goto GetPlayersAgain;
3695         }
3696 #endif
3697
3698         // Remove players that aren't marked.
3699         N_players = 0;
3700         for (i=0; i<save_nplayers; i++ )        {
3701                 if (m[i].value) 
3702                 {
3703                         if (i > N_players)
3704                         {
3705                                 if (Network_game_type == IPX_GAME) {
3706                                         memcpy(NetPlayers.players[N_players].network.ipx.node, NetPlayers.players[i].network.ipx.node, 6);
3707                                         memcpy(NetPlayers.players[N_players].network.ipx.server, NetPlayers.players[i].network.ipx.server, 4);
3708                                 } else {
3709                                         NetPlayers.players[N_players].network.appletalk.node = NetPlayers.players[i].network.appletalk.node;
3710                                         NetPlayers.players[N_players].network.appletalk.net = NetPlayers.players[i].network.appletalk.net;
3711                                         NetPlayers.players[N_players].network.appletalk.socket = NetPlayers.players[i].network.appletalk.socket;
3712                                 }
3713                                 memcpy(NetPlayers.players[N_players].callsign, NetPlayers.players[i].callsign, CALLSIGN_LEN+1);
3714                                 NetPlayers.players[N_players].version_major=NetPlayers.players[i].version_major;
3715                                 NetPlayers.players[N_players].version_minor=NetPlayers.players[i].version_minor;
3716                                 NetPlayers.players[N_players].rank=NetPlayers.players[i].rank;
3717                                 ClipRank (&NetPlayers.players[N_players].rank);
3718                                 network_check_for_old_version(i);
3719                         }
3720                         Players[N_players].connected = 1;
3721                         N_players++;
3722                 }
3723                 else
3724                 {
3725                         if (Network_game_type == IPX_GAME)
3726                                 network_dump_player(NetPlayers.players[i].network.ipx.server,NetPlayers.players[i].network.ipx.node, DUMP_DORK);
3727                         #ifdef MACINTOSH
3728                         else
3729                                 network_dump_appletalk_player(NetPlayers.players[i].network.appletalk.node,NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket, DUMP_DORK);
3730                         #endif
3731                 }
3732         }
3733
3734         for (i = N_players; i < MAX_NUM_NET_PLAYERS; i++) {
3735                 if (Network_game_type == IPX_GAME) {
3736                 memset(NetPlayers.players[i].network.ipx.node, 0, 6);
3737                 memset(NetPlayers.players[i].network.ipx.server, 0, 4);
3738             } else {
3739                 NetPlayers.players[i].network.appletalk.node = 0;
3740                 NetPlayers.players[i].network.appletalk.net = 0;
3741                 NetPlayers.players[i].network.appletalk.socket = 0;
3742             }
3743         memset(NetPlayers.players[i].callsign, 0, CALLSIGN_LEN+1);
3744                 NetPlayers.players[i].version_major=0;
3745                 NetPlayers.players[i].version_minor=0;
3746                 NetPlayers.players[i].rank=0;
3747         }
3748
3749    mprintf ((0,"Select teams: Game mode is %d\n",Netgame.gamemode));
3750
3751         if (Netgame.gamemode == NETGAME_TEAM_ANARCHY ||
3752             Netgame.gamemode == NETGAME_CAPTURE_FLAG ||
3753                  Netgame.gamemode == NETGAME_TEAM_HOARD)
3754                  if (!network_select_teams())
3755                         goto abort;
3756
3757         return(1); 
3758 }
3759
3760 void 
3761 network_start_game()
3762 {
3763         int i;
3764         char game_name[NETGAME_NAME_LEN+1];
3765         int chosen_game_mode, game_flags, level;
3766
3767         if (Network_game_type == IPX_GAME) {
3768
3769                 Assert( FRAME_INFO_SIZE < IPX_MAX_DATA_SIZE );
3770                 mprintf((0, "Using frame_info len %d, max %d.\n", FRAME_INFO_SIZE, IPX_MAX_DATA_SIZE));
3771                 
3772                 if ( !Network_active )
3773                 {
3774                         nm_messagebox(NULL, 1, TXT_OK, TXT_IPX_NOT_FOUND );
3775                         return;
3776                 }
3777         #ifdef MACINTOSH
3778         } else {
3779                 int err;
3780                 char buf[256];
3781
3782                 Assert( FRAME_INFO_SIZE < APPLETALK_MAX_DATA_SIZE );            
3783                 mprintf((0, "Using frame_info len %d, max %d.\n", sizeof(frame_info), APPLETALK_MAX_DATA_SIZE));
3784                 if (Appletalk_active <= 0) {
3785                         switch (Appletalk_active) {
3786                         case APPLETALK_NOT_OPEN:
3787                                 sprintf(buf, "Appletalk is not currently active.\nPlease enable AppleTalk from the\nChooser and restart Descent.");
3788                                 break;
3789                         case APPLETALK_BAD_LISTENER:
3790                                 sprintf(buf, "The Resource Fork of Descent appears damaged.\nPlease re-install Descent or contact\nMacPlay technical support.");
3791                                 break;
3792                         case APPLETALK_NO_LOCAL_ADDR:
3793                                 sprintf(buf, "Wow! Strange!\n\nNo Local Address.");
3794                                 break;
3795                         case APPLETALK_NO_SOCKET:
3796                                 sprintf(buf, "All AppleTalk sockets are in use.\nTry shutting down other network\napplications and restarting Descent.\n");
3797                                 break;
3798                         }
3799                         nm_messagebox(NULL, 1, TXT_OK, buf);
3800                         return;
3801                 }
3802                 strcpy(Network_zone_name, DEFAULT_ZONE_NAME);
3803         #endif
3804         }
3805
3806         network_init();
3807         change_playernum_to(0);
3808
3809         if (network_find_game())
3810         {
3811                 nm_messagebox(NULL, 1, TXT_OK, TXT_NET_FULL);
3812                 return;
3813         }
3814
3815         game_flags = 0;
3816         i = network_get_game_params( game_name, &chosen_game_mode, &game_flags, &level );
3817
3818         if (i<0) return;
3819
3820         N_players = 0;
3821
3822 // LoadLevel(level); Old, no longer used.
3823
3824         Netgame.difficulty = Difficulty_level;
3825         Netgame.gamemode = chosen_game_mode;
3826         Netgame.game_status = NETSTAT_STARTING;
3827         Netgame.numplayers = 0;
3828         Netgame.max_numplayers = MaxNumNetPlayers;
3829         Netgame.levelnum = level;
3830         Netgame.protocol_version = MULTI_PROTO_VERSION;
3831
3832         strcpy(Netgame.game_name, game_name);
3833         
3834         Network_status = NETSTAT_STARTING;
3835
3836         #ifdef MACINTOSH
3837         if (Network_game_type == APPLETALK_GAME) {
3838                 OSErr err;
3839                 fix t1;
3840                 int count = 0;
3841                 
3842                 show_boxed_message("Registering Netgame");
3843                 do {
3844                         err = appletalk_register_netgame( game_name, TickCount() );
3845                         t1 = timer_get_fixed_seconds() + F1_0;
3846                         while (timer_get_fixed_seconds() < t1) ;
3847                         count++;
3848                 } while ( (err == nbpDuplicate) && (count != MAX_REGISTER_TRIES) );
3849                 clear_boxed_message();
3850                 if ( (err == tooManyReqs) || (count == MAX_REGISTER_TRIES) ) {
3851                         nm_messagebox(NULL, 1, TXT_OK, "AppleTalk Network is too busy.\nPlease try again shortly.");
3852                         Game_mode = GM_GAME_OVER;
3853                         return;
3854                 }
3855         }
3856         #endif
3857
3858         network_set_game_mode(Netgame.gamemode);
3859
3860    d_srand( timer_get_fixed_seconds() );
3861    Netgame.Security=d_rand();  // For syncing Netgames with player packets
3862
3863         if(network_select_players())
3864         {
3865                 StartNewLevel(Netgame.levelnum, 0);
3866         }
3867         else
3868                 Game_mode = GM_GAME_OVER;
3869         
3870 }
3871
3872 void restart_net_searching(newmenu_item * m)
3873 {
3874         int i;
3875         N_players = 0;
3876         num_active_games = 0;
3877
3878         memset(Active_games, 0, sizeof(netgame_info)*MAX_ACTIVE_NETGAMES);
3879
3880         for (i = 0; i < MAX_ACTIVE_NETGAMES; i++)
3881         {
3882                 sprintf(m[i+2].text, "%d.      &nbs