]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake2/q2map/main.c
initial
[divverent/netradiant.git] / tools / quake2 / q2map / main.c
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22
23 /* marker */
24 #define MAIN_C
25
26
27
28 /* dependencies */
29 #include "q2map.h"
30
31 #define qtrue true
32 #define qfalse false
33
34 char    *mapname;
35 char    game[64];
36 extern  qboolean        verbose;
37
38 /// BSP 
39 extern  qboolean        drawflag;
40 extern  qboolean        noprune;
41 extern  qboolean        glview;
42 extern  qboolean        nodetail;
43 extern  qboolean        fulldetail;
44 extern  qboolean        onlyents;
45 extern  qboolean        nomerge;
46 extern  qboolean        nowater;
47 extern  qboolean        nofill;
48 extern  qboolean        nocsg;
49 extern  qboolean        noweld;
50 extern  qboolean        noshare;
51 extern  qboolean        nosubdiv;
52 extern  qboolean        notjunc;
53 extern  qboolean        noopt;
54 extern  qboolean        leaktest;
55 extern  qboolean        verboseentities;
56 extern  char            outbase[32];
57 extern  int             block_xl, block_xh, block_yl, block_yh;
58 extern  vec_t           microvolume;
59 extern  float           subdivide_size;
60
61 // VIS
62 extern  char            inbase[32];
63 extern  qboolean        fastvis;
64 extern  qboolean        nosort;
65 extern  int             testlevel;
66
67 // RAD
68 extern  qboolean        dumppatches;
69 extern  int             numbounce;
70 extern  qboolean        extrasamples;
71 extern  float           subdiv;
72 extern  float           lightscale;
73 extern  float           direct_scale;
74 extern  float           entity_scale;
75 extern  qboolean        nopvs;
76 extern  float           ambient;
77 extern  float           maxlight;
78
79
80 void InitPaths( int *argc, char **argv );
81
82 /*
83 Random()
84 returns a pseudorandom number between 0 and 1
85 */
86 /*
87 vec_t Random( void )
88 {
89         return (vec_t) rand() / RAND_MAX;
90 }
91 */
92
93
94 /*
95 ExitQ2Map()
96 cleanup routine
97 */
98 /*
99 static void ExitQ2Map( void )
100 {
101         BSPFilesCleanup();
102         if( mapDrawSurfs != NULL )
103                 free( mapDrawSurfs );
104 }
105 */
106
107
108 /*
109 BSPInfo()
110 emits statistics about the bsp file
111 */
112
113 int BSPInfo()
114 {
115         char            source[ 1024 ], ext[ 64 ];
116         int                     size;
117         FILE            *f;
118         
119         Sys_Printf ("\n----- INFO ----\n\n");
120         
121         /* dummy check */
122         if( mapname == NULL )
123         {
124                 Sys_Printf( "No files to dump info for.\n");
125                 return -1;
126         }
127         
128         /* enable info mode */
129         //infoMode = qtrue;
130         
131         
132         /* mangle filename and get size */
133         strcpy( source, mapname );
134         ExtractFileExtension( source, ext );
135         if( !Q_stricmp( ext, "map" ) )
136                 StripExtension( source );
137         DefaultExtension( source, ".bsp" );
138         f = fopen( source, "rb" );
139         if( f )
140         {
141                 size = Q_filelength (f);
142                 fclose( f );
143         }
144         else
145                 size = 0;
146         
147         /* load the bsp file and print lump sizes */
148         Sys_Printf( "Map: %s\n\n", source );
149         
150         Sys_Printf( "-----------------------------------------------------\n" );
151         
152         LoadBSPFile( source );          
153         PrintBSPFileSizes();
154         
155         Sys_Printf( "-----------------------------------------------------\n" );
156         
157         /* print sizes */
158         Sys_Printf( "Total:  %d B = %.3f kB = %.3f MB\n", size, size / 1024.0, size / (1024.0 * 1024.0) );
159         
160         Sys_Printf( "-----------------------------------------------------\n" );
161         
162         /* return count */
163         return 0;
164 }
165
166
167
168 /*
169 ScaleBSPMain()
170 amaze and confuse your enemies with wierd scaled maps!
171 */
172 /*
173 int ScaleBSPMain( int argc, char **argv )
174 {
175         int                     i;
176         float           f, scale;
177         vec3_t          vec;
178         char            str[ 1024 ];
179         
180         
181         // arg checking 
182         if( argc < 2 )
183         {
184                 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
185                 return 0;
186         }
187         
188         // get scale 
189         scale = atof( argv[ argc - 2 ] );
190         if( scale == 0.0f )
191         {
192                 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
193                 Sys_Printf( "Non-zero scale value required.\n" );
194                 return 0;
195         }
196         
197         // do some path mangling 
198         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
199         StripExtension( source );
200         DefaultExtension( source, ".bsp" );
201         
202         // load the bsp 
203         Sys_Printf( "Loading %s\n", source );
204         LoadBSPFile( source );
205         ParseEntities();
206         
207         // note it 
208         Sys_Printf( "--- ScaleBSP ---\n" );
209         Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
210         
211         // scale entity keys 
212         for( i = 0; i < numBSPEntities && i < numEntities; i++ )
213         {
214                 // scale origin 
215                 GetVectorForKey( &entities[ i ], "origin", vec );
216                 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) )
217                 {
218                         VectorScale( vec, scale, vec );
219                         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
220                         SetKeyValue( &entities[ i ], "origin", str );
221                 }
222                 
223                 // scale door lip 
224                 f = FloatForKey( &entities[ i ], "lip" );
225                 if( f )
226                 {
227                         f *= scale;
228                         sprintf( str, "%f", f );
229                         SetKeyValue( &entities[ i ], "lip", str );
230                 }
231         }
232         
233         // scale models 
234         for( i = 0; i < numBSPModels; i++ )
235         {
236                 VectorScale( bspModels[ i ].mins, scale, bspModels[ i ].mins );
237                 VectorScale( bspModels[ i ].maxs, scale, bspModels[ i ].maxs );
238         }
239         
240         // scale nodes 
241         for( i = 0; i < numBSPNodes; i++ )
242         {
243                 VectorScale( bspNodes[ i ].mins, scale, bspNodes[ i ].mins );
244                 VectorScale( bspNodes[ i ].maxs, scale, bspNodes[ i ].maxs );
245         }
246         
247         // scale leafs 
248         for( i = 0; i < numBSPLeafs; i++ )
249         {
250                 VectorScale( bspLeafs[ i ].mins, scale, bspLeafs[ i ].mins );
251                 VectorScale( bspLeafs[ i ].maxs, scale, bspLeafs[ i ].maxs );
252         }
253         
254         // scale drawverts 
255         for( i = 0; i < numBSPDrawVerts; i++ )
256                 VectorScale( bspDrawVerts[ i ].xyz, scale, bspDrawVerts[ i ].xyz );
257         
258         // scale planes 
259         for( i = 0; i < numBSPPlanes; i++ )
260                 bspPlanes[ i ].dist *= scale;
261         
262         // scale gridsize 
263         GetVectorForKey( &entities[ 0 ], "gridsize", vec );
264         if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
265                 VectorCopy( gridSize, vec );
266         VectorScale( vec, scale, vec );
267         sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
268         SetKeyValue( &entities[ 0 ], "gridsize", str );
269         
270         // write the bsp 
271         UnparseEntities();
272         StripExtension( source );
273         DefaultExtension( source, "_s.bsp" );
274         Sys_Printf( "Writing %s\n", source );
275         WriteBSPFile( source );
276         
277         // return to sender 
278         return 0;
279 }
280 */
281
282
283 /*
284 ConvertBSPMain()
285 main argument processing function for bsp conversion
286 */
287 /*
288 int ConvertBSPMain( int argc, char **argv )
289 {
290         int             i;
291         int             (*convertFunc)( char * );
292         
293         
294         // set default 
295         convertFunc = ConvertBSPToASE;
296         
297         // arg checking 
298         if( argc < 1 )
299         {
300                 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
301                 return 0;
302         }
303         
304         // process arguments 
305         for( i = 1; i < (argc - 1); i++ )
306         {
307                 // -format map|ase|... 
308                 if( !strcmp( argv[ i ],  "-format" ) )
309                 {
310                         i++;
311                         if( !Q_stricmp( argv[ i ], "ase" ) )
312                                 convertFunc = ConvertBSPToASE;
313                         else if( !Q_stricmp( argv[ i ], "map" ) )
314                                 convertFunc = ConvertBSPToMap;
315                         else
316                                 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
317                 }
318         }
319         
320         // clean up map name 
321         strcpy( source, ExpandArg( argv[ i ] ) );
322         StripExtension( source );
323         DefaultExtension( source, ".bsp" );
324         
325         LoadShaderInfo();
326         
327         Sys_Printf( "Loading %s\n", source );
328         
329         // ydnar: load surface file 
330         //%     LoadSurfaceExtraFile( source );
331         
332         LoadBSPFile( source );
333         
334         // parse bsp entities 
335         ParseEntities();
336         
337         // convert 
338         return convertFunc( source );
339 }
340 */
341
342 int Check_BSP_Options( int argc, char **argv )
343 {
344         int i;
345         
346         for (i=1 ; i<argc ; i++)
347         {
348                 if (!strcmp(argv[i],"-glview"))
349                 {
350                         glview = true;
351                 }
352                 else if (!strcmp(argv[i], "-draw"))
353                 {
354                         Sys_Printf ("drawflag = true\n");
355                         drawflag = true;
356                 }
357                 else if (!strcmp(argv[i], "-noweld"))
358                 {
359                         Sys_Printf ("noweld = true\n");
360                         noweld = true;
361                 }
362                 else if (!strcmp(argv[i], "-nocsg"))
363                 {
364                         Sys_Printf ("nocsg = true\n");
365                         nocsg = true;
366                 }
367                 else if (!strcmp(argv[i], "-noshare"))
368                 {
369                         Sys_Printf ("noshare = true\n");
370                         noshare = true;
371                 }
372                 else if (!strcmp(argv[i], "-notjunc"))
373                 {
374                         Sys_Printf ("notjunc = true\n");
375                         notjunc = true;
376                 }
377                 else if (!strcmp(argv[i], "-nowater"))
378                 {
379                         Sys_Printf ("nowater = true\n");
380                         nowater = true;
381                 }
382                 else if (!strcmp(argv[i], "-noopt"))
383                 {
384                         Sys_Printf ("noopt = true\n");
385                         noopt = true;
386                 }
387                 else if (!strcmp(argv[i], "-noprune"))
388                 {
389                         Sys_Printf ("noprune = true\n");
390                         noprune = true;
391                 }
392                 else if (!strcmp(argv[i], "-nofill"))
393                 {
394                         Sys_Printf ("nofill = true\n");
395                         nofill = true;
396                 }
397                 else if (!strcmp(argv[i], "-nomerge"))
398                 {
399                         Sys_Printf ("nomerge = true\n");
400                         nomerge = true;
401                 }
402                 else if (!strcmp(argv[i], "-nosubdiv"))
403                 {
404                         Sys_Printf ("nosubdiv = true\n");
405                         nosubdiv = true;
406                 }
407                 else if (!strcmp(argv[i], "-nodetail"))
408                 {
409                         Sys_Printf ("nodetail = true\n");
410                         nodetail = true;
411                 }
412                 else if (!strcmp(argv[i], "-fulldetail"))
413                 {
414                         Sys_Printf ("fulldetail = true\n");
415                         fulldetail = true;
416                 }
417                 else if (!strcmp(argv[i], "-onlyents"))
418                 {
419                         Sys_Printf ("onlyents = true\n");
420                         onlyents = true;
421                 }
422                 else if (!strcmp(argv[i], "-micro"))
423                 {
424                         microvolume = atof(argv[i+1]);
425                         Sys_Printf ("microvolume = %f\n", microvolume);
426                         i++;
427                 }
428                 else if (!strcmp(argv[i], "-leaktest"))
429                 {
430                         Sys_Printf ("leaktest = true\n");
431                         leaktest = true;
432                 }
433                 else if (!strcmp(argv[i], "-verboseentities"))
434                 {
435                         Sys_Printf ("verboseentities = true\n");
436                         verboseentities = true;
437                 }
438                 else if (!strcmp(argv[i], "-chop"))
439                 {
440                         subdivide_size = atof(argv[i+1]);
441                         Sys_Printf ("subdivide_size = %f\n", subdivide_size);
442                         i++;
443                 }
444                 else if (!strcmp(argv[i], "-block"))
445                 {
446                         block_xl = block_xh = atoi(argv[i+1]);
447                         block_yl = block_yh = atoi(argv[i+2]);
448                         Sys_Printf ("block: %i,%i\n", block_xl, block_yl);
449                         i+=2;
450                 }
451                 else if (!strcmp(argv[i], "-blocks"))
452                 {
453                         block_xl = atoi(argv[i+1]);
454                         block_yl = atoi(argv[i+2]);
455                         block_xh = atoi(argv[i+3]);
456                         block_yh = atoi(argv[i+4]);
457                         Sys_Printf ("blocks: %i,%i to %i,%i\n", 
458                                 block_xl, block_yl, block_xh, block_yh);
459                         i+=4;
460                 }
461                 else if (!strcmp (argv[i],"-tmpout"))
462                 {
463                         strcpy (outbase, "/tmp");
464                 }
465                 else
466                         break;
467         }
468
469
470         return 0;
471 }
472
473 int Check_VIS_Options( int argc, char **argv )
474 {
475         int i;
476         
477         for (i=1 ; i<argc ; i++)
478         {
479                 if (!strcmp(argv[i], "-fast"))
480                 {
481                         Sys_Printf ("fastvis = true\n");
482                         fastvis = true;
483                 }
484                 else if (!strcmp(argv[i], "-level"))
485                 {
486                         testlevel = atoi(argv[i+1]);
487                         Sys_Printf ("testlevel = %i\n", testlevel);
488                         i++;
489                 }
490                 else if (!strcmp (argv[i],"-nosort"))
491                 {
492                         Sys_Printf ("nosort = true\n");
493                         nosort = true;
494                 }
495                 else if (!strcmp (argv[i],"-tmpin"))
496                         strcpy (inbase, "/tmp");
497                 else if (!strcmp (argv[i],"-tmpout"))
498                         strcpy (outbase, "/tmp");
499                 else
500                         break;
501         }
502
503         return 0;
504 }
505
506 int Check_RAD_Options( int argc, char **argv )
507 {
508         int i;
509         
510         for (i=1 ; i<argc ; i++)
511         {
512                 if (!strcmp(argv[i],"-dump"))
513                         dumppatches = true;
514                 else if (!strcmp(argv[i],"-bounce"))
515                 {
516                         numbounce = atoi (argv[i+1]);
517                         i++;
518                 }
519                 else if (!strcmp(argv[i],"-extra"))
520                 {
521                         extrasamples = true;
522                         Sys_Printf ("extrasamples = true\n");
523                 }
524                 else if (!strcmp(argv[i],"-chop"))
525                 {
526                         subdiv = atoi (argv[i+1]);
527                         i++;
528                 }
529                 else if (!strcmp(argv[i],"-scale"))
530                 {
531                         lightscale = atof (argv[i+1]);
532                         i++;
533                 }
534                 else if (!strcmp(argv[i],"-direct"))
535                 {
536                         direct_scale *= atof(argv[i+1]);
537                         Sys_Printf ("direct light scaling at %f\n", direct_scale);
538                         i++;
539                 }
540                 else if (!strcmp(argv[i],"-entity"))
541                 {
542                         entity_scale *= atof(argv[i+1]);
543                         Sys_Printf ("entity light scaling at %f\n", entity_scale);
544                         i++;
545                 }
546                 else if (!strcmp(argv[i],"-glview"))
547                 {
548                         glview = true;
549                         Sys_Printf ("glview = true\n");
550                 }
551                 else if (!strcmp(argv[i],"-nopvs"))
552                 {
553                         nopvs = true;
554                         Sys_Printf ("nopvs = true\n");
555                 }
556                 else if (!strcmp(argv[i],"-ambient"))
557                 {
558                         ambient = atof (argv[i+1]) * 128;
559                         i++;
560                 }
561                 else if (!strcmp(argv[i],"-maxlight"))
562                 {
563                         maxlight = atof (argv[i+1]) * 128;
564                         i++;
565                 }
566                 else if (!strcmp (argv[i],"-tmpin"))
567                         strcpy (inbase, "/tmp");
568                 else if (!strcmp (argv[i],"-tmpout"))
569                         strcpy (outbase, "/tmp");
570                 else
571                         break;
572         }
573
574         return 0;
575 }
576
577 /*
578 main()
579 */
580
581 int main( int argc, char **argv )
582 {
583         int     i;
584         int     r=0;
585         int     total_time;
586         double  start, end;
587         int     alt_argc;
588         char**  alt_argv;
589         qboolean        do_info=qfalse;
590         qboolean        do_bsp=qfalse;
591         qboolean        do_vis=qfalse;
592         qboolean        do_rad=qfalse;
593         
594         
595         /* we want consistent 'randomness' */
596         srand( 0 );
597         
598         /* start timer */
599         start = I_FloatTime();
600
601         Sys_Printf( "\nQ2Map - Ver. 1.0\n" );
602
603         /* set exit call */
604         //atexit( ExitQ3Map );
605         
606         game[0] = 0;
607         
608         if ( argc < 2 )
609         {
610                 Sys_Printf(" %s: -game [quake2,heretic2] -fs_basepath basepath -info -bsp -vis -rad mapname\n",argv[0]);
611                 return -1;
612         }
613         /* read general options first */
614         for( i = 1; i < argc; i++ )
615         {
616                 /* -connect */
617                 if( !strcmp( argv[ i ], "-connect" ) )
618                 {
619                         i++;
620                         Broadcast_Setup( argv[ i ] );
621                 }
622                 
623                 /* verbose */
624                 else if( !strcmp( argv[ i ], "-v" ) )
625                 {
626                         verbose = qtrue;
627                 }
628                 
629                 /* threads */
630                  else if( !strcmp( argv[ i ], "-threads" ) )
631                 {
632                         i++;
633                         numthreads = atoi( argv[ i ] );
634                 }
635                 else if( !strcmp( argv[ i ], "-game" ) )
636                 {
637                         i++;
638                         strncpy (game, argv[ i ] , 64);
639                         strlower(game);
640                 } 
641         }
642         
643         /* set number of threads */
644         ThreadSetDefault();
645         
646         /* ydnar: new path initialization */
647         InitPaths( &argc, argv );
648         
649         /* read compiling options */
650         for( i = 1; i < argc; i++ )
651         {
652                 /* info */
653                 if( !strcmp( argv[ i ], "-info" ) )
654                 {
655                         do_info=qtrue;
656                 }
657
658                 /* bsp */
659                 if( !strcmp( argv[ i ], "-bsp" ) )
660                 {
661                         do_bsp=qtrue;
662                         alt_argc=argc - i;
663                         alt_argv= (char **) ( argv + i );
664                         Check_BSP_Options( alt_argc, alt_argv );
665                 }
666
667                 /* vis */
668                 if( !strcmp( argv[ i ], "-vis" ) )
669                 {
670                         do_vis=qtrue;
671                         alt_argc=argc - i;
672                         alt_argv= (char **) ( argv + i );
673                         Check_VIS_Options( alt_argc, alt_argv );
674                 }
675
676                 /* rad */
677                 if( !strcmp( argv[ i ], "-rad" ) )
678                 {
679                         do_rad=qtrue;
680                         alt_argc=argc - i;
681                         alt_argv= (char **) ( argv + i );
682                         Check_RAD_Options( alt_argc, alt_argv );
683                 }
684         }
685         
686         if (game[0] == 0)
687                 strncpy( game, "quake2", 7);
688                 
689         Sys_Printf("Game: %s\n", game);
690         
691         if ( !do_info && !do_bsp && !do_vis && !do_rad)
692                 Sys_Printf("ERROR: -bsp, -vis, -light, nor -info specified.\nWhat to you want me to do?\n\n");
693         else
694         {
695                 mapname=argv[argc-1];
696                 
697                 if (do_bsp)
698                         BSP_Main();
699                 if (do_vis)
700                         VIS_Main();
701                 if (do_rad)
702                         RAD_Main();
703                 if (do_info)
704                         BSPInfo();
705                 
706         }
707         
708         /* emit time */
709         end = I_FloatTime();
710         total_time = (int) (end-start);
711         Sys_Printf("\nTotal Time: ");
712         if ( total_time > 59 )
713                 Sys_Printf("%d Minutes ", total_time/60 );
714         Sys_Printf( "%d Seconds\n", total_time%60 );
715         
716         /* shut down connection */
717         Broadcast_Shutdown();
718         
719         /* return any error code */
720         return r;
721 }