forked from bnoordhuis/bspc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tree.cpp
283 lines (257 loc) · 7.34 KB
/
tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "qbsp.h"
extern int c_nodes;
int c_pruned;
int freedtreemem = 0;
void RemovePortalFromNode (portal_t *portal, node_t *l);
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
node_t *NodeForPoint (node_t *node, vec3_t origin)
{
plane_t *plane;
vec_t d;
while (node->planenum != PLANENUM_LEAF)
{
plane = &mapplanes[node->planenum];
d = DotProduct (origin, plane->normal) - plane->dist;
if (d >= 0)
node = node->children[0];
else
node = node->children[1];
}
return node;
} //end of the function NodeForPoint
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Tree_FreePortals_r (node_t *node)
{
portal_t *p, *nextp;
int s;
// free children
if (node->planenum != PLANENUM_LEAF)
{
Tree_FreePortals_r(node->children[0]);
Tree_FreePortals_r(node->children[1]);
}
// free portals
for (p = node->portals; p; p = nextp)
{
s = (p->nodes[1] == node);
nextp = p->next[s];
RemovePortalFromNode (p, p->nodes[!s]);
#ifdef ME
if (p->winding) freedtreemem += MemorySize(p->winding);
freedtreemem += MemorySize(p);
#endif //ME
FreePortal(p);
}
node->portals = NULL;
} //end of the function Tree_FreePortals_r
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Tree_Free_r (node_t *node)
{
// face_t *f, *nextf;
bspbrush_t *brush, *nextbrush;
//free children
if (node->planenum != PLANENUM_LEAF)
{
Tree_Free_r (node->children[0]);
Tree_Free_r (node->children[1]);
} //end if
//free bspbrushes
// FreeBrushList (node->brushlist);
for (brush = node->brushlist; brush; brush = nextbrush)
{
nextbrush = brush->next;
#ifdef ME
freedtreemem += MemorySize(brush);
#endif //ME
FreeBrush(brush);
} //end for
node->brushlist = NULL;
/*
NOTE: only used when creating Q2 bsp
// free faces
for (f = node->faces; f; f = nextf)
{
nextf = f->next;
#ifdef ME
if (f->w) freedtreemem += MemorySize(f->w);
freedtreemem += sizeof(face_t);
#endif //ME
FreeFace(f);
} //end for
*/
// free the node
if (node->volume)
{
#ifdef ME
freedtreemem += MemorySize(node->volume);
#endif //ME
FreeBrush (node->volume);
} //end if
if (numthreads == 1) c_nodes--;
#ifdef ME
freedtreemem += MemorySize(node);
#endif //ME
FreeMemory(node);
} //end of the function Tree_Free_r
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Tree_Free(tree_t *tree)
{
//if no tree just return
if (!tree) return;
//
freedtreemem = 0;
//
Tree_FreePortals_r(tree->headnode);
Tree_Free_r(tree->headnode);
#ifdef ME
freedtreemem += MemorySize(tree);
#endif //ME
FreeMemory(tree);
#ifdef ME
Log_Print("freed ");
PrintMemorySize(freedtreemem);
Log_Print(" of tree memory\n");
#endif //ME
} //end of the function Tree_Free
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
tree_t *Tree_Alloc(void)
{
tree_t *tree;
tree = (tree_t*)GetMemory(sizeof(*tree));
memset (tree, 0, sizeof(*tree));
ClearBounds (tree->mins, tree->maxs);
return tree;
} //end of the function Tree_Alloc
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Tree_Print_r (node_t *node, int depth)
{
int i;
plane_t *plane;
bspbrush_t *bb;
for (i=0 ; i<depth ; i++)
printf (" ");
if (node->planenum == PLANENUM_LEAF)
{
if (!node->brushlist)
printf ("NULL\n");
else
{
for (bb=node->brushlist ; bb ; bb=bb->next)
printf ("%i ", bb->original->brushnum);
printf ("\n");
}
return;
}
plane = &mapplanes[node->planenum];
printf ("#%i (%5.2f %5.2f %5.2f):%5.2f\n", node->planenum,
plane->normal[0], plane->normal[1], plane->normal[2],
plane->dist);
Tree_Print_r (node->children[0], depth+1);
Tree_Print_r (node->children[1], depth+1);
} //end of the function Tree_Print_r
//===========================================================================
// NODES THAT DON'T SEPERATE DIFFERENT CONTENTS CAN BE PRUNED
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Tree_PruneNodes_r (node_t *node)
{
bspbrush_t *b, *next;
if (node->planenum == PLANENUM_LEAF) return;
Tree_PruneNodes_r (node->children[0]);
Tree_PruneNodes_r (node->children[1]);
if (create_aas)
{
if ((node->children[0]->contents & CONTENTS_LADDER) ||
(node->children[1]->contents & CONTENTS_LADDER)) return;
}
if ((node->children[0]->contents & CONTENTS_SOLID)
&& (node->children[1]->contents & CONTENTS_SOLID))
{
if (node->faces)
Error ("node->faces seperating CONTENTS_SOLID");
if (node->children[0]->faces || node->children[1]->faces)
Error ("!node->faces with children");
// FIXME: free stuff
node->planenum = PLANENUM_LEAF;
node->contents = CONTENTS_SOLID;
node->detail_seperator = qfalse;
if (node->brushlist)
Error ("PruneNodes: node->brushlist");
// combine brush lists
node->brushlist = node->children[1]->brushlist;
for (b = node->children[0]->brushlist; b; b = next)
{
next = b->next;
b->next = node->brushlist;
node->brushlist = b;
} //end for
//free the child nodes
FreeMemory(node->children[0]);
FreeMemory(node->children[1]);
//two nodes are cut away
c_pruned += 2;
} //end if
} //end of the function Tree_PruneNodes_r
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Tree_PruneNodes(node_t *node)
{
Log_Print("------- Prune Nodes --------\n");
c_pruned = 0;
Tree_PruneNodes_r(node);
Log_Print("%5i pruned nodes\n", c_pruned);
} //end of the function Tree_PruneNodes