Fix: death in autoresolve should have always resulted in corpses, but this only rarely happened

git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@8592 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
Flugente
2018-08-14 23:11:58 +00:00
parent a59b75b0be
commit ce80ab68e6
+31 -46
View File
@@ -2588,26 +2588,22 @@ BOOLEAN AddRottingCorpseToUnloadedSectorsRottingCorpseFile( INT16 sMapX, INT16 s
{ {
HWFILE hFile; HWFILE hFile;
UINT32 uiNumberOfCorpses; UINT32 uiNumberOfCorpses;
// CHAR8 zTempName[ 128 ]; CHAR8 zMapName[ 128 ];
CHAR8 zMapName[ 128 ];
UINT32 uiNumBytesRead; UINT32 uiNumBytesRead;
UINT32 uiNumBytesWritten;
/* ROTTING_CORPSE_DEFINITION def;
//Convert the current sector location into a file name std::vector<ROTTING_CORPSE_DEFINITION> corpsedefvector;
GetMapFileName( sMapX,sMapY, bMapZ, zTempName, FALSE );
// Flugente 2018-08-15: the old method of reading and writing in the same file at the same time is faulty.
// We instead open the existing file if it exists, take it's contents, and write them with the new corpse to a new file
//add the 'r' for 'Rotting Corpses' to the front of the map name
sprintf( zMapName, "%s\\r_%s", MAPS_DIR, zTempName);
*/
GetMapTempFileName( SF_ROTTING_CORPSE_TEMP_FILE_EXISTS, zMapName, sMapX, sMapY, bMapZ ); GetMapTempFileName( SF_ROTTING_CORPSE_TEMP_FILE_EXISTS, zMapName, sMapX, sMapY, bMapZ );
//CHECK TO SEE if the file exist if ( DoesTempFileExistsForMap( SF_ROTTING_CORPSE_TEMP_FILE_EXISTS, sMapX, sMapY, bMapZ ) && FileExists( zMapName ) )
if( FileExists( zMapName ) )
{ {
//Open the file for reading //Open the file for reading
hFile = FileOpen( zMapName, FILE_ACCESS_READWRITE | FILE_OPEN_EXISTING, FALSE ); hFile = FileOpen( zMapName, FILE_ACCESS_READ | FILE_OPEN_EXISTING, FALSE );
if( hFile == 0 ) if ( hFile == 0 )
{ {
//Error opening map modification file, //Error opening map modification file,
return( FALSE ); return( FALSE );
@@ -2615,55 +2611,44 @@ BOOLEAN AddRottingCorpseToUnloadedSectorsRottingCorpseFile( INT16 sMapX, INT16 s
// Load the number of Rotting corpses // Load the number of Rotting corpses
FileRead( hFile, &uiNumberOfCorpses, sizeof( UINT32 ), &uiNumBytesRead ); FileRead( hFile, &uiNumberOfCorpses, sizeof( UINT32 ), &uiNumBytesRead );
if( uiNumBytesRead != sizeof( UINT32 ) ) if ( uiNumBytesRead != sizeof( UINT32 ) )
{ {
//Error Writing size of array to disk //Error Writing size of array to disk
FileClose( hFile ); FileClose( hFile );
return( FALSE ); return( FALSE );
} }
}
else // load existing corpses
{ for ( UINT32 cnt = 0; cnt < uiNumberOfCorpses; ++cnt )
//the file doesnt exists, create a new one
hFile = FileOpen( zMapName, FILE_ACCESS_WRITE | FILE_OPEN_ALWAYS, FALSE );
if( hFile == 0 )
{ {
//Error opening map modification file // Load the Rotting corpses info
return( FALSE ); FileRead( hFile, &def, sizeof( ROTTING_CORPSE_DEFINITION ), &uiNumBytesRead );
if ( uiNumBytesRead != sizeof( ROTTING_CORPSE_DEFINITION ) )
{
//Error Writing size of array to disk
continue;
}
corpsedefvector.push_back( def );
} }
uiNumberOfCorpses = 0;
}
//Start at the begining of the file
FileSeek( hFile, 0, FILE_SEEK_FROM_START );
//Add on to the number and save it back to disk
uiNumberOfCorpses++;
FileWrite( hFile, &uiNumberOfCorpses, sizeof( UINT32 ), &uiNumBytesWritten );
if( uiNumBytesWritten != sizeof( UINT32 ) )
{
//Error Writing size of array to disk
FileClose( hFile ); FileClose( hFile );
return( FALSE );
} }
corpsedefvector.push_back( *pRottingCorpseDef );
//Go to the end of the file // create the file anew
FileSeek( hFile, 0, FILE_SEEK_FROM_END ); SaveRottingCorpsesToTempCorpseFile( sMapX, sMapY, bMapZ, corpsedefvector );
//Append the new rotting corpse def to the end of the file // Flugente: update number of corpses in this sector
FileWrite( hFile, pRottingCorpseDef, sizeof( ROTTING_CORPSE_DEFINITION ), &uiNumBytesWritten ); if ( !bMapZ )
if( uiNumBytesWritten != sizeof( ROTTING_CORPSE_DEFINITION ) )
{ {
//Error Writing size of array to disk SECTORINFO *pSectorInfo = &( SectorInfo[SECTOR( sMapX, sMapY )] );
FileClose( hFile );
return( FALSE ); if ( pSectorInfo )
pSectorInfo->usNumCorpses = corpsedefvector.size();
} }
FileClose( hFile );
SetSectorFlag( sMapX, sMapY, bMapZ, SF_ROTTING_CORPSE_TEMP_FILE_EXISTS );
return( TRUE ); return( TRUE );
} }