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

805 lines
19 KiB
C
Raw Normal View History

2008-09-10 22:00:00 +02:00
//=======================================================================
// Copyright XashXT Group 2007 <20>
// bspfile.c - read\save bsp file
//=======================================================================
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;
2008-09-10 22:00:00 +02:00
file_t *wadfile;
2008-08-16 22:00:00 +02:00
int s_table;
2008-09-10 22:00:00 +02:00
dheader_t *header;
dheader_t outheader;
int num_entities;
bsp_entity_t entities[MAX_MAP_ENTITIES];
2008-09-28 22:00:00 +02:00
int num_lightbytes;
byte *lightbytes;
2008-09-10 22:00:00 +02:00
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-28 22:00:00 +02:00
dfog_t dfogs[MAX_MAP_FOGS];
int numfogs;
2008-09-07 22:00:00 +02:00
dsurface_t dsurfaces[MAX_MAP_SURFACES];
int numsurfaces;
2008-09-28 22:00:00 +02:00
byte dcollision[MAX_MAP_COLLISION];
int dcollisiondatasize;
dlightgrid_t dlightgrid[MAX_MAP_LIGHTGRID];
2008-09-10 22:00:00 +02:00
int numgridpoints;
byte dvisdata[MAX_MAP_VISIBILITY];
dvis_t *dvis = (dvis_t *)dvisdata;
2008-09-28 22:00:00 +02:00
int visdatasize;
2007-06-21 22:00:00 +02:00
//=============================================================================
2008-09-10 22:00:00 +02:00
/*
===============
CompressVis
===============
*/
int CompressVis( byte *vis, byte *dest )
{
int j;
int rep;
int visrow;
byte *dest_p;
dest_p = dest;
visrow = (dvis->numclusters + 7)>>3;
for( j = 0; j < visrow; j++ )
{
*dest_p++ = vis[j];
if( vis[j] ) continue;
rep = 1;
for( j++; j < visrow; j++ )
if( vis[j] || rep == 255 )
break;
else rep++;
*dest_p++ = rep;
j--;
}
return dest_p - dest;
}
/*
===================
DecompressVis
===================
*/
void DecompressVis( byte *in, byte *decompressed )
{
int c;
byte *out;
int row;
row = (dvis->numclusters+7)>>3;
out = decompressed;
do
{
if( *in )
{
*out++ = *in++;
continue;
}
c = in[1];
if( !c ) Sys_Error( "DecompressVis: 0 repeat\n" );
in += 2;
while( c )
{
*out++ = 0;
c--;
}
} while( out - decompressed < row );
}
2007-06-21 22:00:00 +02:00
/*
=============
SwapBSPFile
Byte swaps all data in a bsp file.
=============
*/
2008-09-10 22:00:00 +02:00
void SwapBSPFile( bool todisk )
2007-06-21 22:00:00 +02:00
{
2008-09-10 22:00:00 +02:00
int i, j;
2007-06-21 22:00:00 +02:00
2008-09-07 22:00:00 +02:00
// shaders
for( i = 0; i < numshaders; i++ )
{
2008-09-28 22:00:00 +02:00
dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
2008-09-07 22:00:00 +02:00
dshaders[i].contents = LittleLong( dshaders[i].contents );
2008-09-28 22:00:00 +02:00
}
// fogs
for( i = 0; i < numfogs; i++ )
{
dfogs[i].brushnum = LittleLong( dfogs[i].brushnum );
dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
2008-09-07 22:00:00 +02:00
}
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
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-10 22:00:00 +02:00
if( todisk ) j = dvis->numclusters;
else j = LittleLong( dvis->numclusters );
dvis->numclusters = LittleLong( dvis->numclusters );
for( i = 0; i < j; i++ )
{
dvis->bitofs[i][0] = LittleLong( dvis->bitofs[i][0] );
dvis->bitofs[i][1] = LittleLong( dvis->bitofs[i][1] );
}
2007-06-21 22:00:00 +02:00
}
2008-09-10 22:00:00 +02:00
#ifdef BSP_WADFILE
size_t CopyLump( const char *lumpname, void *dest, size_t block_size )
2008-08-16 22:00:00 +02:00
{
size_t length;
byte *in;
if( !handle ) return 0;
in = WAD_Read( handle, lumpname, &length, TYPE_BINDATA );
2008-09-10 22:00:00 +02:00
if( length % block_size ) Sys_Break( "LoadBSPFile: %s funny lump size\n", lumpname );
2008-08-16 22:00:00 +02:00
Mem_Copy( dest, in, length );
Mem_Free( in ); // no need more
return length / block_size;
}
2008-09-10 22:00:00 +02:00
void AddLump( const char *lumpname, const void *data, size_t length )
2008-08-16 22:00:00 +02:00
{
2008-09-10 22:00:00 +02:00
int compress = CMP_NONE;
2008-08-16 22:00:00 +02:00
if( !handle ) return;
2008-09-10 22:00:00 +02:00
if( length > 0xffff ) compress = CMP_ZLIB;
WAD_Write( handle, lumpname, data, length, TYPE_BINDATA, compress );
2008-08-16 22:00:00 +02:00
}
2008-09-10 22:00:00 +02:00
#else
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
size_t CopyLump( const int lumpname, void *dest, size_t block_size )
2007-06-21 22:00:00 +02:00
{
2008-09-10 22:00:00 +02:00
size_t length, ofs;
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
length = header->lumps[lumpname].filelen;
ofs = header->lumps[lumpname].fileofs;
2007-06-21 22:00:00 +02:00
2008-09-28 22:00:00 +02:00
if( length % block_size) Sys_Break( "LoadBSPFile: %i funny lump size\n", lumpname );
2007-12-13 22:00:00 +01:00
Mem_Copy(dest, (byte *)header + ofs, length);
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
return length / block_size;
2007-06-21 22:00:00 +02:00
}
2008-09-10 22:00:00 +02:00
void AddLump( const int lumpname, const void *data, size_t length )
{
lump_t *lump;
lump = &header->lumps[lumpname];
lump->fileofs = LittleLong( FS_Tell( wadfile ));
lump->filelen = LittleLong( length );
FS_Write( wadfile, data, (length + 3) & ~3 );
}
#endif
2008-09-28 22:00:00 +02:00
static void CopyLightGridLumps( void )
{
int i, index;
word *inArray;
dlightgrid_t *in, *out;
if( header->lumps[LUMP_LIGHTARRAY].filelen % sizeof(*inArray))
Sys_Break( "LoadBSPFile: %i funny lump size\n", LUMP_LIGHTARRAY );
numgridpoints = header->lumps[LUMP_LIGHTARRAY].filelen / sizeof(*inArray);
inArray = (void*)( (byte*)header + header->lumps[LUMP_LIGHTARRAY].fileofs);
in = (void*)( (byte*)header + header->lumps[LUMP_LIGHTGRID].fileofs);
out = &dlightgrid[0];
for( i = 0; i < numgridpoints; i++ )
{
index = LittleShort( *inArray );
Mem_Copy( out, &in[index], sizeof( *in ));
inArray++;
out++;
}
}
static void AddLightGridLumps( void )
{
int i, j, k, c, d;
int numGridPoints, maxGridPoints;
dlightgrid_t *gridPoints, *in, *out;
int numGridArray;
word *gridArray;
bool bad;
maxGridPoints = (numgridpoints < MAX_MAP_GRID) ? numgridpoints : MAX_MAP_GRID;
gridPoints = BSP_Malloc( maxGridPoints * sizeof( *gridPoints ));
gridArray = BSP_Malloc( numgridpoints * sizeof( *gridArray ));
numGridPoints = 0;
numGridArray = numgridpoints;
// for each bsp grid point, find an approximate twin
MsgDev( D_INFO, "Storing lightgrid: %d points\n", numgridpoints );
for( i = 0; i < numGridArray; i++ )
{
in = &dlightgrid[i];
for( j = 0; j < numgridpoints; j++ )
{
out = &gridPoints[j];
if(*((uint*) in->styles) != *((uint*) out->styles))
continue;
d = abs( in->latLong[0] - out->latLong[0] );
if( d < (255 - LG_EPSILON) && d > LG_EPSILON )
continue;
d = abs( in->latLong[1] - out->latLong[1] );
if( d < 255 - LG_EPSILON && d > LG_EPSILON )
continue;
// compare light
bad = false;
for( k = 0; (k < LM_STYLES && bad == false); k++ )
{
for( c = 0; c < 3; c++ )
{
if(abs((int)in->ambient[k][c] - (int)out->ambient[k][c]) > LG_EPSILON
|| abs((int)in->direct[k][c] - (int)out->direct[k][c]) > LG_EPSILON )
{
bad = true;
break;
}
}
}
if( bad ) continue;
break; // found
}
// set sample index
gridArray[i] = (word)j;
// if no sample found, add a new one
if( j >= numGridPoints && numGridPoints < maxGridPoints )
{
out = &gridPoints[numGridPoints++];
memcpy( out, in, sizeof( *in ) );
}
}
// swap array
for( i = 0; i < numGridArray; i++ ) gridArray[i] = LittleShort( gridArray[i] );
AddLump( LUMP_LIGHTGRID, gridPoints, (numGridPoints * sizeof( *gridPoints )));
AddLump( LUMP_LIGHTARRAY, gridArray, (numGridArray * sizeof( *gridArray )));
if( gridPoints ) Mem_Free( gridPoints );
if( gridArray ) Mem_Free( gridArray );
}
2007-06-21 22:00:00 +02:00
/*
=============
LoadBSPFile
=============
*/
bool LoadBSPFile( void )
{
2007-12-13 22:00:00 +01:00
byte *buffer;
2007-06-21 22:00:00 +02:00
2008-09-12 22:00:00 +02:00
buffer = (byte *)FS_LoadFile( va("maps/%s.bsp", gs_filename ), NULL );
2008-09-10 22:00:00 +02:00
if( !buffer ) return false;
2007-12-13 22:00:00 +01:00
header = (dheader_t *)buffer; // load the file header
2008-09-10 22:00:00 +02:00
if( pe ) pe->LoadBSP( buffer );
2007-12-13 22:00:00 +01:00
2008-09-10 22:00:00 +02:00
MsgDev( D_NOTE, "reading %s.bsp\n", gs_filename );
2007-06-21 22:00:00 +02:00
// swap the header
2008-09-10 22:00:00 +02:00
SwapBlock( (int *)header, sizeof( header ));
if( header->ident != IDBSPMODHEADER )
Sys_Break( "%s.bsp is not a IBSP file\n", gs_filename );
if( header->version != BSPMOD_VERSION )
Sys_Break( "%s.bsp is version %i, not %i\n", gs_filename, header->version, BSPMOD_VERSION );
entdatasize = CopyLump( LUMP_ENTITIES, dentdata, 1);
numshaders = CopyLump( LUMP_SHADERS, dshaders, sizeof(dshader_t));
numplanes = CopyLump( LUMP_PLANES, dplanes, sizeof(dplane_t));
numnodes = CopyLump( LUMP_NODES, dnodes, sizeof(dnode_t));
numleafs = CopyLump( LUMP_LEAFS, dleafs, sizeof(dleaf_t));
2008-09-28 22:00:00 +02:00
numleaffaces = CopyLump( LUMP_LEAFSURFACES, dleaffaces, sizeof(dleaffaces[0]));
2008-09-10 22:00:00 +02:00
numleafbrushes = CopyLump( LUMP_LEAFBRUSHES, dleafbrushes, sizeof(dleafbrushes[0]));
nummodels = CopyLump( LUMP_MODELS, dmodels, sizeof(dmodel_t));
numbrushes = CopyLump( LUMP_BRUSHES, dbrushes, sizeof(dbrush_t));
numbrushsides = CopyLump( LUMP_BRUSHSIDES, dbrushsides, sizeof(dbrushside_t));
numvertexes = CopyLump( LUMP_VERTICES, dvertexes, sizeof(dvertex_t));
numindexes = CopyLump( LUMP_INDICES, dindexes, sizeof(dindexes[0]));
2008-09-28 22:00:00 +02:00
numfogs = CopyLump( LUMP_FOGS, dfogs, sizeof( dfog_t ));
2008-09-10 22:00:00 +02:00
numsurfaces = CopyLump( LUMP_SURFACES, dsurfaces, sizeof(dsurfaces[0]));
2008-09-28 22:00:00 +02:00
dcollisiondatasize = CopyLump( LUMP_COLLISION, dcollision, 1);
2008-09-10 22:00:00 +02:00
visdatasize = CopyLump( LUMP_VISIBILITY, dvisdata, 1);
2008-09-28 22:00:00 +02:00
CopyLightGridLumps();
2007-06-21 22:00:00 +02:00
// swap everything
2008-09-10 22:00:00 +02:00
SwapBSPFile( false );
2007-06-21 22:00:00 +02:00
return true;
}
//============================================================================
/*
=============
WriteBSPFile
Swaps the bsp file in place, so it should not be referenced again
=============
*/
void WriteBSPFile( void )
{
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-10 22:00:00 +02:00
SwapBSPFile( true );
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
2008-09-10 22:00:00 +02:00
MsgDev( D_NOTE, "writing %s.bsp\n", gs_filename );
2008-08-12 22:00:00 +02:00
if( pe ) pe->FreeBSP();
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
wadfile = FS_Open( va( "maps/%s.bsp", gs_filename ), "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]));
2008-09-28 22:00:00 +02:00
AddLump (LUMP_LEAFSURFACES, dleaffaces, numleaffaces*sizeof(dleaffaces[0]));
2007-06-21 22:00:00 +02:00
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]));
2008-09-28 22:00:00 +02:00
AddLump (LUMP_FOGS, dfogs, numfogs*sizeof(dfogs[0]));
2008-09-09 22:00:00 +02:00
AddLump (LUMP_SURFACES, dsurfaces, numsurfaces*sizeof(dsurfaces[0]));
2008-09-28 22:00:00 +02:00
AddLump (LUMP_COLLISION, dcollision, dcollisiondatasize );
AddLightGridLumps();
2008-09-10 22:00:00 +02:00
AddLump (LUMP_VISIBILITY, dvisdata, visdatasize);
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
}
2008-09-10 22:00:00 +02:00
//============================================
// misc parse functions
2007-06-21 22:00:00 +02:00
//============================================
2008-09-10 22:00:00 +02:00
void StripTrailing( char *e )
2007-06-21 22:00:00 +02:00
{
char *s;
2008-09-10 22:00:00 +02:00
s = e + com.strlen( e ) - 1;
while( s >= e && *s <= 32 )
2007-06-21 22:00:00 +02:00
{
*s = 0;
s--;
}
}
/*
=================
ParseEpair
=================
*/
2008-09-10 22:00:00 +02:00
epair_t *ParseEpair( void )
2007-06-21 22:00:00 +02:00
{
epair_t *e;
2008-09-10 22:00:00 +02:00
e = BSP_Malloc( sizeof( epair_t ));
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
if( com.strlen( com_token ) >= MAX_KEY - 1 )
Sys_Break( "ParseEpair: token too long\n" );
e->key = copystring( com_token );
Com_GetToken( false );
if( com.strlen( com_token ) >= MAX_VALUE - 1 )
Sys_Break( "ParseEpair: token too long\n" );
e->value = copystring( com_token );
2007-06-21 22:00:00 +02:00
// strip trailing spaces
2008-09-10 22:00:00 +02:00
StripTrailing( e->key );
StripTrailing( e->value );
2007-06-21 22:00:00 +02:00
return e;
}
/*
================
ParseEntity
================
*/
2008-09-10 22:00:00 +02:00
bool ParseEntity( void )
2007-06-21 22:00:00 +02:00
{
2008-09-10 22:00:00 +02:00
epair_t *e;
2007-07-28 22:00:00 +02:00
bsp_entity_t *mapent;
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
if( !Com_GetToken( true ))
return false;
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
if( !Com_MatchToken( "{" ))
Sys_Break( "ParseEntity: '{' not found\n" );
if( num_entities == MAX_MAP_ENTITIES )
Sys_Break( "MAX_MAP_ENTITIES limit excceded\n" );
2007-06-21 22:00:00 +02:00
mapent = &entities[num_entities];
num_entities++;
2008-09-10 22:00:00 +02:00
while( 1 )
2007-06-21 22:00:00 +02:00
{
2008-09-10 22:00:00 +02:00
if( !Com_GetToken( true ))
Sys_Break( "ParseEntity: EOF without closing brace\n" );
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;
2008-09-10 22:00:00 +02:00
}
2007-06-21 22:00:00 +02:00
return true;
}
/*
================
ParseEntities
Parses the dentdata string into entities
================
*/
2008-09-10 22:00:00 +02:00
void ParseEntities( void )
2007-06-21 22:00:00 +02:00
{
num_entities = 0;
2008-09-10 22:00:00 +02:00
if( Com_LoadScript( "entities", dentdata, entdatasize ))
while( ParseEntity( ));
2007-06-21 22:00:00 +02:00
}
/*
================
UnparseEntities
Generates the dentdata string from all the entities
================
*/
2008-09-10 22:00:00 +02:00
void UnparseEntities( void )
2007-06-21 22:00:00 +02:00
{
2008-09-28 22:00:00 +02:00
epair_t *ep;
char *buf, *end;
char line[2048];
char key[MAX_KEY], value[MAX_VALUE];
const char *value2;
int i;
2007-06-21 22:00:00 +02:00
buf = dentdata;
end = buf;
*end = 0;
2008-09-10 22:00:00 +02:00
for( i = 0; i < num_entities; i++ )
2007-06-21 22:00:00 +02:00
{
ep = entities[i].epairs;
2008-09-10 22:00:00 +02:00
if( !ep ) continue; // ent got removed
2008-09-28 22:00:00 +02:00
// certain entities get stripped from bsp file */
value2 = ValueForKey( &entities[i], "classname" );
if(!com.stricmp( value2, "_decal" ) || !com.stricmp( value2, "_skybox" ))
continue;
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
com.strcat( end, "{\n" );
2007-06-21 22:00:00 +02:00
end += 2;
2008-09-10 22:00:00 +02:00
for( ep = entities[i].epairs; ep; ep = ep->next )
2007-06-21 22:00:00 +02:00
{
2008-09-10 22:00:00 +02:00
com.strncpy( key, ep->key, MAX_KEY );
StripTrailing( key );
com.strncpy( value, ep->value, MAX_VALUE );
StripTrailing( value );
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
com.snprintf( line, 2048, "\"%s\" \"%s\"\n", key, value );
com.strcat( end, line );
end += com.strlen( line );
2007-06-21 22:00:00 +02:00
}
2008-09-10 22:00:00 +02:00
com.strcat( end, "}\n" );
2007-06-21 22:00:00 +02:00
end += 2;
2008-09-10 22:00:00 +02:00
if( end > buf + MAX_MAP_ENTSTRING )
Sys_Break( "Entity text too long\n" );
2007-06-21 22:00:00 +02:00
}
entdatasize = end - buf + 1;
}
2008-09-10 22:00:00 +02:00
void SetKeyValue( bsp_entity_t *ent, const char *key, const char *value )
2007-06-21 22:00:00 +02:00
{
epair_t *ep;
2008-09-10 22:00:00 +02:00
for( ep = ent->epairs; ep; ep = ep->next )
{
if(!com.strcmp( ep->key, key ))
2007-06-21 22:00:00 +02:00
{
2008-09-10 22:00:00 +02:00
Mem_Free( ep->value );
ep->value = copystring( value );
2007-06-21 22:00:00 +02:00
return;
}
2008-09-10 22:00:00 +02:00
}
ep = BSP_Malloc( sizeof( *ep ));
2007-06-21 22:00:00 +02:00
ep->next = ent->epairs;
ent->epairs = ep;
2008-09-10 22:00:00 +02:00
ep->key = copystring( key );
ep->value = copystring( value );
2007-06-21 22:00:00 +02:00
}
2008-09-28 22:00:00 +02:00
char *ValueForKey( const bsp_entity_t *ent, const char *key )
2007-06-21 22:00:00 +02:00
{
epair_t *ep;
2008-09-28 22:00:00 +02:00
if( !ent ) return "";
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
for( ep = ent->epairs; ep; ep = ep->next )
{
if(!com.strcmp( ep->key, key ))
2007-06-21 22:00:00 +02:00
return ep->value;
2008-09-10 22:00:00 +02:00
}
2007-06-21 22:00:00 +02:00
return "";
}
2008-09-28 22:00:00 +02:00
long IntForKey( const bsp_entity_t *ent, const char *key )
{
char *k;
k = ValueForKey( ent, key );
return com.atoi( k );
}
vec_t FloatForKey( const bsp_entity_t *ent, const char *key )
2007-06-21 22:00:00 +02:00
{
char *k;
2008-09-10 22:00:00 +02:00
k = ValueForKey( ent, key );
return com.atof( k );
2007-06-21 22:00:00 +02:00
}
2008-09-28 22:00:00 +02:00
void GetVectorForKey( const bsp_entity_t *ent, const char *key, vec3_t vec )
2007-06-21 22:00:00 +02:00
{
char *k;
double v1, v2, v3;
2008-09-28 22:00:00 +02:00
k = ValueForKey( ent, key );
2008-09-10 22:00:00 +02:00
2007-06-21 22:00:00 +02:00
// scanf into doubles, then assign, so it is vec_t size independent
v1 = v2 = v3 = 0;
2008-09-10 22:00:00 +02:00
sscanf( k, "%lf %lf %lf", &v1, &v2, &v3 );
VectorSet( vec, v1, v2, v3 );
2007-06-21 22:00:00 +02:00
}
2008-09-28 22:00:00 +02:00
/*
================
FindTargetEntity
finds an entity target
================
*/
bsp_entity_t *FindTargetEntity( const char *target )
{
int i;
const char *n;
// walk entity list
for( i = 0; i < num_entities; i++ )
{
n = ValueForKey( &entities[i], "targetname" );
if( !com.strcmp( n, target ))
return &entities[i];
}
return NULL;
}
/*
================
GetEntityShadowFlags
gets an entity's shadow flags
note: does not set them to defaults if the keys are not found!
================
*/
void GetEntityShadowFlags( const bsp_entity_t *ent, const bsp_entity_t *ent2, int *castShadows, int *recvShadows )
{
const char *value;
// get cast shadows
// FIXME: make ZHLT shadowflags support here
if( castShadows != NULL )
{
value = ValueForKey( ent, "_castShadows" );
if( value[0] == '\0' ) value = ValueForKey( ent, "_cs" );
if( value[0] == '\0' ) value = ValueForKey( ent2, "_castShadows" );
if( value[0] == '\0' ) value = ValueForKey( ent2, "_cs" );
if( value[0] != '\0' ) *castShadows = com.atoi( value );
}
// receive
if( recvShadows != NULL )
{
value = ValueForKey( ent, "_receiveShadows" );
if( value[0] == '\0' ) value = ValueForKey( ent, "_rs" );
if( value[0] == '\0' ) value = ValueForKey( ent2, "_receiveShadows" );
if( value[0] == '\0' ) value = ValueForKey( ent2, "_rs" );
if( value[0] != '\0' ) *recvShadows = com.atoi( value );
}
}
/*
================
Com_GetTokenAppend
gets a token and appends its text to the specified buffer
================
*/
static int oldScriptLine = 0;
static int tabDepth = 0;
bool Com_GetTokenAppend( char *buffer, bool crossline )
{
bool r;
int i;
r = Com_GetToken( crossline ) ? true : false;
if( r == false || buffer == NULL || com_token[0] == '\0' )
return r;
// pre-tabstops
if( com_token[0] == '}' ) tabDepth--;
// append?
if( oldScriptLine != scriptline )
{
com.strcat( buffer, "\n" );
for( i = 0; i < tabDepth; i++ )
com.strcat( buffer, "\t" );
}
else com.strcat( buffer, " " );
oldScriptLine = scriptline;
com.strcat( buffer, com_token );
// post-tabstops
if( com_token[0] == '{' ) tabDepth++;
return r;
}
2008-09-10 22:00:00 +02:00
void Com_CheckToken( const char *match )
{
Com_GetToken( true );
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
if(!Com_MatchToken( match ))
{
2008-09-28 22:00:00 +02:00
Sys_Break( "Com_CheckToken: \"%s\" found, when excpecting '%s'\n", com_token, match );
2008-09-10 22:00:00 +02:00
}
}
void Com_Parse1DMatrix( int x, vec_t *m )
{
int i;
Com_CheckToken( "(" );
for( i = 0; i < x; i++ )
{
Com_GetToken( false );
m[i] = com.atof( com_token );
}
Com_CheckToken( ")" );
}
2008-09-28 22:00:00 +02:00
void Com_Parse1DMatrixAppend( char *buffer, int x, vec_t *m )
{
int i;
if( !Com_GetTokenAppend( buffer, true ) || !Com_MatchToken( "(" ))
Sys_Break( "Com_Parse1DMatrixAppend: line %d: ( not found!\n", scriptline );
for( i = 0; i < x; i++ )
{
if(!Com_GetTokenAppend( buffer, false ))
Sys_Break( "Com_Parse1DMatrixAppend: line %d: Number not found!\n", scriptline );
m[i] = com.atof( com_token );
}
if( !Com_GetTokenAppend( buffer, true ) || !Com_MatchToken( "(" ))
Sys_Break( "Com_Parse1DMatrixAppend: line %d: ) not found!", scriptline );
}
2008-09-10 22:00:00 +02:00
void Com_Parse2DMatrix( int y, int x, vec_t *m )
{
int i;
Com_CheckToken( "(" );
for( i = 0; i < y; i++ )
{
Com_Parse1DMatrix( x, m+i*x );
}
Com_CheckToken( ")" );
}
void Com_Parse3DMatrix( int z, int y, int x, vec_t *m )
{
int i;
Com_CheckToken( "(" );
for( i = 0; i < z; i++ )
{
Com_Parse2DMatrix( y, x, m+i*x*y );
}
Com_CheckToken( ")" );
}