--[[
	autorun/client/Sattelite_Location.lua
	EmilyBliss
	===DStream===
]]

if ( !ConVarExists( "Radar_CamHeight" ) ) then
   CreateConVar( "Radar_CamHeight" , 1514, FCVAR_NOTIFY )
   end 
CamHeight =GetConVarNumber( "Radar_CamHeight" ) //The Height of the Big map Cam


	/*panel:AddControl("Slider", {
		Label = "Size",
		Type = "Float",
		Min = "16",
		Max = "512",
		Command = "massigniter_size"
	})  */
// Some Pre Definied Variables
Posizizw = 0.88
Posizizh = 0.11
Sizizw = 160
Sizizh = 160
Bigmap = false
Drawthemap = false

//Define some other things
Keyreleased2 = 0
//Bind m to the map mode


local X = ScrW()
local Y = ScrH()

function Drawe()
if Radar == true then
--draw.RoundedBox( 10, X - 175, 95, 160, 160, Color(220, 220, 220, 220) )
--draw.RoundedBox( 10, X - 170, 100, 150, 150, Color(20, 20, 20, 200) )

local LocalPos = LocalPlayer():GetPos()
	if Bigmap == true then
	Posizizw = math.Approach(Posizizw, 0.2 , 0.14)
	Posizizh = math.Approach(Posizizh, 0.2, 0.02)
    Sizizw = math.Approach(Sizizw, X * 0.6, 160)
	Sizizh = math.Approach(Sizizh, Y * 0.6, 100)
	Drawthemap = true
	elseif Bigmap == false then
    Posizizw= math.Approach(Posizizw, 0.895, 0.14)
	Posizizh = math.Approach(Posizizh, 0.11 , 0.02)
    Sizizw = math.Approach(Sizizw, 160, 170)
	Sizizh = math.Approach(Sizizh, 160, 120)
	if Posizizw == 0.895 then
	Drawthemap = false
	end
    end	
	if Drawthemap == true then
	local LocalPos = LocalPlayer():GetPos()
	local x = LocalPos.x
	local y = LocalPos.y
	local z = LocalPos.z
	draw.RoundedBox( 10, X * Posizizw - 10, Y * Posizizh - 30,  Sizizw + 20 ,  Sizizh + 40, Color(220, 220, 220, 220) )
	if Posizizw == 0.2 then
	draw.SimpleText("Satellite Cam", "DefaultSmall", X  * 0.5 ,Y * 0.2 - 20, Color(0,0,0,255),1,1) 
	draw.SimpleText("Your Position: " .. math.Round(x) .. "," .. math.Round(y) .."," ..math.Round(z), "DefaultSmall", X  * 0.65 + 30,Y * 0.2 - 20, Color(0,0,0,255),1,1) 
	draw.SimpleText(" Height: " .. math.Round(CamHeight), "DefaultSmall", X  * 0.22 ,Y * 0.2 - 22, Color(0,0,0,255),1,1) 
	slider:SetVisible(true)
	button:SetVisible(true)
    end
	surface.SetDrawColor( 255,255 ,255 , 255 ) 
	local CamData = {}
   CamData.angles = Angle(90,0,0)
   CamData.origin = LocalPos + Vector(0,0,CamHeight)
   CamData.x = X * Posizizw
   CamData.y = Y * Posizizh
   CamData.w =  Sizizw
   CamData.h =  Sizizh
   render.RenderView( CamData ) 
    end
	for k,v in pairs(player.GetAll()) do  // Every Player gets drawn.
		local Ang = (LocalPos - v:GetPos()):Angle() - LocalPlayer():GetAngles() + Angle(0,90,0)
		local Distance = LocalPos:Distance(v:GetPos()) / 70 + 15
		local Dis = (( v:GetPos() - LocalPos ) / Distance)

		local DisUnit = math.sqrt((Dis.x) * (Dis.x) + (Dis.y) * (Dis.y)) //Thanks
		local posy = (math.sin( math.rad(Ang.y) )) * DisUnit  // to
		local posx = (math.cos( math.rad(Ang.y) )) * DisUnit  //CommodoFox aka. Scarecrow :D Your an Math genius

		local Differ = (LocalPos.z - v:GetPos().z) / 120
		if Differ > 1 then
			Colorgb = 5 + Differ 
			Colorg = 0
			Colorgl = 0
		elseif Differ < 1 && Differ > -1 then
			Colorg = 255
			Colorgb = 255
			Colorgl = 0
		elseif Differ < -1 then
			Colorgl = 0 - Differ
			Colorg = 0 - (Differ / 3)
			Colorgb = 0 - (Differ / 3)
		end
	surface.SetDrawColor( Colorgb, Colorg, Colorgl, 255 ) 
	surface.DrawLine( X  - 92.8 - posx, 175 + posy, X  - 92.8 - posx, 175 + posy + Differ )

	local Team =  v:Team() 
	local Coloria = team.GetColor( Team )
	surface.SetFont( "DefaultSmall" )
	local w, h = surface.GetTextSize( v:Nick() )
	if w > 45 then
	stringi = string.sub(v:Nick(),1,12 ).. ".."
	else
	stringi = v:Nick()
	end
	--draw.SimpleText(stringi .. " (" .. team.GetName( Team ) .. ")", "DefaultSmall", X  - 92  - posx , 168 + posy , Color(Coloria.r,Coloria.g,Coloria.b,245),1,1) 

	--draw.SimpleText(".", "ScoreboardText", X  - 92 - posx ,172 + posy , Color(Colorgb,Colorg,Colorgl,253),1,1) 
	
	// I know,it sucks,but it works.
	
	if Bigmap == true && Posizizw == 0.2  then
    local ztar = LocalPos.z -(v:GetPos().z)
	local Nor = (LocalPos - v:GetPos())
	Nor:Rotate( Angle(0,180,0) );
	local Norxi = Nor.x * (Sizizw / ((CamHeight *(1.48  * (ScrW() / ScrH()))) + ztar * (1.48  * (ScrW() / ScrH()))))
	local Noryi = Nor.y * (Sizizh / ((CamHeight * 1.5) + (ztar * 1.5)))
	
	if Norxi < (Sizizh / 2) && Noryi < (Sizizw / 2) && Norxi > (-Sizizh / 2) && Noryi > (-Sizizw / 2) then
	surface.DrawLine( X / 2, Y / 2,X / 2 - Noryi,Y / 2 - Norxi )
	--draw.SimpleText(stringi .. " (" .. team.GetName( Team) .. ")", "ScoreboardText", X  * 0.5  - Noryi , Y * 0.5 - Norxi , Color(Coloria.r,Coloria.g,Coloria.b,255),1,1) 
    end
	end
