CapsAdmin

Untitled

Dec 19th, 2011
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.74 KB | None | 0 0
  1. chatcinematic = {}
  2. local lib = chatcinematic
  3.  
  4. lib.random_seed = ""
  5.  
  6. function lib.RandomSeed(min, max, seed)
  7.     seed = seed or lib.random_seed
  8.     max = max + 1
  9.     seed = seed .. tostring(max) .. tostring(min)
  10.  
  11.     return min + (max - min) * (util.CRC(seed)/10000000000)
  12. end
  13.  
  14. local function HOOK(name, ...)
  15.     hook.Add(name, "chatcinematic_" .. name, lib[name])
  16.     for key, value in pairs{...} do
  17.         hook.Add(value, "chatcinematic_" .. value, lib[name])
  18.     end
  19. end
  20.  
  21. function lib.Initialize()
  22.     lib.AddPlayer(LocalPlayer())
  23. end
  24.  
  25.  
  26. function lib.Stop()
  27.     lib.players = {}
  28.     lib.active_player = NULL
  29. end
  30.  
  31. function lib.IsActive()
  32.     return lib.GetActivePlayer():IsPlayer()
  33. end
  34.  
  35. do -- players
  36.  
  37.     do -- manage
  38.         lib.players = {}
  39.  
  40.         function lib.CheckPlayers()
  41.             for ply in pairs(lib.players) do
  42.                 if not lib.CanChat(ply) then
  43.                     if lib.active_player == ply then
  44.                         lib.active_player = NULL
  45.                     end
  46.                     lib.players[ply] = nil
  47.                 end
  48.             end
  49.         end
  50.  
  51.         function lib.GetPlayers()
  52.             lib.CheckPlayers()
  53.             return lib.players
  54.         end
  55.  
  56.         function lib.HasPlayer(ply)
  57.             lib.CheckPlayers()
  58.             return lib.players[ply] and true or false
  59.         end
  60.  
  61.         function lib.AddPlayer(ply)
  62.             if not lib.CanChat(ply) then
  63.                 lib.RemovePlayer(ply)
  64.                 return
  65.             end
  66.  
  67.             lib.players[ply] = ply
  68.         end
  69.  
  70.         function lib.RemovePlayer(ply)
  71.             if lib.GetActivePlayer() == ply then
  72.                 lib.SetActivePlayer(NULL)
  73.             end
  74.  
  75.             lib.players[ply] = nil
  76.         end
  77.     end
  78.  
  79.     function lib.SetActivePlayer(ply)
  80.         if lib.HasPlayer(ply) then
  81.             lib.active_player = ply
  82.         end
  83.     end
  84.  
  85.     function lib.GetActivePlayer()
  86.         return lib.active_player or NULL
  87.     end
  88.  
  89.     function lib.CanChat(a)
  90.         local b = LocalPlayer()
  91.  
  92.         if b:GetVelocity():Length() > 300 then
  93.             return false
  94.         end
  95.  
  96.         if not a:IsPlayer() or a == b then
  97.             return true
  98.         end
  99.  
  100.         if a:GetVelocity():Length() > 300 then
  101.             return false
  102.         end
  103.  
  104.         local apos = a:EyePos()
  105.         local bpos = b:EyePos()
  106.  
  107.         if apos:Distance(bpos) > lib.cam_distance then
  108.             return false
  109.         end
  110.  
  111.         if a:GetAimVector():Dot((apos - bpos):Normalize()) > 0.9 then
  112.             return false
  113.         end
  114.  
  115.         return true
  116.     end
  117.  
  118.     function lib.OnPlayerChat(ply, str)
  119.         if ply == LocalPlayer() then
  120.             for key, ply in pairs(ents.FindInSphere(ply:EyePos(), lib.cam_distance)) do
  121.                 if ply:IsPlayer() then
  122.                     lib.AddPlayer(ply)
  123.                 end
  124.             end
  125.         end
  126.  
  127.         lib.random_seed = str
  128.  
  129.         if lib.CanChat(ply) then
  130.             lib.AddPlayer(ply)
  131.  
  132.             if (ply.last_active_chatcinematic or 0) < CurTime() and (lib.last_active_chatcinematic or 0) < CurTime() then
  133.                 lib.ChooseRandomCameraPos(ply)
  134.                 lib.SetActivePlayer(ply)
  135.  
  136.                 ply.last_active_chatcinematic = CurTime() + 3
  137.                 lib.last_active_chatcinematic = CurTime() + 1.5
  138.             end
  139.         else
  140.             lib.RemovePlayer(ply)
  141.         end
  142.     end
  143.  
  144.     HOOK("OnPlayerChat")
  145.  
  146. end
  147.  
  148. do -- view
  149.     lib.cam_distance = 200
  150.     lib.smooth_speed = 2
  151.  
  152.     lib.pos_smooth = vector_origin
  153.     lib.dir_smooth = vector_origin
  154.     lib.fov_smooth = 0
  155.  
  156.     lib.pos_target = vector_origin
  157.     lib.dir_target = vector_origin
  158.     lib.fov_target = 75
  159.  
  160.     lib.local_camera_pos = vector_origin
  161.     lib.angle_offset = Angle(0,0,0)
  162.     function lib.GetEyePos(ply)
  163.         ply = not ply:Alive() and ply:GetRagdollEntity() or ply
  164.  
  165.         local pos, ang = ply:GetBonePosition(ply:LookupBone("ValveBiped.Bip01_Head1"))
  166.         return pos + ang:Forward() * 2
  167.     end
  168.  
  169.     function lib.ChooseRandomCameraPos(ply)
  170.         local prev = lib.GetActivePlayer()
  171.  
  172.         if prev ~= ply then
  173.             ply = ply or prev
  174.  
  175.             if ply:IsPlayer() then
  176.                 lib.local_camera_pos = Vector(lib.RandomSeed(-1,1)*0.2, lib.RandomSeed(-1,1)*0.2, lib.RandomSeed(-1,1)*0.3)
  177.                 lib.angle_offset = Angle(lib.RandomSeed(-1,1)*2, lib.RandomSeed(-1,1)*8, lib.RandomSeed(-1,1))
  178.                 lib.fov_target = lib.RandomSeed(10, 40)
  179.  
  180.                 lib.new_angle = true
  181.                 lib.panning_dir = Angle(0,0,0)
  182.                 lib.panning_vel = lib.RandomSeed(1,3) * lib.angle_offset.y > 0 and -1 or 1
  183.                 lib.pos_target = nil
  184.             end
  185.         end
  186.     end
  187.  
  188.     function lib.GetLocalCameraPos()
  189.         local ply = lib.GetActivePlayer()
  190.  
  191.         if ply:IsPlayer() then
  192.             return (ply:GetAimVector() * Vector(1,1,0)) + lib.local_camera_pos
  193.         end
  194.  
  195.         return vector_origin
  196.     end
  197.  
  198.     function lib.GetWorldCameraPos()
  199.         local middle = vector_origin
  200.         local players = lib.GetPlayers()
  201.  
  202.         for ply in pairs(lib.GetPlayers()) do
  203.             middle = middle + lib.GetEyePos(ply)
  204.         end
  205.  
  206.         if middle ~= vector_origin then
  207.             middle = middle / table.Count(players)
  208.         end
  209.  
  210.         local ply = lib.GetActivePlayer()
  211.  
  212.         return LerpVector(0.2, lib.GetEyePos(ply) + (lib.GetLocalCameraPos() * (lib.cam_distance * 0.5)), middle)
  213.     end
  214.  
  215.     function lib.CalcSmooth()
  216.         local delta = FrameTime()
  217.  
  218.         lib.pos_smooth = lib.pos_smooth + ((lib.pos_target - lib.pos_smooth) * (delta * lib.smooth_speed))
  219.         lib.dir_smooth = lib.dir_smooth + ((lib.dir_target - lib.dir_smooth) * (delta * lib.smooth_speed))
  220.         lib.fov_smooth = lib.fov_smooth + ((lib.fov_target - lib.fov_smooth) * (delta * lib.smooth_speed))
  221.     end
  222.  
  223.     lib.panning_dir = vector_origin
  224.     lib.panning_vel = 0
  225.  
  226.     function lib.CalcPanning()
  227.         if math.abs(lib.panning_dir.y) < 5 then
  228.             lib.panning_dir = lib.panning_dir + (Angle(0, FrameTime()*lib.panning_vel, 0))
  229.         end
  230.     end
  231.  
  232.     local params = {}
  233.  
  234.     function lib.CalcView()
  235.         lib.CheckPlayers()
  236.  
  237.         local ply = lib.GetActivePlayer()
  238.  
  239.         if ply:IsPlayer() then
  240.             lib.pos_target = lib.pos_target or lib.GetWorldCameraPos()
  241.             lib.dir_target = lib.GetEyePos(ply) - lib.pos_target + lib.panning_dir
  242.  
  243.             lib.CalcSmooth()
  244.             lib.CalcPanning()
  245.  
  246.             if lib.new_angle then
  247.                 lib.pos_smooth = lib.pos_target
  248.                 lib.dir_smooth = lib.dir_target
  249.                 lib.fov_smooth = lib.fov_target
  250.  
  251.                 lib.new_angle = false
  252.             end
  253.  
  254.             params.origin = lib.pos_smooth
  255.             params.angles = lib.dir_smooth:Angle() + lib.angle_offset + lib.panning_dir
  256.             params.fov = lib.fov_smooth
  257.  
  258.             return params
  259.         end
  260.     end
  261.  
  262.     HOOK("CalcView")
  263.  
  264.     function lib.ShouldDrawLocalPlayer()
  265.         local ply = lib.GetActivePlayer()
  266.  
  267.         if ply:IsPlayer() then
  268.             return true
  269.         end
  270.     end
  271.  
  272.     HOOK("ShouldDrawLocalPlayer")
  273.  
  274.     surface.CreateFont("calibri", 48, 500, true, false, "subtitle")
  275.  
  276.     lib.letterbox_ratio = 2.39 / 1
  277.     lib.letterbox_color = Color(0, 0, 0, 255)
  278.     lib.letterbox_vignette = 2
  279.     lib.letterbox_message = "hi2"
  280.     lib.letterbox_fade = 255
  281.  
  282.     function lib.PreChatSound(data)
  283.         lib.letterbox_message = data.key
  284.         lib.letterbox_fade = 255
  285.     end
  286.  
  287.     HOOK("PreChatSound")
  288.  
  289.     local grad_up = surface.GetTextureID("gui/gradient_up")
  290.     local grad_down = surface.GetTextureID("gui/gradient_down")
  291.  
  292.     function lib.DrawLetterBox()
  293.         local width, height = surface.ScreenWidth(), surface.ScreenHeight()
  294.         local ratio = width / height
  295.  
  296.         surface.SetDrawColor(lib.letterbox_color)
  297.         local vx, vy, vw, vh
  298.  
  299.         if ratio < lib.letterbox_ratio then
  300.             vw = width
  301.             vh = width / lib.letterbox_ratio
  302.             vx = (width - vw) / 2
  303.             vy = (height - vh) / 2
  304.  
  305.             surface.DrawRect(-1, -1, vw, vy)
  306.             surface.DrawRect(-1, vy + vh, vw, height - (vy + vh))
  307.         else
  308.             vw = height * lib.letterbox_ratio
  309.             vh = height
  310.             vx = (width - vw) / 2
  311.             vy = (height - vh) / 2
  312.  
  313.             surface.DrawRect(0, 0, vx, vh)
  314.             surface.DrawRect(vx + vw, 0, width - (vx + vw), vh)
  315.         end
  316.  
  317.         local c = lib.letterbox_color
  318.         surface.SetDrawColor(c.r, c.g, c.b, c.a*0.8)
  319.  
  320.         surface.SetTexture(grad_up)
  321.         surface.DrawTexturedRect(vx, vy + vh - vh / lib.letterbox_vignette, vw, vh / lib.letterbox_vignette)
  322.  
  323.         surface.SetTexture(grad_down)
  324.         surface.DrawTexturedRect(vx, vy, vw, vh / lib.letterbox_vignette)
  325.     end
  326.  
  327.     function lib.DrawSubtitles()
  328.         surface.SetFont("subtitle")
  329.         local w, h = surface.GetTextSize(lib.letterbox_message)
  330.         surface.SetTextPos((ScrW() - w) / 2, (ScrH() - h) * 0.95)
  331.         surface.SetTextColor(Color(255, 255, 255, lib.letterbox_fade))
  332.         surface.DrawText(lib.letterbox_message)
  333.         lib.letterbox_fade = math.Clamp(lib.letterbox_fade - 1, 0, 255)
  334.     end
  335.  
  336.     local mat = Material("particle/Particle_Glow_04_Additive")
  337.     local size = 400
  338.     function lib.RenderScreenspaceEffects()
  339.         if not chatcinematic.IsActive() then return end
  340.  
  341.         DrawSharpen(1.5, 0.3)
  342.  
  343.  
  344.         DrawColorModify({
  345.             ["$pp_colour_colour"] = 0.8,
  346.             ["$pp_colour_brightness"] = -0.10,
  347.             ["$pp_colour_contrast"] = 0.5,
  348.         })
  349.  
  350.         DrawBloom( 0, 2, 0, 0, 0.15, 0.1, 1, 1, 1 )
  351.  
  352.         cam.Start2D()
  353.             surface.SetMaterial(mat)
  354.             surface.SetDrawColor(150,150,150,5)
  355.             surface.DrawTexturedRect(-size, -size, ScrW()+size*2, ScrH()+size*2)
  356.         cam.End2D()
  357.  
  358.         lib.DrawLetterBox()
  359.  
  360.         lib.DrawSubtitles()
  361.  
  362.         --cam.Start2D()
  363.         --  surface.SetDrawColor(120,110,150,10)
  364.         --  surface.DrawRect(0, 0, ScrW(), ScrH())
  365.         --cam.End2D()
  366.  
  367.     end
  368.  
  369.     HOOK("RenderScreenspaceEffects")
  370.  
  371.     function chatcinematic.HUDShouldDraw(str)
  372.         if not chatcinematic.IsActive() then return end
  373.         if str ~= "CHudWeaponSelection" then
  374.             return false
  375.         end
  376.     end
  377.  
  378.     HOOK("HUDShouldDraw")
  379.  
  380.     local emitter = ParticleEmitter(EyePos())
  381.     emitter:SetNoDraw(true)
  382.  
  383.     function lib.PostDrawOpaqueRenderables()
  384.         if not chatcinematic.IsActive() then return end
  385.  
  386.         for i=1, 5 do
  387.             local particle = emitter:Add("particle/Particle_Glow_05", LocalPlayer():EyePos() + VectorRand() * 500)
  388.             if particle then
  389.                 local col = HSVToColor(math.random()*30, 0.1, 1)
  390.                 particle:SetColor(col.r, col.g, col.b, 266)
  391.  
  392.                 particle:SetVelocity(VectorRand() )
  393.  
  394.                 particle:SetDieTime((math.random()+4)*3)
  395.                 particle:SetLifeTime(0)
  396.  
  397.                 local size = 1
  398.  
  399.                 particle:SetAngles(AngleRand())
  400.                 particle:SetStartSize((math.random()+1)*2)
  401.                 particle:SetEndSize(0)
  402.  
  403.                 particle:SetStartAlpha(0)
  404.                 particle:SetEndAlpha(255)
  405.  
  406.                 --particle:SetRollDelta(math.Rand(-1,1)*20)
  407.                 particle:SetAirResistance(500)
  408.                 particle:SetGravity(VectorRand() * 10)
  409.             end
  410.         end
  411.         emitter:Draw()
  412.     end
  413.  
  414.     HOOK("PostDrawOpaqueRenderables")
  415.  
  416.     timer.Create("chat_cinametic_check_players", 0.2, 0, lib.CheckPlayers)
  417. end
  418.  
  419. lib.Initialize()
Advertisement
Add Comment
Please, Sign In to add comment