Skip to content

Commit dd4d1e9

Browse files
committed
serverside ragdoll
Player ragdolls are now server-side ( On Dobrograd this is already there, but for the addon it's necessary, otherwise I can't implement to make it work for players )
1 parent 120b972 commit dd4d1e9

File tree

1 file changed

+99
-1
lines changed

1 file changed

+99
-1
lines changed

putrefaction/lua/autorun/sh_crunchydecay.lua

+99-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,102 @@ if CLIENT then
5555
end
5656
)
5757
end
58-
end
58+
end
59+
60+
---
61+
62+
if CLIENT then return end
63+
AddCSLuaFile("cl_serversideragdolls.lua")
64+
util.AddNetworkString("ragColor")
65+
local meta = FindMetaTable("Player")
66+
if not meta then return end
67+
if not meta.CreateRagdollOld then meta.CreateRagdollOld = meta.CreateRagdoll end
68+
local EnableServerSideRagdolls = CreateConVar("sv_ragdolls", "1", {FCVAR_REPLICATED, FCVAR_NOTIFY}, "Enable server side player corpse ragdolls.")
69+
local RagdollSpeedMultiplier = CreateConVar("sv_ragdolls_speed", "1", {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "Speed multiplier of player corpse ragdolls.")
70+
local RagdollWeightMultiplier = CreateConVar("sv_ragdolls_weight", "1", {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "Weight multiplier of player corpse ragdolls.")
71+
local RagdollsCollideWithPlayers = CreateConVar("sv_ragdolls_collide_players", "1", {FCVAR_REPLICATED, FCVAR_ARCHIVE}, "Whether player corpses should collide with players or not.")
72+
function meta:CreateRagdoll()
73+
if not EnableServerSideRagdolls:GetBool() then
74+
self:CreateRagdollOld()
75+
return
76+
end
77+
78+
if not (self or self:IsValid() or self:IsPlayer()) then return end
79+
self.DeathRagdoll = self.DeathRagdoll or false
80+
if self.DeathRagdoll then if self.DeathRagdoll:IsValid() then self.DeathRagdoll:Remove() end end
81+
local ply_pos = self:GetPos()
82+
local ply_ang = self:GetAngles()
83+
local ply_mdl = self:GetModel()
84+
local ply_skn = self:GetSkin()
85+
local ply_col = self:GetColor()
86+
local ply_mat = self:GetMaterial()
87+
local playerModelIsRagdoll = self:GetBoneCount() > 1
88+
local ent
89+
if playerModelIsRagdoll then
90+
ent = ents.Create("prop_ragdoll")
91+
else
92+
ent = ents.Create("prop_physics")
93+
end
94+
95+
ent:SetPos(ply_pos)
96+
ent:SetAngles(ply_ang - Angle(ply_ang.p, 0, 0))
97+
ent:SetModel(ply_mdl)
98+
ent:SetSkin(ply_skn)
99+
ent:SetColor(ply_col)
100+
ent:SetMaterial(ply_mat)
101+
ent:SetCreator(self)
102+
for k, v in ipairs(self:GetBodyGroups()) do
103+
--MsgN("Setting " .. self:GetBodygroupName(v.id).. " to " .. self:GetBodygroup(v.id))
104+
ent:SetBodygroup(v.id, self:GetBodygroup(v.id))
105+
end
106+
107+
ent:Spawn()
108+
local phys = ent:GetPhysicsObject()
109+
if phys:IsValid() then phys:SetMass(phys:GetMass() * RagdollWeightMultiplier:GetFloat()) end
110+
if not ent:IsValid() then return end
111+
self.DeathRagdoll = ent
112+
local plyvel = self:GetVelocity()
113+
if playerModelIsRagdoll then
114+
-- Position and rotate the ragdoll's limbs, set their velocity
115+
for i = 0, ent:GetPhysicsObjectCount() - 1 do
116+
local bone = ent:GetPhysicsObjectNum(i)
117+
if bone and bone:IsValid() then
118+
local bonepos, boneang = self:GetBonePosition(ent:TranslatePhysBoneToBone(i))
119+
bone:SetPos(bonepos)
120+
bone:SetAngles(boneang)
121+
bone:SetVelocity(plyvel * RagdollSpeedMultiplier:GetFloat())
122+
end
123+
end
124+
125+
-- Pose the ragdoll's face
126+
ent:SetFlexScale(self:GetFlexScale())
127+
for i = 1, ent:GetFlexNum() do
128+
ent:SetFlexWeight(i, self:GetFlexWeight(i))
129+
end
130+
131+
-- Set the ragdoll's playermodel color
132+
local ply_ragcol = self:GetPlayerColor()
133+
ent.RagColor = Vector(ply_ragcol.r, ply_ragcol.g, ply_ragcol.b)
134+
net.Start("ragColor")
135+
net.WriteInt(ent:EntIndex(), 16)
136+
net.WriteVector(ent.RagColor)
137+
net.Broadcast()
138+
else
139+
phys:SetVelocity(plyvel * RagdollSpeedMultiplier:GetFloat())
140+
end
141+
142+
ent:SetCreator(self)
143+
if self:IsOnFire() then ent:Ignite(math.Rand(6, 8), 0) end
144+
if not RagdollsCollideWithPlayers:GetBool() then ent:SetCollisionGroup(COLLISION_GROUP_WEAPON) end
145+
self:SpectateEntity(ent)
146+
self:Spectate(OBS_MODE_CHASE)
147+
end
148+
149+
if SERVER then return end
150+
151+
net.Receive("ragColor", function(len)
152+
local rag = Entity(net.ReadInt(16))
153+
local col = net.ReadVector()
154+
155+
rag.GetPlayerColor = function() return col end
156+
end)

0 commit comments

Comments
 (0)