///////////////////////////////////////////////////////////////////////////////
//	dk_scoreboard.c
//
//	routines for drawing deathmatch scoreboard
//	should be moved to client-side DLLs if there is time
//
//	FIXME:	implement with real art instead of printxy
///////////////////////////////////////////////////////////////////////////////

#include	"client.h"
#include	"daikatana.h"

///////////////////////////////////////////////////////////////////////////////
//	defines
///////////////////////////////////////////////////////////////////////////////

#define	SB_MAX_CLIENTS			16

///////////////////////////////////////////////////////////////////////////////
//	typedefs
///////////////////////////////////////////////////////////////////////////////

typedef	struct
{
	short	int		client_index;		//	index into cl.clientinfo
	short	int		frags;				//	number of frags this client has
	short	int		ping;				//	ping time of this client
	short	int		time_on;			//	time this client has been on server
	short	int		shots_fired;		//	number of shots this client has fired
	short	int		shots_hit;			//	number of shots that hit a player or monster target
	short	int		num_deaths;			//	number of times this player has been killed
} playerInfo_t;

typedef	struct
{
	int		total_scores;		//	current number of scores being displayed
	int		top_score;			//	first score in the list (so clients can
								//	scroll through a long list
	int		sx, sy;				//	starting x and starting y
	int		x, y;				//	current x and current y
	int		max_visible_scores;	//	maximum number of lines that can be displayed in 
								//	current video mode
	int		char_width;			//	width of a character
	int		char_height;		//	height of a character + 2 (really height of a line)
	int		up_last_frame;		//	score was displayed last frame

	playerInfo_t	scores [SB_MAX_CLIENTS];	//	array of client scores
} scoreboard_t;

///////////////////////////////////////////////////////////////////////////////
//	globals
///////////////////////////////////////////////////////////////////////////////

scoreboard_t	g_Scoreboard;

///////////////////////////////////////////////////////////////////////////////
//	sb_InitScoreboard
//
///////////////////////////////////////////////////////////////////////////////

void	sb_InitScoreboard (void)
{
	memset (&g_Scoreboard, 0x00, sizeof (scoreboard_t));

	g_Scoreboard.char_width = 8;
	g_Scoreboard.char_height = 8 + 1;

	g_Scoreboard.sx = (viddef.width - g_Scoreboard.char_width * 52) / 2;
	g_Scoreboard.sy = (viddef.height - g_Scoreboard.char_height * 18) / 2;
	g_Scoreboard.x = g_Scoreboard.sx;
	g_Scoreboard.y = g_Scoreboard.sy;
}

///////////////////////////////////////////////////////////////////////////////
//	sb_PrintLine
//
///////////////////////////////////////////////////////////////////////////////

void	sb_PrintLine (char *fmt, ...)
{
	va_list		argptr;
	char		buffer [256];

	va_start (argptr,fmt);
	vsprintf (buffer, fmt, argptr);
	va_end (argptr);

	dk_printxy (g_Scoreboard.x, g_Scoreboard.y, buffer);

	g_Scoreboard.x = g_Scoreboard.sx;
	g_Scoreboard.y += g_Scoreboard.char_height;
}

///////////////////////////////////////////////////////////////////////////////
//	sb_ClearScreen
//
///////////////////////////////////////////////////////////////////////////////

void	sb_ClearScreen (void)
{
	dk_print_clear ();

	g_Scoreboard.sx = (viddef.width - g_Scoreboard.char_width * 52) / 2;
	g_Scoreboard.sy = (viddef.height - g_Scoreboard.char_height * 18) / 2;
	
	g_Scoreboard.x = g_Scoreboard.sx;
	g_Scoreboard.y = g_Scoreboard.sy;
}

///////////////////////////////////////////////////////////////////////////////
//	sb_DrawScoreboard
//
///////////////////////////////////////////////////////////////////////////////

#define	LAYOUT_SCOREBOARD	0x0001

void	sb_DrawScoreboard (void)
{
	int				i, j, cur_index;
	char			out [128];
	char			name [128];
	char			*cl_name;
	playerInfo_t	*score;

	if (!cl.frame.playerstate.stats[STAT_LAYOUTS] & LAYOUT_SCOREBOARD)
	{
		if (g_Scoreboard.up_last_frame)
		{
			dk_print_clear ();
			g_Scoreboard.up_last_frame = 0;
		}

		return;
	}

	sb_ClearScreen ();

	sb_PrintLine ("Name             Kills  Ping  Time Shots  Hits  Died");
	sb_PrintLine ("---------------- ----- ----- ----- ----- ----- -----");

	//	FIXME:	get real value here
	g_Scoreboard.max_visible_scores = 16;
	g_Scoreboard.top_score = 0;

	for (i = 0; i < g_Scoreboard.total_scores && i < g_Scoreboard.max_visible_scores; i++)
	{
		cur_index = i + g_Scoreboard.top_score;

		score = &g_Scoreboard.scores [cur_index];
		
		//	fill name with spaces
		memset (name, 0x20, 128);
		cl_name = cl.clientinfo [score->client_index].name;
		for (j = 0; cl_name [j] != 0x00; j++)
			name [j] = cl_name [j];
		name [16] = 0x00;

		sprintf (out, "%s %5i %5i %5i %5i %5i %5i", name, score->frags, score->ping, 
			score->time_on, score->shots_fired, score->shots_hit, score->num_deaths);

		sb_PrintLine (out);
	}

	g_Scoreboard.up_last_frame = 1;
}

///////////////////////////////////////////////////////////////////////////////
//	sb_ParseScoreboardMessage
//
//
///////////////////////////////////////////////////////////////////////////////

void	sb_ParseScoreboardMessage (void)
{
	playerInfo_t	*score;
	int				i;

	g_Scoreboard.total_scores = MSG_ReadByte (&net_message);
	score = &g_Scoreboard.scores [0];
	
	for (i = 0; i < g_Scoreboard.total_scores; i++, score++)
	{
		score->client_index	= MSG_ReadShort (&net_message);
		score->frags		= MSG_ReadShort (&net_message);
		score->ping			= MSG_ReadShort (&net_message);
		score->time_on		= MSG_ReadShort (&net_message);
		score->shots_fired	= MSG_ReadShort (&net_message);
		score->shots_hit	= MSG_ReadShort (&net_message);
		score->num_deaths	= MSG_ReadShort (&net_message);
	}
}