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