-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsv_pvp_movespeed.lua
111 lines (87 loc) · 3.56 KB
/
sv_pvp_movespeed.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
-- default run and walk speed with 0 weapons
local normalRunSpeed = 400
local normalWalkSpeed = 200
local slowWalkSpeed = 100 -- Default GMod speed when holding +walk, not configurable.
-- minimum run and walk speed ( must be greater than 0 )
local minRunSpeed = 70
local minWalkSpeed = 35
local weaponWeights = {
weapon_lfsmissilelauncher = 6,
glide_homing_launcher = 5,
ins2_atow_rpg7 = 6,
weapon_rpg = 5,
tfa_l4d2mw_riotshield = 5,
m9k_suicide_bomb = 5, -- experimental
m9k_minigun = 5,
}
local plyMeta = FindMetaTable( "Player" )
plyMeta.o_SetRunSpeed = plyMeta.o_SetRunSpeed or plyMeta.SetRunSpeed
local o_SetRunSpeed = plyMeta.o_SetRunSpeed
plyMeta.o_SetWalkSpeed = plyMeta.o_SetWalkSpeed or plyMeta.SetWalkSpeed
local o_SetWalkSpeed = plyMeta.o_SetWalkSpeed
-- Helper Functions --
local function movementMultiplier( weight )
if weight < 1 then return 1 end
local multiplier = 1 - ( 1.9 ^ weight ) / 100
return math.Clamp( multiplier, 0, 1 )
end
local function getBaseRunSpeed( ply )
return ply.CFC_PlyMS_BaseRunSpeed or normalRunSpeed
end
local function getBaseWalkSpeed( ply )
return ply.CFC_PlyMS_BaseWalkSpeed or normalWalkSpeed
end
local function setSpeedFromWeight( ply, totalWeight )
local multiplier = movementMultiplier( totalWeight )
local baseRunSpeed = getBaseRunSpeed( ply )
local baseWalkSpeed = getBaseWalkSpeed( ply )
local newRunSpeed = baseRunSpeed * multiplier
local newWalkSpeed = baseWalkSpeed * multiplier
o_SetRunSpeed( ply, math.max( newRunSpeed, minRunSpeed ) )
o_SetWalkSpeed( ply, math.max( newWalkSpeed, minWalkSpeed ) )
local slowerThanSlowWalk = newWalkSpeed < slowWalkSpeed
ply:SetCanWalk( not slowerThanSlowWalk ) -- Prevent +walk from letting the player move faster when overencumbered, without having to manage a third speed type
end
local function getWeaponWeight( weapon )
if string.sub( weapon:GetClass(), 1, 4 ) == "pac_" then return 0 end
return weaponWeights[weapon:GetClass()] or 0
end
local function getPlayerWeight( ply )
if ply.IsInBuild and ply:IsInBuild() then return 0 end
local activeWeapon = ply:GetActiveWeapon()
local totalWeight = 0
if IsValid( activeWeapon ) then
totalWeight = getWeaponWeight( activeWeapon )
end
return totalWeight
end
-- Wrappers --
function plyMeta:SetRunSpeed( speed )
local weight = getPlayerWeight( self )
self.CFC_PlyMS_BaseRunSpeed = speed or normalRunSpeed
setSpeedFromWeight( self, weight )
end
function plyMeta:SetWalkSpeed( speed )
local weight = getPlayerWeight( self )
self.CFC_PlyMS_BaseWalkSpeed = speed or normalWalkSpeed
setSpeedFromWeight( self, weight )
end
-- New Player:() Functions --
-- Sets run and walk speed at the same time
function plyMeta:SetMoveSpeed( runSpeed, walkSpeed )
local weight = getPlayerWeight( self )
self.CFC_PlyMS_BaseRunSpeed = runSpeed or normalRunSpeed
self.CFC_PlyMS_BaseWalkSpeed = walkSpeed or normalWalkSpeed
setSpeedFromWeight( self, weight ) -- Avoid double-calling this by not using :SRS() and :SWS()
end
-- Sets run and walk speed based on a multiplier of the default speed
function plyMeta:SetMoveSpeedMultiplier( multiplier )
multiplier = math.max( multiplier or 1, 0 )
self:SetMoveSpeed( normalRunSpeed * multiplier, normalWalkSpeed * multiplier )
end
-- Hook Functions --
local function onWeaponSwitch( ply, _, wep )
setSpeedFromWeight( ply, getWeaponWeight( wep ) )
end
-- Hooks --
hook.Add( "PlayerSwitchWeapon", "CFC_PlyMS_PlayerSwitchWeapon", onWeaponSwitch )