]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/main.c
Merge commit '9fed37bae007bd5e53963ec67e925381609a2980' 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 scale */
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         /* scale entity keys */
163         for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
164         {
165                 /* scale 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         /* scale 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         /* scale 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         /* scale 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         if ( texscale ) {
217                 Sys_Printf( "Using texture unlocking (and probably breaking texture alignment a lot)\n" );
218                 old_xyzst = safe_malloc( sizeof( *old_xyzst ) * numBSPDrawVerts * 5 );
219                 for ( i = 0; i < numBSPDrawVerts; i++ )
220                 {
221                         old_xyzst[5 * i + 0] = bspDrawVerts[i].xyz[0];
222                         old_xyzst[5 * i + 1] = bspDrawVerts[i].xyz[1];
223                         old_xyzst[5 * i + 2] = bspDrawVerts[i].xyz[2];
224                         old_xyzst[5 * i + 3] = bspDrawVerts[i].st[0];
225                         old_xyzst[5 * i + 4] = bspDrawVerts[i].st[1];
226                 }
227         }
228 */
229         /* scale drawverts */
230         for ( i = 0; i < numBSPDrawVerts; i++ )
231         {
232                 bspDrawVerts[i].xyz[0] += scale[0];
233                 bspDrawVerts[i].xyz[1] += scale[1];
234                 bspDrawVerts[i].xyz[2] += scale[2];
235 //              bspDrawVerts[i].normal[0] /= scale[0];
236 //              bspDrawVerts[i].normal[1] /= scale[1];
237 //              bspDrawVerts[i].normal[2] /= scale[2];
238 //              VectorNormalize( bspDrawVerts[i].normal, bspDrawVerts[i].normal );
239         }
240 /*
241         if ( texscale ) {
242                 for ( i = 0; i < numBSPDrawSurfaces; i++ )
243                 {
244                         switch ( bspDrawSurfaces[i].surfaceType )
245                         {
246                         case SURFACE_FACE:
247                         case SURFACE_META:
248                                 if ( bspDrawSurfaces[i].numIndexes % 3 ) {
249                                         Error( "Not a triangulation!" );
250                                 }
251                                 for ( j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3 )
252                                 {
253                                         int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j + 1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j + 2] + bspDrawSurfaces[i].firstVert;
254                                         bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
255                                         float *oa = &old_xyzst[ia * 5], *ob = &old_xyzst[ib * 5], *oc = &old_xyzst[ic * 5];
256                                         // extrapolate:
257                                         //   a->xyz -> oa
258                                         //   b->xyz -> ob
259                                         //   c->xyz -> oc
260                                         ExtrapolateTexcoords(
261                                                 &oa[0], &oa[3],
262                                                 &ob[0], &ob[3],
263                                                 &oc[0], &oc[3],
264                                                 a->xyz, a->st,
265                                                 b->xyz, b->st,
266                                                 c->xyz, c->st );
267                                 }
268                                 break;
269                         }
270                 }
271         }
272 */
273         /* scale planes */
274
275         for ( i = 0; i < numBSPPlanes; i++ )
276         {
277                 if ( bspPlanes[ i ].dist > 0 ){
278                                 if ( bspPlanes[ i ].normal[0] ){
279                                         bspPlanes[ i ].dist += scale[0];
280                                         continue;
281                                 }
282                                 else if ( bspPlanes[ i ].normal[1] ){
283                                         bspPlanes[ i ].dist += scale[1];
284                                         continue;
285                                 }
286                                 else if ( bspPlanes[ i ].normal[2] ){
287                                         bspPlanes[ i ].dist += scale[2];
288                                         continue;
289                                 }
290                 }
291                 else{
292                                 if ( bspPlanes[ i ].normal[0] ){
293                                         bspPlanes[ i ].dist -= scale[0];
294                                         continue;
295                                 }
296                                 else if ( bspPlanes[ i ].normal[1] ){
297                                         bspPlanes[ i ].dist -= scale[1];
298                                         continue;
299                                 }
300                                 else if ( bspPlanes[ i ].normal[2] ){
301                                         bspPlanes[ i ].dist -= scale[2];
302                                         continue;
303                                 }
304                 }
305         }
306
307
308 /*      if ( uniform ) {
309                 for ( i = 0; i < numBSPPlanes; i++ )
310                 {
311                         bspPlanes[ i ].dist += scale[0];
312                 }
313         }
314         else
315         {
316                 for ( i = 0; i < numBSPPlanes; i++ )
317                 {
318 //                      bspPlanes[ i ].normal[0] /= scale[0];
319 //                      bspPlanes[ i ].normal[1] /= scale[1];
320 //                      bspPlanes[ i ].normal[2] /= scale[2];
321                         f = 1 / VectorLength( bspPlanes[i].normal );
322                         VectorScale( bspPlanes[i].normal, f, bspPlanes[i].normal );
323                         bspPlanes[ i ].dist *= f;
324                 }
325         }*/
326
327         /* scale gridsize */
328         /*
329         GetVectorForKey( &entities[ 0 ], "gridsize", vec );
330         if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
331                 VectorCopy( gridSize, vec );
332         }
333         vec[0] *= scale[0];
334         vec[1] *= scale[1];
335         vec[2] *= scale[2];
336         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
337         SetKeyValue( &entities[ 0 ], "gridsize", str );
338 */
339         /* inject command line parameters */
340         InjectCommandLine( argv, 0, argc - 1 );
341
342         /* write the bsp */
343         UnparseEntities();
344         StripExtension( source );
345         DefaultExtension( source, "_sh.bsp" );
346         Sys_Printf( "Writing %s\n", source );
347         WriteBSPFile( source );
348
349         /* return to sender */
350         return 0;
351 }
352
353
354
355 /*
356    main()
357    q3map mojo...
358  */
359
360 int main( int argc, char **argv ){
361         int i, r;
362         double start, end;
363         extern qboolean werror;
364
365
366         /* we want consistent 'randomness' */
367         srand( 0 );
368
369         /* start timer */
370         start = I_FloatTime();
371
372         /* this was changed to emit version number over the network */
373         printf( Q3MAP_VERSION "\n" );
374
375         /* set exit call */
376         atexit( ExitQ3Map );
377
378         /* read general options first */
379         for ( i = 1; i < argc; i++ )
380         {
381                 /* -help */
382                 if ( !strcmp( argv[ i ], "-h" ) || !strcmp( argv[ i ], "--help" )
383                         || !strcmp( argv[ i ], "-help" ) ) {
384                         HelpMain(argv[i+1]);
385                         return 0;
386                 }
387
388                 /* -connect */
389                 if ( !strcmp( argv[ i ], "-connect" ) ) {
390                         argv[ i ] = NULL;
391                         i++;
392                         Broadcast_Setup( argv[ i ] );
393                         argv[ i ] = NULL;
394                 }
395
396                 /* verbose */
397                 else if ( !strcmp( argv[ i ], "-v" ) ) {
398                         if ( !verbose ) {
399                                 verbose = qtrue;
400                                 argv[ i ] = NULL;
401                         }
402                 }
403
404                 /* force */
405                 else if ( !strcmp( argv[ i ], "-force" ) ) {
406                         force = qtrue;
407                         argv[ i ] = NULL;
408                 }
409
410                 /* make all warnings into errors */
411                 else if ( !strcmp( argv[ i ], "-werror" ) ) {
412                         werror = qtrue;
413                         argv[ i ] = NULL;
414                 }
415
416                 /* patch subdivisions */
417                 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
418                         argv[ i ] = NULL;
419                         i++;
420                         patchSubdivisions = atoi( argv[ i ] );
421                         argv[ i ] = NULL;
422                         if ( patchSubdivisions <= 0 ) {
423                                 patchSubdivisions = 1;
424                         }
425                 }
426
427                 /* threads */
428                 else if ( !strcmp( argv[ i ], "-threads" ) ) {
429                         argv[ i ] = NULL;
430                         i++;
431                         numthreads = atoi( argv[ i ] );
432                         argv[ i ] = NULL;
433                 }
434                 else if( !strcmp( argv[ i ], "-nocmdline" ) )
435                 {
436                         Sys_Printf( "noCmdLine\n" );
437                         nocmdline = qtrue;
438                         argv[ i ] = NULL;
439                 }
440
441         }
442
443         /* init model library */
444         PicoInit();
445         PicoSetMallocFunc( safe_malloc );
446         PicoSetFreeFunc( free );
447         PicoSetPrintFunc( PicoPrintFunc );
448         PicoSetLoadFileFunc( PicoLoadFileFunc );
449         PicoSetFreeFileFunc( free );
450
451         /* set number of threads */
452         ThreadSetDefault();
453
454         /* generate sinusoid jitter table */
455         for ( i = 0; i < MAX_JITTERS; i++ )
456         {
457                 jitters[ i ] = sin( i * 139.54152147 );
458                 //%     Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
459         }
460
461         /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
462            and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
463
464         Sys_Printf( "Q3Map         - v1.0r (c) 1999 Id Software Inc.\n" );
465         Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
466         Sys_Printf( "NetRadiant    - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
467         Sys_Printf( "%s\n", Q3MAP_MOTD );
468
469         /* ydnar: new path initialization */
470         InitPaths( &argc, argv );
471
472         /* set game options */
473         if ( !patchSubdivisions ) {
474                 patchSubdivisions = game->patchSubdivisions;
475         }
476
477         /* check if we have enough options left to attempt something */
478         if ( argc < 2 ) {
479                 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
480         }
481
482         /* fixaas */
483         if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
484                 r = FixAASMain( argc - 1, argv + 1 );
485         }
486
487         /* analyze */
488         else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
489                 r = AnalyzeBSPMain( argc - 1, argv + 1 );
490         }
491
492         /* info */
493         else if ( !strcmp( argv[ 1 ], "-info" ) ) {
494                 r = BSPInfoMain( argc - 2, argv + 2 );
495         }
496
497         /* vis */
498         else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
499                 r = VisMain( argc - 1, argv + 1 );
500         }
501
502         /* light */
503         else if ( !strcmp( argv[ 1 ], "-light" ) ) {
504                 r = LightMain( argc - 1, argv + 1 );
505         }
506
507         /* vlight */
508         else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
509                 Sys_FPrintf( SYS_WRN, "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
510                 argv[ 1 ] = "-fast";    /* eek a hack */
511                 r = LightMain( argc, argv );
512         }
513
514         /* QBall: export entities */
515         else if ( !strcmp( argv[ 1 ], "-exportents" ) ) {
516                 r = ExportEntitiesMain( argc - 1, argv + 1 );
517         }
518
519         /* ydnar: lightmap export */
520         else if ( !strcmp( argv[ 1 ], "-export" ) ) {
521                 r = ExportLightmapsMain( argc - 1, argv + 1 );
522         }
523
524         /* ydnar: lightmap import */
525         else if ( !strcmp( argv[ 1 ], "-import" ) ) {
526                 r = ImportLightmapsMain( argc - 1, argv + 1 );
527         }
528
529         /* ydnar: bsp scaling */
530         else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
531                 r = ScaleBSPMain( argc - 1, argv + 1 );
532         }
533
534         /* bsp shifting */
535         else if ( !strcmp( argv[ 1 ], "-shift" ) ) {
536                 r = ShiftBSPMain( argc - 1, argv + 1 );
537         }
538
539         /* ydnar: bsp conversion */
540         else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
541                 r = ConvertBSPMain( argc - 1, argv + 1 );
542         }
543
544         /* div0: minimap */
545         else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
546                 r = MiniMapBSPMain( argc - 1, argv + 1 );
547         }
548
549         /* ydnar: otherwise create a bsp */
550         else{
551                 r = BSPMain( argc, argv );
552         }
553
554         /* emit time */
555         end = I_FloatTime();
556         Sys_Printf( "%9.0f seconds elapsed\n", end - start );
557
558         /* shut down connection */
559         Broadcast_Shutdown();
560
561         /* return any error code */
562         return r;
563 }