-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp_move.cpp
1551 lines (1239 loc) · 36.6 KB
/
p_move.cpp
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) ZeniMax Media Inc.
// Licensed under the GNU General Public License 2.0.
#include "q_std.h"
#define GAME_INCLUDE
#include "bg_local.h"
// [Paril-KEX] generic code to detect & fix a stuck object
stuck_result_t G_FixStuckObject_Generic(vec3_t &origin, const vec3_t &own_mins, const vec3_t &own_maxs, std::function<stuck_object_trace_fn_t> trace) {
if (!trace(origin, own_mins, own_maxs, origin).startsolid)
return stuck_result_t::GOOD_POSITION;
struct {
float distance;
vec3_t origin;
} good_positions[6];
size_t num_good_positions = 0;
constexpr struct {
std::array<int8_t, 3> normal;
std::array<int8_t, 3> mins, maxs;
} side_checks[] = {
{ { 0, 0, 1 }, { -1, -1, 0 }, { 1, 1, 0 } },
{ { 0, 0, -1 }, { -1, -1, 0 }, { 1, 1, 0 } },
{ { 1, 0, 0 }, { 0, -1, -1 }, { 0, 1, 1 } },
{ { -1, 0, 0 }, { 0, -1, -1 }, { 0, 1, 1 } },
{ { 0, 1, 0 }, { -1, 0, -1 }, { 1, 0, 1 } },
{ { 0, -1, 0 }, { -1, 0, -1 }, { 1, 0, 1 } },
};
for (size_t sn = 0; sn < q_countof(side_checks); sn++) {
auto &side = side_checks[sn];
vec3_t start = origin;
vec3_t mins{}, maxs{};
for (size_t n = 0; n < 3; n++) {
if (side.normal[n] < 0)
start[n] += own_mins[n];
else if (side.normal[n] > 0)
start[n] += own_maxs[n];
if (side.mins[n] == -1)
mins[n] = own_mins[n];
else if (side.mins[n] == 1)
mins[n] = own_maxs[n];
if (side.maxs[n] == -1)
maxs[n] = own_mins[n];
else if (side.maxs[n] == 1)
maxs[n] = own_maxs[n];
}
trace_t tr = trace(start, mins, maxs, start);
int8_t needed_epsilon_fix = -1;
int8_t needed_epsilon_dir;
if (tr.startsolid) {
for (size_t e = 0; e < 3; e++) {
if (side.normal[e] != 0)
continue;
vec3_t ep_start = start;
ep_start[e] += 1;
tr = trace(ep_start, mins, maxs, ep_start);
if (!tr.startsolid) {
start = ep_start;
needed_epsilon_fix = e;
needed_epsilon_dir = 1;
break;
}
ep_start[e] -= 2;
tr = trace(ep_start, mins, maxs, ep_start);
if (!tr.startsolid) {
start = ep_start;
needed_epsilon_fix = e;
needed_epsilon_dir = -1;
break;
}
}
}
// no good
if (tr.startsolid)
continue;
vec3_t opposite_start = origin;
auto &other_side = side_checks[sn ^ 1];
for (size_t n = 0; n < 3; n++) {
if (other_side.normal[n] < 0)
opposite_start[n] += own_mins[n];
else if (other_side.normal[n] > 0)
opposite_start[n] += own_maxs[n];
}
if (needed_epsilon_fix >= 0)
opposite_start[needed_epsilon_fix] += needed_epsilon_dir;
// potentially a good side; start from our center, push back to the opposite side
// to find how much clearance we have
tr = trace(start, mins, maxs, opposite_start);
// ???
if (tr.startsolid)
continue;
// check the delta
vec3_t end = tr.endpos;
// push us very slightly away from the wall
end += vec3_t{ (float)side.normal[0], (float)side.normal[1], (float)side.normal[2] } *0.125f;
// calculate delta
const vec3_t delta = end - opposite_start;
vec3_t new_origin = origin + delta;
if (needed_epsilon_fix >= 0)
new_origin[needed_epsilon_fix] += needed_epsilon_dir;
tr = trace(new_origin, own_mins, own_maxs, new_origin);
// bad
if (tr.startsolid)
continue;
good_positions[num_good_positions].origin = new_origin;
good_positions[num_good_positions].distance = delta.lengthSquared();
num_good_positions++;
}
if (num_good_positions) {
std::sort(&good_positions[0], &good_positions[num_good_positions - 1], [](const auto &a, const auto &b) { return a.distance < b.distance; });
origin = good_positions[0].origin;
return stuck_result_t::FIXED;
}
return stuck_result_t::NO_GOOD_POSITION;
}
// all of the locals will be zeroed before each
// pmove, just to make damn sure we don't have
// any differences when running on client or server
struct pml_t {
vec3_t origin; // full float precision
vec3_t velocity; // full float precision
vec3_t forward, right, up;
float frametime;
csurface_t *groundsurface;
int groundcontents;
vec3_t previous_origin;
vec3_t start_velocity;
};
pm_config_t pm_config;
pmove_t *pm;
pml_t pml;
// movement parameters
float pm_stopspeed = 100;
float pm_maxspeed = 300;
float pm_duckspeed = 100;
float pm_accelerate = 10;
float pm_wateraccelerate = 10;
float pm_friction = 6;
float pm_waterfriction = 1;
float pm_waterspeed = 400;
float pm_laddermod = 0.5f;
/*
walking up a step should kill some velocity
*/
/*
==================
PM_ClipVelocity
Slide off of the impacting object
returns the blocked flags (1 = floor, 2 = step / wall)
==================
*/
static void PM_ClipVelocity(const vec3_t &in, const vec3_t &normal, vec3_t &out, float overbounce) {
float backoff;
float change;
int i;
backoff = in.dot(normal) * overbounce;
for (i = 0; i < 3; i++) {
change = normal[i] * backoff;
out[i] = in[i] - change;
if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
out[i] = 0;
}
}
static trace_t PM_Clip(const vec3_t &start, const vec3_t &mins, const vec3_t &maxs, const vec3_t &end, contents_t mask) {
return pm->clip(start, &mins, &maxs, end, mask);
}
static trace_t PM_Trace(const vec3_t &start, const vec3_t &mins, const vec3_t &maxs, const vec3_t &end, contents_t mask = CONTENTS_NONE) {
if (pm->s.pm_type == PM_SPECTATOR)
return PM_Clip(start, mins, maxs, end, MASK_SOLID);
if (mask == CONTENTS_NONE) {
if (pm->s.pm_type == PM_DEAD || pm->s.pm_type == PM_GIB)
mask = MASK_DEADSOLID;
else if (pm->s.pm_type == PM_SPECTATOR)
mask = MASK_SOLID;
else
mask = MASK_PLAYERSOLID;
if (pm->s.pm_flags & PMF_IGNORE_PLAYER_COLLISION)
mask &= ~CONTENTS_PLAYER;
}
return pm->trace(start, &mins, &maxs, end, pm->player, mask);
}
// only here to satisfy pm_trace_t
static inline trace_t PM_Trace_Auto(const vec3_t &start, const vec3_t &mins, const vec3_t &maxs, const vec3_t &end) {
return PM_Trace(start, mins, maxs, end);
}
/*
==================
PM_StepSlideMove
Each intersection will try to step over the obstruction instead of
sliding along it.
Returns a new origin, velocity, and contact entity
Does not modify any world state?
==================
*/
constexpr float MIN_STEP_NORMAL = 0.7f; // can't step up onto very steep slopes
constexpr size_t MAX_CLIP_PLANES = 5;
static inline void PM_RecordTrace(touch_list_t &touch, trace_t &tr) {
if (touch.num == MAXTOUCH)
return;
for (size_t i = 0; i < touch.num; i++)
if (touch.traces[i].ent == tr.ent)
return;
touch.traces[touch.num++] = tr;
}
// [Paril-KEX] made generic so you can run this without
// needing a pml/pm
void PM_StepSlideMove_Generic(vec3_t &origin, vec3_t &velocity, float frametime, const vec3_t &mins, const vec3_t &maxs, touch_list_t &touch, bool has_time, pm_trace_t trace_func) {
int bumpcount, numbumps;
vec3_t dir;
float d;
int numplanes;
vec3_t planes[MAX_CLIP_PLANES];
vec3_t primal_velocity;
int i, j;
trace_t trace;
vec3_t end;
float time_left;
numbumps = 4;
primal_velocity = velocity;
numplanes = 0;
time_left = frametime;
for (bumpcount = 0; bumpcount < numbumps; bumpcount++) {
for (i = 0; i < 3; i++)
end[i] = origin[i] + time_left * velocity[i];
trace = trace_func(origin, mins, maxs, end);
if (trace.allsolid) { // entity is trapped in another solid
velocity[2] = 0; // don't build up falling damage
// save entity for contact
PM_RecordTrace(touch, trace);
return;
}
// [Paril-KEX] experimental attempt to fix stray collisions on curved
// surfaces; easiest to see on q2dm1 by running/jumping against the sides
// of the curved map.
if (trace.surface2) {
vec3_t clipped_a, clipped_b;
PM_ClipVelocity(velocity, trace.plane.normal, clipped_a, 1.01f);
PM_ClipVelocity(velocity, trace.plane2.normal, clipped_b, 1.01f);
bool better = false;
for (int i = 0; i < 3; i++) {
if (fabsf(clipped_a[i]) < fabsf(clipped_b[i])) {
better = true;
break;
}
}
if (better) {
trace.plane = trace.plane2;
trace.surface = trace.surface2;
}
}
if (trace.fraction > 0) { // actually covered some distance
origin = trace.endpos;
numplanes = 0;
}
if (trace.fraction == 1)
break; // moved the entire distance
// save entity for contact
PM_RecordTrace(touch, trace);
time_left -= time_left * trace.fraction;
// slide along this plane
if (numplanes >= MAX_CLIP_PLANES) { // this shouldn't really happen
velocity = vec3_origin;
break;
}
//
// if this is the same plane we hit before, nudge origin
// out along it, which fixes some epsilon issues with
// non-axial planes (xswamp, q2dm1 sometimes...)
//
for (i = 0; i < numplanes; i++) {
if (trace.plane.normal.dot(planes[i]) > 0.99f) {
pml.origin.x += trace.plane.normal.x * 0.01f;
pml.origin.y += trace.plane.normal.y * 0.01f;
G_FixStuckObject_Generic(pml.origin, mins, maxs, trace_func);
break;
}
}
if (i < numplanes)
continue;
planes[numplanes] = trace.plane.normal;
numplanes++;
//
// modify original_velocity so it parallels all of the clip planes
//
for (i = 0; i < numplanes; i++) {
PM_ClipVelocity(velocity, planes[i], velocity, 1.01f);
for (j = 0; j < numplanes; j++)
if (j != i) {
if (velocity.dot(planes[j]) < 0)
break; // not ok
}
if (j == numplanes)
break;
}
if (i != numplanes) { // go along this plane
} else { // go along the crease
if (numplanes != 2) {
velocity = vec3_origin;
break;
}
dir = planes[0].cross(planes[1]);
d = dir.dot(velocity);
velocity = dir * d;
}
//
// if velocity is against the original velocity, stop dead
// to avoid tiny oscillations in sloping corners
//
if (velocity.dot(primal_velocity) <= 0) {
velocity = vec3_origin;
break;
}
}
if (has_time) {
velocity = primal_velocity;
}
}
static inline void PM_StepSlideMove_() {
PM_StepSlideMove_Generic(pml.origin, pml.velocity, pml.frametime, pm->mins, pm->maxs, pm->touch, pm->s.pm_time, PM_Trace_Auto);
}
/*
==================
PM_StepSlideMove
==================
*/
static void PM_StepSlideMove() {
vec3_t start_o, start_v;
vec3_t down_o, down_v;
trace_t trace;
float down_dist, up_dist;
// vec3_t delta;
vec3_t up, down;
start_o = pml.origin;
start_v = pml.velocity;
PM_StepSlideMove_();
down_o = pml.origin;
down_v = pml.velocity;
up = start_o;
up[2] += (pml.origin[2] < 0) ? STEPSIZE_BELOW : STEPSIZE;
trace = PM_Trace(start_o, pm->mins, pm->maxs, up);
if (trace.allsolid)
return; // can't step up
float stepSize = trace.endpos[2] - start_o[2];
// try sliding above
pml.origin = trace.endpos;
pml.velocity = start_v;
PM_StepSlideMove_();
// push down the final amount
down = pml.origin;
down[2] -= stepSize;
// [Paril-KEX] jitspoe suggestion for stair clip fix; store
// the old down position, and pick a better spot for downwards
// trace if the start origin's Z position is lower than the down end pt.
vec3_t original_down = down;
if (start_o[2] < down[2])
down[2] = start_o[2] - 1.f;
trace = PM_Trace(pml.origin, pm->mins, pm->maxs, down);
if (!trace.allsolid) {
// [Paril-KEX] from above, do the proper trace now
trace_t real_trace = PM_Trace(pml.origin, pm->mins, pm->maxs, original_down);
pml.origin = real_trace.endpos;
// only an upwards jump is a stair clip
if (pml.velocity.z > 0.f) {
pm->step_clip = true;
}
}
up = pml.origin;
// decide which one went farther
down_dist = (down_o[0] - start_o[0]) * (down_o[0] - start_o[0]) + (down_o[1] - start_o[1]) * (down_o[1] - start_o[1]);
up_dist = (up[0] - start_o[0]) * (up[0] - start_o[0]) + (up[1] - start_o[1]) * (up[1] - start_o[1]);
if (down_dist > up_dist || trace.plane.normal[2] < MIN_STEP_NORMAL) {
pml.origin = down_o;
pml.velocity = down_v;
}
// [Paril-KEX] NB: this line being commented is crucial for ramp-jumps to work.
// thanks to Jitspoe for pointing this one out.
else// if (pm->s.pm_flags & PMF_ON_GROUND)
//!! Special case
// if we were walking along a plane, then we need to copy the Z over
pml.velocity[2] = down_v[2];
// Paril: step down stairs/slopes
if ((pm->s.pm_flags & PMF_ON_GROUND) && !(pm->s.pm_flags & PMF_ON_LADDER) &&
(pm->waterlevel < WATER_WAIST || (!(pm->cmd.buttons & BUTTON_JUMP) && pml.velocity.z <= 0))) {
down = pml.origin;
down[2] -= (pml.origin[2] < 0) ? STEPSIZE_BELOW : STEPSIZE;
trace = PM_Trace(pml.origin, pm->mins, pm->maxs, down);
if (trace.fraction < 1.f) {
pml.origin = trace.endpos;
}
}
}
/*
==================
PM_Friction
Handles both ground friction and water friction
==================
*/
static void PM_Friction() {
float *vel;
float speed, newspeed, control;
float friction;
float drop;
vel = &pml.velocity.x;
speed = sqrt(vel[0] * vel[0] + vel[1] * vel[1] + vel[2] * vel[2]);
if (speed < 1) {
vel[0] = 0;
vel[1] = 0;
return;
}
drop = 0;
// apply ground friction
if ((pm->groundentity && pml.groundsurface && !(pml.groundsurface->flags & SURF_SLICK)) || (pm->s.pm_flags & PMF_ON_LADDER)) {
friction = pm_friction;
control = speed < pm_stopspeed ? pm_stopspeed : speed;
drop += control * friction * pml.frametime;
}
// apply water friction
if (pm->waterlevel && !(pm->s.pm_flags & PMF_ON_LADDER))
drop += speed * pm_waterfriction * (float)pm->waterlevel * pml.frametime;
// scale the velocity
newspeed = speed - drop;
if (newspeed < 0) {
newspeed = 0;
}
newspeed /= speed;
vel[0] = vel[0] * newspeed;
vel[1] = vel[1] * newspeed;
vel[2] = vel[2] * newspeed;
}
/*
==============
PM_Accelerate
Handles user intended acceleration
==============
*/
static void PM_Accelerate(const vec3_t &wishdir, float wishspeed, float accel) {
int i;
float addspeed, accelspeed, currentspeed;
currentspeed = pml.velocity.dot(wishdir);
addspeed = wishspeed - currentspeed;
if (addspeed <= 0)
return;
accelspeed = accel * pml.frametime * wishspeed;
if (accelspeed > addspeed)
accelspeed = addspeed;
for (i = 0; i < 3; i++)
pml.velocity[i] += accelspeed * wishdir[i];
}
static void PM_AirAccelerate(const vec3_t &wishdir, float wishspeed, float accel) {
int i;
float addspeed, accelspeed, currentspeed, wishspd = wishspeed;
if (wishspd > 30)
wishspd = 30;
currentspeed = pml.velocity.dot(wishdir);
addspeed = wishspd - currentspeed;
if (addspeed <= 0)
return;
accelspeed = accel * wishspeed * pml.frametime;
if (accelspeed > addspeed)
accelspeed = addspeed;
for (i = 0; i < 3; i++)
pml.velocity[i] += accelspeed * wishdir[i];
}
/*
=============
PM_AddCurrents
=============
*/
static void PM_AddCurrents(vec3_t &wishvel) {
vec3_t v;
float s;
//
// account for ladders
//
if (pm->s.pm_flags & PMF_ON_LADDER) {
if (pm->cmd.buttons & (BUTTON_JUMP | BUTTON_CROUCH)) {
// [Paril-KEX]: if we're underwater, use full speed on ladders
float ladder_speed = pm->waterlevel >= WATER_WAIST ? pm_maxspeed : 200;
if (pm->cmd.buttons & BUTTON_JUMP)
wishvel[2] = ladder_speed;
else if (pm->cmd.buttons & BUTTON_CROUCH)
wishvel[2] = -ladder_speed;
} else if (pm->cmd.forwardmove) {
// [Paril-KEX] clamp the speed a bit so we're not too fast
float ladder_speed = std::clamp(pm->cmd.forwardmove, -200.f, 200.f);
if (pm->cmd.forwardmove > 0) {
if (pm->viewangles[PITCH] < 15)
wishvel[2] = ladder_speed;
else
wishvel[2] = -ladder_speed;
}
// [Paril-KEX] allow using "back" arrow to go down on ladder
else if (pm->cmd.forwardmove < 0) {
// if we haven't touched ground yet, remove x/y so we don't
// slide off of the ladder
if (!pm->groundentity)
wishvel[0] = wishvel[1] = 0;
wishvel[2] = ladder_speed;
}
} else
wishvel[2] = 0;
// limit horizontal speed when on a ladder
// [Paril-KEX] unless we're on the ground
if (!pm->groundentity) {
// [Paril-KEX] instead of left/right not doing anything,
// have them move you perpendicular to the ladder plane
if (pm->cmd.sidemove) {
// clamp side speed so it's not jarring...
float ladder_speed = std::clamp(pm->cmd.sidemove, -150.f, 150.f);
if (pm->waterlevel < WATER_WAIST)
ladder_speed *= pm_laddermod;
// check for ladder
vec3_t flatforward, spot;
flatforward[0] = pml.forward[0];
flatforward[1] = pml.forward[1];
flatforward[2] = 0;
flatforward.normalize();
spot = pml.origin + (flatforward * 1);
trace_t trace = PM_Trace(pml.origin, pm->mins, pm->maxs, spot, CONTENTS_LADDER);
if (trace.fraction != 1.f && (trace.contents & CONTENTS_LADDER)) {
vec3_t right = trace.plane.normal.cross({ 0, 0, 1 });
wishvel[0] = wishvel[1] = 0;
wishvel += (right * -ladder_speed);
}
} else {
if (wishvel[0] < -25)
wishvel[0] = -25;
else if (wishvel[0] > 25)
wishvel[0] = 25;
if (wishvel[1] < -25)
wishvel[1] = -25;
else if (wishvel[1] > 25)
wishvel[1] = 25;
}
}
}
//
// add water currents
//
if (pm->watertype & MASK_CURRENT) {
v = {};
if (pm->watertype & CONTENTS_CURRENT_0)
v[0] += 1;
if (pm->watertype & CONTENTS_CURRENT_90)
v[1] += 1;
if (pm->watertype & CONTENTS_CURRENT_180)
v[0] -= 1;
if (pm->watertype & CONTENTS_CURRENT_270)
v[1] -= 1;
if (pm->watertype & CONTENTS_CURRENT_UP)
v[2] += 1;
if (pm->watertype & CONTENTS_CURRENT_DOWN)
v[2] -= 1;
s = pm_waterspeed;
if ((pm->waterlevel == WATER_FEET) && (pm->groundentity))
s /= 2;
wishvel += (v * s);
}
//
// add conveyor belt velocities
//
if (pm->groundentity) {
v = {};
if (pml.groundcontents & CONTENTS_CURRENT_0)
v[0] += 1;
if (pml.groundcontents & CONTENTS_CURRENT_90)
v[1] += 1;
if (pml.groundcontents & CONTENTS_CURRENT_180)
v[0] -= 1;
if (pml.groundcontents & CONTENTS_CURRENT_270)
v[1] -= 1;
if (pml.groundcontents & CONTENTS_CURRENT_UP)
v[2] += 1;
if (pml.groundcontents & CONTENTS_CURRENT_DOWN)
v[2] -= 1;
wishvel += v * 100;
}
}
/*
===================
PM_WaterMove
===================
*/
static void PM_WaterMove() {
int i;
vec3_t wishvel;
float wishspeed;
vec3_t wishdir;
//
// user intentions
//
for (i = 0; i < 3; i++)
wishvel[i] = pml.forward[i] * pm->cmd.forwardmove + pml.right[i] * pm->cmd.sidemove;
if (!pm->cmd.forwardmove && !pm->cmd.sidemove &&
!(pm->cmd.buttons & (BUTTON_JUMP | BUTTON_CROUCH))) {
if (!pm->groundentity)
wishvel[2] -= 60; // drift towards bottom
} else {
if (pm->cmd.buttons & BUTTON_CROUCH)
wishvel[2] -= pm_waterspeed * 0.5f;
else if (pm->cmd.buttons & BUTTON_JUMP)
wishvel[2] += pm_waterspeed * 0.5f;
}
PM_AddCurrents(wishvel);
wishdir = wishvel;
wishspeed = wishdir.normalize();
if (wishspeed > pm_maxspeed) {
wishvel *= pm_maxspeed / wishspeed;
wishspeed = pm_maxspeed;
}
wishspeed *= 0.5f;
if ((pm->s.pm_flags & PMF_DUCKED) && wishspeed > pm_duckspeed) {
wishvel *= pm_duckspeed / wishspeed;
wishspeed = pm_duckspeed;
}
PM_Accelerate(wishdir, wishspeed, pm_wateraccelerate);
PM_StepSlideMove();
}
/*
===================
PM_AirMove
===================
*/
static void PM_AirMove() {
int i;
vec3_t wishvel;
float fmove, smove;
vec3_t wishdir;
float wishspeed;
float maxspeed;
fmove = pm->cmd.forwardmove;
smove = pm->cmd.sidemove;
for (i = 0; i < 2; i++)
wishvel[i] = pml.forward[i] * fmove + pml.right[i] * smove;
wishvel[2] = 0;
PM_AddCurrents(wishvel);
wishdir = wishvel;
wishspeed = wishdir.normalize();
//
// clamp to server defined max speed
//
maxspeed = (pm->s.pm_flags & PMF_DUCKED) ? pm_duckspeed : pm_maxspeed;
if (wishspeed > maxspeed) {
wishvel *= maxspeed / wishspeed;
wishspeed = maxspeed;
}
if (pm->s.pm_flags & PMF_ON_LADDER) {
PM_Accelerate(wishdir, wishspeed, pm_accelerate);
if (!wishvel[2]) {
if (pml.velocity[2] > 0) {
pml.velocity[2] -= pm->s.gravity * pml.frametime;
if (pml.velocity[2] < 0)
pml.velocity[2] = 0;
} else {
pml.velocity[2] += pm->s.gravity * pml.frametime;
if (pml.velocity[2] > 0)
pml.velocity[2] = 0;
}
}
PM_StepSlideMove();
} else if (pm->groundentity) { // walking on ground
pml.velocity[2] = 0; //!!! this is before the accel
PM_Accelerate(wishdir, wishspeed, pm_accelerate);
if (pm->s.gravity > 0)
pml.velocity[2] = 0;
else
pml.velocity[2] -= pm->s.gravity * pml.frametime;
if (!pml.velocity[0] && !pml.velocity[1])
return;
PM_StepSlideMove();
} else { // not on ground, so little effect on velocity
if (pm_config.airaccel)
PM_AirAccelerate(wishdir, wishspeed, pm_config.airaccel);
else
PM_Accelerate(wishdir, wishspeed, 1);
// add gravity
if (pm->s.pm_type != PM_GRAPPLE)
pml.velocity[2] -= pm->s.gravity * pml.frametime;
PM_StepSlideMove();
}
}
static inline void PM_GetWaterLevel(const vec3_t &position, water_level_t &level, contents_t &type) {
//
// get waterlevel, accounting for ducking
//
level = WATER_NONE;
type = CONTENTS_NONE;
int32_t sample2 = (int)(pm->s.viewheight - pm->mins[2]);
int32_t sample1 = sample2 / 2;
vec3_t point = position;
point[2] += pm->mins[2] + 1;
contents_t cont = pm->pointcontents(point);
if (cont & MASK_WATER) {
type = cont;
level = WATER_FEET;
point[2] = pml.origin[2] + pm->mins[2] + sample1;
cont = pm->pointcontents(point);
if (cont & MASK_WATER) {
level = WATER_WAIST;
point[2] = pml.origin[2] + pm->mins[2] + sample2;
cont = pm->pointcontents(point);
if (cont & MASK_WATER)
level = WATER_UNDER;
}
}
}
/*
=============
PM_CatagorizePosition
=============
*/
static void PM_CatagorizePosition() {
vec3_t point;
trace_t trace;
// if the player hull point one unit down is solid, the player
// is on ground
// see if standing on something solid
point[0] = pml.origin[0];
point[1] = pml.origin[1];
point[2] = pml.origin[2] - 0.25f;
if (pml.velocity[2] > 180 || pm->s.pm_type == PM_GRAPPLE) //!!ZOID changed from 100 to 180 (ramp accel)
{
pm->s.pm_flags &= ~PMF_ON_GROUND;
pm->groundentity = nullptr;
} else {
trace = PM_Trace(pml.origin, pm->mins, pm->maxs, point);
pm->groundplane = trace.plane;
pml.groundsurface = trace.surface;
pml.groundcontents = trace.contents;
// [Paril-KEX] to attempt to fix edge cases where you get stuck
// wedged between a slope and a wall (which is irrecoverable
// most of the time), we'll allow the player to "stand" on
// slopes if they are right up against a wall
bool slanted_ground = trace.fraction < 1.0f && trace.plane.normal[2] < 0.7f;
if (slanted_ground) {
trace_t slant = PM_Trace(pml.origin, pm->mins, pm->maxs, pml.origin + trace.plane.normal);
if (slant.fraction < 1.0f && !slant.startsolid)
slanted_ground = false;
}
if (trace.fraction == 1.0f || (slanted_ground && !trace.startsolid)) {
pm->groundentity = nullptr;
pm->s.pm_flags &= ~PMF_ON_GROUND;
} else {
pm->groundentity = trace.ent;
// hitting solid ground will end a waterjump
if (pm->s.pm_flags & PMF_TIME_WATERJUMP) {
pm->s.pm_flags &= ~(PMF_TIME_WATERJUMP | PMF_TIME_LAND | PMF_TIME_TELEPORT | PMF_TIME_TRICK);
pm->s.pm_time = 0;
}
if (!(pm->s.pm_flags & PMF_ON_GROUND)) {
// just hit the ground
// [Paril-KEX]
if (!pm_config.n64_physics && pml.velocity[2] >= 100.f && pm->groundplane.normal[2] >= 0.9f && !(pm->s.pm_flags & PMF_DUCKED)) {
pm->s.pm_flags |= PMF_TIME_TRICK;
pm->s.pm_time = 64;
}
// [Paril-KEX] calculate impact delta; this also fixes triple jumping
vec3_t clipped_velocity;
PM_ClipVelocity(pml.velocity, pm->groundplane.normal, clipped_velocity, 1.01f);
pm->impact_delta = pml.start_velocity[2] - clipped_velocity[2];
pm->s.pm_flags |= PMF_ON_GROUND;
if (pm_config.n64_physics || (pm->s.pm_flags & PMF_DUCKED)) {
pm->s.pm_flags |= PMF_TIME_LAND;
pm->s.pm_time = 128;
}
}
}
PM_RecordTrace(pm->touch, trace);
}
//
// get waterlevel, accounting for ducking
//
PM_GetWaterLevel(pml.origin, pm->waterlevel, pm->watertype);
}
/*
=============
PM_CheckJump
=============
*/
static void PM_CheckJump() {
if (pm->s.pm_flags & PMF_TIME_LAND) { // hasn't been long enough since landing to jump again
return;
}
if (!(pm->cmd.buttons & BUTTON_JUMP)) { // not holding jump
pm->s.pm_flags &= ~PMF_JUMP_HELD;
return;
}
// must wait for jump to be released
if (pm->s.pm_flags & PMF_JUMP_HELD)
return;
if (pm->s.pm_type == PM_DEAD)
return;
if (pm->waterlevel >= WATER_WAIST) { // swimming, not jumping
pm->groundentity = nullptr;
return;
}
if (pm->groundentity == nullptr)
return; // in air, so no effect
pm->s.pm_flags |= PMF_JUMP_HELD;
pm->jump_sound = true;
pm->groundentity = nullptr;
pm->s.pm_flags &= ~PMF_ON_GROUND;
float jump_height = 270.f;
if (pml.origin[2] < 0)
jump_height += 4.0f;