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__ # define __C_CHUNK_INLINE__
#endif #endif
#include <list>
namespace Json namespace Json
{ {
class Value; class Value;

View File

@ -3,7 +3,6 @@
#include "cChunkMap.h" #include "cChunkMap.h"
#include "cChunk.h" #include "cChunk.h"
#include "cMCLogger.h"
#include "cWorld.h" #include "cWorld.h"
#include "cRoot.h" #include "cRoot.h"
#include "cMakeDir.h" #include "cMakeDir.h"
@ -25,105 +24,24 @@
cChunkMap::cChunkMap( int a_Width, int a_Height, cWorld* a_World ) cChunkMap::cChunkMap(cWorld* a_World )
: m_Nodes( new cChunkNode[ a_Width * a_Height ] ) : m_Layers( 0 )
, m_Width( a_Width )
, m_Height( a_Height )
, m_Layers( 0 )
, m_NumLayers( 0 ) , m_NumLayers( 0 )
, m_World( a_World ) , m_World( a_World )
{ {
} }
cChunkMap::~cChunkMap() 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 ) 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; return 0;
} }
bool cChunkMap::RemoveLayer( cChunkLayer* a_Layer ) bool cChunkMap::RemoveLayer( cChunkLayer* a_Layer )
{ {
cChunkLayer* NewLayers = 0; cChunkLayer* NewLayers = 0;
@ -170,6 +92,10 @@ bool cChunkMap::RemoveLayer( cChunkLayer* a_Layer )
return true; return true;
} }
cChunkMap::cChunkLayer* cChunkMap::AddLayer( const cChunkLayer & a_Layer ) cChunkMap::cChunkLayer* cChunkMap::AddLayer( const cChunkLayer & a_Layer )
{ {
cChunkLayer* TempLayers = new cChunkLayer[m_NumLayers+1]; cChunkLayer* TempLayers = new cChunkLayer[m_NumLayers+1];
@ -187,6 +113,10 @@ cChunkMap::cChunkLayer* cChunkMap::AddLayer( const cChunkLayer & a_Layer )
return NewLayer; return NewLayer;
} }
void cChunkMap::AddChunk( cChunk* a_Chunk ) void cChunkMap::AddChunk( cChunk* a_Chunk )
{ {
/* // OLD /* // OLD
@ -217,6 +147,10 @@ void cChunkMap::AddChunk( cChunk* a_Chunk )
FoundLayer->m_NumChunksLoaded++; FoundLayer->m_NumChunksLoaded++;
} }
void cChunkMap::RemoveChunk( cChunk* a_Chunk ) void cChunkMap::RemoveChunk( cChunk* a_Chunk )
{ {
/* // OLD /* // OLD
@ -237,6 +171,10 @@ void cChunkMap::RemoveChunk( cChunk* a_Chunk )
} }
} }
void cChunkMap::CompressChunk( cChunkData* a_ChunkData ) void cChunkMap::CompressChunk( cChunkData* a_ChunkData )
{ {
if( a_ChunkData->m_LiveChunk ) 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 ) 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 ); return GetLayer( LayerX, LayerZ );
} }
cChunkMap::cChunkLayer* cChunkMap::GetLayer( int a_LayerX, int a_LayerZ ) cChunkMap::cChunkLayer* cChunkMap::GetLayer( int a_LayerX, int a_LayerZ )
{ {
// Find layer in memory // Find layer in memory
@ -314,33 +253,32 @@ cChunkMap::cChunkLayer* cChunkMap::GetLayer( int a_LayerX, int a_LayerZ )
return NewLayer; return NewLayer;
} }
cChunk* cChunkMap::GetChunk( int a_X, int a_Y, int a_Z ) 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 ); cChunkLayer* Layer = GetLayerForChunk( a_X, a_Z );
if( Layer ) if (Layer == NULL)
{ {
cChunkData* Data = Layer->GetChunk( a_X, a_Z ); return NULL;
if( Data->m_LiveChunk ) return Data->m_LiveChunk; }
// Decompress cached chunk cChunkData* Data = Layer->GetChunk( a_X, a_Z );
if( Data->m_Compressed ) if (Data->m_LiveChunk != NULL)
{ {
// Already loaded and alive
return Data->m_LiveChunk;
}
// 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; uLongf DestSize = Data->m_UncompressedSize;
char* BlockData = new char[ DestSize ]; char* BlockData = new char[ DestSize ];
int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)Data->m_Compressed, Data->m_CompressedSize ); int errorcode = uncompress( (Bytef*)BlockData, &DestSize, (Bytef*)Data->m_Compressed, Data->m_CompressedSize );
@ -388,9 +326,6 @@ cChunk* cChunkMap::GetChunk( int a_X, int a_Y, int a_Z )
} }
else else
{ {
//Json::StyledWriter writer;
//LOGINFO("Trailing Json:" );
//printf("%s", writer.write( root ).c_str() );
Chunk->LoadFromJson( root ); Chunk->LoadFromJson( root );
} }
} }
@ -399,9 +334,7 @@ cChunk* cChunkMap::GetChunk( int a_X, int a_Y, int a_Z )
delete [] Data->m_Compressed; Data->m_Compressed = 0; Data->m_CompressedSize = 0; delete [] Data->m_Compressed; Data->m_Compressed = 0; Data->m_CompressedSize = 0;
return Chunk; return Chunk;
} }
} return NULL;
}
return 0;
} }
@ -410,56 +343,26 @@ cChunk* cChunkMap::GetChunk( int a_X, int a_Y, int a_Z )
void cChunkMap::Tick( float a_Dt ) void cChunkMap::Tick( float a_Dt )
{ {
/* // OLD for( int lay = 0; lay < m_NumLayers; ++lay )
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 i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i ) for( int i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i )
{ {
cChunk* Chunk = m_Layers[l].m_Chunks[i].m_LiveChunk; cChunk* Chunk = m_Layers[lay].m_Chunks[i].m_LiveChunk;
if( Chunk ) if ( Chunk != NULL)
{
Chunk->Tick( a_Dt ); Chunk->Tick( a_Dt );
} }
} }
} // for lay - m_Layers[]
} }
void cChunkMap::UnloadUnusedChunks() void cChunkMap::UnloadUnusedChunks()
{ {
cWorld* World = m_World; 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 ) for( int l = 0; l < m_NumLayers; ++l )
{ {
cChunkLayer & Layer = m_Layers[l]; cChunkLayer & Layer = m_Layers[l];
@ -495,25 +398,12 @@ void cChunkMap::UnloadUnusedChunks()
} }
} }
bool cChunkMap::RemoveEntityFromChunk( cEntity & a_Entity, cChunk* a_CalledFrom /* = 0 */ ) 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 ) for( int i = 0; i < m_NumLayers; ++i )
{ {
cChunkLayer & Layer = m_Layers[i]; cChunkLayer & Layer = m_Layers[i];
@ -532,18 +422,12 @@ bool cChunkMap::RemoveEntityFromChunk( cEntity & a_Entity, cChunk* a_CalledFrom
return false; return false;
} }
void cChunkMap::SaveAllChunks() 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 ) for( int i = 0; i < m_NumLayers; ++i )
{ {
SaveLayer( &m_Layers[i] ); SaveLayer( &m_Layers[i] );
@ -552,6 +436,8 @@ void cChunkMap::SaveAllChunks()
/******************************** /********************************
* Saving and loading * Saving and loading
**/ **/
@ -567,11 +453,15 @@ void cChunkMap::SaveLayer( cChunkLayer* a_Layer )
FILE* f = 0; FILE* f = 0;
#ifdef _WIN32 #ifdef _WIN32
if( fopen_s(&f, SourceFile, "wb" ) == 0 ) // no error if (fopen_s(&f, SourceFile, "wb" ) != 0 ) // no error
#else #else
if( (f = fopen(SourceFile, "wb" )) != 0 ) // no error if( (f = fopen(SourceFile, "wb" )) == 0 ) // no error
#endif #endif
{ {
LOGERROR("ERROR: Could not write to file %s", SourceFile );
return;
}
//--------------- //---------------
// Header // Header
char PakVersion = 1; char PakVersion = 1;
@ -584,8 +474,10 @@ void cChunkMap::SaveLayer( cChunkLayer* a_Layer )
for( int i = 0; i < LAYER_SIZE*LAYER_SIZE; ++i ) 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 ); fwrite( &NumChunks, sizeof( NumChunks ), 1, f );
LOG("Num Chunks in layer: %i", NumChunks ); LOG("Num Chunks in layer: %i", NumChunks );
@ -629,14 +521,12 @@ void cChunkMap::SaveLayer( cChunkLayer* a_Layer )
} }
} }
fclose(f); fclose(f);
} }
else
{
LOGERROR("ERROR: Could not write to file %s", SourceFile );
}
}
cChunkMap::cChunkLayer* cChunkMap::LoadLayer(int a_LayerX, int a_LayerZ ) cChunkMap::cChunkLayer* cChunkMap::LoadLayer(int a_LayerX, int a_LayerZ )
{ {
@ -647,20 +537,34 @@ cChunkMap::cChunkLayer* cChunkMap::LoadLayer(int a_LayerX, int a_LayerZ )
FILE* f = 0; FILE* f = 0;
#ifdef _WIN32 #ifdef _WIN32
if( fopen_s(&f, SourceFile, "rb" ) == 0 ) // no error if (fopen_s(&f, SourceFile, "rb" ) != 0 ) // no error
#else #else
if( (f = fopen(SourceFile, "rb" )) != 0 ) // no error if ((f = fopen(SourceFile, "rb" )) == 0 ) // no error
#endif #endif
{ {
return NULL;
}
char PakVersion = 0; char PakVersion = 0;
char ChunkVersion = 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( 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( 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( 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; } if( ChunkVersion != 1 )
{
LOGERROR("WRONG CHUNK VERSION!");
fclose(f);
return NULL;
}
short NumChunks = 0; short NumChunks = 0;
if( fread( &NumChunks, sizeof(NumChunks), 1, f) != 1 ) { LOGERROR("ERROR 3 READING FROM FILE %s", SourceFile); fclose(f); return false; } 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("Num chunks: %i", NumChunks );
LOG("Source File: %s", SourceFile ); LOG("Source File: %s", SourceFile );
@ -668,6 +572,7 @@ cChunkMap::cChunkLayer* cChunkMap::LoadLayer(int a_LayerX, int a_LayerZ )
Layer->m_X = a_LayerX; Layer->m_X = a_LayerX;
Layer->m_Z = a_LayerZ; Layer->m_Z = a_LayerZ;
cChunkData** OrderedData = new cChunkData*[ NumChunks ]; // So we can loop over the chunks in the order they were loaded cChunkData** OrderedData = new cChunkData*[ NumChunks ]; // So we can loop over the chunks in the order they were loaded
// Loop over all chunk headers // Loop over all chunk headers
for( short i = 0; i < NumChunks; ++i ) for( short i = 0; i < NumChunks; ++i )
{ {
@ -685,7 +590,7 @@ cChunkMap::cChunkLayer* cChunkMap::LoadLayer(int a_LayerX, int a_LayerZ )
{ {
LOGERROR("Chunk with wrong coordinates in pak file! %i %i", ChunkX, ChunkZ ); LOGERROR("Chunk with wrong coordinates in pak file! %i %i", ChunkX, ChunkZ );
fclose(f); fclose(f);
return 0; return NULL;
} }
OrderedData[i] = Data; OrderedData[i] = Data;
@ -697,29 +602,22 @@ cChunkMap::cChunkLayer* cChunkMap::LoadLayer(int a_LayerX, int a_LayerZ )
cChunkData* Data = OrderedData[i]; cChunkData* Data = OrderedData[i];
Data->m_Compressed = new char[ Data->m_CompressedSize ]; Data->m_Compressed = new char[ Data->m_CompressedSize ];
//printf("Compressed chunk size: %i\n",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; } if( fread( Data->m_Compressed, Data->m_CompressedSize, 1, f) != 1 )
/* // 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"); LOGERROR("ERROR 8 READING FROM FILE %s", SourceFile);
fclose(f);
return NULL;
} }
*/
} }
delete [] OrderedData; delete [] OrderedData;
fclose(f); fclose(f);
return Layer; return Layer;
} }
else
{
//LOGWARN("Could not open file %s", SourceFile );
}
return 0;
}
int cChunkMap::GetNumChunks() int cChunkMap::GetNumChunks()
{ {
@ -730,3 +628,7 @@ int cChunkMap::GetNumChunks()
} }
return NumChunks; return NumChunks;
} }

View File

@ -6,7 +6,7 @@ class cChunk;
class cChunkMap class cChunkMap
{ {
public: public:
cChunkMap( int a_Width, int a_Height, cWorld* a_World ); cChunkMap(cWorld* a_World );
~cChunkMap(); ~cChunkMap();
void AddChunk( cChunk* a_Chunk ); void AddChunk( cChunk* a_Chunk );
@ -72,26 +72,5 @@ private:
int m_NumLayers; int m_NumLayers;
cChunkLayer* m_Layers; 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; 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_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_pState->pChunkGenerator = new cChunkGenerator( m_ChunkMap );
m_Time = 0; m_Time = 0;