]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/main.c
Merge commit 'a255fbd84e64e4f8bb6e6786b6f65579071742b6' 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: works correctly only with axial faces, placed in positive half of axis
99    for testing physics with huge coordinates
100  */
101
102 int ShiftBSPMain( int argc, char **argv ){
103         int i, j;
104         float f, a;
105         vec3_t scale;
106         vec3_t vec;
107         char str[ 1024 ];
108         int uniform, axis;
109         qboolean texscale;
110         float *old_xyzst = NULL;
111         float spawn_ref = 0;
112
113
114         /* arg checking */
115         if ( argc < 3 ) {
116                 Sys_Printf( "Usage: q3map [-v] -shift [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
117                 return 0;
118         }
119
120         texscale = qfalse;
121         for ( i = 1; i < argc - 2; ++i )
122         {
123                 if ( !strcmp( argv[i], "-tex" ) ) {
124                         texscale = qtrue;
125                 }
126                 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
127                         spawn_ref = atof( argv[i + 1] );
128                         ++i;
129                 }
130                 else{
131                         break;
132                 }
133         }
134
135         /* get shift */
136         // if(argc-2 >= i) // always true
137         scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
138         if ( argc - 3 >= i ) {
139                 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
140         }
141         if ( argc - 4 >= i ) {
142                 scale[0] = atof( argv[ argc - 4 ] );
143         }
144
145         uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
146
147
148         /* do some path mangling */
149         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
150         StripExtension( source );
151         DefaultExtension( source, ".bsp" );
152
153         /* load the bsp */
154         Sys_Printf( "Loading %s\n", source );
155         LoadBSPFile( source );
156         ParseEntities();
157
158         /* note it */
159         Sys_Printf( "--- ShiftBSP ---\n" );
160         Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
161
162         /* shift entity keys */
163         for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
164         {
165                 /* shift origin */
166                 GetVectorForKey( &entities[ i ], "origin", vec );
167                 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
168                         if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
169                                 vec[2] += spawn_ref;
170                         }
171                         vec[0] += scale[0];
172                         vec[1] += scale[1];
173                         vec[2] += scale[2];
174                         if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
175                                 vec[2] -= spawn_ref;
176                         }
177                         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
178                         SetKeyValue( &entities[ i ], "origin", str );
179                 }
180
181         }
182
183         /* shift models */
184         for ( i = 0; i < numBSPModels; i++ )
185         {
186                 bspModels[ i ].mins[0] += scale[0];
187                 bspModels[ i ].mins[1] += scale[1];
188                 bspModels[ i ].mins[2] += scale[2];
189                 bspModels[ i ].maxs[0] += scale[0];
190                 bspModels[ i ].maxs[1] += scale[1];
191                 bspModels[ i ].maxs[2] += scale[2];
192         }
193
194         /* shift nodes */
195         for ( i = 0; i < numBSPNodes; i++ )
196         {
197                 bspNodes[ i ].mins[0] += scale[0];
198                 bspNodes[ i ].mins[1] += scale[1];
199                 bspNodes[ i ].mins[2] += scale[2];
200                 bspNodes[ i ].maxs[0] += scale[0];
201                 bspNodes[ i ].maxs[1] += scale[1];
202                 bspNodes[ i ].maxs[2] += scale[2];
203         }
204
205         /* shift leafs */
206         for ( i = 0; i < numBSPLeafs; i++ )
207         {
208                 bspLeafs[ i ].mins[0] += scale[0];
209                 bspLeafs[ i ].mins[1] += scale[1];
210                 bspLeafs[ i ].mins[2] += scale[2];
211                 bspLeafs[ i ].maxs[0] += scale[0];
212                 bspLeafs[ i ].maxs[1] += scale[1];
213                 bspLeafs[ i ].maxs[2] += scale[2];
214         }
215
216         /* shift drawverts */
217         for ( i = 0; i < numBSPDrawVerts; i++ )
218         {
219                 bspDrawVerts[i].xyz[0] += scale[0];
220                 bspDrawVerts[i].xyz[1] += scale[1];
221                 bspDrawVerts[i].xyz[2] += scale[2];
222         }
223
224         /* shift planes */
225
226         vec3_t point;
227
228         for ( i = 0; i < numBSPPlanes; i++ )
229         {
230                 //find point on plane
231                 for ( j=0; j<3; j++ ){
232                         if ( fabs( bspPlanes[ i ].normal[j] ) > 0.5 ){
233                                 point[j] = bspPlanes[ i ].dist / bspPlanes[ i ].normal[j];
234                                 point[(j+1)%3] = point[(j+2)%3] = 0;
235                                 break;
236                         }
237                 }
238                 //shift point
239                 for ( j=0; j<3; j++ ){
240                         point[j] += scale[j];
241                 }
242                 //calc new plane dist
243                 bspPlanes[ i ].dist = DotProduct( point, bspPlanes[ i ].normal );
244         }
245
246         /* scale gridsize */
247         /*
248         GetVectorForKey( &entities[ 0 ], "gridsize", vec );
249         if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
250                 VectorCopy( gridSize, vec );
251         }
252         vec[0] *= scale[0];
253         vec[1] *= scale[1];
254         vec[2] *= scale[2];
255         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
256         SetKeyValue( &entities[ 0 ], "gridsize", str );
257 */
258         /* inject command line parameters */
259         InjectCommandLine( argv, 0, argc - 1 );
260
261         /* write the bsp */
262         UnparseEntities();
263         StripExtension( source );
264         DefaultExtension( source, "_sh.bsp" );
265         Sys_Printf( "Writing %s\n", source );
266         WriteBSPFile( source );
267
268         /* return to sender */
269         return 0;
270 }
271
272
273
274 /*
275    main()
276    q3map mojo...
277  */
278
279 int main( int argc, char **argv ){
280         int i, r;
281         double start, end;
282         extern qboolean werror;
283
284
285         /* we want consistent 'randomness' */
286         srand( 0 );
287
288         /* start timer */
289         start = I_FloatTime();
290
291         /* this was changed to emit version number over the network */
292         printf( Q3MAP_VERSION "\n" );
293
294         /* set exit call */
295         atexit( ExitQ3Map );
296
297         /* read general options first */
298         for ( i = 1; i < argc; i++ )
299         {
300                 /* -help */
301                 if ( !strcmp( argv[ i ], "-h" ) || !strcmp( argv[ i ], "--help" )
302                         || !strcmp( argv[ i ], "-help" ) ) {
303                         HelpMain(argv[i+1]);
304                         return 0;
305                 }
306
307                 /* -connect */
308                 if ( !strcmp( argv[ i ], "-connect" ) ) {
309                         argv[ i ] = NULL;
310                         i++;
311                         Broadcast_Setup( argv[ i ] );
312                         argv[ i ] = NULL;
313                 }
314
315                 /* verbose */
316                 else if ( !strcmp( argv[ i ], "-v" ) ) {
317                         if ( !verbose ) {
318                                 verbose = qtrue;
319                                 argv[ i ] = NULL;
320                         }
321                 }
322
323                 /* force */
324                 else if ( !strcmp( argv[ i ], "-force" ) ) {
325                         force = qtrue;
326                         argv[ i ] = NULL;
327                 }
328
329                 /* make all warnings into errors */
330                 else if ( !strcmp( argv[ i ], "-werror" ) ) {
331                         werror = qtrue;
332                         argv[ i ] = NULL;
333                 }
334
335                 /* patch subdivisions */
336                 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
337                         argv[ i ] = NULL;
338                         i++;
339                         patchSubdivisions = atoi( argv[ i ] );
340                         argv[ i ] = NULL;
341                         if ( patchSubdivisions <= 0 ) {
342                                 patchSubdivisions = 1;
343                         }
344                 }
345
346                 /* threads */
347                 else if ( !strcmp( argv[ i ], "-threads" ) ) {
348                         argv[ i ] = NULL;
349                         i++;
350                         numthreads = atoi( argv[ i ] );
351                         argv[ i ] = NULL;
352                 }
353                 else if( !strcmp( argv[ i ], "-nocmdline" ) )
354                 {
355                         Sys_Printf( "noCmdLine\n" );
356                         nocmdline = qtrue;
357                         argv[ i ] = NULL;
358                 }
359
360         }
361
362         /* init model library */
363         PicoInit();
364         PicoSetMallocFunc( safe_malloc );
365         PicoSetFreeFunc( free );
366         PicoSetPrintFunc( PicoPrintFunc );
367         PicoSetLoadFileFunc( PicoLoadFileFunc );
368         PicoSetFreeFileFunc( free );
369
370         /* set number of threads */
371         ThreadSetDefault();
372
373         /* generate sinusoid jitter table */
374         for ( i = 0; i < MAX_JITTERS; i++ )
375         {
376                 jitters[ i ] = sin( i * 139.54152147 );
377                 //%     Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
378         }
379
380         /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
381            and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
382
383         Sys_Printf( "Q3Map         - v1.0r (c) 1999 Id Software Inc.\n" );
384         Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
385         Sys_Printf( "NetRadiant    - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
386         Sys_Printf( "%s\n", Q3MAP_MOTD );
387
388         /* ydnar: new path initialization */
389         InitPaths( &argc, argv );
390
391         /* set game options */
392         if ( !patchSubdivisions ) {
393                 patchSubdivisions = game->patchSubdivisions;
394         }
395
396         /* check if we have enough options left to attempt something */
397         if ( argc < 2 ) {
398                 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
399         }
400
401         /* fixaas */
402         if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
403                 r = FixAASMain( argc - 1, argv + 1 );
404         }
405
406         /* analyze */
407         else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
408                 r = AnalyzeBSPMain( argc - 1, argv + 1 );
409         }
410
411         /* info */
412         else if ( !strcmp( argv[ 1 ], "-info" ) ) {
413                 r = BSPInfoMain( argc - 2, argv + 2 );
414         }
415
416         /* vis */
417         else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
418                 r = VisMain( argc - 1, argv + 1 );
419         }
420
421         /* light */
422         else if ( !strcmp( argv[ 1 ], "-light" ) ) {
423                 r = LightMain( argc - 1, argv + 1 );
424         }
425
426         /* vlight */
427         else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
428                 Sys_FPrintf( SYS_WRN, "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
429                 argv[ 1 ] = "-fast";    /* eek a hack */
430                 r = LightMain( argc, argv );
431         }
432
433         /* QBall: export entities */
434         else if ( !strcmp( argv[ 1 ], "-exportents" ) ) {
435                 r = ExportEntitiesMain( argc - 1, argv + 1 );
436         }
437
438         /* ydnar: lightmap export */
439         else if ( !strcmp( argv[ 1 ], "-export" ) ) {
440                 r = ExportLightmapsMain( argc - 1, argv + 1 );
441         }
442
443         /* ydnar: lightmap import */
444         else if ( !strcmp( argv[ 1 ], "-import" ) ) {
445                 r = ImportLightmapsMain( argc - 1, argv + 1 );
446         }
447
448         /* ydnar: bsp scaling */
449         else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
450                 r = ScaleBSPMain( argc - 1, argv + 1 );
451         }
452
453         /* bsp shifting */
454         else if ( !strcmp( argv[ 1 ], "-shift" ) ) {
455                 r = ShiftBSPMain( argc - 1, argv + 1 );
456         }
457
458         /* ydnar: bsp conversion */
459         else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
460                 r = ConvertBSPMain( argc - 1, argv + 1 );
461         }
462
463         /* div0: minimap */
464         else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
465                 r = MiniMapBSPMain( argc - 1, argv + 1 );
466         }
467
468         /* ydnar: otherwise create a bsp */
469         else{
470                 r = BSPMain( argc, argv );
471         }
472
473         /* emit time */
474         end = I_FloatTime();
475         Sys_Printf( "%9.0f seconds elapsed\n", end - start );
476
477         /* shut down connection */
478         Broadcast_Shutdown();
479
480         /* return any error code */
481         return r;
482 }