| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include "qbsp.h" |
| 23 |
|
| 24 |
extern int c_nodes; |
| 25 |
|
| 26 |
void RemovePortalFromNode (portal_t *portal, node_t *l); |
| 27 |
|
| 28 |
node_t *NodeForPoint (node_t *node, vec3_t origin) |
| 29 |
{ |
| 30 |
plane_t *plane; |
| 31 |
vec_t d; |
| 32 |
|
| 33 |
while (node->planenum != PLANENUM_LEAF) |
| 34 |
{ |
| 35 |
plane = &mapplanes[node->planenum]; |
| 36 |
d = DotProduct (origin, plane->normal) - plane->dist; |
| 37 |
if (d >= 0) |
| 38 |
node = node->children[0]; |
| 39 |
else |
| 40 |
node = node->children[1]; |
| 41 |
} |
| 42 |
|
| 43 |
return node; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
void FreeTreePortals_r (node_t *node) |
| 54 |
{ |
| 55 |
portal_t *p, *nextp; |
| 56 |
int s; |
| 57 |
|
| 58 |
|
| 59 |
if (node->planenum != PLANENUM_LEAF) |
| 60 |
{ |
| 61 |
FreeTreePortals_r (node->children[0]); |
| 62 |
FreeTreePortals_r (node->children[1]); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
for (p=node->portals ; p ; p=nextp) |
| 67 |
{ |
| 68 |
s = (p->nodes[1] == node); |
| 69 |
nextp = p->next[s]; |
| 70 |
|
| 71 |
RemovePortalFromNode (p, p->nodes[!s]); |
| 72 |
FreePortal (p); |
| 73 |
} |
| 74 |
node->portals = NULL; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void FreeTree_r (node_t *node) |
| 83 |
{ |
| 84 |
face_t *f, *nextf; |
| 85 |
|
| 86 |
|
| 87 |
if (node->planenum != PLANENUM_LEAF) |
| 88 |
{ |
| 89 |
FreeTree_r (node->children[0]); |
| 90 |
FreeTree_r (node->children[1]); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
FreeBrushList (node->brushlist); |
| 95 |
|
| 96 |
|
| 97 |
for (f=node->faces ; f ; f=nextf) |
| 98 |
{ |
| 99 |
nextf = f->next; |
| 100 |
FreeFace (f); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
if (node->volume) |
| 105 |
FreeBrush (node->volume); |
| 106 |
|
| 107 |
if (numthreads == 1) |
| 108 |
c_nodes--; |
| 109 |
free (node); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
void FreeTree (tree_t *tree) |
| 119 |
{ |
| 120 |
FreeTreePortals_r (tree->headnode); |
| 121 |
FreeTree_r (tree->headnode); |
| 122 |
free (tree); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
void PrintTree_r (node_t *node, int depth) |
| 128 |
{ |
| 129 |
int i; |
| 130 |
plane_t *plane; |
| 131 |
bspbrush_t *bb; |
| 132 |
|
| 133 |
for (i=0 ; i<depth ; i++) |
| 134 |
printf (" "); |
| 135 |
if (node->planenum == PLANENUM_LEAF) |
| 136 |
{ |
| 137 |
if (!node->brushlist) |
| 138 |
printf ("NULL\n"); |
| 139 |
else |
| 140 |
{ |
| 141 |
for (bb=node->brushlist ; bb ; bb=bb->next) |
| 142 |
printf ("%i ", bb->original->brushnum); |
| 143 |
printf ("\n"); |
| 144 |
} |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
plane = &mapplanes[node->planenum]; |
| 149 |
printf ("#%i (%5.2f %5.2f %5.2f):%5.2f\n", node->planenum, |
| 150 |
plane->normal[0], plane->normal[1], plane->normal[2], |
| 151 |
plane->dist); |
| 152 |
PrintTree_r (node->children[0], depth+1); |
| 153 |
PrintTree_r (node->children[1], depth+1); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
int c_pruned; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
void PruneNodes_r (node_t *node) |
| 172 |
{ |
| 173 |
bspbrush_t *b, *next; |
| 174 |
|
| 175 |
if (node->planenum == PLANENUM_LEAF) |
| 176 |
return; |
| 177 |
PruneNodes_r (node->children[0]); |
| 178 |
PruneNodes_r (node->children[1]); |
| 179 |
|
| 180 |
if ( (node->children[0]->contents & CONTENTS_SOLID) |
| 181 |
&& (node->children[1]->contents & CONTENTS_SOLID) ) |
| 182 |
{ |
| 183 |
if (node->faces) |
| 184 |
Error ("node->faces seperating CONTENTS_SOLID"); |
| 185 |
if (node->children[0]->faces || node->children[1]->faces) |
| 186 |
Error ("!node->faces with children"); |
| 187 |
|
| 188 |
|
| 189 |
node->planenum = PLANENUM_LEAF; |
| 190 |
node->contents = CONTENTS_SOLID; |
| 191 |
node->detail_seperator = false; |
| 192 |
|
| 193 |
if (node->brushlist) |
| 194 |
Error ("PruneNodes: node->brushlist"); |
| 195 |
|
| 196 |
|
| 197 |
node->brushlist = node->children[1]->brushlist; |
| 198 |
|
| 199 |
for (b=node->children[0]->brushlist ; b ; b=next) |
| 200 |
{ |
| 201 |
next = b->next; |
| 202 |
b->next = node->brushlist; |
| 203 |
node->brushlist = b; |
| 204 |
} |
| 205 |
|
| 206 |
c_pruned++; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
void PruneNodes (node_t *node) |
| 212 |
{ |
| 213 |
qprintf ("--- PruneNodes ---\n"); |
| 214 |
c_pruned = 0; |
| 215 |
PruneNodes_r (node); |
| 216 |
qprintf ("%5i pruned nodes\n", c_pruned); |
| 217 |
} |
| 218 |
|
| 219 |
|