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

631 lines
16 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
2008-11-10 22:00:00 +01:00
#include <stdio.h> // sscanf support
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-11-10 22:00:00 +01:00
//=============================================================================
2008-08-16 22:00:00 +02:00
wfile_t *handle;
2008-09-10 22:00:00 +02:00
file_t *wadfile;
dheader_t *header;
int num_entities;
bsp_entity_t entities[MAX_MAP_ENTITIES];
2008-11-10 22:00:00 +01:00
int nummodels;
dmodel_t dmodels[MAX_MAP_MODELS];
int visdatasize;
byte dvisdata[MAX_MAP_VISIBILITY];
dvis_t *dvis = (dvis_t *)dvisdata;
int lightdatasize;
byte dlightdata[MAX_MAP_LIGHTING];
2008-09-07 22:00:00 +02:00
int entdatasize;
2008-11-10 22:00:00 +01:00
char dentdata[MAX_MAP_ENTSTRING];
int numleafs;
dleaf_t dleafs[MAX_MAP_LEAFS];
2008-09-07 22:00:00 +02:00
int numplanes;
2008-11-10 22:00:00 +01:00
dplane_t dplanes[MAX_MAP_PLANES];
int numvertexes;
dvertex_t dvertexes[MAX_MAP_VERTS];
2008-09-07 22:00:00 +02:00
int numnodes;
2008-11-10 22:00:00 +01:00
dnode_t dnodes[MAX_MAP_NODES];
int numtexinfo;
dtexinfo_t texinfo[MAX_MAP_TEXINFO];
int numsurfaces;
dsurface_t dsurfaces[MAX_MAP_SURFACES];
int numedges;
dedge_t dedges[MAX_MAP_EDGES];
int numleafsurfaces;
dleafface_t dleafsurfaces[MAX_MAP_LEAFFACES];
2008-09-07 22:00:00 +02:00
int numleafbrushes;
2008-11-10 22:00:00 +01:00
dleafbrush_t dleafbrushes[MAX_MAP_LEAFBRUSHES];
int numsurfedges;
dsurfedge_t dsurfedges[MAX_MAP_SURFEDGES];
2008-09-07 22:00:00 +02:00
int numbrushes;
2008-11-10 22:00:00 +01:00
dbrush_t dbrushes[MAX_MAP_BRUSHES];
2008-09-07 22:00:00 +02:00
int numbrushsides;
2008-11-10 22:00:00 +01:00
dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
int numshaders;
dshader_t dshaders[MAX_MAP_SHADERS];
int numareas;
darea_t dareas[MAX_MAP_AREAS];
int numareaportals;
dareaportal_t dareaportals[MAX_MAP_AREAPORTALS];
2008-11-06 22:00:00 +01:00
byte dcollision[MAX_MAP_COLLISION];
int dcollisiondatasize;
2007-06-21 22:00:00 +02:00
2008-09-10 22:00:00 +02:00
/*
===============
CompressVis
2008-11-10 22:00:00 +01:00
2008-09-10 22:00:00 +02:00
===============
*/
int CompressVis( byte *vis, byte *dest )
{
2008-11-10 22:00:00 +01:00
int j;
int rep;
int visrow;
2008-09-10 22:00:00 +02:00
byte *dest_p;
dest_p = dest;
visrow = (dvis->numclusters + 7)>>3;
2008-11-10 22:00:00 +01:00
for (j=0 ; j<visrow ; j++)
2008-09-10 22:00:00 +02:00
{
*dest_p++ = vis[j];
2008-11-10 22:00:00 +01:00
if (vis[j])
continue;
2008-09-10 22:00:00 +02:00
rep = 1;
2008-11-10 22:00:00 +01:00
for ( j++; j<visrow ; j++)
if (vis[j] || rep == 255)
2008-09-10 22:00:00 +02:00
break;
else rep++;
*dest_p++ = rep;
j--;
}
2008-11-10 22:00:00 +01:00
2008-09-10 22:00:00 +02:00
return dest_p - dest;
}
2008-11-10 22:00:00 +01:00
2008-09-10 22:00:00 +02:00
/*
===================
DecompressVis
===================
*/
void DecompressVis( byte *in, byte *decompressed )
{
int c;
byte *out;
int row;
row = (dvis->numclusters+7)>>3;
out = decompressed;
do
{
2008-11-10 22:00:00 +01:00
if (*in)
2008-09-10 22:00:00 +02:00
{
*out++ = *in++;
continue;
}
c = in[1];
2008-11-10 22:00:00 +01:00
if (!c) Sys_Error("DecompressVis: 0 repeat");
2008-09-10 22:00:00 +02:00
in += 2;
2008-11-10 22:00:00 +01:00
while (c)
2008-09-10 22:00:00 +02:00
{
*out++ = 0;
c--;
}
2008-11-10 22:00:00 +01:00
} while (out - decompressed < row);
2008-09-10 22:00:00 +02:00
}
2008-11-10 22:00:00 +01:00
//=============================================================================
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;
2008-11-10 22:00:00 +01:00
// models
SwapBlock((int *)dmodels, nummodels * sizeof( dmodels[0] ));
2007-06-21 22:00:00 +02:00
2008-11-10 22:00:00 +01:00
// vertexes
SwapBlock( (int *)dvertexes, numvertexes * sizeof( dvertexes[0] ));
2008-08-12 22:00:00 +02:00
2007-06-21 22:00:00 +02:00
// planes
2008-11-10 22:00:00 +01:00
SwapBlock( (int *)dplanes, numplanes * sizeof( dplanes[0] ));
// texinfos
SwapBlock( (int *)texinfo, numtexinfo * sizeof( texinfo[0] ));
2007-06-21 22:00:00 +02:00
// nodes
2008-11-10 22:00:00 +01:00
SwapBlock( (int *)dnodes, numnodes * sizeof( dnodes[0] ));
2007-06-21 22:00:00 +02:00
// leafs
2008-11-10 22:00:00 +01:00
SwapBlock( (int *)dleafs, numleafs * sizeof( dleafs[0] ));
2007-06-21 22:00:00 +02:00
// leaffaces
2008-11-10 22:00:00 +01:00
SwapBlock( (int *)dleafsurfaces, numleafsurfaces * sizeof( dleafsurfaces[0] ));
2007-06-21 22:00:00 +02:00
// leafbrushes
2008-11-10 22:00:00 +01:00
SwapBlock( (int *)dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ));
2007-06-21 22:00:00 +02:00
2008-11-10 22:00:00 +01:00
// surfedges
SwapBlock( (int *)dsurfedges, numsurfedges * sizeof( dsurfedges[0] ));
// edges
SwapBlock( (int *)dedges, numedges * sizeof( dedges[0] ));
2007-06-21 22:00:00 +02:00
// brushes
2008-11-10 22:00:00 +01:00
SwapBlock( (int *)dbrushes, numbrushes * sizeof( dbrushes[0] ));
2007-06-21 22:00:00 +02:00
2008-11-10 22:00:00 +01:00
// areas
SwapBlock( (int *)dareas, numareas * sizeof( dareas[0] ));
2008-08-12 22:00:00 +02:00
2008-11-10 22:00:00 +01:00
// areasportals
SwapBlock( (int *)dareaportals, numareaportals * sizeof( dareaportals[0] ));
2008-09-07 22:00:00 +02:00
2008-11-10 22:00:00 +01:00
// brushsides
SwapBlock( (int *)dbrushsides, numbrushsides * sizeof( dbrushsides[0] ));
// shaders
for( i = 0; i < numshaders; i++ )
{
dshaders[i].size[0] = LittleLong( dshaders[i].size[0] );
dshaders[i].size[1] = LittleLong( dshaders[i].size[1] );
dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
}
2008-08-18 22:00:00 +02:00
2008-11-10 22:00:00 +01:00
// faces
for( i = 0; i < numsurfaces; i++ )
{
dsurfaces[i].planenum = LittleLong( dsurfaces[i].planenum );
dsurfaces[i].side = LittleLong (dsurfaces[i].side);
dsurfaces[i].firstedge = LittleLong( dsurfaces[i].firstedge );
dsurfaces[i].numedges = LittleLong( dsurfaces[i].numedges );
dsurfaces[i].texinfo = LittleLong( dsurfaces[i].texinfo );
dsurfaces[i].lightofs = LittleLong( dsurfaces[i].lightofs );
}
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 );
2008-11-10 22:00:00 +01:00
Mem_Free( in ); // no need more
2008-08-16 22:00:00 +02:00
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-11-10 22:00:00 +01:00
if( length > 0xffff ) compress = CMP_ZLIB; // save hdd space
2008-09-10 22:00:00 +02:00
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-11-06 22:00:00 +01: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-11-06 22:00:00 +01:00
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 );
2008-11-10 22:00:00 +01:00
entdatasize = CopyLump( LUMP_ENTITIES, dentdata, 1 );
numplanes = CopyLump( LUMP_PLANES, dplanes, sizeof( dplanes[0] ));
numvertexes = CopyLump( LUMP_VERTEXES, dvertexes, sizeof( dvertexes[0] ));
visdatasize = CopyLump( LUMP_VISIBILITY, dvisdata, 1 );
numnodes = CopyLump( LUMP_NODES, dnodes, sizeof( dnodes[0] ));
numtexinfo = CopyLump( LUMP_TEXINFO, texinfo, sizeof( texinfo[0] ));
numsurfaces = CopyLump( LUMP_SURFACES, dsurfaces, sizeof( dsurfaces[0] ));
lightdatasize = CopyLump( LUMP_LIGHTING, dlightdata, 1 );
numleafs = CopyLump( LUMP_LEAFS, dleafs, sizeof( dleafs[0] ));
numleafsurfaces = CopyLump( LUMP_LEAFFACES, dleafsurfaces, sizeof( dleafsurfaces[0] ));
numleafbrushes = CopyLump( LUMP_LEAFBRUSHES, dleafbrushes, sizeof( dleafbrushes[0] ));
numedges = CopyLump( LUMP_EDGES, dedges, sizeof( dedges[0] ));
numsurfedges = CopyLump( LUMP_SURFEDGES, dsurfedges, sizeof( dsurfedges[0] ));
nummodels = CopyLump( LUMP_MODELS, dmodels, sizeof( dmodels[0] ));
numbrushes = CopyLump( LUMP_BRUSHES, dbrushes, sizeof( dbrushes[0] ));
numbrushsides = CopyLump( LUMP_BRUSHSIDES, dbrushsides, sizeof( dbrushsides[0] ));
dcollisiondatasize = CopyLump( LUMP_COLLISION, dcollision, 1 );
numshaders = CopyLump( LUMP_SHADERS, dshaders, sizeof( dshaders[0] ));
numareas = CopyLump ( LUMP_AREAS, dareas, sizeof( dareas[0] ));
numareaportals = CopyLump( LUMP_AREAPORTALS, dareaportals, sizeof( dareaportals[0] ));
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 )
{
2008-11-10 22:00:00 +01:00
static dheader_t outheader;
2007-06-21 22:00:00 +02:00
header = &outheader;
2008-11-10 22:00:00 +01:00
Mem_Set( 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-11-06 22:00:00 +01:00
MsgDev( D_NOTE, "\n\nwriting %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
2008-11-10 22:00:00 +01:00
AddLump( LUMP_ENTITIES, dentdata, entdatasize );
AddLump( LUMP_PLANES, dplanes, numplanes * sizeof( dplanes[0] ));
AddLump( LUMP_VERTEXES, dvertexes, numvertexes * sizeof( dvertexes[0] ));
AddLump( LUMP_VISIBILITY, dvisdata, visdatasize );
AddLump( LUMP_NODES, dnodes, numnodes * sizeof( dnodes[0] ));
AddLump( LUMP_TEXINFO, texinfo, numtexinfo * sizeof( texinfo[0] ));
AddLump( LUMP_SURFACES, dsurfaces, numsurfaces * sizeof( dsurfaces[0] ));
AddLump( LUMP_LIGHTING, dlightdata, lightdatasize );
AddLump( LUMP_LEAFS, dleafs, numleafs * sizeof( dleafs[0] ));
AddLump( LUMP_LEAFFACES, dleafsurfaces, numleafsurfaces * sizeof( dleafsurfaces[0] ));
AddLump( LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ));
AddLump( LUMP_EDGES, dedges, numedges * sizeof( dedges[0] ));
AddLump( LUMP_SURFEDGES, dsurfedges, numsurfedges * sizeof( dsurfedges[0] ));
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_COLLISION, dcollision, dcollisiondatasize );
AddLump( LUMP_SHADERS, dshaders, numshaders * sizeof( dshaders[0] ));
AddLump( LUMP_AREAS, dareas, numareas * sizeof( dareas[0] ));
AddLump( LUMP_AREAPORTALS, dareaportals, numareaportals * sizeof( dareaportals[0] ));
// merge header
2007-06-21 22:00:00 +02:00
FS_Seek( wadfile, 0, SEEK_SET );
2008-11-10 22:00:00 +01:00
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-11-01 22:00:00 +01:00
epair_t *ParseEpair( token_t *token )
2007-06-21 22:00:00 +02:00
{
epair_t *e;
2008-11-10 22:00:00 +01:00
e = Malloc( sizeof( epair_t ));
2007-06-21 22:00:00 +02:00
2008-11-01 22:00:00 +01:00
if( com.strlen( token->string ) >= MAX_KEY - 1 )
2008-09-10 22:00:00 +02:00
Sys_Break( "ParseEpair: token too long\n" );
2008-11-01 22:00:00 +01:00
e->key = copystring( token->string );
2008-11-06 22:00:00 +01:00
Com_ReadToken( mapfile, SC_PARSE_GENERIC, token );
2008-11-01 22:00:00 +01:00
if( com.strlen( token->string ) >= MAX_VALUE - 1 )
2008-09-10 22:00:00 +02:00
Sys_Break( "ParseEpair: token too long\n" );
2008-11-01 22:00:00 +01:00
e->value = copystring( token->string );
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;
2008-11-01 22:00:00 +01:00
token_t token;
2007-07-28 22:00:00 +02:00
bsp_entity_t *mapent;
2007-06-21 22:00:00 +02:00
2008-11-01 22:00:00 +01:00
if( !Com_ReadToken( mapfile, SC_ALLOW_NEWLINES, &token ))
2008-09-10 22:00:00 +02:00
return false;
2007-06-21 22:00:00 +02:00
2008-11-01 22:00:00 +01:00
if( com.stricmp( token.string, "{" )) 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-11-06 22:00:00 +01:00
if( !Com_ReadToken( mapfile, SC_ALLOW_NEWLINES|SC_PARSE_GENERIC, &token ))
2008-09-10 22:00:00 +02:00
Sys_Break( "ParseEntity: EOF without closing brace\n" );
2008-11-01 22:00:00 +01:00
if( !com.stricmp( token.string, "}" )) break;
e = ParseEpair( &token );
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-11-01 22:00:00 +01:00
mapfile = Com_OpenScript( "entities", dentdata, entdatasize );
if( mapfile ) while( ParseEntity( ));
Com_CloseScript( mapfile );
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-11-06 22:00:00 +01: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-11-06 22:00:00 +01: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-11-10 22:00:00 +01:00
Mem_Free( ep->value );
2008-09-10 22:00:00 +02:00
ep->value = copystring( value );
2007-06-21 22:00:00 +02:00
return;
}
2008-09-10 22:00:00 +02:00
}
2008-11-10 22:00:00 +01:00
ep = 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-11-06 22:00:00 +01:00
char *ValueForKey( const bsp_entity_t *ent, const char *key )
2007-06-21 22:00:00 +02:00
{
epair_t *ep;
2008-11-06 22:00:00 +01: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-11-06 22:00:00 +01: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-11-06 22:00:00 +01: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-11-01 22:00:00 +01: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-11-06 22:00:00 +01: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;
}
void Com_CheckToken( script_t *script, const char *match )
2008-09-10 22:00:00 +02:00
{
2008-11-01 22:00:00 +01:00
token_t token;
2008-11-06 22:00:00 +01:00
Com_ReadToken( script, SC_ALLOW_NEWLINES, &token );
2007-06-21 22:00:00 +02:00
2008-11-01 22:00:00 +01:00
if( com.stricmp( token.string, match ))
2008-09-10 22:00:00 +02:00
{
2008-10-19 22:00:00 +02:00
Sys_Break( "Com_CheckToken: \"%s\" not found\n", match );
2008-09-10 22:00:00 +02:00
}
}
2008-11-06 22:00:00 +01:00
void Com_Parse1DMatrix( script_t *script, int x, vec_t *m )
2008-09-10 22:00:00 +02:00
{
int i;
2008-11-06 22:00:00 +01:00
Com_CheckToken( script, "(" );
2008-09-10 22:00:00 +02:00
for( i = 0; i < x; i++ )
2008-11-06 22:00:00 +01:00
Com_ReadFloat( script, false, &m[i] );
Com_CheckToken( script, ")" );
2008-09-10 22:00:00 +02:00
}
2008-11-06 22:00:00 +01:00
void Com_Parse2DMatrix( script_t *script, int y, int x, vec_t *m )
2008-09-10 22:00:00 +02:00
{
int i;
2008-11-06 22:00:00 +01:00
Com_CheckToken( script, "(" );
2008-09-10 22:00:00 +02:00
for( i = 0; i < y; i++ )
2008-11-06 22:00:00 +01:00
Com_Parse1DMatrix( script, x, m+i*x );
Com_CheckToken( script, ")" );
2008-09-10 22:00:00 +02:00
}