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                           &n