]> icculus.org git repositories - taylor/freespace2.git/blob - src/io/keycontrol.cpp
merge in updated FS1 code
[taylor/freespace2.git] / src / io / keycontrol.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Io/KeyControl.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Routines to read and deal with keyboard input.
16  *
17  * $Log$
18  * Revision 1.5  2003/05/25 02:30:42  taylor
19  * Freespace 1 support
20  *
21  * Revision 1.4  2002/07/28 05:05:08  relnev
22  * removed some old stuff
23  *
24  * Revision 1.3  2002/06/18 08:58:53  relnev
25  * last few struct changes
26  *
27  * Revision 1.2  2002/06/09 04:41:21  relnev
28  * added copyright header
29  *
30  * Revision 1.1.1.1  2002/05/03 03:28:09  root
31  * Initial import.
32  *
33  * 
34  * 47    9/09/99 11:40p Dave
35  * Handle an SDL_assert() in beam code. Added supernova sounds. Play the right
36  * 2 end movies properly, based upon what the player did in the mission.
37  * 
38  * 46    9/07/99 11:26p Andsager
39  * Fix "r" targeting key, making evaluate_ship_as_closest_target() and
40  * hud_target_live_turret() consider if turret is targeting player
41  * 
42  * 45    9/03/99 1:31a Dave
43  * CD checking by act. Added support to play 2 cutscenes in a row
44  * seamlessly. Fixed super low level cfile bug related to files in the
45  * root directory of a CD. Added cheat code to set campaign mission # in
46  * main hall.
47  * 
48  * 44    9/01/99 10:09a Dave
49  * Pirate bob.
50  * 
51  * 43    8/27/99 10:36a Dave
52  * Impose a 2% penalty for hitting the shield balance key.
53  * 
54  * 42    8/27/99 9:57a Dave
55  * Enabled standard cheat codes. Allow player to continue in a campaing
56  * after using cheat codes.
57  * 
58  * 41    8/26/99 9:45a Dave
59  * First pass at easter eggs and cheats.
60  * 
61  * 40    8/24/99 1:49a Dave
62  * Fixed client-side afterburner stuttering. Added checkbox for no version
63  * checking on PXO join. Made button info passing more friendly between
64  * client and server.
65  * 
66  * 39    8/22/99 5:53p Dave
67  * Scoring fixes. Added self destruct key. Put callsigns in the logfile
68  * instead of ship designations for multiplayer players.
69  * 
70  * 38    8/19/99 10:59a Dave
71  * Packet loss detection.
72  * 
73  * 37    8/18/99 12:09p Andsager
74  * Add debug if message has no anim for message.  Make messages come from
75  * wing leader.
76  * 
77  * 36    8/05/99 2:05a Dave
78  * Whee.
79  * 
80  * 35    8/01/99 12:39p Dave
81  * Added HUD contrast control key (for nebula).
82  * 
83  * 34    7/31/99 2:30p Dave
84  * Added nifty mission message debug viewing keys.
85  * 
86  * 33    7/21/99 8:10p Dave
87  * First run of supernova effect.
88  * 
89  * 32    7/15/99 4:09p Andsager
90  * Disable cheats for FS2_DEMO
91  * 
92  * 31    7/15/99 9:20a Andsager
93  * FS2_DEMO initial checkin
94  * 
95  * 30    7/07/99 3:32p Dave
96  * Oops. Forgot to remove this.
97  * 
98  * 29    7/02/99 4:31p Dave
99  * Much more sophisticated lightning support.
100  * 
101  * 28    6/10/99 3:43p Dave
102  * Do a better job of syncing text colors to HUD gauges.
103  * 
104  * 27    6/09/99 2:55p Andsager
105  * Allow multiple asteroid subtypes (of large, medium, small) and follow
106  * family.
107  * 
108  * 26    5/24/99 5:45p Dave
109  * Added detail levels to the nebula, with a decent speedup. Split nebula
110  * lightning into its own section.
111  * 
112  * 25    5/08/99 8:25p Dave
113  * Upped object pairs. First run of nebula lightning.
114  * 
115  * 24    5/05/99 9:02p Dave
116  * Fixed D3D aabitmap rendering. Spiffed up nebula effect a bit (added
117  * rotations, tweaked values, made bitmap selection more random). Fixed
118  * D3D beam weapon clipping problem. Added D3d frame dumping.
119  * 
120  * 23    5/03/99 9:07a Dave
121  * Pirate Bob. Changed beam test code a bit.
122  * 
123  * 22    4/21/99 6:15p Dave
124  * Did some serious housecleaning in the beam code. Made it ready to go
125  * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
126  * a handy macro for recalculating collision pairs for a given object.
127  * 
128  * 21    4/16/99 5:54p Dave
129  * Support for on/off style "stream" weapons. Real early support for
130  * target-painting lasers.
131  * 
132  * 20    3/31/99 8:24p Dave
133  * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
134  * and background nebulae. Added per-ship non-dimming pixel colors.
135  * 
136  * 19    3/29/99 6:17p Dave
137  * More work on demo system. Got just about everything in except for
138  * blowing ships up, secondary weapons and player death/warpout.
139  * 
140  * 18    3/28/99 5:58p Dave
141  * Added early demo code. Make objects move. Nice and framerate
142  * independant, but not much else. Don't use yet unless you're me :)
143  * 
144  * 17    3/09/99 6:24p Dave
145  * More work on object update revamping. Identified several sources of
146  * unnecessary bandwidth.
147  * 
148  * 16    2/21/99 6:01p Dave
149  * Fixed standalone WSS packets. 
150  * 
151  * 15    2/21/99 1:48p Dave
152  * Some code for monitoring datarate for multiplayer in detail.
153  * 
154  * 14    1/21/99 2:06p Dave
155  * Final checkin for multiplayer testing.
156  * 
157  * 13    1/19/99 3:57p Andsager
158  * Round 2 of variables
159  * 
160  * 12    1/12/99 12:53a Dave
161  * More work on beam weapons - made collision detection very efficient -
162  * collide against all object types properly - made 3 movement types
163  * smooth. Put in test code to check for possible non-darkening pixels on
164  * object textures.
165  * 
166  * 11    1/08/99 2:08p Dave
167  * Fixed software rendering for pofview. Super early support for AWACS and
168  * beam weapons.
169  * 
170  * 10    12/06/98 2:36p Dave
171  * Drastically improved nebula fogging.
172  * 
173  * 9     12/04/98 3:37p Andsager
174  * Added comment out asteroid launcher
175  * 
176  * 8     11/19/98 4:19p Dave
177  * Put IPX sockets back in psnet. Consolidated all multiplayer config
178  * files into one.
179  * 
180  * 7     11/05/98 5:55p Dave
181  * Big pass at reducing #includes
182  * 
183  * 6     10/26/98 9:42a Dave
184  * Early flak gun support.
185  * 
186  * 5     10/20/98 1:39p Andsager
187  * Make so sparks follow animated ship submodels.  Modify
188  * ship_weapon_do_hit_stuff() and ship_apply_local_damage() to add
189  * submodel_num.  Add submodel_num to multiplayer hit packet.
190  * 
191  * 4     10/13/98 9:28a Dave
192  * Started neatening up freespace.h. Many variables renamed and
193  * reorganized. Added AlphaColors.[h,cpp]
194  * 
195  * 3     10/09/98 2:57p Dave
196  * Starting splitting up OS stuff.
197  * 
198  * 2     10/07/98 10:53a Dave
199  * Initial checkin.
200  * 
201  * 1     10/07/98 10:49a Dave
202  * 
203  * 387   6/17/98 11:02a Lawrance
204  * show what cheat code is in the comment
205  * 
206  * 386   6/09/98 5:15p Lawrance
207  * French/German localization
208  * 
209  * 385   6/09/98 10:31a Hoffoss
210  * Created index numbers for all xstr() references.  Any new xstr() stuff
211  * added from here on out should be added to the end if the list.  The
212  * current list count can be found in FreeSpace.cpp (search for
213  * XSTR_SIZE).
214  * 
215  * 384   6/01/98 11:43a John
216  * JAS & MK:  Classified all strings for localization.
217  * 
218  * 383   5/24/98 1:46p Mike
219  * Final cheat codes.
220  * 
221  * 382   5/19/98 2:20p Mike
222  * Comment out nprintf().
223  * 
224  * 381   5/19/98 12:19p Mike
225  * Cheat codes!
226  * 
227  * 380   5/19/98 11:11a Lawrance
228  * Make 'G' only target hostiles
229  * 
230  * 379   5/18/98 11:00p Mike
231  * Adding support for cheat system.
232  * 
233  * 378   5/18/98 12:41a Allender
234  * fixed subsystem problems on clients (i.e. not reporting properly on
235  * damage indicator).  Fixed ingame join problem with respawns.  minor
236  * comm menu stuff
237  * 
238  * 377   5/17/98 1:43a Dave
239  * Eradicated chatbox problems. Remove speed match for observers. Put in
240  * help screens for PXO. Fix messaging and end mission privelges. Fixed
241  * team select screen bugs. Misc UI fixes.
242  * 
243  * 376   5/15/98 8:36p Lawrance
244  * Add 'target ship that last sent transmission' target key
245  * 
246  * 375   5/14/98 11:07a Lawrance
247  * Ensure looped sounds get stopped before stopping all channels
248  * 
249  * 374   5/12/98 11:59p Dave
250  * Put in some more functionality for Parallax Online.
251  * 
252  * 373   5/11/98 5:29p Hoffoss
253  * Added mouse button mapped to joystick button support.
254  * 
255  * 372   5/08/98 10:14a Lawrance
256  * Play sound when auto-targeting gets toggled.
257  * 
258  * 371   5/04/98 9:25a Allender
259  * don't allow time compression in multiplayer
260  * 
261  * 370   4/30/98 4:43p Allender
262  * trap player obj when changing dual fire status on ship
263  * 
264  * 369   4/30/98 4:16p Peter
265  * fixes for critical button functions when a player (client) ship is dead
266  * or dying
267  * 
268  * 368   4/27/98 9:03a Dave
269  * Fixed a multiplayer sequencing bug where paused players who were in the
270  * options screen got an SDL_assert when unpausing. Removed an optimiized
271  * build warning in keycontrol. 
272  * 
273  * 367   4/26/98 4:29p Lawrance
274  * Put back time compression keys... somehow they were trashed by a later
275  * checkin.
276  * 
277  * 366   4/25/98 5:36p Mike
278  * Prevent player warpout if engine < 10%.
279  * 
280  * $NoKeywords: $
281  */
282
283 #include "pstypes.h"
284 #include "linklist.h"
285 #include "key.h"
286 #include "joy.h"
287 #include "timer.h"
288 #include "ship.h"
289 #include "player.h"
290 #include "weapon.h"
291 #include "hud.h"
292 #include "contexthelp.h"
293 #include "gamesequence.h"
294 #include "osapi.h"
295 #include "missiongoals.h"
296 #include "hud.h"
297 #include "hudtarget.h"
298 #include "optionsmenu.h"
299 #include "freespace.h"
300 #include "controlsconfig.h"
301 #include "mouse.h"
302 #include "hudets.h"
303 #include "multi.h"
304 #include "multiutil.h"
305 #include "sound.h"
306 #include "gamesnd.h"
307 #include "bmpman.h"
308 #include "hudsquadmsg.h"
309 #include "eventmusic.h"
310 #include "animplay.h"
311 #include "freespace.h"
312 #include "cmeasure.h"
313 #include "missionhotkey.h"
314 #include "afterburner.h"
315 #include "missionparse.h"
316 #include "hudescort.h"
317 #include "hudshield.h"
318 #include "multiutil.h"
319 #include "multimsgs.h"
320 #include "keycontrol.h"
321 #include "shiphit.h"
322 #include "shipfx.h"
323 #include "hud.h"
324 #include "hudobserver.h"
325 #include "missionlog.h"
326 #include "hudtargetbox.h"
327 #include "popup.h"
328 #include "objcollide.h"
329 #include "hudconfig.h"
330 #include "missioncampaign.h"
331 #include "rtvoice.h"
332 #include "multi_respawn.h"
333 #include "multi_pmsg.h"
334 #include "crypt.h"
335 #include "ui.h"
336 #include "multi_pause.h"
337 #include "multi_observer.h"
338 #include "multi_endgame.h"
339 #include "beam.h"
340 #include "neblightning.h"
341 #include "supernova.h"
342 #include "missionmessage.h"
343 #include "mainhallmenu.h"
344 #include "aigoals.h"
345 #include "localize.h"
346
347 // --------------------------------------------------------------
348 // Global to file 
349 // --------------------------------------------------------------
350
351 // --------------------------------------------------------------
352 // Externals
353 // --------------------------------------------------------------
354 typedef struct asteroid_field {
355         vector  min_bound;                                              //      Minimum range of field.
356         vector  max_bound;                                              //      Maximum range of field.
357         vector  vel;                                                            //      Average asteroid moves at this velocity.
358         float           speed;                                                  // Average speed of field
359         int             num_initial_asteroids;          //      Number of asteroids at creation.
360 } asteroid_field;
361
362 #define CHEAT_BUFFER_LEN        20
363 #define CHEATSPOT                               (CHEAT_BUFFER_LEN - 1)
364
365 char CheatBuffer[CHEAT_BUFFER_LEN+1];
366
367 #if defined(FS2_DEMO)
368         const char *Cheat_code_demo = NOX("33BE^(8]C01(:=BHt");
369 #elif defined(MAKE_FS1)
370         const char *Cheat_code_gr = NOX("BNdEgDB\\s?0XD1?0)");          // de:www.volition-inc.com
371         const char *Cheat_code = NOX("E5B?(\"=H'5UTE$o%D");                     // www.volition-inc.com
372         const char *Cheat_code_movies = NOX("iC:1C06,'00SoXyY6");               // freespacestandsalone
373 #else
374         const char *Cheat_code = NOX("33BE^(8]C01(:=BHt");                                      // www.freespace2.com
375         const char *Cheat_code_fish = NOX("bDc9y+$;#AIDRoouM");                 // vasudanswuvfishes
376         const char *Cheat_code_headz = NOX("!;:::@>F7L?@@2:@A");                        // humanheadsinside.
377         const char *Cheat_code_tooled = NOX("sipp-^rM@L!U^usjX");               // tooledworkedowned
378         const char *Cheat_code_pirate = NOX("MAP4YP[4=-2uC(yJ^");               // arrrrwalktheplank    
379         const char *Cheat_code_skip = NOX("7!ICkSI\"(8n3JesBP");                        // skipmemymissionyo
380 #endif
381                                                                                   // 666)6=N79+Z45=BE0e
382 int Tool_enabled = 0;
383
384         /*
385 #else 
386         // list of the cheat codes
387         //#ifdef INTERPLAYQA
388         // "DavidPerry" NOX("0!XZQ*K.pu");
389         // NOX("&BvWJe=a?$VP*=@2W,2Y"); // Super-secret 20 character string!
390         //NOX("STs`nHqW\\lv#KD_aCSWN"); //      solveditonceandforall (note double \\ as string contains \.
391         //XSTR:OFF
392         char *Cheat_code_in_game = NOX("///FES)P<A5=7CCB!n10"); //      www.volition-inc.com
393         char *Cheat_code_movies =  NOX("&BvWJe=a?$VP*=@2W,2Y"); // freespacestandsalone
394         char *Cheat_code_pirate = NOX("%,sPzoE>\\+_(Qs#+h-8o");                         // arrwalktheplankmatey
395         //XSTR:ON
396 #endif
397         */
398
399 int All_movies_enabled = 0;
400
401 //int Debug_allowed = 0;
402
403 //#endif
404
405 extern int AI_watch_object;
406 extern int Countermeasures_enabled;
407
408 extern fix Game_time_compression;
409
410 extern float do_subobj_hit_stuff(object *ship_obj, object *other_obj, vector *hitpos, float damage);
411
412 extern void mission_goal_mark_all_true( int type );
413
414 int Normal_key_set[] = {
415         TARGET_NEXT,
416         TARGET_PREV,
417         TARGET_NEXT_CLOSEST_HOSTILE,
418         TARGET_PREV_CLOSEST_HOSTILE,
419         TARGET_NEXT_CLOSEST_FRIENDLY,
420         TARGET_PREV_CLOSEST_FRIENDLY,
421         TARGET_TARGETS_TARGET,
422         TARGET_SHIP_IN_RETICLE,
423         TARGET_LAST_TRANMISSION_SENDER,
424         TARGET_CLOSEST_SHIP_ATTACKING_TARGET,
425         TARGET_CLOSEST_SHIP_ATTACKING_SELF,
426         STOP_TARGETING_SHIP,
427         TOGGLE_AUTO_TARGETING,
428         TARGET_SUBOBJECT_IN_RETICLE,
429         TARGET_PREV_SUBOBJECT,
430         TARGET_NEXT_SUBOBJECT,
431         STOP_TARGETING_SUBSYSTEM,
432
433         TARGET_NEXT_UNINSPECTED_CARGO,
434         TARGET_PREV_UNINSPECTED_CARGO,
435         TARGET_NEWEST_SHIP,
436         TARGET_NEXT_LIVE_TURRET,
437         TARGET_PREV_LIVE_TURRET,
438         TARGET_NEXT_BOMB,
439         TARGET_PREV_BOMB,
440
441         ATTACK_MESSAGE,
442         DISARM_MESSAGE,
443         DISABLE_MESSAGE,
444         ATTACK_SUBSYSTEM_MESSAGE,
445         CAPTURE_MESSAGE,
446         ENGAGE_MESSAGE,
447         FORM_MESSAGE,
448         PROTECT_MESSAGE,
449         COVER_MESSAGE,
450         WARP_MESSAGE,
451         REARM_MESSAGE,
452         IGNORE_MESSAGE,
453         SQUADMSG_MENU,
454
455         VIEW_CHASE,
456         VIEW_OTHER_SHIP,
457
458         SHOW_GOALS,
459         END_MISSION,
460
461         ADD_REMOVE_ESCORT,
462         ESCORT_CLEAR,
463         TARGET_NEXT_ESCORT_SHIP,
464
465         XFER_SHIELD,
466         XFER_LASER,
467         INCREASE_SHIELD,
468         INCREASE_WEAPON,
469         INCREASE_ENGINE,
470         DECREASE_SHIELD,
471         DECREASE_WEAPON,
472         DECREASE_ENGINE,
473         ETS_EQUALIZE,
474         SHIELD_EQUALIZE,
475         SHIELD_XFER_TOP,
476         SHIELD_XFER_BOTTOM,
477         SHIELD_XFER_RIGHT,
478         SHIELD_XFER_LEFT,
479
480         CYCLE_NEXT_PRIMARY,
481         CYCLE_PREV_PRIMARY,
482         CYCLE_SECONDARY,
483         CYCLE_NUM_MISSLES,
484         RADAR_RANGE_CYCLE,
485
486         MATCH_TARGET_SPEED,
487         TOGGLE_AUTO_MATCH_TARGET_SPEED,
488
489         VIEW_EXTERNAL,
490         VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK,
491         LAUNCH_COUNTERMEASURE,
492         ONE_THIRD_THROTTLE,
493         TWO_THIRDS_THROTTLE,
494         PLUS_5_PERCENT_THROTTLE,
495         MINUS_5_PERCENT_THROTTLE,
496         ZERO_THROTTLE,
497         MAX_THROTTLE,
498
499         TARGET_CLOSEST_REPAIR_SHIP,
500
501         MULTI_MESSAGE_ALL,
502         MULTI_MESSAGE_FRIENDLY,
503         MULTI_MESSAGE_HOSTILE,
504         MULTI_MESSAGE_TARGET,
505         MULTI_OBSERVER_ZOOM_TO,
506
507         TIME_SPEED_UP,
508         TIME_SLOW_DOWN,
509 #ifndef MAKE_FS1
510         TOGGLE_HUD_CONTRAST,
511
512         MULTI_TOGGLE_NETINFO,
513         MULTI_SELF_DESTRUCT
514 #endif
515 };
516
517 int Dead_key_set[] = {
518         TARGET_NEXT,
519         TARGET_PREV,
520         TARGET_NEXT_CLOSEST_HOSTILE,
521         TARGET_PREV_CLOSEST_HOSTILE,
522         TARGET_NEXT_CLOSEST_FRIENDLY,
523         TARGET_PREV_CLOSEST_FRIENDLY,
524         TARGET_TARGETS_TARGET,
525         TARGET_CLOSEST_SHIP_ATTACKING_TARGET,
526         STOP_TARGETING_SHIP,
527         TOGGLE_AUTO_TARGETING,
528         TARGET_SUBOBJECT_IN_RETICLE,
529         TARGET_PREV_SUBOBJECT,
530         TARGET_NEXT_SUBOBJECT,
531         STOP_TARGETING_SUBSYSTEM,
532         TARGET_NEWEST_SHIP,
533         TARGET_NEXT_LIVE_TURRET,
534         TARGET_PREV_LIVE_TURRET,
535         TARGET_NEXT_BOMB,
536         TARGET_PREV_BOMB,
537
538         VIEW_CHASE,
539         VIEW_OTHER_SHIP,
540
541         SHOW_GOALS,
542
543         ADD_REMOVE_ESCORT,
544         ESCORT_CLEAR,
545         TARGET_NEXT_ESCORT_SHIP,
546         TARGET_CLOSEST_REPAIR_SHIP,     
547
548         MULTI_MESSAGE_ALL,
549         MULTI_MESSAGE_FRIENDLY,
550         MULTI_MESSAGE_HOSTILE,
551         MULTI_MESSAGE_TARGET,
552         MULTI_OBSERVER_ZOOM_TO,
553
554         TIME_SPEED_UP,
555         TIME_SLOW_DOWN
556 };
557
558 int Critical_key_set[] = {              
559         CYCLE_SECONDARY,                        
560         CYCLE_NUM_MISSLES,                      
561         INCREASE_WEAPON,                        
562         DECREASE_WEAPON,        
563         INCREASE_SHIELD,                        
564         DECREASE_SHIELD,                        
565         INCREASE_ENGINE,                        
566         DECREASE_ENGINE,                        
567         ETS_EQUALIZE,
568         SHIELD_EQUALIZE,                        
569         SHIELD_XFER_TOP,                        
570         SHIELD_XFER_BOTTOM,                     
571         SHIELD_XFER_LEFT,                       
572         SHIELD_XFER_RIGHT,                      
573         XFER_SHIELD,                    
574         XFER_LASER,                     
575 };
576
577 int Non_critical_key_set[] = {
578         CYCLE_NEXT_PRIMARY,             
579         CYCLE_PREV_PRIMARY,                     
580         MATCH_TARGET_SPEED,                     
581         TOGGLE_AUTO_MATCH_TARGET_SPEED,                 
582         TARGET_NEXT,    
583         TARGET_PREV,                    
584         TARGET_NEXT_CLOSEST_HOSTILE,                    
585         TARGET_PREV_CLOSEST_HOSTILE,                    
586         TOGGLE_AUTO_TARGETING,                  
587         TARGET_NEXT_CLOSEST_FRIENDLY,                   
588         TARGET_PREV_CLOSEST_FRIENDLY,                   
589         TARGET_SHIP_IN_RETICLE,                 
590         TARGET_LAST_TRANMISSION_SENDER,
591         TARGET_CLOSEST_REPAIR_SHIP,                     
592         TARGET_CLOSEST_SHIP_ATTACKING_TARGET,                   
593         STOP_TARGETING_SHIP,                    
594         TARGET_CLOSEST_SHIP_ATTACKING_SELF,                     
595         TARGET_TARGETS_TARGET,                  
596         TARGET_SUBOBJECT_IN_RETICLE,                    
597         TARGET_PREV_SUBOBJECT,                  
598         TARGET_NEXT_SUBOBJECT,                  
599         STOP_TARGETING_SUBSYSTEM,
600         TARGET_NEXT_BOMB,
601         TARGET_PREV_BOMB,
602         TARGET_NEXT_UNINSPECTED_CARGO,
603         TARGET_PREV_UNINSPECTED_CARGO,
604         TARGET_NEWEST_SHIP,
605         TARGET_NEXT_LIVE_TURRET,
606         TARGET_PREV_LIVE_TURRET,
607         ATTACK_MESSAGE,
608         DISARM_MESSAGE,
609         DISABLE_MESSAGE,
610         ATTACK_SUBSYSTEM_MESSAGE,
611         CAPTURE_MESSAGE,
612         ENGAGE_MESSAGE,
613    FORM_MESSAGE,
614         PROTECT_MESSAGE,
615         COVER_MESSAGE,
616         WARP_MESSAGE,
617         IGNORE_MESSAGE,
618         REARM_MESSAGE,
619         VIEW_CHASE,
620         VIEW_EXTERNAL,
621         VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK,
622         VIEW_OTHER_SHIP,
623         RADAR_RANGE_CYCLE,
624         SQUADMSG_MENU,
625         SHOW_GOALS,
626         END_MISSION,
627         ADD_REMOVE_ESCORT,
628         ESCORT_CLEAR,
629         TARGET_NEXT_ESCORT_SHIP,        
630         MULTI_MESSAGE_ALL,
631         MULTI_MESSAGE_FRIENDLY,
632         MULTI_MESSAGE_HOSTILE,
633         MULTI_MESSAGE_TARGET,
634         MULTI_OBSERVER_ZOOM_TO,
635 #ifndef MAKE_FS1
636         TOGGLE_HUD_CONTRAST,
637
638         MULTI_TOGGLE_NETINFO,
639         MULTI_SELF_DESTRUCT
640 #endif
641 };
642
643
644 // set sizes of the key sets automatically
645 int Normal_key_set_size = sizeof(Normal_key_set) / sizeof(int);
646 int Dead_key_set_size = sizeof(Dead_key_set) / sizeof(int);
647 int Critical_key_set_size = sizeof(Critical_key_set) / sizeof(int);
648 int Non_critical_key_set_size = sizeof(Non_critical_key_set) / sizeof(int);
649
650 // --------------------------------------------------------------
651 // routine to process keys used only for debugging
652 // --------------------------------------------------------------
653 //#ifndef NDEBUG
654
655 void debug_cycle_player_ship(int delta)
656 {
657         if ( Player_obj == NULL )
658                 return;
659
660         int si_index = Ships[Player_obj->instance].ship_info_index;
661         int sanity = 0;
662         ship_info       *sip;
663         while ( TRUE ) {
664                 si_index += delta;
665                 if ( si_index > MAX_SHIP_TYPES ){
666                         si_index = 0;
667                 }
668                 if ( si_index < 0 ){
669                         si_index = MAX_SHIP_TYPES - 1;
670                 }
671                 sip = &Ship_info[si_index];
672                 if ( sip->flags & SIF_PLAYER_SHIP ){
673                         break;
674                 }
675
676                 // just in case
677                 sanity++;
678                 if ( sanity > MAX_SHIP_TYPES ){
679                         break;
680                 }
681         }
682
683         change_ship_type(Player_obj->instance, si_index);
684         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Player ship changed to %s", 0), Ship_info[si_index].name);                 
685 }
686
687 // cycle targeted ship to next ship in that species
688 void debug_cycle_targeted_ship(int delta)
689 {
690         object          *objp;
691         ship_info       *sip;
692         int                     si_index, species;
693         char                    name[NAME_LENGTH];
694
695         if ( Player_ai->target_objnum == -1 )
696                 return;
697
698         objp = &Objects[Player_ai->target_objnum];
699         if ( objp->type != OBJ_SHIP )
700                 return;
701
702         si_index = Ships[objp->instance].ship_info_index;
703         SDL_assert(si_index != -1 );
704         species = Ship_info[si_index].species;
705
706         int sanity = 0;
707
708         while ( TRUE ) {
709                 si_index += delta;
710                 if ( si_index > MAX_SHIP_TYPES )
711                         si_index = 0;
712                 if ( si_index < 0 )
713                         si_index = MAX_SHIP_TYPES-1;
714
715         
716                 sip = &Ship_info[si_index];
717         
718                 // if it has test in the name, jump over it
719                 SDL_strlcpy(name, sip->name, SDL_arraysize(name));
720                 SDL_strlwr(name);
721                 if ( strstr(name,NOX("test")) != NULL )
722                         continue;
723
724                 if ( sip->species == species && (sip->flags & (SIF_FIGHTER | SIF_BOMBER | SIF_TRANSPORT) ) )
725                         break;
726
727                 // just in case
728                 sanity++;
729                 if ( sanity > MAX_SHIP_TYPES )
730                         break;
731         }
732
733         change_ship_type(objp->instance, si_index);
734         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Changed player target to %s", 1), Ship_info[si_index].name);                       
735 }
736
737 void debug_max_secondary_weapons(object *objp)
738 {
739         int index;
740         ship *shipp = &Ships[objp->instance];
741         ship_info *sip = &Ship_info[shipp->ship_info_index];
742
743         for ( index = 0; index < MAX_SECONDARY_BANKS; index++ ) {
744                 shipp->weapons.secondary_bank_ammo[index] = sip->secondary_bank_ammo_capacity[index];
745         }
746 }
747
748 void debug_change_song(int delta)
749 {
750         char buf[256];
751         if ( event_music_next_soundtrack(delta) != -1 ) {
752                 event_music_get_soundtrack_name(buf, sizeof(buf));
753                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Soundtrack changed to: %s", 2), buf);
754
755         } else {
756                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Event music is not playing", 3));
757         }
758 }
759
760 //extern void set_global_ignore_object(int objnum);
761
762 extern void hud_target_asteroid();
763 extern int Framerate_delay;
764
765 extern void snd_stop_any_sound();
766
767 void process_debug_keys(int k)
768 {
769 #ifdef INTERPLAYQA
770         if ( !Debug_allowed )
771                 return;
772 #endif
773
774         // if ( (k & KEY_DEBUGGED) && (Game_mode & GM_RECORDING_DEMO) )
775                 // return;
776
777         switch (k) {
778                 case KEY_DEBUGGED + SDLK_h:
779                         hud_target_toggle_hidden_from_sensors();
780                         break;
781
782                 case KEY_DEBUGGED + SDLK_f:
783                         /*
784                         int i;
785                         for (i=0; i<NUM_HUD_GAUGES; i++) {
786                                 hud_gauge_start_flash(i);
787                         }
788                         */
789                         extern int wacky_scheme;
790                         if(wacky_scheme == 3){
791                                 wacky_scheme = 0;
792                         } else {
793                                 wacky_scheme++;
794                         }
795                         break;
796                 
797                 case KEY_DEBUGGED + KEY_ALTED + SDLK_f:
798                         Framerate_delay += 10;
799                         HUD_printf(XSTR( "Framerate delay increased to %i milliseconds per frame.", 4), Framerate_delay);
800                         break;
801
802                 case KEY_DEBUGGED + KEY_ALTED + KEY_SHIFTED + SDLK_f:
803                         Framerate_delay -= 10;
804                         if (Framerate_delay < 0)
805                                 Framerate_delay = 0;
806
807                         HUD_printf(XSTR( "Framerate delay decreased to %i milliseconds per frame.", 5), Framerate_delay);
808                         break;
809
810                 case KEY_DEBUGGED + SDLK_c:
811                 case KEY_DEBUGGED1 + SDLK_c:
812                         // hud_enemymsg_toggle();
813                         if(Player_obj->flags & OF_COLLIDES){
814                                 obj_set_flags(Player_obj, Player_obj->flags & ~(OF_COLLIDES));
815                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, "Player no longer collides");
816                         } else {
817                                 obj_set_flags(Player_obj, Player_obj->flags | OF_COLLIDES);
818                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, "Player collides");
819                         }
820                         break;
821
822                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_c:
823                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_c:
824                         Countermeasures_enabled = !Countermeasures_enabled;
825                         HUD_printf(XSTR( "Countermeasure firing: %s", 6), Countermeasures_enabled ? XSTR( "ENABLED", 7) : XSTR( "DISABLED", 8));
826                         break;
827
828                 case KEY_DEBUGGED + SDLK_e:
829                         gameseq_post_event(GS_EVENT_EVENT_DEBUG);
830                         break;
831
832                 case KEY_DEBUGGED + SDLK_COMMA:
833                         if ( Game_time_compression > (F1_0/4) ){                // can't compress below 0.25
834                                 Game_time_compression /= 2;
835                         }
836                         break;
837                 case KEY_DEBUGGED + SDLK_PERIOD:
838                         if ( Game_time_compression < (F1_0*8) ){
839                                 Game_time_compression *= 2;
840                         }
841                         break;
842
843                 //      Kill! the currently targeted ship.
844                 case KEY_DEBUGGED + SDLK_k:
845                 case KEY_DEBUGGED1 + SDLK_k:
846                         if (Player_ai->target_objnum != -1) {
847                                 object  *objp = &Objects[Player_ai->target_objnum];
848
849                                 switch (objp->type) {
850                                 case OBJ_SHIP:
851                                         ship_apply_local_damage( objp, Player_obj, &objp->pos, 100000.0f, MISS_SHIELDS, CREATE_SPARKS);
852                                         ship_apply_local_damage( objp, Player_obj, &objp->pos, 1.0f, MISS_SHIELDS, CREATE_SPARKS);
853                                         break;
854                                 case OBJ_WEAPON:
855                                         Weapons[objp->instance].lifeleft = 0.01f;
856                                         break;
857                                 }
858                         }
859
860                         break;
861                 
862                 // play the next mission message
863                 case KEY_DEBUGGED + SDLK_v:
864                         extern int Message_debug_index;
865                         extern int Num_messages_playing;
866                         // stop any other messages
867                         if(Num_messages_playing){
868                                 message_kill_all(1);
869                         }
870
871                         // next message
872                         if(Message_debug_index >= Num_messages - 1){
873                                 Message_debug_index = Num_builtin_messages;
874                         } else {
875                                 Message_debug_index++;
876                         }
877                         
878                         // play the message
879                         message_send_unique_to_player( Messages[Message_debug_index].name, Message_waves[Messages[Message_debug_index].wave_info.index].name, MESSAGE_SOURCE_SPECIAL, MESSAGE_PRIORITY_HIGH, 0, 0 );                    
880                         if (Messages[Message_debug_index].avi_info.index == -1) {
881                                 HUD_printf("No anim set for message \"%s\"; None will play!", Messages[Message_debug_index].name);
882                         }
883                         break;
884
885                 // play the previous mission message
886                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_v:
887                         extern int Message_debug_index;
888                         extern int Num_messages_playing;
889                         // stop any other messages
890                         if(Num_messages_playing){
891                                 message_kill_all(1);
892                         }
893
894                         // go maybe go down one
895                         if(Message_debug_index == Num_builtin_messages - 1){
896                                 Message_debug_index = Num_builtin_messages;
897                         } else if(Message_debug_index > Num_builtin_messages){
898                                 Message_debug_index--;
899                         }
900                         
901                         // play the message
902                         message_send_unique_to_player( Messages[Message_debug_index].name, Message_waves[Messages[Message_debug_index].wave_info.index].name, MESSAGE_SOURCE_SPECIAL, MESSAGE_PRIORITY_HIGH, 0, 0 );
903                         if (Messages[Message_debug_index].avi_info.index == -1) {
904                                 HUD_printf("No avi associated with this message; None will play!");
905                         }
906                         break;
907
908                 // reset to the beginning of mission messages
909                 case KEY_DEBUGGED + KEY_ALTED + SDLK_v:
910                         extern int Message_debug_index;
911                         Message_debug_index = Num_builtin_messages - 1;
912                         HUD_printf("Resetting to first mission message");
913                         break;
914
915                 //      Kill! the currently targeted ship.
916                 case KEY_DEBUGGED + KEY_ALTED + KEY_SHIFTED + SDLK_k:
917                 case KEY_DEBUGGED1 + KEY_ALTED + KEY_SHIFTED + SDLK_k:
918                         if (Player_ai->target_objnum != -1) {
919                                 object  *objp = &Objects[Player_ai->target_objnum];
920
921                                 if (objp->type == OBJ_SHIP) {
922                                         ship_apply_local_damage( objp, Player_obj, &objp->pos, Ship_info[Ships[objp->instance].ship_info_index].initial_hull_strength * 0.1f + 10.0f, MISS_SHIELDS, CREATE_SPARKS);
923                                 }
924                         }
925                         break;
926
927                         //      Kill the currently targeted subsystem.
928                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_k:
929                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_k:
930                         if ((Player_ai->target_objnum != -1) && (Player_ai->targeted_subsys != NULL)) {
931                                 object  *objp = &Objects[Player_ai->target_objnum];
932                                 if ( objp->type == OBJ_SHIP ) {
933                                         ship            *sp = &Ships[objp->instance];
934                                         vector  g_subobj_pos;
935
936                                         get_subsystem_world_pos(objp, Player_ai->targeted_subsys, &g_subobj_pos);
937
938                                         do_subobj_hit_stuff(objp, Player_obj, &g_subobj_pos, (float) -Player_ai->targeted_subsys->system_info->type); //100.0f);
939
940                                         if ( sp->subsys_info[SUBSYSTEM_ENGINE].current_hits <= 0.0f ) {
941                                                 mission_log_add_entry(LOG_SHIP_DISABLED, sp->ship_name, NULL );
942                                                 sp->flags |= SF_DISABLED;                               // add the disabled flag
943                                         }
944
945                                         if ( sp->subsys_info[SUBSYSTEM_TURRET].current_hits <= 0.0f ) {
946                                                 mission_log_add_entry(LOG_SHIP_DISARMED, sp->ship_name, NULL );
947                                                 // sp->flags |= SF_DISARMED;                            // add the disarmed flag
948                                         }
949                                 }
950                         }
951                         break;
952
953                 case KEY_DEBUGGED + KEY_ALTED + SDLK_k:
954                 case KEY_DEBUGGED1 + KEY_ALTED + SDLK_k:
955                         {
956                                 float   shield, integrity;
957                                 vector  pos, randvec;
958
959                                 vm_vec_rand_vec_quick(&randvec);
960                                 vm_vec_scale_add(&pos, &Player_obj->pos, &randvec, Player_obj->radius);
961                         ship_apply_local_damage(Player_obj, Player_obj, &pos, 25.0f, MISS_SHIELDS, CREATE_SPARKS);
962                         hud_get_target_strength(Player_obj, &shield, &integrity);
963                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "You knocked yourself down to %7.3f percent hull.\n", 9), 100.0f * integrity);
964                         break;
965                         }
966                         
967                 //      Whack down the player's shield and hull by a little more than 50%
968                 //      Select next object to be viewed by AI.
969                 case KEY_DEBUGGED + SDLK_i:
970                 case KEY_DEBUGGED1 + SDLK_i:
971                         Player_obj->flags ^= OF_INVULNERABLE;
972                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "You are %s", 10), Player_obj->flags & OF_INVULNERABLE ? XSTR( "now INVULNERABLE!", 11) : XSTR( "no longer invulnerable...", 12));
973                         break;
974
975                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_i:
976                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_i:
977                         if (Player_ai->target_objnum != -1) {
978                                 object  *objp = &Objects[Player_ai->target_objnum];
979
980                                 objp->flags ^= OF_INVULNERABLE;
981                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Player's target [%s] is %s", 13), Ships[objp->instance].ship_name, objp->flags & OF_INVULNERABLE ? XSTR( "now INVULNERABLE!", 11) : XSTR( "no longer invulnerable...", 12));
982                         }
983                         break;
984 /*
985                 case KEY_DEBUGGED + KEY_ALTED + SDLK_i:
986                         if (Player_ai->target_objnum != -1)
987                                 set_global_ignore_object(Player_ai->target_objnum);
988                         break;
989 */
990
991                 case KEY_DEBUGGED + SDLK_n:
992                         AI_watch_object++;
993                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Spewing debug info about object #%d", 14), AI_watch_object);
994                         break;
995
996                 case KEY_DEBUGGED + SDLK_o:
997 #ifdef MAKE_FS1
998                 case KEY_DEBUGGED1 + SDLK_o:
999 #endif
1000                         toggle_player_object();
1001                         break;                          
1002
1003                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_o:
1004                         extern int Debug_octant;
1005                         if(Debug_octant == 7){
1006                                 Debug_octant = -1;
1007                         } else {
1008                                 Debug_octant++;
1009                         }
1010                         nprintf(("General", "Debug_octant == %d\n", Debug_octant));
1011                         break;
1012
1013 #ifndef MAKE_FS1
1014                 case KEY_DEBUGGED + SDLK_p:
1015                         supernova_start(20);
1016                         break;
1017 #endif
1018
1019                 case KEY_DEBUGGED + SDLK_w:
1020                 case KEY_DEBUGGED1 + SDLK_w:
1021                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_w:
1022                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_w:
1023                         // temp code for testing purposes, toggles weapon energy cheat
1024                         Weapon_energy_cheat = !Weapon_energy_cheat;
1025                         if (Weapon_energy_cheat) {
1026                                 if (k & KEY_SHIFTED)
1027                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Weapon energy and missile count will always be at full ALL SHIPS!", 15));
1028                                 else
1029                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Weapon energy and missile count will always be at full for player", 16));
1030
1031                                 debug_max_secondary_weapons(Player_obj);
1032                                 if (k & KEY_SHIFTED) {
1033                                         object  *objp;
1034
1035                                         for ( objp = GET_FIRST(&obj_used_list); objp !=END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) )
1036                                                 if (objp->type == OBJ_SHIP)
1037                                                         debug_max_secondary_weapons(objp);
1038                                 }
1039
1040                         } else
1041                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Normal weapon energy system / missile count restored", 17));
1042
1043                         break;
1044
1045                 case KEY_DEBUGGED + SDLK_g:
1046 #ifdef MAKE_FS1
1047                 case KEY_DEBUGGED1 + SDLK_g:
1048 #endif
1049                         mission_goal_mark_all_true( PRIMARY_GOAL );
1050                         break;
1051
1052                 case KEY_DEBUGGED + SDLK_g + KEY_SHIFTED:
1053 #ifdef MAKE_FS1
1054                 case KEY_DEBUGGED1 + SDLK_g + KEY_SHIFTED:
1055 #endif
1056                         mission_goal_mark_all_true( SECONDARY_GOAL );
1057                         break;
1058
1059                 case KEY_DEBUGGED + SDLK_g + KEY_ALTED:
1060 #ifdef MAKE_FS1
1061                 case KEY_DEBUGGED1 + SDLK_g + KEY_ALTED:
1062 #endif
1063                         mission_goal_mark_all_true( BONUS_GOAL );
1064                         break;
1065
1066                 case KEY_DEBUGGED + SDLK_9: {
1067                 case KEY_DEBUGGED1 + SDLK_9:
1068                         ship* shipp;
1069
1070                         shipp = &Ships[Player_obj->instance];
1071                         shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank]++;
1072                         if ( shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank] >= Num_weapon_types )
1073                                 shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank] = First_secondary_index;
1074
1075                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Secondary Weapon forced to %s", 18), Weapon_info[shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank]].name);
1076                         break;
1077                 }
1078
1079 #ifdef MAKE_FS1
1080                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_9: {
1081                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_9:
1082                         ship* shipp;
1083
1084                         shipp = &Ships[Player_obj->instance];
1085                         shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank]--;
1086                         if ( shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank] < 0)
1087                                 shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank] = Num_weapon_types - 1;
1088
1089                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Secondary Weapon forced to %s", 18), Weapon_info[shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank]].name);
1090                         break;
1091                 }
1092 #endif
1093
1094 #if !(defined(FS2_DEMO) || defined(FS1_DEMO))
1095                 case KEY_DEBUGGED + SDLK_u: {
1096                 case KEY_DEBUGGED1 + SDLK_u:
1097                         // launch asteroid
1098                         extern asteroid_field Asteroid_field;
1099                         object *asteroid_create(asteroid_field *asfieldp, int asteroid_type, int subtype);
1100                         object *objp = asteroid_create(&Asteroid_field, 0, 0);
1101                         vector vel;
1102                         vm_vec_copy_scale(&vel, &Player_obj->orient.v.fvec, 50.0f);
1103                         objp->phys_info.vel = vel;
1104                         objp->phys_info.desired_vel = vel;
1105                         objp->pos = Player_obj->pos;
1106                         //mission_goal_mark_all_true( PRIMARY_GOAL );
1107                         break;
1108                 }
1109 #endif
1110
1111                 case KEY_DEBUGGED + SDLK_0: {
1112                 case KEY_DEBUGGED1 + SDLK_0:
1113                         ship* shipp;
1114
1115                         shipp = &Ships[Player_obj->instance];
1116                         shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank]++;
1117                         if ( shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank] >= First_secondary_index )
1118                                 shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank] = 0;
1119
1120                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Primary Weapon forced to %s", 19), Weapon_info[shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank]].name);
1121                         break;
1122                 }
1123
1124                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_0: {
1125                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_0:
1126                         ship* shipp;
1127
1128                         shipp = &Ships[Player_obj->instance];
1129                         shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank]--;
1130                         if ( shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank] < 0)
1131                                 shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank] = First_secondary_index-1 ;
1132
1133                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Primary Weapon forced to %s", 19), Weapon_info[shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank]].name);
1134                         break;
1135                 }
1136
1137                 case KEY_DEBUGGED + SDLK_j: {
1138                         int new_pattern = event_music_return_current_pattern();
1139
1140                         new_pattern++;
1141                         if ( new_pattern >= MAX_PATTERNS )
1142                                 new_pattern = 0;
1143
1144                         event_music_change_pattern(new_pattern);
1145                         break;
1146                 }
1147
1148                 case KEY_DEBUGGED + SDLK_m: {
1149                         if ( Event_music_enabled ) {
1150                                 event_music_disable();
1151                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Event music disabled", 20));
1152
1153                         } else {
1154                                 event_music_enable();
1155                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Event music enabled", 21));
1156                         }
1157
1158                         break;
1159                 }
1160
1161                 case KEY_DEBUGGED + SDLK_r: {
1162                 // case KEY_DEBUGGED1 + SDLK_r:
1163                         if (Player_ai->target_objnum != -1)
1164                                 ai_issue_rearm_request(&Objects[Player_ai->target_objnum]);
1165                         else
1166                                 ai_issue_rearm_request(Player_obj);
1167
1168                         break;
1169                 }
1170
1171                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_UP:
1172                         Game_detail_level++;
1173                         HUD_printf( XSTR( "Detail level set to %+d\n", 22), Game_detail_level );
1174                         break;
1175
1176                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_DOWN:
1177                         Game_detail_level--;
1178                         HUD_printf( XSTR( "Detail level set to %+d\n", 22), Game_detail_level );
1179                         break;
1180
1181 #ifndef NDEBUG
1182                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_t:       {
1183                         extern int Test_begin;
1184
1185                         if ( Test_begin == 1 )
1186                                 break;
1187
1188                         Test_begin = 1;
1189                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Frame Rate test started", 23));
1190
1191                         break;
1192                 }
1193 #endif
1194                 case KEY_DEBUGGED + SDLK_d:
1195                         extern int OO_update_index;                     
1196
1197                         if(MULTIPLAYER_MASTER){
1198                                 do {
1199                                         OO_update_index++;
1200                                 } while((OO_update_index < (MAX_PLAYERS-1)) && !MULTI_CONNECTED(Net_players[OO_update_index]));
1201                                 if(OO_update_index >= MAX_PLAYERS-1){
1202                                         OO_update_index = -1;
1203                                 }                       
1204                         } else {
1205                                 if(OO_update_index < 0){
1206                                         OO_update_index = MY_NET_PLAYER_NUM;
1207                                 } else {
1208                                         OO_update_index = -1;
1209                                 }
1210                         }
1211                         break;
1212
1213                 // change player ship to next flyable type
1214                 case KEY_DEBUGGED + SDLK_RIGHT:
1215                         debug_cycle_player_ship(1);
1216                         break;
1217
1218                 // change player ship to previous flyable ship
1219                 case KEY_DEBUGGED + SDLK_LEFT:
1220                         debug_cycle_player_ship(-1);
1221                         break;
1222                 
1223                 // cycle target to ship
1224                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_RIGHT:
1225                         debug_cycle_targeted_ship(1);
1226                         break;
1227
1228                 // cycle target to previous ship
1229                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_LEFT:
1230                         debug_cycle_targeted_ship(-1);
1231                         break;
1232
1233                 // change species of the targeted ship
1234                 case KEY_DEBUGGED + SDLK_s: {
1235                         if ( Player_ai->target_objnum < 0 )
1236                                 break;
1237
1238                         object          *objp;
1239                         ship_info       *sip;
1240
1241                         objp = &Objects[Player_ai->target_objnum];
1242                         if ( objp->type != OBJ_SHIP )
1243                                 return;
1244
1245                         sip = &Ship_info[Ships[objp->instance].ship_info_index];
1246                         sip->species++;
1247                         if ( sip->species > SPECIES_SHIVAN )
1248                                 sip->species = 0;
1249
1250                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Species of target changed to: %s", 24), Species_names[sip->species]);
1251                         break;
1252                 }
1253                         
1254                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_s:
1255                         game_increase_skill_level();
1256                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Skill level set to %s.", 25), Skill_level_names(Game_skill_level));
1257                         break;
1258
1259 #ifndef MAKE_FS1
1260                 // kill all missiles
1261                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_1:
1262                         beam_test(1);
1263                         break;                          
1264                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_2:
1265                         beam_test(2);
1266                         break;          
1267                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_3:
1268                         beam_test(3);
1269                         break;                          
1270                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_4:
1271                         beam_test(4);
1272                         break;          
1273                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_5:
1274                         beam_test(5);
1275                         break;                          
1276                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_6:
1277                         beam_test(6);
1278                         break;          
1279                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_7:
1280                         beam_test(7);
1281                         break;                          
1282                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_8:
1283                         beam_test(8);
1284                         break;          
1285                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_9:
1286                         beam_test(9);
1287                         break;                          
1288
1289                 case KEY_DEBUGGED + KEY_CTRLED + SDLK_1:
1290                         beam_test_new(1);
1291                         break;                          
1292                 case KEY_DEBUGGED + KEY_CTRLED + SDLK_2:
1293                         beam_test_new(2);
1294                         break;          
1295                 case KEY_DEBUGGED + KEY_CTRLED + SDLK_3:
1296                         beam_test_new(3);
1297                         break;
1298 #endif
1299                                         
1300                 case KEY_DEBUGGED + SDLK_t: {
1301                         char buf[256];
1302                         event_music_get_info(buf, sizeof(buf));
1303                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, buf);
1304                         break;
1305                 }
1306
1307                 case KEY_DEBUGGED + SDLK_UP:
1308                         debug_change_song(1);
1309                         break;
1310
1311                 case KEY_DEBUGGED + SDLK_DOWN:
1312                         debug_change_song(-1);
1313                         break;
1314
1315 #ifndef MAKE_FS1
1316                 case SDLK_KP_MINUS: {
1317                         int init_flag = 0;
1318
1319                         if ( key_pressed(SDLK_1) )      {
1320                                 init_flag = 1;
1321                                 HUD_color_red -= 4;
1322                         } 
1323
1324                         if ( key_pressed(SDLK_2) )      {
1325                                 init_flag = 1;
1326                                 HUD_color_green -= 4;
1327                         } 
1328
1329                         if ( key_pressed(SDLK_3) )      {
1330                                 init_flag = 1;
1331                                 HUD_color_blue -= 4;
1332                         } 
1333
1334                         if (init_flag)
1335                                 HUD_init_colors();
1336
1337                         break;
1338                 }
1339 #endif
1340                 
1341                 case KEY_DEBUGGED + SDLK_y:
1342                         /*
1343                         // blast a debug lightning bolt in front of the player
1344                         vector start, strike;
1345                         
1346                         vm_vec_scale_add(&start, &Player_obj->pos, &Player_obj->orient.fvec, 300.0f);
1347                         vm_vec_scale_add2(&start, &Player_obj->orient.rvec, -300.0f);
1348                         vm_vec_scale_add(&strike, &start, &Player_obj->orient.rvec, 600.0f);
1349                         nebl_bolt(DEBUG_BOLT, &start, &strike);
1350                         */
1351                         extern int tst;
1352                         tst = 2;
1353                         break;
1354
1355 #ifndef MAKE_FS1
1356                 case SDLK_KP_PLUS: {
1357                         int init_flag = 0;
1358
1359                         if ( key_pressed(SDLK_1) )      {
1360                                 init_flag = 1;
1361                                 HUD_color_red += 4;
1362                         } 
1363
1364                         if ( key_pressed(SDLK_2) )      {
1365                                 init_flag = 1;
1366                                 HUD_color_green += 4;
1367                         } 
1368
1369                         if ( key_pressed(SDLK_3) )      {
1370                                 init_flag = 1;
1371                                 HUD_color_blue += 4;
1372                         } 
1373
1374                         if (init_flag)
1375                                 HUD_init_colors();
1376
1377                         break;
1378                 }
1379 #endif
1380         }       // end switch
1381
1382 }
1383 //#endif
1384
1385 void ppsk_hotkeys(int k)
1386 {
1387
1388 #if !(defined(FS2_DEMO) || defined(FS1_DEMO))
1389
1390         // use k to check for keys that can have Shift,Ctrl,Alt,Del status
1391         int hotkey_set;
1392
1393 #ifndef NDEBUG
1394         k &= ~KEY_DEBUGGED;                     // since hitting F11 will set this bit
1395 #endif
1396
1397         switch (k) {
1398                 case SDLK_F5:
1399                 case SDLK_F6:
1400                 case SDLK_F7:
1401                 case SDLK_F8:
1402                 case SDLK_F9:
1403                 case SDLK_F10:
1404                 case SDLK_F11:
1405                 case SDLK_F12:
1406                         hotkey_set = mission_hotkey_get_set_num(k);
1407                         if ( !(Players[Player_num].flags & PLAYER_FLAGS_MSG_MODE) )
1408                                 hud_target_hotkey_select( hotkey_set );
1409                         else
1410                                 hud_squadmsg_hotkey_select( hotkey_set );
1411
1412                         break;
1413
1414                 case SDLK_F5 + KEY_SHIFTED:
1415                 case SDLK_F6 + KEY_SHIFTED:
1416                 case SDLK_F7 + KEY_SHIFTED:
1417                 case SDLK_F8 + KEY_SHIFTED:
1418                 case SDLK_F9 + KEY_SHIFTED:
1419                 case SDLK_F10 + KEY_SHIFTED:
1420                 case SDLK_F11 + KEY_SHIFTED:
1421                 case SDLK_F12 + KEY_SHIFTED:
1422                         hotkey_set = mission_hotkey_get_set_num(k&(~KEY_SHIFTED));
1423                         mprintf(("Adding to set %d\n", hotkey_set+1));
1424                         if ( Player_ai->target_objnum == -1)
1425                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "No target to add/remove from set %d.", 26), hotkey_set+1);
1426                         else  {
1427                                 hud_target_hotkey_add_remove( hotkey_set, &Objects[Player_ai->target_objnum], HOTKEY_USER_ADDED);
1428                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "%s added to set %d. (F%d)", 27), Ships[Objects[Player_ai->target_objnum].instance].ship_name, hotkey_set, 4+hotkey_set+1);
1429                         }
1430
1431                         break;
1432
1433                 case SDLK_F5 + KEY_SHIFTED + KEY_ALTED:
1434                 case SDLK_F6 + KEY_SHIFTED + KEY_ALTED:
1435                 case SDLK_F7 + KEY_SHIFTED + KEY_ALTED:
1436                 case SDLK_F8 + KEY_SHIFTED + KEY_ALTED:
1437                 case SDLK_F9 + KEY_SHIFTED + KEY_ALTED:
1438                 case SDLK_F10 + KEY_SHIFTED + KEY_ALTED:
1439                 case SDLK_F11 + KEY_SHIFTED + KEY_ALTED:
1440                 case SDLK_F12 + KEY_SHIFTED + KEY_ALTED:
1441                         hotkey_set = mission_hotkey_get_set_num(k & ~(KEY_SHIFTED+KEY_ALTED));
1442                         hud_target_hotkey_clear( hotkey_set );
1443                         break;
1444
1445                 case KEY_SHIFTED + SDLK_MINUS:
1446                         if ( HUD_color_alpha > HUD_COLOR_ALPHA_USER_MIN )       {
1447                                 HUD_color_alpha--;
1448                                 HUD_init_colors();
1449                         }
1450                         break;
1451 /*              case KEY_SHIFTED + SDLK_u:
1452                         {
1453                         object *debris_create(object *source_obj, int model_num, int submodel_num, vector *pos, vector *exp_center, int hull_flag, float exp_force);
1454
1455                         object *temp = debris_create(Player_obj, Ships[0].modelnum, model_get(Ships[0].modelnum)->debris_objects[0], &Player_obj->pos, &Player_obj->pos, 1, 1.0f);
1456                         if (temp) {
1457                                 temp->hull_strength = 5000.0f;
1458                                 int objnum = temp - Objects;
1459                                 vm_vec_copy_scale(&Objects[objnum].phys_info.vel, &Player_obj->orient.fvec, 30.0f);
1460                         }
1461                         }
1462                         break;
1463 */
1464
1465                 case KEY_SHIFTED + SDLK_EQUALS:
1466                         if ( HUD_color_alpha < HUD_COLOR_ALPHA_USER_MAX ) {
1467                                 HUD_color_alpha++;
1468                                 HUD_init_colors();
1469                         }
1470                         break;
1471         }       // end switch
1472
1473 #endif
1474
1475 }
1476
1477 // check keypress 'key' against a set of valid controls and mark the match in the
1478 // player's button info bitfield.  Also checks joystick controls in the set.
1479 //
1480 // key = scancode (plus modifiers).
1481 // count = total size of the list
1482 // list = list of Control_config struct action indices to check for
1483 void process_set_of_keys(int key, int count, int *list)
1484 {
1485         int i;
1486
1487         for (i=0; i<count; i++)
1488                 if (check_control(list[i], key))
1489                         button_info_set(&Player->bi, list[i]);
1490 }
1491
1492 // routine to process keys used for player ship stuff (*not* ship movement).
1493 void process_player_ship_keys(int k)
1494 {
1495         int masked_k;
1496
1497         masked_k = k & ~KEY_CTRLED;     // take out CTRL modifier only  
1498
1499         // moved this line to beginning of function since hotkeys now encompass
1500         // F5 - F12.  We can return after using F11 as a hotkey.
1501         ppsk_hotkeys(masked_k);
1502         if (key_pressed(KEY_DEBUG_KEY)){
1503                 return;
1504         }
1505
1506         // if we're in supernova mode. do nothing
1507         if(Player->control_mode == PCM_SUPERNOVA){
1508                 return;
1509         }
1510
1511         // pass the key to the squadmate messaging code.  If the messaging code took the key, then return
1512         // from here immediately since we don't want to do further key processing.
1513         if ( hud_squadmsg_read_key(k) )
1514                 return;
1515
1516         if ( Player->control_mode == PCM_NORMAL )       {
1517                 //      The following things are not legal to do while dead.
1518                 if ( !(Game_mode & GM_DEAD) ) {
1519                         process_set_of_keys(masked_k, Normal_key_set_size, Normal_key_set);
1520                 } else  {
1521                         process_set_of_keys(masked_k, Dead_key_set_size, Dead_key_set);
1522                 }
1523         } else {
1524
1525         }
1526 }
1527
1528 // Handler for when player hits 'ESC' during the game
1529 void game_do_end_mission_popup()
1530 {
1531         int     pf_flags, choice;
1532 //      char    savegame_filename[_MAX_FNAME];
1533
1534         // do the multiplayer version of this
1535         if(Game_mode & GM_MULTIPLAYER){
1536                 multi_quit_game(PROMPT_ALL);
1537         } else {
1538
1539                 // single player version....
1540                 // do housekeeping things.
1541                 game_stop_time();
1542                 game_stop_looped_sounds();
1543                 snd_stop_all();
1544
1545                 pf_flags = PF_BODY_BIG | PF_USE_AFFIRMATIVE_ICON | PF_USE_NEGATIVE_ICON;
1546                 choice = popup(pf_flags, 3, POPUP_NO, XSTR( "&Yes, Quit", 28), XSTR( "Yes, &Restart", 29), XSTR( "Do you really want to end the mission?", 30));
1547
1548                 switch (choice) {
1549                 case 1:
1550                         // save the game before quitting if in campaign mode
1551                         // MWA -- 3/26/98 -- no more save/restore!!!!
1552 /*
1553                         if ( Game_mode & GM_CAMPAIGN_MODE ) {
1554                                 memset(savegame_filename, 0, _MAX_FNAME);
1555                                 mission_campaign_savefile_generate_root(savegame_filename);
1556                                 strcat(savegame_filename, NOX("svg"));
1557                                 if ( state_save_all(savegame_filename) ) {
1558                                         Int3(); // could not save this game
1559                                 }
1560                         }
1561 */
1562                         gameseq_post_event(GS_EVENT_END_GAME);
1563                         break;
1564
1565                 case 2:
1566                         gameseq_post_event(GS_EVENT_ENTER_GAME);
1567                         break;
1568
1569                 default:
1570                         break;  // do nothing
1571                 }
1572
1573                 game_start_time();
1574                 game_flush();
1575         }
1576 }
1577
1578 // handle pause keypress
1579 void game_process_pause_key()
1580 {
1581         // special processing for multiplayer
1582         if (Game_mode & GM_MULTIPLAYER) {                                                       
1583                 if(Multi_pause_status){
1584                         multi_pause_request(0);
1585                 } else {
1586                         multi_pause_request(1);
1587                 }               
1588         } else {
1589                 gameseq_post_event( GS_EVENT_PAUSE_GAME );
1590         }
1591 }
1592
1593 #define WITHIN_BBOX()   do { \
1594         float scale = 2.0f; \
1595         polymodel *pm = model_get(s_check->modelnum); \
1596         collided = 0; \
1597         if(pm != NULL){ \
1598                 vector temp = new_obj->pos; \
1599                 vector gpos; \
1600                 vm_vec_sub2(&temp, &hit_check->pos); \
1601                 vm_vec_rotate(&gpos, &temp, &hit_check->orient); \
1602                 if((gpos.xyz.x >= pm->mins.xyz.x * scale) && (gpos.xyz.y >= pm->mins.xyz.y * scale) && (gpos.xyz.z >= pm->mins.xyz.z * scale) && (gpos.xyz.x <= pm->maxs.xyz.x * scale) && (gpos.xyz.y <= pm->maxs.xyz.y * scale) && (gpos.xyz.z <= pm->maxs.xyz.z * scale)) { \
1603                         collided = 1; \
1604                 } \
1605         } \
1606 } while(0)
1607
1608 #define MOVE_AWAY_BBOX() do { \
1609         polymodel *pm = model_get(s_check->modelnum); \
1610         if(pm != NULL){ \
1611                 switch((int)frand_range(0.0f, 3.9f)){ \
1612                 case 0: \
1613                         new_obj->pos.xyz.x += 200.0f; \
1614                         break; \
1615                 case 1: \
1616                         new_obj->pos.xyz.x -= 200.0f; \
1617                         break; \
1618                 case 2: \
1619                         new_obj->pos.xyz.y += 200.0f; \
1620                         break; \
1621                 case 3: \
1622                         new_obj->pos.xyz.y -= 200.0f; \
1623                         break; \
1624                 default : \
1625                         new_obj->pos.xyz.z -= 200.0f; \
1626                         break; \
1627                 } \
1628         } \
1629 } while(0)
1630
1631 // process cheat codes
1632 void game_process_cheats(int k)
1633 {
1634         int i;
1635         char *cryptstring;
1636
1637         if ( k == 0 ){
1638                 return;
1639         }
1640
1641         // no cheats in multiplayer, ever
1642         if(Game_mode & GM_MULTIPLAYER){
1643                 Cheats_enabled = 0;
1644                 return;
1645         }
1646
1647         k = key_get_text_input();
1648
1649         if ( (k < 0) || (k > 255) ) {
1650                 return;
1651         }
1652
1653         for (i = 0; i < CHEAT_BUFFER_LEN; i++){
1654                 CheatBuffer[i]=CheatBuffer[i+1];
1655         }
1656
1657         CheatBuffer[CHEATSPOT]=(char)k;
1658
1659         cryptstring=jcrypt(&CheatBuffer[CHEAT_BUFFER_LEN - CRYPT_STRING_LENGTH]);               
1660
1661 #if defined(FS2_DEMO)
1662         if ( !strcmp(Cheat_code_demo, cryptstring) ) {
1663                 HUD_printf(XSTR( "Cheats enabled.", 31));
1664                 Cheats_enabled = 1;
1665                 if (Player->flags & PLAYER_FLAGS_MSG_MODE){
1666                         hud_squadmsg_toggle();
1667                 }
1668         }
1669         
1670 #else
1671
1672 #ifdef MAKE_FS1
1673         // two possible cheat codes for FS1, German and English
1674         if (Lcl_gr) {
1675                 if( !strcmp(Cheat_code_gr, cryptstring) && !(Game_mode & GM_MULTIPLAYER)){
1676                         Cheats_enabled = 1;
1677                         HUD_printf("Cheats enabled");
1678                 }
1679         } else {
1680                 if( !strcmp(Cheat_code, cryptstring) && !(Game_mode & GM_MULTIPLAYER)){
1681                         Cheats_enabled = 1;
1682                         HUD_printf("Cheats enabled");
1683                 }
1684         }
1685 #else
1686         if( !strcmp(Cheat_code, cryptstring) && !(Game_mode & GM_MULTIPLAYER)){
1687                 Cheats_enabled = 1;
1688                 HUD_printf("Cheats enabled");
1689         }
1690 #endif
1691
1692 #ifndef MAKE_FS1
1693         if( !strcmp(Cheat_code_fish, cryptstring) ){
1694                 // only enable in the main hall
1695                 if((gameseq_get_state() == GS_STATE_MAIN_MENU) && (main_hall_id() == 1)){
1696                         extern void fishtank_start();
1697                         fishtank_start();
1698                 }
1699         }
1700         if( !strcmp(Cheat_code_headz, cryptstring) ){
1701                 main_hall_vasudan_funny();
1702         }
1703         if( !strcmp(Cheat_code_skip, cryptstring) && (gameseq_get_state() == GS_STATE_MAIN_MENU)){
1704                 extern void main_hall_campaign_cheat();
1705                 main_hall_campaign_cheat();
1706         }
1707         if( !strcmp(Cheat_code_tooled, cryptstring) && (Game_mode & GM_IN_MISSION)){
1708                 Tool_enabled = 1;
1709                 HUD_printf("Prepare to be taken to school");
1710         }
1711         if( !strcmp(Cheat_code_pirate, cryptstring) && (Game_mode & GM_IN_MISSION) && (Player_obj != NULL)){
1712                 HUD_printf(NOX("Walk the plank"));
1713                 
1714                 for(int idx=0; idx<1; idx++){
1715                         vector add = Player_obj->pos;
1716                         add.xyz.x += frand_range(-700.0f, 700.0f);
1717                         add.xyz.y += frand_range(-700.0f, 700.0f);
1718                         add.xyz.z += frand_range(-700.0f, 700.0f);
1719
1720                         int objnum = ship_create(&vmd_identity_matrix, &add, Num_ship_types - 1);
1721
1722                         if(objnum >= 0){
1723                                 int collided;
1724                                 ship_obj *moveup;
1725                                 object *hit_check;
1726                                 ship *s_check;
1727                                 object *new_obj = &Objects[objnum];
1728
1729                                 // place him
1730                                 // now make sure we're not colliding with anyone                
1731                                 do {
1732                                         collided = 0;
1733                                         moveup = GET_FIRST(&Ship_obj_list);
1734                                         while(moveup!=END_OF_LIST(&Ship_obj_list)){
1735                                                 // don't check the new_obj itself!!
1736                                                 if(moveup->objnum != objnum){
1737                                                         hit_check = &Objects[moveup->objnum];
1738                                                         SDL_assert(hit_check->type == OBJ_SHIP);
1739                                                         SDL_assert(hit_check->instance >= 0);
1740                                                         if((hit_check->type != OBJ_SHIP) || (hit_check->instance < 0)){
1741                                                                 continue;
1742                                                         }
1743                                                         s_check = &Ships[hit_check->instance];
1744                                                         
1745                                                         // just to make sure we don't get any strange magnitude errors
1746                                                         if(vm_vec_same(&hit_check->pos, &Objects[objnum].pos)){
1747                                                                 Objects[objnum].pos.xyz.x += 1.0f;
1748                                                         }
1749                                                         
1750                                                         WITHIN_BBOX();                          
1751                                                         if(collided){                                           
1752                                                                 MOVE_AWAY_BBOX();
1753                                                                 break;
1754                                                         } 
1755                                                         collided = 0;
1756                                                 }
1757                                                 moveup = GET_NEXT(moveup);
1758                                         }
1759                                 } while(collided);                                      
1760                         
1761                                 // warpin
1762                                 shipfx_warpin_start(&Objects[objnum]);
1763
1764                                 // tell him to attack                           
1765                                 // ai_add_ship_goal_player( AIG_TYPE_PLAYER_SHIP, AI_GOAL_CHASE_ANY, SM_ATTACK, NULL, &Ai_info[Ships[Objects[objnum].instance].ai_index] );
1766                         }
1767                 }
1768         }
1769 #endif  // !MAKE_FS1
1770 #endif
1771
1772 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
1773         if ( !strcmp(Cheat_code_movies, cryptstring) ) {
1774                 HUD_printf(XSTR( "All movies available in Tech Room", 32));
1775                 All_movies_enabled = 1;
1776                 if (Player->flags & PLAYER_FLAGS_MSG_MODE){
1777                         hud_squadmsg_toggle();
1778                 }
1779         }
1780 #endif
1781
1782 /*
1783 //#ifdef INTERPLAYQA
1784         if ( !strcmp(Cheat_code_in_game, cryptstring) ) {
1785                 HUD_printf(XSTR( "Cheats enabled.", 31));
1786                 Cheats_enabled = 1;
1787                 if (Player->flags & PLAYER_FLAGS_MSG_MODE){
1788                         hud_squadmsg_toggle();
1789                 }
1790         } else if ( !strcmp(Cheat_code_movies, cryptstring) ) {
1791                 HUD_printf(XSTR( "All movies available in Tech Room", 32));
1792                 All_movies_enabled = 1;
1793                 if (Player->flags & PLAYER_FLAGS_MSG_MODE){
1794                         hud_squadmsg_toggle();
1795                 }
1796         } else if( !strcmp(Cheat_code_pirate, cryptstring) ){
1797                 HUD_printf(NOX("Walk the plank"));
1798                 
1799                 for(int idx=0; idx<1; idx++){
1800                         vector add;
1801                         add.x = frand_range(-1000.0f, 1000.0f);
1802                         add.y = frand_range(-1000.0f, 1000.0f);
1803                         add.z = frand_range(-1000.0f, 1000.0f);
1804
1805                         int objnum = ship_create(&vmd_identity_matrix, &add, Num_ship_types - 1);                       
1806
1807                         if(objnum >= 0){
1808                                 shipfx_warpin_start(&Objects[objnum]);
1809                         }
1810                 }
1811         }
1812 #endif
1813         */
1814 }
1815
1816 void game_process_keys()
1817 {
1818         int k;
1819
1820         button_info_clear(&Player->bi); // clear out the button info struct for the player
1821    do {
1822                 k = game_poll();                
1823         
1824                 // AL 12-10-97: Scan for keys used to leave the dead state      (don't process any)
1825                 // DB 1-13-98 : New popup code will run the game do state, so we must skip 
1826                 //              all key processing in this function, since everything should be run through the popup dialog
1827                 if ( Game_mode & GM_DEAD_BLEW_UP ) {
1828                         continue;
1829                 }
1830
1831                 game_process_cheats( k );
1832
1833                 // mwa -- 4/5/97 Moved these two function calls before the switch statement.  I don't think
1834                 // that this has adverse affect on anything and is acutally desireable because of the
1835                 // ESC key being used to quit any HUD message/input mode that might be currently in use
1836                 process_player_ship_keys(k);
1837
1838 //              #ifndef NDEBUG
1839                 process_debug_keys(k);          //      Note, also processed for cheats.
1840 //              #endif          
1841
1842                 switch (k) {
1843                         case 0:
1844                                 // No key
1845                                 break;
1846                         
1847                         case SDLK_ESCAPE:
1848                                 if ( Player->control_mode != PCM_NORMAL )       {
1849                                         if ( Player->control_mode == PCM_WARPOUT_STAGE1 )       {
1850                                                 gameseq_post_event( GS_EVENT_PLAYER_WARPOUT_STOP );
1851                                         } else {
1852                                                 // too late to abort warp out!
1853                                         }
1854                                 } else {
1855                                         // let the ESC key break out of messaging mode
1856                                         if ( Players[Player_num].flags & PLAYER_FLAGS_MSG_MODE ) {
1857                                                 hud_squadmsg_toggle();
1858                                                 break;
1859                                         }
1860
1861                                         // if in external view or chase view, go back to cockpit view
1862                                         if ( Viewer_mode & (VM_EXTERNAL|VM_CHASE|VM_OTHER_SHIP) ) {
1863                                                 Viewer_mode &= ~(VM_EXTERNAL|VM_CHASE|VM_OTHER_SHIP);
1864                                                 break;
1865                                         }
1866
1867                                         if (!(Game_mode & GM_DEAD_DIED))
1868                                                 game_do_end_mission_popup();
1869
1870                                 }
1871                                 break;
1872
1873                         case SDLK_y:
1874                                 break;
1875
1876                         case SDLK_n:
1877                                 break;                  
1878
1879                         case KEY_ALTED + KEY_SHIFTED+SDLK_j:
1880                                 // treat the current joystick position as the center position
1881                                 joy_set_cen();
1882                                 break;
1883
1884                         case KEY_DEBUGGED | SDLK_PAUSE:
1885                                 gameseq_post_event( GS_EVENT_DEBUG_PAUSE_GAME );
1886                                 break;
1887
1888                         case SDLK_PAUSE:
1889                                 game_process_pause_key();
1890                                 break;
1891                 } // end switch
1892         } while (k);
1893
1894         button_info_do(&Player->bi);    // call functions based on status of button_info bit vectors
1895 }
1896
1897 int button_function_critical(int n, net_player *p = NULL)
1898 {
1899         object *objp;
1900         player *pl;
1901         net_player *npl;
1902         int at_self;    // flag indicating the object is local (for hud messages, etc)
1903
1904         SDL_assert(n >= 0);
1905    
1906         // multiplayer clients should leave critical button bits alone and pass them to the server instead
1907         if ((Game_mode & GM_MULTIPLAYER) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER)) {
1908                 // if this flag is set, we should apply the button itself (came from the server)
1909                 if (!Multi_button_info_ok){
1910                         return 0;
1911                 }
1912         }
1913
1914         // in single player mode make sure we're using the player object and the player himself, otherwise use the object and
1915         // player pertaining to the passed net_player
1916         npl = NULL;
1917         if (p == NULL) {
1918                 objp = Player_obj;
1919                 pl = Player;
1920                 if(Game_mode & GM_MULTIPLAYER){
1921                         npl = Net_player;
1922
1923                         // if we're the server in multiplayer and we're an observer, don't process our own critical button functions
1924                         if((Net_player->flags & NETINFO_FLAG_AM_MASTER) && (Net_player->flags & NETINFO_FLAG_OBSERVER)){
1925                                 return 0;
1926                         }
1927                 }
1928                 at_self = 1;
1929         } else {
1930                 objp = &Objects[p->player->objnum];
1931                 pl = p->player;
1932                 npl = p;
1933                 at_self = 0;
1934
1935                 if ( NETPLAYER_IS_DEAD(npl) || (Ships[Objects[pl->objnum].instance].flags & SF_DYING) )
1936                         return 0;
1937         }
1938         
1939         switch (n) {                            
1940                 // cycle to next secondary weapon
1941                 case CYCLE_SECONDARY:
1942                         if(at_self)
1943                                 control_used(CYCLE_SECONDARY);
1944                         
1945                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
1946                         if (ship_select_next_secondary(objp)) {
1947                                 ship* shipp = &Ships[objp->instance];
1948                                 if ( timestamp_elapsed(shipp->weapons.next_secondary_fire_stamp[shipp->weapons.current_secondary_bank]) ) {
1949                                         shipp->weapons.next_secondary_fire_stamp[shipp->weapons.current_secondary_bank] = timestamp(250);       //      1/4 second delay until can fire
1950                                 }
1951
1952                                 // multiplayer server should maintain bank/link status here
1953                                 if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
1954                                         SDL_assert(npl != NULL);
1955                                         multi_server_update_player_weapons(npl,shipp);                                                                          
1956                                 }                                       
1957                         }                       
1958                         break;
1959
1960                 // cycle number of missiles
1961                 case CYCLE_NUM_MISSLES:
1962                         if(at_self)
1963                                 control_used(CYCLE_NUM_MISSLES);
1964
1965                         if ( objp == Player_obj ) {
1966                                 if ( Player_ship->weapons.num_secondary_banks <= 0 ) {
1967                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "This ship has no secondary weapons", 33));
1968                                         gamesnd_play_iface(SND_GENERAL_FAIL);
1969                                         break;
1970                                 }
1971                         }
1972                                         
1973                         if ( Ships[objp->instance].flags & SF_SECONDARY_DUAL_FIRE ) {           
1974                                 Ships[objp->instance].flags &= ~SF_SECONDARY_DUAL_FIRE;
1975                                 if(at_self) {
1976                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Secondary weapon set to normal fire mode", 34));
1977                                         snd_play( &Snds[SND_SECONDARY_CYCLE] );
1978                                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
1979                                 }
1980                         } else {
1981                                 Ships[objp->instance].flags |= SF_SECONDARY_DUAL_FIRE;
1982                                 if(at_self) {
1983                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Secondary weapon set to dual fire mode", 35));
1984                                         snd_play( &Snds[SND_SECONDARY_CYCLE] );
1985                                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
1986                                 }
1987                         }
1988
1989                         // multiplayer server should maintain bank/link status here
1990                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
1991                                 SDL_assert(npl != NULL);
1992                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
1993                         }                                       
1994                         break;
1995
1996                 // increase weapon recharge rate
1997                 case INCREASE_WEAPON:
1998                         if(at_self)
1999                                 control_used(INCREASE_WEAPON);
2000                         increase_recharge_rate(objp, WEAPONS);
2001
2002                         // multiplayer server should maintain bank/link status here
2003                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2004                                 SDL_assert(npl != NULL);
2005                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2006                         }                                       
2007                         break;
2008
2009                 // decrease weapon recharge rate
2010                 case DECREASE_WEAPON:
2011                         if(at_self)
2012                                 control_used(DECREASE_WEAPON);
2013                         decrease_recharge_rate(objp, WEAPONS);
2014
2015                         // multiplayer server should maintain bank/link status here
2016                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2017                                 SDL_assert(npl != NULL);
2018                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2019                         }                                                               
2020                         break;
2021
2022                 // increase shield recharge rate
2023                 case INCREASE_SHIELD:
2024                         if(at_self)
2025                                 control_used(INCREASE_SHIELD);
2026                         increase_recharge_rate(objp, SHIELDS);
2027
2028                         // multiplayer server should maintain bank/link status here
2029                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2030                                 SDL_assert(npl != NULL);
2031                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2032                         }                                                               
2033                         break;
2034
2035                 // decrease shield recharge rate
2036                 case DECREASE_SHIELD:
2037                         if(at_self)
2038                                 control_used(DECREASE_SHIELD);
2039                         decrease_recharge_rate(objp, SHIELDS);
2040
2041                         // multiplayer server should maintain bank/link status here
2042                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2043                                 SDL_assert(npl != NULL);
2044                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2045                         }                                                               
2046                         break;
2047
2048                 // increase energy to engines
2049                 case INCREASE_ENGINE:
2050                         if(at_self)
2051                                 control_used(INCREASE_ENGINE);
2052                         increase_recharge_rate(objp, ENGINES);
2053
2054                         // multiplayer server should maintain bank/link status here
2055                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2056                                 SDL_assert(npl != NULL);
2057                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2058                         }                                                       
2059                         break;
2060
2061                 // decrease energy to engines
2062                 case DECREASE_ENGINE:
2063                         if(at_self)
2064                         control_used(DECREASE_ENGINE);
2065                         decrease_recharge_rate(objp, ENGINES);
2066
2067                         // multiplayer server should maintain bank/link status here
2068                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2069                                 SDL_assert(npl != NULL);
2070                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2071                         }                                                                               
2072                         break;
2073
2074                 // equalize recharge rates
2075                 case ETS_EQUALIZE:
2076                         if (at_self) {
2077                         control_used(ETS_EQUALIZE);
2078                         }
2079
2080                         set_default_recharge_rates(objp);
2081                         snd_play( &Snds[SND_ENERGY_TRANS] );
2082                         // multiplayer server should maintain bank/link status here
2083                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2084                                 SDL_assert(npl != NULL);
2085                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2086                         }                                                                               
2087                         break;
2088
2089                 // equalize shield energy to all quadrants
2090                 case SHIELD_EQUALIZE:
2091                         if(at_self){
2092                                 control_used(SHIELD_EQUALIZE);
2093                         }
2094                         hud_shield_equalize(objp, pl);
2095                         break;
2096
2097                 // transfer shield energy to front
2098                 case SHIELD_XFER_TOP:
2099                         if(at_self){
2100                         control_used(SHIELD_XFER_TOP);
2101                         }
2102                         hud_augment_shield_quadrant(objp, 1);
2103                         break;
2104
2105                 // transfer shield energy to rear
2106                 case SHIELD_XFER_BOTTOM:
2107                         if(at_self)
2108                                 control_used(SHIELD_XFER_BOTTOM);
2109                         hud_augment_shield_quadrant(objp, 2);
2110                         break;
2111
2112                 // transfer shield energy to left
2113                 case SHIELD_XFER_LEFT:
2114                         if(at_self)
2115                                 control_used(SHIELD_XFER_LEFT);
2116                         hud_augment_shield_quadrant(objp, 3);
2117                         break;
2118                         
2119                 // transfer shield energy to right
2120                 case SHIELD_XFER_RIGHT:
2121                         if(at_self)
2122                                 control_used(SHIELD_XFER_RIGHT);
2123                         hud_augment_shield_quadrant(objp, 0);
2124                         break;
2125
2126                 // transfer energy to shield from weapons
2127                 case XFER_SHIELD:
2128                         if(at_self)
2129                                 control_used(XFER_SHIELD);
2130                         transfer_energy_to_shields(objp);
2131                         break;
2132
2133                 // transfer energy to weapons from shield
2134                 case XFER_LASER:
2135                         if(at_self)
2136                                 control_used(XFER_LASER);
2137                         transfer_energy_to_weapons(objp);
2138                         break;
2139
2140                 // following are not handled here, but we need to bypass the Int3()
2141                 case LAUNCH_COUNTERMEASURE:
2142                 case VIEW_SLEW:
2143                 case VIEW_EXTERNAL:
2144                 case VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK:
2145                 case ONE_THIRD_THROTTLE:
2146                 case TWO_THIRDS_THROTTLE:
2147                 case MINUS_5_PERCENT_THROTTLE:
2148                 case PLUS_5_PERCENT_THROTTLE:
2149                 case ZERO_THROTTLE:
2150                 case MAX_THROTTLE:
2151                         return 0;
2152
2153                 default :
2154                         Int3(); // bad bad bad
2155                         break;
2156         }
2157
2158         return 1;
2159 }
2160
2161 // return !0 if the action is allowed, otherwise return 0
2162 int button_allowed(int n)
2163 {
2164         if ( hud_disabled() ) {
2165                 switch (n) {
2166                 case SHOW_GOALS:
2167                 case END_MISSION:
2168                 case CYCLE_NEXT_PRIMARY:
2169                 case CYCLE_PREV_PRIMARY:
2170                 case CYCLE_SECONDARY:
2171                 case ONE_THIRD_THROTTLE:
2172                 case TWO_THIRDS_THROTTLE:
2173                 case PLUS_5_PERCENT_THROTTLE:
2174                 case MINUS_5_PERCENT_THROTTLE:
2175                 case ZERO_THROTTLE:
2176                 case MAX_THROTTLE:
2177                         return 1;
2178                 default:
2179                         return 0;
2180                 }
2181         }
2182
2183         return 1;
2184 }
2185
2186 // execute function corresponding to action n
2187 // basically, these are actions which don't affect demo playback at all
2188 int button_function_demo_valid(int n)
2189 {
2190         // by default, we'll return "not processed". ret will get set to 1, if this is one of the keys which is always allowed, even in demo
2191         // playback.
2192         int ret = 0;
2193
2194         //      No keys, not even targeting keys, when player in death roll.  He can press keys after he blows up.
2195         if (Game_mode & GM_DEAD_DIED){
2196                 return 0;
2197         }
2198
2199         // any of these buttons are valid
2200         switch(n){
2201         case VIEW_CHASE:
2202                 control_used(VIEW_CHASE);
2203                 Viewer_mode ^= VM_CHASE;
2204                 if ( Viewer_mode & VM_CHASE ) {
2205                         Viewer_mode &= ~VM_EXTERNAL;
2206                 }
2207                 ret = 1;
2208                 break;
2209
2210         case VIEW_EXTERNAL:
2211                 control_used(VIEW_EXTERNAL);
2212                 Viewer_mode ^= VM_EXTERNAL;
2213                 Viewer_mode &= ~VM_EXTERNAL_CAMERA_LOCKED;      // reset camera lock when leave/entering external view
2214                 if ( Viewer_mode & VM_EXTERNAL ) {
2215                         Viewer_mode &= ~VM_CHASE;
2216                 }
2217                 ret = 1;
2218                 break;
2219
2220         case VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK:
2221                 control_used(VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK);
2222                 if ( Viewer_mode & VM_EXTERNAL ) {
2223                 Viewer_mode ^= VM_EXTERNAL_CAMERA_LOCKED;
2224                 if ( Viewer_mode & VM_EXTERNAL_CAMERA_LOCKED ) {
2225                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "External camera is locked, controls will move ship", 36));
2226                         } else {
2227                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "External camera is free, controls will move the camera, not the ship", 37));
2228                         }
2229                 }
2230                 ret = 1;
2231                 break;
2232
2233         case VIEW_OTHER_SHIP:
2234                 control_used(VIEW_OTHER_SHIP);
2235                 if ( Player_ai->target_objnum < 0 ) {
2236                         snd_play( &Snds[SND_TARGET_FAIL] );
2237                 } else {
2238                         if ( Objects[Player_ai->target_objnum].type != OBJ_SHIP )  {
2239                                 snd_play( &Snds[SND_TARGET_FAIL] );
2240                         } else {
2241                                 Viewer_mode ^= VM_OTHER_SHIP;
2242                         }
2243                 }
2244                 ret = 1;
2245                 break;
2246
2247         case TIME_SLOW_DOWN:
2248                 if ( Game_mode & GM_NORMAL ) {
2249                         if ( Game_time_compression > F1_0) {
2250                                 Game_time_compression /= 2;
2251                         } else {
2252                                 gamesnd_play_error_beep();
2253                         }
2254                 } else {
2255                         gamesnd_play_error_beep();
2256                 }
2257                 ret = 1;
2258                 break;
2259
2260         case TIME_SPEED_UP:
2261                 if ( Game_mode & GM_NORMAL ) {
2262                         if ( Game_time_compression < (F1_0*4) ) {
2263                                 Game_time_compression *= 2;
2264                         } else {
2265                                 gamesnd_play_error_beep();
2266                         }
2267                 } else {
2268                         gamesnd_play_error_beep();
2269                 }
2270                 ret = 1;
2271                 break;
2272         }
2273
2274         // done
2275         return ret;
2276 }
2277
2278 // execute function corresponding to action n (BUTTON_ #define from KeyControl.h)
2279 int button_function(int n)
2280 {
2281         SDL_assert(n >= 0);
2282
2283         if ( !button_allowed(n) ) {
2284                 return 0;
2285         }
2286
2287         //      No keys, not even targeting keys, when player in death roll.  He can press keys after he blows up.
2288         if (Game_mode & GM_DEAD_DIED){
2289                 return 0;
2290         }
2291                 
2292         switch (n) {
2293                 // cycle to next primary weapon
2294                 case CYCLE_NEXT_PRIMARY:                        
2295                         // bogus?
2296                         if((Player_obj == NULL) || (Player_ship == NULL)){
2297                                 break;
2298                         }
2299
2300                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
2301                         if (ship_select_next_primary(Player_obj, CYCLE_PRIMARY_NEXT)) {
2302                                 ship* shipp = Player_ship;
2303                                 shipp->weapons.next_primary_fire_stamp[shipp->weapons.current_primary_bank] = timestamp(250);   //      1/4 second delay until can fire                         
2304                                 // multiplayer server should maintain bank/link status here
2305                                 // if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2306 //                                      SDL_assert(npl != NULL);
2307 //                                      multi_server_update_player_weapons(npl,shipp);                                                                          
2308 //                              }                                       
2309                         }                       
2310                         break;
2311
2312                 // cycle to previous primary weapon
2313                 case CYCLE_PREV_PRIMARY:                        
2314                         // bogus?
2315                         if((Player_obj == NULL) || (Player_ship == NULL)){
2316                                 break;
2317                         }
2318
2319                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
2320                         if (ship_select_next_primary(Player_obj, CYCLE_PRIMARY_PREV)) {
2321                                 ship* shipp = Player_ship;
2322                                 shipp->weapons.next_primary_fire_stamp[shipp->weapons.current_primary_bank] = timestamp(250);   //      1/4 second delay until can fire
2323
2324                                 // multiplayer server should maintain bank/link status here
2325                                 // if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2326                                         // SDL_assert(npl != NULL);
2327                                         // multi_server_update_player_weapons(npl,shipp);                                                                               
2328                                 // }                                    
2329                         }                       
2330                         break;
2331
2332                 // cycle to next secondary weapon
2333                 case CYCLE_SECONDARY:
2334                         return button_function_critical(CYCLE_SECONDARY);
2335                         break;
2336
2337                 // cycle number of missiles fired from secondary bank
2338                 case CYCLE_NUM_MISSLES:
2339          return button_function_critical(CYCLE_NUM_MISSLES);                    
2340                         break;
2341
2342                 // undefined in multiplayer for clients right now
2343                 // match target speed
2344                 case MATCH_TARGET_SPEED:                        
2345                         control_used(MATCH_TARGET_SPEED);
2346                         // If player is auto-matching, break auto-match speed
2347                         if ( Player->flags & PLAYER_FLAGS_AUTO_MATCH_SPEED ) {
2348                                 Player->flags &= ~PLAYER_FLAGS_AUTO_MATCH_SPEED;
2349                         }
2350                         player_match_target_speed();                                            
2351                         break;
2352
2353                 // undefined in multiplayer for clients right now
2354                 // toggle auto-match target speed
2355                 case TOGGLE_AUTO_MATCH_TARGET_SPEED:
2356                         // multiplayer observers can't match target speed
2357                         if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER)) ){
2358                                 break;
2359                         }
2360         
2361                         Player->flags ^= PLAYER_FLAGS_AUTO_MATCH_SPEED;                 
2362                         control_used(TOGGLE_AUTO_MATCH_TARGET_SPEED);
2363                         hud_gauge_popup_start(HUD_AUTO_SPEED);
2364                         if ( Players[Player_num].flags & PLAYER_FLAGS_AUTO_MATCH_SPEED ) {
2365                                 snd_play(&Snds[SND_SHIELD_XFER_OK], 1.0f);
2366 //                              HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Auto match target speed activated", -1));
2367                                 if ( !Player->flags & PLAYER_FLAGS_MATCH_TARGET ) {
2368                                         player_match_target_speed();
2369                                 }
2370                         }
2371                         else {
2372 //                              HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Auto match target deactivated", -1));
2373                                 snd_play(&Snds[SND_SHIELD_XFER_OK], 1.0f);
2374                                 player_match_target_speed();
2375                         }                       
2376                         break;
2377
2378                 // target next
2379                 case TARGET_NEXT:
2380                         control_used(TARGET_NEXT);                      
2381                         if ( hud_sensors_ok(Player_ship) ) {
2382                                 hud_target_next();
2383                         }
2384                         break;
2385
2386                 // target previous
2387                 case TARGET_PREV:
2388                         control_used(TARGET_PREV);                      
2389                         if ( hud_sensors_ok(Player_ship) ) {
2390                                 hud_target_prev();                      
2391                         }
2392                         break;
2393
2394                 // target the next hostile target
2395                 case TARGET_NEXT_CLOSEST_HOSTILE:
2396                         control_used(TARGET_NEXT_CLOSEST_HOSTILE);                      
2397                         if (hud_sensors_ok(Player_ship)){
2398                                 hud_target_next_list();
2399                         }                       
2400                         break;
2401
2402                 // target the previous closest hostile 
2403                 case TARGET_PREV_CLOSEST_HOSTILE:
2404                         control_used(TARGET_PREV_CLOSEST_HOSTILE);                      
2405                         if (hud_sensors_ok(Player_ship)){
2406                                 hud_target_next_list(1,0);
2407                         }
2408                         break;
2409
2410                 // toggle auto-targeting
2411                 case TOGGLE_AUTO_TARGETING:
2412                         control_used(TOGGLE_AUTO_TARGETING);
2413                         hud_gauge_popup_start(HUD_AUTO_TARGET);
2414                         Players[Player_num].flags ^= PLAYER_FLAGS_AUTO_TARGETING;
2415                         if ( Players[Player_num].flags & PLAYER_FLAGS_AUTO_TARGETING ) {
2416                                 if (hud_sensors_ok(Player_ship)) {
2417                                         hud_target_closest(opposing_team_mask(Player_ship->team), -1, FALSE, TRUE );
2418                                         snd_play(&Snds[SND_SHIELD_XFER_OK], 1.0f);
2419 //                                      HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Auto targeting activated", -1));
2420                                 } else {
2421                                         Players[Player_num].flags ^= PLAYER_FLAGS_AUTO_TARGETING;
2422                                 }
2423                         } else {
2424                                 snd_play(&Snds[SND_SHIELD_XFER_OK], 1.0f);
2425 //                              HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Auto targeting deactivated", -1));
2426                         }
2427                         break;
2428
2429                 // target the next friendly ship
2430                 case TARGET_NEXT_CLOSEST_FRIENDLY:
2431                         control_used(TARGET_NEXT_CLOSEST_FRIENDLY);                     
2432                         if (hud_sensors_ok(Player_ship)){
2433                                 hud_target_next_list(0);
2434                         }
2435                         break;
2436
2437                 // target the closest friendly ship
2438                 case TARGET_PREV_CLOSEST_FRIENDLY:
2439                         control_used(TARGET_PREV_CLOSEST_FRIENDLY);                     
2440                         if (hud_sensors_ok(Player_ship)) {
2441                                 hud_target_next_list(0,0);
2442                         }
2443                         break;
2444
2445                 // target ship closest to center of reticle
2446                 case TARGET_SHIP_IN_RETICLE:
2447                         control_used(TARGET_SHIP_IN_RETICLE);                   
2448                         if (hud_sensors_ok(Player_ship)){
2449                                 hud_target_in_reticle_new();
2450                         }
2451                         break;
2452
2453                 case TARGET_LAST_TRANMISSION_SENDER:
2454                         control_used(TARGET_LAST_TRANMISSION_SENDER);
2455                         if ( hud_sensors_ok(Player_ship)) {
2456                                 hud_target_last_transmit();
2457                         }
2458                         break;
2459
2460                 // target the closest repair ship
2461                 case TARGET_CLOSEST_REPAIR_SHIP:
2462                         control_used(TARGET_CLOSEST_REPAIR_SHIP);
2463                         // AL: Try to find the closest repair ship coming to repair the player... if no support
2464                         //               ships are coming to rearm the player, just try for the closest repair ship
2465                         if ( hud_target_closest_repair_ship(OBJ_INDEX(Player_obj)) == 0 ) {
2466                                 if ( hud_target_closest_repair_ship() == 0 ) {
2467                                         snd_play(&Snds[SND_TARGET_FAIL]);
2468                                 }
2469                         }
2470                         break;
2471
2472                 // target the closest ship attacking current target
2473                 case TARGET_CLOSEST_SHIP_ATTACKING_TARGET:
2474                         control_used(TARGET_CLOSEST_SHIP_ATTACKING_TARGET);
2475                         if (hud_sensors_ok(Player_ship)){
2476                                 hud_target_closest(opposing_team_mask(Player_ship->team), Player_ai->target_objnum);
2477                         }
2478                         break;
2479
2480                 // stop targeting ship
2481                 case STOP_TARGETING_SHIP:
2482                         control_used(STOP_TARGETING_SHIP);
2483                         hud_cease_targeting();
2484                         break;
2485
2486                 // target closest ship that is attacking player
2487                 case TARGET_CLOSEST_SHIP_ATTACKING_SELF:
2488                         control_used(TARGET_CLOSEST_SHIP_ATTACKING_SELF);
2489                         if (hud_sensors_ok(Player_ship)){
2490                                 hud_target_closest(opposing_team_mask(Player_ship->team), OBJ_INDEX(Player_obj), TRUE, 0, 1);
2491                         }
2492                         break;
2493
2494                 // target your target's target
2495                 case TARGET_TARGETS_TARGET:
2496                         control_used(TARGET_TARGETS_TARGET);
2497                         if (hud_sensors_ok(Player_ship)){
2498                                 hud_target_targets_target();
2499                         }
2500                         break;
2501
2502                 // target ships subsystem in reticle
2503                 case TARGET_SUBOBJECT_IN_RETICLE:
2504                         control_used(TARGET_SUBOBJECT_IN_RETICLE);
2505                         if (hud_sensors_ok(Player_ship)){
2506                                 hud_target_subsystem_in_reticle();
2507                         }
2508                         break;
2509
2510                 case TARGET_PREV_SUBOBJECT:
2511                         control_used(TARGET_PREV_SUBOBJECT);
2512                         if (hud_sensors_ok(Player_ship)){
2513                                 hud_target_prev_subobject();
2514                         }
2515                         break;
2516
2517                 // target next subsystem on current target
2518                 case TARGET_NEXT_SUBOBJECT:
2519                         control_used(TARGET_NEXT_SUBOBJECT);
2520                         if (hud_sensors_ok(Player_ship)){
2521                                 hud_target_next_subobject();
2522                         }
2523                         break;
2524
2525                 // stop targeting subsystems on ship
2526                 case STOP_TARGETING_SUBSYSTEM:
2527                         control_used(STOP_TARGETING_SUBSYSTEM);
2528                         hud_cease_subsystem_targeting();
2529                         break;
2530                         
2531                 case TARGET_NEXT_BOMB:
2532                         control_used(TARGET_NEXT_BOMB);
2533                         hud_target_missile(Player_obj, 1);
2534                         break;
2535
2536                 case TARGET_PREV_BOMB:
2537                         control_used(TARGET_PREV_BOMB);
2538                         hud_target_missile(Player_obj, 0);
2539                         break;
2540
2541                 case TARGET_NEXT_UNINSPECTED_CARGO:
2542                         hud_target_uninspected_object(1);
2543                         break;
2544
2545                 case TARGET_PREV_UNINSPECTED_CARGO:
2546                         hud_target_uninspected_object(0);
2547                         break;
2548
2549                 case TARGET_NEWEST_SHIP:
2550                         hud_target_newest_ship();
2551                         break;
2552
2553                 case TARGET_NEXT_LIVE_TURRET:
2554                         hud_target_live_turret(1);
2555                         break;
2556
2557                 case TARGET_PREV_LIVE_TURRET:
2558                         hud_target_live_turret(0);
2559                         break;
2560
2561                 // wingman message: attack current target
2562                 case ATTACK_MESSAGE:
2563                         control_used(ATTACK_MESSAGE);
2564                         hud_squadmsg_shortcut( ATTACK_TARGET_ITEM );
2565                         break;
2566
2567                 // wingman message: disarm current target
2568                 case DISARM_MESSAGE:
2569                         control_used(DISARM_MESSAGE);
2570                         hud_squadmsg_shortcut( DISARM_TARGET_ITEM );
2571                         break;
2572
2573                 // wingman message: disable current target
2574                 case DISABLE_MESSAGE:
2575                         control_used(DISABLE_MESSAGE);
2576                         hud_squadmsg_shortcut( DISABLE_TARGET_ITEM );
2577                         break;
2578
2579                 // wingman message: disable current target
2580                 case ATTACK_SUBSYSTEM_MESSAGE:
2581                         control_used(ATTACK_SUBSYSTEM_MESSAGE);
2582                         hud_squadmsg_shortcut( DISABLE_SUBSYSTEM_ITEM );
2583                         break;
2584
2585                 // wingman message: capture current target
2586                 case CAPTURE_MESSAGE:
2587                         control_used(CAPTURE_MESSAGE);
2588                         hud_squadmsg_shortcut( CAPTURE_TARGET_ITEM );
2589                         break;
2590
2591                 // wingman message: engage enemy
2592                 case ENGAGE_MESSAGE:
2593                 control_used(ENGAGE_MESSAGE);
2594                         hud_squadmsg_shortcut( ENGAGE_ENEMY_ITEM );
2595                         break;
2596
2597                 // wingman message: form on my wing
2598                 case FORM_MESSAGE:
2599                         control_used(FORM_MESSAGE);
2600                         hud_squadmsg_shortcut( FORMATION_ITEM );
2601                         break;
2602
2603                 // wingman message: protect current target
2604                 case PROTECT_MESSAGE:
2605                         control_used(PROTECT_MESSAGE);
2606                         hud_squadmsg_shortcut( PROTECT_TARGET_ITEM );
2607                         break;
2608
2609                 // wingman message: cover me
2610                 case COVER_MESSAGE:
2611                         control_used(COVER_MESSAGE);
2612                         hud_squadmsg_shortcut( COVER_ME_ITEM );
2613                         break;
2614                 
2615                 // wingman message: warp out
2616                 case WARP_MESSAGE:
2617                         control_used(WARP_MESSAGE);
2618                         hud_squadmsg_shortcut( DEPART_ITEM );
2619                         break;
2620
2621                 case IGNORE_MESSAGE:
2622                         control_used(IGNORE_MESSAGE);
2623                         hud_squadmsg_shortcut( IGNORE_TARGET_ITEM );
2624                         break;
2625
2626                 // rearm message
2627                 case REARM_MESSAGE:
2628                         control_used(REARM_MESSAGE);
2629                         hud_squadmsg_rearm_shortcut();
2630                         break;
2631
2632                 // cycle to next radar range
2633                 case RADAR_RANGE_CYCLE:
2634                         control_used(RADAR_RANGE_CYCLE);
2635                         HUD_config.rp_dist++;
2636                         if ( HUD_config.rp_dist >= RR_MAX_RANGES )
2637                                 HUD_config.rp_dist = 0;
2638
2639                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Radar range set to %s", 38), Radar_range_text(HUD_config.rp_dist));
2640                         break;
2641
2642                 // toggle the squadmate messaging menu
2643                 case SQUADMSG_MENU:
2644                         control_used(SQUADMSG_MENU);
2645                         hud_squadmsg_toggle();                          // leave the details to the messaging code!!!
2646                         break;
2647
2648                 // show the mission goals screen
2649                 case SHOW_GOALS:
2650                         control_used(SHOW_GOALS);
2651                         gameseq_post_event( GS_EVENT_SHOW_GOALS );
2652                         break;
2653
2654                 // end the mission
2655                 case END_MISSION:
2656                         // in multiplayer, all end mission requests should go through the server
2657                         if(Game_mode & GM_MULTIPLAYER){                         
2658                                 multi_handle_end_mission_request();
2659                                 break;
2660                         }
2661
2662                         control_used(END_MISSION);
2663                         
2664                         if (collide_predict_large_ship(Player_obj, 200.0f)) {
2665                                 gamesnd_play_iface(SND_GENERAL_FAIL);
2666                                 HUD_printf(XSTR( "** WARNING ** Collision danger.  Warpout not activated.", 39));
2667                         } else if (ship_get_subsystem_strength( Player_ship, SUBSYSTEM_ENGINE ) < 0.1f) {
2668                                 gamesnd_play_iface(SND_GENERAL_FAIL);
2669                                 HUD_printf(XSTR( "Engine failure.  Cannot engage warp drive.", 40));
2670                         } else {
2671                                 gameseq_post_event( GS_EVENT_PLAYER_WARPOUT_START );
2672                         }                       
2673                         break;
2674
2675                 case ADD_REMOVE_ESCORT:
2676                         if ( Player_ai->target_objnum >= 0 ) {
2677                                 control_used(ADD_REMOVE_ESCORT);
2678                                 hud_add_remove_ship_escort(Player_ai->target_objnum);
2679                         }
2680                         break;
2681
2682                 case ESCORT_CLEAR:
2683                         control_used(ESCORT_CLEAR);
2684                         hud_escort_clear_all();
2685                         break;
2686
2687                 case TARGET_NEXT_ESCORT_SHIP:
2688                         control_used(TARGET_NEXT_ESCORT_SHIP);
2689                         hud_escort_target_next();
2690                         break;
2691
2692                 // increase weapon recharge rate
2693                 case INCREASE_WEAPON:
2694                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2695                         return button_function_critical(INCREASE_WEAPON);
2696                         break;
2697
2698                 // decrease weapon recharge rate
2699                 case DECREASE_WEAPON:
2700                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2701                         return button_function_critical(DECREASE_WEAPON);
2702                         break;
2703
2704                 // increase shield recharge rate
2705                 case INCREASE_SHIELD:
2706                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2707                         return button_function_critical(INCREASE_SHIELD);
2708                         break;
2709
2710                 // decrease shield recharge rate
2711                 case DECREASE_SHIELD:
2712                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2713                         return button_function_critical(DECREASE_SHIELD);
2714                         break;
2715
2716                 // increase energy to engines
2717                 case INCREASE_ENGINE:
2718                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2719                         return button_function_critical(INCREASE_ENGINE);
2720                         break;
2721
2722                 // decrease energy to engines
2723                 case DECREASE_ENGINE:
2724                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2725                         return button_function_critical(DECREASE_ENGINE);
2726                         break;
2727
2728                 case ETS_EQUALIZE:
2729                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2730                         return button_function_critical(ETS_EQUALIZE);
2731                         break;
2732
2733                 // equalize shield energy to all quadrants
2734                 case SHIELD_EQUALIZE:
2735                         return button_function_critical(SHIELD_EQUALIZE);
2736                         break;
2737
2738                 // transfer shield energy to front
2739                 case SHIELD_XFER_TOP:
2740          return button_function_critical(SHIELD_XFER_TOP);
2741                         break;
2742
2743                 // transfer shield energy to rear
2744                 case SHIELD_XFER_BOTTOM:
2745                         return button_function_critical(SHIELD_XFER_BOTTOM);
2746                         break;
2747
2748                 // transfer shield energy to left
2749                 case SHIELD_XFER_LEFT:
2750                         return button_function_critical(SHIELD_XFER_LEFT);
2751                         break;
2752                 
2753                 // transfer shield energy to right
2754                 case SHIELD_XFER_RIGHT:
2755                         return button_function_critical(SHIELD_XFER_RIGHT);
2756                         break;
2757
2758                 // transfer energy to shield from weapons
2759                 case XFER_SHIELD:
2760                         return button_function_critical(XFER_SHIELD);
2761                         break;
2762
2763                 // transfer energy to weapons from shield
2764                 case XFER_LASER:
2765                         return button_function_critical(XFER_LASER);
2766                         break;
2767
2768                 // message all netplayers button
2769                 case MULTI_MESSAGE_ALL:
2770                         multi_msg_key_down(MULTI_MSG_ALL);
2771                         break;
2772
2773                 // message all friendlies button
2774                 case MULTI_MESSAGE_FRIENDLY:
2775                         multi_msg_key_down(MULTI_MSG_FRIENDLY);
2776                         break;
2777
2778                 // message all hostiles button
2779                 case MULTI_MESSAGE_HOSTILE:
2780                         multi_msg_key_down(MULTI_MSG_HOSTILE);
2781                         break;
2782
2783                 // message targeted ship (if player)
2784                 case MULTI_MESSAGE_TARGET:
2785                         multi_msg_key_down(MULTI_MSG_TARGET);
2786                         break;
2787
2788                 // if i'm an observer, zoom to my targeted object
2789                 case MULTI_OBSERVER_ZOOM_TO:
2790                         multi_obs_zoom_to_target();
2791                         break;          
2792 #ifndef MAKE_FS1
2793                 // toggle between high and low HUD contrast
2794                 case TOGGLE_HUD_CONTRAST:
2795                         gamesnd_play_iface(SND_USER_SELECT);
2796                         hud_toggle_contrast();
2797                         break;
2798
2799                 // toggle network info
2800                 case MULTI_TOGGLE_NETINFO:
2801                         extern int Multi_display_netinfo;
2802                         Multi_display_netinfo = !Multi_display_netinfo;
2803                         break;
2804
2805                 // self destruct (multiplayer only)
2806                 case MULTI_SELF_DESTRUCT:
2807                         if(!(Game_mode & GM_MULTIPLAYER)){
2808                                 break;
2809                         }
2810
2811                         // bogus netplayer
2812                         if((Net_player == NULL) || (Net_player->player == NULL)){
2813                                 break;
2814                         }
2815
2816                         // blow myself up, if I'm the server
2817                         if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
2818                                 if((Net_player->player->objnum >= 0) && (Net_player->player->objnum < MAX_OBJECTS) && 
2819                                         (Objects[Net_player->player->objnum].type == OBJ_SHIP) && (Objects[Net_player->player->objnum].instance >= 0) && (Objects[Net_player->player->objnum].instance < MAX_SHIPS)){
2820
2821                                         ship_self_destruct(&Objects[Net_player->player->objnum]);
2822                                 }
2823                         }
2824                         // otherwise send a packet to the server
2825                         else {
2826                                 send_self_destruct_packet();
2827                         }
2828                         break;
2829 #endif
2830                 // following are not handled here, but we need to bypass the Int3()
2831                 case LAUNCH_COUNTERMEASURE:
2832                 case VIEW_SLEW:
2833                 case ONE_THIRD_THROTTLE:
2834                 case TWO_THIRDS_THROTTLE:
2835                 case MINUS_5_PERCENT_THROTTLE:
2836                 case PLUS_5_PERCENT_THROTTLE:
2837                 case ZERO_THROTTLE:
2838                 case MAX_THROTTLE:
2839                         return 0;
2840
2841                 default:
2842                         Int3();
2843                         break;
2844         } // end switch
2845
2846         return 1;
2847 }
2848
2849 // Call functions for when buttons are pressed
2850 void button_info_do(button_info *bi)
2851 {
2852         int i, j;
2853
2854         for (i=0; i<NUM_BUTTON_FIELDS; i++) {
2855                 if ( bi->status[i] == 0 ){
2856                         continue;
2857                 }
2858
2859                 // at least one bit is set in the status integer
2860                 for (j=0; j<32; j++) {
2861
2862                         // check if the bit is set. If button_function returns 1 (implying the action was taken), then unset the bit
2863                         if ( bi->status[i] & (1 << j) ) {
2864                                 // always process buttons which are valid for demo playback
2865                                 if(button_function_demo_valid(32 * i + j)){
2866                                         bi->status[i] &= ~(1 << j);
2867                                 }
2868                                 // other buttons
2869                                 else {
2870                                         // if we're in demo playback, always clear the bits
2871                                         if(Game_mode & GM_DEMO_PLAYBACK){
2872                                                 bi->status[i] &= ~(1 << j);
2873                                         }
2874                                         // otherwise check as normal
2875                                         else if (button_function(32 * i + j)) {
2876                                                 bi->status[i] &= ~(1 << j);                                     
2877                                         }
2878                                 }
2879                         }
2880                 }
2881         }
2882 }
2883
2884
2885 // set the bit for the corresponding action n (BUTTON_ #define from KeyControl.h)
2886 void button_info_set(button_info *bi, int n)
2887 {
2888         int field_num, bit_num;
2889         
2890         field_num = n / 32;
2891         bit_num = n % 32;
2892
2893         bi->status[field_num] |= (1 << bit_num);        
2894 }
2895
2896 // unset the bit for the corresponding action n (BUTTON_ #define from KeyControl.h)
2897 void button_info_unset(button_info *bi, int n)
2898 {
2899         int field_num, bit_num;
2900         
2901         field_num = n / 32;
2902         bit_num = n % 32;
2903
2904         bi->status[field_num] &= ~(1 << bit_num);       
2905 }
2906
2907 int button_info_query(button_info *bi, int n)
2908 {
2909         return bi->status[n / 32] & (1 << (n % 32));
2910 }
2911
2912 // clear out the button_info struct
2913 void button_info_clear(button_info *bi)
2914 {
2915         int i;
2916
2917         for (i=0; i<NUM_BUTTON_FIELDS; i++) {
2918                 bi->status[i] = 0;
2919         }
2920 }
2921
2922 // strip out all noncritical keys from the button info struct
2923 void button_strip_noncritical_keys(button_info *bi)
2924 {
2925         int idx;
2926
2927         // clear out all noncritical keys
2928         for(idx=0;idx<Non_critical_key_set_size;idx++){
2929                 button_info_unset(bi,Non_critical_key_set[idx]);
2930         }
2931 }