-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
pm_shared.cpp
3267 lines (2660 loc) · 72.7 KB
/
pm_shared.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
#include "precompiled.h"
BOOL pm_shared_initialized = FALSE;
vec3_t rgv3tStuckTable[54];
int rgStuckLast[MAX_CLIENTS][2];
int pm_gcTextures = 0;
char pm_grgszTextureName[MAX_TEXTURES][MAX_TEXTURENAME_LENGHT];
char pm_grgchTextureType[MAX_TEXTURES];
playermove_t *pmove = nullptr;
BOOL g_onladder = FALSE;
#ifdef CLIENT_DLL
int iJumpSpectator;
float vJumpOrigin[3];
float vJumpAngles[3];
#endif
void PM_SwapTextures(int i, int j)
{
char chTemp;
char szTemp[MAX_TEXTURENAME_LENGHT];
Q_strlcpy(szTemp, pm_grgszTextureName[i]);
chTemp = pm_grgchTextureType[i];
Q_strcpy_s(pm_grgszTextureName[i], pm_grgszTextureName[j]);
pm_grgchTextureType[i] = pm_grgchTextureType[j];
Q_strlcpy(pm_grgszTextureName[j], szTemp);
pm_grgchTextureType[j] = chTemp;
}
NOXREF qboolean PM_IsThereGrassTexture()
{
for (int i = 0; i < pm_gcTextures; i++)
{
if (pm_grgchTextureType[i] == CHAR_TEX_GRASS)
return TRUE;
}
return FALSE;
}
void PM_SortTextures()
{
// Bubble sort, yuck, but this only occurs at startup and it's only 512 elements...
int i, j;
for (i = 0; i < pm_gcTextures; i++)
{
for (j = i + 1; j < pm_gcTextures; j++)
{
if (Q_stricmp(pm_grgszTextureName[i], pm_grgszTextureName[j]) > 0)
{
// Swap
PM_SwapTextures(i, j);
}
}
}
}
void PM_InitTextureTypes()
{
char buffer[512];
int i, j;
byte *pMemFile;
int fileSize, filePos = 0;
static bool bTextureTypeInit = false;
if (bTextureTypeInit)
return;
Q_memset(&(pm_grgszTextureName[0][0]), 0, sizeof(pm_grgszTextureName));
Q_memset(pm_grgchTextureType, 0, sizeof(pm_grgchTextureType));
pm_gcTextures = 0;
Q_memset(buffer, 0, sizeof(buffer));
pMemFile = pmove->COM_LoadFile("sound/materials.txt", 5, &fileSize);
if (!pMemFile)
return;
// for each line in the file...
while (pmove->memfgets(pMemFile, fileSize, &filePos, buffer, sizeof(buffer) - 1) && (pm_gcTextures < MAX_TEXTURES))
{
// skip whitespace
i = 0;
while (buffer[i] && isspace(buffer[i]))
i++;
if (!buffer[i])
continue;
// skip comment lines
if (buffer[i] == '/' || !isalpha(buffer[i]))
continue;
// get texture type
pm_grgchTextureType[pm_gcTextures] = toupper(buffer[i++]);
// skip whitespace
while (buffer[i] && isspace(buffer[i]))
i++;
if (!buffer[i])
continue;
// get sentence name
j = i;
while (buffer[j] && !isspace(buffer[j]))
j++;
if (!buffer[j])
continue;
// null-terminate name and save in sentences array
j = Q_min(j, MAX_TEXTURENAME_LENGHT - 1 + i);
buffer[j] = '\0';
Q_strcpy(&(pm_grgszTextureName[pm_gcTextures++][0]), &(buffer[i]));
}
// Must use engine to free since we are in a .dll
pmove->COM_FreeFile(pMemFile);
PM_SortTextures();
bTextureTypeInit = true;
}
char EXT_FUNC PM_FindTextureType(char *name)
{
int left, right, pivot;
int val;
assert(pm_shared_initialized);
left = 0;
right = pm_gcTextures - 1;
while (left <= right)
{
pivot = (left + right) / 2;
val = Q_strnicmp(name, pm_grgszTextureName[pivot], MAX_TEXTURENAME_LENGHT - 1);
if (val == 0)
{
return pm_grgchTextureType[pivot];
}
else if (val > 0)
{
left = pivot + 1;
}
else if (val < 0)
{
right = pivot - 1;
}
}
return CHAR_TEX_CONCRETE;
}
void PM_PlayStepSound(int step, float fvol)
{
static int iSkipStep = 0;
int irand;
pmove->iStepLeft = !pmove->iStepLeft;
if (!pmove->runfuncs)
{
return;
}
irand = pmove->RandomLong(0, 1) + (pmove->iStepLeft * 2);
// FIXME mp_footsteps needs to be a movevar
if (pmove->multiplayer && !pmove->movevars->footsteps)
return;
// irand - 0,1 for right foot, 2,3 for left foot
// used to alternate left and right foot
// FIXME, move to player state
switch (step)
{
default:
case STEP_CONCRETE:
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_step1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_step3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_step2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_step4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_METAL:
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_metal1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_metal3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_metal2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_metal4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_DIRT:
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_dirt1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_dirt3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_dirt2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_dirt4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_VENT:
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_duct1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_duct3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_duct2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_duct4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_GRATE:
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_grate1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_grate3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_grate2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_grate4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_TILE:
if (!pmove->RandomLong(0, 4))
irand = 4;
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_tile1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_tile3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_tile2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_tile4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 4: pmove->PM_PlaySound(CHAN_BODY, "player/pl_tile5.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_SLOSH:
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_slosh1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_slosh3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_slosh2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_slosh4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_WADE:
if (iSkipStep == 0)
{
iSkipStep++;
break;
}
if (iSkipStep++ == 3)
{
iSkipStep = 0;
}
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_wade1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_wade2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_wade3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_wade4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_LADDER:
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_ladder1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_ladder3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_ladder2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_ladder4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
case STEP_SNOW:
switch (irand)
{
// right foot
case 0: pmove->PM_PlaySound(CHAN_BODY, "player/pl_snow1.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 1: pmove->PM_PlaySound(CHAN_BODY, "player/pl_snow3.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
// left foot
case 2: pmove->PM_PlaySound(CHAN_BODY, "player/pl_snow2.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
case 3: pmove->PM_PlaySound(CHAN_BODY, "player/pl_snow4.wav", fvol, ATTN_NORM, 0, PITCH_NORM); break;
}
break;
}
}
int PM_MapTextureTypeStepType(char chTextureType)
{
switch (chTextureType)
{
default:
case CHAR_TEX_CONCRETE: return STEP_CONCRETE;
case CHAR_TEX_METAL: return STEP_METAL;
case CHAR_TEX_DIRT: return STEP_DIRT;
case CHAR_TEX_VENT: return STEP_VENT;
case CHAR_TEX_GRATE: return STEP_GRATE;
case CHAR_TEX_TILE: return STEP_TILE;
case CHAR_TEX_SLOSH: return STEP_SLOSH;
case CHAR_TEX_SNOW: return STEP_SNOW;
}
}
void PM_CatagorizeTextureType()
{
vec3_t start, end;
const char *pTextureName;
VectorCopy(pmove->origin, start);
VectorCopy(pmove->origin, end);
// Straight down
end[2] -= 64.0f;
// Fill in default values, just in case.
pmove->sztexturename[0] = '\0';
pmove->chtexturetype = CHAR_TEX_CONCRETE;
pTextureName = pmove->PM_TraceTexture(pmove->onground, start, end);
if (!pTextureName)
return;
// strip leading '-0' or '+0~' or '{' or '!'
if (*pTextureName == '-' || *pTextureName == '+')
pTextureName += 2;
if (*pTextureName == '{' || *pTextureName == '!' || *pTextureName == '~' || *pTextureName == ' ')
pTextureName++;
Q_strcpy(pmove->sztexturename, pTextureName);
pmove->sztexturename[MAX_TEXTURENAME_LENGHT - 1] = '\0';
// get texture type
pmove->chtexturetype = PM_FindTextureType(pmove->sztexturename);
}
LINK_HOOK_VOID_CHAIN2(PM_UpdateStepSound);
void EXT_FUNC __API_HOOK(PM_UpdateStepSound)()
{
float fvol;
vec3_t knee;
vec3_t feet;
vec3_t center;
float height;
float speed;
int fLadder;
int step;
int onground;
if (pmove->flTimeStepSound > 0)
return;
if (pmove->flags & FL_FROZEN)
return;
speed = Length(pmove->velocity);
if (speed <= 150.0)
{
pmove->flTimeStepSound = 400;
return;
}
// determine if we are on a ladder
fLadder = (pmove->movetype == MOVETYPE_FLY);
// determine if we are not in air
onground = (pmove->onground != -1);
// If we're on a ladder or on the ground play step sound.
if (fLadder || onground)
{
PM_CatagorizeTextureType();
VectorCopy(pmove->origin, center);
VectorCopy(pmove->origin, knee);
VectorCopy(pmove->origin, feet);
height = pmove->player_maxs[pmove->usehull][2] - pmove->player_mins[pmove->usehull][2];
knee[2] = pmove->origin[2] - 0.3 * height;
feet[2] = pmove->origin[2] - 0.5 * height;
// find out what we're stepping in or on...
if (fLadder)
{
step = STEP_LADDER;
fvol = 0.35;
pmove->flTimeStepSound = 350;
}
else if (pmove->PM_PointContents(knee, nullptr) == CONTENTS_WATER)
{
step = STEP_WADE;
fvol = 0.65;
pmove->flTimeStepSound = 600;
}
else if (pmove->PM_PointContents(feet, nullptr) == CONTENTS_WATER)
{
step = STEP_SLOSH;
fvol = 0.5;
pmove->flTimeStepSound = 300;
}
else
{
// find texture under player, if different from current texture,
// get material type
step = PM_MapTextureTypeStepType(pmove->chtexturetype);
switch (pmove->chtexturetype)
{
default:
case CHAR_TEX_CONCRETE:
fvol = 0.5;
pmove->flTimeStepSound = 300;
break;
case CHAR_TEX_METAL:
fvol = 0.5;
pmove->flTimeStepSound = 300;
break;
case CHAR_TEX_DIRT:
fvol = 0.55;
pmove->flTimeStepSound = 300;
break;
case CHAR_TEX_VENT:
fvol = 0.7;
pmove->flTimeStepSound = 300;
break;
case CHAR_TEX_GRATE:
fvol = 0.5;
pmove->flTimeStepSound = 300;
break;
case CHAR_TEX_TILE:
fvol = 0.5;
pmove->flTimeStepSound = 300;
break;
case CHAR_TEX_SLOSH:
fvol = 0.5;
pmove->flTimeStepSound = 300;
break;
}
}
if ((pmove->flags & FL_DUCKING) || fLadder)
{
// slower step time if ducking
pmove->flTimeStepSound += 100;
// play the sound
// 35% volume if ducking
if ((pmove->flags & FL_DUCKING) && pmove->flDuckTime < 950.0)
{
fvol *= 0.35;
}
}
PM_PlayStepSound(step, fvol);
}
}
// Add's the trace result to touch list, if contact is not already in list.
qboolean PM_AddToTouched(pmtrace_t tr, vec_t *impactvelocity)
{
int i;
for (i = 0; i < pmove->numtouch; i++)
{
if (pmove->touchindex[i].ent == tr.ent)
break;
}
// Already in list.
if (i != pmove->numtouch)
{
return FALSE;
}
VectorCopy(impactvelocity, tr.deltavelocity);
if (pmove->numtouch >= MAX_PHYSENTS)
{
pmove->Con_DPrintf("Too many entities were touched!\n");
}
pmove->touchindex[pmove->numtouch++] = tr;
return TRUE;
}
void PM_CheckVelocity()
{
int i;
// bound velocity
for (i = 0; i < 3; i++)
{
// See if it's bogus.
if (IS_NAN(pmove->velocity[i]))
{
pmove->Con_Printf("PM Got a NaN velocity %i\n", i);
pmove->velocity[i] = 0;
}
if (IS_NAN(pmove->origin[i]))
{
pmove->Con_Printf("PM Got a NaN origin on %i\n", i);
pmove->origin[i] = 0;
}
// Bound it.
if (pmove->velocity[i] > pmove->movevars->maxvelocity)
{
pmove->Con_DPrintf("PM Got a velocity too high on %i\n", i);
pmove->velocity[i] = pmove->movevars->maxvelocity;
}
else if (pmove->velocity[i] < -pmove->movevars->maxvelocity)
{
pmove->Con_DPrintf("PM Got a velocity too low on %i\n", i);
pmove->velocity[i] = -pmove->movevars->maxvelocity;
}
}
}
// Slide off of the impacting object
// returns the blocked flags:
// 0x01 == floor
// 0x02 == step / wall
int PM_ClipVelocity(vec_t *in, vec_t *normal, vec_t *out, float overbounce)
{
float change;
real_t angle;
real_t backoff;
int i, blocked;
angle = normal[2];
// Assume unblocked.
blocked = 0x00;
// If the plane that is blocking us has a positive z component, then assume it's a floor.
if (angle > 0)
{
blocked |= 0x01;
}
// If the plane has no Z, it is vertical (wall/step)
if (!angle)
{
blocked |= 0x02;
}
// Determine how far along plane to slide based on incoming direction.
// Scale by overbounce factor.
backoff = DotProduct(in, normal) * overbounce;
for (i = 0; i < 3; i++)
{
change = in[i] - normal[i] * backoff;
out[i] = change;
// If out velocity is too small, zero it out.
if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
{
out[i] = 0;
}
}
// Return blocking flags.
return blocked;
}
void PM_AddCorrectGravity()
{
real_t ent_gravity;
if (pmove->waterjumptime)
return;
if (pmove->gravity != 0.0f)
ent_gravity = pmove->gravity;
else
ent_gravity = 1.0f;
// Add gravity so they'll be in the correct position during movement
// yes, this 0.5 looks wrong, but it's not.
pmove->velocity[2] -= (ent_gravity * pmove->movevars->gravity * 0.5f * pmove->frametime);
pmove->velocity[2] += pmove->basevelocity[2] * pmove->frametime;
pmove->basevelocity[2] = 0;
PM_CheckVelocity();
}
void PM_FixupGravityVelocity()
{
real_t ent_gravity;
if (pmove->waterjumptime)
return;
if (pmove->gravity != 0.0)
ent_gravity = pmove->gravity;
else
ent_gravity = 1.0;
// Get the correct velocity for the end of the dt
pmove->velocity[2] -= (pmove->movevars->gravity * pmove->frametime * ent_gravity * 0.5);
PM_CheckVelocity();
}
int PM_FlyMove()
{
int bumpcount, numbumps;
vec3_t dir;
float d;
int numplanes;
vec3_t planes[MAX_CLIP_PLANES];
vec3_t primal_velocity, original_velocity;
vec3_t new_velocity;
int i, j;
pmtrace_t trace;
vec3_t end;
float time_left, allFraction;
int blocked;
numbumps = 4; // Bump up to four times
blocked = 0x00; // Assume not blocked
numplanes = 0; // and not sliding along any planes
VectorCopy(pmove->velocity, original_velocity); // Store original velocity
VectorCopy(pmove->velocity, primal_velocity);
allFraction = 0;
time_left = pmove->frametime; // Total time for this movement operation.
for (bumpcount = 0; bumpcount < numbumps; bumpcount++)
{
if (!pmove->velocity[0] && !pmove->velocity[1] && !pmove->velocity[2])
break;
// Assume we can move all the way from the current origin to the
// end point.
for (i = 0; i < 3; i++)
{
real_t flScale = time_left * pmove->velocity[i];
end[i] = pmove->origin[i] + flScale;
}
// See if we can make it from origin to end point.
trace = pmove->PM_PlayerTrace(pmove->origin, end, PM_NORMAL, -1);
allFraction += trace.fraction;
// If we started in a solid object, or we were in solid space
// the whole way, zero out our velocity and return that we
// are blocked by floor and wall.
if (trace.allsolid)
{
// entity is trapped in another solid
VectorCopy(vec3_origin, pmove->velocity);
return 4;
}
// If we moved some portion of the total distance, then
// copy the end position into the pmove->origin and
// zero the plane counter.
if (trace.fraction > 0.0f)
{
// actually covered some distance
VectorCopy(trace.endpos, pmove->origin);
VectorCopy(pmove->velocity, original_velocity);
numplanes = 0;
}
// If we covered the entire distance, we are done
// and can return.
if (trace.fraction == 1.0f)
{
// moved the entire distance
break;
}
// Save entity that blocked us (since fraction was < 1.0)
// for contact
// Add it if it's not already in the list
PM_AddToTouched(trace, pmove->velocity);
// If the plane we hit has a high z component in the normal, then
// it's probably a floor
if (trace.plane.normal[2] > 0.7f)
{
// floor
blocked |= 0x01;
}
// If the plane has a zero z component in the normal, then it's a
// step or wall
if (!trace.plane.normal[2])
{
// step / wall
blocked |= 0x02;
}
// Reduce amount of pmove->frametime left by total time left * fraction
// that we covered.
time_left -= time_left * trace.fraction;
// Did we run out of planes to clip against?
if (numplanes >= MAX_CLIP_PLANES)
{
// this shouldn't really happen
// Stop our movement if so.
VectorCopy(vec3_origin, pmove->velocity);
break;
}
// Set up next clipping plane
VectorCopy(trace.plane.normal, planes[numplanes]);
numplanes++;
// modify original_velocity so it parallels all of the clip planes
// relfect player velocity
if (numplanes == 1 && pmove->movetype == MOVETYPE_WALK && (pmove->onground == -1 || pmove->friction != 1))
{
for (i = 0; i < numplanes; i++)
{
if (planes[i][2] > 0.7f)
{
// floor or slope
PM_ClipVelocity(original_velocity, planes[i], new_velocity, 1);
VectorCopy(new_velocity, original_velocity);
}
else
PM_ClipVelocity(original_velocity, planes[i], new_velocity, 1.0 + pmove->movevars->bounce * (1.0 - pmove->friction));
}
VectorCopy(new_velocity, pmove->velocity);
VectorCopy(new_velocity, original_velocity);
}
else
{
for (i = 0; i < numplanes; i++)
{
PM_ClipVelocity(original_velocity, planes[i], pmove->velocity, 1);
for (j = 0; j < numplanes; j++)
{
if (j != i && DotProduct(pmove->velocity, planes[j]) < 0)
{
break;
}
}
if (j == numplanes)
break;
}
if (i == numplanes)
{
if (numplanes != 2)
{
VectorCopy(vec3_origin, pmove->velocity);
break;
}
CrossProduct(planes[0], planes[1], dir);
d = DotProduct(dir, pmove->velocity);
VectorScale(dir, d, pmove->velocity);
}
if (DotProduct(pmove->velocity, primal_velocity) <= 0)
{
VectorCopy(vec3_origin, pmove->velocity);
break;
}
}
}
if (allFraction == 0.0f)
{
VectorCopy(vec3_origin, pmove->velocity);
}
return blocked;
}
void PM_Accelerate(vec_t *wishdir, real_t wishspeed, float accel)
{
int i;
float addspeed;
real_t currentspeed;
real_t accelspeed;
// Dead player's don't accelerate
if (pmove->dead)
return;
// If waterjumping, don't accelerate
if (pmove->waterjumptime)
return;
// See if we are changing direction a bit
currentspeed = DotProduct(pmove->velocity, wishdir);
// Reduce wishspeed by the amount of veer.
addspeed = wishspeed - currentspeed;
// If not going to add any speed, done.
if (addspeed <= 0)
return;
// Determine amount of accleration.
accelspeed = accel * pmove->frametime * wishspeed * pmove->friction;
// Cap at addspeed
if (accelspeed > addspeed)
accelspeed = addspeed;
// Adjust velocity.
for (i = 0; i < 3; i++)
{
pmove->velocity[i] += accelspeed * wishdir[i];
}
}
// Only used by players. Moves along the ground when player is a MOVETYPE_WALK.
void PM_WalkMove()
{
int clip;
int oldonground;
int i;
vec3_t wishvel;
real_t spd;
float fmove, smove;
vec3_t wishdir;
real_t wishspeed;
//vec3_t start; // TODO: unused
vec3_t dest;
vec3_t original, originalvel;
vec3_t down, downvel;
float downdist, updist;
pmtrace_t trace;
if (pmove->fuser2 > 0.0)
{
real_t flRatio = (100 - pmove->fuser2 * 0.001 * 19) * 0.01;
pmove->velocity[0] *= flRatio;
pmove->velocity[1] *= flRatio;
}
// Copy movement amounts
fmove = pmove->cmd.forwardmove;
smove = pmove->cmd.sidemove;
// Zero out z components of movement vectors
pmove->forward[2] = 0;
pmove->right[2] = 0;
// Normalize remainder of vectors.
VectorNormalize(pmove->forward);
VectorNormalize(pmove->right);
// Determine x and y parts of velocity
for (i = 0; i < 2; i++)
{
wishvel[i] = pmove->forward[i] * fmove + pmove->right[i] * smove;
}
// Zero out z part of velocity
wishvel[2] = 0;
// Determine maginitude of speed of move
VectorCopy(wishvel, wishdir);
wishspeed = VectorNormalize(wishdir);
// Clamp to server defined max speed
if (wishspeed > pmove->maxspeed)
{
VectorScale(wishvel, pmove->maxspeed / wishspeed, wishvel);
wishspeed = pmove->maxspeed;
}
// Set pmove velocity
pmove->velocity[2] = 0;
PM_Accelerate(wishdir, wishspeed, pmove->movevars->accelerate);
pmove->velocity[2] = 0;
// Add in any base velocity to the current velocity.
VectorAdd(pmove->velocity, pmove->basevelocity, pmove->velocity);
spd = Length(pmove->velocity);
if (spd < 1.0)
{
VectorClear(pmove->velocity);
return;
}
// If we are not moving, do nothing
//if (!pmove->velocity[0] && !pmove->velocity[1] && !pmove->velocity[2])
// return;
oldonground = pmove->onground;
// first try just moving to the destination
dest[0] = pmove->origin[0] + pmove->velocity[0] * pmove->frametime;
dest[1] = pmove->origin[1] + pmove->velocity[1] * pmove->frametime;
dest[2] = pmove->origin[2];
// first try moving directly to the next spot
// VectorCopy(dest, start);
trace = pmove->PM_PlayerTrace(pmove->origin, dest, PM_NORMAL, -1);
// If we made it all the way, then copy trace end
// as new player position.
if (trace.fraction == 1.0f)
{
VectorCopy(trace.endpos, pmove->origin);
return;
}
// Don't walk up stairs if not on ground.
if (oldonground == -1 && pmove->waterlevel == 0)
{
return;
}
// If we are jumping out of water, don't do anything more.
if (pmove->waterjumptime)
return;
// Try sliding forward both on ground and up 16 pixels
// take the move that goes farthest
// Save out original pos &
VectorCopy(pmove->origin, original);
// velocity.
VectorCopy(pmove->velocity, originalvel);
// Slide move
clip = PM_FlyMove();
// Copy the results out
VectorCopy(pmove->origin, down);
VectorCopy(pmove->velocity, downvel);
// Reset original values.
VectorCopy(original, pmove->origin);
VectorCopy(originalvel, pmove->velocity);
// Start out up one stair height
VectorCopy(pmove->origin, dest);
dest[2] += pmove->movevars->stepsize;
trace = pmove->PM_PlayerTrace(pmove->origin, dest, PM_NORMAL, -1);
// If we started okay and made it part of the way at least,
// copy the results to the movement start position and then
// run another move try.
if (!trace.startsolid && !trace.allsolid)
{