#ifndef __ROADS
#define __ROADS

#include "vectors.h"
#include "entities.h"

typedef struct{
	int texture;
	int materialFlags;
	int surfaceType;
	tVector2 vertex1,vertex2;
	float texCoord1,texCoord2,texZoom;
}tRoadTypeVertex;

typedef struct{
	int endTopVertices,startTopVertices;
	int startShadow,endShadow;
	float minTrack,maxTrack;
	float traction;
	float reviveX,reviveY;
	char name[80];
	int numVertices;
	tRoadTypeVertex *vertices;
} tRoadType;

typedef struct{
	int numTypes;
	tRoadType *types;
} tRoadTypeList;

typedef struct{
	float grip,slideGrip;
	float bumpHeight,bumpFreq;
	float brakeFactor;
	int smokeEnable,trackEnable,soundEnable;
	int soundEcho,sparksEnable,squeachEnable;
	int reflectionEnable,smokeStickEnable;
	float minSmokeSlideVelo,maxSmokeSlideVelo;
	float minTrackSlideVelo,maxTrackSlideVelo;
	float minSkidPitchSlideVelo,maxSkidPitchSlideVelo;
	float minSkidGainSlideVelo,maxSkidGainSlideVelo;
	int skidSound;
	float smokeSize,smokeGravity,smokeSpeed,smokeSpread,smokeMaxLife,smokeMaxVelo;
	int smokeTexture,trackTexture;
} tSurfaceType;

typedef struct{
	int numTypes;
	tSurfaceType *types;
} tSurfaceTypeList;

//one segment of a road
typedef struct{
	tVector3 pos;    //the point the road passes through
	tVector3 normal; //a normal pointing upwards on the road surface
	int type;		 //an index specifying the type of the road segment (bridge, tunnel, etc..)
	float speedModifier;
	int unused2;
	float track;		//the racing line the ai will follow: 1 is all to the right of the road and -1 all to the left
						//any value >1 will be ignored. instead a value will be interpolated from other road points.
	float typeSeg;		//when using a road segment type which is not "symmetrical", ie. which looks different on
						//one end than on the other, typeSeg can be used to "split" the type among several segments.
						//one has to take a look at some road files to understand this properly.
} tRoadSeg;

//the road
typedef struct{
	int roadSize;		//number of road segments
	tRoadSeg road[1];	//and the road data
} tRoadData;

typedef struct{
	tVector3 v1,v2;
	tVector3 n1,n2;
	float d1,d2;
} tConvertedRoadVertex;

//tRoadSeg structures get converted into tConvertedRoadSeg structures, which contain all the vertices
//of each road segment, so these don't need to be calculated from the road segment type every time
//they are needed.
typedef struct{
	float length;			//the distance in meters from the beginning of the road (measured along the road)
	float maxExtends;		//used for clipping: the maximum distance from 
							//a road vertex from the point in the tRoadSeg's pos field.
	tVector3 trackl,trackr; //the left-most and right-most points of the road, where AI players may drive
	float speedIndex,reverseSpeedIndex;   	//tells the AI how fast to go on this road
	float turboSpeedIndex,reverseTurboSpeedIndex;   	//tells the AI how fast to go on this road
	float lateralAccel;
	float maxGrip;
	float track;			//the racing line the ai will follow: 1 is all to the right of the road and -1 all to the left
	tConvertedRoadVertex *vertices;     //a pointer to the vertex coordinates for this road segment.
} tConvertedRoadSeg;

typedef struct{
	tConvertedRoadVertex* vertexStorage;	//the buffer storing all the vertex coordinates
	tConvertedRoadSeg road[1];	//converted road segments
} tConvertedRoad;

void CompileRoadPoints(int id);
void RoadRender(tVector3 *clipPlanes);
float RoadGetYValue(tVector3 point,int *lastRoadIndex,tVector3 *groundNormal,int *surfaceType,float *bumpOut);
void RoadCenterCar(tGameEntity *carEntity);
tVector3 RoadGetNextWaypoint(tVector3 pos,int *lastRoadIndex,float *overtaking,float *speedIndex,float aheadDistance);
float RoadGetPosition(tVector3 pos,int lastSegment,float *track);
tVector3 RoadGetDir(int segment);
void RoadInitCheckPoints(tVector3 startPos);
void RoadInitCheckPoints(tVector3 startPos,tVector3 endPos);
void RoadGetWayPointData(int startRoadSeg,float dist,float *minTrack,float *maxTrack,float *minSpeed);
float RoadGetLength(tVector3 pos,int lastSegment);
void RoadInitCornerSigns();

extern tRoadTypeList *gRoadTypes;
extern tSurfaceTypeList *gSurfaceTypes;
extern int gRoadRestrictedBorders,gQuickRoadCollision;
#endif
