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