/**********************************************************************

	Title :			Collision Module header file 
	Filename :		collide.h
	Author :		Thomas Szirtes
	Revision :		2.0

	Notes :			Advanced collision toolkit for all types of cases.
					Incorporates volume checking, polyhedra checking and polygon
					intersection with ray

	Copyright :		(C) SEGA Europe 1998

  **********************************************************************/

#ifndef COLLIDE_H_INCLUDE
#define COLLIDE_H_INCLUDE

/*
 *		Definitions
 */

#define COLOBJ_TYPE_CUBOID			0
#define COLOBJ_TYPE_CYLINDER		1
#define COLOBJ_TYPE_CONE			2
#define COLOBJ_TYPE_SPHERE			3
#define COLOBJ_TYPE_POLYH			4

/*
 *		Structure Definitions
 */

/* plane equation */
typedef struct {
	float	a;
	float	b;
	float	c;
	float	d;
} PLANE;

/* CUBOID */
typedef struct {
	NJS_POINT3	p1;			/* bottom left */
	NJS_POINT3	p2;			/* top right */
} COLOBJ_CUBOID;

/* CYLINDER */
typedef struct {
	int			orient;		/* orientation 0=vertical/1=horizontal */
	float		r;			/* radius */
	float		length;		/* length */
} COLOBJ_CYLINDER;

/* CONE */
typedef struct {
	float		r1;			/* bottom radius */
	float		r2;			/* top radius */
	float		height;		/* height */
} COLOBJ_CONE;

/* SPHERE */
typedef struct {
	NJS_VECTOR	centre;		/* centroid */
	float		rad;		/* radius */
} COLOBJ_SPHERE;

/* CONVEX POLYHEDRA */
typedef struct {
	int	count;				/* plane count */
	PLANE	*pl;			/* plane array */
} COLOBJ_POLYH;


/* COLLISION OBJECT */
typedef struct collide_obj
{
	Uint8					type;			/* type of collide object */
											/* SPHERE, CYLINDER, CONE, CUBOID, POLYHEDRA */
	Uint8					mode;			/* ON, OFF */
	void					*collide;		/* cast depending on type */
	NJS_POINT3				pos;			/* position relative to parent */
	struct collide_obj		*child;			/* child object */
	struct collide_obj		*sibling;		/* sibling object */
} COLLIDE_OBJ;




/*
 *	FUNCTION PROTOTYPES
 */



/*---------------------------------------------------------------------------
	COLLIDE_Building

	Inputs :	p	 - check point
				inst - building instance
				coll - normal address

	Check collision with building - returns collision status and hit normal
  ----------------------------------------------------------------------------*/
int				COLLIDE_Building( NJS_POINT3 *, Uint16, NJS_VECTOR *);


/*---------------------------------------------------------------------------
	COLLIDE_NjtoPolyh
	Convertor from ninja model to optomized polyhedra format for collision
	detection

	Inputs	:	mdl		-	ninja model
				tmp		-	temporary workspace (to save mem-frag)
	
	Return	:	poly hedra object pointer

	Notes - assumes that model is convex, assumes single triangle/quad mesh only
	(ie. polygon co-planar) will then optomize based to duplicate normals 
	(hence quite slow).

    temporary workspace size needs to be 
		max number of triangles * 4 * sizeof(float)
  ---------------------------------------------------------------------------*/
COLOBJ_POLYH	*COLLIDE_NjtoPolyh( NJS_MODEL *, PLANE * );


/*---------------------------------------------------------------------------
	COLLIDE_Free
	Deallocates all memory generated by COLLIDE_NjtoPolyH and others...

	Inputs :	obj		-	object to deallocate
  ---------------------------------------------------------------------------*/
void			COLLIDE_Free( COLLIDE_OBJ * );


/*--------------------------------------------------------------------------
 
  int	COLLIDE_Floor( NJS_POINT3	*pt, float rad, NJS_VECTOR	*col )
 
  Parameters:
 	pt			center of check sphere
 	rad			radius of sphere
 	col			collision normal
 
  Returns:
 
  Notes:
	Does a very optomized check against the triangle on the floor below
	it
 
  --------------------------------------------------------------------------*/
int				COLLIDE_Floor( NJS_POINT3 *, float, NJS_VECTOR * );

/*---------------------------------------------------------------------------
	COLLIDE_Sphere

	Inputs :	p	   - check point
				colobj - collision object
				coll   - normal address

	test against sphere
  ----------------------------------------------------------------------------*/
int COLLIDE_Sphere( NJS_POINT3 *, COLOBJ_SPHERE	*, NJS_VECTOR *, float );

#endif