This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/common/bsplib/bspfile.c

447 lines
11 KiB
C
Raw Normal View History

2007-06-21 22:00:00 +02:00
#include "bsplib.h"
2008-07-23 22:00:00 +02:00
#include "byteorder.h"
2008-08-16 22:00:00 +02:00
#include "const.h"
2007-06-21 22:00:00 +02:00
//=============================================================================
2008-08-16 22:00:00 +02:00
wfile_t *handle;
int s_table;
2008-08-12 22:00:00 +02:00
char dentdata[MAX_MAP_ENTSTRING];
2008-09-07 22:00:00 +02:00
int entdatasize;
dshader_t dshaders[MAX_MAP_SHADERS];
int numshaders;
2008-08-12 22:00:00 +02:00
dplane_t dplanes[MAX_MAP_PLANES];
2008-09-07 22:00:00 +02:00
int numplanes;
2008-08-12 22:00:00 +02:00
dnode_t dnodes[MAX_MAP_NODES];
2008-09-07 22:00:00 +02:00
int numnodes;
dleaf_t dleafs[MAX_MAP_LEAFS];
int numleafs;
2008-08-12 22:00:00 +02:00
dword dleaffaces[MAX_MAP_LEAFFACES];
2008-09-07 22:00:00 +02:00
int numleaffaces;
2008-08-12 22:00:00 +02:00
dword dleafbrushes[MAX_MAP_LEAFBRUSHES];
2008-09-07 22:00:00 +02:00
int numleafbrushes;
dmodel_t dmodels[MAX_MAP_MODELS];
int nummodels;
2008-08-12 22:00:00 +02:00
dbrush_t dbrushes[MAX_MAP_BRUSHES];
2008-09-07 22:00:00 +02:00
int numbrushes;
2007-06-21 22:00:00 +02:00
dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
2008-09-07 22:00:00 +02:00
int numbrushsides;
dvertex_t dvertexes[MAX_MAP_VERTEXES];
int numvertexes;
2008-08-18 22:00:00 +02:00
int dindexes[MAX_MAP_INDEXES];
int numindexes;
2008-09-07 22:00:00 +02:00
byte dcollision[MAX_MAP_COLLISION];
int dcollisiondatasize = 256; // variable sized
dsurface_t dsurfaces[MAX_MAP_SURFACES];
int numsurfaces;
byte dlightdata[MAX_MAP_LIGHTDATA];
int lightdatasize;
dlightgrid_t dlightgrid[MAX_MAP_LIGHTGRID];
int numlightgrids;
byte dpvsdata[MAX_MAP_VISIBILITY];
dvis_t *dpvs = (dvis_t *)dpvsdata;
int pvsdatasize;
byte dphsdata[MAX_MAP_VISIBILITY];
dvis_t *dphs = (dvis_t *)dphsdata;
int phsdatasize;
2007-06-21 22:00:00 +02:00
//=============================================================================
/*
=============
SwapBSPFile
Byte swaps all data in a bsp file.
=============
*/
2008-09-07 22:00:00 +02:00
void SwapBSPFile( void )
2007-06-21 22:00:00 +02:00
{
2008-09-07 22:00:00 +02:00
int i;
2007-06-21 22:00:00 +02:00
2008-09-07 22:00:00 +02:00
// shaders
for( i = 0; i < numshaders; i++ )
{
dshaders[i].contents = LittleLong( dshaders[i].contents );
dshaders[i].flags = LittleLong( dshaders[i].flags );
}
2008-08-12 22:00:00 +02:00
2007-06-21 22:00:00 +02:00
// planes
2008-08-12 22:00:00 +02:00
SwapBlock( (int *)dplanes, numplanes * sizeof(dplanes[0]));
2007-06-21 22:00:00 +02:00
// nodes
2008-09-07 22:00:00 +02:00
SwapBlock( (int *)dnodes, numnodes * sizeof(dnodes[0]));
2007-06-21 22:00:00 +02:00
// leafs
2008-08-12 22:00:00 +02:00
SwapBlock( (int *)dleafs, numleafs * sizeof(dleafs[0]));
2007-06-21 22:00:00 +02:00
// leaffaces
2008-08-12 22:00:00 +02:00
SwapBlock( (int *)dleaffaces, numleaffaces * sizeof(dleaffaces[0]));
2007-06-21 22:00:00 +02:00
// leafbrushes
2008-08-12 22:00:00 +02:00
SwapBlock( (int *)dleafbrushes, numleafbrushes * sizeof(dleafbrushes[0]));
2007-06-21 22:00:00 +02:00
2008-09-07 22:00:00 +02:00
// models
SwapBlock( (int *)dmodels, nummodels * sizeof(dmodels[0]));
2007-06-21 22:00:00 +02:00
// brushes
2008-08-12 22:00:00 +02:00
SwapBlock( (int *)dbrushes, numbrushes * sizeof(dbrushes[0]));
2007-06-21 22:00:00 +02:00
// brushsides
2008-08-12 22:00:00 +02:00
SwapBlock( (int *)dbrushsides, numbrushsides * sizeof(dbrushsides[0]));
2008-09-07 22:00:00 +02:00
// vertices
// FIXME: these code swapped colors
SwapBlock( (int *)dvertexes, numvertexes * sizeof(dvertexes[0]));
2008-08-18 22:00:00 +02:00
// indices
SwapBlock( (int *)dindexes, numindexes * sizeof(dindexes[0]));
2008-09-07 22:00:00 +02:00
// surfaces
SwapBlock( (int *)dsurfaces, numsurfaces * sizeof(dsurfaces[0]));
2007-06-21 22:00:00 +02:00
// visibility
2008-09-07 22:00:00 +02:00
dpvs->numclusters = LittleLong( dpvs->numclusters );
dpvs->rowsize = LittleLong( dpvs->rowsize );
// hearability
dphs->numclusters = LittleLong( dphs->numclusters );
dphs->rowsize = LittleLong( dphs->rowsize );
2007-06-21 22:00:00 +02:00
}
2008-08-16 22:00:00 +02:00
size_t BSP_LoadLump( const char *lumpname, void *dest, size_t block_size )
{
size_t length;
byte *in;
if( !handle ) return 0;
in = WAD_Read( handle, lumpname, &length, TYPE_BINDATA );
if( length % block_size ) Sys_Break( "BSP_CopyLump: %s funny lump size\n", lumpname );
Mem_Copy( dest, in, length );
Mem_Free( in ); // no need more
return length / block_size;
}
void BSP_SaveLump( const char *lumpname, const void *data, size_t length, bool compress )
{
if( !handle ) return;
WAD_Write( handle, lumpname, data, length, TYPE_BINDATA, ( compress ? CMP_ZLIB : CMP_NONE ));
}
2007-06-21 22:00:00 +02:00
dheader_t *header;
2007-12-13 22:00:00 +01:00
int CopyLump( int lump, void *dest, int size )
2007-06-21 22:00:00 +02:00
{
int length, ofs;
length = header->lumps[lump].filelen;
ofs = header->lumps[lump].fileofs;
2007-12-13 22:00:00 +01:00
if (length % size) Sys_Error("LoadBSPFile: odd lump size");
Mem_Copy(dest, (byte *)header + ofs, length);
2007-06-21 22:00:00 +02:00
return length / size;
}
/*
=============
LoadBSPFile
=============
*/
bool LoadBSPFile( void )
{
2007-12-13 22:00:00 +01:00
int i, size;
char path[MAX_SYSPATH];
byte *buffer;
2007-06-21 22:00:00 +02:00
2008-01-05 22:00:00 +01:00
sprintf(path, "maps/%s.bsp", gs_filename );
2007-12-13 22:00:00 +01:00
buffer = (byte *)FS_LoadFile(path, &size);
2007-06-21 22:00:00 +02:00
if(!size) return false;
2007-12-13 22:00:00 +01:00
header = (dheader_t *)buffer; // load the file header
if(pe) pe->LoadBSP( buffer );
MsgDev(D_NOTE, "reading %s\n", path);
2007-06-21 22:00:00 +02:00
// swap the header
2007-11-25 22:00:00 +01:00
for(i = 0; i < sizeof(dheader_t)/4; i++)
2007-06-21 22:00:00 +02:00
((int *)header)[i] = LittleLong (((int *)header)[i]);
2008-01-05 22:00:00 +01:00
if(header->ident != IDBSPMODHEADER) Sys_Error("%s is not a IBSP file", gs_filename);
if(header->version != BSPMOD_VERSION) Sys_Error("%s is version %i, not %i", gs_filename, header->version, BSPMOD_VERSION);
2007-06-21 22:00:00 +02:00
2007-12-13 22:00:00 +01:00
entdatasize = CopyLump (LUMP_ENTITIES, dentdata, 1);
2008-09-07 22:00:00 +02:00
numshaders = CopyLump(LUMP_SHADERS, dshaders, sizeof(dshader_t));
2007-06-21 22:00:00 +02:00
numplanes = CopyLump (LUMP_PLANES, dplanes, sizeof(dplane_t));
numnodes = CopyLump (LUMP_NODES, dnodes, sizeof(dnode_t));
2007-12-13 22:00:00 +01:00
numleafs = CopyLump (LUMP_LEAFS, dleafs, sizeof(dleaf_t));
2007-06-21 22:00:00 +02:00
numleaffaces = CopyLump (LUMP_LEAFFACES, dleaffaces, sizeof(dleaffaces[0]));
numleafbrushes = CopyLump (LUMP_LEAFBRUSHES, dleafbrushes, sizeof(dleafbrushes[0]));
2007-12-13 22:00:00 +01:00
nummodels = CopyLump (LUMP_MODELS, dmodels, sizeof(dmodel_t));
2007-06-21 22:00:00 +02:00
numbrushes = CopyLump (LUMP_BRUSHES, dbrushes, sizeof(dbrush_t));
numbrushsides = CopyLump (LUMP_BRUSHSIDES, dbrushsides, sizeof(dbrushside_t));
2008-09-07 22:00:00 +02:00
numvertexes = CopyLump (LUMP_VERTICES, dvertexes, sizeof(dvertex_t));
numindexes = CopyLump (LUMP_INDICES, dindexes, sizeof(dindexes[0]));
2007-12-13 22:00:00 +01:00
dcollisiondatasize = CopyLump(LUMP_COLLISION, dcollision, 1);
2008-09-07 22:00:00 +02:00
numsurfaces = CopyLump (LUMP_SURFACES, dsurfaces, sizeof(dsurface_t));
lightdatasize = CopyLump (LUMP_LIGHTMAPS, dlightdata, 1);
pvsdatasize = CopyLump (LUMP_VISIBILITY, dpvsdata, 1);
phsdatasize = CopyLump (LUMP_HEARABILITY, dphsdata, 1);
2007-06-21 22:00:00 +02:00
// swap everything
2008-09-07 22:00:00 +02:00
SwapBSPFile();
2007-06-21 22:00:00 +02:00
return true;
}
//============================================================================
2008-08-12 22:00:00 +02:00
file_t *wadfile; // because bsp it's really wadfile with fixed lump counter
2007-06-21 22:00:00 +02:00
dheader_t outheader;
2008-08-12 22:00:00 +02:00
void AddLump( int lumpnum, const void *data, size_t length )
2007-06-21 22:00:00 +02:00
{
lump_t *lump;
lump = &header->lumps[lumpnum];
2008-08-12 22:00:00 +02:00
lump->fileofs = LittleLong( FS_Tell( wadfile ));
lump->filelen = LittleLong( length );
2007-12-13 22:00:00 +01:00
2008-08-12 22:00:00 +02:00
FS_Write( wadfile, data, (length + 3) & ~3 );
2007-06-21 22:00:00 +02:00
}
/*
=============
WriteBSPFile
Swaps the bsp file in place, so it should not be referenced again
=============
*/
void WriteBSPFile( void )
{
char path[MAX_SYSPATH];
header = &outheader;
2008-09-07 22:00:00 +02:00
memset( header, 0, sizeof( dheader_t ));
2007-06-21 22:00:00 +02:00
2008-09-07 22:00:00 +02:00
SwapBSPFile();
2007-06-21 22:00:00 +02:00
2008-09-07 22:00:00 +02:00
header->ident = LittleLong( IDBSPMODHEADER );
header->version = LittleLong( BSPMOD_VERSION );
2007-06-21 22:00:00 +02:00
2008-08-12 22:00:00 +02:00
// build path
sprintf( path, "maps/%s.bsp", gs_filename );
MsgDev( D_NOTE, "writing %s\n", path );
if( pe ) pe->FreeBSP();
2007-06-21 22:00:00 +02:00
2007-11-10 22:00:00 +01:00
wadfile = FS_Open( path, "wb" );
2008-09-07 22:00:00 +02:00
FS_Write( wadfile, header, sizeof( dheader_t )); // overwritten later
2007-06-21 22:00:00 +02:00
2007-12-13 22:00:00 +01:00
AddLump (LUMP_ENTITIES, dentdata, entdatasize);
2008-09-07 22:00:00 +02:00
AddLump (LUMP_SHADERS, dshaders, numshaders*sizeof(dshaders[0]));
AddLump (LUMP_PLANES, dplanes, numplanes*sizeof(dplanes[0]));
AddLump (LUMP_NODES, dnodes, numnodes*sizeof(dnodes[0]));
AddLump (LUMP_LEAFS, dleafs, numleafs*sizeof(dleafs[0]));
2007-06-21 22:00:00 +02:00
AddLump (LUMP_LEAFFACES, dleaffaces, numleaffaces*sizeof(dleaffaces[0]));
AddLump (LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0]));
2008-09-07 22:00:00 +02:00
AddLump (LUMP_MODELS, dmodels, nummodels*sizeof(dmodels[0]));
AddLump (LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrushes[0]));
AddLump (LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushsides[0]));
AddLump (LUMP_VERTICES, dvertexes, numvertexes*sizeof(dvertexes[0]));
AddLump (LUMP_INDICES, dindexes, numindexes*sizeof(dindexes[0]));
2007-12-13 22:00:00 +01:00
AddLump (LUMP_COLLISION, dcollision, dcollisiondatasize );
2008-09-07 22:00:00 +02:00
AddLump (LUMP_SURFACES, dsurfaces, numsurfaces*sizeof(dsurface_t));
AddLump (LUMP_LIGHTMAPS, dlightdata, lightdatasize);
AddLump (LUMP_VISIBILITY, dpvsdata, pvsdatasize);
AddLump (LUMP_HEARABILITY, dphsdata, phsdatasize);
2007-06-21 22:00:00 +02:00
FS_Seek( wadfile, 0, SEEK_SET );
FS_Write( wadfile, header, sizeof(dheader_t));
2007-12-13 22:00:00 +01:00
FS_Close( wadfile );
2007-06-21 22:00:00 +02:00
}
//============================================
int num_entities;
2007-07-28 22:00:00 +02:00
bsp_entity_t entities[MAX_MAP_ENTITIES];
2007-06-21 22:00:00 +02:00
void StripTrailing (char *e)
{
char *s;
s = e + strlen(e)-1;
while (s >= e && *s <= 32)
{
*s = 0;
s--;
}
}
/*
=================
ParseEpair
=================
*/
epair_t *ParseEpair (void)
{
epair_t *e;
e = Malloc (sizeof(epair_t));
2007-11-25 22:00:00 +01:00
if (strlen(com_token) >= MAX_KEY - 1)Sys_Error ("ParseEpar: token too long");
2007-11-11 22:00:00 +01:00
e->key = copystring(com_token);
Com_GetToken (false);
2007-11-25 22:00:00 +01:00
if (strlen(com_token) >= MAX_VALUE - 1)Sys_Error ("ParseEpar: token too long");
2007-11-11 22:00:00 +01:00
e->value = copystring(com_token);
2007-06-21 22:00:00 +02:00
// strip trailing spaces
StripTrailing (e->key);
StripTrailing (e->value);
return e;
}
/*
================
ParseEntity
================
*/
bool ParseEntity (void)
{
epair_t *e;
2007-07-28 22:00:00 +02:00
bsp_entity_t *mapent;
2007-06-21 22:00:00 +02:00
2007-11-11 22:00:00 +01:00
if (!Com_GetToken (true)) return false;
2007-06-21 22:00:00 +02:00
2007-11-25 22:00:00 +01:00
if (!Com_MatchToken( "{")) Sys_Error ("ParseEntity: { not found");
2007-06-21 22:00:00 +02:00
if (num_entities == MAX_MAP_ENTITIES) Sys_Error ("num_entities == MAX_MAP_ENTITIES");
mapent = &entities[num_entities];
num_entities++;
do
{
2007-11-25 22:00:00 +01:00
if (!Com_GetToken (true)) Sys_Error("ParseEntity: EOF without closing brace");
2007-11-11 22:00:00 +01:00
if (Com_MatchToken("}")) break;
2007-11-25 22:00:00 +01:00
e = ParseEpair();
2007-06-21 22:00:00 +02:00
e->next = mapent->epairs;
mapent->epairs = e;
} while (1);
return true;
}
/*
================
ParseEntities
Parses the dentdata string into entities
================
*/
void ParseEntities (void)
{
num_entities = 0;
2007-11-11 22:00:00 +01:00
if(Com_LoadScript("entities", dentdata, entdatasize))
2007-06-21 22:00:00 +02:00
while(ParseEntity());
}
/*
================
UnparseEntities
Generates the dentdata string from all the entities
================
*/
void UnparseEntities (void)
{
char *buf, *end;
epair_t *ep;
char line[2048];
int i;
char key[1024], value[1024];
buf = dentdata;
end = buf;
*end = 0;
2007-11-25 22:00:00 +01:00
for (i = 0; i < num_entities; i++)
2007-06-21 22:00:00 +02:00
{
ep = entities[i].epairs;
if (!ep) continue; // ent got removed
strcat (end,"{\n");
end += 2;
for (ep = entities[i].epairs ; ep ; ep=ep->next)
{
strcpy (key, ep->key);
StripTrailing (key);
strcpy (value, ep->value);
StripTrailing (value);
sprintf (line, "\"%s\" \"%s\"\n", key, value);
strcat (end, line);
end += strlen(line);
}
strcat (end,"}\n");
end += 2;
2007-11-25 22:00:00 +01:00
if (end > buf + MAX_MAP_ENTSTRING) Sys_Error("Entity text too long");
2007-06-21 22:00:00 +02:00
}
entdatasize = end - buf + 1;
}
2007-07-28 22:00:00 +02:00
void SetKeyValue (bsp_entity_t *ent, char *key, char *value)
2007-06-21 22:00:00 +02:00
{
epair_t *ep;
for (ep=ent->epairs ; ep ; ep=ep->next)
if (!strcmp (ep->key, key) )
{
2008-01-28 22:00:00 +01:00
Mem_Free (ep->value);
2007-06-21 22:00:00 +02:00
ep->value = copystring(value);
return;
}
ep = Malloc (sizeof(*ep));
ep->next = ent->epairs;
ent->epairs = ep;
ep->key = copystring(key);
ep->value = copystring(value);
}
2007-07-28 22:00:00 +02:00
char *ValueForKey (bsp_entity_t *ent, char *key)
2007-06-21 22:00:00 +02:00
{
epair_t *ep;
for (ep=ent->epairs ; ep ; ep=ep->next)
if (!strcmp (ep->key, key) )
return ep->value;
return "";
}
2007-07-28 22:00:00 +02:00
vec_t FloatForKey (bsp_entity_t *ent, char *key)
2007-06-21 22:00:00 +02:00
{
char *k;
k = ValueForKey (ent, key);
return atof(k);
}
2007-07-28 22:00:00 +02:00
void GetVectorForKey (bsp_entity_t *ent, char *key, vec3_t vec)
2007-06-21 22:00:00 +02:00
{
char *k;
double v1, v2, v3;
k = ValueForKey (ent, key);
// scanf into doubles, then assign, so it is vec_t size independent
v1 = v2 = v3 = 0;
sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);
vec[0] = v1;
vec[1] = v2;
vec[2] = v3;
}