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