-- warning: do not edit this file as it will make scripts incompatible

-- incremenal version number, you can use it to provide a version condition in case a feature gets deprecated
script_version = 4

-- header --

local all_blocks = {}
local b_ct = 1

local all_types = {}
local t_ct = 1

local all_rends = {}
local r_ct = 1

-- creates a new box collider
function col()
	local bt = {
		t_id = t_ct-1,			-- collider id
		dim = {x=2, y=1, z=1}	-- dimensions
	}
	all_types[t_ct] = bt
	t_ct = t_ct + 1
	return bt
end

-- creates a new renderer
function rend()
	local r = {
		r_id = r_ct-1,		-- renderer id
		color = "#FFFFFF"	-- color
	}
	all_rends[r_ct] = r
	r_ct = r_ct + 1
	return r
end

-- get or create collider
function __get_col()
	if t_ct == 1 then
		return col()
	else
		return all_types[t_ct-1]
	end
end

-- get or create renderer
function __get_rend()
	if r_ct == 1 then
		return rend()
	else
		return all_rends[r_ct-1]
	end
end

-- creates a brick
function brk(_col, _rend)
	local _c = _col or __get_col()
	local _r = _rend or __get_rend()

	local b = {
		col = _c,				-- collider lua reference
		t_id = _c.t_id,			-- collider id
		rend = _r,				-- renderer lua reference
		r_id = _r.r_id,			-- renderer id
		pos = {x=0, y=0, z=0},	-- position
		rot = {x=0, y=0, z=0},	-- rotation
	}
	all_blocks[b_ct] = b
	b_ct = b_ct + 1
	return b
end

function set_col(_brick, _col)
	_brick.col = _col
	_brick.t_id = _col.t_id
end

function set_rend(_brick, _rend)
	_brick.rend = _rend
	_brick.r_id = _rend.r_id
end

-- standard functions --

-- builds a 3d vector
function vec(x,y,z)
	return {
		x = x,
		y = y,
		z = z
	}
end

-- vector operators

function vlen(a,b)
    if (b == nil) then
        b = {x=0,y=0,z=0}
    end
    local x, y, z = b.x-a.x, b.y-a.y, b.z-a.z
    return (x*x + y*y + z*z)^0.5
end

function vnorm(v)
	local len = vlen(v)
	
	return	{
	x = (v.x / len),
	y = (v.y / len),
	z = (v.z / len),
	}
end

--[[ -- vnorm that also returns length, do we need it?
function vnorm( v )
	local len = vlen( v )
	v.x = v.x / len
	v.y = v.y / len
	v.z = v.z / len
	return len
end
]]--

function vcross(a,b)
	local x, y, z
	x = a.y * (b.z or 0) - (a.z or 0) * b.y
	y = (a.z or 0) * b.x - a.x * (b.z or 0)
	z = a.x * b.y - a.y * b.x
	return { x=x, y=y, z=z }
end

function vadd(a,b)
	return {
	x = a.x + b.x,
	y = a.y + b.y,
	z = a.z + b.z,
}
end

function vmul(a,b)
	return {
	x = a.x * b.x,
	y = a.y * b.y,
	z = a.z * b.z,
}
end

function vneg(v)
	return {
	x = -v.x,
	y = -v.y,
	z = -v.z,
	}
end

function vscl(v, num)
	return {
		x = v.x*num,
		y = v.y*num,
		z = v.z*num,
	}
end



-- added in v2
function deg2rad(num)
	return num * 0.0174533
end

-- added in v2
function rad2deg(num)
	return num * 57.295779513
end

-- added in v2
function vdeg2rad(rot)
	return {
		x = rot.x * 0.0174533,
		y = rot.y * 0.0174533,
		z = rot.z * 0.0174533,
	}
end

-- added in v2
function vrad2deg(rot)
	return {
		x = rot.x * 57.295779513,
		y = rot.y * 57.295779513,
		z = rot.z * 57.295779513,
	}
end

function dir2angle(dir)
	_dir = vnorm(dir)
	return rad2deg(math.atan(_dir.z, _dir.x))
end

-- other utils

-- added in v1
function randomize_colors(color1, color2)
	local r1=rend()
	r1.color = color1 or "#C0C0C0"
	local r2=rend()
	r2.color = color2 or "#898989"

	local ct = b_ct
	for i=1,ct-1 do
		local r = r1
		if math.random() < 0.5 then r = r2 end
		set_rend(all_blocks[i], r)
	end
end

-- end of header --

--@--

-- footer --

return {all_types, all_rends, all_blocks}