-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathplayercontrol.cpp
More file actions
2235 lines (1880 loc) · 67.5 KB
/
Copy pathplayercontrol.cpp
File metadata and controls
2235 lines (1880 loc) · 67.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include "autopilot/autopilot.h"
#include "camera/camera.h"
#include "controlconfig/controlsconfig.h"
#include "debugconsole/console.h"
#include "freespace.h"
#include "gamesequence/gamesequence.h"
#include "gamesnd/gamesnd.h"
#include "globalincs/linklist.h"
#include "hud/hud.h"
#include "hud/hudmessage.h"
#include "hud/hudsquadmsg.h"
#include "hud/hudtargetbox.h"
#include "io/joy.h"
#include "io/joy_ff.h"
#include "io/mouse.h"
#include "io/timer.h"
#include "headtracking/headtracking.h"
#include "mission/missiongoals.h"
#include "mission/missionmessage.h"
#include "network/multi_obj.h"
#include "network/multiutil.h"
#include "object/object.h"
#include "object/objectdock.h"
#include "observer/observer.h"
#include "parse/parselo.h"
#include "playerman/player.h"
#include "ship/ship.h"
#include "ship/shipfx.h"
#include "weapon/weapon.h"
#ifndef NDEBUG
#include "io/key.h"
#endif
////////////////////////////////////////////////////////////
// Global object and other interesting player type things
////////////////////////////////////////////////////////////
player Players[MAX_PLAYERS];
int Player_num;
player *Player = NULL;
// Goober5000
int Player_use_ai = 0;
int lua_game_control = 0;
physics_info Descent_physics; // used when we want to control the player like the descent ship
angles chase_slew_angles;
int toggle_glide = 0;
int press_glide = 0;
////////////////////////////////////////////////////////////
// Module data
////////////////////////////////////////////////////////////
static int Player_all_alone_msg_inited=0; // flag used for initializing a player-specific voice msg
#ifndef NDEBUG
int Show_killer_weapon = 0;
DCF_BOOL( show_killer_weapon, Show_killer_weapon )
#endif
/**
* @brief Reads and combines the axes from the player's joystick and mouse.
* @param[out] axis Pointer to axes array
* @param[in] frame_time The frame time when this was called
*/
void playercontrol_read_stick(int *axis, float frame_time);
/**
* @brief Slew angles chase towards a value like they're on a spring.
*
* @param[in,out] ap The current view angles. Is modified towards target angles.
* @param[in] bp The target view angles
*
* @details When furthest away, move fastest. Minimum speed set so that doesn't take too long. When gets close, clamps to the value.
*/
void chase_angles_to_value(angles *ap, angles *bp, int scale)
{
float sk;
angles delta;
// Make sure we actually need to do all this math.
if ((ap->p == bp->p) && (ap->h == bp->h))
return;
sk = 1.0f - scale*flRealframetime;
CLAMP(sk, 0.0f, 1.0f);
delta.p = ap->p - bp->p;
delta.h = ap->h - bp->h;
// If we're very close, put ourselves at goal.
if ((fl_abs(delta.p) < 0.005f) && (fl_abs(delta.h) < 0.005f)) {
ap->p = bp->p;
ap->h = bp->h;
} else {
// Else, apply the changes
ap->p -= (delta.p * (1.0f - sk));
ap->h -= (delta.h * (1.0f - sk));
}
}
/**
* @brief Resets the given angles to 0.0f without delay
*
* @param[in,out] Angles to reset
*/
void reset_angles(angles *ap) {
ap->p = 0.0f;
ap->h = 0.0f;
ap->b = 0.0f;
}
angles Viewer_slew_angles_delta;
angles Viewer_external_angles_delta;
/**
* @brief Modifies the camera veiw angles according to its current view mode: External, External Locked,
* TrackIR, Freelook (Unlocked), Normal (Locked), and Centering
*
* @param[in,out] ma The camera veiw angles to modify (magnitude is saturated to be within max_p and max_h).
* @param[out] da The delta angles applied to ma (magnitude is saturated to 1 radian).
* @param[in] max_p The maximum pitch magnitude ma may have (radians).
* @param[in] max_h The maximum heading magnitude ma may have (radians).
* @param[in] frame_time The frame time at which this function is called.
*/
void view_modify(angles *ma, angles *da, float max_p, float max_h, float frame_time)
{
// Digital inputs
float t = 0;
float u = 0;
// Analog inputs
int axis[NUM_JOY_AXIS_ACTIONS];
float h = 0.0f;
float p = 0.0f;
if (Viewer_mode & VM_CENTERING) {
// Center view then bail
ma->p = 0.0f;
ma->h = 0.0f;
ma->b = 0.0f;
return;
} else if (Viewer_mode & VM_CAMERA_LOCKED) {
if (Viewer_mode & VM_EXTERNAL) {
// External camera is locked in place, nothing to do here
return;
}
if (Viewer_mode & VM_PADLOCK_ANY) {
// Do Padlock view then bail
if (Viewer_mode & VM_PADLOCK_UP) {
ma->h = 0.0f;
ma->p = -max_p;
} else if (Viewer_mode & VM_PADLOCK_REAR) {
ma->h = -PI;
ma->p = 0.0f;
} else if (Viewer_mode & VM_PADLOCK_RIGHT) {
ma->h = max_h;
ma->p = 0.0f;
} else if (Viewer_mode & VM_PADLOCK_LEFT) {
ma->h = -max_h;
ma->p = 0.0f;
} // Else, don't do any ajustments. player_set_padlock_state will reset the states
return;
} else if (headtracking::isEnabled()) {
// Do TrackIR
vec3d trans = ZERO_VECTOR;
headtracking::query();
headtracking::HeadTrackingStatus* status = headtracking::getStatus();
ma->h = -PI2*(status->yaw);
ma->p = PI2*(status->pitch);
trans.xyz.x = -0.4f*status->x;
trans.xyz.y = 0.4f*status->y;
trans.xyz.z = -status->z;
if (trans.xyz.z < 0) {
trans.xyz.z = 0.0f;
}
vm_vec_unrotate(&leaning_position,&trans,&Eye_matrix);
} else {
// Do slew
/* These have been commented out until the controls are added into Controls_config[]
t = (check_control_timef(SLEW_LEFT) - check_control_timef(SLEW_RIGHT));
u = (check_control_timef(SLEW_UP) - check_control_timef(SLEW_DOWN);
*/
} // Else, don't do any slewing
} else {
// Camera is unlocked - Pitch and Yaw axes control X and Y slew axes
t = (check_control_timef(YAW_LEFT) - check_control_timef(YAW_RIGHT));
u = (check_control_timef(PITCH_BACK) - check_control_timef(PITCH_FORWARD));
playercontrol_read_stick(axis, frame_time);
h = -f2fl(axis[JOY_HEADING_AXIS]);
p = -f2fl(axis[JOY_PITCH_AXIS]);
}
// Combine Analog and Digital slew commands
da->h = 0.0f;
da->p = 0.0f;
da->b = 0.0f;
if (t != 0.0f) {
da->h += t;
}
if (h != 0.0f) {
da->h += h;
}
if (u != 0.0f) {
da->p += u;
}
if (p != 0.0f) {
da->p += p;
}
// Clamp deltas to be within 1 radian
CLAMP(da->h, -1.0f, 1.0f);
CLAMP(da->p, -1.0f, 1.0f);
// Apply view modifications to camera
if ((Game_time_compression >= F1_0) && !(Viewer_mode & VM_EXTERNAL)) {
ma->p += 2*da->p * flFrametime;
ma->b += 2*da->b * flFrametime;
ma->h += 2*da->h * flFrametime;
} else {
//If time compression is less than normal, still move camera at same speed
//This gives a cool matrix effect
ma->p += da->p * flRealframetime;
ma->b += da->b * flRealframetime;
ma->h += da->h * flRealframetime;
}
// Clamp resulting angles to their maximums
CLAMP(ma->p, -max_p, max_p);
CLAMP(ma->h, -max_h, max_h);
}
void do_view_track_target(float frame_time)
{
vec3d view_vector;
vec3d targetpos_rotated;
vec3d playerpos_rotated;
vec3d forwardvec_rotated;
vec3d target_pos;
angles view_angles;
angles forward_angles;
if ((Player_ai->target_objnum == -1) || (Viewer_mode & VM_OTHER_SHIP)) {
// If the object isn't targeted or we're viewing from the target's perspective, center the view and turn off target padlock
// because the target won't be at the angle we've calculated from the player's perspective.
Viewer_mode ^= VM_TRACK;
chase_slew_angles.p = 0.0f;
chase_slew_angles.h = 0.0f;
return;
}
object * targetp = &Objects[Player_ai->target_objnum];
// check to see if there is even a current target. if not, switch off the
// target padlock tracking flag, make the camera slew to the center,
// and exit the procedure
if ( targetp == &obj_used_list ) {
Viewer_mode ^= VM_TRACK;
chase_slew_angles.p = 0.0f;
chase_slew_angles.h = 0.0f;
return;
}
// look at a subsystem if there is one.
if ( Player_ai->targeted_subsys != NULL ) {
get_subsystem_world_pos(targetp, Player_ai->targeted_subsys, &target_pos);
} else {
target_pos = targetp->pos;
}
vm_vec_rotate(&targetpos_rotated, &target_pos, &Player_obj->orient);
vm_vec_rotate(&playerpos_rotated, &Player_obj->pos, &Player_obj->orient);
vm_vec_rotate(&forwardvec_rotated, &Player_obj->orient.vec.fvec, &Player_obj->orient);
vm_vec_normalized_dir(&view_vector,&targetpos_rotated,&playerpos_rotated);
vm_extract_angles_vector(&view_angles,&view_vector);
vm_extract_angles_vector(&forward_angles,&forwardvec_rotated);
chase_slew_angles.h = forward_angles.h - view_angles.h;
chase_slew_angles.p = -(forward_angles.p - view_angles.p);
// the gimbal limits of the player's virtual neck.
// These nested ifs prevent the player from looking up and
// down beyond 90 degree angles.
if (chase_slew_angles.p > PI_2)
chase_slew_angles.p = PI_2;
else if (chase_slew_angles.p < -PI_2)
chase_slew_angles.p = -PI_2;
// prevents the player from looking completely behind himself; just over his shoulder
if (chase_slew_angles.h > PI2/3)
chase_slew_angles.h = PI2/3;
else if (chase_slew_angles.h < -PI2/3)
chase_slew_angles.h = -PI2/3;
}
/**
* @brief When VIEW_SLEW is pressed, pitch and heading axes controls viewer direction slewing.
*
* @param[in] frame_time The frame time at which this function is called
*
* @details Prevents the player's "head" from swiveling unrealistacally far with a max_p of Pi/2 and a max_h of2/3 Pi.
*
* @note Some mods may prefer to set their own limits, so, maybe make this as a table option in the future
*/
void do_view_slew(float frame_time)
{
view_modify(&chase_slew_angles, &Viewer_slew_angles_delta, PI_2, PI2/3, frame_time);
// Check Track target
if (Viewer_mode & VM_TRACK) {
// Player's vision will track current target.
do_view_track_target(frame_time);
Viewer_mode |= VM_CAMERA_LOCKED;
return;
}
// Check Padlock controls
if (check_control(PADLOCK_UP)) {
Viewer_mode |= (VM_PADLOCK_UP | VM_CAMERA_LOCKED);
Viewer_mode &= ~(VM_CENTERING);
return;
} else if (check_control(PADLOCK_DOWN)) {
Viewer_mode |= (VM_PADLOCK_REAR | VM_CAMERA_LOCKED);
Viewer_mode &= ~(VM_CENTERING);
return;
} else if (check_control(PADLOCK_RIGHT)) {
Viewer_mode |= (VM_PADLOCK_RIGHT | VM_CAMERA_LOCKED);
Viewer_mode &= ~(VM_CENTERING);
return;
} else if (check_control(PADLOCK_LEFT)) {
Viewer_mode |= (VM_PADLOCK_LEFT | VM_CAMERA_LOCKED);
Viewer_mode &= ~(VM_CENTERING);
return;
} else if (Viewer_mode & VM_PADLOCK_ANY) {
// clear padlock views and center the view once
// the player lets go of a padlock control
Viewer_mode &= ~(VM_PADLOCK_ANY);
Viewer_mode |= (VM_CENTERING | VM_CAMERA_LOCKED);
}
if (Viewer_mode & VM_CENTERING) {
// If we're centering the view, check to see if we're actually centered and bypass any view modifications
// until the view has finally been centered.
if ((Viewer_slew_angles.h == 0.0f) && (Viewer_slew_angles.p == 0.0f)) {
// View has been centered, allow the player to freelook again.
Viewer_mode &= ~VM_CENTERING;
}
Viewer_mode |= VM_CAMERA_LOCKED;
}
if (!(Viewer_mode & VM_PADLOCK_ANY)) {
if (headtracking::isEnabled()) {
// Can't do slewing if TrackIR is enabled
Viewer_mode |= VM_CAMERA_LOCKED;
return;
}
if (check_control_timef(VIEW_SLEW)) {
// Enable freelook mode
Viewer_mode &= ~VM_CAMERA_LOCKED;
} else if (check_control_timef(VIEW_CENTER) || !(Viewer_mode & VM_CAMERA_LOCKED)) {
// Start centering the view if:
// VIEW_CENTER was pressed, or
// The player let go of VIEW_SLEW
Viewer_mode |= (VM_CENTERING | VM_CAMERA_LOCKED);
}
}
}
void do_view_chase(float frame_time)
{
float t;
if (Viewer_mode & VM_TRACK) {
// Snap back to zero and disable target tracking
reset_angles(&Viewer_slew_angles);
reset_angles(&chase_slew_angles);
Viewer_mode &= ~VM_TRACK;
}
// Process centering key.
if (check_control_timef(VIEW_CENTER)) {
Viewer_chase_info.distance = 0.0f;
}
// Process distance +/- keys
t = check_control_timef(VIEW_DIST_INCREASE) - check_control_timef(VIEW_DIST_DECREASE);
Viewer_chase_info.distance += t*4;
if (Viewer_chase_info.distance < 0.0f)
Viewer_chase_info.distance = 0.0f;
Viewer_mode |= VM_CAMERA_LOCKED;
}
float camera_zoom_scale = 1.0f;
DCF(camera_speed, "Sets the camera zoom scale")
{
if (dc_optional_string_either("status", "--status") || dc_optional_string_either("?", "--?")) {
dc_printf("Camera zoom scale is %f\n", camera_zoom_scale);
return;
}
dc_stuff_float(&camera_zoom_scale);
dc_printf("Camera zoom scale set to %f\n", camera_zoom_scale);
}
void do_view_external(float frame_time)
{
float t;
if (Viewer_mode & VM_TRACK) {
// Snap back to zero and disable target tracking
reset_angles(&Viewer_slew_angles);
reset_angles(&chase_slew_angles);
Viewer_mode &= ~VM_TRACK;
}
view_modify(&Viewer_external_info.angles, &Viewer_external_angles_delta, PI2, PI2, frame_time);
// Process centering key.
if (check_control_timef(VIEW_CENTER)) {
Viewer_external_info.angles.p = 0.0f;
Viewer_external_info.angles.h = 0.0f;
Viewer_external_info.distance = 0.0f;
}
t = check_control_timef(VIEW_DIST_INCREASE) - check_control_timef(VIEW_DIST_DECREASE);
Viewer_external_info.distance += t*4*camera_zoom_scale;
if (Viewer_external_info.distance < 0.0f){
Viewer_external_info.distance = 0.0f;
}
// Do over-the-top correction.
if (Viewer_external_info.angles.p > PI)
Viewer_external_info.angles.p = -PI2 + Viewer_external_info.angles.p;
else if (Viewer_external_info.angles.p < -PI)
Viewer_external_info.angles.p = PI2 + Viewer_external_info.angles.p;
if (Viewer_external_info.angles.h > PI)
Viewer_external_info.angles.h = -PI2 + Viewer_external_info.angles.h;
else if (Viewer_external_info.angles.h < -PI)
Viewer_external_info.angles.h = PI2 + Viewer_external_info.angles.h;
}
/**
* Separate out the reading of thrust keys, so we can call this from external view as well as from normal view
*/
void do_thrust_keys(control_info *ci)
{
ci->forward = check_control_timef(FORWARD_THRUST) - check_control_timef(REVERSE_THRUST);
ci->sideways = (check_control_timef(RIGHT_SLIDE_THRUST) - check_control_timef(LEFT_SLIDE_THRUST));//for slideing-Bobboau
ci->vertical = (check_control_timef(UP_SLIDE_THRUST) - check_control_timef(DOWN_SLIDE_THRUST));//for slideing-Bobboau
}
/**
* Called by single and multiplayer modes to reset information inside of control info structure
*/
void player_control_reset_ci( control_info *ci )
{
float t1, t2, oldspeed;
t1 = ci->heading;
t2 = ci->pitch;
oldspeed = ci->forward_cruise_percent;
memset( ci, 0, sizeof(control_info) );
ci->heading = t1;
ci->pitch = t2;
ci->forward_cruise_percent = oldspeed;
}
/**
* @brief Reads the joystick Pitch, Bank, and Heading axes.
*
* @param[out] axis The axis array to store the scaled joystick position. (Must have a size of NUM_JOY_AXIS_ACTIONS)
* @param[in] frame_time The frame time at which this function is called.
*
* @details This is its own function because we only want to read it at a certain rate, since it takes time.
*/
void playercontrol_read_stick(int *axis, float frame_time)
{
int i;
// Read the stick
control_get_axes_readings(&axis[0], &axis[1], &axis[2], &axis[3], &axis[4]);
if (Use_mouse_to_fly) {
int dx, dy, dz;
float factor;
factor = (float) Mouse_sensitivity + 1.77f;
factor = factor * factor / frame_time / 0.6f;
mouse_get_delta(&dx, &dy, &dz);
int x_axis, y_axis, z_axis;
x_axis = y_axis = z_axis = -1;
for (i = 0; i < NUM_JOY_AXIS_ACTIONS; i++) {
switch(Axis_map_to[i])
{
case JOY_X_AXIS:
x_axis = i;
break;
case JOY_Y_AXIS:
y_axis = i;
break;
case JOY_Z_AXIS:
z_axis = i;
break;
}
}
if (x_axis >= 0) {
if (Invert_axis[x_axis]) {
dx = -dx;
}
axis[x_axis] += (int) ((float) dx * factor);
}
if (y_axis >= 0) {
if (Invert_axis[y_axis]) {
dy = -dy;
}
axis[y_axis] += (int) ((float) dy * factor);
}
if (z_axis >= 0) {
if (Invert_axis[z_axis]) {
dz = -dz;
}
axis[z_axis] += (int) ((float) dz * factor);
}
}
}
void read_keyboard_controls( control_info * ci, float frame_time, physics_info *pi )
{
float kh=0.0f, scaled, newspeed, delta, oldspeed;
int axis[NUM_JOY_AXIS_ACTIONS];
static int afterburner_last = 0;
static float analog_throttle_last = 9e9f;
static int override_analog_throttle = 0;
static float savedspeed = ci->forward_cruise_percent; //Backslash
int centering_speed = 7; // the scale speed in which the camera will smoothly center when the player presses Center View
oldspeed = ci->forward_cruise_percent;
player_control_reset_ci( ci );
// Camera & View controls
if ( Viewer_mode & VM_EXTERNAL ) {
control_used(VIEW_EXTERNAL);
do_view_external(frame_time);
do_thrust_keys(ci);
} else if ( Viewer_mode & VM_CHASE ) {
do_view_chase(frame_time);
} else {
// We're in the cockpit.
do_view_slew(frame_time);
}
chase_angles_to_value(&Viewer_slew_angles, &chase_slew_angles, centering_speed);
// Ship controls
if (Viewer_mode & VM_CAMERA_LOCKED) {
// From keyboard...
do_thrust_keys(ci);
if ( check_control(BANK_WHEN_PRESSED) ) {
ci->bank = check_control_timef(BANK_LEFT) + check_control_timef(YAW_LEFT) - check_control_timef(YAW_RIGHT) - check_control_timef(BANK_RIGHT);
ci->heading = 0.0f;
} else {
kh = (check_control_timef(YAW_RIGHT) - check_control_timef(YAW_LEFT)) / 8.0f;
if (kh == 0.0f) {
ci->heading = 0.0f;
} else if (kh > 0.0f) {
if (ci->heading < 0.0f)
ci->heading = 0.0f;
} else { // kh < 0
if (ci->heading > 0.0f)
ci->heading = 0.0f;
}
ci->bank = check_control_timef(BANK_LEFT) - check_control_timef(BANK_RIGHT);
}
ci->heading += kh;
kh = (check_control_timef(PITCH_FORWARD) - check_control_timef(PITCH_BACK)) / 8.0f;
if (kh == 0.0f) {
ci->pitch = 0.0f;
} else if (kh > 0.0f) {
if (ci->pitch < 0.0f)
ci->pitch = 0.0f;
} else { // kh < 0
if (ci->pitch > 0.0f)
ci->pitch = 0.0f;
}
ci->pitch += kh;
}
if (!(Game_mode & GM_DEAD)) {
if ( button_info_query(&Player->bi, ONE_THIRD_THROTTLE) ) {
control_used(ONE_THIRD_THROTTLE);
player_clear_speed_matching();
if ( Player->ci.forward_cruise_percent < 33.3f ) {
snd_play( &Snds[ship_get_sound(Player_obj, SND_THROTTLE_UP)], 0.0f );
} else if ( Player->ci.forward_cruise_percent > 33.3f ) {
snd_play( &Snds[ship_get_sound(Player_obj, SND_THROTTLE_DOWN)], 0.0f );
}
Player->ci.forward_cruise_percent = 33.3f;
override_analog_throttle = 1;
}
if ( button_info_query(&Player->bi, TWO_THIRDS_THROTTLE) ) {
control_used(TWO_THIRDS_THROTTLE);
player_clear_speed_matching();
if ( Player->ci.forward_cruise_percent < 66.6f ) {
snd_play( &Snds[ship_get_sound(Player_obj, SND_THROTTLE_UP)], 0.0f );
} else if (Player->ci.forward_cruise_percent > 66.6f) {
snd_play( &Snds[ship_get_sound(Player_obj, SND_THROTTLE_DOWN)], 0.0f );
}
Player->ci.forward_cruise_percent = 66.6f;
override_analog_throttle = 1;
}
if ( button_info_query(&Player->bi, PLUS_5_PERCENT_THROTTLE) ) {
control_used(PLUS_5_PERCENT_THROTTLE);
Player->ci.forward_cruise_percent += 5.0f;
if (Player->ci.forward_cruise_percent > 100.0f)
Player->ci.forward_cruise_percent = 100.0f;
}
if ( button_info_query(&Player->bi, MINUS_5_PERCENT_THROTTLE) ) {
control_used(MINUS_5_PERCENT_THROTTLE);
Player->ci.forward_cruise_percent -= 5.0f;
if (Player->ci.forward_cruise_percent < 0.0f)
Player->ci.forward_cruise_percent = 0.0f;
}
if ( button_info_query(&Player->bi, ZERO_THROTTLE) ) {
control_used(ZERO_THROTTLE);
player_clear_speed_matching();
if ( ci->forward_cruise_percent > 0.0f && Player_obj->phys_info.fspeed > 0.5) {
snd_play( &Snds[ship_get_sound(Player_obj, SND_ZERO_THROTTLE)], 0.0f );
}
ci->forward_cruise_percent = 0.0f;
override_analog_throttle = 1;
}
if ( button_info_query(&Player->bi, MAX_THROTTLE) ) {
control_used(MAX_THROTTLE);
player_clear_speed_matching();
if ( ci->forward_cruise_percent < 100.0f ) {
snd_play( &Snds[ship_get_sound(Player_obj, SND_FULL_THROTTLE)], 0.0f );
}
ci->forward_cruise_percent = 100.0f;
override_analog_throttle = 1;
}
// allow a minimum speed to be set for the player ship (only)
if ( Player_ship != nullptr && Player_ship->ship_info_index >= 0 ) {
float z_min = Ship_info[Player_ship->ship_info_index].min_vel.xyz.z;
if (z_min > 0.0f && pi->speed <= z_min) {
ci->forward = MAX(ci->forward, 0.0f);
ci->forward_cruise_percent = MAX(ci->forward_cruise_percent, z_min / Ship_info[Player_ship->ship_info_index].max_vel.xyz.z * 100.0f);
override_analog_throttle = 1;
}
}
// AL 12-29-97: If afterburner key is down, player should have full forward thrust (even if afterburners run out)
if ( check_control(AFTERBURNER) ) {
ci->forward = 1.0f;
}
if ( check_control(REVERSE_THRUST) && check_control(AFTERBURNER) ) {
ci->forward = -pi->max_rear_vel * 1.0f;
}
if ( Player->flags & PLAYER_FLAGS_MATCH_TARGET ) {
if ( (Player_ai->last_target == Player_ai->target_objnum) && (Player_ai->target_objnum != -1) && ( ci->forward_cruise_percent == oldspeed) ) {
float tspeed, pmax_speed;
object *targeted_objp = &Objects[Player_ai->target_objnum];
tspeed = targeted_objp->phys_info.speed; //changed from fspeed. If target is reversing, sliding, or gliding we still want to keep up. -- Backslash
// maybe need to get speed from docked partner
if ( tspeed < MATCH_SPEED_THRESHOLD ) {
Assert(targeted_objp->type == OBJ_SHIP);
// Goober5000
if (object_is_docked(targeted_objp))
{
tspeed = dock_calc_docked_speed(targeted_objp); //changed from fspeed
}
}
// Note, if closer than 100 units, scale down speed a bit. Prevents repeated collisions. -- MK, 12/17/97
float dist = vm_vec_dist(&Player_obj->pos, &targeted_objp->pos);
if (dist < 100.0f) {
tspeed = tspeed * (0.5f + dist/200.0f);
}
//SUSHI: If gliding, don't do anything for speed matching
if (!( (Objects[Player->objnum].phys_info.flags & PF_GLIDING) || (Objects[Player->objnum].phys_info.flags & PF_FORCE_GLIDE) )) {
pmax_speed = Ships[Player_obj->instance].current_max_speed;
if (pmax_speed > 0.0f) {
ci->forward_cruise_percent = (tspeed / pmax_speed) * 100.0f;
} else {
ci->forward_cruise_percent = 0.0f;
}
override_analog_throttle = 1;
}
} else
Player->flags &= ~PLAYER_FLAGS_MATCH_TARGET;
}
// code to read joystick axis for pitch/heading. Code to read joystick buttons
// for bank.
if ( !(Game_mode & GM_DEAD) ) {
playercontrol_read_stick(axis, frame_time);
} else {
axis[0] = axis[1] = axis[2] = axis[3] = axis[4] = 0;
}
if (Viewer_mode & VM_CAMERA_LOCKED) {
// Player has control of the ship
if (Axis_map_to[JOY_HEADING_AXIS] >= 0) {
// check the heading on the x axis
if ( check_control(BANK_WHEN_PRESSED) ) {
delta = f2fl( axis[JOY_HEADING_AXIS] );
if ( (delta > 0.05f) || (delta < -0.05f) ) {
ci->bank -= delta;
}
} else {
ci->heading += f2fl( axis[JOY_HEADING_AXIS] );
}
}
// check the pitch on the y axis
if (Axis_map_to[JOY_PITCH_AXIS] >= 0) {
ci->pitch -= f2fl( axis[JOY_PITCH_AXIS] );
}
} else {
// Player has control of the camera
ci->pitch = 0.0f;
ci->heading = 0.0f;
}
if (Axis_map_to[JOY_BANK_AXIS] >= 0) {
ci->bank -= f2fl( axis[JOY_BANK_AXIS] ) * 1.5f;
}
// axis 2 is for throttle
if (Axis_map_to[JOY_ABS_THROTTLE_AXIS] >= 0) {
scaled = (float) axis[JOY_ABS_THROTTLE_AXIS] * 1.2f / (float) F1_0 - 0.1f; // convert to -0.1 - 1.1 range
oldspeed = ci->forward_cruise_percent;
newspeed = (1.0f - scaled) * 100.0f;
delta = analog_throttle_last - newspeed;
if (!override_analog_throttle || (delta < -1.5f) || (delta > 1.5f)) {
ci->forward_cruise_percent = newspeed;
analog_throttle_last = newspeed;
override_analog_throttle = 0;
}
}
if (Axis_map_to[JOY_REL_THROTTLE_AXIS] >= 0)
ci->forward_cruise_percent += f2fl(axis[JOY_REL_THROTTLE_AXIS]) * 100.0f * frame_time;
CLAMP(ci->forward_cruise_percent, 0, 100);
// set up the firing stuff. Read into control info ala Descent so that weapons will be
// created during the object simulation phase, and not immediately as was happening before.
//keyboard: fire the current primary weapon
if (check_control(FIRE_PRIMARY)) {
ci->fire_primary_count++;
}
// for debugging, check to see if the debug key is down -- if so, make fire the debug laser instead
#ifndef NDEBUG
if ( keyd_pressed[KEY_DEBUG_KEY] ) {
ci->fire_debug_count = ci->fire_primary_count;
ci->fire_primary_count = 0;
}
#endif
// keyboard: fire the current secondary weapon
if (check_control(FIRE_SECONDARY)) {
ci->fire_secondary_count++;
// if we're a multiplayer client, set our accum bits now
if( MULTIPLAYER_CLIENT && (Net_player != NULL)){
Net_player->s_info.accum_buttons |= OOC_FIRE_SECONDARY;
}
}
// keyboard: launch countermeasures, but not if AI controlling Player
if (button_info_query(&Player->bi, LAUNCH_COUNTERMEASURE) && !Player_use_ai) {
control_used(LAUNCH_COUNTERMEASURE);
ci->fire_countermeasure_count++;
hud_gauge_popup_start(HUD_CMEASURE_GAUGE);
}
// see if the afterburner has been started (keyboard + joystick)
if (check_control(AFTERBURNER) && !Player_use_ai) {
if (!afterburner_last) {
Assert(Player_ship);
if ( !(Ship_info[Player_ship->ship_info_index].flags[Ship::Info_Flags::Afterburner]) ) {
gamesnd_play_error_beep();
} else {
ci->afterburner_start = 1;
}
}
afterburner_last = 1;
} else {
if (afterburner_last)
ci->afterburner_stop = 1;
afterburner_last = 0;
}
// new gliding systems combining code by Backslash, Turey, Kazan, and WMCoolmon
// Check for toggle button pressed.
if ( button_info_query(&Player->bi, TOGGLE_GLIDING) ) {
control_used(TOGGLE_GLIDING);
if ( Player_obj != NULL && Ship_info[Player_ship->ship_info_index].can_glide ) {
toggle_glide = !toggle_glide;
}
}
// This logic is a bit tricky. It checks to see if the glide_when_pressed button is in a different state
// than press_glide. Since it sets press_glide equal to glide_when_pressed inside of this if statement,
// this only evaluates to true when the state of the button is different than it was last time.
if ( check_control(GLIDE_WHEN_PRESSED) != press_glide ) {
if ( Player_obj != NULL && Ship_info[Player_ship->ship_info_index].can_glide ) {
// This only works if check_control returns only 1 or 0. Shouldn't be a problem,
// but this comment's here just in case it is.
press_glide = !press_glide;
}
}
// if the player is warping out, cancel gliding
if (Player_ship->flags[Ship::Ship_Flags::Depart_warp]) {
toggle_glide = 0;
press_glide = 0;
}
// Do we want to be gliding?
if ( toggle_glide || press_glide ) {
// Probably don't need to do this check, but just in case...
if ( Player_obj != NULL && Ship_info[Player_ship->ship_info_index].can_glide ) {
// Only bother doing this if we need to.
if ( toggle_glide && press_glide ) {
// Overkill -- if gliding is toggled on and glide_when_pressed is pressed, turn glide off
if ( object_get_gliding(Player_obj) && !object_glide_forced(Player_obj) ) {
object_set_gliding(Player_obj, false);
ci->forward_cruise_percent = savedspeed;
press_glide = !press_glide;
snd_play( &Snds[ship_get_sound(Player_obj, SND_THROTTLE_UP)], 0.0f );
}
} else if ( !object_get_gliding(Player_obj) ) {
object_set_gliding(Player_obj, true);
savedspeed = ci->forward_cruise_percent;
ci->forward_cruise_percent = 0.0f;
override_analog_throttle = 1;
if (Ship_info[Player_ship->ship_info_index].glide_start_snd > 0) {
//If a custom glide start sound was specified, play it
snd_play( &Snds[Ship_info[Player_ship->ship_info_index].glide_start_snd], 0.0f );
} else {
//If glide_start_snd wasn't set (probably == 0), use the default throttle down sound
snd_play( &Snds[ship_get_sound(Player_obj, SND_THROTTLE_DOWN)], 0.0f );
}
}
}
} else {
// Probably don't need to do the second half of this check, but just in case...
if ( Player_obj != NULL && Ship_info[Player_ship->ship_info_index].can_glide ) {
// Only bother doing this if we need to.
if ( object_get_gliding(Player_obj) && !object_glide_forced(Player_obj) ) {
object_set_gliding(Player_obj, false);
ci->forward_cruise_percent = savedspeed;
if (Ship_info[Player_ship->ship_info_index].glide_end_snd > 0) {
//If a custom glide end sound was specified, play it
snd_play( &Snds[Ship_info[Player_ship->ship_info_index].glide_end_snd], 0.0f );
} else {
//If glide_end_snd wasn't set (probably == 0), use the default throttle up sound
snd_play( &Snds[ship_get_sound(Player_obj, SND_THROTTLE_UP)], 0.0f );
}
}
}
}
}
if ( (Viewer_mode & VM_EXTERNAL) ) {
if (!(Viewer_mode & VM_CAMERA_LOCKED)) {
ci->heading=0.0f;
ci->pitch=0.0f;
ci->bank=0.0f;
}
}
}
void copy_control_info(control_info *dest_ci, control_info *src_ci)
{
if (dest_ci == NULL)
return;
if (src_ci == NULL) {
dest_ci->pitch = 0.0f;
dest_ci->vertical = 0.0f;
dest_ci->heading = 0.0f;
dest_ci->sideways = 0.0f;
dest_ci->bank = 0.0f;
dest_ci->forward = 0.0f;
dest_ci->forward_cruise_percent = 0.0f;
dest_ci->fire_countermeasure_count = 0;
dest_ci->fire_secondary_count = 0;
dest_ci->fire_primary_count = 0;
} else {
dest_ci->pitch = src_ci->pitch;
dest_ci->vertical = src_ci->vertical;
dest_ci->heading = src_ci->heading;
dest_ci->sideways = src_ci->sideways;
dest_ci->bank = src_ci->bank;
dest_ci->forward = src_ci->forward;
dest_ci->forward_cruise_percent = src_ci->forward_cruise_percent;
}
}
void read_player_controls(object *objp, float frametime)
{
float diff;
float target_warpout_speed;
joy_ff_adjust_handling((int) objp->phys_info.speed);
switch( Player->control_mode )
{