2 Copyright (C) 1999-2007 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
21 ----------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
41 ==============================================================================
43 PORTAL FILE GENERATION
45 Save out name.prt for qvis to read
46 ==============================================================================
50 #define PORTALFILE "PRT1"
53 int num_visclusters; // clusters the player can be in
57 void WriteFloat (FILE *f, vec_t v)
59 if ( fabs(v - Q_rint(v)) < 0.001 )
60 fprintf (f,"%i ",(int)Q_rint(v));
70 void WritePortalFile_r (node_t *node)
79 if (node->planenum != PLANENUM_LEAF) {
80 WritePortalFile_r (node->children[0]);
81 WritePortalFile_r (node->children[1]);
89 for (p = node->portals ; p ; p=p->next[s])
92 s = (p->nodes[1] == node);
93 if (w && p->nodes[0] == node)
95 if (!PortalPassable(p))
97 // write out to the file
99 // sometimes planes get turned around when they are very near
100 // the changeover point between different axis. interpret the
101 // plane the same way vis will, and flip the side orders if needed
102 // FIXME: is this still relevent?
103 WindingPlane (w, normal, &dist);
104 if ( DotProduct (p->plane.normal, normal) < 0.99 )
106 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster);
109 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster);
111 /* ydnar: added this change to make antiportals work */
112 if( p->compileFlags & C_HINT )
117 /* write the winding */
118 for (i=0 ; i<w->numpoints ; i++)
121 WriteFloat (pf, w->p[i][0]);
122 WriteFloat (pf, w->p[i][1]);
123 WriteFloat (pf, w->p[i][2]);
137 void WriteFaceFile_r (node_t *node)
144 if (node->planenum != PLANENUM_LEAF) {
145 WriteFaceFile_r (node->children[0]);
146 WriteFaceFile_r (node->children[1]);
154 for (p = node->portals ; p ; p=p->next[s])
157 s = (p->nodes[1] == node);
160 if (PortalPassable(p))
162 // write out to the file
164 if (p->nodes[0] == node)
166 fprintf (pf,"%i %i ",w->numpoints, p->nodes[0]->cluster);
167 for (i=0 ; i<w->numpoints ; i++)
170 WriteFloat (pf, w->p[i][0]);
171 WriteFloat (pf, w->p[i][1]);
172 WriteFloat (pf, w->p[i][2]);
179 fprintf (pf,"%i %i ",w->numpoints, p->nodes[1]->cluster);
180 for (i = w->numpoints-1; i >= 0; i--)
183 WriteFloat (pf, w->p[i][0]);
184 WriteFloat (pf, w->p[i][1]);
185 WriteFloat (pf, w->p[i][2]);
199 void NumberLeafs_r (node_t *node)
203 if ( node->planenum != PLANENUM_LEAF ) {
206 NumberLeafs_r (node->children[0]);
207 NumberLeafs_r (node->children[1]);
213 if ( node->opaque ) {
214 // solid block, viewpoint never inside
219 node->cluster = num_visclusters;
223 for (p = node->portals ; p ; )
225 if (p->nodes[0] == node) // only write out from first leaf
227 if (PortalPassable(p))
235 if (!PortalPassable(p))
248 void NumberClusters(tree_t *tree) {
253 Sys_FPrintf (SYS_VRB,"--- NumberClusters ---\n");
255 // set the cluster field in every leaf and count the total number of portals
256 NumberLeafs_r (tree->headnode);
258 Sys_FPrintf( SYS_VRB, "%9d visclusters\n", num_visclusters );
259 Sys_FPrintf( SYS_VRB, "%9d visportals\n", num_visportals );
260 Sys_FPrintf( SYS_VRB, "%9d solidfaces\n", num_solidfaces );
268 void WritePortalFile (tree_t *tree)
272 Sys_FPrintf (SYS_VRB,"--- WritePortalFile ---\n");
275 sprintf (filename, "%s.prt", source);
276 Sys_Printf ("writing %s\n", filename);
277 pf = fopen (filename, "w");
279 Error ("Error opening %s", filename);
281 fprintf (pf, "%s\n", PORTALFILE);
282 fprintf (pf, "%i\n", num_visclusters);
283 fprintf (pf, "%i\n", num_visportals);
284 fprintf (pf, "%i\n", num_solidfaces);
286 WritePortalFile_r(tree->headnode);
287 WriteFaceFile_r(tree->headnode);