/*
	DHD SENT for GarrysMod10
	Copyright (C) 2007  aVoN

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
StarGate.LifeSupportAndWire(ENT); -- When you need to add LifeSupport and Wire capabilities, you NEED TO CALL this before anything else or it wont work!
ENT.Type = "anim";
ENT.PrintName = "DHD_base";
ENT.Author = "aVoN";
ENT.Category = 	"Stargate";
ENT.Spawnable = false;
ENT.AdminSpawnable = false;
ENT.IsDHD = true;

-- The directionvectors, relativly from the EntPos to to the chevrons pos - The numbers and chars behind it will aquire a human readable adress like 1B3D5F-Chevron7 - Chevron7 will always be "Â", because the gmod10 servers are on earth :D
ENT.ChevronPositions = {
	-- Inner Ring
	[0] = Vector(-4.0658,-2.4584,50.2675), -- 0
	[1] = Vector(-4.7312,-5.4609,50.6237), -- 1
	[2] = Vector(-6.0485,-8.1647,51.0135), -- 2
	[3] = Vector(-7.9624,-9.9222,51.6190), -- 3
	[4] = Vector(-10.3395,-11.3804,52.1868), -- 4
	[5] = Vector(-13.3051,-11.5586,52.8772), -- 5
	[6] = Vector(-16.2198,-10.8844,53.3689), -- 6
	[7] = Vector(-18.5908,-9.2996,53.7240), -- 7
	[8] = Vector(-20.5543,-6.7615,54.0810), -- 8
	[9] = Vector(-21.3200,-3.7891,54.1376), -- 9
	A = Vector(-21.3386,-0.9607,54.1197), -- A
	B = Vector(-20.4192,1.7792,54.0696), -- B
	C = Vector(-18.5956,4.4294,53.6600), -- C
	D = Vector(-15.9048,5.8632,53.3163), -- D
	E = Vector(-13.0053,6.5919,52.8201), -- E
	F = Vector(-10.4893,6.1567,52.2995), -- F
	G = Vector(-7.8930,5.1709,51.5176), -- G
	H = Vector(-5.6452,3.4045,50.7085), -- H
	I = Vector(-4.5230,0.6274,50.4277), -- I
	-- Outer Ring
	J = Vector(0.3713,-2.3076,47.1455), -- J
	K = Vector(-0.1070,-7.0895,47.2995), -- K
	L = Vector(-2.1838,-11.2771,48.0627), -- L
	M = Vector(-5.5071,-14.4634,49.1690), -- M
	N = Vector(-9.4098,-16.0330,50.4037), -- N
	O = Vector(-13.8272,-16.4415,51.4188), -- O
	["#"] = Vector(-18.3230,-15.3159,52.1698), -- Chevron7
	P = Vector(-22.1870,-12.4302,52.7326), -- P 
	Q = Vector(-25.0480,-9.1257,53.1468), -- Q
	R = Vector(-26.3908,-4.6379,53.1207), -- R
	S = Vector(-26.5401,-0.0283,53.0676), -- S
	T = Vector(-24.9045,4.1786,53.1408), -- T
	U = Vector(-21.9577,7.7969,52.6461), -- U
	V = Vector(-18.0805,10.4695,52.0660), -- V
	W = Vector(-13.9203,11.4396,51.4206), -- W
	X = Vector(-9.4366,11.0476,50.3865), -- X
	Y = Vector(-5.6275,9.1893,49.2970), -- Y
	Z = Vector(-2.1252,6.5420,47.9454), -- Z
	["@"] = Vector(-0.1822,2.2189,47.3186), -- @
	-- Engage
	DIAL = Vector(-12.7467,-2.5975,55.0572), -- The middle "enter" button ;)
}

--################# Gets the button, a player is aiming at @aVoN
function ENT:GetCurrentButton(p,multi)
	local e = self.Entity;
	local c = self.ChevronPositions;
	local t = p:GetEyeTrace();
	local cv = self.Entity:WorldToLocal(t.HitPos):Normalize()
	local btn -- Possible chevron;
	if(multi) then
		btn = {};
	end
	local lastd = 1337; -- Last distance
	for k,v in pairs(c) do
		local da = math.deg(math.acos(v:DotProduct(cv)/v:Length())); -- The differangle
		if(k == "DIAL" and not multi) then
			if(da < 5) then
				btn = k;
				break;
			end
		else
			if(multi) then
				if(da < multi) then
					table.insert(btn,{button=k,angle=da});
				end
			else
				if(da < 3.1) then
					if(da < lastd) then
						lastd = da;
						btn = k;
					end
				end
			end
		end
	end
	return btn;
end
