1
0

Old code begone! ChunkMap cleanup

git-svn-id: http://mc-server.googlecode.com/svn/trunk@189 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2012-01-29 21:40:21 +00:00
parent 89afb970d8
commit 87a7c39dfc
4 changed files with 272 additions and 393 deletions

View File

@ -9,8 +9,6 @@
# define __C_CHUNK_INLINE__
#endif
#include <list>
namespace Json
{
class Value;

View File

@ -3,7 +3,6 @@
#include "cChunkMap.h"
#include "cChunk.h"
#include "cMCLogger.h"
#include "cWorld.h"
#include "cRoot.h"
#include "cMakeDir.h"
@ -25,105 +24,24 @@
cChunkMap::cChunkMap( int a_Width, int a_Height, cWorld* a_World )
: m_Nodes( new cChunkNode[ a_Width * a_Height ] )
, m_Width( a_Width )
, m_Height( a_Height )
, m_Layers( 0 )
cChunkMap::cChunkMap(cWorld* a_World )
: m_Layers( 0 )
, m_NumLayers( 0 )
, m_World( a_World )
{
}
cChunkMap::~cChunkMap()
{
delete [] m_Nodes;
}
cChunkMap::cChunkNode::cChunkNode()
{
m_Size = 0;
m_Allocated = 0;
m_Chunks = 0;
}
cChunkMap::cChunkNode::~cChunkNode()
{
if( m_Allocated > 0 )
{
for( unsigned int i = 0; i < m_Size; ++i )
{
delete m_Chunks[i];
}
delete [] m_Chunks;
}
// m_Chunks = 0;
// m_Allocated = 0;
// m_Size = 0;
}
void cChunkMap::cChunkNode::push_back( cChunk* a_Chunk )
{
if( m_Allocated == 0 )
{
resize( 1 );
}
if( m_Size >= m_Allocated )
{
resize( m_Allocated*2 );
}
m_Chunks[ m_Size ] = a_Chunk;
m_Size++;
}
void cChunkMap::cChunkNode::resize( unsigned int a_NewSize )
{
cChunk** TempChunks = new cChunk*[a_NewSize];
if( m_Allocated > 0 )
{
#ifdef USE_MEMCPY
memcpy( TempChunks, m_Chunks, sizeof( cChunk* ) * m_Size );
#else
for( unsigned int i = 0; i < a_NewSize; ++i )
TempChunks[i] = m_Chunks[i];
#endif
delete [] m_Chunks;
}
m_Chunks = TempChunks;
m_Allocated = a_NewSize;
}
void cChunkMap::cChunkNode::erase( cChunk* a_Chunk )
{
if( m_Size == 0 ) return;
cChunk** TempChunks = new cChunk*[m_Size];
unsigned int TempIdx = 0;
for( unsigned int i = 0; i < m_Size; ++i )
{
if( m_Chunks[i] != a_Chunk )
{
TempChunks[TempIdx] = m_Chunks[i];
TempIdx++;
}
}
delete [] m_Chunks;
m_Chunks = 0;
if( TempIdx > 0 )
{
m_Chunks = new cChunk*[ TempIdx ];
#ifdef USE_MEMCPY
memcpy( m_Chunks, TempChunks, sizeof( cChunk* ) * TempIdx );
#else
for( unsigned int i = 0; i < TempIdx; ++i )
m_Chunks[i] = TempChunks[i];
#endif
}
delete [] TempChunks;
m_Allocated = TempIdx;
m_Size = TempIdx;
}
cChunkMap::cChunkData* cChunkMap::cChunkLayer::GetChunk( int a_X, int a_Z )
{
@ -135,6 +53,10 @@ cChunkMap::cChunkData* cChunkMap::cChunkLayer::GetChunk( int a_X, int a_Z )
return 0;
}
bool cChunkMap::RemoveLayer( cChunkLayer* a_Layer )
{
cChunkLayer* NewLayers = 0;
@ -170,6 +92,10 @@ bool cChunkMap::RemoveLayer( cChunkLayer* a_Layer )
return true;
}
cChunkMap::cChunkLayer* cChunkMap::AddLayer( const cChunkLayer & a_Layer )
{
cChunkLayer* TempLayers = new cChunkLayer[m_NumLayers+1];
@ -187,6 +113,10 @@ cChunkMap::cChunkLayer* cChunkMap::AddLayer( const cChunkLayer & a_Layer )
return NewLayer;
}
void cChunkMap::AddChunk( cChunk* a_Chunk )
{
/* // OLD
@ -217,6 +147,10 @@ void cChunkMap::AddChunk( cChunk* a_Chunk )
FoundLayer->m_NumChunksLoaded++;
}
void cChunkMap::RemoveChunk( cChunk* a_Chunk )
{
/* // OLD
@ -237,6 +171,10 @@ void cChunkMap::RemoveChunk( cChunk* a_Chunk )
}
}
void cChunkMap::CompressChunk( cChunkData* a_ChunkData )
{
if( a_ChunkData->m_LiveChunk )
@ -280,12 +218,9 @@ void cChunkMap::CompressChunk( cChunkData* a_ChunkData )
}
}
unsigned int cChunkMap::MakeHash( int a_X, int a_Z )
{
const unsigned int HashX = abs( a_X ) % m_Width;
const unsigned int HashZ = abs( a_Z ) % m_Height;
return HashX + HashZ * m_Width;
}
cChunkMap::cChunkLayer* cChunkMap::GetLayerForChunk( int a_ChunkX, int a_ChunkZ )
{
@ -294,6 +229,10 @@ cChunkMap::cChunkLayer* cChunkMap::GetLayerForChunk( int a_ChunkX, int a_ChunkZ
return GetLayer( LayerX, LayerZ );
}
cChunkMap::cChunkLayer* cChunkMap::GetLayer( int a_LayerX, int a_LayerZ )
{
// Find layer in memory
@ -314,94 +253,88 @@ cChunkMap::cChunkLayer* cChunkMap::GetLayer( int a_LayerX, int a_LayerZ )
return NewLayer;
}
cChunk* cChunkMap::GetChunk( int a_X, int a_Y, int a_Z )
{
/* // OLD
unsigned int Hash = MakeHash( a_X, a_Z );
cChunkNode & Node = m_Nodes[ Hash ];
cChunk** Chunks = Node.GetChunks();
for( unsigned int i = 0; i < Node.size(); ++i )
{
if( Chunks[i]->GetPosX() == a_X && // Check if we found the right chunk
Chunks[i]->GetPosY() == a_Y &&
Chunks[i]->GetPosZ() == a_Z )
{
return Chunks[i];
}
}
*/
// NEW
cChunkLayer* Layer = GetLayerForChunk( a_X, a_Z );
if( Layer )
if (Layer == NULL)
{
cChunkData* Data = Layer->GetChunk( a_X, a_Z );
if( Data->m_LiveChunk ) return Data->m_LiveChunk;
return NULL;
}
cChunkData* Data = Layer->GetChunk( a_X, a_Z );
if (Data->m_LiveChunk != NULL)
{
// Already loaded and alive
return Data->m_LiveChunk;
}
// Decompress cached chunk
if( Data->m_Compressed )
// Do we at least have the compressed chunk?
if (Data->m_Compressed == NULL)
{
return NULL;
}
// The chunk has been cached (loaded from file, but not decompressed):
uLongf DestSize = Data->m_UncompressedSize;
char* BlockData = new char[ DestSize ];
int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)Data->m_Compressed, Data->m_CompressedSize );
if( Data->m_UncompressedSize != DestSize )
{
LOGWARN("Lulwtf, expected uncompressed size differs!");
delete [] BlockData;
}
else if( errorcode != Z_OK )
{
LOGERROR("ERROR: Decompressing chunk data! %i", errorcode );
switch( errorcode )
{
uLongf DestSize = Data->m_UncompressedSize;
char* BlockData = new char[ DestSize ];
int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)Data->m_Compressed, Data->m_CompressedSize );
if( Data->m_UncompressedSize != DestSize )
{
LOGWARN("Lulwtf, expected uncompressed size differs!");
delete [] BlockData;
}
else if( errorcode != Z_OK )
{
LOGERROR("ERROR: Decompressing chunk data! %i", errorcode );
switch( errorcode )
{
case Z_MEM_ERROR:
LOGERROR("Not enough memory");
break;
case Z_BUF_ERROR:
LOGERROR("Not enough room in output buffer");
break;
case Z_DATA_ERROR:
LOGERROR("Input data corrupted or incomplete");
break;
default:
break;
};
case Z_MEM_ERROR:
LOGERROR("Not enough memory");
break;
case Z_BUF_ERROR:
LOGERROR("Not enough room in output buffer");
break;
case Z_DATA_ERROR:
LOGERROR("Input data corrupted or incomplete");
break;
default:
break;
};
delete [] BlockData;
delete [] BlockData;
}
else
{
cChunk* Chunk = new cChunk(a_X, a_Y, a_Z, m_World);
memcpy( Chunk->m_BlockData, BlockData, cChunk::c_BlockDataSize );
Chunk->CalculateHeightmap();
Data->m_LiveChunk = Chunk;
Layer->m_NumChunksLoaded++;
if( DestSize > cChunk::c_BlockDataSize ) // We gots some extra data :D
{
LOGINFO("Parsing trailing JSON");
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
if( !reader.parse( BlockData + cChunk::c_BlockDataSize, root, false ) )
{
LOGERROR("Failed to parse trailing JSON!");
}
else
{
cChunk* Chunk = new cChunk(a_X, a_Y, a_Z, m_World);
memcpy( Chunk->m_BlockData, BlockData, cChunk::c_BlockDataSize );
Chunk->CalculateHeightmap();
Data->m_LiveChunk = Chunk;
Layer->m_NumChunksLoaded++;
if( DestSize > cChunk::c_BlockDataSize ) // We gots some extra data :D
{
LOGINFO("Parsing trailing JSON");
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
if( !reader.parse( BlockData + cChunk::c_BlockDataSize, root, false ) )
{
LOGERROR("Failed to parse trailing JSON!");
}
else
{
//Json::StyledWriter writer;
//LOGINFO("Trailing Json:" );
//printf("%s", writer.write( root ).c_str() );
Chunk->LoadFromJson( root );
}
}
delete [] BlockData;
delete [] Data->m_Compressed; Data->m_Compressed = 0; Data->m_CompressedSize = 0;
return Chunk;
Chunk->LoadFromJson( root );
}
}
delete [] BlockData;
delete [] Data->m_Compressed; Data->m_Compressed = 0; Data->m_CompressedSize = 0;
return Chunk;
}
return 0;
return NULL;
}
@ -410,56 +343,26 @@ cChunk* cChunkMap::GetChunk( int a_X, int a_Y, int a_Z )
void cChunkMap::Tick( float a_Dt )
{
/* // OLD
for( int i = 0; i < m_Width*m_Height; ++i )
{
cChunkNode & Node = m_Nodes[ i ];
cChunk** Chunks = Node.GetChunks();
for( unsigned int i = 0; i < Node.size(); ++i )
{
Chunks[i]->Tick( a_Dt );
}
}
*/
// NEW
for( int l = 0; l < m_NumLayers; ++l )
for( int lay = 0; lay < m_NumLayers; ++lay )
{
for( int i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i )
{
cChunk* Chunk = m_Layers[l].m_Chunks[i].m_LiveChunk;
if( Chunk )
cChunk* Chunk = m_Layers[lay].m_Chunks[i].m_LiveChunk;
if ( Chunk != NULL)
{
Chunk->Tick( a_Dt );
}
}
}
} // for lay - m_Layers[]
}
void cChunkMap::UnloadUnusedChunks()
{
cWorld* World = m_World;
/* // OLD
for( int i = 0; i < m_Width*m_Height; ++i )
{
cChunkNode & Node = m_Nodes[ i ];
cChunk** Chunks = Node.GetChunks();
for( unsigned int i = 0; i < Node.size(); ++i )
{
if( Chunks[i]->GetClients().size() == 0 )
{
Chunks[i]->SaveToDisk();
LOG("Unloading %p", Chunks[i] );
World->RemoveSpread( Chunks[i] );
cChunk* TheChunk = Chunks[i];
RemoveChunk( TheChunk );
delete TheChunk;
//Node.erase( Chunks[i] );
Chunks = Node.GetChunks(); // Chunks pointer is no longer valid, get a new one
}
}
}
*/
// NEW
for( int l = 0; l < m_NumLayers; ++l )
{
cChunkLayer & Layer = m_Layers[l];
@ -468,7 +371,7 @@ void cChunkMap::UnloadUnusedChunks()
cChunk* Chunk = Layer.m_Chunks[i].m_LiveChunk;
if( Chunk && Chunk->GetClients().size() == 0 && Chunk->GetReferenceCount() <= 0 )
{
Chunk->SaveToDisk();
Chunk->SaveToDisk();
World->RemoveSpread( ptr_cChunk( Chunk ) );
RemoveChunk( Chunk );
delete Chunk;
@ -495,25 +398,12 @@ void cChunkMap::UnloadUnusedChunks()
}
}
bool cChunkMap::RemoveEntityFromChunk( cEntity & a_Entity, cChunk* a_CalledFrom /* = 0 */ )
{
/* // OLD
for( int i = 0; i < m_Width*m_Height; ++i )
{
cChunkNode & Node = m_Nodes[ i ];
cChunk** Chunks = Node.GetChunks();
for( unsigned int i = 0; i < Node.size(); ++i )
{
if( Chunks[i] != a_CalledFrom )
{
if( Chunks[i]->RemoveEntity( a_Entity, a_CalledFrom ) )
return true;
}
}
}
*/
// NEW
for( int i = 0; i < m_NumLayers; ++i )
{
cChunkLayer & Layer = m_Layers[i];
@ -532,18 +422,12 @@ bool cChunkMap::RemoveEntityFromChunk( cEntity & a_Entity, cChunk* a_CalledFrom
return false;
}
void cChunkMap::SaveAllChunks()
{
for( int i = 0; i < m_Width*m_Height; ++i )
{
cChunkNode & Node = m_Nodes[ i ];
cChunk** Chunks = Node.GetChunks();
for( unsigned int i = 0; i < Node.size(); ++i )
{
Chunks[i]->SaveToDisk();
}
}
for( int i = 0; i < m_NumLayers; ++i )
{
SaveLayer( &m_Layers[i] );
@ -552,6 +436,8 @@ void cChunkMap::SaveAllChunks()
/********************************
* Saving and loading
**/
@ -567,77 +453,81 @@ void cChunkMap::SaveLayer( cChunkLayer* a_Layer )
FILE* f = 0;
#ifdef _WIN32
if( fopen_s(&f, SourceFile, "wb" ) == 0 ) // no error
if (fopen_s(&f, SourceFile, "wb" ) != 0 ) // no error
#else
if( (f = fopen(SourceFile, "wb" )) != 0 ) // no error
if( (f = fopen(SourceFile, "wb" )) == 0 ) // no error
#endif
{
//---------------
// Header
char PakVersion = 1;
char ChunkVersion = 1;
fwrite( &PakVersion, sizeof(PakVersion), 1, f ); // pak version
fwrite( &ChunkVersion, sizeof(ChunkVersion), 1, f ); // chunk version
// Count number of chunks in layer
short NumChunks = 0;
for( int i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i )
{
if( a_Layer->m_Chunks[i].m_Compressed || a_Layer->m_Chunks[i].m_LiveChunk )
NumChunks++;
}
fwrite( &NumChunks, sizeof( NumChunks ), 1, f );
LOG("Num Chunks in layer: %i", NumChunks );
//---------------
// Chunk headers
for( int z = 0; z < LAYER_SIZE; ++z )
{
for( int x = 0; x < LAYER_SIZE; ++x )
{
cChunkData & Data = a_Layer->m_Chunks[x + z*LAYER_SIZE];
CompressChunk( &Data );
if( Data.m_Compressed || Data.m_LiveChunk )
{
int ChunkX = a_Layer->m_X*LAYER_SIZE + x;
int ChunkZ = a_Layer->m_Z*LAYER_SIZE + z;
unsigned int Size = Data.m_CompressedSize; // Needs to be size of compressed data
unsigned int USize = Data.m_UncompressedSize; // Uncompressed size
fwrite( &ChunkX, sizeof( ChunkX ), 1, f );
fwrite( &ChunkZ, sizeof( ChunkZ ), 1, f );
fwrite( &Size, sizeof( Size ), 1, f );
fwrite( &USize, sizeof( USize ), 1, f );
}
}
}
//----------------
// Chunk data
for( int i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i )
{
char* Compressed = a_Layer->m_Chunks[i].m_Compressed;
if( Compressed )
{
fwrite( Compressed, a_Layer->m_Chunks[i].m_CompressedSize, 1, f );
if(a_Layer->m_Chunks[i].m_LiveChunk) // If there's a live chunk we have no need for compressed data
{
delete [] a_Layer->m_Chunks[i].m_Compressed;
a_Layer->m_Chunks[i].m_Compressed = 0;
a_Layer->m_Chunks[i].m_CompressedSize = 0;
}
}
}
fclose(f);
}
else
{
LOGERROR("ERROR: Could not write to file %s", SourceFile );
return;
}
//---------------
// Header
char PakVersion = 1;
char ChunkVersion = 1;
fwrite( &PakVersion, sizeof(PakVersion), 1, f ); // pak version
fwrite( &ChunkVersion, sizeof(ChunkVersion), 1, f ); // chunk version
// Count number of chunks in layer
short NumChunks = 0;
for( int i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i )
{
if( a_Layer->m_Chunks[i].m_Compressed || a_Layer->m_Chunks[i].m_LiveChunk )
{
NumChunks++;
}
}
fwrite( &NumChunks, sizeof( NumChunks ), 1, f );
LOG("Num Chunks in layer: %i", NumChunks );
//---------------
// Chunk headers
for( int z = 0; z < LAYER_SIZE; ++z )
{
for( int x = 0; x < LAYER_SIZE; ++x )
{
cChunkData & Data = a_Layer->m_Chunks[x + z*LAYER_SIZE];
CompressChunk( &Data );
if( Data.m_Compressed || Data.m_LiveChunk )
{
int ChunkX = a_Layer->m_X*LAYER_SIZE + x;
int ChunkZ = a_Layer->m_Z*LAYER_SIZE + z;
unsigned int Size = Data.m_CompressedSize; // Needs to be size of compressed data
unsigned int USize = Data.m_UncompressedSize; // Uncompressed size
fwrite( &ChunkX, sizeof( ChunkX ), 1, f );
fwrite( &ChunkZ, sizeof( ChunkZ ), 1, f );
fwrite( &Size, sizeof( Size ), 1, f );
fwrite( &USize, sizeof( USize ), 1, f );
}
}
}
//----------------
// Chunk data
for( int i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i )
{
char* Compressed = a_Layer->m_Chunks[i].m_Compressed;
if( Compressed )
{
fwrite( Compressed, a_Layer->m_Chunks[i].m_CompressedSize, 1, f );
if(a_Layer->m_Chunks[i].m_LiveChunk) // If there's a live chunk we have no need for compressed data
{
delete [] a_Layer->m_Chunks[i].m_Compressed;
a_Layer->m_Chunks[i].m_Compressed = 0;
a_Layer->m_Chunks[i].m_CompressedSize = 0;
}
}
}
fclose(f);
}
cChunkMap::cChunkLayer* cChunkMap::LoadLayer(int a_LayerX, int a_LayerZ )
{
std::string WorldName = m_World->GetName();
@ -647,80 +537,88 @@ cChunkMap::cChunkLayer* cChunkMap::LoadLayer(int a_LayerX, int a_LayerZ )
FILE* f = 0;
#ifdef _WIN32
if( fopen_s(&f, SourceFile, "rb" ) == 0 ) // no error
if (fopen_s(&f, SourceFile, "rb" ) != 0 ) // no error
#else
if( (f = fopen(SourceFile, "rb" )) != 0 ) // no error
if ((f = fopen(SourceFile, "rb" )) == 0 ) // no error
#endif
{
char PakVersion = 0;
char ChunkVersion = 0;
if( fread( &PakVersion, sizeof(PakVersion), 1, f) != 1 ) { LOGERROR("ERROR 1 READING FROM FILE %s", SourceFile); fclose(f); return false; }
if( PakVersion != 1 ) { LOGERROR("WRONG PAK VERSION!"); fclose(f); return 0; }
if( fread( &ChunkVersion, sizeof(ChunkVersion), 1, f) != 1 ) { LOGERROR("ERROR 2 READING FROM FILE %s", SourceFile); fclose(f); return false; }
if( ChunkVersion != 1 ) { LOGERROR("WRONG CHUNK VERSION!"); fclose(f); return 0; }
short NumChunks = 0;
if( fread( &NumChunks, sizeof(NumChunks), 1, f) != 1 ) { LOGERROR("ERROR 3 READING FROM FILE %s", SourceFile); fclose(f); return false; }
LOG("Num chunks: %i", NumChunks );
LOG("Source File: %s", SourceFile );
cChunkLayer* Layer = new cChunkLayer( LAYER_SIZE*LAYER_SIZE );
Layer->m_X = a_LayerX;
Layer->m_Z = a_LayerZ;
cChunkData** OrderedData = new cChunkData*[ NumChunks ]; // So we can loop over the chunks in the order they were loaded
// Loop over all chunk headers
for( short i = 0; i < NumChunks; ++i )
{
int ChunkX = 0;
int ChunkZ = 0;
if( fread( &ChunkX, sizeof(ChunkX), 1, f) != 1 ) { LOGERROR("ERROR 4 READING FROM FILE %s", SourceFile); fclose(f); return false; }
if( fread( &ChunkZ, sizeof(ChunkZ), 1, f) != 1 ) { LOGERROR("ERROR 5 READING FROM FILE %s", SourceFile); fclose(f); return false; }
cChunkData* Data = Layer->GetChunk( ChunkX, ChunkZ );
if( Data )
{
if( fread( &Data->m_CompressedSize, sizeof(Data->m_CompressedSize), 1, f) != 1 ) { LOGERROR("ERROR 6 READING FROM FILE %s", SourceFile); fclose(f); return false; }
if( fread( &Data->m_UncompressedSize, sizeof(Data->m_UncompressedSize), 1, f) != 1 ) { LOGERROR("ERROR 7 READING FROM FILE %s", SourceFile); fclose(f); return false; }
}
else
{
LOGERROR("Chunk with wrong coordinates in pak file! %i %i", ChunkX, ChunkZ );
fclose(f);
return 0;
}
OrderedData[i] = Data;
}
// Loop over chunks again, in the order they were loaded, and load their compressed data
for( short i = 0; i < NumChunks; ++i )
{
cChunkData* Data = OrderedData[i];
Data->m_Compressed = new char[ Data->m_CompressedSize ];
//printf("Compressed chunk size: %i\n",Data->m_CompressedSize);
if( fread( Data->m_Compressed, Data->m_CompressedSize, 1, f) != 1 ) { LOGERROR("ERROR 8 READING FROM FILE %s", SourceFile); fclose(f); return false; }
/* // Some testing...
uLongf DestSize = Data->m_UncompressedSize;
char* BlockData = new char[ DestSize ];
int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)Data->m_Compressed, Data->m_CompressedSize );
if( errorcode != Z_OK )
{
LOGERROR("lulwut");
}
*/
}
delete [] OrderedData;
fclose(f);
return Layer;
return NULL;
}
else
char PakVersion = 0;
char ChunkVersion = 0;
if( fread( &PakVersion, sizeof(PakVersion), 1, f) != 1 ) { LOGERROR("ERROR 1 READING FROM FILE %s", SourceFile); fclose(f); return false; }
if( PakVersion != 1 ) { LOGERROR("WRONG PAK VERSION!"); fclose(f); return 0; }
if( fread( &ChunkVersion, sizeof(ChunkVersion), 1, f) != 1 ) { LOGERROR("ERROR 2 READING FROM FILE %s", SourceFile); fclose(f); return false; }
if( ChunkVersion != 1 )
{
//LOGWARN("Could not open file %s", SourceFile );
LOGERROR("WRONG CHUNK VERSION!");
fclose(f);
return NULL;
}
return 0;
short NumChunks = 0;
if( fread( &NumChunks, sizeof(NumChunks), 1, f) != 1 )
{
LOGERROR("ERROR 3 READING FROM FILE %s", SourceFile);
fclose(f);
return NULL;
}
LOG("Num chunks: %i", NumChunks );
LOG("Source File: %s", SourceFile );
cChunkLayer* Layer = new cChunkLayer( LAYER_SIZE*LAYER_SIZE );
Layer->m_X = a_LayerX;
Layer->m_Z = a_LayerZ;
cChunkData** OrderedData = new cChunkData*[ NumChunks ]; // So we can loop over the chunks in the order they were loaded
// Loop over all chunk headers
for( short i = 0; i < NumChunks; ++i )
{
int ChunkX = 0;
int ChunkZ = 0;
if( fread( &ChunkX, sizeof(ChunkX), 1, f) != 1 ) { LOGERROR("ERROR 4 READING FROM FILE %s", SourceFile); fclose(f); return false; }
if( fread( &ChunkZ, sizeof(ChunkZ), 1, f) != 1 ) { LOGERROR("ERROR 5 READING FROM FILE %s", SourceFile); fclose(f); return false; }
cChunkData* Data = Layer->GetChunk( ChunkX, ChunkZ );
if( Data )
{
if( fread( &Data->m_CompressedSize, sizeof(Data->m_CompressedSize), 1, f) != 1 ) { LOGERROR("ERROR 6 READING FROM FILE %s", SourceFile); fclose(f); return false; }
if( fread( &Data->m_UncompressedSize, sizeof(Data->m_UncompressedSize), 1, f) != 1 ) { LOGERROR("ERROR 7 READING FROM FILE %s", SourceFile); fclose(f); return false; }
}
else
{
LOGERROR("Chunk with wrong coordinates in pak file! %i %i", ChunkX, ChunkZ );
fclose(f);
return NULL;
}
OrderedData[i] = Data;
}
// Loop over chunks again, in the order they were loaded, and load their compressed data
for( short i = 0; i < NumChunks; ++i )
{
cChunkData* Data = OrderedData[i];
Data->m_Compressed = new char[ Data->m_CompressedSize ];
//printf("Compressed chunk size: %i\n",Data->m_CompressedSize);
if( fread( Data->m_Compressed, Data->m_CompressedSize, 1, f) != 1 )
{
LOGERROR("ERROR 8 READING FROM FILE %s", SourceFile);
fclose(f);
return NULL;
}
}
delete [] OrderedData;
fclose(f);
return Layer;
}
int cChunkMap::GetNumChunks()
{
int NumChunks = 0;
@ -729,4 +627,8 @@ int cChunkMap::GetNumChunks()
NumChunks += m_Layers[i].m_NumChunksLoaded;
}
return NumChunks;
}
}

View File

@ -6,7 +6,7 @@ class cChunk;
class cChunkMap
{
public:
cChunkMap( int a_Width, int a_Height, cWorld* a_World );
cChunkMap(cWorld* a_World );
~cChunkMap();
void AddChunk( cChunk* a_Chunk );
@ -72,26 +72,5 @@ private:
int m_NumLayers;
cChunkLayer* m_Layers;
class cChunkNode
{
public:
cChunkNode();
~cChunkNode();
void push_back( cChunk* a_Chunk );
unsigned int size() { return m_Size; }
unsigned int allocated() { return m_Allocated; }
void resize( unsigned int a_NewSize );
void erase( cChunk* a_Chunk );
cChunk** GetChunks() { return m_Chunks; }
private:
unsigned int m_Size;
unsigned int m_Allocated;
cChunk** m_Chunks;
};
cChunkNode* m_Nodes;
int m_Width, m_Height;
cWorld* m_World;
};

View File

@ -229,7 +229,7 @@ cWorld::cWorld( const char* a_WorldName )
m_Description = IniFile2.GetValue("Server", "Description", "MCServer! - It's OVER 9000!").c_str();
}
m_ChunkMap = new cChunkMap( 32, 32, this );
m_ChunkMap = new cChunkMap(this );
m_pState->pChunkGenerator = new cChunkGenerator( m_ChunkMap );
m_Time = 0;