-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathg_skins.c
257 lines (211 loc) · 5.21 KB
/
g_skins.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "g_local.h"
#include "g_skins.h"
#include "g_ctffunc.h"
#define SKIN_RED 0
#define SKIN_BLUE 1
#define SKIN_MAXCOLOR 2
#define SKIN_MODELNAME 0
#define SKIN_SKINNAME 1
#define SKIN_MAXNAME 2
#define SKIN_MAXSKINS 256
qboolean skinlistinuse = false;
char *skinlist[SKIN_MAXCOLOR][SKIN_MAXSKINS][SKIN_MAXNAME];
char* CopyString(char* in)
{
char* out;
out = gi.TagMalloc((int)strlen(in) + 1, TAG_GAME);
strcpy(out, in);
return out;
}
void SkinsReadFile()
{
FILE *fp;
char name[MAX_INFO_STRING], *newb = NULL;
size_t size;
char *tempbuf;
char line[MAX_INFO_STRING] = { 0 };
int colorindex;
char skin[MAX_INFO_STRING] = { 0 };
char model[MAX_INFO_STRING] = { 0 };
int red, blue;
int count, ch;
strcpy(name, gamedir->string);
strcat(name, "/");
strcat(name, skin_file->string);
// Open the skins file
fp = fopen (name, "rt");
if (!fp)
return;
// Prescan to get size we need.
for (count = 0; (ch = fgetc(fp)) != EOF; count++);
fseek(fp, 0, SEEK_SET); //rewind
// Read the whole skins file and place it in a spot of memory
tempbuf = (char *) gi.TagMalloc(count + 2, TAG_GAME);
if (tempbuf)
{
size = fread(tempbuf, 1, count, fp);
fclose(fp);
if (size)
{
newb = tempbuf;
newb[size] = 0;
}
else
{
if (size >= (size_t)count + 1)
gi.dprintf("Error: skins file truncated.\n");
else
gi.dprintf("Error: skins file empty.\n");
return;
}
}
else
{
gi.dprintf("Error: unable to allocate memory for entities.\n");
return;
}
// Parse our input
// One list of red skins, one of blue
// Each list contains model and skin name
red = blue = 0;
skinlist[SKIN_RED][red][SKIN_MODELNAME] = NULL;
skinlist[SKIN_RED][red][SKIN_SKINNAME] = NULL;
skinlist[SKIN_BLUE][blue][SKIN_MODELNAME] = NULL;
skinlist[SKIN_BLUE][blue][SKIN_SKINNAME] = NULL;
if (!newb)
return; // No valid file
skinlistinuse = true;
colorindex = SKIN_RED;
while (tempbuf - newb < (ptrdiff_t)size && sscanf(tempbuf,"%[^\n]", line))
{
tempbuf += strlen(line);
// Step over newline
while (*tempbuf == '\n')
tempbuf++;
// Ignore lines with # or / or ;
if (line[0] == '#' || line[0] == '/' || line[0] == ';')
continue;
// Determine which skinset we are dealing with
if (!strcmp(line, "[red]"))
colorindex = SKIN_RED;
if (!strcmp(line, "[blue]"))
colorindex = SKIN_BLUE;
// If it is a skin, add it to the list
if (strlen(line) && sscanf(line, "%[^/]/%s", model, skin) == 2)
{
// Add to the red list?
if (colorindex == SKIN_RED)
{
skinlist[SKIN_RED][red][SKIN_MODELNAME] = CopyString(model);
skinlist[SKIN_RED][red][SKIN_SKINNAME] = CopyString(skin);
red++;
skinlist[SKIN_RED][red][SKIN_MODELNAME] = NULL;
skinlist[SKIN_RED][red][SKIN_SKINNAME] = NULL;
}
else if (colorindex == SKIN_BLUE)
{
skinlist[SKIN_BLUE][blue][SKIN_MODELNAME] = CopyString(model);
skinlist[SKIN_BLUE][blue][SKIN_SKINNAME] = CopyString(skin);
blue++;
skinlist[SKIN_BLUE][blue][SKIN_MODELNAME] = NULL;
skinlist[SKIN_BLUE][blue][SKIN_SKINNAME] = NULL;
}
}
}
// Free the memory
gi.TagFree(newb);
if (skin_debug->value)
{
red = 0;
gi.dprintf("[red]\n");
while (skinlist[SKIN_RED][red][SKIN_MODELNAME])
{
gi.dprintf("MODEL: [%s] SKIN: [%s]\n",
skinlist[SKIN_RED][red][SKIN_MODELNAME],
skinlist[SKIN_RED][red][SKIN_SKINNAME]);
red++;
}
red = 0;
gi.dprintf("\n[blue]\n");
while (skinlist[SKIN_BLUE][red][SKIN_MODELNAME])
{
gi.dprintf("MODEL: [%s] SKIN: [%s]\n",
skinlist[SKIN_BLUE][red][SKIN_MODELNAME],
skinlist[SKIN_BLUE][red][SKIN_SKINNAME]);
red++;
}
}
}
qboolean
SkinValid(edict_t *ent, char *input)
{
int teamnum, i;
qboolean valid = false;
char skin[MAX_INFO_STRING] = { 0 };
char model[MAX_INFO_STRING] = { 0 };
if (!ent->client)
return false;
if (!input)
return false;
// Parse our input skin and model
if (sscanf(input, "%[^/]/%s", model, skin) != 2)
return false; // Doesn't contain skin and model
if (ent->client->ctf.teamnum == CTF_TEAM_RED)
teamnum = SKIN_RED;
else
teamnum = SKIN_BLUE;
i = 0;
while(skinlist[teamnum][i][SKIN_MODELNAME])
{
if (!strcmp(skinlist[teamnum][i][SKIN_SKINNAME], skin) &&
!strcmp(skinlist[teamnum][i][SKIN_MODELNAME], model))
{
valid = true;
break;
}
i++;
}
return valid;
}
qboolean SkinListInUse()
{
return skinlistinuse;
}
char *SkinRandom(edict_t *ent)
{
int teamnum,i;
static char skin[MAX_INFO_STRING]; // Returned and used as a temp
if (ent->client->ctf.teamnum == CTF_TEAM_RED)
teamnum = SKIN_RED;
else
teamnum = SKIN_BLUE;
i = 0;
while (skinlist[teamnum][i][SKIN_MODELNAME])
{
i++;
}
i = rand() % i;
sprintf(skin, "%s/%s", skinlist[teamnum][i][SKIN_MODELNAME],
skinlist[teamnum][i][SKIN_SKINNAME]);
return skin;
}
char **SkinGetList(edict_t *ent)
{
int teamnum, i;
static char *skins[SKIN_MAXSKINS]; // Returned and used as a temp
static char storage[SKIN_MAXSKINS][30];
if (ent->client->ctf.teamnum == CTF_TEAM_RED)
teamnum = SKIN_RED;
else
teamnum = SKIN_BLUE;
i = 0;
while (skinlist[teamnum][i][SKIN_MODELNAME])
{
sprintf(storage[i], "%s/%s", skinlist[teamnum][i][SKIN_MODELNAME],
skinlist[teamnum][i][SKIN_SKINNAME]);
skins[i] = storage[i];
i++;
}
skins[i] = NULL;
return skins;
}