]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/main.c
Merge commit '2b85b7c20c20df677e30541c93b80725dc2277c9' into garux-merge
[xonotic/netradiant.git] / tools / quake3 / q3map2 / main.c
1 /* -------------------------------------------------------------------------------
2
3    Copyright (C) 1999-2007 id Software, Inc. and contributors.
4    For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6    This file is part of GtkRadiant.
7
8    GtkRadiant is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    GtkRadiant is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GtkRadiant; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22    -------------------------------------------------------------------------------
23
24    This code has been altered significantly from its original form, to support
25    several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27    ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define MAIN_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38 #include <glib.h>
39
40 /*
41    Random()
42    returns a pseudorandom number between 0 and 1
43  */
44
45 vec_t Random( void ){
46         return (vec_t) rand() / RAND_MAX;
47 }
48
49
50 char *Q_strncpyz( char *dst, const char *src, size_t len ) {
51         if ( len == 0 ) {
52                 abort();
53         }
54
55         strncpy( dst, src, len );
56         dst[ len - 1 ] = '\0';
57         return dst;
58 }
59
60
61 char *Q_strcat( char *dst, size_t dlen, const char *src ) {
62         size_t n = strlen( dst );
63
64         if ( n > dlen ) {
65                 abort(); /* buffer overflow */
66         }
67
68         return Q_strncpyz( dst + n, src, dlen - n );
69 }
70
71
72 char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen ) {
73         size_t n = strlen( dst );
74
75         if ( n > dlen ) {
76                 abort(); /* buffer overflow */
77         }
78
79         return Q_strncpyz( dst + n, src, MIN( slen, dlen - n ) );
80 }
81
82
83 /*
84    ExitQ3Map()
85    cleanup routine
86  */
87
88 static void ExitQ3Map( void ){
89         BSPFilesCleanup();
90         if ( mapDrawSurfs != NULL ) {
91                 free( mapDrawSurfs );
92         }
93 }
94
95
96 /*
97    ShiftBSPMain()
98    shifts a map: for testing physics with huge coordinates
99  */
100
101 int ShiftBSPMain( int argc, char **argv ){
102         int i, j;
103         float f, a;
104         vec3_t scale;
105         vec3_t vec;
106         char str[ 1024 ];
107         int uniform, axis;
108         qboolean texscale;
109         float *old_xyzst = NULL;
110         float spawn_ref = 0;
111
112
113         /* arg checking */
114         if ( argc < 3 ) {
115                 Sys_Printf( "Usage: q3map [-v] -shift [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
116                 return 0;
117         }
118
119         texscale = qfalse;
120         for ( i = 1; i < argc - 2; ++i )
121         {
122                 if ( !strcmp( argv[i], "-tex" ) ) {
123                         texscale = qtrue;
124                 }
125                 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
126                         spawn_ref = atof( argv[i + 1] );
127                         ++i;
128                 }
129                 else{
130                         break;
131                 }
132         }
133
134         /* get shift */
135         // if(argc-2 >= i) // always true
136         scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
137         if ( argc - 3 >= i ) {
138                 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
139         }
140         if ( argc - 4 >= i ) {
141                 scale[0] = atof( argv[ argc - 4 ] );
142         }
143
144         uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
145
146
147         /* do some path mangling */
148         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
149         StripExtension( source );
150         DefaultExtension( source, ".bsp" );
151
152         /* load the bsp */
153         Sys_Printf( "Loading %s\n", source );
154         LoadBSPFile( source );
155         ParseEntities();
156
157         /* note it */
158         Sys_Printf( "--- ShiftBSP ---\n" );
159         Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
160
161         /* shift entity keys */
162         for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
163         {
164                 /* shift origin */
165                 GetVectorForKey( &entities[ i ], "origin", vec );
166                 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
167                         if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
168                                 vec[2] += spawn_ref;
169                         }
170                         vec[0] += scale[0];
171                         vec[1] += scale[1];
172                         vec[2] += scale[2];
173                         if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
174                                 vec[2] -= spawn_ref;
175                         }
176                         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
177                         SetKeyValue( &entities[ i ], "origin", str );
178                 }
179
180         }
181
182         /* shift models */
183         for ( i = 0; i < numBSPModels; i++ )
184         {
185                 bspModels[ i ].mins[0] += scale[0];
186                 bspModels[ i ].mins[1] += scale[1];
187                 bspModels[ i ].mins[2] += scale[2];
188                 bspModels[ i ].maxs[0] += scale[0];
189                 bspModels[ i ].maxs[1] += scale[1];
190                 bspModels[ i ].maxs[2] += scale[2];
191         }
192
193         /* shift nodes */
194         for ( i = 0; i < numBSPNodes; i++ )
195         {
196                 bspNodes[ i ].mins[0] += scale[0];
197                 bspNodes[ i ].mins[1] += scale[1];
198                 bspNodes[ i ].mins[2] += scale[2];
199                 bspNodes[ i ].maxs[0] += scale[0];
200                 bspNodes[ i ].maxs[1] += scale[1];
201                 bspNodes[ i ].maxs[2] += scale[2];
202         }
203
204         /* shift leafs */
205         for ( i = 0; i < numBSPLeafs; i++ )
206         {
207                 bspLeafs[ i ].mins[0] += scale[0];
208                 bspLeafs[ i ].mins[1] += scale[1];
209                 bspLeafs[ i ].mins[2] += scale[2];
210                 bspLeafs[ i ].maxs[0] += scale[0];
211                 bspLeafs[ i ].maxs[1] += scale[1];
212                 bspLeafs[ i ].maxs[2] += scale[2];
213         }
214
215         /* shift drawverts */
216         for ( i = 0; i < numBSPDrawVerts; i++ )
217         {
218                 bspDrawVerts[i].xyz[0] += scale[0];
219                 bspDrawVerts[i].xyz[1] += scale[1];
220                 bspDrawVerts[i].xyz[2] += scale[2];
221         }
222
223         /* shift planes */
224
225         vec3_t point;
226
227         for ( i = 0; i < numBSPPlanes; i++ )
228         {
229                 //find point on plane
230                 for ( j=0; j<3; j++ ){
231                         if ( fabs( bspPlanes[ i ].normal[j] ) > 0.5 ){
232                                 point[j] = bspPlanes[ i ].dist / bspPlanes[ i ].normal[j];
233                                 point[(j+1)%3] = point[(j+2)%3] = 0;
234                                 break;
235                         }
236                 }
237                 //shift point
238                 for ( j=0; j<3; j++ ){
239                         point[j] += scale[j];
240                 }
241                 //calc new plane dist
242                 bspPlanes[ i ].dist = DotProduct( point, bspPlanes[ i ].normal );
243         }
244
245         /* scale gridsize */
246         /*
247         GetVectorForKey( &entities[ 0 ], "gridsize", vec );
248         if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
249                 VectorCopy( gridSize, vec );
250         }
251         vec[0] *= scale[0];
252         vec[1] *= scale[1];
253         vec[2] *= scale[2];
254         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
255         SetKeyValue( &entities[ 0 ], "gridsize", str );
256 */
257         /* inject command line parameters */
258         InjectCommandLine( argv, 0, argc - 1 );
259
260         /* write the bsp */
261         UnparseEntities();
262         StripExtension( source );
263         DefaultExtension( source, "_sh.bsp" );
264         Sys_Printf( "Writing %s\n", source );
265         WriteBSPFile( source );
266
267         /* return to sender */
268         return 0;
269 }
270
271
272 void FixDOSName( char *src ){
273         if ( src == NULL ) {
274                 return;
275         }
276
277         while ( *src )
278         {
279                 if ( *src == '\\' ) {
280                         *src = '/';
281                 }
282                 src++;
283         }
284 }
285
286 /*
287         Check if newcoming texture is unique and not excluded
288 */
289 void tex2list( char* texlist, int *texnum, char* EXtex, int *EXtexnum ){
290         int i;
291         if ( token[0] == '\0') return;
292         StripExtension( token );
293         FixDOSName( token );
294         for ( i = 0; i < *texnum; i++ ){
295                 if ( !Q_stricmp( texlist[i], token ) ) return;
296         }
297         for ( i = 0; i < *EXtexnum; i++ ){
298                 if ( !Q_stricmp( EXtex[i], token ) ) return;
299         }
300         strcpy ( texlist + (*texnum)*65, token );
301         (*texnum)++;
302         return;
303 }
304
305
306 /*
307         Check if newcoming res is unique
308 */
309 void res2list( char* data, int *num ){
310         int i;
311         if ( *( data + (*num)*65 ) == '\0') return;
312         for ( i = 0; i < *num; i++ ){
313                 if ( !Q_stricmp( data[i], data[*num] ) ) return;
314         }
315         (*num)++;
316         return;
317 }
318
319 void parseEXblock ( char* data, int *num, const char *exName ){
320         if ( !GetToken( qtrue ) || strcmp( token, "{" ) ) {
321                 Error( "ReadExclusionsFile: %s, line %d: { not found", exName, scriptline );
322         }
323         while ( 1 )
324         {
325                 if ( !GetToken( qtrue ) ) {
326                         break;
327                 }
328                 if ( !strcmp( token, "}" ) ) {
329                         break;
330                 }
331                 if ( token[0] == '{' ) {
332                         Error( "ReadExclusionsFile: %s, line %d: brace, opening twice in a row.", exName, scriptline );
333                 }
334
335                 /* add to list */
336                 strcpy( data + (*num)*65, token );
337                 (*num)++;
338         }
339         return;
340 }
341
342 char q3map2path[1024];
343 /*
344    pk3BSPMain()
345    map autopackager, works for Q3 type of shaders and ents
346  */
347
348 int pk3BSPMain( int argc, char **argv ){
349         int i, j, len;
350         qboolean dbg = qfalse, png = qfalse, packFAIL = qfalse;
351
352         /* process arguments */
353         for ( i = 1; i < ( argc - 1 ); i++ ){
354                 if ( !strcmp( argv[ i ],  "-dbg" ) ) {
355                         dbg = qtrue;
356                 }
357                 else if ( !strcmp( argv[ i ],  "-png" ) ) {
358                         png = qtrue;
359                 }
360         }
361
362         /* do some path mangling */
363         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
364         StripExtension( source );
365         DefaultExtension( source, ".bsp" );
366
367         /* load the bsp */
368         Sys_Printf( "Loading %s\n", source );
369         LoadBSPFile( source );
370         ParseEntities();
371
372
373         char packname[ 1024 ], packFailName[ 1024 ], base[ 1024 ], nameOFmap[ 1024 ], temp[ 1024 ];
374
375         /* copy map name */
376         strcpy( base, source );
377         StripExtension( base );
378
379         /* extract map name */
380         len = strlen( base ) - 1;
381         while ( len > 0 && base[ len ] != '/' && base[ len ] != '\\' )
382                 len--;
383         strcpy( nameOFmap, &base[ len + 1 ] );
384
385
386         qboolean drawsurfSHs[1024] = { qfalse };
387
388         for ( i = 0; i < numBSPDrawSurfaces; i++ ){
389                 /* can't exclude nodraw patches here (they want shaders :0!) */
390                 //if ( !( bspDrawSurfaces[i].surfaceType == 2 && bspDrawSurfaces[i].numIndexes == 0 ) ) drawsurfSHs[bspDrawSurfaces[i].shaderNum] = qtrue;
391                 drawsurfSHs[ bspDrawSurfaces[i].shaderNum ] = qtrue;
392                 //Sys_Printf( "%s\n", bspShaders[bspDrawSurfaces[i].shaderNum].shader );
393         }
394
395         int pk3ShadersN = 0;
396         char* pk3Shaders = (char *)calloc( 1024*65, sizeof( char ) );
397         int pk3SoundsN = 0;
398         char* pk3Sounds = (char *)calloc( 1024*65, sizeof( char ) );
399         int pk3ShaderfilesN = 0;
400         char* pk3Shaderfiles = (char *)calloc( 1024*65, sizeof( char ) );
401         int pk3TexturesN = 0;
402         char* pk3Textures = (char *)calloc( 1024*65, sizeof( char ) );
403         int pk3VideosN = 0;
404         char* pk3Videos = (char *)calloc( 1024*65, sizeof( char ) );
405
406
407
408         for ( i = 0; i < numBSPShaders; i++ ){
409                 if ( drawsurfSHs[i] ){
410                         strcpy( pk3Shaders + pk3ShadersN*65, bspShaders[i].shader );
411                         res2list( pk3Shaders, &pk3ShadersN );
412                         //pk3ShadersN++;
413                         //Sys_Printf( "%s\n", bspShaders[i].shader );
414                 }
415         }
416
417         /* Ent keys */
418         epair_t *ep;
419         for ( ep = entities[0].epairs; ep != NULL; ep = ep->next )
420         {
421                 if ( !Q_strncasecmp( ep->key, "vertexremapshader", 17 ) ) {
422                         sscanf( ep->value, "%*[^;] %*[;] %s", pk3Shaders + pk3ShadersN*65 );
423                         res2list( pk3Shaders, &pk3ShadersN );
424                 }
425         }
426         strcpy( pk3Sounds + pk3SoundsN*65, ValueForKey( &entities[0], "music" ) );
427         if ( *( pk3Sounds + pk3SoundsN*65 ) != '\0' ){
428                 FixDOSName( pk3Sounds + pk3SoundsN*65 );
429                 DefaultExtension( pk3Sounds + pk3SoundsN*65, ".wav" );
430                 res2list( pk3Sounds, &pk3SoundsN );
431         }
432
433         for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
434         {
435                 strcpy( pk3Sounds + pk3SoundsN*65, ValueForKey( &entities[i], "noise" ) );
436                 if ( *( pk3Sounds + pk3SoundsN*65 ) != '\0' && *( pk3Sounds + pk3SoundsN*65 ) != '*' ){
437                         FixDOSName( pk3Sounds + pk3SoundsN*65 );
438                         DefaultExtension( pk3Sounds + pk3SoundsN*65, ".wav" );
439                         res2list( pk3Sounds, &pk3SoundsN );
440                 }
441
442                 if ( !Q_stricmp( ValueForKey( &entities[i], "classname" ), "func_plat" ) ){
443                         strcpy( pk3Sounds + pk3SoundsN*65, "sound/movers/plats/pt1_strt.wav");
444                         res2list( pk3Sounds, &pk3SoundsN );
445                         strcpy( pk3Sounds + pk3SoundsN*65, "sound/movers/plats/pt1_end.wav");
446                         res2list( pk3Sounds, &pk3SoundsN );
447                 }
448                 if ( !Q_stricmp( ValueForKey( &entities[i], "classname" ), "target_push" ) ){
449                         if ( !(IntForKey( &entities[i], "spawnflags") & 1) ){
450                                 strcpy( pk3Sounds + pk3SoundsN*65, "sound/misc/windfly.wav");
451                                 res2list( pk3Sounds, &pk3SoundsN );
452                         }
453                 }
454                 strcpy( pk3Shaders + pk3ShadersN*65, ValueForKey( &entities[i], "targetShaderNewName" ) );
455                 res2list( pk3Shaders, &pk3ShadersN );
456         }
457
458         //levelshot
459         sprintf( pk3Shaders + pk3ShadersN*65, "levelshots/%s", nameOFmap );
460         res2list( pk3Shaders, &pk3ShadersN );
461
462
463         if( dbg ){
464                 Sys_Printf( "\n\tDrawsurface+ent calls....%i\n", pk3ShadersN );
465                 for ( i = 0; i < pk3ShadersN; i++ ){
466                         Sys_Printf( "%s\n", pk3Shaders + i*65 );
467                 }
468                 Sys_Printf( "\n\tSounds....%i\n", pk3SoundsN );
469                 for ( i = 0; i < pk3SoundsN; i++ ){
470                         Sys_Printf( "%s\n", pk3Sounds + i*65 );
471                 }
472         }
473
474         vfsListShaderFiles( pk3Shaderfiles, &pk3ShaderfilesN );
475
476         if( dbg ){
477                 Sys_Printf( "\n\tSchroider fileses.....%i\n", pk3ShaderfilesN );
478                 for ( i = 0; i < pk3ShaderfilesN; i++ ){
479                         Sys_Printf( "%s\n", pk3Shaderfiles + i*65 );
480                 }
481         }
482
483
484         /* load exclusions file */
485         int ExTexturesN = 0;
486         char* ExTextures = (char *)calloc( 4096*65, sizeof( char ) );
487         int ExShadersN = 0;
488         char* ExShaders = (char *)calloc( 4096*65, sizeof( char ) );
489         int ExSoundsN = 0;
490         char* ExSounds = (char *)calloc( 4096*65, sizeof( char ) );
491         int ExShaderfilesN = 0;
492         char* ExShaderfiles = (char *)calloc( 4096*65, sizeof( char ) );
493         int ExVideosN = 0;
494         char* ExVideos = (char *)calloc( 4096*65, sizeof( char ) );
495         int ExPureTexturesN = 0;
496         char* ExPureTextures = (char *)calloc( 4096*65, sizeof( char ) );
497
498         char* ExReasonShader[4096] = { NULL };
499         char* ExReasonShaderFile[4096] = { NULL };
500
501         char exName[ 1024 ];
502         byte *buffer;
503         int size;
504
505         strcpy( exName, q3map2path );
506         char *cut = strrchr( exName, '\\' );
507         char *cut2 = strrchr( exName, '/' );
508         if ( cut == NULL && cut2 == NULL ){
509                 Sys_Printf( "WARNING: Unable to load exclusions file.\n" );
510                 goto skipEXfile;
511         }
512         if ( cut2 > cut ) cut = cut2;
513         cut[1] = '\0';
514         strcat( exName, game->arg );
515         strcat( exName, ".exclude" );
516
517         Sys_Printf( "Loading %s\n", exName );
518         size = TryLoadFile( exName, (void**) &buffer );
519         if ( size <= 0 ) {
520                 Sys_Printf( "WARNING: Unable to find exclusions file %s.\n", exName );
521                 goto skipEXfile;
522         }
523
524         /* parse the file */
525         ParseFromMemory( (char *) buffer, size );
526
527         /* tokenize it */
528         while ( 1 )
529         {
530                 /* test for end of file */
531                 if ( !GetToken( qtrue ) ) {
532                         break;
533                 }
534
535                 /* blocks */
536                 if ( !Q_stricmp( token, "textures" ) ){
537                         parseEXblock ( ExTextures, &ExTexturesN, exName );
538                 }
539                 else if ( !Q_stricmp( token, "shaders" ) ){
540                         parseEXblock ( ExShaders, &ExShadersN, exName );
541                 }
542                 else if ( !Q_stricmp( token, "shaderfiles" ) ){
543                         parseEXblock ( ExShaderfiles, &ExShaderfilesN, exName );
544                 }
545                 else if ( !Q_stricmp( token, "sounds" ) ){
546                         parseEXblock ( ExSounds, &ExSoundsN, exName );
547                 }
548                 else if ( !Q_stricmp( token, "videos" ) ){
549                         parseEXblock ( ExVideos, &ExVideosN, exName );
550                 }
551                 else{
552                         Error( "ReadExclusionsFile: %s, line %d: unknown block name!\nValid ones are: textures, shaders, shaderfiles, sounds, videos.", exName, scriptline );
553                 }
554         }
555
556         /* free the buffer */
557         free( buffer );
558
559         for ( i = 0; i < ExTexturesN; i++ ){
560                 for ( j = 0; j < ExShadersN; j++ ){
561                         if ( !Q_stricmp( ExTextures + i*65, ExShaders + j*65 ) ){
562                                 break;
563                         }
564                 }
565                 if ( j == ExShadersN ){
566                         strcpy ( ExPureTextures + ExPureTexturesN*65, ExTextures + i*65 );
567                         ExPureTexturesN++;
568                 }
569         }
570
571 skipEXfile:
572
573         if( dbg ){
574                 Sys_Printf( "\n\tExTextures....%i\n", ExTexturesN );
575                 for ( i = 0; i < ExTexturesN; i++ ) Sys_Printf( "%s\n", ExTextures + i*65 );
576                 Sys_Printf( "\n\tExPureTextures....%i\n", ExPureTexturesN );
577                 for ( i = 0; i < ExPureTexturesN; i++ ) Sys_Printf( "%s\n", ExPureTextures + i*65 );
578                 Sys_Printf( "\n\tExShaders....%i\n", ExShadersN );
579                 for ( i = 0; i < ExShadersN; i++ ) Sys_Printf( "%s\n", ExShaders + i*65 );
580                 Sys_Printf( "\n\tExShaderfiles....%i\n", ExShaderfilesN );
581                 for ( i = 0; i < ExShaderfilesN; i++ ) Sys_Printf( "%s\n", ExShaderfiles + i*65 );
582                 Sys_Printf( "\n\tExSounds....%i\n", ExSoundsN );
583                 for ( i = 0; i < ExSoundsN; i++ ) Sys_Printf( "%s\n", ExSounds + i*65 );
584                 Sys_Printf( "\n\tExVideos....%i\n", ExVideosN );
585                 for ( i = 0; i < ExVideosN; i++ ) Sys_Printf( "%s\n", ExVideos + i*65 );
586         }
587
588         /* can exclude pure textures right now, shouldn't create shaders for them anyway */
589         for ( i = 0; i < pk3ShadersN ; i++ ){
590                 for ( j = 0; j < ExPureTexturesN ; j++ ){
591                         if ( !Q_stricmp( pk3Shaders + i*65, ExPureTextures + j*65 ) ){
592                                 *( pk3Shaders + i*65 ) = '\0';
593                                 break;
594                         }
595                 }
596         }
597
598         //Parse Shader Files
599          /* hack */
600         endofscript = qtrue;
601
602         for ( i = 0; i < pk3ShaderfilesN; i++ ){
603                 qboolean wantShader = qfalse, wantShaderFile = qfalse, ShaderFileExcluded = qfalse;
604                 int shader;
605                 char* reasonShader = NULL, reasonShaderFile = NULL;
606
607                 /* load the shader */
608                 sprintf( temp, "%s/%s", game->shaderPath, pk3Shaderfiles + i*65 );
609                 SilentLoadScriptFile( temp, 0 );
610                 if( dbg ) Sys_Printf( "\n\tentering %s\n", pk3Shaderfiles + i*65 );
611
612                 /* do wanna le shader file? */
613                 for ( j = 0; j < ExShaderfilesN; j++ ){
614                         if ( !Q_stricmp( ExShaderfiles + j*65, pk3Shaderfiles + i*65 ) ){
615                                 ShaderFileExcluded = qtrue;
616                                 reasonShaderFile = ExShaderfiles + j*65;
617                                 break;
618                         }
619                 }
620                 /* tokenize it */
621                 /* check if shader file has to be excluded */
622                 while ( !ShaderFileExcluded )
623                 {
624                         /* test for end of file */
625                         if ( !GetToken( qtrue ) ) {
626                                 break;
627                         }
628
629                         /* does it contain restricted shaders/textures? */
630                         for ( j = 0; j < ExShadersN; j++ ){
631                                 if ( !Q_stricmp( ExShaders + j*65, token ) ){
632                                         ShaderFileExcluded = qtrue;
633                                         reasonShader = ExShaders + j*65;
634                                         break;
635                                 }
636                         }
637                         if ( ShaderFileExcluded )
638                                 break;
639                         for ( j = 0; j < ExPureTexturesN; j++ ){
640                                 if ( !Q_stricmp( ExPureTextures + j*65, token ) ){
641                                         ShaderFileExcluded = qtrue;
642                                         reasonShader = ExPureTextures + j*65;
643                                         break;
644                                 }
645                         }
646                         if ( ShaderFileExcluded )
647                                 break;
648
649                         /* handle { } section */
650                         if ( !GetToken( qtrue ) ) {
651                                 break;
652                         }
653                         if ( strcmp( token, "{" ) ) {
654                                         Error( "ParseShaderFile: %s, line %d: { not found!\nFound instead: %s",
655                                                 temp, scriptline, token );
656                         }
657
658                         while ( 1 )
659                         {
660                                 /* get the next token */
661                                 if ( !GetToken( qtrue ) ) {
662                                         break;
663                                 }
664                                 if ( !strcmp( token, "}" ) ) {
665                                         break;
666                                 }
667                                 /* parse stage directives */
668                                 if ( !strcmp( token, "{" ) ) {
669                                         while ( 1 )
670                                         {
671                                                 if ( !GetToken( qtrue ) ) {
672                                                         break;
673                                                 }
674                                                 if ( !strcmp( token, "}" ) ) {
675                                                         break;
676                                                 }
677                                         }
678                                 }
679                         }
680                 }
681
682                 /* tokenize it again */
683                 SilentLoadScriptFile( temp, 0 );
684                 while ( 1 )
685                 {
686                         /* test for end of file */
687                         if ( !GetToken( qtrue ) ) {
688                                 break;
689                         }
690                         //dump shader names
691                         if( dbg ) Sys_Printf( "%s\n", token );
692
693                         /* do wanna le shader? */
694                         wantShader = qfalse;
695                         for ( j = 0; j < pk3ShadersN; j++ ){
696                                 if ( !Q_stricmp( pk3Shaders + j*65, token) ){
697                                         shader = j;
698                                         wantShader = qtrue;
699                                         break;
700                                 }
701                         }
702
703                         /* handle { } section */
704                         if ( !GetToken( qtrue ) ) {
705                                 break;
706                         }
707                         if ( strcmp( token, "{" ) ) {
708                                         Error( "ParseShaderFile: %s, line %d: { not found!\nFound instead: %s",
709                                                 temp, scriptline, token );
710                         }
711
712                         while ( 1 )
713                         {
714                                 /* get the next token */
715                                 if ( !GetToken( qtrue ) ) {
716                                         break;
717                                 }
718                                 if ( !strcmp( token, "}" ) ) {
719                                         break;
720                                 }
721
722
723                                 /* -----------------------------------------------------------------
724                                 shader stages (passes)
725                                 ----------------------------------------------------------------- */
726
727                                 /* parse stage directives */
728                                 if ( !strcmp( token, "{" ) ) {
729                                         while ( 1 )
730                                         {
731                                                 if ( !GetToken( qtrue ) ) {
732                                                         break;
733                                                 }
734                                                 if ( !strcmp( token, "}" ) ) {
735                                                         break;
736                                                 }
737                                                 /* skip the shader */
738                                                 if ( !wantShader ) continue;
739
740                                                 /* digest any images */
741                                                 if ( !Q_stricmp( token, "map" ) ||
742                                                         !Q_stricmp( token, "clampMap" ) ) {
743
744                                                         /* get an image */
745                                                         GetToken( qfalse );
746                                                         if ( token[ 0 ] != '*' && token[ 0 ] != '$' ) {
747                                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
748                                                         }
749                                                 }
750                                                 else if ( !Q_stricmp( token, "animMap" ) ||
751                                                         !Q_stricmp( token, "clampAnimMap" ) ) {
752                                                         GetToken( qfalse );// skip num
753                                                         while ( TokenAvailable() ){
754                                                                 GetToken( qfalse );
755                                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
756                                                         }
757                                                 }
758                                                 else if ( !Q_stricmp( token, "videoMap" ) ){
759                                                         GetToken( qfalse );
760                                                         FixDOSName( token );
761                                                         if ( strchr( token, "/" ) == NULL ){
762                                                                 sprintf( temp, "video/%s", token );
763                                                                 strcpy( token, temp );
764                                                         }
765                                                         for ( j = 0; j < pk3VideosN; j++ ){
766                                                                 if ( !Q_stricmp( pk3Videos + j*65, token ) ){
767                                                                         goto away;
768                                                                 }
769                                                         }
770                                                         for ( j = 0; j < ExVideosN; j++ ){
771                                                                 if ( !Q_stricmp( ExVideos + j*65, token ) ){
772                                                                         goto away;
773                                                                 }
774                                                         }
775                                                         strcpy ( pk3Videos + pk3VideosN*65, token );
776                                                         pk3VideosN++;
777                                                         away:
778                                                         j = 0;
779                                                 }
780                                         }
781                                 }
782                                 /* skip the shader */
783                                 else if ( !wantShader ) continue;
784
785                                 /* -----------------------------------------------------------------
786                                 surfaceparm * directives
787                                 ----------------------------------------------------------------- */
788
789                                 /* match surfaceparm */
790                                 else if ( !Q_stricmp( token, "surfaceparm" ) ) {
791                                         GetToken( qfalse );
792                                         if ( !Q_stricmp( token, "nodraw" ) ) {
793                                                 wantShader = qfalse;
794                                                 *( pk3Shaders + shader*65 ) = '\0';
795                                         }
796                                 }
797
798                                 /* skyparms <outer image> <cloud height> <inner image> */
799                                 else if ( !Q_stricmp( token, "skyParms" ) ) {
800                                         /* get image base */
801                                         GetToken( qfalse );
802
803                                         /* ignore bogus paths */
804                                         if ( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) ) {
805                                                 strcpy ( temp, token );
806                                                 sprintf( token, "%s_up", temp );
807                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
808                                                 sprintf( token, "%s_dn", temp );
809                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
810                                                 sprintf( token, "%s_lf", temp );
811                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
812                                                 sprintf( token, "%s_rt", temp );
813                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
814                                                 sprintf( token, "%s_bk", temp );
815                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
816                                                 sprintf( token, "%s_ft", temp );
817                                                 tex2list( pk3Textures, &pk3TexturesN, ExTextures, &ExTexturesN );
818                                         }
819                                         /* skip rest of line */
820                                         GetToken( qfalse );
821                                         GetToken( qfalse );
822                                 }
823                         }
824
825                         //exclude shader
826                         if ( wantShader ){
827                                 for ( j = 0; j < ExShadersN; j++ ){
828                                         if ( !Q_stricmp( ExShaders + j*65, pk3Shaders + shader*65 ) ){
829                                                 wantShader = qfalse;
830                                                 *( pk3Shaders + shader*65 ) = '\0';
831                                                 break;
832                                         }
833                                 }
834                                 if ( wantShader ){
835                                         if ( ShaderFileExcluded ){
836                                                 if ( reasonShaderFile != NULL ){
837                                                         ExReasonShaderFile[ shader ] = reasonShaderFile;
838                                                 }
839                                                 else{
840                                                         ExReasonShaderFile[ shader ] = ( char* ) calloc( 65, sizeof( char ) );
841                                                         strcpy( ExReasonShaderFile[ shader ], pk3Shaderfiles + i*65 );
842                                                 }
843                                                 ExReasonShader[ shader ] = reasonShader;
844                                         }
845                                         else{
846                                                 wantShaderFile = qtrue;
847                                                 *( pk3Shaders + shader*65 ) = '\0';
848                                         }
849                                 }
850                         }
851                 }
852                 if ( !wantShaderFile ){
853                         *( pk3Shaderfiles + i*65 ) = '\0';
854                 }
855         }
856
857
858
859 /* exclude stuff */
860 //wanted shaders from excluded .shaders
861         Sys_Printf( "\n" );
862         for ( i = 0; i < pk3ShadersN; i++ ){
863                 if ( *( pk3Shaders + i*65 ) != '\0' && ( ExReasonShader[i] != NULL || ExReasonShaderFile[i] != NULL ) ){
864                         Sys_Printf( "  !FAIL! %s\n", pk3Shaders + i*65 );
865                         packFAIL = qtrue;
866                         if ( ExReasonShader[i] != NULL ){
867                                 Sys_Printf( "     reason: is located in %s,\n     containing restricted shader %s\n", ExReasonShaderFile[i], ExReasonShader[i] );
868                         }
869                         else{
870                                 Sys_Printf( "     reason: is located in restricted %s\n", ExReasonShaderFile[i] );
871                         }
872                         *( pk3Shaders + i*65 ) = '\0';
873                 }
874         }
875 //pure textures (shader ones are done)
876         for ( i = 0; i < pk3ShadersN; i++ ){
877                 if ( *( pk3Shaders + i*65 ) != '\0' ){
878                         FixDOSName( pk3Shaders + i*65 );
879                         for ( j = 0; j < pk3TexturesN; j++ ){
880                                 if ( !Q_stricmp( pk3Shaders + i*65, pk3Textures + j*65 ) ){
881                                         *( pk3Shaders + i*65 ) = '\0';
882                                         break;
883                                 }
884                         }
885                         if ( *( pk3Shaders + i*65 ) == '\0' ) continue;
886                         for ( j = 0; j < ExTexturesN; j++ ){
887                                 if ( !Q_stricmp( pk3Shaders + i*65, ExTextures + j*65 ) ){
888                                         *( pk3Shaders + i*65 ) = '\0';
889                                         break;
890                                 }
891                         }
892                 }
893         }
894
895 //snds
896         for ( i = 0; i < pk3SoundsN; i++ ){
897                 for ( j = 0; j < ExSoundsN; j++ ){
898                         if ( !Q_stricmp( pk3Sounds + i*65, ExSounds + j*65 ) ){
899                                 *( pk3Sounds + i*65 ) = '\0';
900                                 break;
901                         }
902                 }
903         }
904
905         /* make a pack */
906         sprintf( packname, "%s/%s_autopacked.pk3", EnginePath, nameOFmap );
907         remove( packname );
908         sprintf( packFailName, "%s/%s_FAILEDpack.pk3", EnginePath, nameOFmap );
909         remove( packFailName );
910
911         Sys_Printf( "\n--- ZipZip ---\n" );
912
913         Sys_Printf( "\n\tShader referenced textures....\n" );
914
915         for ( i = 0; i < pk3TexturesN; i++ ){
916                 if ( png ){
917                         sprintf( temp, "%s.png", pk3Textures + i*65 );
918                         if ( vfsPackFile( temp, packname ) ){
919                                 Sys_Printf( "++%s\n", temp );
920                                 continue;
921                         }
922                 }
923                 sprintf( temp, "%s.tga", pk3Textures + i*65 );
924                 if ( vfsPackFile( temp, packname ) ){
925                         Sys_Printf( "++%s\n", temp );
926                         continue;
927                 }
928                 sprintf( temp, "%s.jpg", pk3Textures + i*65 );
929                 if ( vfsPackFile( temp, packname ) ){
930                         Sys_Printf( "++%s\n", temp );
931                         continue;
932                 }
933                 Sys_Printf( "  !FAIL! %s\n", pk3Textures + i*65 );
934                 packFAIL = qtrue;
935         }
936
937         Sys_Printf( "\n\tPure textures....\n" );
938
939         for ( i = 0; i < pk3ShadersN; i++ ){
940                 if ( *( pk3Shaders + i*65 ) != '\0' ){
941                         if ( png ){
942                                 sprintf( temp, "%s.png", pk3Shaders + i*65 );
943                                 if ( vfsPackFile( temp, packname ) ){
944                                         Sys_Printf( "++%s\n", temp );
945                                         continue;
946                                 }
947                         }
948                         sprintf( temp, "%s.tga", pk3Shaders + i*65 );
949                         if ( vfsPackFile( temp, packname ) ){
950                                 Sys_Printf( "++%s\n", temp );
951                                 continue;
952                         }
953                         sprintf( temp, "%s.jpg", pk3Shaders + i*65 );
954                         if ( vfsPackFile( temp, packname ) ){
955                                 Sys_Printf( "++%s\n", temp );
956                                 continue;
957                         }
958                         Sys_Printf( "  !FAIL! %s\n", pk3Shaders + i*65 );
959                         if ( i != pk3ShadersN - 1 ) packFAIL = qtrue; //levelshot typically
960                 }
961         }
962
963         Sys_Printf( "\n\tShaizers....\n" );
964
965         for ( i = 0; i < pk3ShaderfilesN; i++ ){
966                 if ( *( pk3Shaderfiles + i*65 ) != '\0' ){
967                         sprintf( temp, "%s/%s", game->shaderPath, pk3Shaderfiles + i*65 );
968                         if ( vfsPackFile( temp, packname ) ){
969                                 Sys_Printf( "++%s\n", temp );
970                                 continue;
971                         }
972                         Sys_Printf( "  !FAIL! %s\n", pk3Shaders + i*65 );
973                         packFAIL = qtrue;
974                 }
975         }
976
977         Sys_Printf( "\n\tSounds....\n" );
978
979         for ( i = 0; i < pk3SoundsN; i++ ){
980                 if ( *( pk3Sounds + i*65 ) != '\0' ){
981                         if ( vfsPackFile( pk3Sounds + i*65, packname ) ){
982                                 Sys_Printf( "++%s\n", pk3Sounds + i*65 );
983                                 continue;
984                         }
985                         Sys_Printf( "  !FAIL! %s\n", pk3Sounds + i*65 );
986                         packFAIL = qtrue;
987                 }
988         }
989
990         Sys_Printf( "\n\tVideos....\n" );
991
992         for ( i = 0; i < pk3VideosN; i++ ){
993                 if ( vfsPackFile( pk3Videos + i*65, packname ) ){
994                         Sys_Printf( "++%s\n", pk3Videos + i*65 );
995                         continue;
996                 }
997                 Sys_Printf( "  !FAIL! %s\n", pk3Videos + i*65 );
998                 packFAIL = qtrue;
999         }
1000
1001         Sys_Printf( "\n\t.bsp and stuff\n" );
1002
1003         sprintf( temp, "maps/%s.bsp", nameOFmap );
1004         if ( vfsPackFile( temp, packname ) ){
1005                         Sys_Printf( "++%s\n", temp );
1006                 }
1007         else{
1008                 Sys_Printf( "  !FAIL! %s\n", temp );
1009                 packFAIL = qtrue;
1010         }
1011
1012         sprintf( temp, "maps/%s.aas", nameOFmap );
1013         if ( vfsPackFile( temp, packname ) ){
1014                         Sys_Printf( "++%s\n", temp );
1015                 }
1016         else{
1017                 Sys_Printf( "  !FAIL! %s\n", temp );
1018         }
1019
1020         sprintf( temp, "scripts/%s.arena", nameOFmap );
1021         if ( vfsPackFile( temp, packname ) ){
1022                         Sys_Printf( "++%s\n", temp );
1023                 }
1024         else{
1025                 Sys_Printf( "  !FAIL! %s\n", temp );
1026         }
1027
1028         sprintf( temp, "scripts/%s.defi", nameOFmap );
1029         if ( vfsPackFile( temp, packname ) ){
1030                         Sys_Printf( "++%s\n", temp );
1031                 }
1032         else{
1033                 Sys_Printf( "  !FAIL! %s\n", temp );
1034         }
1035
1036         if ( !packFAIL ){
1037         Sys_Printf( "\nSaved to %s\n", packname );
1038         }
1039         else{
1040                 rename( packname, packFailName );
1041                 Sys_Printf( "\nSaved to %s\n", packFailName );
1042         }
1043         /* return to sender */
1044         return 0;
1045 }
1046
1047
1048 /*
1049    main()
1050    q3map mojo...
1051  */
1052
1053 int main( int argc, char **argv ){
1054         int i, r;
1055         double start, end;
1056         extern qboolean werror;
1057
1058
1059         /* we want consistent 'randomness' */
1060         srand( 0 );
1061
1062         /* start timer */
1063         start = I_FloatTime();
1064
1065         /* this was changed to emit version number over the network */
1066         printf( Q3MAP_VERSION "\n" );
1067
1068         /* set exit call */
1069         atexit( ExitQ3Map );
1070
1071         /* read general options first */
1072         for ( i = 1; i < argc; i++ )
1073         {
1074                 /* -help */
1075                 if ( !strcmp( argv[ i ], "-h" ) || !strcmp( argv[ i ], "--help" )
1076                         || !strcmp( argv[ i ], "-help" ) ) {
1077                         HelpMain(argv[i+1]);
1078                         return 0;
1079                 }
1080
1081                 /* -connect */
1082                 if ( !strcmp( argv[ i ], "-connect" ) ) {
1083                         argv[ i ] = NULL;
1084                         i++;
1085                         Broadcast_Setup( argv[ i ] );
1086                         argv[ i ] = NULL;
1087                 }
1088
1089                 /* verbose */
1090                 else if ( !strcmp( argv[ i ], "-v" ) ) {
1091                         if ( !verbose ) {
1092                                 verbose = qtrue;
1093                                 argv[ i ] = NULL;
1094                         }
1095                 }
1096
1097                 /* force */
1098                 else if ( !strcmp( argv[ i ], "-force" ) ) {
1099                         force = qtrue;
1100                         argv[ i ] = NULL;
1101                 }
1102
1103                 /* make all warnings into errors */
1104                 else if ( !strcmp( argv[ i ], "-werror" ) ) {
1105                         werror = qtrue;
1106                         argv[ i ] = NULL;
1107                 }
1108
1109                 /* patch subdivisions */
1110                 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
1111                         argv[ i ] = NULL;
1112                         i++;
1113                         patchSubdivisions = atoi( argv[ i ] );
1114                         argv[ i ] = NULL;
1115                         if ( patchSubdivisions <= 0 ) {
1116                                 patchSubdivisions = 1;
1117                         }
1118                 }
1119
1120                 /* threads */
1121                 else if ( !strcmp( argv[ i ], "-threads" ) ) {
1122                         argv[ i ] = NULL;
1123                         i++;
1124                         numthreads = atoi( argv[ i ] );
1125                         argv[ i ] = NULL;
1126                 }
1127
1128                 else if( !strcmp( argv[ i ], "-nocmdline" ) )
1129                 {
1130                         Sys_Printf( "noCmdLine\n" );
1131                         nocmdline = qtrue;
1132                         argv[ i ] = NULL;
1133                 }
1134
1135         }
1136
1137         /* init model library */
1138         PicoInit();
1139         PicoSetMallocFunc( safe_malloc );
1140         PicoSetFreeFunc( free );
1141         PicoSetPrintFunc( PicoPrintFunc );
1142         PicoSetLoadFileFunc( PicoLoadFileFunc );
1143         PicoSetFreeFileFunc( free );
1144
1145         /* set number of threads */
1146         ThreadSetDefault();
1147
1148         /* generate sinusoid jitter table */
1149         for ( i = 0; i < MAX_JITTERS; i++ )
1150         {
1151                 jitters[ i ] = sin( i * 139.54152147 );
1152                 //%     Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1153         }
1154
1155         /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1156            and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1157
1158         Sys_Printf( "Q3Map         - v1.0r (c) 1999 Id Software Inc.\n" );
1159         Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1160         Sys_Printf( "NetRadiant    - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1161         Sys_Printf( "%s\n", Q3MAP_MOTD );
1162         Sys_Printf( "%s\n", argv[0] );
1163
1164         strcpy( q3map2path, argv[0] );//fuer autoPack func
1165
1166         /* ydnar: new path initialization */
1167         InitPaths( &argc, argv );
1168
1169         /* set game options */
1170         if ( !patchSubdivisions ) {
1171                 patchSubdivisions = game->patchSubdivisions;
1172         }
1173
1174         /* check if we have enough options left to attempt something */
1175         if ( argc < 2 ) {
1176                 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1177         }
1178
1179         /* fixaas */
1180         if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
1181                 r = FixAASMain( argc - 1, argv + 1 );
1182         }
1183
1184         /* analyze */
1185         else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
1186                 r = AnalyzeBSPMain( argc - 1, argv + 1 );
1187         }
1188
1189         /* info */
1190         else if ( !strcmp( argv[ 1 ], "-info" ) ) {
1191                 r = BSPInfoMain( argc - 2, argv + 2 );
1192         }
1193
1194         /* vis */
1195         else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
1196                 r = VisMain( argc - 1, argv + 1 );
1197         }
1198
1199         /* light */
1200         else if ( !strcmp( argv[ 1 ], "-light" ) ) {
1201                 r = LightMain( argc - 1, argv + 1 );
1202         }
1203
1204         /* vlight */
1205         else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
1206                 Sys_FPrintf( SYS_WRN, "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1207                 argv[ 1 ] = "-fast";    /* eek a hack */
1208                 r = LightMain( argc, argv );
1209         }
1210
1211         /* QBall: export entities */
1212         else if ( !strcmp( argv[ 1 ], "-exportents" ) ) {
1213                 r = ExportEntitiesMain( argc - 1, argv + 1 );
1214         }
1215
1216         /* ydnar: lightmap export */
1217         else if ( !strcmp( argv[ 1 ], "-export" ) ) {
1218                 r = ExportLightmapsMain( argc - 1, argv + 1 );
1219         }
1220
1221         /* ydnar: lightmap import */
1222         else if ( !strcmp( argv[ 1 ], "-import" ) ) {
1223                 r = ImportLightmapsMain( argc - 1, argv + 1 );
1224         }
1225
1226         /* ydnar: bsp scaling */
1227         else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
1228                 r = ScaleBSPMain( argc - 1, argv + 1 );
1229         }
1230
1231         /* bsp shifting */
1232         else if ( !strcmp( argv[ 1 ], "-shift" ) ) {
1233                 r = ShiftBSPMain( argc - 1, argv + 1 );
1234         }
1235
1236         /* autopacking */
1237         else if ( !strcmp( argv[ 1 ], "-pk3" ) ) {
1238                 r = pk3BSPMain( argc - 1, argv + 1 );
1239         }
1240
1241         /* ydnar: bsp conversion */
1242         else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
1243                 r = ConvertBSPMain( argc - 1, argv + 1 );
1244         }
1245
1246         /* div0: minimap */
1247         else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
1248                 r = MiniMapBSPMain( argc - 1, argv + 1 );
1249         }
1250
1251         /* ydnar: otherwise create a bsp */
1252         else{
1253                 r = BSPMain( argc, argv );
1254         }
1255
1256         /* emit time */
1257         end = I_FloatTime();
1258         Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1259
1260         /* shut down connection */
1261         Broadcast_Shutdown();
1262
1263         /* return any error code */
1264         return r;
1265 }