end

--local NorthPoint = LocalPos + Vector(6200,0,0)

--local Ang = (LocalPos - NorthPoint):Angle() - LocalPlayer():GetAngles() + Angle(0,90,0)
--local Distance = LocalPos:Distance(NorthPoint) / 70 + 15
--local Dis = (( NorthPoint - LocalPos ) / Distance)

--local DisUnit = math.sqrt((Dis.x) * (Dis.x) + (Dis.y) * (Dis.y))
--local posy = (math.sin( math.rad(Ang.y) )) * DisUnit  
--local posx = (math.cos( math.rad(Ang.y) )) * DisUnit

	--draw.SimpleText("N","ScoreboardText", X  - 92  - posx , 168 + posy , Color(180,0,0,255),1,1) 
	--draw.SimpleText("S","ScoreboardText", X  - 92  + posx , 168 - posy , Color(180,0,0,255),1,1)
	--draw.SimpleText("o","DefaultSmall", X  - 92  - posx , 172 + posy , Color(220,220,180,205),1,1) 
	--	draw.SimpleText("o","DefaultSmall", X  - 92  + posx , 172 - posy , Color(220,220,180,205),1,1) 
	--surface.SetDrawColor( 100,200 ,200 , 255 ) 
	--surface.DrawLine( ScrW()  - 92.8 - posx , 175 + posy , X  - 92.8 + posx , 175 - posy )
end
end
hook.Add("HUDPaint", "Draw", Drawe)

	
function Enableradar(ply,com,args)
	slider = vgui.Create("Slider")
	slider:SetPos(X *0.25,Y * 0.2 -25)
	slider:SetSize(100,20)
	slider:SetMin(100)
	slider:SetMax(10000)
	slider:SetText("Camera Height")
	slider:SetValue(GetConVarNumber("Radar_CamHeight"))
	
		button = vgui.Create("Button")
	button:SetPos(X*0.26 + 90,Y * 0.2 - 25)
	button:SetSize(50,20)
	button:SetText("Apply")
	button:SetVisible(false)
	slider:SetVisible(false)
Radar = util.tobool(args[1])
	if tonumber(args[1]) == 3 then 
	if Radar == true then
Radar = false
	else
Radar = true
	end
	end
end
concommand.Add("radar_enable",Enableradar)

function AnRadar(ply,k)
if k == "+map" then
if Bigmap == false then
Bigmap = true
if Radar == true then
surface.PlaySound("weapons/sniper/sniper_zoomin.wav")
	gui.EnableScreenClicker(true) 
	function button:DoClick()
	ApplynewHeight()
	surface.PlaySound("buttons/button14.wav")
	end
end
end
end
end
function ApplynewHeight()
slider:SetConVar("Radar_CamHeight")
CamHeight = GetConVarNumber("Radar_CamHeight")
end
function AusRadar(ply,k)
if k == "-map" then
if Bigmap == true then
Bigmap = false
if Radar == true then
surface.PlaySound("weapons/sniper/sniper_zoomout.wav")
button:SetVisible(false)

slider:SetVisible(false)
  gui.EnableScreenClicker(false) 
end
end
end
end
hook.Add( "KeyPress", "KeyPressedHook", AnRadar )  
hook.Add( "KeyRelease", "KeyPressedHook", AusRadar )  
concommand.Add("+map",AnRadar)
concommand.Add("-map",AusRadar)
//hook.Add("ShowTeam", "School", Enableradar,LocalPlayer(),"radar_enable",3) 

