Debian Codegraph @

code/ui/ui_gameinfo.c

raw
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23 //
24 // gameinfo.c
25 //
26
27 #include "ui_local.h"
28
29
30 //
31 // arena and bot info
32 //
33
34
35 int ui_numBots;
36 static char *ui_botInfos[MAX_BOTS];
37
38 static int ui_numArenas;
39 static char *ui_arenaInfos[MAX_ARENAS];
40
41 #ifndef MISSIONPACK // bk001206
42 static int ui_numSinglePlayerArenas;
43 static int ui_numSpecialSinglePlayerArenas;
44 #endif
45
46 /*
47 ===============
48 UI_ParseInfos
49 ===============
50 */
51 int UI_ParseInfos( char *buf, int max, char *infos[] ) {
52 char *token;
53 int count;
54 char key[MAX_TOKEN_CHARS];
55 char info[MAX_INFO_STRING];
56
57 count = 0;
58
59 while ( 1 ) {
60 token = COM_Parse( &buf );
61 if ( !token[0] ) {
62 break;
63 }
64 if ( strcmp( token, "{" ) ) {
65 Com_Printf( "Missing { in info file\n" );
66 break;
67 }
68
69 if ( count == max ) {
70 Com_Printf( "Max infos exceeded\n" );
71 break;
72 }
73
74 info[0] = '\0';
75 while ( 1 ) {
76 token = COM_ParseExt( &buf, qtrue );
77 if ( !token[0] ) {
78 Com_Printf( "Unexpected end of info file\n" );
79 break;
80 }
81 if ( !strcmp( token, "}" ) ) {
82 break;
83 }
84 Q_strncpyz( key, token, sizeof( key ) );
85
86 token = COM_ParseExt( &buf, qfalse );
87 if ( !token[0] ) {
88 strcpy( token, "<NULL>" );
89 }
90 Info_SetValueForKey( info, key, token );
91 }
92 //NOTE: extra space for arena number
93 infos[count] = UI_Alloc(strlen(info) + strlen("\\num\\") + strlen(va("%d", MAX_ARENAS)) + 1);
94 if (infos[count]) {
95 strcpy(infos[count], info);
96 count++;
97 }
98 }
99 return count;
100 }
101
102 /*
103 ===============
104 UI_LoadArenasFromFile
105 ===============
106 */
107 static void UI_LoadArenasFromFile( char *filename ) {
108 int len;
109 fileHandle_t f;
110 char buf[MAX_ARENAS_TEXT];
111
112 len = trap_FS_FOpenFile( filename, &f, FS_READ );
113 if ( !f ) {
114 trap_Print( va( S_COLOR_RED "file not found: %s\n", filename ) );
115 return;
116 }
117 if ( len >= MAX_ARENAS_TEXT ) {
118 trap_Print( va( S_COLOR_RED "file too large: %s is %i, max allowed is %i\n", filename, len, MAX_ARENAS_TEXT ) );
119 trap_FS_FCloseFile( f );
120 return;
121 }
122
123 trap_FS_Read( buf, len, f );
124 buf[len] = 0;
125 trap_FS_FCloseFile( f );
126
127 ui_numArenas += UI_ParseInfos( buf, MAX_ARENAS - ui_numArenas, &ui_arenaInfos[ui_numArenas] );
128 }
129
130 /*
131 ===============
132 UI_LoadArenas
133 ===============
134 */
135 void UI_LoadArenas( void ) {
136 int numdirs;
137 vmCvar_t arenasFile;
138 char filename[128];
139 char dirlist[1024];
140 char* dirptr;
141 int i, n;
142 int dirlen;
143 char *type;
144
145 ui_numArenas = 0;
146 uiInfo.mapCount = 0;
147
148 trap_Cvar_Register( &arenasFile, "g_arenasFile", "", CVAR_INIT|CVAR_ROM );
149 if( *arenasFile.string ) {
150 UI_LoadArenasFromFile(arenasFile.string);
151 }
152 else {
153 UI_LoadArenasFromFile("scripts/arenas.txt");
154 }
155
156 // get all arenas from .arena files
157 numdirs = trap_FS_GetFileList("scripts", ".arena", dirlist, 1024 );
158 dirptr = dirlist;
159 for (i = 0; i < numdirs; i++, dirptr += dirlen+1) {
160 dirlen = strlen(dirptr);
161 strcpy(filename, "scripts/");
162 strcat(filename, dirptr);
163 UI_LoadArenasFromFile(filename);
164 }
165 trap_Print( va( "%i arenas parsed\n", ui_numArenas ) );
166 if (UI_OutOfMemory()) {
167 trap_Print(S_COLOR_YELLOW"WARNING: not anough memory in pool to load all arenas\n");
168 }
169
170 for( n = 0; n < ui_numArenas; n++ ) {
171 // determine type
172
173 uiInfo.mapList[uiInfo.mapCount].cinematic = -1;
174 uiInfo.mapList[uiInfo.mapCount].mapLoadName = String_Alloc(Info_ValueForKey(ui_arenaInfos[n], "map"));
175 uiInfo.mapList[uiInfo.mapCount].mapName = String_Alloc(Info_ValueForKey(ui_arenaInfos[n], "longname"));
176 uiInfo.mapList[uiInfo.mapCount].levelShot = -1;
177 uiInfo.mapList[uiInfo.mapCount].imageName = String_Alloc(va("levelshots/%s", uiInfo.mapList[uiInfo.mapCount].mapLoadName));
178 uiInfo.mapList[uiInfo.mapCount].typeBits = 0;
179
180 type = Info_ValueForKey( ui_arenaInfos[n], "type" );
181 // if no type specified, it will be treated as "ffa"
182 if( *type ) {
183 if( strstr( type, "ffa" ) ) {
184 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_FFA);
185 }
186 if( strstr( type, "tourney" ) ) {
187 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_TOURNAMENT);
188 }
189 if( strstr( type, "ctf" ) ) {
190 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_CTF);
191 }
192 if( strstr( type, "oneflag" ) ) {
193 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_1FCTF);
194 }
195 if( strstr( type, "overload" ) ) {
196 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_OBELISK);
197 }
198 if( strstr( type, "harvester" ) ) {
199 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_HARVESTER);
200 }
201 if( strstr( type, "elimination" ) ) {
202 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_ELIMINATION);
203 }
204 if( strstr( type, "ctfelimination" ) ) {
205 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_CTF_ELIMINATION);
206 }
207 if( strstr( type, "lms" ) ) {
208 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_LMS);
209 }
210 if( strstr( type, "dd" ) ) {
211 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_DOUBLE_D);
212 }
213 } else {
214 uiInfo.mapList[uiInfo.mapCount].typeBits |= (1 << GT_FFA);
215 }
216
217 uiInfo.mapCount++;
218 if (uiInfo.mapCount >= MAX_MAPS) {
219 break;
220 }
221 }
222 }
223
224
225 /*
226 ===============
227 UI_LoadBotsFromFile
228 ===============
229 */
230 static void UI_LoadBotsFromFile( char *filename ) {
231 int len;
232 fileHandle_t f;
233 char buf[MAX_BOTS_TEXT];
234
235 len = trap_FS_FOpenFile( filename, &f, FS_READ );
236 if ( !f ) {
237 trap_Print( va( S_COLOR_RED "file not found: %s\n", filename ) );
238 return;
239 }
240 if ( len >= MAX_BOTS_TEXT ) {
241 trap_Print( va( S_COLOR_RED "file too large: %s is %i, max allowed is %i\n", filename, len, MAX_BOTS_TEXT ) );
242 trap_FS_FCloseFile( f );
243 return;
244 }
245
246 trap_FS_Read( buf, len, f );
247 buf[len] = 0;
248 trap_FS_FCloseFile( f );
249
250 COM_Compress(buf);
251
252 ui_numBots += UI_ParseInfos( buf, MAX_BOTS - ui_numBots, &ui_botInfos[ui_numBots] );
253 }
254
255 /*
256 ===============
257 UI_LoadBots
258 ===============
259 */
260 void UI_LoadBots( void ) {
261 vmCvar_t botsFile;
262 int numdirs;
263 char filename[128];
264 char dirlist[1024];
265 char* dirptr;
266 int i;
267 int dirlen;
268
269 ui_numBots = 0;
270
271 trap_Cvar_Register( &botsFile, "g_botsFile", "", CVAR_INIT|CVAR_ROM );
272 if( *botsFile.string ) {
273 UI_LoadBotsFromFile(botsFile.string);
274 }
275 else {
276 UI_LoadBotsFromFile("scripts/bots.txt");
277 }
278
279 // get all bots from .bot files
280 numdirs = trap_FS_GetFileList("scripts", ".bot", dirlist, 1024 );
281 dirptr = dirlist;
282 for (i = 0; i < numdirs; i++, dirptr += dirlen+1) {
283 dirlen = strlen(dirptr);
284 strcpy(filename, "scripts/");
285 strcat(filename, dirptr);
286 UI_LoadBotsFromFile(filename);
287 }
288 trap_Print( va( "%i bots parsed\n", ui_numBots ) );
289 }
290
291
292 /*
293 ===============
294 UI_GetBotInfoByNumber
295 ===============
296 */
297 char *UI_GetBotInfoByNumber( int num ) {
298 if( num < 0 || num >= ui_numBots ) {
299 trap_Print( va( S_COLOR_RED "Invalid bot number: %i\n", num ) );
300 return NULL;
301 }
302 return ui_botInfos[num];
303 }
304
305
306 /*
307 ===============
308 UI_GetBotInfoByName
309 ===============
310 */
311 char *UI_GetBotInfoByName( const char *name ) {
312 int n;
313 char *value;
314
315 for ( n = 0; n < ui_numBots ; n++ ) {
316 value = Info_ValueForKey( ui_botInfos[n], "name" );
317 if ( !Q_stricmp( value, name ) ) {
318 return ui_botInfos[n];
319 }
320 }
321
322 return NULL;
323 }
324
325 int UI_GetNumBots( void ) {
326 return ui_numBots;
327 }
328
329
330 char *UI_GetBotNameByNumber( int num ) {
331 char *info = UI_GetBotInfoByNumber(num);
332 if (info) {
333 return Info_ValueForKey( info, "name" );
334 }
335 return "Sarge";
336 }