From 87a7c39dfc2072a2197de2a6c9ac99aefc5919ca Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 29 Jan 2012 21:40:21 +0000 Subject: Old code begone! ChunkMap cleanup git-svn-id: http://mc-server.googlecode.com/svn/trunk@189 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cChunk.h | 2 - source/cChunkMap.cpp | 598 +++++++++++++++++++++------------------------------ source/cChunkMap.h | 23 +- source/cWorld.cpp | 2 +- 4 files changed, 252 insertions(+), 373 deletions(-) diff --git a/source/cChunk.h b/source/cChunk.h index 15577d372..bd9882659 100644 --- a/source/cChunk.h +++ b/source/cChunk.h @@ -9,8 +9,6 @@ # define __C_CHUNK_INLINE__ #endif -#include - namespace Json { class Value; diff --git a/source/cChunkMap.cpp b/source/cChunkMap.cpp index cb2b5f3f3..417b4a98e 100644 --- a/source/cChunkMap.cpp +++ b/source/cChunkMap.cpp @@ -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 ) +cChunkMap::~cChunkMap() { - 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 ) + cChunkLayer* Layer = GetLayerForChunk( a_X, a_Z ); + if (Layer == NULL) { - 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]; - } + return NULL; + } + + cChunkData* Data = Layer->GetChunk( a_X, a_Z ); + if (Data->m_LiveChunk != NULL) + { + // Already loaded and alive + return Data->m_LiveChunk; } - */ - // NEW - cChunkLayer* Layer = GetLayerForChunk( a_X, a_Z ); - if( Layer ) + // Do we at least have the compressed chunk? + if (Data->m_Compressed == NULL) { - cChunkData* Data = Layer->GetChunk( a_X, a_Z ); - if( Data->m_LiveChunk ) return Data->m_LiveChunk; + return NULL; + } - // Decompress cached chunk - if( Data->m_Compressed ) + // 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 ) + 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; + } + 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("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; - }; - - delete [] BlockData; + 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 ) + 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 ) { - if( a_Layer->m_Chunks[i].m_Compressed || a_Layer->m_Chunks[i].m_LiveChunk ) - NumChunks++; + NumChunks++; } + } - fwrite( &NumChunks, sizeof( NumChunks ), 1, f ); - LOG("Num Chunks in layer: %i", NumChunks ); + fwrite( &NumChunks, sizeof( NumChunks ), 1, f ); + LOG("Num Chunks in layer: %i", NumChunks ); - //--------------- - // Chunk headers - for( int z = 0; z < LAYER_SIZE; ++z ) + //--------------- + // Chunk headers + for( int z = 0; z < LAYER_SIZE; ++z ) + { + for( int x = 0; x < LAYER_SIZE; ++x ) { - 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 ) { - 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 ); - } + 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 ) + //---------------- + // Chunk data + for( int i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i ) + { + char* Compressed = a_Layer->m_Chunks[i].m_Compressed; + if( Compressed ) { - 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 { - 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; - } + 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 ); } + + 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; - } + return NULL; + } + + 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 NULL; + } - OrderedData[i] = Data; - } + 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 ); - // Loop over chunks again, in the order they were loaded, and load their compressed data - for( short i = 0; i < NumChunks; ++i ) + 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 ) { - 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"); - } - */ + 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; } - delete [] OrderedData; - fclose(f); - return Layer; + OrderedData[i] = Data; } - else + + // Loop over chunks again, in the order they were loaded, and load their compressed data + for( short i = 0; i < NumChunks; ++i ) { - //LOGWARN("Could not open file %s", SourceFile ); + 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; + } } - return 0; + 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; -} \ No newline at end of file +} + + + + diff --git a/source/cChunkMap.h b/source/cChunkMap.h index b2292870f..e375a0de0 100644 --- a/source/cChunkMap.h +++ b/source/cChunkMap.h @@ -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; }; diff --git a/source/cWorld.cpp b/source/cWorld.cpp index 9891946be..0d09c76f5 100644 --- a/source/cWorld.cpp +++ b/source/cWorld.cpp @@ -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; -- cgit v1.2.3