newtree/source/skin.c

355 lines
7.4 KiB
C
Raw Normal View History

/*
skin.c
(description)
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
$Id$
*/
2000-05-10 11:29:38 +00:00
2000-05-17 10:03:19 +00:00
#ifdef HAVE_CONFIG_H
# include "config.h"
2000-05-17 10:03:19 +00:00
#endif
2000-12-30 02:16:36 +00:00
#ifdef HAVE_STRING_H
2001-02-03 07:39:45 +00:00
# include <string.h>
2000-12-30 02:16:36 +00:00
#endif
#ifdef HAVE_STRINGS_H
2001-02-03 07:39:45 +00:00
# include <strings.h>
2000-12-30 02:16:36 +00:00
#endif
2000-12-30 02:16:36 +00:00
#include "cl_parse.h"
#include "client.h"
#include "console.h"
2000-12-30 02:16:36 +00:00
#include "cmd.h"
2001-01-17 08:43:26 +00:00
#include "host.h"
#include "msg.h"
#include "pcx.h"
2000-12-01 08:59:44 +00:00
#include "qendian.h"
#include "screen.h"
#include "skin.h"
2000-12-30 02:16:36 +00:00
#include "sys.h"
#include "texture.h"
2000-12-30 02:16:36 +00:00
#include "va.h"
cvar_t *baseskin;
cvar_t *noskins;
2001-01-17 07:28:34 +00:00
cvar_t *skin;
cvar_t *topcolor;
cvar_t *bottomcolor;
char allskins[128];
2000-05-10 11:29:38 +00:00
skin_t skin_cache[MAX_CACHED_SKINS];
int numskins;
2000-05-10 11:29:38 +00:00
/*
Skin_Find
2000-05-10 11:29:38 +00:00
Determines the best skin for the given scoreboard
slot, and sets scoreboard->skin
2000-05-10 11:29:38 +00:00
*/
void
Skin_Find (player_info_t *sc)
2000-05-10 11:29:38 +00:00
{
skin_t *skin;
int i;
char name[128], *s;
2000-05-10 11:29:38 +00:00
if (allskins[0])
strcpy (name, allskins);
else {
2000-05-10 11:29:38 +00:00
s = Info_ValueForKey (sc->userinfo, "skin");
if (s && s[0])
strcpy (name, s);
else
strcpy (name, baseskin->string);
2000-05-10 11:29:38 +00:00
}
if (strstr (name, "..") || *name == '.')
strcpy (name, "base");
COM_StripExtension (name, name);
for (i = 0; i < numskins; i++) {
if (!strcmp (name, skin_cache[i].name)) {
sc->skin = &skin_cache[i];
2000-05-10 11:29:38 +00:00
Skin_Cache (sc->skin);
return;
}
}
if (numskins == MAX_CACHED_SKINS) { // ran out of spots, so flush
// everything
2000-05-10 11:29:38 +00:00
Skin_Skins_f ();
return;
}
skin = &skin_cache[numskins];
2000-05-10 11:29:38 +00:00
sc->skin = skin;
numskins++;
memset (skin, 0, sizeof (*skin));
strncpy (skin->name, name, sizeof (skin->name) - 1);
2000-05-10 11:29:38 +00:00
}
/*
Skin_Cache
2000-05-10 11:29:38 +00:00
Returns a pointer to the skin bitmap, or NULL to use the default
2000-05-10 11:29:38 +00:00
*/
tex_t *
Skin_Cache (skin_t *skin)
2000-05-10 11:29:38 +00:00
{
char name[1024];
tex_t *out;
QFile *file;
tex_t *tex;
int pixels;
byte *ipix, *opix;
int i;
2000-05-10 11:29:38 +00:00
if (cls.downloadtype == dl_skin)
return NULL; // use base until downloaded
2000-05-10 11:29:38 +00:00
if (noskins->int_val) // JACK: So NOSKINS > 1 will show
// skins, but
return NULL; // not download new ones.
2000-05-10 11:29:38 +00:00
if (skin->failedload)
return NULL;
out = Cache_Check (&skin->cache);
if (out)
return out;
2000-12-28 06:58:56 +00:00
// load the pic from disk
snprintf (name, sizeof (name), "skins/%s.pcx", skin->name);
COM_FOpenFile (name, &file);
if (!file) {
2000-05-10 11:29:38 +00:00
Con_Printf ("Couldn't load skin %s\n", name);
snprintf (name, sizeof (name), "skins/%s.pcx", baseskin->string);
COM_FOpenFile (name, &file);
if (!file) {
2000-05-10 11:29:38 +00:00
skin->failedload = true;
return NULL;
}
}
tex = LoadPCX (file, 0);
Qclose (file);
2000-12-28 06:58:56 +00:00
if (!tex || tex->width > 320 || tex->height > 200) {
2000-05-10 11:29:38 +00:00
skin->failedload = true;
Con_Printf ("Bad skin %s\n", name);
return NULL;
}
pixels = 320 * 200;
out = Cache_Alloc (&skin->cache, sizeof (tex_t) + pixels, skin->name);
2000-05-10 11:29:38 +00:00
if (!out)
Sys_Error ("Skin_Cache: couldn't allocate");
opix = out->data;
out->width = 320;
out->height = 200;
2001-01-24 00:02:43 +00:00
out->palette = tex->palette; //FIXME assumes 0 or host_basepal
memset (opix, 0, pixels);
for (i = 0, ipix = tex->data; i < tex->height;
i++, opix += 320, ipix += tex->width)
memcpy (opix, ipix, tex->width);
2000-05-10 11:29:38 +00:00
Skin_Process (skin, out);
2001-01-19 04:00:02 +00:00
2000-05-10 11:29:38 +00:00
skin->failedload = false;
return out;
}
/*
Skin_NextDownload
2000-05-10 11:29:38 +00:00
*/
void
Skin_NextDownload (void)
2000-05-10 11:29:38 +00:00
{
player_info_t *sc;
int i;
2000-05-10 11:29:38 +00:00
if (cls.downloadnumber == 0) {
2000-05-10 11:29:38 +00:00
Con_Printf ("Checking skins...\n");
SCR_UpdateScreen ();
}
2000-05-10 11:29:38 +00:00
cls.downloadtype = dl_skin;
for (; cls.downloadnumber != MAX_CLIENTS; cls.downloadnumber++) {
2000-05-10 11:29:38 +00:00
sc = &cl.players[cls.downloadnumber];
if (!sc->name[0])
continue;
Skin_Find (sc);
if (noskins->int_val)
2000-05-10 11:29:38 +00:00
continue;
if (!CL_CheckOrDownloadFile (va ("skins/%s.pcx", sc->skin->name)))
return; // started a download
2000-05-10 11:29:38 +00:00
}
cls.downloadtype = dl_none;
// now load them in for real
for (i = 0; i < MAX_CLIENTS; i++) {
2000-05-10 11:29:38 +00:00
sc = &cl.players[i];
if (!sc->name[0])
continue;
Skin_Find (sc);
Skin_Cache (sc->skin);
sc->skin = NULL;
2000-05-10 11:29:38 +00:00
}
if (cls.state != ca_active) { // get next signon phase
2000-05-10 11:29:38 +00:00
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
MSG_WriteString (&cls.netchan.message, va ("begin %i", cl.servercount));
Cache_Report (); // print remaining memory
2000-05-10 11:29:38 +00:00
}
}
/*
Skin_Skins_f
2000-05-10 11:29:38 +00:00
Refind all skins, downloading if needed.
2000-05-10 11:29:38 +00:00
*/
void
Skin_Skins_f (void)
2000-05-10 11:29:38 +00:00
{
int i;
2000-05-10 11:29:38 +00:00
for (i = 0; i < numskins; i++) {
if (skin_cache[i].cache.data)
Cache_Free (&skin_cache[i].cache);
2000-05-10 11:29:38 +00:00
}
numskins = 0;
cls.downloadnumber = 0;
cls.downloadtype = dl_skin;
Skin_NextDownload ();
}
/*
Skin_AllSkins_f
2000-05-10 11:29:38 +00:00
Sets all skins to one specific one
2000-05-10 11:29:38 +00:00
*/
void
Skin_AllSkins_f (void)
2000-05-10 11:29:38 +00:00
{
strcpy (allskins, Cmd_Argv (1));
2000-05-10 11:29:38 +00:00
Skin_Skins_f ();
}
2001-01-17 07:28:34 +00:00
void
CL_Color_f (void)
{
// just for quake compatability...
int top, bottom;
char num[16];
if (Cmd_Argc () == 1) {
Con_Printf ("\"color\" is \"%s %s\"\n",
Info_ValueForKey (cls.userinfo, "topcolor"),
Info_ValueForKey (cls.userinfo, "bottomcolor"));
Con_Printf ("color <0-13> [0-13]\n");
return;
}
if (Cmd_Argc () == 2)
top = bottom = atoi (Cmd_Argv (1));
else {
top = atoi (Cmd_Argv (1));
bottom = atoi (Cmd_Argv (2));
}
top &= 15;
if (top > 13)
top = 13;
bottom &= 15;
if (bottom > 13)
bottom = 13;
snprintf (num, sizeof (num), "%i", top);
Cvar_Set (topcolor, num);
snprintf (num, sizeof (num), "%i", bottom);
Cvar_Set (bottomcolor, num);
}
void
Skin_Init (void)
{
Cmd_AddCommand ("skins", Skin_Skins_f, "Download all skins that are currently in use");
Cmd_AddCommand ("allskins", Skin_AllSkins_f, "Download all skins that are currently in use");
Cmd_AddCommand ("color", CL_Color_f, "The pant and shirt color (color shirt pants) Note that if only shirt color is given, pants will match");
2001-01-17 08:43:26 +00:00
Skin_Init_Translation ();
2001-01-17 07:28:34 +00:00
}
void
Skin_Init_Cvars (void)
{
baseskin = Cvar_Get ("baseskin", "base", CVAR_NONE,
"default base skin name");
noskins = Cvar_Get ("noskins", "0", CVAR_NONE,
"set to 1 to not download new skins");
skin = Cvar_Get ("skin", "", CVAR_ARCHIVE | CVAR_USERINFO, "Players skin");
topcolor = Cvar_Get ("topcolor", "0", CVAR_ARCHIVE | CVAR_USERINFO,
"Players color on top");
bottomcolor = Cvar_Get ("bottomcolor", "0", CVAR_ARCHIVE | CVAR_USERINFO,
"Players color on bottom");
}
2001-01-17 08:43:26 +00:00
/*
CL_NewTranslation
*/
void
CL_NewTranslation (int slot)
{
player_info_t *player;
char s[512];
if (slot > MAX_CLIENTS)
Host_EndGame ("CL_NewTranslation: slot > MAX_CLIENTS");
player = &cl.players[slot];
if (!player->name[0])
return;
strcpy (s, Info_ValueForKey (player->userinfo, "skin"));
COM_StripExtension (s, s);
if (player->skin && !strequal (s, player->skin->name))
player->skin = NULL;
if (player->_topcolor != player->topcolor ||
player->_bottomcolor != player->bottomcolor || !player->skin) {
player->_topcolor = player->topcolor;
player->_bottomcolor = player->bottomcolor;
Skin_Set_Translate (player);
Skin_Do_Translation (player);
}
}