include( 'shared.lua' )
surface.CreateFont ("coolvetica", 28, 400, true, false, "ShieldFont")
surface.CreateFont ("coolvetica", 33, 400, true, false, "HullFont")
include( 'VGUI.lua' )
include( 'cl_radials.lua')
--include( 'cl_frettapanels.lua')

CreateClientConVar("DS_HudYOffset", "0", true, false)
HudHeightConVar = CreateClientConVar("DS_HudYSize", "1", true, false)
WMMapName = ""
local TLTick = 0
credits = 0
local PowerDis = Vector(2,2,2)



/*===========Targetting==================*/

DSTargetArray = {}

function DSHitWeapons(ply, bind, pressed)
		if bind == "slot1" then
		DS_PreviousTarget()
		elseif bind == "slot2" then
		DS_NextTarget()
		elseif bind == "slot3" then
		DS_ClosestTarget()
		end
end
hook.Add("PlayerBindPress", "DSHitWeapons", DSHitWeapons)

function DS_PreviousTarget()
DS_TargetFind()
if #DSTargetArray <= 1 then return false end
		table.insert(DSTargetArray, table.remove(DSTargetArray, 1))	
end

function DS_NextTarget()
DS_TargetFind()
if #DSTargetArray <= 1 then return false end
		table.insert(DSTargetArray, 1, table.remove(DSTargetArray))	
end

function DS_ClosestTarget()
DS_TargetFind()
if #DSTargetArray < 2 then return false end
local selfpos = LocalPlayer():GetNetworkedEntity("SpaceShip"):GetPos()
local bestdist = 5000
local curwinner = 1
	for k,v in pairs(DSTargetArray) do
	local tempdist =  selfpos:Distance(v:GetPos())
		if v:IsValid() && tempdist < bestdist then
		bestdist = tempdist
		curwinner = k
		end		
	end
table.insert(DSTargetArray, 1, table.remove(DSTargetArray, curwinner))
end

function DS_DistanceCheck()
	if LocalPlayer():GetNetworkedEntity("SpaceShip"):IsValid() then
		for k,v in pairs (DSTargetArray) do
			if DSTargetArray[v]:IsValid() && LocalPlayer():GetNetworkedEntity("SpaceShip"):GetPos():Distance(DSTargetArray[v]:GetPos()) < 5000 then
			
			else
			table.Remove(DSTargetArray, k)
			end
		end
	end
	timer.Simple(5, DS_DistanceCheck)
end
timer.Simple(5, DS_DistanceCheck)

function DS_TargetFind()
if LocalPlayer():GetNetworkedEntity("SpaceShip"):IsValid() then
local selfpos = LocalPlayer():GetNetworkedEntity("SpaceShip"):GetPos()
	for k,v in pairs (ents.FindByClass("ds_s_*")) do
		if v:IsValid() && v.dt.Team ~= LocalPlayer():GetNetworkedEntity("SpaceShip").dt.Team then
			if selfpos:Distance(v:GetPos()) <= 2500 && table.HasValue(DSTargetArray, v) == false then
			table.insert(DSTargetArray, v)	
			end
		end
	end
end
end


ForbElem = {
"CHudSuitPower",
"CHudHealth",
"CHudBattery",
"CHudAmmo",
"CHudSecondaryAmmo"
}
 
--"CHudCrosshair"

function HideThings(name)
   for _,v in pairs(ForbElem) do
 if name == v then
       return false
     end
    end
   return true
 end

hook.Add( "HUDShouldDraw", "HideThings", HideThings ) 

local helpbar = CreateClientConVar("cl_wm_helpbar", 1 , true)

local MaxShields = 10000
local Shields = 10000
local Hull = 10000
local MaxHull = 10000
local MaxEnergy = 1000
local Energy = 1000
local ABFuel = 1000
local MaxFuel = 1000
target = nil

function ABFUpdate(um)
ABFuel = um:ReadShort()
end
usermessage.Hook("ABFUpdate", ABFUpdate)

function UpdateShipMaxes(um)
local vec = um:ReadVector()
MaxHull = vec.x
MaxShields = vec.y
MaxEnergy = vec.z
Energy = MaxEnergy
MaxFuel = um:ReadShort()
ABFuel = MaxFuel
PowerDis = Vector(2,2,2)
end
usermessage.Hook("ShipMaxes", UpdateShipMaxes)

function UpdateShipNRG(um)
Energy = um:ReadShort()
end
usermessage.Hook("NRGUpdate", UpdateShipNRG)

function CreditUpdate(um)
credits = um:ReadShort()
end
usermessage.Hook("CreditUpdate", CreditUpdate)

/*function OtherShipStatUpdate(um)
Msg("Suck my cock")
local e = um:ReadEntity()
Msg(tostring(e))
e.MaxHull = um:ReadLong()
e.MaxShields = um:ReadShort()
end
usermessage.Hook("OtherShipStatUpdate", OtherShipStatUpdate)*/

/*local polytex = surface.GetTextureID( "gui/ds_gradient3")

healthvertex = {{},{},{},{}}
 --First Vertex
healthvertex[1]["x"] = centerx
healthvertex[1]["y"] = centery
healthvertex[1]["u"] = 0 //Top Left
healthvertex[1]["v"] = 0
 
 --Second Vertex
healthvertex[2]["x"] = centerx + 200
healthvertex[2]["y"] = centery
healthvertex[2]["u"] = 1 //Top Right
healthvertex[2]["v"] = 0
 
 --Third Vertex
healthvertex[3]["x"] = centerx + 200
healthvertex[3]["y"] = centery + 100
healthvertex[3]["u"] = 1 //Bottom Left
healthvertex[3]["v"] = 1

healthvertex[4]["x"] = centerx
healthvertex[4]["y"] = centery + 50
healthvertex[4]["v"] = 0
healthvertex[4]["u"] = 1 //Bottom Left
*/
local centerx = ScrW()/2
local centery = ScrH()/2
local sidelength = 512
local centlength = 256
local centmat = Material("gui/ds_middle_center.vtf")
local leftmat = Material("gui/ds_left_side.vtf")
local rightmat = Material("gui/ds_right_side.vtf")
local leftbarmat = Material("gui/ds_left_bar.vtf")
local rightbarmat = Material("gui/ds_right_bar.vtf")
local barmat = Material("gui/ds_bar.vtf")
local lefttarmat = Material("gui/left_tar.vtf")
local righttarmat = Material("gui/right_tar.vtf")
local colar = Color(255,255,255,255)

local halfcentlength = 0
local leftpanelX = 0
local rightpanelX = 0
local topbarY1 = 0
local topbarY2 = 0
local bottombarY1 = 0
local bottombarY2 = 0
local leftbarsXAnchor = 0
local rightbarsXAnchor = 0
local bladelength = 0
local fullbarlength = 0
local healthlength = 0
local shieldlength = 0
local energylength = 0
local abfuellength = 0
local percenthealth = 100
local percentshields = 100
local percentabfuel = 100
local percentenergy = 100
local centertextoffset = 0
local barYOffset = 0
local panelY = 0
local hudtextYstart = 0
local pbarwidth = 0
local pbarheight = 0
local pbarstartX = 0
local pbarstartY = 0
local pbaroffset = 0
--local teem = LocalPlayer():Team()

--------------------------------------------------
--Vertexes for Hud Bars
--------------------------------------------------
local healthvertex = {{},{},{},{}}
local shieldvertex = {{},{},{},{}}
local abfuelvertex = {{},{},{},{}}
local energyvertex = {{},{},{},{}}

local crosshair = Material("Sprites/Hud/v_crosshair1")

function WMHudDraw()

--colar = team.GetColor(teem)
centerx = ScrW()/2
centery = ScrH()/2

/*if helpbar:GetFloat() == 1 then
draw.RoundedBox( 10, ScrW() - 355, 0, 355, 29, Color(0, 0, 0, 180) );
draw.DrawText("F1-Help F2-Encyclopedia F3-Ship Design F4-Team ", "ScoreboardSubtitle", ScrW(), 2, colar, TEXT_ALIGN_RIGHT)
end*/
local ship = LocalPlayer():GetNetworkedEntity("SpaceShip")
if ship:IsValid() then
Shields = ship.dt.Shields
Hull = ship.dt.Hull
else
return
end

percenthealth = Hull / MaxHull
percentshields = Shields / MaxShields
percentabfuel = ABFuel / MaxFuel
percentenergy = Energy / MaxEnergy
--draw.RoundedBox( 10, ScrH() - 300, ScrW() - 250,50, 10 + (PowerDis.x *20), Color(255, 255, 255, 255) );
/*
draw.RoundedBox( 10, 110, ScrH() - 140,120, 30, Color(0, 0, 0, 180) );
draw.DrawText("Credits: " .. credits, "Default", 118, ScrH() - 135, Color(255,255,255,255),0)
draw.RoundedBox( 10, 0, ScrH() - 140,105, 30, Color(0, 0, 0, 180) );
    if GetGlobalInt("WMScenarioMode") == 0 then
    local a, b = math.modf((TLTick - CurTime()) / 60)
    draw.DrawText("Time Left: " .. math.floor(a) .. ":" .. math.Round(60 * b), "Default", 10, ScrH() - 134, Color(255,255,255,255),0)
    else
    local a, b = math.modf((CurTime() - TLTick)/ 60)
    draw.DrawText("Time: " .. math.floor(a) .. ":" .. math.Round(60 * b), "Default", 10, ScrH() - 134, Color(255,255,255,255),0)
    end
draw.RoundedBox( 10, 0, ScrH() - 110,230, 60, Color(0, 0, 0, 180) );
draw.DrawText("Shields: " .. Shields .. "/" .. MaxShields, "ShieldFont", 10, ScrH() - 93, Color(255,255,255,255),0)
draw.RoundedBox( 10, 0, ScrH() - 50,230, 60, Color(0, 0, 0, 180) );
draw.DrawText("Hull:" .. math.floor(Hull) .. "/" .. MaxHull, "HullFont", 10, ScrH() - 43, colar,0)
draw.DrawText("NRG:" .. Energy, "HullFont", 10, ScrH() - 300, colar,0)
draw.DrawText("ABFuel:" .. ABFuel, "HullFont", 10, ScrH() - 250, colar,0)
*/


if ScrW() < 1280 then
centlength = ScrW() * 0.2
sidelength = ScrW() * 0.4
else
centlength = ScrW() * 0.2
sidelength = ScrW() * 0.4
end
hudheight = 128 * HudHeightConVar:GetFloat()
barheight = 32  * HudHeightConVar:GetFloat()
barYOffset = 24 * HudHeightConVar:GetFloat()
halfcentlength = centlength / 2
leftpanelX = centerx - (sidelength + halfcentlength)
rightpanelX = centerx + (halfcentlength)
panelY = ScrH() - hudheight + 18
surface.SetDrawColor(255,255,255,255)
surface.SetMaterial( leftmat )
surface.DrawTexturedRect(leftpanelX,panelY, sidelength, hudheight)
surface.SetMaterial( centmat )
surface.DrawTexturedRect(centerx - halfcentlength, panelY, centlength, hudheight)
surface.SetMaterial( rightmat )
surface.DrawTexturedRect(rightpanelX, panelY, sidelength, hudheight)

topbarY1 = panelY + barYOffset
topbarY2 = topbarY1 + barheight
bottombarY1 = topbarY2 + 1
bottombarY2 = bottombarY1 + barheight
leftbarsXAnchor = leftpanelX + (sidelength * 0.835)
rightbarsXAnchor = rightpanelX + (sidelength * 0.175)
bladelength = sidelength * 0.06
fullbarlength = sidelength * 0.55
healthlength = leftbarsXAnchor - ((fullbarlength + bladelength) * percenthealth)
shieldlength = leftbarsXAnchor - (fullbarlength * percentshields)
energylength = rightbarsXAnchor + ((fullbarlength + bladelength) * percentenergy)
abfuellength = rightbarsXAnchor + (fullbarlength * percentabfuel)
centertextoffset = centerx - (halfcentlength * 0.62)
hudtextYstart = ScrH() - hudheight + (hudheight * 0.172) + 18
draw.DrawText("Shield: " .. percentshields * 100 .. "%", "Default", centertextoffset, hudtextYstart, Color(13,217,221,255),0)
draw.DrawText("Hull: " .. percenthealth * 100 .. "%", "Default", centertextoffset, hudtextYstart + 18, Color(13,217,221,255),0)
draw.DrawText("Boost: " .. percentabfuel * 100 .. "%", "Default", centertextoffset, hudtextYstart + 36, Color(13,217,221,255),0)
draw.DrawText("NRG: " .. percentenergy * 100 .. "%", "Default", centertextoffset, hudtextYstart + 54, Color(13,217,221,255),0)


----Yea I probably should have just done a textured rect with transparency here, but there were some issues with GIMP and stuff that was making fiddling with the image a bitch.


	 --First Vertex
	shieldvertex[1]["x"] = shieldlength
	shieldvertex[1]["y"] = topbarY1
	shieldvertex[1]["u"] = 0 //Top Left
	shieldvertex[1]["v"] = 0
	 
	 --Second Vertex
	shieldvertex[2]["x"] = leftbarsXAnchor
	shieldvertex[2]["y"] = topbarY1
	shieldvertex[2]["u"] = 1 //Top Right
	shieldvertex[2]["v"] = 0
	 
	 --Third Vertex
	 	shieldvertex[3]["x"] = leftbarsXAnchor
	shieldvertex[3]["y"] = topbarY2
	shieldvertex[3]["u"] = 1
	shieldvertex[3]["v"] = 1 //Bottom Right

	--Fourth Vertex
	shieldvertex[4]["x"] = shieldlength - bladelength
	shieldvertex[4]["y"] = topbarY2
	shieldvertex[4]["u"] = 0 //Bottom Left
	shieldvertex[4]["v"] = 1

--==========================================================
	 --First Vertex
	healthvertex[1]["x"] = healthlength
	healthvertex[1]["y"] = bottombarY1
	healthvertex[1]["u"] = 0 //Top Left
	healthvertex[1]["v"] = 0
	 
	 --Second Vertex
	healthvertex[2]["x"] = leftbarsXAnchor
	healthvertex[2]["y"] = bottombarY1
	healthvertex[2]["u"] = 1 //Top Right
	healthvertex[2]["v"] = 0
	 
	 --Third Vertex
	healthvertex[3]["x"] = leftbarsXAnchor
	healthvertex[3]["y"] = bottombarY2
	healthvertex[3]["u"] = 1
	healthvertex[3]["v"] = 1 //Bottom Right

	--Fourth Vertex
	healthvertex[4]["x"] = healthlength - bladelength
	healthvertex[4]["y"] = bottombarY2
	healthvertex[4]["u"] = 0 //Bottom Left
	healthvertex[4]["v"] = 1
	
--===================================================================--
	 --First Vertex
	abfuelvertex[1]["x"] = rightbarsXAnchor
	abfuelvertex[1]["y"] = topbarY1
	abfuelvertex[1]["u"] = 0 //Top Left
	abfuelvertex[1]["v"] = 0
	 
	 --Second Vertex
	abfuelvertex[2]["x"] = abfuellength
	abfuelvertex[2]["y"] = topbarY1
	abfuelvertex[2]["u"] = 1 //Top Right
	abfuelvertex[2]["v"] = 0
	 
	 --Third Vertex
	abfuelvertex[3]["x"] = abfuellength + bladelength
	abfuelvertex[3]["y"] = topbarY2
	abfuelvertex[3]["u"] = 1
	abfuelvertex[3]["v"] = 1 //Bottom Right

	--Fourth Vertex
	abfuelvertex[4]["x"] = rightbarsXAnchor
	abfuelvertex[4]["y"] = topbarY2
	abfuelvertex[4]["u"] = 0 //Bottom Left
	abfuelvertex[4]["v"] = 1
	
--===================================================================--
	 --First Vertex
	energyvertex[1]["x"] = rightbarsXAnchor
	energyvertex[1]["y"] = bottombarY1
	energyvertex[1]["u"] = 0 //Top Left
	energyvertex[1]["v"] = 0
	 
	 --Second Vertex
	energyvertex[2]["x"] = energylength
	energyvertex[2]["y"] = bottombarY1
	energyvertex[2]["u"] = 1 //Top Right
	energyvertex[2]["v"] = 0
	 
	 --Third Vertex
	energyvertex[3]["x"] = energylength + bladelength
	energyvertex[3]["y"] = bottombarY2
	energyvertex[3]["v"] = 1
	energyvertex[3]["u"] = 1 //Bottom Right

	--Fourth Vertex
	energyvertex[4]["x"] = rightbarsXAnchor
	energyvertex[4]["y"] = bottombarY2
	energyvertex[4]["u"] = 0 //Bottom Left
	energyvertex[4]["v"] = 1

pbarwidth = centlength * 0.08
pbarheight = hudheight * 0.11
pbarstartX = centerx + centlength * 0.08
pbarstartY = ScrH() - (hudheight * 0.57) + 18
pbaroffset = pbarwidth / 2
	local WeaponTable = {} 
 
 	WeaponTable.texture 	= surface.GetTextureID("gui/ds_bar")
 	WeaponTable.color		= Color( 200, 200, 0, 255 ) 
 
 	WeaponTable.y = pbarstartY + (math.Clamp(2 - PowerDis.x, -2, 0) * pbarheight)
 	WeaponTable.x = pbarstartX
 	WeaponTable.w = pbarwidth 
 	WeaponTable.h = 2.5 + (math.abs(2 - PowerDis.x) * pbarheight) 
 	draw.TexturedQuad( WeaponTable )

local ShieldTable = {} 
 
 	ShieldTable.texture 	= surface.GetTextureID( "gui/ds_bar" )
 	ShieldTable.color		= Color( 0,0,200 , 255 ) 
 
 	ShieldTable.y = pbarstartY + (math.Clamp(2 - PowerDis.y, -2, 0) * pbarheight)
 	ShieldTable.x = pbarstartX + pbarwidth
 	ShieldTable.w = pbarwidth 
 	ShieldTable.h = 2.5 + (math.abs(2 - PowerDis.y) * pbarheight) 
 	draw.TexturedQuad( ShieldTable )

local EngineTable = {} 
 
 	EngineTable.texture 	= surface.GetTextureID( "gui/ds_bar" )
 	EngineTable.color		= Color( 0, 200, 0, 255 ) 
 
 	EngineTable.y = pbarstartY + (math.Clamp(2 - PowerDis.z, -2, 0) * pbarheight)
 	EngineTable.x = pbarstartX + pbarwidth * 2
 	EngineTable.w = pbarwidth
 	EngineTable.h = 2.5 + (math.abs(2 - PowerDis.z) * pbarheight) 
 	draw.TexturedQuad( EngineTable )
	
surface.SetDrawColor(0,0,200,255)
surface.SetMaterial( barmat )
surface.DrawPoly( shieldvertex )
surface.SetDrawColor(200,0,0,255)
surface.DrawPoly( healthvertex )
surface.SetDrawColor(0,255,0,255)
surface.DrawPoly( abfuelvertex )
surface.SetDrawColor(255,255,0,255)
surface.DrawPoly( energyvertex )
/*
surface.SetDrawColor(0,0,200,255)
surface.SetMaterial( barmat )
surface.SetDrawColor(200,0,0,255)
surface.SetMaterial( rightbarmat )
surface.SetDrawColor(0,255,0,255)
surface.DrawTexturedRect( rightbarsXAnchor, topbarY1,  (fullbarlength * percentabfuel)+ bladelength, barheight)
surface.SetDrawColor(255,255,0,255)
surface.DrawTexturedRect( rightbarsXAnchor, bottombarY1, (fullbarlength * percentenergy) + bladelength, barheight )
*/
--surface.SetDrawColor( 255, 0, 0, 255 )
--surface.DrawPoly( healthvertex )
--surface.SetDrawColor( 255, 0, 0, 150 )
--surface.DrawTexturedRect( centerx, centery, 32, 32 )

/*=====================================TARGET=====================*/


	if #DSTargetArray ~= 0 && DSTargetArray[1]:IsValid() && DSTargetArray[1].dt.ply:IsValid() then
	local EnemyShieldPercent = DSTargetArray[1].dt.Shields / DSTargetArray[1].dt.MaxShields
	local EnemyHullPercent = DSTargetArray[1].dt.Hull / DSTargetArray[1].dt.MaxHull
	local tarname = DSTargetArray[1].dt.ply:GetName()
		if string.len(tarname) > 20 then
		end
	if tarname == "" or tarname == nil then tarname = "Unknown" end
		
	draw.DrawText(tarname, "Default", centerx, 32, Color(255,0,0,0),1)
	draw.DrawText(LocalPlayer():GetNetworkedEntity("SpaceShip"):GetPos():Distance(DSTargetArray[1]:GetPos()), "Default", centerx, 42, Color(255,0,0,0),1)
	surface.SetMaterial( lefttarmat )
	surface.SetDrawColor(255,255,255,255)
	surface.DrawTexturedRect( centerx - 64 - 64, 0,  64, 32)
	surface.SetMaterial(  righttarmat )
	surface.DrawTexturedRect( centerx + 64, 0,  64, 32)
	surface.SetMaterial( barmat )
	surface.SetDrawColor(0,0,255,255)
	surface.DrawTexturedRect( centerx + 106, 2,  (EnemyShieldPercent * 128), 30)
	surface.SetDrawColor(255,0,0, 255)
	surface.DrawTexturedRect( centerx - (EnemyHullPercent * 128) - 96, 2,  (EnemyHullPercent * 128), 30)
	end




	
local ent = LocalPlayer():GetEyeTraceNoCursor().Entity
	if ent:IsValid() && ent.ForwardSpeed ~= nil && ent.dt.ply:IsValid() then
	local screen = ent:GetPos():ToScreen()
	draw.RoundedBox( 6, screen.x, screen.y, 100, 23, Color( 255, 255, 255, 150 ))
	end
	
	if DSTargetArray[1] ~= nil && DSTargetArray[1] :IsValid() then
	local pos = DSTargetArray[1]:GetPos() --(LocalPlayer():EyeAngles():Forward()) * 1090)
	local targetloc = pos:ToScreen()
		if targetloc.visible == false then
		surface.SetDrawColor(255,0,0,255)
		
			/*if targetloc.x > ScrW() then
				if math.abs(targetloc.x / ScrW()) > math.abs(targetloc.y / ScrH()) then
				targetloc.x = ScrW()
				targetloc.y = math.abs(math.modf(targetloc.y, ScrH()))
				else
				targetloc.x = math.abs(math.modf(targetloc.x,ScrW()))
					if targetloc.y > ScrH() then
					targetloc.y = ScrH()
					elseif targetloc.y < 0 then
					targetloc.y = 0
					end
				end
			elseif targetloc.x < 0 then
				if math.abs(targetloc.x / ScrW()) > math.abs(targetloc.y / ScrH()) then
				targetloc.x = 0
				targetloc.y = math.abs(math.modf(targetloc.y,ScrH()))
				else
				targetloc.x = math.abs(math.modf(targetloc.x,ScrW()))
					if targetloc.y > ScrH() then
					targetloc.y = ScrH()
					elseif targetloc.y < 0 then
					targetloc.y = 0
					end
				end
			else
				if targetloc.y > ScrH() then
				targetloc.y = ScrH()
				elseif targetloc.y < 0 then
				targetloc.y = 0
				end
			end

			PrintTable(targetloc)
			Msg("/n\n")*/
			surface.DrawOutlinedRect(  targetloc.x,  targetloc.y,  25,  25 )
		/*local multiplier = 0
		if targetloc.x > ScrW() then
			while(targetloc.x > ScrW()) do
			targetloc.x = targetloc.x - ScrW()
			multiplier = multiplier + 1
			end
			
			if targetloc.y > ScrH() then
			targetloc.y = targetloc
			end
			
		elseif targetloc.x < ScrW() then
		targetloc.x = 0
		end
		if targetloc.y < ScrH() then
		targetloc.y = 0
		elseif targetloc.y > ScrH() then
		targetloc.y = ScrW
		end*/
		
		/*if targetloc.x > ScrW() || targetloc.x >then
		math.fmod(targetloc.x,ScrW())
		end*/
		--LocalPlayer():EyePos() - pos
		--surface.DrawLine(  0,  0,  targetloc.x,  targetloc.y )
		--surface.DrawLine(  targetloc.x,  targetloc.y - cross_size,  targetloc.x,  targetloc.y + cross_size )
		else

			local dist = LocalPlayer():EyePos():Distance(DSTargetArray[1]:GetPos())
			local size = math.Clamp(20000 - dist,20, DSTargetArray[1]:BoundingRadius())
			surface.SetDrawColor(255,0,0,255)
			surface.DrawOutlinedRect(  targetloc.x - (size * 0.5),  targetloc.y - (size * 0.5),  size,  size )
		end
	end
end

hook.Add("HUDPaint", "WMHud", WMHudDraw); 

local TarBeam = Material( "cable/redlaser" )



function DSPostDraw()

--targetfinding beam
	local shuttleent = LocalPlayer():GetNetworkedEntity("SpaceShip")
	if LocalPlayer():GetNetworkedBool("isDriveShuttle") == true && shuttleent:IsValid() then
	render.SetMaterial(crosshair)
	render.DrawSprite((shuttleent:GetPos() + shuttleent:GetForward() * 1000), 32,32, Color(255,255,255,255))
	end
	
	if target ~= nil && target:IsValid() then
		local targetloc = target:GetPos():ToScreen()
		if !targetloc.visible then
		ender.SetMaterial( TarBeam )
		render.DrawBeam(target:GetPos(), LocalPlayer():GetNetworkedEntity("SpaceShip"):GetPos(), 5, 0,0, Color (255,0,0,100))
		end
	
	end
	
	--2D 3D cam tests. None of them really are worth doing.
		/* 
		local tarpos = target:GetPos()
		cam.Start3D2D(tarpos, LocalPlayer():GetNetworkedEntity("SpaceShip"):GetPos()- ,1)
		surface.SetDrawColor( 255, 0, 0, 255)
		local targetrad = target:BoundingRadius():
		draw.DrawText("Your Mother", "ScoreboardText", 0, 0, Color(255, 255, 255, 255), TEXT_ALIGN_CENTER )
		surface.DrawOutlinedRect(  0-targetrad, 0-targetrad ,  targetrad * 2, targetrad * 2  )
		surface.D
		cam.End3D2D()
		--surface.DrawRect(targetloc.x, targetloc.y, target:OBBMins():):ToScreen().x, util.LocalToWorld(target, target:OBBMins()*2):ToScreen().y )
		end
	end*/
end
hook.Add("PostDrawOpaqueRenderables", "DSPostDraw", DSPostDraw)

function WMDebug()
PrintTable(LocalPlayer():GetEyeTrace().Entity:GetTable()) 
end
concommand.Add("WMendebug", WMDebug)


function wNRGup( ply, cmd, arg )
   if PowerDis.x <= 3 then 
   PowerDis.x = PowerDis.x + 1
	if PowerDis.y >= 2 then
	PowerDis.y = PowerDis.y - 1
	else
	PowerDis.z = PowerDis.z - 1
	end
   end
end

function sNRGup( ply, cmd, arg )
   if PowerDis.y <= 3 then 
   PowerDis.y = PowerDis.y + 1
	if PowerDis.z >= 2 then
	PowerDis.z = PowerDis.z - 1
	else
	PowerDis.x = PowerDis.x - 1
	end
   end
end

function eNRGup( ply, cmd, arg )
   if PowerDis.z <= 3 then 
   PowerDis.z = PowerDis.z + 1
	if PowerDis.x >= 2 then
	PowerDis.x = PowerDis.x - 1
	else
	PowerDis.y = PowerDis.y - 1
	end
   end
end

function wNRGdown( ply, cmd, arg )
   if PowerDis.x >= 1 then 
   PowerDis.x = PowerDis.x - 1
	if PowerDis.z <= 2 then
	PowerDis.z = PowerDis.z + 1
	else
	PowerDis.y = PowerDis.y + 1
	end
   end
end

function sNRGdown( ply, cmd, arg )
   if PowerDis.y >= 1 then 
   PowerDis.y = PowerDis.y - 1
	if PowerDis.x <= 2 then
	PowerDis.x = PowerDis.x + 1
	else
	PowerDis.z = PowerDis.z + 1
	end
   end
end

function eNRGdown( ply, cmd, arg )
   if PowerDis.z >= 1 then 
   PowerDis.z = PowerDis.z - 1
	if PowerDis.y <= 2 then
	PowerDis.y = PowerDis.y + 1
	else
	PowerDis.x = PowerDis.x + 1
	end
   end
end

usermessage.Hook("wUp", wNRGup)
usermessage.Hook("sUp", sNRGup)
usermessage.Hook("eUp", eNRGup)
usermessage.Hook("wDown", wNRGdown)
usermessage.Hook("sDown", sNRGdown)
usermessage.Hook("eDown", eNRGdown)