stwFP = stwFP or {}
local fpEnabled = false
local nonFPWeapons = {
['weapon_physgun'] = true,
['gmod_tool'] = true,
['weapon_physcannon'] = true,
['gmod_camera'] = true,
}
function stwFP.hideHead(b)
local ply = LocalPlayer()
if b then
ply:ManipulateBoneScale(ply:LookupBone('ValveBiped.Bip01_Head1') or 0, Vector(0.01, 0.01, 0.01))
else
ply:ManipulateBoneScale(ply:LookupBone('ValveBiped.Bip01_Head1') or 0, Vector(1, 1, 1))
end
ply.headHided = b
end
function stwFP.blind(b)
if b then
hook.Add('RenderScreenspaceEffects', 'qwb.fp.setBlind', function()
DrawColorModify({
['$pp_colour_addr'] = 0, -- no change
['$pp_colour_addg'] = 0, -- no change
['$pp_colour_addb'] = 0, -- no change
['$pp_colour_brightness'] = -1,
['$pp_colour_contrast'] = 1, -- no change
['$pp_colour_colour'] = 1, -- no change
['$pp_colour_mulr'] = 0, -- no change
['$pp_colour_mulg'] = 0, -- no change
['$pp_colour_mulb'] = 0, -- no change
})
end)
else
hook.Remove('RenderScreenspaceEffects', 'qwb.fp.setBlind')
end
end
function stwFP.startFP()
fpEnabled = true
if not LocalPlayer().headHided then stwFP.hideHead(true) end
local texturesHit = false
hook.Add('RenderScreenspaceEffects', 'qwb.fp.effects', function()
if not texturesHit then return end
DrawColorModify({
['$pp_colour_addr'] = 0, -- no change
['$pp_colour_addg'] = 0, -- no change
['$pp_colour_addb'] = 0, -- no change
['$pp_colour_brightness'] = -1,
['$pp_colour_contrast'] = 1, -- no change
['$pp_colour_colour'] = 1, -- no change
['$pp_colour_mulr'] = 0, -- no change
['$pp_colour_mulg'] = 0, -- no change
['$pp_colour_mulb'] = 0, -- no change
})
end)
hook.Add('CalcView', 'qwb.fp', function(ply, origin, ang, fov, znear, zfar)
if not IsValid(LocalPlayer()) then return end
if ply ~= LocalPlayer() then return end
if not LocalPlayer():LookupBone('ValveBiped.Bip01_Head1') then return end
if ply:Alive() then
local pos, _ = LocalPlayer():GetBonePosition(LocalPlayer():LookupBone('ValveBiped.Bip01_Head1'))
local cameraPos = pos + ang:Right() * 0
cameraPos = cameraPos + ang:Up() * 4
cameraPos = cameraPos + ply:GetForward() * 1
local filter = player.GetAll()
filter[#filter + 1] = ply
filter[#filter + 1] = ply:GetActiveWeapon()
table.Add(filter, ents.FindByClass('gmod_sent_vehicle_fphysics_base'))
local tr = util.TraceHull({
start = pos,
endpos = cameraPos,
filter = filter,
mins = Vector(-4.5, -4.5, -4.5),
maxs = Vector(4.5, 4.5, 4.5),
})
cameraPos = tr.HitPos
if tr.Hit and not texturesHit then
texturesHit = true
elseif not tr.Hit and texturesHit then
texturesHit = false
end
-- if not qwb.ironsighted and qwb.IronSightLerp then
-- local sub = qwb.IronSightLerp - cameraPos
-- if math.abs(sub.x) > 1.5 or math.abs(sub.y) > 1.5 or math.abs(sub.z) > 1.5 then
-- qwb.IronSightLerp = Lerp(0.4, qwb.IronSightLerp, cameraPos)
-- else
-- qwb.IronSightLerp = nil
-- end
-- elseif qwb.ironsighted and not qwb.IronSightLerp then
-- qwb.IronSightLerp = cameraPos
-- end
return {
-- origin = qwb.IronSightLerp or cameraPos,
origin = cameraPos,
angles = Angle((ang.p > 65 and 65) or (ang.p < -65 and -65) or ang.p, ang.y + math.sin(SysTime() / 1.5) * 0.5, ang.r + math.sin(SysTime()) * 0.5),
fov = fov,
drawviewer = true,
}
else
if IsValid(ply:GetRagdollEntity()) then
if texturesHit then texturesHit = false end
local ragdoll = ply:GetRagdollEntity()
local bone = ragdoll:LookupBone('ValveBiped.Bip01_Head1') or 0
if ragdoll:GetManipulateBoneScale(bone) ~= Vector(0.01, 0.01, 0.01) then ragdoll:ManipulateBoneScale(bone, Vector(0.01, 0.01, 0.01)) end
local pos, angles = ragdoll:GetBonePosition(bone)
return {
origin = pos,
angles = angles + Angle(-90, 0, -90),
fov = fov,
drawviewer = false,
}
end
end
end)
hook.Add('InputMouseApply', 'qwb.fp', function(cmd, x, y, ang)
ang.p = math.Clamp(ang.p + y / 50, -65, 65)
ang.y = ang.y - x / 50
cmd:SetViewAngles(ang)
return true
end)
hook.Add('HUDShouldDraw', 'qwb.fp.crosshair', function(name) if name == 'CHudCrosshair' then return false end end)
end
function stwFP.stopFP()
hook.Remove('CalcView', 'qwb.fp')
hook.Remove('InputMouseApply', 'qwb.fp')
hook.Remove('RenderScreenspaceEffects', 'qwb.fp.effects')
fpEnabled = false
if LocalPlayer().headHided then stwFP.hideHead(false) end
end
hook.Add('qwb.fp.canEnableFP', 'qwb.fp', function()
if not IsValid(LocalPlayer()) then return end
if not IsValid(LocalPlayer():GetActiveWeapon()) then return end
if GetViewEntity() ~= LocalPlayer() then return false end
if nonFPWeapons[LocalPlayer():GetActiveWeapon():GetClass()] then return false end
end)
hook.Add('Think', 'qwb.fp.waiting', function()
if not LocalPlayer() or not IsValid(LocalPlayer()) or LocalPlayer() == NULL then return end
if hook.Run('qwb.fp.canEnableFP') ~= false and not fpEnabled then
stwFP.startFP()
elseif hook.Run('qwb.fp.canEnableFP') == false and fpEnabled then
stwFP.stopFP()
end
if LocalPlayer():Alive() and IsValid(LocalPlayer():GetRagdollEntity()) then
local ragdoll = LocalPlayer():GetRagdollEntity()
if ragdoll:GetManipulateBoneScale(ragdoll:LookupBone('ValveBiped.Bip01_Head1') or 0) ~= Vector(1, 1, 1) then ragdoll:ManipulateBoneScale(ragdoll:LookupBone('ValveBiped.Bip01_Head1') or 0, Vector(1, 1, 1)) end
end
end)
timer.Create('qwb.fp.checkHead', 3.5, 0, function() if LocalPlayer().headHided and LocalPlayer():GetManipulateBoneScale(LocalPlayer():LookupBone('ValveBiped.Bip01_Head1') or 0) ~= Vector(0.01, 0.01, 0.01) then stwFP.hideHead(true) end end)