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