#include "g_local.h"
/* ____________ *\
** ŻŻŻŻŻŻŻŻŻŻŻŻ **
** Damage Skins **
** ____________ **
\* ŻŻŻŻŻŻŻŻŻŻŻŻ */
void DMGSKINSdmgskins (edict_t *ent)
{
	int	i = 0;

	while (dmgskins_skin[i][0])
	{
		if (!Q_strcasecmp(ent->client->pers.skin, dmgskins_skin[i]))
		{
			Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "%s", dmgskins_pain[i]);
			ent->client->pers.dmgstate = true;
		}
		i++;
	}
}
char *DMGSKINSgender (edict_t *ent, char *userinfo)
{
	char *s;

	s = Info_ValueForKey(userinfo, "gender");
	if (!Q_strcasecmp(s, "male"))
		return "male";
	else if (!Q_strcasecmp(s, "female"))
		return "female";
	else
		return "cyborg";
}
void DMGSKINSg_spawn_c (void)
{
	cvar_t	*basedir = gi.cvar("basedir", ".", CVAR_SERVERINFO);
	cvar_t	*gamedir = gi.cvar("gamedir", "baseq2", CVAR_SERVERINFO);
	FILE	*file = NULL;
	char	dmgskins_lst[MAX_QPATH], skin[MAX_OSPATH], *pain;
	int	i = 0;

	dmgskins = gi.cvar("dmgskins", "25", CVAR_SERVERINFO);
#ifdef _WIN32
	Com_sprintf(dmgskins_lst, sizeof(dmgskins_lst), "%s\\%s\\dmgskins.lst", basedir->string, gamedir->string);
#else
	Com_sprintf(dmgskins_lst, sizeof(dmgskins_lst), "%s/%s/dmgskins.lst", basedir->string, gamedir->string);
#endif
	if (file = fopen(dmgskins_lst, "r"))
	{
		while (fgets(skin, sizeof(skin), file) && i < MAX_DMGSKINS)
		{
			if (skin[0] == '/')
				continue;
			if (pain = strchr(skin, '\n'))
				*pain = 0;
			if (pain = strchr(skin, ' '))
				*pain++ = 0;
			else
				continue;
			Com_sprintf(dmgskins_skin[i], sizeof(dmgskins_skin[i]), "%s", skin);
			Com_sprintf(dmgskins_pain[i], sizeof(dmgskins_pain[i]), "%s", pain);
			i++;
		}
		fclose(file);
	}
	Com_sprintf(dmgskins_skin[i], sizeof(dmgskins_skin[i]), "");
	Com_sprintf(dmgskins_pain[i], sizeof(dmgskins_pain[i]), "");
}
char *DMGSKINSp_client_c (edict_t *ent, char *userinfo, char *s)
{
	ent->client->pers.dmgstate = false;
	if (dmgskins->value)
	{
		char skin[MAX_QPATH], *pain;

		Com_sprintf(skin, sizeof(skin), "%s", s);
		if (pain = strchr(skin, '/'))
			*pain++ = 0;
		if (!pain || !skin || !strlen(pain) || !strlen(skin) || strstr(pain, "/"))
			pain = "skin";
		if (!skin || !strlen(skin))
			strcpy(skin, DMGSKINSgender(ent, userinfo));
		Com_sprintf(ent->client->pers.skin, sizeof(ent->client->pers.skin), "%s/%s", skin, pain);
		s = Info_ValueForKey(userinfo, "pain");
		if (dmgskins->value > 0 && strlen(s))
		{
			Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "%s", s);
			ent->client->pers.dmgstate = true;
		}
		if (!ent->client->pers.dmgstate)
			DMGSKINSdmgskins(ent);
		if (!ent->client->pers.dmgstate)
		{
			int	i = 0;

			while (dmgskins_skin[i][0])
			{
				if (!Q_strcasecmp(skin, dmgskins_skin[i]))
				{
					if (!strstr(dmgskins_pain[i], "/"))
						Com_sprintf(ent->client->pers.skin, sizeof(ent->client->pers.skin), "%s/%s", skin, dmgskins_pain[i]);
					else
						Com_sprintf(ent->client->pers.skin, sizeof(ent->client->pers.skin), "%s", dmgskins_pain[i]);
				}
				i++;
			}
			DMGSKINSdmgskins(ent);
		}
		if (!ent->client->pers.dmgstate)
		{
			if (dmgskins->value > 0 && !Q_strcasecmp(pain, "skin"))
			{
				Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "pain");
				ent->client->pers.dmgstate = true;
			}
		}
		if (!ent->client->pers.dmgstate)
		{
			if (dmgskins->value > 0)
				Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "%s", ent->client->pers.skin);
			else
			{
				strcpy(skin, DMGSKINSgender(ent, userinfo));
				Com_sprintf(ent->client->pers.skin, sizeof(ent->client->pers.skin), "%s/skin", skin);
				Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "%s/pain", skin);
			}
		}
		else
		{
			pain = ent->client->pers.pain;
			if (!strstr(pain, "/"))
				Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "%s/%s", skin, pain);
			ent->client->pers.dmgstate = false;
		}
	}
	else
	{
		Com_sprintf(ent->client->pers.skin, sizeof(ent->client->pers.skin), "%s", s);
		Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "%s", s);
	}
	return ent->client->pers.skin;
}
void DMGSKINSp_view_c (edict_t *ent)
{
	if (dmgskins->value && abs(dmgskins->value) < 100)
	{
		if (ent->client->pers.dmgstate && ent->health > abs(dmgskins->value))
		{
			gi.configstring(CS_PLAYERSKINS+ent-g_edicts-1, va("%s\\%s", ent->client->pers.netname, ent->client->pers.skin));
			ent->client->pers.dmgstate = false;
		}
		else if (!ent->client->pers.dmgstate && ent->health <= abs(dmgskins->value))
		{
			gi.configstring(CS_PLAYERSKINS+ent-g_edicts-1, va("%s\\%s", ent->client->pers.netname, ent->client->pers.pain));
			ent->client->pers.dmgstate = true;
		}
	}
}
#ifdef IT_TECH
void DMGSKINSg_ctf_c (edict_t *ent, char *s)
{
	char skin[MAX_QPATH], pain[MAX_QPATH], *p;

	Com_sprintf(skin, sizeof(skin), "%s", ent->client->pers.skin);
	if (p = strrchr(skin, '/'))
		*p = 0;
	else
		strcpy(skin, DMGSKINSgender(ent, ent->client->pers.userinfo));
	Com_sprintf(pain, sizeof(pain), "%s", ent->client->pers.pain);
	if (p = strrchr(pain, '/'))
		*p = 0;
	else
		strcpy(pain, DMGSKINSgender(ent, ent->client->pers.userinfo));
	switch (ent->client->resp.ctf_team)
	{
	case CTF_TEAM1:
		Com_sprintf(ent->client->pers.skin, sizeof(ent->client->pers.skin), "%s/%s", skin, CTF_TEAM1_SKIN);
		Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "%s/%s_p", pain, CTF_TEAM1_SKIN);
		break;
	case CTF_TEAM2:
		Com_sprintf(ent->client->pers.skin, sizeof(ent->client->pers.skin), "%s/%s", skin, CTF_TEAM2_SKIN);
		Com_sprintf(ent->client->pers.pain, sizeof(ent->client->pers.pain), "%s/%s_p", pain, CTF_TEAM2_SKIN);
		break;
	default:
		s = DMGSKINSp_client_c(ent, ent->client->pers.userinfo, s);
		break;
	}
	gi.configstring(CS_PLAYERSKINS+ent-g_edicts-1, va("%s\\%s", ent->client->pers.netname, ent->client->pers.skin));
}
#endif
//Damage Skins
