home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
D!Zone (Collector's Edition)
/
D_ZONE_CD.ISO
/
programs
/
editors
/
bsp12x
/
bsp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-12-06
|
20KB
|
766 lines
/*- BSP.C -------------------------------------------------------------------*
Node builder for DOOM levels (c) 1994 Colin Reed, version 1.2 (dos extended)
Many thanks to Mark Harrison for finding a bug in 1.1 which caused some
texture align problems when a flipped SEG was split.
Credit to:-
Raphael Quinet (A very small amount of code has been borrowed from DEU).
Matt Fell for the doom specs.
Also, the original idea for some of the techniques where also taken from the
comment at the bottom of OBJECTS.C in DEU, and the doc by Matt Fell about
the nodes.
Use this code for your own editors, but please credit me.
*---------------------------------------------------------------------------*/
#include "bsp.h"
#include "structs.h"
/*- Global Vars ------------------------------------------------------------*/
FILE *infile,*outfile;
char *testwad;
char *outwad;
struct wad_header *wad = NULL;
struct directory *direc = NULL;
char rname[]="\0\0\0\0\0\0\0\0";
struct Thing *things;
long num_things = 0;
long things_start = 0;
struct Vertex *vertices;
long num_verts = 0;
long vertices_start = 0;
struct LineDef *linedefs;
long num_lines = 0;
long linedefs_start = 0;
struct SideDef *sidedefs;
long num_sides = 0;
long sidedefs_start = 0;
struct Sector *sectors;
long num_sects = 0;
long sectors_start = 0;
struct SSector *ssectors;
long num_ssectors = 0;
long ssectors_start = 0;
struct Pseg *psegs = NULL;
long num_psegs = 0;
long segs_start = 0;
struct Pnode *pnodes = NULL;
long num_pnodes = 0;
long pnode_indx = 0;
long pnodes_start = 0;
struct Seg *tsegs = NULL;
long num_tsegs = 0;
struct Node *nodelist = NULL;
long num_nodes = 0;
long reject_start;
long reject_size;
struct Block blockhead;
short int *blockptrs;
short int *blocklists=NULL;
long blockptrs_size;
long blockmap_size;
long blockmap_start;
struct splitter sp;
short node_x;
short node_y;
short node_dx;
short node_dy;
short lminx;
short lmaxx;
short lminy;
short lmaxy;
short mapminx;
short mapmaxx;
short mapminy;
short mapmaxy;
long psx,psy,pex,pey,pdx,pdy;
long lsx,lsy,lex,ley;
unsigned char pcnt;
long fagcount = 0;
/*- Prototypes -------------------------------------------------------------*/
void OpenWadFile(char *);
void Printname(struct directory *);
int FindDir(char *);
void GetThings(void);
void GetVertexes(void);
void GetLinedefs(void);
void GetSidedefs(void);
void GetSectors(void);
void FindLimits(struct Seg *);
struct Seg *CreateSegs();
struct Node *CreateNode(struct Seg *);
void DivideSegs(struct Seg *,struct Seg **,struct Seg **);
int IsItConvex(struct Seg *);
struct Seg *PickNode(struct Seg *);
void ComputeIntersection(short int *,short int *);
int DoLinesIntersect();
int SplitDist(struct Seg *);
void ReverseNodes(struct Node *);
long CreateBlockmap(void);
inline void progress(void);
/*--------------------------------------------------------------------------*/
#include "makenode.c"
#include "picknode.c"
#include "funcs.c"
/*- Main Program -----------------------------------------------------------*/
int main(int argc,char *argv[])
{
long dir_start = 12; /* Skip Pwad header*/
long dir_entries = 0;
int n;
unsigned char *data;
printf("** Doom BSP node builder ver 1.2x (c) 1994 Colin Reed **\n");
if(argc<2 || argc>3)
{
printf("\nThis Node builder was created from the basic theory stated in DEU5 (OBJECTS.C)\n");
printf("\nCredits should go to :-\n");
printf("Matt Fell (matt.burnett@acebbs.com) for the Doom Specs.\n");
printf("Raphael Quinet (quinet@montefiore.ulg.ac.be) for DEU and the original idea.\n");
printf("Mark Harrison (harrison@lclark.edu) for finding a bug in 1.1x\n");
printf("\nUsage: BSP name.wad {output.wad}\n");
printf(" : (If no output.wad is specified, TMP.WAD is written)\n");
exit(0);
}
testwad = argv[1]; /* Get input name*/
if(argc == 3) outwad = argv[2]; /* Get output name*/
else outwad = "tmp.wad";
OpenWadFile(testwad); /* Opens and reads directory*/
GetThings(); /* of wad file*/
GetLinedefs(); /* Get linedefs and vertices*/
GetVertexes(); /* and delete redundant.*/
GetSidedefs();
GetSectors();
num_tsegs = 0;
tsegs = CreateSegs(); /* Initially create segs*/
FindLimits(tsegs); /* Find limits of vertices*/
mapminx = lminx; /* store as map limits*/
mapmaxx = lmaxx;
mapminy = lminy;
mapmaxy = lmaxy;
printf("Map goes from X (%d,%d) Y (%d,%d)\n",lminx,lmaxx,lminy,lmaxy);
num_nodes = 0;
nodelist = CreateNode(tsegs); /* recursively create nodes*/
printf("%lu NODES created, with %lu SSECTORS.\n",num_nodes,num_ssectors);
pnodes = GetMemory(sizeof(struct Pnode)*num_nodes);
num_pnodes = 0;
pnode_indx = 0;
ReverseNodes(nodelist);
dir_entries++; /* Skip level number*/
things_start = dir_start;
dir_start = dir_start + (sizeof(struct Thing)*num_things);
direc[dir_entries].start = things_start;
direc[dir_entries].length = (sizeof(struct Thing)*num_things);
dir_entries++;
linedefs_start = dir_start;
dir_start = dir_start + (sizeof(struct LineDef)*num_lines);
direc[dir_entries].start = linedefs_start;
direc[dir_entries].length = (sizeof(struct LineDef)*num_lines);
dir_entries++;
sidedefs_start = dir_start;
dir_start = dir_start + (sizeof(struct SideDef)*num_sides);
direc[dir_entries].start = sidedefs_start;
direc[dir_entries].length = (sizeof(struct SideDef)*num_sides);
dir_entries++;
printf("Found %lu used vertices\n",num_verts);
vertices_start = dir_start;
dir_start = dir_start + (sizeof(struct Vertex)*num_verts);
direc[dir_entries].start = vertices_start;
direc[dir_entries].length = (sizeof(struct Vertex)*num_verts);
dir_entries++;
segs_start = dir_start;
dir_start = dir_start + (sizeof(struct Pseg)*num_psegs);
direc[dir_entries].start = segs_start;
direc[dir_entries].length = (sizeof(struct Pseg)*num_psegs);
dir_entries++;
ssectors_start = dir_start;
dir_start = dir_start + (sizeof(struct SSector)*num_ssectors);
direc[dir_entries].start = ssectors_start;
direc[dir_entries].length = (sizeof(struct SSector)*num_ssectors);
dir_entries++;
pnodes_start = dir_start;
dir_start = dir_start + (sizeof(struct Pnode)*num_pnodes);
direc[dir_entries].start = pnodes_start;
direc[dir_entries].length = (sizeof(struct Pnode)*num_pnodes);
dir_entries++;
sectors_start = dir_start;
dir_start = dir_start + (sizeof(struct Sector)*num_sects);
direc[dir_entries].start = sectors_start;
direc[dir_entries].length = (sizeof(struct Sector)*num_sects);
dir_entries++;
reject_size = (num_sects*num_sects+7)/8;
data = calloc(reject_size,1);
reject_start = dir_start;
dir_start+=reject_size;
direc[dir_entries].start = reject_start; /* Skip reject map*/
direc[dir_entries].length = reject_size;
dir_entries++;
blockmap_size = CreateBlockmap();
blockmap_start = dir_start;
dir_start = dir_start + (blockmap_size+blockptrs_size+8);
direc[dir_entries].start = blockmap_start;
direc[dir_entries].length = (blockmap_size+blockptrs_size+8);
dir_entries++;
printf("Completed blockmap building and saved PWAD as %s\n",outwad);
if((outfile = fopen(outwad,"wb")) == NULL)
{
printf("Error: Could not open output PWAD file %s", outwad);
exit(0);
}
fwrite(wad,4,1,outfile);
fwrite(&dir_entries,sizeof(long),1,outfile);
fwrite(&dir_start,sizeof(long),1,outfile);
fwrite(things,(sizeof(struct Thing)*num_things),1,outfile);
fwrite(linedefs,(sizeof(struct LineDef)*num_lines),1,outfile);
fwrite(sidedefs,(sizeof(struct SideDef)*num_sides),1,outfile);
fwrite(vertices,(sizeof(struct Vertex)*num_verts),1,outfile);
fwrite(psegs,(sizeof(struct Pseg)*num_psegs),1,outfile);
fwrite(ssectors,(sizeof(struct SSector)*num_ssectors),1,outfile);
fwrite(pnodes,(sizeof(struct Pnode)*num_pnodes),1,outfile);
fwrite(sectors,(sizeof(struct Sector)*num_sects),1,outfile);