Advertisement
Guest User

Garry's Mod GTA4 HUD

a guest
Jul 24th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 KB | None | 0 0
  1. --[[
  2.         DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  3.                     Version 2, December 2004
  4.  
  5.  Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  6.  
  7.  Everyone is permitted to copy and distribute verbatim or modified
  8.  copies of this license document, and changing it is allowed as long
  9.  as the name is changed.
  10.  
  11.             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  12.    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  13.  
  14.   0. You just DO WHAT THE FUCK YOU WANT TO.
  15. ]]
  16.  
  17. local clip = {}
  18.  
  19. do
  20.     local render = render
  21.  
  22.     local STENCILOPERATION_REPLACE = STENCILOPERATION_REPLACE
  23.     local STENCILOPERATION_ZERO = STENCILOPERATION_ZERO
  24.     local STENCILCOMPARISONFUNCTION_EQUAL = STENCILCOMPARISONFUNCTION_EQUAL
  25.     local STENCILCOMPARISONFUNCTION_NEVER = STENCILCOMPARISONFUNCTION_NEVER
  26.     local STENCILCOMPARISONFUNCTION_NOTEQUAL = STENCILCOMPARISONFUNCTION_NOTEQUAL
  27.  
  28.     function clip.Start()
  29.         render.SetStencilEnable(true)
  30.         render.SetStencilWriteMask(1)
  31.         render.SetStencilTestMask(1)
  32.         render.SetStencilReferenceValue(1)
  33.     end
  34.  
  35.     function clip.End()
  36.         render.ClearStencil()
  37.         render.SetStencilEnable(false)
  38.     end
  39.  
  40.     function clip.Render(call1, call2, clipOuter)
  41.         render.ClearStencil()
  42.        
  43.         render.SetStencilFailOperation(STENCILOPERATION_REPLACE)
  44.         render.SetStencilPassOperation(STENCILOPERATION_ZERO)
  45.         render.SetStencilZFailOperation(STENCILOPERATION_ZERO)
  46.         render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_NEVER)
  47.  
  48.         call1()
  49.  
  50.         render.SetStencilFailOperation(STENCILOPERATION_ZERO)
  51.         render.SetStencilPassOperation(STENCILOPERATION_REPLACE)
  52.         render.SetStencilZFailOperation(STENCILOPERATION_ZERO)
  53.  
  54.         if clipOuter then
  55.             render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL)
  56.         else
  57.             render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_NOTEQUAL)
  58.         end
  59.  
  60.         call2()
  61.     end
  62. end
  63.  
  64. local GenerateCircleVertices = nil
  65.  
  66. do
  67.     local cos, rad, sin = math.cos, math.rad, math.sin
  68.  
  69.     function GenerateCircleVertices(centreX, centreY, radius, angStart, angSize)
  70.         local vertices = {}
  71.         local passes = 64 -- Seems to look pretty enough
  72.        
  73.         vertices[1] = { -- Ensure vertices resemble sector and not a chord
  74.             x = centreX,
  75.             y = centreY
  76.         }
  77.  
  78.         for i = 0, passes do
  79.             local sectorVertexAng = rad(-90 + angStart + angSize * (i / passes))
  80.  
  81.             vertices[i + 2] = {
  82.                 x = centreX + cos(sectorVertexAng) * radius,
  83.                 y = centreY + sin(sectorVertexAng) * radius
  84.             }
  85.         end
  86.  
  87.         return vertices
  88.     end
  89. end
  90.    
  91. local miniMapMat = Material("roleplay/minimap.png")
  92. local miniMapRT = GetRenderTarget("MiniMapRT", 512, 512, true)
  93. local miniMapRTMat = CreateMaterial("MiniMapRTMat", "UnlitGeneric", {["$basetexture"] = "MiniMapRT"})
  94.  
  95. local radius = math.Clamp(math.floor(ScrH() / 8.5), 80, 256)
  96. local x, y = 32 + radius, 32 + radius
  97. local barSize = radius / 9
  98. local outlineSize = 3
  99.  
  100. local innerBarVertices = GenerateCircleVertices(x, y, radius - barSize, 0, 360)
  101. local healthBarVertices = GenerateCircleVertices(x, y, radius, 0, 0)
  102. local armorBarVertices = GenerateCircleVertices(x, y, radius, 0, 0)
  103.  
  104. local innerBackgroundVertices = innerBarVertices
  105. local backgroundVertices = GenerateCircleVertices(x, y, radius, 0, 360)
  106.  
  107. local innerOutlineVertices = backgroundVertices
  108. local innerInlineVertices = GenerateCircleVertices(x, y, radius - barSize - outlineSize, 0, 360)
  109. local outlineVertices = GenerateCircleVertices(x, y, radius + outlineSize, 0, 360)
  110. local inlineVertices = GenerateCircleVertices(x, y, radius - barSize, 0, 360)
  111.  
  112. local lastHealth = nil
  113. local lastArmor = nil
  114.  
  115. hook.Add("Think", "rp_radar", function()
  116.     if not IsValid(LocalPlayer()) then return end
  117.  
  118.     local ply = LocalPlayer()
  119.    
  120.     if ply:Health() ~= lastHealth or ply:Armor() ~= lastArmor then
  121.         local hp = Lerp(0.25, lastHealth or 100, ply:Health())
  122.         local armor = Lerp(0.25, lastArmor or 100, ply:Armor())
  123.         local healthFrac = hp / 100
  124.         local armorFrac = armor / 100
  125.  
  126.         local healthAngSize = 180 * healthFrac
  127.         local armorAngSize = 180 * armorFrac
  128.        
  129.         local healthAngStart = 180 + (180 - healthAngSize)
  130.         local armorAngStart = healthAngStart - armorAngSize
  131.  
  132.         healthBarVertices = GenerateCircleVertices(x, y, radius, healthAngStart, healthAngSize)
  133.         armorBarVertices = GenerateCircleVertices(x, y, radius, armorAngStart, armorAngSize)
  134.  
  135.         lastHealth = hp
  136.         lastArmor = armor
  137.     end
  138. end)
  139.  
  140. local color_white = color_white
  141. local color_black = color_black
  142. local color_health = Color(92, 131, 86)
  143. local color_lowhealth = Color(148, 76, 67)
  144. local color_armor = Color(74, 148, 160)
  145. local color_innerbackground = Color(0, 0, 0, 210)
  146. local texWhite = surface.GetTextureID("vgui/white")
  147.  
  148. local rendering_minimap = false
  149.  
  150. hook.Add("PreDrawViewModel", "Radar", function()
  151.     if rendering_minimap then
  152.         return true
  153.     end
  154. end)
  155.  
  156. hook.Add("ShouldDrawLocalPlayer", "Radar", function()
  157.     if rendering_minimap then
  158.         return true
  159.     end
  160. end)
  161.  
  162. hook.Add("HUDPaint", "Radar", function()
  163.     if not IsValid(LocalPlayer()) then return end
  164.  
  165.     local ply = LocalPlayer()
  166.     surface.SetDrawColor(color_white)
  167.     surface.SetTexture(texWhite)
  168.  
  169.     do
  170.         local oldRT = render.GetRenderTarget()
  171.         local oldW, oldH = ScrW(), ScrH()
  172.  
  173.         render.SetRenderTarget(miniMapRT)
  174.             render.SetViewPort(0, 0, 512, 512)
  175.                 render.Clear(40, 40, 40, 255)
  176.                
  177.                 cam.Start2D()
  178.                     local ang = EyeAngles()
  179.                     local pos = EyePos()
  180.  
  181.                     render.SuppressEngineLighting(true)
  182.                        
  183.                         rendering_minimap = true
  184.  
  185.                         render.RenderView({
  186.                             angles = Angle(90, ang.y, 0),
  187.                             origin = Vector(pos.x, pos.y, 2400),
  188.                             x = 0,
  189.                             y = 0,
  190.                             w = 512,
  191.                             h = 512,
  192.                             fov = 40,
  193.                             dopostprocess = false
  194.                         })
  195.  
  196.                         rendering_minimap = false
  197.  
  198.                     render.SuppressEngineLighting(false)
  199.  
  200.                 cam.End2D()
  201.             render.SetViewPort(0, 0, oldW, oldH)
  202.         render.SetRenderTarget(oldRT)
  203.     end
  204.  
  205.     clip.Start()
  206.  
  207.         clip.Render(
  208.             function()
  209.                 surface.DrawPoly(innerInlineVertices)
  210.             end,
  211.             function()
  212.                 surface.SetMaterial(miniMapRTMat)
  213.                 surface.DrawTexturedRect(x - 256, y - 256, 512, 512)
  214.                 surface.SetTexture(texWhite)
  215.             end,
  216.             true
  217.         )
  218.  
  219.  
  220.         clip.Render(
  221.             function()
  222.                 surface.DrawPoly(innerOutlineVertices)
  223.             end,
  224.             function()
  225.                 surface.SetDrawColor(color_black)
  226.                 surface.DrawPoly(outlineVertices)
  227.             end
  228.         )
  229.  
  230.         clip.Render(
  231.             function()
  232.                 surface.DrawPoly(innerInlineVertices)
  233.             end,
  234.             function()
  235.                 surface.SetDrawColor(color_black)
  236.                 surface.DrawPoly(inlineVertices)
  237.             end
  238.  
  239.         )
  240.  
  241.         clip.Render(
  242.             function()
  243.                 surface.DrawPoly(innerBackgroundVertices)
  244.             end,
  245.             function()
  246.                 surface.SetDrawColor(color_innerbackground)
  247.                 surface.DrawPoly(backgroundVertices)
  248.             end
  249.         )
  250.  
  251.         clip.Render(
  252.             function()
  253.                 surface.DrawPoly(innerBarVertices)
  254.             end,
  255.             function()
  256.                 if ply:Health() > 10 or math.ceil(RealTime()) % 2 == 0 then
  257.                     surface.SetDrawColor(color_health)
  258.                 else
  259.                     surface.SetDrawColor(color_lowhealth)
  260.                 end
  261.  
  262.                 surface.DrawPoly(healthBarVertices)
  263.             end
  264.         )
  265.  
  266.         clip.Render(
  267.             function()
  268.                 surface.DrawPoly(innerBarVertices)
  269.             end,
  270.             function()
  271.                 surface.SetDrawColor(color_armor)
  272.                 surface.DrawPoly(armorBarVertices)
  273.             end
  274.         )
  275.  
  276.     clip.End()
  277. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement