]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/main.c
split help stuff from q3map2
[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
39
40
41 /*
42    Random()
43    returns a pseudorandom number between 0 and 1
44  */
45
46 vec_t Random( void ){
47         return (vec_t) rand() / RAND_MAX;
48 }
49
50
51 char *Q_strncpyz( char *dst, const char *src, size_t len ) {
52         if ( len == 0 ) {
53                 abort();
54         }
55
56         strncpy( dst, src, len );
57         dst[ len - 1 ] = '\0';
58         return dst;
59 }
60
61
62 char *Q_strcat( char *dst, size_t dlen, const char *src ) {
63         size_t n = strlen( dst  );
64
65         if ( n > dlen ) {
66                 abort(); /* buffer overflow */
67         }
68
69         return Q_strncpyz( dst + n, src, dlen - n );
70 }
71
72
73 char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen ) {
74         size_t n = strlen( dst );
75
76         if ( n > dlen ) {
77                 abort(); /* buffer overflow */
78         }
79
80         return Q_strncpyz( dst + n, src, MIN( slen, dlen - n ) );
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    MD4BlockChecksum()
97    calculates an md4 checksum for a block of data
98  */
99
100 static int MD4BlockChecksum( void *buffer, int length ){
101         return Com_BlockChecksum( buffer, length );
102 }
103
104 /*
105    FixAAS()
106    resets an aas checksum to match the given BSP
107  */
108
109 int FixAAS( int argc, char **argv ){
110         int length, checksum;
111         void        *buffer;
112         FILE        *file;
113         char aas[ 1024 ], **ext;
114         char        *exts[] =
115         {
116                 ".aas",
117                 "_b0.aas",
118                 "_b1.aas",
119                 NULL
120         };
121
122
123         /* arg checking */
124         if ( argc < 2 ) {
125                 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
126                 return 0;
127         }
128
129         /* do some path mangling */
130         strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
131         StripExtension( source );
132         DefaultExtension( source, ".bsp" );
133
134         /* note it */
135         Sys_Printf( "--- FixAAS ---\n" );
136
137         /* load the bsp */
138         Sys_Printf( "Loading %s\n", source );
139         length = LoadFile( source, &buffer );
140
141         /* create bsp checksum */
142         Sys_Printf( "Creating checksum...\n" );
143         checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
144
145         /* write checksum to aas */
146         ext = exts;
147         while ( *ext )
148         {
149                 /* mangle name */
150                 strcpy( aas, source );
151                 StripExtension( aas );
152                 strcat( aas, *ext );
153                 Sys_Printf( "Trying %s\n", aas );
154                 ext++;
155
156                 /* fix it */
157                 file = fopen( aas, "r+b" );
158                 if ( !file ) {
159                         continue;
160                 }
161                 if ( fwrite( &checksum, 4, 1, file ) != 1 ) {
162                         Error( "Error writing checksum to %s", aas );
163                 }
164                 fclose( file );
165         }
166
167         /* return to sender */
168         return 0;
169 }
170
171 /*
172    main()
173    q3map mojo...
174  */
175
176 int main( int argc, char **argv ){
177         int i, r;
178         double start, end;
179
180
181         /* we want consistent 'randomness' */
182         srand( 0 );
183
184         /* start timer */
185         start = I_FloatTime();
186
187         /* this was changed to emit version number over the network */
188         printf( Q3MAP_VERSION "\n" );
189
190         /* set exit call */
191         atexit( ExitQ3Map );
192
193         /* read general options first */
194         for ( i = 1; i < argc; i++ )
195         {
196                 /* -help */
197                 if ( !strcmp( argv[ i ], "-h" ) || !strcmp( argv[ i ], "--help" )
198                         || !strcmp( argv[ i ], "-help" ) ) {
199                         HelpMain(argv[i+1]);
200                         return 0;
201                 }
202
203                 /* -connect */
204                 if ( !strcmp( argv[ i ], "-connect" ) ) {
205                         argv[ i ] = NULL;
206                         i++;
207                         Broadcast_Setup( argv[ i ] );
208                         argv[ i ] = NULL;
209                 }
210
211                 /* verbose */
212                 else if ( !strcmp( argv[ i ], "-v" ) ) {
213                         if ( !verbose ) {
214                                 verbose = qtrue;
215                                 argv[ i ] = NULL;
216                         }
217                 }
218
219                 /* force */
220                 else if ( !strcmp( argv[ i ], "-force" ) ) {
221                         force = qtrue;
222                         argv[ i ] = NULL;
223                 }
224
225                 /* patch subdivisions */
226                 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
227                         argv[ i ] = NULL;
228                         i++;
229                         patchSubdivisions = atoi( argv[ i ] );
230                         argv[ i ] = NULL;
231                         if ( patchSubdivisions <= 0 ) {
232                                 patchSubdivisions = 1;
233                         }
234                 }
235
236                 /* threads */
237                 else if ( !strcmp( argv[ i ], "-threads" ) ) {
238                         argv[ i ] = NULL;
239                         i++;
240                         numthreads = atoi( argv[ i ] );
241                         argv[ i ] = NULL;
242                 }
243         }
244
245         /* init model library */
246         PicoInit();
247         PicoSetMallocFunc( safe_malloc );
248         PicoSetFreeFunc( free );
249         PicoSetPrintFunc( PicoPrintFunc );
250         PicoSetLoadFileFunc( PicoLoadFileFunc );
251         PicoSetFreeFileFunc( free );
252
253         /* set number of threads */
254         ThreadSetDefault();
255
256         /* generate sinusoid jitter table */
257         for ( i = 0; i < MAX_JITTERS; i++ )
258         {
259                 jitters[ i ] = sin( i * 139.54152147 );
260                 //%     Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
261         }
262
263         /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
264            and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
265
266         Sys_Printf( "Q3Map         - v1.0r (c) 1999 Id Software Inc.\n" );
267         Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
268         Sys_Printf( "NetRadiant    - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
269         Sys_Printf( "%s\n", Q3MAP_MOTD );
270
271         /* ydnar: new path initialization */
272         InitPaths( &argc, argv );
273
274         /* set game options */
275         if ( !patchSubdivisions ) {
276                 patchSubdivisions = game->patchSubdivisions;
277         }
278
279         /* check if we have enough options left to attempt something */
280         if ( argc < 2 ) {
281                 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
282         }
283
284         /* fixaas */
285         if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
286                 r = FixAAS( argc - 1, argv + 1 );
287         }
288
289         /* analyze */
290         else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
291                 r = AnalyzeBSP( argc - 1, argv + 1 );
292         }
293
294         /* info */
295         else if ( !strcmp( argv[ 1 ], "-info" ) ) {
296                 r = BSPInfo( argc - 2, argv + 2 );
297         }
298
299         /* vis */
300         else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
301                 r = VisMain( argc - 1, argv + 1 );
302         }
303
304         /* light */
305         else if ( !strcmp( argv[ 1 ], "-light" ) ) {
306                 r = LightMain( argc - 1, argv + 1 );
307         }
308
309         /* vlight */
310         else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
311                 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
312                 argv[ 1 ] = "-fast";    /* eek a hack */
313                 r = LightMain( argc, argv );
314         }
315
316         /* ydnar: lightmap export */
317         else if ( !strcmp( argv[ 1 ], "-export" ) ) {
318                 r = ExportLightmapsMain( argc - 1, argv + 1 );
319         }
320
321         /* ydnar: lightmap import */
322         else if ( !strcmp( argv[ 1 ], "-import" ) ) {
323                 r = ImportLightmapsMain( argc - 1, argv + 1 );
324         }
325
326         /* ydnar: bsp scaling */
327         else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
328                 r = ScaleBSPMain( argc - 1, argv + 1 );
329         }
330
331         /* ydnar: bsp conversion */
332         else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
333                 r = ConvertBSPMain( argc - 1, argv + 1 );
334         }
335
336         /* div0: minimap */
337         else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
338                 r = MiniMapBSPMain( argc - 1, argv + 1 );
339         }
340
341         /* ydnar: otherwise create a bsp */
342         else{
343                 r = BSPMain( argc, argv );
344         }
345
346         /* emit time */
347         end = I_FloatTime();
348         Sys_Printf( "%9.0f seconds elapsed\n", end - start );
349
350         /* shut down connection */
351         Broadcast_Shutdown();
352
353         /* return any error code */
354         return r;
355 }