| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/main/bsp.cpp $ |
| 21 | * $Revision: 32 $ |
| 22 | * $Date: 4/19/00 10:18a $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Some more of BSP tree code, I presume. |
| 26 | * |
| 27 | * $Log: /DescentIII/main/bsp.cpp $ |
| 28 | * |
| 29 | * 32 4/19/00 10:18a Matt |
| 30 | * Added check for BSP tree initialized before freeing it. |
| 31 | * |
| 32 | * 31 4/21/99 11:05a Kevin |
| 33 | * new ps_rand and ps_srand to replace rand & srand |
| 34 | * |
| 35 | * 30 1/21/99 11:15p Jeff |
| 36 | * pulled out some structs and defines from header files and moved them |
| 37 | * into seperate header files so that multiplayer dlls don't require major |
| 38 | * game headers, just those new headers. Side effect is a shorter build |
| 39 | * time. Also cleaned up some header file #includes that weren't needed. |
| 40 | * This affected polymodel.h, object.h, player.h, vecmat.h, room.h, |
| 41 | * manage.h and multi.h |
| 42 | * |
| 43 | * 29 9/22/98 12:01p Matt |
| 44 | * Added SourceSafe headers |
| 45 | * |
| 46 | */ |
| 47 | |
| 48 | #include "bsp.h"cxx . . $ `<file>/Descent3/bsp.h`/ |
| 49 | #include "room.h"cxx . . $ `<file>/Descent3/room.h`/ |
| 50 | #include "mem.h"cxx . . $ `<file>/mem/mem.h`/ |
| 51 | #include "polymodel.h"cxx . . $ `<file>/model/polymodel.h`/ |
| 52 | #include <stdlib.h> |
| 53 | #include "object.h"cxx . . $ `<file>/Descent3/object.h`/ |
| 54 | #include "psrand.h"cxx . . $ `<file>/misc/psrand.h`/ |
| 55 | |
| 56 | #define BSP_TREE_VERSIONcxx . . $ `Descent3/bsp.cpp:56:9`! 10003 |
| 57 | |
| 58 | bsptreecxx . . $ bsptree# MineBSPcxx . . $ MineBSP.; |
| 59 | uint8_tcxx . . $ uint8_t# BSP_inittedcxx . . $ BSP_initted. = 0; |
| 60 | static int ConvexSubspacescxx . . $ ConvexSubspaces. = 0, ConvexPolyscxx . . $ ConvexPolys. = 0; |
| 61 | static int Solidscxx . . $ Solids. = 0, Emptycxx . . $ Empty. = 0; |
| 62 | |
| 63 | int BSPChecksumcxx . . $ BSPChecksum. = -1; |
| 64 | // TODO: MTS: Only used here? |
| 65 | uint8_tcxx . . $ uint8_t# UseBSPcxx . . $ UseBSP. = 0; |
| 66 | |
| 67 | // Goes through all the valid points in the indoor engine and returns a unique |
| 68 | // checksum |
| 69 | int BSPGetMineChecksumcxx . . $ BSPGetMineChecksum(b126dc7c1de90089).() { |
| 70 | int i, t, k; |
| 71 | float total = 0; |
| 72 | int checksum; |
| 73 | |
| 74 | for (i = 0; i <= Highest_room_indexcxx . . $ Highest_room_index.; i++) { |
| 75 | roomcxx . . $ room# *rp = &Roomscxx . . $ Rooms.[i]; |
| 76 | |
| 77 | if (!Roomscxx . . $ Rooms.[i].usedcxx . . $ room#used.) |
| 78 | continue; |
| 79 | |
| 80 | if (Roomscxx . . $ Rooms.[i].flagscxx . . $ room#flags. & RF_EXTERNALcxx . . $ `Descent3/room_external.h:177:9`!) |
| 81 | continue; |
| 82 | |
| 83 | for (t = 0; t < rp->num_facescxx . . $ room#num_faces.; t++) { |
| 84 | facecxx . . $ face# *fp = &rp->facescxx . . $ room#faces.[t]; |
| 85 | |
| 86 | for (k = 0; k < fp->num_vertscxx . . $ face#num_verts.; k++) { |
| 87 | total += rp->vertscxx . . $ room#verts.[fp->face_vertscxx . . $ face#face_verts.[k]].xcxx . . $ vector#x.; |
| 88 | total += rp->vertscxx . . $ room#verts.[fp->face_vertscxx . . $ face#face_verts.[k]].ycxx . . $ vector#y.; |
| 89 | total += rp->vertscxx . . $ room#verts.[fp->face_vertscxx . . $ face#face_verts.[k]].zcxx . . $ vector#z.; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | for (t = 0; t <= Highest_object_indexcxx . . $ Highest_object_index.; t++) { |
| 95 | objectcxx . . $ object# *obj = &Objectscxx . . $ Objects.[t]; |
| 96 | vectorcxx . . $ vector# vert; |
| 97 | |
| 98 | if (obj->typecxx . . $ object#type. == OBJ_NONEcxx . . $ `Descent3/object_external.h:159:9`!) |
| 99 | continue; |
| 100 | |
| 101 | if (OBJECT_OUTSIDEcxx . . $ `lib/object_external_struct.h:130:9`!(obj)) |
| 102 | continue; |
| 103 | |
| 104 | if (obj->lighting_render_typecxx . . $ object#lighting_render_type. == LRT_LIGHTMAPScxx . . $ `Descent3/object.h:651:9`!) { |
| 105 | poly_modelcxx . . $ poly_model# *po = &Poly_modelscxx . . $ Poly_models.[obj->rtypecxx . . $ object#rtype..pobj_infocxx . . $ object#$anonymous_type_10#pobj_info..model_numcxx . . $ polyobj_info#model_num.]; |
| 106 | if (!po->new_stylecxx . . $ poly_model#new_style.) |
| 107 | continue; |
| 108 | |
| 109 | for (k = 0; k < po->n_modelscxx . . $ poly_model#n_models.; k++) { |
| 110 | bsp_infocxx . . $ bsp_info# *sm = &po->submodelcxx . . $ poly_model#submodel.[k]; |
| 111 | |
| 112 | if (IsNonRenderableSubmodelcxx . . $ IsNonRenderableSubmodel(bb32db98789388a4).(po, k)) |
| 113 | continue; |
| 114 | |
| 115 | for (int j = 0; j < sm->num_facescxx . . $ bsp_info#num_faces.; j++) { |
| 116 | for (int x = 0; x < sm->facescxx . . $ bsp_info#faces.[j].nvertscxx . . $ polyface#nverts.; x++) { |
| 117 | GetObjectPointInWorldcxx . . $ GetObjectPointInWorld(a1f5449c483432f8).(&vert, obj, k, sm->facescxx . . $ bsp_info#faces.[j].vertnumscxx . . $ polyface#vertnums.[x]); |
| 118 | total += vert.xcxx . . $ vector#x.; |
| 119 | total += vert.ycxx . . $ vector#y.; |
| 120 | total += vert.zcxx . . $ vector#z.; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | checksum = total * BSP_TREE_VERSIONcxx . . $ `Descent3/bsp.cpp:56:9`!; |
| 128 | |
| 129 | return checksum; |
| 130 | } |
| 131 | |
| 132 | // Allocates memory for a bspnode |
| 133 | // Returns pointer to node |
| 134 | bspnodecxx . . $ bspnode# *NewBSPNodecxx . . $ NewBSPNode(6196f39e84d2a8c6).(void) { |
| 135 | bspnodecxx . . $ bspnode# *node; |
| 136 | |
| 137 | if ((node = (bspnodecxx . . $ bspnode# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(bspnode))) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 138 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 139 | } |
| 140 | |
| 141 | /* Initialize the node, set its variables to zero */ |
| 142 | |
| 143 | node->typecxx . . $ bspnode#type. = BSP_NODEcxx . . $ `Descent3/bsp.h:56:9`!; |
| 144 | node->polylistcxx . . $ bspnode#polylist. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 145 | node->num_polyscxx . . $ bspnode#num_polys. = 0; |
| 146 | node->node_facenumcxx . . $ bspnode#node_facenum. = 0; |
| 147 | node->node_roomnumcxx . . $ bspnode#node_roomnum. = 0; |
| 148 | node->node_subnumcxx . . $ bspnode#node_subnum. = 0; |
| 149 | |
| 150 | node->planecxx . . $ bspnode#plane..acxx . . $ bspplane#a. = 0.0; |
| 151 | node->planecxx . . $ bspnode#plane..bcxx . . $ bspplane#b. = 0.0; |
| 152 | node->planecxx . . $ bspnode#plane..ccxx . . $ bspplane#c. = 0.0; |
| 153 | node->planecxx . . $ bspnode#plane..dcxx . . $ bspplane#d. = 0.0; |
| 154 | node->frontcxx . . $ bspnode#front. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 155 | node->backcxx . . $ bspnode#back. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 156 | |
| 157 | return node; |
| 158 | } |
| 159 | |
| 160 | // Allocates memory for a polygon |
| 161 | // Returns pointer to polygon |
| 162 | bsppolygoncxx . . $ bsppolygon# *NewPolygoncxx . . $ NewPolygon(97c438c4b083d002).(int roomnum, int facenum, int numverts) { |
| 163 | bsppolygoncxx . . $ bsppolygon# *newpoly; |
| 164 | vectorcxx . . $ vector# *verts; |
| 165 | |
| 166 | newpoly = (bsppolygoncxx . . $ bsppolygon# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(bsppolygon)); |
| 167 | |
| 168 | if (newpoly == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 169 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "NewPolygon: Couldn't allocate polygon\n"); |
| 170 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 171 | } |
| 172 | |
| 173 | verts = (vectorcxx . . $ vector# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(vector) * numverts); |
| 174 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(verts != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 175 | |
| 176 | newpoly->nvcxx . . $ bsppolygon#nv. = numverts; |
| 177 | newpoly->vertscxx . . $ bsppolygon#verts. = verts; |
| 178 | |
| 179 | newpoly->roomnumcxx . . $ bsppolygon#roomnum. = roomnum; |
| 180 | newpoly->facenumcxx . . $ bsppolygon#facenum. = facenum; |
| 181 | newpoly->colorcxx . . $ bsppolygon#color. = ps_randcxx . . $ ps_rand(b126dc7c1de90089).() * ps_randcxx . . $ ps_rand(b126dc7c1de90089).(); |
| 182 | |
| 183 | return newpoly; |
| 184 | } |
| 185 | |
| 186 | // Saves and BSP node to an open file and recurses with the nodes children |
| 187 | void SaveBSPNodecxx . . $ SaveBSPNode(4506aeb41eee2e7).(CFILEcxx . . $ CFILE# *outfile, bspnodecxx . . $ bspnode# *node) { |
| 188 | if (node->typecxx . . $ bspnode#type. == BSP_SOLID_LEAFcxx . . $ `Descent3/bsp.h:58:9`!) { |
| 189 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(outfile, BSP_SOLID_LEAFcxx . . $ `Descent3/bsp.h:58:9`!); |
| 190 | } else if (node->typecxx . . $ bspnode#type. == BSP_EMPTY_LEAFcxx . . $ `Descent3/bsp.h:57:9`!) { |
| 191 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(outfile, BSP_EMPTY_LEAFcxx . . $ `Descent3/bsp.h:57:9`!); |
| 192 | } else { |
| 193 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(node->front); |
| 194 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(node->back); |
| 195 | |
| 196 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(outfile, BSP_NODEcxx . . $ `Descent3/bsp.h:56:9`!); |
| 197 | |
| 198 | // Write out this planes node |
| 199 | cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(outfile, node->planecxx . . $ bspnode#plane..acxx . . $ bspplane#a.); |
| 200 | cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(outfile, node->planecxx . . $ bspnode#plane..bcxx . . $ bspplane#b.); |
| 201 | cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(outfile, node->planecxx . . $ bspnode#plane..ccxx . . $ bspplane#c.); |
| 202 | cf_WriteFloatcxx . . $ cf_WriteFloat(c51c6cf5f6d5270c).(outfile, node->planecxx . . $ bspnode#plane..dcxx . . $ bspplane#d.); |
| 203 | cf_WriteShortcxx . . $ cf_WriteShort(d7cf1a05aca96fe5).(outfile, node->node_roomnumcxx . . $ bspnode#node_roomnum.); |
| 204 | cf_WriteShortcxx . . $ cf_WriteShort(d7cf1a05aca96fe5).(outfile, node->node_facenumcxx . . $ bspnode#node_facenum.); |
| 205 | cf_WriteBytecxx . . $ cf_WriteByte(17377ebd5835367f).(outfile, node->node_subnumcxx . . $ bspnode#node_subnum.); |
| 206 | |
| 207 | // Write out its children |
| 208 | SaveBSPNodecxx . . $ SaveBSPNode(4506aeb41eee2e7).(outfile, node->frontcxx . . $ bspnode#front.); |
| 209 | SaveBSPNodecxx . . $ SaveBSPNode(4506aeb41eee2e7).(outfile, node->backcxx . . $ bspnode#back.); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Loads a bsp node from an open file and recurses with its children |
| 214 | void LoadBSPNodecxx . . $ LoadBSPNode(f7470add2ab7fa30).(CFILEcxx . . $ CFILE# *infile, bspnodecxx . . $ bspnode# **node) { |
| 215 | // Get the node type |
| 216 | uint8_tcxx . . $ uint8_t# type = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 217 | |
| 218 | // Allocate the node and set its type |
| 219 | *node = NewBSPNodecxx . . $ NewBSPNode(6196f39e84d2a8c6).(); |
| 220 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(*node); |
| 221 | bspnodecxx . . $ bspnode# *localnode = *node; |
| 222 | |
| 223 | localnode->typecxx . . $ bspnode#type. = type; |
| 224 | |
| 225 | if (type == BSP_EMPTY_LEAFcxx . . $ `Descent3/bsp.h:57:9`! || type == BSP_SOLID_LEAFcxx . . $ `Descent3/bsp.h:58:9`!) |
| 226 | return; |
| 227 | |
| 228 | // Load the nodes plane |
| 229 | localnode->planecxx . . $ bspnode#plane..acxx . . $ bspplane#a. = cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(infile); |
| 230 | localnode->planecxx . . $ bspnode#plane..bcxx . . $ bspplane#b. = cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(infile); |
| 231 | localnode->planecxx . . $ bspnode#plane..ccxx . . $ bspplane#c. = cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(infile); |
| 232 | localnode->planecxx . . $ bspnode#plane..dcxx . . $ bspplane#d. = cf_ReadFloatcxx . . $ cf_ReadFloat(9a03a00aead0d542).(infile); |
| 233 | localnode->node_roomnumcxx . . $ bspnode#node_roomnum. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 234 | localnode->node_facenumcxx . . $ bspnode#node_facenum. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(infile); |
| 235 | localnode->node_subnumcxx . . $ bspnode#node_subnum. = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(infile); |
| 236 | |
| 237 | // Recurse to get the children |
| 238 | LoadBSPNodecxx . . $ LoadBSPNode(f7470add2ab7fa30).(infile, &localnode->frontcxx . . $ bspnode#front.); |
| 239 | LoadBSPNodecxx . . $ LoadBSPNode(f7470add2ab7fa30).(infile, &localnode->backcxx . . $ bspnode#back.); |
| 240 | } |
| 241 | |
| 242 | // Frees up a polygon and all the vertices associated with it |
| 243 | void FreePolygoncxx . . $ FreePolygon(444f9c48bcf7d9ca).(bsppolygoncxx . . $ bsppolygon# *poly) { |
| 244 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(poly != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 245 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(poly->nv > 2); |
| 246 | |
| 247 | if (poly->nvcxx . . $ bsppolygon#nv. > 0) { |
| 248 | mem_freecxx . . $ `mem/mem.h:80:9`!(poly->verts); |
| 249 | } |
| 250 | |
| 251 | mem_freecxx . . $ `mem/mem.h:80:9`!(poly); |
| 252 | } |
| 253 | |
| 254 | // Calculates the plane equation for a given polygon |
| 255 | void CalculatePolygonPlanecxx . . $ CalculatePolygonPlane(444f9c48bcf7d9ca).(bsppolygoncxx . . $ bsppolygon# *poly) { |
| 256 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(poly->nv >= 3); |
| 257 | |
| 258 | vectorcxx . . $ vector# *vec = &poly->vertscxx . . $ bsppolygon#verts.[0]; |
| 259 | |
| 260 | poly->planecxx . . $ bsppolygon#plane..dcxx . . $ bspplane#d. = -(vec->xcxx . . $ vector#x. * poly->planecxx . . $ bsppolygon#plane..acxx . . $ bspplane#a. + vec->ycxx . . $ vector#y. * poly->planecxx . . $ bsppolygon#plane..bcxx . . $ bspplane#b. + vec->zcxx . . $ vector#z. * poly->planecxx . . $ bsppolygon#plane..ccxx . . $ bspplane#c.); |
| 261 | } |
| 262 | |
| 263 | // Classifies whether or not a point is in front,behind, or on a plane |
| 264 | int ClassifyVectorcxx . . $ ClassifyVector(f094b1bd794b7e86).(bspplanecxx . . $ bspplane# *plane, vectorcxx . . $ vector# *vec) { |
| 265 | float dist; |
| 266 | |
| 267 | dist = vec->xcxx . . $ vector#x. * plane->acxx . . $ bspplane#a. + vec->ycxx . . $ vector#y. * plane->bcxx . . $ bspplane#b. + vec->zcxx . . $ vector#z. * plane->ccxx . . $ bspplane#c. + plane->dcxx . . $ bspplane#d.; |
| 268 | |
| 269 | if (dist > BSP_EPSILONcxx . . $ `Descent3/bsp.h:54:9`!) |
| 270 | return BSP_IN_FRONTcxx . . $ `Descent3/bsp.h:48:9`!; |
| 271 | else if (dist < -BSP_EPSILONcxx . . $ `Descent3/bsp.h:54:9`!) |
| 272 | return BSP_BEHINDcxx . . $ `Descent3/bsp.h:49:9`!; |
| 273 | |
| 274 | return BSP_ON_PLANEcxx . . $ `Descent3/bsp.h:50:9`!; |
| 275 | } |
| 276 | |
| 277 | // Classifies whether or not a polygon is behind,in front, or straddles a plane |
| 278 | int ClassifyPolygoncxx . . $ ClassifyPolygon(277ec9bd376ba3be).(bspplanecxx . . $ bspplane# *plane, bsppolygoncxx . . $ bsppolygon# *poly) { |
| 279 | int numfront, numback, numon; |
| 280 | int i; |
| 281 | int nv = poly->nvcxx . . $ bsppolygon#nv.; |
| 282 | |
| 283 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(nv >= 3); |
| 284 | |
| 285 | numfront = numback = numon = 0; |
| 286 | |
| 287 | for (i = 0; i < nv; i++) { |
| 288 | int fate; |
| 289 | vectorcxx . . $ vector# *vec = &poly->vertscxx . . $ bsppolygon#verts.[i]; |
| 290 | |
| 291 | fate = ClassifyVectorcxx . . $ ClassifyVector(f094b1bd794b7e86).(plane, vec); |
| 292 | |
| 293 | switch (fate) { |
| 294 | case BSP_IN_FRONTcxx . . $ `Descent3/bsp.h:48:9`!: |
| 295 | numfront++; |
| 296 | break; |
| 297 | case BSP_BEHINDcxx . . $ `Descent3/bsp.h:49:9`!: |
| 298 | numback++; |
| 299 | break; |
| 300 | case BSP_ON_PLANEcxx . . $ `Descent3/bsp.h:50:9`!: |
| 301 | numfront++; |
| 302 | numback++; |
| 303 | numon++; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (numon == nv) { |
| 309 | return BSP_COINCIDENTcxx . . $ `Descent3/bsp.h:52:9`!; |
| 310 | } else if (numfront == nv) { |
| 311 | return BSP_IN_FRONTcxx . . $ `Descent3/bsp.h:48:9`!; |
| 312 | } else if (numback == nv) { |
| 313 | return BSP_BEHINDcxx . . $ `Descent3/bsp.h:49:9`!; |
| 314 | } |
| 315 | |
| 316 | // The polygon straddles the plane |
| 317 | return BSP_SPANNINGcxx . . $ `Descent3/bsp.h:51:9`!; |
| 318 | } |
| 319 | |
| 320 | // Tries to split a polygon across a plane |
| 321 | int SplitPolygoncxx . . $ SplitPolygon(a400e6726716d1f9).(bspplanecxx . . $ bspplane# *plane, bsppolygoncxx . . $ bsppolygon# *testpoly, bsppolygoncxx . . $ bsppolygon# **frontpoly, bsppolygoncxx . . $ bsppolygon# **backpoly) { |
| 322 | float dists[256], t; |
| 323 | int numvert, numfront, numback, i, codes[256] = {}; |
| 324 | vectorcxx . . $ vector# *frontvert[256], *backvert[256], *polyvert[256]; |
| 325 | vectorcxx . . $ vector# *vertptr1, *vertptr2; |
| 326 | vectorcxx . . $ vector# delta, *newvert[256]; |
| 327 | int num_new_verts = 0; |
| 328 | |
| 329 | /* Set up the function. Set all counters, variables, lists etc to |
| 330 | * zero or NULL, which provides a fall back result, should nothing |
| 331 | * be changed, and simply means that particular result is to be |
| 332 | * ignored |
| 333 | */ |
| 334 | |
| 335 | numvert = testpoly->nvcxx . . $ bsppolygon#nv.; |
| 336 | numfront = numback = 0; |
| 337 | |
| 338 | /* Now, we shall classify each vertex in the polygon, with both |
| 339 | * a plane distance, and a classification code. This is done here, |
| 340 | * as the results are often re-used, which saves time. |
| 341 | */ |
| 342 | |
| 343 | for (i = 0; i < numvert; i++) { |
| 344 | vertptr1 = &testpoly->vertscxx . . $ bsppolygon#verts.[i]; |
| 345 | polyvert[i] = vertptr1; |
| 346 | codes[i] = ClassifyVectorcxx . . $ ClassifyVector(f094b1bd794b7e86).(plane, vertptr1); |
| 347 | dists[i] = plane->acxx . . $ bspplane#a. * vertptr1->xcxx . . $ vector#x. + plane->bcxx . . $ bspplane#b. * vertptr1->ycxx . . $ vector#y. + plane->ccxx . . $ bspplane#c. * vertptr1->zcxx . . $ vector#z. + plane->dcxx . . $ bspplane#d.; |
| 348 | } |
| 349 | |
| 350 | /* We must duplicate the first entry in the numvert+1 slot, so that |
| 351 | * we can use wraparound to easily check things |
| 352 | */ |
| 353 | |
| 354 | codes[numvert] = codes[0]; |
| 355 | dists[numvert] = dists[0]; |
| 356 | |
| 357 | /* Now, the actual splitting work. We must work through each vertex |
| 358 | * of the polygon, examining it with regard to the plane |
| 359 | * Where the classification codes differ, we have to split |
| 360 | */ |
| 361 | |
| 362 | for (i = 0; i < numvert; i++) { |
| 363 | vertptr1 = polyvert[i]; |
| 364 | |
| 365 | /* A point is found to be on the plane. This counts for |
| 366 | * both polygons, as often a vertex may lie along a plane, |
| 367 | * but will not be splitting it |
| 368 | */ |
| 369 | |
| 370 | if (codes[i] == BSP_ON_PLANEcxx . . $ `Descent3/bsp.h:50:9`!) { |
| 371 | frontvert[numfront++] = vertptr1; |
| 372 | backvert[numback++] = vertptr1; |
| 373 | } else if (codes[i] == BSP_IN_FRONTcxx . . $ `Descent3/bsp.h:48:9`!) { |
| 374 | /* Simple cases of point being in front or behind |
| 375 | * the plane. Just insert them to the appropriate |
| 376 | * lists. |
| 377 | */ |
| 378 | frontvert[numfront++] = vertptr1; |
| 379 | } else if (codes[i] == BSP_BEHINDcxx . . $ `Descent3/bsp.h:49:9`!) { |
| 380 | backvert[numback++] = vertptr1; |
| 381 | } |
| 382 | |
| 383 | /* If we're on the plane, or, the next point is the same |
| 384 | * side of the plane as this point, go to the next vertex |
| 385 | */ |
| 386 | |
| 387 | if ((codes[i] == BSP_ON_PLANEcxx . . $ `Descent3/bsp.h:50:9`!) || (codes[i] == codes[i + 1])) |
| 388 | continue; |
| 389 | |
| 390 | /* Now, we need to generate a split point. This is just |
| 391 | * standard ray-plane intersection stuff. Now this is where |
| 392 | * pre-calculating the distances etc comes in handy, as |
| 393 | * we're using them a lot... |
| 394 | */ |
| 395 | |
| 396 | vertptr2 = polyvert[(i + 1) % numvert]; |
| 397 | t = dists[i] / (dists[i] - dists[i + 1]); |
| 398 | newvert[num_new_verts] = (vectorcxx . . $ vector# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(sizeof(vector)); |
| 399 | |
| 400 | /* Now we must generate the split point. This is simply |
| 401 | * an equation in the form Origin + t*Direction |
| 402 | */ |
| 403 | |
| 404 | delta =cxx . . $ vector#`operator=`(c0842353a20857d). *vertptr2 -cxx . . $ operator-(b77b703cd1e43e33). *vertptr1; |
| 405 | *newvert[num_new_verts] =cxx . . $ vector#`operator=`(c0842353a20857d). *vertptr1 +cxx . . $ operator+(b77b703cd1e43e33). (t *cxx . . $ `operator*`(9d6577ff3e9faa29). delta); |
| 406 | |
| 407 | /* vector tempvec=*newvert[num_new_verts]-*vertptr1; |
| 408 | float mag=vm_GetMagnitude(&tempvec); |
| 409 | if (mag<=BSP_EPSILON) |
| 410 | Int3(); */ |
| 411 | |
| 412 | frontvert[numfront++] = newvert[num_new_verts]; |
| 413 | backvert[numback++] = newvert[num_new_verts]; |
| 414 | num_new_verts++; |
| 415 | } |
| 416 | |
| 417 | /* Now, we must construct the polygons, given the vertex lists |
| 418 | * If the number of vertices is invalid, we don't do anything ... |
| 419 | */ |
| 420 | |
| 421 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(numfront >= 3); |
| 422 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(numback >= 3); |
| 423 | |
| 424 | (*frontpoly) = NewPolygoncxx . . $ NewPolygon(97c438c4b083d002).(testpoly->roomnumcxx . . $ bsppolygon#roomnum., testpoly->facenumcxx . . $ bsppolygon#facenum., numfront); |
| 425 | (*backpoly) = NewPolygoncxx . . $ NewPolygon(97c438c4b083d002).(testpoly->roomnumcxx . . $ bsppolygon#roomnum., testpoly->facenumcxx . . $ bsppolygon#facenum., numback); |
| 426 | |
| 427 | for (i = 0; i < numfront; i++) { |
| 428 | bsppolygoncxx . . $ bsppolygon# *temppoly = *frontpoly; |
| 429 | temppoly->vertscxx . . $ bsppolygon#verts.[i] =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). *frontvert[i]; |
| 430 | temppoly->planecxx . . $ bsppolygon#plane. =cxx . . $ bspplane#`operator=`(29215c698b3d783). testpoly->planecxx . . $ bsppolygon#plane.; |
| 431 | temppoly->facenumcxx . . $ bsppolygon#facenum. = testpoly->facenumcxx . . $ bsppolygon#facenum.; |
| 432 | temppoly->roomnumcxx . . $ bsppolygon#roomnum. = testpoly->roomnumcxx . . $ bsppolygon#roomnum.; |
| 433 | temppoly->subnumcxx . . $ bsppolygon#subnum. = testpoly->subnumcxx . . $ bsppolygon#subnum.; |
| 434 | } |
| 435 | |
| 436 | for (i = 0; i < numback; i++) { |
| 437 | bsppolygoncxx . . $ bsppolygon# *temppoly = *backpoly; |
| 438 | temppoly->vertscxx . . $ bsppolygon#verts.[i] =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). *backvert[i]; |
| 439 | temppoly->planecxx . . $ bsppolygon#plane. =cxx . . $ bspplane#`operator=`(29215c698b3d783). testpoly->planecxx . . $ bsppolygon#plane.; |
| 440 | temppoly->facenumcxx . . $ bsppolygon#facenum. = testpoly->facenumcxx . . $ bsppolygon#facenum.; |
| 441 | temppoly->roomnumcxx . . $ bsppolygon#roomnum. = testpoly->roomnumcxx . . $ bsppolygon#roomnum.; |
| 442 | temppoly->subnumcxx . . $ bsppolygon#subnum. = testpoly->subnumcxx . . $ bsppolygon#subnum.; |
| 443 | } |
| 444 | |
| 445 | for (i = 0; i < num_new_verts; i++) |
| 446 | mem_freecxx . . $ `mem/mem.h:80:9`!(newvert[i]); |
| 447 | |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | char Twirlycxx . . $ Twirly.[] = {'|', '/', '-', '\\'}; |
| 452 | uint8_tcxx . . $ uint8_t# Plane_twirlcxx . . $ Plane_twirl. = 0; |
| 453 | uint8_tcxx . . $ uint8_t# Node_twirlcxx . . $ Node_twirl. = 0; |
| 454 | |
| 455 | // Selects the best plane to partition with, returning the pointer to polygon to split with |
| 456 | bsppolygoncxx . . $ bsppolygon# *SelectPlanecxx . . $ SelectPlane(d17f744472f21a5b).(listnodecxx . . $ listnode# **polylist) { |
| 457 | listnodecxx . . $ listnode# *outer, *inner; |
| 458 | bsppolygoncxx . . $ bsppolygon# *outerpoly, *innerpoly, *bestpoly; |
| 459 | int splits, front, back, planar, result, balance; |
| 460 | float score, bestscore; |
| 461 | float g_kbalance = 1.0; |
| 462 | float g_ksplit = 0.0; |
| 463 | float g_kplanar = 0.0; |
| 464 | int checked = 0; |
| 465 | |
| 466 | bestscore = 9999999.0; |
| 467 | bestpoly = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 468 | |
| 469 | /* Iterate through every polygon, checking its plane against all |
| 470 | * the other polygons. But don't check it against itself |
| 471 | */ |
| 472 | |
| 473 | mprintf_atcxx . . $ `ddebug/mono.h:83:9`!(2, 4, 0, "Plane = %c", Twirly[(Plane_twirl++) % 4]); |
| 474 | |
| 475 | for (outer = *polylist; outer != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; outer = outer->nextcxx . . $ listnode#next.) { |
| 476 | outerpoly = (bsppolygoncxx . . $ bsppolygon# *)outer->datacxx . . $ listnode#data.; |
| 477 | splits = front = back = planar = 0; |
| 478 | score = 0.0; |
| 479 | |
| 480 | if (outerpoly->planecxx . . $ bsppolygon#plane..usedcxx . . $ bspplane#used.) |
| 481 | continue; |
| 482 | |
| 483 | // Only check 300 times |
| 484 | if (checked >= 300) |
| 485 | return bestpoly; |
| 486 | |
| 487 | for (inner = *polylist; inner != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; inner = inner->nextcxx . . $ listnode#next.) { |
| 488 | innerpoly = (bsppolygoncxx . . $ bsppolygon# *)inner->datacxx . . $ listnode#data.; |
| 489 | if (innerpoly != outerpoly) { |
| 490 | result = ClassifyPolygoncxx . . $ ClassifyPolygon(277ec9bd376ba3be).(&outerpoly->planecxx . . $ bsppolygon#plane., innerpoly); |
| 491 | switch (result) { |
| 492 | case BSP_SPANNINGcxx . . $ `Descent3/bsp.h:51:9`!: |
| 493 | splits++; |
| 494 | break; |
| 495 | case BSP_IN_FRONTcxx . . $ `Descent3/bsp.h:48:9`!: |
| 496 | front++; |
| 497 | break; |
| 498 | case BSP_BEHINDcxx . . $ `Descent3/bsp.h:49:9`!: |
| 499 | back++; |
| 500 | break; |
| 501 | case BSP_COINCIDENTcxx . . $ `Descent3/bsp.h:52:9`!: |
| 502 | planar++; |
| 503 | break; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* Calculate the score. If we have a "perfect" tree, ie |
| 509 | * it has either no splits, or is perfectly balanced, |
| 510 | * return the plane to the caller |
| 511 | */ |
| 512 | |
| 513 | balance = abscxx . . $ abs(a1851578aea3f541).(front - back); |
| 514 | score = g_kbalance * balance + g_ksplit * splits + g_kplanar * planar; |
| 515 | |
| 516 | checked++; |
| 517 | |
| 518 | if (score < 0.005) |
| 519 | return outerpoly; |
| 520 | |
| 521 | if (score < bestscore) { |
| 522 | bestscore = score; |
| 523 | bestpoly = outerpoly; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return bestpoly; |
| 528 | } |
| 529 | |
| 530 | // This is the routine that recursively builds the bsptree |
| 531 | int BuildBSPNodecxx . . $ BuildBSPNode(562d90246a8fabae).(bspnodecxx . . $ bspnode# *tree, listnodecxx . . $ listnode# **polylist, int numpolys) { |
| 532 | bsppolygoncxx . . $ bsppolygon# *partition_poly; |
| 533 | listnodecxx . . $ listnode# *lnode = *polylist; |
| 534 | listnodecxx . . $ listnode# *next; |
| 535 | bspnodecxx . . $ bspnode# *frontnode, *backnode; |
| 536 | listnodecxx . . $ listnode# *frontlist = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, *backlist = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 537 | bspplanecxx . . $ bspplane# partition_plane; |
| 538 | int numfront = 0, numback = 0, numsplits = 0; |
| 539 | |
| 540 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(numpolys > 0); |
| 541 | partition_poly = SelectPlanecxx . . $ SelectPlane(d17f744472f21a5b).(polylist); |
| 542 | |
| 543 | mprintf_atcxx . . $ `ddebug/mono.h:83:9`!(2, 5, 0, "Node = %c", Twirly[(Node_twirl++) % 4]); |
| 544 | |
| 545 | if (partition_poly == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 546 | // We hit a leaf! Fill in the appropriate leaf stuff |
| 547 | |
| 548 | /*vector center; |
| 549 | int total=0; |
| 550 | |
| 551 | vm_MakeZero (¢er); |
| 552 | |
| 553 | for (int i=0;i<numpolys;i++) |
| 554 | { |
| 555 | bsppolygon *poly=(bsppolygon *)GetListItem (polylist,i); |
| 556 | ASSERT (poly!=NULL); |
| 557 | AddListItem (&tree->polylist,poly); |
| 558 | tree->num_polys++; |
| 559 | vector polycenter; |
| 560 | |
| 561 | ConvexPolys++; |
| 562 | vm_MakeZero (&polycenter); |
| 563 | |
| 564 | // Find out if this is a solid or empty leaf |
| 565 | // This routine may cause some problems because of convex subspace |
| 566 | for (int t=0;t<poly->nv;t++) |
| 567 | { |
| 568 | polycenter+=poly->verts[t]; |
| 569 | |
| 570 | } |
| 571 | |
| 572 | polycenter/=poly->nv; |
| 573 | center+=polycenter; |
| 574 | |
| 575 | } |
| 576 | |
| 577 | center/=numpolys; |
| 578 | int ins=0,outs=0; |
| 579 | |
| 580 | for (i=0;i<numpolys;i++) |
| 581 | { |
| 582 | bsppolygon *thispoly=(bsppolygon *)GetListItem (polylist,i); |
| 583 | int fate=ClassifyVector (&thispoly->plane,¢er); |
| 584 | |
| 585 | if (fate==BSP_IN_FRONT || fate==BSP_ON_PLANE) |
| 586 | { |
| 587 | outs++; |
| 588 | } |
| 589 | else |
| 590 | ins++; |
| 591 | |
| 592 | } |
| 593 | |
| 594 | if (ins==numpolys) |
| 595 | { |
| 596 | tree->type=BSP_SOLID_LEAF; |
| 597 | Solids++; |
| 598 | } |
| 599 | else if (outs==numpolys) |
| 600 | { |
| 601 | tree->type=BSP_EMPTY_LEAF; |
| 602 | Empty++; |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | mprintf(0,"Ins/outs don't match! ins=%d outs=%d np=%d\n",ins,outs,numpolys); |
| 607 | |
| 608 | for (i=0;i<numpolys;i++) |
| 609 | { |
| 610 | bsppolygon *thispoly=(bsppolygon *)GetListItem (polylist,i); |
| 611 | mprintf(0,"Poly %d: Room=%d facenum=%d\n",i,thispoly->roomnum,thispoly->facenum); |
| 612 | } |
| 613 | |
| 614 | if (ins>outs) |
| 615 | { |
| 616 | tree->type=BSP_SOLID_LEAF; |
| 617 | Solids++; |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | tree->type=BSP_EMPTY_LEAF; |
| 622 | Empty++; |
| 623 | } |
| 624 | |
| 625 | }*/ |
| 626 | |
| 627 | tree->typecxx . . $ bspnode#type. = BSP_EMPTY_LEAFcxx . . $ `Descent3/bsp.h:57:9`!; |
| 628 | |
| 629 | ConvexSubspacescxx . . $ ConvexSubspaces.++; |
| 630 | return 1; |
| 631 | } |
| 632 | |
| 633 | // We need to process this node and classify all child polygons |
| 634 | tree->node_facenumcxx . . $ bspnode#node_facenum. = partition_poly->facenumcxx . . $ bsppolygon#facenum.; |
| 635 | tree->node_roomnumcxx . . $ bspnode#node_roomnum. = partition_poly->roomnumcxx . . $ bsppolygon#roomnum.; |
| 636 | tree->node_subnumcxx . . $ bspnode#node_subnum. = partition_poly->subnumcxx . . $ bsppolygon#subnum.; |
| 637 | |
| 638 | partition_poly->planecxx . . $ bsppolygon#plane..usedcxx . . $ bspplane#used. = 1; |
| 639 | tree->planecxx . . $ bspnode#plane. =cxx . . $ bspplane#`operator=`(29215c698b3d783). partition_poly->planecxx . . $ bsppolygon#plane.; |
| 640 | partition_plane =cxx . . $ bspplane#`operator=`(29215c698b3d783). partition_poly->planecxx . . $ bsppolygon#plane.; |
| 641 | |
| 642 | frontlist = backlist = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 643 | |
| 644 | for (lnode = *polylist; lnode != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; lnode = next) { |
| 645 | next = lnode->nextcxx . . $ listnode#next.; |
| 646 | |
| 647 | bsppolygoncxx . . $ bsppolygon# *testpoly = (bsppolygoncxx . . $ bsppolygon# *)lnode->datacxx . . $ listnode#data.; |
| 648 | int fate = ClassifyPolygoncxx . . $ ClassifyPolygon(277ec9bd376ba3be).(&partition_plane, testpoly); |
| 649 | |
| 650 | if (fate == BSP_IN_FRONTcxx . . $ `Descent3/bsp.h:48:9`!) { |
| 651 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&frontlist, testpoly); |
| 652 | numfront++; |
| 653 | } else if (fate == BSP_BEHINDcxx . . $ `Descent3/bsp.h:49:9`!) { |
| 654 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&backlist, testpoly); |
| 655 | numback++; |
| 656 | } else if (fate == BSP_COINCIDENTcxx . . $ `Descent3/bsp.h:52:9`!) { |
| 657 | // Test to see if this plane is exactly the same as the partition plane |
| 658 | // If so, remove it from the list. If its facing the other way, send it down the back list |
| 659 | float dot = testpoly->planecxx . . $ bsppolygon#plane..acxx . . $ bspplane#a. * partition_plane.acxx . . $ bspplane#a. + testpoly->planecxx . . $ bsppolygon#plane..bcxx . . $ bspplane#b. * partition_plane.bcxx . . $ bspplane#b. + |
| 660 | testpoly->planecxx . . $ bsppolygon#plane..ccxx . . $ bspplane#c. * partition_plane.ccxx . . $ bspplane#c.; |
| 661 | |
| 662 | if (dot > -BSP_EPSILONcxx . . $ `Descent3/bsp.h:54:9`!) { |
| 663 | // Send down front list and deactivate this plane |
| 664 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&frontlist, testpoly); |
| 665 | numfront++; |
| 666 | // testpoly->plane.used=1; |
| 667 | } else { |
| 668 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&frontlist, testpoly); |
| 669 | // testpoly->plane.used=1; |
| 670 | numfront++; |
| 671 | } |
| 672 | } else { |
| 673 | // Must split this polygon |
| 674 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(fate == BSP_SPANNINGcxx . . $ `Descent3/bsp.h:51:9`!); |
| 675 | bsppolygoncxx . . $ bsppolygon# *frontpoly, *backpoly; |
| 676 | |
| 677 | numsplits++; |
| 678 | |
| 679 | SplitPolygoncxx . . $ SplitPolygon(a400e6726716d1f9).(&partition_plane, testpoly, &frontpoly, &backpoly); |
| 680 | |
| 681 | numfront++; |
| 682 | numback++; |
| 683 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&frontlist, frontpoly); |
| 684 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&backlist, backpoly); |
| 685 | |
| 686 | RemoveListItemcxx . . $ RemoveListItem(69ddacfa89c563ce).(polylist, testpoly); |
| 687 | FreePolygoncxx . . $ FreePolygon(444f9c48bcf7d9ca).(testpoly); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | // mprintf(0,"BuildBSPNode: Incoming=%d numfront=%d numback=%d splits=%d\n",numpolys,numfront,numback,numsplits); |
| 692 | |
| 693 | if (frontlist != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 694 | if ((frontnode = NewBSPNodecxx . . $ NewBSPNode(6196f39e84d2a8c6).()) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 695 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "BuildBSPNode: Error, can't allocate front node\n"); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | if (!BuildBSPNodecxx . . $ BuildBSPNode(562d90246a8fabae).((bspnodecxx . . $ bspnode# *)frontnode, &frontlist, numfront)) { |
| 700 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "BuildBSPNode: Error building front node\n"); |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | tree->frontcxx . . $ bspnode#front. = frontnode; |
| 705 | DestroyListcxx . . $ DestroyList(7102129d4b5eb48d).(&frontlist); |
| 706 | } else { |
| 707 | if ((frontnode = NewBSPNodecxx . . $ NewBSPNode(6196f39e84d2a8c6).()) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 708 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "BuildBSPNode: Error, can't allocate front node\n"); |
| 709 | return 0; |
| 710 | } |
| 711 | frontnode->typecxx . . $ bspnode#type. = BSP_EMPTY_LEAFcxx . . $ `Descent3/bsp.h:57:9`!; |
| 712 | tree->frontcxx . . $ bspnode#front. = frontnode; |
| 713 | } |
| 714 | |
| 715 | if (backlist != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 716 | if ((backnode = NewBSPNodecxx . . $ NewBSPNode(6196f39e84d2a8c6).()) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 717 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "BuildBSPNode: Error, can't allocate front node\n"); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | if (!BuildBSPNodecxx . . $ BuildBSPNode(562d90246a8fabae).((bspnodecxx . . $ bspnode# *)backnode, &backlist, numback)) { |
| 722 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "BuildBSPNode: Error building back node\n"); |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | tree->backcxx . . $ bspnode#back. = backnode; |
| 727 | DestroyListcxx . . $ DestroyList(7102129d4b5eb48d).(&backlist); |
| 728 | } else { |
| 729 | if ((backnode = NewBSPNodecxx . . $ NewBSPNode(6196f39e84d2a8c6).()) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 730 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "BuildBSPNode: Error, can't allocate front node\n"); |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | backnode->typecxx . . $ bspnode#type. = BSP_SOLID_LEAFcxx . . $ `Descent3/bsp.h:58:9`!; |
| 735 | tree->backcxx . . $ bspnode#back. = backnode; |
| 736 | } |
| 737 | |
| 738 | return 1; |
| 739 | } |
| 740 | |
| 741 | // Releases memory for a bspnode. This function calls itself recursively |
| 742 | void DestroyBSPNodecxx . . $ DestroyBSPNode(8533c9bc809e48fd).(bspnodecxx . . $ bspnode# *node) { |
| 743 | if (node->typecxx . . $ bspnode#type. == BSP_NODEcxx . . $ `Descent3/bsp.h:56:9`!) { |
| 744 | if (node->frontcxx . . $ bspnode#front.) |
| 745 | DestroyBSPNodecxx . . $ DestroyBSPNode(8533c9bc809e48fd).(node->frontcxx . . $ bspnode#front.); |
| 746 | if (node->backcxx . . $ bspnode#back.) |
| 747 | DestroyBSPNodecxx . . $ DestroyBSPNode(8533c9bc809e48fd).(node->backcxx . . $ bspnode#back.); |
| 748 | } |
| 749 | |
| 750 | int np = CountListItemscxx . . $ CountListItems(738f935373f69df8).(&node->polylistcxx . . $ bspnode#polylist.); |
| 751 | |
| 752 | for (int i = 0; i < np; i++) { |
| 753 | bsppolygoncxx . . $ bsppolygon# *poly = (bsppolygoncxx . . $ bsppolygon# *)GetListItemcxx . . $ GetListItem(559f24056c1df4cf).(&node->polylistcxx . . $ bspnode#polylist., i); |
| 754 | FreePolygoncxx . . $ FreePolygon(444f9c48bcf7d9ca).(poly); |
| 755 | } |
| 756 | if (np > 0) |
| 757 | DestroyListcxx . . $ DestroyList(7102129d4b5eb48d).(&node->polylistcxx . . $ bspnode#polylist.); |
| 758 | |
| 759 | mem_freecxx . . $ `mem/mem.h:80:9`!(node); |
| 760 | } |
| 761 | |
| 762 | // Walks the BSP tree and frees up any nodes/polygons that we might be using |
| 763 | void DestroyBSPTreecxx . . $ DestroyBSPTree(450b197686544216).(bsptreecxx . . $ bsptree# *tree) { |
| 764 | if (BSP_inittedcxx . . $ BSP_initted. == 0) |
| 765 | return; |
| 766 | |
| 767 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Destroying bsptree!\n"); |
| 768 | DestroyBSPNodecxx . . $ DestroyBSPNode(8533c9bc809e48fd).(tree->rootcxx . . $ bsptree#root.); |
| 769 | BSP_inittedcxx . . $ BSP_initted. = 0; |
| 770 | } |
| 771 | |
| 772 | // Destroys the mines BSP tree at shutdown |
| 773 | void DestroyDefaultBSPTreecxx . . $ DestroyDefaultBSPTree(49f6e7a06ebc5aa8).() { DestroyBSPTreecxx . . $ DestroyBSPTree(450b197686544216).(&MineBSPcxx . . $ MineBSP.); } |
| 774 | |
| 775 | // Given an object, a submodel, and a vertex number, calculates the world position |
| 776 | // of that point |
| 777 | void GetObjectPointInWorldcxx . . $ GetObjectPointInWorld(a1f5449c483432f8).(vectorcxx . . $ vector# *dest, objectcxx . . $ object# *obj, int subnum, int vertnum); |
| 778 | |
| 779 | // Builds a bsp tree for the indoor rooms |
| 780 | void BuildBSPTreecxx . . $ BuildBSPTree(49f6e7a06ebc5aa8).() { |
| 781 | int i, t, k, j, x; |
| 782 | int numpolys = 0; |
| 783 | int check; |
| 784 | |
| 785 | if (!UseBSPcxx . . $ UseBSP.) |
| 786 | return; |
| 787 | |
| 788 | // Check to see if we even need to build a new tree |
| 789 | check = BSPGetMineChecksumcxx . . $ BSPGetMineChecksum(b126dc7c1de90089).(); |
| 790 | |
| 791 | if (check == BSPChecksumcxx . . $ BSPChecksum.) { |
| 792 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "BSP tree already built!\n"); |
| 793 | return; // The BSP tree has already been built for this mine |
| 794 | } |
| 795 | |
| 796 | BSPChecksumcxx . . $ BSPChecksum. = check; |
| 797 | |
| 798 | // Free up any BSP trees that we might have lying around |
| 799 | InitDefaultBSPcxx . . $ InitDefaultBSP(49f6e7a06ebc5aa8).(); |
| 800 | |
| 801 | MineBSPcxx . . $ MineBSP..rootcxx . . $ bsptree#root. = NewBSPNodecxx . . $ NewBSPNode(6196f39e84d2a8c6).(); |
| 802 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(MineBSP.root); |
| 803 | |
| 804 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Building BSP Tree...\n"); |
| 805 | |
| 806 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Adding polygons to tree\n"); |
| 807 | |
| 808 | // Go through the whole mine and add each polygon to the possible BSP |
| 809 | // partition list. Don't include portals... |
| 810 | for (i = 0; i <= Highest_room_indexcxx . . $ Highest_room_index.; i++) { |
| 811 | bsppolygoncxx . . $ bsppolygon# *newpoly; |
| 812 | |
| 813 | if (Roomscxx . . $ Rooms.[i].usedcxx . . $ room#used. == 0 || (Roomscxx . . $ Rooms.[i].flagscxx . . $ room#flags. & RF_EXTERNALcxx . . $ `Descent3/room_external.h:177:9`!)) |
| 814 | continue; |
| 815 | |
| 816 | roomcxx . . $ room# *rp = &Roomscxx . . $ Rooms.[i]; |
| 817 | |
| 818 | for (t = rp->objectscxx . . $ room#objects.; (t != -1); t = Objectscxx . . $ Objects.[t].nextcxx . . $ object#next.) { |
| 819 | objectcxx . . $ object# *obj = &Objectscxx . . $ Objects.[t]; |
| 820 | |
| 821 | if (obj->typecxx . . $ object#type. == OBJ_NONEcxx . . $ `Descent3/object_external.h:159:9`!) |
| 822 | continue; |
| 823 | |
| 824 | if (OBJECT_OUTSIDEcxx . . $ `lib/object_external_struct.h:130:9`!(obj)) |
| 825 | continue; |
| 826 | |
| 827 | if (obj->lighting_render_typecxx . . $ object#lighting_render_type. == LRT_LIGHTMAPScxx . . $ `Descent3/object.h:651:9`!) { |
| 828 | poly_modelcxx . . $ poly_model# *po = &Poly_modelscxx . . $ Poly_models.[obj->rtypecxx . . $ object#rtype..pobj_infocxx . . $ object#$anonymous_type_10#pobj_info..model_numcxx . . $ polyobj_info#model_num.]; |
| 829 | |
| 830 | if (!po->new_stylecxx . . $ poly_model#new_style.) |
| 831 | continue; |
| 832 | |
| 833 | for (k = 0; k < po->n_modelscxx . . $ poly_model#n_models.; k++) { |
| 834 | bsp_infocxx . . $ bsp_info# *sm = &po->submodelcxx . . $ poly_model#submodel.[k]; |
| 835 | |
| 836 | if (IsNonRenderableSubmodelcxx . . $ IsNonRenderableSubmodel(bb32db98789388a4).(po, k)) |
| 837 | continue; |
| 838 | |
| 839 | for (j = 0; j < sm->num_facescxx . . $ bsp_info#num_faces.; j++) { |
| 840 | vectorcxx . . $ vector# world_verts[64]; |
| 841 | |
| 842 | newpoly = NewPolygoncxx . . $ NewPolygon(97c438c4b083d002).(i, t, sm->facescxx . . $ bsp_info#faces.[j].nvertscxx . . $ polyface#nverts.); |
| 843 | if (!newpoly) { |
| 844 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Couldn't get a new polygon!\n"); |
| 845 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 846 | return; |
| 847 | } |
| 848 | |
| 849 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(sm->faces[j].nverts < 64); |
| 850 | for (x = 0; x < sm->facescxx . . $ bsp_info#faces.[j].nvertscxx . . $ polyface#nverts.; x++) |
| 851 | GetObjectPointInWorldcxx . . $ GetObjectPointInWorld(a1f5449c483432f8).(&world_verts[x], obj, k, sm->facescxx . . $ bsp_info#faces.[j].vertnumscxx . . $ polyface#vertnums.[x]); |
| 852 | |
| 853 | vectorcxx . . $ vector# norm; |
| 854 | |
| 855 | vm_GetNormalcxx . . $ vm_GetNormal(f84d76fdbff5e570).(&norm, &world_verts[0], &world_verts[1], &world_verts[2]); |
| 856 | |
| 857 | // Add the vertices to this poly |
| 858 | for (x = 0; x < sm->facescxx . . $ bsp_info#faces.[j].nvertscxx . . $ polyface#nverts.; x++) |
| 859 | newpoly->vertscxx . . $ bsppolygon#verts.[x] =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). world_verts[x]; |
| 860 | |
| 861 | newpoly->planecxx . . $ bsppolygon#plane..acxx . . $ bspplane#a. = norm.xcxx . . $ vector#x.; |
| 862 | newpoly->planecxx . . $ bsppolygon#plane..bcxx . . $ bspplane#b. = norm.ycxx . . $ vector#y.; |
| 863 | newpoly->planecxx . . $ bsppolygon#plane..ccxx . . $ bspplane#c. = norm.zcxx . . $ vector#z.; |
| 864 | |
| 865 | CalculatePolygonPlanecxx . . $ CalculatePolygonPlane(444f9c48bcf7d9ca).(newpoly); |
| 866 | |
| 867 | // Add it to our bsp list |
| 868 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist., newpoly); |
| 869 | |
| 870 | newpoly->planecxx . . $ bsppolygon#plane..usedcxx . . $ bspplane#used. = 0; |
| 871 | numpolys++; |
| 872 | |
| 873 | newpoly->roomnumcxx . . $ bsppolygon#roomnum. = t; |
| 874 | newpoly->facenumcxx . . $ bsppolygon#facenum. = j; |
| 875 | newpoly->subnumcxx . . $ bsppolygon#subnum. = k; |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | for (t = 0; t < Roomscxx . . $ Rooms.[i].num_facescxx . . $ room#num_faces.; t++) { |
| 882 | |
| 883 | // Don't include portals in BSP calculation |
| 884 | // Unless they go outside |
| 885 | if (Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].portal_numcxx . . $ face#portal_num. != -1) { |
| 886 | if (Roomscxx . . $ Rooms.[i].portalscxx . . $ room#portals.[Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].portal_numcxx . . $ face#portal_num.].croomcxx . . $ portal#croom. >= 0) |
| 887 | continue; |
| 888 | } |
| 889 | |
| 890 | // Construct a new polygon |
| 891 | newpoly = NewPolygoncxx . . $ NewPolygon(97c438c4b083d002).(i, t, Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].num_vertscxx . . $ face#num_verts.); |
| 892 | if (!newpoly) { |
| 893 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Couldn't get a new polygon!\n"); |
| 894 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | // Add the vertices to this poly |
| 899 | for (k = 0; k < Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].num_vertscxx . . $ face#num_verts.; k++) |
| 900 | newpoly->vertscxx . . $ bsppolygon#verts.[k] =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). Roomscxx . . $ Rooms.[i].vertscxx . . $ room#verts.[Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].face_vertscxx . . $ face#face_verts.[k]]; |
| 901 | |
| 902 | newpoly->planecxx . . $ bsppolygon#plane..acxx . . $ bspplane#a. = Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].normalcxx . . $ face#normal..xcxx . . $ vector#x.; |
| 903 | newpoly->planecxx . . $ bsppolygon#plane..bcxx . . $ bspplane#b. = Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].normalcxx . . $ face#normal..ycxx . . $ vector#y.; |
| 904 | newpoly->planecxx . . $ bsppolygon#plane..ccxx . . $ bspplane#c. = Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].normalcxx . . $ face#normal..zcxx . . $ vector#z.; |
| 905 | |
| 906 | CalculatePolygonPlanecxx . . $ CalculatePolygonPlane(444f9c48bcf7d9ca).(newpoly); |
| 907 | |
| 908 | // Add it to our bsp list |
| 909 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist., newpoly); |
| 910 | |
| 911 | newpoly->planecxx . . $ bsppolygon#plane..usedcxx . . $ bspplane#used. = 0; |
| 912 | |
| 913 | newpoly->roomnumcxx . . $ bsppolygon#roomnum. = i; |
| 914 | newpoly->facenumcxx . . $ bsppolygon#facenum. = t; |
| 915 | newpoly->subnumcxx . . $ bsppolygon#subnum. = -1; |
| 916 | |
| 917 | numpolys++; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | ConvexSubspacescxx . . $ ConvexSubspaces. = 0; |
| 922 | ConvexPolyscxx . . $ ConvexPolys. = 0; |
| 923 | Solidscxx . . $ Solids. = 0; |
| 924 | Emptycxx . . $ Empty. = 0; |
| 925 | |
| 926 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "%d polygons added, starting node building...\n", numpolys); |
| 927 | |
| 928 | // Build the BSP tree! |
| 929 | BuildBSPNodecxx . . $ BuildBSPNode(562d90246a8fabae).(MineBSPcxx . . $ MineBSP..rootcxx . . $ bsptree#root., &MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist., numpolys); |
| 930 | DestroyListcxx . . $ DestroyList(7102129d4b5eb48d).(&MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist.); |
| 931 | |
| 932 | // Print some stats |
| 933 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Total number of convex subspaces=%d\n", ConvexSubspaces); |
| 934 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Total number of convex polys=%d\n", ConvexPolys); |
| 935 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Solid=%d Empty=%d\n", Solids, Empty); |
| 936 | } |
| 937 | |
| 938 | // Builds a bsp tree for a single room |
| 939 | void BuildSingleBSPTreecxx . . $ BuildSingleBSPTree(d4f767463ce0a6b3).(int roomnum) { |
| 940 | int i, t, k, j, x; |
| 941 | int numpolys = 0; |
| 942 | |
| 943 | if (!UseBSPcxx . . $ UseBSP.) |
| 944 | return; |
| 945 | |
| 946 | // Free up any BSP trees that we might have lying around |
| 947 | InitDefaultBSPcxx . . $ InitDefaultBSP(49f6e7a06ebc5aa8).(); |
| 948 | |
| 949 | MineBSPcxx . . $ MineBSP..rootcxx . . $ bsptree#root. = NewBSPNodecxx . . $ NewBSPNode(6196f39e84d2a8c6).(); |
| 950 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(MineBSP.root); |
| 951 | |
| 952 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Building BSP Tree...\n"); |
| 953 | |
| 954 | // Go through the whole mine and add each polygon to the possible BSP |
| 955 | // partition list. Don't include portals... |
| 956 | |
| 957 | i = roomnum; |
| 958 | bsppolygoncxx . . $ bsppolygon# *newpoly; |
| 959 | |
| 960 | for (t = 0; t <= Highest_object_indexcxx . . $ Highest_object_index.; t++) { |
| 961 | objectcxx . . $ object# *obj = &Objectscxx . . $ Objects.[t]; |
| 962 | |
| 963 | if (obj->typecxx . . $ object#type. == OBJ_NONEcxx . . $ `Descent3/object_external.h:159:9`!) |
| 964 | continue; |
| 965 | |
| 966 | if (OBJECT_OUTSIDEcxx . . $ `lib/object_external_struct.h:130:9`!(obj)) |
| 967 | continue; |
| 968 | if (obj->roomnumcxx . . $ object#roomnum. != i) |
| 969 | continue; |
| 970 | |
| 971 | if (obj->lighting_render_typecxx . . $ object#lighting_render_type. == LRT_LIGHTMAPScxx . . $ `Descent3/object.h:651:9`!) { |
| 972 | poly_modelcxx . . $ poly_model# *po = &Poly_modelscxx . . $ Poly_models.[obj->rtypecxx . . $ object#rtype..pobj_infocxx . . $ object#$anonymous_type_10#pobj_info..model_numcxx . . $ polyobj_info#model_num.]; |
| 973 | if (!po->new_stylecxx . . $ poly_model#new_style.) |
| 974 | continue; |
| 975 | |
| 976 | for (k = 0; k < po->n_modelscxx . . $ poly_model#n_models.; k++) { |
| 977 | bsp_infocxx . . $ bsp_info# *sm = &po->submodelcxx . . $ poly_model#submodel.[k]; |
| 978 | |
| 979 | if (IsNonRenderableSubmodelcxx . . $ IsNonRenderableSubmodel(bb32db98789388a4).(po, k)) |
| 980 | continue; |
| 981 | |
| 982 | for (j = 0; j < sm->num_facescxx . . $ bsp_info#num_faces.; j++) { |
| 983 | vectorcxx . . $ vector# world_verts[64]; |
| 984 | newpoly = NewPolygoncxx . . $ NewPolygon(97c438c4b083d002).(i, t, sm->facescxx . . $ bsp_info#faces.[j].nvertscxx . . $ polyface#nverts.); |
| 985 | if (!newpoly) { |
| 986 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Couldn't get a new polygon!\n"); |
| 987 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | for (x = 0; x < sm->facescxx . . $ bsp_info#faces.[j].nvertscxx . . $ polyface#nverts.; x++) |
| 992 | GetObjectPointInWorldcxx . . $ GetObjectPointInWorld(a1f5449c483432f8).(&world_verts[x], obj, k, sm->facescxx . . $ bsp_info#faces.[j].vertnumscxx . . $ polyface#vertnums.[x]); |
| 993 | |
| 994 | vectorcxx . . $ vector# norm; |
| 995 | |
| 996 | vm_GetNormalcxx . . $ vm_GetNormal(f84d76fdbff5e570).(&norm, &world_verts[0], &world_verts[1], &world_verts[2]); |
| 997 | |
| 998 | // Add the vertices to this poly |
| 999 | for (x = 0; x < sm->facescxx . . $ bsp_info#faces.[j].nvertscxx . . $ polyface#nverts.; x++) |
| 1000 | newpoly->vertscxx . . $ bsppolygon#verts.[x] =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). world_verts[x]; |
| 1001 | |
| 1002 | newpoly->planecxx . . $ bsppolygon#plane..acxx . . $ bspplane#a. = norm.xcxx . . $ vector#x.; |
| 1003 | newpoly->planecxx . . $ bsppolygon#plane..bcxx . . $ bspplane#b. = norm.ycxx . . $ vector#y.; |
| 1004 | newpoly->planecxx . . $ bsppolygon#plane..ccxx . . $ bspplane#c. = norm.zcxx . . $ vector#z.; |
| 1005 | |
| 1006 | CalculatePolygonPlanecxx . . $ CalculatePolygonPlane(444f9c48bcf7d9ca).(newpoly); |
| 1007 | |
| 1008 | // Add it to our bsp list |
| 1009 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist., newpoly); |
| 1010 | |
| 1011 | newpoly->planecxx . . $ bsppolygon#plane..usedcxx . . $ bspplane#used. = 0; |
| 1012 | numpolys++; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | for (t = 0; t < Roomscxx . . $ Rooms.[i].num_facescxx . . $ room#num_faces.; t++) { |
| 1019 | // Don't include portals in BSP calculation |
| 1020 | if (Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].portal_numcxx . . $ face#portal_num. != -1) |
| 1021 | continue; |
| 1022 | |
| 1023 | // Construct a new polygon |
| 1024 | newpoly = NewPolygoncxx . . $ NewPolygon(97c438c4b083d002).(i, t, Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].num_vertscxx . . $ face#num_verts.); |
| 1025 | if (!newpoly) { |
| 1026 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Couldn't get a new polygon!\n"); |
| 1027 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 1028 | return; |
| 1029 | } |
| 1030 | // Add the vertices to this poly |
| 1031 | for (k = 0; k < Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].num_vertscxx . . $ face#num_verts.; k++) |
| 1032 | newpoly->vertscxx . . $ bsppolygon#verts.[k] =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). Roomscxx . . $ Rooms.[i].vertscxx . . $ room#verts.[Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].face_vertscxx . . $ face#face_verts.[k]]; |
| 1033 | |
| 1034 | newpoly->planecxx . . $ bsppolygon#plane..acxx . . $ bspplane#a. = Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].normalcxx . . $ face#normal..xcxx . . $ vector#x.; |
| 1035 | newpoly->planecxx . . $ bsppolygon#plane..bcxx . . $ bspplane#b. = Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].normalcxx . . $ face#normal..ycxx . . $ vector#y.; |
| 1036 | newpoly->planecxx . . $ bsppolygon#plane..ccxx . . $ bspplane#c. = Roomscxx . . $ Rooms.[i].facescxx . . $ room#faces.[t].normalcxx . . $ face#normal..zcxx . . $ vector#z.; |
| 1037 | |
| 1038 | CalculatePolygonPlanecxx . . $ CalculatePolygonPlane(444f9c48bcf7d9ca).(newpoly); |
| 1039 | |
| 1040 | // Add it to our bsp list |
| 1041 | AddListItemcxx . . $ AddListItem(69ddacfa89c563ce).(&MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist., newpoly); |
| 1042 | |
| 1043 | newpoly->planecxx . . $ bsppolygon#plane..usedcxx . . $ bspplane#used. = 0; |
| 1044 | numpolys++; |
| 1045 | } |
| 1046 | |
| 1047 | ConvexSubspacescxx . . $ ConvexSubspaces. = 0; |
| 1048 | ConvexPolyscxx . . $ ConvexPolys. = 0; |
| 1049 | Solidscxx . . $ Solids. = 0; |
| 1050 | Emptycxx . . $ Empty. = 0; |
| 1051 | |
| 1052 | // Build the BSP tree! |
| 1053 | BuildBSPNodecxx . . $ BuildBSPNode(562d90246a8fabae).(MineBSPcxx . . $ MineBSP..rootcxx . . $ bsptree#root., &MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist., numpolys); |
| 1054 | DestroyListcxx . . $ DestroyList(7102129d4b5eb48d).(&MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist.); |
| 1055 | |
| 1056 | // Print some stats |
| 1057 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Total number of convex subspaces=%d\n", ConvexSubspaces); |
| 1058 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Total number of convex polys=%d\n", ConvexPolys); |
| 1059 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Solid=%d Empty=%d\n", Solids, Empty); |
| 1060 | |
| 1061 | BSPChecksumcxx . . $ BSPChecksum. = -1; |
| 1062 | } |
| 1063 | |
| 1064 | // Returns true if the point is inside the min,max |
| 1065 | static inline int BSPInMinMaxcxx . . $ BSPInMinMax(2bec3a87eb3c1b73).(vectorcxx . . $ vector# *pos, vectorcxx . . $ vector# *min_xyz, vectorcxx . . $ vector# *max_xyz) { |
| 1066 | if (pos->xcxx . . $ vector#x. < min_xyz->xcxx . . $ vector#x. || pos->ycxx . . $ vector#y. < min_xyz->ycxx . . $ vector#y. || pos->zcxx . . $ vector#z. < min_xyz->zcxx . . $ vector#z. || pos->xcxx . . $ vector#x. > max_xyz->xcxx . . $ vector#x. || pos->ycxx . . $ vector#y. > max_xyz->ycxx . . $ vector#y. || |
| 1067 | pos->zcxx . . $ vector#z. > max_xyz->zcxx . . $ vector#z.) |
| 1068 | return 0; |
| 1069 | |
| 1070 | return 1; |
| 1071 | } |
| 1072 | // see if a point in inside a face by projecting into 2d |
| 1073 | extern uint32_tcxx . . $ uint32_t# check_point_to_facecxx . . $ check_point_to_face(ccece0347280792).(vectorcxx . . $ vector# *colp, vectorcxx . . $ vector# *face_normal, int nv, vectorcxx . . $ vector# **vertex_ptr_list); |
| 1074 | |
| 1075 | // Returns true if passed in point collides with a nodes polygon |
| 1076 | static inline int BSPPointInPolygoncxx . . $ BSPPointInPolygon(1bca2165ccd1151b).(vectorcxx . . $ vector# *pos, bspnodecxx . . $ bspnode# *node) { |
| 1077 | if (node->node_subnumcxx . . $ bspnode#node_subnum. < 0) // Room face |
| 1078 | { |
| 1079 | roomcxx . . $ room# *rp = &Roomscxx . . $ Rooms.[node->node_roomnumcxx . . $ bspnode#node_roomnum.]; |
| 1080 | facecxx . . $ face# *fp = &rp->facescxx . . $ room#faces.[node->node_facenumcxx . . $ bspnode#node_facenum.]; |
| 1081 | if (!(BSPInMinMaxcxx . . $ BSPInMinMax(2bec3a87eb3c1b73).(pos, &fp->min_xyzcxx . . $ face#min_xyz., &fp->max_xyzcxx . . $ face#max_xyz.))) |
| 1082 | return 0; |
| 1083 | |
| 1084 | // Test to see if this point in inside the polygon |
| 1085 | vectorcxx . . $ vector# verts[MAX_VERTS_PER_FACEcxx . . $ `Descent3/room_external.h:108:9`!], *vertp[MAX_VERTS_PER_FACEcxx . . $ `Descent3/room_external.h:108:9`!]; |
| 1086 | for (int i = 0; i < fp->num_vertscxx . . $ face#num_verts.; i++) { |
| 1087 | verts[i] =cxx . . $ vector#`operator=`(a48bc7e93a8dae5e). rp->vertscxx . . $ room#verts.[fp->face_vertscxx . . $ face#face_verts.[i]]; |
| 1088 | vertp[i] = &verts[i]; |
| 1089 | } |
| 1090 | |
| 1091 | if ((check_point_to_facecxx . . $ check_point_to_face(ccece0347280792).(pos, &fp->normalcxx . . $ face#normal., fp->num_vertscxx . . $ face#num_verts., vertp))) |
| 1092 | return 0; |
| 1093 | |
| 1094 | return 1; |
| 1095 | } else // Object face |
| 1096 | { |
| 1097 | objectcxx . . $ object# *obj = &Objectscxx . . $ Objects.[node->node_roomnumcxx . . $ bspnode#node_roomnum.]; |
| 1098 | |
| 1099 | if (!(BSPInMinMaxcxx . . $ BSPInMinMax(2bec3a87eb3c1b73).(pos, &obj->min_xyzcxx . . $ object#min_xyz., &obj->max_xyzcxx . . $ object#max_xyz.))) |
| 1100 | return 0; |
| 1101 | |
| 1102 | // Test to see if this point in inside the polygon |
| 1103 | vectorcxx . . $ vector# verts[MAX_VERTS_PER_FACEcxx . . $ `Descent3/room_external.h:108:9`!], *vertp[MAX_VERTS_PER_FACEcxx . . $ `Descent3/room_external.h:108:9`!]; |
| 1104 | vectorcxx . . $ vector# norm; |
| 1105 | poly_modelcxx . . $ poly_model# *po = &Poly_modelscxx . . $ Poly_models.[obj->rtypecxx . . $ object#rtype..pobj_infocxx . . $ object#$anonymous_type_10#pobj_info..model_numcxx . . $ polyobj_info#model_num.]; |
| 1106 | bsp_infocxx . . $ bsp_info# *sm = &po->submodelcxx . . $ poly_model#submodel.[node->node_subnumcxx . . $ bspnode#node_subnum.]; |
| 1107 | |
| 1108 | for (int i = 0; i < sm->facescxx . . $ bsp_info#faces.[node->node_facenumcxx . . $ bspnode#node_facenum.].nvertscxx . . $ polyface#nverts.; i++) { |
| 1109 | GetObjectPointInWorldcxx . . $ GetObjectPointInWorld(a1f5449c483432f8).(&verts[i], obj, node->node_subnumcxx . . $ bspnode#node_subnum., sm->facescxx . . $ bsp_info#faces.[node->node_facenumcxx . . $ bspnode#node_facenum.].vertnumscxx . . $ polyface#vertnums.[i]); |
| 1110 | vertp[i] = &verts[i]; |
| 1111 | } |
| 1112 | |
| 1113 | norm.xcxx . . $ vector#x. = node->planecxx . . $ bspnode#plane..acxx . . $ bspplane#a.; |
| 1114 | norm.ycxx . . $ vector#y. = node->planecxx . . $ bspnode#plane..bcxx . . $ bspplane#b.; |
| 1115 | norm.zcxx . . $ vector#z. = node->planecxx . . $ bspnode#plane..ccxx . . $ bspplane#c.; |
| 1116 | |
| 1117 | if ((check_point_to_facecxx . . $ check_point_to_face(ccece0347280792).(pos, &norm, sm->facescxx . . $ bsp_info#faces.[node->node_facenumcxx . . $ bspnode#node_facenum.].nvertscxx . . $ polyface#nverts., vertp))) |
| 1118 | return 0; |
| 1119 | |
| 1120 | return 1; |
| 1121 | } |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | // Runs a ray through the bsp tree |
| 1127 | // Returns true if a ray is occludes |
| 1128 | #define MAX_RAY_STACKcxx . . $ `Descent3/bsp.cpp:1128:9`! 1000 |
| 1129 | #define PUSH_BSP_RAYcxx . . $ `Descent3/bsp.cpp:1129:9`!(s, e, n) \ |
| 1130 | { \ |
| 1131 | stack_start[si] = s; \ |
| 1132 | stack_end[si] = e; \ |
| 1133 | stack_node[si] = n; \ |
| 1134 | si++; \ |
| 1135 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(si < MAX_RAY_STACKcxx . . $ `Descent3/bsp.cpp:1128:9`!); \ |
| 1136 | } |
| 1137 | #define POP_BSP_RAYcxx . . $ `Descent3/bsp.cpp:1137:9`!() \ |
| 1138 | { \ |
| 1139 | si--; \ |
| 1140 | start = stack_start[si]; \ |
| 1141 | end = stack_end[si]; \ |
| 1142 | node = stack_node[si]; \ |
| 1143 | } |
| 1144 | |
| 1145 | vectorcxx . . $ vector# stack_startcxx . . $ stack_start.[MAX_RAY_STACKcxx . . $ `Descent3/bsp.cpp:1128:9`!], stack_endcxx . . $ stack_end.[MAX_RAY_STACKcxx . . $ `Descent3/bsp.cpp:1128:9`!]; |
| 1146 | bspnodecxx . . $ bspnode# *stack_nodecxx . . $ stack_node.[MAX_RAY_STACKcxx . . $ `Descent3/bsp.cpp:1128:9`!]; |
| 1147 | static int sicxx . . $ si.; |
| 1148 | |
| 1149 | int BSPRayOccludedcxx . . $ BSPRayOccluded(96b8ef6b7178d0f9).(vectorcxx . . $ vector# *line_start, vectorcxx . . $ vector# *line_end, bspnodecxx . . $ bspnode# *start_node) { |
| 1150 | sicxx . . $ si. = 0; |
| 1151 | vectorcxx . . $ vector# start, end; |
| 1152 | bspnodecxx . . $ bspnode# *node; |
| 1153 | |
| 1154 | PUSH_BSP_RAYcxx . . $ `Descent3/bsp.cpp:1129:9`!(*line_start, *line_end, start_node); |
| 1155 | |
| 1156 | while (sicxx . . $ si. > 0) { |
| 1157 | POP_BSP_RAYcxx . . $ `Descent3/bsp.cpp:1137:9`!(); |
| 1158 | while (node->typecxx . . $ bspnode#type. == BSP_NODEcxx . . $ `Descent3/bsp.h:56:9`!) { |
| 1159 | float dist1 = node->planecxx . . $ bspnode#plane..acxx . . $ bspplane#a. * start.xcxx . . $ vector#x. + node->planecxx . . $ bspnode#plane..bcxx . . $ bspplane#b. * start.ycxx . . $ vector#y. + node->planecxx . . $ bspnode#plane..ccxx . . $ bspplane#c. * start.zcxx . . $ vector#z. + node->planecxx . . $ bspnode#plane..dcxx . . $ bspplane#d.; |
| 1160 | float dist2 = node->planecxx . . $ bspnode#plane..acxx . . $ bspplane#a. * end.xcxx . . $ vector#x. + node->planecxx . . $ bspnode#plane..bcxx . . $ bspplane#b. * end.ycxx . . $ vector#y. + node->planecxx . . $ bspnode#plane..ccxx . . $ bspplane#c. * end.zcxx . . $ vector#z. + node->planecxx . . $ bspnode#plane..dcxx . . $ bspplane#d.; |
| 1161 | |
| 1162 | if (dist1 >= 0 && dist2 >= 0) { |
| 1163 | node = (bspnodecxx . . $ bspnode# *)node->frontcxx . . $ bspnode#front.; |
| 1164 | } else if (dist1 < 0 && dist2 < 0) { |
| 1165 | node = (bspnodecxx . . $ bspnode# *)node->backcxx . . $ bspnode#back.; |
| 1166 | } else { |
| 1167 | vectorcxx . . $ vector# mid, delta; |
| 1168 | float t; |
| 1169 | |
| 1170 | // Generate split point |
| 1171 | t = dist1 / (dist1 - dist2); |
| 1172 | delta =cxx . . $ vector#`operator=`(c0842353a20857d). end -cxx . . $ operator-(b77b703cd1e43e33). start; |
| 1173 | mid =cxx . . $ vector#`operator=`(c0842353a20857d). start +cxx . . $ operator+(b77b703cd1e43e33). (t *cxx . . $ `operator*`(9d6577ff3e9faa29). delta); |
| 1174 | if (BSPPointInPolygoncxx . . $ BSPPointInPolygon(1bca2165ccd1151b).(&mid, node)) |
| 1175 | return 1; |
| 1176 | |
| 1177 | // vm_NormalizeVectorFast (&delta); |
| 1178 | |
| 1179 | vectorcxx . . $ vector# mid1 = mid; //-(delta*BSP_EPSILON); |
| 1180 | vectorcxx . . $ vector# mid2 = mid; //+(delta*BSP_EPSILON); |
| 1181 | |
| 1182 | if (dist1 >= 0.0) { |
| 1183 | PUSH_BSP_RAYcxx . . $ `Descent3/bsp.cpp:1129:9`!(mid1, end, (bspnode *)node->back); |
| 1184 | PUSH_BSP_RAYcxx . . $ `Descent3/bsp.cpp:1129:9`!(start, mid2, (bspnode *)node->front); |
| 1185 | } else { |
| 1186 | PUSH_BSP_RAYcxx . . $ `Descent3/bsp.cpp:1129:9`!(mid2, end, (bspnode *)node->front); |
| 1187 | PUSH_BSP_RAYcxx . . $ `Descent3/bsp.cpp:1129:9`!(start, mid1, (bspnode *)node->back); |
| 1188 | } |
| 1189 | |
| 1190 | goto break_out; |
| 1191 | } |
| 1192 | } |
| 1193 | break_out:; |
| 1194 | } |
| 1195 | |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | // Initializes some variables for the indoor bsp tree |
| 1200 | void InitDefaultBSPcxx . . $ InitDefaultBSP(49f6e7a06ebc5aa8).() { |
| 1201 | if (BSP_inittedcxx . . $ BSP_initted.) { |
| 1202 | DestroyBSPTreecxx . . $ DestroyBSPTree(450b197686544216).(&MineBSPcxx . . $ MineBSP.); |
| 1203 | } else { |
| 1204 | atexit(DestroyDefaultBSPTreecxx . . $ DestroyDefaultBSPTree(49f6e7a06ebc5aa8).); |
| 1205 | } |
| 1206 | |
| 1207 | MineBSPcxx . . $ MineBSP..polylistcxx . . $ bsptree#polylist. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 1208 | MineBSPcxx . . $ MineBSP..vertlistcxx . . $ bsptree#vertlist. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 1209 | |
| 1210 | BSP_inittedcxx . . $ BSP_initted. = 1; |
| 1211 | } |