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