#include "qcommon.h"
#include "client.h"
#include "server.h"
#include "daikatana.h"

void dk_FrameNameFromServer (int modelindex, int frameindex, char *name);
void dk_FrameNameFromClient (int modelindex, int frameindex, char *name);

// Shawn:
///////////////////////////////////////////////////////////////////////////////
//
// ADDED FOR SERVER SIDE MODEL INFORMATION
//
///////////////////////////////////////////////////////////////////////////////

/*
================
dk_GetFrameName

//	FIXME!!!
//	RELIES ON THE SERVER HAVING ACCESS TO LOCAL CLIENT DATA
//	NEED TO MAKE SURE THIS DATA IS AVAILABLE IN DEDICATED SERVERS, TOO
================
*/
void dk_GetFrameName (int modelindex, int frameindex, char *framename)
{
	char	modelname[128];
/*
	if (modelindex != 39)
	{
		framename[0] = 0x00;
		return;
	}
*/
	strcpy (modelname, sv.configstrings[CS_MODELS+modelindex]);

	if (modelname [0] == 0x00)
	{
		framename[0] = 0x00;
		return;
	}
	
//	if (dedicated->value)
//		dk_FrameNameFromServer (modelindex, frameindex, name);
//	else
		re.GetFrameName (modelname, frameindex, framename);
}

///////////////////////////////////////////////////////////////////////////////
//	dk_ForceModelLoad
//
//	forces a model at a specific index to be reloaded at any time
//
//	FIXME!!!
//	RELIES ON THE SERVER HAVING ACCESS TO LOCAL CLIENT DATA
//	NEED TO MAKE SURE THIS DATA IS AVAILABLE IN DEDICATED SERVERS, TOO
///////////////////////////////////////////////////////////////////////////////

void	dk_ForceModelLoad (int modelindex, char *name)
{
	strcpy (cl.configstrings [CS_MODELS + modelindex], name);
	strcpy (sv.configstrings [CS_MODELS + modelindex], name);

	//	load model, don't load textures
	cl.model_draw [modelindex] = re.RegisterModel (cl.configstrings [CS_MODELS + modelindex], RESOURCE_RELOAD);
}

///////////////////////////////////////////////////////////////////////////////
//	dk_VertInfo
//
//	returns the transformed location of a specified vertex in a specified
//	surface for the specified frame of the specified entity, got that?
//
//	FIXME!!!
//	RELIES ON THE SERVER HAVING ACCESS TO LOCAL CLIENT DATA
//	NEED TO MAKE SURE THIS DATA IS AVAILABLE IN DEDICATED SERVERS, TOO
///////////////////////////////////////////////////////////////////////////////

int	dk_VertInfo (edict_t *ent, int surfIndex, int vertIndex, vec3_t vert)
{
	void	*mod;

	mod = cl.model_draw [ent->s.modelindex];

	if(!mod)
		return 0;

	return re.VertInfo (mod, surfIndex, ent->s.frame, vertIndex, ent->s.origin, ent->s.old_origin, ent->s.angles, vert);
}

///////////////////////////////////////////////////////////////////////////////
//	dk_SurfIndex
//
//	returns an index for the passed surface name
//
//	FIXME!!!
//	RELIES ON THE SERVER HAVING ACCESS TO LOCAL CLIENT DATA
//	NEED TO MAKE SURE THIS DATA IS AVAILABLE IN DEDICATED SERVERS, TOO
///////////////////////////////////////////////////////////////////////////////

int	dk_SurfIndex (int modelIndex, char *surfName)
{
	void	*mod;

	mod = cl.model_draw [modelIndex];

	if(!mod)
		//	so hierarchical code knows that model's haven't been loaded
		return 0;

	return	re.SurfIndex (mod, surfName);
}

///////////////////////////////////////////////////////////////////////////////
//	dk_TriVerts
//
//	returns three transformed vertices
//
//	FIXME!!!
//	RELIES ON THE SERVER HAVING ACCESS TO LOCAL CLIENT DATA
//	NEED TO MAKE SURE THIS DATA IS AVAILABLE IN DEDICATED SERVERS, TOO
//////////////////////////////////////////////////////////////////////

int	dk_TriVerts (edict_t *ent, int surfIndex, int vertIndex, vec3_t v1, vec3_t v2, vec3_t v3)
{
	void	*mod;

	mod = cl.model_draw [ent->s.modelindex];

	if(!mod)
		return 0;

	return	re.TriVerts (mod, surfIndex, ent->s.frame, vertIndex, ent->s.origin, ent->s.old_origin, ent->s.angles, v1, v2, v3);
}

///////////////////////////////////////////////////////////////////////////////
//	dk_Trace
//
///////////////////////////////////////////////////////////////////////////////

client_trace_t	dk_Trace (vec3_t origin, vec3_t mins, vec3_t maxs, vec3_t end, int passent_num, int clipmask)
{
	edict_t			*passedict;
	trace_t			trace;
	client_trace_t	ctrace;

	//	make sure we're connected to the server (ie. the server is set up)
	if (cls.state != ca_active || sv.state != ss_game)
	{
		memset (&ctrace, 0x00, sizeof (client_trace_t));
		return	ctrace;
	}

	passedict = EDICT_NUM (passent_num);

	//	Nelno:	pass a real clip mask from client
	clipmask = MASK_SOLID;

	trace = SV_Trace (origin, mins, maxs, end, passedict, clipmask);

	ctrace.allsolid = trace.allsolid;
	ctrace.startsolid = trace.startsolid;
	ctrace.fraction = trace.fraction;
	VectorCopy (trace.endpos, ctrace.endpos);
	ctrace.plane = trace.plane;
	ctrace.surface = trace.surface;
	ctrace.contents = trace.contents;
	ctrace.ent = NUM_FOR_EDICT (trace.ent);

	return	ctrace;
}