2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 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 GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 void GetLeafNums (void);
31 //=============================================================================
34 dmodel_t dmodels[MAX_MAP_MODELS];
37 byte dvisdata[MAX_MAP_VISIBILITY];
38 dvis_t *dvis = (dvis_t *)dvisdata;
41 byte dlightdata[MAX_MAP_LIGHTING];
44 char dentdata[MAX_MAP_ENTSTRING];
47 dleaf_t dleafs[MAX_MAP_LEAFS];
50 dplane_t dplanes[MAX_MAP_PLANES];
53 dvertex_t dvertexes[MAX_MAP_VERTS];
56 dnode_t dnodes[MAX_MAP_NODES];
59 texinfo_t texinfo[MAX_MAP_TEXINFO];
62 dface_t dfaces[MAX_MAP_FACES];
65 dedge_t dedges[MAX_MAP_EDGES];
68 unsigned short dleaffaces[MAX_MAP_LEAFFACES];
71 unsigned short dleafbrushes[MAX_MAP_LEAFBRUSHES];
74 int dsurfedges[MAX_MAP_SURFEDGES];
77 dbrush_t dbrushes[MAX_MAP_BRUSHES];
80 dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
83 darea_t dareas[MAX_MAP_AREAS];
86 dareaportal_t dareaportals[MAX_MAP_AREAPORTALS];
96 int CompressVis (byte *vis, byte *dest)
104 // visrow = (r_numvisleafs + 7)>>3;
105 visrow = (dvis->numclusters + 7)>>3;
107 for (j=0 ; j<visrow ; j++)
114 for ( j++; j<visrow ; j++)
115 if (vis[j] || rep == 255)
123 return dest_p - dest;
132 void DecompressVis (byte *in, byte *decompressed)
138 // row = (r_numvisleafs+7)>>3;
139 row = (dvis->numclusters+7)>>3;
152 Error ("DecompressVis: 0 repeat");
159 } while (out - decompressed < row);
162 //=============================================================================
168 Byte swaps all data in a bsp file.
171 void SwapBSPFile (qboolean todisk)
178 for (i=0 ; i<nummodels ; i++)
182 d->firstface = LittleLong (d->firstface);
183 d->numfaces = LittleLong (d->numfaces);
184 d->headnode = LittleLong (d->headnode);
186 for (j=0 ; j<3 ; j++)
188 d->mins[j] = LittleFloat(d->mins[j]);
189 d->maxs[j] = LittleFloat(d->maxs[j]);
190 d->origin[j] = LittleFloat(d->origin[j]);
197 for (i=0 ; i<numvertexes ; i++)
199 for (j=0 ; j<3 ; j++)
200 dvertexes[i].point[j] = LittleFloat (dvertexes[i].point[j]);
206 for (i=0 ; i<numplanes ; i++)
208 for (j=0 ; j<3 ; j++)
209 dplanes[i].normal[j] = LittleFloat (dplanes[i].normal[j]);
210 dplanes[i].dist = LittleFloat (dplanes[i].dist);
211 dplanes[i].type = LittleLong (dplanes[i].type);
217 for (i=0 ; i<numtexinfo ; i++)
219 for (j=0 ; j<8 ; j++)
220 texinfo[i].vecs[0][j] = LittleFloat (texinfo[i].vecs[0][j]);
221 texinfo[i].flags = LittleLong (texinfo[i].flags);
222 texinfo[i].value = LittleLong (texinfo[i].value);
223 texinfo[i].nexttexinfo = LittleLong (texinfo[i].nexttexinfo);
229 for (i=0 ; i<numfaces ; i++)
231 dfaces[i].texinfo = LittleShort (dfaces[i].texinfo);
232 dfaces[i].planenum = LittleShort (dfaces[i].planenum);
233 dfaces[i].side = LittleShort (dfaces[i].side);
234 dfaces[i].lighting.c = LittleLong (dfaces[i].lighting.c);
235 dfaces[i].lightofs = LittleLong (dfaces[i].lightofs);
236 dfaces[i].firstedge = LittleLong (dfaces[i].firstedge);
237 dfaces[i].numedges = LittleShort (dfaces[i].numedges);
243 for (i=0 ; i<numnodes ; i++)
245 dnodes[i].planenum = LittleLong (dnodes[i].planenum);
246 for (j=0 ; j<3 ; j++)
248 dnodes[i].mins[j] = LittleShort (dnodes[i].mins[j]);
249 dnodes[i].maxs[j] = LittleShort (dnodes[i].maxs[j]);
251 dnodes[i].children[0] = LittleLong (dnodes[i].children[0]);
252 dnodes[i].children[1] = LittleLong (dnodes[i].children[1]);
253 dnodes[i].firstface = LittleShort (dnodes[i].firstface);
254 dnodes[i].numfaces = LittleShort (dnodes[i].numfaces);
260 for (i=0 ; i<numleafs ; i++)
262 dleafs[i].contents = LittleLong (dleafs[i].contents);
263 dleafs[i].cluster = LittleShort (dleafs[i].cluster);
264 dleafs[i].area = LittleShort (dleafs[i].area);
265 for (j=0 ; j<3 ; j++)
267 dleafs[i].mins[j] = LittleShort (dleafs[i].mins[j]);
268 dleafs[i].maxs[j] = LittleShort (dleafs[i].maxs[j]);
271 dleafs[i].firstleafface = LittleShort (dleafs[i].firstleafface);
272 dleafs[i].numleaffaces = LittleShort (dleafs[i].numleaffaces);
273 dleafs[i].firstleafbrush = LittleShort (dleafs[i].firstleafbrush);
274 dleafs[i].numleafbrushes = LittleShort (dleafs[i].numleafbrushes);
280 for (i=0 ; i<numleaffaces ; i++)
281 dleaffaces[i] = LittleShort (dleaffaces[i]);
286 for (i=0 ; i<numleafbrushes ; i++)
287 dleafbrushes[i] = LittleShort (dleafbrushes[i]);
292 for (i=0 ; i<numsurfedges ; i++)
293 dsurfedges[i] = LittleLong (dsurfedges[i]);
298 for (i=0 ; i<numedges ; i++)
300 dedges[i].v[0] = LittleShort (dedges[i].v[0]);
301 dedges[i].v[1] = LittleShort (dedges[i].v[1]);
307 for (i=0 ; i<numbrushes ; i++)
309 dbrushes[i].firstside = LittleLong (dbrushes[i].firstside);
310 dbrushes[i].numsides = LittleLong (dbrushes[i].numsides);
311 dbrushes[i].contents = LittleLong (dbrushes[i].contents);
317 for (i=0 ; i<numareas ; i++)
319 dareas[i].numareaportals = LittleLong (dareas[i].numareaportals);
320 dareas[i].firstareaportal = LittleLong (dareas[i].firstareaportal);
326 for (i=0 ; i<numareaportals ; i++)
328 dareaportals[i].portalnum = LittleLong (dareaportals[i].portalnum);
329 dareaportals[i].otherarea = LittleLong (dareaportals[i].otherarea);
335 for (i=0 ; i<numbrushsides ; i++)
337 dbrushsides[i].planenum = LittleShort (dbrushsides[i].planenum);
338 dbrushsides[i].texinfo = LittleShort (dbrushsides[i].texinfo);
345 j = dvis->numclusters;
347 j = LittleLong(dvis->numclusters);
348 dvis->numclusters = LittleLong (dvis->numclusters);
349 for (i=0 ; i<j ; i++)
351 dvis->bitofs[i][0] = LittleLong (dvis->bitofs[i][0]);
352 dvis->bitofs[i][1] = LittleLong (dvis->bitofs[i][1]);
359 int CopyLump (int lump, void *dest, int size)
363 length = header->lumps[lump].filelen;
364 ofs = header->lumps[lump].fileofs;
367 Error ("LoadBSPFile: odd lump size");
369 memcpy (dest, (byte *)header + ofs, length);
371 return length / size;
379 void LoadBSPFile (char *filename)
384 // load the file header
386 LoadFile (filename, (void **)&header);
389 for (i=0 ; i< sizeof(dheader_t)/4 ; i++)
390 ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
392 if (header->ident != IDBSPHEADER)
393 Error ("%s is not a IBSP file", filename);
394 if (header->version != BSPVERSION)
395 Error ("%s is version %i, not %i", filename, header->version, BSPVERSION);
397 nummodels = CopyLump (LUMP_MODELS, dmodels, sizeof(dmodel_t));
398 numvertexes = CopyLump (LUMP_VERTEXES, dvertexes, sizeof(dvertex_t));
399 numplanes = CopyLump (LUMP_PLANES, dplanes, sizeof(dplane_t));
400 numleafs = CopyLump (LUMP_LEAFS, dleafs, sizeof(dleaf_t));
401 numnodes = CopyLump (LUMP_NODES, dnodes, sizeof(dnode_t));
402 numtexinfo = CopyLump (LUMP_TEXINFO, texinfo, sizeof(texinfo_t));
403 numfaces = CopyLump (LUMP_FACES, dfaces, sizeof(dface_t));
404 numleaffaces = CopyLump (LUMP_LEAFFACES, dleaffaces, sizeof(dleaffaces[0]));
405 numleafbrushes = CopyLump (LUMP_LEAFBRUSHES, dleafbrushes, sizeof(dleafbrushes[0]));
406 numsurfedges = CopyLump (LUMP_SURFEDGES, dsurfedges, sizeof(dsurfedges[0]));
407 numedges = CopyLump (LUMP_EDGES, dedges, sizeof(dedge_t));
408 numbrushes = CopyLump (LUMP_BRUSHES, dbrushes, sizeof(dbrush_t));
409 numbrushsides = CopyLump (LUMP_BRUSHSIDES, dbrushsides, sizeof(dbrushside_t));
410 numareas = CopyLump (LUMP_AREAS, dareas, sizeof(darea_t));
411 numareaportals = CopyLump (LUMP_AREAPORTALS, dareaportals, sizeof(dareaportal_t));
413 visdatasize = CopyLump (LUMP_VISIBILITY, dvisdata, 1);
414 lightdatasize = CopyLump (LUMP_LIGHTING, dlightdata, 1);
415 entdatasize = CopyLump (LUMP_ENTITIES, dentdata, 1);
417 CopyLump (LUMP_POP, dpop, 1);
419 free (header); // everything has been copied out
432 Only loads the texinfo lump, so qdata can scan for textures
435 void LoadBSPFileTexinfo (char *filename)
441 header = malloc(sizeof(dheader_t));
443 f = fopen (filename, "rb");
444 fread (header, sizeof(dheader_t), 1, f);
447 for (i=0 ; i< sizeof(dheader_t)/4 ; i++)
448 ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
450 if (header->ident != IDBSPHEADER)
451 Error ("%s is not a IBSP file", filename);
452 if (header->version != BSPVERSION)
453 Error ("%s is version %i, not %i", filename, header->version, BSPVERSION);
456 length = header->lumps[LUMP_TEXINFO].filelen;
457 ofs = header->lumps[LUMP_TEXINFO].fileofs;
459 fseek (f, ofs, SEEK_SET);
460 fread (texinfo, length, 1, f);
463 numtexinfo = length / sizeof(texinfo_t);
465 free (header); // everything has been copied out
471 //============================================================================
476 void AddLump (int lumpnum, void *data, int len)
480 lump = &header->lumps[lumpnum];
482 lump->fileofs = LittleLong( ftell(wadfile) );
483 lump->filelen = LittleLong(len);
484 SafeWrite (wadfile, data, (len+3)&~3);
491 Swaps the bsp file in place, so it should not be referenced again
494 void WriteBSPFile (char *filename)
497 memset (header, 0, sizeof(dheader_t));
501 header->ident = LittleLong (IDBSPHEADER);
502 header->version = LittleLong (BSPVERSION);
504 wadfile = SafeOpenWrite (filename);
505 SafeWrite (wadfile, header, sizeof(dheader_t)); // overwritten later
507 AddLump (LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t));
508 AddLump (LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t));
509 AddLump (LUMP_VERTEXES, dvertexes, numvertexes*sizeof(dvertex_t));
510 AddLump (LUMP_NODES, dnodes, numnodes*sizeof(dnode_t));
511 AddLump (LUMP_TEXINFO, texinfo, numtexinfo*sizeof(texinfo_t));
512 AddLump (LUMP_FACES, dfaces, numfaces*sizeof(dface_t));
513 AddLump (LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrush_t));
514 AddLump (LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushside_t));
515 AddLump (LUMP_LEAFFACES, dleaffaces, numleaffaces*sizeof(dleaffaces[0]));
516 AddLump (LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0]));
517 AddLump (LUMP_SURFEDGES, dsurfedges, numsurfedges*sizeof(dsurfedges[0]));
518 AddLump (LUMP_EDGES, dedges, numedges*sizeof(dedge_t));
519 AddLump (LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t));
520 AddLump (LUMP_AREAS, dareas, numareas*sizeof(darea_t));
521 AddLump (LUMP_AREAPORTALS, dareaportals, numareaportals*sizeof(dareaportal_t));
523 AddLump (LUMP_LIGHTING, dlightdata, lightdatasize);
524 AddLump (LUMP_VISIBILITY, dvisdata, visdatasize);
525 AddLump (LUMP_ENTITIES, dentdata, entdatasize);
526 AddLump (LUMP_POP, dpop, sizeof(dpop));
528 fseek (wadfile, 0, SEEK_SET);
529 SafeWrite (wadfile, header, sizeof(dheader_t));
533 //============================================================================
539 Dumps info about current file
542 void PrintBSPFileSizes (void)
547 printf ("%5i models %7i\n"
548 ,nummodels, (int)(nummodels*sizeof(dmodel_t)));
549 printf ("%5i brushes %7i\n"
550 ,numbrushes, (int)(numbrushes*sizeof(dbrush_t)));
551 printf ("%5i brushsides %7i\n"
552 ,numbrushsides, (int)(numbrushsides*sizeof(dbrushside_t)));
553 printf ("%5i planes %7i\n"
554 ,numplanes, (int)(numplanes*sizeof(dplane_t)));
555 printf ("%5i texinfo %7i\n"
556 ,numtexinfo, (int)(numtexinfo*sizeof(texinfo_t)));
557 printf ("%5i entdata %7i\n", num_entities, entdatasize);
561 printf ("%5i vertexes %7i\n"
562 ,numvertexes, (int)(numvertexes*sizeof(dvertex_t)));
563 printf ("%5i nodes %7i\n"
564 ,numnodes, (int)(numnodes*sizeof(dnode_t)));
565 printf ("%5i faces %7i\n"
566 ,numfaces, (int)(numfaces*sizeof(dface_t)));
567 printf ("%5i leafs %7i\n"
568 ,numleafs, (int)(numleafs*sizeof(dleaf_t)));
569 printf ("%5i leaffaces %7i\n"
570 ,numleaffaces, (int)(numleaffaces*sizeof(dleaffaces[0])));
571 printf ("%5i leafbrushes %7i\n"
572 ,numleafbrushes, (int)(numleafbrushes*sizeof(dleafbrushes[0])));
573 printf ("%5i surfedges %7i\n"
574 ,numsurfedges, (int)(numsurfedges*sizeof(dsurfedges[0])));
575 printf ("%5i edges %7i\n"
576 ,numedges, (int)(numedges*sizeof(dedge_t)));
577 printf (" lightdata %7i\n", lightdatasize);
578 printf (" visdata %7i\n", visdatasize);
582 //============================================
585 entity_t entities[MAX_MAP_ENTITIES];
587 void StripTrailing (char *e)
592 while (s >= e && *s <= 32)
604 epair_t *ParseEpair (void)
608 e = malloc (sizeof(epair_t));
609 memset (e, 0, sizeof(epair_t));
611 if (strlen(token) >= MAX_KEY-1)
612 Error ("ParseEpar: token too long");
613 e->key = copystring(token);
614 GetScriptToken (false);
615 if (strlen(token) >= MAX_VALUE-1)
616 Error ("ParseEpar: token too long");
617 e->value = copystring(token);
619 // strip trailing spaces
620 StripTrailing (e->key);
621 StripTrailing (e->value);
632 qboolean ParseEntity (void)
637 if (!GetScriptToken (true))
640 if (strcmp (token, "{") )
641 Error ("ParseEntity: { not found");
643 if (num_entities == MAX_MAP_ENTITIES)
644 Error ("num_entities == MAX_MAP_ENTITIES");
646 mapent = &entities[num_entities];
651 if (!GetScriptToken (true))
652 Error ("ParseEntity: EOF without closing brace");
653 if (!strcmp (token, "}") )
656 e->next = mapent->epairs;
667 Parses the dentdata string into entities
670 void ParseEntities (void)
673 ParseFromMemory (dentdata, entdatasize);
675 while (ParseEntity ())
685 Generates the dentdata string from all the entities
688 void UnparseEntities (void)
694 char key[1024], value[1024];
700 for (i=0 ; i<num_entities ; i++)
702 ep = entities[i].epairs;
704 continue; // ent got removed
709 for (ep = entities[i].epairs ; ep ; ep=ep->next)
711 strcpy (key, ep->key);
713 strcpy (value, ep->value);
714 StripTrailing (value);
716 sprintf (line, "\"%s\" \"%s\"\n", key, value);
723 if (end > buf + MAX_MAP_ENTSTRING)
724 Error ("Entity text too long");
726 entdatasize = end - buf + 1;
729 void PrintEntity (entity_t *ent)
733 printf ("------- entity %p -------\n", ent);
734 for (ep=ent->epairs ; ep ; ep=ep->next)
736 printf ("%s = %s\n", ep->key, ep->value);
741 void SetKeyValue (entity_t *ent, char *key, char *value)
745 for (ep=ent->epairs ; ep ; ep=ep->next)
746 if (!strcmp (ep->key, key) )
749 ep->value = copystring(value);
752 ep = malloc (sizeof(*ep));
754 Error("SetKeyValue MALLOC failed! Could not allocate %s bytes.", sizeof(*ep));
755 ep->next = ent->epairs;
757 ep->key = copystring(key);
758 ep->value = copystring(value);
761 char *ValueForKey (entity_t *ent, char *key)
765 for (ep=ent->epairs ; ep ; ep=ep->next)
766 if (!strcmp (ep->key, key) )
771 vec_t FloatForKey (entity_t *ent, char *key)
775 k = ValueForKey (ent, key);
779 void GetVectorForKey (entity_t *ent, char *key, vec3_t vec)
784 k = ValueForKey (ent, key);
785 // scanf into doubles, then assign, so it is vec_t size independent
787 sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);