]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/main.c
q3map2: fix crash if command-line argument is missing
[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    main()
98    q3map mojo...
99  */
100
101 int main( int argc, char **argv ){
102         int i, r;
103         double start, end;
104         extern qboolean werror;
105
106
107         /* we want consistent 'randomness' */
108         srand( 0 );
109
110         /* start timer */
111         start = I_FloatTime();
112
113         /* this was changed to emit version number over the network */
114         printf( Q3MAP_VERSION "\n" );
115
116         /* set exit call */
117         atexit( ExitQ3Map );
118
119         /* read general options first */
120         for ( i = 1; i < argc; i++ )
121         {
122                 /* -help */
123                 if ( !strcmp( argv[ i ], "-h" ) || !strcmp( argv[ i ], "--help" )
124                         || !strcmp( argv[ i ], "-help" ) ) {
125                         HelpMain( ( i + 1 < argc ) ? argv[ i + 1 ] : NULL );
126                         return 0;
127                 }
128
129                 /* -connect */
130                 if ( !strcmp( argv[ i ], "-connect" ) ) {
131                         if ( ++i >= argc || !argv[ i ] ) {
132                                 Error( "Out of arguments: No address specified after %s", argv[ i - 1 ] );
133                         }
134                         argv[ i - 1 ] = NULL;
135                         Broadcast_Setup( argv[ i ] );
136                         argv[ i ] = NULL;
137                 }
138
139                 /* verbose */
140                 else if ( !strcmp( argv[ i ], "-v" ) ) {
141                         if ( !verbose ) {
142                                 verbose = qtrue;
143                                 argv[ i ] = NULL;
144                         }
145                 }
146
147                 /* force */
148                 else if ( !strcmp( argv[ i ], "-force" ) ) {
149                         force = qtrue;
150                         argv[ i ] = NULL;
151                 }
152
153                 /* make all warnings into errors */
154                 else if ( !strcmp( argv[ i ], "-werror" ) ) {
155                         werror = qtrue;
156                         argv[ i ] = NULL;
157                 }
158
159                 /* patch subdivisions */
160                 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
161                         if ( ++i >= argc || !argv[ i ] ) {
162                                 Error( "Out of arguments: No value specified after %s", argv[ i - 1 ] );
163                         }
164                         argv[ i - 1 ] = NULL;
165                         patchSubdivisions = atoi( argv[ i ] );
166                         argv[ i ] = NULL;
167                         if ( patchSubdivisions <= 0 ) {
168                                 patchSubdivisions = 1;
169                         }
170                 }
171
172                 /* threads */
173                 else if ( !strcmp( argv[ i ], "-threads" ) ) {
174                         if ( ++i >= argc || !argv[ i ] ) {
175                                 Error( "Out of arguments: No value specified after %s", argv[ i - 1 ] );
176                         }
177                         argv[ i - 1 ] = NULL;
178                         numthreads = atoi( argv[ i ] );
179                         argv[ i ] = NULL;
180                 }
181         }
182
183         /* init model library */
184         PicoInit();
185         PicoSetMallocFunc( safe_malloc );
186         PicoSetFreeFunc( free );
187         PicoSetPrintFunc( PicoPrintFunc );
188         PicoSetLoadFileFunc( PicoLoadFileFunc );
189         PicoSetFreeFileFunc( free );
190
191         /* set number of threads */
192         ThreadSetDefault();
193
194         /* generate sinusoid jitter table */
195         for ( i = 0; i < MAX_JITTERS; i++ )
196         {
197                 jitters[ i ] = sin( i * 139.54152147 );
198                 //%     Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
199         }
200
201         /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
202            and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
203
204         Sys_Printf( "Q3Map         - v1.0r (c) 1999 Id Software Inc.\n" );
205         Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
206         Sys_Printf( RADIANT_NAME "    - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
207         Sys_Printf( "%s\n", Q3MAP_MOTD );
208
209         /* ydnar: new path initialization */
210         InitPaths( &argc, argv );
211
212         /* set game options */
213         if ( !patchSubdivisions ) {
214                 patchSubdivisions = game->patchSubdivisions;
215         }
216
217         /* check if we have enough options left to attempt something */
218         if ( argc < 2 ) {
219                 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
220         }
221
222         /* fixaas */
223         if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
224                 r = FixAASMain( argc - 1, argv + 1 );
225         }
226
227         /* analyze */
228         else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
229                 r = AnalyzeBSPMain( argc - 1, argv + 1 );
230         }
231
232         /* info */
233         else if ( !strcmp( argv[ 1 ], "-info" ) ) {
234                 r = BSPInfoMain( argc - 2, argv + 2 );
235         }
236
237         /* vis */
238         else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
239                 r = VisMain( argc - 1, argv + 1 );
240         }
241
242         /* light */
243         else if ( !strcmp( argv[ 1 ], "-light" ) ) {
244                 r = LightMain( argc - 1, argv + 1 );
245         }
246
247         /* vlight */
248         else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
249                 Sys_FPrintf( SYS_WRN, "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
250                 argv[ 1 ] = "-fast";    /* eek a hack */
251                 r = LightMain( argc, argv );
252         }
253
254         /* QBall: export entities */
255         else if ( !strcmp( argv[ 1 ], "-exportents" ) ) {
256                 r = ExportEntitiesMain( argc - 1, argv + 1 );
257         }
258
259         /* ydnar: lightmap export */
260         else if ( !strcmp( argv[ 1 ], "-export" ) ) {
261                 r = ExportLightmapsMain( argc - 1, argv + 1 );
262         }
263
264         /* ydnar: lightmap import */
265         else if ( !strcmp( argv[ 1 ], "-import" ) ) {
266                 r = ImportLightmapsMain( argc - 1, argv + 1 );
267         }
268
269         /* ydnar: bsp scaling */
270         else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
271                 r = ScaleBSPMain( argc - 1, argv + 1 );
272         }
273
274         /* ydnar: bsp conversion */
275         else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
276                 r = ConvertBSPMain( argc - 1, argv + 1 );
277         }
278
279         /* div0: minimap */
280         else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
281                 r = MiniMapBSPMain( argc - 1, argv + 1 );
282         }
283
284         /* ydnar: otherwise create a bsp */
285         else {
286                 /* used to write Smokin'Guns like tex file */
287                 compile_map = qtrue;
288
289                 r = BSPMain( argc, argv );
290         }
291
292         /* emit time */
293         end = I_FloatTime();
294         Sys_Printf( "%9.0f seconds elapsed\n", end - start );
295
296         /* shut down connection */
297         Broadcast_Shutdown();
298
299         /* return any error code */
300         return r;
301 }