Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "vbsp.h"
  9. extern int c_nodes;
  10. void RemovePortalFromNode (portal_t *portal, node_t *l);
  11. node_t *NodeForPoint (node_t *node, Vector& origin)
  12. {
  13. plane_t *plane;
  14. vec_t d;
  15. while (node->planenum != PLANENUM_LEAF)
  16. {
  17. plane = &g_MainMap->mapplanes[node->planenum];
  18. d = DotProduct (origin, plane->normal) - plane->dist;
  19. if (d >= 0)
  20. node = node->children[0];
  21. else
  22. node = node->children[1];
  23. }
  24. return node;
  25. }
  26. /*
  27. =============
  28. FreeTreePortals_r
  29. =============
  30. */
  31. void FreeTreePortals_r (node_t *node)
  32. {
  33. portal_t *p, *nextp;
  34. int s;
  35. // free children
  36. if (node->planenum != PLANENUM_LEAF)
  37. {
  38. FreeTreePortals_r (node->children[0]);
  39. FreeTreePortals_r (node->children[1]);
  40. }
  41. // free portals
  42. for (p=node->portals ; p ; p=nextp)
  43. {
  44. s = (p->nodes[1] == node);
  45. nextp = p->next[s];
  46. RemovePortalFromNode (p, p->nodes[!s]);
  47. FreePortal (p);
  48. }
  49. node->portals = NULL;
  50. }
  51. /*
  52. =============
  53. FreeTree_r
  54. =============
  55. */
  56. void FreeTree_r (node_t *node)
  57. {
  58. face_t *f, *nextf;
  59. // free children
  60. if (node->planenum != PLANENUM_LEAF)
  61. {
  62. FreeTree_r (node->children[0]);
  63. FreeTree_r (node->children[1]);
  64. }
  65. // free bspbrushes
  66. FreeBrushList (node->brushlist);
  67. // free faces
  68. for (f=node->faces ; f ; f=nextf)
  69. {
  70. nextf = f->next;
  71. FreeFace (f);
  72. }
  73. // free the node
  74. if (node->volume)
  75. FreeBrush (node->volume);
  76. if (numthreads == 1)
  77. c_nodes--;
  78. free (node);
  79. }
  80. /*
  81. =============
  82. FreeTree
  83. =============
  84. */
  85. void FreeTree (tree_t *tree)
  86. {
  87. if ( !tree )
  88. return;
  89. FreeTreePortals_r (tree->headnode);
  90. FreeTree_r (tree->headnode);
  91. free (tree);
  92. }
  93. //===============================================================
  94. void PrintTree_r (node_t *node, int depth)
  95. {
  96. int i;
  97. plane_t *plane;
  98. bspbrush_t *bb;
  99. for (i=0 ; i<depth ; i++)
  100. Msg (" ");
  101. if (node->planenum == PLANENUM_LEAF)
  102. {
  103. if (!node->brushlist)
  104. Msg ("NULL\n");
  105. else
  106. {
  107. for (bb=node->brushlist ; bb ; bb=bb->next)
  108. Msg ("%i ", bb->original->brushnum);
  109. Msg ("\n");
  110. }
  111. return;
  112. }
  113. plane = &g_MainMap->mapplanes[node->planenum];
  114. Msg ("#%i (%5.2f %5.2f %5.2f):%5.2f\n", node->planenum,
  115. plane->normal[0], plane->normal[1], plane->normal[2],
  116. plane->dist);
  117. PrintTree_r (node->children[0], depth+1);
  118. PrintTree_r (node->children[1], depth+1);
  119. }
  120. /*
  121. =========================================================
  122. NODES THAT DON'T SEPERATE DIFFERENT CONTENTS CAN BE PRUNED
  123. =========================================================
  124. */
  125. int c_pruned;
  126. /*
  127. ============
  128. PruneNodes_r
  129. ============
  130. */
  131. void PruneNodes_r (node_t *node)
  132. {
  133. bspbrush_t *b, *next;
  134. if (node->planenum == PLANENUM_LEAF)
  135. return;
  136. PruneNodes_r (node->children[0]);
  137. PruneNodes_r (node->children[1]);
  138. if ( (node->children[0]->contents & CONTENTS_SOLID)
  139. && (node->children[1]->contents & CONTENTS_SOLID) )
  140. {
  141. if (node->faces)
  142. Error ("node->faces seperating CONTENTS_SOLID");
  143. if (node->children[0]->faces || node->children[1]->faces)
  144. Error ("!node->faces with children");
  145. // FIXME: free stuff
  146. node->planenum = PLANENUM_LEAF;
  147. node->contents = CONTENTS_SOLID;
  148. if (node->brushlist)
  149. Error ("PruneNodes: node->brushlist");
  150. // combine brush lists
  151. node->brushlist = node->children[1]->brushlist;
  152. for (b=node->children[0]->brushlist ; b ; b=next)
  153. {
  154. next = b->next;
  155. b->next = node->brushlist;
  156. node->brushlist = b;
  157. }
  158. c_pruned++;
  159. }
  160. }
  161. void PruneNodes (node_t *node)
  162. {
  163. qprintf ("--- PruneNodes ---\n");
  164. c_pruned = 0;
  165. PruneNodes_r (node);
  166. qprintf ("%5i pruned nodes\n", c_pruned);
  167. }
  168. //===========================================================