]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/tree.c
set eol-style
[xonotic/netradiant.git] / tools / quake3 / q3map2 / tree.c
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20
21 ----------------------------------------------------------------------------------
22
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."
25
26 ------------------------------------------------------------------------------- */
27
28
29
30 /* marker */
31 #define TREE_C
32
33
34
35 /* dependencies */
36 #include "q3map2.h"
37
38
39
40
41 void RemovePortalFromNode (portal_t *portal, node_t *l);
42
43 node_t *NodeForPoint (node_t *node, vec3_t origin)
44 {
45         plane_t *plane;
46         vec_t   d;
47
48         while (node->planenum != PLANENUM_LEAF)
49         {
50                 plane = &mapplanes[node->planenum];
51                 d = DotProduct (origin, plane->normal) - plane->dist;
52                 if (d >= 0)
53                         node = node->children[0];
54                 else
55                         node = node->children[1];
56         }
57
58         return node;
59 }
60
61
62
63 /*
64 =============
65 FreeTreePortals_r
66 =============
67 */
68 void FreeTreePortals_r (node_t *node)
69 {
70         portal_t        *p, *nextp;
71         int                     s;
72
73         // free children
74         if (node->planenum != PLANENUM_LEAF)
75         {
76                 FreeTreePortals_r (node->children[0]);
77                 FreeTreePortals_r (node->children[1]);
78         }
79
80         // free portals
81         for (p=node->portals ; p ; p=nextp)
82         {
83                 s = (p->nodes[1] == node);
84                 nextp = p->next[s];
85
86                 RemovePortalFromNode (p, p->nodes[!s]);
87                 FreePortal (p);
88         }
89         node->portals = NULL;
90 }
91
92 /*
93 =============
94 FreeTree_r
95 =============
96 */
97 void FreeTree_r (node_t *node)
98 {
99         // free children
100         if (node->planenum != PLANENUM_LEAF)
101         {
102                 FreeTree_r (node->children[0]);
103                 FreeTree_r (node->children[1]);
104         }
105
106         // free bspbrushes
107         FreeBrushList (node->brushlist);
108
109         // free the node
110         if (node->volume)
111                 FreeBrush (node->volume);
112
113         free (node);
114 }
115
116
117 /*
118 =============
119 FreeTree
120 =============
121 */
122 void FreeTree (tree_t *tree)
123 {
124         FreeTreePortals_r (tree->headnode);
125         FreeTree_r (tree->headnode);
126         free (tree);
127 }
128
129 //===============================================================
130
131 void PrintTree_r (node_t *node, int depth)
132 {
133         int             i;
134         plane_t *plane;
135         brush_t *bb;
136
137         for (i=0 ; i<depth ; i++)
138                 Sys_Printf ("  ");
139         if (node->planenum == PLANENUM_LEAF)
140         {
141                 if (!node->brushlist)
142                         Sys_Printf ("NULL\n");
143                 else
144                 {
145                         for (bb=node->brushlist ; bb ; bb=bb->next)
146                                 Sys_Printf ("%d ", bb->original->brushNum);
147                         Sys_Printf ("\n");
148                 }
149                 return;
150         }
151
152         plane = &mapplanes[node->planenum];
153         Sys_Printf ("#%d (%5.2f %5.2f %5.2f):%5.2f\n", node->planenum,
154                 plane->normal[0], plane->normal[1], plane->normal[2],
155                 plane->dist);
156         PrintTree_r (node->children[0], depth+1);
157         PrintTree_r (node->children[1], depth+1);
158 }