1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
42 ==============================================================================
44 PORTAL FILE GENERATION
46 Save out name.prt for qvis to read
47 ==============================================================================
51 #define PORTALFILE "PRT1"
54 int num_visclusters; // clusters the player can be in
58 void WriteFloat (FILE *f, vec_t v)
60 if ( fabs(v - Q_rint(v)) < 0.001 )
61 fprintf (f,"%i ",(int)Q_rint(v));
71 void WritePortalFile_r (node_t *node)
80 if (node->planenum != PLANENUM_LEAF) {
81 WritePortalFile_r (node->children[0]);
82 WritePortalFile_r (node->children[1]);
90 for (p = node->portals ; p ; p=p->next[s])
93 s = (p->nodes[1] == node);
94 if (w && p->nodes[0] == node)
96 if (!PortalPassable(p))
98 // write out to the file
100 // sometimes planes get turned around when they are very near
101 // the changeover point between different axis. interpret the
102 // plane the same way vis will, and flip the side orders if needed
103 // FIXME: is this still relevent?
104 WindingPlane (w, normal, &dist);
105 if ( DotProduct (p->plane.normal, normal) < 0.99 )
107 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster);
110 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster);
112 /* ydnar: added this change to make antiportals work */
113 if( p->compileFlags & C_HINT )
118 /* write the winding */
119 for (i=0 ; i<w->numpoints ; i++)
122 WriteFloat (pf, w->p[i][0]);
123 WriteFloat (pf, w->p[i][1]);
124 WriteFloat (pf, w->p[i][2]);
138 void WriteFaceFile_r (node_t *node)
145 if (node->planenum != PLANENUM_LEAF) {
146 WriteFaceFile_r (node->children[0]);
147 WriteFaceFile_r (node->children[1]);
155 for (p = node->portals ; p ; p=p->next[s])
158 s = (p->nodes[1] == node);
161 if (PortalPassable(p))
163 // write out to the file
165 if (p->nodes[0] == node)
167 fprintf (pf,"%i %i ",w->numpoints, p->nodes[0]->cluster);
168 for (i=0 ; i<w->numpoints ; i++)
171 WriteFloat (pf, w->p[i][0]);
172 WriteFloat (pf, w->p[i][1]);
173 WriteFloat (pf, w->p[i][2]);
180 fprintf (pf,"%i %i ",w->numpoints, p->nodes[1]->cluster);
181 for (i = w->numpoints-1; i >= 0; i--)
184 WriteFloat (pf, w->p[i][0]);
185 WriteFloat (pf, w->p[i][1]);
186 WriteFloat (pf, w->p[i][2]);
200 void NumberLeafs_r (node_t *node)
204 if ( node->planenum != PLANENUM_LEAF ) {
207 NumberLeafs_r (node->children[0]);
208 NumberLeafs_r (node->children[1]);
214 if ( node->opaque ) {
215 // solid block, viewpoint never inside
220 node->cluster = num_visclusters;
224 for (p = node->portals ; p ; )
226 if (p->nodes[0] == node) // only write out from first leaf
228 if (PortalPassable(p))
236 if (!PortalPassable(p))
249 void NumberClusters(tree_t *tree) {
254 Sys_FPrintf (SYS_VRB,"--- NumberClusters ---\n");
256 // set the cluster field in every leaf and count the total number of portals
257 NumberLeafs_r (tree->headnode);
259 Sys_FPrintf( SYS_VRB, "%9d visclusters\n", num_visclusters );
260 Sys_FPrintf( SYS_VRB, "%9d visportals\n", num_visportals );
261 Sys_FPrintf( SYS_VRB, "%9d solidfaces\n", num_solidfaces );
269 void WritePortalFile (tree_t *tree)
273 Sys_FPrintf (SYS_VRB,"--- WritePortalFile ---\n");
276 sprintf (filename, "%s.prt", source);
277 Sys_Printf ("writing %s\n", filename);
278 pf = fopen (filename, "w");
280 Error ("Error opening %s", filename);
282 fprintf (pf, "%s\n", PORTALFILE);
283 fprintf (pf, "%i\n", num_visclusters);
284 fprintf (pf, "%i\n", num_visportals);
285 fprintf (pf, "%i\n", num_solidfaces);
287 WritePortalFile_r(tree->headnode);
288 WriteFaceFile_r(tree->headnode);