


local radar = {
	Enabled				= CreateClientConVar("bb_radar", 1, true, false),
	scanfor 			= {"player", "rpg_missile", "crossbow_bolt", "npc_", "prop_vehicle_"},
	dangerous 			= {"sent_nuke_missile", "sent_nuke_detpack"},
	hazardmode			= false,
	player_show			= true,
	player_showname		= true,
	player_showhealth 	= true,
	player_showarmor	= false,
	player_showammo		= true,
	screendetail		= 64,
	screenrotation		= 0,
	radius				= 5000,
	w = 200,
	h = 200,
	x = 0,
	y = 0,
}


function DrawFadingColorBoxradar(x,y,w,h,alpha,mode)
	if (mode == 2) then
		local calculater	= h / 100
		local calculate		= h / 200
		local calculate2	= h / 200
		
		for i = 0,h do
			surface.SetDrawColor(200-i/calculate,200-i/calculate,200-i/calculate,alpha)
			surface.DrawLine(x,i+y,x+w,i+y)
		end
	else
		local calculater	= h / 100
		local calculate		= h / 200
		local calculate2	= h / 200
		
		for i=0,h do
			surface.SetDrawColor(i/calculate,i/calculate,i/calculate,alpha)
			surface.DrawLine(x,i+y,x+w,i+y)
		end
	end
end

local function DrawRotatingCrosshair(x,y,time,length,gap)
    surface.DrawLine(
        x + (math.sin(math.rad(time)) * length),
        y + (math.cos(math.rad(time)) * length),
        x + (math.sin(math.rad(time)) * gap),
        y + (math.cos(math.rad(time)) * gap)
    )
end

local function ScaleColorAlpha(col,scale)
	return Color(col.r,col.g,col.b,col.a*scale)
end


local function ValidTarget(ent)
	if not ValidEntity(ent) then return end
	if !(ent:IsPlayer() or ent:IsNPC()) then return false end
	if ent == LocalPlayer() then return false end
	
	if ent:IsNPC() then
		if ent:GetMoveType() == MOVETYPE_NONE then return false end		
		return true
	end
end

local function HeadPos(ply) 
	if not ValidEntity(ply) then return end
	
	if (ply:GetClass() == "npc_tripmine") && (ply:GetClass() == "npc_satchel") && (ply:GetClass() == "npc_grenade_frag") then
		return false
	end
	
	if ply:IsPlayer() then
		local hdpos = ply:LookupAttachment("eyes")
		return ply:GetAttachment(hdpos).Pos
	else
		return ply:LookupBone("ValveBiped.Bip01_Head1")
	end
end
local function Visible(ply) 
	local Trace = util.TraceLine( {start = LocalPlayer():GetShootPos(),endpos = HeadPos(ply),filter = {LocalPlayer(), ply}} )
	
	return (Trace.Fraction == 1)
end


local function BRadar()
	if not radar.Enabled:GetBool() then return end
	
	surface.SetDrawColor(0,0,0,255)
	surface.DrawRect(0,0,200,200)
	
	DrawFadingColorBoxradar(0,0,200,30,255,2)
	DrawFadingColorBoxradar(0,200-31,200,30,255,1)
	
	surface.SetDrawColor(90,106,79,255) 
	surface.DrawOutlinedRect(0,0,200,200) 
	surface.DrawLine(10,100,190,100) 
	surface.DrawLine(100,10,100,190)
	
	local time = (CurTime()*2) * -180
	DrawRotatingCrosshair(100,100,time,90,0)
	
	--Default values
	radar.bgcolor			= Color(255,0,0,255)
	radar.fgcolor			= Color(0,0,255,255)
	radar.player_color		= Color(0,150,255,255)
	
	--surface.CreateFont("Arial",64,400,false,false,"RadarPlayerLabel")
	
	local ETable = {}
	local ply = LocalPlayer()
	
	
	if (radar.player_show) then
		local vertices = {}
		
		for i=1,radar.screendetail do
			local shift = math.fmod(CurTime()*radar.screenrotation,360)
			local sizescale = 1
			
			local tab = {}
				tab.x = radar.x+radar.w/2 + math.cos(math.Deg2Rad((360/radar.screendetail)*i+shift)) * radar.w/2 * sizescale
				tab.y = radar.y+radar.h/2 + math.sin(math.Deg2Rad((360/radar.screendetail)*i+shift)) * radar.h/2 * sizescale
				tab.u = 0
				tab.v = 0
			table.insert(vertices,tab)
		end
		
		local players = {}
		for k,ent in pairs(ents.GetAll()) do
			if ent:IsValid() then
				for k,v in ipairs(radar.scanfor) do
					if string.find(ent:GetClass(),v) then
						table.insert(players,ent)
					end
				end
			end
		end
		
		for i,pl in ipairs(players) do
			local cx = radar.x+radar.w/2
			local cy = radar.y+radar.h/2
			local vdiff = pl:GetPos()-ply:GetPos()
			if pl:IsPlayer() then
				if (pl:Alive() and ply~=pl) then
					local px = (vdiff.x/radar.radius)
					local py = (vdiff.y/radar.radius)
					local z = math.sqrt(px*px + py*py)
					local phi = math.Deg2Rad(math.Rad2Deg(math.atan2(px,py)) - math.Rad2Deg(math.atan2(ply:GetAimVector().x,ply:GetAimVector().y)) - 90)
					px = math.cos(phi)*z
					py = math.sin(phi)*z
					draw.RoundedBox(0,math.Clamp(cx+px*radar.w/2-4,0,190),math.Clamp(cy+py*radar.h/2-4,0,190),8,8,Color(0,100,255,255))
					surface.SetDrawColor(0,0,0,255)
					surface.DrawOutlinedRect(math.Clamp(cx+px*radar.w/2-4,0,190),math.Clamp(cy+py*radar.h/2-4,0,190),8,8)
				end
			end
			
			if ((not pl:IsPlayer()) and pl:IsValid()) then
				local isDangerous = false
				if (radar.hazardmode) then
					for k,v in ipairs(radar.dangerous) do
						if (pl:GetClass() == v) then
							table.insert(ETable,pl)
							isDangerous = true
						end
					end
				end
				
				local px = (vdiff.x/radar.radius)
				local py = (vdiff.y/radar.radius)
				local z = math.sqrt(px*px + py*py)
				local phi = math.Deg2Rad(math.Rad2Deg(math.atan2(px,py)) - math.Rad2Deg(math.atan2(ply:GetAimVector().x,ply:GetAimVector().y)) - 90)
				px = math.cos(phi)*z
				py = math.sin(phi)*z
				
				if (isDangerous == false) then
					draw.RoundedBox(0,math.Clamp(cx+px*radar.w/2-4,0,190),math.Clamp(cy+py*radar.h/2-4,0,190),8,8,Color(255,255,255,255))
					surface.SetDrawColor(0,0,0,255)
					surface.DrawOutlinedRect(math.Clamp(cx+px*radar.w/2-4,0,190),math.Clamp(cy+py*radar.h/2-4,0,190),8,8)
					
					if pl:IsNPC() then
						if ValidTarget(pl) then
							if(Visible(pl)) then
								drawColor = Color(255,0,0,255)
							else
								drawColor = Color(0,255,255,255)
							end
							draw.RoundedBox(0,math.Clamp(cx+px*radar.w/2-4,0,190),math.Clamp(cy+py*radar.h/2-4,0,190),8,8,drawColor)
							surface.SetDrawColor(0,0,0,255)
							surface.DrawOutlinedRect(math.Clamp(cx+px*radar.w/2-4,0,190),math.Clamp(cy+py*radar.h/2-4,0,190),8,8)
						end
					end
				end
				
				if radar.player_showname then
					local nametag = ""
					
					if string.find(pl:GetClass(),"blastfungus") then
						nametag = ""
					elseif string.find(pl:GetClass(),"npc_") then
						nametag = string.sub(pl:GetClass(),5)
					elseif string.find(pl:GetClass(),"sent_") then
						nametag = string.sub(pl:GetClass(),6)
					elseif string.find(pl:GetClass(),"prop_vehicle_") then
						nametag = string.sub(pl:GetClass(),14)
					else
						nametag = pl:GetClass()
					end
					
					local nametable = string.Explode("_",nametag)
					nametag = table.concat(nametable," ")
					local nametag1 = string.sub(nametag,0,1)
					local nametag2 = string.sub(nametag,2)
					nametag1 = string.upper(nametag1)
					nametag = nametag1..nametag2
				end
			end
		end
	end
	
	local count = table.Count(ETable)
	if (count > 0) then
		for k,pl in ipairs(ETable) do
			local cx = radar.x+radar.w/2
			local cy = radar.y+radar.h/2
			local vdiff = pl:GetPos()-ply:GetPos()
			local px = (vdiff.x/radar.radius)
			local py = (vdiff.y/radar.radius)
			local z = math.sqrt(px*px + py*py)
			
			local phi = math.Deg2Rad(math.Rad2Deg(math.atan2(px,py)) - math.Rad2Deg(math.atan2(ply:GetAimVector().x,ply:GetAimVector().y)) - 90)
			px = math.cos(phi)*z
			py = math.sin(phi)*z
			draw.RoundedBox(0,math.Clamp(cx+px*radar.w/2-4,0,190),math.Clamp(cy+py*radar.h/2-4,0,190),8,8,ScaleColorAlpha(radar.player_color,255))
			surface.SetDrawColor(0,0,0,255)
			surface.DrawOutlinedRect(math.Clamp(cx+px*radar.w/2-4,0,190),math.Clamp(cy+py*radar.h/2-4,0,190),8,8)
		end
		
		radar.bgcolor = Color(255,255,255,0)
		radar.fgcolor = Color(60,60,60,100)
	end
end

hook.Add("HUDPaint", "BRadar", BRadar)






