]> icculus.org git repositories - taylor/freespace2.git/blob - src/io/keycontrol.cpp
PXO, works and everything (probably)
[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
510         TOGGLE_HUD_CONTRAST,
511
512         MULTI_TOGGLE_NETINFO,
513         MULTI_SELF_DESTRUCT
514 };
515
516 int Dead_key_set[] = {
517         TARGET_NEXT,
518         TARGET_PREV,
519         TARGET_NEXT_CLOSEST_HOSTILE,
520         TARGET_PREV_CLOSEST_HOSTILE,
521         TARGET_NEXT_CLOSEST_FRIENDLY,
522         TARGET_PREV_CLOSEST_FRIENDLY,
523         TARGET_TARGETS_TARGET,
524         TARGET_CLOSEST_SHIP_ATTACKING_TARGET,
525         STOP_TARGETING_SHIP,
526         TOGGLE_AUTO_TARGETING,
527         TARGET_SUBOBJECT_IN_RETICLE,
528         TARGET_PREV_SUBOBJECT,
529         TARGET_NEXT_SUBOBJECT,
530         STOP_TARGETING_SUBSYSTEM,
531         TARGET_NEWEST_SHIP,
532         TARGET_NEXT_LIVE_TURRET,
533         TARGET_PREV_LIVE_TURRET,
534         TARGET_NEXT_BOMB,
535         TARGET_PREV_BOMB,
536
537         VIEW_CHASE,
538         VIEW_OTHER_SHIP,
539
540         SHOW_GOALS,
541
542         ADD_REMOVE_ESCORT,
543         ESCORT_CLEAR,
544         TARGET_NEXT_ESCORT_SHIP,
545         TARGET_CLOSEST_REPAIR_SHIP,     
546
547         MULTI_MESSAGE_ALL,
548         MULTI_MESSAGE_FRIENDLY,
549         MULTI_MESSAGE_HOSTILE,
550         MULTI_MESSAGE_TARGET,
551         MULTI_OBSERVER_ZOOM_TO,
552
553         TIME_SPEED_UP,
554         TIME_SLOW_DOWN
555 };
556
557 int Critical_key_set[] = {              
558         CYCLE_SECONDARY,                        
559         CYCLE_NUM_MISSLES,                      
560         INCREASE_WEAPON,                        
561         DECREASE_WEAPON,        
562         INCREASE_SHIELD,                        
563         DECREASE_SHIELD,                        
564         INCREASE_ENGINE,                        
565         DECREASE_ENGINE,                        
566         ETS_EQUALIZE,
567         SHIELD_EQUALIZE,                        
568         SHIELD_XFER_TOP,                        
569         SHIELD_XFER_BOTTOM,                     
570         SHIELD_XFER_LEFT,                       
571         SHIELD_XFER_RIGHT,                      
572         XFER_SHIELD,                    
573         XFER_LASER,                     
574 };
575
576 int Non_critical_key_set[] = {
577         CYCLE_NEXT_PRIMARY,             
578         CYCLE_PREV_PRIMARY,                     
579         MATCH_TARGET_SPEED,                     
580         TOGGLE_AUTO_MATCH_TARGET_SPEED,                 
581         TARGET_NEXT,    
582         TARGET_PREV,                    
583         TARGET_NEXT_CLOSEST_HOSTILE,                    
584         TARGET_PREV_CLOSEST_HOSTILE,                    
585         TOGGLE_AUTO_TARGETING,                  
586         TARGET_NEXT_CLOSEST_FRIENDLY,                   
587         TARGET_PREV_CLOSEST_FRIENDLY,                   
588         TARGET_SHIP_IN_RETICLE,                 
589         TARGET_LAST_TRANMISSION_SENDER,
590         TARGET_CLOSEST_REPAIR_SHIP,                     
591         TARGET_CLOSEST_SHIP_ATTACKING_TARGET,                   
592         STOP_TARGETING_SHIP,                    
593         TARGET_CLOSEST_SHIP_ATTACKING_SELF,                     
594         TARGET_TARGETS_TARGET,                  
595         TARGET_SUBOBJECT_IN_RETICLE,                    
596         TARGET_PREV_SUBOBJECT,                  
597         TARGET_NEXT_SUBOBJECT,                  
598         STOP_TARGETING_SUBSYSTEM,
599         TARGET_NEXT_BOMB,
600         TARGET_PREV_BOMB,
601         TARGET_NEXT_UNINSPECTED_CARGO,
602         TARGET_PREV_UNINSPECTED_CARGO,
603         TARGET_NEWEST_SHIP,
604         TARGET_NEXT_LIVE_TURRET,
605         TARGET_PREV_LIVE_TURRET,
606         ATTACK_MESSAGE,
607         DISARM_MESSAGE,
608         DISABLE_MESSAGE,
609         ATTACK_SUBSYSTEM_MESSAGE,
610         CAPTURE_MESSAGE,
611         ENGAGE_MESSAGE,
612    FORM_MESSAGE,
613         PROTECT_MESSAGE,
614         COVER_MESSAGE,
615         WARP_MESSAGE,
616         IGNORE_MESSAGE,
617         REARM_MESSAGE,
618         VIEW_CHASE,
619         VIEW_EXTERNAL,
620         VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK,
621         VIEW_OTHER_SHIP,
622         RADAR_RANGE_CYCLE,
623         SQUADMSG_MENU,
624         SHOW_GOALS,
625         END_MISSION,
626         ADD_REMOVE_ESCORT,
627         ESCORT_CLEAR,
628         TARGET_NEXT_ESCORT_SHIP,        
629         MULTI_MESSAGE_ALL,
630         MULTI_MESSAGE_FRIENDLY,
631         MULTI_MESSAGE_HOSTILE,
632         MULTI_MESSAGE_TARGET,
633         MULTI_OBSERVER_ZOOM_TO,                 
634         TOGGLE_HUD_CONTRAST,
635
636         MULTI_TOGGLE_NETINFO,
637         MULTI_SELF_DESTRUCT
638 };
639
640
641 // set sizes of the key sets automatically
642 int Normal_key_set_size = sizeof(Normal_key_set) / sizeof(int);
643 int Dead_key_set_size = sizeof(Dead_key_set) / sizeof(int);
644 int Critical_key_set_size = sizeof(Critical_key_set) / sizeof(int);
645 int Non_critical_key_set_size = sizeof(Non_critical_key_set) / sizeof(int);
646
647 // --------------------------------------------------------------
648 // routine to process keys used only for debugging
649 // --------------------------------------------------------------
650 //#ifndef NDEBUG
651
652 void debug_cycle_player_ship(int delta)
653 {
654         if ( Player_obj == NULL )
655                 return;
656
657         int si_index = Ships[Player_obj->instance].ship_info_index;
658         int sanity = 0;
659         ship_info       *sip;
660         while ( TRUE ) {
661                 si_index += delta;
662                 if ( si_index > MAX_SHIP_TYPES ){
663                         si_index = 0;
664                 }
665                 if ( si_index < 0 ){
666                         si_index = MAX_SHIP_TYPES - 1;
667                 }
668                 sip = &Ship_info[si_index];
669                 if ( sip->flags & SIF_PLAYER_SHIP ){
670                         break;
671                 }
672
673                 // just in case
674                 sanity++;
675                 if ( sanity > MAX_SHIP_TYPES ){
676                         break;
677                 }
678         }
679
680         change_ship_type(Player_obj->instance, si_index);
681         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Player ship changed to %s", 0), Ship_info[si_index].name);                 
682 }
683
684 // cycle targeted ship to next ship in that species
685 void debug_cycle_targeted_ship(int delta)
686 {
687         object          *objp;
688         ship_info       *sip;
689         int                     si_index, species;
690         char                    name[NAME_LENGTH];
691
692         if ( Player_ai->target_objnum == -1 )
693                 return;
694
695         objp = &Objects[Player_ai->target_objnum];
696         if ( objp->type != OBJ_SHIP )
697                 return;
698
699         si_index = Ships[objp->instance].ship_info_index;
700         SDL_assert(si_index != -1 );
701         species = Ship_info[si_index].species;
702
703         int sanity = 0;
704
705         while ( TRUE ) {
706                 si_index += delta;
707                 if ( si_index > MAX_SHIP_TYPES )
708                         si_index = 0;
709                 if ( si_index < 0 )
710                         si_index = MAX_SHIP_TYPES-1;
711
712         
713                 sip = &Ship_info[si_index];
714         
715                 // if it has test in the name, jump over it
716                 SDL_strlcpy(name, sip->name, SDL_arraysize(name));
717                 SDL_strlwr(name);
718                 if ( strstr(name,NOX("test")) != NULL )
719                         continue;
720
721                 if ( sip->species == species && (sip->flags & (SIF_FIGHTER | SIF_BOMBER | SIF_TRANSPORT) ) )
722                         break;
723
724                 // just in case
725                 sanity++;
726                 if ( sanity > MAX_SHIP_TYPES )
727                         break;
728         }
729
730         change_ship_type(objp->instance, si_index);
731         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Changed player target to %s", 1), Ship_info[si_index].name);                       
732 }
733
734 void debug_max_secondary_weapons(object *objp)
735 {
736         int index;
737         ship *shipp = &Ships[objp->instance];
738         ship_info *sip = &Ship_info[shipp->ship_info_index];
739
740         for ( index = 0; index < MAX_SECONDARY_BANKS; index++ ) {
741                 shipp->weapons.secondary_bank_ammo[index] = sip->secondary_bank_ammo_capacity[index];
742         }
743 }
744
745 void debug_change_song(int delta)
746 {
747         char buf[256];
748         if ( event_music_next_soundtrack(delta) != -1 ) {
749                 event_music_get_soundtrack_name(buf, sizeof(buf));
750                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Soundtrack changed to: %s", 2), buf);
751
752         } else {
753                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Event music is not playing", 3));
754         }
755 }
756
757 //extern void set_global_ignore_object(int objnum);
758
759 extern void hud_target_asteroid();
760 extern int Framerate_delay;
761
762 extern void snd_stop_any_sound();
763
764 void process_debug_keys(int k)
765 {
766 #ifdef INTERPLAYQA
767         if ( !Debug_allowed )
768                 return;
769 #endif
770
771         // if ( (k & KEY_DEBUGGED) && (Game_mode & GM_RECORDING_DEMO) )
772                 // return;
773
774         switch (k) {
775                 case KEY_DEBUGGED + SDLK_h:
776                         hud_target_toggle_hidden_from_sensors();
777                         break;
778
779                 case KEY_DEBUGGED + SDLK_f:
780                         /*
781                         int i;
782                         for (i=0; i<NUM_HUD_GAUGES; i++) {
783                                 hud_gauge_start_flash(i);
784                         }
785                         */
786                         extern int wacky_scheme;
787                         if(wacky_scheme == 3){
788                                 wacky_scheme = 0;
789                         } else {
790                                 wacky_scheme++;
791                         }
792                         break;
793                 
794                 case KEY_DEBUGGED + KEY_ALTED + SDLK_f:
795                         Framerate_delay += 10;
796                         HUD_printf(XSTR( "Framerate delay increased to %i milliseconds per frame.", 4), Framerate_delay);
797                         break;
798
799                 case KEY_DEBUGGED + KEY_ALTED + KEY_SHIFTED + SDLK_f:
800                         Framerate_delay -= 10;
801                         if (Framerate_delay < 0)
802                                 Framerate_delay = 0;
803
804                         HUD_printf(XSTR( "Framerate delay decreased to %i milliseconds per frame.", 5), Framerate_delay);
805                         break;
806
807                 case KEY_DEBUGGED + SDLK_c:
808                 case KEY_DEBUGGED1 + SDLK_c:
809                         // hud_enemymsg_toggle();
810                         if(Player_obj->flags & OF_COLLIDES){
811                                 obj_set_flags(Player_obj, Player_obj->flags & ~(OF_COLLIDES));
812                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, "Player no longer collides");
813                         } else {
814                                 obj_set_flags(Player_obj, Player_obj->flags | OF_COLLIDES);
815                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, "Player collides");
816                         }
817                         break;
818
819                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_c:
820                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_c:
821                         Countermeasures_enabled = !Countermeasures_enabled;
822                         HUD_printf(XSTR( "Countermeasure firing: %s", 6), Countermeasures_enabled ? XSTR( "ENABLED", 7) : XSTR( "DISABLED", 8));
823                         break;
824
825                 case KEY_DEBUGGED + SDLK_e:
826                         gameseq_post_event(GS_EVENT_EVENT_DEBUG);
827                         break;
828
829                 case KEY_DEBUGGED + SDLK_COMMA:
830                         if ( Game_time_compression > (F1_0/4) ){                // can't compress below 0.25
831                                 Game_time_compression /= 2;
832                         }
833                         break;
834                 case KEY_DEBUGGED + SDLK_PERIOD:
835                         if ( Game_time_compression < (F1_0*8) ){
836                                 Game_time_compression *= 2;
837                         }
838                         break;
839
840                 //      Kill! the currently targeted ship.
841                 case KEY_DEBUGGED + SDLK_k:
842                 case KEY_DEBUGGED1 + SDLK_k:
843                         if (Player_ai->target_objnum != -1) {
844                                 object  *objp = &Objects[Player_ai->target_objnum];
845
846                                 switch (objp->type) {
847                                 case OBJ_SHIP:
848                                         ship_apply_local_damage( objp, Player_obj, &objp->pos, 100000.0f, MISS_SHIELDS, CREATE_SPARKS);
849                                         ship_apply_local_damage( objp, Player_obj, &objp->pos, 1.0f, MISS_SHIELDS, CREATE_SPARKS);
850                                         break;
851                                 case OBJ_WEAPON:
852                                         Weapons[objp->instance].lifeleft = 0.01f;
853                                         break;
854                                 }
855                         }
856
857                         break;
858                 
859                 // play the next mission message
860                 case KEY_DEBUGGED + SDLK_v:
861                         extern int Message_debug_index;
862                         extern int Num_messages_playing;
863                         // stop any other messages
864                         if(Num_messages_playing){
865                                 message_kill_all(1);
866                         }
867
868                         // next message
869                         if(Message_debug_index >= Num_messages - 1){
870                                 Message_debug_index = Num_builtin_messages;
871                         } else {
872                                 Message_debug_index++;
873                         }
874                         
875                         // play the message
876                         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 );                    
877                         if (Messages[Message_debug_index].avi_info.index == -1) {
878                                 HUD_printf("No anim set for message \"%s\"; None will play!", Messages[Message_debug_index].name);
879                         }
880                         break;
881
882                 // play the previous mission message
883                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_v:
884                         extern int Message_debug_index;
885                         extern int Num_messages_playing;
886                         // stop any other messages
887                         if(Num_messages_playing){
888                                 message_kill_all(1);
889                         }
890
891                         // go maybe go down one
892                         if(Message_debug_index == Num_builtin_messages - 1){
893                                 Message_debug_index = Num_builtin_messages;
894                         } else if(Message_debug_index > Num_builtin_messages){
895                                 Message_debug_index--;
896                         }
897                         
898                         // play the message
899                         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 );
900                         if (Messages[Message_debug_index].avi_info.index == -1) {
901                                 HUD_printf("No avi associated with this message; None will play!");
902                         }
903                         break;
904
905                 // reset to the beginning of mission messages
906                 case KEY_DEBUGGED + KEY_ALTED + SDLK_v:
907                         extern int Message_debug_index;
908                         Message_debug_index = Num_builtin_messages - 1;
909                         HUD_printf("Resetting to first mission message");
910                         break;
911
912                 //      Kill! the currently targeted ship.
913                 case KEY_DEBUGGED + KEY_ALTED + KEY_SHIFTED + SDLK_k:
914                 case KEY_DEBUGGED1 + KEY_ALTED + KEY_SHIFTED + SDLK_k:
915                         if (Player_ai->target_objnum != -1) {
916                                 object  *objp = &Objects[Player_ai->target_objnum];
917
918                                 if (objp->type == OBJ_SHIP) {
919                                         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);
920                                 }
921                         }
922                         break;
923
924                         //      Kill the currently targeted subsystem.
925                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_k:
926                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_k:
927                         if ((Player_ai->target_objnum != -1) && (Player_ai->targeted_subsys != NULL)) {
928                                 object  *objp = &Objects[Player_ai->target_objnum];
929                                 if ( objp->type == OBJ_SHIP ) {
930                                         ship            *sp = &Ships[objp->instance];
931                                         vector  g_subobj_pos;
932
933                                         get_subsystem_world_pos(objp, Player_ai->targeted_subsys, &g_subobj_pos);
934
935                                         do_subobj_hit_stuff(objp, Player_obj, &g_subobj_pos, (float) -Player_ai->targeted_subsys->system_info->type); //100.0f);
936
937                                         if ( sp->subsys_info[SUBSYSTEM_ENGINE].current_hits <= 0.0f ) {
938                                                 mission_log_add_entry(LOG_SHIP_DISABLED, sp->ship_name, NULL );
939                                                 sp->flags |= SF_DISABLED;                               // add the disabled flag
940                                         }
941
942                                         if ( sp->subsys_info[SUBSYSTEM_TURRET].current_hits <= 0.0f ) {
943                                                 mission_log_add_entry(LOG_SHIP_DISARMED, sp->ship_name, NULL );
944                                                 // sp->flags |= SF_DISARMED;                            // add the disarmed flag
945                                         }
946                                 }
947                         }
948                         break;
949
950                 case KEY_DEBUGGED + KEY_ALTED + SDLK_k:
951                 case KEY_DEBUGGED1 + KEY_ALTED + SDLK_k:
952                         {
953                                 float   shield, integrity;
954                                 vector  pos, randvec;
955
956                                 vm_vec_rand_vec_quick(&randvec);
957                                 vm_vec_scale_add(&pos, &Player_obj->pos, &randvec, Player_obj->radius);
958                         ship_apply_local_damage(Player_obj, Player_obj, &pos, 25.0f, MISS_SHIELDS, CREATE_SPARKS);
959                         hud_get_target_strength(Player_obj, &shield, &integrity);
960                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "You knocked yourself down to %7.3f percent hull.\n", 9), 100.0f * integrity);
961                         break;
962                         }
963                         
964                 //      Whack down the player's shield and hull by a little more than 50%
965                 //      Select next object to be viewed by AI.
966                 case KEY_DEBUGGED + SDLK_i:
967                 case KEY_DEBUGGED1 + SDLK_i:
968                         Player_obj->flags ^= OF_INVULNERABLE;
969                         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));
970                         break;
971
972                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_i:
973                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_i:
974                         if (Player_ai->target_objnum != -1) {
975                                 object  *objp = &Objects[Player_ai->target_objnum];
976
977                                 objp->flags ^= OF_INVULNERABLE;
978                                 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));
979                         }
980                         break;
981 /*
982                 case KEY_DEBUGGED + KEY_ALTED + SDLK_i:
983                         if (Player_ai->target_objnum != -1)
984                                 set_global_ignore_object(Player_ai->target_objnum);
985                         break;
986 */
987
988                 case KEY_DEBUGGED + SDLK_n:
989                         AI_watch_object++;
990                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Spewing debug info about object #%d", 14), AI_watch_object);
991                         break;
992
993                 case KEY_DEBUGGED + SDLK_o:
994 #ifdef MAKE_FS1
995                 case KEY_DEBUGGED1 + SDLK_o:
996 #endif
997                         toggle_player_object();
998                         break;                          
999
1000                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_o:
1001                         extern int Debug_octant;
1002                         if(Debug_octant == 7){
1003                                 Debug_octant = -1;
1004                         } else {
1005                                 Debug_octant++;
1006                         }
1007                         nprintf(("General", "Debug_octant == %d\n", Debug_octant));
1008                         break;
1009
1010                 case KEY_DEBUGGED + SDLK_p:
1011                         supernova_start(20);
1012                         break;
1013
1014                 case KEY_DEBUGGED + SDLK_w:
1015                 case KEY_DEBUGGED1 + SDLK_w:
1016                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_w:
1017                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_w:
1018                         // temp code for testing purposes, toggles weapon energy cheat
1019                         Weapon_energy_cheat = !Weapon_energy_cheat;
1020                         if (Weapon_energy_cheat) {
1021                                 if (k & KEY_SHIFTED)
1022                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Weapon energy and missile count will always be at full ALL SHIPS!", 15));
1023                                 else
1024                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Weapon energy and missile count will always be at full for player", 16));
1025
1026                                 debug_max_secondary_weapons(Player_obj);
1027                                 if (k & KEY_SHIFTED) {
1028                                         object  *objp;
1029
1030                                         for ( objp = GET_FIRST(&obj_used_list); objp !=END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) )
1031                                                 if (objp->type == OBJ_SHIP)
1032                                                         debug_max_secondary_weapons(objp);
1033                                 }
1034
1035                         } else
1036                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Normal weapon energy system / missile count restored", 17));
1037
1038                         break;
1039
1040                 case KEY_DEBUGGED + SDLK_g:
1041 #ifdef MAKE_FS1
1042                 case KEY_DEBUGGED1 + SDLK_g:
1043 #endif
1044                         mission_goal_mark_all_true( PRIMARY_GOAL );
1045                         break;
1046
1047                 case KEY_DEBUGGED + SDLK_g + KEY_SHIFTED:
1048 #ifdef MAKE_FS1
1049                 case KEY_DEBUGGED1 + SDLK_g + KEY_SHIFTED:
1050 #endif
1051                         mission_goal_mark_all_true( SECONDARY_GOAL );
1052                         break;
1053
1054                 case KEY_DEBUGGED + SDLK_g + KEY_ALTED:
1055 #ifdef MAKE_FS1
1056                 case KEY_DEBUGGED1 + SDLK_g + KEY_ALTED:
1057 #endif
1058                         mission_goal_mark_all_true( BONUS_GOAL );
1059                         break;
1060
1061                 case KEY_DEBUGGED + SDLK_9: {
1062                 case KEY_DEBUGGED1 + SDLK_9:
1063                         ship* shipp;
1064
1065                         shipp = &Ships[Player_obj->instance];
1066                         shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank]++;
1067                         if ( shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank] >= Num_weapon_types )
1068                                 shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank] = First_secondary_index;
1069
1070                         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);
1071                         break;
1072                 }
1073
1074 #ifdef MAKE_FS1
1075                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_9: {
1076                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_9:
1077                         ship* shipp;
1078
1079                         shipp = &Ships[Player_obj->instance];
1080                         shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank]--;
1081                         if ( shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank] < 0)
1082                                 shipp->weapons.secondary_bank_weapons[shipp->weapons.current_secondary_bank] = Num_weapon_types - 1;
1083
1084                         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);
1085                         break;
1086                 }
1087 #endif
1088
1089 #if !(defined(FS2_DEMO) || defined(FS1_DEMO))
1090                 case KEY_DEBUGGED + SDLK_u: {
1091                 case KEY_DEBUGGED1 + SDLK_u:
1092                         // launch asteroid
1093                         extern asteroid_field Asteroid_field;
1094                         object *asteroid_create(asteroid_field *asfieldp, int asteroid_type, int subtype);
1095                         object *objp = asteroid_create(&Asteroid_field, 0, 0);
1096                         vector vel;
1097                         vm_vec_copy_scale(&vel, &Player_obj->orient.v.fvec, 50.0f);
1098                         objp->phys_info.vel = vel;
1099                         objp->phys_info.desired_vel = vel;
1100                         objp->pos = Player_obj->pos;
1101                         //mission_goal_mark_all_true( PRIMARY_GOAL );
1102                         break;
1103                 }
1104 #endif
1105
1106                 case KEY_DEBUGGED + SDLK_0: {
1107                 case KEY_DEBUGGED1 + SDLK_0:
1108                         ship* shipp;
1109
1110                         shipp = &Ships[Player_obj->instance];
1111                         shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank]++;
1112                         if ( shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank] >= First_secondary_index )
1113                                 shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank] = 0;
1114
1115                         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);
1116                         break;
1117                 }
1118
1119                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_0: {
1120                 case KEY_DEBUGGED1 + KEY_SHIFTED + SDLK_0:
1121                         ship* shipp;
1122
1123                         shipp = &Ships[Player_obj->instance];
1124                         shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank]--;
1125                         if ( shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank] < 0)
1126                                 shipp->weapons.primary_bank_weapons[shipp->weapons.current_primary_bank] = First_secondary_index-1 ;
1127
1128                         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);
1129                         break;
1130                 }
1131
1132                 case KEY_DEBUGGED + SDLK_j: {
1133                         int new_pattern = event_music_return_current_pattern();
1134
1135                         new_pattern++;
1136                         if ( new_pattern >= MAX_PATTERNS )
1137                                 new_pattern = 0;
1138
1139                         event_music_change_pattern(new_pattern);
1140                         break;
1141                 }
1142
1143                 case KEY_DEBUGGED + SDLK_m: {
1144                         if ( Event_music_enabled ) {
1145                                 event_music_disable();
1146                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Event music disabled", 20));
1147
1148                         } else {
1149                                 event_music_enable();
1150                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Event music enabled", 21));
1151                         }
1152
1153                         break;
1154                 }
1155
1156                 case KEY_DEBUGGED + SDLK_r: {
1157                 // case KEY_DEBUGGED1 + SDLK_r:
1158                         if (Player_ai->target_objnum != -1)
1159                                 ai_issue_rearm_request(&Objects[Player_ai->target_objnum]);
1160                         else
1161                                 ai_issue_rearm_request(Player_obj);
1162
1163                         break;
1164                 }
1165
1166                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_UP:
1167                         Game_detail_level++;
1168                         HUD_printf( XSTR( "Detail level set to %+d\n", 22), Game_detail_level );
1169                         break;
1170
1171                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_DOWN:
1172                         Game_detail_level--;
1173                         HUD_printf( XSTR( "Detail level set to %+d\n", 22), Game_detail_level );
1174                         break;
1175
1176 #ifndef NDEBUG
1177                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_t:       {
1178                         extern int Test_begin;
1179
1180                         if ( Test_begin == 1 )
1181                                 break;
1182
1183                         Test_begin = 1;
1184                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Frame Rate test started", 23));
1185
1186                         break;
1187                 }
1188 #endif
1189                 case KEY_DEBUGGED + SDLK_d:
1190                         extern int OO_update_index;                     
1191
1192                         if(MULTIPLAYER_MASTER){
1193                                 do {
1194                                         OO_update_index++;
1195                                 } while((OO_update_index < (MAX_PLAYERS-1)) && !MULTI_CONNECTED(Net_players[OO_update_index]));
1196                                 if(OO_update_index >= MAX_PLAYERS-1){
1197                                         OO_update_index = -1;
1198                                 }                       
1199                         } else {
1200                                 if(OO_update_index < 0){
1201                                         OO_update_index = MY_NET_PLAYER_NUM;
1202                                 } else {
1203                                         OO_update_index = -1;
1204                                 }
1205                         }
1206                         break;
1207
1208                 // change player ship to next flyable type
1209                 case KEY_DEBUGGED + SDLK_RIGHT:
1210                         debug_cycle_player_ship(1);
1211                         break;
1212
1213                 // change player ship to previous flyable ship
1214                 case KEY_DEBUGGED + SDLK_LEFT:
1215                         debug_cycle_player_ship(-1);
1216                         break;
1217                 
1218                 // cycle target to ship
1219                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_RIGHT:
1220                         debug_cycle_targeted_ship(1);
1221                         break;
1222
1223                 // cycle target to previous ship
1224                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_LEFT:
1225                         debug_cycle_targeted_ship(-1);
1226                         break;
1227
1228                 // change species of the targeted ship
1229                 case KEY_DEBUGGED + SDLK_s: {
1230                         if ( Player_ai->target_objnum < 0 )
1231                                 break;
1232
1233                         object          *objp;
1234                         ship_info       *sip;
1235
1236                         objp = &Objects[Player_ai->target_objnum];
1237                         if ( objp->type != OBJ_SHIP )
1238                                 return;
1239
1240                         sip = &Ship_info[Ships[objp->instance].ship_info_index];
1241                         sip->species++;
1242                         if ( sip->species > SPECIES_SHIVAN )
1243                                 sip->species = 0;
1244
1245                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Species of target changed to: %s", 24), Species_names[sip->species]);
1246                         break;
1247                 }
1248                         
1249                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_s:
1250                         game_increase_skill_level();
1251                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Skill level set to %s.", 25), Skill_level_names(Game_skill_level));
1252                         break;
1253
1254                 // kill all missiles
1255                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_1:
1256                         beam_test(1);
1257                         break;                          
1258                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_2:
1259                         beam_test(2);
1260                         break;          
1261                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_3:
1262                         beam_test(3);
1263                         break;                          
1264                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_4:
1265                         beam_test(4);
1266                         break;          
1267                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_5:
1268                         beam_test(5);
1269                         break;                          
1270                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_6:
1271                         beam_test(6);
1272                         break;          
1273                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_7:
1274                         beam_test(7);
1275                         break;                          
1276                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_8:
1277                         beam_test(8);
1278                         break;          
1279 #ifndef MAKE_FS1
1280                 case KEY_DEBUGGED + KEY_SHIFTED + SDLK_9:
1281                         beam_test(9);
1282                         break;                          
1283 #endif
1284
1285                 case KEY_DEBUGGED + KEY_CTRLED + SDLK_1:
1286                         beam_test_new(1);
1287                         break;                          
1288                 case KEY_DEBUGGED + KEY_CTRLED + SDLK_2:
1289                         beam_test_new(2);
1290                         break;          
1291                 case KEY_DEBUGGED + KEY_CTRLED + SDLK_3:
1292                         beam_test_new(3);
1293                         break;
1294                                         
1295                 case KEY_DEBUGGED + SDLK_t: {
1296                         char buf[256];
1297                         event_music_get_info(buf, sizeof(buf));
1298                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, buf);
1299                         break;
1300                 }
1301
1302                 case KEY_DEBUGGED + SDLK_UP:
1303                         debug_change_song(1);
1304                         break;
1305
1306                 case KEY_DEBUGGED + SDLK_DOWN:
1307                         debug_change_song(-1);
1308                         break;
1309
1310                 case SDLK_KP_MINUS: {
1311                         int init_flag = 0;
1312
1313                         if ( key_pressed(SDLK_1) )      {
1314                                 init_flag = 1;
1315                                 HUD_color_red -= 4;
1316                         } 
1317
1318                         if ( key_pressed(SDLK_2) )      {
1319                                 init_flag = 1;
1320                                 HUD_color_green -= 4;
1321                         } 
1322
1323                         if ( key_pressed(SDLK_3) )      {
1324                                 init_flag = 1;
1325                                 HUD_color_blue -= 4;
1326                         } 
1327
1328                         if (init_flag)
1329                                 HUD_init_colors();
1330
1331                         break;
1332                 }
1333                 
1334                 case KEY_DEBUGGED + SDLK_y:
1335                         /*
1336                         // blast a debug lightning bolt in front of the player
1337                         vector start, strike;
1338                         
1339                         vm_vec_scale_add(&start, &Player_obj->pos, &Player_obj->orient.fvec, 300.0f);
1340                         vm_vec_scale_add2(&start, &Player_obj->orient.rvec, -300.0f);
1341                         vm_vec_scale_add(&strike, &start, &Player_obj->orient.rvec, 600.0f);
1342                         nebl_bolt(DEBUG_BOLT, &start, &strike);
1343                         */
1344                         extern int tst;
1345                         tst = 2;
1346                         break;
1347
1348                 case SDLK_KP_PLUS: {
1349                         int init_flag = 0;
1350
1351                         if ( key_pressed(SDLK_1) )      {
1352                                 init_flag = 1;
1353                                 HUD_color_red += 4;
1354                         } 
1355
1356                         if ( key_pressed(SDLK_2) )      {
1357                                 init_flag = 1;
1358                                 HUD_color_green += 4;
1359                         } 
1360
1361                         if ( key_pressed(SDLK_3) )      {
1362                                 init_flag = 1;
1363                                 HUD_color_blue += 4;
1364                         } 
1365
1366                         if (init_flag)
1367                                 HUD_init_colors();
1368
1369                         break;
1370                 }
1371         }       // end switch
1372
1373 }
1374 //#endif
1375
1376 void ppsk_hotkeys(int k)
1377 {
1378
1379 #if !(defined(FS2_DEMO) || defined(FS1_DEMO))
1380
1381         // use k to check for keys that can have Shift,Ctrl,Alt,Del status
1382         int hotkey_set;
1383
1384 #ifndef NDEBUG
1385         k &= ~KEY_DEBUGGED;                     // since hitting F11 will set this bit
1386 #endif
1387
1388         switch (k) {
1389                 case SDLK_F5:
1390                 case SDLK_F6:
1391                 case SDLK_F7:
1392                 case SDLK_F8:
1393                 case SDLK_F9:
1394                 case SDLK_F10:
1395                 case SDLK_F11:
1396                 case SDLK_F12:
1397                         hotkey_set = mission_hotkey_get_set_num(k);
1398                         if ( !(Players[Player_num].flags & PLAYER_FLAGS_MSG_MODE) )
1399                                 hud_target_hotkey_select( hotkey_set );
1400                         else
1401                                 hud_squadmsg_hotkey_select( hotkey_set );
1402
1403                         break;
1404
1405                 case SDLK_F5 + KEY_SHIFTED:
1406                 case SDLK_F6 + KEY_SHIFTED:
1407                 case SDLK_F7 + KEY_SHIFTED:
1408                 case SDLK_F8 + KEY_SHIFTED:
1409                 case SDLK_F9 + KEY_SHIFTED:
1410                 case SDLK_F10 + KEY_SHIFTED:
1411                 case SDLK_F11 + KEY_SHIFTED:
1412                 case SDLK_F12 + KEY_SHIFTED:
1413                         hotkey_set = mission_hotkey_get_set_num(k&(~KEY_SHIFTED));
1414                         mprintf(("Adding to set %d\n", hotkey_set+1));
1415                         if ( Player_ai->target_objnum == -1)
1416                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "No target to add/remove from set %d.", 26), hotkey_set+1);
1417                         else  {
1418                                 hud_target_hotkey_add_remove( hotkey_set, &Objects[Player_ai->target_objnum], HOTKEY_USER_ADDED);
1419                                 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);
1420                         }
1421
1422                         break;
1423
1424                 case SDLK_F5 + KEY_SHIFTED + KEY_ALTED:
1425                 case SDLK_F6 + KEY_SHIFTED + KEY_ALTED:
1426                 case SDLK_F7 + KEY_SHIFTED + KEY_ALTED:
1427                 case SDLK_F8 + KEY_SHIFTED + KEY_ALTED:
1428                 case SDLK_F9 + KEY_SHIFTED + KEY_ALTED:
1429                 case SDLK_F10 + KEY_SHIFTED + KEY_ALTED:
1430                 case SDLK_F11 + KEY_SHIFTED + KEY_ALTED:
1431                 case SDLK_F12 + KEY_SHIFTED + KEY_ALTED:
1432                         hotkey_set = mission_hotkey_get_set_num(k & ~(KEY_SHIFTED+KEY_ALTED));
1433                         hud_target_hotkey_clear( hotkey_set );
1434                         break;
1435
1436                 case KEY_SHIFTED + SDLK_MINUS:
1437                         if ( HUD_color_alpha > HUD_COLOR_ALPHA_USER_MIN )       {
1438                                 HUD_color_alpha--;
1439                                 HUD_init_colors();
1440                         }
1441                         break;
1442 /*              case KEY_SHIFTED + SDLK_u:
1443                         {
1444                         object *debris_create(object *source_obj, int model_num, int submodel_num, vector *pos, vector *exp_center, int hull_flag, float exp_force);
1445
1446                         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);
1447                         if (temp) {
1448                                 temp->hull_strength = 5000.0f;
1449                                 int objnum = temp - Objects;
1450                                 vm_vec_copy_scale(&Objects[objnum].phys_info.vel, &Player_obj->orient.fvec, 30.0f);
1451                         }
1452                         }
1453                         break;
1454 */
1455
1456                 case KEY_SHIFTED + SDLK_EQUALS:
1457                         if ( HUD_color_alpha < HUD_COLOR_ALPHA_USER_MAX ) {
1458                                 HUD_color_alpha++;
1459                                 HUD_init_colors();
1460                         }
1461                         break;
1462         }       // end switch
1463
1464 #endif
1465
1466 }
1467
1468 // check keypress 'key' against a set of valid controls and mark the match in the
1469 // player's button info bitfield.  Also checks joystick controls in the set.
1470 //
1471 // key = scancode (plus modifiers).
1472 // count = total size of the list
1473 // list = list of Control_config struct action indices to check for
1474 void process_set_of_keys(int key, int count, int *list)
1475 {
1476         int i;
1477
1478         for (i=0; i<count; i++)
1479                 if (check_control(list[i], key))
1480                         button_info_set(&Player->bi, list[i]);
1481 }
1482
1483 // routine to process keys used for player ship stuff (*not* ship movement).
1484 void process_player_ship_keys(int k)
1485 {
1486         int masked_k;
1487
1488         masked_k = k & ~KEY_CTRLED;     // take out CTRL modifier only  
1489
1490         // moved this line to beginning of function since hotkeys now encompass
1491         // F5 - F12.  We can return after using F11 as a hotkey.
1492         ppsk_hotkeys(masked_k);
1493         if (key_pressed(KEY_DEBUG_KEY)){
1494                 return;
1495         }
1496
1497         // if we're in supernova mode. do nothing
1498         if(Player->control_mode == PCM_SUPERNOVA){
1499                 return;
1500         }
1501
1502         // pass the key to the squadmate messaging code.  If the messaging code took the key, then return
1503         // from here immediately since we don't want to do further key processing.
1504         if ( hud_squadmsg_read_key(k) )
1505                 return;
1506
1507         if ( Player->control_mode == PCM_NORMAL )       {
1508                 //      The following things are not legal to do while dead.
1509                 if ( !(Game_mode & GM_DEAD) ) {
1510                         process_set_of_keys(masked_k, Normal_key_set_size, Normal_key_set);
1511                 } else  {
1512                         process_set_of_keys(masked_k, Dead_key_set_size, Dead_key_set);
1513                 }
1514         } else {
1515
1516         }
1517 }
1518
1519 // Handler for when player hits 'ESC' during the game
1520 void game_do_end_mission_popup()
1521 {
1522         int     pf_flags, choice;
1523 //      char    savegame_filename[_MAX_FNAME];
1524
1525         // do the multiplayer version of this
1526         if(Game_mode & GM_MULTIPLAYER){
1527                 multi_quit_game(PROMPT_ALL);
1528         } else {
1529
1530                 // single player version....
1531                 // do housekeeping things.
1532                 game_stop_time();
1533                 game_stop_looped_sounds();
1534                 snd_stop_all();
1535
1536                 pf_flags = PF_BODY_BIG | PF_USE_AFFIRMATIVE_ICON | PF_USE_NEGATIVE_ICON;
1537                 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));
1538
1539                 switch (choice) {
1540                 case 1:
1541                         // save the game before quitting if in campaign mode
1542                         // MWA -- 3/26/98 -- no more save/restore!!!!
1543 /*
1544                         if ( Game_mode & GM_CAMPAIGN_MODE ) {
1545                                 memset(savegame_filename, 0, _MAX_FNAME);
1546                                 mission_campaign_savefile_generate_root(savegame_filename);
1547                                 strcat(savegame_filename, NOX("svg"));
1548                                 if ( state_save_all(savegame_filename) ) {
1549                                         Int3(); // could not save this game
1550                                 }
1551                         }
1552 */
1553                         gameseq_post_event(GS_EVENT_END_GAME);
1554                         break;
1555
1556                 case 2:
1557                         gameseq_post_event(GS_EVENT_ENTER_GAME);
1558                         break;
1559
1560                 default:
1561                         break;  // do nothing
1562                 }
1563
1564                 game_start_time();
1565                 game_flush();
1566         }
1567 }
1568
1569 // handle pause keypress
1570 void game_process_pause_key()
1571 {
1572         // special processing for multiplayer
1573         if (Game_mode & GM_MULTIPLAYER) {                                                       
1574                 if(Multi_pause_status){
1575                         multi_pause_request(0);
1576                 } else {
1577                         multi_pause_request(1);
1578                 }               
1579         } else {
1580                 gameseq_post_event( GS_EVENT_PAUSE_GAME );
1581         }
1582 }
1583
1584 #define WITHIN_BBOX()   do { \
1585         float scale = 2.0f; \
1586         polymodel *pm = model_get(s_check->modelnum); \
1587         collided = 0; \
1588         if(pm != NULL){ \
1589                 vector temp = new_obj->pos; \
1590                 vector gpos; \
1591                 vm_vec_sub2(&temp, &hit_check->pos); \
1592                 vm_vec_rotate(&gpos, &temp, &hit_check->orient); \
1593                 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)) { \
1594                         collided = 1; \
1595                 } \
1596         } \
1597 } while(0)
1598
1599 #define MOVE_AWAY_BBOX() do { \
1600         polymodel *pm = model_get(s_check->modelnum); \
1601         if(pm != NULL){ \
1602                 switch((int)frand_range(0.0f, 3.9f)){ \
1603                 case 0: \
1604                         new_obj->pos.xyz.x += 200.0f; \
1605                         break; \
1606                 case 1: \
1607                         new_obj->pos.xyz.x -= 200.0f; \
1608                         break; \
1609                 case 2: \
1610                         new_obj->pos.xyz.y += 200.0f; \
1611                         break; \
1612                 case 3: \
1613                         new_obj->pos.xyz.y -= 200.0f; \
1614                         break; \
1615                 default : \
1616                         new_obj->pos.xyz.z -= 200.0f; \
1617                         break; \
1618                 } \
1619         } \
1620 } while(0)
1621
1622 // process cheat codes
1623 void game_process_cheats(int k)
1624 {
1625         int i;
1626         char *cryptstring;
1627
1628         if ( k == 0 ){
1629                 return;
1630         }
1631
1632         // no cheats in multiplayer, ever
1633         if(Game_mode & GM_MULTIPLAYER){
1634                 Cheats_enabled = 0;
1635                 return;
1636         }
1637
1638         k = key_get_text_input();
1639
1640         if ( (k < 0) || (k > 255) ) {
1641                 return;
1642         }
1643
1644         for (i = 0; i < CHEAT_BUFFER_LEN; i++){
1645                 CheatBuffer[i]=CheatBuffer[i+1];
1646         }
1647
1648         CheatBuffer[CHEATSPOT]=(char)k;
1649
1650         cryptstring=jcrypt(&CheatBuffer[CHEAT_BUFFER_LEN - CRYPT_STRING_LENGTH]);               
1651
1652 #if defined(FS2_DEMO)
1653         if ( !strcmp(Cheat_code_demo, cryptstring) ) {
1654                 HUD_printf(XSTR( "Cheats enabled.", 31));
1655                 Cheats_enabled = 1;
1656                 if (Player->flags & PLAYER_FLAGS_MSG_MODE){
1657                         hud_squadmsg_toggle();
1658                 }
1659         }
1660         
1661 #else
1662
1663 #ifdef MAKE_FS1
1664         // two possible cheat codes for FS1, German and English
1665         if (Lcl_gr) {
1666                 if( !strcmp(Cheat_code_gr, cryptstring) && !(Game_mode & GM_MULTIPLAYER)){
1667                         Cheats_enabled = 1;
1668                         HUD_printf("Cheats enabled");
1669                 }
1670         } else {
1671                 if( !strcmp(Cheat_code, cryptstring) && !(Game_mode & GM_MULTIPLAYER)){
1672                         Cheats_enabled = 1;
1673                         HUD_printf("Cheats enabled");
1674                 }
1675         }
1676 #else
1677         if( !strcmp(Cheat_code, cryptstring) && !(Game_mode & GM_MULTIPLAYER)){
1678                 Cheats_enabled = 1;
1679                 HUD_printf("Cheats enabled");
1680         }
1681 #endif
1682
1683 #ifndef MAKE_FS1
1684         if( !strcmp(Cheat_code_fish, cryptstring) ){
1685                 // only enable in the main hall
1686                 if((gameseq_get_state() == GS_STATE_MAIN_MENU) && (main_hall_id() == 1)){
1687                         extern void fishtank_start();
1688                         fishtank_start();
1689                 }
1690         }
1691         if( !strcmp(Cheat_code_headz, cryptstring) ){
1692                 main_hall_vasudan_funny();
1693         }
1694         if( !strcmp(Cheat_code_skip, cryptstring) && (gameseq_get_state() == GS_STATE_MAIN_MENU)){
1695                 extern void main_hall_campaign_cheat();
1696                 main_hall_campaign_cheat();
1697         }
1698         if( !strcmp(Cheat_code_tooled, cryptstring) && (Game_mode & GM_IN_MISSION)){
1699                 Tool_enabled = 1;
1700                 HUD_printf("Prepare to be taken to school");
1701         }
1702         if( !strcmp(Cheat_code_pirate, cryptstring) && (Game_mode & GM_IN_MISSION) && (Player_obj != NULL)){
1703                 HUD_printf(NOX("Walk the plank"));
1704                 
1705                 for(int idx=0; idx<1; idx++){
1706                         vector add = Player_obj->pos;
1707                         add.xyz.x += frand_range(-700.0f, 700.0f);
1708                         add.xyz.y += frand_range(-700.0f, 700.0f);
1709                         add.xyz.z += frand_range(-700.0f, 700.0f);
1710
1711                         int objnum = ship_create(&vmd_identity_matrix, &add, Num_ship_types - 1);
1712
1713                         if(objnum >= 0){
1714                                 int collided;
1715                                 ship_obj *moveup;
1716                                 object *hit_check;
1717                                 ship *s_check;
1718                                 object *new_obj = &Objects[objnum];
1719
1720                                 // place him
1721                                 // now make sure we're not colliding with anyone                
1722                                 do {
1723                                         collided = 0;
1724                                         moveup = GET_FIRST(&Ship_obj_list);
1725                                         while(moveup!=END_OF_LIST(&Ship_obj_list)){
1726                                                 // don't check the new_obj itself!!
1727                                                 if(moveup->objnum != objnum){
1728                                                         hit_check = &Objects[moveup->objnum];
1729                                                         SDL_assert(hit_check->type == OBJ_SHIP);
1730                                                         SDL_assert(hit_check->instance >= 0);
1731                                                         if((hit_check->type != OBJ_SHIP) || (hit_check->instance < 0)){
1732                                                                 continue;
1733                                                         }
1734                                                         s_check = &Ships[hit_check->instance];
1735                                                         
1736                                                         // just to make sure we don't get any strange magnitude errors
1737                                                         if(vm_vec_same(&hit_check->pos, &Objects[objnum].pos)){
1738                                                                 Objects[objnum].pos.xyz.x += 1.0f;
1739                                                         }
1740                                                         
1741                                                         WITHIN_BBOX();                          
1742                                                         if(collided){                                           
1743                                                                 MOVE_AWAY_BBOX();
1744                                                                 break;
1745                                                         } 
1746                                                         collided = 0;
1747                                                 }
1748                                                 moveup = GET_NEXT(moveup);
1749                                         }
1750                                 } while(collided);                                      
1751                         
1752                                 // warpin
1753                                 shipfx_warpin_start(&Objects[objnum]);
1754
1755                                 // tell him to attack                           
1756                                 // ai_add_ship_goal_player( AIG_TYPE_PLAYER_SHIP, AI_GOAL_CHASE_ANY, SM_ATTACK, NULL, &Ai_info[Ships[Objects[objnum].instance].ai_index] );
1757                         }
1758                 }
1759         }
1760 #endif  // !MAKE_FS1
1761 #endif
1762
1763 #if defined(MAKE_FS1) && !defined(FS1_DEMO)
1764         if ( !strcmp(Cheat_code_movies, cryptstring) ) {
1765                 HUD_printf(XSTR( "All movies available in Tech Room", 32));
1766                 All_movies_enabled = 1;
1767                 if (Player->flags & PLAYER_FLAGS_MSG_MODE){
1768                         hud_squadmsg_toggle();
1769                 }
1770         }
1771 #endif
1772
1773 /*
1774 //#ifdef INTERPLAYQA
1775         if ( !strcmp(Cheat_code_in_game, cryptstring) ) {
1776                 HUD_printf(XSTR( "Cheats enabled.", 31));
1777                 Cheats_enabled = 1;
1778                 if (Player->flags & PLAYER_FLAGS_MSG_MODE){
1779                         hud_squadmsg_toggle();
1780                 }
1781         } else if ( !strcmp(Cheat_code_movies, cryptstring) ) {
1782                 HUD_printf(XSTR( "All movies available in Tech Room", 32));
1783                 All_movies_enabled = 1;
1784                 if (Player->flags & PLAYER_FLAGS_MSG_MODE){
1785                         hud_squadmsg_toggle();
1786                 }
1787         } else if( !strcmp(Cheat_code_pirate, cryptstring) ){
1788                 HUD_printf(NOX("Walk the plank"));
1789                 
1790                 for(int idx=0; idx<1; idx++){
1791                         vector add;
1792                         add.x = frand_range(-1000.0f, 1000.0f);
1793                         add.y = frand_range(-1000.0f, 1000.0f);
1794                         add.z = frand_range(-1000.0f, 1000.0f);
1795
1796                         int objnum = ship_create(&vmd_identity_matrix, &add, Num_ship_types - 1);                       
1797
1798                         if(objnum >= 0){
1799                                 shipfx_warpin_start(&Objects[objnum]);
1800                         }
1801                 }
1802         }
1803 #endif
1804         */
1805 }
1806
1807 void game_process_keys()
1808 {
1809         int k;
1810
1811         button_info_clear(&Player->bi); // clear out the button info struct for the player
1812    do {
1813                 k = game_poll();                
1814         
1815                 // AL 12-10-97: Scan for keys used to leave the dead state      (don't process any)
1816                 // DB 1-13-98 : New popup code will run the game do state, so we must skip 
1817                 //              all key processing in this function, since everything should be run through the popup dialog
1818                 if ( Game_mode & GM_DEAD_BLEW_UP ) {
1819                         continue;
1820                 }
1821
1822                 game_process_cheats( k );
1823
1824                 // mwa -- 4/5/97 Moved these two function calls before the switch statement.  I don't think
1825                 // that this has adverse affect on anything and is acutally desireable because of the
1826                 // ESC key being used to quit any HUD message/input mode that might be currently in use
1827                 process_player_ship_keys(k);
1828
1829 //              #ifndef NDEBUG
1830                 process_debug_keys(k);          //      Note, also processed for cheats.
1831 //              #endif          
1832
1833                 switch (k) {
1834                         case 0:
1835                                 // No key
1836                                 break;
1837                         
1838                         case SDLK_ESCAPE:
1839                                 if ( Player->control_mode != PCM_NORMAL )       {
1840                                         if ( Player->control_mode == PCM_WARPOUT_STAGE1 )       {
1841                                                 gameseq_post_event( GS_EVENT_PLAYER_WARPOUT_STOP );
1842                                         } else {
1843                                                 // too late to abort warp out!
1844                                         }
1845                                 } else {
1846                                         // let the ESC key break out of messaging mode
1847                                         if ( Players[Player_num].flags & PLAYER_FLAGS_MSG_MODE ) {
1848                                                 hud_squadmsg_toggle();
1849                                                 break;
1850                                         }
1851
1852                                         // if in external view or chase view, go back to cockpit view
1853                                         if ( Viewer_mode & (VM_EXTERNAL|VM_CHASE|VM_OTHER_SHIP) ) {
1854                                                 Viewer_mode &= ~(VM_EXTERNAL|VM_CHASE|VM_OTHER_SHIP);
1855                                                 break;
1856                                         }
1857
1858                                         if (!(Game_mode & GM_DEAD_DIED))
1859                                                 game_do_end_mission_popup();
1860
1861                                 }
1862                                 break;
1863
1864                         case SDLK_y:
1865                                 break;
1866
1867                         case SDLK_n:
1868                                 break;                  
1869
1870                         case KEY_ALTED + KEY_SHIFTED+SDLK_j:
1871                                 // treat the current joystick position as the center position
1872                                 joy_set_cen();
1873                                 break;
1874
1875                         case KEY_DEBUGGED | SDLK_PAUSE:
1876                                 gameseq_post_event( GS_EVENT_DEBUG_PAUSE_GAME );
1877                                 break;
1878
1879                         case SDLK_PAUSE:
1880                                 game_process_pause_key();
1881                                 break;
1882                 } // end switch
1883         } while (k);
1884
1885         button_info_do(&Player->bi);    // call functions based on status of button_info bit vectors
1886 }
1887
1888 int button_function_critical(int n, net_player *p = NULL)
1889 {
1890         object *objp;
1891         player *pl;
1892         net_player *npl;
1893         int at_self;    // flag indicating the object is local (for hud messages, etc)
1894
1895         SDL_assert(n >= 0);
1896    
1897         // multiplayer clients should leave critical button bits alone and pass them to the server instead
1898         if ((Game_mode & GM_MULTIPLAYER) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER)) {
1899                 // if this flag is set, we should apply the button itself (came from the server)
1900                 if (!Multi_button_info_ok){
1901                         return 0;
1902                 }
1903         }
1904
1905         // in single player mode make sure we're using the player object and the player himself, otherwise use the object and
1906         // player pertaining to the passed net_player
1907         npl = NULL;
1908         if (p == NULL) {
1909                 objp = Player_obj;
1910                 pl = Player;
1911                 if(Game_mode & GM_MULTIPLAYER){
1912                         npl = Net_player;
1913
1914                         // if we're the server in multiplayer and we're an observer, don't process our own critical button functions
1915                         if((Net_player->flags & NETINFO_FLAG_AM_MASTER) && (Net_player->flags & NETINFO_FLAG_OBSERVER)){
1916                                 return 0;
1917                         }
1918                 }
1919                 at_self = 1;
1920         } else {
1921                 objp = &Objects[p->player->objnum];
1922                 pl = p->player;
1923                 npl = p;
1924                 at_self = 0;
1925
1926                 if ( NETPLAYER_IS_DEAD(npl) || (Ships[Objects[pl->objnum].instance].flags & SF_DYING) )
1927                         return 0;
1928         }
1929         
1930         switch (n) {                            
1931                 // cycle to next secondary weapon
1932                 case CYCLE_SECONDARY:
1933                         if(at_self)
1934                                 control_used(CYCLE_SECONDARY);
1935                         
1936                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
1937                         if (ship_select_next_secondary(objp)) {
1938                                 ship* shipp = &Ships[objp->instance];
1939                                 if ( timestamp_elapsed(shipp->weapons.next_secondary_fire_stamp[shipp->weapons.current_secondary_bank]) ) {
1940                                         shipp->weapons.next_secondary_fire_stamp[shipp->weapons.current_secondary_bank] = timestamp(250);       //      1/4 second delay until can fire
1941                                 }
1942
1943                                 // multiplayer server should maintain bank/link status here
1944                                 if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
1945                                         SDL_assert(npl != NULL);
1946                                         multi_server_update_player_weapons(npl,shipp);                                                                          
1947                                 }                                       
1948                         }                       
1949                         break;
1950
1951                 // cycle number of missiles
1952                 case CYCLE_NUM_MISSLES:
1953                         if(at_self)
1954                                 control_used(CYCLE_NUM_MISSLES);
1955
1956                         if ( objp == Player_obj ) {
1957                                 if ( Player_ship->weapons.num_secondary_banks <= 0 ) {
1958                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "This ship has no secondary weapons", 33));
1959                                         gamesnd_play_iface(SND_GENERAL_FAIL);
1960                                         break;
1961                                 }
1962                         }
1963                                         
1964                         if ( Ships[objp->instance].flags & SF_SECONDARY_DUAL_FIRE ) {           
1965                                 Ships[objp->instance].flags &= ~SF_SECONDARY_DUAL_FIRE;
1966                                 if(at_self) {
1967                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Secondary weapon set to normal fire mode", 34));
1968                                         snd_play( &Snds[SND_SECONDARY_CYCLE] );
1969                                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
1970                                 }
1971                         } else {
1972                                 Ships[objp->instance].flags |= SF_SECONDARY_DUAL_FIRE;
1973                                 if(at_self) {
1974                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Secondary weapon set to dual fire mode", 35));
1975                                         snd_play( &Snds[SND_SECONDARY_CYCLE] );
1976                                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
1977                                 }
1978                         }
1979
1980                         // multiplayer server should maintain bank/link status here
1981                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
1982                                 SDL_assert(npl != NULL);
1983                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
1984                         }                                       
1985                         break;
1986
1987                 // increase weapon recharge rate
1988                 case INCREASE_WEAPON:
1989                         if(at_self)
1990                                 control_used(INCREASE_WEAPON);
1991                         increase_recharge_rate(objp, WEAPONS);
1992
1993                         // multiplayer server should maintain bank/link status here
1994                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
1995                                 SDL_assert(npl != NULL);
1996                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
1997                         }                                       
1998                         break;
1999
2000                 // decrease weapon recharge rate
2001                 case DECREASE_WEAPON:
2002                         if(at_self)
2003                                 control_used(DECREASE_WEAPON);
2004                         decrease_recharge_rate(objp, WEAPONS);
2005
2006                         // multiplayer server should maintain bank/link status here
2007                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2008                                 SDL_assert(npl != NULL);
2009                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2010                         }                                                               
2011                         break;
2012
2013                 // increase shield recharge rate
2014                 case INCREASE_SHIELD:
2015                         if(at_self)
2016                                 control_used(INCREASE_SHIELD);
2017                         increase_recharge_rate(objp, SHIELDS);
2018
2019                         // multiplayer server should maintain bank/link status here
2020                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2021                                 SDL_assert(npl != NULL);
2022                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2023                         }                                                               
2024                         break;
2025
2026                 // decrease shield recharge rate
2027                 case DECREASE_SHIELD:
2028                         if(at_self)
2029                                 control_used(DECREASE_SHIELD);
2030                         decrease_recharge_rate(objp, SHIELDS);
2031
2032                         // multiplayer server should maintain bank/link status here
2033                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2034                                 SDL_assert(npl != NULL);
2035                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2036                         }                                                               
2037                         break;
2038
2039                 // increase energy to engines
2040                 case INCREASE_ENGINE:
2041                         if(at_self)
2042                                 control_used(INCREASE_ENGINE);
2043                         increase_recharge_rate(objp, ENGINES);
2044
2045                         // multiplayer server should maintain bank/link status here
2046                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2047                                 SDL_assert(npl != NULL);
2048                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2049                         }                                                       
2050                         break;
2051
2052                 // decrease energy to engines
2053                 case DECREASE_ENGINE:
2054                         if(at_self)
2055                         control_used(DECREASE_ENGINE);
2056                         decrease_recharge_rate(objp, ENGINES);
2057
2058                         // multiplayer server should maintain bank/link status here
2059                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2060                                 SDL_assert(npl != NULL);
2061                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2062                         }                                                                               
2063                         break;
2064
2065                 // equalize recharge rates
2066                 case ETS_EQUALIZE:
2067                         if (at_self) {
2068                         control_used(ETS_EQUALIZE);
2069                         }
2070
2071                         set_default_recharge_rates(objp);
2072                         snd_play( &Snds[SND_ENERGY_TRANS] );
2073                         // multiplayer server should maintain bank/link status here
2074                         if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2075                                 SDL_assert(npl != NULL);
2076                                 multi_server_update_player_weapons(npl,&Ships[objp->instance]);                                                                         
2077                         }                                                                               
2078                         break;
2079
2080                 // equalize shield energy to all quadrants
2081                 case SHIELD_EQUALIZE:
2082                         if(at_self){
2083                                 control_used(SHIELD_EQUALIZE);
2084                         }
2085                         hud_shield_equalize(objp, pl);
2086                         break;
2087
2088                 // transfer shield energy to front
2089                 case SHIELD_XFER_TOP:
2090                         if(at_self){
2091                         control_used(SHIELD_XFER_TOP);
2092                         }
2093                         hud_augment_shield_quadrant(objp, 1);
2094                         break;
2095
2096                 // transfer shield energy to rear
2097                 case SHIELD_XFER_BOTTOM:
2098                         if(at_self)
2099                                 control_used(SHIELD_XFER_BOTTOM);
2100                         hud_augment_shield_quadrant(objp, 2);
2101                         break;
2102
2103                 // transfer shield energy to left
2104                 case SHIELD_XFER_LEFT:
2105                         if(at_self)
2106                                 control_used(SHIELD_XFER_LEFT);
2107                         hud_augment_shield_quadrant(objp, 3);
2108                         break;
2109                         
2110                 // transfer shield energy to right
2111                 case SHIELD_XFER_RIGHT:
2112                         if(at_self)
2113                                 control_used(SHIELD_XFER_RIGHT);
2114                         hud_augment_shield_quadrant(objp, 0);
2115                         break;
2116
2117                 // transfer energy to shield from weapons
2118                 case XFER_SHIELD:
2119                         if(at_self)
2120                                 control_used(XFER_SHIELD);
2121                         transfer_energy_to_shields(objp);
2122                         break;
2123
2124                 // transfer energy to weapons from shield
2125                 case XFER_LASER:
2126                         if(at_self)
2127                                 control_used(XFER_LASER);
2128                         transfer_energy_to_weapons(objp);
2129                         break;
2130
2131                 // following are not handled here, but we need to bypass the Int3()
2132                 case LAUNCH_COUNTERMEASURE:
2133                 case VIEW_SLEW:
2134                 case VIEW_EXTERNAL:
2135                 case VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK:
2136                 case ONE_THIRD_THROTTLE:
2137                 case TWO_THIRDS_THROTTLE:
2138                 case MINUS_5_PERCENT_THROTTLE:
2139                 case PLUS_5_PERCENT_THROTTLE:
2140                 case ZERO_THROTTLE:
2141                 case MAX_THROTTLE:
2142                         return 0;
2143
2144                 default :
2145                         Int3(); // bad bad bad
2146                         break;
2147         }
2148
2149         return 1;
2150 }
2151
2152 // return !0 if the action is allowed, otherwise return 0
2153 int button_allowed(int n)
2154 {
2155         if ( hud_disabled() ) {
2156                 switch (n) {
2157                 case SHOW_GOALS:
2158                 case END_MISSION:
2159                 case CYCLE_NEXT_PRIMARY:
2160                 case CYCLE_PREV_PRIMARY:
2161                 case CYCLE_SECONDARY:
2162                 case ONE_THIRD_THROTTLE:
2163                 case TWO_THIRDS_THROTTLE:
2164                 case PLUS_5_PERCENT_THROTTLE:
2165                 case MINUS_5_PERCENT_THROTTLE:
2166                 case ZERO_THROTTLE:
2167                 case MAX_THROTTLE:
2168                         return 1;
2169                 default:
2170                         return 0;
2171                 }
2172         }
2173
2174         return 1;
2175 }
2176
2177 // execute function corresponding to action n
2178 // basically, these are actions which don't affect demo playback at all
2179 int button_function_demo_valid(int n)
2180 {
2181         // 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
2182         // playback.
2183         int ret = 0;
2184
2185         //      No keys, not even targeting keys, when player in death roll.  He can press keys after he blows up.
2186         if (Game_mode & GM_DEAD_DIED){
2187                 return 0;
2188         }
2189
2190         // any of these buttons are valid
2191         switch(n){
2192         case VIEW_CHASE:
2193                 control_used(VIEW_CHASE);
2194                 Viewer_mode ^= VM_CHASE;
2195                 if ( Viewer_mode & VM_CHASE ) {
2196                         Viewer_mode &= ~VM_EXTERNAL;
2197                 }
2198                 ret = 1;
2199                 break;
2200
2201         case VIEW_EXTERNAL:
2202                 control_used(VIEW_EXTERNAL);
2203                 Viewer_mode ^= VM_EXTERNAL;
2204                 Viewer_mode &= ~VM_EXTERNAL_CAMERA_LOCKED;      // reset camera lock when leave/entering external view
2205                 if ( Viewer_mode & VM_EXTERNAL ) {
2206                         Viewer_mode &= ~VM_CHASE;
2207                 }
2208                 ret = 1;
2209                 break;
2210
2211         case VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK:
2212                 control_used(VIEW_EXTERNAL_TOGGLE_CAMERA_LOCK);
2213                 if ( Viewer_mode & VM_EXTERNAL ) {
2214                 Viewer_mode ^= VM_EXTERNAL_CAMERA_LOCKED;
2215                 if ( Viewer_mode & VM_EXTERNAL_CAMERA_LOCKED ) {
2216                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "External camera is locked, controls will move ship", 36));
2217                         } else {
2218                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "External camera is free, controls will move the camera, not the ship", 37));
2219                         }
2220                 }
2221                 ret = 1;
2222                 break;
2223
2224         case VIEW_OTHER_SHIP:
2225                 control_used(VIEW_OTHER_SHIP);
2226                 if ( Player_ai->target_objnum < 0 ) {
2227                         snd_play( &Snds[SND_TARGET_FAIL] );
2228                 } else {
2229                         if ( Objects[Player_ai->target_objnum].type != OBJ_SHIP )  {
2230                                 snd_play( &Snds[SND_TARGET_FAIL] );
2231                         } else {
2232                                 Viewer_mode ^= VM_OTHER_SHIP;
2233                         }
2234                 }
2235                 ret = 1;
2236                 break;
2237
2238         case TIME_SLOW_DOWN:
2239                 if ( Game_mode & GM_NORMAL ) {
2240                         if ( Game_time_compression > F1_0) {
2241                                 Game_time_compression /= 2;
2242                         } else {
2243                                 gamesnd_play_error_beep();
2244                         }
2245                 } else {
2246                         gamesnd_play_error_beep();
2247                 }
2248                 ret = 1;
2249                 break;
2250
2251         case TIME_SPEED_UP:
2252                 if ( Game_mode & GM_NORMAL ) {
2253                         if ( Game_time_compression < (F1_0*4) ) {
2254                                 Game_time_compression *= 2;
2255                         } else {
2256                                 gamesnd_play_error_beep();
2257                         }
2258                 } else {
2259                         gamesnd_play_error_beep();
2260                 }
2261                 ret = 1;
2262                 break;
2263         }
2264
2265         // done
2266         return ret;
2267 }
2268
2269 // execute function corresponding to action n (BUTTON_ #define from KeyControl.h)
2270 int button_function(int n)
2271 {
2272         SDL_assert(n >= 0);
2273
2274         if ( !button_allowed(n) ) {
2275                 return 0;
2276         }
2277
2278         //      No keys, not even targeting keys, when player in death roll.  He can press keys after he blows up.
2279         if (Game_mode & GM_DEAD_DIED){
2280                 return 0;
2281         }
2282                 
2283         switch (n) {
2284                 // cycle to next primary weapon
2285                 case CYCLE_NEXT_PRIMARY:                        
2286                         // bogus?
2287                         if((Player_obj == NULL) || (Player_ship == NULL)){
2288                                 break;
2289                         }
2290
2291                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
2292                         if (ship_select_next_primary(Player_obj, CYCLE_PRIMARY_NEXT)) {
2293                                 ship* shipp = Player_ship;
2294                                 shipp->weapons.next_primary_fire_stamp[shipp->weapons.current_primary_bank] = timestamp(250);   //      1/4 second delay until can fire                         
2295                                 // multiplayer server should maintain bank/link status here
2296                                 // if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2297 //                                      SDL_assert(npl != NULL);
2298 //                                      multi_server_update_player_weapons(npl,shipp);                                                                          
2299 //                              }                                       
2300                         }                       
2301                         break;
2302
2303                 // cycle to previous primary weapon
2304                 case CYCLE_PREV_PRIMARY:                        
2305                         // bogus?
2306                         if((Player_obj == NULL) || (Player_ship == NULL)){
2307                                 break;
2308                         }
2309
2310                         hud_gauge_popup_start(HUD_WEAPONS_GAUGE);
2311                         if (ship_select_next_primary(Player_obj, CYCLE_PRIMARY_PREV)) {
2312                                 ship* shipp = Player_ship;
2313                                 shipp->weapons.next_primary_fire_stamp[shipp->weapons.current_primary_bank] = timestamp(250);   //      1/4 second delay until can fire
2314
2315                                 // multiplayer server should maintain bank/link status here
2316                                 // if((Game_mode & GM_MULTIPLAYER) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
2317                                         // SDL_assert(npl != NULL);
2318                                         // multi_server_update_player_weapons(npl,shipp);                                                                               
2319                                 // }                                    
2320                         }                       
2321                         break;
2322
2323                 // cycle to next secondary weapon
2324                 case CYCLE_SECONDARY:
2325                         return button_function_critical(CYCLE_SECONDARY);
2326                         break;
2327
2328                 // cycle number of missiles fired from secondary bank
2329                 case CYCLE_NUM_MISSLES:
2330          return button_function_critical(CYCLE_NUM_MISSLES);                    
2331                         break;
2332
2333                 // undefined in multiplayer for clients right now
2334                 // match target speed
2335                 case MATCH_TARGET_SPEED:                        
2336                         control_used(MATCH_TARGET_SPEED);
2337                         // If player is auto-matching, break auto-match speed
2338                         if ( Player->flags & PLAYER_FLAGS_AUTO_MATCH_SPEED ) {
2339                                 Player->flags &= ~PLAYER_FLAGS_AUTO_MATCH_SPEED;
2340                         }
2341                         player_match_target_speed();                                            
2342                         break;
2343
2344                 // undefined in multiplayer for clients right now
2345                 // toggle auto-match target speed
2346                 case TOGGLE_AUTO_MATCH_TARGET_SPEED:
2347                         // multiplayer observers can't match target speed
2348                         if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER)) ){
2349                                 break;
2350                         }
2351         
2352                         Player->flags ^= PLAYER_FLAGS_AUTO_MATCH_SPEED;                 
2353                         control_used(TOGGLE_AUTO_MATCH_TARGET_SPEED);
2354                         hud_gauge_popup_start(HUD_AUTO_SPEED);
2355                         if ( Players[Player_num].flags & PLAYER_FLAGS_AUTO_MATCH_SPEED ) {
2356                                 snd_play(&Snds[SND_SHIELD_XFER_OK], 1.0f);
2357 //                              HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Auto match target speed activated", -1));
2358                                 if ( !Player->flags & PLAYER_FLAGS_MATCH_TARGET ) {
2359                                         player_match_target_speed();
2360                                 }
2361                         }
2362                         else {
2363 //                              HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Auto match target deactivated", -1));
2364                                 snd_play(&Snds[SND_SHIELD_XFER_OK], 1.0f);
2365                                 player_match_target_speed();
2366                         }                       
2367                         break;
2368
2369                 // target next
2370                 case TARGET_NEXT:
2371                         control_used(TARGET_NEXT);                      
2372                         if ( hud_sensors_ok(Player_ship) ) {
2373                                 hud_target_next();
2374                         }
2375                         break;
2376
2377                 // target previous
2378                 case TARGET_PREV:
2379                         control_used(TARGET_PREV);                      
2380                         if ( hud_sensors_ok(Player_ship) ) {
2381                                 hud_target_prev();                      
2382                         }
2383                         break;
2384
2385                 // target the next hostile target
2386                 case TARGET_NEXT_CLOSEST_HOSTILE:
2387                         control_used(TARGET_NEXT_CLOSEST_HOSTILE);                      
2388                         if (hud_sensors_ok(Player_ship)){
2389                                 hud_target_next_list();
2390                         }                       
2391                         break;
2392
2393                 // target the previous closest hostile 
2394                 case TARGET_PREV_CLOSEST_HOSTILE:
2395                         control_used(TARGET_PREV_CLOSEST_HOSTILE);                      
2396                         if (hud_sensors_ok(Player_ship)){
2397                                 hud_target_next_list(1,0);
2398                         }
2399                         break;
2400
2401                 // toggle auto-targeting
2402                 case TOGGLE_AUTO_TARGETING:
2403                         control_used(TOGGLE_AUTO_TARGETING);
2404                         hud_gauge_popup_start(HUD_AUTO_TARGET);
2405                         Players[Player_num].flags ^= PLAYER_FLAGS_AUTO_TARGETING;
2406                         if ( Players[Player_num].flags & PLAYER_FLAGS_AUTO_TARGETING ) {
2407                                 if (hud_sensors_ok(Player_ship)) {
2408                                         hud_target_closest(opposing_team_mask(Player_ship->team), -1, FALSE, TRUE );
2409                                         snd_play(&Snds[SND_SHIELD_XFER_OK], 1.0f);
2410 //                                      HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Auto targeting activated", -1));
2411                                 } else {
2412                                         Players[Player_num].flags ^= PLAYER_FLAGS_AUTO_TARGETING;
2413                                 }
2414                         } else {
2415                                 snd_play(&Snds[SND_SHIELD_XFER_OK], 1.0f);
2416 //                              HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Auto targeting deactivated", -1));
2417                         }
2418                         break;
2419
2420                 // target the next friendly ship
2421                 case TARGET_NEXT_CLOSEST_FRIENDLY:
2422                         control_used(TARGET_NEXT_CLOSEST_FRIENDLY);                     
2423                         if (hud_sensors_ok(Player_ship)){
2424                                 hud_target_next_list(0);
2425                         }
2426                         break;
2427
2428                 // target the closest friendly ship
2429                 case TARGET_PREV_CLOSEST_FRIENDLY:
2430                         control_used(TARGET_PREV_CLOSEST_FRIENDLY);                     
2431                         if (hud_sensors_ok(Player_ship)) {
2432                                 hud_target_next_list(0,0);
2433                         }
2434                         break;
2435
2436                 // target ship closest to center of reticle
2437                 case TARGET_SHIP_IN_RETICLE:
2438                         control_used(TARGET_SHIP_IN_RETICLE);                   
2439                         if (hud_sensors_ok(Player_ship)){
2440                                 hud_target_in_reticle_new();
2441                         }
2442                         break;
2443
2444                 case TARGET_LAST_TRANMISSION_SENDER:
2445                         control_used(TARGET_LAST_TRANMISSION_SENDER);
2446                         if ( hud_sensors_ok(Player_ship)) {
2447                                 hud_target_last_transmit();
2448                         }
2449                         break;
2450
2451                 // target the closest repair ship
2452                 case TARGET_CLOSEST_REPAIR_SHIP:
2453                         control_used(TARGET_CLOSEST_REPAIR_SHIP);
2454                         // AL: Try to find the closest repair ship coming to repair the player... if no support
2455                         //               ships are coming to rearm the player, just try for the closest repair ship
2456                         if ( hud_target_closest_repair_ship(OBJ_INDEX(Player_obj)) == 0 ) {
2457                                 if ( hud_target_closest_repair_ship() == 0 ) {
2458                                         snd_play(&Snds[SND_TARGET_FAIL]);
2459                                 }
2460                         }
2461                         break;
2462
2463                 // target the closest ship attacking current target
2464                 case TARGET_CLOSEST_SHIP_ATTACKING_TARGET:
2465                         control_used(TARGET_CLOSEST_SHIP_ATTACKING_TARGET);
2466                         if (hud_sensors_ok(Player_ship)){
2467                                 hud_target_closest(opposing_team_mask(Player_ship->team), Player_ai->target_objnum);
2468                         }
2469                         break;
2470
2471                 // stop targeting ship
2472                 case STOP_TARGETING_SHIP:
2473                         control_used(STOP_TARGETING_SHIP);
2474                         hud_cease_targeting();
2475                         break;
2476
2477                 // target closest ship that is attacking player
2478                 case TARGET_CLOSEST_SHIP_ATTACKING_SELF:
2479                         control_used(TARGET_CLOSEST_SHIP_ATTACKING_SELF);
2480                         if (hud_sensors_ok(Player_ship)){
2481                                 hud_target_closest(opposing_team_mask(Player_ship->team), OBJ_INDEX(Player_obj), TRUE, 0, 1);
2482                         }
2483                         break;
2484
2485                 // target your target's target
2486                 case TARGET_TARGETS_TARGET:
2487                         control_used(TARGET_TARGETS_TARGET);
2488                         if (hud_sensors_ok(Player_ship)){
2489                                 hud_target_targets_target();
2490                         }
2491                         break;
2492
2493                 // target ships subsystem in reticle
2494                 case TARGET_SUBOBJECT_IN_RETICLE:
2495                         control_used(TARGET_SUBOBJECT_IN_RETICLE);
2496                         if (hud_sensors_ok(Player_ship)){
2497                                 hud_target_subsystem_in_reticle();
2498                         }
2499                         break;
2500
2501                 case TARGET_PREV_SUBOBJECT:
2502                         control_used(TARGET_PREV_SUBOBJECT);
2503                         if (hud_sensors_ok(Player_ship)){
2504                                 hud_target_prev_subobject();
2505                         }
2506                         break;
2507
2508                 // target next subsystem on current target
2509                 case TARGET_NEXT_SUBOBJECT:
2510                         control_used(TARGET_NEXT_SUBOBJECT);
2511                         if (hud_sensors_ok(Player_ship)){
2512                                 hud_target_next_subobject();
2513                         }
2514                         break;
2515
2516                 // stop targeting subsystems on ship
2517                 case STOP_TARGETING_SUBSYSTEM:
2518                         control_used(STOP_TARGETING_SUBSYSTEM);
2519                         hud_cease_subsystem_targeting();
2520                         break;
2521                         
2522                 case TARGET_NEXT_BOMB:
2523                         control_used(TARGET_NEXT_BOMB);
2524                         hud_target_missile(Player_obj, 1);
2525                         break;
2526
2527                 case TARGET_PREV_BOMB:
2528                         control_used(TARGET_PREV_BOMB);
2529                         hud_target_missile(Player_obj, 0);
2530                         break;
2531
2532                 case TARGET_NEXT_UNINSPECTED_CARGO:
2533                         hud_target_uninspected_object(1);
2534                         break;
2535
2536                 case TARGET_PREV_UNINSPECTED_CARGO:
2537                         hud_target_uninspected_object(0);
2538                         break;
2539
2540                 case TARGET_NEWEST_SHIP:
2541                         hud_target_newest_ship();
2542                         break;
2543
2544                 case TARGET_NEXT_LIVE_TURRET:
2545                         hud_target_live_turret(1);
2546                         break;
2547
2548                 case TARGET_PREV_LIVE_TURRET:
2549                         hud_target_live_turret(0);
2550                         break;
2551
2552                 // wingman message: attack current target
2553                 case ATTACK_MESSAGE:
2554                         control_used(ATTACK_MESSAGE);
2555                         hud_squadmsg_shortcut( ATTACK_TARGET_ITEM );
2556                         break;
2557
2558                 // wingman message: disarm current target
2559                 case DISARM_MESSAGE:
2560                         control_used(DISARM_MESSAGE);
2561                         hud_squadmsg_shortcut( DISARM_TARGET_ITEM );
2562                         break;
2563
2564                 // wingman message: disable current target
2565                 case DISABLE_MESSAGE:
2566                         control_used(DISABLE_MESSAGE);
2567                         hud_squadmsg_shortcut( DISABLE_TARGET_ITEM );
2568                         break;
2569
2570                 // wingman message: disable current target
2571                 case ATTACK_SUBSYSTEM_MESSAGE:
2572                         control_used(ATTACK_SUBSYSTEM_MESSAGE);
2573                         hud_squadmsg_shortcut( DISABLE_SUBSYSTEM_ITEM );
2574                         break;
2575
2576                 // wingman message: capture current target
2577                 case CAPTURE_MESSAGE:
2578                         control_used(CAPTURE_MESSAGE);
2579                         hud_squadmsg_shortcut( CAPTURE_TARGET_ITEM );
2580                         break;
2581
2582                 // wingman message: engage enemy
2583                 case ENGAGE_MESSAGE:
2584                 control_used(ENGAGE_MESSAGE);
2585                         hud_squadmsg_shortcut( ENGAGE_ENEMY_ITEM );
2586                         break;
2587
2588                 // wingman message: form on my wing
2589                 case FORM_MESSAGE:
2590                         control_used(FORM_MESSAGE);
2591                         hud_squadmsg_shortcut( FORMATION_ITEM );
2592                         break;
2593
2594                 // wingman message: protect current target
2595                 case PROTECT_MESSAGE:
2596                         control_used(PROTECT_MESSAGE);
2597                         hud_squadmsg_shortcut( PROTECT_TARGET_ITEM );
2598                         break;
2599
2600                 // wingman message: cover me
2601                 case COVER_MESSAGE:
2602                         control_used(COVER_MESSAGE);
2603                         hud_squadmsg_shortcut( COVER_ME_ITEM );
2604                         break;
2605                 
2606                 // wingman message: warp out
2607                 case WARP_MESSAGE:
2608                         control_used(WARP_MESSAGE);
2609                         hud_squadmsg_shortcut( DEPART_ITEM );
2610                         break;
2611
2612                 case IGNORE_MESSAGE:
2613                         control_used(IGNORE_MESSAGE);
2614                         hud_squadmsg_shortcut( IGNORE_TARGET_ITEM );
2615                         break;
2616
2617                 // rearm message
2618                 case REARM_MESSAGE:
2619                         control_used(REARM_MESSAGE);
2620                         hud_squadmsg_rearm_shortcut();
2621                         break;
2622
2623                 // cycle to next radar range
2624                 case RADAR_RANGE_CYCLE:
2625                         control_used(RADAR_RANGE_CYCLE);
2626                         HUD_config.rp_dist++;
2627                         if ( HUD_config.rp_dist >= RR_MAX_RANGES )
2628                                 HUD_config.rp_dist = 0;
2629
2630                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Radar range set to %s", 38), Radar_range_text(HUD_config.rp_dist));
2631                         break;
2632
2633                 // toggle the squadmate messaging menu
2634                 case SQUADMSG_MENU:
2635                         control_used(SQUADMSG_MENU);
2636                         hud_squadmsg_toggle();                          // leave the details to the messaging code!!!
2637                         break;
2638
2639                 // show the mission goals screen
2640                 case SHOW_GOALS:
2641                         control_used(SHOW_GOALS);
2642                         gameseq_post_event( GS_EVENT_SHOW_GOALS );
2643                         break;
2644
2645                 // end the mission
2646                 case END_MISSION:
2647                         // in multiplayer, all end mission requests should go through the server
2648                         if(Game_mode & GM_MULTIPLAYER){                         
2649                                 multi_handle_end_mission_request();
2650                                 break;
2651                         }
2652
2653                         control_used(END_MISSION);
2654                         
2655                         if (collide_predict_large_ship(Player_obj, 200.0f)) {
2656                                 gamesnd_play_iface(SND_GENERAL_FAIL);
2657                                 HUD_printf(XSTR( "** WARNING ** Collision danger.  Warpout not activated.", 39));
2658                         } else if (ship_get_subsystem_strength( Player_ship, SUBSYSTEM_ENGINE ) < 0.1f) {
2659                                 gamesnd_play_iface(SND_GENERAL_FAIL);
2660                                 HUD_printf(XSTR( "Engine failure.  Cannot engage warp drive.", 40));
2661                         } else {
2662                                 gameseq_post_event( GS_EVENT_PLAYER_WARPOUT_START );
2663                         }                       
2664                         break;
2665
2666                 case ADD_REMOVE_ESCORT:
2667                         if ( Player_ai->target_objnum >= 0 ) {
2668                                 control_used(ADD_REMOVE_ESCORT);
2669                                 hud_add_remove_ship_escort(Player_ai->target_objnum);
2670                         }
2671                         break;
2672
2673                 case ESCORT_CLEAR:
2674                         control_used(ESCORT_CLEAR);
2675                         hud_escort_clear_all();
2676                         break;
2677
2678                 case TARGET_NEXT_ESCORT_SHIP:
2679                         control_used(TARGET_NEXT_ESCORT_SHIP);
2680                         hud_escort_target_next();
2681                         break;
2682
2683                 // increase weapon recharge rate
2684                 case INCREASE_WEAPON:
2685                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2686                         return button_function_critical(INCREASE_WEAPON);
2687                         break;
2688
2689                 // decrease weapon recharge rate
2690                 case DECREASE_WEAPON:
2691                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2692                         return button_function_critical(DECREASE_WEAPON);
2693                         break;
2694
2695                 // increase shield recharge rate
2696                 case INCREASE_SHIELD:
2697                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2698                         return button_function_critical(INCREASE_SHIELD);
2699                         break;
2700
2701                 // decrease shield recharge rate
2702                 case DECREASE_SHIELD:
2703                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2704                         return button_function_critical(DECREASE_SHIELD);
2705                         break;
2706
2707                 // increase energy to engines
2708                 case INCREASE_ENGINE:
2709                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2710                         return button_function_critical(INCREASE_ENGINE);
2711                         break;
2712
2713                 // decrease energy to engines
2714                 case DECREASE_ENGINE:
2715                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2716                         return button_function_critical(DECREASE_ENGINE);
2717                         break;
2718
2719                 case ETS_EQUALIZE:
2720                         hud_gauge_popup_start(HUD_ETS_GAUGE);
2721                         return button_function_critical(ETS_EQUALIZE);
2722                         break;
2723
2724                 // equalize shield energy to all quadrants
2725                 case SHIELD_EQUALIZE:
2726                         return button_function_critical(SHIELD_EQUALIZE);
2727                         break;
2728
2729                 // transfer shield energy to front
2730                 case SHIELD_XFER_TOP:
2731          return button_function_critical(SHIELD_XFER_TOP);
2732                         break;
2733
2734                 // transfer shield energy to rear
2735                 case SHIELD_XFER_BOTTOM:
2736                         return button_function_critical(SHIELD_XFER_BOTTOM);
2737                         break;
2738
2739                 // transfer shield energy to left
2740                 case SHIELD_XFER_LEFT:
2741                         return button_function_critical(SHIELD_XFER_LEFT);
2742                         break;
2743                 
2744                 // transfer shield energy to right
2745                 case SHIELD_XFER_RIGHT:
2746                         return button_function_critical(SHIELD_XFER_RIGHT);
2747                         break;
2748
2749                 // transfer energy to shield from weapons
2750                 case XFER_SHIELD:
2751                         return button_function_critical(XFER_SHIELD);
2752                         break;
2753
2754                 // transfer energy to weapons from shield
2755                 case XFER_LASER:
2756                         return button_function_critical(XFER_LASER);
2757                         break;
2758
2759                 // message all netplayers button
2760                 case MULTI_MESSAGE_ALL:
2761                         multi_msg_key_down(MULTI_MSG_ALL);
2762                         break;
2763
2764                 // message all friendlies button
2765                 case MULTI_MESSAGE_FRIENDLY:
2766                         multi_msg_key_down(MULTI_MSG_FRIENDLY);
2767                         break;
2768
2769                 // message all hostiles button
2770                 case MULTI_MESSAGE_HOSTILE:
2771                         multi_msg_key_down(MULTI_MSG_HOSTILE);
2772                         break;
2773
2774                 // message targeted ship (if player)
2775                 case MULTI_MESSAGE_TARGET:
2776                         multi_msg_key_down(MULTI_MSG_TARGET);
2777                         break;
2778
2779                 // if i'm an observer, zoom to my targeted object
2780                 case MULTI_OBSERVER_ZOOM_TO:
2781                         multi_obs_zoom_to_target();
2782                         break;          
2783
2784                 // toggle between high and low HUD contrast
2785                 case TOGGLE_HUD_CONTRAST:
2786                         gamesnd_play_iface(SND_USER_SELECT);
2787                         hud_toggle_contrast();
2788                         break;
2789
2790                 // toggle network info
2791                 case MULTI_TOGGLE_NETINFO:
2792                         extern int Multi_display_netinfo;
2793                         Multi_display_netinfo = !Multi_display_netinfo;
2794                         break;
2795
2796                 // self destruct (multiplayer only)
2797                 case MULTI_SELF_DESTRUCT:
2798                         if(!(Game_mode & GM_MULTIPLAYER)){
2799                                 break;
2800                         }
2801
2802                         // bogus netplayer
2803                         if((Net_player == NULL) || (Net_player->player == NULL)){
2804                                 break;
2805                         }
2806
2807                         // blow myself up, if I'm the server
2808                         if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
2809                                 if((Net_player->player->objnum >= 0) && (Net_player->player->objnum < MAX_OBJECTS) && 
2810                                         (Objects[Net_player->player->objnum].type == OBJ_SHIP) && (Objects[Net_player->player->objnum].instance >= 0) && (Objects[Net_player->player->objnum].instance < MAX_SHIPS)){
2811
2812                                         ship_self_destruct(&Objects[Net_player->player->objnum]);
2813                                 }
2814                         }
2815                         // otherwise send a packet to the server
2816                         else {
2817                                 send_self_destruct_packet();
2818                         }
2819                         break;
2820
2821                 // following are not handled here, but we need to bypass the Int3()
2822                 case LAUNCH_COUNTERMEASURE:
2823                 case VIEW_SLEW:
2824                 case ONE_THIRD_THROTTLE:
2825                 case TWO_THIRDS_THROTTLE:
2826                 case MINUS_5_PERCENT_THROTTLE:
2827                 case PLUS_5_PERCENT_THROTTLE:
2828                 case ZERO_THROTTLE:
2829                 case MAX_THROTTLE:
2830                         return 0;
2831
2832                 default:
2833                         Int3();
2834                         break;
2835         } // end switch
2836
2837         return 1;
2838 }
2839
2840 // Call functions for when buttons are pressed
2841 void button_info_do(button_info *bi)
2842 {
2843         int i, j;
2844
2845         for (i=0; i<NUM_BUTTON_FIELDS; i++) {
2846                 if ( bi->status[i] == 0 ){
2847                         continue;
2848                 }
2849
2850                 // at least one bit is set in the status integer
2851                 for (j=0; j<32; j++) {
2852
2853                         // check if the bit is set. If button_function returns 1 (implying the action was taken), then unset the bit
2854                         if ( bi->status[i] & (1 << j) ) {
2855                                 // always process buttons which are valid for demo playback
2856                                 if(button_function_demo_valid(32 * i + j)){
2857                                         bi->status[i] &= ~(1 << j);
2858                                 }
2859                                 // other buttons
2860                                 else {
2861                                         // if we're in demo playback, always clear the bits
2862                                         if(Game_mode & GM_DEMO_PLAYBACK){
2863                                                 bi->status[i] &= ~(1 << j);
2864                                         }
2865                                         // otherwise check as normal
2866                                         else if (button_function(32 * i + j)) {
2867                                                 bi->status[i] &= ~(1 << j);                                     
2868                                         }
2869                                 }
2870                         }
2871                 }
2872         }
2873 }
2874
2875
2876 // set the bit for the corresponding action n (BUTTON_ #define from KeyControl.h)
2877 void button_info_set(button_info *bi, int n)
2878 {
2879         int field_num, bit_num;
2880         
2881         field_num = n / 32;
2882         bit_num = n % 32;
2883
2884         bi->status[field_num] |= (1 << bit_num);        
2885 }
2886
2887 // unset the bit for the corresponding action n (BUTTON_ #define from KeyControl.h)
2888 void button_info_unset(button_info *bi, int n)
2889 {
2890         int field_num, bit_num;
2891         
2892         field_num = n / 32;
2893         bit_num = n % 32;
2894
2895         bi->status[field_num] &= ~(1 << bit_num);       
2896 }
2897
2898 int button_info_query(button_info *bi, int n)
2899 {
2900         return bi->status[n / 32] & (1 << (n % 32));
2901 }
2902
2903 // clear out the button_info struct
2904 void button_info_clear(button_info *bi)
2905 {
2906         int i;
2907
2908         for (i=0; i<NUM_BUTTON_FIELDS; i++) {
2909                 bi->status[i] = 0;
2910         }
2911 }
2912
2913 // strip out all noncritical keys from the button info struct
2914 void button_strip_noncritical_keys(button_info *bi)
2915 {
2916         int idx;
2917
2918         // clear out all noncritical keys
2919         for(idx=0;idx<Non_critical_key_set_size;idx++){
2920                 button_info_unset(bi,Non_critical_key_set[idx]);
2921         }
2922 }