PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/quake2/q2map/main.c

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