Advertisement
CapsAdmin

Untitled

Feb 20th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.08 KB | None | 0 0
  1. local battlecam = {}
  2.  
  3. -- utilities
  4.  
  5. local HOOK = function(event) hook.Add(event, "battlecam", battlecam[event]) end
  6. local UNHOOK = function(event) hook.Remove(event, "battlecam") end
  7.  
  8.  
  9. function battlecam.LimitAngles(pos, dir, fov, prevpos)
  10.     local a1 = dir:Angle()
  11.     local a2 = (pos - prevpos):Angle()
  12.    
  13.     fov = fov / 3
  14.     dir = a2:Forward() *-1
  15.  
  16.     a1.p = a2.p + math.Clamp(math.AngleDifference(a1.p, a2.p), -fov, fov)
  17.     fov = fov / (ScrH()/ScrW())
  18.     a1.y = a2.y + math.Clamp(math.AngleDifference(a1.y, a2.y), -fov, fov)
  19.  
  20.     a1.p = math.NormalizeAngle(a1.p)
  21.     a1.y = math.NormalizeAngle(a1.y)
  22.  
  23.     return LerpVector(math.Clamp(Angle(0, a1.y, 0):Forward():DotProduct(dir), 0, 1), a1:Forward(), dir * -1)
  24. end
  25.  
  26. function battlecam.LimitPos(pos, dir, ply)
  27.     local vehicle = ply:GetVehicle()
  28.  
  29.     local trace_forward = util.TraceHull({
  30.         start = ply:EyePos(),
  31.         endpos = pos,
  32.         mins = ply:OBBMins() / 2,
  33.         maxs = ply:OBBMaxs() / 2,
  34.         filter = ents.FindInSphere(pos, 300),
  35.         mask = MASK_SOLID_BRUSHONLY,
  36.     })
  37.  
  38.     if trace_forward.Hit and trace_forward.Entity ~= ply and not trace_forward.Entity:IsPlayer() and not trace_forward.Entity:IsVehicle() then
  39.         return trace_forward.HitPos + trace_forward.HitNormal * 1
  40.     end
  41.    
  42.     return pos
  43. end
  44.  
  45. function battlecam.FindHeadPos(ent)
  46.     if not ent.bc_head or ent.bc_last_mdl ~= ent:GetModel() then
  47.         for i = 0, ent:GetBoneCount() do
  48.             local name = ent:GetBoneName(i):lower()
  49.             if name:find("head") then
  50.                 ent.bc_head = i
  51.                 ent.bc_last_mdl = ent:GetModel()
  52.                 break
  53.             end
  54.         end
  55.     end
  56.        
  57.     local pos, ang = ent.bc_head and ent:GetBonePosition(ent.bc_head)
  58.  
  59.     return pos or ent:EyePos(), ang or ent:EyeAngles()
  60. end
  61.  
  62. function battlecam.CreateCrosshair()
  63.  
  64.     for k,v in pairs(ents.GetAll()) do
  65.         if v.battlecam_crosshair then
  66.             SafeRemoveEntity(v)
  67.         end
  68.     end
  69.  
  70.     local ent = ClientsideModel("models/hunter/misc/cone1x05.mdl")
  71.    
  72.     ent:SetMaterial("models/shiny")
  73.    
  74.     local mat = Matrix()
  75.         mat:SetAngles(Angle(-90,0,0))
  76.         mat:Scale(Vector(1,1,2) * 0.25)
  77.         mat:Translate(Vector(0,0,-25))
  78.     ent:EnableMatrix("RenderMultiply", mat)
  79.        
  80.     ent.RenderOverride = function(ent)
  81.         local c = Vector(GetConVarString("cl_weaponcolor")) * 1.5
  82.         render.SetColorModulation(c.r ^ 10, c.g ^ 10, c.b ^ 10)
  83.             render.SetBlend(0.75)
  84.                 ent:DrawModel()
  85.             render.SetBlend(1)
  86.         render.SetColorModulation(1, 1, 1)
  87.     end
  88.    
  89.     ent.battlecam_crosshair = true
  90.    
  91.     battlecam.crosshair_ent = ent
  92. end
  93.  
  94. -- common
  95.  
  96. local ply = NULL
  97. local aim_pos = Vector()
  98. local aim_dir = Vector()
  99.  
  100. battlecam.enabled = false
  101.  
  102. for k,v in pairs(ents.GetAll()) do
  103.     if v.battlecam_crosshair then
  104.         SafeRemoveEntity(v)
  105.     end
  106. end
  107. battlecam.crosshair_ent = NULL
  108.  
  109. function battlecam.Enable()
  110.     HOOK("CalcView")
  111.     HOOK("CreateMove")
  112.     HOOK("HUDShouldDraw")
  113.     HOOK("ShouldDrawLocalPlayer")
  114.     HOOK("InputMouseApply")
  115.    
  116.     battlecam.enabled = true
  117.     ply = LocalPlayer()
  118.    
  119.     battlecam.CreateCrosshair()
  120. end
  121.  
  122. function battlecam.Disable()
  123.     UNHOOK("CalcView")
  124.     UNHOOK("CreateMove")
  125.     UNHOOK("HUDShouldDraw")
  126.     UNHOOK("ShouldDrawLocalPlayer")
  127.     UNHOOK("InputMouseApply")
  128.    
  129.     battlecam.enabled = false
  130.    
  131.     SafeRemoveEntity(battlecam.crosshair_ent)
  132. end
  133.  
  134. function battlecam.IsEnabled()
  135.     return battlecam.enabled
  136. end
  137.  
  138. -- hooks
  139.  
  140. do -- view
  141.     battlecam.cam_speed = 10
  142.  
  143.     local smooth_pos = Vector()
  144.     local smooth_dir = Vector()
  145.     local smooth_roll = 0
  146.     local smooth_fov = 0
  147.  
  148.     local last_pos = Vector()
  149.  
  150.     function battlecam.CalcView()
  151.         aim_pos = ply:GetShootPos()
  152.         aim_dir = ply:GetAimVector()
  153.        
  154.         if not battlecam.crosshair_ent:IsValid() then
  155.             battlecam.CreateCrosshair()
  156.         end
  157.        
  158.         battlecam.SetupCrosshair(battlecam.crosshair_ent)
  159.         battlecam.CalcEnemySelect()
  160.        
  161.         local delta = FrameTime()
  162.         local target_pos = aim_pos * 1
  163.         local target_dir = aim_dir * 1
  164.         local target_fov = 60
  165.        
  166.         target_dir.z = 0
  167.        
  168.         -- roll
  169.         local target_roll = 0--math.Clamp(-smooth_dir:Angle():Right():Dot(last_pos - smooth_pos)  * delta * 40, -30, 30)
  170.         last_pos = smooth_pos
  171.        
  172.         -- do a more usefull and less cinematic view if we're holding ctrl
  173.         if ply:KeyDown(IN_DUCK) then
  174.             target_dir = aim_dir * 1
  175.             target_pos = target_pos + aim_dir * - 50
  176.             target_fov = 90
  177.            
  178.             delta = delta * 2
  179.         else
  180.             local ent = battlecam.selected_enemy
  181.            
  182.             if ent:IsValid() then
  183.                 local size = ent:BoundingRadius() * ent:GetModelScale()
  184.                 size = size / 2
  185.                
  186.                 target_dir = (ent:EyePos() - target_pos):GetNormalized()
  187.                 target_dir.z = target_dir.z / 10
  188.                 target_pos = target_pos + target_dir * -(200 + size)
  189.                 target_fov = target_fov - 10           
  190.             else
  191.                 target_dir = battlecam.LimitAngles(target_pos, target_dir, target_fov, smooth_pos)
  192.                 target_pos = target_pos + target_dir * -200
  193.                 target_fov = 60
  194.             end
  195.         end
  196.        
  197.         -- move the camera right a little bit
  198.         target_pos = target_pos + target_dir:Angle():Right() * -30
  199.        
  200.         -- smoothing
  201.         smooth_pos = smooth_pos + ((target_pos - smooth_pos) * delta * battlecam.cam_speed)
  202.         smooth_dir = smooth_dir + ((target_dir - smooth_dir) * delta * battlecam.cam_speed)
  203.         smooth_fov = smooth_fov + ((target_fov - smooth_fov) * delta * battlecam.cam_speed)
  204.         smooth_roll = smooth_roll + ((target_roll - smooth_roll) * delta * battlecam.cam_speed)
  205.        
  206.         -- trace block
  207.         smooth_pos = battlecam.LimitPos(smooth_pos, smooth_dir, ply)
  208.        
  209.         -- return
  210.         local params = {}
  211.        
  212.         params.origin = smooth_pos
  213.         params.angles = smooth_dir:Angle()
  214.         params.angles.r = smooth_roll
  215.         params.fov = smooth_fov
  216.        
  217.         return params
  218.     end
  219. end
  220.  
  221. function battlecam.SetupCrosshair(ent)
  222.     local enemy = battlecam.selected_enemy
  223.    
  224.     if enemy:IsValid() then
  225.         ent:SetPos(enemy:EyePos() + enemy:GetUp() * (15 + math.sin(RealTime() * 20)))
  226.         ent:SetAngles(Angle(-90,0,0))  
  227.     else
  228.         local trace_res = util.QuickTrace(aim_pos, aim_dir * 2500, {ply, ply:GetVehicle()})
  229.  
  230.         ent:SetPos(trace_res.HitPos + Vector(0, 0, math.sin(RealTime() * 10)))
  231.         ent:SetAngles(trace_res.HitNormal:Angle()) 
  232.     end
  233. end
  234.  
  235.  
  236. do -- selection
  237.     battlecam.selected_enemy = NULL
  238.    
  239.     local last_use = 0
  240.    
  241.     function battlecam.CalcEnemySelect()
  242.         if ply:KeyDown(IN_USE) and last_use < RealTime() then
  243.                    
  244.             local end_pos = aim_pos + aim_dir * 2000
  245.             local data = util.TraceHull({
  246.                 start = aim_pos,
  247.                 endpos = end_pos,
  248.                 mins = ply:OBBMins()*2,
  249.                 maxs = ply:OBBMaxs()*2,
  250.                 filter = ents.FindInSphere(end_pos, 100),
  251.             })
  252.            
  253.             local ent = data.Entity
  254.            
  255.             if ent:IsValid() and (ent:IsPlayer() or ent:IsNPC()) and battlecam.selected_enemy ~= ent then
  256.                 battlecam.selected_enemy = ent
  257.             else
  258.                 battlecam.selected_enemy = NULL
  259.             end
  260.            
  261.             last_use = RealTime() + 0.25
  262.         end
  263.     end
  264. end
  265.  
  266. function battlecam.InputMouseApply(ucmd)
  267.     local ent = battlecam.selected_enemy
  268.            
  269.     if ent:IsValid() then
  270.    
  271.         if ent:IsPlayer() and (not ent:Alive() or not ply:Alive()) then
  272.             battlecam.selected_enemy = NULL
  273.         end
  274.        
  275.         local head_pos = battlecam.FindHeadPos(ent)
  276.         local aim_ang = (head_pos - ply:EyePos()):Angle()
  277.        
  278.         aim_ang.p = math.NormalizeAngle(aim_ang.p)
  279.         aim_ang.y = math.NormalizeAngle(aim_ang.y)
  280.         aim_ang.r = 0
  281.        
  282.         ucmd:SetViewAngles(aim_ang)
  283.     end
  284. end
  285.  
  286. function battlecam.CreateMove(ucmd)
  287.  
  288. end
  289.  
  290. function battlecam.HUDShouldDraw(hud_type)
  291.     if hud_type == "CHudCrosshair" then
  292.         return false
  293.     end
  294. end
  295.  
  296. function battlecam.ShouldDrawLocalPlayer()
  297.     return true
  298. end
  299.  
  300. CreateClientConVar("battle_cam", 0, true, true)
  301.  
  302. cvars.AddChangeCallback("battle_cam", function(_, old, new)
  303.     if new =="1" then
  304.         battlecam.Enable()
  305.     end
  306.    
  307.     if new == "0" then
  308.         battlecam.Disable()
  309.     end
  310. end)
  311.  
  312. if _G.battlecam and _G.battlecam.IsEnabled() then
  313.     battlecam.Disable()
  314.     battlecam.Enable()
  315. end
  316.  
  317. _G.battlecam =
  318. {  
  319.     Enable = battlecam.Enable,
  320.     Disable = battlecam.Disable,
  321.     IsEnabled = battlecam.IsEnabled,
  322. }
  323.  
  324. do return end
  325.  
  326. local ugh = Vector(-9731.6884765625, 12257.26953125, -13243.96875)
  327.  
  328. timer.Create("jrpg_enable", 0, 0, function()
  329.         do return end
  330.     local v = LocalPlayer()
  331.     if v:GetPos():Distance(ugh) < 7000 then
  332.         if not battlecam.IsEnabled() then
  333.             battlecam.Enable(true)
  334.         end
  335.     else
  336.         if battlecam.IsEnabled() then
  337.             battlecam.Disable(false)
  338.         end
  339.     end
  340. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement