--[[
	autorun/client/trails.lua
	0ubliette | (STEAM_0:0:51345479)
	===DStream===
]]

include("shared.lua")

t = {} -- t[Player][K] = v(Vector)

local function MoveTableStuffUP(ply)
	for i = 1,32 do
		if i == 32 then continue end
		t[ply][i] = t[ply][i+1]
	end
end

timer.Create("timer1", 0.05, 0, function()
	for k,v in pairs(t) do
		MoveTableStuffUP(k)
	end
end)



local function distanceVec(vec,vec2)
	local dis = vec2 - vec
	return math.sqrt(math.sqrt(dis.x^2 + dis.y^2)^2 + dis.z^2)
end

local function GetDirection(k1,k2)
	local dir = k2 - k1
	if dir.x > dir.y && dir.x > dir.z then
		return "L",dir
	elseif dir.x < dir.y && dir.x < dir.z then
		return "R",dir
	elseif dir.y > dir.x && dir.y > dir.z then
		return "F",dir
	elseif dir.y < dir.x && dir.y < dir.z then
		return "B",dir
	elseif dir.z > dir.x && dir.z > dir.y then
		return "U",dir
	elseif dir.z < dir.x && dir.z < dir.y then
		return "D",dir
	end
	return dir
end
	
hook.Add("Think","trails",function()
	for k,v in pairs(t) do
		if distanceVec(v[32],k:GetPos()) > 64 then
			MoveTableStuffUP(k)
			v[32] = k:GetPos()
		end
	end
end)

local mat = Material( "models/shiny" )

hook.Add("HUDPaint", "DrawBox", function()
	for k,v in pairs(t) do
		local matrix = Matrix( );
		matrix:Translate( LocalPlayer():GetPos():ToScreen() );
		matrix:Rotate( Angle(0,0,0) );
	
		cam.PushModelMatrix( matrix );
	
			render.SetMaterial( mat );
			if i == 1 then continue end
			for i = 32,1,-1 do
			mesh.Begin( MATERIAL_QUADS, i );
				local d = GetDirection(v[i],(v[i-1] or v[i]))
				if i == 32  then
					mesh.Quad(Vector( -32*(i/32), 32*(i/32), 0 ) + k:GetPos() ,
							Vector( 32*(i/32), 32*(i/32), 0 ) + k:GetPos(),
							Vector( 32*((i-1)/32), -32*((i-1)/32), 0 ) + (v[i-1] or Vector(0,0,0)),
							Vector( -32*((i-1)/32), -32*((i-1)/32), 0 ) + (v[i-1] or Vector(0,0,0)));
					
				else
				
					mesh.Quad( Vector( -32*(i/32), 32*(i/32), 0 ) + v[i] ,
						Vector( 32*(i/32), 32*(i/32), 0 ) + v[i],
						Vector( 32*((i-1)/32), -32*((i-1)/32), 0 ) + (v[i-1] or Vector(0,0,0)),
						Vector( -32*((i-1)/32), -32*((i-1)/32), 0 ) + (v[i-1] or Vector(0,0,0)));
					
				end
			
			mesh.End( );
			end
	
		cam.PopModelMatrix( );
	end
end)

function InsertPlayer(ply)
	t[ply] = {}
	for i = 1,32 do
		t[ply][i] = Vector(0,0,0)
	end
end
for k,v in pairs(player.GetAll()) do InsertPlayer(v) end;

PrintTable(t)