Skip to content

Instantly share code, notes, and snippets.

@NanoAi
Last active April 20, 2017 00:21
Show Gist options
  • Save NanoAi/f85bd7abcae5f16e26009d981a71b89f to your computer and use it in GitHub Desktop.
Save NanoAi/f85bd7abcae5f16e26009d981a71b89f to your computer and use it in GitHub Desktop.
-- Forked from https://git.io/vShFa
-- Edited by LuaTenshi
local function GetFallDamage(ply, len)
ply.fdmg_read = true
return hook.Run("GetFallDamage", ply, len)
end
hook.Add("Move", "realistic_falldamage", function(ply, data)
local vel = data:GetVelocity()
if ply.fdmg_last_vel and ply:GetMoveType() == MOVETYPE_WALK then
local diff = vel - ply.fdmg_last_vel
local len = diff:Length()
if len > 500 then
local params = {}
params.start = data:GetOrigin() + ply:OBBCenter()
params.endpos = params.start - diff:GetNormalized() * ply:BoundingRadius()
params.filter = ply
params.mins = ply:OBBMins()
params.maxs = ply:OBBMaxs()
local res = util.TraceHull(params)
local dmg = (len - 500) / 4
if not res.HitWorld and res.Entity:IsValid() then
local owner = res.Entity.CPPIGetOwner and res.Entity:CPPIGetOwner()
if owner and not owner:CanAlter(ply) then return end
local info = DamageInfo()
info:SetDamagePosition(data:GetOrigin())
info:SetDamage(dmg)
info:SetDamageType(DMG_FALL)
info:SetAttacker(ply)
info:SetInflictor(ply)
info:SetDamageForce(ply.fdmg_last_vel)
res.Entity:TakeDamageInfo(info)
end
local z = math.abs(res.HitNormal.z)
if res.Hit and (z < 0.1 or z > 0.9) then
local fall_damage = GetFallDamage(ply, len) -- Prepare Override & Check Expected Fall Damage
if fall_damage <= 0 then return end
local pos = data:GetOrigin()
if hook.Run("RealisticFallDamage", ply, pos, dmg, len, res, params) ~= true then
local info = DamageInfo()
info:SetDamagePosition(pos)
info:SetDamage(dmg)
info:SetDamageType(DMG_FALL)
info:SetAttacker(Entity(0))
info:SetInflictor(Entity(0))
info:SetDamageForce(ply.fdmg_last_vel)
if hook.Run("RealisticFallDamageInfo", ply, info, fall_damage) ~= true then
ply:TakeDamageInfo(info)
end
end
end
end
end
ply.fdmg_last_vel = vel
end)
hook.Add("EntityTakeDamage", "realistic_falldamage", function(ply, di)
if di:IsFallDamage() then
if ply.fdmg_read then
ply.fdmg_read = nil
else
return true
end
end
end)
@NanoAi
Copy link
Author

NanoAi commented Apr 20, 2017

This is an edited version of PAC3-Server/notagain/../realistic_falldamage.lua made with love by me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment