shithub: nanobsp

Download patch

ref: ded57deabd3633ca2597c62dc0f562adb344193e
parent: 567224136999a7b5db85f7926b01bf27dabf7089
author: Andrew Apted <ajapted@gmail.com>
date: Sat Dec 9 16:38:17 EST 2023

rewrote P_LoadSegs(), P_LoadSubsectors() and P_LoadNodes().

These can now be used instead of running the internal nodes builder,
and *should* produce nodes which are functionally identical to the
original code.  Proof of this will be in the demos....

--- a/nano_bsp.h
+++ b/nano_bsp.h
@@ -17,6 +17,8 @@
 #ifndef __NANO_BSP_H__
 #define __NANO_BSP_H__
 
+node_t * BSP_NewNode (void);
+
 void Nano_BuildBSP (void);
 
 #endif
--- a/p_setup.c
+++ b/p_setup.c
@@ -207,51 +207,50 @@
 //
 // P_LoadSegs
 //
+// andrewj: updated this code (etc) for nano-bsp.
+//
 void P_LoadSegs (int lump)
 {
-// andrewj: TODO update this code
-#if 0
-    byte*		data;
-    int			i;
-    mapseg_t*		ml;
-    seg_t*		li;
-    line_t*		ldef;
-    int			linedef;
-    int			side;
-    int                 sidenum;
-	
+    byte*        data;
+    int          i;
+    mapseg_t*    ml;
+    seg_t*       li;
+    line_t*      ldef;
+    int          linedef;
+    int          sidenum;
+
     numsegs = W_LumpLength (lump) / sizeof(mapseg_t);
-    segs = Z_Malloc (numsegs*sizeof(seg_t),PU_LEVEL,0);	
+    segs = Z_Malloc (numsegs*sizeof(seg_t),PU_LEVEL,0);
     memset (segs, 0, numsegs*sizeof(seg_t));
     data = W_CacheLumpNum (lump,PU_STATIC);
-	
+
     ml = (mapseg_t *)data;
     li = segs;
     for (i=0 ; i<numsegs ; i++, li++, ml++)
     {
-	li->v1 = &vertexes[LE_SHORT(ml->v1)];
-	li->v2 = &vertexes[LE_SHORT(ml->v2)];
+        li->v1 = &vertexes[LE_SHORT(ml->v1)];
+        li->v2 = &vertexes[LE_SHORT(ml->v2)];
 
-	li->angle = (LE_SHORT(ml->angle))<<FRACBITS;
-	li->offset = (LE_SHORT(ml->offset))<<FRACBITS;
-	linedef = LE_SHORT(ml->linedef);
-	ldef = &lines[linedef];
-	li->linedef = ldef;
-	side = LE_SHORT(ml->side);
+        li->angle = (LE_SHORT(ml->angle))<<FRACBITS;
+        li->offset = (LE_SHORT(ml->offset))<<FRACBITS;
+        linedef = LE_SHORT(ml->linedef);
+        ldef = &lines[linedef];
+        li->linedef = ldef;
+        li->side = LE_SHORT(ml->side);
 
         // e6y: check for wrong indexes
-        if ((unsigned)ldef->sidenum[side] >= (unsigned)numsides)
+        if ((unsigned)ldef->sidenum[li->side] >= (unsigned)numsides)
         {
             I_Error("P_LoadSegs: linedef %d for seg %d references a non-existent sidedef %d",
-                    linedef, i, (unsigned)ldef->sidenum[side]);
+                    linedef, i, (unsigned)ldef->sidenum[li->side]);
         }
 
-	li->sidedef = &sides[ldef->sidenum[side]];
-	li->frontsector = sides[ldef->sidenum[side]].sector;
+        li->sidedef = &sides[ldef->sidenum[li->side]];
+        li->frontsector = sides[ldef->sidenum[li->side]].sector;
 
         if (ldef-> flags & ML_TWOSIDED)
         {
-            sidenum = ldef->sidenum[side ^ 1];
+            sidenum = ldef->sidenum[li->side ^ 1];
 
             // If the sidenum is out of range, this may be a "glass hack"
             // impassible window.  Point at side #0 (this may not be
@@ -270,12 +269,11 @@
         }
         else
         {
-	    li->backsector = 0;
+            li->backsector = NULL;
         }
     }
-	
+
     W_ReleaseLumpNum(lump);
-#endif
 }
 
 
@@ -284,29 +282,36 @@
 //
 void P_LoadSubsectors (int lump)
 {
-// andrewj: TODO update this code
-#if 0
-    byte*		data;
-    int			i;
-    mapsubsector_t*	ms;
-    subsector_t*	ss;
-	
+    byte*            data;
+    int              i;
+    mapsubsector_t*  ms;
+    subsector_t*     ss;
+
     numsubsectors = W_LumpLength (lump) / sizeof(mapsubsector_t);
-    subsectors = Z_Malloc (numsubsectors*sizeof(subsector_t),PU_LEVEL,0);	
+    subsectors = Z_Malloc (numsubsectors*sizeof(subsector_t),PU_LEVEL,0);
+    memset (subsectors, 0, numsubsectors*sizeof(subsector_t));
     data = W_CacheLumpNum (lump,PU_STATIC);
-	
+
     ms = (mapsubsector_t *)data;
     memset (subsectors,0, numsubsectors*sizeof(subsector_t));
     ss = subsectors;
-    
+
     for (i=0 ; i<numsubsectors ; i++, ss++, ms++)
     {
-	ss->numlines = LE_SHORT(ms->numsegs);
-	ss->firstline = LE_SHORT(ms->firstseg);
+        int numlines  = LE_SHORT(ms->numsegs);
+        int firstline = LE_SHORT(ms->firstseg);
+
+        if (numlines > 0)
+        {
+            ss->segs = &segs[firstline];
+
+            int i;
+            for (i = 1 ; i < numlines ; i++)
+                segs[firstline + i - 1].next = &segs[firstline + i];
+        }
     }
-	
+
     W_ReleaseLumpNum(lump);
-#endif
 }
 
 
@@ -315,30 +320,30 @@
 //
 void P_LoadSectors (int lump)
 {
-    byte*		data;
-    int			i;
-    mapsector_t*	ms;
-    sector_t*		ss;
-	
+    byte*        data;
+    int          i;
+    mapsector_t* ms;
+    sector_t*    ss;
+
     numsectors = W_LumpLength (lump) / sizeof(mapsector_t);
-    sectors = Z_Malloc (numsectors*sizeof(sector_t),PU_LEVEL,0);	
+    sectors = Z_Malloc (numsectors*sizeof(sector_t),PU_LEVEL,0);
     memset (sectors, 0, numsectors*sizeof(sector_t));
     data = W_CacheLumpNum (lump,PU_STATIC);
-	
+
     ms = (mapsector_t *)data;
     ss = sectors;
     for (i=0 ; i<numsectors ; i++, ss++, ms++)
     {
-	ss->floorheight = LE_SHORT(ms->floorheight)<<FRACBITS;
-	ss->ceilingheight = LE_SHORT(ms->ceilingheight)<<FRACBITS;
-	ss->floorpic = R_FlatNumForName(ms->floorpic);
-	ss->ceilingpic = R_FlatNumForName(ms->ceilingpic);
-	ss->lightlevel = LE_SHORT(ms->lightlevel);
-	ss->special = LE_SHORT(ms->special);
-	ss->tag = LE_SHORT(ms->tag);
-	ss->thinglist = NULL;
+        ss->floorheight = LE_SHORT(ms->floorheight)<<FRACBITS;
+        ss->ceilingheight = LE_SHORT(ms->ceilingheight)<<FRACBITS;
+        ss->floorpic = R_FlatNumForName(ms->floorpic);
+        ss->ceilingpic = R_FlatNumForName(ms->ceilingpic);
+        ss->lightlevel = LE_SHORT(ms->lightlevel);
+        ss->special = LE_SHORT(ms->special);
+        ss->tag = LE_SHORT(ms->tag);
+        ss->thinglist = NULL;
     }
-	
+
     W_ReleaseLumpNum(lump);
 }
 
