bspfile.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, 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 Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "cmdlib.h"
  19. #include "mathlib.h"
  20. #include "bspfile.h"
  21. #include "scriplib.h"
  22. void GetLeafNums (void);
  23. //=============================================================================
  24. int nummodels;
  25. dmodel_t dmodels[MAX_MAP_MODELS];
  26. int numShaders;
  27. dshader_t dshaders[MAX_MAP_SHADERS];
  28. int entdatasize;
  29. char dentdata[MAX_MAP_ENTSTRING];
  30. int numleafs;
  31. dleaf_t dleafs[MAX_MAP_LEAFS];
  32. int numplanes;
  33. dplane_t dplanes[MAX_MAP_PLANES];
  34. int numnodes;
  35. dnode_t dnodes[MAX_MAP_NODES];
  36. int numleafsurfaces;
  37. int dleafsurfaces[MAX_MAP_LEAFFACES];
  38. int numleafbrushes;
  39. int dleafbrushes[MAX_MAP_LEAFBRUSHES];
  40. int numbrushes;
  41. dbrush_t dbrushes[MAX_MAP_BRUSHES];
  42. int numbrushsides;
  43. dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
  44. int numLightBytes;
  45. byte lightBytes[MAX_MAP_LIGHTING];
  46. int numGridPoints;
  47. byte gridData[MAX_MAP_LIGHTGRID];
  48. int numVisBytes;
  49. byte visBytes[MAX_MAP_VISIBILITY];
  50. int numDrawVerts;
  51. drawVert_t drawVerts[MAX_MAP_DRAW_VERTS];
  52. int numDrawIndexes;
  53. int drawIndexes[MAX_MAP_DRAW_INDEXES];
  54. int numDrawSurfaces;
  55. dsurface_t drawSurfaces[MAX_MAP_DRAW_SURFS];
  56. int numFogs;
  57. dfog_t dfogs[MAX_MAP_FOGS];
  58. //=============================================================================
  59. /*
  60. =============
  61. SwapBlock
  62. If all values are 32 bits, this can be used to swap everything
  63. =============
  64. */
  65. void SwapBlock( int *block, int sizeOfBlock ) {
  66. int i;
  67. sizeOfBlock >>= 2;
  68. for ( i = 0 ; i < sizeOfBlock ; i++ ) {
  69. block[i] = LittleLong( block[i] );
  70. }
  71. }
  72. /*
  73. =============
  74. SwapBSPFile
  75. Byte swaps all data in a bsp file.
  76. =============
  77. */
  78. void SwapBSPFile( void ) {
  79. int i;
  80. // models
  81. SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
  82. // shaders (don't swap the name)
  83. for ( i = 0 ; i < numShaders ; i++ ) {
  84. dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
  85. dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
  86. }
  87. // planes
  88. SwapBlock( (int *)dplanes, numplanes * sizeof( dplanes[0] ) );
  89. // nodes
  90. SwapBlock( (int *)dnodes, numnodes * sizeof( dnodes[0] ) );
  91. // leafs
  92. SwapBlock( (int *)dleafs, numleafs * sizeof( dleafs[0] ) );
  93. // leaffaces
  94. SwapBlock( (int *)dleafsurfaces, numleafsurfaces * sizeof( dleafsurfaces[0] ) );
  95. // leafbrushes
  96. SwapBlock( (int *)dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ) );
  97. // brushes
  98. SwapBlock( (int *)dbrushes, numbrushes * sizeof( dbrushes[0] ) );
  99. // brushsides
  100. SwapBlock( (int *)dbrushsides, numbrushsides * sizeof( dbrushsides[0] ) );
  101. // vis
  102. ((int *)&visBytes)[0] = LittleLong( ((int *)&visBytes)[0] );
  103. ((int *)&visBytes)[1] = LittleLong( ((int *)&visBytes)[1] );
  104. // drawverts (don't swap colors )
  105. for ( i = 0 ; i < numDrawVerts ; i++ ) {
  106. drawVerts[i].lightmap[0] = LittleFloat( drawVerts[i].lightmap[0] );
  107. drawVerts[i].lightmap[1] = LittleFloat( drawVerts[i].lightmap[1] );
  108. drawVerts[i].st[0] = LittleFloat( drawVerts[i].st[0] );
  109. drawVerts[i].st[1] = LittleFloat( drawVerts[i].st[1] );
  110. drawVerts[i].xyz[0] = LittleFloat( drawVerts[i].xyz[0] );
  111. drawVerts[i].xyz[1] = LittleFloat( drawVerts[i].xyz[1] );
  112. drawVerts[i].xyz[2] = LittleFloat( drawVerts[i].xyz[2] );
  113. drawVerts[i].normal[0] = LittleFloat( drawVerts[i].normal[0] );
  114. drawVerts[i].normal[1] = LittleFloat( drawVerts[i].normal[1] );
  115. drawVerts[i].normal[2] = LittleFloat( drawVerts[i].normal[2] );
  116. }
  117. // drawindexes
  118. SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
  119. // drawsurfs
  120. SwapBlock( (int *)drawSurfaces, numDrawSurfaces * sizeof( drawSurfaces[0] ) );
  121. // fogs
  122. for ( i = 0 ; i < numFogs ; i++ ) {
  123. dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
  124. dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
  125. }
  126. }
  127. /*
  128. =============
  129. CopyLump
  130. =============
  131. */
  132. int CopyLump( dheader_t *header, int lump, void *dest, int size ) {
  133. int length, ofs;
  134. length = header->lumps[lump].filelen;
  135. ofs = header->lumps[lump].fileofs;
  136. if ( length % size ) {
  137. Error ("LoadBSPFile: odd lump size");
  138. }
  139. memcpy( dest, (byte *)header + ofs, length );
  140. return length / size;
  141. }
  142. /*
  143. =============
  144. LoadBSPFile
  145. =============
  146. */
  147. void LoadBSPFile( const char *filename ) {
  148. dheader_t *header;
  149. // load the file header
  150. LoadFile (filename, (void **)&header);
  151. // swap the header
  152. SwapBlock( (int *)header, sizeof(*header) );
  153. if ( header->ident != BSP_IDENT ) {
  154. Error( "%s is not a IBSP file", filename );
  155. }
  156. if ( header->version != BSP_VERSION ) {
  157. Error( "%s is version %i, not %i", filename, header->version, BSP_VERSION );
  158. }
  159. numShaders = CopyLump( header, LUMP_SHADERS, dshaders, sizeof(dshader_t) );
  160. nummodels = CopyLump( header, LUMP_MODELS, dmodels, sizeof(dmodel_t) );
  161. numplanes = CopyLump( header, LUMP_PLANES, dplanes, sizeof(dplane_t) );
  162. numleafs = CopyLump( header, LUMP_LEAFS, dleafs, sizeof(dleaf_t) );
  163. numnodes = CopyLump( header, LUMP_NODES, dnodes, sizeof(dnode_t) );
  164. numleafsurfaces = CopyLump( header, LUMP_LEAFSURFACES, dleafsurfaces, sizeof(dleafsurfaces[0]) );
  165. numleafbrushes = CopyLump( header, LUMP_LEAFBRUSHES, dleafbrushes, sizeof(dleafbrushes[0]) );
  166. numbrushes = CopyLump( header, LUMP_BRUSHES, dbrushes, sizeof(dbrush_t) );
  167. numbrushsides = CopyLump( header, LUMP_BRUSHSIDES, dbrushsides, sizeof(dbrushside_t) );
  168. numDrawVerts = CopyLump( header, LUMP_DRAWVERTS, drawVerts, sizeof(drawVert_t) );
  169. numDrawSurfaces = CopyLump( header, LUMP_SURFACES, drawSurfaces, sizeof(dsurface_t) );
  170. numFogs = CopyLump( header, LUMP_FOGS, dfogs, sizeof(dfog_t) );
  171. numDrawIndexes = CopyLump( header, LUMP_DRAWINDEXES, drawIndexes, sizeof(drawIndexes[0]) );
  172. numVisBytes = CopyLump( header, LUMP_VISIBILITY, visBytes, 1 );
  173. numLightBytes = CopyLump( header, LUMP_LIGHTMAPS, lightBytes, 1 );
  174. entdatasize = CopyLump( header, LUMP_ENTITIES, dentdata, 1);
  175. numGridPoints = CopyLump( header, LUMP_LIGHTGRID, gridData, 8 );
  176. free( header ); // everything has been copied out
  177. // swap everything
  178. SwapBSPFile();
  179. }
  180. //============================================================================
  181. /*
  182. =============
  183. AddLump
  184. =============
  185. */
  186. void AddLump( FILE *bspfile, dheader_t *header, int lumpnum, const void *data, int len ) {
  187. lump_t *lump;
  188. lump = &header->lumps[lumpnum];
  189. lump->fileofs = LittleLong( ftell(bspfile) );
  190. lump->filelen = LittleLong( len );
  191. SafeWrite( bspfile, data, (len+3)&~3 );
  192. }
  193. /*
  194. =============
  195. WriteBSPFile
  196. Swaps the bsp file in place, so it should not be referenced again
  197. =============
  198. */
  199. void WriteBSPFile( const char *filename ) {
  200. dheader_t outheader, *header;
  201. FILE *bspfile;
  202. header = &outheader;
  203. memset( header, 0, sizeof(dheader_t) );
  204. SwapBSPFile();
  205. header->ident = LittleLong( BSP_IDENT );
  206. header->version = LittleLong( BSP_VERSION );
  207. bspfile = SafeOpenWrite( filename );
  208. SafeWrite( bspfile, header, sizeof(dheader_t) ); // overwritten later
  209. AddLump( bspfile, header, LUMP_SHADERS, dshaders, numShaders*sizeof(dshader_t) );
  210. AddLump( bspfile, header, LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t) );
  211. AddLump( bspfile, header, LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t) );
  212. AddLump( bspfile, header, LUMP_NODES, dnodes, numnodes*sizeof(dnode_t) );
  213. AddLump( bspfile, header, LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrush_t) );
  214. AddLump( bspfile, header, LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushside_t) );
  215. AddLump( bspfile, header, LUMP_LEAFSURFACES, dleafsurfaces, numleafsurfaces*sizeof(dleafsurfaces[0]) );
  216. AddLump( bspfile, header, LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0]) );
  217. AddLump( bspfile, header, LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t) );
  218. AddLump( bspfile, header, LUMP_DRAWVERTS, drawVerts, numDrawVerts*sizeof(drawVert_t) );
  219. AddLump( bspfile, header, LUMP_SURFACES, drawSurfaces, numDrawSurfaces*sizeof(dsurface_t) );
  220. AddLump( bspfile, header, LUMP_VISIBILITY, visBytes, numVisBytes );
  221. AddLump( bspfile, header, LUMP_LIGHTMAPS, lightBytes, numLightBytes );
  222. AddLump( bspfile, header, LUMP_LIGHTGRID, gridData, 8 * numGridPoints );
  223. AddLump( bspfile, header, LUMP_ENTITIES, dentdata, entdatasize );
  224. AddLump( bspfile, header, LUMP_FOGS, dfogs, numFogs * sizeof(dfog_t) );
  225. AddLump( bspfile, header, LUMP_DRAWINDEXES, drawIndexes, numDrawIndexes * sizeof(drawIndexes[0]) );
  226. fseek (bspfile, 0, SEEK_SET);
  227. SafeWrite (bspfile, header, sizeof(dheader_t));
  228. fclose (bspfile);
  229. }
  230. //============================================================================
  231. /*
  232. =============
  233. PrintBSPFileSizes
  234. Dumps info about current file
  235. =============
  236. */
  237. void PrintBSPFileSizes( void ) {
  238. if ( !num_entities ) {
  239. ParseEntities();
  240. }
  241. printf ("%6i models %7i\n"
  242. ,nummodels, (int)(nummodels*sizeof(dmodel_t)));
  243. printf ("%6i shaders %7i\n"
  244. ,numShaders, (int)(numShaders*sizeof(dshader_t)));
  245. printf ("%6i brushes %7i\n"
  246. ,numbrushes, (int)(numbrushes*sizeof(dbrush_t)));
  247. printf ("%6i brushsides %7i\n"
  248. ,numbrushsides, (int)(numbrushsides*sizeof(dbrushside_t)));
  249. printf ("%6i fogs %7i\n"
  250. ,numFogs, (int)(numFogs*sizeof(dfog_t)));
  251. printf ("%6i planes %7i\n"
  252. ,numplanes, (int)(numplanes*sizeof(dplane_t)));
  253. printf ("%6i entdata %7i\n", num_entities, entdatasize);
  254. printf ("\n");
  255. printf ("%6i nodes %7i\n"
  256. ,numnodes, (int)(numnodes*sizeof(dnode_t)));
  257. printf ("%6i leafs %7i\n"
  258. ,numleafs, (int)(numleafs*sizeof(dleaf_t)));
  259. printf ("%6i leafsurfaces %7i\n"
  260. ,numleafsurfaces, (int)(numleafsurfaces*sizeof(dleafsurfaces[0])));
  261. printf ("%6i leafbrushes %7i\n"
  262. ,numleafbrushes, (int)(numleafbrushes*sizeof(dleafbrushes[0])));
  263. printf ("%6i drawverts %7i\n"
  264. ,numDrawVerts, (int)(numDrawVerts*sizeof(drawVerts[0])));
  265. printf ("%6i drawindexes %7i\n"
  266. ,numDrawIndexes, (int)(numDrawIndexes*sizeof(drawIndexes[0])));
  267. printf ("%6i drawsurfaces %7i\n"
  268. ,numDrawSurfaces, (int)(numDrawSurfaces*sizeof(drawSurfaces[0])));
  269. printf ("%6i lightmaps %7i\n"
  270. ,numLightBytes / (LIGHTMAP_WIDTH*LIGHTMAP_HEIGHT*3), numLightBytes );
  271. printf (" visibility %7i\n"
  272. , numVisBytes );
  273. }
  274. //============================================
  275. int num_entities;
  276. entity_t entities[MAX_MAP_ENTITIES];
  277. void StripTrailing( char *e ) {
  278. char *s;
  279. s = e + strlen(e)-1;
  280. while (s >= e && *s <= 32)
  281. {
  282. *s = 0;
  283. s--;
  284. }
  285. }
  286. /*
  287. =================
  288. ParseEpair
  289. =================
  290. */
  291. epair_t *ParseEpair( void ) {
  292. epair_t *e;
  293. e = malloc( sizeof(epair_t) );
  294. memset( e, 0, sizeof(epair_t) );
  295. if ( strlen(token) >= MAX_KEY-1 ) {
  296. Error ("ParseEpar: token too long");
  297. }
  298. e->key = copystring( token );
  299. GetToken( qfalse );
  300. if ( strlen(token) >= MAX_VALUE-1 ) {
  301. Error ("ParseEpar: token too long");
  302. }
  303. e->value = copystring( token );
  304. // strip trailing spaces that sometimes get accidentally
  305. // added in the editor
  306. StripTrailing( e->key );
  307. StripTrailing( e->value );
  308. return e;
  309. }
  310. /*
  311. ================
  312. ParseEntity
  313. ================
  314. */
  315. qboolean ParseEntity( void ) {
  316. epair_t *e;
  317. entity_t *mapent;
  318. if ( !GetToken (qtrue) ) {
  319. return qfalse;
  320. }
  321. if ( strcmp (token, "{") ) {
  322. Error ("ParseEntity: { not found");
  323. }
  324. if ( num_entities == MAX_MAP_ENTITIES ) {
  325. Error ("num_entities == MAX_MAP_ENTITIES");
  326. }
  327. mapent = &entities[num_entities];
  328. num_entities++;
  329. do {
  330. if ( !GetToken (qtrue) ) {
  331. Error ("ParseEntity: EOF without closing brace");
  332. }
  333. if ( !strcmp (token, "}") ) {
  334. break;
  335. }
  336. e = ParseEpair ();
  337. e->next = mapent->epairs;
  338. mapent->epairs = e;
  339. } while (1);
  340. return qtrue;
  341. }
  342. /*
  343. ================
  344. ParseEntities
  345. Parses the dentdata string into entities
  346. ================
  347. */
  348. void ParseEntities( void ) {
  349. num_entities = 0;
  350. ParseFromMemory( dentdata, entdatasize );
  351. while ( ParseEntity () ) {
  352. }
  353. }
  354. /*
  355. ================
  356. UnparseEntities
  357. Generates the dentdata string from all the entities
  358. This allows the utilities to add or remove key/value pairs
  359. to the data created by the map editor.
  360. ================
  361. */
  362. void UnparseEntities( void ) {
  363. char *buf, *end;
  364. epair_t *ep;
  365. char line[2048];
  366. int i;
  367. char key[1024], value[1024];
  368. buf = dentdata;
  369. end = buf;
  370. *end = 0;
  371. for (i=0 ; i<num_entities ; i++) {
  372. ep = entities[i].epairs;
  373. if ( !ep ) {
  374. continue; // ent got removed
  375. }
  376. strcat (end,"{\n");
  377. end += 2;
  378. for ( ep = entities[i].epairs ; ep ; ep=ep->next ) {
  379. strcpy (key, ep->key);
  380. StripTrailing (key);
  381. strcpy (value, ep->value);
  382. StripTrailing (value);
  383. sprintf (line, "\"%s\" \"%s\"\n", key, value);
  384. strcat (end, line);
  385. end += strlen(line);
  386. }
  387. strcat (end,"}\n");
  388. end += 2;
  389. if (end > buf + MAX_MAP_ENTSTRING) {
  390. Error ("Entity text too long");
  391. }
  392. }
  393. entdatasize = end - buf + 1;
  394. }
  395. void PrintEntity( const entity_t *ent ) {
  396. epair_t *ep;
  397. printf ("------- entity %p -------\n", ent);
  398. for (ep=ent->epairs ; ep ; ep=ep->next) {
  399. printf( "%s = %s\n", ep->key, ep->value );
  400. }
  401. }
  402. void SetKeyValue( entity_t *ent, const char *key, const char *value ) {
  403. epair_t *ep;
  404. for ( ep=ent->epairs ; ep ; ep=ep->next ) {
  405. if ( !strcmp (ep->key, key) ) {
  406. free (ep->value);
  407. ep->value = copystring(value);
  408. return;
  409. }
  410. }
  411. ep = malloc (sizeof(*ep));
  412. ep->next = ent->epairs;
  413. ent->epairs = ep;
  414. ep->key = copystring(key);
  415. ep->value = copystring(value);
  416. }
  417. const char *ValueForKey( const entity_t *ent, const char *key ) {
  418. epair_t *ep;
  419. for (ep=ent->epairs ; ep ; ep=ep->next) {
  420. if (!strcmp (ep->key, key) ) {
  421. return ep->value;
  422. }
  423. }
  424. return "";
  425. }
  426. vec_t FloatForKey( const entity_t *ent, const char *key ) {
  427. const char *k;
  428. k = ValueForKey( ent, key );
  429. return atof(k);
  430. }
  431. void GetVectorForKey( const entity_t *ent, const char *key, vec3_t vec ) {
  432. const char *k;
  433. double v1, v2, v3;
  434. k = ValueForKey (ent, key);
  435. // scanf into doubles, then assign, so it is vec_t size independent
  436. v1 = v2 = v3 = 0;
  437. sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);
  438. vec[0] = v1;
  439. vec[1] = v2;
  440. vec[2] = v3;
  441. }