Advertisement
Guest User

cl_radar.lua

a guest
Jul 3rd, 2011
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.20 KB | None | 0 0
  1. -- Traitor radar rendering
  2.  
  3. local render = render
  4. local surface = surface
  5. local string = string
  6. local player = player
  7. local math = math
  8.  
  9. RADAR = {}
  10. RADAR.targets = {}
  11. RADAR.enable = false
  12. RADAR.duration = 30
  13. RADAR.endtime = 0
  14. RADAR.bombs = {}
  15. RADAR.bombs_count = 0
  16. RADAR.repeating = true
  17. RADAR.samples = {}
  18. RADAR.samples_count = 0
  19.  
  20. RADAR.called_corpses = {}
  21.  
  22. function RADAR:EndScan()
  23.    self.enable = false
  24.    self.endtime = CurTime()
  25. end
  26.  
  27. function RADAR:Clear()
  28.    self:EndScan()
  29.    self.bombs = {}
  30.    self.samples = {}
  31.  
  32.    self.bombs_count = 0
  33.    self.samples_count = 0
  34. end
  35.  
  36. function RADAR:Timeout()
  37.    self:EndScan()
  38.  
  39.    if self.repeating and LocalPlayer() and (LocalPlayer():IsActiveTraitor() or LocalPlayer():IsActiveDetective()) then
  40.       RunConsoleCommand("ttt_radar_scan")
  41.    end
  42. end
  43.  
  44. -- cache stuff we'll be drawing
  45. function RADAR.CacheEnts()
  46.    -- also do some corpse cleanup here
  47.    for k, corpse in pairs(RADAR.called_corpses) do
  48.       if (corpse.called + 45) < CurTime() then
  49.          RADAR.called_corpses[k] = nil -- will make # inaccurate, no big deal
  50.       end
  51.    end
  52.  
  53.    if RADAR.bombs_count == 0 then return end
  54.  
  55.    -- Update bomb positions for those we know about
  56.    for idx, b in pairs(RADAR.bombs) do
  57.       local ent = Entity(idx)
  58.       if IsValid(ent) then
  59.          b.pos = ent:GetPos()
  60.       end
  61.    end
  62. end
  63.  
  64. local function DrawTarget(tgt, size, offset, no_shrink)
  65.    local scrpos = tgt.pos:ToScreen() -- sweet
  66.    local sz = (IsOffScreen(scrpos) and (not no_shrink)) and size/2 or size
  67.  
  68.    scrpos.x = math.Clamp(scrpos.x, sz, ScrW() - sz)
  69.    scrpos.y = math.Clamp(scrpos.y, sz, ScrH() - sz)
  70.  
  71.    surface.DrawTexturedRect(scrpos.x - sz, scrpos.y - sz, sz * 2, sz * 2)
  72.  
  73.    -- Drawing full size?
  74.    if sz == size then
  75.       local text = math.ceil(LocalPlayer():GetPos():Distance(tgt.pos))
  76.       local w, h = surface.GetTextSize(text)
  77.  
  78.       -- Show range to target
  79.       surface.SetTextPos(scrpos.x - w/2, scrpos.y + (offset * sz) - h/2)
  80.       surface.DrawText(text)
  81.  
  82.       if tgt.t then
  83.          -- Show time
  84.          text = string.FormattedTime(tgt.t - CurTime(), "%02i:%02i")
  85.          w, h = surface.GetTextSize(text)
  86.          
  87.          surface.SetTextPos(scrpos.x - w / 2, scrpos.y + sz / 2)
  88.          surface.DrawText(text)
  89.       elseif tgt.nick then
  90.          -- Show nickname
  91.          text = tgt.nick
  92.          w, h = surface.GetTextSize(text)
  93.          
  94.          surface.SetTextPos(scrpos.x - w / 2, scrpos.y + sz / 2)
  95.          surface.DrawText(text)
  96.       end
  97.    end
  98. end
  99.  
  100. local indicator   = surface.GetTextureID("effects/select_ring")
  101. local c4warn      = surface.GetTextureID("VGUI/ttt/icon_c4warn")
  102. local sample_scan = surface.GetTextureID("VGUI/ttt/sample_scan")
  103. local det_beacon  = surface.GetTextureID("VGUI/ttt/det_beacon")
  104.  
  105. local GetPTranslation = LANG.GetParamTranslation
  106. local FormatTime = string.FormattedTime
  107.  
  108. local near_cursor_dist = 180
  109.  
  110. function RADAR:Draw(client)
  111.    if not client then return end
  112.  
  113.    surface.SetFont("HudSelectionText")
  114.  
  115.    -- C4 warnings
  116.    if self.bombs_count != 0 and client:IsActiveTraitor() then
  117.       surface.SetTexture(c4warn)
  118.       surface.SetTextColor(200, 55, 55, 220)
  119.       surface.SetDrawColor(255, 255, 255, 200)
  120.      
  121.       for k, bomb in pairs(self.bombs) do
  122.          DrawTarget(bomb, 24, 0, true)
  123.       end
  124.    end
  125.  
  126.    -- Corpse calls
  127.    if client:IsActiveDetective() and #self.called_corpses then
  128.       surface.SetTexture(det_beacon)
  129.       surface.SetTextColor(255, 255, 255, 240)
  130.       surface.SetDrawColor(255, 255, 255, 230)
  131.  
  132.       for k, corpse in pairs(self.called_corpses) do
  133.          DrawTarget(corpse, 16, 0.5)
  134.       end
  135.    end
  136.  
  137.    -- Samples
  138.    if self.samples_count != 0 then
  139.       surface.SetTexture(sample_scan)
  140.       surface.SetTextColor(200, 50, 50, 255)
  141.       surface.SetDrawColor(255, 255, 255, 240)
  142.  
  143.       for k, sample in pairs(self.samples) do
  144.          DrawTarget(sample, 16, 0.5, true)
  145.       end
  146.    end
  147.  
  148.    -- Player radar
  149.    if (not self.enable) or (not client:IsActiveSpecial()) then return end
  150.  
  151.    surface.SetTexture(indicator)
  152.  
  153.    local remaining = math.max(0, RADAR.endtime - CurTime())
  154.    local alpha_base = 50 + 180 * (remaining / RADAR.duration)
  155.  
  156.    local mpos = Vector(ScrW() / 2, ScrH() / 2, 0)
  157.  
  158.    local role, alpha, scrpos, md
  159.    for k, tgt in pairs(RADAR.targets) do
  160.       alpha = alpha_base
  161.  
  162.       scrpos = tgt.pos:ToScreen()
  163.       md = mpos:Distance(Vector(scrpos.x, scrpos.y, 0))
  164.       if md < near_cursor_dist then
  165.          alpha = math.Clamp(alpha * (md / near_cursor_dist), 40, 230)
  166.       end
  167.  
  168.       role = tgt.role or ROLE_INNOCENT
  169.       if role == ROLE_TRAITOR then
  170.          surface.SetDrawColor(255, 0, 0, alpha)
  171.          surface.SetTextColor(255, 0, 0, alpha)
  172.  
  173.       elseif role == ROLE_DETECTIVE then
  174.          surface.SetDrawColor(0, 0, 255, alpha)
  175.          surface.SetTextColor(0, 0, 255, alpha)
  176.  
  177.       elseif role == -1 then -- decoys
  178.          surface.SetDrawColor(150, 150, 150, alpha)
  179.          surface.SetTextColor(150, 150, 150, alpha)
  180.  
  181.       else
  182.          surface.SetDrawColor(0, 255, 0, alpha)
  183.          surface.SetTextColor(0, 255, 0, alpha)
  184.       end
  185.  
  186.       DrawTarget(tgt, 24, 0)
  187.    end
  188.  
  189.    -- Time until next scan
  190.    surface.SetFont("TabLarge")
  191.    surface.SetTextColor(255, 0, 0, 230)
  192.  
  193.    local text = GetPTranslation("radar_hud", {time = FormatTime(remaining, "%02i:%02i")})
  194.    local w, h = surface.GetTextSize(text)
  195.  
  196.    surface.SetTextPos(36, ScrH() - 140 - h)
  197.    surface.DrawText(text)
  198. end
  199.  
  200. local function ReceiveC4Warn(um)
  201.    local idx = um:ReadShort()
  202.    local armed = um:ReadBool()
  203.  
  204.    if armed then
  205.       local pos = um:ReadVector()
  206.       local etime = um:ReadFloat()
  207.  
  208.       RADAR.bombs[idx] = {pos=pos, t=etime}
  209.    else
  210.       RADAR.bombs[idx] = nil
  211.    end
  212.  
  213.    RADAR.bombs_count = table.Count(RADAR.bombs)
  214. end
  215. usermessage.Hook("c4_warn", ReceiveC4Warn)
  216.  
  217. local function ReceiveCorpseCall(um)
  218.    local pos = um:ReadVector()
  219.    table.insert(RADAR.called_corpses, {pos = pos, called = CurTime()})
  220. end
  221. usermessage.Hook("corpse_call", ReceiveCorpseCall)
  222.  
  223. local function ReceiveRadarScan(um)
  224.    local num_targets = um:ReadChar()
  225.  
  226.    RADAR.targets = {}
  227.    for i=1, num_targets do
  228.       local r = um:ReadChar()
  229.  
  230.       local pos = Vector()
  231.       pos.x = um:ReadShort()
  232.       pos.y = um:ReadShort()
  233.       pos.z = um:ReadShort()
  234.  
  235.       table.insert(RADAR.targets, {role=r, pos=pos})
  236.    end
  237.  
  238.    RADAR.enable = true
  239.    RADAR.endtime = CurTime() + RADAR.duration
  240.    
  241.    timer.Create("radartimeout", RADAR.duration + 1, 1, RADAR.Timeout, RADAR)
  242. end
  243. usermessage.Hook("radar", ReceiveRadarScan)
  244.  
  245. local GetTranslation = LANG.GetTranslation
  246. function RADAR.CreateMenu(parent, frame)
  247.    local w, h = parent:GetSize()
  248.  
  249.    local dform = vgui.Create("DForm", parent)
  250.    dform:SetName(GetTranslation("radar_menutitle"))
  251.    dform:StretchToParent(0,0,0,0)
  252.    dform:SetAutoSize(false)
  253.  
  254.    local owned = LocalPlayer():HasEquipmentItem(EQUIP_RADAR)
  255.  
  256.    if not owned then
  257.       dform:Help(GetTranslation("radar_not_owned"))
  258.       return dform
  259.    end
  260.  
  261.    local bw, bh = 100, 25
  262.    local dscan = vgui.Create("DButton", dform)
  263.    dscan:SetSize(bw, bh)
  264.    dscan:SetText(GetTranslation("radar_scan"))
  265.    dscan.DoClick = function(s)
  266.                       s:SetDisabled(true)
  267.                       RunConsoleCommand("ttt_radar_scan")
  268.                       frame:Close()
  269.                    end
  270.    dform:AddItem(dscan)
  271.  
  272.    local dlabel = vgui.Create("DLabel", dform)
  273.    dlabel:SetText(GetPTranslation("radar_help", {num = RADAR.duration}))
  274.    dlabel:SetWrap(true)
  275.    dlabel:SetTall(50)
  276.    dform:AddItem(dlabel)
  277.  
  278.    local dcheck = vgui.Create("DCheckBoxLabel", dform)
  279.    dcheck:SetText(GetTranslation("radar_auto"))
  280.    dcheck:SetIndent(5)
  281.    dcheck:SetValue(RADAR.repeating)
  282.    dcheck.OnChange = function(s, val)
  283.                         RADAR.repeating = val
  284.                      end
  285.    dform:AddItem(dcheck)
  286.  
  287.    dform.Think = function(s)
  288.                     if RADAR.enable or not owned then
  289.                        dscan:SetDisabled(true)
  290.                     else
  291.                        dscan:SetDisabled(false)
  292.                     end
  293.                  end
  294.  
  295.    dform:SetVisible(true)
  296.  
  297.    return dform
  298. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement