]> icculus.org git repositories - taylor/freespace2.git/blob - src/playerman/playercontrol.cpp
added copyright header
[taylor/freespace2.git] / src / playerman / playercontrol.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/Playerman/PlayerControl.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Routines to deal with player ship movement
16  *
17  * $Log$
18  * Revision 1.3  2002/06/09 04:41:25  relnev
19  * added copyright header
20  *
21  * Revision 1.2  2002/05/07 03:16:51  theoddone33
22  * The Great Newline Fix
23  *
24  * Revision 1.1.1.1  2002/05/03 03:28:11  root
25  * Initial import.
26  *
27  * 
28  * 33    10/13/99 3:42p Jefff
29  * fixed unnumbered XSTRs
30  * 
31  * 32    9/06/99 9:43p Jefff
32  * skip mission support
33  * 
34  * 31    8/30/99 10:39a Mikeb
35  * Fixed can't-warpout problem.
36  * 
37  * 30    8/29/99 4:52p Andsager
38  * Don't rotate from engine wash when warping out.
39  * 
40  * 29    8/26/99 8:51p Dave
41  * Gave multiplayer TvT messaging a heavy dose of sanity. Cheat codes.
42  * 
43  * 28    8/03/99 12:06p Dave
44  * Fixed player death messages when a player has killed himself in
45  * multiplayer.
46  * 
47  * 27    7/28/99 1:36p Andsager
48  * Modify cargo1 to include flag CARGO_NO_DEPLETE.  Add sexp
49  * cargo-no-deplete (only for BIG / HUGE).  Modify ship struct to pack
50  * better.
51  * 
52  * 26    7/21/99 8:10p Dave
53  * First run of supernova effect.
54  * 
55  * 25    7/19/99 7:20p Dave
56  * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
57  * pre-rendering.
58  * 
59  * 24    7/08/99 10:53a Dave
60  * New multiplayer interpolation scheme. Not 100% done yet, but still
61  * better than the old way.
62  * 
63  * 23    7/02/99 3:48p Mikeb
64  * Remove semi-bogus assert. Handle properly.
65  * 
66  * 22    6/16/99 10:21a Dave
67  * Added send-message-list sexpression.
68  * 
69  * 21    6/14/99 5:19p Dave
70  * 
71  * 20    6/09/99 3:26p Dave
72  * Changed zing message for beam weapon deaths.
73  * 
74  * 19    6/04/99 4:13p Johne
75  * Made a potentially unsafe normalize safe.
76  * 
77  * 18    5/21/99 5:03p Andsager
78  * Add code to display engine wash death.  Modify ship_kill_packet
79  * 
80  * 17    5/18/99 10:08a Andsager
81  * Modified single maximum range before blown up to also be multi
82  * friendly.
83  * 
84  * 16    5/17/99 6:03p Dave
85  * Added new timing code. Put in code to allow camera speed zooming.
86  * 
87  * 15    5/11/99 10:16p Andsager
88  * First pass on engine wash effect.  Rotation (control input), damage,
89  * shake.  
90  * 
91  * 14    4/23/99 12:01p Johnson
92  * Added SIF_HUGE_SHIP
93  * 
94  * 13    3/30/99 5:40p Dave
95  * Fixed reinforcements for TvT in multiplayer.
96  * 
97  * 12    3/28/99 5:58p Dave
98  * Added early demo code. Make objects move. Nice and framerate
99  * independant, but not much else. Don't use yet unless you're me :)
100  * 
101  * 11    3/10/99 6:50p Dave
102  * Changed the way we buffer packets for all clients. Optimized turret
103  * fired packets. Did some weapon firing optimizations.
104  * 
105  * 10    2/26/99 6:01p Andsager
106  * Add sexp has-been-tagged-delay and cap-subsys-cargo-known-delay
107  * 
108  * 9     2/21/99 6:02p Dave
109  * Fixed standalone WSS packets. 
110  * 
111  * 8     1/30/99 1:29a Dave
112  * Fixed nebula thumbnail problem. Full support for 1024x768 choose pilot
113  * screen.  Fixed beam weapon death messages.
114  * 
115  * 7     1/14/99 6:06p Dave
116  * 100% full squad logo support for single player and multiplayer.
117  * 
118  * 6     1/12/99 5:45p Dave
119  * Moved weapon pipeline in multiplayer to almost exclusively client side.
120  * Very good results. Bandwidth goes down, playability goes up for crappy
121  * connections. Fixed object update problem for ship subsystems.
122  * 
123  * 5     12/10/98 10:54a Dave
124  * Fixed problem where a mission ended in multiplayer and a player was
125  * dead. There was an Int3() when getting player eye-point.
126  * 
127  * 4     11/12/98 12:13a Dave
128  * Tidied code up for multiplayer test. Put in network support for flak
129  * guns.
130  * 
131  * 3     10/13/98 9:29a Dave
132  * Started neatening up freespace.h. Many variables renamed and
133  * reorganized. Added AlphaColors.[h,cpp]
134  * 
135  * 2     10/07/98 10:53a Dave
136  * Initial checkin.
137  * 
138  * 1     10/07/98 10:50a Dave
139  * 
140  * 219   6/30/98 2:16p Dave
141  * Revised object update system. Removed updates for all weapons. Put
142  * button info back into control info packet.
143  * 
144  * 218   6/09/98 5:17p Lawrance
145  * French/German localization
146  * 
147  * 217   6/09/98 10:31a Hoffoss
148  * Created index numbers for all xstr() references.  Any new xstr() stuff
149  * added from here on out should be added to the end if the list.  The
150  * current list count can be found in FreeSpace.cpp (search for
151  * XSTR_SIZE).
152  * 
153  * 216   6/01/98 11:43a John
154  * JAS & MK:  Classified all strings for localization.
155  * 
156  * 215   5/22/98 11:27a Hoffoss
157  * Fixed the bank when pressed bug where you continue to turn while using
158  * it.
159  * 
160  * 214   5/19/98 8:19p Lawrance
161  * Make mouse invert work
162  * 
163  * 213   5/19/98 12:19p Mike
164  * Cheat codes!
165  * 
166  * 212   5/18/98 2:50p Lawrance
167  * Change text for when warp drive is inoperable
168  * 
169  * 211   5/18/98 1:58a Mike
170  * Make Phoenix not be fired at fighters (but yes bombers).
171  * Improve positioning of ships in guard mode.
172  * Make turrets on player ship not fire near end of support ship docking.
173  * 
174  * 210   5/17/98 1:43a Dave
175  * Eradicated chatbox problems. Remove speed match for observers. Put in
176  * help screens for PXO. Fix messaging and end mission privelges. Fixed
177  * team select screen bugs. Misc UI fixes.
178  * 
179  * 209   5/15/98 2:41p Hoffoss
180  * Made mouse default to off (for flying ship) and fixed some new pilot
181  * init bugs.
182  * 
183  * 208   5/15/98 1:14p Hoffoss
184  * Added a deadzone to ends of abs throttle range.
185  * 
186  * 207   5/14/98 5:32p Hoffoss
187  * Improved axis binding code some more.
188  * 
189  * 206   5/13/98 11:55a Frank
190  * Fixed bug with axis 2 and 3 not working correctly.
191  * 
192  * 205   5/13/98 1:17a Hoffoss
193  * Added joystick axes configurability.
194  * 
195  * 204   5/11/98 5:29p Hoffoss
196  * Added mouse button mapped to joystick button support.
197  * 
198  * 203   5/11/98 12:01p Hoffoss
199  * Changed mouse sensitivity code to approx. double top limit while
200  * keeping low limit the same.
201  * 
202  * 202   5/09/98 4:52p Lawrance
203  * Implement padlock view (up/rear/left/right)
204  * 
205  * 201   5/08/98 5:35p Lawrance
206  * only allow cargo inspection if sensors are ok
207  * 
208  * 200   5/08/98 5:31p Hoffoss
209  * Isolated the joystick force feedback code more from dependence on other
210  * libraries.
211  * 
212  * 199   5/07/98 6:58p Hoffoss
213  * Made changes to mouse code to fix a number of problems.
214  * 
215  * 198   5/06/98 12:02a Hoffoss
216  * Fixed throttle problems with joysticks.
217  * 
218  * 197   5/05/98 8:38p Hoffoss
219  * Added sensitivity adjustment to options menu and made it save to pilot
220  * file.
221  * 
222  * 196   5/05/98 1:33p Hoffoss
223  * Changed mouse formula around a little.
224  * 
225  * 195   5/04/98 11:08p Hoffoss
226  * Expanded on Force Feedback code, and moved it all into Joy_ff.cpp.
227  * Updated references everywhere to it.
228  * 
229  * 194   5/01/98 5:45p Hoffoss
230  * Made further improvements to the mouse code.
231  * 
232  * 193   5/01/98 4:24p Lawrance
233  * Play error sound if player tries to engage afterburner when none are on
234  * ship
235  * 
236  * 192   5/01/98 1:14p Hoffoss
237  * Changed mouse usage so directInput is only used for release version.
238  * 
239  * 191   4/30/98 9:12p Hoffoss
240  * Fixed a questionable precidence issue to be known.
241  * 
242  * 190   4/30/98 5:40p Hoffoss
243  * Added mouse as a supported control to fly the ship.
244  * 
245  * 189   4/29/98 1:44p Lawrance
246  * Fix bug that would cause primaries to be linked when returning from a
247  * menu in-game (if primaries were linked at mission start)
248  * 
249  * 188   4/25/98 4:36p Lawrance
250  * init auto_advance field when pilot is created
251  * 
252  * 187   4/25/98 3:49p Lawrance
253  * Save briefing auto-advance pref
254  * 
255  * 186   4/18/98 9:52p Mike
256  * Don't restore player flags from save file in training mission, as it
257  * enables auto-target to persist.
258  * 
259  * 185   4/17/98 11:16a Allender
260  * use short callsign when showing player name in cargo area of target
261  * info box
262  * 
263  * 184   4/09/98 12:32a Lawrance
264  * Fix bugs related to multiple screams from same ship, builtin messages
265  * playing after screams, or praising while severly damaged.
266  * 
267  * 183   4/08/98 7:11p Dave
268  * Fix auto-targeting and auto-matching reset after player respawn.
269  * Removed incorrect call to furball update if in a non-furball game.
270  * 
271  * 182   4/08/98 11:11a Hoffoss
272  * Fixed some bugs that showed up due to fixing other bugs the other day
273  * with controls.
274  * 
275  * 181   4/08/98 10:46a Lawrance
276  * fix uninitialized data warning
277  * 
278  * 180   4/06/98 11:00a Hoffoss
279  * Fixed bank when pressed key to also be usable with keyboard yaw
280  * controls.
281  * 
282  * 179   4/05/98 7:43p Lawrance
283  * fix up saving/restoring of link status and auto-target/match-speed.
284  * 
285  * 178   4/01/98 7:42p Lawrance
286  * Enable auto-targeting by default when a new pilot is created.
287  * 
288  * 177   4/01/98 1:31p Allender
289  * don't play all alone message in multiplayer
290  * 
291  * 176   3/31/98 11:47p Lawrance
292  * 
293  * 175   3/31/98 5:18p John
294  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
295  * bunch of debug stuff out of player file.  Made model code be able to
296  * unload models and malloc out only however many models are needed.
297  *  
298  * 
299  * 174   3/30/98 12:19a Lawrance
300  * Make scan time a property of a ship
301  * 
302  * 173   3/24/98 4:25p Lawrance
303  * Make finding out if player killed self easier and more reliable
304  * 
305  * 172   3/23/98 12:28p Hoffoss
306  * Fixed +/-5% throttle controls to actually do 5%, rather than whatever
307  * the heck it was doing before.
308  * 
309  * 171   3/21/98 3:35p Lawrance
310  * Fix joystick turning in external view
311  * 
312  * 170   3/19/98 5:35p Lawrance
313  * Correctly inform player if killed by ship explosion.
314  * 
315  * 169   3/18/98 6:04p Lawrance
316  * change text for warp destroyed message
317  * 
318  * 168   3/18/98 12:03p John
319  * Marked all the new strings as externalized or not.
320  * 
321  * 167   3/16/98 5:54p Lawrance
322  * Play cargo scanning sound
323  * 
324  * 166   3/14/98 4:58p Lawrance
325  * surpress HUD messages for auto-match speed
326  * 
327  * 165   3/11/98 12:11p Mike
328  * Increase max speed to enable warpout even if ship has little energy
329  * directed towards engines.
330  * 
331  * 164   3/11/98 11:19a Mike
332  * Quick fix since I completely broke warping out.
333  * 
334  * 163   3/11/98 9:08a Mike
335  * Intermediate checkin to resolve link errors.  Working on forcing
336  * player's engine energy high enough to enable warpout.
337  * 
338  * 162   3/10/98 5:08p Allender
339  * fixed up multiplayer death messages (I hope).  changes in object update
340  * packets
341  * 
342  * 161   3/09/98 4:30p Allender
343  * multiplayer secondary weapon changes.  red-alert and cargo-known-delay
344  * sexpressions.  Add time cargo revealed to ship structure
345  * 
346  * 160   3/07/98 3:49p Lawrance
347  * store killer species in player struct
348  * 
349  * 159   3/05/98 10:16p Lawrance
350  * Add 'save_flags' to player struct to save/restore certain player flags
351  * cleanly.
352  * 
353  * 158   2/28/98 7:03p Lawrance
354  * get slew working in any view
355  * 
356  * 157   2/26/98 12:33a Lawrance
357  * Added back slew mode,  lots of changes to external and chase views.
358  * 
359  * 156   2/23/98 6:54p Lawrance
360  * Make interface to real-time voice more generic and useful.
361  * 
362  * 155   2/22/98 4:30p John
363  * More string externalization classification
364  * 
365  * 154   2/22/98 3:56p Lawrance
366  * Make external view a toggle like chase view.  Allow extenal view to
367  * override chase view.
368  * 
369  * 153   2/22/98 2:48p John
370  * More String Externalization Classification
371  * 
372  * 152   2/20/98 8:30p Lawrance
373  * Play 'all alone' message when player is last ship left and primary
374  * objective is incomplete.
375  * 
376  * 151   2/19/98 5:30p Lawrance
377  * Allow player turrets to fire automatically.
378  * 
379  * 150   2/16/98 7:33p Lawrance
380  * add function to determine range of current secondary weapon
381  * 
382  * 149   2/15/98 9:51p Sandeep
383  * AL: Check for 'Player 1' as a ship name when displaying the death
384  * message
385  * 
386  * 148   2/14/98 4:41p Lawrance
387  * Add some new fields to keep track of how the player died.
388  * 
389  * 147   2/11/98 9:53a John
390  * Put in a real fix to the bug with not resetting joystick time between
391  * missions
392  * 
393  * 146   2/10/98 9:29p Mike
394  * Hack-fix bug with joystick reading not being allowed in 2nd playthrough
395  * of a mission until time exceeds duration of previous mission.
396  * 
397  * 145   2/10/98 1:15p John
398  * Made the joystick only read at 10Hz max
399  * 
400  * 144   2/06/98 4:31p Lawrance
401  * revamp radar code, allow dim radar dots
402  * 
403  * 143   2/06/98 3:48p Allender
404  * added code to clear out control info for multiplayer clients so they
405  * don't accidentally start firing all the time
406  * 
407  * 142   2/06/98 12:13a Lawrance
408  * Don't play throttle sounds when using analog throttle.
409  * 
410  * 141   2/05/98 10:41a Lawrance
411  * Scale frequency of 'good kill' messages by skill level.
412  * 
413  * 140   2/02/98 4:24p Lawrance
414  * Only inspect cargo and transport in single player.
415  * 
416  * 139   2/02/98 4:12p Allender
417  * fixed bug I introduced with multiplayer callsigns displaying in cargo
418  * spot
419  * 
420  * $NoKeywords: $
421 */
422
423 #include "pstypes.h"
424 #include "physics.h"
425 #include "key.h"
426 #include "joy.h"
427 #include "joy_ff.h"
428 #include "mouse.h"
429 #include "fix.h"
430 #include "floating.h"
431 #include "object.h"
432 #include "player.h"
433 #include "hud.h"
434 #include "hudtarget.h"
435 #include "hudtargetbox.h"
436 #include "ship.h"
437 #include "freespace.h"
438 #include "controlsconfig.h"
439 #include "sound.h"
440 #include "gamesnd.h"
441 #include "missionshipchoice.h"
442 #include "afterburner.h"
443 #include "timer.h"
444 #include "gamesequence.h"
445 #include "missionmessage.h"
446 #include "multiutil.h"
447 #include "linklist.h"
448 #include "missiongoals.h"
449 #include "hudsquadmsg.h"
450 #include "multi_obj.h"
451 #include "observer.h"
452
453 ////////////////////////////////////////////////////////////
454 // Global object and other interesting player type things
455 ////////////////////////////////////////////////////////////
456 player  Players[MAX_PLAYERS];
457
458 int             Player_num;
459 player  *Player = NULL;
460
461 physics_info Descent_physics;                   // used when we want to control the player like the descent ship
462
463 ////////////////////////////////////////////////////////////
464 // Module data
465 ////////////////////////////////////////////////////////////
466 static int Player_all_alone_msg_inited=0;       // flag used for initalizing a player-specific voice msg
467
468 #ifndef NDEBUG
469         int Show_killer_weapon = 0;
470         DCF_BOOL( show_killer_weapon, Show_killer_weapon );
471 #endif
472
473 void playercontrol_read_stick(int *axis, float frame_time);
474 void player_set_padlock_state();
475
476 //      Slew angles chase towards zero like they're on a spring.
477 //      When furthest away, move fastest.
478 //      Minimum speed set so that doesn't take too long.
479 //      When gets close, clamps to zero.
480 void chase_angles_to_zero(angles *ap)
481 {
482         float   k1, k2;
483         float   sk;
484
485         //      Make sure we actually need to do all this math.
486         if ((ap->p == 0.0f) && (ap->h == 0.0f))
487                 return;
488
489         //      This is what we'll scale each value by.
490         sk = 1.0f - 2*flFrametime;
491
492         //      These are the amounts that will be subtracted from pitch and heading.
493         //      They are only needed to make sure we aren't moving too slowly.
494         k1 = fl_abs(ap->p * (1.0f - sk));
495         k2 = fl_abs(ap->h * (1.0f - sk));
496
497         //      See if the larger dimension of movement is too small.
498         // If so, boost amount of movement in both dimensions.
499         if (k1 >= k2) {
500                 if (k1 < flFrametime)
501                         sk = 1.0f - (1.0f - sk) * flFrametime/k1;
502         } else if (k2 > k1) {
503                 if (k2 < flFrametime)
504                         sk = 1.0f - (1.0f - sk) * flFrametime/k2;
505         }
506
507         //      It's possible we made the scale factor negative above.
508         if (sk < 0.0f)
509                 sk = 0.0f;
510
511         ap->p *= sk;
512         ap->h *= sk;
513
514         //      If we're very close, put ourselves at goal.
515         if ((fl_abs(ap->p) < 0.005f) && (fl_abs(ap->h) < 0.005f)) {
516                 ap->p = 0.0f;
517                 ap->h = 0.0f;
518         }
519
520         //      Update Viewer_mode based on whether we're looking dead ahead.   
521         if ((ap->p == 0.0f) && (ap->b == 0.0f) && (ap->h == 0.0f))
522                 Viewer_mode &= ~VM_SLEWED;
523         else
524                 Viewer_mode |= VM_SLEWED;
525
526 }
527
528 angles  Viewer_slew_angles_delta;
529 angles  Viewer_external_angles_delta;
530
531 void view_modify(angles *ma, angles *da, float minv, float maxv, int slew, float frame_time)
532 {
533         int axis[JOY_NUM_AXES];
534         float   t;
535
536         if ( (!slew) && (Viewer_mode & VM_EXTERNAL) && (Viewer_mode & VM_EXTERNAL_CAMERA_LOCKED) ) {
537                 return;
538         }
539
540         if ( Viewer_mode & VM_EXTERNAL ) {
541                 t = (check_control_timef(YAW_LEFT) - check_control_timef(YAW_RIGHT)) / 16.0f;
542         } else {
543                 t = (check_control_timef(YAW_RIGHT) - check_control_timef(YAW_LEFT)) / 16.0f;
544         }
545
546         if (t != 0.0f)
547                 da->h += t;
548         else
549                 da->h = 0.0f;
550
551         t = (check_control_timef(PITCH_FORWARD) - check_control_timef(PITCH_BACK)) / 16.0f;
552         if (t != 0.0f)
553                 da->p += t;
554         else
555                 da->p = 0.0f;
556                         
557         da->b = 0.0f;
558
559         playercontrol_read_stick(axis, frame_time);
560
561         if ( Viewer_mode & VM_EXTERNAL ) {
562                 // check the heading on the x axis
563                 da->h -= f2fl( axis[0] );
564
565         } else {
566                 // check the heading on the x axis
567                 da->h += f2fl( axis[0] );
568         }
569
570         // check the pitch on the y axis
571         da->p -= f2fl( axis[1] );
572
573         if (da->h > 1.0f)
574                 da->h = 1.0f;
575         else if (da->h < -1.0f)
576                 da->h = -1.0f;
577
578         if (da->p > 1.0f)
579                 da->p = 1.0f;
580         else if (da->p < -1.0f)
581                 da->p = -1.0f;
582
583         ma->p += da->p * flFrametime;
584         ma->b += da->b * flFrametime;
585         ma->h += da->h * flFrametime;
586
587         if (ma->p > maxv)
588                 ma->p = maxv;
589         else if (ma->p < minv)
590                 ma->p = minv;
591
592         if (ma->h > maxv)
593                 ma->h = maxv;
594         else if (ma->h < minv)
595                 ma->h = minv;
596 }
597
598 //      When PAD0 is pressed, keypad controls viewer direction slewing.
599 void do_view_slew(float frame_time)
600 {
601         view_modify(&Viewer_slew_angles, &Viewer_slew_angles_delta, -PI/3, PI/3, 1, frame_time);
602
603         if ((Viewer_slew_angles.p == 0.0f) && (Viewer_slew_angles.b == 0.0f) && (Viewer_slew_angles.h == 0.0f))
604                 Viewer_mode &= ~VM_SLEWED;
605         else
606                 Viewer_mode |= VM_SLEWED;
607 }
608
609 void do_view_chase(float frame_time)
610 {
611         float t;
612
613         //      Process centering key.
614         if (check_control_timef(VIEW_CENTER)) {
615                 Viewer_chase_info.distance = 0.0f;
616         }
617         
618         t = check_control_timef(VIEW_DIST_INCREASE) - check_control_timef(VIEW_DIST_DECREASE);
619         Viewer_chase_info.distance += t*4;
620         if (Viewer_chase_info.distance < 0.0f)
621                 Viewer_chase_info.distance = 0.0f;
622 }
623
624 float camera_zoom_scale = 1.0f;
625
626 DCF(camera_speed, "")
627 {
628         dc_get_arg(ARG_FLOAT);
629         camera_zoom_scale = Dc_arg_float;
630 }
631
632 void do_view_external(float frame_time)
633 {
634         float   t;
635
636         view_modify(&Viewer_external_info.angles, &Viewer_external_angles_delta, -2*PI, 2*PI, 0, frame_time);
637
638         //      Process centering key.
639         if (check_control_timef(VIEW_CENTER)) {
640                 Viewer_external_info.angles.p = 0.0f;
641                 Viewer_external_info.angles.h = 0.0f;
642                 Viewer_external_info.distance = 0.0f;
643         }
644         
645         t = check_control_timef(VIEW_DIST_INCREASE) - check_control_timef(VIEW_DIST_DECREASE);
646         Viewer_external_info.distance += t*4*camera_zoom_scale;
647         if (Viewer_external_info.distance < 0.0f){
648                 Viewer_external_info.distance = 0.0f;
649         }
650
651         //      Do over-the-top correction.
652
653         if (Viewer_external_info.angles.p > PI)
654                 Viewer_external_info.angles.p = -2*PI + Viewer_external_info.angles.p;
655         else if (Viewer_external_info.angles.p < -PI)
656                 Viewer_external_info.angles.p = 2*PI + Viewer_external_info.angles.p;
657
658         if (Viewer_external_info.angles.h > PI)
659                 Viewer_external_info.angles.h = -2*PI + Viewer_external_info.angles.h;
660         else if (Viewer_external_info.angles.h < -PI)
661                 Viewer_external_info.angles.h = 2*PI + Viewer_external_info.angles.h;
662 }
663
664 // separate out the reading of thrust keys, so we can call this from external
665 // view as well as from normal view
666 void do_thrust_keys(control_info *ci)
667 {
668         ci->forward = check_control_timef(FORWARD_THRUST) - check_control_timef(REVERSE_THRUST);
669 }
670
671 // called by single and multiplayer modes to reset information inside of control info structure
672 void player_control_reset_ci( control_info *ci )
673 {
674         float t1, t2, oldspeed;
675
676         t1 = ci->heading;
677         t2 = ci->pitch;
678         oldspeed = ci->forward_cruise_percent;
679         memset( ci, 0, sizeof(control_info) );
680         ci->heading = t1;
681         ci->pitch = t2;
682         ci->forward_cruise_percent = oldspeed;
683 }
684
685 // Read the 4 joystick axis.  This is its own function
686 // because we only want to read it at a certain rate,
687 // since it takes time.
688
689 static int Joystick_saved_reading[JOY_NUM_AXES];
690 static int Joystick_last_reading = -1;
691
692 void playercontrol_read_stick(int *axis, float frame_time)
693 {
694         int i;
695
696 #ifndef NDEBUG
697         // Make sure things get reset properly between missions.
698         if ( (Joystick_last_reading != -1) && (timestamp_until(Joystick_last_reading) > 1000) ) {
699                 Int3();         // Get John!  John, the joystick last reading didn't get reset btwn levels.
700                 Joystick_last_reading = -1;
701         }
702 #endif
703
704         if ( (Joystick_last_reading == -1)  || timestamp_elapsed(Joystick_last_reading) ) {
705                 // Read the stick
706                 control_get_axes_readings(&Joystick_saved_reading[0], &Joystick_saved_reading[1], &Joystick_saved_reading[2], &Joystick_saved_reading[3], &Joystick_saved_reading[4]);
707                 Joystick_last_reading = timestamp( 1000/10 );   // Read 10x per second, like we did in Descent.
708         }
709
710         for (i=0; i<NUM_JOY_AXIS_ACTIONS; i++) {
711                 axis[i] = Joystick_saved_reading[i];
712         }
713
714         if (Use_mouse_to_fly) {
715                 int dx, dy, dz;
716                 float factor;
717
718 //              factor = (float) Mouse_sensitivity + 2.5f;
719 //              factor = factor * factor / frame_time / 1.2f;
720                 factor = (float) Mouse_sensitivity + 1.77f;
721                 factor = factor * factor / frame_time / 0.6f;
722
723                 mouse_get_delta(&dx, &dy, &dz);
724
725                 if ( Invert_axis[0] ) {
726                         dx = -dx;
727                 }
728
729                 if ( Invert_axis[1] ) {
730                         dy = -dy;
731                 }
732
733                 if ( Invert_axis[3] ) {
734                         dz = -dz;
735                 }
736
737                 axis[0] += (int) ((float) dx * factor);
738                 axis[1] += (int) ((float) dy * factor);
739                 axis[3] += (int) ((float) dz * factor);
740         }
741 }
742
743 void read_keyboard_controls( control_info * ci, float frame_time, physics_info *pi )
744 {
745         float kh=0.0f, scaled, newspeed, delta, oldspeed;
746         int axis[JOY_NUM_AXES], ignore_pitch, slew_active=0;
747         static int afterburner_last = 0;
748         static float analog_throttle_last = 9e9f;
749         static int override_analog_throttle = 0; 
750         int ok_to_read_ci_pitch_yaw=1;
751
752         oldspeed = ci->forward_cruise_percent;
753         player_control_reset_ci( ci );
754
755         if ( check_control(VIEW_SLEW) ) {
756                 do_view_slew(frame_time);
757                 slew_active=1;
758         }
759
760         if ( Viewer_mode & VM_EXTERNAL ) {
761                 control_used(VIEW_EXTERNAL);
762                 if ( !(Viewer_mode & VM_EXTERNAL_CAMERA_LOCKED) ) {
763                         ok_to_read_ci_pitch_yaw=0;
764                         slew_active=0;
765                 }
766
767                 do_view_external(frame_time);
768                 do_thrust_keys(ci);
769         }
770
771         if ( !slew_active ) {
772                 if ( Viewer_mode & VM_CHASE ) {
773                         do_view_chase(frame_time);
774                 }
775         }
776         
777         if ( ok_to_read_ci_pitch_yaw ) {
778                 // From keyboard...
779                 if ( check_control(BANK_WHEN_PRESSED) ) {
780                         ci->bank = check_control_timef(BANK_LEFT) + check_control_timef(YAW_LEFT) - check_control_timef(YAW_RIGHT) - check_control_timef(BANK_RIGHT);
781                         ci->heading = 0.0f;
782
783                 } else {
784                         kh = (check_control_timef(YAW_RIGHT) - check_control_timef(YAW_LEFT)) / 8.0f;
785                         if (kh == 0.0f) {
786                                 ci->heading = 0.0f;
787
788                         } else if (kh > 0.0f) {
789                                 if (ci->heading < 0.0f)
790                                         ci->heading = 0.0f;
791
792                         } else {  // kh < 0
793                                 if (ci->heading > 0.0f)
794                                         ci->heading = 0.0f;
795                         }
796
797                         ci->bank = check_control_timef(BANK_LEFT) - check_control_timef(BANK_RIGHT);
798                 }
799
800                 ci->heading += kh;
801
802                 kh = (check_control_timef(PITCH_FORWARD) - check_control_timef(PITCH_BACK)) / 8.0f;
803                 if (kh == 0.0f) {
804                         ci->pitch = 0.0f;
805
806                 } else if (kh > 0.0f) {
807                         if (ci->pitch < 0.0f)
808                                 ci->pitch = 0.0f;
809
810                 } else {  // kh < 0
811                         if (ci->pitch > 0.0f)
812                                 ci->pitch = 0.0f;
813                 }
814
815                 ci->pitch += kh;
816
817                 ci->sideways = (key_down_timef(KEY_PAD3) - key_down_timef(KEY_PAD1));
818                 ci->vertical = (key_down_timef(KEY_PADPLUS) - key_down_timef(KEY_PADENTER));
819
820                 do_thrust_keys(ci);
821         }
822
823         if ( !slew_active ) {
824                 chase_angles_to_zero(&Viewer_slew_angles);
825         }
826
827         player_set_padlock_state();
828
829         if (!(Game_mode & GM_DEAD)) {
830                 if ( button_info_query(&Player->bi, ONE_THIRD_THROTTLE) ) {
831                         control_used(ONE_THIRD_THROTTLE);
832                         player_clear_speed_matching();
833                         if ( Player->ci.forward_cruise_percent < 33.3f ) {
834                                 snd_play( &Snds[SND_THROTTLE_UP], 0.0f );
835
836                         } else if ( Player->ci.forward_cruise_percent > 33.3f ) {
837                                 snd_play( &Snds[SND_THROTTLE_DOWN], 0.0f );
838                         }
839
840                         Player->ci.forward_cruise_percent = 33.3f;
841                         override_analog_throttle = 1;
842                 }
843
844                 if ( button_info_query(&Player->bi, TWO_THIRDS_THROTTLE) ) {
845                         control_used(TWO_THIRDS_THROTTLE);
846                         player_clear_speed_matching();
847                         if ( Player->ci.forward_cruise_percent < 66.6f ) {
848                                 snd_play( &Snds[SND_THROTTLE_UP], 0.0f );
849
850                         } else if (Player->ci.forward_cruise_percent > 66.6f) {
851                                 snd_play( &Snds[SND_THROTTLE_DOWN], 0.0f );
852                         }
853
854                         Player->ci.forward_cruise_percent = 66.6f;
855                         override_analog_throttle = 1;
856                 }
857
858 //              if ( button_info_query(&Player->bi, PLUS_5_PERCENT_THROTTLE) ) {
859 //                      control_used(PLUS_5_PERCENT_THROTTLE);
860 //                      Player->ci.forward_cruise_percent += (100.0f/Player_ship->current_max_speed);
861 //              }
862
863                 if ( button_info_query(&Player->bi, PLUS_5_PERCENT_THROTTLE) ) {
864                         control_used(PLUS_5_PERCENT_THROTTLE);
865                         Player->ci.forward_cruise_percent += 5.0f;
866                         if (Player->ci.forward_cruise_percent > 100.0f)
867                                 Player->ci.forward_cruise_percent = 100.0f;
868                 }
869
870 //              if ( button_info_query(&Player->bi, MINUS_5_PERCENT_THROTTLE) ) {
871 //                      control_used(MINUS_5_PERCENT_THROTTLE);
872 //                      Player->ci.forward_cruise_percent -= (100.0f/Player_ship->current_max_speed);
873 //              }
874
875                 if ( button_info_query(&Player->bi, MINUS_5_PERCENT_THROTTLE) ) {
876                         control_used(MINUS_5_PERCENT_THROTTLE);
877                         Player->ci.forward_cruise_percent -= 5.0f;
878                         if (Player->ci.forward_cruise_percent < 0.0f)
879                                 Player->ci.forward_cruise_percent = 0.0f;
880                 }
881
882                 if ( button_info_query(&Player->bi, ZERO_THROTTLE) ) {
883                         control_used(ZERO_THROTTLE);
884                         player_clear_speed_matching();
885                         if ( ci->forward_cruise_percent > 0.0f && Player_obj->phys_info.fspeed > 0.5) {
886                                 snd_play( &Snds[SND_ZERO_THROTTLE], 0.0f );
887                         }
888
889                         ci->forward_cruise_percent = 0.0f;
890                         override_analog_throttle = 1;
891                 }
892
893                 if ( button_info_query(&Player->bi, MAX_THROTTLE) ) {
894                         control_used(MAX_THROTTLE);
895                         player_clear_speed_matching();
896                         if ( ci->forward_cruise_percent < 100.0f ) {
897                                 snd_play( &Snds[SND_FULL_THROTTLE], 0.0f );
898                         }
899
900                         ci->forward_cruise_percent = 100.0f;
901                         override_analog_throttle = 1;
902                 }
903
904                 // AL 12-29-97: If afterburner key is down, player should have full forward thrust (even if afterburners run out)
905                 if ( check_control(AFTERBURNER) ) {
906                         ci->forward = 1.0f;
907                 }
908
909                 if ( Player->flags & PLAYER_FLAGS_MATCH_TARGET ) {
910                         if ( (Player_ai->last_target == Player_ai->target_objnum) && (Player_ai->target_objnum != -1) && ( ci->forward_cruise_percent == oldspeed) ) {
911                                 float tspeed, pmax_speed;
912
913                                 tspeed = Objects[Player_ai->target_objnum].phys_info.fspeed;
914
915                                 // maybe need to get speed from docked partner
916                                 if ( tspeed < MATCH_SPEED_THRESHOLD ) {
917                                         ai_info *aip;
918
919                                         Assert(Objects[Player_ai->target_objnum].type == OBJ_SHIP);
920
921                                         aip = &Ai_info[Ships[Objects[Player_ai->target_objnum].instance].ai_index];
922                                         if ( aip->ai_flags & AIF_DOCKED ) {
923                                                 Assert( aip->dock_objnum != -1 );
924                                                 tspeed = Objects[aip->dock_objnum].phys_info.fspeed;
925                                         }
926                                 }
927
928                                 //      Note, if closer than 100 units, scale down speed a bit.  Prevents repeated collisions. -- MK, 12/17/97
929                                 float dist = vm_vec_dist(&Player_obj->pos, &Objects[Player_ai->target_objnum].pos);
930
931                                 if (dist < 100.0f) {
932                                         tspeed = tspeed * (0.5f + dist/200.0f);
933                                 }
934
935                                 //pmax_speed = Ship_info[Ships[Player_obj->instance].ship_info_index].max_speed;
936                                 pmax_speed = Ships[Player_obj->instance].current_max_speed;
937                                 ci->forward_cruise_percent = (tspeed / pmax_speed) * 100.0f;
938                                 override_analog_throttle = 1;
939         //                      if ( ci->forward_cruise_percent > 100.0f )
940         //                              HUD_printf ("Cannot travel that fast.  Setting throttle to full.");
941                                 // mprintf(("forward -- %7.3f\n", ci->forward_cruise_percent));
942
943                         } else
944                                 Player->flags &= ~PLAYER_FLAGS_MATCH_TARGET;
945                 }
946
947 //                      player_read_joystick();
948                 // code to read joystick axis for pitch/heading.  Code to read joystick buttons
949                 // fo1r bank.
950                 if ( !(Game_mode & GM_DEAD) )   {
951                         playercontrol_read_stick(axis, frame_time);
952                 } else {
953                         axis[0] = axis[1] = axis[2] = axis[3] = axis[4] = 0;
954                 }
955
956                 ignore_pitch = FALSE;
957
958                 if (Axis_map_to[JOY_HEADING_AXIS] >= 0) {
959                         // check the heading on the x axis
960                         if ( check_control(BANK_WHEN_PRESSED) ) {
961                                 delta = f2fl( axis[JOY_HEADING_AXIS] );
962                                 if ( (delta > 0.05f) || (delta < -0.05f) ) {
963                                         ci->bank -= delta;
964                                         ignore_pitch = TRUE;
965                                 }
966
967                         } else {
968                                 ci->heading += f2fl( axis[JOY_HEADING_AXIS] );
969                         }
970                 }
971
972                 // check the pitch on the y axis
973                 if (Axis_map_to[JOY_PITCH_AXIS] >= 0)
974                         ci->pitch -= f2fl( axis[JOY_PITCH_AXIS] );
975
976                 if (Axis_map_to[JOY_BANK_AXIS] >= 0) {
977                         ci->bank -= f2fl( axis[JOY_BANK_AXIS] ) * 1.5f;
978                 }
979
980                 // axis 2 is for throttle
981                 if (Axis_map_to[JOY_ABS_THROTTLE_AXIS] >= 0) {
982                         scaled = (float) axis[JOY_ABS_THROTTLE_AXIS] * 1.2f / (float) F1_0 - 0.1f;  // convert to -0.1 - 1.1 range
983                         oldspeed = ci->forward_cruise_percent;
984
985 //                      scaled = (scaled + 1.0f) / 1.85f;
986                         newspeed = (1.0f - scaled) * 100.0f;
987
988                         delta = analog_throttle_last - newspeed;
989                         if (!override_analog_throttle || (delta < -1.5f) || (delta > 1.5f)) {
990                                 ci->forward_cruise_percent = newspeed;
991                                 analog_throttle_last = newspeed;
992                                 override_analog_throttle = 0;
993 /*
994                                 // AL 1-5-98: don't play throttle sounds when using analog control
995
996                                 if ( (oldspeed < 1.0f) && (newspeed >= 1.0f) )
997                                         snd_play( &Snds[SND_THROTTLE_UP], 0.0f );
998                                 else if ( (oldspeed < 66.6f) && (newspeed >= 66.6f) )
999                                         snd_play( &Snds[SND_THROTTLE_UP], 0.0f );
1000                                 else if ( (oldspeed < 33.3f) && (newspeed >= 33.3f) )
1001                                         snd_play( &Snds[SND_THROTTLE_UP], 0.0f );
1002                                 else if ( (oldspeed > 99.0f) && (newspeed <= 99.0f) )
1003                                         snd_play( &Snds[SND_THROTTLE_DOWN], 0.0f );
1004                                 else if ( (oldspeed > 33.3f) && (newspeed <= 33.3f) )
1005                                         snd_play( &Snds[SND_THROTTLE_DOWN], 0.0f );
1006                                 else if ( (oldspeed > 66.6f) && (newspeed <= 66.6f) )
1007                                         snd_play( &Snds[SND_THROTTLE_DOWN], 0.0f );
1008 */
1009                         }
1010                 }
1011
1012                 if (Axis_map_to[JOY_REL_THROTTLE_AXIS] >= 0)
1013                         ci->forward_cruise_percent += f2fl(axis[JOY_REL_THROTTLE_AXIS]) * 100.0f * frame_time;
1014
1015                 if ( ci->forward_cruise_percent > 100.0f )
1016                         ci->forward_cruise_percent = 100.0f;
1017                 if ( ci->forward_cruise_percent < 0.0f )
1018                         ci->forward_cruise_percent = 0.0f;
1019
1020                 // set up the firing stuff.  Read into control info ala Descent so that weapons will be
1021                 // created during the object simulation phase, and not immediately as was happening before.
1022
1023                 //keyboard: fire the current primary weapon
1024                 if (check_control(FIRE_PRIMARY)) {
1025                         ci->fire_primary_count++;
1026
1027                         // if we're a multiplayer client, set our accum bits now
1028                         // if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER) && !(Netgame.debug_flags & NETD_FLAG_CLIENT_FIRING)){
1029                         // if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER) && !(Netgame.debug_flags & NETD_FLAG_CLIENT_FIRING)){
1030                                 // Net_player->s_info.accum_buttons |= OOC_FIRE_PRIMARY;
1031                         // }
1032                 }
1033
1034                 // mouse: fire the current primary weapon
1035 //              ci->fire_primary_count += mouse_down(1);
1036
1037                 // for debugging, check to see if the debug key is down -- if so, make fire the debug laser instead
1038 #ifndef NDEBUG
1039                 if ( keyd_pressed[KEY_DEBUG_KEY] ) {
1040                         ci->fire_debug_count = ci->fire_primary_count;
1041                         ci->fire_primary_count = 0;
1042                 }
1043 #endif
1044
1045                 // keyboard: fire the current secondary weapon
1046                 if (check_control(FIRE_SECONDARY)) {
1047                         ci->fire_secondary_count++;
1048
1049                         // if we're a multiplayer client, set our accum bits now
1050                         if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER)){
1051                                 Net_player->s_info.accum_buttons |= OOC_FIRE_SECONDARY;
1052                         }
1053                 }
1054
1055                 // keyboard: launch countermeasures
1056                 if ( button_info_query(&Player->bi, LAUNCH_COUNTERMEASURE) ) {
1057                         control_used(LAUNCH_COUNTERMEASURE);
1058                         ci->fire_countermeasure_count++;
1059                         hud_gauge_popup_start(HUD_CMEASURE_GAUGE);
1060
1061                         // if we're a multiplayer client, set our accum bits now
1062                         // if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && !(Net_player->flags & NETINFO_FLAG_AM_MASTER) && !(Netgame.debug_flags & NETD_FLAG_CLIENT_FIRING)){
1063                                 // Net_player->s_info.accum_buttons |= OOC_FIRE_COUNTERMEASURE;
1064                         // }
1065                 }
1066
1067                 // see if the afterburner has been started (keyboard + joystick)
1068                 if (check_control(AFTERBURNER)) {
1069                         if (!afterburner_last) {
1070                                 Assert(Player_ship);
1071                                 if ( !(Ship_info[Player_ship->ship_info_index].flags & SIF_AFTERBURNER) ) {
1072                                         gamesnd_play_error_beep();
1073                                 } else {
1074                                         ci->afterburner_start = 1;
1075                                 }
1076                         }
1077
1078                         afterburner_last = 1;
1079
1080                 } else {
1081                         if (afterburner_last)
1082                                 ci->afterburner_stop = 1;
1083
1084                         afterburner_last = 0;
1085                 }
1086         }
1087
1088         if ( (Viewer_mode & VM_EXTERNAL) || slew_active ) {
1089                 if ( !(Viewer_mode & VM_EXTERNAL_CAMERA_LOCKED) || slew_active ) {
1090                         ci->heading=0.0f;
1091                         ci->pitch=0.0f;
1092                         ci->bank=0.0f;
1093                 }
1094         }
1095 }
1096
1097 void read_player_controls(object *objp, float frametime)
1098 {
1099 //      if (Game_mode & GM_DEAD)
1100 //              return;
1101
1102         joy_ff_adjust_handling((int) objp->phys_info.speed);
1103
1104         {
1105                 switch( Player->control_mode )  {
1106                 case PCM_SUPERNOVA:
1107                         break;
1108
1109                 case PCM_NORMAL:
1110               read_keyboard_controls(&(Player->ci), frametime, &objp->phys_info );
1111                         break;
1112                 case PCM_WARPOUT_STAGE1:        // Accelerate to 40 km/s
1113                 case PCM_WARPOUT_STAGE2:        // Go 40 km/s steady up to the effect
1114                 case PCM_WARPOUT_STAGE3:        // Go 40 km/s steady through the effect
1115
1116                         memset(&(Player->ci), 0, sizeof(control_info) );                // set the controls to 0
1117
1118                         if ( (objp->type == OBJ_SHIP) && (!(Game_mode & GM_DEAD)) )     {
1119
1120                                 Warpout_time += flFrametime;
1121
1122                                 if ( Warpout_forced )   {
1123
1124                                         Ships[objp->instance].current_max_speed = TARGET_WARPOUT_SPEED*2.0f;
1125
1126                                         float diff = TARGET_WARPOUT_SPEED-objp->phys_info.fspeed;                               
1127                                         if ( diff < 0.0f ) diff = 0.0f;
1128                                         
1129                                         Player->ci.forward = ((TARGET_WARPOUT_SPEED+diff) / Ships[objp->instance].current_max_speed);
1130
1131                                 } else {
1132                                         int warp_failed=0;
1133                                         // check if warp ability has been disabled
1134                                         if ( Ships[objp->instance].flags & ( SF_WARP_BROKEN|SF_WARP_NEVER ) ) {
1135                                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Cannot warp out at this time.", 81));
1136                                                 warp_failed=1;
1137                                         } else {
1138                                                 int     can_warp = 0;
1139                                                 if ( (!warp_failed) && (Ships[objp->instance].current_max_speed >= TARGET_WARPOUT_SPEED) )      {
1140                                                         can_warp = 1;
1141                                                 } else {
1142                                                         if (Ship_info[Ships[objp->instance].ship_info_index].max_overclocked_speed < TARGET_WARPOUT_SPEED) {
1143                                                                 // Cannot go fast enough, so abort sequence.
1144                                                                 warp_failed=1;
1145                                                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR( "Unable to engage warp... ship must be able to reach %.1f km/s", 82), TARGET_WARPOUT_SPEED );
1146                                                                 can_warp = 0;
1147                                                         } else {
1148                                                                 Ships[objp->instance].current_max_speed = TARGET_WARPOUT_SPEED + 5.0f;
1149                                                                 //Ships[objp->instance].current_max_speed = Ship_info[Ships[objp->instance].ship_info_index].max_overclocked_speed;
1150                                                                 can_warp = 1;
1151                                                         }
1152                                                 }
1153                                                 if (can_warp) {
1154                                                         float diff = TARGET_WARPOUT_SPEED-objp->phys_info.fspeed;                               
1155                                                         if ( diff < 0.0f ) 
1156                                                                 diff = 0.0f;
1157                                                 
1158                                                         Player->ci.forward = ((TARGET_WARPOUT_SPEED+diff) / Ships[objp->instance].current_max_speed);
1159                                                 }
1160                                         }
1161
1162                                         if ( warp_failed ) {
1163                                                 snd_play(&Snds[SND_PLAYER_WARP_FAIL]);
1164                                                 gameseq_post_event( GS_EVENT_PLAYER_WARPOUT_STOP );
1165                                         }
1166                                 }
1167                         
1168                                 if ( Player->control_mode == PCM_WARPOUT_STAGE1 )       {
1169
1170
1171                                         // Wait at least 3 seconds before making sure warp speed is set.
1172                                         if ( Warpout_time>MINIMUM_PLAYER_WARPOUT_TIME ) {
1173                                                 // If we are going around 5% of the target speed, progress to next stage
1174                                                 float diff = fl_abs(objp->phys_info.fspeed - TARGET_WARPOUT_SPEED )/TARGET_WARPOUT_SPEED;
1175                                                 if ( diff < TARGET_WARPOUT_MATCH_PERCENT )      {
1176                                                         gameseq_post_event( GS_EVENT_PLAYER_WARPOUT_DONE_STAGE1 );
1177                                                 }
1178                                         }
1179                                 }
1180                         }
1181                         break;
1182                 }
1183         }
1184
1185         // the ships maximum velocity now depends on the energy flowing to engines
1186         if(objp->type != OBJ_OBSERVER){
1187                 objp->phys_info.max_vel.z = Ships[objp->instance].current_max_speed;
1188         } 
1189         if(Player_obj->type == OBJ_SHIP){       
1190                 // only read player control info if player ship is not dead
1191                 if ( !(Ships[Player_obj->instance].flags & SF_DYING) ) {
1192                         vector wash_rot;
1193                         if ((Ships[objp->instance].wash_intensity > 0) && !((Player->control_mode == PCM_WARPOUT_STAGE1) || (Player->control_mode == PCM_WARPOUT_STAGE2) || (Player->control_mode == PCM_WARPOUT_STAGE3)) ) {
1194                                 float intensity = 0.3f * min(Ships[objp->instance].wash_intensity, 1.0f);
1195                                 vm_vec_copy_scale(&wash_rot, &Ships[objp->instance].wash_rot_axis, intensity);
1196                                 physics_read_flying_controls( &objp->orient, &objp->phys_info, &(Player->ci), flFrametime, &wash_rot);
1197                         } else {
1198                                 physics_read_flying_controls( &objp->orient, &objp->phys_info, &(Player->ci), flFrametime);
1199                         }
1200                 }
1201         } else if(Player_obj->type == OBJ_OBSERVER){
1202                 physics_read_flying_controls(&objp->orient,&objp->phys_info,&(Player->ci), flFrametime);
1203         }
1204 }
1205
1206 void player_controls_init()
1207 {
1208         static int initted = 0;
1209
1210         if (initted)
1211                 return;
1212
1213         initted = 1;
1214         physics_init( &Descent_physics );
1215         Descent_physics.flags |= PF_ACCELERATES | PF_SLIDE_ENABLED;
1216
1217         Viewer_slew_angles_delta.p = 0.0f;
1218         Viewer_slew_angles_delta.b = 0.0f;
1219         Viewer_slew_angles_delta.h = 0.0f;
1220 }
1221
1222 // Clear current speed matching and auto-speed matching flags
1223 void player_clear_speed_matching()
1224 {
1225         if ( !Player ) {
1226                 Int3(); // why is Player NULL?
1227                 return;
1228         }
1229
1230         Player->flags &= ~PLAYER_FLAGS_MATCH_TARGET;
1231         Player->flags &= ~PLAYER_FLAGS_AUTO_MATCH_SPEED;
1232 }
1233
1234 // function which computes the forward_thrust_time needed for the player ship to match
1235 // velocities with the currently selected target
1236 // input:       no_target_text  => default parm (NULL), used to override HUD output when no target exists
1237 //                              match_off_text  =>      default parm (NULL), used to overide HUD output when matching toggled off
1238 //                              match_on_text   =>      default parm (NULL), used to overide HUD output when matching toggled on
1239 void player_match_target_speed(char *no_target_text, char *match_off_text, char *match_on_text)
1240 {
1241         if ( Objects[Player_ai->target_objnum].type != OBJ_SHIP ) {
1242                 return;
1243         }
1244
1245         // multiplayer observers can't match target speed
1246         if((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER)) ){
1247                 return;
1248         }
1249
1250         if ( Player_ai->target_objnum == -1) {
1251                 if ( no_target_text ) {
1252                         if ( no_target_text[0] ) {
1253                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, no_target_text );
1254                         }
1255                 } else {
1256 //                      HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR("No currently selected target.",-1) );
1257                 }
1258                 return;
1259         }
1260
1261         if ( Player->flags & PLAYER_FLAGS_MATCH_TARGET ) {
1262                 Player->flags &= ~PLAYER_FLAGS_MATCH_TARGET;
1263                 if ( match_off_text ) {
1264                         if ( match_off_text[0] ) {
1265                                 HUD_sourced_printf(HUD_SOURCE_HIDDEN, match_off_text );
1266                         }
1267                 } else {
1268 //                      HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR("No longer matching speed with current target.",-1) );
1269                 }
1270         } else {
1271                 int can_match=0;
1272
1273                 if ( Objects[Player_ai->target_objnum].phys_info.speed > MATCH_SPEED_THRESHOLD ) {
1274                         can_match=1;
1275                 } else {
1276                         // account for case of matching speed with docked ship 
1277                         ai_info *aip;
1278                         aip = &Ai_info[Ships[Objects[Player_ai->target_objnum].instance].ai_index];
1279                         if ( aip->ai_flags & AIF_DOCKED ) {
1280                                 Assert( aip->dock_objnum != -1 );
1281                                 if ( Objects[aip->dock_objnum].phys_info.fspeed > MATCH_SPEED_THRESHOLD ) {
1282                                         can_match=1;
1283                                 }
1284                         }
1285                 }
1286
1287                 if ( can_match ) {
1288                         Player->flags |= PLAYER_FLAGS_MATCH_TARGET;
1289                         if ( match_on_text ) {
1290                                 if ( match_on_text[0] ) {
1291                                         HUD_sourced_printf(HUD_SOURCE_HIDDEN, match_on_text );
1292                                 }
1293                         } else {
1294 //                              HUD_sourced_printf(HUD_SOURCE_HIDDEN, XSTR("Matching speed with current target.",-1) );
1295                         }
1296                 }
1297         }
1298 }
1299
1300
1301 //#ifndef NDEBUG
1302
1303 // toggle_player_object toggles between the player objects (i.e. the ship they are currently flying)
1304 // and a descent style ship.
1305
1306 int use_descent = 0;
1307 LOCAL physics_info phys_save;
1308
1309 void toggle_player_object()
1310 {
1311         if ( use_descent ) {
1312                 memcpy( &Player_obj->phys_info, &phys_save, sizeof(physics_info) );
1313         } else {
1314                 memcpy( &phys_save, &Player_obj->phys_info, sizeof(physics_info) );
1315                 memcpy( &Player_obj->phys_info, &Descent_physics, sizeof(physics_info) );
1316         }
1317         use_descent = !use_descent;
1318
1319         HUD_sourced_printf(HUD_SOURCE_HIDDEN, NOX("Using %s style physics for player ship."), use_descent ? NOX("DESCENT") : NOX("FreeSpace"));
1320 }
1321
1322 //#endif                // ifndef NDEBUG
1323
1324 // Init the data required for determining whether 'all alone' message should play
1325 void player_init_all_alone_msg()
1326 {
1327         ship_obj        *so;
1328         object  *objp;
1329
1330         Player->check_for_all_alone_msg=timestamp(0);
1331
1332         // See if there are any friendly ships present, if so return without preventing msg
1333         for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
1334                 objp = &Objects[so->objnum];
1335                 if ( objp == Player_obj ) {
1336                         continue;
1337                 }
1338
1339                 if ( Ships[objp->instance].team == Player_ship->team ) {
1340                         if ( !(Ship_info[Ships[objp->instance].ship_info_index].flags & SIF_HARMLESS) ) {
1341                                 return;
1342                         }
1343                 }
1344         }
1345
1346         // There must be no friendly ships present, so prevent the 'all alone' message from ever playing
1347         Player->flags |= PLAYER_FLAGS_NO_CHECK_ALL_ALONE_MSG;
1348 }
1349
1350 // Called when a new pilot is created
1351 void player_set_pilot_defaults(player *p)
1352 {
1353         // Enable auto-targeting by default for all new pilots
1354         p->flags |= PLAYER_FLAGS_AUTO_TARGETING;
1355         p->save_flags |= PLAYER_FLAGS_AUTO_TARGETING;
1356
1357         p->auto_advance = 1;
1358 }
1359
1360 // Store some player preferences to Player->save_flags
1361 void player_save_target_and_weapon_link_prefs()
1362 {
1363         Player->save_flags = 0;
1364         if ( Player->flags & PLAYER_FLAGS_AUTO_TARGETING ) {
1365                 Player->save_flags |= PLAYER_FLAGS_AUTO_TARGETING;
1366         }
1367
1368
1369         if ( Player->flags & PLAYER_FLAGS_AUTO_MATCH_SPEED ) {
1370                 // multiplayer observers can't match target speed
1371                 if(!((Game_mode & GM_MULTIPLAYER) && (Net_player != NULL) && ((Net_player->flags & NETINFO_FLAG_OBSERVER) || (Player_obj->type == OBJ_OBSERVER))) ){                            
1372                         Player->save_flags |= PLAYER_FLAGS_AUTO_MATCH_SPEED;
1373                 }               
1374         }
1375
1376         // if we're in multiplayer mode don't do this because we will desync ourselves with the server
1377         if(!(Game_mode & GM_MULTIPLAYER)){
1378                 if ( Player_ship->flags & SF_PRIMARY_LINKED ) {
1379                         Player->save_flags |= PLAYER_FLAGS_LINK_PRIMARY;
1380                 } else {
1381                         Player->flags &= ~PLAYER_FLAGS_LINK_PRIMARY;
1382                 }
1383                 if ( Player_ship->flags & SF_SECONDARY_DUAL_FIRE ) {
1384                         Player->save_flags |= PLAYER_FLAGS_LINK_SECONDARY;
1385                 } else {
1386                         Player->flags &= ~PLAYER_FLAGS_LINK_SECONDARY;
1387                 }
1388         }
1389 }
1390
1391 // Store some player preferences to Player->save_flags
1392 void player_restore_target_and_weapon_link_prefs()
1393 {
1394         //      Don't restores the save flags in training, as we must ensure certain things are off, such as speed matching.
1395         if ( !(The_mission.game_type & MISSION_TYPE_TRAINING )) {
1396                 Player->flags |= Player->save_flags;
1397         }
1398
1399         if ( Player->flags & PLAYER_FLAGS_LINK_PRIMARY ) {
1400                 if ( Player_ship->weapons.num_primary_banks > 1 ) {
1401                         Player_ship->flags |= SF_PRIMARY_LINKED;
1402                 }
1403         }
1404
1405         if ( Player->flags & PLAYER_FLAGS_LINK_SECONDARY ) {
1406                 Player_ship->flags |= SF_SECONDARY_DUAL_FIRE;
1407         }
1408 }
1409
1410 // initialize player statistics on a per mission basis
1411 void player_level_init()
1412 {       
1413         Player->flags = PLAYER_FLAGS_STRUCTURE_IN_USE;                  // reset the player flags
1414         Player->flags |= Player->save_flags;
1415
1416         memset(&(Player->ci), 0, sizeof(control_info) );                // set the controls to 0
1417
1418         Viewer_slew_angles.p = 0.0f;    Viewer_slew_angles.b = 0.0f;    Viewer_slew_angles.h = 0.0f;
1419         Viewer_external_info.angles.p = 0.0f;
1420         Viewer_external_info.angles.b = 0.0f;
1421         Viewer_external_info.angles.h = 0.0f;
1422         Viewer_external_info.distance = 0.0f;
1423
1424         Viewer_mode = 0;
1425  
1426         Player_obj = NULL;
1427         Player_ship = NULL;
1428         Player_ai = NULL;
1429         
1430         //      Init variables for friendly fire monitoring.
1431         Player->friendly_last_hit_time = 0;
1432         Player->friendly_hits = 0;
1433         Player->friendly_damage = 0.0f;
1434         Player->last_warning_message_time = 0;
1435
1436         Player->control_mode = PCM_NORMAL;
1437
1438         Player->allow_warn_timestamp = 1;               // init timestamp that is used for managing attack warnings sent to player
1439         Player->check_warn_timestamp = 1;
1440         Player->warn_count = 0;                                         // number of attack warnings player has received this mission
1441
1442         Player->distance_warning_count = 0;             // Number of warning too far from origin
1443         Player->distance_warning_time = 0;              // Time at which last warning was given
1444
1445         Player->praise_count = 0;                                       // number of praises player has received this mission
1446         Player->allow_praise_timestamp = 1;             // timestamp until next praise is allowed
1447         Player->praise_delay_timestamp = 0;             // timstamp used to delay praises given to the player
1448
1449         Player->ask_help_count = 0;                             // number of times player has been asked for help by wingmen
1450         Player->allow_ask_help_timestamp = 1;   // timestamp until next ask_help is allowed
1451
1452         Player->scream_count = 0;                                       // number of times player has heard wingman screams this mission
1453         Player->allow_scream_timestamp = 1;             // timestamp until next wingman scream is allowed
1454
1455         Player->request_repair_timestamp = 1;   // timestamp until next 'requesting repair sir' message can be played
1456
1457         Player->repair_sound_loop = -1;
1458         Player->cargo_scan_loop = -1;
1459         Player->cargo_inspect_time = 0;                 // time that current target's cargo has been inspected for
1460
1461         Player->target_is_dying = -1;                           // The player target is dying, set to -1 if no target
1462         Player->current_target_sx = -1;                 // Screen x-pos of current target (or subsystem if applicable)
1463         Player->current_target_sy = -1;                 // Screen y-pos of current target (or subsystem if applicable)
1464         Player->target_in_lock_cone = -1;               // Is the current target in secondary weapon lock cone?
1465         Player->locking_subsys=NULL;                            // Subsystem pointer that missile lock is trying to seek
1466         Player->locking_on_center=0;                            // boolean, whether missile lock is trying for center of ship or not
1467         Player->locking_subsys_parent=-1;
1468
1469         Player->killer_objtype=-1;                                      // type of object that killed player
1470         Player->killer_weapon_index;                            // weapon used to kill player (if applicable)
1471         Player->killer_parent_name[0]=0;                        // name of parent object that killed the player
1472
1473         Player_all_alone_msg_inited=0;
1474         Player->flags &= ~PLAYER_FLAGS_NO_CHECK_ALL_ALONE_MSG;
1475
1476         // Player->insignia_bitmap = -1;
1477
1478         Joystick_last_reading = -1;                             // Make the joystick read right away.
1479 }
1480
1481 // player_init() initializes global veriables once a game -- needed because of mallocing that
1482 // goes on in structures in the player file
1483 void player_init()
1484 {
1485         Player_num = 0;
1486         Player = &Players[Player_num];
1487         Player->num_campaigns = 0;
1488         Player->flags |= PLAYER_FLAGS_STRUCTURE_IN_USE;
1489         Player->failures_this_session = 0;
1490         Player->show_skip_popup = (ubyte) 1;
1491 }
1492
1493 // stop any looping sounds associated with the Player, called from game_stop_looped_sounds().
1494 void player_stop_looped_sounds()
1495 {
1496         Assert(Player);
1497         if ( Player->repair_sound_loop > -1 )   {
1498                 snd_stop(Player->repair_sound_loop);
1499                 Player->repair_sound_loop = -1;
1500         }
1501         if ( Player->cargo_scan_loop > -1 )     {
1502                 snd_stop(Player->cargo_scan_loop);
1503                 Player->cargo_scan_loop = -1;
1504         }
1505 }
1506
1507 // Start the repair sound if it hasn't already been started.  Called when a player ship is being
1508 // repaired by a support ship
1509 void player_maybe_start_repair_sound()
1510 {
1511         Assert(Player);
1512         if ( Player->repair_sound_loop == -1 ) {
1513                 Player->repair_sound_loop = snd_play_looping( &Snds[SND_SHIP_REPAIR] );
1514         }
1515 }
1516
1517 // stop the player repair sound if it is already playing
1518 void player_stop_repair_sound()
1519 {
1520         Assert(Player);
1521         if ( Player->repair_sound_loop != -1 ) {
1522                 snd_stop(Player->repair_sound_loop);
1523                 Player->repair_sound_loop  = -1;
1524         }
1525 }
1526
1527 // start the cargo scanning sound if it hasn't already been started
1528 void player_maybe_start_cargo_scan_sound()
1529 {
1530         Assert(Player);
1531         if ( Player->cargo_scan_loop == -1 ) {
1532                 Player->cargo_scan_loop = snd_play_looping( &Snds[SND_CARGO_SCAN] );
1533         }
1534 }
1535
1536 // stop the player repair sound if it is already playing
1537 void player_stop_cargo_scan_sound()
1538 {
1539         Assert(Player);
1540         if ( Player->cargo_scan_loop != -1 ) {
1541                 snd_stop(Player->cargo_scan_loop);
1542                 Player->cargo_scan_loop  = -1;
1543         }
1544 }
1545
1546 // See if there is a praise message to deliver to the player.  We want to delay the praise messages
1547 // a bit, to make them more realistic
1548 //
1549 // exit:        1       =>      a praise message was delivered to the player, or a praise is pending
1550 //                      0       => no praise is pending
1551
1552 #define PLAYER_ALLOW_PRAISE_INTERVAL    60000           // minimum time between praises
1553
1554 int player_process_pending_praise()
1555 {
1556         // in multiplayer, never praise
1557         if(Game_mode & GM_MULTIPLAYER){
1558                 return 0;
1559         }
1560
1561         if ( timestamp_elapsed(Player->praise_delay_timestamp) ) {
1562                 int ship_index;
1563
1564                 Player->praise_delay_timestamp = 0;
1565                 ship_index = ship_get_random_player_wing_ship( SHIP_GET_NO_PLAYERS, 1000.0f );
1566                 if ( ship_index >= 0 ) {
1567                         // Only praise if above 50% integrity
1568                         if ( Objects[Ships[ship_index].objnum].hull_strength/Ship_info[Ships[ship_index].ship_info_index].initial_hull_strength > 0.5f ) {
1569                                 message_send_builtin_to_player(MESSAGE_PRAISE, &Ships[ship_index], MESSAGE_PRIORITY_HIGH, MESSAGE_TIME_SOON, 0, 0, -1, -1);
1570                                 Player->allow_praise_timestamp = timestamp(PLAYER_ALLOW_PRAISE_INTERVAL*(Game_skill_level+1) );
1571                                 Player->allow_scream_timestamp = timestamp(20000);              // prevent death scream following praise
1572                                 Player->praise_count++;
1573                                 return 1;
1574                         }
1575                 }
1576         }
1577
1578         if ( Player->praise_delay_timestamp == 0 ) {
1579                 return 0;
1580         }
1581
1582         return 1;
1583 }
1584
1585 int player_inspect_cap_subsys_cargo(float frametime, char *outstr);
1586 // See if the player should be inspecting cargo, and update progress.
1587 // input:       frametime       =>              time since last frame in seconds
1588 // input:       outstr          =>              (output parm) holds string that HUD should display
1589 //
1590 //      exit:           1                               =>              player should display outstr on HUD
1591 //                              0                               =>              don't display cargo on HUD
1592 int player_inspect_cargo(float frametime, char *outstr)
1593 {
1594         object          *cargo_objp;
1595         ship                    *cargo_sp;
1596         ship_info       *cargo_sip;
1597         vector          vec_to_cargo;
1598         float                   dot;
1599
1600         outstr[0] = 0;
1601
1602         if ( Player_ai->target_objnum < 0 ) {
1603                 return 0;
1604         }
1605
1606         cargo_objp = &Objects[Player_ai->target_objnum];
1607         Assert(cargo_objp->type == OBJ_SHIP);
1608         cargo_sp = &Ships[cargo_objp->instance];
1609         cargo_sip = &Ship_info[cargo_sp->ship_info_index];
1610
1611         if (cargo_sip->flags & SIF_HUGE_SHIP) {
1612                 return player_inspect_cap_subsys_cargo(frametime, outstr);
1613         }
1614
1615         // check if target is ship class that can be inspected
1616         // MWA -- 1/27/98 -- added fighters/bombers to this list.  For multiplayer, we
1617         // want to show callsign of player
1618
1619         // scannable cargo behaves differently.  Scannable cargo is either "scanned" or "not scanned".  This flag
1620         // can be set on any ship.  Any ship with this set won't have "normal" cargo behavior
1621         if ( !(cargo_sp->flags & SF_SCANNABLE) ) {
1622                 if ( Game_mode & GM_NORMAL ) {
1623                         if ( !(cargo_sip->flags & (SIF_CARGO|SIF_TRANSPORT)) ) {
1624                                 return 0;
1625                         }
1626                 } else {
1627                         if ( !(cargo_sip->flags & (SIF_CARGO|SIF_TRANSPORT|SIF_FIGHTER|SIF_BOMBER)) ) {
1628                                 return 0;
1629                         }
1630                 }
1631
1632                 // won't show callsign information for single player games
1633                 if ( (Game_mode & GM_MULTIPLAYER) && !((cargo_sip->flags & (SIF_FIGHTER|SIF_BOMBER)) && (cargo_objp->flags & OF_PLAYER_SHIP)) )
1634                         return 0;
1635         }
1636
1637         // if cargo is already revealed
1638         if ( cargo_sp->flags & SF_CARGO_REVEALED ) {
1639                 if ( !(cargo_sp->flags & SF_SCANNABLE) ) {
1640                         char *cargo_name;
1641                         cargo_name = Cargo_names[cargo_sp->cargo1 & CARGO_INDEX_MASK];
1642                         Assert ( cargo_name );
1643
1644                         if ( cargo_sip->flags & (SIF_CARGO|SIF_TRANSPORT) ) {
1645                                 if ( cargo_name[0] == '#' )
1646                                         sprintf(outstr, XSTR( "passengers:\n   %s", 83), cargo_name+1 );
1647                                 else
1648                                         sprintf(outstr,XSTR( "cargo: %s", 84), cargo_name );
1649                         } else {
1650                                 int pn;
1651
1652                                 Assert( Game_mode & GM_MULTIPLAYER );
1653
1654                                 // get a player num from the object, then get a callsign from the player structure.
1655                                 pn = multi_find_player_by_object( cargo_objp );
1656                                 // Assert( pn != -1 );
1657                                 if(pn == -1){
1658                                         strcpy(outstr, "");
1659                                 } else {
1660                                         sprintf(outstr, "%s", Net_players[pn].player->short_callsign );
1661                                 }
1662                         }
1663                 } else {
1664                         sprintf(outstr, XSTR( "Scanned", 85) );
1665                 }
1666
1667                 // always bash cargo_inspect_time to 0 since AI ships can reveal cargo that we
1668                 // are in the process of scanning
1669                 Player->cargo_inspect_time = 0;
1670
1671                 return 1;
1672         }
1673
1674         // see if player is within inspection range
1675         if ( Player_ai->current_target_distance < max(CARGO_REVEAL_MIN_DIST, (cargo_objp->radius+CARGO_RADIUS_DELTA)) ) {
1676
1677                 // check if player is facing cargo, do not proceed with inspection if not
1678                 vm_vec_normalized_dir(&vec_to_cargo, &cargo_objp->pos, &Player_obj->pos);
1679                 dot = vm_vec_dot(&vec_to_cargo, &Player_obj->orient.fvec);
1680                 if ( dot < CARGO_MIN_DOT_TO_REVEAL ) {
1681                         if ( !(cargo_sp->flags & SF_SCANNABLE) )
1682                                 sprintf(outstr,XSTR( "cargo: <unknown>", 86));
1683                         else
1684                                 sprintf(outstr,XSTR( "not scanned", 87));
1685                         hud_targetbox_end_flash(TBOX_FLASH_CARGO);
1686                         Player->cargo_inspect_time = 0;
1687                         return 1;
1688                 }
1689
1690                 // player is facing the cargo, and withing range, so proceed with inspection
1691                 if ( hud_sensors_ok(Player_ship, 0) ) {
1692                         Player->cargo_inspect_time += fl2i(frametime*1000+0.5f);
1693                 }
1694
1695                 if ( !(cargo_sp->flags & SF_SCANNABLE) )
1696                         sprintf(outstr,XSTR( "cargo: inspecting", 88));
1697                 else
1698                         sprintf(outstr,XSTR( "scanning", 89));
1699
1700                 if ( Player->cargo_inspect_time > cargo_sip->scan_time ) {
1701                         ship_do_cargo_revealed( cargo_sp );
1702                         snd_play( &Snds[SND_CARGO_REVEAL], 0.0f );
1703                         Player->cargo_inspect_time = 0;
1704                 }
1705         } else {
1706                 if ( !(cargo_sp->flags & SF_SCANNABLE) )
1707                         sprintf(outstr,XSTR( "cargo: <unknown>", 86));
1708                 else
1709                         sprintf(outstr,XSTR( "not scanned", 87));
1710         }
1711
1712         return 1;
1713 }
1714
1715 //      exit:           1                               =>              player should display outstr on HUD
1716 //                              0                               =>              don't display cargo on HUD
1717 int player_inspect_cap_subsys_cargo(float frametime, char *outstr)
1718 {
1719         object          *cargo_objp;
1720         ship                    *cargo_sp;
1721         ship_info       *cargo_sip;
1722         vector          vec_to_cargo;
1723         float                   dot;
1724         ship_subsys     *subsys;
1725
1726         outstr[0] = 0;
1727         subsys = Player_ai->targeted_subsys;
1728
1729         if ( subsys == NULL ) {
1730                 return 0;
1731         } 
1732
1733         cargo_objp = &Objects[Player_ai->target_objnum];
1734         Assert(cargo_objp->type == OBJ_SHIP);
1735         cargo_sp = &Ships[cargo_objp->instance];
1736         cargo_sip = &Ship_info[cargo_sp->ship_info_index];
1737
1738         Assert(cargo_sip->flags & SIF_HUGE_SHIP);
1739
1740         if ( !(cargo_sp->flags & SF_SCANNABLE) ) {
1741                 return 0;
1742         }
1743
1744         // dont scan cargo on turrets, radar, etc.  only the majors: fighterbay, sensor, engines, weapons, nav, comm
1745         if (!valid_cap_subsys_cargo_list(subsys->system_info->name)) {
1746                 return 0;
1747         }
1748
1749         // if cargo is already revealed
1750         if ( subsys->subsys_cargo_revealed ) {
1751                 char *cargo_name;
1752                 if (subsys->subsys_cargo_name == -1) {
1753                         cargo_name = XSTR("Nothing", 1493);
1754                 } else {
1755                         cargo_name = Cargo_names[subsys->subsys_cargo_name];
1756                 }
1757                 Assert ( cargo_name );
1758
1759                 sprintf(outstr,XSTR( "cargo: %s", 84), cargo_name );
1760         
1761                 // always bash cargo_inspect_time to 0 since AI ships can reveal cargo that we
1762                 // are in the process of scanning
1763                 Player->cargo_inspect_time = 0;
1764
1765                 return 1;
1766         }
1767
1768         // see if player is within inspection range [ok for subsys]
1769         vector  subsys_pos;
1770         float           subsys_rad;
1771         int             subsys_in_view, x, y;
1772
1773         get_subsystem_world_pos(cargo_objp, Player_ai->targeted_subsys, &subsys_pos);
1774         subsys_rad = subsys->system_info->radius;
1775
1776         if ( Player_ai->current_target_distance < max(CAP_CARGO_REVEAL_MIN_DIST, (subsys_rad + CAPITAL_CARGO_RADIUS_DELTA)) ) {
1777
1778                 // check if player is facing cargo, do not proceed with inspection if not
1779                 vm_vec_normalized_dir(&vec_to_cargo, &subsys_pos, &Player_obj->pos);
1780                 dot = vm_vec_dot(&vec_to_cargo, &Player_obj->orient.fvec);
1781                 int hud_targetbox_subsystem_in_view(object *target_objp, int *sx, int *sy);
1782                 subsys_in_view = hud_targetbox_subsystem_in_view(cargo_objp, &x, &y);
1783
1784                 if ( (dot < CARGO_MIN_DOT_TO_REVEAL) || (!subsys_in_view) ) {
1785                         sprintf(outstr,XSTR( "cargo: <unknown>", 86));
1786                         hud_targetbox_end_flash(TBOX_FLASH_CARGO);
1787                         Player->cargo_inspect_time = 0;
1788                         return 1;
1789                 }
1790
1791                 // player is facing the cargo, and withing range, so proceed with inspection
1792                 if ( hud_sensors_ok(Player_ship, 0) ) {
1793                         Player->cargo_inspect_time += fl2i(frametime*1000+0.5f);
1794                 }
1795
1796                 sprintf(outstr,XSTR( "cargo: inspecting", 88));
1797
1798                 if ( Player->cargo_inspect_time > cargo_sip->scan_time ) {
1799                         void ship_do_cap_subsys_cargo_revealed( ship *shipp, ship_subsys *subsys, int from_network );
1800                         ship_do_cap_subsys_cargo_revealed( cargo_sp, subsys, 0);
1801                         snd_play( &Snds[SND_CARGO_REVEAL], 0.0f );
1802                         Player->cargo_inspect_time = 0;
1803                 }
1804         } else {
1805                 sprintf(outstr,XSTR( "cargo: <unknown>", 86));
1806         }
1807
1808         return 1;
1809 }
1810
1811
1812 // get the maximum weapon range for the player (of both primary and secondary)
1813 float   player_farthest_weapon_range()
1814 {
1815         float prange,srange;
1816
1817         hud_get_best_primary_bank(&prange);
1818         srange=ship_get_secondary_weapon_range(Player_ship);
1819
1820         return max(prange,srange);
1821 }
1822
1823 // Determine text name for the weapon that killed the player.
1824 // input:       weapon_info_index       =>              weapon type that killed the player (can be -1 if no weapon involved)
1825 //                              killer_species          =>              species of ship that fired weapon
1826 //                              weapon_name                     =>              output parameter... stores weapon name generated in this function       
1827 void player_generate_killer_weapon_name(int weapon_info_index, int killer_species, char *weapon_name)
1828 {
1829         if ( weapon_info_index < 0 ) {
1830                 return;
1831         }
1832
1833         #ifndef NDEBUG
1834         if ( Show_killer_weapon ) {
1835                 killer_species = SPECIES_TERRAN;
1836         }
1837         #endif
1838
1839         switch ( killer_species ) {
1840         case SPECIES_TERRAN:
1841                 strcpy(weapon_name, Weapon_info[weapon_info_index].name);
1842                 break;
1843         default:
1844                 if ( Weapon_info[weapon_info_index].subtype == WP_MISSILE ) {
1845                         strcpy(weapon_name, XSTR( "missile", 90));
1846                 } else {
1847                         strcpy(weapon_name, XSTR( "laser fire", 91));
1848                 }
1849                 break;
1850         }
1851 }
1852
1853 // function to generate the text for death of a player given the information stored in the player object.
1854 // a pointer to the text is returned
1855 char *player_generate_death_text( player *player_p, char *death_text )
1856 {
1857         char weapon_name[NAME_LENGTH];
1858         weapon_name[0] = 0;     
1859
1860         player_generate_killer_weapon_name(player_p->killer_weapon_index, player_p->killer_species, weapon_name);
1861
1862         switch ( player_p->killer_objtype ) {
1863         case OBJ_SHOCKWAVE:
1864                 if ( weapon_name[0] ) {
1865 //                      sprintf(death_text, XSTR("%s was killed by a shockwave from a %s, fired by %s",-1), player_p->callsign, weapon_name, player_p->killer_parent_name);
1866                         sprintf(death_text, XSTR( "%s was killed by a missile shockwave", 92), player_p->callsign);
1867                 } else {
1868                         sprintf(death_text, XSTR( "%s was killed by a shockwave from %s exploding", 93), player_p->callsign, player_p->killer_parent_name);
1869                 }
1870                 break;
1871         case OBJ_WEAPON:
1872                 Assert(weapon_name[0]);
1873
1874                 // is this from a friendly ship?
1875                 int ship_index;
1876                 ship_index = ship_name_lookup(player_p->killer_parent_name, 1);
1877                 if((ship_index >= 0) && (Player_ship != NULL) && (Player_ship->team == Ships[ship_index].team)){
1878                         sprintf(death_text, XSTR( "%s was killed by friendly fire from %s", 1338), player_p->callsign, player_p->killer_parent_name);
1879                 } else {
1880                         sprintf(death_text, XSTR( "%s was killed by %s", 94), player_p->callsign, player_p->killer_parent_name);
1881                 }
1882                 break;
1883         case OBJ_SHIP:
1884                 if ( player_p->flags & PLAYER_FLAGS_KILLED_BY_EXPLOSION ) {
1885                         sprintf(death_text, XSTR( "%s was killed by a blast from %s exploding", 95), player_p->callsign, player_p->killer_parent_name);
1886                 } else if (player_p->flags & PLAYER_FLAGS_KILLED_BY_ENGINE_WASH) {
1887                         sprintf(death_text, XSTR( "%s was killed by engine wash from %s", 1494), player_p->callsign, player_p->killer_parent_name);
1888                 } else {
1889                         sprintf(death_text, XSTR( "%s was killed by a collision with %s", 96), player_p->callsign, player_p->killer_parent_name);
1890                 }
1891                 break;
1892         case OBJ_DEBRIS:
1893                 sprintf(death_text, XSTR( "%s was killed by a collision with debris", 97), player_p->callsign);
1894                 break;
1895         case OBJ_ASTEROID:
1896                 sprintf(death_text, XSTR( "%s was killed by a collision with an asteroid", 98), player_p->callsign);
1897                 break;
1898         case OBJ_BEAM:
1899                 if(strlen(player_p->killer_parent_name) <= 0){                  
1900                         Int3();
1901                         sprintf(death_text, XSTR( "%s was killed by a beam from an unknown source", 1081), player_p->callsign);
1902                 } else {                                        
1903                         // is this from a friendly ship?
1904                         int ship_index;
1905                         ship_index = ship_name_lookup(player_p->killer_parent_name, 1);
1906                         if((ship_index >= 0) && (Player_ship != NULL) && (Player_ship->team == Ships[ship_index].team)){
1907                                 sprintf(death_text, XSTR( "%s was destroyed by friendly beam fire from %s", 1339), player_p->callsign, player_p->killer_parent_name);
1908                         } else {
1909                                 sprintf(death_text, XSTR( "%s was destroyed by a beam from %s", 1082), player_p->callsign, player_p->killer_parent_name);
1910                         }                       
1911                 }
1912                 break;
1913         default:
1914                 sprintf(death_text, XSTR( "%s was killed", 99), player_p->callsign);
1915                 break;
1916         }
1917
1918         return death_text;
1919 }
1920
1921 // display what/who killed the player
1922 void player_show_death_message()
1923 {
1924         char death_text[256];
1925
1926         // check if player killed self
1927         if ( Player->flags & PLAYER_KILLED_SELF ) {
1928                 // reasons he killed himself
1929                 if(Player->flags & PLAYER_FLAGS_KILLED_SELF_SHOCKWAVE){
1930                         sprintf(death_text, XSTR( "You have killed yourself with a shockwave from your own weapon", 1421));                     
1931                 }
1932                 else if(Player->flags & PLAYER_FLAGS_KILLED_SELF_MISSILES){
1933                         sprintf(death_text, XSTR( "You have killed yourself with your own missiles", 1422));                    
1934                 } else {
1935                         sprintf(death_text, XSTR( "You have killed yourself", 100));
1936                 }
1937
1938                 Player->flags &= ~(PLAYER_FLAGS_KILLED_SELF_MISSILES | PLAYER_FLAGS_KILLED_SELF_SHOCKWAVE);
1939         } else {
1940                 player_generate_death_text( Player, death_text );
1941         }
1942
1943         HUD_fixed_printf(30.0f, death_text);
1944 }
1945
1946
1947 extern void ai_fire_from_turret(ship *shipp, ship_subsys *ss, int parent_objnum);
1948
1949 // maybe fire a turret that is on a player ship (single or multi)
1950 void player_maybe_fire_turret(object *objp)
1951 {
1952         model_subsystem *psub;
1953         ship_subsys             *pss;
1954
1955         ship                    *shipp = &Ships[objp->instance];
1956         ai_info                 *aip = &Ai_info[shipp->ai_index];
1957         ship_info               *sip = &Ship_info[shipp->ship_info_index];
1958
1959         // do a quick out if this isn't a bomber
1960         if ( !(sip->flags & SIF_BOMBER) ) {
1961                 return;
1962         }
1963
1964         if (aip->ai_flags & (AIF_AWAITING_REPAIR | AIF_BEING_REPAIRED)) {
1965                 if (aip->dock_objnum > -1) {
1966                         if (vm_vec_dist_quick(&objp->pos, &Objects[aip->dock_objnum].pos) < (objp->radius + Objects[aip->dock_objnum].radius) * 1.25f)
1967                                 return;
1968                 }
1969         }
1970
1971         // See if there are any turrets on the ship, if so see if they should fire
1972         for ( pss = GET_FIRST(&shipp->subsys_list); pss !=END_OF_LIST(&shipp->subsys_list); pss = GET_NEXT(pss) ) {
1973
1974                 if ( pss->current_hits <= 0.0f ) 
1975                         continue;
1976
1977                 psub = pss->system_info;
1978
1979                 if ( psub->type == SUBSYSTEM_TURRET ) {
1980                         if ( psub->turret_num_firing_points > 0 ) {
1981                                 ai_fire_from_turret(shipp, pss, OBJ_INDEX(objp));
1982                         }
1983                 }
1984         }
1985 }
1986
1987 void player_set_next_all_alone_msg_timestamp()
1988 {
1989         Player->check_for_all_alone_msg=timestamp(30000);
1990 }
1991
1992 // maybe play message from Terran Command 'You're all alone now Alpha 1'
1993 void player_maybe_play_all_alone_msg()
1994 {
1995         if ( Game_mode & GM_MULTIPLAYER ){
1996                 return;
1997         }
1998
1999         if ( !Player_all_alone_msg_inited ) {
2000                 player_init_all_alone_msg();
2001                 Player_all_alone_msg_inited=1;
2002                 return;
2003         }
2004
2005         if ( Player->flags & PLAYER_FLAGS_NO_CHECK_ALL_ALONE_MSG ) {
2006                 return;
2007         }
2008
2009         // only check every N seconds
2010         if ( !timestamp_elapsed(Player->check_for_all_alone_msg) ) {
2011                 return;
2012         }
2013
2014         player_set_next_all_alone_msg_timestamp();
2015         
2016         // at least one primary objective must be not complete (but not failed)
2017         if ( !mission_goals_incomplete(PRIMARY_GOAL) ) {
2018                 Player->flags |= PLAYER_FLAGS_NO_CHECK_ALL_ALONE_MSG;
2019                 return;
2020         }
2021
2022         // there must be no reinforcements available, hold off on message
2023         if ( (Player_ship != NULL) && hud_squadmsg_reinforcements_available(Player_ship->team) ) {
2024                 return;
2025         }
2026
2027         // there must be no ships present that are on the same team as the player
2028         ship_obj *so;
2029         object  *objp;
2030
2031         for ( so = GET_FIRST(&Ship_obj_list); so != END_OF_LIST(&Ship_obj_list); so = GET_NEXT(so) ) {
2032                 objp = &Objects[so->objnum];
2033
2034                 if ( objp == Player_obj ) {
2035                         continue;
2036                 }
2037
2038                 if ( Ships[objp->instance].team == Player_ship->team ) {
2039                         if ( !(Ship_info[Ships[objp->instance].ship_info_index].flags & SIF_HARMLESS) ) {
2040                                 return;
2041                         }
2042                 }
2043         }
2044
2045         // met all the requirements, now only play 50% of the time :)
2046         if ( rand()&1 ) {
2047                 message_send_builtin_to_player(MESSAGE_ALL_ALONE, NULL, MESSAGE_PRIORITY_HIGH, MESSAGE_TIME_ANYTIME, 0, 0, -1, -1);
2048         }
2049         Player->flags |= PLAYER_FLAGS_NO_CHECK_ALL_ALONE_MSG;
2050
2051
2052
2053 void player_set_padlock_state()
2054 {
2055         // clear padlock views
2056         Viewer_mode &= ~(VM_PADLOCK_ANY);
2057
2058         if ( check_control(PADLOCK_UP) ) {
2059                 Viewer_mode |= VM_PADLOCK_UP;
2060                 return;
2061         }
2062
2063         if ( check_control(PADLOCK_DOWN) ) {
2064                 Viewer_mode |= VM_PADLOCK_REAR;
2065                 return;
2066         }
2067
2068         if ( check_control(PADLOCK_RIGHT) ) {
2069                 Viewer_mode |= VM_PADLOCK_RIGHT;
2070                 return;
2071         }
2072
2073         if ( check_control(PADLOCK_LEFT) ) {
2074                 Viewer_mode |= VM_PADLOCK_LEFT;
2075                 return;
2076         }
2077 }
2078
2079 void player_get_padlock_orient(matrix *eye_orient)
2080 {
2081         Assert(Viewer_mode & VM_PADLOCK_ANY);
2082
2083         matrix old_eye_orient;
2084         old_eye_orient = *eye_orient;
2085
2086         if ( Viewer_mode & VM_PADLOCK_UP ) {
2087                 eye_orient->fvec = old_eye_orient.uvec;
2088                 vm_vec_copy_scale( &eye_orient->uvec, &old_eye_orient.fvec, -1.0f );
2089         } else if ( Viewer_mode & VM_PADLOCK_REAR ) {
2090                 vm_vec_negate(&eye_orient->fvec);
2091                 vm_vec_negate(&eye_orient->rvec);
2092         } else if ( Viewer_mode & VM_PADLOCK_LEFT ) {
2093                 vm_vec_copy_scale( &eye_orient->fvec, &old_eye_orient.rvec, -1.0f );
2094                 eye_orient->rvec = old_eye_orient.fvec;
2095         } else if ( Viewer_mode & VM_PADLOCK_RIGHT ) {
2096                 eye_orient->fvec = old_eye_orient.rvec;
2097                 vm_vec_copy_scale( &eye_orient->rvec, &old_eye_orient.fvec, -1.0f );
2098         } else {
2099                 Int3();
2100         }
2101 }
2102
2103 void player_display_packlock_view()
2104 {
2105         int padlock_view_index=0;
2106
2107         if ( Viewer_mode & VM_PADLOCK_UP ) {
2108                 padlock_view_index = 0;
2109         } else if ( Viewer_mode & VM_PADLOCK_REAR ) {
2110                 padlock_view_index = 1;
2111         } else if ( Viewer_mode & VM_PADLOCK_LEFT ) {
2112                 padlock_view_index = 2;
2113         } else if ( Viewer_mode & VM_PADLOCK_RIGHT ) {
2114                 padlock_view_index = 3;
2115         } else {
2116                 Int3();
2117                 return;
2118         }
2119
2120         char    str[128];
2121
2122         if ( !(Viewer_mode & (VM_CHASE|VM_EXTERNAL|VM_SLEWED)) ) {
2123                 switch (padlock_view_index) {
2124                 case 0:
2125                         strcpy(str, XSTR( "top view", 101));    break;
2126                 case 1:
2127                         strcpy(str, XSTR( "rear view", 102));   break;
2128                 case 2:
2129                         strcpy(str, XSTR( "left view", 103));   break;
2130                 case 3:
2131                         strcpy(str, XSTR( "right view", 104));  break;
2132                         }
2133
2134                 HUD_fixed_printf(0.01f, str);
2135         }
2136 }
2137
2138 // get the player's eye position and orient
2139 // NOTE : this is mostly just copied from game_render_frame_setup()
2140 extern vector Dead_player_last_vel;
2141 extern vector Camera_pos;
2142 extern void compute_slew_matrix(matrix *orient, angles *a);
2143 #define MIN_DIST_TO_DEAD_CAMERA                 50.0f
2144 void player_get_eye(vector *eye_pos, matrix *eye_orient)
2145 {
2146         object *viewer_obj;
2147         vector eye_dir;
2148
2149         // if the player object is NULL, return
2150         if(Player_obj == NULL){
2151                 return;
2152         }
2153
2154         // standalone servers can bail here
2155         if(Game_mode & GM_STANDALONE_SERVER){
2156                 return;
2157         }
2158
2159         // if we're not in-mission, don't do this
2160         if(!(Game_mode & GM_IN_MISSION)){
2161                 return;
2162         }
2163
2164         Assert(eye_pos != NULL);
2165         Assert(eye_orient != NULL);
2166
2167         if (Game_mode & GM_DEAD) {
2168                 vector  vec_to_deader, view_pos;
2169                 float           dist;           
2170                 if (Player_ai->target_objnum != -1) {
2171                         int view_from_player = 1;
2172
2173                         if (Viewer_mode & VM_OTHER_SHIP) {
2174                                 //      View from target.
2175                                 viewer_obj = &Objects[Player_ai->target_objnum];
2176                                 if ( viewer_obj->type == OBJ_SHIP ) {
2177                                         ship_get_eye( eye_pos, eye_orient, viewer_obj );
2178                                         view_from_player = 0;
2179                                 }
2180                         }
2181
2182                         if ( view_from_player ) {
2183                                 //      View target from player ship.
2184                                 viewer_obj = NULL;
2185                                 *eye_pos = Player_obj->pos;
2186                                 vm_vec_normalized_dir(&eye_dir, &Objects[Player_ai->target_objnum].pos, eye_pos);
2187                                 vm_vector_2_matrix(eye_orient, &eye_dir, NULL, NULL);
2188                         }
2189                 } else {
2190                         dist = vm_vec_normalized_dir(&vec_to_deader, &Player_obj->pos, &Dead_camera_pos);
2191                         
2192                         if (dist < MIN_DIST_TO_DEAD_CAMERA){
2193                                 dist += flFrametime * 16.0f;
2194                         }
2195
2196                         vm_vec_scale(&vec_to_deader, -dist);
2197                         vm_vec_add(&Dead_camera_pos, &Player_obj->pos, &vec_to_deader);
2198                         
2199                         view_pos = Player_obj->pos;
2200
2201                         if (!(Game_mode & GM_DEAD_BLEW_UP)) {                                                           
2202                         } else if (Player_ai->target_objnum != -1) {
2203                                 view_pos = Objects[Player_ai->target_objnum].pos;
2204                         } else {
2205                                 //      Make camera follow explosion, but gradually slow down.
2206                                 vm_vec_scale_add2(&Player_obj->pos, &Dead_player_last_vel, flFrametime);
2207                                 view_pos = Player_obj->pos;                             
2208                         }
2209
2210                         *eye_pos = Dead_camera_pos;
2211
2212                         vm_vec_normalized_dir(&eye_dir, &Player_obj->pos, eye_pos);
2213
2214                         vm_vector_2_matrix(eye_orient, &eye_dir, NULL, NULL);
2215                         viewer_obj = NULL;
2216                 }
2217         } 
2218         
2219         //      If already blown up, these other modes can override.
2220         if (!(Game_mode & (GM_DEAD | GM_DEAD_BLEW_UP))) {
2221                 viewer_obj = Player_obj;
2222  
2223                 if (Viewer_mode & VM_OTHER_SHIP) {
2224                         if (Player_ai->target_objnum != -1){
2225                                 viewer_obj = &Objects[Player_ai->target_objnum];
2226                         } 
2227                 }
2228
2229                 if (Viewer_mode & VM_EXTERNAL) {
2230                         matrix  tm, tm2;
2231
2232                         vm_angles_2_matrix(&tm2, &Viewer_external_info.angles);
2233                         vm_matrix_x_matrix(&tm, &viewer_obj->orient, &tm2);
2234
2235                         vm_vec_scale_add(eye_pos, &viewer_obj->pos, &tm.fvec, 2.0f * viewer_obj->radius + Viewer_external_info.distance);
2236
2237                         vm_vec_sub(&eye_dir, &viewer_obj->pos, eye_pos);
2238                         vm_vec_normalize(&eye_dir);
2239                         vm_vector_2_matrix(eye_orient, &eye_dir, &viewer_obj->orient.uvec, NULL);
2240                         viewer_obj = NULL;
2241
2242                         //      Modify the orientation based on head orientation.
2243                         compute_slew_matrix(eye_orient, &Viewer_slew_angles);
2244                 } else if ( Viewer_mode & VM_CHASE ) {
2245                         vector  move_dir;
2246
2247                         if ( viewer_obj->phys_info.speed < 0.1 ){
2248                                 move_dir = viewer_obj->orient.fvec;
2249                         } else {
2250                                 move_dir = viewer_obj->phys_info.vel;
2251                                 vm_vec_normalize_safe(&move_dir);
2252                         }
2253
2254                         vm_vec_scale_add(eye_pos, &viewer_obj->pos, &move_dir, -3.0f * viewer_obj->radius - Viewer_chase_info.distance);
2255                         vm_vec_scale_add2(eye_pos, &viewer_obj->orient.uvec, 0.75f * viewer_obj->radius);
2256                         vm_vec_sub(&eye_dir, &viewer_obj->pos, eye_pos);
2257                         vm_vec_normalize(&eye_dir);
2258
2259                         // JAS: I added the following code because if you slew up using
2260                         // Descent-style physics, eye_dir and Viewer_obj->orient.uvec are
2261                         // equal, which causes a zero-length vector in the vm_vector_2_matrix
2262                         // call because the up and the forward vector are the same.   I fixed
2263                         // it by adding in a fraction of the right vector all the time to the
2264                         // up vector.
2265                         vector tmp_up = viewer_obj->orient.uvec;
2266                         vm_vec_scale_add2( &tmp_up, &viewer_obj->orient.rvec, 0.00001f );
2267
2268                         vm_vector_2_matrix(eye_orient, &eye_dir, &tmp_up, NULL);
2269                         viewer_obj = NULL;
2270
2271                         //      Modify the orientation based on head orientation.
2272                         compute_slew_matrix(eye_orient, &Viewer_slew_angles);
2273                 } else if ( Viewer_mode & VM_WARP_CHASE ) {
2274                         *eye_pos = Camera_pos;
2275
2276                         ship * shipp = &Ships[Player_obj->instance];
2277
2278                         vm_vec_sub(&eye_dir, &shipp->warp_effect_pos, eye_pos);
2279                         vm_vec_normalize(&eye_dir);
2280                         vm_vector_2_matrix(eye_orient, &eye_dir, &Player_obj->orient.uvec, NULL);
2281                         viewer_obj = NULL;
2282                 } else {
2283                         // get an eye position based upon the correct type of object
2284                         switch(viewer_obj->type){
2285                         case OBJ_SHIP:
2286                                 // make a call to get the eye point for the player object
2287                                 ship_get_eye( eye_pos, eye_orient, viewer_obj );
2288                                 break;
2289                         case OBJ_OBSERVER:
2290                                 // make a call to get the eye point for the player object
2291                                 observer_get_eye( eye_pos, eye_orient, viewer_obj );                            
2292                                 break;
2293                         default :
2294                                 Int3();
2295                         }                       
2296                 }
2297         }
2298 }