2016-03-01 15:47:10 +00:00
|
|
|
/*
|
|
|
|
** gl_nodes.cpp
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2005-2010 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
#include <math.h>
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <malloc.h> // for alloca()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include <direct.h>
|
|
|
|
|
|
|
|
#define rmdir _rmdir
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-03-12 21:45:49 +00:00
|
|
|
#include <zlib.h>
|
2016-03-01 15:47:10 +00:00
|
|
|
#include "templates.h"
|
|
|
|
#include "m_alloc.h"
|
|
|
|
#include "m_argv.h"
|
|
|
|
#include "c_dispatch.h"
|
|
|
|
#include "m_swap.h"
|
|
|
|
#include "g_game.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "w_wad.h"
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "p_local.h"
|
|
|
|
#include "nodebuild.h"
|
|
|
|
#include "doomstat.h"
|
|
|
|
#include "vectors.h"
|
|
|
|
#include "stats.h"
|
|
|
|
#include "doomerrors.h"
|
|
|
|
#include "p_setup.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "version.h"
|
|
|
|
#include "md5.h"
|
|
|
|
#include "m_misc.h"
|
|
|
|
#include "r_utility.h"
|
|
|
|
#include "cmdlib.h"
|
2017-01-08 17:45:30 +00:00
|
|
|
#include "g_levellocals.h"
|
2017-11-12 08:06:40 +00:00
|
|
|
#include "i_time.h"
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
void P_GetPolySpots (MapData * lump, TArray<FNodeBuilder::FPolyStart> &spots, TArray<FNodeBuilder::FPolyStart> &anchors);
|
|
|
|
|
|
|
|
CVAR(Bool, gl_cachenodes, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
CVAR(Float, gl_cachetime, 0.6f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
|
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
void P_LoadZNodes (FileReader &dalump, uint32_t id);
|
2016-03-01 15:47:10 +00:00
|
|
|
static bool CheckCachedNodes(MapData *map);
|
|
|
|
static void CreateCachedNodes(MapData *map);
|
|
|
|
|
|
|
|
|
|
|
|
// fixed 32 bit gl_vert format v2.0+ (glBsp 1.91)
|
|
|
|
struct mapglvertex_t
|
|
|
|
{
|
2017-03-03 18:35:08 +00:00
|
|
|
int32_t x,y;
|
2016-03-01 15:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gl3_mapsubsector_t
|
|
|
|
{
|
2017-03-03 18:35:08 +00:00
|
|
|
int32_t numsegs;
|
|
|
|
int32_t firstseg; // Index of first one; segs are stored sequentially.
|
2016-03-01 15:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct glseg_t
|
|
|
|
{
|
2017-03-08 14:20:00 +00:00
|
|
|
uint16_t v1; // start vertex (16 bit)
|
|
|
|
uint16_t v2; // end vertex (16 bit)
|
|
|
|
uint16_t linedef; // linedef, or -1 for minisegs
|
|
|
|
uint16_t side; // side on linedef: 0 for right, 1 for left
|
|
|
|
uint16_t partner; // corresponding partner seg, or 0xffff on one-sided walls
|
2016-03-01 15:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct glseg3_t
|
|
|
|
{
|
2017-03-03 18:35:08 +00:00
|
|
|
int32_t v1;
|
|
|
|
int32_t v2;
|
2017-03-08 14:20:00 +00:00
|
|
|
uint16_t linedef;
|
|
|
|
uint16_t side;
|
2017-03-03 18:35:08 +00:00
|
|
|
int32_t partner;
|
2016-03-01 15:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct gl5_mapnode_t
|
|
|
|
{
|
2017-03-08 14:20:00 +00:00
|
|
|
int16_t x,y,dx,dy; // partition line
|
|
|
|
int16_t bbox[2][4]; // bounding box for each child
|
2016-03-01 15:47:10 +00:00
|
|
|
// If NF_SUBSECTOR is or'ed in, it's a subsector,
|
|
|
|
// else it's a node of another subtree.
|
2017-03-08 14:20:00 +00:00
|
|
|
uint32_t children[2];
|
2016-03-01 15:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Collect all sidedefs which are not entirely covered by segs
|
|
|
|
// Old ZDBSPs could create such maps. If such a BSP is discovered
|
|
|
|
// a node rebuild must be done to ensure proper rendering
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
static int CheckForMissingSegs()
|
|
|
|
{
|
2017-01-08 17:45:30 +00:00
|
|
|
auto numsides = level.sides.Size();
|
2016-03-29 14:13:16 +00:00
|
|
|
double *added_seglen = new double[numsides];
|
2016-03-01 15:47:10 +00:00
|
|
|
int missing = 0;
|
|
|
|
|
2016-03-29 14:13:16 +00:00
|
|
|
memset(added_seglen, 0, sizeof(double)*numsides);
|
2017-03-16 20:33:13 +00:00
|
|
|
for (auto &seg : level.segs)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
if (seg.sidedef != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
// check all the segs and calculate the length they occupy on their sidedef
|
2017-03-16 20:33:13 +00:00
|
|
|
DVector2 vec1(seg.v2->fX() - seg.v1->fX(), seg.v2->fY() - seg.v1->fY());
|
|
|
|
added_seglen[seg.sidedef->Index()] += vec1.Length();
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 20:33:13 +00:00
|
|
|
for (unsigned i = 0; i < numsides; i++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-01-08 17:45:30 +00:00
|
|
|
double linelen = level.sides[i].linedef->Delta().Length();
|
2016-03-29 14:13:16 +00:00
|
|
|
missing += (added_seglen[i] < linelen - 1.);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 20:33:13 +00:00
|
|
|
delete[] added_seglen;
|
2016-03-01 15:47:10 +00:00
|
|
|
return missing;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Checks whether the nodes are suitable for GL rendering
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool P_CheckForGLNodes()
|
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
for(auto &sub : level.subsectors)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
seg_t * firstseg = sub.firstline;
|
|
|
|
seg_t * lastseg = sub.firstline + sub.numlines - 1;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (firstseg->v1 != lastseg->v2)
|
|
|
|
{
|
|
|
|
// This subsector is incomplete which means that these
|
|
|
|
// are normal nodes
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
for(uint32_t j=0;j<sub.numlines;j++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
if (level.segs[j].linedef==NULL) // miniseg
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
// We already have GL nodes. Great!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// all subsectors were closed but there are no minisegs
|
|
|
|
// Although unlikely this can happen. Such nodes are not a problem.
|
|
|
|
// all that is left is to check whether the BSP covers all sidedefs completely.
|
|
|
|
int missing = CheckForMissingSegs();
|
|
|
|
if (missing > 0)
|
|
|
|
{
|
|
|
|
Printf("%d missing segs counted\nThe BSP needs to be rebuilt.\n", missing);
|
|
|
|
}
|
|
|
|
return missing == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// LoadGLVertexes
|
|
|
|
//
|
|
|
|
// loads GL vertices
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
#define gNd2 MAKE_ID('g','N','d','2')
|
|
|
|
#define gNd4 MAKE_ID('g','N','d','4')
|
|
|
|
#define gNd5 MAKE_ID('g','N','d','5')
|
|
|
|
|
|
|
|
#define GL_VERT_OFFSET 4
|
|
|
|
static int firstglvertex;
|
|
|
|
static bool format5;
|
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
static bool LoadGLVertexes(FileReader &lump)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-01-08 23:46:16 +00:00
|
|
|
firstglvertex = level.vertexes.Size();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
lump.Seek(0, FileReader::SeekSet);
|
2018-11-10 13:15:39 +00:00
|
|
|
auto glbuf = lump.Read();
|
|
|
|
auto gllen=lump.GetLength();
|
|
|
|
auto gldata = glbuf.Data();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (*(int *)gldata == gNd5)
|
|
|
|
{
|
|
|
|
format5=true;
|
|
|
|
}
|
|
|
|
else if (*(int *)gldata != gNd2)
|
|
|
|
{
|
|
|
|
// GLNodes V1 and V4 are unsupported.
|
|
|
|
// V1 because the precision is insufficient and
|
|
|
|
// V4 due to the missing partner segs
|
|
|
|
Printf("GL nodes v%d found. This format is not supported by " GAMENAME "\n",
|
|
|
|
(*(int *)gldata == gNd4)? 4:1);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else format5=false;
|
|
|
|
|
2017-01-08 23:46:16 +00:00
|
|
|
mapglvertex_t* mgl = (mapglvertex_t *)(gldata + GL_VERT_OFFSET);
|
2018-03-11 11:33:46 +00:00
|
|
|
unsigned numvertexes = (unsigned)(firstglvertex + (gllen - GL_VERT_OFFSET)/sizeof(mapglvertex_t));
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-03-17 11:09:38 +00:00
|
|
|
auto oldvertexes = &level.vertexes[0];
|
|
|
|
level.vertexes.Resize(numvertexes);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-01-08 13:39:16 +00:00
|
|
|
for(auto &line : level.lines)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-01-08 23:46:16 +00:00
|
|
|
// Remap vertex pointers in linedefs
|
2017-03-17 11:09:38 +00:00
|
|
|
line.v1 = &level.vertexes[line.v1 - oldvertexes];
|
|
|
|
line.v2 = &level.vertexes[line.v2 - oldvertexes];
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-08 23:46:16 +00:00
|
|
|
for (i = firstglvertex; i < (int)numvertexes; i++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-01-08 23:46:16 +00:00
|
|
|
level.vertexes[i].set(LittleLong(mgl->x)/65536., LittleLong(mgl->y)/65536.);
|
2016-03-01 15:47:10 +00:00
|
|
|
mgl++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// GL Nodes utilities
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
static inline int checkGLVertex(int num)
|
|
|
|
{
|
|
|
|
if (num & 0x8000)
|
|
|
|
num = (num&0x7FFF)+firstglvertex;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int checkGLVertex3(int num)
|
|
|
|
{
|
|
|
|
if (num & 0xc0000000)
|
|
|
|
num = (num&0x3FFFFFFF)+firstglvertex;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// LoadGLSegs
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
static bool LoadGLSegs(FileReader &lump)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
line_t *ldef=NULL;
|
2018-11-10 13:15:39 +00:00
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
lump.Seek(0, FileReader::SeekSet);
|
2018-11-10 13:15:39 +00:00
|
|
|
auto data = lump.Read();
|
|
|
|
int numsegs = (int)lump.GetLength();
|
2017-03-16 20:33:13 +00:00
|
|
|
auto &segs = level.segs;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
if (!format5 && memcmp(data.Data(), "gNd3", 4))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-11-10 13:15:39 +00:00
|
|
|
numsegs/=sizeof(glseg_t);
|
|
|
|
level.segs.Alloc(numsegs);
|
|
|
|
memset(&segs[0],0,sizeof(seg_t)*numsegs);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
glseg_t * ml = (glseg_t*)data.Data();
|
|
|
|
for(i = 0; i < numsegs; i++)
|
|
|
|
{
|
|
|
|
// check for gl-vertices
|
|
|
|
segs[i].v1 = &level.vertexes[checkGLVertex(LittleShort(ml->v1))];
|
|
|
|
segs[i].v2 = &level.vertexes[checkGLVertex(LittleShort(ml->v2))];
|
|
|
|
segs[i].PartnerSeg = ml->partner == 0xFFFF ? nullptr : &segs[LittleShort(ml->partner)];
|
|
|
|
if(ml->linedef != 0xffff)
|
|
|
|
{
|
|
|
|
ldef = &level.lines[LittleShort(ml->linedef)];
|
|
|
|
segs[i].linedef = ldef;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
ml->side=LittleShort(ml->side);
|
|
|
|
segs[i].sidedef = ldef->sidedef[ml->side];
|
|
|
|
if (ldef->sidedef[ml->side] != NULL)
|
|
|
|
{
|
|
|
|
segs[i].frontsector = ldef->sidedef[ml->side]->sector;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
segs[i].frontsector = NULL;
|
|
|
|
}
|
2018-11-10 13:15:39 +00:00
|
|
|
if (ldef->flags & ML_TWOSIDED && ldef->sidedef[ml->side^1] != NULL)
|
|
|
|
{
|
|
|
|
segs[i].backsector = ldef->sidedef[ml->side^1]->sector;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ldef->flags &= ~ML_TWOSIDED;
|
|
|
|
segs[i].backsector = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
segs[i].linedef = NULL;
|
|
|
|
segs[i].sidedef = NULL;
|
|
|
|
|
|
|
|
segs[i].frontsector = NULL;
|
|
|
|
segs[i].backsector = NULL;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2018-11-10 13:15:39 +00:00
|
|
|
ml++;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2018-11-10 13:15:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!format5) numsegs-=4;
|
|
|
|
numsegs/=sizeof(glseg3_t);
|
|
|
|
level.segs.Alloc(numsegs);
|
|
|
|
memset(&segs[0],0,sizeof(seg_t)*numsegs);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
glseg3_t * ml = (glseg3_t*)(data.Data() + (format5? 0:4));
|
|
|
|
for(i = 0; i < numsegs; i++)
|
|
|
|
{ // check for gl-vertices
|
|
|
|
segs[i].v1 = &level.vertexes[checkGLVertex3(LittleLong(ml->v1))];
|
|
|
|
segs[i].v2 = &level.vertexes[checkGLVertex3(LittleLong(ml->v2))];
|
|
|
|
|
|
|
|
const uint32_t partner = LittleLong(ml->partner);
|
2019-01-30 21:34:11 +00:00
|
|
|
segs[i].PartnerSeg = UINT_MAX == partner ? nullptr : &segs[partner];
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
if(ml->linedef != 0xffff) // skip minisegs
|
|
|
|
{
|
|
|
|
ldef = &level.lines[LittleLong(ml->linedef)];
|
|
|
|
segs[i].linedef = ldef;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
ml->side=LittleShort(ml->side);
|
|
|
|
segs[i].sidedef = ldef->sidedef[ml->side];
|
|
|
|
if (ldef->sidedef[ml->side] != NULL)
|
|
|
|
{
|
|
|
|
segs[i].frontsector = ldef->sidedef[ml->side]->sector;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
segs[i].frontsector = NULL;
|
|
|
|
}
|
2018-11-10 13:15:39 +00:00
|
|
|
if (ldef->flags & ML_TWOSIDED && ldef->sidedef[ml->side^1] != NULL)
|
|
|
|
{
|
|
|
|
segs[i].backsector = ldef->sidedef[ml->side^1]->sector;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ldef->flags &= ~ML_TWOSIDED;
|
|
|
|
segs[i].backsector = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
segs[i].linedef = NULL;
|
|
|
|
segs[i].sidedef = NULL;
|
|
|
|
segs[i].frontsector = NULL;
|
|
|
|
segs[i].backsector = NULL;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2018-11-10 13:15:39 +00:00
|
|
|
ml++;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-10 13:15:39 +00:00
|
|
|
return true;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// LoadGLSubsectors
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
static bool LoadGLSubsectors(FileReader &lump)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
2018-11-10 13:15:39 +00:00
|
|
|
|
2018-03-11 11:33:46 +00:00
|
|
|
int numsubsectors = (int)lump.GetLength();
|
2018-03-11 17:32:00 +00:00
|
|
|
lump.Seek(0, FileReader::SeekSet);
|
2018-11-10 13:15:39 +00:00
|
|
|
auto datab = lump.Read();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (numsubsectors == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
if (!format5 && memcmp(datab.Data(), "gNd3", 4))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2018-11-10 13:15:39 +00:00
|
|
|
mapsubsector_t * data = (mapsubsector_t*) datab.Data();
|
2016-03-01 15:47:10 +00:00
|
|
|
numsubsectors /= sizeof(mapsubsector_t);
|
2017-03-16 23:22:52 +00:00
|
|
|
level.subsectors.Alloc(numsubsectors);
|
|
|
|
auto &subsectors = level.subsectors;
|
|
|
|
memset(&subsectors[0],0,numsubsectors * sizeof(subsector_t));
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
for (i=0; i<numsubsectors; i++)
|
|
|
|
{
|
|
|
|
subsectors[i].numlines = LittleShort(data[i].numsegs );
|
2017-03-16 20:33:13 +00:00
|
|
|
subsectors[i].firstline = &level.segs[LittleShort(data[i].firstseg)];
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (subsectors[i].numlines == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-10 13:15:39 +00:00
|
|
|
gl3_mapsubsector_t * data = (gl3_mapsubsector_t*) (datab.Data()+(format5? 0:4));
|
2016-03-01 15:47:10 +00:00
|
|
|
numsubsectors /= sizeof(gl3_mapsubsector_t);
|
2017-03-16 23:22:52 +00:00
|
|
|
level.subsectors.Alloc(numsubsectors);
|
|
|
|
auto &subsectors = level.subsectors;
|
|
|
|
memset(&subsectors[0],0,numsubsectors * sizeof(subsector_t));
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
for (i=0; i<numsubsectors; i++)
|
|
|
|
{
|
|
|
|
subsectors[i].numlines = LittleLong(data[i].numsegs );
|
2017-03-16 20:33:13 +00:00
|
|
|
subsectors[i].firstline = &level.segs[LittleLong(data[i].firstseg)];
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (subsectors[i].numlines == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:22:52 +00:00
|
|
|
for (auto &sub : level.subsectors)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
for(unsigned j=0;j<sub.numlines;j++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
seg_t * seg = sub.firstline + j;
|
|
|
|
if (seg->linedef==NULL) seg->frontsector = seg->backsector = sub.firstline->frontsector;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-03-16 23:22:52 +00:00
|
|
|
seg_t *firstseg = sub.firstline;
|
|
|
|
seg_t *lastseg = sub.firstline + sub.numlines - 1;
|
2016-03-01 15:47:10 +00:00
|
|
|
// The subsector must be closed. If it isn't we can't use these nodes and have to do a rebuild.
|
|
|
|
if (lastseg->v2 != firstseg->v1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// P_LoadNodes
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
static bool LoadNodes (FileReader &lump)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
const int NF_SUBSECTOR = 0x8000;
|
|
|
|
const int GL5_NF_SUBSECTOR = (1 << 31);
|
|
|
|
|
|
|
|
int j;
|
|
|
|
int k;
|
|
|
|
node_t* no;
|
2018-11-10 13:15:39 +00:00
|
|
|
TArray<uint16_t> used;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (!format5)
|
|
|
|
{
|
|
|
|
mapnode_t* mn, * basemn;
|
2018-03-11 11:33:46 +00:00
|
|
|
unsigned numnodes = unsigned(lump.GetLength() / sizeof(mapnode_t));
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (numnodes == 0) return false;
|
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
level.nodes.Alloc(numnodes);
|
2018-03-11 17:32:00 +00:00
|
|
|
lump.Seek(0, FileReader::SeekSet);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
auto buf = lump.Read();
|
|
|
|
basemn = mn = (mapnode_t*)buf.Data();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
used.Resize(numnodes);
|
|
|
|
memset (used.Data(), 0, sizeof(uint16_t)*numnodes);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
no = &level.nodes[0];
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
for (unsigned i = 0; i < numnodes; i++, no++, mn++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
no->x = LittleShort(mn->x)<<FRACBITS;
|
|
|
|
no->y = LittleShort(mn->y)<<FRACBITS;
|
|
|
|
no->dx = LittleShort(mn->dx)<<FRACBITS;
|
|
|
|
no->dy = LittleShort(mn->dy)<<FRACBITS;
|
|
|
|
for (j = 0; j < 2; j++)
|
|
|
|
{
|
2017-03-08 14:20:00 +00:00
|
|
|
uint16_t child = LittleShort(mn->children[j]);
|
2016-03-01 15:47:10 +00:00
|
|
|
if (child & NF_SUBSECTOR)
|
|
|
|
{
|
|
|
|
child &= ~NF_SUBSECTOR;
|
2017-03-16 23:22:52 +00:00
|
|
|
if (child >= level.subsectors.Size())
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-16 23:22:52 +00:00
|
|
|
no->children[j] = (uint8_t *)&level.subsectors[child] + 1;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
else if (child >= numnodes)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (used[child])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-17 00:42:37 +00:00
|
|
|
no->children[j] = &level.nodes[child];
|
2016-03-01 15:47:10 +00:00
|
|
|
used[child] = j + 1;
|
|
|
|
}
|
|
|
|
for (k = 0; k < 4; k++)
|
|
|
|
{
|
2016-05-01 03:37:02 +00:00
|
|
|
no->bbox[j][k] = (float)LittleShort(mn->bbox[j][k]);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gl5_mapnode_t* mn, * basemn;
|
2018-03-11 11:33:46 +00:00
|
|
|
auto numnodes = unsigned(lump.GetLength() / sizeof(gl5_mapnode_t));
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
if (numnodes == 0) return false;
|
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
level.nodes.Alloc(numnodes);
|
2018-03-11 17:32:00 +00:00
|
|
|
lump.Seek(0, FileReader::SeekSet);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
auto buf = lump.Read();
|
|
|
|
basemn = mn = (gl5_mapnode_t*)buf.Data();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
used.Resize(numnodes);
|
|
|
|
memset(used.Data(), 0, sizeof(uint16_t)*numnodes);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
no = &level.nodes[0];
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
for (unsigned i = 0; i < numnodes; i++, no++, mn++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
no->x = LittleShort(mn->x)<<FRACBITS;
|
|
|
|
no->y = LittleShort(mn->y)<<FRACBITS;
|
|
|
|
no->dx = LittleShort(mn->dx)<<FRACBITS;
|
|
|
|
no->dy = LittleShort(mn->dy)<<FRACBITS;
|
|
|
|
for (j = 0; j < 2; j++)
|
|
|
|
{
|
2017-03-03 18:35:08 +00:00
|
|
|
int32_t child = LittleLong(mn->children[j]);
|
2016-03-01 15:47:10 +00:00
|
|
|
if (child & GL5_NF_SUBSECTOR)
|
|
|
|
{
|
|
|
|
child &= ~GL5_NF_SUBSECTOR;
|
2017-03-16 23:22:52 +00:00
|
|
|
if ((unsigned)child >= level.subsectors.Size())
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-16 23:22:52 +00:00
|
|
|
no->children[j] = (uint8_t *)&level.subsectors[child] + 1;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-03-17 00:42:37 +00:00
|
|
|
else if ((unsigned)child >= numnodes)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (used[child])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-17 00:42:37 +00:00
|
|
|
no->children[j] = &level.nodes[child];
|
2016-03-01 15:47:10 +00:00
|
|
|
used[child] = j + 1;
|
|
|
|
}
|
|
|
|
for (k = 0; k < 4; k++)
|
|
|
|
{
|
2016-05-01 03:37:02 +00:00
|
|
|
no->bbox[j][k] = (float)LittleShort(mn->bbox[j][k]);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// loads the GL node data
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2018-03-11 17:32:00 +00:00
|
|
|
static bool DoLoadGLNodes(FileReader * lumps)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-17 10:34:05 +00:00
|
|
|
int missing = 0;
|
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
if (!LoadGLVertexes(lumps[0]) ||
|
|
|
|
!LoadGLSegs(lumps[1]) ||
|
|
|
|
!LoadGLSubsectors(lumps[2]) ||
|
|
|
|
!LoadNodes(lumps[3]))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-17 00:42:37 +00:00
|
|
|
goto fail;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Quick check for the validity of the nodes
|
|
|
|
// For invalid nodes there is a high chance that this test will fail
|
|
|
|
|
2017-03-16 23:22:52 +00:00
|
|
|
for (auto &sub : level.subsectors)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
seg_t * seg = sub.firstline;
|
2016-03-01 15:47:10 +00:00
|
|
|
if (!seg->sidedef)
|
|
|
|
{
|
|
|
|
Printf("GL nodes contain invalid data. The BSP has to be rebuilt.\n");
|
2017-03-17 00:42:37 +00:00
|
|
|
goto fail;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check whether the BSP covers all sidedefs completely.
|
2017-03-17 10:34:05 +00:00
|
|
|
missing = CheckForMissingSegs();
|
2016-03-01 15:47:10 +00:00
|
|
|
if (missing > 0)
|
|
|
|
{
|
|
|
|
Printf("%d missing segs counted in GL nodes.\nThe BSP has to be rebuilt.\n", missing);
|
|
|
|
}
|
|
|
|
return missing == 0;
|
2017-03-17 00:42:37 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
level.nodes.Clear();
|
|
|
|
level.subsectors.Clear();
|
|
|
|
level.segs.Clear();
|
|
|
|
return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// MatchHeader
|
|
|
|
//
|
|
|
|
// Checks whether a GL_LEVEL header belongs to this level
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
static bool MatchHeader(const char * label, const char * hdata)
|
|
|
|
{
|
|
|
|
if (memcmp(hdata, "LEVEL=", 6) == 0)
|
|
|
|
{
|
|
|
|
size_t labellen = strlen(label);
|
|
|
|
labellen = MIN(size_t(8), labellen);
|
|
|
|
|
|
|
|
if (strnicmp(hdata+6, label, labellen)==0 &&
|
|
|
|
(hdata[6+labellen]==0xa || hdata[6+labellen]==0xd))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// FindGLNodesInWAD
|
|
|
|
//
|
|
|
|
// Looks for GL nodes in the same WAD as the level itself
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
static int FindGLNodesInWAD(int labellump)
|
|
|
|
{
|
|
|
|
int wadfile = Wads.GetLumpFile(labellump);
|
|
|
|
FString glheader;
|
|
|
|
|
|
|
|
glheader.Format("GL_%s", Wads.GetLumpFullName(labellump));
|
|
|
|
if (glheader.Len()<=8)
|
|
|
|
{
|
|
|
|
int gllabel = Wads.CheckNumForName(glheader, ns_global, wadfile);
|
|
|
|
if (gllabel >= 0) return gllabel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Before scanning the entire WAD directory let's check first whether
|
|
|
|
// it is necessary.
|
|
|
|
int gllabel = Wads.CheckNumForName("GL_LEVEL", ns_global, wadfile);
|
|
|
|
|
|
|
|
if (gllabel >= 0)
|
|
|
|
{
|
|
|
|
int lastlump=0;
|
|
|
|
int lump;
|
|
|
|
while ((lump=Wads.FindLump("GL_LEVEL", &lastlump))>=0)
|
|
|
|
{
|
|
|
|
if (Wads.GetLumpFile(lump)==wadfile)
|
|
|
|
{
|
|
|
|
FMemLump mem = Wads.ReadLump(lump);
|
|
|
|
if (MatchHeader(Wads.GetLumpFullName(labellump), (const char *)mem.GetMem())) return lump;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// FindGLNodesInFile
|
|
|
|
//
|
|
|
|
// Looks for GL nodes in the same WAD as the level itself
|
|
|
|
// Function returns the lump number within the file. Returns -1 if the input
|
|
|
|
// resource file is NULL.
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
static int FindGLNodesInFile(FResourceFile * f, const char * label)
|
|
|
|
{
|
|
|
|
// No file open? Probably shouldn't happen but assume no GL nodes
|
|
|
|
if(!f)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
FString glheader;
|
|
|
|
bool mustcheck=false;
|
2017-03-08 14:20:00 +00:00
|
|
|
uint32_t numentries = f->LumpCount();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
glheader.Format("GL_%.8s", label);
|
|
|
|
if (glheader.Len()>8)
|
|
|
|
{
|
|
|
|
glheader="GL_LEVEL";
|
|
|
|
mustcheck=true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numentries > 4)
|
|
|
|
{
|
2017-03-08 14:20:00 +00:00
|
|
|
for(uint32_t i=0;i<numentries-4;i++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
if (!strnicmp(f->GetLump(i)->Name, glheader, 8))
|
|
|
|
{
|
|
|
|
if (mustcheck)
|
|
|
|
{
|
|
|
|
char check[16]={0};
|
2018-03-11 11:33:46 +00:00
|
|
|
auto fr = f->GetLump(i)->GetReader();
|
2016-03-01 15:47:10 +00:00
|
|
|
fr->Read(check, 16);
|
|
|
|
if (MatchHeader(label, check)) return i;
|
|
|
|
}
|
|
|
|
else return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Checks for the presence of GL nodes in the loaded WADs or a .GWA file
|
|
|
|
// returns true if successful
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool P_LoadGLNodes(MapData * map)
|
|
|
|
{
|
2018-03-10 20:48:33 +00:00
|
|
|
if (map->Size(ML_GLZNODES) != 0)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
const int idcheck1a = MAKE_ID('Z','G','L','N');
|
|
|
|
const int idcheck2a = MAKE_ID('Z','G','L','2');
|
|
|
|
const int idcheck3a = MAKE_ID('Z','G','L','3');
|
|
|
|
const int idcheck1b = MAKE_ID('X','G','L','N');
|
|
|
|
const int idcheck2b = MAKE_ID('X','G','L','2');
|
|
|
|
const int idcheck3b = MAKE_ID('X','G','L','3');
|
|
|
|
int id;
|
|
|
|
|
2018-03-10 20:48:33 +00:00
|
|
|
auto &file = map->Reader(ML_GLZNODES);
|
|
|
|
file.Read (&id, 4);
|
2016-03-01 15:47:10 +00:00
|
|
|
if (id == idcheck1a || id == idcheck2a || id == idcheck3a ||
|
|
|
|
id == idcheck1b || id == idcheck2b || id == idcheck3b)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
level.subsectors.Clear();
|
2017-03-16 20:33:13 +00:00
|
|
|
level.segs.Clear();
|
2017-03-17 00:42:37 +00:00
|
|
|
level.nodes.Clear();
|
2018-03-10 20:48:33 +00:00
|
|
|
P_LoadZNodes (file, id);
|
2016-03-01 15:47:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (CRecoverableError &)
|
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
level.subsectors.Clear();
|
2017-03-16 20:33:13 +00:00
|
|
|
level.segs.Clear();
|
2017-03-17 00:42:37 +00:00
|
|
|
level.nodes.Clear();
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CheckCachedNodes(map))
|
|
|
|
{
|
2018-03-11 17:32:00 +00:00
|
|
|
FileReader gwalumps[4];
|
2016-03-01 15:47:10 +00:00
|
|
|
char path[256];
|
|
|
|
int li;
|
|
|
|
int lumpfile = Wads.GetLumpFile(map->lumpnum);
|
|
|
|
bool mapinwad = map->InWad;
|
|
|
|
FResourceFile * f_gwa = map->resource;
|
|
|
|
|
|
|
|
const char * name = Wads.GetWadFullName(lumpfile);
|
|
|
|
|
|
|
|
if (mapinwad)
|
|
|
|
{
|
|
|
|
li = FindGLNodesInWAD(map->lumpnum);
|
|
|
|
|
|
|
|
if (li>=0)
|
|
|
|
{
|
|
|
|
// GL nodes are loaded with a WAD
|
|
|
|
for(int i=0;i<4;i++)
|
|
|
|
{
|
2018-03-11 11:33:46 +00:00
|
|
|
gwalumps[i]=Wads.ReopenLumpReader(li+i+1);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
return DoLoadGLNodes(gwalumps);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(path, name);
|
|
|
|
|
|
|
|
char * ext = strrchr(path, '.');
|
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
strcpy(ext, ".gwa");
|
|
|
|
// Todo: Compare file dates
|
|
|
|
|
2018-03-11 11:33:46 +00:00
|
|
|
f_gwa = FResourceFile::OpenResourceFile(path, true);
|
2016-03-01 15:47:10 +00:00
|
|
|
if (f_gwa==NULL) return false;
|
|
|
|
|
|
|
|
strncpy(map->MapLumps[0].Name, Wads.GetLumpFullName(map->lumpnum), 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
li = FindGLNodesInFile(f_gwa, map->MapLumps[0].Name);
|
|
|
|
if (li!=-1)
|
|
|
|
{
|
|
|
|
static const char check[][9]={"GL_VERT","GL_SEGS","GL_SSECT","GL_NODES"};
|
|
|
|
result=true;
|
|
|
|
for(unsigned i=0; i<4;i++)
|
|
|
|
{
|
|
|
|
if (strnicmp(f_gwa->GetLump(li+i+1)->Name, check[i], 8))
|
|
|
|
{
|
|
|
|
result=false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gwalumps[i] = f_gwa->GetLump(li+i+1)->NewReader();
|
|
|
|
}
|
|
|
|
if (result) result = DoLoadGLNodes(gwalumps);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f_gwa != map->resource)
|
|
|
|
delete f_gwa;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Checks whether nodes are GL friendly or not
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool P_CheckNodes(MapData * map, bool rebuilt, int buildtime)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
bool loaded = false;
|
|
|
|
|
|
|
|
// If the map loading code has performed a node rebuild we don't need to check for it again.
|
|
|
|
if (!rebuilt && !P_CheckForGLNodes())
|
|
|
|
{
|
|
|
|
ret = true; // we are not using the level's original nodes if we get here.
|
2017-03-17 11:09:38 +00:00
|
|
|
for (auto &sub : level.subsectors)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
sub.sector = sub.firstline->sidedef->sector;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 11:09:38 +00:00
|
|
|
// The nodes and subsectors need to be preserved for gameplay related purposes.
|
|
|
|
level.gamenodes = std::move(level.nodes);
|
|
|
|
level.gamesubsectors = std::move(level.subsectors);
|
2017-03-16 20:33:13 +00:00
|
|
|
level.segs.Clear();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// Try to load GL nodes (cached or GWA)
|
|
|
|
loaded = P_LoadGLNodes(map);
|
|
|
|
if (!loaded)
|
|
|
|
{
|
|
|
|
// none found - we have to build new ones!
|
2017-11-16 01:33:08 +00:00
|
|
|
uint64_t startTime, endTime;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-11-12 23:54:32 +00:00
|
|
|
startTime = I_msTime ();
|
2016-03-01 15:47:10 +00:00
|
|
|
TArray<FNodeBuilder::FPolyStart> polyspots, anchors;
|
|
|
|
P_GetPolySpots (map, polyspots, anchors);
|
|
|
|
FNodeBuilder::FLevel leveldata =
|
|
|
|
{
|
2017-01-08 23:46:16 +00:00
|
|
|
&level.vertexes[0], (int)level.vertexes.Size(),
|
2017-01-08 17:45:30 +00:00
|
|
|
&level.sides[0], (int)level.sides.Size(),
|
2017-01-08 13:39:16 +00:00
|
|
|
&level.lines[0], (int)level.lines.Size(),
|
2016-03-01 15:47:10 +00:00
|
|
|
0, 0, 0, 0
|
|
|
|
};
|
|
|
|
leveldata.FindMapBounds ();
|
|
|
|
FNodeBuilder builder (leveldata, polyspots, anchors, true);
|
2017-01-08 23:46:16 +00:00
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
builder.Extract (level);
|
2017-11-12 23:54:32 +00:00
|
|
|
endTime = I_msTime ();
|
2017-03-16 20:33:13 +00:00
|
|
|
DPrintf (DMSG_NOTIFY, "BSP generation took %.3f sec (%u segs)\n", (endTime - startTime) * 0.001, level.segs.Size());
|
2017-11-16 01:33:08 +00:00
|
|
|
buildtime = (int32_t)(endTime - startTime);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!loaded)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Building nodes in debug is much slower so let's cache them only if cachetime is 0
|
|
|
|
buildtime = 0;
|
|
|
|
#endif
|
|
|
|
if (level.maptype != MAPTYPE_BUILD && gl_cachenodes && buildtime/1000.f >= gl_cachetime)
|
|
|
|
{
|
2016-08-28 07:55:04 +00:00
|
|
|
DPrintf(DMSG_NOTIFY, "Caching nodes\n");
|
2016-03-01 15:47:10 +00:00
|
|
|
CreateCachedNodes(map);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-28 07:55:04 +00:00
|
|
|
DPrintf(DMSG_NOTIFY, "Not caching nodes (time = %f)\n", buildtime/1000.f);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Node caching
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2017-03-08 14:20:00 +00:00
|
|
|
typedef TArray<uint8_t> MemFile;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
static FString CreateCacheName(MapData *map, bool create)
|
|
|
|
{
|
|
|
|
FString path = M_GetCachePath(create);
|
|
|
|
FString lumpname = Wads.GetLumpFullPath(map->lumpnum);
|
|
|
|
int separator = lumpname.IndexOf(':');
|
|
|
|
path << '/' << lumpname.Left(separator);
|
|
|
|
if (create) CreatePath(path);
|
|
|
|
|
|
|
|
lumpname.ReplaceChars('/', '%');
|
2017-07-24 07:20:35 +00:00
|
|
|
lumpname.ReplaceChars(':', '$');
|
2016-03-01 15:47:10 +00:00
|
|
|
path << '/' << lumpname.Right(lumpname.Len() - separator - 1) << ".gzc";
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:20:00 +00:00
|
|
|
static void WriteByte(MemFile &f, uint8_t b)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
f.Push(b);
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:20:00 +00:00
|
|
|
static void WriteWord(MemFile &f, uint16_t b)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
int v = f.Reserve(2);
|
2017-03-08 14:20:00 +00:00
|
|
|
f[v] = (uint8_t)b;
|
|
|
|
f[v+1] = (uint8_t)(b>>8);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 14:20:00 +00:00
|
|
|
static void WriteLong(MemFile &f, uint32_t b)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
int v = f.Reserve(4);
|
2017-03-08 14:20:00 +00:00
|
|
|
f[v] = (uint8_t)b;
|
|
|
|
f[v+1] = (uint8_t)(b>>8);
|
|
|
|
f[v+2] = (uint8_t)(b>>16);
|
|
|
|
f[v+3] = (uint8_t)(b>>24);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CreateCachedNodes(MapData *map)
|
|
|
|
{
|
|
|
|
MemFile ZNodes;
|
|
|
|
|
|
|
|
WriteLong(ZNodes, 0);
|
2017-01-08 23:46:16 +00:00
|
|
|
WriteLong(ZNodes, level.vertexes.Size());
|
|
|
|
for(auto &vert : level.vertexes)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-01-08 23:46:16 +00:00
|
|
|
WriteLong(ZNodes, vert.fixX());
|
|
|
|
WriteLong(ZNodes, vert.fixY());
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 23:22:52 +00:00
|
|
|
WriteLong(ZNodes, level.subsectors.Size());
|
|
|
|
for (auto &sub : level.subsectors)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
WriteLong(ZNodes, sub.numlines);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 20:33:13 +00:00
|
|
|
WriteLong(ZNodes, level.segs.Size());
|
|
|
|
for(auto &seg : level.segs)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
WriteLong(ZNodes, seg.v1->Index());
|
|
|
|
WriteLong(ZNodes, seg.PartnerSeg == nullptr? 0xffffffffu : uint32_t(seg.PartnerSeg->Index()));
|
|
|
|
if (seg.linedef)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
WriteLong(ZNodes, uint32_t(seg.linedef->Index()));
|
|
|
|
WriteByte(ZNodes, seg.sidedef == seg.linedef->sidedef[0]? 0:1);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WriteLong(ZNodes, 0xffffffffu);
|
|
|
|
WriteByte(ZNodes, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:42:37 +00:00
|
|
|
WriteLong(ZNodes, level.nodes.Size());
|
|
|
|
for(auto &node : level.nodes)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-17 00:42:37 +00:00
|
|
|
WriteLong(ZNodes, node.x);
|
|
|
|
WriteLong(ZNodes, node.y);
|
|
|
|
WriteLong(ZNodes, node.dx);
|
|
|
|
WriteLong(ZNodes, node.dy);
|
2016-03-01 15:47:10 +00:00
|
|
|
for (int j = 0; j < 2; ++j)
|
|
|
|
{
|
|
|
|
for (int k = 0; k < 4; ++k)
|
|
|
|
{
|
2017-03-17 00:42:37 +00:00
|
|
|
WriteWord(ZNodes, (short)node.bbox[j][k]);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < 2; ++j)
|
|
|
|
{
|
2017-03-08 14:20:00 +00:00
|
|
|
uint32_t child;
|
2017-03-17 00:42:37 +00:00
|
|
|
if ((size_t)node.children[j] & 1)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-17 00:42:37 +00:00
|
|
|
child = 0x80000000 | uint32_t(((subsector_t *)((uint8_t *)node.children[j] - 1))->Index());
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-17 00:42:37 +00:00
|
|
|
child = ((node_t *)node.children[j])->Index();
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
WriteLong(ZNodes, child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uLongf outlen = ZNodes.Size();
|
2018-11-10 13:15:39 +00:00
|
|
|
TArray<Bytef> compressed;
|
2017-01-08 13:39:16 +00:00
|
|
|
int offset = level.lines.Size() * 8 + 12 + 16;
|
2016-03-01 15:47:10 +00:00
|
|
|
int r;
|
|
|
|
do
|
|
|
|
{
|
2018-11-10 13:15:39 +00:00
|
|
|
compressed.Resize(outlen + offset);
|
|
|
|
r = compress (compressed.Data() + offset, &outlen, &ZNodes[0], ZNodes.Size());
|
2016-03-01 15:47:10 +00:00
|
|
|
if (r == Z_BUF_ERROR)
|
|
|
|
{
|
|
|
|
outlen += 1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (r == Z_BUF_ERROR);
|
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
memcpy(compressed.Data(), "CACH", 4);
|
2017-03-08 14:20:00 +00:00
|
|
|
uint32_t len = LittleLong(level.lines.Size());
|
2018-11-10 13:15:39 +00:00
|
|
|
memcpy(&compressed[4], &len, 4);
|
|
|
|
map->GetChecksum(&compressed[8]);
|
2017-01-08 13:39:16 +00:00
|
|
|
for (unsigned i = 0; i < level.lines.Size(); i++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-08 14:20:00 +00:00
|
|
|
uint32_t ndx[2] = { LittleLong(uint32_t(level.lines[i].v1->Index())), LittleLong(uint32_t(level.lines[i].v2->Index())) };
|
2018-11-10 13:15:39 +00:00
|
|
|
memcpy(&compressed[8 + 16 + 8 * i], ndx, 8);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2018-11-10 13:15:39 +00:00
|
|
|
memcpy(&compressed[offset - 4], "ZGL3", 4);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
FString path = CreateCacheName(map, true);
|
2017-12-02 11:08:28 +00:00
|
|
|
FileWriter *fw = FileWriter::Open(path);
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-12-02 11:08:28 +00:00
|
|
|
if (fw != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-12-10 13:10:32 +00:00
|
|
|
const size_t length = outlen + offset;
|
2018-11-10 13:15:39 +00:00
|
|
|
if (fw->Write(compressed.Data(), length) != length)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
Printf("Error saving nodes to file %s\n", path.GetChars());
|
|
|
|
}
|
2017-12-02 11:08:28 +00:00
|
|
|
delete fw;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Printf("Cannot open nodes file %s for writing\n", path.GetChars());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool CheckCachedNodes(MapData *map)
|
|
|
|
{
|
|
|
|
char magic[4] = {0,0,0,0};
|
2017-03-08 14:20:00 +00:00
|
|
|
uint8_t md5[16];
|
|
|
|
uint8_t md5map[16];
|
|
|
|
uint32_t numlin;
|
2018-11-10 13:15:39 +00:00
|
|
|
TArray<uint32_t> verts;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
FString path = CreateCacheName(map, false);
|
2018-03-11 17:32:00 +00:00
|
|
|
FileReader fr;
|
2017-12-02 11:08:28 +00:00
|
|
|
|
2018-03-10 20:48:33 +00:00
|
|
|
if (!fr.OpenFile(path)) return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
if (fr.Read(magic, 4) != 4) return false;
|
|
|
|
if (memcmp(magic, "CACH", 4)) return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
if (fr.Read(&numlin, 4) != 4) return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
numlin = LittleLong(numlin);
|
2018-11-10 13:15:39 +00:00
|
|
|
if (numlin != level.lines.Size()) return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
if (fr.Read(md5, 16) != 16) return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
map->GetChecksum(md5map);
|
2018-11-10 13:15:39 +00:00
|
|
|
if (memcmp(md5, md5map, 16)) return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
verts.Resize(numlin * 2);
|
|
|
|
if (fr.Read(verts.Data(), 8 * numlin) != 8 * numlin) return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2018-11-10 13:15:39 +00:00
|
|
|
if (fr.Read(magic, 4) != 4) return false;
|
|
|
|
if (memcmp(magic, "ZGL2", 4) && memcmp(magic, "ZGL3", 4)) return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
P_LoadZNodes (fr, MAKE_ID(magic[0],magic[1],magic[2],magic[3]));
|
|
|
|
}
|
|
|
|
catch (CRecoverableError &error)
|
|
|
|
{
|
|
|
|
Printf ("Error loading nodes: %s\n", error.GetMessage());
|
|
|
|
|
2017-03-16 23:22:52 +00:00
|
|
|
level.subsectors.Clear();
|
2017-03-16 20:33:13 +00:00
|
|
|
level.segs.Clear();
|
2017-03-17 00:42:37 +00:00
|
|
|
level.nodes.Clear();
|
2018-11-10 13:15:39 +00:00
|
|
|
return false;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-08 13:39:16 +00:00
|
|
|
for(auto &line : level.lines)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-01-08 13:39:16 +00:00
|
|
|
int i = line.Index();
|
2017-01-08 23:46:16 +00:00
|
|
|
line.v1 = &level.vertexes[LittleLong(verts[i*2])];
|
|
|
|
line.v2 = &level.vertexes[LittleLong(verts[i*2+1])];
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-30 13:42:03 +00:00
|
|
|
UNSAFE_CCMD(clearnodecache)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
TArray<FFileList> list;
|
|
|
|
FString path = M_GetCachePath(false);
|
|
|
|
path += "/";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ScanDirectory(list, path);
|
|
|
|
}
|
|
|
|
catch (CRecoverableError &err)
|
|
|
|
{
|
2017-01-12 21:56:06 +00:00
|
|
|
Printf("%s\n", err.GetMessage());
|
2016-03-01 15:47:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan list backwards so that when we reach a directory
|
|
|
|
// all files within are already deleted.
|
|
|
|
for(int i = list.Size()-1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (list[i].isDirectory)
|
|
|
|
{
|
|
|
|
rmdir(list[i].Filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remove(list[i].Filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Keep both the original nodes from the WAD and the GL nodes created here.
|
|
|
|
// The original set is only being used to get the sector for in-game
|
|
|
|
// positioning of actors but not for rendering.
|
|
|
|
//
|
|
|
|
// This is necessary because ZDBSP is much more sensitive
|
|
|
|
// to sloppy mapping practices that produce overlapping sectors.
|
|
|
|
// The crane in P:AR E1M3 is a good example that would be broken if
|
|
|
|
// this wasn't done.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// PointOnLine
|
|
|
|
//
|
|
|
|
// Same as the one im the node builder, but not part of a specific class
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
static bool PointOnLine (int x, int y, int x1, int y1, int dx, int dy)
|
|
|
|
{
|
|
|
|
const double SIDE_EPSILON = 6.5536;
|
|
|
|
|
|
|
|
// For most cases, a simple dot product is enough.
|
|
|
|
double d_dx = double(dx);
|
|
|
|
double d_dy = double(dy);
|
|
|
|
double d_x = double(x);
|
|
|
|
double d_y = double(y);
|
|
|
|
double d_x1 = double(x1);
|
|
|
|
double d_y1 = double(y1);
|
|
|
|
|
|
|
|
double s_num = (d_y1-d_y)*d_dx - (d_x1-d_x)*d_dy;
|
|
|
|
|
|
|
|
if (fabs(s_num) < 17179869184.0) // 4<<32
|
|
|
|
{
|
|
|
|
// Either the point is very near the line, or the segment defining
|
|
|
|
// the line is very short: Do a more expensive test to determine
|
|
|
|
// just how far from the line the point is.
|
2016-03-11 14:45:47 +00:00
|
|
|
double l = g_sqrt(d_dx*d_dx+d_dy*d_dy);
|
2016-03-01 15:47:10 +00:00
|
|
|
double dist = fabs(s_num)/l;
|
|
|
|
if (dist < SIDE_EPSILON)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// SetRenderSector
|
|
|
|
//
|
|
|
|
// Sets the render sector for each GL subsector so that the proper flat
|
|
|
|
// information can be retrieved
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void P_SetRenderSector()
|
|
|
|
{
|
|
|
|
int i;
|
2017-03-08 14:20:00 +00:00
|
|
|
uint32_t j;
|
2016-03-01 15:47:10 +00:00
|
|
|
TArray<subsector_t *> undetermined;
|
|
|
|
subsector_t * ss;
|
|
|
|
|
|
|
|
// Check for incorrect partner seg info so that the following code does not crash.
|
|
|
|
|
2017-03-16 20:33:13 +00:00
|
|
|
for (auto &seg : level.segs)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
auto p = seg.PartnerSeg;
|
2017-01-09 00:28:07 +00:00
|
|
|
if (p != nullptr)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
int partner = p->Index();
|
2016-03-01 15:47:10 +00:00
|
|
|
|
2017-03-16 20:33:13 +00:00
|
|
|
if (partner < 0 || partner >= (int)level.segs.Size() || &level.segs[partner] != p)
|
2017-01-09 00:28:07 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
seg.PartnerSeg = nullptr;
|
2017-01-09 00:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// glbsp creates such incorrect references for Strife.
|
2017-03-16 20:33:13 +00:00
|
|
|
if (seg.linedef && seg.PartnerSeg != nullptr && !seg.PartnerSeg->linedef)
|
2017-01-09 00:28:07 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
seg.PartnerSeg = seg.PartnerSeg->PartnerSeg = nullptr;
|
2017-01-09 00:28:07 +00:00
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
2017-01-19 00:39:05 +00:00
|
|
|
}
|
2017-03-16 20:33:13 +00:00
|
|
|
for (auto &seg : level.segs)
|
2017-01-19 00:39:05 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
if (seg.PartnerSeg != nullptr && seg.PartnerSeg->PartnerSeg != &seg)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 20:33:13 +00:00
|
|
|
seg.PartnerSeg = nullptr;
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// look up sector number for each subsector
|
2017-03-16 23:22:52 +00:00
|
|
|
for (auto &ss : level.subsectors)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
// For rendering pick the sector from the first seg that is a sector boundary
|
|
|
|
// this takes care of self-referencing sectors
|
2017-03-16 23:22:52 +00:00
|
|
|
seg_t *seg = ss.firstline;
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
// Check for one-dimensional subsectors. These should be ignored when
|
|
|
|
// being processed for automap drawing etc.
|
2017-03-16 23:22:52 +00:00
|
|
|
ss.flags |= SSECF_DEGENERATE;
|
|
|
|
for(j=2; j<ss.numlines; j++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2016-03-29 08:07:06 +00:00
|
|
|
if (!PointOnLine(seg[j].v1->fixX(), seg[j].v1->fixY(), seg->v1->fixX(), seg->v1->fixY(), seg->v2->fixX() -seg->v1->fixX(), seg->v2->fixY() -seg->v1->fixY()))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
// Not on the same line
|
2017-03-16 23:22:52 +00:00
|
|
|
ss.flags &= ~SSECF_DEGENERATE;
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:22:52 +00:00
|
|
|
seg = ss.firstline;
|
|
|
|
for(j=0; j<ss.numlines; j++)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-01-11 09:46:26 +00:00
|
|
|
if(seg->sidedef && (seg->PartnerSeg == nullptr || (seg->PartnerSeg->sidedef != nullptr && seg->sidedef->sector!=seg->PartnerSeg->sidedef->sector)))
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
ss.render_sector = seg->sidedef->sector;
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
seg++;
|
|
|
|
}
|
2017-03-16 23:22:52 +00:00
|
|
|
if(ss.render_sector == NULL)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-03-16 23:22:52 +00:00
|
|
|
undetermined.Push(&ss);
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assign a vaild render sector to all subsectors which haven't been processed yet.
|
|
|
|
while (undetermined.Size())
|
|
|
|
{
|
|
|
|
bool deleted=false;
|
|
|
|
for(i=undetermined.Size()-1;i>=0;i--)
|
|
|
|
{
|
|
|
|
ss=undetermined[i];
|
|
|
|
seg_t * seg = ss->firstline;
|
|
|
|
|
|
|
|
for(j=0; j<ss->numlines; j++)
|
|
|
|
{
|
2017-01-09 00:28:07 +00:00
|
|
|
if (seg->PartnerSeg != nullptr && seg->PartnerSeg->Subsector)
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-01-09 00:28:07 +00:00
|
|
|
sector_t * backsec = seg->PartnerSeg->Subsector->render_sector;
|
2016-03-01 15:47:10 +00:00
|
|
|
if (backsec)
|
|
|
|
{
|
2017-01-09 00:28:07 +00:00
|
|
|
ss->render_sector = backsec;
|
2016-03-01 15:47:10 +00:00
|
|
|
undetermined.Delete(i);
|
2017-01-09 00:28:07 +00:00
|
|
|
deleted = 1;
|
2016-03-01 15:47:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
seg++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We still got some left but the loop above was unable to assign them.
|
|
|
|
// This only happens when a subsector is off the map.
|
|
|
|
// Don't bother and just assign the real sector for rendering
|
|
|
|
if (!deleted && undetermined.Size())
|
|
|
|
{
|
|
|
|
for(i=undetermined.Size()-1;i>=0;i--)
|
|
|
|
{
|
|
|
|
ss=undetermined[i];
|
|
|
|
ss->render_sector=ss->sector;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 // may be useful later so let's keep it here for now
|
|
|
|
// now group the subsectors by sector
|
|
|
|
subsector_t ** subsectorbuffer = new subsector_t * [numsubsectors];
|
|
|
|
|
|
|
|
for(i=0, ss=subsectors; i<numsubsectors; i++, ss++)
|
|
|
|
{
|
|
|
|
ss->render_sector->subsectorcount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<numsectors; i++)
|
|
|
|
{
|
|
|
|
sectors[i].subsectors = subsectorbuffer;
|
|
|
|
subsectorbuffer += sectors[i].subsectorcount;
|
|
|
|
sectors[i].subsectorcount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0, ss = subsectors; i<numsubsectors; i++, ss++)
|
|
|
|
{
|
|
|
|
ss->render_sector->subsectors[ss->render_sector->subsectorcount++]=ss;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|