]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/bspfile_abstract.c
Merge commit '146d231d90b19e2a0edcabff1c4eb9ca44e053e7' into master-merge
[xonotic/netradiant.git] / tools / quake3 / q3map2 / bspfile_abstract.c
index 1dab66dbda76c4cf7a83b62b9d3816ab0d0e431d..55d358f0e69b65c9d5aa859bcb5036f12beb3d98 100644 (file)
@@ -68,14 +68,19 @@ void IncDrawVerts(){
 
        }
        else if ( numBSPDrawVerts > numBSPDrawVertsBuffer ) {
+               bspDrawVert_t *newBspDrawVerts;
+
                numBSPDrawVertsBuffer *= 3; // multiply by 1.5
                numBSPDrawVertsBuffer /= 2;
 
-               bspDrawVerts = realloc( bspDrawVerts, sizeof( bspDrawVert_t ) * numBSPDrawVertsBuffer );
+               newBspDrawVerts = realloc( bspDrawVerts, sizeof( bspDrawVert_t ) * numBSPDrawVertsBuffer );
 
-               if ( !bspDrawVerts ) {
+               if ( !newBspDrawVerts ) {
+                       free (bspDrawVerts);
                        Error( "realloc() failed (IncDrawVerts)" );
                }
+
+               bspDrawVerts = newBspDrawVerts;
        }
 
        memset( bspDrawVerts + ( numBSPDrawVerts - 1 ), 0, sizeof( bspDrawVert_t ) );
@@ -89,9 +94,7 @@ void SetDrawVerts( int n ){
        numBSPDrawVerts = n;
        numBSPDrawVertsBuffer = numBSPDrawVerts;
 
-       bspDrawVerts = safe_malloc_info( sizeof( bspDrawVert_t ) * numBSPDrawVertsBuffer, "IncDrawVerts" );
-
-       memset( bspDrawVerts, 0, n * sizeof( bspDrawVert_t ) );
+       bspDrawVerts = safe_malloc0_info( sizeof( bspDrawVert_t ) * numBSPDrawVertsBuffer, "IncDrawVerts" );
 }
 
 int numBSPDrawSurfacesBuffer = 0;
@@ -102,9 +105,7 @@ void SetDrawSurfacesBuffer(){
 
        numBSPDrawSurfacesBuffer = MAX_MAP_DRAW_SURFS;
 
-       bspDrawSurfaces = safe_malloc_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
-
-       memset( bspDrawSurfaces, 0, MAX_MAP_DRAW_SURFS * sizeof( bspDrawVert_t ) );
+       bspDrawSurfaces = safe_malloc0_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
 }
 
 void SetDrawSurfaces( int n ){
@@ -115,9 +116,7 @@ void SetDrawSurfaces( int n ){
        numBSPDrawSurfaces = n;
        numBSPDrawSurfacesBuffer = numBSPDrawSurfaces;
 
-       bspDrawSurfaces = safe_malloc_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
-
-       memset( bspDrawSurfaces, 0, n * sizeof( bspDrawVert_t ) );
+       bspDrawSurfaces = safe_malloc0_info( sizeof( bspDrawSurface_t ) * numBSPDrawSurfacesBuffer, "IncDrawSurfaces" );
 }
 
 void BSPFilesCleanup(){
@@ -169,7 +168,7 @@ void SwapBlock( int *block, int size ){
 
 void SwapBSPFile( void ){
        int i, j;
-
+       shaderInfo_t    *si;
 
        /* models */
        SwapBlock( (int*) bspModels, numBSPModels * sizeof( bspModels[ 0 ] ) );
@@ -177,6 +176,12 @@ void SwapBSPFile( void ){
        /* shaders (don't swap the name) */
        for ( i = 0; i < numBSPShaders ; i++ )
        {
+       if ( doingBSP ){
+               si = ShaderInfoForShader( bspShaders[ i ].shader );
+               if ( si->remapShader && si->remapShader[ 0 ] ) {
+                       strcpy( bspShaders[ i ].shader, si->remapShader );
+               }
+       }
                bspShaders[ i ].contentFlags = LittleLong( bspShaders[ i ].contentFlags );
                bspShaders[ i ].surfaceFlags = LittleLong( bspShaders[ i ].surfaceFlags );
        }
@@ -257,8 +262,6 @@ void SwapBSPFile( void ){
        }
 }
 
-
-
 /*
    GetLumpElements()
    gets the number of elements in a bsp lump
@@ -268,7 +271,7 @@ int GetLumpElements( bspHeader_t *header, int lump, int size ){
        /* check for odd size */
        if ( header->lumps[ lump ].length % size ) {
                if ( force ) {
-                       Sys_Printf( "WARNING: GetLumpElements: odd lump size (%d) in lump %d\n", header->lumps[ lump ].length, lump );
+                       Sys_FPrintf( SYS_WRN, "WARNING: GetLumpElements: odd lump size (%d) in lump %d\n", header->lumps[ lump ].length, lump );
                        return 0;
                }
                else{
@@ -312,7 +315,7 @@ int CopyLump( bspHeader_t *header, int lump, void *dest, int size ){
        }
        if ( length % size ) {
                if ( force ) {
-                       Sys_Printf( "WARNING: CopyLump: odd lump size (%d) in lump %d\n", length, lump );
+                       Sys_FPrintf( SYS_WRN, "WARNING: CopyLump: odd lump size (%d) in lump %d\n", length, lump );
                        return 0;
                }
                else{
@@ -341,14 +344,16 @@ int CopyLump_Allocate( bspHeader_t *header, int lump, void **dest, int size, int
 void AddLump( FILE *file, bspHeader_t *header, int lumpNum, const void *data, int length ){
        bspLump_t   *lump;
 
-
        /* add lump to bsp file header */
        lump = &header->lumps[ lumpNum ];
        lump->offset = LittleLong( ftell( file ) );
        lump->length = LittleLong( length );
 
        /* write lump to file */
-       SafeWrite( file, data, ( length + 3 ) & ~3 );
+       SafeWrite( file, data, length );
+
+       /* write padding zeros */
+       SafeWrite( file, (const byte[3]){ 0, 0, 0 }, ( ( length + 3 ) & ~3 ) - length );
 }
 
 
@@ -369,7 +374,37 @@ void LoadBSPFile( const char *filename ){
        SwapBSPFile();
 }
 
+/*
+   PartialLoadBSPFile()
+   partially loads a bsp file into memory
+   for autopacker
+ */
+
+void PartialLoadBSPFile( const char *filename ){
+       /* dummy check */
+       if ( game == NULL || game->load == NULL ) {
+               Error( "LoadBSPFile: unsupported BSP file format" );
+       }
+
+       /* load it, then byte swap the in-memory version */
+       //game->load( filename );
+       PartialLoadIBSPFile( filename );
+
+       /* PartialSwapBSPFile() */
+       int i, j;
+       shaderInfo_t    *si;
+
+       /* shaders (don't swap the name) */
+       for ( i = 0; i < numBSPShaders ; i++ )
+       {
+               bspShaders[ i ].contentFlags = LittleLong( bspShaders[ i ].contentFlags );
+               bspShaders[ i ].surfaceFlags = LittleLong( bspShaders[ i ].surfaceFlags );
+       }
 
+       /* drawsurfs */
+       /* note: rbsp files (and hence q3map2 abstract bsp) have byte lightstyles index arrays, this follows sof2map convention */
+       SwapBlock( (int*) bspDrawSurfaces, numBSPDrawSurfaces * sizeof( bspDrawSurfaces[ 0 ] ) );
+}
 
 /*
    WriteBSPFile()
@@ -497,8 +532,7 @@ epair_t *ParseEPair( void ){
 
 
        /* allocate and clear new epair */
-       e = safe_malloc( sizeof( epair_t ) );
-       memset( e, 0, sizeof( epair_t ) );
+       e = safe_malloc0( sizeof( epair_t ) );
 
        /* handle key */
        if ( strlen( token ) >= ( MAX_KEY - 1 ) ) {
@@ -581,6 +615,8 @@ void ParseEntities( void ){
        numBSPEntities = numEntities;
 }
 
+
+
 /*
  * must be called before UnparseEntities
  */
@@ -592,6 +628,10 @@ void InjectCommandLine( char **argv, int beginArgs, int endArgs ){
        char *sentinel = newCommandLine + sizeof( newCommandLine ) - 1;
        int i;
 
+if (nocmdline)
+{
+       return;
+}
        previousCommandLine = ValueForKey( &entities[0], "_q3map2_cmdline" );
        if ( previousCommandLine && *previousCommandLine ) {
                inpos = previousCommandLine;
@@ -607,6 +647,9 @@ void InjectCommandLine( char **argv, int beginArgs, int endArgs ){
 
        for ( i = beginArgs; i < endArgs; ++i )
        {
+               if ( argv[i] == NULL ) {
+                       continue;
+               }
                if ( outpos != sentinel && i != beginArgs ) {
                        *outpos++ = ' ';
                }
@@ -622,6 +665,8 @@ void InjectCommandLine( char **argv, int beginArgs, int endArgs ){
        SetKeyValue( &entities[0], "_q3map2_version", Q3MAP_VERSION );
 }
 
+
+
 /*
    UnparseEntities()
    generates the dentdata string from all the entities.
@@ -748,6 +793,8 @@ void SetKeyValue( entity_t *ent, const char *key, const char *value ){
        ep->value = copystring( value );
 }
 
+
+
 /*
    KeyExists()
    returns true if entity has this key
@@ -768,6 +815,8 @@ qboolean KeyExists( const entity_t *ent, const char *key ){
        return qfalse;
 }
 
+
+
 /*
    ValueForKey()
    gets the value for an entity key
@@ -831,7 +880,7 @@ vec_t FloatForKey( const entity_t *ent, const char *key ){
    gets a 3-element vector value for an entity key
  */
 
-void GetVectorForKey( const entity_t *ent, const char *key, vec3_t vec ){
+qboolean GetVectorForKey( const entity_t *ent, const char *key, vec3_t vec ){
        const char  *k;
        double v1, v2, v3;
 
@@ -845,6 +894,9 @@ void GetVectorForKey( const entity_t *ent, const char *key, vec3_t vec ){
        vec[ 0 ] = v1;
        vec[ 1 ] = v2;
        vec[ 2 ] = v3;
+
+       /* true if the key is found, false otherwise */
+       return strlen( k );
 }
 
 
@@ -917,7 +969,7 @@ void GetEntityShadowFlags( const entity_t *ent, const entity_t *ent2, int *castS
                }
        }
 
-       /* vortex: game-specific default eneity keys */
+       /* vortex: game-specific default entity keys */
        value = ValueForKey( ent, "classname" );
        if ( !Q_stricmp( game->magic, "dq" ) || !Q_stricmp( game->magic, "prophecy" ) ) {
                /* vortex: deluxe quake default shadow flags */