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.

1684 lines
36 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "vbsp.h"
  9. #include "utlvector.h"
  10. #include "mathlib/vmatrix.h"
  11. #include "iscratchpad3d.h"
  12. #include "csg.h"
  13. #include "fmtstr.h"
  14. int c_active_portals;
  15. int c_peak_portals;
  16. int c_boundary;
  17. int c_boundary_sides;
  18. /*
  19. ===========
  20. AllocPortal
  21. ===========
  22. */
  23. portal_t *AllocPortal (void)
  24. {
  25. static int s_PortalCount = 0;
  26. portal_t *p;
  27. if (numthreads == 1)
  28. c_active_portals++;
  29. if (c_active_portals > c_peak_portals)
  30. c_peak_portals = c_active_portals;
  31. p = (portal_t*)malloc (sizeof(portal_t));
  32. memset (p, 0, sizeof(portal_t));
  33. p->id = s_PortalCount;
  34. ++s_PortalCount;
  35. return p;
  36. }
  37. void FreePortal (portal_t *p)
  38. {
  39. if (p->winding)
  40. FreeWinding (p->winding);
  41. if (numthreads == 1)
  42. c_active_portals--;
  43. free (p);
  44. }
  45. //==============================================================
  46. /*
  47. ==============
  48. VisibleContents
  49. Returns the single content bit of the
  50. strongest visible content present
  51. ==============
  52. */
  53. int VisibleContents (int contents)
  54. {
  55. int i;
  56. for (i=1 ; i<=LAST_VISIBLE_CONTENTS ; i<<=1)
  57. {
  58. if (contents & i )
  59. {
  60. return i;
  61. }
  62. }
  63. return 0;
  64. }
  65. /*
  66. ===============
  67. ClusterContents
  68. ===============
  69. */
  70. int ClusterContents (node_t *node)
  71. {
  72. int c1, c2, c;
  73. if (node->planenum == PLANENUM_LEAF)
  74. return node->contents;
  75. c1 = ClusterContents(node->children[0]);
  76. c2 = ClusterContents(node->children[1]);
  77. c = c1|c2;
  78. // a cluster may include some solid detail areas, but
  79. // still be seen into
  80. if ( ! (c1&CONTENTS_SOLID) || ! (c2&CONTENTS_SOLID) )
  81. c &= ~CONTENTS_SOLID;
  82. return c;
  83. }
  84. /*
  85. =============
  86. Portal_VisFlood
  87. Returns true if the portal is empty or translucent, allowing
  88. the PVS calculation to see through it.
  89. The nodes on either side of the portal may actually be clusters,
  90. not leafs, so all contents should be ored together
  91. =============
  92. */
  93. qboolean Portal_VisFlood (portal_t *p)
  94. {
  95. int c1, c2;
  96. if (!p->onnode)
  97. return false; // to global outsideleaf
  98. c1 = ClusterContents(p->nodes[0]);
  99. c2 = ClusterContents(p->nodes[1]);
  100. if (!VisibleContents (c1^c2))
  101. return true;
  102. if (c1 & (CONTENTS_TRANSLUCENT|CONTENTS_DETAIL))
  103. c1 = 0;
  104. if (c2 & (CONTENTS_TRANSLUCENT|CONTENTS_DETAIL))
  105. c2 = 0;
  106. if ( (c1|c2) & CONTENTS_SOLID )
  107. return false; // can't see through solid
  108. if (! (c1 ^ c2))
  109. return true; // identical on both sides
  110. if (!VisibleContents (c1^c2))
  111. return true;
  112. return false;
  113. }
  114. /*
  115. ===============
  116. Portal_EntityFlood
  117. The entity flood determines which areas are
  118. "outside" on the map, which are then filled in.
  119. Flowing from side s to side !s
  120. ===============
  121. */
  122. qboolean Portal_EntityFlood (portal_t *p, int s)
  123. {
  124. if (p->nodes[0]->planenum != PLANENUM_LEAF
  125. || p->nodes[1]->planenum != PLANENUM_LEAF)
  126. Error ("Portal_EntityFlood: not a leaf");
  127. // can never cross to a solid
  128. if ( (p->nodes[0]->contents & CONTENTS_SOLID)
  129. || (p->nodes[1]->contents & CONTENTS_SOLID) )
  130. return false;
  131. // can flood through everything else
  132. return true;
  133. }
  134. qboolean Portal_AreaLeakFlood (portal_t *p, int s)
  135. {
  136. if ( !Portal_EntityFlood( p, s ) )
  137. return false;
  138. // can never cross through areaportal
  139. if ( (p->nodes[0]->contents & CONTENTS_AREAPORTAL)
  140. || (p->nodes[1]->contents & CONTENTS_AREAPORTAL) )
  141. return false;
  142. // can flood through everything else
  143. return true;
  144. }
  145. //=============================================================================
  146. int c_tinyportals;
  147. /*
  148. =============
  149. AddPortalToNodes
  150. =============
  151. */
  152. void AddPortalToNodes (portal_t *p, node_t *front, node_t *back)
  153. {
  154. if (p->nodes[0] || p->nodes[1])
  155. Error ("AddPortalToNode: allready included");
  156. p->nodes[0] = front;
  157. p->next[0] = front->portals;
  158. front->portals = p;
  159. p->nodes[1] = back;
  160. p->next[1] = back->portals;
  161. back->portals = p;
  162. }
  163. /*
  164. =============
  165. RemovePortalFromNode
  166. =============
  167. */
  168. void RemovePortalFromNode (portal_t *portal, node_t *l)
  169. {
  170. portal_t **pp, *t;
  171. // remove reference to the current portal
  172. pp = &l->portals;
  173. while (1)
  174. {
  175. t = *pp;
  176. if (!t)
  177. Error ("RemovePortalFromNode: portal not in leaf");
  178. if ( t == portal )
  179. break;
  180. if (t->nodes[0] == l)
  181. pp = &t->next[0];
  182. else if (t->nodes[1] == l)
  183. pp = &t->next[1];
  184. else
  185. Error ("RemovePortalFromNode: portal not bounding leaf");
  186. }
  187. if (portal->nodes[0] == l)
  188. {
  189. *pp = portal->next[0];
  190. portal->nodes[0] = NULL;
  191. }
  192. else if (portal->nodes[1] == l)
  193. {
  194. *pp = portal->next[1];
  195. portal->nodes[1] = NULL;
  196. }
  197. }
  198. //============================================================================
  199. void PrintPortal (portal_t *p)
  200. {
  201. int i;
  202. winding_t *w;
  203. w = p->winding;
  204. for (i=0 ; i<w->numpoints ; i++)
  205. Msg ("(%5.0f,%5.0f,%5.0f)\n",w->p[i][0]
  206. , w->p[i][1], w->p[i][2]);
  207. }
  208. // because of water areaportals support, the areaportal may not be the only brush on this node
  209. bspbrush_t *AreaportalBrushForNode( node_t *node )
  210. {
  211. bspbrush_t *b = node->brushlist;
  212. while ( b && !(b->original->contents & CONTENTS_AREAPORTAL) )
  213. {
  214. b = b->next;
  215. }
  216. Assert( b->original->entitynum != 0 );
  217. return b;
  218. }
  219. /*
  220. ================
  221. MakeHeadnodePortals
  222. The created portals will face the global outside_node
  223. ================
  224. */
  225. // buffer space around sides of nodes
  226. #define SIDESPACE 8
  227. void MakeHeadnodePortals (tree_t *tree)
  228. {
  229. Vector bounds[2];
  230. int i, j, n;
  231. portal_t *p, *portals[6];
  232. plane_t bplanes[6], *pl;
  233. node_t *node;
  234. node = tree->headnode;
  235. // pad with some space so there will never be null volume leafs
  236. for (i=0 ; i<3 ; i++)
  237. {
  238. bounds[0][i] = tree->mins[i] - SIDESPACE;
  239. bounds[1][i] = tree->maxs[i] + SIDESPACE;
  240. }
  241. tree->outside_node.planenum = PLANENUM_LEAF;
  242. tree->outside_node.brushlist = NULL;
  243. tree->outside_node.portals = NULL;
  244. tree->outside_node.contents = 0;
  245. for (i=0 ; i<3 ; i++)
  246. for (j=0 ; j<2 ; j++)
  247. {
  248. n = j*3 + i;
  249. p = AllocPortal ();
  250. portals[n] = p;
  251. pl = &bplanes[n];
  252. memset (pl, 0, sizeof(*pl));
  253. if (j)
  254. {
  255. pl->normal[i] = -1;
  256. pl->dist = -bounds[j][i];
  257. }
  258. else
  259. {
  260. pl->normal[i] = 1;
  261. pl->dist = bounds[j][i];
  262. }
  263. p->plane = *pl;
  264. p->winding = BaseWindingForPlane (pl->normal, pl->dist);
  265. AddPortalToNodes (p, node, &tree->outside_node);
  266. }
  267. // clip the basewindings by all the other planes
  268. for (i=0 ; i<6 ; i++)
  269. {
  270. for (j=0 ; j<6 ; j++)
  271. {
  272. if (j == i)
  273. continue;
  274. ChopWindingInPlace (&portals[i]->winding, bplanes[j].normal, bplanes[j].dist, ON_EPSILON);
  275. }
  276. }
  277. }
  278. //===================================================
  279. /*
  280. ================
  281. BaseWindingForNode
  282. ================
  283. */
  284. #define BASE_WINDING_EPSILON 0.001
  285. #define SPLIT_WINDING_EPSILON 0.001
  286. winding_t *BaseWindingForNode (node_t *node)
  287. {
  288. winding_t *w;
  289. node_t *n;
  290. plane_t *plane;
  291. Vector normal;
  292. vec_t dist;
  293. w = BaseWindingForPlane (g_MainMap->mapplanes[node->planenum].normal, g_MainMap->mapplanes[node->planenum].dist);
  294. // clip by all the parents
  295. for (n=node->parent ; n && w ; )
  296. {
  297. plane = &g_MainMap->mapplanes[n->planenum];
  298. if (n->children[0] == node)
  299. { // take front
  300. ChopWindingInPlace (&w, plane->normal, plane->dist, BASE_WINDING_EPSILON);
  301. }
  302. else
  303. { // take back
  304. VectorSubtract (vec3_origin, plane->normal, normal);
  305. dist = -plane->dist;
  306. ChopWindingInPlace (&w, normal, dist, BASE_WINDING_EPSILON);
  307. }
  308. node = n;
  309. n = n->parent;
  310. }
  311. return w;
  312. }
  313. //============================================================
  314. /*
  315. ==================
  316. MakeNodePortal
  317. create the new portal by taking the full plane winding for the cutting plane
  318. and clipping it by all of parents of this node
  319. ==================
  320. */
  321. void MakeNodePortal (node_t *node)
  322. {
  323. portal_t *new_portal, *p;
  324. winding_t *w;
  325. Vector normal;
  326. float dist = 0.0f;
  327. int side = 0;
  328. w = BaseWindingForNode (node);
  329. // clip the portal by all the other portals in the node
  330. for (p = node->portals ; p && w; p = p->next[side])
  331. {
  332. if (p->nodes[0] == node)
  333. {
  334. side = 0;
  335. VectorCopy (p->plane.normal, normal);
  336. dist = p->plane.dist;
  337. }
  338. else if (p->nodes[1] == node)
  339. {
  340. side = 1;
  341. VectorSubtract (vec3_origin, p->plane.normal, normal);
  342. dist = -p->plane.dist;
  343. }
  344. else
  345. {
  346. Error ("CutNodePortals_r: mislinked portal");
  347. }
  348. ChopWindingInPlace (&w, normal, dist, 0.1);
  349. }
  350. if (!w)
  351. {
  352. return;
  353. }
  354. if (WindingIsTiny (w))
  355. {
  356. c_tinyportals++;
  357. FreeWinding (w);
  358. return;
  359. }
  360. new_portal = AllocPortal ();
  361. new_portal->plane = g_MainMap->mapplanes[node->planenum];
  362. new_portal->onnode = node;
  363. new_portal->winding = w;
  364. AddPortalToNodes (new_portal, node->children[0], node->children[1]);
  365. }
  366. /*
  367. ==============
  368. SplitNodePortals
  369. Move or split the portals that bound node so that the node's
  370. children have portals instead of node.
  371. ==============
  372. */
  373. void SplitNodePortals (node_t *node)
  374. {
  375. portal_t *p, *next_portal, *new_portal;
  376. node_t *f, *b, *other_node;
  377. int side = 0;
  378. plane_t *plane;
  379. winding_t *frontwinding, *backwinding;
  380. plane = &g_MainMap->mapplanes[node->planenum];
  381. f = node->children[0];
  382. b = node->children[1];
  383. for (p = node->portals ; p ; p = next_portal)
  384. {
  385. if (p->nodes[0] == node)
  386. side = 0;
  387. else if (p->nodes[1] == node)
  388. side = 1;
  389. else
  390. Error ("CutNodePortals_r: mislinked portal");
  391. next_portal = p->next[side];
  392. other_node = p->nodes[!side];
  393. RemovePortalFromNode (p, p->nodes[0]);
  394. RemovePortalFromNode (p, p->nodes[1]);
  395. //
  396. // cut the portal into two portals, one on each side of the cut plane
  397. //
  398. ClipWindingEpsilon (p->winding, plane->normal, plane->dist,
  399. SPLIT_WINDING_EPSILON, &frontwinding, &backwinding);
  400. if (frontwinding && WindingIsTiny(frontwinding))
  401. {
  402. FreeWinding (frontwinding);
  403. frontwinding = NULL;
  404. c_tinyportals++;
  405. }
  406. if (backwinding && WindingIsTiny(backwinding))
  407. {
  408. FreeWinding (backwinding);
  409. backwinding = NULL;
  410. c_tinyportals++;
  411. }
  412. if (!frontwinding && !backwinding)
  413. { // tiny windings on both sides
  414. continue;
  415. }
  416. if (!frontwinding)
  417. {
  418. FreeWinding (backwinding);
  419. if (side == 0)
  420. AddPortalToNodes (p, b, other_node);
  421. else
  422. AddPortalToNodes (p, other_node, b);
  423. continue;
  424. }
  425. if (!backwinding)
  426. {
  427. FreeWinding (frontwinding);
  428. if (side == 0)
  429. AddPortalToNodes (p, f, other_node);
  430. else
  431. AddPortalToNodes (p, other_node, f);
  432. continue;
  433. }
  434. // the winding is split
  435. new_portal = AllocPortal ();
  436. *new_portal = *p;
  437. new_portal->winding = backwinding;
  438. FreeWinding (p->winding);
  439. p->winding = frontwinding;
  440. if (side == 0)
  441. {
  442. AddPortalToNodes (p, f, other_node);
  443. AddPortalToNodes (new_portal, b, other_node);
  444. }
  445. else
  446. {
  447. AddPortalToNodes (p, other_node, f);
  448. AddPortalToNodes (new_portal, other_node, b);
  449. }
  450. }
  451. node->portals = NULL;
  452. }
  453. /*
  454. ================
  455. CalcNodeBounds
  456. ================
  457. */
  458. void CalcNodeBounds (node_t *node)
  459. {
  460. portal_t *p;
  461. int s;
  462. int i;
  463. // calc mins/maxs for both leafs and nodes
  464. ClearBounds (node->mins, node->maxs);
  465. for (p = node->portals ; p ; p = p->next[s])
  466. {
  467. s = (p->nodes[1] == node);
  468. for (i=0 ; i<p->winding->numpoints ; i++)
  469. AddPointToBounds (p->winding->p[i], node->mins, node->maxs);
  470. }
  471. }
  472. /*
  473. ==================
  474. MakeTreePortals_r
  475. ==================
  476. */
  477. void MakeTreePortals_r (node_t *node)
  478. {
  479. int i;
  480. CalcNodeBounds (node);
  481. if (node->mins[0] >= node->maxs[0])
  482. {
  483. Warning("WARNING: node without a volume\n");
  484. }
  485. for (i=0 ; i<3 ; i++)
  486. {
  487. if (node->mins[i] < (MIN_COORD_INTEGER-SIDESPACE) || node->maxs[i] > (MAX_COORD_INTEGER+SIDESPACE))
  488. {
  489. const char *pMatName = "<NO BRUSH>";
  490. // split by brush side
  491. if ( node->side )
  492. {
  493. texinfo_t *pTexInfo = &texinfo[node->side->texinfo];
  494. dtexdata_t *pTexData = GetTexData( pTexInfo->texdata );
  495. pMatName = TexDataStringTable_GetString( pTexData->nameStringTableID );
  496. }
  497. Vector point = node->portals->winding->p[0];
  498. Warning("WARNING: BSP node with unbounded volume (material: %s, near %s)\n", pMatName, VecToString(point) );
  499. break;
  500. }
  501. }
  502. if (node->planenum == PLANENUM_LEAF)
  503. return;
  504. MakeNodePortal (node);
  505. SplitNodePortals (node);
  506. MakeTreePortals_r (node->children[0]);
  507. MakeTreePortals_r (node->children[1]);
  508. }
  509. /*
  510. ==================
  511. MakeTreePortals
  512. ==================
  513. */
  514. void MakeTreePortals (tree_t *tree)
  515. {
  516. MakeHeadnodePortals (tree);
  517. MakeTreePortals_r (tree->headnode);
  518. }
  519. /*
  520. =========================================================
  521. FLOOD ENTITIES
  522. =========================================================
  523. */
  524. //-----------------------------------------------------------------------------
  525. // Purpose: Floods outward from the given node, marking visited nodes with
  526. // the number of hops from a node with an entity. If we ever mark
  527. // the outside_node for this tree, we've leaked.
  528. // Input : node -
  529. // dist -
  530. //-----------------------------------------------------------------------------
  531. void FloodPortals_r (node_t *node, int dist)
  532. {
  533. portal_t *p;
  534. int s;
  535. node->occupied = dist;
  536. for (p=node->portals ; p ; p = p->next[s])
  537. {
  538. s = (p->nodes[1] == node);
  539. // Skip nodes that have already been marked.
  540. if (p->nodes[!s]->occupied)
  541. continue;
  542. // Skip portals that lead to or from nodes with solid contents.
  543. if (!Portal_EntityFlood (p, s))
  544. continue;
  545. FloodPortals_r (p->nodes[!s], dist+1);
  546. }
  547. }
  548. void FloodAreaLeak_r( node_t *node, int dist )
  549. {
  550. portal_t *p;
  551. int s;
  552. node->occupied = dist;
  553. for (p=node->portals ; p ; p = p->next[s])
  554. {
  555. s = (p->nodes[1] == node);
  556. if (p->nodes[!s]->occupied)
  557. continue;
  558. if (!Portal_AreaLeakFlood (p, s))
  559. continue;
  560. FloodAreaLeak_r( p->nodes[!s], dist+1 );
  561. }
  562. }
  563. void ClearOccupied_r( node_t *headnode )
  564. {
  565. if ( !headnode )
  566. return;
  567. headnode->occupied = 0;
  568. ClearOccupied_r( headnode->children[0] );
  569. ClearOccupied_r( headnode->children[1] );
  570. }
  571. void FloodAreaLeak( node_t *headnode, node_t *pFirstSide )
  572. {
  573. ClearOccupied_r( headnode );
  574. FloodAreaLeak_r( pFirstSide, 2 );
  575. }
  576. //-----------------------------------------------------------------------------
  577. // Purpose: For the given entity at the given origin, finds the leaf node in the
  578. // BSP tree that the entity occupies.
  579. //
  580. // We then flood outward from that leaf to see if the entity leaks.
  581. // Input : headnode -
  582. // origin -
  583. // occupant -
  584. // Output : Returns false if the entity is in solid, true if it is not.
  585. //-----------------------------------------------------------------------------
  586. qboolean PlaceOccupant (node_t *headnode, Vector& origin, entity_t *occupant)
  587. {
  588. node_t *node;
  589. vec_t d;
  590. plane_t *plane;
  591. // find the leaf to start in
  592. node = headnode;
  593. while (node->planenum != PLANENUM_LEAF)
  594. {
  595. plane = &g_MainMap->mapplanes[node->planenum];
  596. d = DotProduct (origin, plane->normal) - plane->dist;
  597. if (d >= 0)
  598. node = node->children[0];
  599. else
  600. node = node->children[1];
  601. }
  602. if (node->contents == CONTENTS_SOLID)
  603. return false;
  604. node->occupant = occupant;
  605. // Flood outward from here to see if this entity leaks.
  606. FloodPortals_r (node, 1);
  607. return true;
  608. }
  609. /*
  610. =============
  611. FloodEntities
  612. Marks all nodes that can be reached by entites
  613. =============
  614. */
  615. qboolean FloodEntities (tree_t *tree)
  616. {
  617. int i;
  618. Vector origin;
  619. char *cl;
  620. qboolean inside;
  621. node_t *headnode;
  622. headnode = tree->headnode;
  623. qprintf ("--- FloodEntities ---\n");
  624. inside = false;
  625. tree->outside_node.occupied = 0;
  626. for (i=1 ; i<num_entities ; i++)
  627. {
  628. GetVectorForKey (&entities[i], "origin", origin);
  629. if (VectorCompare(origin, vec3_origin))
  630. continue;
  631. cl = ValueForKey (&entities[i], "classname");
  632. origin[2] += 1; // so objects on floor are ok
  633. // nudge playerstart around if needed so clipping hulls allways
  634. // have a valid point
  635. if (!strcmp (cl, "info_player_start"))
  636. {
  637. int x, y;
  638. for (x=-16 ; x<=16 ; x += 16)
  639. {
  640. for (y=-16 ; y<=16 ; y += 16)
  641. {
  642. origin[0] += x;
  643. origin[1] += y;
  644. if (PlaceOccupant (headnode, origin, &entities[i]))
  645. {
  646. inside = true;
  647. goto gotit;
  648. }
  649. origin[0] -= x;
  650. origin[1] -= y;
  651. }
  652. }
  653. gotit: ;
  654. }
  655. else
  656. {
  657. if (PlaceOccupant (headnode, origin, &entities[i]))
  658. inside = true;
  659. }
  660. }
  661. if (!inside)
  662. {
  663. qprintf ("no entities in open -- no filling\n");
  664. }
  665. if (tree->outside_node.occupied)
  666. {
  667. qprintf ("entity reached from outside -- no filling\n" );
  668. }
  669. return (qboolean)(inside && !tree->outside_node.occupied);
  670. }
  671. /*
  672. =========================================================
  673. FLOOD AREAS
  674. =========================================================
  675. */
  676. int c_areas;
  677. bool IsAreaportalNode( node_t *node )
  678. {
  679. return ( node->contents & CONTENTS_AREAPORTAL ) ? true : false;
  680. }
  681. /*
  682. =============
  683. FloodAreas_r
  684. =============
  685. */
  686. void FloodAreas_r (node_t *node, portal_t *pSeeThrough)
  687. {
  688. portal_t *p;
  689. int s;
  690. bspbrush_t *b;
  691. entity_t *e;
  692. if ( IsAreaportalNode(node) )
  693. {
  694. // this node is part of an area portal
  695. b = AreaportalBrushForNode( node );
  696. e = &entities[b->original->entitynum];
  697. // if the current area has allready touched this
  698. // portal, we are done
  699. if (e->portalareas[0] == c_areas || e->portalareas[1] == c_areas)
  700. return;
  701. // note the current area as bounding the portal
  702. if (e->portalareas[1])
  703. {
  704. Warning("WARNING: areaportal entity %i (brush %i) touches > 2 areas\n", b->original->entitynum, b->original->id );
  705. return;
  706. }
  707. if (e->portalareas[0])
  708. {
  709. e->portalareas[1] = c_areas;
  710. e->m_pPortalsLeadingIntoAreas[1] = pSeeThrough;
  711. }
  712. else
  713. {
  714. e->portalareas[0] = c_areas;
  715. e->m_pPortalsLeadingIntoAreas[0] = pSeeThrough;
  716. }
  717. return;
  718. }
  719. if (node->area)
  720. return; // allready got it
  721. node->area = c_areas;
  722. for (p=node->portals ; p ; p = p->next[s])
  723. {
  724. s = (p->nodes[1] == node);
  725. #if 0
  726. if (p->nodes[!s]->occupied)
  727. continue;
  728. #endif
  729. if (!Portal_EntityFlood (p, s))
  730. continue;
  731. FloodAreas_r (p->nodes[!s], p);
  732. }
  733. }
  734. /*
  735. =============
  736. FindAreas_r
  737. Just decend the tree, and for each node that hasn't had an
  738. area set, flood fill out from there
  739. =============
  740. */
  741. void FindAreas_r (node_t *node)
  742. {
  743. if (node->planenum != PLANENUM_LEAF)
  744. {
  745. FindAreas_r (node->children[0]);
  746. FindAreas_r (node->children[1]);
  747. return;
  748. }
  749. if (node->area)
  750. return; // allready got it
  751. if (node->contents & CONTENTS_SOLID)
  752. return;
  753. if (!node->occupied)
  754. return; // not reachable by entities
  755. // area portals are allways only flooded into, never
  756. // out of
  757. if (IsAreaportalNode(node))
  758. return;
  759. c_areas++;
  760. FloodAreas_r (node, NULL);
  761. }
  762. void ReportAreaportalLeak( tree_t *tree, node_t *node )
  763. {
  764. portal_t *p, *pStart = NULL;
  765. int s;
  766. // Find a portal out of this areaportal into empty space
  767. for (p=node->portals ; p ; p = p->next[s])
  768. {
  769. s = (p->nodes[1] == node);
  770. if ( !Portal_EntityFlood( p, !s ) )
  771. continue;
  772. if ( p->nodes[!s]->contents & CONTENTS_AREAPORTAL )
  773. continue;
  774. pStart = p;
  775. break;
  776. }
  777. if ( pStart )
  778. {
  779. s = pStart->nodes[0] == node;
  780. Assert(!(pStart->nodes[s]->contents & CONTENTS_AREAPORTAL) );
  781. // flood fill the area outside this areaportal brush
  782. FloodAreaLeak( tree->headnode, pStart->nodes[s] );
  783. // find the portal into the longest path around the portal
  784. portal_t *pBest = NULL;
  785. int bestDist = 0;
  786. for (p=node->portals ; p ; p = p->next[s])
  787. {
  788. if ( p == pStart )
  789. continue;
  790. s = (p->nodes[1] == node);
  791. if ( p->nodes[!s]->occupied > bestDist )
  792. {
  793. pBest = p;
  794. bestDist = p->nodes[!s]->occupied;
  795. }
  796. }
  797. if ( pBest )
  798. {
  799. s = (pBest->nodes[0] == node);
  800. // write the linefile that goes from pBest to pStart
  801. AreaportalLeakFile( tree, pStart, pBest, pBest->nodes[s] );
  802. }
  803. }
  804. }
  805. /*
  806. =============
  807. SetAreaPortalAreas_r
  808. Just decend the tree, and for each node that hasn't had an
  809. area set, flood fill out from there
  810. =============
  811. */
  812. void SetAreaPortalAreas_r (tree_t *tree, node_t *node)
  813. {
  814. bspbrush_t *b;
  815. entity_t *e;
  816. if (node->planenum != PLANENUM_LEAF)
  817. {
  818. SetAreaPortalAreas_r (tree, node->children[0]);
  819. SetAreaPortalAreas_r (tree, node->children[1]);
  820. return;
  821. }
  822. if (IsAreaportalNode(node))
  823. {
  824. if (node->area)
  825. return; // already set
  826. b = AreaportalBrushForNode( node );
  827. e = &entities[b->original->entitynum];
  828. node->area = e->portalareas[0];
  829. if (!e->portalareas[1])
  830. {
  831. ReportAreaportalLeak( tree, node );
  832. Warning("\nBrush %i: areaportal brush doesn't touch two areas\n", b->original->id);
  833. return;
  834. }
  835. }
  836. }
  837. // Return a positive value between 0 and 2*PI telling the angle distance
  838. // from flBaseAngle to flTestAngle.
  839. float AngleOffset( float flBaseAngle, float flTestAngle )
  840. {
  841. while( flTestAngle > flBaseAngle )
  842. flTestAngle -= 2 * M_PI;
  843. return fmod( flBaseAngle - flTestAngle, (float) (2 * M_PI) );
  844. }
  845. int FindUniquePoints( const Vector2D *pPoints, int nPoints, int *indexMap, int nMaxIndexMapPoints, float flTolerance )
  846. {
  847. float flToleranceSqr = flTolerance * flTolerance;
  848. // This could be slightly more efficient.
  849. int nUniquePoints = 0;
  850. for ( int i=0; i < nPoints; i++ )
  851. {
  852. int j;
  853. for ( j=0; j < nUniquePoints; j++ )
  854. {
  855. if ( pPoints[i].DistToSqr( pPoints[indexMap[j]] ) < flToleranceSqr )
  856. break;
  857. }
  858. if ( j == nUniquePoints )
  859. {
  860. if ( nUniquePoints >= nMaxIndexMapPoints )
  861. Error( "FindUniquePoints: overflowed unique point list (size %d).", nMaxIndexMapPoints );
  862. indexMap[nUniquePoints++] = i;
  863. }
  864. }
  865. return nUniquePoints;
  866. }
  867. // Build a 2D convex hull of the set of points.
  868. // This essentially giftwraps the points as it walks around the perimeter.
  869. int Convex2D( Vector2D const *pPoints, int nPoints, int *indices, int nMaxIndices )
  870. {
  871. int nIndices = 0;
  872. bool touched[512];
  873. int indexMap[512];
  874. if( nPoints == 0 )
  875. return 0;
  876. // If we don't collapse the points into a unique set, we can loop around forever
  877. // and max out nMaxIndices.
  878. nPoints = FindUniquePoints( pPoints, nPoints, indexMap, ARRAYSIZE( indexMap ), 0.1f );
  879. memset( touched, 0, nPoints*sizeof(touched[0]) );
  880. // Find the (lower) left side.
  881. int i;
  882. int iBest = 0;
  883. for( i=1; i < nPoints; i++ )
  884. {
  885. if( pPoints[indexMap[i]].x < pPoints[indexMap[iBest]].x ||
  886. (pPoints[indexMap[i]].x == pPoints[indexMap[iBest]].x && pPoints[indexMap[i]].y < pPoints[indexMap[iBest]].y) )
  887. {
  888. iBest = i;
  889. }
  890. }
  891. touched[iBest] = true;
  892. indices[0] = indexMap[iBest];
  893. nIndices = 1;
  894. Vector2D curEdge( 0, 1 );
  895. // Wind around clockwise.
  896. while( 1 )
  897. {
  898. Vector2D const *pStartPoint = &pPoints[ indices[nIndices-1] ];
  899. float flEdgeAngle = atan2( curEdge.y, curEdge.x );
  900. int iMinAngle = -1;
  901. float flMinAngle = 5000;
  902. for( i=0; i < nPoints; i++ )
  903. {
  904. Vector2D vTo = pPoints[indexMap[i]] - *pStartPoint;
  905. float flDistToSqr = vTo.LengthSqr();
  906. if ( flDistToSqr <= 0.1f )
  907. continue;
  908. // Get the angle from the edge to this point.
  909. float flAngle = atan2( vTo.y, vTo.x );
  910. flAngle = AngleOffset( flEdgeAngle, flAngle );
  911. if( fabs( flAngle - flMinAngle ) < 0.00001f )
  912. {
  913. float flDistToTestSqr = pStartPoint->DistToSqr( pPoints[iMinAngle] );
  914. // If the angle is the same, pick the point farthest away.
  915. // unless the current one is closing the face loop
  916. if ( iMinAngle != indices[0] && flDistToSqr > flDistToTestSqr )
  917. {
  918. flMinAngle = flAngle;
  919. iMinAngle = indexMap[i];
  920. }
  921. }
  922. else if( flAngle < flMinAngle )
  923. {
  924. flMinAngle = flAngle;
  925. iMinAngle = indexMap[i];
  926. }
  927. }
  928. if( iMinAngle == -1 )
  929. {
  930. // Couldn't find a point?
  931. Assert( false );
  932. break;
  933. }
  934. else if( iMinAngle == indices[0] )
  935. {
  936. // Finished.
  937. break;
  938. }
  939. else
  940. {
  941. // Add this point.
  942. if( nIndices >= nMaxIndices )
  943. break;
  944. for ( int jj = 0; jj < nIndices; jj++ )
  945. {
  946. // if this assert hits, this routine is broken and is generating a spiral
  947. // rather than a closed polygon - basically an edge overlap of some kind
  948. Assert(indices[jj] != iMinAngle );
  949. }
  950. indices[nIndices] = iMinAngle;
  951. ++nIndices;
  952. }
  953. curEdge = pPoints[indices[nIndices-1]] - pPoints[indices[nIndices-2]];
  954. }
  955. return nIndices;
  956. }
  957. void FindPortalsLeadingToArea_R(
  958. node_t *pHeadNode,
  959. int iSrcArea,
  960. int iDestArea,
  961. plane_t *pPlane,
  962. CUtlVector<portal_t*> &portals )
  963. {
  964. if (pHeadNode->planenum != PLANENUM_LEAF)
  965. {
  966. FindPortalsLeadingToArea_R( pHeadNode->children[0], iSrcArea, iDestArea, pPlane, portals );
  967. FindPortalsLeadingToArea_R( pHeadNode->children[1], iSrcArea, iDestArea, pPlane, portals );
  968. return;
  969. }
  970. // Ok.. this is a leaf, check its portals.
  971. int s;
  972. for( portal_t *p = pHeadNode->portals; p ;p = p->next[!s] )
  973. {
  974. s = (p->nodes[0] == pHeadNode);
  975. if( !p->nodes[0]->occupied || !p->nodes[1]->occupied )
  976. continue;
  977. if( p->nodes[1]->area == iDestArea && p->nodes[0]->area == iSrcArea ||
  978. p->nodes[0]->area == iDestArea && p->nodes[1]->area == iSrcArea )
  979. {
  980. // Make sure the plane normals point the same way.
  981. plane_t *pMapPlane = &g_MainMap->mapplanes[p->onnode->planenum];
  982. float flDot = fabs( pMapPlane->normal.Dot( pPlane->normal ) );
  983. if( fabs( 1 - flDot ) < 0.01f )
  984. {
  985. Vector vPlanePt1 = pPlane->normal * pPlane->dist;
  986. Vector vPlanePt2 = pMapPlane->normal * pMapPlane->dist;
  987. if( vPlanePt1.DistToSqr( vPlanePt2 ) < 0.01f )
  988. {
  989. portals.AddToTail( p );
  990. }
  991. }
  992. }
  993. }
  994. }
  995. void EmitClipPortalGeometry( node_t *pHeadNode, portal_t *pPortal, int iSrcArea, dareaportal_t *dp )
  996. {
  997. // Build a list of all the points in portals from the same original face.
  998. CUtlVector<portal_t*> portals;
  999. FindPortalsLeadingToArea_R(
  1000. pHeadNode,
  1001. iSrcArea,
  1002. dp->otherarea,
  1003. &pPortal->plane,
  1004. portals );
  1005. CUtlVector<Vector> points;
  1006. for( int iPortal=0; iPortal < portals.Size(); iPortal++ )
  1007. {
  1008. portal_t *pPointPortal = portals[iPortal];
  1009. winding_t *pWinding = pPointPortal->winding;
  1010. for( int i=0; i < pWinding->numpoints; i++ )
  1011. {
  1012. points.AddToTail( pWinding->p[i] );
  1013. }
  1014. }
  1015. // Get the 2D convex hull.
  1016. //// First transform them into a plane.
  1017. QAngle vAngles;
  1018. Vector vecs[3];
  1019. VectorAngles( pPortal->plane.normal, vAngles );
  1020. AngleVectors( vAngles, &vecs[0], &vecs[1], &vecs[2] );
  1021. VMatrix mTransform;
  1022. mTransform.Identity();
  1023. mTransform.SetBasisVectors( vecs[0], vecs[1], vecs[2] );
  1024. VMatrix mInvTransform = mTransform.Transpose();
  1025. int i;
  1026. CUtlVector<Vector2D> points2D;
  1027. for( i=0; i < points.Size(); i++ )
  1028. {
  1029. Vector vTest = mTransform * points[i];
  1030. points2D.AddToTail( Vector2D( vTest.y, vTest.z ) );
  1031. }
  1032. // Build the hull.
  1033. int indices[512];
  1034. int nIndices = Convex2D( points2D.Base(), points2D.Size(), indices, 512 );
  1035. // Output the hull.
  1036. dp->m_FirstClipPortalVert = g_nClipPortalVerts;
  1037. dp->m_nClipPortalVerts = nIndices;
  1038. if ( nIndices >= 32 )
  1039. {
  1040. Warning( "Warning: area portal has %d verts. Could be a vbsp bug.\n", nIndices );
  1041. }
  1042. if( dp->m_FirstClipPortalVert + dp->m_nClipPortalVerts >= MAX_MAP_PORTALVERTS )
  1043. {
  1044. Vector *p = pPortal->winding->p;
  1045. Error( "MAX_MAP_PORTALVERTS (probably a broken areaportal near %.1f %.1f %.1f ", p->x, p->y, p->z );
  1046. }
  1047. for( i=0; i < nIndices; i++ )
  1048. {
  1049. g_ClipPortalVerts[g_nClipPortalVerts] = points[ indices[i] ];
  1050. ++g_nClipPortalVerts;
  1051. }
  1052. }
  1053. // Sets node_t::area for non-leaf nodes (this allows an optimization in the renderer).
  1054. void SetNodeAreaIndices_R( node_t *node )
  1055. {
  1056. // All leaf area indices should already be set.
  1057. if( node->planenum == PLANENUM_LEAF )
  1058. return;
  1059. // Have the children set their area indices.
  1060. SetNodeAreaIndices_R( node->children[0] );
  1061. SetNodeAreaIndices_R( node->children[1] );
  1062. // If all children (leaves or nodes) are in the same area, then set our area
  1063. // to this area as well. Otherwise, set it to -1.
  1064. if( node->children[0]->area == node->children[1]->area )
  1065. node->area = node->children[0]->area;
  1066. else
  1067. node->area = -1;
  1068. }
  1069. /*
  1070. =============
  1071. EmitAreaPortals
  1072. =============
  1073. */
  1074. void EmitAreaPortals (node_t *headnode)
  1075. {
  1076. entity_t *e;
  1077. dareaportal_t *dp;
  1078. if (c_areas > MAX_MAP_AREAS)
  1079. Error ("Map is split into too many unique areas (max = %d)\nProbably too many areaportals", MAX_MAP_AREAS);
  1080. numareas = c_areas+1;
  1081. numareaportals = 1; // leave 0 as an error
  1082. // Reset the clip portal vert info.
  1083. g_nClipPortalVerts = 0;
  1084. for (int iSrcArea=1 ; iSrcArea<=c_areas ; iSrcArea++)
  1085. {
  1086. dareas[iSrcArea].firstareaportal = numareaportals;
  1087. for (int j=0 ; j<num_entities ; j++)
  1088. {
  1089. e = &entities[j];
  1090. if (!e->areaportalnum)
  1091. continue;
  1092. if (e->portalareas[0] == iSrcArea || e->portalareas[1] == iSrcArea)
  1093. {
  1094. int iSide = (e->portalareas[0] == iSrcArea);
  1095. // We're only interested in the portal that divides the two areas.
  1096. // One of the portals that leads into the CONTENTS_AREAPORTAL just bounds
  1097. // the same two areas but the other bounds two different ones.
  1098. portal_t *pLeadingPortal = e->m_pPortalsLeadingIntoAreas[0];
  1099. if( pLeadingPortal->nodes[0]->area == pLeadingPortal->nodes[1]->area )
  1100. pLeadingPortal = e->m_pPortalsLeadingIntoAreas[1];
  1101. if( pLeadingPortal )
  1102. {
  1103. Assert( pLeadingPortal->nodes[0]->area != pLeadingPortal->nodes[1]->area );
  1104. dp = &dareaportals[numareaportals];
  1105. numareaportals++;
  1106. dp->m_PortalKey = e->areaportalnum;
  1107. dp->otherarea = e->portalareas[iSide];
  1108. dp->planenum = pLeadingPortal->onnode->planenum;
  1109. Assert( pLeadingPortal->nodes[0]->planenum == PLANENUM_LEAF );
  1110. Assert( pLeadingPortal->nodes[1]->planenum == PLANENUM_LEAF );
  1111. if( pLeadingPortal->nodes[0]->area == dp->otherarea )
  1112. {
  1113. // Use the flipped version of the plane.
  1114. dp->planenum = (dp->planenum & ~1) | (~dp->planenum & 1);
  1115. }
  1116. EmitClipPortalGeometry( headnode, pLeadingPortal, iSrcArea, dp );
  1117. }
  1118. }
  1119. }
  1120. dareas[iSrcArea].numareaportals = numareaportals - dareas[iSrcArea].firstareaportal;
  1121. }
  1122. SetNodeAreaIndices_R( headnode );
  1123. qprintf ("%5i numareas\n", numareas);
  1124. qprintf ("%5i numareaportals\n", numareaportals);
  1125. }
  1126. /*
  1127. =============
  1128. FloodAreas
  1129. Mark each leaf with an area, bounded by CONTENTS_AREAPORTAL
  1130. =============
  1131. */
  1132. void FloodAreas (tree_t *tree)
  1133. {
  1134. int start = Plat_FloatTime();
  1135. qprintf ("--- FloodAreas ---\n");
  1136. Msg("Processing areas...");
  1137. FindAreas_r (tree->headnode);
  1138. SetAreaPortalAreas_r (tree, tree->headnode);
  1139. qprintf ("%5i areas\n", c_areas);
  1140. Msg("done (%d)\n", (int)(Plat_FloatTime() - start) );
  1141. }
  1142. //======================================================
  1143. int c_outside;
  1144. int c_inside;
  1145. int c_solid;
  1146. void FillOutside_r (node_t *node)
  1147. {
  1148. if (node->planenum != PLANENUM_LEAF)
  1149. {
  1150. FillOutside_r (node->children[0]);
  1151. FillOutside_r (node->children[1]);
  1152. return;
  1153. }
  1154. // anything not reachable by an entity
  1155. // can be filled away
  1156. if (!node->occupied)
  1157. {
  1158. if (node->contents != CONTENTS_SOLID)
  1159. {
  1160. c_outside++;
  1161. node->contents = CONTENTS_SOLID;
  1162. }
  1163. else
  1164. c_solid++;
  1165. }
  1166. else
  1167. c_inside++;
  1168. }
  1169. /*
  1170. =============
  1171. FillOutside
  1172. Fill all nodes that can't be reached by entities
  1173. =============
  1174. */
  1175. void FillOutside (node_t *headnode)
  1176. {
  1177. c_outside = 0;
  1178. c_inside = 0;
  1179. c_solid = 0;
  1180. qprintf ("--- FillOutside ---\n");
  1181. FillOutside_r (headnode);
  1182. qprintf ("%5i solid leafs\n", c_solid);
  1183. qprintf ("%5i leafs filled\n", c_outside);
  1184. qprintf ("%5i inside leafs\n", c_inside);
  1185. }
  1186. static float ComputeDistFromPlane( winding_t *pWinding, plane_t *pPlane, float maxdist )
  1187. {
  1188. float totaldist = 0.0f;
  1189. for (int i = 0; i < pWinding->numpoints; ++i)
  1190. {
  1191. totaldist += fabs(DotProduct( pPlane->normal, pWinding->p[i] ) - pPlane->dist);
  1192. if (totaldist > maxdist)
  1193. return totaldist;
  1194. }
  1195. return totaldist;
  1196. }
  1197. //-----------------------------------------------------------------------------
  1198. // Display portal error
  1199. //-----------------------------------------------------------------------------
  1200. static void DisplayPortalError( portal_t *p, int viscontents )
  1201. {
  1202. char contents[3][1024];
  1203. PrintBrushContentsToString( p->nodes[0]->contents, contents[0], sizeof( contents[0] ) );
  1204. PrintBrushContentsToString( p->nodes[1]->contents, contents[1], sizeof( contents[1] ) );
  1205. PrintBrushContentsToString( viscontents, contents[2], sizeof( contents[2] ) );
  1206. Vector center;
  1207. WindingCenter( p->winding, center );
  1208. Warning( "\nFindPortalSide: Couldn't find a good match for which brush to assign to a portal near (%.1f %.1f %.1f)\n", center.x, center.y, center.z);
  1209. Warning( "Leaf 0 contents: %s\n", contents[0] );
  1210. Warning( "Leaf 1 contents: %s\n", contents[1] );
  1211. Warning( "viscontents (node 0 contents ^ node 1 contents): %s\n", contents[2] );
  1212. Warning( "This means that none of the brushes in leaf 0 or 1 that touches the portal has %s\n", contents[2] );
  1213. Warning( "Check for a huge brush enclosing the coordinates above that has contents %s\n", contents[2] );
  1214. Warning( "Candidate brush IDs: " );
  1215. CUtlVector<int> listed;
  1216. for (int j=0 ; j<2 ; j++)
  1217. {
  1218. node_t *n = p->nodes[j];
  1219. for (bspbrush_t *bb=n->brushlist ; bb ; bb=bb->next)
  1220. {
  1221. mapbrush_t *brush = bb->original;
  1222. if ( brush->contents & viscontents )
  1223. {
  1224. if ( listed.Find( brush->brushnum ) == -1 )
  1225. {
  1226. listed.AddToTail( brush->brushnum );
  1227. Warning( "Brush %d: ", brush->id );
  1228. }
  1229. }
  1230. }
  1231. }
  1232. Warning( "\n\n" );
  1233. }
  1234. //==============================================================
  1235. /*
  1236. ============
  1237. FindPortalSide
  1238. Finds a brush side to use for texturing the given portal
  1239. ============
  1240. */
  1241. void FindPortalSide (portal_t *p)
  1242. {
  1243. int viscontents;
  1244. bspbrush_t *bb;
  1245. mapbrush_t *brush;
  1246. node_t *n;
  1247. int i,j;
  1248. int planenum;
  1249. side_t *side, *bestside;
  1250. float bestdist;
  1251. plane_t *p1, *p2;
  1252. // decide which content change is strongest
  1253. // solid > lava > water, etc
  1254. viscontents = VisibleContents (p->nodes[0]->contents ^ p->nodes[1]->contents);
  1255. if (!viscontents)
  1256. {
  1257. return;
  1258. }
  1259. planenum = p->onnode->planenum;
  1260. bestside = NULL;
  1261. bestdist = 1000000;
  1262. for (j=0 ; j<2 ; j++)
  1263. {
  1264. n = p->nodes[j];
  1265. p1 = &g_MainMap->mapplanes[p->onnode->planenum];
  1266. for (bb=n->brushlist ; bb ; bb=bb->next)
  1267. {
  1268. brush = bb->original;
  1269. if ( !(brush->contents & viscontents) )
  1270. continue;
  1271. for (i=0 ; i<brush->numsides ; i++)
  1272. {
  1273. side = &brush->original_sides[i];
  1274. if (side->bevel)
  1275. continue;
  1276. if (side->texinfo == TEXINFO_NODE)
  1277. continue; // non-visible
  1278. if ((side->planenum&~1) == planenum)
  1279. { // exact match
  1280. bestside = &brush->original_sides[i];
  1281. bestdist = 0.0f;
  1282. goto gotit;
  1283. }
  1284. p2 = &g_MainMap->mapplanes[side->planenum&~1];
  1285. float dist = ComputeDistFromPlane( p->winding, p2, bestdist );
  1286. if (dist < bestdist)
  1287. {
  1288. bestside = side;
  1289. bestdist = dist;
  1290. }
  1291. }
  1292. }
  1293. }
  1294. gotit:
  1295. if (!bestside)
  1296. qprintf ("WARNING: side not found for portal\n");
  1297. // Compute average dist, check for problems...
  1298. if ((bestdist / p->winding->numpoints) > 2)
  1299. {
  1300. static int nWarnCount = 0;
  1301. if ( nWarnCount < 8 )
  1302. {
  1303. DisplayPortalError( p, viscontents );
  1304. if ( ++nWarnCount == 8 )
  1305. {
  1306. Warning("*** Suppressing further FindPortalSide errors.... ***\n" );
  1307. }
  1308. }
  1309. }
  1310. p->sidefound = true;
  1311. p->side = bestside;
  1312. }
  1313. /*
  1314. ===============
  1315. MarkVisibleSides_r
  1316. ===============
  1317. */
  1318. void MarkVisibleSides_r (node_t *node)
  1319. {
  1320. portal_t *p;
  1321. int s;
  1322. if (node->planenum != PLANENUM_LEAF)
  1323. {
  1324. MarkVisibleSides_r (node->children[0]);
  1325. MarkVisibleSides_r (node->children[1]);
  1326. return;
  1327. }
  1328. // empty leafs are never boundary leafs
  1329. if (!node->contents)
  1330. return;
  1331. // see if there is a visible face
  1332. for (p=node->portals ; p ; p = p->next[!s])
  1333. {
  1334. s = (p->nodes[0] == node);
  1335. if (!p->onnode)
  1336. continue; // edge of world
  1337. if (!p->sidefound)
  1338. FindPortalSide (p);
  1339. if (p->side)
  1340. p->side->visible = true;
  1341. }
  1342. }
  1343. /*
  1344. =============
  1345. MarkVisibleSides
  1346. =============
  1347. */
  1348. // UNDONE: Put detail brushes in a separate list (not mapbrushes) ?
  1349. void MarkVisibleSides (tree_t *tree, int startbrush, int endbrush, int detailScreen)
  1350. {
  1351. int i, j;
  1352. mapbrush_t *mb;
  1353. int numsides;
  1354. qboolean detail;
  1355. qprintf ("--- MarkVisibleSides ---\n");
  1356. // clear all the visible flags
  1357. for (i=startbrush ; i<endbrush ; i++)
  1358. {
  1359. mb = &g_MainMap->mapbrushes[i];
  1360. if ( detailScreen != FULL_DETAIL )
  1361. {
  1362. qboolean onlyDetail = (detailScreen==ONLY_DETAIL)?true:false;
  1363. // true for detail brushes
  1364. detail = (mb->contents & CONTENTS_DETAIL) ? true : false;
  1365. if ( onlyDetail ^ detail )
  1366. {
  1367. // both of these must have the same value or we're not interested in this brush
  1368. continue;
  1369. }
  1370. }
  1371. numsides = mb->numsides;
  1372. for (j=0 ; j<numsides ; j++)
  1373. mb->original_sides[j].visible = false;
  1374. }
  1375. // set visible flags on the sides that are used by portals
  1376. MarkVisibleSides_r (tree->headnode);
  1377. }
  1378. //-----------------------------------------------------------------------------
  1379. // Used to determine which sides are visible
  1380. //-----------------------------------------------------------------------------
  1381. void MarkVisibleSides (tree_t *tree, mapbrush_t **ppBrushes, int nCount )
  1382. {
  1383. qprintf ("--- MarkVisibleSides ---\n");
  1384. // clear all the visible flags
  1385. int i, j;
  1386. for ( i=0; i < nCount; ++i )
  1387. {
  1388. mapbrush_t *mb = ppBrushes[i];
  1389. int numsides = mb->numsides;
  1390. for (j=0 ; j<numsides ; j++)
  1391. {
  1392. mb->original_sides[j].visible = false;
  1393. }
  1394. }
  1395. // set visible flags on the sides that are used by portals
  1396. MarkVisibleSides_r( tree->headnode );
  1397. }