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