Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

l_bsp_q1.c (17966B)


      1 /*
      2 ===========================================================================
      3 Copyright (C) 1999-2005 Id Software, Inc.
      4 
      5 This file is part of Quake III Arena source code.
      6 
      7 Quake III Arena source code is free software; you can redistribute it
      8 and/or modify it under the terms of the GNU General Public License as
      9 published by the Free Software Foundation; either version 2 of the License,
     10 or (at your option) any later version.
     11 
     12 Quake III Arena source code is distributed in the hope that it will be
     13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 GNU General Public License for more details.
     16 
     17 You should have received a copy of the GNU General Public License
     18 along with Foobar; if not, write to the Free Software
     19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20 ===========================================================================
     21 */
     22 
     23 #include "l_cmd.h"
     24 #include "l_math.h"
     25 #include "l_mem.h"
     26 #include "l_log.h"
     27 #include "../botlib/l_script.h"
     28 #include "l_bsp_q1.h"
     29 #include "l_bsp_ent.h"
     30 
     31 //=============================================================================
     32 
     33 int				q1_nummodels;
     34 q1_dmodel_t		*q1_dmodels;//[MAX_MAP_MODELS];
     35 
     36 int				q1_visdatasize;
     37 byte				*q1_dvisdata;//[MAX_MAP_VISIBILITY];
     38 
     39 int				q1_lightdatasize;
     40 byte				*q1_dlightdata;//[MAX_MAP_LIGHTING];
     41 
     42 int				q1_texdatasize;
     43 byte				*q1_dtexdata;//[MAX_MAP_MIPTEX]; // (dmiptexlump_t)
     44 
     45 int				q1_entdatasize;
     46 char				*q1_dentdata;//[MAX_MAP_ENTSTRING];
     47 
     48 int				q1_numleafs;
     49 q1_dleaf_t		*q1_dleafs;//[MAX_MAP_LEAFS];
     50 
     51 int				q1_numplanes;
     52 q1_dplane_t		*q1_dplanes;//[MAX_MAP_PLANES];
     53 
     54 int				q1_numvertexes;
     55 q1_dvertex_t	*q1_dvertexes;//[MAX_MAP_VERTS];
     56 
     57 int				q1_numnodes;
     58 q1_dnode_t		*q1_dnodes;//[MAX_MAP_NODES];
     59 
     60 int				q1_numtexinfo;
     61 q1_texinfo_t	*q1_texinfo;//[MAX_MAP_TEXINFO];
     62 
     63 int				q1_numfaces;
     64 q1_dface_t		*q1_dfaces;//[MAX_MAP_FACES];
     65 
     66 int				q1_numclipnodes;
     67 q1_dclipnode_t	*q1_dclipnodes;//[MAX_MAP_CLIPNODES];
     68 
     69 int				q1_numedges;
     70 q1_dedge_t		*q1_dedges;//[MAX_MAP_EDGES];
     71 
     72 int				q1_nummarksurfaces;
     73 unsigned short	*q1_dmarksurfaces;//[MAX_MAP_MARKSURFACES];
     74 
     75 int				q1_numsurfedges;
     76 int				*q1_dsurfedges;//[MAX_MAP_SURFEDGES];
     77 
     78 //=============================================================================
     79 
     80 int q1_bspallocated = false;
     81 int q1_allocatedbspmem = 0;
     82 
     83 void Q1_AllocMaxBSP(void)
     84 {
     85 	//models
     86 	q1_nummodels = 0;
     87 	q1_dmodels = (q1_dmodel_t *) GetMemory(Q1_MAX_MAP_MODELS * sizeof(q1_dmodel_t));
     88 	q1_allocatedbspmem = Q1_MAX_MAP_MODELS * sizeof(q1_dmodel_t);
     89 	//visibility
     90 	q1_visdatasize = 0;
     91 	q1_dvisdata = (byte *) GetMemory(Q1_MAX_MAP_VISIBILITY * sizeof(byte));
     92 	q1_allocatedbspmem += Q1_MAX_MAP_VISIBILITY * sizeof(byte);
     93 	//light data
     94 	q1_lightdatasize = 0;
     95 	q1_dlightdata = (byte *) GetMemory(Q1_MAX_MAP_LIGHTING * sizeof(byte));
     96 	q1_allocatedbspmem += Q1_MAX_MAP_LIGHTING * sizeof(byte);
     97 	//texture data
     98 	q1_texdatasize = 0;
     99 	q1_dtexdata = (byte *) GetMemory(Q1_MAX_MAP_MIPTEX * sizeof(byte)); // (dmiptexlump_t)
    100 	q1_allocatedbspmem += Q1_MAX_MAP_MIPTEX * sizeof(byte);
    101 	//entities
    102 	q1_entdatasize = 0;
    103 	q1_dentdata = (char *) GetMemory(Q1_MAX_MAP_ENTSTRING * sizeof(char));
    104 	q1_allocatedbspmem += Q1_MAX_MAP_ENTSTRING * sizeof(char);
    105 	//leaves
    106 	q1_numleafs = 0;
    107 	q1_dleafs = (q1_dleaf_t *) GetMemory(Q1_MAX_MAP_LEAFS * sizeof(q1_dleaf_t));
    108 	q1_allocatedbspmem += Q1_MAX_MAP_LEAFS * sizeof(q1_dleaf_t);
    109 	//planes
    110 	q1_numplanes = 0;
    111 	q1_dplanes = (q1_dplane_t *) GetMemory(Q1_MAX_MAP_PLANES * sizeof(q1_dplane_t));
    112 	q1_allocatedbspmem += Q1_MAX_MAP_PLANES * sizeof(q1_dplane_t);
    113 	//vertexes
    114 	q1_numvertexes = 0;
    115 	q1_dvertexes = (q1_dvertex_t *) GetMemory(Q1_MAX_MAP_VERTS * sizeof(q1_dvertex_t));
    116 	q1_allocatedbspmem += Q1_MAX_MAP_VERTS * sizeof(q1_dvertex_t);
    117 	//nodes
    118 	q1_numnodes = 0;
    119 	q1_dnodes = (q1_dnode_t *) GetMemory(Q1_MAX_MAP_NODES * sizeof(q1_dnode_t));
    120 	q1_allocatedbspmem += Q1_MAX_MAP_NODES * sizeof(q1_dnode_t);
    121 	//texture info
    122 	q1_numtexinfo = 0;
    123 	q1_texinfo = (q1_texinfo_t *) GetMemory(Q1_MAX_MAP_TEXINFO * sizeof(q1_texinfo_t));
    124 	q1_allocatedbspmem += Q1_MAX_MAP_TEXINFO * sizeof(q1_texinfo_t);
    125 	//faces
    126 	q1_numfaces = 0;
    127 	q1_dfaces = (q1_dface_t *) GetMemory(Q1_MAX_MAP_FACES * sizeof(q1_dface_t));
    128 	q1_allocatedbspmem += Q1_MAX_MAP_FACES * sizeof(q1_dface_t);
    129 	//clip nodes
    130 	q1_numclipnodes = 0;
    131 	q1_dclipnodes = (q1_dclipnode_t *) GetMemory(Q1_MAX_MAP_CLIPNODES * sizeof(q1_dclipnode_t));
    132 	q1_allocatedbspmem += Q1_MAX_MAP_CLIPNODES * sizeof(q1_dclipnode_t);
    133 	//edges
    134 	q1_numedges = 0;
    135 	q1_dedges = (q1_dedge_t *) GetMemory(Q1_MAX_MAP_EDGES * sizeof(q1_dedge_t));
    136 	q1_allocatedbspmem += Q1_MAX_MAP_EDGES, sizeof(q1_dedge_t);
    137 	//mark surfaces
    138 	q1_nummarksurfaces = 0;
    139 	q1_dmarksurfaces = (unsigned short *) GetMemory(Q1_MAX_MAP_MARKSURFACES * sizeof(unsigned short));
    140 	q1_allocatedbspmem += Q1_MAX_MAP_MARKSURFACES * sizeof(unsigned short);
    141 	//surface edges
    142 	q1_numsurfedges = 0;
    143 	q1_dsurfedges = (int *) GetMemory(Q1_MAX_MAP_SURFEDGES * sizeof(int));
    144 	q1_allocatedbspmem += Q1_MAX_MAP_SURFEDGES * sizeof(int);
    145 	//print allocated memory
    146 	Log_Print("allocated ");
    147 	PrintMemorySize(q1_allocatedbspmem);
    148 	Log_Print(" of BSP memory\n");
    149 } //end of the function Q1_AllocMaxBSP
    150 
    151 void Q1_FreeMaxBSP(void)
    152 {
    153 	//models
    154 	q1_nummodels = 0;
    155 	FreeMemory(q1_dmodels);
    156 	q1_dmodels = NULL;
    157 	//visibility
    158 	q1_visdatasize = 0;
    159 	FreeMemory(q1_dvisdata);
    160 	q1_dvisdata = NULL;
    161 	//light data
    162 	q1_lightdatasize = 0;
    163 	FreeMemory(q1_dlightdata);
    164 	q1_dlightdata = NULL;
    165 	//texture data
    166 	q1_texdatasize = 0;
    167 	FreeMemory(q1_dtexdata);
    168 	q1_dtexdata = NULL;
    169 	//entities
    170 	q1_entdatasize = 0;
    171 	FreeMemory(q1_dentdata);
    172 	q1_dentdata = NULL;
    173 	//leaves
    174 	q1_numleafs = 0;
    175 	FreeMemory(q1_dleafs);
    176 	q1_dleafs = NULL;
    177 	//planes
    178 	q1_numplanes = 0;
    179 	FreeMemory(q1_dplanes);
    180 	q1_dplanes = NULL;
    181 	//vertexes
    182 	q1_numvertexes = 0;
    183 	FreeMemory(q1_dvertexes);
    184 	q1_dvertexes = NULL;
    185 	//nodes
    186 	q1_numnodes = 0;
    187 	FreeMemory(q1_dnodes);
    188 	q1_dnodes = NULL;
    189 	//texture info
    190 	q1_numtexinfo = 0;
    191 	FreeMemory(q1_texinfo);
    192 	q1_texinfo = NULL;
    193 	//faces
    194 	q1_numfaces = 0;
    195 	FreeMemory(q1_dfaces);
    196 	q1_dfaces = NULL;
    197 	//clip nodes
    198 	q1_numclipnodes = 0;
    199 	FreeMemory(q1_dclipnodes);
    200 	q1_dclipnodes = NULL;
    201 	//edges
    202 	q1_numedges = 0;
    203 	FreeMemory(q1_dedges);
    204 	q1_dedges = NULL;
    205 	//mark surfaces
    206 	q1_nummarksurfaces = 0;
    207 	FreeMemory(q1_dmarksurfaces);
    208 	q1_dmarksurfaces = NULL;
    209 	//surface edges
    210 	q1_numsurfedges = 0;
    211 	FreeMemory(q1_dsurfedges);
    212 	q1_dsurfedges = NULL;
    213 	//
    214 	Log_Print("freed ");
    215 	PrintMemorySize(q1_allocatedbspmem);
    216 	Log_Print(" of BSP memory\n");
    217 	q1_allocatedbspmem = 0;
    218 } //end of the function Q1_FreeMaxBSP
    219 //#endif //ME
    220 
    221 /*
    222 =============
    223 Q1_SwapBSPFile
    224 
    225 Byte swaps all data in a bsp file.
    226 =============
    227 */
    228 void Q1_SwapBSPFile (qboolean todisk)
    229 {
    230 	int i, j, c;
    231 	q1_dmodel_t *d;
    232 	q1_dmiptexlump_t *mtl;
    233 
    234 	
    235 // models	
    236 	for (i=0 ; i<q1_nummodels ; i++)
    237 	{
    238 		d = &q1_dmodels[i];
    239 
    240 		for (j=0 ; j<Q1_MAX_MAP_HULLS ; j++)
    241 			d->headnode[j] = LittleLong (d->headnode[j]);
    242 
    243 		d->visleafs = LittleLong (d->visleafs);
    244 		d->firstface = LittleLong (d->firstface);
    245 		d->numfaces = LittleLong (d->numfaces);
    246 		
    247 		for (j=0 ; j<3 ; j++)
    248 		{
    249 			d->mins[j] = LittleFloat(d->mins[j]);
    250 			d->maxs[j] = LittleFloat(d->maxs[j]);
    251 			d->origin[j] = LittleFloat(d->origin[j]);
    252 		}
    253 	}
    254 
    255 //
    256 // vertexes
    257 //
    258 	for (i=0 ; i<q1_numvertexes ; i++)
    259 	{
    260 		for (j=0 ; j<3 ; j++)
    261 			q1_dvertexes[i].point[j] = LittleFloat(q1_dvertexes[i].point[j]);
    262 	}
    263 		
    264 //
    265 // planes
    266 //	
    267 	for (i=0 ; i<q1_numplanes ; i++)
    268 	{
    269 		for (j=0 ; j<3 ; j++)
    270 			q1_dplanes[i].normal[j] = LittleFloat(q1_dplanes[i].normal[j]);
    271 		q1_dplanes[i].dist = LittleFloat(q1_dplanes[i].dist);
    272 		q1_dplanes[i].type = LittleLong(q1_dplanes[i].type);
    273 	}
    274 	
    275 //
    276 // texinfos
    277 //	
    278 	for (i=0 ; i<q1_numtexinfo ; i++)
    279 	{
    280 		for (j=0 ; j<8 ; j++)
    281 			q1_texinfo[i].vecs[0][j] = LittleFloat(q1_texinfo[i].vecs[0][j]);
    282 		q1_texinfo[i].miptex = LittleLong(q1_texinfo[i].miptex);
    283 		q1_texinfo[i].flags = LittleLong(q1_texinfo[i].flags);
    284 	}
    285 	
    286 //
    287 // faces
    288 //
    289 	for (i=0 ; i<q1_numfaces ; i++)
    290 	{
    291 		q1_dfaces[i].texinfo = LittleShort(q1_dfaces[i].texinfo);
    292 		q1_dfaces[i].planenum = LittleShort(q1_dfaces[i].planenum);
    293 		q1_dfaces[i].side = LittleShort(q1_dfaces[i].side);
    294 		q1_dfaces[i].lightofs = LittleLong(q1_dfaces[i].lightofs);
    295 		q1_dfaces[i].firstedge = LittleLong(q1_dfaces[i].firstedge);
    296 		q1_dfaces[i].numedges = LittleShort(q1_dfaces[i].numedges);
    297 	}
    298 
    299 //
    300 // nodes
    301 //
    302 	for (i=0 ; i<q1_numnodes ; i++)
    303 	{
    304 		q1_dnodes[i].planenum = LittleLong(q1_dnodes[i].planenum);
    305 		for (j=0 ; j<3 ; j++)
    306 		{
    307 			q1_dnodes[i].mins[j] = LittleShort(q1_dnodes[i].mins[j]);
    308 			q1_dnodes[i].maxs[j] = LittleShort(q1_dnodes[i].maxs[j]);
    309 		}
    310 		q1_dnodes[i].children[0] = LittleShort(q1_dnodes[i].children[0]);
    311 		q1_dnodes[i].children[1] = LittleShort(q1_dnodes[i].children[1]);
    312 		q1_dnodes[i].firstface = LittleShort(q1_dnodes[i].firstface);
    313 		q1_dnodes[i].numfaces = LittleShort(q1_dnodes[i].numfaces);
    314 	}
    315 
    316 //
    317 // leafs
    318 //
    319 	for (i=0 ; i<q1_numleafs ; i++)
    320 	{
    321 		q1_dleafs[i].contents = LittleLong(q1_dleafs[i].contents);
    322 		for (j=0 ; j<3 ; j++)
    323 		{
    324 			q1_dleafs[i].mins[j] = LittleShort(q1_dleafs[i].mins[j]);
    325 			q1_dleafs[i].maxs[j] = LittleShort(q1_dleafs[i].maxs[j]);
    326 		}
    327 
    328 		q1_dleafs[i].firstmarksurface = LittleShort(q1_dleafs[i].firstmarksurface);
    329 		q1_dleafs[i].nummarksurfaces = LittleShort(q1_dleafs[i].nummarksurfaces);
    330 		q1_dleafs[i].visofs = LittleLong(q1_dleafs[i].visofs);
    331 	}
    332 
    333 //
    334 // clipnodes
    335 //
    336 	for (i=0 ; i<q1_numclipnodes ; i++)
    337 	{
    338 		q1_dclipnodes[i].planenum = LittleLong(q1_dclipnodes[i].planenum);
    339 		q1_dclipnodes[i].children[0] = LittleShort(q1_dclipnodes[i].children[0]);
    340 		q1_dclipnodes[i].children[1] = LittleShort(q1_dclipnodes[i].children[1]);
    341 	}
    342 
    343 //
    344 // miptex
    345 //
    346 	if (q1_texdatasize)
    347 	{
    348 		mtl = (q1_dmiptexlump_t *)q1_dtexdata;
    349 		if (todisk)
    350 			c = mtl->nummiptex;
    351 		else
    352 			c = LittleLong(mtl->nummiptex);
    353 		mtl->nummiptex = LittleLong (mtl->nummiptex);
    354 		for (i=0 ; i<c ; i++)
    355 			mtl->dataofs[i] = LittleLong(mtl->dataofs[i]);
    356 	}
    357 	
    358 //
    359 // marksurfaces
    360 //
    361 	for (i=0 ; i<q1_nummarksurfaces ; i++)
    362 		q1_dmarksurfaces[i] = LittleShort(q1_dmarksurfaces[i]);
    363 
    364 //
    365 // surfedges
    366 //
    367 	for (i=0 ; i<q1_numsurfedges ; i++)
    368 		q1_dsurfedges[i] = LittleLong(q1_dsurfedges[i]);
    369 
    370 //
    371 // edges
    372 //
    373 	for (i=0 ; i<q1_numedges ; i++)
    374 	{
    375 		q1_dedges[i].v[0] = LittleShort(q1_dedges[i].v[0]);
    376 		q1_dedges[i].v[1] = LittleShort(q1_dedges[i].v[1]);
    377 	}
    378 }
    379 
    380 
    381 q1_dheader_t *q1_header;
    382 int			q1_fileLength;
    383 
    384 int Q1_CopyLump (int lump, void *dest, int size, int maxsize)
    385 {
    386 	int		length, ofs;
    387 
    388 	length = q1_header->lumps[lump].filelen;
    389 	ofs = q1_header->lumps[lump].fileofs;
    390 	
    391 	if (length % size) {
    392 		Error ("LoadBSPFile: odd lump size");
    393 	}
    394 	// somehow things got out of range
    395 	if ((length/size) > maxsize) {
    396 		printf("WARNING: exceeded max size for lump %d size %d > maxsize %d\n", lump, (length/size), maxsize);
    397 		length = maxsize * size;
    398 	}
    399 	if ( ofs + length > q1_fileLength ) {
    400 		printf("WARNING: exceeded file length for lump %d\n", lump);
    401 		length = q1_fileLength - ofs;
    402 		if ( length <= 0 ) {
    403 			return 0;
    404 		}
    405 	}
    406 
    407 	memcpy (dest, (byte *)q1_header + ofs, length);
    408 
    409 	return length / size;
    410 }
    411 
    412 /*
    413 =============
    414 Q1_LoadBSPFile
    415 =============
    416 */
    417 void	Q1_LoadBSPFile(char *filename, int offset, int length)
    418 {
    419 	int			i;
    420 	
    421 //
    422 // load the file header
    423 //
    424 	q1_fileLength = LoadFile(filename, (void **)&q1_header, offset, length);
    425 
    426 // swap the header
    427 	for (i=0 ; i< sizeof(q1_dheader_t)/4 ; i++)
    428 		((int *)q1_header)[i] = LittleLong ( ((int *)q1_header)[i]);
    429 
    430 	if (q1_header->version != Q1_BSPVERSION)
    431 		Error ("%s is version %i, not %i", filename, i, Q1_BSPVERSION);
    432 
    433 	q1_nummodels = Q1_CopyLump (Q1_LUMP_MODELS, q1_dmodels, sizeof(q1_dmodel_t), Q1_MAX_MAP_MODELS );
    434 	q1_numvertexes = Q1_CopyLump (Q1_LUMP_VERTEXES, q1_dvertexes, sizeof(q1_dvertex_t), Q1_MAX_MAP_VERTS );
    435 	q1_numplanes = Q1_CopyLump (Q1_LUMP_PLANES, q1_dplanes, sizeof(q1_dplane_t), Q1_MAX_MAP_PLANES );
    436 	q1_numleafs = Q1_CopyLump (Q1_LUMP_LEAFS, q1_dleafs, sizeof(q1_dleaf_t), Q1_MAX_MAP_LEAFS );
    437 	q1_numnodes = Q1_CopyLump (Q1_LUMP_NODES, q1_dnodes, sizeof(q1_dnode_t), Q1_MAX_MAP_NODES );
    438 	q1_numtexinfo = Q1_CopyLump (Q1_LUMP_TEXINFO, q1_texinfo, sizeof(q1_texinfo_t), Q1_MAX_MAP_TEXINFO );
    439 	q1_numclipnodes = Q1_CopyLump (Q1_LUMP_CLIPNODES, q1_dclipnodes, sizeof(q1_dclipnode_t), Q1_MAX_MAP_CLIPNODES );
    440 	q1_numfaces = Q1_CopyLump (Q1_LUMP_FACES, q1_dfaces, sizeof(q1_dface_t), Q1_MAX_MAP_FACES );
    441 	q1_nummarksurfaces = Q1_CopyLump (Q1_LUMP_MARKSURFACES, q1_dmarksurfaces, sizeof(q1_dmarksurfaces[0]), Q1_MAX_MAP_MARKSURFACES );
    442 	q1_numsurfedges = Q1_CopyLump (Q1_LUMP_SURFEDGES, q1_dsurfedges, sizeof(q1_dsurfedges[0]), Q1_MAX_MAP_SURFEDGES );
    443 	q1_numedges = Q1_CopyLump (Q1_LUMP_EDGES, q1_dedges, sizeof(q1_dedge_t), Q1_MAX_MAP_EDGES );
    444 
    445 	q1_texdatasize = Q1_CopyLump (Q1_LUMP_TEXTURES, q1_dtexdata, 1, Q1_MAX_MAP_MIPTEX );
    446 	q1_visdatasize = Q1_CopyLump (Q1_LUMP_VISIBILITY, q1_dvisdata, 1, Q1_MAX_MAP_VISIBILITY );
    447 	q1_lightdatasize = Q1_CopyLump (Q1_LUMP_LIGHTING, q1_dlightdata, 1, Q1_MAX_MAP_LIGHTING );
    448 	q1_entdatasize = Q1_CopyLump (Q1_LUMP_ENTITIES, q1_dentdata, 1, Q1_MAX_MAP_ENTSTRING );
    449 
    450 	FreeMemory(q1_header);		// everything has been copied out
    451 		
    452 //
    453 // swap everything
    454 //	
    455 	Q1_SwapBSPFile (false);
    456 }
    457 
    458 //============================================================================
    459 
    460 FILE *q1_wadfile;
    461 q1_dheader_t q1_outheader;
    462 
    463 void Q1_AddLump (int lumpnum, void *data, int len)
    464 {
    465 	q1_lump_t *lump;
    466 
    467 	lump = &q1_header->lumps[lumpnum];
    468 	
    469 	lump->fileofs = LittleLong(ftell(q1_wadfile));
    470 	lump->filelen = LittleLong(len);
    471 	SafeWrite(q1_wadfile, data, (len+3)&~3);
    472 }
    473 
    474 /*
    475 =============
    476 Q1_WriteBSPFile
    477 
    478 Swaps the bsp file in place, so it should not be referenced again
    479 =============
    480 */
    481 void	Q1_WriteBSPFile (char *filename)
    482 {		
    483 	q1_header = &q1_outheader;
    484 	memset (q1_header, 0, sizeof(q1_dheader_t));
    485 	
    486 	Q1_SwapBSPFile (true);
    487 
    488 	q1_header->version = LittleLong (Q1_BSPVERSION);
    489 	
    490 	q1_wadfile = SafeOpenWrite (filename);
    491 	SafeWrite (q1_wadfile, q1_header, sizeof(q1_dheader_t));	// overwritten later
    492 
    493 	Q1_AddLump (Q1_LUMP_PLANES, q1_dplanes, q1_numplanes*sizeof(q1_dplane_t));
    494 	Q1_AddLump (Q1_LUMP_LEAFS, q1_dleafs, q1_numleafs*sizeof(q1_dleaf_t));
    495 	Q1_AddLump (Q1_LUMP_VERTEXES, q1_dvertexes, q1_numvertexes*sizeof(q1_dvertex_t));
    496 	Q1_AddLump (Q1_LUMP_NODES, q1_dnodes, q1_numnodes*sizeof(q1_dnode_t));
    497 	Q1_AddLump (Q1_LUMP_TEXINFO, q1_texinfo, q1_numtexinfo*sizeof(q1_texinfo_t));
    498 	Q1_AddLump (Q1_LUMP_FACES, q1_dfaces, q1_numfaces*sizeof(q1_dface_t));
    499 	Q1_AddLump (Q1_LUMP_CLIPNODES, q1_dclipnodes, q1_numclipnodes*sizeof(q1_dclipnode_t));
    500 	Q1_AddLump (Q1_LUMP_MARKSURFACES, q1_dmarksurfaces, q1_nummarksurfaces*sizeof(q1_dmarksurfaces[0]));
    501 	Q1_AddLump (Q1_LUMP_SURFEDGES, q1_dsurfedges, q1_numsurfedges*sizeof(q1_dsurfedges[0]));
    502 	Q1_AddLump (Q1_LUMP_EDGES, q1_dedges, q1_numedges*sizeof(q1_dedge_t));
    503 	Q1_AddLump (Q1_LUMP_MODELS, q1_dmodels, q1_nummodels*sizeof(q1_dmodel_t));
    504 
    505 	Q1_AddLump (Q1_LUMP_LIGHTING, q1_dlightdata, q1_lightdatasize);
    506 	Q1_AddLump (Q1_LUMP_VISIBILITY, q1_dvisdata, q1_visdatasize);
    507 	Q1_AddLump (Q1_LUMP_ENTITIES, q1_dentdata, q1_entdatasize);
    508 	Q1_AddLump (Q1_LUMP_TEXTURES, q1_dtexdata, q1_texdatasize);
    509 	
    510 	fseek (q1_wadfile, 0, SEEK_SET);
    511 	SafeWrite (q1_wadfile, q1_header, sizeof(q1_dheader_t));
    512 	fclose (q1_wadfile);	
    513 }
    514 
    515 //============================================================================
    516 
    517 /*
    518 =============
    519 Q1_PrintBSPFileSizes
    520 
    521 Dumps info about current file
    522 =============
    523 */
    524 void Q1_PrintBSPFileSizes (void)
    525 {
    526 	printf ("%5i planes       %6i\n"
    527 		,q1_numplanes, (int)(q1_numplanes*sizeof(q1_dplane_t)));
    528 	printf ("%5i vertexes     %6i\n"
    529 		,q1_numvertexes, (int)(q1_numvertexes*sizeof(q1_dvertex_t)));
    530 	printf ("%5i nodes        %6i\n"
    531 		,q1_numnodes, (int)(q1_numnodes*sizeof(q1_dnode_t)));
    532 	printf ("%5i texinfo      %6i\n"
    533 		,q1_numtexinfo, (int)(q1_numtexinfo*sizeof(q1_texinfo_t)));
    534 	printf ("%5i faces        %6i\n"
    535 		,q1_numfaces, (int)(q1_numfaces*sizeof(q1_dface_t)));
    536 	printf ("%5i clipnodes    %6i\n"
    537 		,q1_numclipnodes, (int)(q1_numclipnodes*sizeof(q1_dclipnode_t)));
    538 	printf ("%5i leafs        %6i\n"
    539 		,q1_numleafs, (int)(q1_numleafs*sizeof(q1_dleaf_t)));
    540 	printf ("%5i marksurfaces %6i\n"
    541 		,q1_nummarksurfaces, (int)(q1_nummarksurfaces*sizeof(q1_dmarksurfaces[0])));
    542 	printf ("%5i surfedges    %6i\n"
    543 		,q1_numsurfedges, (int)(q1_numsurfedges*sizeof(q1_dmarksurfaces[0])));
    544 	printf ("%5i edges        %6i\n"
    545 		,q1_numedges, (int)(q1_numedges*sizeof(q1_dedge_t)));
    546 	if (!q1_texdatasize)
    547 		printf ("    0 textures          0\n");
    548 	else
    549 		printf ("%5i textures     %6i\n",((q1_dmiptexlump_t*)q1_dtexdata)->nummiptex, q1_texdatasize);
    550 	printf ("      lightdata    %6i\n", q1_lightdatasize);
    551 	printf ("      visdata      %6i\n", q1_visdatasize);
    552 	printf ("      entdata      %6i\n", q1_entdatasize);
    553 } //end of the function Q1_PrintBSPFileSizes
    554 
    555 
    556 /*
    557 ================
    558 Q1_ParseEntities
    559 
    560 Parses the dentdata string into entities
    561 ================
    562 */
    563 void Q1_ParseEntities (void)
    564 {
    565 	script_t *script;
    566 
    567 	num_entities = 0;
    568 	script = LoadScriptMemory(q1_dentdata, q1_entdatasize, "*Quake1 bsp file");
    569 	SetScriptFlags(script, SCFL_NOSTRINGWHITESPACES |
    570 									SCFL_NOSTRINGESCAPECHARS);
    571 
    572 	while(ParseEntity(script))
    573 	{
    574 	} //end while
    575 
    576 	FreeScript(script);
    577 } //end of the function Q1_ParseEntities
    578 
    579 
    580 /*
    581 ================
    582 Q1_UnparseEntities
    583 
    584 Generates the dentdata string from all the entities
    585 ================
    586 */
    587 void Q1_UnparseEntities (void)
    588 {
    589 	char *buf, *end;
    590 	epair_t *ep;
    591 	char line[2048];
    592 	int i;
    593 	
    594 	buf = q1_dentdata;
    595 	end = buf;
    596 	*end = 0;
    597 	
    598 	for (i=0 ; i<num_entities ; i++)
    599 	{
    600 		ep = entities[i].epairs;
    601 		if (!ep)
    602 			continue;	// ent got removed
    603 		
    604 		strcat (end,"{\n");
    605 		end += 2;
    606 				
    607 		for (ep = entities[i].epairs ; ep ; ep=ep->next)
    608 		{
    609 			sprintf (line, "\"%s\" \"%s\"\n", ep->key, ep->value);
    610 			strcat (end, line);
    611 			end += strlen(line);
    612 		}
    613 		strcat (end,"}\n");
    614 		end += 2;
    615 
    616 		if (end > buf + Q1_MAX_MAP_ENTSTRING)
    617 			Error ("Entity text too long");
    618 	}
    619 	q1_entdatasize = end - buf + 1;
    620 } //end of the function Q1_UnparseEntities