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