@@ -348,38 +353,78 @@
 //
 void P_LoadNodes (int lump)
 {
-// andrewj: TODO update this code
-#if 0
-    byte*	data;
-    int		i;
-    int		j;
-    int		k;
-    mapnode_t*	mn;
-    node_t*	no;
-	
+    byte*       data;
+    int         i;
+    int         j;
+    int         k;
+    mapnode_t*  mn;
+    node_t*     no;
+
     numnodes = W_LumpLength (lump) / sizeof(mapnode_t);
-    nodes = Z_Malloc (numnodes*sizeof(node_t),PU_LEVEL,0);	
+    nodes = Z_Malloc (numnodes*sizeof(node_t),PU_LEVEL,0);
+    memset (nodes, 0, numnodes*sizeof(node_t));
     data = W_CacheLumpNum (lump,PU_STATIC);
-	
+
     mn = (mapnode_t *)data;
     no = nodes;
-    
-    for (i=0 ; i<numnodes ; i++, no++, mn++)
+
+    for (i=0 ; i < numnodes ; i++, no++, mn++)
     {
-	no->x = LE_SHORT(mn->x)<<FRACBITS;
-	no->y = LE_SHORT(mn->y)<<FRACBITS;
-	no->dx = LE_SHORT(mn->dx)<<FRACBITS;
-	no->dy = LE_SHORT(mn->dy)<<FRACBITS;
-	for (j=0 ; j<2 ; j++)
-	{
-	    no->children[j] = LE_SHORT(mn->children[j]);
-	    for (k=0 ; k<4 ; k++)
-		no->bbox[j][k] = LE_SHORT(mn->bbox[j][k])<<FRACBITS;
-	}
+        no->x  = LE_SHORT(mn->x)  << FRACBITS;
+        no->y  = LE_SHORT(mn->y)  << FRACBITS;
+        no->dx = LE_SHORT(mn->dx) << FRACBITS;
+        no->dy = LE_SHORT(mn->dy) << FRACBITS;
+
+        for (j=0 ; j < 2 ; j++)
+        {
+            unsigned short childnum = LE_SHORT(mn->children[j]);
+
+            node_t * child;
+
+            if (childnum & NF_SUBSECTOR)
+            {
+                child = BSP_NewNode ();
+                child->sub = &subsectors[childnum ^ NF_SUBSECTOR];
+            }
+            else
+            {
+                child = &nodes[childnum];
+            }
+
+            if (j == 0)
+                no->right = child;
+            else
+                no->left = child;
+        }
     }
-	
+
+    // the second pass handles the bounding boxes
+
+    mn = (mapnode_t *)data;
+    no = nodes;
+
+    for (i=0 ; i < numnodes ; i++, no++, mn++)
+    {
+        for (j=0 ; j < 2 ; j++)
+        {
+            node_t * child = (j == 0) ? no->right : no->left;
+
+            for (k=0 ; k < 4 ; k++)
+                child->bbox[k] = LE_SHORT(mn->bbox[j][k]) << FRACBITS;
+        }
+    }
+
     W_ReleaseLumpNum(lump);
-#endif
+
+    if (numnodes > 0)
+    {
+        root_node = &nodes[numnodes-1];
+    }
+    else
+    {
+        root_node = BSP_NewNode ();
+        root_node->sub = &subsectors[0];
+    }
 }
 
 
@@ -861,9 +906,9 @@
     }
     else
     {
+        P_LoadSegs (lumpnum+ML_SEGS);
         P_LoadSubsectors (lumpnum+ML_SSECTORS);
         P_LoadNodes (lumpnum+ML_NODES);
-        P_LoadSegs (lumpnum+ML_SEGS);
     }
 
     P_GroupLines ();
--