]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/eclass.cpp
This is fix for failure of compilation on new libpng
[xonotic/netradiant.git] / radiant / eclass.cpp
1 /*
2    Copyright (C) 1999-2007 id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    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 GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "stdafx.h"
23 #include <sys/stat.h>
24 #if defined ( __linux__ ) || defined ( __APPLE__ )
25 #include <dirent.h>
26 #endif
27 #include "assert.h"
28
29 eclass_t    *eclass = NULL;
30 eclass_t    *eclass_bad = NULL;
31 const vec3_t smallbox[2] = {{-8,-8,-8},{8,8,8}};
32 char eclass_directory[1024];
33
34 qboolean parsing_single = false;
35 eclass_t *eclass_e;
36
37 /*!
38    implementation of the EClass manager API
39  */
40 eclass_t** Get_EClass_E(){
41         return &eclass_e;
42 }
43
44 void Set_Eclass_Found( qboolean b ){
45         eclass_found = b;
46 }
47
48 qboolean Get_Parsing_Single(){
49         return parsing_single;
50 }
51
52
53 // md3 cache for misc_models
54 //eclass_t *g_md3Cache = NULL;
55
56 /*
57
58    the classname, color triple, and bounding box are parsed out of comments
59    A ? size means take the exact brush size.
60
61    / *QUAKED <classname> (0 0 0) ?
62    / *QUAKED <classname> (0 0 0) (-8 -8 -8) (8 8 8)
63
64    Flag names can follow the size description:
65
66    / *QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY
67
68  */
69
70 void CleanEntityList( eclass_t *&pList ){
71         while ( pList )
72         {
73                 eclass_t* pTemp = pList->next;
74
75                 entitymodel *model = pList->model;
76                 while ( model != NULL )
77                 {
78                         delete []model->pTriList;
79                         if ( model->strSkin ) {
80                                 g_string_free( (GString *)model->strSkin, TRUE );
81                         }
82                         model->strSkin = NULL;
83                         model = model->pNext;
84                 }
85
86                 if ( pList->modelpath ) {
87                         free( pList->modelpath );
88                         pList->modelpath = NULL;
89                 }
90                 if ( pList->skinpath ) {
91                         free( pList->skinpath );
92                         pList->skinpath = NULL;
93                 }
94
95                 free( pList->name );
96                 free( pList->comments );
97                 free( pList );
98                 pList = pTemp;
99         }
100
101         pList = NULL;
102
103 }
104
105
106 void CleanUpEntities(){
107         // NOTE: maybe some leak checks needed .. older versions of Radiant looked like they were freezing more stuff
108         CleanEntityList( eclass );
109         //CleanEntityList(g_md3Cache);
110         if ( eclass_bad ) {
111                 free( eclass_bad->name );
112                 free( eclass_bad->comments );
113                 free( eclass_bad );
114                 eclass_bad = NULL;
115         }
116 }
117
118 void EClass_InsertSortedList( eclass_t *&pList, eclass_t *e ){
119         eclass_t    *s;
120
121         if ( !pList ) {
122                 pList = e;
123                 return;
124         }
125
126
127         s = pList;
128         if ( stricmp( e->name, s->name ) < 0 ) {
129                 e->next = s;
130                 pList = e;
131                 return;
132         }
133
134         do
135         {
136                 if ( !s->next || stricmp( e->name, s->next->name ) < 0 ) {
137                         e->next = s->next;
138                         s->next = e;
139                         return;
140                 }
141                 s = s->next;
142         } while ( 1 );
143 }
144
145 /*
146    =================
147    Eclass_InsertAlphabetized
148    =================
149  */
150 void Eclass_InsertAlphabetized( eclass_t *e ){
151 #if 1
152         EClass_InsertSortedList( eclass, e );
153 #else
154         eclass_t    *s;
155
156         if ( !eclass ) {
157                 eclass = e;
158                 return;
159         }
160
161
162         s = eclass;
163         if ( stricmp( e->name, s->name ) < 0 ) {
164                 e->next = s;
165                 eclass = e;
166                 return;
167         }
168
169         do
170         {
171                 if ( !s->next || stricmp( e->name, s->next->name ) < 0 ) {
172                         e->next = s->next;
173                         s->next = e;
174                         return;
175                 }
176                 s = s->next;
177         } while ( 1 );
178 #endif
179 }
180
181 /*!
182    This looks at each eclass_t, if it has a "modelpath" set then it leaves it alone
183    if it's not set it checks to see if a file called "sprites/<eclassname>.*" exists, and
184    if it does exist then it sets the "modelpath" to "sprites/<eclassname>.spr"
185  */
186 void Eclass_CreateSpriteModelPaths(){
187         int Counts[4] = { 0, 0, 0, 0 };
188         char filename[512]; // should be big enough, ExtractFileBase doesn't take a buffer size...
189         eclass_t *e;
190
191         // get a list of all sprite/*>* files in all sprite/ directories
192         Sys_Printf( "Searching VFS for files in sprites/*.* that match entity names...\n" );
193         GSList *pFiles = vfsGetFileList( "sprites", NULL );
194         GSList *pFile;
195
196         if ( pFiles ) {
197
198                 // find an eclass without a modelpath.
199                 for ( e = eclass ; e ; e = e->next )
200                 {
201                         Counts[0]++;
202                         if ( e->modelpath ) {
203 #ifdef _DEBUG
204                                 Sys_Printf( "Ignoring sprite for entity %s (modelpath: \"%s\")\n",e->name,e->modelpath );
205 #endif
206                                 Counts[1]++;
207                                 continue; // ignore this eclass, it's already got a model
208                         }
209
210                         // TODO: remove this check when we can have sprites for non-fixed size entities.
211                         if ( !e->fixedsize ) {
212 #ifdef _DEBUG
213                                 Sys_Printf( "Ignoring sprite for non-fixed-size entity %s\n",e->name );
214 #endif
215                                 Counts[2]++;
216                                 continue; // can't have sprites for non-fixed size entities (yet!)
217                         }
218
219
220                         Sys_Printf( "Searching for sprite for fixed-size entity %s...",e->name );
221
222                         pFile = pFiles; // point to start of list
223
224                         // look for a file that has the same name, with any extension.
225                         bool Found = FALSE;
226                         while ( pFile )
227                         {
228
229                                 // strip the path/ and the .extension.
230                                 ExtractFileBase( (char *)pFile->data,filename );
231
232                                 // does the eclass name match the filename?
233                                 if ( stricmp( e->name,filename ) == 0 ) {
234                                         // yes, so generate a sprite filename using the all-encompasing .spr extension
235                                         // so that the model wrapper knows the sprite model plugin will be the model
236                                         // plugin used to render it.
237                                         CString strSpriteName;
238                                         strSpriteName.Format( "sprites/%s.spr",e->name );
239                                         e->modelpath = strdup( strSpriteName.GetBuffer() );
240                                         Sys_Printf( "Found! (\"%s\")\n",(char *)pFile->data );
241                                         Counts[3]++;
242                                         Found = TRUE;
243                                 }
244                                 pFile = pFile->next;
245                         }
246
247                         if ( !Found ) {
248                                 Sys_Printf( "not found\n" );
249                         }
250
251                 }
252
253                 vfsClearFileDirList( &pFiles );
254         }
255         Sys_Printf( "%d entities were scanned\n"
256                                 "%d entities that already had models/sprites were ignored\n"
257                                 "%d non-fixed-size entities were ignored\n"
258                                 "%d entities did not have matching sprite files\n"
259                                 "%d entities had sprite files and have been attached\n",
260                                 Counts[0],Counts[1],Counts[2],Counts[0] - Counts[3],Counts[3] );
261
262 }
263
264 void EClass_InitForFileList( GSList *pFiles, _EClassTable *pTable ){
265         GSList *pFile = pFiles;
266         while ( pFile )
267         {
268                 // for a given name, we grab the first .def in the vfs
269                 // this allows to override baseq3/scripts/entities.def for instance
270                 char relPath[PATH_MAX];
271                 strcpy( relPath, "scripts/" );
272                 strcat( relPath, (char*)pFile->data );
273                 if ( !vfsGetFullPath( relPath, 0, 0 ) ) {
274                         Sys_FPrintf( SYS_ERR, "Failed to find the full path for '%s' in the VFS\n", relPath );
275                 }
276                 else{
277                         pTable->m_pfnScanFile( vfsGetFullPath( relPath, 0, 0 ) );
278                 }
279                 pFile = pFile->next;
280         }
281 }
282
283 /*!
284    Manually create an eclass_t, for when no modules exist.
285    this replaces and centralizes the eclass_t allocation
286  */
287 eclass_t * EClass_Create( const char *name, float col1, float col2, float col3, const vec3_t *mins, const vec3_t *maxs, const char *comments ){
288         eclass_t *e;
289         char color[128];
290
291         e = (eclass_t*)malloc( sizeof( *e ) );
292         memset( e, 0, sizeof( *e ) );
293
294         e->name = strdup( name );
295
296         // grab the color, reformat as texture name
297         e->color[0] = col1;
298         e->color[1] = col2;
299         e->color[2] = col3;
300         sprintf( color, "(%f %f %f)", e->color[0], e->color[1], e->color[2] );
301         e->texdef.SetName( color );
302
303         // supplied size ?
304         if ( mins && maxs ) {
305                 // Hydra:
306                 // If we set worldspawn to be a fixed-size all the textures are
307                 // displayed as flat-shaded.  This is a KLUDGE now that we have
308                 // multiple game support as the worldspawn entity is game specific.
309                 // Note that this is only ever fixed for the user if a definition
310                 // for the worldspawn entity was not loaded, this can happen for
311                 // several reasons:
312                 // a) no entity definition plugin exists
313                 // b) no entity definition files were found
314                 // c) no entity definition file contained an entry for worldspawn.
315
316                 if ( stricmp( name,"worldspawn" ) != 0 ) {
317                         e->fixedsize = true;
318                 }
319
320                 // copy the sizes..
321                 memcpy( e->mins,mins,sizeof( vec3_t ) );
322                 memcpy( e->maxs,maxs,sizeof( vec3_t ) );
323         }
324
325         if ( comments ) {
326                 e->comments = strdup( comments );
327         }
328         else
329         {
330                 e->comments = (char*)malloc( 1 );
331                 e->comments[0] = '\0';
332         }
333
334         return e;
335 }
336
337 void Eclass_Init(){
338         GSList *pFiles;
339
340         // start by creating the default unknown eclass
341         eclass_bad = EClass_Create( "UNKNOWN_CLASS", 0, 0.5, 0,NULL,NULL,NULL );
342
343         // now scan the definitions
344         _EClassTable *pTable = &g_EClassDefTable;
345         while ( pTable )
346         {
347                 // read in all scripts/*.<extension>
348                 pFiles = vfsGetFileList( "scripts", pTable->m_pfnGetExtension() );
349                 if ( pFiles ) {
350                         GSList *pFile = pFiles;
351                         while ( pFile )
352                         {
353                                 /*!
354                                    \todo the MP/SP filtering rules need to be CLEANED UP and SANITIZED
355                                  */
356                                 // HACK
357                                 // JKII SP/MP mapping mode
358                                 if ( g_pGameDescription->mGameFile == "jk2.game" || g_pGameDescription->mGameFile == "ja.game" ) {
359                                         if ( !strcmp( ValueForKey( g_qeglobals.d_project_entity, "gamemode" ), "sp" ) ) {
360                                                 // SP mapping, ignore mp_*.def
361                                                 char *name = (char *)pFile->data;
362                                                 if ( name[0] == 'm' && name[1] == 'p' && name[2] == '_' ) {
363                                                         Sys_Printf( "Single Player mapping mode. Ignoring '%s'\n", name );
364                                                         pFile = pFile->next;
365                                                         continue;
366                                                 }
367                                         }
368                                         else
369                                         {
370                                                 // MP mapping, ignore sp_*.def
371                                                 char *name = (char *)pFile->data;
372                                                 if ( name[0] == 's' && name[1] == 'p' && name[2] == '_' ) {
373                                                         Sys_Printf( "Multiplayer mapping mode. Ignoring '%s'\n", name );
374                                                         pFile = pFile->next;
375                                                         continue;
376                                                 }
377                                         }
378                                 }
379                                 // RIANT
380                                 // STVEF SP/MP mapping mode
381                                 else if ( g_pGameDescription->mGameFile == "stvef.game" ) {
382                                         if ( !strcmp( ValueForKey( g_qeglobals.d_project_entity, "gamemode" ), "sp" ) ) {
383                                                 // SP mapping, ignore mp_*.def
384                                                 char *name = (char *)pFile->data;
385                                                 if ( name[0] == 'm' && name[1] == 'p' && name[2] == '_' ) {
386                                                         Sys_Printf( "Single Player mapping mode. Ignoring '%s'\n", name );
387                                                         pFile = pFile->next;
388                                                         continue;
389                                                 }
390                                         }
391                                         else
392                                         {
393                                                 // HM mapping, ignore sp_*.def
394                                                 char *name = (char *)pFile->data;
395                                                 if ( name[0] == 'h' && name[1] == 'm' && name[2] == '_' ) {
396                                                         Sys_Printf( "HoloMatch mapping mode. Ignoring '%s'\n", name );
397                                                         pFile = pFile->next;
398                                                         continue;
399                                                 }
400                                         }
401                                 }
402                                 // for a given name, we grab the first .def in the vfs
403                                 // this allows to override baseq3/scripts/entities.def for instance
404                                 char relPath[PATH_MAX];
405                                 strcpy( relPath, "scripts/" );
406                                 strcat( relPath, (char*)pFile->data );
407                                 char *fullpath = vfsGetFullPath( relPath, 0, 0 );
408                                 if ( !fullpath ) {
409                                         Sys_FPrintf( SYS_ERR, "Failed to find the full path for \"%s\" in the VFS\n", relPath );
410                                 }
411                                 else{
412                                         pTable->m_pfnScanFile( fullpath );
413                                 }
414                                 if ( g_pGameDescription->mEClassSingleLoad ) {
415                                         break;
416                                 }
417                                 pFile = pFile->next;
418                         }
419                         vfsClearFileDirList( &pFiles );
420                         pFiles = NULL;
421                 }
422                 else{
423                         Sys_FPrintf( SYS_ERR, "Didn't find any scripts/*.%s files to load EClass information\n", pTable->m_pfnGetExtension() );
424                 }
425
426                 // we deal with two formats max, if the other table exists, loop again
427                 if ( g_bHaveEClassExt && pTable == &g_EClassDefTable ) {
428                         pTable = &g_EClassExtTable;
429                 }
430                 else{
431                         pTable = NULL; // done, exit
432                 }
433         }
434         Eclass_CreateSpriteModelPaths();
435 }
436
437 eclass_t *Eclass_ForName( const char *name, qboolean has_brushes ){
438         eclass_t    *e;
439
440         if ( !name || *name == '\0' ) {
441                 return eclass_bad;
442         }
443
444 #ifdef _DEBUG
445         // grouping stuff, not an eclass
446         if ( strcmp( name, "group_info" ) == 0 ) {
447                 Sys_Printf( "WARNING: unexpected group_info entity in Eclass_ForName\n" );
448         }
449 #endif
450
451         if ( !name ) {
452                 return eclass_bad;
453         }
454
455         for ( e = eclass ; e ; e = e->next )
456                 if ( !strcmp( name, e->name ) ) {
457                         return e;
458                 }
459
460         // create a new class for it
461         if ( has_brushes ) {
462                 e = EClass_Create( name, 0, 0.5, 0,NULL,NULL,"Not found in source." );
463         }
464         else
465         {
466                 e = EClass_Create( name, 0, 0.5, 0,&smallbox[0],&smallbox[1],"Not found in source." );
467         }
468
469         Eclass_InsertAlphabetized( e );
470
471         return e;
472 }