]> icculus.org git repositories - btb/d2x.git/blob - main/multi.c
comments/whitespace
[btb/d2x.git] / main / multi.c
1 /* $Id: multi.c,v 1.13 2003-10-04 02:58:23 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Multiplayer code shared by serial and network play.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <ctype.h>
30
31 #include "u_mem.h"
32 #include "strutil.h"
33 #include "game.h"
34 #include "modem.h"
35 #include "network.h"
36 #include "multi.h"
37 #include "object.h"
38 #include "laser.h"
39 #include "fuelcen.h"
40 #include "scores.h"
41 #include "gauges.h"
42 #include "collide.h"
43 #include "error.h"
44 #include "fireball.h"
45 #include "newmenu.h"
46 #include "mono.h"
47 #include "wall.h"
48 #include "cntrlcen.h"
49 #include "powerup.h"
50 #include "polyobj.h"
51 #include "bm.h"
52 #include "endlevel.h"
53 #include "key.h"
54 #include "playsave.h"
55 #include "timer.h"
56 #include "digi.h"
57 #include "sounds.h"
58 #include "kconfig.h"
59 #include "newdemo.h"
60 #include "text.h"
61 #include "kmatrix.h"
62 #include "multibot.h"
63 #include "gameseq.h"
64 #include "physics.h"
65 #include "config.h"
66 #include "state.h"
67 #include "ai.h"
68 #include "switch.h"
69 #include "textures.h"
70 #include "byteswap.h"
71 #include "sounds.h"
72 #include "args.h"
73 #include "cfile.h"
74 #include "effects.h"
75
76 void multi_reset_player_object(object *objp);
77 void multi_reset_object_texture(object *objp);
78 void multi_add_lifetime_killed();
79 void multi_add_lifetime_kills();
80 void multi_send_play_by_play(int num,int spnum,int dpnum);
81 void multi_send_heartbeat();
82 void multi_send_modem_ping();
83 void multi_cap_objects();
84 void multi_adjust_remote_cap(int pnum);
85 void multi_save_game(ubyte slot, uint id, char *desc);
86 void multi_restore_game(ubyte slot, uint id);
87 void multi_set_robot_ai(void);
88 void multi_send_powerup_update();
89 void bash_to_shield(int i,char *s);
90 void init_hoard_data();
91 void multi_apply_goal_textures();
92 int  find_goal_texture(ubyte t);
93 void multi_bad_restore();
94 void multi_do_capture_bonus(char *buf);
95 void multi_do_orb_bonus(char *buf);
96 void multi_send_drop_flag(int objnum,int seed);
97 void multi_send_ranking();
98 void multi_do_play_by_play(char *buf);
99
100 //
101 // Local macros and prototypes
102 //
103
104 // LOCALIZE ME!!
105
106 #define vm_angvec_zero(v) (v)->p=(v)->b=(v)->h=0
107
108 void drop_player_eggs(object *player); // from collide.c
109 void GameLoop(int, int); // From game.c
110
111 //
112 // Global variables
113 //
114
115 extern vms_vector MarkerPoint[];
116 extern char MarkerMessage[16][40];
117 extern char MarkerOwner[16][40];
118 extern int MarkerObject[];
119
120 int control_invul_time = 0;
121 int who_killed_controlcen = -1;  // -1 = noone
122
123 //do we draw the kill list on the HUD?
124 int Show_kill_list = 1;
125 int Show_reticle_name = 1;
126 fix Show_kill_list_timer = 0;
127
128 char Multi_is_guided=0;
129 char PKilledFlags[MAX_NUM_NET_PLAYERS];
130
131 int multi_sending_message = 0;
132 int multi_defining_message = 0;
133 int multi_message_index = 0;
134
135 char multibuf[MAX_MULTI_MESSAGE_LEN+4];            // This is where multiplayer message are built
136
137 short remote_to_local[MAX_NUM_NET_PLAYERS][MAX_OBJECTS];  // Remote object number for each local object
138 short local_to_remote[MAX_OBJECTS];
139 sbyte object_owner[MAX_OBJECTS];   // Who created each object in my universe, -1 = loaded at start
140
141 int   Net_create_objnums[MAX_NET_CREATE_OBJECTS]; // For tracking object creation that will be sent to remote
142 int   Net_create_loc = 0;       // pointer into previous array
143 int   Network_laser_fired = 0;  // How many times we shot
144 int   Network_laser_gun;        // Which gun number we shot
145 int   Network_laser_flags;      // Special flags for the shot
146 int   Network_laser_level;      // What level
147 short Network_laser_track;      // Who is it tracking?
148 char  Network_message[MAX_MESSAGE_LEN];
149 char  Network_message_macro[4][MAX_MESSAGE_LEN];
150 int   Network_message_reciever=-1;
151 int   sorted_kills[MAX_NUM_NET_PLAYERS];
152 short kill_matrix[MAX_NUM_NET_PLAYERS][MAX_NUM_NET_PLAYERS];
153 int   multi_goto_secret = 0;
154 short team_kills[2];
155 int   multi_in_menu = 0;
156 int   multi_leave_menu = 0;
157 int   multi_quit_game = 0;
158
159 netgame_info Netgame;
160 AllNetPlayers_info NetPlayers;
161
162 bitmap_index multi_player_textures[MAX_NUM_NET_PLAYERS][N_PLAYER_SHIP_TEXTURES];
163
164 typedef struct netplayer_stats {
165         ubyte  message_type;
166         ubyte  Player_num;              // Who am i?
167         uint   flags;                   // Powerup flags, see below...
168         fix    energy;                  // Amount of energy remaining.
169         fix    shields;                 // shields remaining (protection)
170         ubyte  lives;                   // Lives remaining, 0 = game over.
171         ubyte  laser_level;             // Current level of the laser.
172         ubyte  primary_weapon_flags;    // bit set indicates the player has this weapon.
173         ubyte  secondary_weapon_flags;  // bit set indicates the player has this weapon.
174         ushort primary_ammo[MAX_PRIMARY_WEAPONS];     // How much ammo of each type.
175         ushort secondary_ammo[MAX_SECONDARY_WEAPONS]; // How much ammo of each type.
176         int    last_score;              // Score at beginning of current level.
177         int    score;                   // Current score.
178         fix    cloak_time;              // Time cloaked
179         fix    invulnerable_time;       // Time invulnerable
180         fix    homing_object_dist;      // Distance of nearest homing object.
181         short  KillGoalCount;
182         short  net_killed_total;        // Number of times killed total
183         short  net_kills_total;         // Number of net kills total
184         short  num_kills_level;         // Number of kills this level
185         short  num_kills_total;         // Number of kills total
186         short  num_robots_level;        // Number of initial robots this level
187         short  num_robots_total;        // Number of robots total
188         ushort hostages_rescued_total;  // Total number of hostages rescued.
189         ushort hostages_total;          // Total number of hostages.
190         ubyte  hostages_on_board;       // Number of hostages on ship.
191         ubyte  unused[16];
192 } netplayer_stats;
193
194 int message_length[MULTI_MAX_TYPE+1] = {
195         24, // POSITION
196         3,  // REAPPEAR
197         8,  // FIRE
198         5,  // KILL
199         4,  // REMOVE_OBJECT
200         97+9, // PLAYER_EXPLODE
201         37, // MESSAGE (MAX_MESSAGE_LENGTH = 40)
202         2,  // QUIT
203         4,  // PLAY_SOUND
204         41, // BEGIN_SYNC
205         4,  // CONTROLCEN
206         5,  // CLAIM ROBOT
207         4,  // END_SYNC
208         2,  // CLOAK
209         3,  // ENDLEVEL_START
210         5,  // DOOR_OPEN
211         2,  // CREATE_EXPLOSION
212         16, // CONTROLCEN_FIRE
213         97+9, // PLAYER_DROP
214         19, // CREATE_POWERUP
215         9,  // MISSILE_TRACK
216         2,  // DE-CLOAK
217         2,  // MENU_CHOICE
218         28, // ROBOT_POSITION  (shortpos_length (23) + 5 = 28)
219         9,  // ROBOT_EXPLODE
220         5,  // ROBOT_RELEASE
221         18, // ROBOT_FIRE
222         6,  // SCORE
223         6,  // CREATE_ROBOT
224         3,  // TRIGGER
225         10, // BOSS_ACTIONS
226         27, // ROBOT_POWERUPS
227         7,  // HOSTAGE_DOOR
228         2+24, // SAVE_GAME      (ubyte slot, uint id, char name[20])
229         2+4,  // RESTORE_GAME   (ubyte slot, uint id)
230         1+1,  // MULTI_REQ_PLAYER
231         sizeof(netplayer_stats), // MULTI_SEND_PLAYER
232         55, // MULTI_MARKER
233         12, // MULTI_DROP_WEAPON
234 #ifndef MACINTOSH
235         3+sizeof(shortpos), // MULTI_GUIDED, IF SHORTPOS CHANGES, CHANGE MAC VALUE BELOW
236 #else
237         26, // MULTI_GUIDED IF SIZE OF SHORTPOS CHANGES, CHANGE THIS VALUE AS WELL!!!!!!
238 #endif
239         11, // MULTI_STOLEN_ITEMS
240         6,  // MULTI_WALL_STATUS
241         5,  // MULTI_HEARTBEAT
242         9,  // MULTI_KILLGOALS
243         9,  // MULTI_SEISMIC
244         18, // MULTI_LIGHT
245         2,  // MULTI_START_TRIGGER
246         6,  // MULTI_FLAGS
247         2,  // MULTI_DROP_BLOB
248         MAX_POWERUP_TYPES+1, // MULTI_POWERUP_UPDATE
249         sizeof(active_door)+3, // MULTI_ACTIVE_DOOR
250         4,  // MULTI_SOUND_FUNCTION
251         2,  // MULTI_CAPTURE_BONUS
252         2,  // MULTI_GOT_FLAG
253         12, // MULTI_DROP_FLAG
254         142, // MULTI_ROBOT_CONTROLS
255         2,  // MULTI_FINISH_GAME
256         3,  // MULTI_RANK
257         1,  // MULTI_MODEM_PING
258         1,  // MULTI_MODEM_PING_RETURN
259         3,  // MULTI_ORB_BONUS
260         2,  // MULTI_GOT_ORB
261         12, // MULTI_DROP_ORB
262         4,  // MULTI_PLAY_BY_PLAY
263 };
264
265 void extract_netplayer_stats( netplayer_stats *ps, player * pd );
266 void use_netplayer_stats( player * ps, netplayer_stats *pd );
267 char PowerupsInMine[MAX_POWERUP_TYPES],MaxPowerupsAllowed[MAX_POWERUP_TYPES];
268 extern fix ThisLevelTime;
269
270 //
271 //  Functions that replace what used to be macros
272 //
273
274 int objnum_remote_to_local(int remote_objnum, int owner)
275 {
276         // Map a remote object number from owner to a local object number
277
278         int result;
279
280         if ((owner >= N_players) || (owner < -1)) {
281                 Int3(); // Illegal!
282                 return(remote_objnum);
283         }
284
285         if (owner == -1)
286                 return(remote_objnum);
287
288         if ((remote_objnum < 0) || (remote_objnum >= MAX_OBJECTS))
289                 return(-1);
290
291         result = remote_to_local[owner][remote_objnum];
292
293         if (result < 0)
294         {
295                 mprintf((1, "Remote object owner %d number %d mapped to -1!\n", owner, remote_objnum));
296                 return(-1);
297         }
298
299 #ifndef NDEBUG
300         if (object_owner[result] != owner)
301         {
302                 mprintf((1, "Remote object owner %d number %d doesn't match owner %d.\n", owner, remote_objnum, object_owner[result]));
303         }
304 #endif
305         //Assert(object_owner[result] == owner);
306
307         return(result);
308 }
309
310 int objnum_local_to_remote(int local_objnum, sbyte *owner)
311 {
312         // Map a local object number to a remote + owner
313
314         int result;
315
316         if ((local_objnum < 0) || (local_objnum > Highest_object_index)) {
317                 *owner = -1;
318                 return(-1);
319         }
320
321         *owner = object_owner[local_objnum];
322
323         if (*owner == -1)
324                 return(local_objnum);
325
326         if ((*owner >= N_players) || (*owner < -1)) {
327                 Int3(); // Illegal!
328                 *owner = -1;
329                 return local_objnum;
330         }
331
332         result = local_to_remote[local_objnum];
333
334         //mprintf((0, "Local object %d mapped to owner %d objnum %d.\n", local_objnum,
335         //      *owner, result));
336
337         if (result < 0)
338         {
339                 Int3(); // See Rob, object has no remote number!
340         }
341
342         return(result);
343 }
344
345 void
346 map_objnum_local_to_remote(int local_objnum, int remote_objnum, int owner)
347 {
348         // Add a mapping from a network remote object number to a local one
349
350         Assert(local_objnum > -1);
351         Assert(remote_objnum > -1);
352         Assert(owner > -1);
353         Assert(owner != Player_num);
354         Assert(local_objnum < MAX_OBJECTS);
355         Assert(remote_objnum < MAX_OBJECTS);
356
357         object_owner[local_objnum] = owner;
358
359         remote_to_local[owner][remote_objnum] = local_objnum;
360         local_to_remote[local_objnum] = remote_objnum;
361
362         return;
363 }
364
365 void
366 map_objnum_local_to_local(int local_objnum)
367 {
368         // Add a mapping for our locally created objects
369
370         Assert(local_objnum > -1);
371         Assert(local_objnum < MAX_OBJECTS);
372
373         object_owner[local_objnum] = Player_num;
374         remote_to_local[Player_num][local_objnum] = local_objnum;
375         local_to_remote[local_objnum] = local_objnum;
376
377         return;
378 }
379
380 //
381 // Part 1 : functions whose main purpose in life is to divert the flow
382 //          of execution to either network or serial specific code based
383 //          on the curretn Game_mode value.
384 //
385
386 void
387 multi_endlevel_score(void)
388 {
389         int old_connect=0;
390         int i;
391
392         // Show a score list to end of net players
393
394         // Save connect state and change to new connect state
395 #ifdef NETWORK
396         if (Game_mode & GM_NETWORK)
397         {
398                 old_connect = Players[Player_num].connected;
399                 if (Players[Player_num].connected!=3)
400                         Players[Player_num].connected = CONNECT_END_MENU;
401                 Network_status = NETSTAT_ENDLEVEL;
402
403         }
404 #endif
405
406         // Do the actual screen we wish to show
407
408         Function_mode = FMODE_MENU;
409
410         kmatrix_view(Game_mode & GM_NETWORK);
411
412         Function_mode = FMODE_GAME;
413
414         // Restore connect state
415
416         if (Game_mode & GM_NETWORK)
417         {
418                 Players[Player_num].connected = old_connect;
419         }
420
421 #ifndef SHAREWARE
422         if (Game_mode & GM_MULTI_COOP)
423         {
424                 int i;
425                 for (i = 0; i < MaxNumNetPlayers; i++)
426                         // Reset keys
427                         Players[i].flags &= ~(PLAYER_FLAGS_BLUE_KEY | PLAYER_FLAGS_RED_KEY | PLAYER_FLAGS_GOLD_KEY);
428         }
429         for (i = 0; i < MaxNumNetPlayers; i++)
430                 Players[i].flags &= ~(PLAYER_FLAGS_FLAG);  // Clear capture flag
431
432 #endif
433
434         for (i=0;i<MAX_PLAYERS;i++)
435                 Players[i].KillGoalCount=0;
436
437         for (i=0;i<MAX_POWERUP_TYPES;i++)
438         {
439                 MaxPowerupsAllowed[i]=0;
440                 PowerupsInMine[i]=0;
441         }
442
443 }
444
445 int
446 get_team(int pnum)
447 {
448         if ((Game_mode & GM_CAPTURE) && ((Game_mode & GM_SERIAL) || (Game_mode & GM_MODEM)))
449                 return pnum;
450
451         if (Netgame.team_vector & (1 << pnum))
452                 return 1;
453         else
454                 return 0;
455 }
456
457 int
458 multi_choose_mission(int *anarchy_only)
459 {
460         int i, n_missions;
461         int default_mission;
462         char *m[MAX_MISSIONS];
463         int new_mission_num = 0;
464
465         *anarchy_only = 0;
466
467         n_missions = build_mission_list(1);
468
469         if (n_missions > 1) {
470
471                 default_mission = 0;
472                 for (i=0;i<n_missions;i++) {
473                         m[i] = Mission_list[i].mission_name;
474                         if ( !stricmp( m[i], config_last_mission ) )
475                                 default_mission = i;
476                 }
477
478       ExtGameStatus=GAMESTAT_START_MULTIPLAYER_MISSION;
479                 new_mission_num = newmenu_listbox1(TXT_MULTI_MISSION, n_missions, m, 1, default_mission, NULL );
480
481                 if (new_mission_num == -1)
482                         return -1;      //abort!
483
484                 strcpy(config_last_mission, m[new_mission_num]  );
485
486                 if (!load_mission(new_mission_num)) {
487                         nm_messagebox( NULL, 1, TXT_OK, TXT_MISSION_ERROR);
488                         return -1;
489                 }
490
491                 *anarchy_only = Mission_list[new_mission_num].anarchy_only_flag;
492         }
493         return(new_mission_num);
494 }
495
496 extern void game_disable_cheats();
497
498 void
499 multi_new_game(void)
500 {
501         int i;
502
503         // Reset variables for a new net game
504
505         memset(kill_matrix, 0, MAX_NUM_NET_PLAYERS*MAX_NUM_NET_PLAYERS*2); // Clear kill matrix
506
507         for (i = 0; i < MAX_NUM_NET_PLAYERS; i++)
508         {
509                 sorted_kills[i] = i;
510                 Players[i].net_killed_total = 0;
511                 Players[i].net_kills_total = 0;
512                 Players[i].flags = 0;
513                 Players[i].KillGoalCount=0;
514         }
515
516 #ifndef SHAREWARE
517         for (i = 0; i < MAX_ROBOTS_CONTROLLED; i++)
518         {
519                 robot_controlled[i] = -1;
520                 robot_agitation[i] = 0;
521                 robot_fired[i] = 0;
522         }
523 #endif
524
525         team_kills[0] = team_kills[1] = 0;
526         Endlevel_sequence = 0;
527         Player_is_dead = 0;
528         multi_leave_menu = 0;
529         multi_quit_game = 0;
530         Show_kill_list = 1;
531         game_disable_cheats();
532         Player_exploded = 0;
533         Dead_player_camera = 0;
534 }
535
536 void
537 multi_make_player_ghost(int playernum)
538 {
539         object *obj;
540
541         //Assert(playernum != Player_num);
542         //Assert(playernum < MAX_NUM_NET_PLAYERS);
543
544         if ((playernum == Player_num) || (playernum >= MAX_NUM_NET_PLAYERS) || (playernum < 0))
545         {
546                 Int3(); // Non-terminal, see Rob
547                 return;
548         }
549
550 //      if (Objects[Players[playernum].objnum].type != OBJ_PLAYER)
551 //              mprintf((1, "Warning: Player %d is not currently a player.\n", playernum));
552
553         obj = &Objects[Players[playernum].objnum];
554
555         obj->type = OBJ_GHOST;
556         obj->render_type = RT_NONE;
557         obj->movement_type = MT_NONE;
558         multi_reset_player_object(obj);
559
560         if (Game_mode & GM_MULTI_ROBOTS)
561                 multi_strip_robots(playernum);
562 }
563
564 void
565 multi_make_ghost_player(int playernum)
566 {
567         object *obj;
568
569 // Assert(playernum != Player_num);
570 // Assert(playernum < MAX_NUM_NET_PLAYERS);
571
572         if ((playernum == Player_num) || (playernum >= MAX_NUM_NET_PLAYERS))
573         {
574                 Int3(); // Non-terminal, see rob
575                 return;
576         }
577
578 //      if(Objects[Players[playernum].objnum].type != OBJ_GHOST)
579 //              mprintf((1, "Warning: Player %d is not currently a ghost.\n", playernum));
580
581         obj = &Objects[Players[playernum].objnum];
582
583         obj->type = OBJ_PLAYER;
584         obj->movement_type = MT_PHYSICS;
585         multi_reset_player_object(obj);
586 }
587
588 int multi_get_kill_list(int *plist)
589 {
590         // Returns the number of active net players and their
591         // sorted order of kills
592         int i;
593         int n = 0;
594
595         for (i = 0; i < N_players; i++)
596                 //if (Players[sorted_kills[i]].connected)
597                 plist[n++] = sorted_kills[i];
598
599         if (n == 0)
600                 Int3(); // SEE ROB OR MATT
601
602         //memcpy(plist, sorted_kills, N_players*sizeof(int));
603
604         return(n);
605 }
606
607 void
608 multi_sort_kill_list(void)
609 {
610         // Sort the kills list each time a new kill is added
611
612         int kills[MAX_NUM_NET_PLAYERS];
613         int i;
614         int changed = 1;
615
616         for (i = 0; i < MAX_NUM_NET_PLAYERS; i++)
617         {
618 #ifndef SHAREWARE
619                 if (Game_mode & GM_MULTI_COOP)
620                         kills[i] = Players[i].score;
621                 else
622 #endif
623                 if (Show_kill_list==2)
624                 {
625                         if (Players[i].net_killed_total+Players[i].net_kills_total==0)
626                                 kills[i]=-1;  // always draw the ones without any ratio last
627                         else
628                                 kills[i]=(int)((float)((float)Players[i].net_kills_total/((float)Players[i].net_killed_total+(float)Players[i].net_kills_total))*100.0);
629                 }
630                 else
631                         kills[i] = Players[i].net_kills_total;
632         }
633
634         while (changed)
635         {
636                 changed = 0;
637                 for (i = 0; i < N_players-1; i++)
638                 {
639                         if (kills[sorted_kills[i]] < kills[sorted_kills[i+1]])
640                         {
641                                 changed = sorted_kills[i];
642                                 sorted_kills[i] = sorted_kills[i+1];
643                                 sorted_kills[i+1] = changed;
644                                 changed = 1;
645                         }
646                 }
647         }
648         mprintf((0, "Sorted kills %d %d.\n", sorted_kills[0], sorted_kills[1]));
649 }
650
651 extern object *obj_find_first_of_type (int);
652 char Multi_killed_yourself=0;
653
654 void multi_compute_kill(int killer, int killed)
655 {
656         // Figure out the results of a network kills and add it to the
657         // appropriate player's tally.
658
659         int killed_pnum, killed_type;
660         int killer_pnum, killer_type,killer_id;
661         int TheGoal;
662         char killed_name[(CALLSIGN_LEN*2)+4];
663         char killer_name[(CALLSIGN_LEN*2)+4];
664
665         kmatrix_kills_changed = 1;
666         Multi_killed_yourself=0;
667
668         // Both object numbers are localized already!
669
670         mprintf((0, "compute_kill passed: object %d killed object %d.\n", killer, killed));
671
672         if ((killed < 0) || (killed > Highest_object_index) || (killer < 0) || (killer > Highest_object_index))
673         {
674                 Int3(); // See Rob, illegal value passed to compute_kill;
675                 return;
676         }
677
678         killed_type = Objects[killed].type;
679         killer_type = Objects[killer].type;
680         killer_id = Objects[killer].id;
681
682         if ((killed_type != OBJ_PLAYER) && (killed_type != OBJ_GHOST))
683         {
684                 Int3(); // compute_kill passed non-player object!
685                 return;
686         }
687
688         killed_pnum = Objects[killed].id;
689
690         PKilledFlags[killed_pnum]=1;
691
692         Assert ((killed_pnum >= 0) && (killed_pnum < N_players));
693
694         if (Game_mode & GM_TEAM)
695                 sprintf(killed_name, "%s (%s)", Players[killed_pnum].callsign, Netgame.team_name[get_team(killed_pnum)]);
696         else
697                 sprintf(killed_name, "%s", Players[killed_pnum].callsign);
698
699         if (Newdemo_state == ND_STATE_RECORDING)
700                 newdemo_record_multi_death(killed_pnum);
701
702         digi_play_sample( SOUND_HUD_KILL, F3_0 );
703
704         if (Control_center_destroyed)
705                 Players[killed_pnum].connected=3;
706
707         if (killer_type == OBJ_CNTRLCEN)
708         {
709                 Players[killed_pnum].net_killed_total++;
710                 Players[killed_pnum].net_kills_total--;
711
712                 if (Newdemo_state == ND_STATE_RECORDING)
713                         newdemo_record_multi_kill(killed_pnum, -1);
714
715                 if (killed_pnum == Player_num)
716                 {
717                         HUD_init_message("%s %s.", TXT_YOU_WERE, TXT_KILLED_BY_NONPLAY);
718                         multi_add_lifetime_killed ();
719                 }
720                 else
721                         HUD_init_message("%s %s %s.", killed_name, TXT_WAS, TXT_KILLED_BY_NONPLAY );
722                 return;
723         }
724
725 #ifndef SHAREWARE
726         else if ((killer_type != OBJ_PLAYER) && (killer_type != OBJ_GHOST))
727         {
728                 if (killer_id==PMINE_ID && killer_type!=OBJ_ROBOT)
729                 {
730                         if (killed_pnum == Player_num)
731                                 HUD_init_message("You were killed by a mine!");
732                         else
733                                 HUD_init_message("%s was killed by a mine!",killed_name);
734                 }
735                 else
736                 {
737                         if (killed_pnum == Player_num)
738                         {
739                                 HUD_init_message("%s %s.", TXT_YOU_WERE, TXT_KILLED_BY_ROBOT);
740                                 multi_add_lifetime_killed();
741                         }
742                         else
743                                 HUD_init_message("%s %s %s.", killed_name, TXT_WAS, TXT_KILLED_BY_ROBOT );
744                 }
745                 Players[killed_pnum].net_killed_total++;
746                 return;
747         }
748 #else
749         else if ((killer_type != OBJ_PLAYER) && (killer_type != OBJ_GHOST) && (killer_id!=PMINE_ID))
750         {
751                 Int3(); // Illegal killer type?
752                 return;
753         }
754         if (killer_id==PMINE_ID)
755         {
756                 if (killed_pnum==Player_num)
757                         HUD_init_message("You were killed by a mine!");
758                 else
759                         HUD_init_message("%s was killed by a mine!",killed_name);
760
761                 Players[killed_pnum].net_killed_total++;
762
763                 return;
764         }
765 #endif
766
767         killer_pnum = Objects[killer].id;
768
769         if (Game_mode & GM_TEAM)
770                 sprintf(killer_name, "%s (%s)", Players[killer_pnum].callsign, Netgame.team_name[get_team(killer_pnum)]);
771         else
772                 sprintf(killer_name, "%s", Players[killer_pnum].callsign);
773
774         // Beyond this point, it was definitely a player-player kill situation
775
776         if ((killer_pnum < 0) || (killer_pnum >= N_players))
777                 Int3(); // See rob, tracking down bug with kill HUD messages
778         if ((killed_pnum < 0) || (killed_pnum >= N_players))
779                 Int3(); // See rob, tracking down bug with kill HUD messages
780
781         if (killer_pnum == killed_pnum)
782         {
783                 if (!(Game_mode & GM_HOARD))
784                 {
785                         if (Game_mode & GM_TEAM)
786                                 team_kills[get_team(killed_pnum)] -= 1;
787
788                         Players[killed_pnum].net_killed_total += 1;
789                         Players[killed_pnum].net_kills_total -= 1;
790
791                         if (Newdemo_state == ND_STATE_RECORDING)
792                                 newdemo_record_multi_kill(killed_pnum, -1);
793                 }
794                 kill_matrix[killed_pnum][killed_pnum] += 1; // # of suicides
795
796                 if (killer_pnum == Player_num)
797                 {
798                         HUD_init_message("%s %s %s!", TXT_YOU, TXT_KILLED, TXT_YOURSELF );
799                         Multi_killed_yourself=1;
800                         multi_add_lifetime_killed();
801                 }
802                 else
803                         HUD_init_message("%s %s", killed_name, TXT_SUICIDE);
804         }
805
806         else
807         {
808                 if (!(Game_mode & GM_HOARD))
809                 {
810                         if (Game_mode & GM_TEAM)
811                         {
812                                 if (get_team(killed_pnum) == get_team(killer_pnum))
813                                         team_kills[get_team(killed_pnum)] -= 1;
814                                 else
815                                         team_kills[get_team(killer_pnum)] += 1;
816                         }
817
818                         Players[killer_pnum].net_kills_total += 1;
819                         Players[killer_pnum].KillGoalCount+=1;
820
821                         if (Newdemo_state == ND_STATE_RECORDING)
822                                 newdemo_record_multi_kill(killer_pnum, 1);
823                 }
824                 else
825                 {
826                         if (Game_mode & GM_TEAM)
827                         {
828                                 if (killed_pnum==Player_num && get_team(killed_pnum) == get_team(killer_pnum))
829                                         Multi_killed_yourself=1;
830                         }
831                 }
832
833                 kill_matrix[killer_pnum][killed_pnum] += 1;
834                 Players[killed_pnum].net_killed_total += 1;
835                 if (killer_pnum == Player_num) {
836                         HUD_init_message("%s %s %s!", TXT_YOU, TXT_KILLED, killed_name);
837                         multi_add_lifetime_kills();
838                         if ((Game_mode & GM_MULTI_COOP) && (Players[Player_num].score >= 1000))
839                                 add_points_to_score(-1000);
840                 }
841                 else if (killed_pnum == Player_num)
842                 {
843                         HUD_init_message("%s %s %s!", killer_name, TXT_KILLED, TXT_YOU);
844                         multi_add_lifetime_killed();
845                         if (Game_mode & GM_HOARD)
846                         {
847                                 if (Players[Player_num].secondary_ammo[PROXIMITY_INDEX]>3)
848                                         multi_send_play_by_play (1,killer_pnum,Player_num);
849                                 else if (Players[Player_num].secondary_ammo[PROXIMITY_INDEX]>0)
850                                         multi_send_play_by_play (0,killer_pnum,Player_num);
851                         }
852                 }
853                 else
854                         HUD_init_message("%s %s %s!", killer_name, TXT_KILLED, killed_name);
855         }
856
857         TheGoal=Netgame.KillGoal*5;
858
859         if (Netgame.KillGoal>0)
860         {
861                 if (Players[killer_pnum].KillGoalCount>=TheGoal)
862                 {
863                         if (killer_pnum==Player_num)
864                         {
865                                 HUD_init_message("You reached the kill goal!");
866                                 Players[Player_num].shields=i2f(200);
867                         }
868                         else
869                                 HUD_init_message ("%s has reached the kill goal!",Players[killer_pnum].callsign);
870
871                         HUD_init_message ("The control center has been destroyed!");
872                         net_destroy_controlcen (obj_find_first_of_type (OBJ_CNTRLCEN));
873                 }
874         }
875
876         multi_sort_kill_list();
877         multi_show_player_list();
878         Players[killed_pnum].flags&=(~(PLAYER_FLAGS_HEADLIGHT_ON));  // clear the killed guys flags/headlights
879 }
880
881 void
882 multi_do_frame(void)
883 {
884         static int lasttime=0;
885         int i;
886
887         if (!(Game_mode & GM_MULTI))
888         {
889                 Int3();
890                 return;
891         }
892
893         if ((Game_mode & GM_NETWORK) && Netgame.PlayTimeAllowed && lasttime!=f2i (ThisLevelTime))
894         {
895                 for (i=0;i<N_players;i++)
896                         if (Players[i].connected)
897                         {
898                                 if (i==Player_num)
899                                 {
900                                         multi_send_heartbeat();
901                                         lasttime=f2i(ThisLevelTime);
902                                 }
903                                 break;
904                         }
905         }
906
907         multi_send_message(); // Send any waiting messages
908
909         if (!multi_in_menu)
910                 multi_leave_menu = 0;
911
912 #ifndef SHAREWARE
913         if (Game_mode & GM_MULTI_ROBOTS)
914         {
915                 multi_check_robot_timeout();
916         }
917 #endif
918
919         if ((Game_mode & GM_SERIAL) || (Game_mode & GM_MODEM))
920         {
921                 com_do_frame();
922         }
923         else
924         {
925                 network_do_frame(0, 1);
926         }
927
928         if (multi_quit_game && !multi_in_menu)
929         {
930                 multi_quit_game = 0;
931                 longjmp(LeaveGame, 0);
932         }
933 }
934
935 void
936 multi_send_data(char *buf, int len, int repeat)
937 {
938         Assert(len == message_length[(int)buf[0]]);
939         Assert(buf[0] <= MULTI_MAX_TYPE);
940         //      Assert(buf[0] >= 0);
941
942         if (Game_mode & GM_NETWORK)
943                 Assert(buf[0] > 0);
944
945         if ((Game_mode & GM_SERIAL) || (Game_mode & GM_MODEM))
946                 com_send_data(buf, len, repeat);
947         else if (Game_mode & GM_NETWORK)
948                 network_send_data(buf, len, repeat);
949 }
950
951 void
952 multi_leave_game(void)
953 {
954
955         //      if (Function_mode != FMODE_GAME)
956         //              return;
957
958         if (!(Game_mode & GM_MULTI))
959                 return;
960
961         if (Game_mode & GM_NETWORK)
962         {
963                 mprintf((0, "Sending explosion message.\n"));
964
965                 Net_create_loc = 0;
966                 AdjustMineSpawn();
967                 multi_cap_objects();
968                 drop_player_eggs(ConsoleObject);
969                 multi_send_position(Players[Player_num].objnum);
970                 multi_send_player_explode(MULTI_PLAYER_DROP);
971         }
972
973         mprintf((1, "Sending leave game.\n"));
974         multi_send_quit(MULTI_QUIT);
975
976         if ((Game_mode & GM_SERIAL) || (Game_mode & GM_MODEM))
977                 serial_leave_game();
978         if (Game_mode & GM_NETWORK)
979                 network_leave_game();
980
981         Game_mode |= GM_GAME_OVER;
982
983         if (Function_mode!=FMODE_EXIT)
984                 Function_mode = FMODE_MENU;
985
986         //      N_players = 0;
987
988         //      change_playernum_to(0);
989         //      Viewer = ConsoleObject = &Objects[0];
990
991 }
992
993 void
994 multi_show_player_list()
995 {
996         if (!(Game_mode & GM_MULTI) || (Game_mode & GM_MULTI_COOP))
997                 return;
998
999         if (Show_kill_list)
1000                 return;
1001
1002         Show_kill_list_timer = F1_0*5; // 5 second timer
1003         Show_kill_list = 1;
1004 }
1005
1006 int
1007 multi_endlevel(int *secret)
1008 {
1009         int result = 0;
1010
1011         if ((Game_mode & GM_SERIAL) || (Game_mode & GM_MODEM))
1012                 com_endlevel(secret);          // an opportunity to re-sync or whatever
1013         else if (Game_mode & GM_NETWORK)
1014                 result = network_endlevel(secret);
1015
1016         return(result);
1017 }
1018
1019 //
1020 // Part 2 : functions that act on network/serial messages and change the
1021 //          the state of the game in some way.
1022 //
1023
1024 #ifndef MACINTOSH
1025 //extern PORT *com_port;
1026 #endif
1027
1028 int
1029 multi_menu_poll(void)
1030 {
1031         fix old_shields;
1032         int was_fuelcen_alive;
1033         int player_was_dead;
1034
1035         was_fuelcen_alive = Control_center_destroyed;
1036
1037         // Special polling function for in-game menus for multiplayer and serial
1038
1039         if (! ((Game_mode & GM_MULTI) && (Function_mode == FMODE_GAME)) )
1040                 return(0);
1041
1042         if (multi_leave_menu)
1043                 return(-1);
1044
1045         old_shields = Players[Player_num].shields;
1046         player_was_dead = Player_is_dead;
1047
1048         multi_in_menu++; // Track level of menu nesting
1049
1050         GameLoop( 0, 0 );
1051
1052         multi_in_menu--;
1053
1054         timer_delay(f0_1);   // delay 100 milliseconds
1055
1056         if (Endlevel_sequence || (Control_center_destroyed && !was_fuelcen_alive) || (Player_is_dead != player_was_dead) || (Players[Player_num].shields < old_shields))
1057         {
1058                 multi_leave_menu = 1;
1059                 return(-1);
1060         }
1061         if ((Control_center_destroyed) && (Countdown_seconds_left < 10))
1062         {
1063                 multi_leave_menu = 1;
1064                 return(-1);
1065         }
1066
1067 #if 0
1068         if ((Game_mode & GM_MODEM) && (!GetCd(com_port)))
1069         {
1070                 multi_leave_menu = 1;
1071                 return(-1);
1072         }
1073 #endif
1074
1075         return(0);
1076 }
1077
1078 void
1079 multi_define_macro(int key)
1080 {
1081         if (!(Game_mode & GM_MULTI))
1082                 return;
1083
1084         key &= (~KEY_SHIFTED);
1085
1086         switch(key)
1087         {
1088         case KEY_F9:
1089                 multi_defining_message = 1; break;
1090         case KEY_F10:
1091                 multi_defining_message = 2; break;
1092         case KEY_F11:
1093                 multi_defining_message = 3; break;
1094         case KEY_F12:
1095                 multi_defining_message = 4; break;
1096         default:
1097                 Int3();
1098         }
1099
1100         if (multi_defining_message)     {
1101                 multi_message_index = 0;
1102                 Network_message[multi_message_index] = 0;
1103         }
1104
1105 }
1106
1107 char feedback_result[200];
1108
1109 void
1110 multi_message_feedback(void)
1111 {
1112         char *colon;
1113         int found = 0;
1114         int i;
1115
1116         if (!( ((colon = strrchr(Network_message, ':')) == NULL) || (colon-Network_message < 1) || (colon-Network_message > CALLSIGN_LEN) ))
1117         {
1118                 sprintf(feedback_result, "%s ", TXT_MESSAGE_SENT_TO);
1119                 if ((Game_mode & GM_TEAM) && (atoi(Network_message) > 0) && (atoi(Network_message) < 3))
1120                 {
1121                         sprintf(feedback_result+strlen(feedback_result), "%s '%s'", TXT_TEAM, Netgame.team_name[atoi(Network_message)-1]);
1122                         found = 1;
1123                 }
1124                 if (Game_mode & GM_TEAM)
1125                 {
1126                         for (i = 0; i < N_players; i++)
1127                         {
1128                                 if (!strnicmp(Netgame.team_name[i], Network_message, colon-Network_message))
1129                                 {
1130                                         if (found)
1131                                                 strcat(feedback_result, ", ");
1132                                         found++;
1133                                         if (!(found % 4))
1134                                                 strcat(feedback_result, "\n");
1135                                         sprintf(feedback_result+strlen(feedback_result), "%s '%s'", TXT_TEAM, Netgame.team_name[i]);
1136                                 }
1137                         }
1138                 }
1139                 for (i = 0; i < N_players; i++)
1140                 {
1141                         if ((!strnicmp(Players[i].callsign, Network_message, colon-Network_message)) && (i != Player_num) && (Players[i].connected))
1142                         {
1143                                 if (found)
1144                                         strcat(feedback_result, ", ");
1145                                 found++;
1146                                 if (!(found % 4))
1147                                         strcat(feedback_result, "\n");
1148                                 sprintf(feedback_result+strlen(feedback_result), "%s", Players[i].callsign);
1149                         }
1150                 }
1151                 if (!found)
1152                         strcat(feedback_result, TXT_NOBODY);
1153                 else
1154                         strcat(feedback_result, ".");
1155
1156                 digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
1157
1158                 Assert(strlen(feedback_result) < 200);
1159
1160                 HUD_init_message(feedback_result);
1161                 //sprintf (temp,"%s",colon);
1162                 //sprintf (Network_message,"%s",temp);
1163
1164         }
1165 }
1166
1167 void
1168 multi_send_macro(int key)
1169 {
1170         if (! (Game_mode & GM_MULTI) )
1171                 return;
1172
1173         switch(key)
1174         {
1175         case KEY_F9:
1176                 key = 0; break;
1177         case KEY_F10:
1178                 key = 1; break;
1179         case KEY_F11:
1180                 key = 2; break;
1181         case KEY_F12:
1182                 key = 3; break;
1183         default:
1184                 Int3();
1185         }
1186
1187         if (!Network_message_macro[key][0])
1188         {
1189                 HUD_init_message(TXT_NO_MACRO);
1190                 return;
1191         }
1192
1193         strcpy(Network_message, Network_message_macro[key]);
1194         Network_message_reciever = 100;
1195
1196         HUD_init_message("%s '%s'", TXT_SENDING, Network_message);
1197         multi_message_feedback();
1198 }
1199
1200
1201 void
1202 multi_send_message_start()
1203 {
1204         if (Game_mode&GM_MULTI) {
1205                 multi_sending_message = 1;
1206                 multi_message_index = 0;
1207                 Network_message[multi_message_index] = 0;
1208         }
1209 }
1210
1211 extern fix StartingShields;
1212 fix PingLaunchTime,PingReturnTime;
1213
1214 extern void network_send_ping (ubyte);
1215 extern int network_who_is_master();
1216 extern char NameReturning;
1217 extern int force_cockpit_redraw;
1218
1219 void network_dump_appletalk_player(ubyte node, ushort net, ubyte socket, int why);
1220
1221 void multi_send_message_end()
1222 {
1223         char *mytempbuf;
1224         int i,t;
1225
1226         Network_message_reciever = 100;
1227
1228         if (!strnicmp (Network_message,"!Names",6))
1229         {
1230                 NameReturning=1-NameReturning;
1231                 HUD_init_message ("Name returning is now %s.",NameReturning?"active":"disabled");
1232         }
1233         else if (!strnicmp (Network_message,"Handicap:",9))
1234         {
1235                 mytempbuf=&Network_message[9];
1236                 mprintf ((0,"Networkhandi=%s\n",mytempbuf));
1237                 StartingShields=atol (mytempbuf);
1238                 if (StartingShields<10)
1239                         StartingShields=10;
1240                 if (StartingShields>100)
1241                 {
1242                         sprintf (Network_message,"%s has tried to cheat!",Players[Player_num].callsign);
1243                         StartingShields=100;
1244                 }
1245                 else
1246                         sprintf (Network_message,"%s handicap is now %d",Players[Player_num].callsign,StartingShields);
1247
1248                 HUD_init_message ("Telling others of your handicap of %d!",StartingShields);
1249                 StartingShields=i2f(StartingShields);
1250         }
1251         else if (!strnicmp (Network_message,"NoBombs",7))
1252                 Netgame.DoSmartMine=0;
1253         else if (!strnicmp (Network_message,"Ping:",5))
1254         {
1255                 if (Game_mode & GM_NETWORK)
1256                 {
1257                         int name_index=5;
1258                         if (strlen(Network_message) > 5)
1259                                 while (Network_message[name_index] == ' ')
1260                                         name_index++;
1261
1262                         if (strlen(Network_message)<=name_index)
1263                         {
1264                                 HUD_init_message ("You must specify a name to ping");
1265                                 return;
1266                         }
1267
1268                         for (i = 0; i < N_players; i++)
1269                                 if ((!strnicmp(Players[i].callsign, &Network_message[name_index], strlen(Network_message)-name_index)) && (i != Player_num) && (Players[i].connected))
1270                                 {
1271                                         PingLaunchTime=timer_get_fixed_seconds();
1272                                         network_send_ping (i);
1273                                         HUD_init_message("Pinging %s...",Players[i].callsign);
1274                                         multi_message_index = 0;
1275                                         multi_sending_message = 0;
1276                                         return;
1277                                 }
1278                 }
1279                 else // Modem/Serial ping
1280                 {
1281                         PingLaunchTime=timer_get_fixed_seconds();
1282                         multi_send_modem_ping ();
1283                         HUD_init_message("Pinging opponent...");
1284                     multi_message_index = 0;
1285                         multi_sending_message = 0;
1286                         return;
1287                 }
1288         }
1289         else if (!strnicmp (Network_message,"move:",5))
1290         {
1291                 mprintf ((0,"moving?\n"));
1292
1293                 if ((Game_mode & GM_NETWORK) && (Game_mode & GM_TEAM))
1294                 {
1295                         int name_index=5;
1296                         if (strlen(Network_message) > 5)
1297                                 while (Network_message[name_index] == ' ')
1298                                         name_index++;
1299
1300                         if (!network_i_am_master())
1301                         {
1302                                 HUD_init_message ("Only %s can move players!",Players[network_who_is_master()].callsign);
1303                                 return;
1304                         }
1305
1306                         if (strlen(Network_message)<=name_index)
1307                         {
1308                                 HUD_init_message ("You must specify a name to move");
1309                                 return;
1310                         }
1311
1312                         for (i = 0; i < N_players; i++)
1313                                 if ((!strnicmp(Players[i].callsign, &Network_message[name_index], strlen(Network_message)-name_index)) && (Players[i].connected))
1314                                 {
1315                                         if ((Game_mode & GM_CAPTURE) && (Players[i].flags & PLAYER_FLAGS_FLAG))
1316                                         {
1317                                                 HUD_init_message ("Can't move player because s/he has a flag!");
1318                                                 return;
1319                                         }
1320
1321                                         if (Netgame.team_vector & (1<<i))
1322                                                 Netgame.team_vector&=(~(1<<i));
1323                                         else
1324                                                 Netgame.team_vector|=(1<<i);
1325
1326                                         for (t=0;t<N_players;t++)
1327                                                 if (Players[t].connected)
1328                                                         multi_reset_object_texture (&Objects[Players[t].objnum]);
1329
1330                                         network_send_netgame_update ();
1331                                         sprintf (Network_message,"%s has changed teams!",Players[i].callsign);
1332                                         if (i==Player_num)
1333                                         {
1334                                                 HUD_init_message ("You have changed teams!");
1335                                                 reset_cockpit();
1336                                         }
1337                                         else
1338                                                 HUD_init_message ("Moving %s to other team.",Players[i].callsign);
1339                                         break;
1340                                 }
1341                 }
1342         }
1343
1344         else if (!strnicmp (Network_message,"kick:",5) && (Game_mode & GM_NETWORK))
1345         {
1346                 int name_index=5;
1347                 if (strlen(Network_message) > 5)
1348                         while (Network_message[name_index] == ' ')
1349                                 name_index++;
1350
1351                 if (!network_i_am_master())
1352                 {
1353                         HUD_init_message ("Only %s can kick others out!",Players[network_who_is_master()].callsign);
1354                         multi_message_index = 0;
1355                         multi_sending_message = 0;
1356                         return;
1357                 }
1358                 if (strlen(Network_message)<=name_index)
1359                 {
1360                         HUD_init_message ("You must specify a name to kick");
1361                         multi_message_index = 0;
1362                         multi_sending_message = 0;
1363                         return;
1364                 }
1365
1366                 if (Network_message[name_index] == '#' && isdigit(Network_message[name_index+1])) {
1367                         int players[MAX_NUM_NET_PLAYERS];
1368                         int listpos = Network_message[name_index+1] - '0';
1369
1370                         mprintf ((0,"Trying to kick %d , show_kill_list=%d\n",listpos,Show_kill_list));
1371
1372                         if (Show_kill_list==1 || Show_kill_list==2) {
1373                                 if (listpos == 0 || listpos >= N_players) {
1374                                         HUD_init_message ("Invalid player number for kick.");
1375                                         multi_message_index = 0;
1376                                         multi_sending_message = 0;
1377                                         return;
1378                                 }
1379                                 multi_get_kill_list(players);
1380                                 i = players[listpos];
1381                                 if ((i != Player_num) && (Players[i].connected))
1382                                         goto kick_player;
1383                         }
1384                         else HUD_init_message ("You cannot use # kicking with in team display.");
1385
1386
1387                     multi_message_index = 0;
1388                     multi_sending_message = 0;
1389                         return;
1390                 }
1391
1392
1393                 for (i = 0; i < N_players; i++)
1394                 if ((!strnicmp(Players[i].callsign, &Network_message[name_index], strlen(Network_message)-name_index)) && (i != Player_num) && (Players[i].connected)) {
1395                         kick_player:;
1396                                 if (Network_game_type == IPX_GAME)
1397                                         network_dump_player(NetPlayers.players[i].network.ipx.server,NetPlayers.players[i].network.ipx.node, 7);
1398                                 else
1399                                         network_dump_appletalk_player(NetPlayers.players[i].network.appletalk.node,NetPlayers.players[i].network.appletalk.net, NetPlayers.players[i].network.appletalk.socket, 7);
1400
1401                                 HUD_init_message("Dumping %s...",Players[i].callsign);
1402                                 multi_message_index = 0;
1403                                 multi_sending_message = 0;
1404                                 return;
1405                         }
1406         }
1407
1408         else
1409                 HUD_init_message("%s '%s'", TXT_SENDING, Network_message);
1410
1411         multi_send_message();
1412         multi_message_feedback();
1413
1414         multi_message_index = 0;
1415         multi_sending_message = 0;
1416 }
1417
1418 void multi_define_macro_end()
1419 {
1420         Assert( multi_defining_message > 0 );
1421
1422         strcpy( Network_message_macro[multi_defining_message-1], Network_message );
1423         write_player_file();
1424
1425         multi_message_index = 0;
1426         multi_defining_message = 0;
1427 }
1428
1429 void multi_message_input_sub( int key )
1430 {
1431         switch( key )   {
1432         case KEY_F8:
1433         case KEY_ESC:
1434                 multi_sending_message = 0;
1435                 multi_defining_message = 0;
1436                 game_flush_inputs();
1437                 break;
1438         case KEY_LEFT:
1439         case KEY_BACKSP:
1440         case KEY_PAD4:
1441                 if (multi_message_index > 0)
1442                         multi_message_index--;
1443                 Network_message[multi_message_index] = 0;
1444                 break;
1445         case KEY_ENTER:
1446                 if ( multi_sending_message )
1447                         multi_send_message_end();
1448                 else if ( multi_defining_message )
1449                         multi_define_macro_end();
1450                 game_flush_inputs();
1451                 break;
1452         default:
1453                 if ( key > 0 )  {
1454                         int ascii = key_to_ascii(key);
1455                         if ((ascii < 255 ))     {
1456                                 if (multi_message_index < MAX_MESSAGE_LEN-2 )   {
1457                                         Network_message[multi_message_index++] = ascii;
1458                                         Network_message[multi_message_index] = 0;
1459                                 } else if ( multi_sending_message )     {
1460                                         int i;
1461                                         char * ptext, * pcolon;
1462                                         ptext = NULL;
1463                                         Network_message[multi_message_index++] = ascii;
1464                                         Network_message[multi_message_index] = 0;
1465                                         for (i=multi_message_index-1; i>=0; i-- )       {
1466                                                 if ( Network_message[i]==32 )   {
1467                                                         ptext = &Network_message[i+1];
1468                                                         Network_message[i] = 0;
1469                                                         break;
1470                                                 }
1471                                         }
1472                                         multi_send_message_end();
1473                                         if ( ptext )    {
1474                                                 multi_sending_message = 1;
1475                                                 pcolon = strchr( Network_message, ':' );
1476                                                 if ( pcolon )
1477                                                         strcpy( pcolon+1, ptext );
1478                                                 else
1479                                                         strcpy( Network_message, ptext );
1480                                                 multi_message_index = strlen( Network_message );
1481                                         }
1482                                 }
1483                         }
1484                 }
1485         }
1486 }
1487
1488 void
1489 multi_send_message_dialog(void)
1490 {
1491         newmenu_item m[1];
1492         int choice;
1493
1494         if (!(Game_mode&GM_MULTI))
1495                 return;
1496
1497         Network_message[0] = 0;             // Get rid of old contents
1498
1499         m[0].type=NM_TYPE_INPUT; m[0].text = Network_message; m[0].text_len = MAX_MESSAGE_LEN-1;
1500         choice = newmenu_do( NULL, TXT_SEND_MESSAGE, 1, m, NULL );
1501
1502         if ((choice > -1) && (strlen(Network_message) > 0)) {
1503                 Network_message_reciever = 100;
1504                 HUD_init_message("%s '%s'", TXT_SENDING, Network_message);
1505                 multi_message_feedback();
1506         }
1507 }
1508
1509
1510
1511 void
1512 multi_do_death(int objnum)
1513 {
1514         // Do any miscellaneous stuff for a new network player after death
1515
1516         objnum = objnum;
1517
1518         if (!(Game_mode & GM_MULTI_COOP))
1519         {
1520                 mprintf((0, "Setting all keys for player %d.\n", Player_num));
1521                 Players[Player_num].flags |= (PLAYER_FLAGS_RED_KEY | PLAYER_FLAGS_BLUE_KEY | PLAYER_FLAGS_GOLD_KEY);
1522         }
1523 }
1524
1525 void
1526 multi_do_fire(char *buf)
1527 {
1528         ubyte weapon;
1529         char pnum;
1530         sbyte flags;
1531         //static dum=0;
1532
1533         // Act out the actual shooting
1534         pnum = buf[1];
1535
1536 #ifndef MACINTOSH
1537         weapon = (int)buf[2];
1538 #else
1539         weapon = buf[2];
1540 #endif
1541         flags = buf[4];
1542         GET_INTEL_SHORT(Network_laser_track, buf+6);
1543
1544         Assert (pnum < N_players);
1545
1546         if (Objects[Players[(int)pnum].objnum].type == OBJ_GHOST)
1547                 multi_make_ghost_player(pnum);
1548
1549         //  mprintf((0,"multi_do_fire, weapon = %d\n",weapon));
1550
1551         if (weapon == FLARE_ADJUST)
1552                 Laser_player_fire( Objects+Players[(int)pnum].objnum, FLARE_ID, 6, 1, 0);
1553         else if (weapon >= MISSILE_ADJUST) {
1554                 int weapon_id,weapon_gun;
1555
1556                 weapon_id = Secondary_weapon_to_weapon_info[weapon-MISSILE_ADJUST];
1557                 weapon_gun = Secondary_weapon_to_gun_num[weapon-MISSILE_ADJUST] + (flags & 1);
1558                 mprintf((0,"missile id = %d, gun = %d\n",weapon_id,weapon_gun));
1559
1560                 if (weapon-MISSILE_ADJUST==GUIDED_INDEX)
1561                 {
1562                         mprintf ((0,"Missile is guided!!!\n"));
1563                         Multi_is_guided=1;
1564                 }
1565
1566                 Laser_player_fire( Objects+Players[(int)pnum].objnum, weapon_id, weapon_gun, 1, 0 );
1567         }
1568         else {
1569                 fix save_charge = Fusion_charge;
1570
1571                 if (weapon == FUSION_INDEX) {
1572                         Fusion_charge = flags << 12;
1573                         mprintf((0, "Fusion charge X%f.\n", f2fl(Fusion_charge)));
1574                 }
1575                 if (weapon == LASER_ID) {
1576                         if (flags & LASER_QUAD)
1577                                 Players[(int)pnum].flags |= PLAYER_FLAGS_QUAD_LASERS;
1578                         else
1579                                 Players[(int)pnum].flags &= ~PLAYER_FLAGS_QUAD_LASERS;
1580                 }
1581
1582                 do_laser_firing(Players[(int)pnum].objnum, weapon, (int)buf[3], flags, (int)buf[5]);
1583
1584                 if (weapon == FUSION_INDEX)
1585                         Fusion_charge = save_charge;
1586         }
1587 }
1588
1589 void
1590 multi_do_message(char *buf)
1591 {
1592         char *colon;
1593         char *tilde,mesbuf[100];
1594         int tloc,t;
1595
1596         int loc = 2;
1597
1598         if ((tilde=strchr (buf+loc,'$')))  // do that stupid name stuff
1599         {                                                                                       // why'd I put this in?  Probably for the
1600                 tloc=tilde-(buf+loc);                           // same reason you can name your guidebot
1601                 mprintf ((0,"Tloc=%d\n",tloc));
1602
1603                 if (tloc>0)
1604                         strncpy (mesbuf,buf+loc,tloc);
1605                 strcpy (mesbuf+tloc,Players[Player_num].callsign);
1606                 strcpy (mesbuf+strlen(Players[Player_num].callsign)+tloc,buf+loc+tloc+1);
1607                 strcpy (buf+loc,mesbuf);
1608         }
1609
1610         if (((colon = strrchr(buf+loc, ':')) == NULL) || (colon-(buf+loc) < 1) || (colon-(buf+loc) > CALLSIGN_LEN))
1611         {
1612                 mesbuf[0] = 1;
1613                 mesbuf[1] = BM_XRGB(28, 0, 0);
1614                 strcpy(&mesbuf[2], Players[(int)buf[1]].callsign);
1615                 t = strlen(mesbuf);
1616                 mesbuf[t] = ':';
1617                 mesbuf[t+1] = 1;
1618                 mesbuf[t+2] = BM_XRGB(0, 31, 0);
1619                 mesbuf[t+3] = 0;
1620
1621                 digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
1622                 HUD_init_message("%s %s", mesbuf, buf+2);
1623         }
1624         else
1625         {
1626                 if ( (!strnicmp(Players[Player_num].callsign, buf+loc, colon-(buf+loc))) ||
1627                          ((Game_mode & GM_TEAM) && ( (get_team(Player_num) == atoi(buf+loc)-1) || !strnicmp(Netgame.team_name[get_team(Player_num)], buf+loc, colon-(buf+loc)))) )
1628                 {
1629                         mesbuf[0] = 1;
1630                         mesbuf[1] = BM_XRGB(0, 32, 32);
1631                         strcpy(&mesbuf[2], Players[(int)buf[1]].callsign);
1632                         t = strlen(mesbuf);
1633                         mesbuf[t] = ':';
1634                         mesbuf[t+1] = 1;
1635                         mesbuf[t+2] = BM_XRGB(0, 31, 0);
1636                         mesbuf[t+3] = 0;
1637
1638                         digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
1639                         HUD_init_message("%s %s", mesbuf, colon+1);
1640                 }
1641         }
1642 }
1643
1644 void
1645 multi_do_position(char *buf)
1646 {
1647 #ifdef WORDS_BIGENDIAN
1648         shortpos sp;
1649 #endif
1650
1651         // This routine does only player positions, mode game only
1652         //      mprintf((0, "Got position packet.\n"));
1653
1654         int pnum = (Player_num+1)%2;
1655
1656         Assert(&Objects[Players[pnum].objnum] != ConsoleObject);
1657
1658         if (Game_mode & GM_NETWORK)
1659         {
1660                 Int3(); // Get Jason, what the hell are we doing here?
1661                 return;
1662     }
1663
1664
1665 #ifndef WORDS_BIGENDIAN
1666         extract_shortpos(&Objects[Players[pnum].objnum], (shortpos *)(buf+1),0);
1667 #else
1668         memcpy((ubyte *)(sp.bytemat), (ubyte *)(buf + 1), 9);
1669         memcpy((ubyte *)&(sp.xo), (ubyte *)(buf + 10), 14);
1670         extract_shortpos(&Objects[Players[pnum].objnum], &sp, 1);
1671 #endif
1672
1673         if (Objects[Players[pnum].objnum].movement_type == MT_PHYSICS)
1674                 set_thrust_from_velocity(&Objects[Players[pnum].objnum]);
1675 }
1676
1677 void
1678 multi_do_reappear(char *buf)
1679 {
1680         short objnum;
1681
1682         GET_INTEL_SHORT(objnum, buf+1);
1683
1684         Assert(objnum >= 0);
1685         //      Assert(Players[Objects[objnum].id]].objnum == objnum);
1686
1687         // mprintf((0, "Switching rendering back on for object %d.\n", objnum));
1688
1689         multi_make_ghost_player(Objects[objnum].id);
1690         create_player_appearance_effect(&Objects[objnum]);
1691         PKilledFlags[Objects[objnum].id]=0;
1692 }
1693
1694 void
1695 multi_do_player_explode(char *buf)
1696 {
1697         // Only call this for players, not robots.  pnum is player number, not
1698         // Object number.
1699
1700         object *objp;
1701         int count;
1702         int pnum;
1703         int i;
1704         char remote_created;
1705
1706         pnum = buf[1];
1707
1708 #ifdef NDEBUG
1709         if ((pnum < 0) || (pnum >= N_players))
1710                 return;
1711 #else
1712         Assert(pnum >= 0);
1713         Assert(pnum < N_players);
1714 #endif
1715
1716 #ifdef NETWORK
1717         // If we are in the process of sending objects to a new player, reset that process
1718         if (Network_send_objects)
1719         {
1720                 mprintf((0, "Resetting object sync due to player explosion.\n"));
1721                 Network_send_objnum = -1;
1722         }
1723 #endif
1724
1725         // Stuff the Players structure to prepare for the explosion
1726
1727         count = 2;
1728         GET_INTEL_SHORT(Players[pnum].primary_weapon_flags, buf+count); count += 2;
1729         GET_INTEL_SHORT(Players[pnum].secondary_weapon_flags, buf+count); count += 2;
1730         Players[pnum].laser_level = buf[count];                                                 count++;
1731         Players[pnum].secondary_ammo[HOMING_INDEX] = buf[count];                count++;
1732         Players[pnum].secondary_ammo[CONCUSSION_INDEX] = buf[count];count++;
1733         Players[pnum].secondary_ammo[SMART_INDEX] = buf[count];         count++;
1734         Players[pnum].secondary_ammo[MEGA_INDEX] = buf[count];          count++;
1735         Players[pnum].secondary_ammo[PROXIMITY_INDEX] = buf[count]; count++;
1736
1737         Players[pnum].secondary_ammo[SMISSILE1_INDEX] = buf[count]; count++;
1738         Players[pnum].secondary_ammo[GUIDED_INDEX]    = buf[count]; count++;
1739         Players[pnum].secondary_ammo[SMART_MINE_INDEX]= buf[count]; count++;
1740         Players[pnum].secondary_ammo[SMISSILE4_INDEX] = buf[count]; count++;
1741         Players[pnum].secondary_ammo[SMISSILE5_INDEX] = buf[count]; count++;
1742
1743         GET_INTEL_SHORT(Players[pnum].primary_ammo[VULCAN_INDEX], buf+count); count += 2;
1744         GET_INTEL_SHORT(Players[pnum].primary_ammo[GAUSS_INDEX], buf+count); count += 2;
1745         GET_INTEL_INT(Players[pnum].flags, buf+count);                     count += 4;
1746
1747         multi_adjust_remote_cap (pnum);
1748
1749         objp = Objects+Players[pnum].objnum;
1750
1751         //      objp->phys_info.velocity = *(vms_vector *)(buf+16); // 12 bytes
1752         //      objp->pos = *(vms_vector *)(buf+28);                // 12 bytes
1753
1754         remote_created = buf[count++]; // How many did the other guy create?
1755
1756         Net_create_loc = 0;
1757
1758         drop_player_eggs(objp);
1759
1760         // Create mapping from remote to local numbering system
1761
1762         mprintf((0, "I Created %d powerups, remote created %d.\n", Net_create_loc, remote_created));
1763
1764         // We now handle this situation gracefully, Int3 not required
1765         //      if (Net_create_loc != remote_created)
1766         //              Int3(); // Probably out of object array space, see Rob
1767
1768         for (i = 0; i < remote_created; i++)
1769         {
1770                 short s;
1771
1772                 GET_INTEL_SHORT(s, buf+count);
1773
1774                 if ((i < Net_create_loc) && (s > 0))
1775                         map_objnum_local_to_remote((short)Net_create_objnums[i], s, pnum);
1776                 else if (s <= 0)
1777                 {
1778                         mprintf((0, "WARNING: Remote created object has non-valid number %d (player %d)", s, pnum));
1779                 }
1780                 else
1781                 {
1782                         mprintf((0, "WARNING: Could not create all powerups created by player %d.\n", pnum));
1783                 }
1784                 //              Assert(s > 0);
1785                 count += 2;
1786         }
1787         for (i = remote_created; i < Net_create_loc; i++) {
1788                 mprintf((0, "WARNING: I Created more powerups than player %d, deleting.\n", pnum));
1789                 Objects[Net_create_objnums[i]].flags |= OF_SHOULD_BE_DEAD;
1790         }
1791
1792         if (buf[0] == MULTI_PLAYER_EXPLODE)
1793         {
1794                 explode_badass_player(objp);
1795
1796                 objp->flags &= ~OF_SHOULD_BE_DEAD;              //don't really kill player
1797                 multi_make_player_ghost(pnum);
1798         }
1799         else
1800         {
1801                 create_player_appearance_effect(objp);
1802         }
1803
1804         Players[pnum].flags &= ~(PLAYER_FLAGS_CLOAKED | PLAYER_FLAGS_INVULNERABLE | PLAYER_FLAGS_FLAG);
1805         Players[pnum].cloak_time = 0;
1806 }
1807
1808 void
1809 multi_do_kill(char *buf)
1810 {
1811         int killer, killed;
1812         int count = 1;
1813         int pnum;
1814
1815         pnum = (int)(buf[count]);
1816         if ((pnum < 0) || (pnum >= N_players))
1817         {
1818                 Int3(); // Invalid player number killed
1819                 return;
1820         }
1821         killed = Players[pnum].objnum;
1822         count += 1;
1823
1824         GET_INTEL_SHORT(killer, buf+count);
1825         if (killer > 0)
1826                 killer = objnum_remote_to_local(killer, (sbyte)buf[count+2]);
1827
1828 #ifdef SHAREWARE
1829         if ((Objects[killed].type != OBJ_PLAYER) && (Objects[killed].type != OBJ_GHOST))
1830         {
1831                 Int3();
1832                 mprintf( (1, "SOFT INT3: MULTI.C Non-player object %d of type %d killed! (JOHN)\n", killed, Objects[killed].type ));
1833                 return;
1834         }
1835 #endif
1836
1837         multi_compute_kill(killer, killed);
1838
1839 }
1840
1841
1842 //      Changed by MK on 10/20/94 to send NULL as object to net_destroy_controlcen if it got -1
1843 // which means not a controlcen object, but contained in another object
1844 void multi_do_controlcen_destroy(char *buf)
1845 {
1846         sbyte who;
1847         short objnum;
1848
1849         GET_INTEL_SHORT(objnum, buf+1);
1850         who = buf[3];
1851
1852         if (Control_center_destroyed != 1)
1853         {
1854                 if ((who < N_players) && (who != Player_num)) {
1855                         HUD_init_message("%s %s", Players[who].callsign, TXT_HAS_DEST_CONTROL);
1856                 }
1857                 else if (who == Player_num)
1858                         HUD_init_message(TXT_YOU_DEST_CONTROL);
1859                 else
1860                         HUD_init_message(TXT_CONTROL_DESTROYED);
1861
1862                 if (objnum != -1)
1863                         net_destroy_controlcen(Objects+objnum);
1864                 else
1865                         net_destroy_controlcen(NULL);
1866         }
1867 }
1868
1869 void
1870 multi_do_escape(char *buf)
1871 {
1872         int objnum;
1873
1874         objnum = Players[(int)buf[1]].objnum;
1875
1876         digi_play_sample(SOUND_HUD_MESSAGE, F1_0);
1877         digi_kill_sound_linked_to_object (objnum);
1878
1879         if (buf[2] == 0)
1880         {
1881                 HUD_init_message("%s %s", Players[(int)buf[1]].callsign, TXT_HAS_ESCAPED);
1882                 if (Game_mode & GM_NETWORK)
1883                         Players[(int)buf[1]].connected = CONNECT_ESCAPE_TUNNEL;
1884                 if (!multi_goto_secret)
1885                         multi_goto_secret = 2;
1886         }
1887         else if (buf[2] == 1)
1888         {
1889                 HUD_init_message("%s %s", Players[(int)buf[1]].callsign, TXT_HAS_FOUND_SECRET);
1890                 if (Game_mode & GM_NETWORK)
1891                         Players[(int)buf[1]].connected = CONNECT_FOUND_SECRET;
1892                 if (!multi_goto_secret)
1893                         multi_goto_secret = 1;
1894         }
1895         create_player_appearance_effect(&Objects[objnum]);
1896         multi_make_player_ghost(buf[1]);
1897 }
1898
1899 void
1900 multi_do_remobj(char *buf)
1901 {
1902         short objnum; // which object to remove
1903         short local_objnum;
1904         sbyte obj_owner; // which remote list is it entered in
1905
1906         GET_INTEL_SHORT(objnum, buf+1);
1907         obj_owner = buf[3];
1908
1909         Assert(objnum >= 0);
1910
1911         if (objnum < 1)
1912                 return;
1913
1914         local_objnum = objnum_remote_to_local(objnum, obj_owner); // translate to local objnum
1915
1916         //      mprintf((0, "multi_do_remobj: %d owner %d = %d.\n", objnum, obj_owner, local_objnum));
1917
1918         if (local_objnum < 0)
1919         {
1920                 mprintf((1, "multi_do_remobj: Could not remove referenced object.\n"));
1921                 return;
1922         }
1923
1924         if ((Objects[local_objnum].type != OBJ_POWERUP) && (Objects[local_objnum].type != OBJ_HOSTAGE))
1925         {
1926                 mprintf((1, "multi_get_remobj: tried to remove invalid type %d.\n", Objects[local_objnum].type));
1927                 return;
1928         }
1929
1930         if (Network_send_objects && network_objnum_is_past(local_objnum))
1931         {
1932                 mprintf((0, "Resetting object sync due to object removal.\n"));
1933                 Network_send_objnum = -1;
1934         }
1935         if (Objects[local_objnum].type==OBJ_POWERUP)
1936                 if (Game_mode & GM_NETWORK)
1937                 {
1938                         if (PowerupsInMine[Objects[local_objnum].id]>0)
1939                                 PowerupsInMine[Objects[local_objnum].id]--;
1940
1941                         if (multi_powerup_is_4pack (Objects[local_objnum].id))
1942                         {
1943                                 mprintf ((0,"Hey babe! Doing that wacky 4 pack stuff."));
1944
1945                                 if (PowerupsInMine[Objects[local_objnum].id-1]-4<0)
1946                                         PowerupsInMine[Objects[local_objnum].id-1]=0;
1947                                 else
1948                                         PowerupsInMine[Objects[local_objnum].id-1]-=4;
1949                         }
1950
1951                         mprintf ((0,"Decrementing powerups! %d\n",PowerupsInMine[Objects[local_objnum].id]));
1952                 }
1953
1954         Objects[local_objnum].flags |= OF_SHOULD_BE_DEAD; // quick and painless
1955
1956 }
1957
1958 void
1959 multi_do_quit(char *buf)
1960 {
1961
1962         if (Game_mode & GM_NETWORK)
1963         {
1964                 int i, n = 0;
1965
1966                 digi_play_sample( SOUND_HUD_MESSAGE, F1_0 );
1967
1968                 HUD_init_message( "%s %s", Players[(int)buf[1]].callsign, TXT_HAS_LEFT_THE_GAME);
1969
1970                 network_disconnect_player(buf[1]);
1971
1972                 if (multi_in_menu)
1973                         return;
1974
1975                 for (i = 0; i < N_players; i++)
1976                         if (Players[i].connected) n++;
1977                 if (n == 1)
1978                 {
1979                         nm_messagebox(NULL, 1, TXT_OK, TXT_YOU_ARE_ONLY);
1980                 }
1981         }
1982
1983         if ((Game_mode & GM_SERIAL) || (Game_mode & GM_MODEM))
1984         {
1985                 Function_mode = FMODE_MENU;
1986                 multi_quit_game = 1;
1987                 multi_leave_menu = 1;
1988                 nm_messagebox(NULL, 1, TXT_OK, TXT_OPPONENT_LEFT);
1989                 Function_mode = FMODE_GAME;
1990                 multi_reset_stuff();
1991         }
1992         return;
1993 }
1994
1995 void
1996 multi_do_cloak(char *buf)
1997 {
1998         int pnum;
1999
2000         pnum = (int)(buf[1]);
2001
2002         Assert(pnum < N_players);
2003
2004         mprintf((0, "Cloaking player %d\n", pnum));
2005
2006         Players[pnum].flags |= PLAYER_FLAGS_CLOAKED;
2007         Players[pnum].cloak_time = GameTime;
2008         ai_do_cloak_stuff();
2009
2010 #ifndef SHAREWARE
2011         if (Game_mode & GM_MULTI_ROBOTS)
2012                 multi_strip_robots(pnum);
2013 #endif
2014
2015         if (Newdemo_state == ND_STATE_RECORDING)
2016                 newdemo_record_multi_cloak(pnum);
2017 }
2018
2019 void
2020 multi_do_decloak(char *buf)
2021 {
2022         int pnum;
2023
2024         pnum = (int)(buf[1]);
2025
2026         if (Newdemo_state == ND_STATE_RECORDING)
2027                 newdemo_record_multi_decloak(pnum);
2028
2029 }
2030
2031 void
2032 multi_do_door_open(char *buf)
2033 {
2034         int segnum;
2035         sbyte side;
2036         segment *seg;
2037         wall *w;
2038         ubyte flag;
2039
2040         GET_INTEL_SHORT(segnum, buf+1);
2041         side = buf[3];
2042         flag= buf[4];
2043
2044         //      mprintf((0, "Opening door on side %d of segment # %d.\n", side, segnum));
2045
2046         if ((segnum < 0) || (segnum > Highest_segment_index) || (side < 0) || (side > 5))
2047         {
2048                 Int3();
2049                 return;
2050         }
2051
2052         seg = &Segments[segnum];
2053
2054         if (seg->sides[side].wall_num == -1) {  //Opening door on illegal wall
2055                 Int3();
2056                 return;
2057         }
2058
2059         w = &Walls[seg->sides[side].wall_num];
2060
2061         if (w->type == WALL_BLASTABLE)
2062         {
2063                 if (!(w->flags & WALL_BLASTED))
2064                 {
2065                         mprintf((0, "Blasting wall by remote command.\n"));
2066                         wall_destroy(seg, side);
2067                 }
2068                 return;
2069         }
2070         else if (w->state != WALL_DOOR_OPENING)
2071         {
2072                 wall_open_door(seg, side);
2073                 w->flags=flag;
2074         }
2075         else
2076                 w->flags=flag;
2077
2078         //      else
2079         //              mprintf((0, "Door already opening!\n"));
2080 }
2081
2082 void
2083 multi_do_create_explosion(char *buf)
2084 {
2085         int pnum;
2086         int count = 1;
2087
2088         pnum = buf[count++];
2089
2090         //      mprintf((0, "Creating small fireball.\n"));
2091         create_small_fireball_on_object(&Objects[Players[pnum].objnum], F1_0, 1);
2092 }
2093
2094 void
2095 multi_do_controlcen_fire(char *buf)
2096 {
2097         vms_vector to_target;
2098         char gun_num;
2099         short objnum;
2100         int count = 1;
2101
2102         memcpy(&to_target, buf+count, 12);          count += 12;
2103 #ifdef WORDS_BIGENDIAN  // swap the vector to_target
2104         to_target.x = (fix)INTEL_INT((int)to_target.x);
2105         to_target.y = (fix)INTEL_INT((int)to_target.y);
2106         to_target.z = (fix)INTEL_INT((int)to_target.z);
2107 #endif
2108         gun_num = buf[count];                       count += 1;
2109         GET_INTEL_SHORT(objnum, buf+count);         count += 2;
2110
2111         Laser_create_new_easy(&to_target, &Gun_pos[(int)gun_num], objnum, CONTROLCEN_WEAPON_NUM, 1);
2112 }
2113
2114 void
2115 multi_do_create_powerup(char *buf)
2116 {
2117         short segnum;
2118         short objnum;
2119         int my_objnum;
2120         char pnum;
2121         int count = 1;
2122         vms_vector new_pos;
2123         char powerup_type;
2124
2125         if (Endlevel_sequence || Control_center_destroyed)
2126                 return;
2127
2128         pnum = buf[count++];
2129         powerup_type = buf[count++];
2130         GET_INTEL_SHORT(segnum, buf+count); count+=2;
2131         GET_INTEL_SHORT(objnum, buf+count); count+=2;
2132
2133         if ((segnum < 0) || (segnum > Highest_segment_index)) {
2134                 Int3();
2135                 return;
2136         }
2137
2138         memcpy(&new_pos, buf+count, sizeof(vms_vector)); count+=sizeof(vms_vector);
2139 #ifdef WORDS_BIGENDIAN
2140         new_pos.x = (fix)SWAPINT((int)new_pos.x);
2141         new_pos.y = (fix)SWAPINT((int)new_pos.y);
2142         new_pos.z = (fix)SWAPINT((int)new_pos.z);
2143 #endif
2144
2145         Net_create_loc = 0;
2146         my_objnum = call_object_create_egg(&Objects[Players[(int)pnum].objnum], 1, OBJ_POWERUP, powerup_type);
2147
2148         if (my_objnum < 0) {
2149                 mprintf((0, "Could not create new powerup!\n"));
2150                 return;
2151         }
2152
2153         if (Network_send_objects && network_objnum_is_past(my_objnum))
2154         {
2155                 mprintf((0, "Resetting object sync due to powerup creation.\n"));
2156                 Network_send_objnum = -1;
2157         }
2158
2159         Objects[my_objnum].pos = new_pos;
2160
2161         vm_vec_zero(&Objects[my_objnum].mtype.phys_info.velocity);
2162
2163         obj_relink(my_objnum, segnum);
2164
2165         map_objnum_local_to_remote(my_objnum, objnum, pnum);
2166
2167         object_create_explosion(segnum, &new_pos, i2f(5), VCLIP_POWERUP_DISAPPEARANCE);
2168         mprintf((0, "Creating powerup type %d in segment %i.\n", powerup_type, segnum));
2169
2170         if (Game_mode & GM_NETWORK)
2171                 PowerupsInMine[(int)powerup_type]++;
2172 }
2173
2174 void
2175 multi_do_play_sound(char *buf)
2176 {
2177         int pnum = (int)(buf[1]);
2178         int sound_num = (int)(buf[2]);
2179         fix volume = (int)(buf[3]) << 12;
2180
2181         if (!Players[pnum].connected)
2182                 return;
2183
2184         Assert(Players[pnum].objnum >= 0);
2185         Assert(Players[pnum].objnum <= Highest_object_index);
2186
2187         digi_link_sound_to_object( sound_num, Players[pnum].objnum, 0, volume);
2188 }
2189
2190 void
2191 multi_do_score(char *buf)
2192 {
2193         int pnum = (int)(buf[1]);
2194
2195         if ((pnum < 0) || (pnum >= N_players))
2196         {
2197                 Int3(); // Non-terminal, see rob
2198                 return;
2199         }
2200
2201         if (Newdemo_state == ND_STATE_RECORDING) {
2202                 int score;
2203                 GET_INTEL_INT(score, buf+2);
2204                 newdemo_record_multi_score(pnum, score);
2205         }
2206
2207         GET_INTEL_INT(Players[pnum].score, buf+2);
2208
2209         multi_sort_kill_list();
2210 }
2211
2212 void
2213 multi_do_trigger(char *buf)
2214 {
2215         int pnum = (int)(buf[1]);
2216         int trigger = (int)(buf[2]);
2217
2218         mprintf ((0,"MULTI doing trigger!\n"));
2219
2220         if ((pnum < 0) || (pnum >= N_players) || (pnum == Player_num))
2221         {
2222                 Int3(); // Got trigger from illegal playernum
2223                 return;
2224         }
2225         if ((trigger < 0) || (trigger >= Num_triggers))
2226         {
2227                 Int3(); // Illegal trigger number in multiplayer
2228                 return;
2229         }
2230         check_trigger_sub(trigger, pnum,0);
2231 }
2232
2233 void multi_do_drop_marker (char *buf)
2234 {
2235         int i;
2236         int pnum=(int)(buf[1]);
2237         int mesnum=(int)(buf[2]);
2238         vms_vector position;
2239
2240         if (pnum==Player_num)  // my marker? don't set it down cuz it might screw up the orientation
2241                 return;
2242
2243         GET_INTEL_INT(position.x, buf+3);
2244         GET_INTEL_INT(position.y, buf+7);
2245         GET_INTEL_INT(position.z, buf+11);
2246
2247         for (i=0;i<40;i++)
2248                 MarkerMessage[(pnum*2)+mesnum][i]=buf[15+i];
2249
2250         MarkerPoint[(pnum*2)+mesnum]=position;
2251
2252         if (MarkerObject[(pnum*2)+mesnum] !=-1 && Objects[MarkerObject[(pnum*2)+mesnum]].type!=OBJ_NONE && MarkerObject[(pnum*2)+mesnum] !=0)
2253                 obj_delete(MarkerObject[(pnum*2)+mesnum]);
2254
2255         MarkerObject[(pnum*2)+mesnum] = drop_marker_object(&position,Objects[Players[Player_num].objnum].segnum,&Objects[Players[Player_num].objnum].orient,(pnum*2)+mesnum);
2256         strcpy (MarkerOwner[(pnum*2)+mesnum],Players[pnum].callsign);
2257         mprintf ((0,"Dropped player %d message: %s\n",pnum,MarkerMessage[(pnum*2)+mesnum]));
2258 }
2259
2260
2261 void multi_do_hostage_door_status(char *buf)
2262 {
2263         // Update hit point status of a door
2264
2265         int count = 1;
2266         int wallnum;
2267         fix hps;
2268
2269         GET_INTEL_SHORT(wallnum, buf+count);        count += 2;
2270         GET_INTEL_INT(hps, buf+count);              count += 4;
2271
2272         if ((wallnum < 0) || (wallnum > Num_walls) || (hps < 0) || (Walls[wallnum].type != WALL_BLASTABLE))
2273         {
2274                 Int3(); // Non-terminal, see Rob
2275                 return;
2276         }
2277
2278         //      mprintf((0, "Damaging wall number %d to %f points.\n", wallnum, f2fl(hps)));
2279
2280         if (hps < Walls[wallnum].hps)
2281                 wall_damage(&Segments[Walls[wallnum].segnum], Walls[wallnum].sidenum, Walls[wallnum].hps - hps);
2282 }
2283
2284 void multi_do_save_game(char *buf)
2285 {
2286         int count = 1;
2287         ubyte slot;
2288         uint id;
2289         char desc[25];
2290
2291         slot = *(ubyte *)(buf+count);           count += 1;
2292         GET_INTEL_INT(id, buf+count);           count += 4;
2293         memcpy( desc, &buf[count], 20 );        count += 20;
2294
2295         multi_save_game( slot, id, desc );
2296 }
2297
2298 void multi_do_restore_game(char *buf)
2299 {
2300         int count = 1;
2301         ubyte slot;
2302         uint id;
2303
2304         slot = *(ubyte *)(buf+count);           count += 1;
2305         GET_INTEL_INT(id, buf+count);           count += 4;
2306
2307         multi_restore_game( slot, id );
2308 }
2309
2310
2311 void multi_do_req_player(char *buf)
2312 {
2313         netplayer_stats ps;
2314         ubyte player_n;
2315         // Send my netplayer_stats to everyone!
2316         player_n = *(ubyte *)(buf+1);
2317         if ( (player_n == Player_num) || (player_n == 255)  )   {
2318                 extract_netplayer_stats( &ps, &Players[Player_num] );
2319                 ps.Player_num = Player_num;
2320                 ps.message_type = MULTI_SEND_PLAYER;            // SET
2321                 multi_send_data((ubyte*)&ps, sizeof(netplayer_stats), 0);
2322         }
2323 }
2324
2325 void multi_do_send_player(char *buf)
2326 {
2327         // Got a player packet from someone!!!
2328         netplayer_stats * p;
2329         p = (netplayer_stats *)buf;
2330
2331         Assert( p->Player_num <= N_players );
2332
2333         mprintf(( 0, "Got netplayer_stats for player %d (I'm %d)\n", p->Player_num, Player_num ));
2334         mprintf(( 0, "Their shields are: %d\n", f2i(p->shields) ));
2335
2336         use_netplayer_stats( &Players[p->Player_num], p );
2337 }
2338
2339 void
2340 multi_reset_stuff(void)
2341 {
2342         // A generic, emergency function to solve problems that crop up
2343         // when a player exits quick-out from the game because of a
2344         // serial connection loss.  Fixes several weird bugs!
2345
2346         dead_player_end();
2347
2348         Players[Player_num].homing_object_dist = -F1_0; // Turn off homing sound.
2349
2350         Dead_player_camera = 0;
2351         Endlevel_sequence = 0;
2352         reset_rear_view();
2353 }
2354
2355 void
2356 multi_reset_player_object(object *objp)
2357 {
2358         int i;
2359
2360         //Init physics for a non-console player
2361
2362         Assert(objp >= Objects);
2363         Assert(objp <= Objects+Highest_object_index);
2364         Assert((objp->type == OBJ_PLAYER) || (objp->type == OBJ_GHOST));
2365
2366         vm_vec_zero(&objp->mtype.phys_info.velocity);
2367         vm_vec_zero(&objp->mtype.phys_info.thrust);
2368         vm_vec_zero(&objp->mtype.phys_info.rotvel);
2369         vm_vec_zero(&objp->mtype.phys_info.rotthrust);
2370         objp->mtype.phys_info.brakes = objp->mtype.phys_info.turnroll = 0;
2371         objp->mtype.phys_info.mass = Player_ship->mass;
2372         objp->mtype.phys_info.drag = Player_ship->drag;
2373         //      objp->mtype.phys_info.flags &= ~(PF_TURNROLL | PF_LEVELLING | PF_WIGGLE | PF_USES_THRUST);
2374         objp->mtype.phys_info.flags &= ~(PF_TURNROLL | PF_LEVELLING | PF_WIGGLE);
2375
2376         //Init render info
2377
2378         objp->render_type = RT_POLYOBJ;
2379         objp->rtype.pobj_info.model_num = Player_ship->model_num;               //what model is this?
2380         objp->rtype.pobj_info.subobj_flags = 0;         //zero the flags
2381         for (i=0;i<MAX_SUBMODELS;i++)
2382                 vm_angvec_zero(&objp->rtype.pobj_info.anim_angles[i]);
2383
2384         //reset textures for this, if not player 0
2385
2386         multi_reset_object_texture (objp);
2387
2388         // Clear misc
2389
2390         objp->flags = 0;
2391
2392         if (objp->type == OBJ_GHOST)
2393                 objp->render_type = RT_NONE;
2394
2395 }
2396
2397 void multi_reset_object_texture (object *objp)
2398 {
2399         int id,i;
2400
2401         if (Game_mode & GM_TEAM)
2402                 id = get_team(objp->id);
2403         else
2404                 id = objp->id;
2405
2406         if (id == 0)
2407                 objp->rtype.pobj_info.alt_textures=0;
2408         else {
2409                 Assert(N_PLAYER_SHIP_TEXTURES == Polygon_models[objp->rtype.pobj_info.model_num].n_textures);
2410
2411                 for (i=0;i<N_PLAYER_SHIP_TEXTURES;i++)
2412                         multi_player_textures[id-1][i] = ObjBitmaps[ObjBitmapPtrs[Polygon_models[objp->rtype.pobj_info.model_num].first_texture+i]];
2413
2414                 multi_player_textures[id-1][4] = ObjBitmaps[ObjBitmapPtrs[First_multi_bitmap_num+(id-1)*2]];
2415                 multi_player_textures[id-1][5] = ObjBitmaps[ObjBitmapPtrs[First_multi_bitmap_num+(id-1)*2+1]];
2416
2417                 objp->rtype.pobj_info.alt_textures = id;
2418         }
2419 }
2420
2421
2422
2423
2424 #ifdef NETPROFILING
2425 extern int TTRecv[];
2426 extern FILE *RecieveLogFile;
2427 #endif
2428
2429 void
2430 multi_process_bigdata(char *buf, int len)
2431 {
2432         // Takes a bunch of messages, check them for validity,
2433         // and pass them to multi_process_data.
2434
2435         int type, sub_len, bytes_processed = 0;
2436
2437         while( bytes_processed < len )  {
2438                 type = buf[bytes_processed];
2439
2440                 if ( (type<0) || (type>MULTI_MAX_TYPE)) {
2441                         mprintf( (1, "multi_process_bigdata: Invalid packet type %d!\n", type ));
2442                         return;
2443                 }
2444                 sub_len = message_length[type];
2445
2446                 Assert(sub_len > 0);
2447
2448                 if ( (bytes_processed+sub_len) > len )  {
2449                         mprintf( (1, "multi_process_bigdata: packet type %d too short (%d>%d)!\n", type, (bytes_processed+sub_len), len ));
2450                         Int3();
2451                         return;
2452                 }
2453
2454                 multi_process_data(&buf[bytes_processed], sub_len);
2455                 bytes_processed += sub_len;
2456         }
2457 }
2458
2459 //
2460 // Part 2 : Functions that send communication messages to inform the other
2461 //          players of something we did.
2462 //
2463
2464 void
2465 multi_send_fire(void)
2466 {
2467         if (!Network_laser_fired)
2468                 return;
2469
2470         multibuf[0] = (char)MULTI_FIRE;
2471         multibuf[1] = (char)Player_num;
2472         multibuf[2] = (char)Network_laser_gun;
2473         multibuf[3] = (char)Network_laser_level;
2474         multibuf[4] = (char)Network_laser_flags;
2475         multibuf[5] = (char)Network_laser_fired;
2476
2477         PUT_INTEL_SHORT(multibuf+6, Network_laser_track);
2478
2479         multi_send_data(multibuf, 8, 0);
2480
2481         Network_laser_fired = 0;
2482 }
2483
2484 void
2485 multi_send_destroy_controlcen(int objnum, int player)
2486 {
2487         if (player == Player_num)
2488                 HUD_init_message(TXT_YOU_DEST_CONTROL);
2489         else if ((player > 0) && (player < N_players))
2490                 HUD_init_message("%s %s", Players[player].callsign, TXT_HAS_DEST_CONTROL);
2491         else
2492                 HUD_init_message(TXT_CONTROL_DESTROYED);
2493
2494         multibuf[0] = (char)MULTI_CONTROLCEN;
2495         PUT_INTEL_SHORT(multibuf+1, objnum);
2496         multibuf[3] = player;
2497         multi_send_data(multibuf, 4, 2);
2498 }
2499
2500 void multi_send_drop_marker (int player,vms_vector position,char messagenum,char text[])
2501 {
2502         int i;
2503
2504         if (player<N_players)
2505         {
2506                 mprintf ((0,"Sending MARKER drop!\n"));
2507                 multibuf[0]=(char)MULTI_MARKER;
2508                 multibuf[1]=(char)player;
2509                 multibuf[2]=messagenum;
2510                 PUT_INTEL_INT(multibuf+3, position.x);
2511                 PUT_INTEL_INT(multibuf+7, position.y);
2512                 PUT_INTEL_INT(multibuf+11, position.z);
2513                 for (i=0;i<40;i++)
2514                         multibuf[15+i]=text[i];
2515         }
2516         multi_send_data(multibuf, 55, 1);
2517 }
2518
2519 void
2520 multi_send_endlevel_start(int secret)
2521 {
2522         multibuf[0] = (char)MULTI_ENDLEVEL_START;
2523         multibuf[1] = Player_num;
2524         multibuf[2] = (char)secret;
2525
2526         if ((secret) && !multi_goto_secret)
2527                 multi_goto_secret = 1;
2528         else if (!multi_goto_secret)
2529                 multi_goto_secret = 2;
2530
2531         multi_send_data(multibuf, 3, 1);
2532         if (Game_mode & GM_NETWORK)
2533         {
2534                 Players[Player_num].connected = 5;
2535                 network_send_endlevel_packet();
2536         }
2537 }
2538
2539 void
2540 multi_send_player_explode(char type)
2541 {
2542         int count = 0;
2543         int i;
2544
2545         Assert( (type == MULTI_PLAYER_DROP) || (type == MULTI_PLAYER_EXPLODE) );
2546
2547         multi_send_position(Players[Player_num].objnum);
2548
2549         if (Network_send_objects)
2550         {
2551                 mprintf((0, "Resetting object sync due to player explosion.\n"));
2552                 Network_send_objnum = -1;
2553         }
2554
2555         multibuf[count++] = type;
2556         multibuf[count++] = Player_num;
2557
2558         PUT_INTEL_SHORT(multibuf+count, Players[Player_num].primary_weapon_flags);
2559         count += 2;
2560         PUT_INTEL_SHORT(multibuf+count, Players[Player_num].secondary_weapon_flags);
2561         count += 2;
2562         multibuf[count++] = (char)Players[Player_num].laser_level;
2563
2564         multibuf[count++] = (char)Players[Player_num].secondary_ammo[HOMING_INDEX];
2565         multibuf[count++] = (char)Players[Player_num].secondary_ammo[CONCUSSION_INDEX];
2566         multibuf[count++] = (char)Players[Player_num].secondary_ammo[SMART_INDEX];
2567         multibuf[count++] = (char)Players[Player_num].secondary_ammo[MEGA_INDEX];
2568         multibuf[count++] = (char)Players[Player_num].secondary_ammo[PROXIMITY_INDEX];
2569
2570         multibuf[count++] = (char)Players[Player_num].secondary_ammo[SMISSILE1_INDEX];
2571         multibuf[count++] = (char)Players[Player_num].secondary_ammo[GUIDED_INDEX];
2572         multibuf[count++] = (char)Players[Player_num].secondary_ammo[SMART_MINE_INDEX];
2573         multibuf[count++] = (char)Players[Player_num].secondary_ammo[SMISSILE4_INDEX];
2574         multibuf[count++] = (char)Players[Player_num].secondary_ammo[SMISSILE5_INDEX];
2575
2576         PUT_INTEL_SHORT(multibuf+count, Players[Player_num].primary_ammo[VULCAN_INDEX] );
2577         count += 2;
2578         PUT_INTEL_SHORT(multibuf+count, Players[Player_num].primary_ammo[GAUSS_INDEX] );
2579         count += 2;
2580         PUT_INTEL_INT(multibuf+count, Players[Player_num].flags );
2581         count += 4;
2582
2583         multibuf[count++] = Net_create_loc;
2584
2585         Assert(Net_create_loc <= MAX_NET_CREATE_OBJECTS);
2586
2587         memset(multibuf+count, -1, MAX_NET_CREATE_OBJECTS*sizeof(short));
2588
2589         mprintf((0, "Created %d explosion objects.\n", Net_create_loc));
2590
2591         for (i = 0; i < Net_create_loc; i++)
2592         {
2593                 if (Net_create_objnums[i] <= 0) {
2594                         Int3(); // Illegal value in created egg object numbers
2595                         count +=2;
2596                         continue;
2597                 }
2598
2599                 PUT_INTEL_SHORT(multibuf+count, Net_create_objnums[i]); count += 2;
2600
2601                 // We created these objs so our local number = the network number
2602                 map_objnum_local_to_local((short)Net_create_objnums[i]);
2603         }
2604
2605         Net_create_loc = 0;
2606
2607         //      mprintf((1, "explode message size = %d, max = %d.\n", count, message_length[MULTI_PLAYER_EXPLODE]));
2608
2609         if (count > message_length[MULTI_PLAYER_EXPLODE])
2610         {
2611                 Int3(); // See Rob
2612         }
2613
2614         multi_send_data(multibuf, message_length[MULTI_PLAYER_EXPLODE], 2);
2615         if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED)
2616                 multi_send_decloak();
2617         if (Game_mode & GM_MULTI_ROBOTS)
2618                 multi_strip_robots(Player_num);
2619 }
2620
2621 extern ubyte Secondary_weapon_to_powerup[];
2622 extern ubyte Primary_weapon_to_powerup[];
2623
2624 // put a lid on how many objects will be spewed by an exploding player
2625 // to prevent rampant powerups in netgames
2626
2627 void multi_cap_objects ()
2628 {
2629         char type,flagtype;
2630         int index;
2631
2632         if (!(Game_mode & GM_NETWORK))
2633                 return;
2634
2635         for (index=0;index<MAX_PRIMARY_WEAPONS;index++)
2636         {
2637                 type=Primary_weapon_to_powerup[index];
2638                 if (PowerupsInMine[(int)type]>=MaxPowerupsAllowed[(int)type])
2639                         if(Players[Player_num].primary_weapon_flags & (1 << index))
2640                         {
2641                                 mprintf ((0,"PIM=%d MPA=%d\n",PowerupsInMine[(int)type],MaxPowerupsAllowed[(int)type]));
2642                                 mprintf ((0,"Killing a primary cuz there's too many! (%d)\n",(int)type));
2643                                 Players[Player_num].primary_weapon_flags&=(~(1 << index));
2644                         }
2645         }
2646
2647
2648         // Don't do the adjustment stuff for Hoard mode
2649         if (!(Game_mode & GM_HOARD))
2650                 Players[Player_num].secondary_ammo[2]/=4;
2651
2652         Players[Player_num].secondary_ammo[7]/=4;
2653
2654         for (index=0;index<MAX_SECONDARY_WEAPONS;index++)
2655         {
2656                 if ((Game_mode & GM_HOARD) && index==PROXIMITY_INDEX)
2657                         continue;
2658
2659                 type=Secondary_weapon_to_powerup[index];
2660
2661                 if ((Players[Player_num].secondary_ammo[index]+PowerupsInMine[(int)type])>MaxPowerupsAllowed[(int)type])
2662                 {
2663                         if (MaxPowerupsAllowed[(int)type]-PowerupsInMine[(int)type]<0)
2664                                 Players[Player_num].secondary_ammo[index]=0;
2665                         else
2666                                 Players[Player_num].secondary_ammo[index]=(MaxPowerupsAllowed[(int)type]-PowerupsInMine[(int)type]);
2667
2668                         mprintf ((0,"Hey! I killed secondary type %d because PIM=%d MPA=%d\n",(int)type,PowerupsInMine[(int)type],MaxPowerupsAllowed[(int)type]));
2669                 }
2670         }
2671
2672         if (!(Game_mode & GM_HOARD))
2673                 Players[Player_num].secondary_ammo[2]*=4;
2674         Players[Player_num].secondary_ammo[7]*=4;
2675
2676         if (Players[Player_num].laser_level > MAX_LASER_LEVEL)
2677                 if (PowerupsInMine[POW_SUPER_LASER]+1 > MaxPowerupsAllowed[POW_SUPER_LASER])
2678                         Players[Player_num].laser_level=0;
2679
2680         if (Players[Player_num].flags & PLAYER_FLAGS_QUAD_LASERS)
2681                 if (PowerupsInMine[POW_QUAD_FIRE]+1 > MaxPowerupsAllowed[POW_QUAD_FIRE])
2682                         Players[Player_num].flags&=(~PLAYER_FLAGS_QUAD_LASERS);
2683
2684         if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED)
2685                 if (PowerupsInMine[POW_CLOAK]+1 > MaxPowerupsAllowed[POW_CLOAK])
2686                         Players[Player_num].flags&=(~PLAYER_FLAGS_CLOAKED);
2687
2688         if (Players[Player_num].flags & PLAYER_FLAGS_MAP_ALL)
2689                 if (PowerupsInMine[POW_FULL_MAP]+1 > MaxPowerupsAllowed[POW_FULL_MAP])
2690                         Players[Player_num].flags&=(~PLAYER_FLAGS_MAP_ALL);
2691
2692         if (Players[Player_num].flags & PLAYER_FLAGS_AFTERBURNER)
2693                 if (PowerupsInMine[POW_AFTERBURNER]+1 > MaxPowerupsAllowed[POW_AFTERBURNER])
2694                         Players[Player_num].flags&=(~PLAYER_FLAGS_AFTERBURNER);
2695
2696         if (Players[Player_num].flags & PLAYER_FLAGS_AMMO_RACK)
2697                 if (PowerupsInMine[POW_AMMO_RACK]+1 > MaxPowerupsAllowed[POW_AMMO_RACK])
2698                         Players[Player_num].flags&=(~PLAYER_FLAGS_AMMO_RACK);
2699
2700         if (Players[Player_num].flags & PLAYER_FLAGS_CONVERTER)
2701                 if (PowerupsInMine[POW_CONVERTER]+1 > MaxPowerupsAllowed[POW_CONVERTER])
2702                         Players[Player_num].flags&=(~PLAYER_FLAGS_CONVERTER);
2703
2704         if (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT)
2705                 if (PowerupsInMine[POW_HEADLIGHT]+1 > MaxPowerupsAllowed[POW_HEADLIGHT])
2706                         Players[Player_num].flags&=(~PLAYER_FLAGS_HEADLIGHT);
2707
2708         if (Game_mode & GM_CAPTURE)
2709         {
2710                 if (Players[Player_num].flags & PLAYER_FLAGS_FLAG)
2711                 {
2712                         if (get_team(Player_num)==TEAM_RED)
2713                                 flagtype=POW_FLAG_BLUE;
2714                         else
2715                                 flagtype=POW_FLAG_RED;
2716
2717                         if (PowerupsInMine[(int)flagtype]+1 > MaxPowerupsAllowed[(int)flagtype])
2718                                 Players[Player_num].flags&=(~PLAYER_FLAGS_FLAG);
2719                 }
2720         }
2721
2722 }
2723
2724 // adds players inventory to multi cap
2725
2726 void multi_adjust_cap_for_player (int pnum)
2727 {
2728         char type;
2729
2730         int index;
2731
2732         if (!(Game_mode & GM_NETWORK))
2733                 return;
2734
2735         for (index=0;index<MAX_PRIMARY_WEAPONS;index++)
2736         {
2737                 type=Primary_weapon_to_powerup[index];
2738                 if (Players[pnum].primary_weapon_flags & (1 << index))
2739                     MaxPowerupsAllowed[(int)type]++;
2740         }
2741
2742         for (index=0;index<MAX_SECONDARY_WEAPONS;index++)
2743         {
2744                 type=Secondary_weapon_to_powerup[index];
2745                 MaxPowerupsAllowed[(int)type]+=Players[pnum].secondary_ammo[index];
2746         }
2747
2748         if (Players[pnum].laser_level > MAX_LASER_LEVEL)
2749                 MaxPowerupsAllowed[POW_SUPER_LASER]++;
2750
2751         if (Players[pnum].flags & PLAYER_FLAGS_QUAD_LASERS)
2752                 MaxPowerupsAllowed[POW_QUAD_FIRE]++;
2753
2754         if (Players[pnum].flags & PLAYER_FLAGS_CLOAKED)
2755                 MaxPowerupsAllowed[POW_CLOAK]++;
2756
2757         if (Players[pnum].flags & PLAYER_FLAGS_MAP_ALL)
2758                 MaxPowerupsAllowed[POW_FULL_MAP]++;
2759
2760         if (Players[pnum].flags & PLAYER_FLAGS_AFTERBURNER)
2761                 MaxPowerupsAllowed[POW_AFTERBURNER]++;
2762
2763         if (Players[pnum].flags & PLAYER_FLAGS_AMMO_RACK)
2764                 MaxPowerupsAllowed[POW_AMMO_RACK]++;
2765
2766         if (Players[pnum].flags & PLAYER_FLAGS_CONVERTER)
2767                 MaxPowerupsAllowed[POW_CONVERTER]++;
2768
2769         if (Players[pnum].flags & PLAYER_FLAGS_HEADLIGHT)
2770                 MaxPowerupsAllowed[POW_HEADLIGHT]++;
2771 }
2772
2773 void multi_adjust_remote_cap (int pnum)
2774 {
2775         char type;
2776
2777         int index;
2778
2779         if (!(Game_mode & GM_NETWORK))
2780                 return;
2781
2782         for (index=0;index<MAX_PRIMARY_WEAPONS;index++)
2783         {
2784                 type=Primary_weapon_to_powerup[index];
2785                 if (Players[pnum].primary_weapon_flags & (1 << index))
2786                     PowerupsInMine[(int)type]++;
2787         }
2788
2789         for (index=0;index<MAX_SECONDARY_WEAPONS;index++)
2790         {
2791                 type=Secondary_weapon_to_powerup[index];
2792
2793                 if ((Game_mode & GM_HOARD) && index==2)
2794                         continue;
2795
2796                 if (index==2 || index==7) // PROX or SMARTMINES? Those bastards...
2797                         PowerupsInMine[(int)type]+=(Players[pnum].secondary_ammo[index]/4);
2798                 else
2799                         PowerupsInMine[(int)type]+=Players[pnum].secondary_ammo[index];
2800
2801         }
2802
2803         if (Players[pnum].laser_level > MAX_LASER_LEVEL)
2804                 PowerupsInMine[POW_SUPER_LASER]++;
2805
2806         if (Players[pnum].flags & PLAYER_FLAGS_QUAD_LASERS)
2807                 PowerupsInMine[POW_QUAD_FIRE]++;
2808
2809         if (Players[pnum].flags & PLAYER_FLAGS_CLOAKED)
2810                 PowerupsInMine[POW_CLOAK]++;
2811
2812         if (Players[pnum].flags & PLAYER_FLAGS_MAP_ALL)
2813                 PowerupsInMine[POW_FULL_MAP]++;
2814
2815         if (Players[pnum].flags & PLAYER_FLAGS_AFTERBURNER)
2816                 PowerupsInMine[POW_AFTERBURNER]++;
2817
2818         if (Players[pnum].flags & PLAYER_FLAGS_AMMO_RACK)
2819                 PowerupsInMine[POW_AMMO_RACK]++;
2820
2821         if (Players[pnum].flags & PLAYER_FLAGS_CONVERTER)
2822                 PowerupsInMine[POW_CONVERTER]++;
2823
2824         if (Players[pnum].flags & PLAYER_FLAGS_HEADLIGHT)
2825                 PowerupsInMine[POW_HEADLIGHT]++;
2826
2827 }
2828
2829 void
2830 multi_send_message(void)
2831 {
2832         int loc = 0;
2833         if (Network_message_reciever != -1)
2834         {
2835                 multibuf[loc] = (char)MULTI_MESSAGE;            loc += 1;
2836                 multibuf[loc] = (char)Player_num;                       loc += 1;
2837                 strncpy(multibuf+loc, Network_message, MAX_MESSAGE_LEN); loc += MAX_MESSAGE_LEN;
2838                 multibuf[loc-1] = '\0';
2839                 multi_send_data(multibuf, loc, 0);
2840                 Network_message_reciever = -1;
2841         }
2842 }
2843
2844 void
2845 multi_send_reappear()
2846 {
2847         multibuf[0] = (char)MULTI_REAPPEAR;
2848         PUT_INTEL_SHORT(multibuf+1, Players[Player_num].objnum);
2849
2850         multi_send_data(multibuf, 3, 2);
2851         PKilledFlags[Player_num]=0;
2852 }
2853
2854 void
2855 multi_send_position(int objnum)
2856 {
2857 #ifdef WORDS_BIGENDIAN
2858         shortpos sp;
2859 #endif
2860         int count=0;
2861
2862         if (Game_mode & GM_NETWORK) {
2863                 return;
2864         }
2865
2866         multibuf[count++] = (char)MULTI_POSITION;
2867 #ifndef WORDS_BIGENDIAN
2868         create_shortpos((shortpos *)(multibuf+count), Objects+objnum,0);
2869         count += sizeof(shortpos);
2870 #else
2871         create_shortpos(&sp, Objects+objnum, 1);
2872         memcpy(&(multibuf[count]), (ubyte *)(sp.bytemat), 9);
2873         count += 9;
2874         memcpy(&(multibuf[count]), (ubyte *)&(sp.xo), 14);
2875         count += 14;
2876 #endif
2877
2878         multi_send_data(multibuf, count, 0);
2879 }
2880
2881 void
2882 multi_send_kill(int objnum)
2883 {
2884         // I died, tell the world.
2885
2886         int killer_objnum;
2887         int count = 0;
2888
2889         Assert(Objects[objnum].id == Player_num);
2890         killer_objnum = Players[Player_num].killer_objnum;
2891
2892         multi_compute_kill(killer_objnum, objnum);
2893
2894         multibuf[0] = (char)MULTI_KILL;     count += 1;
2895         multibuf[1] = Player_num;           count += 1;
2896         if (killer_objnum > -1) {
2897                 short s;                // do it with variable since INTEL_SHORT won't work on return val from function.
2898
2899                 s = (short)objnum_local_to_remote(killer_objnum, (sbyte *)&multibuf[count+2]);
2900                 PUT_INTEL_SHORT(multibuf+count, s);
2901         }
2902         else
2903         {
2904                 PUT_INTEL_SHORT(multibuf+count, -1);
2905                 multibuf[count+2] = (char)-1;
2906         }
2907         count += 3;
2908         multi_send_data(multibuf, count, 1);
2909
2910 #ifndef SHAREWARE
2911         if (Game_mode & GM_MULTI_ROBOTS)
2912                 multi_strip_robots(Player_num);
2913 #endif
2914 }
2915
2916 void
2917 multi_send_remobj(int objnum)
2918 {
2919         // Tell the other guy to remove an object from his list
2920
2921         sbyte obj_owner;
2922         short remote_objnum;
2923
2924         if (Objects[objnum].type==OBJ_POWERUP && (Game_mode & GM_NETWORK))
2925     {
2926                 if (PowerupsInMine[Objects[objnum].id] > 0)
2927                 {
2928                         PowerupsInMine[Objects[objnum].id]--;
2929                         if (multi_powerup_is_4pack (Objects[objnum].id))
2930                         {
2931                                 mprintf ((0,"Hey babe! Doing that wacky 4 pack stuff."));
2932
2933                                 if (PowerupsInMine[Objects[objnum].id-1]-4<0)
2934                                         PowerupsInMine[Objects[objnum].id-1]=0;
2935                                 else
2936                                         PowerupsInMine[Objects[objnum].id-1]-=4;
2937                         }
2938                 }
2939
2940         }
2941
2942         multibuf[0] = (char)MULTI_REMOVE_OBJECT;
2943
2944         remote_objnum = objnum_local_to_remote((short)objnum, &obj_owner);
2945
2946         PUT_INTEL_SHORT(multibuf+1, remote_objnum); // Map to network objnums
2947
2948         multibuf[3] = obj_owner;
2949
2950         //      mprintf((0, "multi_send_remobj: %d = %d owner %d.\n", objnum, remote_objnum, obj_owner));
2951
2952         multi_send_data(multibuf, 4, 0);
2953
2954         if (Network_send_objects && network_objnum_is_past(objnum))
2955         {
2956                 mprintf((0, "Resetting object sync due to object removal.\n"));
2957                 Network_send_objnum = -1;
2958         }
2959 }
2960
2961 void
2962 multi_send_quit(int why)
2963 {
2964         // I am quitting the game, tell the other guy the bad news.
2965
2966         Assert (why == MULTI_QUIT);
2967
2968         multibuf[0] = (char)why;
2969         multibuf[1] = Player_num;
2970         multi_send_data(multibuf, 2, 1);
2971
2972 }
2973
2974 void
2975 multi_send_cloak(void)
2976 {
2977         // Broadcast a change in our pflags (made to support cloaking)
2978
2979         multibuf[0] = MULTI_CLOAK;
2980         multibuf[1] = (char)Player_num;
2981
2982         multi_send_data(multibuf, 2, 1);
2983
2984 #ifndef SHAREWARE
2985         if (Game_mode & GM_MULTI_ROBOTS)
2986                 multi_strip_robots(Player_num);
2987 #endif
2988 }
2989
2990 void
2991 multi_send_decloak(void)
2992 {
2993         // Broadcast a change in our pflags (made to support cloaking)
2994
2995         multibuf[0] = MULTI_DECLOAK;
2996         multibuf[1] = (char)Player_num;
2997
2998         multi_send_data(multibuf, 2, 1);
2999 }
3000
3001 void
3002 multi_send_door_open(int segnum, int side,ubyte flag)
3003 {
3004         // When we open a door make sure everyone else opens that door
3005
3006         multibuf[0] = MULTI_DOOR_OPEN;
3007         PUT_INTEL_SHORT(multibuf+1, segnum );
3008         multibuf[3] = (sbyte)side;
3009         multibuf[4] = flag;
3010
3011         multi_send_data(multibuf, 5, 2);
3012 }
3013
3014 extern void network_send_naked_packet (char *,short,int);
3015
3016 void
3017 multi_send_door_open_specific(int pnum,int segnum, int side,ubyte flag)
3018 {
3019         // For sending doors only to a specific person (usually when they're joining)
3020
3021         Assert (Game_mode & GM_NETWORK);
3022         //   Assert (pnum>-1 && pnum<N_players);
3023
3024         multibuf[0] = MULTI_DOOR_OPEN;
3025         PUT_INTEL_SHORT(multibuf+1, segnum);
3026         multibuf[3] = (sbyte)side;
3027         multibuf[4] = flag;
3028
3029         network_send_naked_packet(multibuf, 5, pnum);
3030 }
3031
3032 //
3033 // Part 3 : Functions that change or prepare the game for multiplayer use.
3034 //          Not including functions needed to syncronize or start the
3035 //          particular type of multiplayer game.  Includes preparing the
3036 //                      mines, player structures, etc.
3037
3038 void
3039 multi_send_create_explosion(int pnum)
3040 {
3041         // Send all data needed to create a remote explosion
3042
3043         int count = 0;
3044
3045         multibuf[count] = MULTI_CREATE_EXPLOSION;       count += 1;
3046         multibuf[count] = (sbyte)pnum;                  count += 1;
3047         //                                                                                                      -----------
3048         //                                                                                                      Total size = 2
3049
3050         multi_send_data(multibuf, count, 0);
3051 }
3052
3053 void
3054 multi_send_controlcen_fire(vms_vector *to_goal, int best_gun_num, int objnum)
3055 {
3056 #ifdef WORDS_BIGENDIAN
3057         vms_vector swapped_vec;
3058 #endif
3059         int count = 0;
3060
3061         multibuf[count] = MULTI_CONTROLCEN_FIRE;                count +=  1;
3062 #ifndef WORDS_BIGENDIAN
3063         memcpy(multibuf+count, to_goal, 12);                    count += 12;
3064 #else
3065         swapped_vec.x = (fix)INTEL_INT( (int)to_goal->x );
3066         swapped_vec.y = (fix)INTEL_INT( (int)to_goal->y );
3067         swapped_vec.z = (fix)INTEL_INT( (int)to_goal->z );
3068         memcpy(multibuf+count, &swapped_vec, 12);                               count += 12;
3069 #endif
3070         multibuf[count] = (char)best_gun_num;                   count +=  1;
3071         PUT_INTEL_SHORT(multibuf+count, objnum );     count +=  2;
3072         //                                                                                                                      ------------
3073         //                                                                                                                      Total  = 16
3074         multi_send_data(multibuf, count, 0);
3075 }
3076
3077 void
3078 multi_send_create_powerup(int powerup_type, int segnum, int objnum, vms_vector *pos)
3079 {
3080         // Create a powerup on a remote machine, used for remote
3081         // placement of used powerups like missiles and cloaking
3082         // powerups.
3083
3084 #ifdef WORDS_BIGENDIAN
3085         vms_vector swapped_vec;
3086 #endif
3087         int count = 0;
3088
3089         if (Game_mode & GM_NETWORK)
3090                 PowerupsInMine[powerup_type]++;
3091
3092         multibuf[count] = MULTI_CREATE_POWERUP;         count += 1;
3093         multibuf[count] = Player_num;                                      count += 1;
3094         multibuf[count] = powerup_type;                                 count += 1;
3095         PUT_INTEL_SHORT(multibuf+count, segnum );     count += 2;
3096         PUT_INTEL_SHORT(multibuf+count, objnum );     count += 2;
3097 #ifndef WORDS_BIGENDIAN
3098         memcpy(multibuf+count, pos, sizeof(vms_vector));  count += sizeof(vms_vector);
3099 #else
3100         swapped_vec.x = (fix)INTEL_INT( (int)pos->x );
3101         swapped_vec.y = (fix)INTEL_INT( (int)pos->y );
3102         swapped_vec.z = (fix)INTEL_INT( (int)pos->z );
3103         memcpy(multibuf+count, &swapped_vec, 12);                               count += 12;
3104 #endif
3105         //                                                                                                            -----------
3106         //                                                                                                            Total =  19
3107         multi_send_data(multibuf, count, 2);
3108
3109         if (Network_send_objects && network_objnum_is_past(objnum))
3110         {
3111                 mprintf((0, "Resetting object sync due to powerup creation.\n"));
3112                 Network_send_objnum = -1;
3113         }
3114
3115         mprintf((0, "Creating powerup type %d in segment %i.\n", powerup_type, segnum));
3116         map_objnum_local_to_local(objnum);
3117 }
3118
3119 void
3120 multi_send_play_sound(int sound_num, fix volume)
3121 {
3122         int count = 0;
3123         multibuf[count] = MULTI_PLAY_SOUND;                     count += 1;
3124         multibuf[count] = Player_num;                                   count += 1;
3125         multibuf[count] = (char)sound_num;                      count += 1;
3126         multibuf[count] = (char)(volume >> 12); count += 1;
3127         //                                                                                                         -----------
3128         //                                                                                                         Total = 4
3129         multi_send_data(multibuf, count, 0);
3130 }
3131
3132 void
3133 multi_send_audio_taunt(int taunt_num)
3134 {
3135         return; // Taken out, awaiting sounds..
3136
3137 #if 0
3138         int audio_taunts[4] = {
3139                 SOUND_CONTROL_CENTER_WARNING_SIREN,
3140                 SOUND_HOSTAGE_RESCUED,
3141                 SOUND_REFUEL_STATION_GIVING_FUEL,
3142                 SOUND_BAD_SELECTION
3143         };
3144
3145
3146         Assert(taunt_num >= 0);
3147         Assert(taunt_num < 4);
3148
3149         digi_play_sample( audio_taunts[taunt_num], F1_0 );
3150         multi_send_play_sound(audio_taunts[taunt_num], F1_0);
3151 #endif
3152 }
3153
3154 void
3155 multi_send_score(void)
3156 {
3157         // Send my current score to all other players so it will remain
3158         // synced.
3159         int count = 0;
3160
3161         if (Game_mode & GM_MULTI_COOP) {
3162                 multi_sort_kill_list();
3163                 multibuf[count] = MULTI_SCORE;                  count += 1;
3164                 multibuf[count] = Player_num;                           count += 1;
3165                 PUT_INTEL_INT(multibuf+count, Players[Player_num].score);  count += 4;
3166                 multi_send_data(multibuf, count, 0);
3167         }
3168 }
3169
3170
3171 void
3172 multi_send_save_game(ubyte slot, uint id, char * desc)
3173 {
3174         int count = 0;
3175
3176         multibuf[count] = MULTI_SAVE_GAME;              count += 1;
3177         multibuf[count] = slot;                         count += 1;    // Save slot=0
3178         PUT_INTEL_INT(multibuf+count, id );           count += 4;             // Save id
3179         memcpy( &multibuf[count], desc, 20 ); count += 20;
3180
3181         multi_send_data(multibuf, count, 2);
3182 }
3183
3184 void
3185 multi_send_restore_game(ubyte slot, uint id)
3186 {
3187         int count = 0;
3188
3189         multibuf[count] = MULTI_RESTORE_GAME;   count += 1;
3190         multibuf[count] = slot;                                                 count += 1;             // Save slot=0
3191         PUT_INTEL_INT(multibuf+count, id);         count += 4;             // Save id
3192
3193         multi_send_data(multibuf, count, 2);
3194 }
3195
3196 void
3197 multi_send_netplayer_stats_request(ubyte player_num)
3198 {
3199         int count = 0;
3200
3201         multibuf[count] = MULTI_REQ_PLAYER;     count += 1;
3202         multibuf[count] = player_num;                   count += 1;
3203
3204         multi_send_data(multibuf, count, 0 );
3205 }
3206
3207 void
3208 multi_send_trigger(int triggernum)
3209 {
3210         // Send an even to trigger something in the mine
3211
3212         int count = 0;
3213
3214         multibuf[count] = MULTI_TRIGGER;                                count += 1;
3215         multibuf[count] = Player_num;                                   count += 1;
3216         multibuf[count] = (ubyte)triggernum;            count += 1;
3217
3218         mprintf ((0,"Sending trigger %d\n",triggernum));
3219
3220         multi_send_data(multibuf, count, 1);
3221         //multi_send_data(multibuf, count, 1); // twice?
3222 }
3223
3224 void
3225 multi_send_hostage_door_status(int wallnum)
3226 {
3227         // Tell the other player what the hit point status of a hostage door
3228         // should be
3229
3230         int count = 0;
3231
3232         Assert(Walls[wallnum].type == WALL_BLASTABLE);
3233
3234         multibuf[count] = MULTI_HOSTAGE_DOOR;           count += 1;
3235         PUT_INTEL_SHORT(multibuf+count, wallnum );           count += 2;
3236         PUT_INTEL_INT(multibuf+count, Walls[wallnum].hps );  count += 4;
3237
3238         //      mprintf((0, "Door %d damaged by %f points.\n", wallnum, f2fl(Walls[wallnum].hps)));
3239
3240         multi_send_data(multibuf, count, 0);
3241 }
3242
3243 extern int ConsistencyCount;
3244 extern int Drop_afterburner_blob_flag;
3245 int PhallicLimit=0;
3246 int PhallicMan=-1;
3247
3248 void multi_prep_level(void)
3249 {
3250         // Do any special stuff to the level required for serial games
3251         // before we begin playing in it.
3252
3253         // Player_num MUST be set before calling this procedure.
3254
3255         // This function must be called before checksuming the Object array,
3256         // since the resulting checksum with depend on the value of Player_num
3257         // at the time this is called.
3258
3259         int i,ng=0;
3260         int     cloak_count, inv_count;
3261
3262         Assert(Game_mode & GM_MULTI);
3263
3264         Assert(NumNetPlayerPositions > 0);
3265
3266         PhallicLimit=0;
3267         PhallicMan=-1;
3268         Drop_afterburner_blob_flag=0;
3269         ConsistencyCount=0;
3270
3271         for (i=0;i<MAX_NUM_NET_PLAYERS;i++)
3272                 PKilledFlags[i]=0;
3273
3274         for (i = 0; i < NumNetPlayerPositions; i++)
3275         {
3276                 if (i != Player_num)
3277                         Objects[Players[i].objnum].control_type = CT_REMOTE;
3278                 Objects[Players[i].objnum].movement_type = MT_PHYSICS;
3279                 multi_reset_player_object(&Objects[Players[i].objnum]);
3280                 LastPacketTime[i] = 0;
3281         }
3282
3283 #ifndef SHAREWARE
3284         for (i = 0; i < MAX_ROBOTS_CONTROLLED; i++)
3285         {
3286                 robot_controlled[i] = -1;
3287                 robot_agitation[i] = 0;
3288                 robot_fired[i] = 0;
3289         }
3290 #endif
3291
3292         Viewer = ConsoleObject = &Objects[Players[Player_num].objnum];
3293
3294         if (!(Game_mode & GM_MULTI_COOP))
3295         {
3296                 multi_delete_extra_objects(); // Removes monsters from level
3297         }
3298
3299         if (Game_mode & GM_MULTI_ROBOTS)
3300         {
3301                 multi_set_robot_ai(); // Set all Robot AI to types we can cope with
3302         }
3303
3304         if (Game_mode & GM_NETWORK)
3305         {
3306                 multi_adjust_cap_for_player(Player_num);
3307                 multi_send_powerup_update();
3308                 ng=1;  // ng means network game
3309         }
3310         ng=1;
3311
3312         inv_count = 0;
3313         cloak_count = 0;
3314         for (i=0; i<=Highest_object_index; i++)
3315         {
3316                 int objnum;
3317
3318                 if ((Objects[i].type == OBJ_HOSTAGE) && !(Game_mode & GM_MULTI_COOP))
3319                 {
3320                         objnum = obj_create(OBJ_POWERUP, POW_SHIELD_BOOST, Objects[i].segnum, &Objects[i].pos, &vmd_identity_matrix, Powerup_info[POW_SHIELD_BOOST].size, CT_POWERUP, MT_PHYSICS, RT_POWERUP);
3321                         obj_delete(i);
3322                         if (objnum != -1)
3323                         {
3324                                 Objects[objnum].rtype.vclip_info.vclip_num = Powerup_info[POW_SHIELD_BOOST].vclip_num;
3325                                 Objects[objnum].rtype.vclip_info.frametime = Vclip[Objects[objnum].rtype.vclip_info.vclip_num].frame_time;
3326                                 Objects[objnum].rtype.vclip_info.framenum = 0;
3327                                 Objects[objnum].mtype.phys_info.drag = 512;     //1024;
3328                                 Objects[objnum].mtype.phys_info.mass = F1_0;
3329                                 vm_vec_zero(&Objects[objnum].mtype.phys_info.velocity);
3330                         }
3331                         continue;
3332                 }
3333
3334                 if (Objects[i].type == OBJ_POWERUP)
3335                 {
3336                         if (Objects[i].id == POW_EXTRA_LIFE)
3337                         {
3338                                 if (ng && !Netgame.DoInvulnerability)
3339                                 {
3340                                         Objects[i].id = POW_SHIELD_BOOST;
3341                                         Objects[i].rtype.vclip_info.vclip_num = Powerup_info[Objects[i].id].vclip_num;
3342                                         Objects[i].rtype.vclip_info.frametime = Vclip[Objects[i].rtype.vclip_info.vclip_num].frame_time;
3343                                 }
3344                                 else
3345                                 {
3346                                         Objects[i].id = POW_INVULNERABILITY;
3347                                         Objects[i].rtype.vclip_info.vclip_num = Powerup_info[Objects[i].id].vclip_num;
3348                                         Objects[i].rtype.vclip_info.frametime = Vclip[Objects[i].rtype.vclip_info.vclip_num].frame_time;
3349                                 }
3350
3351                         }
3352
3353                         if (!(Game_mode & GM_MULTI_COOP))
3354                                 if ((Objects[i].id >= POW_KEY_BLUE) && (Objects[i].id <= POW_KEY_GOLD))
3355                                 {
3356                                         Objects[i].id = POW_SHIELD_BOOST;
3357                                         Objects[i].rtype.vclip_info.vclip_num = Powerup_info[Objects[i].id].vclip_num;
3358                                         Objects[i].rtype.vclip_info.frametime = Vclip[Objects[i].rtype.vclip_info.vclip_num].frame_time;
3359                                 }
3360
3361                         if (Objects[i].id == POW_INVULNERABILITY) {
3362                                 if (inv_count >= 3 || (ng && !Netgame.DoInvulnerability)) {
3363                                         mprintf((0, "Bashing Invulnerability object #%i to shield.\n", i));
3364                                         Objects[i].id = POW_SHIELD_BOOST;
3365                                         Objects[i].rtype.vclip_info.vclip_num = Powerup_info[Objects[i].id].vclip_num;
3366                                         Objects[i].rtype.vclip_info.frametime = Vclip[Objects[i].rtype.vclip_info.vclip_num].frame_time;
3367                                 } else
3368                                         inv_count++;
3369                         }
3370
3371                         if (Objects[i].id == POW_CLOAK) {
3372                                 if (cloak_count >= 3 || (ng && !Netgame.DoCloak)) {
3373                                         mprintf((0, "Bashing Cloak object #%i to shield.\n", i));
3374                                         Objects[i].id = POW_SHIELD_BOOST;
3375                                         Objects[i].rtype.vclip_info.vclip_num = Powerup_info[Objects[i].id].vclip_num;
3376                                         Objects[i].rtype.vclip_info.frametime = Vclip[Objects[i].rtype.vclip_info.vclip_num].frame_time;
3377                                 } else
3378                                         cloak_count++;
3379                         }
3380
3381                         if (Objects[i].id == POW_AFTERBURNER && ng && !Netgame.DoAfterburner)
3382                                 bash_to_shield (i,"afterburner");
3383                         if (Objects[i].id == POW_FUSION_WEAPON && ng &&  !Netgame.DoFusions)
3384                                 bash_to_shield (i,"fusion");
3385                         if (Objects[i].id == POW_PHOENIX_WEAPON && ng && !Netgame.DoPhoenix)
3386                                 bash_to_shield (i,"phoenix");
3387
3388                         if (Objects[i].id == POW_HELIX_WEAPON && ng && !Netgame.DoHelix)
3389                                 bash_to_shield (i,"helix");
3390
3391                         if (Objects[i].id == POW_MEGA_WEAPON && ng && !Netgame.DoMegas)
3392                                 bash_to_shield (i,"mega");
3393
3394                         if (Objects[i].id == POW_SMARTBOMB_WEAPON && ng && !Netgame.DoSmarts)
3395                                 bash_to_shield (i,"smartmissile");
3396
3397                         if (Objects[i].id == POW_GAUSS_WEAPON && ng && !Netgame.DoGauss)
3398                                 bash_to_shield (i,"gauss");
3399
3400                         if (Objects[i].id == POW_VULCAN_WEAPON && ng && !Netgame.DoVulcan)
3401                                 bash_to_shield (i,"vulcan");
3402
3403                         if (Objects[i].id == POW_PLASMA_WEAPON && ng && !Netgame.DoPlasma)
3404                                 bash_to_shield (i,"plasma");
3405
3406                         if (Objects[i].id == POW_OMEGA_WEAPON && ng && !Netgame.DoOmega)
3407                                 bash_to_shield (i,"omega");
3408
3409                         if (Objects[i].id == POW_SUPER_LASER && ng && !Netgame.DoSuperLaser)
3410                                 bash_to_shield (i,"superlaser");
3411
3412                         if (Objects[i].id == POW_PROXIMITY_WEAPON && ng && !Netgame.DoProximity)
3413                                 bash_to_shield (i,"proximity");
3414
3415                         // Special: Make all proximity bombs into shields if in
3416                         // hoard mode because we use the proximity slot in the
3417                         // player struct to signify how many orbs the player has.
3418
3419                         if (Objects[i].id == POW_PROXIMITY_WEAPON && ng && (Game_mode & GM_HOARD))
3420                                 bash_to_shield (i,"proximity");
3421
3422                         if (Objects[i].id==POW_VULCAN_AMMO && ng && (!Netgame.DoVulcan && !Netgame.DoGauss))
3423                                 bash_to_shield(i,"vulcan ammo");
3424
3425                         if (Objects[i].id == POW_SPREADFIRE_WEAPON && ng && !Netgame.DoSpread)
3426                                 bash_to_shield (i,"spread");
3427                         if (Objects[i].id == POW_SMART_MINE && ng && !Netgame.DoSmartMine)
3428                                 bash_to_shield (i,"smartmine");
3429                         if (Objects[i].id == POW_SMISSILE1_1 && ng &&  !Netgame.DoFlash)
3430                                 bash_to_shield (i,"flash");
3431                         if (Objects[i].id == POW_SMISSILE1_4 && ng &&  !Netgame.DoFlash)
3432                                 bash_to_shield (i,"flash");
3433                         if (Objects[i].id == POW_GUIDED_MISSILE_1 && ng &&  !Netgame.DoGuided)
3434                                 bash_to_shield (i,"guided");
3435                         if (Objects[i].id == POW_GUIDED_MISSILE_4 && ng &&  !Netgame.DoGuided)
3436                                 bash_to_shield (i,"guided");
3437                         if (Objects[i].id == POW_EARTHSHAKER_MISSILE && ng &&  !Netgame.DoEarthShaker)
3438                                 bash_to_shield (i,"earth");
3439                         if (Objects[i].id == POW_MERCURY_MISSILE_1 && ng &&  !Netgame.DoMercury)
3440                                 bash_to_shield (i,"Mercury");
3441                         if (Objects[i].id == POW_MERCURY_MISSILE_4 && ng &&  !Netgame.DoMercury)
3442                                 bash_to_shield (i,"Mercury");
3443                         if (Objects[i].id == POW_CONVERTER && ng &&  !Netgame.DoConverter)
3444                                 bash_to_shield (i,"Converter");
3445                         if (Objects[i].id == POW_AMMO_RACK && ng &&  !Netgame.DoAmmoRack)
3446                                 bash_to_shield (i,"Ammo rack");
3447                         if (Objects[i].id == POW_HEADLIGHT && ng &&  !Netgame.DoHeadlight)
3448                                 bash_to_shield (i,"Headlight");
3449                         if (Objects[i].id == POW_LASER && ng &&  !Netgame.DoLaserUpgrade)
3450                                 bash_to_shield (i,"Laser powerup");
3451                         if (Objects[i].id == POW_HOMING_AMMO_1 && ng &&  !Netgame.DoHoming)
3452                                 bash_to_shield (i,"Homing");
3453                         if (Objects[i].id == POW_HOMING_AMMO_4 && ng &&  !Netgame.DoHoming)
3454                                 bash_to_shield (i,"Homing");
3455                         if (Objects[i].id == POW_QUAD_FIRE && ng &&  !Netgame.DoQuadLasers)
3456                                 bash_to_shield (i,"Quad Lasers");
3457                         if (Objects[i].id == POW_FLAG_BLUE && !(Game_mode & GM_CAPTURE))
3458                                 bash_to_shield (i,"Blue flag");
3459                         if (Objects[i].id == POW_FLAG_RED && !(Game_mode & GM_CAPTURE))
3460                                 bash_to_shield (i,"Red flag");
3461                 }
3462         }
3463
3464         if (Game_mode & GM_HOARD)
3465                 init_hoard_data();
3466
3467         if ((Game_mode & GM_CAPTURE) || (Game_mode & GM_HOARD))
3468                 multi_apply_goal_textures();
3469
3470         multi_sort_kill_list();
3471
3472         multi_show_player_list();
3473
3474         ConsoleObject->control_type = CT_FLYING;
3475
3476         reset_player_object();
3477
3478 }
3479
3480 int Goal_blue_segnum,Goal_red_segnum;
3481
3482 void multi_apply_goal_textures()
3483 {
3484         int             i,j,tex;
3485         segment *seg;
3486         segment2        *seg2;
3487
3488         for (i=0; i <= Highest_segment_index; i++)
3489         {
3490                 seg = &Segments[i];
3491                 seg2 = &Segment2s[i];
3492
3493                 if (seg2->special==SEGMENT_IS_GOAL_BLUE)
3494                 {
3495
3496                         Goal_blue_segnum = i;
3497
3498                         if (Game_mode & GM_HOARD)
3499                                 tex=find_goal_texture (TMI_GOAL_HOARD);
3500                         else
3501                                 tex=find_goal_texture (TMI_GOAL_BLUE);
3502
3503                         if (tex>-1)
3504                                 for (j = 0; j < 6; j++) {
3505                                         int v;
3506                                         seg->sides[j].tmap_num=tex;
3507                                         for (v=0;v<4;v++)
3508                                                 seg->sides[j].uvls[v].l = i2f(100);             //max out
3509                                 }
3510
3511                         seg2->static_light = i2f(100);  //make static light bright
3512
3513                 }
3514
3515                 if (seg2->special==SEGMENT_IS_GOAL_RED)
3516                 {
3517                         Goal_red_segnum = i;
3518
3519                         // Make both textures the same if Hoard mode
3520
3521                         if (Game_mode & GM_HOARD)
3522                                 tex=find_goal_texture (TMI_GOAL_HOARD);
3523                         else
3524                                 tex=find_goal_texture (TMI_GOAL_RED);
3525
3526                         if (tex>-1)
3527                                 for (j = 0; j < 6; j++) {
3528                                         int v;
3529                                         seg->sides[j].tmap_num=tex;
3530                                         for (v=0;v<4;v++)
3531                                                 seg->sides[j].uvls[v].l = i2f(1000);            //max out
3532                                 }
3533
3534                         seg2->static_light = i2f(100);  //make static light bright
3535                 }
3536         }
3537 }
3538 int find_goal_texture (ubyte t)
3539 {
3540         int i;
3541
3542         for (i=0;i<NumTextures;i++)
3543                 if (TmapInfo[i].flags & t)
3544                         return i;
3545
3546         Int3(); // Hey, there is no goal texture for this PIG!!!!
3547         // Edit bitmaps.tbl and designate two textures to be RED and BLUE
3548         // goal textures
3549         return (-1);
3550 }
3551
3552
3553 /* DPH: Moved to gameseq.c
3554    void bash_to_shield (int i,char *s)
3555    {
3556    int type=Objects[i].id;
3557
3558    mprintf((0, "Bashing %s object #%i to shield.\n",s, i));
3559
3560    PowerupsInMine[type]=MaxPowerupsAllowed[type]=0;
3561
3562    Objects[i].id = POW_SHIELD_BOOST;
3563    Objects[i].rtype.vclip_info.vclip_num = Powerup_info[Objects[i].id].vclip_num;
3564    Objects[i].rtype.vclip_info.frametime = Vclip[Objects[i].rtype.vclip_info.vclip_num].frame_time;
3565    }
3566 */
3567
3568 void multi_set_robot_ai(void)
3569 {
3570         // Go through the objects array looking for robots and setting
3571         // them to certain supported types of NET AI behavior.
3572
3573         //      int i;
3574         //
3575         //      for (i = 0; i <= Highest_object_index; i++)
3576         //      {
3577         //              if (Objects[i].type == OBJ_ROBOT) {
3578         //                      Objects[i].ai_info.REMOTE_OWNER = -1;
3579         //                      if (Objects[i].ai_info.behavior == AIB_STATION)
3580         //                              Objects[i].ai_info.behavior = AIB_NORMAL;
3581         //              }
3582         //      }
3583 }
3584
3585 int multi_delete_extra_objects()
3586 {
3587         int i;
3588         int nnp=0;
3589         object *objp;
3590
3591         // Go through the object list and remove any objects not used in
3592         // 'Anarchy!' games.
3593
3594         // This function also prints the total number of available multiplayer
3595         // positions in this level, even though this should always be 8 or more!
3596
3597         objp = Objects;
3598         for (i=0;i<=Highest_object_index;i++) {
3599                 if ((objp->type==OBJ_PLAYER) || (objp->type==OBJ_GHOST))
3600                         nnp++;
3601                 else if ((objp->type==OBJ_ROBOT) && (Game_mode & GM_MULTI_ROBOTS))
3602                         ;
3603                 else if ( (objp->type!=OBJ_NONE) && (objp->type!=OBJ_PLAYER) && (objp->type!=OBJ_POWERUP) && (objp->type!=OBJ_CNTRLCEN) && (objp->type!=OBJ_HOSTAGE) && !(objp->type==OBJ_WEAPON && objp->id==PMINE_ID) ) {
3604                         // Before deleting object, if it's a robot, drop it's special powerup, if any
3605                         if (objp->type == OBJ_ROBOT)
3606                                 if (objp->contains_count && (objp->contains_type == OBJ_POWERUP))
3607                                         object_create_egg(objp);
3608                         obj_delete(i);
3609                 }
3610                 objp++;
3611         }
3612
3613         return nnp;
3614 }
3615
3616 void change_playernum_to( int new_Player_num )
3617 {
3618         if (Player_num > -1)
3619                 memcpy( Players[new_Player_num].callsign, Players[Player_num].callsign, CALLSIGN_LEN+1 );
3620         Player_num = new_Player_num;
3621 }
3622
3623 int multi_all_players_alive()
3624 {
3625         int i;
3626         for (i=0;i<N_players;i++)
3627         {
3628                 if (PKilledFlags[i] && Players[i].connected)
3629                         return (0);
3630         }
3631         return (1);
3632 }
3633
3634 void multi_initiate_save_game()
3635 {
3636         uint game_id;
3637         int i, slot;
3638         char filename[128];
3639         char desc[24];
3640
3641         if ((Endlevel_sequence) || (Control_center_destroyed))
3642                 return;
3643
3644         if (!multi_all_players_alive())
3645         {
3646                 HUD_init_message ("Can't save...all players must be alive!");
3647                 return;
3648         }
3649
3650         //multi_send_netplayer_stats_request(255);
3651         //return;
3652
3653         //stop_time();
3654
3655         slot = state_get_save_file(filename, desc, 1 );
3656         if (!slot)      {
3657                 //start_time();
3658                 return;
3659         }
3660         slot--;
3661
3662         //start_time();
3663
3664         // Make a unique game id
3665         game_id = timer_get_fixed_seconds();
3666         game_id ^= N_players<<4;
3667         for (i=0; i<N_players; i++ )
3668                 game_id ^= *(uint *)Players[i].callsign;
3669         if ( game_id == 0 ) game_id = 1;                // 0 is invalid
3670
3671         mprintf(( 1, "Game_id = %8x\n", game_id));
3672         multi_send_save_game(slot, game_id, desc );
3673         multi_do_frame();
3674         multi_save_game(slot,game_id, desc );
3675 }
3676
3677 extern int state_get_game_id(char *);
3678
3679 void multi_initiate_restore_game()
3680 {
3681         int slot;
3682         char filename[128];
3683
3684         if ((Endlevel_sequence) || (Control_center_destroyed))
3685                 return;
3686
3687         if (!multi_all_players_alive())
3688         {
3689                 HUD_init_message ("Can't restore...all players must be alive!");
3690                 return;
3691         }
3692
3693         //stop_time();
3694         slot = state_get_restore_file(filename,1);
3695         if (!slot)      {
3696                 //start_time();
3697                 return;
3698         }
3699         state_game_id=state_get_game_id (filename);
3700         if (!state_game_id)
3701                 return;
3702
3703         slot--;
3704         //start_time();
3705         multi_send_restore_game(slot,state_game_id);
3706         multi_do_frame();
3707         multi_restore_game(slot,state_game_id);
3708 }
3709
3710 void multi_save_game(ubyte slot, uint id, char *desc)
3711 {
3712         char filename[128];
3713
3714         if ((Endlevel_sequence) || (Control_center_destroyed))
3715                 return;
3716
3717 #ifndef MACINTOSH
3718         sprintf( filename, "%s.mg%d", Players[Player_num].callsign, slot );
3719 #else
3720         sprintf( filename, ":Players:%s.mg%d", Players[Player_num].callsign, slot );
3721 #endif
3722         mprintf(( 0, "Save game %x on slot %d\n", id, slot ));
3723         HUD_init_message( "Saving game #%d, '%s'", slot, desc );
3724         stop_time();
3725         state_game_id = id;
3726         state_save_all_sub(filename, desc, 0 );
3727 }
3728
3729 void multi_restore_game(ubyte slot, uint id)
3730 {
3731         char filename[128];
3732         player saved_player;
3733         int pnum,i;
3734         int thisid;
3735
3736         if ((Endlevel_sequence) || (Control_center_destroyed))
3737                 return;
3738
3739         mprintf(( 0, "Restore game %x from slot %d\n", id, slot ));
3740         saved_player = Players[Player_num];
3741 #ifndef MACINTOSH
3742         sprintf( filename, "%s.mg%d", Players[Player_num].callsign, slot );
3743 #else
3744         sprintf( filename, ":Players:%s.mg%d", Players[Player_num].callsign, slot );
3745 #endif
3746
3747         for (i=0;i<N_players;i++)
3748                 multi_strip_robots(i);
3749
3750         thisid=state_get_game_id (filename);
3751         if (thisid!=id)
3752         {
3753                 multi_bad_restore ();
3754                 return;
3755         }
3756
3757         pnum=state_restore_all_sub( filename, 1, 0 );
3758
3759         mprintf ((0,"StateId=%d ThisID=%d\n",state_game_id,id));
3760
3761 #if 0
3762         if (state_game_id != id )       {
3763                 // Game doesn't match!!!
3764                 nm_messagebox( "Error", 1, "Ok", "Cannot restore saved game" );
3765                 Game_mode |= GM_GAME_OVER;
3766                 Function_mode = FMODE_MENU;
3767                 longjmp(LeaveGame, 0);
3768         }
3769
3770         change_playernum_to(pnum-1);
3771         memcpy( Players[Player_num].callsign, saved_player.callsign, CALLSIGN_LEN+1 );
3772         memcpy( Players[Player_num].net_address, saved_player.net_address, 6 );
3773         Players[Player_num].connected = saved_player.connected;
3774         Players[Player_num].n_packets_got  = saved_player.n_packets_got;
3775         Players[Player_num].n_packets_sent = saved_player.n_packets_sent;
3776         Viewer = ConsoleObject = &Objects[pnum-1];
3777 #endif
3778
3779 }
3780
3781
3782 void extract_netplayer_stats( netplayer_stats *ps, player * pd )
3783 {
3784         int i;
3785
3786         ps->flags = INTEL_INT(pd->flags);                                   // Powerup flags, see below...
3787         ps->energy = (fix)INTEL_INT(pd->energy);                            // Amount of energy remaining.
3788         ps->shields = (fix)INTEL_INT(pd->shields);                          // shields remaining (protection)
3789         ps->lives = pd->lives;                                              // Lives remaining, 0 = game over.
3790         ps->laser_level = pd->laser_level;                                  // Current level of the laser.
3791         ps->primary_weapon_flags=pd->primary_weapon_flags;                  // bit set indicates the player has this weapon.
3792         ps->secondary_weapon_flags=pd->secondary_weapon_flags;              // bit set indicates the player has this weapon.
3793         for (i = 0; i < MAX_PRIMARY_WEAPONS; i++)
3794                 ps->primary_ammo[i] = INTEL_SHORT(pd->primary_ammo[i]);
3795         for (i = 0; i < MAX_SECONDARY_WEAPONS; i++)
3796                 ps->secondary_ammo[i] = INTEL_SHORT(pd->secondary_ammo[i]);
3797
3798         //memcpy( ps->primary_ammo, pd->primary_ammo, MAX_PRIMARY_WEAPONS*sizeof(short) );        // How much ammo of each type.
3799         //memcpy( ps->secondary_ammo, pd->secondary_ammo, MAX_SECONDARY_WEAPONS*sizeof(short) ); // How much ammo of each type.
3800
3801         ps->last_score=INTEL_INT(pd->last_score);                           // Score at beginning of current level.
3802         ps->score=INTEL_INT(pd->score);                                     // Current score.
3803         ps->cloak_time=(fix)INTEL_INT(pd->cloak_time);                      // Time cloaked
3804         ps->homing_object_dist=(fix)INTEL_INT(pd->homing_object_dist);      // Distance of nearest homing object.
3805         ps->invulnerable_time=(fix)INTEL_INT(pd->invulnerable_time);        // Time invulnerable
3806         ps->KillGoalCount=INTEL_SHORT(pd->KillGoalCount);
3807         ps->net_killed_total=INTEL_SHORT(pd->net_killed_total);             // Number of times killed total
3808         ps->net_kills_total=INTEL_SHORT(pd->net_kills_total);               // Number of net kills total
3809         ps->num_kills_level=INTEL_SHORT(pd->num_kills_level);               // Number of kills this level
3810         ps->num_kills_total=INTEL_SHORT(pd->num_kills_total);               // Number of kills total
3811         ps->num_robots_level=INTEL_SHORT(pd->num_robots_level);             // Number of initial robots this level
3812         ps->num_robots_total=INTEL_SHORT(pd->num_robots_total);             // Number of robots total
3813         ps->hostages_rescued_total=INTEL_SHORT(pd->hostages_rescued_total); // Total number of hostages rescued.
3814         ps->hostages_total=INTEL_SHORT(pd->hostages_total);                 // Total number of hostages.
3815         ps->hostages_on_board=pd->hostages_on_board;                        // Number of hostages on ship.
3816 }
3817
3818 void use_netplayer_stats( player * ps, netplayer_stats *pd )
3819 {
3820         int i;
3821
3822         ps->flags = INTEL_INT(pd->flags);                       // Powerup flags, see below...
3823         ps->energy = (fix)INTEL_INT((int)pd->energy);           // Amount of energy remaining.
3824         ps->shields = (fix)INTEL_INT((int)pd->shields);         // shields remaining (protection)
3825         ps->lives = pd->lives;                                  // Lives remaining, 0 = game over.
3826         ps->laser_level = pd->laser_level;                      // Current level of the laser.
3827         ps->primary_weapon_flags=pd->primary_weapon_flags;      // bit set indicates the player has this weapon.
3828         ps->secondary_weapon_flags=pd->secondary_weapon_flags;  // bit set indicates the player has this weapon.
3829         for (i = 0; i < MAX_PRIMARY_WEAPONS; i++)
3830                 ps->primary_ammo[i] = INTEL_SHORT(pd->primary_ammo[i]);
3831         for (i = 0; i < MAX_SECONDARY_WEAPONS; i++)
3832                 ps->secondary_ammo[i] = INTEL_SHORT(pd->secondary_ammo[i]);
3833         //memcpy( ps->primary_ammo, pd->primary_ammo, MAX_PRIMARY_WEAPONS*sizeof(short) );  // How much ammo of each type.
3834         //memcpy( ps->secondary_ammo, pd->secondary_ammo, MAX_SECONDARY_WEAPONS*sizeof(short) ); // How much ammo of each type.
3835         ps->last_score = INTEL_INT(pd->last_score);             // Score at beginning of current level.
3836         ps->score = INTEL_INT(pd->score);                       // Current score.
3837         ps->cloak_time = (fix)INTEL_INT((int)pd->cloak_time);   // Time cloaked
3838         ps->homing_object_dist = (fix)INTEL_INT((int)pd->homing_object_dist); // Distance of nearest homing object.
3839         ps->invulnerable_time = (fix)INTEL_INT((int)pd->invulnerable_time); // Time invulnerable
3840         ps->KillGoalCount=INTEL_SHORT(pd->KillGoalCount);
3841         ps->net_killed_total = INTEL_SHORT(pd->net_killed_total); // Number of times killed total
3842         ps->net_kills_total = INTEL_SHORT(pd->net_kills_total); // Number of net kills total
3843         ps->num_kills_level = INTEL_SHORT(pd->num_kills_level); // Number of kills this level
3844         ps->num_kills_total = INTEL_SHORT(pd->num_kills_total); // Number of kills total
3845         ps->num_robots_level = INTEL_SHORT(pd->num_robots_level); // Number of initial robots this level
3846         ps->num_robots_total = INTEL_SHORT(pd->num_robots_total); // Number of robots total
3847         ps->hostages_rescued_total = INTEL_SHORT(pd->hostages_rescued_total); // Total number of hostages rescued.
3848         ps->hostages_total = INTEL_SHORT(pd->hostages_total);   // Total number of hostages.
3849         ps->hostages_on_board=pd->hostages_on_board;            // Number of hostages on ship.
3850 }
3851
3852 void multi_send_drop_weapon (int objnum,int seed)
3853 {
3854         object *objp;
3855         int count=0;
3856         int ammo_count;
3857
3858         objp = &Objects[objnum];
3859
3860         ammo_count = objp->ctype.powerup_info.count;
3861
3862         if (objp->id == POW_OMEGA_WEAPON && ammo_count == F1_0)
3863                 ammo_count = F1_0 - 1; //make fit in short
3864
3865         Assert(ammo_count < F1_0); //make sure fits in short
3866
3867         multibuf[count++]=(char)MULTI_DROP_WEAPON;
3868         multibuf[count++]=(char)objp->id;
3869
3870         PUT_INTEL_SHORT(multibuf+count, Player_num); count += 2;
3871         PUT_INTEL_SHORT(multibuf+count, objnum); count += 2;
3872         PUT_INTEL_SHORT(multibuf+count, ammo_count); count += 2;
3873         PUT_INTEL_INT(multibuf+count, seed);
3874
3875         map_objnum_local_to_local(objnum);
3876
3877         if (Game_mode & GM_NETWORK)
3878                 PowerupsInMine[objp->id]++;
3879
3880         multi_send_data(multibuf, 12, 2);
3881 }
3882
3883 void multi_do_drop_weapon (char *buf)
3884 {
3885         int pnum,ammo,objnum,remote_objnum,seed;
3886         object *objp;
3887         int powerup_id;
3888
3889         powerup_id=(int)(buf[1]);
3890         GET_INTEL_SHORT(pnum, buf+2);
3891         GET_INTEL_SHORT(remote_objnum, buf+4);
3892         GET_INTEL_SHORT(ammo, buf+6);
3893         GET_INTEL_INT(seed, buf+8);
3894
3895         objp = &Objects[Players[pnum].objnum];
3896
3897         objnum = spit_powerup(objp, powerup_id, seed);
3898
3899         map_objnum_local_to_remote(objnum, remote_objnum, pnum);
3900
3901         if (objnum!=-1)
3902                 Objects[objnum].ctype.powerup_info.count = ammo;
3903
3904         if (Game_mode & GM_NETWORK)
3905                 PowerupsInMine[powerup_id]++;
3906
3907         mprintf ((0,"Dropped weapon %d!\n"));
3908
3909 }
3910
3911 void multi_send_guided_info (object *miss,char done)
3912 {
3913 #ifdef WORDS_BIGENDIAN
3914         shortpos sp;
3915 #endif
3916         int count=0;
3917
3918         mprintf ((0,"Sending guided info!\n"));
3919
3920         multibuf[count++]=(char)MULTI_GUIDED;
3921         multibuf[count++]=(char)Player_num;
3922         multibuf[count++]=done;
3923
3924 #ifndef WORDS_BIGENDIAN
3925         create_shortpos((shortpos *)(multibuf+count), miss,0);
3926         count+=sizeof(shortpos);
3927 #else
3928         create_shortpos(&sp, miss, 1);
3929         memcpy(&(multibuf[count]), (ubyte *)(sp.bytemat), 9);
3930         count += 9;
3931         memcpy(&(multibuf[count]), (ubyte *)&(sp.xo), 14);
3932         count += 14;
3933 #endif
3934
3935         multi_send_data(multibuf, count, 0);
3936 }
3937
3938 void multi_do_guided (char *buf)
3939 {
3940         char pnum=buf[1];
3941         int count=3;
3942         static int fun=200;
3943 #ifdef WORDS_BIGENDIAN
3944         shortpos sp;
3945 #endif
3946
3947         if (Guided_missile[(int)pnum]==NULL)
3948         {
3949                 if (++fun>=50)
3950                 {
3951                         mprintf ((0,"Guided missile for %s is NULL!\n",Players[(int)pnum].callsign));
3952                         fun=0;
3953                 }
3954                 return;
3955         }
3956         else if (++fun>=50)
3957         {
3958                 mprintf ((0,"Got guided info for %d (%s)\n",pnum,Players[(int)pnum].callsign));
3959                 fun=0;
3960         }
3961
3962         if (buf[2])
3963         {
3964                 release_guided_missile(pnum);
3965                 return;
3966         }
3967
3968
3969         if (Guided_missile[(int)pnum]-Objects<0 || Guided_missile[(int)pnum]-Objects > Highest_object_index)
3970         {
3971                 Int3();  // Get Jason immediately!
3972                 return;
3973         }
3974
3975 #ifndef WORDS_BIGENDIAN
3976         extract_shortpos(Guided_missile[(int)pnum], (shortpos *)(buf+count),0);
3977 #else
3978         memcpy((ubyte *)(sp.bytemat), (ubyte *)(buf + count), 9);
3979         memcpy((ubyte *)&(sp.xo), (ubyte *)(buf + count + 9), 14);
3980         extract_shortpos(Guided_missile[(int)pnum], &sp, 1);
3981 #endif
3982
3983         count+=sizeof (shortpos);
3984
3985         update_object_seg(Guided_missile[(int)pnum]);
3986 }
3987
3988 void multi_send_stolen_items ()
3989 {
3990         int i,count=1;
3991         multibuf[0]=MULTI_STOLEN_ITEMS;
3992
3993         for (i=0;i<MAX_STOLEN_ITEMS;i++)
3994         {
3995                 multibuf[i+1]=Stolen_items[i];
3996                 mprintf ((0,"[%d]=%d ",i,Stolen_items[i]));
3997                 count++;      // So I like to break my stuff into smaller chunks, so what?
3998         }
3999         mprintf ((0,"\n"));
4000         multi_send_data(multibuf, count, 1);
4001 }
4002
4003 void multi_do_stolen_items (char *buf)
4004 {
4005         int i;
4006
4007         mprintf ((0,"Recieved a stolen item packet...\n"));
4008
4009         for (i=0;i<MAX_STOLEN_ITEMS;i++)
4010         {
4011                 Stolen_items[i]=buf[i+1];
4012                 mprintf ((0,"[%d]=%d ",i,Stolen_items[i]));
4013         }
4014         mprintf ((0,"\n"));
4015 }
4016
4017 extern void network_send_important_packet (char *,int);
4018
4019 void multi_send_wall_status (int wallnum,ubyte type,ubyte flags,ubyte state)
4020 {
4021         int count=0;
4022         multibuf[count]=MULTI_WALL_STATUS;        count++;
4023         PUT_INTEL_SHORT(multibuf+count, wallnum);   count+=2;
4024         multibuf[count]=type;                 count++;
4025         multibuf[count]=flags;                count++;
4026         multibuf[count]=state;                count++;
4027
4028 #if 0
4029         if (Game_mode & GM_NETWORK)
4030         {
4031                 network_send_important_packet (multibuf,count);
4032                 network_send_important_packet (multibuf,count);
4033         }
4034         else
4035 #endif
4036         {
4037                 multi_send_data(multibuf, count, 1); // twice, just to be sure
4038                 multi_send_data(multibuf, count, 1);
4039         }
4040 }
4041 void multi_send_wall_status_specific (int pnum,int wallnum,ubyte type,ubyte flags,ubyte state)
4042 {
4043         // Send wall states a specific rejoining player
4044
4045         int count=0;
4046
4047         Assert (Game_mode & GM_NETWORK);
4048         //Assert (pnum>-1 && pnum<N_players);
4049
4050         multibuf[count]=MULTI_WALL_STATUS;        count++;
4051         PUT_INTEL_SHORT(multibuf+count, wallnum);  count+=2;
4052         multibuf[count]=type;                 count++;
4053         multibuf[count]=flags;                count++;
4054         multibuf[count]=state;                count++;
4055
4056         network_send_naked_packet(multibuf, count,pnum); // twice, just to be sure
4057         network_send_naked_packet(multibuf, count,pnum);
4058 }
4059
4060 void multi_do_wall_status (char *buf)
4061 {
4062         short wallnum;
4063         ubyte flag,type,state;
4064
4065         GET_INTEL_SHORT(wallnum, buf+1);
4066         type=buf[3];
4067         flag=buf[4];
4068         state=buf[5];
4069
4070         Assert (wallnum>=0);
4071         Walls[wallnum].type=type;
4072         Walls[wallnum].flags=flag;
4073         //Assert(state <= 4);
4074         Walls[wallnum].state=state;
4075
4076         if (Walls[wallnum].type==WALL_OPEN)
4077         {
4078                 digi_kill_sound_linked_to_segment(Walls[wallnum].segnum,Walls[wallnum].sidenum,SOUND_FORCEFIELD_HUM);
4079                 //digi_kill_sound_linked_to_segment(csegp-Segments,cside,SOUND_FORCEFIELD_HUM);
4080         }
4081
4082
4083         //mprintf ((0,"Got a walls packet.\n"));
4084 }
4085
4086 void multi_send_jason_cheat (int num)
4087 {
4088         num=num;
4089         return;
4090 }
4091
4092 void multi_send_kill_goal_counts()
4093 {
4094         int i,count=1;
4095         multibuf[0]=MULTI_KILLGOALS;
4096
4097         for (i=0;i<MAX_PLAYERS;i++)
4098         {
4099                 *(char *)(multibuf+count)=(char)Players[i].KillGoalCount;
4100                 count++;
4101         }
4102
4103         mprintf ((0,"MULTI: Sending KillGoalCounts...\n"));
4104         multi_send_data(multibuf, count, 1);
4105 }
4106
4107 void multi_do_kill_goal_counts(char *buf)
4108 {
4109         int i,count=1;
4110
4111         for (i=0;i<MAX_PLAYERS;i++)
4112         {
4113                 Players[i].KillGoalCount=*(char *)(buf+count);
4114                 mprintf ((0,"KGC: %s has %d kills!\n",Players[i].callsign,Players[i].KillGoalCount));
4115                 count++;
4116         }
4117
4118 }
4119
4120 void multi_send_heartbeat ()
4121 {
4122         if (!Netgame.PlayTimeAllowed)
4123                 return;
4124
4125         multibuf[0]=MULTI_HEARTBEAT;
4126         PUT_INTEL_INT(multibuf+1, ThisLevelTime);
4127         multi_send_data(multibuf, 5, 0);
4128 }
4129
4130 void multi_do_heartbeat (char *buf)
4131 {
4132         fix num;
4133
4134         GET_INTEL_INT(num, buf+1);
4135
4136         ThisLevelTime=num;
4137 }
4138
4139 void multi_check_for_killgoal_winner ()
4140 {
4141         int i,best=0,bestnum=0;
4142         object *objp;
4143
4144         if (Control_center_destroyed)
4145                 return;
4146
4147         for (i=0;i<N_players;i++)
4148         {
4149                 if (Players[i].KillGoalCount>best)
4150                 {
4151                         best=Players[i].KillGoalCount;
4152                         bestnum=i;
4153                 }
4154         }
4155
4156         if (bestnum==Player_num)
4157         {
4158                 HUD_init_message("You have the best score at %d kills!",best);
4159                 //Players[Player_num].shields=i2f(200);
4160         }
4161         else
4162
4163                 HUD_init_message ("%s has the best score with %d kills!",Players[bestnum].callsign,best);
4164
4165         HUD_init_message ("The control center has been destroyed!");
4166
4167         objp=obj_find_first_of_type (OBJ_CNTRLCEN);
4168         net_destroy_controlcen (objp);
4169 }
4170
4171 void multi_send_seismic (fix start,fix end)
4172 {
4173         int count=1;
4174
4175         multibuf[0]=MULTI_SEISMIC;
4176         PUT_INTEL_INT(multibuf+count, start); count+=(sizeof(fix));
4177         PUT_INTEL_INT(multibuf+count, end); count+=(sizeof(fix));
4178
4179         multi_send_data(multibuf, count, 1);
4180 }
4181
4182 extern fix Seismic_disturbance_start_time;
4183 extern fix Seismic_disturbance_end_time;
4184
4185 void multi_do_seismic (char *buf)
4186 {
4187         GET_INTEL_INT(Seismic_disturbance_start_time, buf+1);
4188         GET_INTEL_INT(Seismic_disturbance_end_time, buf+5);
4189         digi_play_sample (SOUND_SEISMIC_DISTURBANCE_START, F1_0);
4190 }
4191
4192 void multi_send_light (int segnum,ubyte val)
4193 {
4194         int count=1,i;
4195         multibuf[0]=MULTI_LIGHT;
4196         PUT_INTEL_INT(multibuf+count, segnum); count+=(sizeof(int));
4197         *(char *)(multibuf+count)=val; count++;
4198         for (i=0;i<6;i++)
4199         {
4200                 //mprintf ((0,"Sending %d!\n",Segments[segnum].sides[i].tmap_num2));
4201                 PUT_INTEL_SHORT(multibuf+count, Segments[segnum].sides[i].tmap_num2); count+=2;
4202         }
4203         multi_send_data(multibuf, count, 1);
4204 }
4205 void multi_send_light_specific (int pnum,int segnum,ubyte val)
4206 {
4207         int count=1,i;
4208
4209         Assert (Game_mode & GM_NETWORK);
4210         //  Assert (pnum>-1 && pnum<N_players);
4211
4212         multibuf[0]=MULTI_LIGHT;
4213         PUT_INTEL_INT(multibuf+count, segnum); count+=(sizeof(int));
4214         *(char *)(multibuf+count)=val; count++;
4215
4216         for (i=0;i<6;i++)
4217         {
4218                 //mprintf ((0,"Sending %d!\n",Segments[segnum].sides[i].tmap_num2));
4219                 PUT_INTEL_SHORT(multibuf+count, Segments[segnum].sides[i].tmap_num2); count+=2;
4220         }
4221         network_send_naked_packet(multibuf, count, pnum);
4222 }
4223
4224 void multi_do_light (char *buf)
4225 {
4226         int i, seg;
4227         ubyte sides=*(char *)(buf+5);
4228
4229         GET_INTEL_INT(seg, buf+1);
4230         for (i=0;i<6;i++)
4231         {
4232                 if ((sides & (1<<i)))
4233                 {
4234                         subtract_light (seg,i);
4235                         GET_INTEL_SHORT(Segments[seg].sides[i].tmap_num2, buf+(6+(2*i)) );
4236                         //mprintf ((0,"Got %d!\n",Segments[seg].sides[i].tmap_num2));
4237                 }
4238         }
4239 }
4240
4241 //@@void multi_send_start_trigger(int triggernum)
4242 //@@{
4243 //@@    // Send an even to trigger something in the mine
4244 //@@
4245 //@@    int count = 0;
4246 //@@
4247 //@@    multibuf[count] = MULTI_START_TRIGGER;          count += 1;
4248 //@@    multibuf[count] = Player_num;                   count += 1;
4249 //@@    multibuf[count] = (ubyte)triggernum;            count += 1;
4250 //@@
4251 //@@    //mprintf ((0,"Sending start trigger %d\n",triggernum));
4252 //@@    multi_send_data(multibuf, count, 2);
4253 //@@}
4254 //@@void multi_do_start_trigger(char *buf)
4255 //@@{
4256 //@@    int pnum = buf[1];
4257 //@@    int trigger = buf[2];
4258 //@@
4259 //@@    //mprintf ((0,"MULTI doing start trigger!\n"));
4260 //@@
4261 //@@    if ((pnum < 0) || (pnum >= N_players) || (pnum == Player_num))
4262 //@@    {
4263 //@@            Int3(); // Got trigger from illegal playernum
4264 //@@            return;
4265 //@@    }
4266 //@@    if ((trigger < 0) || (trigger >= Num_triggers))
4267 //@@    {
4268 //@@            Int3(); // Illegal trigger number in multiplayer
4269 //@@            return;
4270 //@@    }
4271 //@@
4272 //@@    if (!(Triggers[trigger].flags & TF_SPRUNG))
4273 //@@            check_trigger_sub(trigger, pnum,0);
4274 //@@}
4275
4276
4277 void multi_do_flags (char *buf)
4278 {
4279         char pnum=buf[1];
4280         uint flags;
4281
4282         GET_INTEL_INT(flags, buf+2);
4283         if (pnum!=Player_num)
4284                 Players[(int)pnum].flags=flags;
4285 }
4286
4287 void multi_send_flags (char pnum)
4288 {
4289         multibuf[0]=MULTI_FLAGS;
4290         multibuf[1]=pnum;
4291         PUT_INTEL_INT(multibuf+2, Players[(int)pnum].flags);
4292  
4293         multi_send_data(multibuf, 6, 1);
4294 }
4295
4296 void multi_send_drop_blobs (char pnum)
4297 {
4298         multibuf[0]=MULTI_DROP_BLOB;
4299         multibuf[1]=pnum;
4300
4301         multi_send_data(multibuf, 2, 0);
4302 }
4303
4304 void multi_do_drop_blob (char *buf)
4305 {
4306         char pnum=buf[1];
4307         drop_afterburner_blobs (&Objects[Players[(int)pnum].objnum], 2, i2f(5)/2, -1);
4308 }
4309
4310 void multi_send_powerup_update ()
4311 {
4312         int i;
4313
4314
4315         multibuf[0]=MULTI_POWERUP_UPDATE;
4316         for (i=0;i<MAX_POWERUP_TYPES;i++)
4317                 multibuf[i+1]=MaxPowerupsAllowed[i];
4318
4319         multi_send_data(multibuf, MAX_POWERUP_TYPES+1, 1);
4320 }
4321 void multi_do_powerup_update (char *buf)
4322 {
4323         int i;
4324
4325         for (i=0;i<MAX_POWERUP_TYPES;i++)
4326                 if (buf[i+1]>MaxPowerupsAllowed[i])
4327                         MaxPowerupsAllowed[i]=buf[i+1];
4328 }
4329
4330 extern active_door ActiveDoors[];
4331 extern int Num_open_doors;          // Number of open doors
4332
4333
4334 #if 0 // never used...
4335 void multi_send_active_door (int i)
4336 {
4337         int count;
4338
4339         multibuf[0]=MULTI_ACTIVE_DOOR;
4340         multibuf[1]=i;
4341         multibuf[2]=Num_open_doors;
4342         count = 3;
4343 #ifndef WORDS_BIGENDIAN
4344         memcpy ((char *)(&multibuf[3]),&ActiveDoors[(int)i],sizeof(struct active_door));
4345         count += sizeof(active_door);
4346 #else
4347         *(int *)(multibuf + count) = INTEL_INT(ActiveDoors[i].n_parts);                 count += 4;
4348         *(short *)(multibuf + count) = INTEL_SHORT(ActiveDoors[i].front_wallnum[0]);    count += 2;
4349         *(short *)(multibuf + count) = INTEL_SHORT(ActiveDoors[i].front_wallnum[1]);    count += 2;
4350         *(short *)(multibuf + count) = INTEL_SHORT(ActiveDoors[i].back_wallnum[0]);     count += 2;
4351         *(short *)(multibuf + count) = INTEL_SHORT(ActiveDoors[i].back_wallnum[1]);     count += 2;
4352         *(int *)(multibuf + count) = INTEL_INT(ActiveDoors[i].time);                    count += 4;
4353 #endif
4354         //multi_send_data (multibuf,sizeof(struct active_door)+3,1);
4355         multi_send_data (multibuf,count,1);
4356 }
4357 #endif // 0 (never used)
4358
4359 void multi_do_active_door (char *buf)
4360 {
4361         char i = multibuf[1];
4362         Num_open_doors = buf[2];
4363
4364         memcpy(&ActiveDoors[(int)i], buf+3, sizeof(struct active_door));
4365 #ifdef WORDS_BIGENDIAN
4366         {
4367                 active_door *ad = &ActiveDoors[(int)i];
4368                 ad->n_parts = INTEL_INT(ad->n_parts);
4369                 ad->front_wallnum[0] = INTEL_SHORT(ad->front_wallnum[0]);
4370                 ad->front_wallnum[1] = INTEL_SHORT(ad->front_wallnum[1]);
4371                 ad->back_wallnum[0] = INTEL_SHORT(ad->back_wallnum[0]);
4372                 ad->back_wallnum[1] = INTEL_SHORT(ad->back_wallnum[1]);
4373                 ad->time = INTEL_INT(ad->time);
4374         }
4375 #endif //WORDS_BIGENDIAN
4376 }
4377
4378 void multi_send_sound_function (char whichfunc, char sound)
4379 {
4380         int count=0;
4381
4382         multibuf[0]=MULTI_SOUND_FUNCTION;   count++;
4383         multibuf[1]=Player_num;             count++;
4384         multibuf[2]=whichfunc;              count++;
4385 #ifndef WORDS_BIGENDIAN
4386         *(uint *)(multibuf+count)=sound;    count++;
4387 #else
4388         multibuf[3] = sound; count++;       // this would probably work on the PC as well.  Jason?
4389 #endif
4390         multi_send_data (multibuf,4,0);
4391 }
4392
4393 #define AFTERBURNER_LOOP_START  20098
4394 #define AFTERBURNER_LOOP_END    25776
4395
4396 void multi_do_sound_function (char *buf)
4397 {
4398         // for afterburner
4399
4400         char pnum,whichfunc;
4401         int sound;
4402
4403         if (Players[Player_num].connected!=1)
4404                 return;
4405
4406         pnum=buf[1];
4407         whichfunc=buf[2];
4408         sound=buf[3];
4409
4410         if (whichfunc==0)
4411                 digi_kill_sound_linked_to_object (Players[(int)pnum].objnum);
4412         else if (whichfunc==3)
4413                 digi_link_sound_to_object3( sound, Players[(int)pnum].objnum, 1,F1_0, i2f(256), AFTERBURNER_LOOP_START, AFTERBURNER_LOOP_END);
4414 }
4415
4416 void multi_send_capture_bonus (char pnum)
4417 {
4418         Assert (Game_mode & GM_CAPTURE);
4419
4420         multibuf[0]=MULTI_CAPTURE_BONUS;
4421         multibuf[1]=pnum;
4422
4423         multi_send_data (multibuf,2,1);
4424         multi_do_capture_bonus (multibuf);
4425 }
4426 void multi_send_orb_bonus (char pnum)
4427 {
4428         Assert (Game_mode & GM_HOARD);
4429
4430         multibuf[0]=MULTI_ORB_BONUS;
4431         multibuf[1]=pnum;
4432         multibuf[2]=Players[Player_num].secondary_ammo[PROXIMITY_INDEX];
4433
4434         multi_send_data (multibuf,3,1);
4435         multi_do_orb_bonus (multibuf);
4436 }
4437 void multi_do_capture_bonus(char *buf)
4438 {
4439         // Figure out the results of a network kills and add it to the
4440         // appropriate player's tally.
4441
4442         char pnum=buf[1];
4443         int TheGoal;
4444
4445         kmatrix_kills_changed = 1;
4446
4447         if (pnum==Player_num)
4448                 HUD_init_message("You have Scored!");
4449         else
4450                 HUD_init_message("%s has Scored!",Players[(int)pnum].callsign);
4451
4452         if (pnum==Player_num)
4453                 digi_play_sample (SOUND_HUD_YOU_GOT_GOAL,F1_0*2);
4454         else if (get_team(pnum)==TEAM_RED)
4455                 digi_play_sample (SOUND_HUD_RED_GOT_GOAL,F1_0*2);
4456         else
4457                 digi_play_sample (SOUND_HUD_BLUE_GOT_GOAL,F1_0*2);
4458
4459         Players[(int)pnum].flags &= ~(PLAYER_FLAGS_FLAG);  // Clear capture flag
4460
4461         team_kills[get_team(pnum)] += 5;
4462         Players[(int)pnum].net_kills_total += 5;
4463         Players[(int)pnum].KillGoalCount+=5;
4464
4465         if (Netgame.KillGoal>0)
4466         {
4467                 TheGoal=Netgame.KillGoal*5;
4468
4469                 if (Players[(int)pnum].KillGoalCount>=TheGoal)
4470                 {
4471                         if (pnum==Player_num)
4472                         {
4473                                 HUD_init_message("You reached the kill goal!");
4474                                 Players[Player_num].shields=i2f(200);
4475                         }
4476                         else
4477                                 HUD_init_message ("%s has reached the kill goal!",Players[(int)pnum].callsign);
4478
4479                         HUD_init_message ("The control center has been destroyed!");
4480                         net_destroy_controlcen (obj_find_first_of_type (OBJ_CNTRLCEN));
4481                 }
4482         }
4483
4484         multi_sort_kill_list();
4485         multi_show_player_list();
4486 }
4487
4488 int GetOrbBonus (char num)
4489 {
4490         int bonus;
4491
4492         bonus=num*(num+1)/2;
4493         return (bonus);
4494 }
4495
4496 void multi_do_orb_bonus(char *buf)
4497 {
4498         // Figure out the results of a network kills and add it to the
4499         // appropriate player's tally.
4500
4501         char pnum=buf[1];
4502         int TheGoal;
4503         int bonus=GetOrbBonus (buf[2]);
4504
4505         kmatrix_kills_changed = 1;
4506
4507         if (pnum==Player_num)
4508                 HUD_init_message("You have scored %d points!",bonus);
4509         else
4510                 HUD_init_message("%s has scored with %d orbs!",Players[(int)pnum].callsign,buf[2]);
4511
4512         if (pnum==Player_num)
4513                 digi_start_sound_queued (SOUND_HUD_YOU_GOT_GOAL,F1_0*2);
4514         else if (Game_mode & GM_TEAM)
4515         {
4516                 if (get_team(pnum)==TEAM_RED)
4517                         digi_play_sample (SOUND_HUD_RED_GOT_GOAL,F1_0*2);
4518                 else
4519                         digi_play_sample (SOUND_HUD_BLUE_GOT_GOAL,F1_0*2);
4520         }
4521         else
4522                 digi_play_sample (SOUND_OPPONENT_HAS_SCORED,F1_0*2);
4523
4524         if (bonus>PhallicLimit)
4525         {
4526                 if (pnum==Player_num)
4527                         HUD_init_message ("You have the record with %d points!",bonus);
4528                 else
4529                         HUD_init_message ("%s has the record with %d points!",Players[(int)pnum].callsign,bonus);
4530                 digi_play_sample (SOUND_BUDDY_MET_GOAL,F1_0*2);
4531                 PhallicMan=pnum;
4532                 PhallicLimit=bonus;
4533         }
4534
4535         Players[(int)pnum].flags &= ~(PLAYER_FLAGS_FLAG);  // Clear orb flag
4536
4537         team_kills[get_team(pnum)] += bonus;
4538         Players[(int)pnum].net_kills_total += bonus;
4539         Players[(int)pnum].KillGoalCount+=bonus;
4540
4541         team_kills[get_team(pnum)]%=1000;
4542         Players[(int)pnum].net_kills_total%=1000;
4543         Players[(int)pnum].KillGoalCount%=1000;
4544
4545         if (Netgame.KillGoal>0)
4546         {
4547                 TheGoal=Netgame.KillGoal*5;
4548
4549                 if (Players[(int)pnum].KillGoalCount>=TheGoal)
4550                 {
4551                         if (pnum==Player_num)
4552                         {
4553                                 HUD_init_message("You reached the kill goal!");
4554                                 Players[Player_num].shields=i2f(200);
4555                         }
4556                         else
4557                                 HUD_init_message ("%s has reached the kill goal!",Players[(int)pnum].callsign);
4558
4559                         HUD_init_message ("The control center has been destroyed!");
4560                         net_destroy_controlcen (obj_find_first_of_type (OBJ_CNTRLCEN));
4561                 }
4562         }
4563         multi_sort_kill_list();
4564         multi_show_player_list();
4565 }
4566
4567 void multi_send_got_flag (char pnum)
4568 {
4569         multibuf[0]=MULTI_GOT_FLAG;
4570         multibuf[1]=pnum;
4571
4572         digi_start_sound_queued (SOUND_HUD_YOU_GOT_FLAG,F1_0*2);
4573
4574         multi_send_data (multibuf,2,1);
4575         multi_send_flags (Player_num);
4576 }
4577
4578 int SoundHacked=0;
4579 digi_sound ReversedSound;
4580
4581 void multi_send_got_orb (char pnum)
4582 {
4583         multibuf[0]=MULTI_GOT_ORB;
4584         multibuf[1]=pnum;
4585
4586         digi_play_sample (SOUND_YOU_GOT_ORB,F1_0*2);
4587
4588         multi_send_data (multibuf,2,1);
4589         multi_send_flags (Player_num);
4590 }
4591
4592 void multi_do_got_flag (char *buf)
4593 {
4594         char pnum=buf[1];
4595
4596         if (pnum==Player_num)
4597                 digi_start_sound_queued (SOUND_HUD_YOU_GOT_FLAG,F1_0*2);
4598         else if (get_team(pnum)==TEAM_RED)
4599                 digi_start_sound_queued (SOUND_HUD_RED_GOT_FLAG,F1_0*2);
4600         else
4601                 digi_start_sound_queued (SOUND_HUD_BLUE_GOT_FLAG,F1_0*2);
4602         Players[(int)pnum].flags|=PLAYER_FLAGS_FLAG;
4603         HUD_init_message ("%s picked up a flag!",Players[(int)pnum].callsign);
4604 }
4605 void multi_do_got_orb (char *buf)
4606 {
4607         char pnum=buf[1];
4608
4609         Assert (Game_mode & GM_HOARD);
4610
4611         if (Game_mode & GM_TEAM)
4612         {
4613                 if (get_team(pnum)==get_team(Player_num))
4614                         digi_play_sample (SOUND_FRIEND_GOT_ORB,F1_0*2);
4615                 else
4616                         digi_play_sample (SOUND_OPPONENT_GOT_ORB,F1_0*2);
4617     }
4618         else
4619                 digi_play_sample (SOUND_OPPONENT_GOT_ORB,F1_0*2);
4620
4621         Players[(int)pnum].flags|=PLAYER_FLAGS_FLAG;
4622         HUD_init_message ("%s picked up an orb!",Players[(int)pnum].callsign);
4623 }
4624
4625
4626 void DropOrb ()
4627 {
4628         int objnum,seed;
4629
4630         if (!(Game_mode & GM_HOARD))
4631                 Int3(); // How did we get here? Get Leighton!
4632
4633         if (!Players[Player_num].secondary_ammo[PROXIMITY_INDEX])
4634         {
4635                 HUD_init_message("No orbs to drop!");
4636                 return;
4637         }
4638
4639         seed = d_rand();
4640
4641         objnum = spit_powerup(ConsoleObject,POW_HOARD_ORB,seed);
4642
4643         if (objnum<0)
4644                 return;
4645
4646         HUD_init_message("Orb dropped!");
4647         digi_play_sample (SOUND_DROP_WEAPON,F1_0);
4648
4649         if ((Game_mode & GM_HOARD) && objnum>-1)
4650                 multi_send_drop_flag(objnum,seed);
4651
4652         Players[Player_num].secondary_ammo[PROXIMITY_INDEX]--;
4653
4654         // If empty, tell everyone to stop drawing the box around me
4655         if (Players[Player_num].secondary_ammo[PROXIMITY_INDEX]==0)
4656                 multi_send_flags (Player_num);
4657 }
4658
4659 void DropFlag ()
4660 {
4661         int objnum,seed;
4662
4663         if (!(Game_mode & GM_CAPTURE) && !(Game_mode & GM_HOARD))
4664                 return;
4665         if (Game_mode & GM_HOARD)
4666         {
4667                 DropOrb();
4668                 return;
4669         }
4670
4671         if (!(Players[Player_num].flags & PLAYER_FLAGS_FLAG))
4672         {
4673                 HUD_init_message("No flag to drop!");
4674                 return;
4675         }
4676
4677
4678         HUD_init_message("Flag dropped!");
4679         digi_play_sample (SOUND_DROP_WEAPON,F1_0);
4680
4681         seed = d_rand();
4682
4683         if (get_team (Player_num)==TEAM_RED)
4684                 objnum = spit_powerup(ConsoleObject,POW_FLAG_BLUE,seed);
4685         else
4686                 objnum = spit_powerup(ConsoleObject,POW_FLAG_RED,seed);
4687
4688         if (objnum<0)
4689                 return;
4690
4691         if ((Game_mode & GM_CAPTURE) && objnum>-1)
4692                 multi_send_drop_flag(objnum,seed);
4693
4694         Players[Player_num].flags &=~(PLAYER_FLAGS_FLAG);
4695 }
4696
4697
4698 void multi_send_drop_flag (int objnum,int seed)
4699 {
4700         object *objp;
4701         int count=0;
4702
4703         objp = &Objects[objnum];
4704
4705         multibuf[count++]=(char)MULTI_DROP_FLAG;
4706         multibuf[count++]=(char)objp->id;
4707
4708         PUT_INTEL_SHORT(multibuf+count, Player_num); count += 2;
4709         PUT_INTEL_SHORT(multibuf+count, objnum); count += 2;
4710         PUT_INTEL_SHORT(multibuf+count, objp->ctype.powerup_info.count); count += 2;
4711         PUT_INTEL_INT(multibuf+count, seed);
4712
4713         map_objnum_local_to_local(objnum);
4714
4715         if (!(Game_mode & GM_HOARD))
4716                 if (Game_mode & GM_NETWORK)
4717                         PowerupsInMine[objp->id]++;
4718
4719         multi_send_data(multibuf, 12, 2);
4720 }
4721
4722 void multi_do_drop_flag (char *buf)
4723 {
4724         int pnum,ammo,objnum,remote_objnum,seed;
4725         object *objp;
4726         int powerup_id;
4727
4728         powerup_id=buf[1];
4729         GET_INTEL_SHORT(pnum, buf+2);
4730         GET_INTEL_SHORT(remote_objnum, buf+4);
4731         GET_INTEL_SHORT(ammo, buf+6);
4732         GET_INTEL_INT(seed, buf+8);
4733
4734         objp = &Objects[Players[pnum].objnum];
4735
4736         objnum = spit_powerup(objp, powerup_id, seed);
4737
4738         map_objnum_local_to_remote(objnum, remote_objnum, pnum);
4739
4740         if (objnum!=-1)
4741                 Objects[objnum].ctype.powerup_info.count = ammo;
4742
4743         if (!(Game_mode & GM_HOARD))
4744         {
4745                 if (Game_mode & GM_NETWORK)
4746                         PowerupsInMine[powerup_id]++;
4747                 Players[pnum].flags &= ~(PLAYER_FLAGS_FLAG);
4748         }
4749         mprintf ((0,"Dropped flag %d!\n"));
4750
4751 }
4752
4753 void multi_bad_restore ()
4754 {
4755         Function_mode = FMODE_MENU;
4756         nm_messagebox(NULL, 1, TXT_OK,
4757                       "A multi-save game was restored\nthat you are missing or does not\nmatch that of the others.\nYou must rejoin if you wish to\ncontinue.");
4758         Function_mode = FMODE_GAME;
4759         multi_quit_game = 1;
4760         multi_leave_menu = 1;
4761         multi_reset_stuff();
4762 }
4763
4764 extern int robot_controlled[MAX_ROBOTS_CONTROLLED];
4765 extern int robot_agitation[MAX_ROBOTS_CONTROLLED];
4766 extern fix robot_controlled_time[MAX_ROBOTS_CONTROLLED];
4767 extern fix robot_last_send_time[MAX_ROBOTS_CONTROLLED];
4768 extern fix robot_last_message_time[MAX_ROBOTS_CONTROLLED];
4769 extern int robot_send_pending[MAX_ROBOTS_CONTROLLED];
4770 extern int robot_fired[MAX_ROBOTS_CONTROLLED];
4771 extern sbyte robot_fire_buf[MAX_ROBOTS_CONTROLLED][18+3];
4772
4773
4774 void multi_send_robot_controls (char pnum)
4775 {
4776         int count=2;
4777
4778         mprintf ((0,"Sending ROBOT_CONTROLS!!!\n"));
4779
4780         multibuf[0]=MULTI_ROBOT_CONTROLS;
4781         multibuf[1]=pnum;
4782         memcpy (&(multibuf[count]),&robot_controlled,MAX_ROBOTS_CONTROLLED*4);
4783         count+=(MAX_ROBOTS_CONTROLLED*4);
4784         memcpy (&(multibuf[count]),&robot_agitation,MAX_ROBOTS_CONTROLLED*4);
4785         count+=(MAX_ROBOTS_CONTROLLED*4);
4786         memcpy (&(multibuf[count]),&robot_controlled_time,MAX_ROBOTS_CONTROLLED*4);
4787         count+=(MAX_ROBOTS_CONTROLLED*4);
4788         memcpy (&(multibuf[count]),&robot_last_send_time,MAX_ROBOTS_CONTROLLED*4);
4789         count+=(MAX_ROBOTS_CONTROLLED*4);
4790         memcpy (&(multibuf[count]),&robot_last_message_time,MAX_ROBOTS_CONTROLLED*4);
4791         count+=(MAX_ROBOTS_CONTROLLED*4);
4792         memcpy (&(multibuf[count]),&robot_send_pending,MAX_ROBOTS_CONTROLLED*4);
4793         count+=(MAX_ROBOTS_CONTROLLED*4);
4794         memcpy (&(multibuf[count]),&robot_fired,MAX_ROBOTS_CONTROLLED*4);
4795         count+=(MAX_ROBOTS_CONTROLLED*4);
4796
4797         network_send_naked_packet (multibuf,142,pnum);
4798 }
4799 void multi_do_robot_controls(char *buf)
4800 {
4801         int count=2;
4802
4803         mprintf ((0,"Recieved ROBOT_CONTROLS!!!\n"));
4804
4805         if (buf[1]!=Player_num)
4806         {
4807                 Int3(); // Get Jason!  Recieved a coop_sync that wasn't ours!
4808                 return;
4809         }
4810
4811         memcpy (&robot_controlled,&(buf[count]),MAX_ROBOTS_CONTROLLED*4);
4812         count+=(MAX_ROBOTS_CONTROLLED*4);
4813         memcpy (&robot_agitation,&(buf[count]),MAX_ROBOTS_CONTROLLED*4);
4814         count+=(MAX_ROBOTS_CONTROLLED*4);
4815         memcpy (&robot_controlled_time,&(buf[count]),MAX_ROBOTS_CONTROLLED*4);
4816         count+=(MAX_ROBOTS_CONTROLLED*4);
4817         memcpy (&robot_last_send_time,&(buf[count]),MAX_ROBOTS_CONTROLLED*4);
4818         count+=(MAX_ROBOTS_CONTROLLED*4);
4819         memcpy (&robot_last_message_time,&(buf[count]),MAX_ROBOTS_CONTROLLED*4);
4820         count+=(MAX_ROBOTS_CONTROLLED*4);
4821         memcpy (&robot_send_pending,&(buf[count]),MAX_ROBOTS_CONTROLLED*4);
4822         count+=(MAX_ROBOTS_CONTROLLED*4);
4823         memcpy (&robot_fired,&(buf[count]),MAX_ROBOTS_CONTROLLED*4);
4824         count+=(MAX_ROBOTS_CONTROLLED*4);
4825 }
4826
4827 #define POWERUPADJUSTS 5
4828 int PowerupAdjustMapping[]={11,19,39,41,44};
4829
4830 int multi_powerup_is_4pack (int id)
4831 {
4832         int i;
4833
4834         for (i=0;i<POWERUPADJUSTS;i++)
4835                 if (id==PowerupAdjustMapping[i])
4836                         return (1);
4837         return (0);
4838 }
4839
4840 int multi_powerup_is_allowed(int id)
4841 {
4842         if (id == POW_INVULNERABILITY && !Netgame.DoInvulnerability)
4843                 return (0);
4844         if (id == POW_CLOAK && !Netgame.DoCloak)
4845                 return (0);
4846         if (id == POW_AFTERBURNER && !Netgame.DoAfterburner)
4847                 return (0);
4848         if (id == POW_FUSION_WEAPON &&  !Netgame.DoFusions)
4849                 return (0);
4850         if (id == POW_PHOENIX_WEAPON && !Netgame.DoPhoenix)
4851                 return (0);
4852         if (id == POW_HELIX_WEAPON && !Netgame.DoHelix)
4853                 return (0);
4854         if (id == POW_MEGA_WEAPON && !Netgame.DoMegas)
4855                 return (0);
4856         if (id == POW_SMARTBOMB_WEAPON && !Netgame.DoSmarts)
4857                 return (0);
4858         if (id == POW_GAUSS_WEAPON && !Netgame.DoGauss)
4859                 return (0);
4860         if (id == POW_VULCAN_WEAPON && !Netgame.DoVulcan)
4861                 return (0);
4862         if (id == POW_PLASMA_WEAPON && !Netgame.DoPlasma)
4863                 return (0);
4864         if (id == POW_OMEGA_WEAPON && !Netgame.DoOmega)
4865                 return (0);
4866         if (id == POW_SUPER_LASER && !Netgame.DoSuperLaser)
4867                 return (0);
4868         if (id == POW_PROXIMITY_WEAPON && !Netgame.DoProximity)
4869                 return (0);
4870         if (id==POW_VULCAN_AMMO && (!Netgame.DoVulcan && !Netgame.DoGauss))
4871                 return (0);
4872         if (id == POW_SPREADFIRE_WEAPON && !Netgame.DoSpread)
4873                 return (0);
4874         if (id == POW_SMART_MINE && !Netgame.DoSmartMine)
4875                 return (0);
4876         if (id == POW_SMISSILE1_1 &&  !Netgame.DoFlash)
4877                 return (0);
4878         if (id == POW_SMISSILE1_4 &&  !Netgame.DoFlash)
4879                 return (0);
4880         if (id == POW_GUIDED_MISSILE_1 &&  !Netgame.DoGuided)
4881                 return (0);
4882         if (id == POW_GUIDED_MISSILE_4 &&  !Netgame.DoGuided)
4883                 return (0);
4884         if (id == POW_EARTHSHAKER_MISSILE &&  !Netgame.DoEarthShaker)
4885                 return (0);
4886         if (id == POW_MERCURY_MISSILE_1 &&  !Netgame.DoMercury)
4887                 return (0);
4888         if (id == POW_MERCURY_MISSILE_4 &&  !Netgame.DoMercury)
4889                 return (0);
4890         if (id == POW_CONVERTER &&  !Netgame.DoConverter)
4891                 return (0);
4892         if (id == POW_AMMO_RACK &&  !Netgame.DoAmmoRack)
4893                 return (0);
4894         if (id == POW_HEADLIGHT &&  !Netgame.DoHeadlight)
4895                 return (0);
4896         if (id == POW_LASER &&  !Netgame.DoLaserUpgrade)
4897                 return (0);
4898         if (id == POW_HOMING_AMMO_1 &&  !Netgame.DoHoming)
4899                 return (0);
4900         if (id == POW_HOMING_AMMO_4 &&  !Netgame.DoHoming)
4901                 return (0);
4902         if (id == POW_QUAD_FIRE &&  !Netgame.DoQuadLasers)
4903                 return (0);
4904         if (id == POW_FLAG_BLUE && !(Game_mode & GM_CAPTURE))
4905                 return (0);
4906         if (id == POW_FLAG_RED && !(Game_mode & GM_CAPTURE))
4907                 return (0);
4908
4909         return (1);
4910 }
4911
4912 void multi_send_finish_game ()
4913 {
4914         multibuf[0]=MULTI_FINISH_GAME;
4915         multibuf[1]=Player_num;
4916
4917         multi_send_data (multibuf,2,1);
4918 }
4919
4920
4921 extern void do_final_boss_hacks();
4922 void multi_do_finish_game (char *buf)
4923 {
4924         if (buf[0]!=MULTI_FINISH_GAME)
4925                 return;
4926
4927         if (Current_level_num!=Last_level)
4928                 return;
4929
4930         do_final_boss_hacks();
4931 }
4932
4933 void multi_send_trigger_specific (char pnum,char trig)
4934 {
4935         multibuf[0] = MULTI_START_TRIGGER;
4936         multibuf[1] = trig;
4937
4938         network_send_naked_packet(multibuf, 2, pnum);
4939 }
4940 void multi_do_start_trigger (char *buf)
4941 {
4942         Triggers[(int)buf[1]].flags |=TF_DISABLED;
4943 }
4944
4945 extern char *RankStrings[];
4946
4947 void multi_add_lifetime_kills ()
4948 {
4949         // This function adds a kill to lifetime stats of this player, and possibly
4950         // gives a promotion.  If so, it will tell everyone else
4951
4952         int oldrank;
4953
4954         if (!Game_mode & GM_NETWORK)
4955                 return;
4956
4957         oldrank=GetMyNetRanking();
4958
4959         Netlife_kills++;
4960
4961         if (oldrank!=GetMyNetRanking())
4962         {
4963                 multi_send_ranking();
4964                 if (!FindArg("-norankings"))
4965                 {
4966                         HUD_init_message ("You have been promoted to %s!",RankStrings[GetMyNetRanking()]);
4967                         digi_play_sample (SOUND_BUDDY_MET_GOAL,F1_0*2);
4968                         NetPlayers.players[Player_num].rank=GetMyNetRanking();
4969                 }
4970         }
4971         write_player_file();
4972 }
4973
4974 void multi_add_lifetime_killed ()
4975 {
4976         // This function adds a "killed" to lifetime stats of this player, and possibly
4977         // gives a demotion.  If so, it will tell everyone else
4978
4979         int oldrank;
4980
4981         if (!Game_mode & GM_NETWORK)
4982                 return;
4983
4984         oldrank=GetMyNetRanking();
4985
4986         Netlife_killed++;
4987
4988         if (oldrank!=GetMyNetRanking())
4989         {
4990                 multi_send_ranking();
4991                 NetPlayers.players[Player_num].rank=GetMyNetRanking();
4992
4993                 if (!FindArg("-norankings"))
4994                         HUD_init_message ("You have been demoted to %s!",RankStrings[GetMyNetRanking()]);
4995
4996         }
4997         write_player_file();
4998
4999 }
5000
5001 void multi_send_ranking ()
5002 {
5003         multibuf[0]=(char)MULTI_RANK;
5004         multibuf[1]=(char)Player_num;
5005         multibuf[2]=(char)GetMyNetRanking();
5006
5007         multi_send_data (multibuf,3,1);
5008 }
5009
5010 void multi_do_ranking (char *buf)
5011 {
5012         char rankstr[20];
5013         char pnum=buf[1];
5014         char rank=buf[2];
5015
5016         if (NetPlayers.players[(int)pnum].rank<rank)
5017                 strcpy (rankstr,"promoted");
5018         else if (NetPlayers.players[(int)pnum].rank>rank)
5019                 strcpy (rankstr,"demoted");
5020         else
5021                 return;
5022
5023         NetPlayers.players[(int)pnum].rank=rank;
5024
5025         if (!FindArg("-norankings"))
5026                 HUD_init_message ("%s has been %s to %s!",Players[(int)pnum].callsign,rankstr,RankStrings[(int)rank]);
5027 }
5028 void multi_send_modem_ping ()
5029 {
5030         multibuf[0]=MULTI_MODEM_PING;
5031         multi_send_data (multibuf,1,1);
5032 }
5033 void multi_send_modem_ping_return ()
5034 {
5035         multibuf[0]=MULTI_MODEM_PING_RETURN;
5036         multi_send_data (multibuf,1,1);
5037 }
5038
5039 void  multi_do_modem_ping_return ()
5040 {
5041         if (PingLaunchTime==0)
5042         {
5043                 mprintf ((0,"Got invalid PING RETURN from opponent!\n"));
5044                 return;
5045         }
5046
5047         PingReturnTime=timer_get_fixed_seconds();
5048
5049         HUD_init_message ("Ping time for opponent is %d ms!",f2i(fixmul(PingReturnTime-PingLaunchTime,i2f(1000))));
5050         PingLaunchTime=0;
5051 }
5052
5053
5054 void multi_quick_sound_hack (int num)
5055 {
5056         int length,i;
5057         num = digi_xlat_sound(num);
5058         length=GameSounds[num].length;
5059         ReversedSound.data=(ubyte *)d_malloc (length);
5060         ReversedSound.length=length;
5061
5062         for (i=0;i<length;i++)
5063                 ReversedSound.data[i]=GameSounds[num].data[length-i-1];
5064
5065         SoundHacked=1;
5066 }
5067
5068 void multi_send_play_by_play (int num,int spnum,int dpnum)
5069 {
5070         if (!(Game_mode & GM_HOARD))
5071                 return;
5072
5073         return;
5074         multibuf[0]=MULTI_PLAY_BY_PLAY;
5075         multibuf[1]=(char)num;
5076         multibuf[2]=(char)spnum;
5077         multibuf[3]=(char)dpnum;
5078         multi_send_data (multibuf,4,1);
5079         multi_do_play_by_play (multibuf);
5080 }
5081
5082 void multi_do_play_by_play (char *buf)
5083 {
5084         int whichplay=buf[1];
5085         int spnum=buf[2];
5086         int dpnum=buf[3];
5087
5088         if (!(Game_mode & GM_HOARD))
5089         {
5090                 Int3(); // Get Leighton, something bad has happened.
5091                 return;
5092         }
5093
5094         switch (whichplay)
5095         {
5096         case 0: // Smacked!
5097                 HUD_init_message ("Ouch! %s has been smacked by %s!",Players[dpnum].callsign,Players[spnum].callsign);
5098                 break;
5099         case 1: // Spanked!
5100                 HUD_init_message ("Haha! %s has been spanked by %s!",Players[dpnum].callsign,Players[spnum].callsign);
5101                 break;
5102         default:
5103                 Int3();
5104         }
5105 }
5106
5107 ///
5108 /// CODE TO LOAD HOARD DATA
5109 ///
5110
5111
5112 void init_bitmap(grs_bitmap *bm,int w,int h,int flags,ubyte *data)
5113 {
5114         bm->bm_x = bm->bm_y = 0;
5115         bm->bm_w = bm->bm_rowsize = w;
5116         bm->bm_h = h;
5117         bm->bm_type = BM_LINEAR;
5118         bm->bm_flags = flags;
5119         bm->bm_data = data;
5120         bm->bm_handle = 0;
5121         bm->avg_color = 0;
5122 }
5123
5124 grs_bitmap Orb_icons[2];
5125
5126 int Hoard_goal_eclip;
5127
5128 void init_hoard_data()
5129 {
5130         static int first_time=1;
5131         static int orb_vclip;
5132         int n_orb_frames,n_goal_frames;
5133         int orb_w,orb_h;
5134         int icon_w,icon_h;
5135         ubyte palette[256*3];
5136         CFILE *ifile;
5137         int i,save_pos;
5138         extern int Num_bitmap_files,Num_effects,Num_sound_files;
5139
5140         ifile = cfopen("hoard.ham","rb");
5141         if (ifile == NULL)
5142                 Error("can't open <hoard.ham>");
5143
5144         n_orb_frames = cfile_read_short(ifile);
5145         orb_w = cfile_read_short(ifile);
5146         orb_h = cfile_read_short(ifile);
5147         save_pos = cftell(ifile);
5148         cfseek(ifile,sizeof(palette)+n_orb_frames*orb_w*orb_h,SEEK_CUR);
5149         n_goal_frames = cfile_read_short(ifile);
5150         cfseek(ifile,save_pos,SEEK_SET);
5151
5152         if (first_time) {
5153                 ubyte *bitmap_data;
5154                 int bitmap_num=Num_bitmap_files;
5155
5156                 //Allocate memory for bitmaps
5157                 MALLOC( bitmap_data, ubyte, n_orb_frames*orb_w*orb_h + n_goal_frames*64*64 );
5158
5159                 //Create orb vclip
5160                 orb_vclip = Num_vclips++;
5161                 Assert(Num_vclips <= VCLIP_MAXNUM);
5162                 Vclip[orb_vclip].play_time = F1_0/2;
5163                 Vclip[orb_vclip].num_frames = n_orb_frames;
5164                 Vclip[orb_vclip].frame_time = Vclip[orb_vclip].play_time / Vclip[orb_vclip].num_frames;
5165                 Vclip[orb_vclip].flags = 0;
5166                 Vclip[orb_vclip].sound_num = -1;
5167                 Vclip[orb_vclip].light_value = F1_0;
5168                 for (i=0;i<n_orb_frames;i++) {
5169                         Vclip[orb_vclip].frames[i].index = bitmap_num;
5170                         init_bitmap(&GameBitmaps[bitmap_num],orb_w,orb_h,BM_FLAG_TRANSPARENT,bitmap_data);
5171                         bitmap_data += orb_w*orb_h;
5172                         bitmap_num++;
5173                         Assert(bitmap_num < MAX_BITMAP_FILES);
5174                 }
5175
5176                 //Create obj powerup
5177                 Powerup_info[POW_HOARD_ORB].vclip_num = orb_vclip;
5178                 Powerup_info[POW_HOARD_ORB].hit_sound = -1; //Powerup_info[POW_SHIELD_BOOST].hit_sound;
5179                 Powerup_info[POW_HOARD_ORB].size = Powerup_info[POW_SHIELD_BOOST].size;
5180                 Powerup_info[POW_HOARD_ORB].light = Powerup_info[POW_SHIELD_BOOST].light;
5181
5182                 //Create orb goal wall effect
5183                 Hoard_goal_eclip = Num_effects++;
5184                 Assert(Num_effects < MAX_EFFECTS);
5185                 Effects[Hoard_goal_eclip] = Effects[94];        //copy from blue goal
5186                 Effects[Hoard_goal_eclip].changing_wall_texture = NumTextures;
5187                 Effects[Hoard_goal_eclip].vc.num_frames=n_goal_frames;
5188
5189                 TmapInfo[NumTextures] = TmapInfo[find_goal_texture(TMI_GOAL_BLUE)];
5190                 TmapInfo[NumTextures].eclip_num = Hoard_goal_eclip;
5191                 TmapInfo[NumTextures].flags = TMI_GOAL_HOARD;
5192                 NumTextures++;
5193                 Assert(NumTextures < MAX_TEXTURES);
5194                 for (i=0;i<n_goal_frames;i++) {
5195                         Effects[Hoard_goal_eclip].vc.frames[i].index = bitmap_num;
5196                         init_bitmap(&GameBitmaps[bitmap_num],64,64,0,bitmap_data);
5197                         bitmap_data += 64*64;
5198                         bitmap_num++;
5199                         Assert(bitmap_num < MAX_BITMAP_FILES);
5200                 }
5201
5202         }
5203
5204         //Load and remap bitmap data for orb
5205         cfread(palette,3,256,ifile);
5206         for (i=0;i<n_orb_frames;i++) {
5207                 grs_bitmap *bm = &GameBitmaps[Vclip[orb_vclip].frames[i].index];
5208                 cfread(bm->bm_data,1,orb_w*orb_h,ifile);
5209                 gr_remap_bitmap_good( bm, palette, 255, -1 );
5210         }
5211
5212         //Load and remap bitmap data for goal texture
5213         cfile_read_short(ifile);        //skip frame count
5214         cfread(palette,3,256,ifile);
5215         for (i=0;i<n_goal_frames;i++) {
5216                 grs_bitmap *bm = &GameBitmaps[Effects[Hoard_goal_eclip].vc.frames[i].index];
5217                 cfread(bm->bm_data,1,64*64,ifile);
5218                 gr_remap_bitmap_good( bm, palette, 255, -1 );
5219         }
5220
5221         //Load and remap bitmap data for HUD icons
5222         for (i=0;i<2;i++) {
5223                 icon_w = cfile_read_short(ifile);
5224                 icon_h = cfile_read_short(ifile);
5225                 if (first_time) {
5226                         ubyte *bitmap_data;
5227                         MALLOC( bitmap_data, ubyte, icon_w*icon_h );
5228                         init_bitmap(&Orb_icons[i],icon_w,icon_h,BM_FLAG_TRANSPARENT,bitmap_data);
5229                 }
5230                 cfread(palette,3,256,ifile);
5231                 cfread(Orb_icons[i].bm_data,1,icon_w*icon_h,ifile);
5232                 gr_remap_bitmap_good( &Orb_icons[i], palette, 255, -1 );
5233         }
5234
5235         if (first_time) {
5236
5237                 //Load sounds for orb game
5238
5239                 for (i=0;i<4;i++) {
5240                         int len;
5241
5242                         len = cfile_read_int(ifile);        //get 11k len
5243
5244                         if (digi_sample_rate == SAMPLE_RATE_22K) {
5245                                 cfseek(ifile,len,SEEK_CUR);     //skip over 11k sample
5246                                 len = cfile_read_int(ifile);    //get 22k len
5247                         }
5248
5249                         GameSounds[Num_sound_files+i].length = len;
5250                         GameSounds[Num_sound_files+i].data = d_malloc(len);
5251                         cfread(GameSounds[Num_sound_files+i].data,1,len,ifile);
5252
5253                         if (digi_sample_rate == SAMPLE_RATE_11K) {
5254                                 len = cfile_read_int(ifile);    //get 22k len
5255                                 cfseek(ifile,len,SEEK_CUR);     //skip over 22k sample
5256                         }
5257
5258                         Sounds[SOUND_YOU_GOT_ORB+i] = Num_sound_files+i;
5259                         AltSounds[SOUND_YOU_GOT_ORB+i] = Sounds[SOUND_YOU_GOT_ORB+i];
5260                 }
5261         }
5262
5263         cfclose(ifile);
5264
5265         first_time = 0;
5266 }
5267
5268 void
5269 multi_process_data(char *buf, int len)
5270 {
5271         // Take an entire message (that has already been checked for validity,
5272         // if necessary) and act on it.
5273
5274         int type;
5275         len = len;
5276
5277         type = buf[0];
5278
5279         if (type > MULTI_MAX_TYPE)
5280         {
5281                 mprintf((1, "multi_process_data: invalid type %d.\n", type));
5282                 Int3();
5283                 return;
5284         }
5285
5286
5287 #ifdef NETPROFILING
5288         TTRecv[type]++;
5289         fprintf (RecieveLogFile,"Packet type: %d Len:%d TT=%d\n",type,len,TTRecv[type]);
5290         fflush (RecieveLogFile);
5291 #endif
5292
5293         switch(type)
5294         {
5295         case MULTI_POSITION:
5296                 if (!Endlevel_sequence) multi_do_position(buf); break;
5297         case MULTI_REAPPEAR:
5298                 if (!Endlevel_sequence) multi_do_reappear(buf); break;
5299         case MULTI_FIRE:
5300                 if (!Endlevel_sequence) multi_do_fire(buf); break;
5301         case MULTI_KILL:
5302                 multi_do_kill(buf); break;
5303         case MULTI_REMOVE_OBJECT:
5304                 if (!Endlevel_sequence) multi_do_remobj(buf); break;
5305         case MULTI_PLAYER_DROP:
5306         case MULTI_PLAYER_EXPLODE:
5307                 if (!Endlevel_sequence) multi_do_player_explode(buf); break;
5308         case MULTI_MESSAGE:
5309                 if (!Endlevel_sequence) multi_do_message(buf); break;
5310         case MULTI_QUIT:
5311                 if (!Endlevel_sequence) multi_do_quit(buf); break;
5312         case MULTI_BEGIN_SYNC:
5313                 break;
5314         case MULTI_CONTROLCEN:
5315                 if (!Endlevel_sequence) multi_do_controlcen_destroy(buf); break;
5316         case MULTI_POWERUP_UPDATE:
5317                 if (!Endlevel_sequence) multi_do_powerup_update(buf); break;
5318         case MULTI_SOUND_FUNCTION:
5319                 multi_do_sound_function(buf); break;
5320         case MULTI_MARKER:
5321                 if (!Endlevel_sequence) multi_do_drop_marker (buf); break;
5322         case MULTI_DROP_WEAPON:
5323                 if (!Endlevel_sequence) multi_do_drop_weapon(buf); break;
5324         case MULTI_DROP_FLAG:
5325                 if (!Endlevel_sequence) multi_do_drop_flag(buf); break;
5326         case MULTI_GUIDED:
5327                 if (!Endlevel_sequence) multi_do_guided (buf); break;
5328         case MULTI_STOLEN_ITEMS:
5329                 if (!Endlevel_sequence) multi_do_stolen_items(buf); break;
5330         case MULTI_WALL_STATUS:
5331                 if (!Endlevel_sequence) multi_do_wall_status(buf); break;
5332         case MULTI_HEARTBEAT:
5333                 if (!Endlevel_sequence) multi_do_heartbeat (buf); break;
5334         case MULTI_SEISMIC:
5335                 if (!Endlevel_sequence) multi_do_seismic (buf); break;
5336         case MULTI_LIGHT:
5337                 if (!Endlevel_sequence) multi_do_light (buf); break;
5338         case MULTI_KILLGOALS:
5339
5340                 if (!Endlevel_sequence) multi_do_kill_goal_counts (buf); break;
5341         case MULTI_ENDLEVEL_START:
5342                 if (!Endlevel_sequence) multi_do_escape(buf); break;
5343         case MULTI_END_SYNC:
5344                 break;
5345         case MULTI_CLOAK:
5346                 if (!Endlevel_sequence) multi_do_cloak(buf); break;
5347         case MULTI_DECLOAK:
5348                 if (!Endlevel_sequence) multi_do_decloak(buf); break;
5349         case MULTI_DOOR_OPEN:
5350                 if (!Endlevel_sequence) multi_do_door_open(buf); break;
5351         case MULTI_CREATE_EXPLOSION:
5352                 if (!Endlevel_sequence) multi_do_create_explosion(buf); break;
5353         case MULTI_CONTROLCEN_FIRE:
5354                 if (!Endlevel_sequence) multi_do_controlcen_fire(buf); break;
5355         case MULTI_CREATE_POWERUP:
5356                 if (!Endlevel_sequence) multi_do_create_powerup(buf); break;
5357         case MULTI_PLAY_SOUND:
5358                 if (!Endlevel_sequence) multi_do_play_sound(buf); break;
5359         case MULTI_CAPTURE_BONUS:
5360                 if (!Endlevel_sequence) multi_do_capture_bonus(buf); break;
5361         case MULTI_ORB_BONUS:
5362                 if (!Endlevel_sequence) multi_do_orb_bonus(buf); break;
5363         case MULTI_GOT_FLAG:
5364                 if (!Endlevel_sequence) multi_do_got_flag(buf); break;
5365         case MULTI_GOT_ORB:
5366                 if (!Endlevel_sequence) multi_do_got_orb(buf); break;
5367         case MULTI_PLAY_BY_PLAY:
5368                 if (!Endlevel_sequence) multi_do_play_by_play(buf); break;
5369         case MULTI_RANK:
5370                 if (!Endlevel_sequence) multi_do_ranking (buf); break;
5371         case MULTI_MODEM_PING:
5372                 if (!Endlevel_sequence) multi_send_modem_ping_return(); break;
5373         case MULTI_MODEM_PING_RETURN:
5374                 if (!Endlevel_sequence) multi_do_modem_ping_return(); break;
5375 #ifndef SHAREWARE
5376         case MULTI_FINISH_GAME:
5377                 multi_do_finish_game(buf); break;  // do this one regardless of endsequence
5378         case MULTI_ROBOT_CONTROLS:
5379                 if (!Endlevel_sequence) multi_do_robot_controls(buf); break;
5380         case MULTI_ROBOT_CLAIM:
5381                 if (!Endlevel_sequence) multi_do_claim_robot(buf); break;
5382         case MULTI_ROBOT_POSITION:
5383                 if (!Endlevel_sequence) multi_do_robot_position(buf); break;
5384         case MULTI_ROBOT_EXPLODE:
5385                 if (!Endlevel_sequence) multi_do_robot_explode(buf); break;
5386         case MULTI_ROBOT_RELEASE:
5387                 if (!Endlevel_sequence) multi_do_release_robot(buf); break;
5388         case MULTI_ROBOT_FIRE:
5389                 if (!Endlevel_sequence) multi_do_robot_fire(buf); break;
5390 #endif
5391         case MULTI_SCORE:
5392                 if (!Endlevel_sequence) multi_do_score(buf); break;
5393         case MULTI_CREATE_ROBOT:
5394                 if (!Endlevel_sequence) multi_do_create_robot(buf); break;
5395         case MULTI_TRIGGER:
5396                 if (!Endlevel_sequence) multi_do_trigger(buf); break;
5397         case MULTI_START_TRIGGER:
5398                 if (!Endlevel_sequence) multi_do_start_trigger(buf); break;
5399         case MULTI_FLAGS:
5400                 if (!Endlevel_sequence) multi_do_flags(buf); break;
5401         case MULTI_DROP_BLOB:
5402                 if (!Endlevel_sequence) multi_do_drop_blob(buf); break;
5403         case MULTI_ACTIVE_DOOR:
5404                 if (!Endlevel_sequence) multi_do_active_door(buf); break;
5405         case MULTI_BOSS_ACTIONS:
5406                 if (!Endlevel_sequence) multi_do_boss_actions(buf); break;
5407         case MULTI_CREATE_ROBOT_POWERUPS:
5408                 if (!Endlevel_sequence) multi_do_create_robot_powerups(buf); break;
5409         case MULTI_HOSTAGE_DOOR:
5410                 if (!Endlevel_sequence) multi_do_hostage_door_status(buf); break;
5411         case MULTI_SAVE_GAME:
5412                 if (!Endlevel_sequence) multi_do_save_game(buf); break;
5413         case MULTI_RESTORE_GAME:
5414                 if (!Endlevel_sequence) multi_do_restore_game(buf); break;
5415         case MULTI_REQ_PLAYER:
5416                 if (!Endlevel_sequence) multi_do_req_player(buf); break;
5417         case MULTI_SEND_PLAYER:
5418                 if (!Endlevel_sequence) multi_do_send_player(buf); break;
5419
5420         default:
5421                 mprintf((1, "Invalid type in multi_process_input().\n"));
5422                 Int3();
5423         }
5424 }