• Print

Author Topic: Retrieve team color of target player?  (Read 4759 times)

0 Members and 1 Guest are viewing this topic.

Offline Chiller252

  • Jr. Member
  • **
  • Posts: 59
  • Karma: 3
  • On a Journey to Combine Technology and Design
    • Personal Website
Retrieve team color of target player?
« on: August 16, 2016, 10:20:08 pm »
A long time ago I found this cool script/addon (whatever you want to call it) that creates a transparent fly-out menu, tracked to players when you look at them, that displays some information about the player. I will provide pictures. The script has built-in compatibility with TTT and DarkRP that retrieves the team color in the game and displays it on some parts of the UI as a sort of highlight color.

What I want to do is repurpose some of this code so that on the sandbox gamemode it retrieves the Ulib team color of the target user and uses it as the highlight color (kind of like the default name/health system does)

I got somewhere trying to figure this out on my own, but rather than using the target player's team color, it applied the user's team color to everyone else's name tag.

Can someone (or a few people) look through the provided code and help me achieve this effect?

http://i.imgur.com/PodsWlP.jpg

Code: Lua
  1. // NORMAL CONFIGS
  2. local PInfo_IsTTT = false                                       // Force TTT, should not be set to true unless you have a custom gamemode name.
  3. local PInfo_IsDarkRP = false                            // Force DARKRP, should not be set to true unless you have a custom gamemode name.
  4. //local LerpSpeed = 10                                          // NOT USED
  5. local PInfo_Size = 0.10                                         // The size of the info panel.
  6. local PInfo_DisplayTime = 1                                     // How many seconds the info is displayed before fading out?
  7. local PInfo_FadeSpeed = 10                                      // The fading speed
  8. local PInfo_ShouldShowOnTab = false                     // Should we show all player's info when tabbing?
  9. local PInfo_OnTabDisplayTime = 4                        // How many seconds the info is displayed when pushing tab?
  10. local PInfo_TabDisplayRange = 800                       // The range at which the info is displayed when pressing tab.
  11. local PInfo_Opacity = 1.2                                       // The opacity (don't go too high);
  12.  
  13. // Camera Variables
  14. local HeightOffset = 65                                         // Offset of the display panel.
  15. local RightOffset = 20                                          // Distance from the player.
  16. local ForwardOffset = -5                                        // Distance in terms of depth
  17. local ForwardBackOffset = 1.5                           // More depth customization
  18.  
  19. // Drawing Variables.
  20. local PaddingX = 40                                                     // Padding X (Size)
  21. local Height = 60                                                       // The height of panels.
  22. local PaddingY = 6                                                      // Padding Y
  23. local InfoRectSize = 10                                         // Size of the small rectangle with the color information.
  24. local InfoRectAlpha = 100                                       // The alpha of said rectangle.
  25. local InfoRectColor = Color( 150, 150, 150, InfoRectAlpha )     // Color, not used in DarkRP or TTT
  26. local BackgroundColor = Color( 50, 50, 50, 100 )
  27. //local gradient = surface.GetTextureID( "gui/gradient" )      
  28.  
  29.  
  30. --Chill:stripped this from another script that does the color thing I want.
  31. --[[
  32. function GetColorForPlayer(ply)
  33.         --if ply:SteamID() == "STEAM_0:0:68825805" then -- You should leave my steam ID here. :3
  34.                 --return HSVToColor( math.abs(math.sin(0.3*RealTime())*128), 1, 1 ) -- Color fade
  35.                
  36.         --elseif ply:IsAdmin() then -- Admins will get a light white color.
  37.                 --return Color(255,255,255,60)
  38.        
  39.         if ply:IsUserGroup("Noob") then
  40.                 return Color(200,30,30,150)
  41.  
  42.         elseif ply:IsUserGroup("user") then
  43.                 return Color(255,255,120,150)      
  44.                
  45.         elseif ply:IsUserGroup("user2") then
  46.                 return Color(255,250,80,150)    
  47.                
  48.         elseif ply:IsUserGroup("user3") then
  49.                 return Color(255,240,50,150)    
  50.                
  51.         elseif ply:IsUserGroup("user4") then
  52.                 return Color(255,230,0,150)    
  53.        
  54.         elseif ply:IsUserGroup("moderator") then
  55.                 return Color(255,200,30,150)
  56.        
  57.     elseif ply:IsUserGroup("admin") then
  58.                 return Color(110,255,110,150)
  59.                
  60.         elseif ply:IsUserGroup("super_admin") then
  61.                 return Color(30,230,30,150)
  62.                
  63.         elseif ply:IsUserGroup("mega_admin") then
  64.                 return Color(30,210,210,150)
  65.                
  66.         elseif ply:IsUserGroup("uber_admin") then
  67.                 return Color(30,255,255,150)
  68.        
  69.     elseif ply:IsUserGroup("operator") then
  70.                 return Color(255,30,255,150)      
  71.                
  72.     elseif ply:IsUserGroup("superadmin") then
  73.                 return Color(150,30,255,150)
  74.                
  75.         end
  76. end
  77. ]]--
  78.  
  79. --Chill: here's the one that applies the viewer's color to everyone else.
  80.  
  81. --color = GetColorForPlayer
  82. --color = team.GetColor( ply:Team() )
  83.                 --color = Color( color.r, color.g, color.b, InfoRectAlpha * ply.PInfo_Alpha * PInfo_Opacity )
  84.  
  85. // Text info
  86. local TextColor = Color( 255, 255, 255, 200 )   // Color of the displayed text.
  87. surface.CreateFont( "PInfo_Font", {
  88.  font = "Verdana",
  89.  size = 50,
  90.  weight = 500,
  91.  blursize = 0,
  92.  scanlines = 0,
  93.  antialias = true,
  94.  underline = false,
  95.  italic = false,
  96.  strikeout = true,
  97.  symbol = false,
  98.  rotary = false,
  99.  shadow = true,
  100.  additive = true,
  101.  outline = false
  102. } )
  103.  
  104. local PInfo_Players = PInfo_Players or {}
  105.  
  106. local PI_IMG_HP = Material( "icon16/add.png" )
  107.  
  108. function PInfo_IsTTTActive()
  109.         if PInfo_IsTTT or gmod.GetGamemode().Name == "Trouble in Terrorist Town" then return true end
  110.         return false
  111. end
  112.  
  113. function PInfo_IsDarkRPActive()
  114.         if PInfo_IsDarkRP or gmod.GetGamemode().Name == "DarkRP" then return true end
  115.         return false
  116. end
  117.  
  118. PInfo_Values = PInfo_Values or {
  119.                         // Return the name.
  120.                         function( ply ) return ply:GetName() end,
  121.                        
  122.                         // Return the health.
  123.                         function( ply ) return "Health: " .. ply:Health(), PI_IMG_HP end,
  124.                        
  125.                        
  126.                         --// Return the player's active weapon.
  127.                         --function( ply )
  128.                                 --if IsValid( ply:GetActiveWeapon() ) then
  129.                                         --return ply:GetActiveWeapon():GetPrintName() or ""
  130.                                 --end
  131.                         --end
  132. }
  133.  
  134. hook.Add( "Initialize", "PInfo_Init", function()
  135.        
  136.         if PInfo_IsDarkRPActive() then
  137.                 PInfo_Values = {               
  138.                         // Return the name.
  139.                         function( ply ) return ply:GetName() end,
  140.                        
  141.                         // Return the job.
  142.                         function( ply ) return ply.DarkRPVars.job or "Jobless" end,
  143.                        
  144.                         // Return the health.
  145.                         function( ply ) return "Health: " .. ply:Health(), PI_IMG_HP end,
  146.                        
  147.                         // Return the cash.
  148.                         function( ply ) return "$".. ( ply.DarkRPVars.money or 0 ) end,
  149.                        
  150.                         --// Return the player's active weapon.
  151.                         --function( ply )
  152.                                 --if IsValid( ply:GetActiveWeapon() ) then
  153.                                         --return ply:GetActiveWeapon():GetPrintName() or ""
  154.                                 --end
  155.                         --end
  156.                 }
  157.        
  158.         // TTT Values
  159.         elseif PInfo_IsTTTActive() then
  160.                 PInfo_Values = {               
  161.                
  162.                         // Return the name.
  163.                         function( ply ) return ply:GetName() end,
  164.                        
  165.                         // Return the role.
  166.                         function( ply )
  167.                                 local txt = "Innocent"
  168.                                 if ply:IsTraitor() then
  169.                                         txt = "Traitor"
  170.                                 elseif ply:IsDetective() then
  171.                                         txt = "Detective"
  172.                                 end
  173.                                 return txt;
  174.                         end,
  175.                         // Return the health status.
  176.                         function( ply ) return LANG.GetUnsafeLanguageTable()[util.HealthToString( ply:Health() )], PI_IMG_HP end,
  177.                        
  178.                         // Return the active weapon's print name.
  179.                         --function( ply )
  180.                                 --if IsValid( ply:GetActiveWeapon() ) then
  181.                                         --return LANG.GetUnsafeLanguageTable()[(ply:GetActiveWeapon():GetPrintName() or "")]
  182.                                 --end
  183.                         --end
  184.                 }
  185.         end
  186.  
  187. end )
  188.  
  189. hook.Add( "PostDrawTranslucentRenderables", "PInfo_PostDrawTranslucentRenderables", function()
  190.        
  191.         for ply,_ in pairs( PInfo_Players ) do
  192.                
  193.                 if !IsValid(ply) or !ply:Alive() then return end
  194.                
  195.                 if !ply.PInfo_Alpha then
  196.                         ply.PInfo_Alpha = 0;
  197.                 end
  198.                
  199.                 if ply.PInfo_Alpha <= 0 then return end
  200.          
  201.                 local offset = Vector( 0, 0, HeightOffset )
  202.                 local ang = LocalPlayer():EyeAngles()
  203.                 local pos = ply:GetPos() + offset + ang:Up() + ang:Right() * RightOffset + ang:Forward() * ( ForwardOffset + ( math.sin( CurTime() * 2 )) )
  204.                 local backPos = pos + ang:Forward() * ForwardBackOffset
  205.          
  206.                 ang:RotateAroundAxis( ang:Forward(), 90 )
  207.                 ang:RotateAroundAxis( ang:Right(), 90 )
  208.                
  209.                 local realAng = ang.y - 5
  210.                
  211.                 // Background cam
  212.                 cam.Start3D2D( backPos, Angle( 0, realAng + 90 - 90 * ( ply.PInfo_Alpha ), 85 ), PInfo_Size )
  213.                         local i = 0;
  214.                         for k,v in pairs( PInfo_Values ) do
  215.                                 PInfo_BackDraw( ply, 5, i * ( Height + PaddingY ), v( ply ) )
  216.                                 i = i + 1;
  217.                         end
  218.                 cam.End3D2D()
  219.          
  220.                 // Foreground cam
  221.                 cam.Start3D2D( pos, Angle( 0, realAng + 90 - 90 * ( ply.PInfo_Alpha ) , 85 ), PInfo_Size )
  222.                         local i = 0;
  223.                         for k,v in pairs( PInfo_Values ) do
  224.                                 PInfo_Draw( ply, 5, i * ( Height + PaddingY ), v( ply ) )
  225.                                 i = i + 1;
  226.                         end
  227.                 cam.End3D2D()
  228.         end
  229.        
  230. end )
  231.  
  232. hook.Add( "Think", "PInfo_Think", function()
  233.  
  234.         if !IsValid( LocalPlayer() ) then return end
  235.        
  236.         local eyeTrace = LocalPlayer():GetEyeTrace()
  237.         if eyeTrace.Entity and eyeTrace.Entity:IsPlayer() then
  238.                 PInfo_AddDrawPlayer( eyeTrace.Entity );
  239.         end
  240.        
  241.         PInfo_UpdateDrawPlayers()
  242.  
  243. end)
  244.  
  245. hook.Add( "ScoreboardShow", "PInfo_ScoreboardShow", function()
  246.        
  247.         if PInfo_ShouldShowOnTab then
  248.        
  249.                 for _,ent in pairs(ents.FindInSphere( LocalPlayer():GetPos(), PInfo_TabDisplayRange )) do
  250.                         if IsValid(ent) and ent:IsPlayer() then
  251.                                 PInfo_AddDrawPlayer( ent, PInfo_OnTabDisplayTime );
  252.                         end
  253.                 end
  254.        
  255.         end
  256.        
  257. end )
  258.  
  259. // DRAWING FUNCTIONS (BACKGROUND)
  260.  
  261. function PInfo_BackDraw( ply, x, y, text, image )
  262.        
  263.         if ( !text ) then return end
  264.         surface.SetFont( "PInfo_Font" )
  265.         local w,h = surface.GetTextSize( text )
  266.        
  267.         surface.SetDrawColor( Color( BackgroundColor.r, BackgroundColor.g, BackgroundColor.b, BackgroundColor.a * ply.PInfo_Alpha * PInfo_Opacity ) )
  268.         surface.DrawRect( x, y, PaddingX + w, Height )
  269.        
  270.         if ply and PInfo_IsDarkRPActive() then
  271.                 color = ply:getJobTable().color
  272.                 color = Color( color.r, color.g, color.b, InfoRectAlpha * ply.PInfo_Alpha * PInfo_Opacity )
  273.         elseif ply and PInfo_IsTTTActive() then
  274.                 if ply:IsTraitor() then
  275.                         color = Color( COLOR_RED.r * 0.75, COLOR_RED.g * 0.75, COLOR_RED.b * 0.75, InfoRectAlpha * ply.PInfo_Alpha * PInfo_Opacity )
  276.                 elseif ply:IsDetective() then
  277.                         color = Color( COLOR_BLUE.r * 0.75, COLOR_BLUE.g * 0.75, COLOR_BLUE.b * 0.75, InfoRectAlpha * ply.PInfo_Alpha * PInfo_Opacity )
  278.                 else
  279.                         color = Color( COLOR_GREEN.r * 0.75, COLOR_GREEN.g * 0.75, COLOR_GREEN.b * 0.75, InfoRectAlpha * ply.PInfo_Alpha * PInfo_Opacity )
  280.                 end
  281.         end
  282.         surface.SetDrawColor( color or InfoRectColor )
  283.         surface.DrawRect( x - InfoRectSize, y, InfoRectSize - InfoRectSize * 0.5, Height )
  284.         surface.DrawRect( x - InfoRectSize, y, InfoRectSize, Height )
  285.        
  286.         draw.DrawText( text, "PInfo_Font", x + w * 0.5 + PaddingX * 0.5 , y + h * 0.5 - 18, Color( 120, 120, 120, 150 * ply.PInfo_Alpha * PInfo_Opacity  ), TEXT_ALIGN_CENTER )
  287.  
  288. end
  289.  
  290. // DRAWING FUNCTIONS (FOREGROUND)
  291. function PInfo_Draw( ply, x, y, text, image )
  292.        
  293.         if ( !text ) then return end
  294.         surface.SetFont( "PInfo_Font" )
  295.         local w,h = surface.GetTextSize( text )
  296.        
  297.         surface.SetDrawColor( Color( 0, 0, 0, 20 * ply.PInfo_Alpha * PInfo_Opacity  ) )
  298.        
  299.         /*if ( image ) then
  300.                 surface.SetMaterial( image )
  301.                 surface.SetDrawColor( Color( 255, 255, 255, 255 * ply.PInfo_Alpha ) )
  302.                 surface.DrawTexturedRect( x, y, Height * 0.25, Height * 0.25 )
  303.         end*/
  304.        
  305.         draw.DrawText( text, "PInfo_Font", x + w * 0.5 + PaddingX * 0.5 , y + h * 0.5 - 18, Color( TextColor.r, TextColor.g, TextColor.b, TextColor.a * ply.PInfo_Alpha * PInfo_Opacity ) , TEXT_ALIGN_CENTER )
  306.  
  307. end
  308.  
  309. function PInfo_UpdateDrawPlayers()
  310.  
  311.         for ply,time in pairs(PInfo_Players) do
  312.        
  313.                 if !IsValid( ply ) or !ply:Alive() then
  314.                         PInfo_RemoveDrawPlayer( ply )
  315.                 // Skip Local Player
  316.                 elseif ply != LocalPlayer() then
  317.  
  318.                         if !ply.PInfo_Alpha or !time then
  319.                                 time = 10
  320.                                 ply.PInfo_Alpha = 1
  321.                         end
  322.                
  323.                         if time > 0 then
  324.                                 if ply.PInfo_Alpha < 1 then
  325.                                         ply.PInfo_Alpha = math.Clamp( ply.PInfo_Alpha + FrameTime() * PInfo_FadeSpeed , 0, 1 )
  326.                                 end
  327.                                 PInfo_Players[ply] = time - FrameTime()
  328.                         elseif ply.PInfo_Alpha > 0 then
  329.                                 ply.PInfo_Alpha = ply.PInfo_Alpha - FrameTime() * PInfo_FadeSpeed
  330.                         else
  331.                                 PInfo_RemoveDrawPlayer( ply )
  332.                         end
  333.                        
  334.                 end
  335.         end
  336.  
  337. end
  338.  
  339. function PInfo_AddDrawPlayer( ply, time )
  340.        
  341.         if !IsValid( ply ) or !ply:Alive() then return end
  342.        
  343.         PInfo_Players[ply] = time or PInfo_DisplayTime;
  344.         ply.PInfo_Alpha = ply.PInfo_Alpha or 0;
  345.        
  346. end
  347.  
  348. function PInfo_RemoveDrawPlayer( ply )
  349.        
  350.         PInfo_Players[ply] = nil;
  351.        
  352. end
  353.  
  354. hook.Add( "EntityRemove", "PInfo_EntityRemove", function( ent )
  355.   if ( IsValid( ent ) ) and ( ent:IsPlayer() ) then
  356.                 PInfo_RemoveDrawPlayer( ent )
  357.   end
  358. end )

Offline Chiller252

  • Jr. Member
  • **
  • Posts: 59
  • Karma: 3
  • On a Journey to Combine Technology and Design
    • Personal Website
Re: Retrieve team color of target player?
« Reply #1 on: August 18, 2016, 03:39:36 pm »
Ok, so how would I just get the team.GetColor of the other players the player looks at?

  • Print