-
Notifications
You must be signed in to change notification settings - Fork 3
/
source_movesystem.lua
1207 lines (825 loc) · 28.4 KB
/
source_movesystem.lua
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 2020 David Mentler
-- Enums
WL_NONE = 0
WL_FEET = 1
WL_WAIST = 2
WL_EYES = 3
-- Localized for fast access
local ZERO = Vector()
local bit_bnot = bit.bnot
local bit_band = bit.band
local bit_bor = bit.bor
local bit_bxor = bit.bxor
local math_min = math.min
local math_max = math.max
local math_sqrt = math.sqrt
local math_Clamp = math.Clamp
local util_TraceHull = util.TraceHull
local util_PointContents = util.PointContents
-- Vector metatable extensions
do
local meta = FindMetaTable( "Vector" )
function meta:Copy()
return Vector( self.x, self.y, self.z )
end
function meta:Size( len )
self:Normalize()
self.x = self.x * len
self.y = self.y * len
self.z = self.z * len
return self
end
end
-- Player metatable extensions
do
local function Accessor( meta, varname, name, def )
meta[ "Set" .. name ] = function( self, var ) self[varname] = var end
meta[ "Get" .. name ] = function( self ) return self[varname] or def end
end
local meta = FindMetaTable( "Player" )
function meta:GetPlayerSolidMask()
return MASK_PLAYERSOLID
end
Accessor( meta, "m_WaterType", "WaterType" )
Accessor( meta, "m_WaterLevel", "WaterLevel" )
Accessor( meta, "m_BaseVelocity", "BaseVelocity" )
Accessor( meta, "m_SurfaceFriction", "SurfaceFriction" )
function meta:InWater()
return self.m_WaterLevel > WL_NONE
end
function meta:AdjustBaseVelocity( delta )
self:SetBaseVelocity( self:GetBaseVelocity() + delta )
end
function meta:GetGravityFactor()
local grav = self:GetGravity()
return grav == 0 and 1 or grav
end
-- Move state flags
Accessor( meta, "m_Ducked", "Ducked" ) -- This replaces the FL_DUCKING engine flag
Accessor( meta, "m_Ducking", "DuckingState" )
-- Timers used while moving
Accessor( meta, "m_DuckTime", "DuckTime" )
Accessor( meta, "m_JumpTime", "JumpTime" )
Accessor( meta, "m_DuckJumpTime", "DuckJumpTime" )
Accessor( meta, "m_WaterJumpTime", "WaterJumpTime" )
Accessor( meta, "m_SwimSoundTime", "SwimSoundTime" )
function meta:ReduceMoveTimers()
local delta = 1000 * FrameTime()
self.m_DuckTime = math_max( self.m_DuckTime - delta, 0 )
self.m_JumpTime = math_max( self.m_JumpTime - delta, 0 )
self.m_DuckJumpTime = math_max( self.m_DuckJumpTime - delta, 0 )
self.m_SwimSoundTime = math_max( self.m_SwimSoundTime - delta, 0 )
end
end
-- Utility functions
local function isFlagSet( num, mask )
return bit_band( num, mask ) != 0
end
local function isWater( mask )
return bit_band( mask, MASK_WATER ) != 0
end
local function VectorMA( add, scale, vec )
return add + vec:GetNormalized() * scale
end
-- Shared variables for easier access
local mv_Player
local mv_Object
local mv_Origin
local mv_Velocity
local mv_FrameTime
local mv_Buttons
local mv_OldButtons
-- Overridable functions
local function GetPlayerMins( ducked )
return mv_Player:OBBMins()
end
local function GetPlayerMaxs( ducked )
-- If not speficied, default to current state
if ( ducked == nil ) then
ducked = mv_Player.m_Ducked
end
local maxs = Vector( 16, 16, 72 ) -- TODO: Why hardcoded?! :(
if ( ducked ) then
maxs.z = 36
end
return maxs
end
local tdata = {}
local function TracePlayerBBox( start, endpos, mask, group )
tdata.start = start
tdata.endpos = endpos
tdata.mask = mask or mv_Player:GetPlayerSolidMask()
tdata.filter = mv_Player
tdata.mins = GetPlayerMins()
tdata.maxs = GetPlayerMaxs()
-- Ignore world and walls?
local ground = mv_Player:GetGroundEntity()
if ( IsValid( ground ) and ground:GetModel():match( "plate" ) ) then
tdata.ignoreworld = true
else
tdata.ignoreworld = false
end
return util_TraceHull( tdata )
end
local function TryTouchGround( start, endpos, mask, group )
return TracePlayerBBox( start, endpos, mask, group )
end
local function CanAccelerate()
return true
end
local function CanUnDuck()
local newOrigin = mv_Origin
if ( mv_Player:GetGroundEntity() != NULL ) then
newOrigin = newOrigin + (GetPlayerMins( true ) - GetPlayerMins( false ))
else
-- If in air while letting go of crouch, make sure we can offset the origin to
-- make up for crouching
local hullNormal = GetPlayerMaxs( false ) - GetPlayerMins( false )
local hullDuck = GetPlayerMaxs( true ) - GetPlayerMins( true )
local delta = hullNormal - hullDuck
newOrigin = newOrigin - delta
end
-- Check if there is space to stand up (fake that we are standing)
mv_Player.m_Ducked = false
local trace = TracePlayerBBox( newOrigin, newOrigin )
mv_Player.m_Ducked = true
return !trace.StartSolid and trace.Fraction == 1
end
local function GetCurrentGravity()
return 600
end
local function GetAirSpeedCap()
return 30
end
-- Movesystem methods
local function SimpleSpline( value )
local squared = value * value
return ( 3 * squared - 2 * squared * value )
end
local function ClipVelocity( vector, normal, bounce )
-- Determine how far to slide along the given plane, based off the direction
local backoff = vector:Dot( normal ) * bounce
vector = vector - normal*backoff
-- Iterate once to make sure we are not moving trough the plane
local adjust = vector:Dot( normal )
if ( adjust < 0 ) then
vector = vector - normal * adjust
end
return vector
end
local function ValidateVelocity()
-- Make sure none of the compontents exceed sv_maxvelocity
local sv_maxvelocity = 3500 -- TODO: Make this a convar
mv_Velocity.x = math_Clamp( mv_Velocity.x, -sv_maxvelocity, sv_maxvelocity )
mv_Velocity.y = math_Clamp( mv_Velocity.y, -sv_maxvelocity, sv_maxvelocity )
mv_Velocity.z = math_Clamp( mv_Velocity.z, -sv_maxvelocity, sv_maxvelocity )
end
local function ApplyBaseVelocity()
mv_Velocity = mv_Velocity + mv_Player:GetBaseVelocity()
end
local function RemoveBaseVelocity()
mv_Velocity = mv_Velocity - mv_Player:GetBaseVelocity()
end
local function Friction()
-- Calculate speed
local speed = mv_Velocity:Length()
-- Do not care to apply on low speed
if ( speed < 0.1 ) then
return
end
-- Apply ground friction
local drop = 0
if ( mv_Player:GetGroundEntity() != NULL ) then
local sv_friction = 8 -- TODO: Make these a convar
local sv_stopspeed = 10
local friction = sv_friction * mv_Player.m_SurfaceFriction
local control = sv_stopspeed > speed and sv_stopspeed or speed
drop = math_max( control * friction * mv_FrameTime, 0 )
end
-- Scale the velocity
local newspeed = speed - drop
if ( newspeed != speed ) then
mv_Velocity:Size( newspeed )
end
end
local function AcceleratePlayer( inAir )
-- Determine movement angles
local moveAngs = mv_Object:GetMoveAngles()
local vForward = moveAngs:Forward()
local vRight = moveAngs:Right()
-- Zero Z components
vForward.z = 0
vRight.z = 0
-- Determine velocity
local wishvel = mv_Object:GetForwardSpeed() * vForward
+ mv_Object:GetSideSpeed() * vRight
local wishdir = wishvel:GetNormalized()
local wishspeed = wishvel:Length()
-- Clamp to max speed
local sMax = mv_Object:GetMaxSpeed()
if ( wishspeed != 0 and wishspeed > sMax ) then
wishvel = wishvel * (sMax/wishspeed)
wishspeed = sMax
end
-- Reduce added speed by the amount of veer
local addspeed = wishspeed - mv_Velocity:DotProduct( wishdir )
-- If not going to add any speed then we are done
if ( addspeed <= 0 ) then
return
end
-- Do inAir specific adjusting
local factor = 0
if ( inAir ) then
-- Cap air speed
wishspeed = math_min( wishspeed, GetAirSpeedCap() )
factor = 10 -- sv_airaccelerate TODO: Make this a convar
else
factor = 10 -- sv_accelerate TODO: Make this a convar
end
-- Determine amount of acceleration
local accelspeed = wishspeed * mv_Player.m_SurfaceFriction * mv_FrameTime * factor
-- Cap at addspeed
accelspeed = math_min( accelspeed, addspeed )
-- Adjust velocity
mv_Velocity = mv_Velocity + accelspeed * wishdir
end
local function SetGroundEntity( ground )
mv_Player:SetGroundEntity( ground )
end
local function CheckWater()
-- Pick a spot just above the players feet
local mins = GetPlayerMins()
local maxs = GetPlayerMaxs()
local spot = mv_Origin + ( mins + maxs ) /2
spot.z = mv_Origin.z + mins.z +1
-- Assume that we are not in water, at all
mv_Player:SetWaterType( CONTENTS_EMPTY )
mv_Player:SetWaterLevel( WL_NONE )
-- Query point contents
local flags = util_PointContents( spot )
-- In water?
if isWater( flags ) then
-- Set water type/depth
mv_Player:SetWaterType( flags )
mv_Player:SetWaterLevel( WL_FEET )
-- Now check the point that is at the hulls midpoint
spot.z = mv_Origin.z + ( mins.z + maxs.z ) /2
if isWater( util_PointContents( spot ) ) then
-- Set a higher water level, and query against the eye position
mv_Player:SetWaterLevel( WL_WAIST )
spot = mv_Origin + mv_Player:GetViewOffset()
if isWater( util_PointContents( spot ) ) then
-- 'Fully' submerged
mv_Player:SetWaterLevel( WL_EYES )
end
end
-- Adjust velocity based on fluid current
local current = bit_band( flags, MASK_CURRENT )
if ( current != 0 ) then
local force = Vector()
-- Figure out current direction
if bit_band( current, CONTENTS_CURRENT_0 ) then force.x = 1 end
if bit_band( current, CONTENTS_CURRENT_90 ) then force.y = 1 end
if bit_band( current, CONTENTS_CURRENT_180 ) then force.x = -1 end
if bit_band( current, CONTENTS_CURRENT_270 ) then force.y = -1 end
if bit_band( current, CONTENTS_CURRENT_UP ) then force.z = 1 end
if bit_band( current, CONTENTS_CURRENT_DOWN ) then force.z = -1 end
-- Scale force, depending on depth
mv_Player:AdjustBaseVelocity( force:Size( mv_Player:GetWaterLevel() *50 ) )
end
end
return mv_Player:InWater()
end
local function CategorizePosition()
-- Reset surface friction
mv_Player.m_SurfaceFriction = 1
-- Update this before anything else, as we may get stuck
-- on the bottom of the water otherwise
CheckWater()
-- Observers have no ground entity
if ( mv_Player:GetObserverMode() != OBS_MODE_NONE ) then
return
end
-- Check Z axis velocity if we are moving upwards, then we have no ground
local velZ = mv_Velocity.z
local movingUp = velZ > 0
local movingUpRapidly = velZ > 140 -- NON_JUMP_VELOCITY
if ( movingUpRapidly || ( movingUp and mv_Player:GetMoveType() == MOVETYPE_LADDER ) ) then
SetGroundEntity( NULL )
else
-- Try and move down
local point = mv_Origin + Vector( 0, 0, -2 )
local trace = TryTouchGround( mv_Origin, point )
-- Not on ground, or at a too steep plane
if ( trace.Entity == NULL or trace.HitNormal.z < 0.7 ) then
-- Test quadrants, to see if one of them has a shllower slope to stand on
-- TODO: TryTouchGroundInQuadrants
SetGroundEntity( NULL )
-- Lower surface friction
if ( mv_Velocity.z > 0 and mv_Player:GetMoveType() != MOVETYPE_NOCLIP ) then
mv_Player.m_SurfaceFriction = 0.25
end
else
SetGroundEntity( trace.Entity )
end
end
end
local function CheckJumpButton()
-- TODO: Ply dead, water jumping
-- TODO: In water, jumping out
-- See if waterjumping, if so then decrement time and return
-- TODO: This actually never happens? Even the SDK has a bug here
if ( mv_Player.m_WaterJumpTime > 0 ) then
mv_Player.m_WaterJumpTime = mv_Player.m_WaterJumpTime - mv_FrameTime * 1000
return false
end
-- If we are in the water most of the way
if ( mv_Player:GetWaterLevel() > WL_WAIST ) then
-- Swimming, not jumping
SetGroundEntity( NULL )
-- Apply velocity based on water contents
local water = mv_Player:GetWaterType()
if ( water == CONTENTS_WATER ) then
mv_Velocity.z = 100
elseif ( water == CONTENTS_SLIME ) then
mv_Velocity.z = 80
end
-- Play swimming sound
if ( mv_Player.SwimSoundTime <= 0 ) then
mv_Player.SwimSoundTime = 1000
-- TODO: Play sound
end
return false
end
-- No more effect
if ( mv_Player:GetGroundEntity() == NULL ) then
mv_OldButtons = bit_bor( mv_OldButtons, IN_JUMP )
return false
end
-- Don't pogo stick
if ( isFlagSet( mv_OldButtons, IN_JUMP ) ) then
return false
end
-- Cannot jump in the duck transition
if ( mv_Player.m_Ducking and mv_Player.m_Ducked ) then
return false
end
-- Still updating the eye position
if ( mv_Player.m_DuckJumpTime > 0 ) then
return false
end
-- In the air now
SetGroundEntity( NULL )
-- TODO: Step sound
mv_Player:SetAnimation( PLAYER_JUMP )
local groundFactor = 1
-- TODO: Surface data
-- local accel = math_sqrt( 2 * GetCurrentGravity() * ( 21 ) ) -- GAMEMOVEMENT_JUMP_HEIGHT
local accel = mv_Player:GetJumpPower()
-- Accelerate upward
local startZ = mv_Velocity.z
if ( mv_Player.m_Ducking or mv_Player.m_Ducked ) then
mv_Velocity.z = groundFactor * accel
else
mv_Velocity.z = mv_Velocity.z + groundFactor * accel
end
mv_Player.m_JumpTime = 510 -- GAMEMOVEMENT_JUMP_TIME
-- TODO: Gravity, jumptime
-- Flag that we jumped
mv_OldButtons = bit_bor( mv_OldButtons, IN_JUMP )
return true
end
-- Position adjusting code
local function StayOnGround()
-- Offset positions a bit, so it fits the player anatomy
local start = mv_Origin:Copy()
local feet = mv_Origin:Copy()
start.z = start.z + 2
feet.z = feet.z - mv_Player:GetStepSize()
-- See how far up we can go without getting stuck
local trace = TracePlayerBBox( mv_Origin, start )
start = trace.HitPos
-- Now trace down from a known safe position
local trace = TracePlayerBBox( start, feet )
if ( trace.Fraction > 0 and -- must go somewhere
trace.Fraction < 1 and -- must hit something
!trace.StartSolid and -- can't be embeeded in solid
trace.HitNormal.z > 0.7 -- can't hit a steep slope we can't stand on anyway
) then
-- Adjust position
mv_Origin = trace.HitPos
end
end
local function TryPlayerMove( origin, velocity )
local maxBumps = 4
local mv_Origin = origin:Copy()
local mv_Velocity = velocity:Copy()
local timeLeft = mv_FrameTime
local cPlanes = {}
local cPlaneNum = 0
local allFrac = 0
for bump = 1, maxBumps do
-- Already stopped
if ( mv_Velocity:LengthSqr() == 0 ) then
break
end
-- Assume we can reach the target in one step
local target = mv_Origin + mv_Velocity * timeLeft
-- Check how far it goes
local trace = TracePlayerBBox( mv_Origin, target )
-- Started in solid, we are stuck there. Zero the velocity and stop bumping
if ( trace.StartSolid ) then
mv_Velocity = ZERO
break
end
-- If we moved some portion of the distance, add it to the fraction,
-- and reset the plane counter
if ( trace.Fraction > 0 ) then
-- TODO: The SDK here refers to a bug, where the player might end up stuck in a displacement.
-- maybe I should implement that?
mv_Origin = trace.HitPos
cPlaneNum = 0
end
-- Record progress and decrease timeLeft
allFrac = allFrac + trace.Fraction
timeLeft = timeLeft * ( 1 - trace.Fraction )
-- Managed to move in one step, stop bumping
if ( trace.Fraction == 1 ) then
break
end
-- TODO: Save the entity that blocked us
-- TODO: What is blocking? blocked flags
-- Add blocking plane to the plane list
cPlaneNum = cPlaneNum +1
cPlanes[cPlaneNum] = trace.HitNormal
-- Modify velocity so it does not intersect with each plane
local pIndex = 1
local cIndex = 1
while ( pIndex <= cPlaneNum ) do
mv_Velocity = ClipVelocity( mv_Velocity, cPlanes[pIndex], 1 )
-- Check if we are now moving against an other plane
cIndex = 1
while ( cIndex <= cPlaneNum ) do
if ( mv_Velocity:Dot( cPlanes[cIndex] ) < 0 ) then
-- We are going against this plane, uh oh
break
end
cIndex = cIndex +1
end
-- Did not have to clip anything, so we are fine
if ( cIndex > cPlaneNum ) then
break
end
pIndex = pIndex +1
end
-- Check if clipping did not work out a compatible direction
if ( pIndex > cPlaneNum +1 ) then
if ( cPlaneNum != 2 ) then
-- Can't work it out, stuck
mv_Velocity = ZERO
break
end
-- Only two planes, move along the crease
local dir = cPlanes[1]:Cross( cPlanes[2] )
dir:Normalize()
mv_Velocity = dir * dir:Dot( mv_Velocity )
end
-- If the new velocity is against the original
if( mv_Velocity:Dot( velocity ) < 0 ) then
-- Stop to avoid oscillation
mv_Velocity = ZERO
break
end
end
-- Did not move even a little?
if ( allFrac <= 0 ) then
mv_Velocity = ZERO
end
-- TODO: Check if the player slammed into a wall
return mv_Origin, mv_Velocity
end
-- Duck, and all of its bullshit
local function SetDuckedEyeOffset( frac )
local normal = mv_Player:GetViewOffset()
local ducked = mv_Player:GetViewOffsetDucked()
local delta = normal - ducked
mv_Player:SetCurrentViewOffset( normal - delta * frac )
end
local function FixPlayerCrouchStuck( upward )
-- First check if we are even stuck at all
local trace = TracePlayerBBox( mv_Origin, mv_Origin )
if ( trace.Entity == NULL ) then
return
end
-- Try to find a safe position to unduck
local origin = mv_Origin:Copy()
-- Do attempts in a 36 unit tall area
for index = 1, 36 do
origin.z = origin.z + ( upward and 1 or -1 )
local trace = TracePlayerBBox( origin, origin )
-- Not stuck here! Set as new origin and finish
if ( trace.Entity == NULL ) then
mv_Origin = origin
return
end
end
end
local function FinishDuck()
-- Change view
SetDuckedEyeOffset( 1 )
-- Fudge for collision bug
if ( mv_Player:GetGroundEntity() != NULL ) then
-- TODO: This is always zero?
mv_Origin = mv_Origin + ( GetPlayerMins( true ) - GetPlayerMins( false ) )
else
-- Offset origin by the duck distance
local normalHull = GetPlayerMaxs( false ) - GetPlayerMins( false )
local duckHull = GetPlayerMaxs( true ) - GetPlayerMins( true )
local delta = normalHull - duckHull
mv_Origin = mv_Origin + delta
end
-- Set flags, and change view offset
mv_Player:AddFlags( FL_DUCKING )
mv_Player.m_Ducked = true
mv_Player.m_Ducking = false
-- Fix getting stuck
FixPlayerCrouchStuck( true )
-- Recategorize position, since the origin might have changed
CategorizePosition()
end
local function FinishUnDuck()
-- Change view
SetDuckedEyeOffset( 0 )
-- Fudge for collision bug
if ( mv_Player:GetGroundEntity() != NULL ) then
-- TODO: This is always zero?
mv_Origin = mv_Origin + ( GetPlayerMins( true ) - GetPlayerMins( false ) )
else
-- Offset origin by the duck distance
local normalHull = GetPlayerMaxs( false ) - GetPlayerMins( false )
local duckHull = GetPlayerMaxs( true ) - GetPlayerMins( true )
local delta = normalHull - duckHull
mv_Origin = mv_Origin - delta
end
-- Set flags, and change view offset
mv_Player:RemoveFlags( FL_DUCKING )
mv_Player.m_Ducked = false
mv_Player.m_Ducking = false
FixPlayerCrouchStuck()
-- Recategorize position, since the origin might have changed
CategorizePosition()
end
local function HandleDuck()
-- Check buttons
local buttonDelta = bit_bxor( mv_OldButtons, mv_Buttons )
local buttonsUp = bit_band( buttonDelta, mv_OldButtons )
local buttonsDown = bit_band( buttonDelta, mv_Buttons )
-- Check to see if we are in the air
local inAir = mv_Player:GetGroundEntity() == NULL
local inDuck = mv_Player.m_Ducked
local inJump = mv_Player.m_JumpTime > 0
local inDuckJump = mv_Player.m_DuckJumpTime > 0
-- Update buttons first
if ( isFlagSet( mv_Buttons, IN_DUCK ) ) then
mv_OldButtons = bit_bor( mv_OldButtons, IN_DUCK )
end
-- TODO: Crop speed of ducking players
-- If the player is holding down the duck button, the player is in duck transition, ducking, or duck-jumping.
if ( isFlagSet( mv_Buttons, IN_DUCK ) or mv_Player.m_Ducking or inDuck or inJump ) then
-- Duck
if ( isFlagSet( mv_Buttons, IN_DUCK ) or inJump ) then
-- Have the duck button pressed, but the player currently isn't in the duck position.
if ( isFlagSet( buttonsDown, IN_DUCK ) and !inDuck and !inDuckJump ) then
mv_Player.m_DuckTime = 1000 -- GAMEMOVEMENT_DUCK_TIME
mv_Player.m_Ducking = true
end
-- The player is in duck transition and not duck-jumping.
if ( mv_Player.m_Ducking and !inDuckJump ) then
local timeleft = math_max( 0, 1000 - mv_Player.m_DuckTime )
if ( timeleft > 200 or inDuck or inAir ) then -- TIME_TO_DUCK
FinishDuck()
else
-- Calc parametric time
SetDuckedEyeOffset( SimpleSpline( timeleft /200 ) ) -- TIME_TO_DUCK
end
end
-- Jumping
if ( inJump ) then
-- TODO: WTF is an UnDuckJump ? Only seems to occur when maxclients == 1
end
else -- Unduck (or attempt to)
-- TODO: WTF is an UnDuckJump ? Only seems to occur when maxclients == 1
-- In duck jump
if ( mv_Player.m_DuckJumpTime > 0 ) then
return
end
-- Try to unduck
if ( inAir or mv_Player.m_Ducking or mv_Player.m_Ducked ) then
-- The duck button is released, but we are not in "duck" and we are not in the air - unduck transition
if ( isFlagSet( buttonsUp, IN_DUCK ) ) then
if ( inDuck and !inJump ) then
mv_Player.m_DuckTime = 1000 -- GAMEMOVEMENT_DUCK_TIME
else
-- Invert time if release before fully ducked!
if ( mv_Player.m_Ducking and !inDuck ) then
local elapsed = 1000 - mv_Player.m_DuckTime -- GAMEMOVEMENT_DUCK_TIME
local frac = elapsed / 200 -- TIME_TO_DUCK
local remain = frac * 200 -- TIME_TO_UNDUCK
mv_Player.m_DuckTime = 1000 - 200 + remain -- GAMEMOVEMENT_DUCK_TIME - UNDUCK_MILISEC + remairingUnduckMiliseconds
end
end
end
-- Check to see if we are allowed to unduck
if ( CanUnDuck() ) then
if ( mv_Player.m_Ducking or mv_Player.m_Ducked ) then
local timeleft = math_max( 0, 1000 - mv_Player.m_DuckTime )
if ( timeleft > 200 or ( inAir and !inJump ) ) then -- TIME_TO_UNDUCK
FinishUnDuck()
else
SetDuckedEyeOffset( SimpleSpline( 1 - timeleft /200 ) ) -- TIME_TO_UNDUCK
mv_Player.m_Ducking = true
end
end
else
-- Still under something that prevents us from unducking, reset the timer
-- to make sure we unduck once we exit the tunnel/whatever was blocking us
if ( mv_Player.m_DuckTime != 1000 ) then
SetDuckedEyeOffset( 1 )
mv_Player.m_DuckTime = 1000
mv_Player.m_Ducked = true
mv_Player.m_Ducking = false
mv_Player:AddFlags( FL_DUCKING )
end
end
end
end
end
end
-- Movement nature
local function StepMove()
-- Try sliding both on ground and up by 16 units,
-- take the one that got farther
-- Slide down
local downOrigin, downVelocity = TryPlayerMove( mv_Origin, mv_Velocity )
-- Move up by the step height, and slide again
local stair = mv_Origin:Copy()
stair.z = stair.z + mv_Player:GetStepSize()
local trace = TracePlayerBBox( mv_Origin, stair )
if ( !trace.StartSolid ) then
stair = trace.HitPos
end
-- Slide up
local upOrigin, upVelocity = TryPlayerMove( stair, mv_Velocity )
-- Move down a stair (attempt to)
stair = upOrigin:Copy()
stair.z = stair.z - mv_Player:GetStepSize()
local trace = TracePlayerBBox( upOrigin, stair )
-- If we are not on the ground, then use the slide over this attempt
if ( trace.HitNormal.z < 0.7 ) then
mv_Origin = downOrigin
mv_Velocity = downVelocity
return
end