#pragma once

noref vector _vlen2;
#define vlen2(v) (_vlen2 = (v), dotproduct(_vlen2, _vlen2))

#if 1
	/// Vector distance comparison, avoids sqrt()
	#define vdist(v, cmp, f) (vlen2(v) cmp ((f) ** 2))
#else
	#define vdist(v, cmp, f) (vlen(v) cmp (f))
#endif

#if 1
	#define dotproduct(a, b) ((a) * (b))
#else
	noref vector _dotproduct_a, _dotproduct_b;
	#define dotproduct(a, b) \
		(_dotproduct_a = (a), _dotproduct_b = (b), \
		  _dotproduct_a.x * _dotproduct_b.x \
		+ _dotproduct_a.y * _dotproduct_b.y \
		+ _dotproduct_a.z * _dotproduct_b.z)
#endif

#if 1
	#define cross(a, b) ((a) >< (b))
#else
	ERASEABLE
	vector cross(vector a, vector b)
	{
		return '1 0 0' * (a.y * b.z - a.z * b.y)
		     + '0 1 0' * (a.z * b.x - a.x * b.z)
		     + '0 0 1' * (a.x * b.y - a.y * b.x);
	}
#endif

noref vector _vmul_a, _vmul_b;
#define vmul(a, b) \
	(_vmul_a = (a), _vmul_b = (b), \
	  '1 0 0' * (_vmul_a.x * _vmul_b.x) \
	+ '0 1 0' * (_vmul_a.y * _vmul_b.y) \
	+ '0 0 1' * (_vmul_a.z * _vmul_b.z))

const vector eX = '1 0 0';
const vector eY = '0 1 0';
const vector eZ = '0 0 1';

ERASEABLE
vector randompos(vector m1, vector m2)
{
	vector v;
	m2 -= m1;
	v.x = m2.x * random() + m1.x;
	v.y = m2.y * random() + m1.y;
	v.z = m2.z * random() + m1.z;
	return v;
}

ERASEABLE
float vlen_maxnorm2d(vector v)
{
	return max(v.x, v.y, -v.x, -v.y);
}

ERASEABLE
float vlen_minnorm2d(vector v)
{
	return min(max(v.x, -v.x), max(v.y, -v.y));
}

/// Requires that m2>m1 in all coordinates, and that m4>m3
ERASEABLE
float boxesoverlap(vector m1, vector m2, vector m3, vector m4)
{
	return m2_x >= m3_x && m1_x <= m4_x && m2_y >= m3_y && m1_y <= m4_y && m2_z >= m3_z && m1_z <= m4_z;
}

/// Requires the same as boxesoverlap, but is a stronger condition
ERASEABLE
float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs)
{
	return smins.x >= bmins.x && smaxs.x <= bmaxs.x && smins.y >= bmins.y && smaxs.y <= bmaxs.y && smins.z >= bmins.z && smaxs.z <= bmaxs.z;
}

#define pointinsidebox(point, bmins, bmaxs) boxinsidebox(point, point, bmins, bmaxs)

#define PITCH(v) ((v).x)
#define YAW(v)   ((v).y)
#define ROLL(v)  ((v).z)

//pseudo prototypes:
// vector vec2(vector v); // returns a vector with just the x and y components of the given vector
// vector vec2(float x, float y); // returns a vector with the given x and y components

noref vector _vec2;
#define vec2(...) EVAL(OVERLOAD(vec2, __VA_ARGS__))
#define vec2_1(v) (_vec2 = (v), _vec2.z = 0, _vec2)
#define vec2_2(x, y) (_vec2_x = (x), _vec2_y = (y), _vec2)

noref vector _vec3;
#define vec3(_x, _y, _z) (_vec3.x = (_x), _vec3.y = (_y), _vec3.z = (_z), _vec3)

#define VEC_NAN vec3(FLOAT_NAN, FLOAT_NAN, FLOAT_NAN);

ERASEABLE
bool is_all_nans(vector v)
{
	return isnan(v.x) && isnan(v.y) && isnan(v.z);
}

ERASEABLE
vector Rotate(vector v, float a)
{
	if (a == 0)
		return v;
	if (a == M_PI)
		return -v;

	float a_sin = sin(a), a_cos = cos(a);
	return vec2(v.x * a_cos + v.y * a_sin, -v.x * a_sin + v.y * a_cos);
}

noref vector _yinvert;
#define yinvert(v) (_yinvert = (v), _yinvert.y = 1 - _yinvert.y, _yinvert)

/// \param[in] p point
/// \param[in] l0 starting point of ldir
/// \param[in] ldir line
/// \return Vector starting from p perpendicular to ldir
ERASEABLE
vector point_line_vec(vector p, vector l0, vector ldir)
{
	ldir = normalize(ldir);
	p = l0 - p;
	// remove the component in line direction from p
	return p - ((p * ldir) * ldir);
}

/**
 * @param dir the directional vector
 * @param norm the normalized normal
 * @returns dir reflected by norm
 */
ERASEABLE
vector reflect(vector dir, vector norm)
{
	return dir - 2 * (dir * norm) * norm;
}

/// Clip vel along the plane defined by norm (assuming 0 distance away), bounciness determined by bounce 0..1
ERASEABLE
vector vec_reflect(vector vel, vector norm, float bounce)
{
	return vel - (1 + bounce) * (vel * norm) * norm;
}

ERASEABLE
vector vec_epsilon(vector this, float eps)
{
	if (this.x > -eps && this.x < eps)  this.x = 0;
	if (this.y > -eps && this.y < eps)  this.y = 0;
	if (this.z > -eps && this.z < eps)  this.z = 0;
	return this;
}

#define ClipVelocity(in, normal, out, overbounce) \
	(out = vec_epsilon(vec_reflect(in, normal, (overbounce) - 1), 0.1))

#ifdef GAMEQC
	ERASEABLE
	vector get_corner_position(entity box, int corner)
	{
		switch (corner)
		{
			case 1:  return box.absmin;
			case 2:  return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
			case 3:  return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
			case 4:  return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
			case 5:  return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
			case 6:  return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
			case 7:  return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
			case 8:  return box.absmax;
			default: return '0 0 0';
		}
	}

	ERASEABLE
	vector NearestPointOnBox(entity box, vector org)
	{
		vector mi = box.mins + box.origin;
		vector ma = box.maxs + box.origin;

		return vec3(
			bound(mi.x, org.x, ma.x),
			bound(mi.y, org.y, ma.y),
			bound(mi.z, org.z, ma.z)
		);
	}

ERASEABLE
vector NearestPointOnBoundingBox(vector mi, vector ma, vector org)
{
	return vec3(
		bound(mi.x, org.x, ma.x),
		bound(mi.y, org.y, ma.y),
		bound(mi.z, org.z, ma.z)
	);
}

// bones_was_here: rounding bbox to nearest perfect floats prevents obscure collision bugs like #2742
// FIXME: QC shouldn't need to work around tracebox potentially returning a tiny trace_fraction when the move should have been blocked.
// Tiny values are valid in some situations and can't simply be ignored.
#define PFLOAT (1/1024) // 1/32 1/64 etc also work
#define RPFLOAT(a) (a = rint(a / PFLOAT) * PFLOAT)
ERASEABLE
vector RoundPerfectVector(vector v)
{
	RPFLOAT(v.x);
	RPFLOAT(v.y);
	RPFLOAT(v.z);
	return v;
}

ERASEABLE
vector vrint(vector v)
{
	v.x = rint(v.x);
	v.y = rint(v.y);
	v.z = rint(v.z);
	return v;
}

#endif // GAMEQC
