2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
7 Quake 2 Tools 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.
12 Quake 2 Tools 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.
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
28 void GetLeafNums (void);
30 //=============================================================================
33 dmodel_t dmodels[MAX_MAP_MODELS];
36 byte dvisdata[MAX_MAP_VISIBILITY];
37 dvis_t *dvis = (dvis_t *)dvisdata;
40 byte dlightdata[MAX_MAP_LIGHTING];
43 char dentdata[MAX_MAP_ENTSTRING];
46 dleaf_t dleafs[MAX_MAP_LEAFS];
49 dplane_t dplanes[MAX_MAP_PLANES];
52 dvertex_t dvertexes[MAX_MAP_VERTS];
55 dnode_t dnodes[MAX_MAP_NODES];
58 texinfo_t texinfo[MAX_MAP_TEXINFO];
61 dface_t dfaces[MAX_MAP_FACES];
64 dedge_t dedges[MAX_MAP_EDGES];
67 unsigned short dleaffaces[MAX_MAP_LEAFFACES];
70 unsigned short dleafbrushes[MAX_MAP_LEAFBRUSHES];
73 int dsurfedges[MAX_MAP_SURFEDGES];
76 dbrush_t dbrushes[MAX_MAP_BRUSHES];
79 dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
82 darea_t dareas[MAX_MAP_AREAS];
85 dareaportal_t dareaportals[MAX_MAP_AREAPORTALS];
95 int CompressVis (byte *vis, byte *dest)
103 // visrow = (r_numvisleafs + 7)>>3;
104 visrow = (dvis->numclusters + 7)>>3;
106 for (j=0 ; j<visrow ; j++)
113 for ( j++; j<visrow ; j++)
114 if (vis[j] || rep == 255)
122 return dest_p - dest;
131 void DecompressVis (byte *in, byte *decompressed)
137 // row = (r_numvisleafs+7)>>3;
138 row = (dvis->numclusters+7)>>3;
151 Error ("DecompressVis: 0 repeat");
158 } while (out - decompressed < row);
161 //=============================================================================
167 Byte swaps all data in a bsp file.
170 void SwapBSPFile (qboolean todisk)
177 for (i=0 ; i<nummodels ; i++)
181 d->firstface = LittleLong (d->firstface);
182 d->numfaces = LittleLong (d->numfaces);
183 d->headnode = LittleLong (d->headnode);
185 for (j=0 ; j<3 ; j++)
187 d->mins[j] = LittleFloat(d->mins[j]);
188 d->maxs[j] = LittleFloat(d->maxs[j]);
189 d->origin[j] = LittleFloat(d->origin[j]);
196 for (i=0 ; i<numvertexes ; i++)
198 for (j=0 ; j<3 ; j++)
199 dvertexes[i].point[j] = LittleFloat (dvertexes[i].point[j]);
205 for (i=0 ; i<numplanes ; i++)
207 for (j=0 ; j<3 ; j++)
208 dplanes[i].normal[j] = LittleFloat (dplanes[i].normal[j]);
209 dplanes[i].dist = LittleFloat (dplanes[i].dist);
210 dplanes[i].type = LittleLong (dplanes[i].type);
216 for (i=0 ; i<numtexinfo ; i++)
218 for (j=0 ; j<8 ; j++)
219 texinfo[i].vecs[0][j] = LittleFloat (texinfo[i].vecs[0][j]);
220 texinfo[i].flags = LittleLong (texinfo[i].flags);
221 texinfo[i].value = LittleLong (texinfo[i].value);
222 texinfo[i].nexttexinfo = LittleLong (texinfo[i].nexttexinfo);
228 for (i=0 ; i<numfaces ; i++)
230 dfaces[i].texinfo = LittleShort (dfaces[i].texinfo);
231 dfaces[i].planenum = LittleShort (dfaces[i].planenum);
232 dfaces[i].side = LittleShort (dfaces[i].side);
233 dfaces[i].lightofs = LittleLong (dfaces[i].lightofs);
234 dfaces[i].firstedge = LittleLong (dfaces[i].firstedge);
235 dfaces[i].numedges = LittleShort (dfaces[i].numedges);
241 for (i=0 ; i<numnodes ; i++)
243 dnodes[i].planenum = LittleLong (dnodes[i].planenum);
244 for (j=0 ; j<3 ; j++)
246 dnodes[i].mins[j] = LittleShort (dnodes[i].mins[j]);
247 dnodes[i].maxs[j] = LittleShort (dnodes[i].maxs[j]);
249 dnodes[i].children[0] = LittleLong (dnodes[i].children[0]);
250 dnodes[i].children[1] = LittleLong (dnodes[i].children[1]);
251 dnodes[i].firstface = LittleShort (dnodes[i].firstface);
252 dnodes[i].numfaces = LittleShort (dnodes[i].numfaces);
258 for (i=0 ; i<numleafs ; i++)
260 dleafs[i].contents = LittleLong (dleafs[i].contents);
261 dleafs[i].cluster = LittleShort (dleafs[i].cluster);
262 dleafs[i].area = LittleShort (dleafs[i].area);
263 for (j=0 ; j<3 ; j++)
265 dleafs[i].mins[j] = LittleShort (dleafs[i].mins[j]);
266 dleafs[i].maxs[j] = LittleShort (dleafs[i].maxs[j]);
269 dleafs[i].firstleafface = LittleShort (dleafs[i].firstleafface);
270 dleafs[i].numleaffaces = LittleShort (dleafs[i].numleaffaces);
271 dleafs[i].firstleafbrush = LittleShort (dleafs[i].firstleafbrush);
272 dleafs[i].numleafbrushes = LittleShort (dleafs[i].numleafbrushes);
278 for (i=0 ; i<numleaffaces ; i++)
279 dleaffaces[i] = LittleShort (dleaffaces[i]);
284 for (i=0 ; i<numleafbrushes ; i++)
285 dleafbrushes[i] = LittleShort (dleafbrushes[i]);
290 for (i=0 ; i<numsurfedges ; i++)
291 dsurfedges[i] = LittleLong (dsurfedges[i]);
296 for (i=0 ; i<numedges ; i++)
298 dedges[i].v[0] = LittleShort (dedges[i].v[0]);
299 dedges[i].v[1] = LittleShort (dedges[i].v[1]);
305 for (i=0 ; i<numbrushes ; i++)
307 dbrushes[i].firstside = LittleLong (dbrushes[i].firstside);
308 dbrushes[i].numsides = LittleLong (dbrushes[i].numsides);
309 dbrushes[i].contents = LittleLong (dbrushes[i].contents);
315 for (i=0 ; i<numareas ; i++)
317 dareas[i].numareaportals = LittleLong (dareas[i].numareaportals);
318 dareas[i].firstareaportal = LittleLong (dareas[i].firstareaportal);
324 for (i=0 ; i<numareaportals ; i++)
326 dareaportals[i].portalnum = LittleLong (dareaportals[i].portalnum);
327 dareaportals[i].otherarea = LittleLong (dareaportals[i].otherarea);
333 for (i=0 ; i<numbrushsides ; i++)
335 dbrushsides[i].planenum = LittleShort (dbrushsides[i].planenum);
336 dbrushsides[i].texinfo = LittleShort (dbrushsides[i].texinfo);
343 j = dvis->numclusters;
345 j = LittleLong(dvis->numclusters);
346 dvis->numclusters = LittleLong (dvis->numclusters);
347 for (i=0 ; i<j ; i++)
349 dvis->bitofs[i][0] = LittleLong (dvis->bitofs[i][0]);
350 dvis->bitofs[i][1] = LittleLong (dvis->bitofs[i][1]);
357 int CopyLump (int lump, void *dest, int size)
361 length = header->lumps[lump].filelen;
362 ofs = header->lumps[lump].fileofs;
365 Error ("LoadBSPFile: odd lump size");
367 memcpy (dest, (byte *)header + ofs, length);
369 return length / size;
377 void LoadBSPFile (char *filename)
382 // load the file header
384 LoadFile (filename, (void **)&header);
387 for (i=0 ; i< sizeof(dheader_t)/4 ; i++)
388 ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
390 if (header->ident != IDBSPHEADER)
391 Error ("%s is not a IBSP file", filename);
392 if (header->version != BSPVERSION)
393 Error ("%s is version %i, not %i", filename, header->version, BSPVERSION);
395 nummodels = CopyLump (LUMP_MODELS, dmodels, sizeof(dmodel_t));
396 numvertexes = CopyLump (LUMP_VERTEXES, dvertexes, sizeof(dvertex_t));
397 numplanes = CopyLump (LUMP_PLANES, dplanes, sizeof(dplane_t));
398 numleafs = CopyLump (LUMP_LEAFS, dleafs, sizeof(dleaf_t));
399 numnodes = CopyLump (LUMP_NODES, dnodes, sizeof(dnode_t));
400 numtexinfo = CopyLump (LUMP_TEXINFO, texinfo, sizeof(texinfo_t));
401 numfaces = CopyLump (LUMP_FACES, dfaces, sizeof(dface_t));
402 numleaffaces = CopyLump (LUMP_LEAFFACES, dleaffaces, sizeof(dleaffaces[0]));
403 numleafbrushes = CopyLump (LUMP_LEAFBRUSHES, dleafbrushes, sizeof(dleafbrushes[0]));
404 numsurfedges = CopyLump (LUMP_SURFEDGES, dsurfedges, sizeof(dsurfedges[0]));
405 numedges = CopyLump (LUMP_EDGES, dedges, sizeof(dedge_t));
406 numbrushes = CopyLump (LUMP_BRUSHES, dbrushes, sizeof(dbrush_t));
407 numbrushsides = CopyLump (LUMP_BRUSHSIDES, dbrushsides, sizeof(dbrushside_t));
408 numareas = CopyLump (LUMP_AREAS, dareas, sizeof(darea_t));
409 numareaportals = CopyLump (LUMP_AREAPORTALS, dareaportals, sizeof(dareaportal_t));
411 visdatasize = CopyLump (LUMP_VISIBILITY, dvisdata, 1);
412 lightdatasize = CopyLump (LUMP_LIGHTING, dlightdata, 1);
413 entdatasize = CopyLump (LUMP_ENTITIES, dentdata, 1);
415 CopyLump (LUMP_POP, dpop, 1);
417 free (header); // everything has been copied out
430 Only loads the texinfo lump, so qdata can scan for textures
433 void LoadBSPFileTexinfo (char *filename)
439 header = malloc(sizeof(dheader_t));
441 f = fopen (filename, "rb");
442 fread (header, sizeof(dheader_t), 1, f);
445 for (i=0 ; i< sizeof(dheader_t)/4 ; i++)
446 ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
448 if (header->ident != IDBSPHEADER)
449 Error ("%s is not a IBSP file", filename);
450 if (header->version != BSPVERSION)
451 Error ("%s is version %i, not %i", filename, header->version, BSPVERSION);
454 length = header->lumps[LUMP_TEXINFO].filelen;
455 ofs = header->lumps[LUMP_TEXINFO].fileofs;
457 fseek (f, ofs, SEEK_SET);
458 fread (texinfo, length, 1, f);
461 numtexinfo = length / sizeof(texinfo_t);
463 free (header); // everything has been copied out
469 //============================================================================
474 void AddLump (int lumpnum, void *data, int len)
478 lump = &header->lumps[lumpnum];
480 lump->fileofs = LittleLong( ftell(wadfile) );
481 lump->filelen = LittleLong(len);
482 SafeWrite (wadfile, data, (len+3)&~3);
489 Swaps the bsp file in place, so it should not be referenced again
492 void WriteBSPFile (char *filename)
495 memset (header, 0, sizeof(dheader_t));
499 header->ident = LittleLong (IDBSPHEADER);
500 header->version = LittleLong (BSPVERSION);
502 wadfile = SafeOpenWrite (filename);
503 SafeWrite (wadfile, header, sizeof(dheader_t)); // overwritten later
505 AddLump (LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t));
506 AddLump (LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t));
507 AddLump (LUMP_VERTEXES, dvertexes, numvertexes*sizeof(dvertex_t));
508 AddLump (LUMP_NODES, dnodes, numnodes*sizeof(dnode_t));
509 AddLump (LUMP_TEXINFO, texinfo, numtexinfo*sizeof(texinfo_t));
510 AddLump (LUMP_FACES, dfaces, numfaces*sizeof(dface_t));
511 AddLump (LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrush_t));
512 AddLump (LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushside_t));
513 AddLump (LUMP_LEAFFACES, dleaffaces, numleaffaces*sizeof(dleaffaces[0]));
514 AddLump (LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0]));
515 AddLump (LUMP_SURFEDGES, dsurfedges, numsurfedges*sizeof(dsurfedges[0]));
516 AddLump (LUMP_EDGES, dedges, numedges*sizeof(dedge_t));
517 AddLump (LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t));
518 AddLump (LUMP_AREAS, dareas, numareas*sizeof(darea_t));
519 AddLump (LUMP_AREAPORTALS, dareaportals, numareaportals*sizeof(dareaportal_t));
521 AddLump (LUMP_LIGHTING, dlightdata, lightdatasize);
522 AddLump (LUMP_VISIBILITY, dvisdata, visdatasize);
523 AddLump (LUMP_ENTITIES, dentdata, entdatasize);
524 AddLump (LUMP_POP, dpop, sizeof(dpop));
526 fseek (wadfile, 0, SEEK_SET);
527 SafeWrite (wadfile, header, sizeof(dheader_t));
531 //============================================================================
537 Dumps info about current file
540 void PrintBSPFileSizes (void)
545 printf ("%5i models %7i\n"
546 ,nummodels, (int)(nummodels*sizeof(dmodel_t)));
547 printf ("%5i brushes %7i\n"
548 ,numbrushes, (int)(numbrushes*sizeof(dbrush_t)));
549 printf ("%5i brushsides %7i\n"
550 ,numbrushsides, (int)(numbrushsides*sizeof(dbrushside_t)));
551 printf ("%5i planes %7i\n"
552 ,numplanes, (int)(numplanes*sizeof(dplane_t)));
553 printf ("%5i texinfo %7i\n"
554 ,numtexinfo, (int)(numtexinfo*sizeof(texinfo_t)));
555 printf ("%5i entdata %7i\n", num_entities, entdatasize);
559 printf ("%5i vertexes %7i\n"
560 ,numvertexes, (int)(numvertexes*sizeof(dvertex_t)));
561 printf ("%5i nodes %7i\n"
562 ,numnodes, (int)(numnodes*sizeof(dnode_t)));
563 printf ("%5i faces %7i\n"
564 ,numfaces, (int)(numfaces*sizeof(dface_t)));
565 printf ("%5i leafs %7i\n"
566 ,numleafs, (int)(numleafs*sizeof(dleaf_t)));
567 printf ("%5i leaffaces %7i\n"
568 ,numleaffaces, (int)(numleaffaces*sizeof(dleaffaces[0])));
569 printf ("%5i leafbrushes %7i\n"
570 ,numleafbrushes, (int)(numleafbrushes*sizeof(dleafbrushes[0])));
571 printf ("%5i surfedges %7i\n"
572 ,numsurfedges, (int)(numsurfedges*sizeof(dsurfedges[0])));
573 printf ("%5i edges %7i\n"
574 ,numedges, (int)(numedges*sizeof(dedge_t)));
575 printf (" lightdata %7i\n", lightdatasize);
576 printf (" visdata %7i\n", visdatasize);
580 //============================================
583 entity_t entities[MAX_MAP_ENTITIES];
585 void StripTrailing (char *e)
590 while (s >= e && *s <= 32)
602 epair_t *ParseEpair (void)
606 e = malloc (sizeof(epair_t));
607 memset (e, 0, sizeof(epair_t));
609 if (strlen(token) >= MAX_KEY-1)
610 Error ("ParseEpar: token too long");
611 e->key = copystring(token);
613 if (strlen(token) >= MAX_VALUE-1)
614 Error ("ParseEpar: token too long");
615 e->value = copystring(token);
617 // strip trailing spaces
618 StripTrailing (e->key);
619 StripTrailing (e->value);
630 qboolean ParseEntity (void)
635 if (!GetToken (true))
638 if (strcmp (token, "{") )
639 Error ("ParseEntity: { not found");
641 if (num_entities == MAX_MAP_ENTITIES)
642 Error ("num_entities == MAX_MAP_ENTITIES");
644 mapent = &entities[num_entities];
649 if (!GetToken (true))
650 Error ("ParseEntity: EOF without closing brace");
651 if (!strcmp (token, "}") )
654 e->next = mapent->epairs;
665 Parses the dentdata string into entities
668 void ParseEntities (void)
671 ParseFromMemory (dentdata, entdatasize);
673 while (ParseEntity ())
683 Generates the dentdata string from all the entities
686 void UnparseEntities (void)
692 char key[1024], value[1024];
698 for (i=0 ; i<num_entities ; i++)
700 ep = entities[i].epairs;
702 continue; // ent got removed
707 for (ep = entities[i].epairs ; ep ; ep=ep->next)
709 strcpy (key, ep->key);
711 strcpy (value, ep->value);
712 StripTrailing (value);
714 sprintf (line, "\"%s\" \"%s\"\n", key, value);
721 if (end > buf + MAX_MAP_ENTSTRING)
722 Error ("Entity text too long");
724 entdatasize = end - buf + 1;
727 void PrintEntity (entity_t *ent)
731 printf ("------- entity %p -------\n", ent);
732 for (ep=ent->epairs ; ep ; ep=ep->next)
734 printf ("%s = %s\n", ep->key, ep->value);
739 void SetKeyValue (entity_t *ent, char *key, char *value)
743 for (ep=ent->epairs ; ep ; ep=ep->next)
744 if (!strcmp (ep->key, key) )
747 ep->value = copystring(value);
750 ep = malloc (sizeof(*ep));
751 ep->next = ent->epairs;
753 ep->key = copystring(key);
754 ep->value = copystring(value);
757 char *ValueForKey (entity_t *ent, char *key)
761 for (ep=ent->epairs ; ep ; ep=ep->next)
762 if (!strcmp (ep->key, key) )
767 vec_t FloatForKey (entity_t *ent, char *key)
771 k = ValueForKey (ent, key);
775 void GetVectorForKey (entity_t *ent, char *key, vec3_t vec)
780 k = ValueForKey (ent, key);
781 // scanf into doubles, then assign, so it is vec_t size independent
783 sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);