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
25 extern qboolean onlyents;
28 mapbrush_t mapbrushes[MAX_MAP_BRUSHES];
31 side_t brushsides[MAX_MAP_SIDES];
32 brush_texture_t side_brushtextures[MAX_MAP_SIDES];
35 plane_t mapplanes[MAX_MAP_PLANES];
37 #define PLANE_HASHES 1024
38 plane_t *planehash[PLANE_HASHES];
40 vec3_t map_mins, map_maxs;
42 // undefine to make plane finding use linear sort
45 void TestExpandBrushes (void);
55 =============================================================================
59 =============================================================================
68 int PlaneTypeForNormal (vec3_t normal)
72 // NOTE: should these have an epsilon around 1.0?
73 if (normal[0] == 1.0 || normal[0] == -1.0)
75 if (normal[1] == 1.0 || normal[1] == -1.0)
77 if (normal[2] == 1.0 || normal[2] == -1.0)
84 if (ax >= ay && ax >= az)
86 if (ay >= ax && ay >= az)
96 #define NORMAL_EPSILON 0.00001
97 #define DIST_EPSILON 0.01
98 qboolean PlaneEqual (plane_t *p, vec3_t normal, vec_t dist)
102 fabs(p->normal[0] - normal[0]) < NORMAL_EPSILON
103 && fabs(p->normal[1] - normal[1]) < NORMAL_EPSILON
104 && fabs(p->normal[2] - normal[2]) < NORMAL_EPSILON
105 && fabs(p->dist - dist) < DIST_EPSILON )
108 if (p->normal[0] == normal[0]
109 && p->normal[1] == normal[1]
110 && p->normal[2] == normal[2]
122 void AddPlaneToHash (plane_t *p)
126 hash = (int)fabs(p->dist) / 8;
127 hash &= (PLANE_HASHES-1);
129 p->hash_chain = planehash[hash];
138 int CreateNewFloatPlane (vec3_t normal, vec_t dist)
142 if (VectorLength(normal) < 0.5)
143 Error ("FloatPlane: bad normal");
144 // create a new plane
145 if (nummapplanes+2 > MAX_MAP_PLANES)
146 Error ("MAX_MAP_PLANES");
148 p = &mapplanes[nummapplanes];
149 VectorCopy (normal, p->normal);
151 p->type = (p+1)->type = PlaneTypeForNormal (p->normal);
153 VectorSubtract (vec3_origin, normal, (p+1)->normal);
158 // allways put axial planes facing positive first
161 if (p->normal[0] < 0 || p->normal[1] < 0 || p->normal[2] < 0)
169 AddPlaneToHash (p+1);
170 return nummapplanes - 1;
175 AddPlaneToHash (p+1);
176 return nummapplanes - 2;
184 void SnapVector (vec3_t normal)
188 for (i=0 ; i<3 ; i++)
190 if ( fabs(normal[i] - 1) < NORMAL_EPSILON )
192 VectorClear (normal);
196 if ( fabs(normal[i] - -1) < NORMAL_EPSILON )
198 VectorClear (normal);
210 void SnapPlane (vec3_t normal, vec_t *dist)
214 if (fabs(*dist-Q_rint(*dist)) < DIST_EPSILON)
215 *dist = Q_rint(*dist);
225 int FindFloatPlane (vec3_t normal, vec_t dist)
230 SnapPlane (normal, &dist);
231 for (i=0, p=mapplanes ; i<nummapplanes ; i++, p++)
233 if (PlaneEqual (p, normal, dist))
237 return CreateNewFloatPlane (normal, dist);
240 int FindFloatPlane (vec3_t normal, vec_t dist)
246 SnapPlane (normal, &dist);
247 hash = (int)fabs(dist) / 8;
248 hash &= (PLANE_HASHES-1);
250 // search the border bins as well
251 for (i=-1 ; i<=1 ; i++)
253 h = (hash+i)&(PLANE_HASHES-1);
254 for (p = planehash[h] ; p ; p=p->hash_chain)
256 if (PlaneEqual (p, normal, dist))
261 return CreateNewFloatPlane (normal, dist);
270 int PlaneFromPoints (int *p0, int *p1, int *p2)
272 vec3_t t1, t2, normal;
275 VectorSubtract (p0, p1, t1);
276 VectorSubtract (p2, p1, t2);
277 CrossProduct (t1, t2, normal);
278 VectorNormalize (normal, normal);
280 dist = DotProduct (p0, normal);
282 return FindFloatPlane (normal, dist);
286 //====================================================================
294 int BrushContents (mapbrush_t *b)
301 s = &b->original_sides[0];
302 contents = s->contents;
303 trans = texinfo[s->texinfo].flags;
304 for (i=1 ; i<b->numsides ; i++, s++)
306 s = &b->original_sides[i];
307 trans |= texinfo[s->texinfo].flags;
308 if (s->contents != contents)
310 Sys_Printf ("Entity %i, Brush %i: mixed face contents\n"
311 , b->entitynum, b->brushnum);
316 // if any side is translucent, mark the contents
317 // and change solid to window
318 if ( trans & (SURF_TRANS33|SURF_TRANS66) )
320 contents |= CONTENTS_TRANSLUCENT;
321 if (contents & CONTENTS_SOLID)
323 contents &= ~CONTENTS_SOLID;
324 contents |= CONTENTS_WINDOW;
332 //============================================================================
338 Adds any additional planes necessary to allow the brush to be expanded
339 against axial bounding boxes
342 void AddBrushBevels (mapbrush_t *b)
345 int i, j, k, l, order;
347 brush_texture_t tdtemp;
356 // add the axial planes
359 for (axis=0 ; axis <3 ; axis++)
361 for (dir=-1 ; dir <= 1 ; dir+=2, order++)
363 // see if the plane is allready present
364 for (i=0, s=b->original_sides ; i<b->numsides ; i++,s++)
366 if (mapplanes[s->planenum].normal[axis] == dir)
370 if (i == b->numsides)
372 if (nummapbrushsides == MAX_MAP_BRUSHSIDES)
373 Error ("MAX_MAP_BRUSHSIDES");
376 VectorClear (normal);
379 dist = b->maxs[axis];
381 dist = -b->mins[axis];
382 s->planenum = FindFloatPlane (normal, dist);
383 s->texinfo = b->original_sides[0].texinfo;
384 s->contents = b->original_sides[0].contents;
389 // if the plane is not in it canonical order, swap it
392 sidetemp = b->original_sides[order];
393 b->original_sides[order] = b->original_sides[i];
394 b->original_sides[i] = sidetemp;
396 j = b->original_sides - brushsides;
397 tdtemp = side_brushtextures[j+order];
398 side_brushtextures[j+order] = side_brushtextures[j+i];
399 side_brushtextures[j+i] = tdtemp;
405 // add the edge bevels
407 if (b->numsides == 6)
408 return; // pure axial
410 // test the non-axial plane edges
411 for (i=6 ; i<b->numsides ; i++)
413 s = b->original_sides + i;
417 for (j=0 ; j<w->numpoints ; j++)
419 k = (j+1)%w->numpoints;
420 VectorSubtract (w->p[j], w->p[k], vec);
421 if (VectorNormalize (vec, vec) < 0.5)
424 for (k=0 ; k<3 ; k++)
425 if ( vec[k] == -1 || vec[k] == 1)
428 continue; // only test non-axial edges
430 // try the six possible slanted axials from this edge
431 for (axis=0 ; axis <3 ; axis++)
433 for (dir=-1 ; dir <= 1 ; dir+=2)
438 CrossProduct (vec, vec2, normal);
439 if (VectorNormalize (normal, normal) < 0.5)
441 dist = DotProduct (w->p[j], normal);
443 // if all the points on all the sides are
444 // behind this plane, it is a proper edge bevel
445 for (k=0 ; k<b->numsides ; k++)
447 // if this plane has allready been used, skip it
448 if (PlaneEqual (&mapplanes[b->original_sides[k].planenum]
452 w2 = b->original_sides[k].winding;
455 for (l=0 ; l<w2->numpoints ; l++)
457 d = DotProduct (w2->p[l], normal) - dist;
459 break; // point in front
461 if (l != w2->numpoints)
465 if (k != b->numsides)
466 continue; // wasn't part of the outer hull
468 if (nummapbrushsides == MAX_MAP_BRUSHSIDES)
469 Error ("MAX_MAP_BRUSHSIDES");
471 s2 = &b->original_sides[b->numsides];
472 s2->planenum = FindFloatPlane (normal, dist);
473 s2->texinfo = b->original_sides[0].texinfo;
474 s2->contents = b->original_sides[0].contents;
489 makes basewindigs for sides and mins / maxs for the brush
492 qboolean MakeBrushWindings (mapbrush_t *ob)
499 ClearBounds (ob->mins, ob->maxs);
501 for (i=0 ; i<ob->numsides ; i++)
503 plane = &mapplanes[ob->original_sides[i].planenum];
504 w = BaseWindingForPlane (plane->normal, plane->dist);
505 for (j=0 ; j<ob->numsides && w; j++)
509 if (ob->original_sides[j].bevel)
511 plane = &mapplanes[ob->original_sides[j].planenum^1];
512 ChopWindingInPlace (&w, plane->normal, plane->dist, 0); //CLIP_EPSILON);
515 side = &ob->original_sides[i];
519 side->visible = true;
520 for (j=0 ; j<w->numpoints ; j++)
521 AddPointToBounds (w->p[j], ob->mins, ob->maxs);
525 for (i=0 ; i<3 ; i++)
527 if (ob->mins[0] < -4096 || ob->maxs[0] > 4096)
528 Sys_Printf ("entity %i, brush %i: bounds out of range\n", ob->entitynum, ob->brushnum);
529 if (ob->mins[0] > 4096 || ob->maxs[0] < -4096)
530 Sys_Printf ("entity %i, brush %i: no visible sides on brush\n", ob->entitynum, ob->brushnum);
542 void ParseBrush (entity_t *mapent)
552 if (nummapbrushes == MAX_MAP_BRUSHES)
553 Error ("nummapbrushes == MAX_MAP_BRUSHES");
555 b = &mapbrushes[nummapbrushes];
556 b->original_sides = &brushsides[nummapbrushsides];
557 b->entitynum = num_entities-1;
558 b->brushnum = nummapbrushes - mapent->firstbrush;
562 if (!GetToken (true))
564 if (!strcmp (token, "}") )
567 if (nummapbrushsides == MAX_MAP_BRUSHSIDES)
568 Error ("MAX_MAP_BRUSHSIDES");
569 side = &brushsides[nummapbrushsides];
571 // read the three point plane definition
572 for (i=0 ; i<3 ; i++)
576 if (strcmp (token, "(") )
577 Error ("parsing brush");
579 for (j=0 ; j<3 ; j++)
582 planepts[i][j] = atoi(token);
586 if (strcmp (token, ")") )
587 Error ("parsing brush");
593 // read the texturedef
596 strcpy (td.name, token);
599 td.shift[0] = atoi(token);
601 td.shift[1] = atoi(token);
603 td.rotate = atoi(token);
605 td.scale[0] = atof(token);
607 td.scale[1] = atof(token);
609 // find default flags and values
610 mt = FindMiptex (td.name);
611 td.flags = textureref[mt].flags;
612 td.value = textureref[mt].value;
613 side->contents = textureref[mt].contents;
614 side->surf = td.flags = textureref[mt].flags;
616 if (TokenAvailable())
619 side->contents = atoi(token);
621 side->surf = td.flags = atoi(token);
623 td.value = atoi(token);
626 // translucent objects are automatically classified as detail
627 if (side->surf & (SURF_TRANS33|SURF_TRANS66) )
628 side->contents |= CONTENTS_DETAIL;
629 if (side->contents & (CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP) )
630 side->contents |= CONTENTS_DETAIL;
632 side->contents &= ~CONTENTS_DETAIL;
633 if (!(side->contents & ((LAST_VISIBLE_CONTENTS-1)
634 | CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP|CONTENTS_MIST) ) )
635 side->contents |= CONTENTS_SOLID;
637 // hints and skips are never detail, and have no content
638 if (side->surf & (SURF_HINT|SURF_SKIP) )
641 side->surf &= ~CONTENTS_DETAIL;
646 // find the plane number
648 planenum = PlaneFromPoints (planepts[0], planepts[1], planepts[2]);
651 Sys_Printf ("Entity %i, Brush %i: plane with no normal\n"
652 , b->entitynum, b->brushnum);
657 // see if the plane has been used already
659 for (k=0 ; k<b->numsides ; k++)
661 s2 = b->original_sides + k;
662 if (s2->planenum == planenum)
664 Sys_Printf ("Entity %i, Brush %i: duplicate plane\n"
665 , b->entitynum, b->brushnum);
668 if ( s2->planenum == (planenum^1) )
670 Sys_Printf ("Entity %i, Brush %i: mirrored plane\n"
671 , b->entitynum, b->brushnum);
675 if (k != b->numsides)
676 continue; // duplicated
682 side = b->original_sides + b->numsides;
683 side->planenum = planenum;
684 side->texinfo = TexinfoForBrushTexture (&mapplanes[planenum],
687 // save the td off in case there is an origin brush and we
688 // have to recalculate the texinfo
689 side_brushtextures[nummapbrushsides] = td;
695 // get the content for the entire brush
696 b->contents = BrushContents (b);
698 // allow detail brushes to be removed
699 if (nodetail && (b->contents & CONTENTS_DETAIL) )
705 // allow water brushes to be removed
706 if (nowater && (b->contents & (CONTENTS_LAVA | CONTENTS_SLIME | CONTENTS_WATER)) )
712 // create windings for sides and bounds for brush
713 MakeBrushWindings (b);
715 // brushes that will not be visible at all will never be
716 // used as bsp splitters
717 if (b->contents & (CONTENTS_PLAYERCLIP|CONTENTS_MONSTERCLIP) )
720 for (i=0 ; i<b->numsides ; i++)
721 b->original_sides[i].texinfo = TEXINFO_NODE;
725 // origin brushes are removed, but they set
726 // the rotation origin for the rest of the brushes
727 // in the entity. After the entire entity is parsed,
728 // the planenums and texinfos will be adjusted for
731 if (b->contents & CONTENTS_ORIGIN)
736 if (num_entities == 1)
738 Error ("Entity %i, Brush %i: origin brushes not allowed in world"
739 , b->entitynum, b->brushnum);
743 VectorAdd (b->mins, b->maxs, origin);
744 VectorScale (origin, 0.5, origin);
746 sprintf (string, "%i %i %i", (int)origin[0], (int)origin[1], (int)origin[2]);
747 SetKeyValue (&entities[b->entitynum], "origin", string);
749 VectorCopy (origin, entities[b->entitynum].origin);
751 // don't keep this brush
760 mapent->numbrushes++;
767 Takes all of the brushes from the current entity and
768 adds them to the world's brush list.
770 Used by func_group and func_areaportal
773 void MoveBrushesToWorld (entity_t *mapent)
780 // this is pretty gross, because the brushes are expected to be
781 // in linear order for each entity
783 newbrushes = mapent->numbrushes;
784 worldbrushes = entities[0].numbrushes;
786 temp = malloc(newbrushes*sizeof(mapbrush_t));
787 memcpy (temp, mapbrushes + mapent->firstbrush, newbrushes*sizeof(mapbrush_t));
789 #if 0 // let them keep their original brush numbers
790 for (i=0 ; i<newbrushes ; i++)
791 temp[i].entitynum = 0;
794 // make space to move the brushes (overlapped copy)
795 memmove (mapbrushes + worldbrushes + newbrushes,
796 mapbrushes + worldbrushes,
797 sizeof(mapbrush_t) * (nummapbrushes - worldbrushes - newbrushes) );
799 // copy the new brushes down
800 memcpy (mapbrushes + worldbrushes, temp, sizeof(mapbrush_t) * newbrushes);
803 entities[0].numbrushes += newbrushes;
804 for (i=1 ; i<num_entities ; i++)
805 entities[i].firstbrush += newbrushes;
808 mapent->numbrushes = 0;
816 qboolean ParseMapEntity (void)
822 int startbrush, startsides;
826 if (!GetToken (true))
829 if (strcmp (token, "{") )
830 Error ("ParseEntity: { not found");
832 if (num_entities == MAX_MAP_ENTITIES)
833 Error ("num_entities == MAX_MAP_ENTITIES");
835 startbrush = nummapbrushes;
836 startsides = nummapbrushsides;
838 mapent = &entities[num_entities];
840 memset (mapent, 0, sizeof(*mapent));
841 mapent->firstbrush = nummapbrushes;
842 mapent->numbrushes = 0;
843 // mapent->portalareas[0] = -1;
844 // mapent->portalareas[1] = -1;
848 if (!GetToken (true))
849 Error ("ParseEntity: EOF without closing brace");
850 if (!strcmp (token, "}") )
852 if (!strcmp (token, "{") )
857 e->next = mapent->epairs;
862 GetVectorForKey (mapent, "origin", mapent->origin);
865 // if there was an origin brush, offset all of the planes and texinfo
867 if (mapent->origin[0] || mapent->origin[1] || mapent->origin[2])
869 for (i=0 ; i<mapent->numbrushes ; i++)
871 b = &mapbrushes[mapent->firstbrush + i];
872 for (j=0 ; j<b->numsides ; j++)
874 s = &b->original_sides[j];
875 newdist = mapplanes[s->planenum].dist -
876 DotProduct (mapplanes[s->planenum].normal, mapent->origin);
877 s->planenum = FindFloatPlane (mapplanes[s->planenum].normal, newdist);
878 s->texinfo = TexinfoForBrushTexture (&mapplanes[s->planenum],
879 &side_brushtextures[s-brushsides], mapent->origin);
881 MakeBrushWindings (b);
885 // group entities are just for editor convenience
886 // toss all brushes into the world entity
887 if (!strcmp ("func_group", ValueForKey (mapent, "classname")))
889 MoveBrushesToWorld (mapent);
890 mapent->numbrushes = 0;
894 // areaportal entities move their brushes, but don't eliminate
896 if (!strcmp ("func_areaportal", ValueForKey (mapent, "classname")))
900 if (mapent->numbrushes != 1)
901 Error ("Entity %i: func_areaportal can only be a single brush", num_entities-1);
903 b = &mapbrushes[nummapbrushes-1];
904 b->contents = CONTENTS_AREAPORTAL;
906 mapent->areaportalnum = c_areaportals;
907 // set the portal number as "style"
908 sprintf (str, "%i", c_areaportals);
909 SetKeyValue (mapent, "style", str);
910 MoveBrushesToWorld (mapent);
917 //===================================================================
924 void LoadMapFile (char *filename)
928 Sys_FPrintf( SYS_VRB, "--- LoadMapFile ---\n");
930 LoadScriptFile (filename);
932 nummapbrushsides = 0;
935 while (ParseMapEntity ())
939 ClearBounds (map_mins, map_maxs);
940 for (i=0 ; i<entities[0].numbrushes ; i++)
942 if (mapbrushes[i].mins[0] > 4096)
943 continue; // no valid points
944 AddPointToBounds (mapbrushes[i].mins, map_mins, map_maxs);
945 AddPointToBounds (mapbrushes[i].maxs, map_mins, map_maxs);
948 Sys_FPrintf( SYS_VRB, "%5i brushes\n", nummapbrushes);
949 Sys_FPrintf( SYS_VRB, "%5i clipbrushes\n", c_clipbrushes);
950 Sys_FPrintf( SYS_VRB, "%5i total sides\n", nummapbrushsides);
951 Sys_FPrintf( SYS_VRB, "%5i boxbevels\n", c_boxbevels);
952 Sys_FPrintf( SYS_VRB, "%5i edgebevels\n", c_edgebevels);
953 Sys_FPrintf( SYS_VRB, "%5i entities\n", num_entities);
954 Sys_FPrintf( SYS_VRB, "%5i planes\n", nummapplanes);
955 Sys_FPrintf( SYS_VRB, "%5i areaportals\n", c_areaportals);
956 Sys_FPrintf( SYS_VRB, "size: %5.0f,%5.0f,%5.0f to %5.0f,%5.0f,%5.0f\n", map_mins[0],map_mins[1],map_mins[2],
957 map_maxs[0],map_maxs[1],map_maxs[2]);
959 // TestExpandBrushes ();
963 //====================================================================
970 Expands all the brush planes and saves a new map out
973 void TestExpandBrushes (void)
979 char *name = "expanded.map";
983 Sys_Printf ("writing %s\n", name);
984 f = fopen (name, "wb");
986 Error ("Can't write %s\b", name);
988 fprintf (f, "{\n\"classname\" \"worldspawn\"\n");
990 for (bn=0 ; bn<nummapbrushes ; bn++)
992 brush = &mapbrushes[bn];
994 for (i=0 ; i<brush->numsides ; i++)
996 s = brush->original_sides + i;
997 dist = mapplanes[s->planenum].dist;
998 for (j=0 ; j<3 ; j++)
999 dist += fabs( 16 * mapplanes[s->planenum].normal[j] );
1001 w = BaseWindingForPlane (mapplanes[s->planenum].normal, dist);
1003 fprintf (f,"( %i %i %i ) ", (int)w->p[0][0], (int)w->p[0][1], (int)w->p[0][2]);
1004 fprintf (f,"( %i %i %i ) ", (int)w->p[1][0], (int)w->p[1][1], (int)w->p[1][2]);
1005 fprintf (f,"( %i %i %i ) ", (int)w->p[2][0], (int)w->p[2][1], (int)w->p[2][2]);
1007 fprintf (f, "%s 0 0 0 1 1\n", texinfo[s->texinfo].texture);
1016 Error ("can't proceed after expanding brushes");