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