UltimateZoneBuilder/Source/Plugins/vpo_dll/p_setup.cc
MaxED 20bb7fd119 GZDoom Builder 1.07
Updated to Doom Builder 2 SVN rev. 1549.
2012-06-01 19:53:14 +00:00

662 lines
14 KiB
C++

// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// Do all the WAD I/O, get map description,
// set up initial state and misc. LUTs.
//
//-----------------------------------------------------------------------------
#include "vpo_local.h"
namespace vpo
{
//
// MAP related Lookup tables.
// Store VERTEXES, LINEDEFS, SIDEDEFS, etc.
//
int numvertexes;
vertex_t* vertexes;
int numsegs;
seg_t* segs;
int numsectors;
sector_t* sectors;
int numsubsectors;
subsector_t* subsectors;
int numnodes;
node_t* nodes;
int numlines;
line_t* lines;
int numsides;
side_t* sides;
static int R_TextureNumForName (const char * name)
{
// "NoTexture" marker.
if (name[0] == '-')
return 0;
return 1; // dummy value
}
static int R_FlatNumForName (const char * name)
{
// SKY ?
if (name[0] == 'F' && name[1] == '_' && name[2] == 'S' && name[3] == 'K')
return skyflatnum;
return 1; // dummy value
}
//
// P_LoadVertexes
//
void P_LoadVertexes (int lump)
{
byte* data;
int i;
mapvertex_t* ml;
vertex_t* li;
// Determine number of lumps:
// total lump length / vertex record length.
numvertexes = W_LumpLength (lump) / sizeof(mapvertex_t);
// Allocate zone memory for buffer.
vertexes = new vertex_t[numvertexes];
// Load data into cache.
data = W_LoadLump (lump);
ml = (mapvertex_t *)data;
li = vertexes;
// Copy and convert vertex coordinates,
// internal representation as fixed.
for (i=0 ; i<numvertexes ; i++, li++, ml++)
{
li->x = SHORT(ml->x)<<FRACBITS;
li->y = SHORT(ml->y)<<FRACBITS;
}
// Free buffer memory.
W_FreeLump(data);
}
//
// GetSectorAtNullAddress
//
sector_t* GetSectorAtNullAddress(void)
{
return sectors + 0;
#if 0
static boolean null_sector_is_initialized = false;
static sector_t null_sector;
if (!null_sector_is_initialized)
{
memset(&null_sector, 0, sizeof(null_sector));
I_GetMemoryValue(0, &null_sector.floorheight, 4);
I_GetMemoryValue(4, &null_sector.ceilingheight, 4);
null_sector_is_initialized = true;
}
return &null_sector;
#endif
}
//
// P_LoadSegs
//
void P_LoadSegs (int lump)
{
byte* data;
int i;
mapseg_t* ml;
seg_t* li;
line_t* ldef;
int linedef;
int side;
int sidenum;
numsegs = W_LumpLength (lump) / sizeof(mapseg_t);
segs = new seg_t[numsegs];
memset (segs, 0, numsegs*sizeof(seg_t));
data = W_LoadLump (lump);
ml = (mapseg_t *)data;
li = segs;
for (i=0 ; i<numsegs ; i++, li++, ml++)
{
li->v1 = &vertexes[SHORT(ml->v1)];
li->v2 = &vertexes[SHORT(ml->v2)];
li->angle = (SHORT(ml->angle))<<16;
li->offset = (SHORT(ml->offset))<<16;
linedef = SHORT(ml->linedef);
ldef = &lines[linedef];
li->linedef = ldef;
side = SHORT(ml->side);
li->sidedef = &sides[ldef->sidenum[side]];
li->frontsector = sides[ldef->sidenum[side]].sector;
if (ldef-> flags & ML_TWOSIDED)
{
sidenum = ldef->sidenum[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
// the correct Vanilla behavior; however, it seems to work for
// OTTAWAU.WAD, which is the one place I've seen this trick
// used).
if (sidenum < 0 || sidenum >= numsides)
{
li->backsector = GetSectorAtNullAddress();
}
else
{
li->backsector = sides[sidenum].sector;
}
}
else
{
li->backsector = 0;
}
}
W_FreeLump(data);
}
//
// P_LoadSubsectors
//
void P_LoadSubsectors (int lump)
{
byte* data;
int i;
mapsubsector_t* ms;
subsector_t* ss;
numsubsectors = W_LumpLength (lump) / sizeof(mapsubsector_t);
subsectors = new subsector_t[numsubsectors];
data = W_LoadLump (lump);
ms = (mapsubsector_t *)data;
memset (subsectors,0, numsubsectors*sizeof(subsector_t));
ss = subsectors;
for (i=0 ; i<numsubsectors ; i++, ss++, ms++)
{
ss->numlines = SHORT(ms->numsegs);
ss->firstline = SHORT(ms->firstseg);
}
W_FreeLump(data);
}
//
// P_LoadSectors
//
void P_LoadSectors (int lump)
{
byte* data;
int i;
mapsector_t* ms;
sector_t* ss;
numsectors = W_LumpLength (lump) / sizeof(mapsector_t);
sectors = new sector_t[numsectors];
memset (sectors, 0, numsectors*sizeof(sector_t));
data = W_LoadLump (lump);
ms = (mapsector_t *)data;
ss = sectors;
for (i=0 ; i<numsectors ; i++, ss++, ms++)
{
ss->floorheight = SHORT(ms->floorheight)<<FRACBITS;
ss->ceilingheight = SHORT(ms->ceilingheight)<<FRACBITS;
ss->floorpic = R_FlatNumForName(ms->floorpic);
ss->ceilingpic = R_FlatNumForName(ms->ceilingpic);
ss->lightlevel = SHORT(ms->lightlevel);
ss->special = SHORT(ms->special);
ss->tag = SHORT(ms->tag);
/// ss->thinglist = NULL;
}
W_FreeLump(data);
}
//
// P_LoadNodes
//
void P_LoadNodes (int lump)
{
byte* data;
int i;
int j;
int k;
mapnode_t* mn;
node_t* no;
numnodes = W_LumpLength (lump) / sizeof(mapnode_t);
nodes = new node_t[numnodes];
data = W_LoadLump (lump);
mn = (mapnode_t *)data;
no = nodes;
for (i=0 ; i<numnodes ; i++, no++, mn++)
{
no->x = SHORT(mn->x)<<FRACBITS;
no->y = SHORT(mn->y)<<FRACBITS;
no->dx = SHORT(mn->dx)<<FRACBITS;
no->dy = SHORT(mn->dy)<<FRACBITS;
for (j=0 ; j<2 ; j++)
{
no->children[j] = SHORT(mn->children[j]);
for (k=0 ; k<4 ; k++)
no->bbox[j][k] = SHORT(mn->bbox[j][k])<<FRACBITS;
}
}
W_FreeLump(data);
}
//
// P_LoadThings
//
void P_LoadThings (int lump)
{
#if 0
byte *data;
int i;
mapthing_t *mt;
mapthing_t spawnthing;
int numthings;
boolean spawn;
data = W_LoadLump (lump);
numthings = W_LumpLength (lump) / sizeof(mapthing_t);
mt = (mapthing_t *)data;
for (i=0 ; i<numthings ; i++, mt++)
{
spawn = true;
// Do not spawn cool, new monsters if !commercial
if (gamemode != commercial)
{
switch (SHORT(mt->type))
{
case 68: // Arachnotron
case 64: // Archvile
case 88: // Boss Brain
case 89: // Boss Shooter
case 69: // Hell Knight
case 67: // Mancubus
case 71: // Pain Elemental
case 65: // Former Human Commando
case 66: // Revenant
case 84: // Wolf SS
spawn = false;
break;
}
}
if (spawn == false)
break;
// Do spawn all other stuff.
spawnthing.x = SHORT(mt->x);
spawnthing.y = SHORT(mt->y);
spawnthing.angle = SHORT(mt->angle);
spawnthing.type = SHORT(mt->type);
spawnthing.options = SHORT(mt->options);
P_SpawnMapThing(&spawnthing);
}
W_FreeLump(data);
#endif
}
//
// P_LoadLineDefs
// Also counts secret lines for intermissions.
//
void P_LoadLineDefs (int lump)
{
byte* data;
int i;
maplinedef_t* mld;
line_t* ld;
vertex_t* v1;
vertex_t* v2;
numlines = W_LumpLength (lump) / sizeof(maplinedef_t);
lines = new line_t[numlines];
memset (lines, 0, numlines*sizeof(line_t));
data = W_LoadLump (lump);
mld = (maplinedef_t *)data;
ld = lines;
for (i=0 ; i<numlines ; i++, mld++, ld++)
{
ld->flags = SHORT(mld->flags);
ld->special = SHORT(mld->special);
ld->tag = SHORT(mld->tag);
v1 = ld->v1 = &vertexes[SHORT(mld->v1)];
v2 = ld->v2 = &vertexes[SHORT(mld->v2)];
ld->dx = v2->x - v1->x;
ld->dy = v2->y - v1->y;
if (!ld->dx)
ld->slopetype = ST_VERTICAL;
else if (!ld->dy)
ld->slopetype = ST_HORIZONTAL;
else
{
if (FixedDiv (ld->dy , ld->dx) > 0)
ld->slopetype = ST_POSITIVE;
else
ld->slopetype = ST_NEGATIVE;
}
if (v1->x < v2->x)
{
ld->bbox[BOXLEFT] = v1->x;
ld->bbox[BOXRIGHT] = v2->x;
}
else
{
ld->bbox[BOXLEFT] = v2->x;
ld->bbox[BOXRIGHT] = v1->x;
}
if (v1->y < v2->y)
{
ld->bbox[BOXBOTTOM] = v1->y;
ld->bbox[BOXTOP] = v2->y;
}
else
{
ld->bbox[BOXBOTTOM] = v2->y;
ld->bbox[BOXTOP] = v1->y;
}
ld->sidenum[0] = SHORT(mld->sidenum[0]);
ld->sidenum[1] = SHORT(mld->sidenum[1]);
if (ld->sidenum[0] != -1)
ld->frontsector = sides[ld->sidenum[0]].sector;
else
ld->frontsector = 0;
if (ld->sidenum[1] != -1)
ld->backsector = sides[ld->sidenum[1]].sector;
else
ld->backsector = 0;
}
W_FreeLump(data);
}
//
// P_LoadSideDefs
//
void P_LoadSideDefs (int lump)
{
byte* data;
int i;
mapsidedef_t* msd;
side_t* sd;
numsides = W_LumpLength (lump) / sizeof(mapsidedef_t);
sides = new side_t[numsides];
memset (sides, 0, numsides*sizeof(side_t));
data = W_LoadLump (lump);
msd = (mapsidedef_t *)data;
sd = sides;
for (i=0 ; i<numsides ; i++, msd++, sd++)
{
sd->textureoffset = SHORT(msd->textureoffset)<<FRACBITS;
sd->rowoffset = SHORT(msd->rowoffset)<<FRACBITS;
sd->toptexture = R_TextureNumForName(msd->toptexture);
sd->bottomtexture = R_TextureNumForName(msd->bottomtexture);
sd->midtexture = R_TextureNumForName(msd->midtexture);
sd->sector = &sectors[SHORT(msd->sector)];
}
W_FreeLump(data);
}
//
// P_GroupLines
// Builds sector line lists and subsector sector numbers.
// Finds block bounding boxes for sectors.
//
void P_GroupLines (void)
{
line_t** linebuffer;
int i;
int j;
line_t* li;
sector_t* sector;
subsector_t* ss;
seg_t* seg;
fixed_t bbox[4];
int totallines;
// look up sector number for each subsector
ss = subsectors;
for (i=0 ; i<numsubsectors ; i++, ss++)
{
seg = &segs[ss->firstline];
ss->sector = seg->sidedef->sector;
}
// count number of lines in each sector
li = lines;
totallines = 0;
for (i=0 ; i<numlines ; i++, li++)
{
totallines++;
li->frontsector->linecount++;
if (li->backsector && li->backsector != li->frontsector)
{
li->backsector->linecount++;
totallines++;
}
}
// build line tables for each sector
linebuffer = new line_t* [totallines];
for (i=0; i<numsectors; ++i)
{
// Assign the line buffer for this sector
sectors[i].lines = linebuffer;
linebuffer += sectors[i].linecount;
// Reset linecount to zero so in the next stage we can count
// lines into the list.
sectors[i].linecount = 0;
}
// Assign lines to sectors
for (i=0; i<numlines; ++i)
{
li = &lines[i];
if (li->frontsector != NULL)
{
sector = li->frontsector;
sector->lines[sector->linecount] = li;
++sector->linecount;
}
if (li->backsector != NULL && li->frontsector != li->backsector)
{
sector = li->backsector;
sector->lines[sector->linecount] = li;
++sector->linecount;
}
}
// Generate bounding boxes for sectors
sector = sectors;
for (i=0 ; i<numsectors ; i++, sector++)
{
M_ClearBox (bbox);
for (j=0 ; j<sector->linecount; j++)
{
li = sector->lines[j];
M_AddToBox (bbox, li->v1->x, li->v1->y);
M_AddToBox (bbox, li->v2->x, li->v2->y);
}
//// // set the degenmobj_t to the middle of the bounding box
//// sector->soundorg.x = (bbox[BOXRIGHT]+bbox[BOXLEFT])/2;
//// sector->soundorg.y = (bbox[BOXTOP]+bbox[BOXBOTTOM])/2;
}
}
//
// P_SetupLevel
//
void P_SetupLevel ( const char *lumpname )
{
int lumpnum;
lumpnum = W_GetNumForName (lumpname);
// note: most of this ordering is important
/// P_LoadBlockMap (lumpnum+ML_BLOCKMAP);
P_LoadVertexes (lumpnum+ML_VERTEXES);
P_LoadSectors (lumpnum+ML_SECTORS);
P_LoadSideDefs (lumpnum+ML_SIDEDEFS);
P_LoadLineDefs (lumpnum+ML_LINEDEFS);
P_LoadSubsectors (lumpnum+ML_SSECTORS);
P_LoadNodes (lumpnum+ML_NODES);
P_LoadSegs (lumpnum+ML_SEGS);
P_GroupLines ();
/// P_LoadReject (lumpnum+ML_REJECT);
P_LoadThings (lumpnum+ML_THINGS);
}
void P_FreeLevelData (void)
{
if (vertexes)
{
delete[] vertexes;
vertexes = NULL;
numvertexes = 0;
}
if (sectors)
{
delete[] sectors;
sectors = NULL;
numsectors = 0;
}
if (sides)
{
delete[] sides;
sides = NULL;
numsides = 0;
}
if (lines)
{
delete[] lines;
lines = NULL;
numlines = 0;
}
if (segs)
{
delete[] segs;
segs = NULL;
numsegs = 0;
}
if (subsectors)
{
delete[] subsectors;
subsectors = NULL;
numsubsectors = 0;
}
if (nodes)
{
delete[] nodes;
nodes = NULL;
numnodes = 0;
}
}
} // namespace vpo