From 19e711a1e3f631079c9c8eef93fe4d62a7e801cd Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Tue, 14 Feb 2012 21:09:14 +0000 Subject: Slight cleanup - removed old code, some additional comments on dangerous functions git-svn-id: http://mc-server.googlecode.com/svn/trunk@261 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/cChunk.h | 19 ++- source/cChunkLoader.cpp | 359 ------------------------------------------------ source/cChunkLoader.h | 30 ---- source/cChunkMap.h | 4 +- source/cWorld.h | 3 +- 5 files changed, 13 insertions(+), 402 deletions(-) delete mode 100644 source/cChunkLoader.cpp delete mode 100644 source/cChunkLoader.h (limited to 'source') diff --git a/source/cChunk.h b/source/cChunk.h index 0e1dccfef..4ff060b30 100644 --- a/source/cChunk.h +++ b/source/cChunk.h @@ -52,7 +52,6 @@ public: cChunk(int a_X, int a_Y, int a_Z, cWorld* a_World); ~cChunk(); - void Initialize(); bool IsValid(void) const {return m_IsValid; } // Returns true if the chunk is valid (loaded / generated) void SetValid(bool a_SendToClients = true); // Also wakes up all clients attached to this chunk to let them finish logging in bool CanUnload(void); @@ -87,9 +86,6 @@ public: void AddEntity( cEntity * a_Entity ); void RemoveEntity( cEntity * a_Entity); - // TODO: This interface is dangerous - OBSOLETE const std::list< cClientHandle* > & GetClients();// { return m_LoadedByClient; } - inline void RecalculateLighting() { m_bCalculateLighting = true; } // Recalculate lighting next tick inline void RecalculateHeightmap() { m_bCalculateHeightmap = true; } // Recalculate heightmap next tick void SpreadLight(char* a_LightBuffer); @@ -102,6 +98,10 @@ public: void Broadcast( const cPacket & a_Packet, cClientHandle * a_Exclude = NULL) {Broadcast(&a_Packet, a_Exclude); } void Broadcast( const cPacket * a_Packet, cClientHandle * a_Exclude = NULL); + // TODO: These functions are dangerous - rewrite to: + // Loaded(blockdata, lightdata, blockentities, entities), + // Generated(blockdata, lightdata, blockentities, entities), + // GetBlockData(blockdatadest) etc. char* pGetBlockData() { return m_BlockData; } char* pGetType() { return m_BlockType; } char* pGetMeta() { return m_BlockMeta; } @@ -109,6 +109,8 @@ public: char* pGetSkyLight() { return m_BlockSkyLight; } void CopyBlockDataFrom(const char * a_NewBlockData); // Copies all blockdata, recalculates heightmap (used by chunk loaders) + + // TODO: Move this into the specific WSSchema: void LoadFromJson( const Json::Value & a_Value ); void SaveToJson( Json::Value & a_Value ); @@ -122,18 +124,15 @@ public: inline static unsigned int MakeIndex(int x, int y, int z ) { if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 ) + { return y + (z * 128) + (x * 128 * 16); + } return 0; } static const int c_NumBlocks = 16*128*16; static const int c_BlockDataSize = c_NumBlocks * 2 + (c_NumBlocks/2); // 2.5 * numblocks - - // Reference counting - void AddReference(); - void RemoveReference(); - int GetReferenceCount(); - + private: bool m_IsValid; // True if the chunk is loaded / generated diff --git a/source/cChunkLoader.cpp b/source/cChunkLoader.cpp deleted file mode 100644 index 41d61cfaf..000000000 --- a/source/cChunkLoader.cpp +++ /dev/null @@ -1,359 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#if 0 // ignore all contents of this file -#include "cChunkLoader.h" -#include "cChunk.h" -#include "cMCLogger.h" -#include "BlockID.h" -#include "cCriticalSection.h" -#include "cEvent.h" -#include "cThread.h" -#include "cSleep.h" - -#include "cChestEntity.h" -#include "cFurnaceEntity.h" -#include "cSignEntity.h" - -#include -#ifndef _WIN32 -#include -#endif - -struct ChunkData -{ - ChunkData() - : x( 0 ) - , z( 0 ) - , Data( 0 ) - , LiveChunk( 0 ) - {} - int x, z; - unsigned int DataSize; - unsigned int ChunkStart; - char* Data; - cChunk* LiveChunk; -}; - -typedef std::list< ChunkData > ChunkDataList; -struct cChunkLoader::ChunkPack -{ - ChunkDataList AllChunks; - int x, y, z; -}; - -typedef std::list< cChunkLoader::ChunkPack > ChunkPackList; -struct cChunkLoader::ChunkPacks -{ - ChunkPackList AllPacks; -}; - -cChunkLoader::cChunkLoader() - : m_bStop( false ) - , m_CriticalSection( new cCriticalSection() ) - , m_Event( new cEvent() ) - , m_ChunkPacks( new ChunkPacks ) -{ - cThread( SaveThread, this ); -} - -cChunkLoader::~cChunkLoader() -{ - m_bStop = true; - m_Event->Wait(); - delete m_CriticalSection; -} - -void cChunkLoader::SaveThread( void* a_Param ) -{ - cChunkLoader* self = (cChunkLoader*)a_Param; - while( !self->m_bStop ) - { - cSleep::MilliSleep( 1000 ); // Only check for saving once a second - } - self->m_Event->Set(); -} - -cChunk* cChunkLoader::LoadChunk( int a_X, int a_Y, int a_Z ) -{ - m_CriticalSection->Lock(); - cChunk* Chunk = 0; - - Chunk = LoadOldFormat( a_X, a_Y, a_Z ); - if( Chunk ) { Chunk->CalculateHeightmap(); } - else - { - 1; // load new format() - } - - m_CriticalSection->Unlock(); - return Chunk; -} - -bool cChunkLoader::SaveChunk( const cChunk & a_Chunk ) -{ - m_CriticalSection->Lock(); - bool Success = SaveOldFormat( a_Chunk ); - m_CriticalSection->Unlock(); - - return Success; -} - -/************************************************** - * Old format stuffs - **/ - -cChunk* cChunkLoader::LoadOldFormat( int a_X, int a_Y, int a_Z ) -{ - AString SourceFile; - Printf(SourceFile, "world/X%i_Y%i_Z%i.bin", a_X, a_Y, a_Z ); - - FILE* f = 0; - #ifdef _WIN32 - if( fopen_s(&f, SourceFile, "rb" ) == 0 ) // no error - #else - if( (f = fopen(SourceFile, "rb" )) != 0 ) // no error - #endif - { - cChunk* Chunk = new cChunk( a_X, a_Y, a_Z ); - if( fread( Chunk->m_BlockData, sizeof(char)*cChunk::c_BlockDataSize, 1, f) != 1 ) { LOGERROR("ERROR READING FROM FILE %s", SourceFile); fclose(f); return false; } - - // Now load Block Entities - ENUM_BLOCK_ID BlockType; - while( fread( &BlockType, sizeof(ENUM_BLOCK_ID), 1, f) == 1 ) - { - switch( BlockType ) - { - case E_BLOCK_CHEST: - { - cChestEntity* ChestEntity = new cChestEntity( 0, 0, 0 ); - if( !ChestEntity->LoadFromFile( f ) ) - { - LOGERROR("ERROR READING CHEST FROM FILE %s", SourceFile ); - delete ChestEntity; - fclose(f); - return false; - } - Chunk->m_BlockEntities.push_back( ChestEntity ); - } - break; - case E_BLOCK_FURNACE: - { - cFurnaceEntity* FurnaceEntity = new cFurnaceEntity( 0, 0, 0 ); - if( !FurnaceEntity->LoadFromFile( f ) ) - { - LOGERROR("ERROR READING FURNACE FROM FILE %s", SourceFile ); - delete FurnaceEntity; - fclose(f); - return false; - } - Chunk->m_BlockEntities.push_back( FurnaceEntity ); - Chunk->m_TickBlockEntities.push_back( FurnaceEntity ); // They need tickin' - } - break; - case E_BLOCK_SIGN_POST: - case E_BLOCK_WALLSIGN: - { - cSignEntity* SignEntity = new cSignEntity(BlockType, 0, 0, 0 ); - if( !SignEntity->LoadFromFile( f ) ) - { - LOGERROR("ERROR READING SIGN FROM FILE %s", SourceFile ); - delete SignEntity; - fclose(f); - return false; - } - Chunk->m_BlockEntities.push_back( SignEntity ); - } - break; - default: - break; - } - } - - fclose(f); - return Chunk; - } - else - { - return 0; - } -} - -bool cChunkLoader::SaveOldFormat( const cChunk & a_Chunk ) -{ - AString SourceFile; - Printf(SourceFile, "world/X%i_Y%i_Z%i.bin", a_Chunk.m_PosX, a_Chunk.m_PosY, a_Chunk.m_PosZ ); - - #ifdef _WIN32 - { - SECURITY_ATTRIBUTES Attrib; - Attrib.nLength = sizeof(SECURITY_ATTRIBUTES); - Attrib.lpSecurityDescriptor = NULL; - Attrib.bInheritHandle = false; - ::CreateDirectory("world", &Attrib); - } - #else - { - mkdir("world", S_IRWXU | S_IRWXG | S_IRWXO); - } - #endif - - FILE* f = 0; -#ifdef _WIN32 - if( fopen_s(&f, SourceFile, "wb" ) == 0 ) // no error - #else - if( (f = fopen(SourceFile, "wb" )) != 0 ) // no error - #endif - { - fwrite( a_Chunk.m_BlockData, sizeof(char)*cChunk::c_BlockDataSize, 1, f ); - - // Now write Block Entities - for( std::list::const_iterator itr = a_Chunk.m_BlockEntities.begin(); itr != a_Chunk.m_BlockEntities.end(); ++itr) - { - cBlockEntity* BlockEntity = *itr; - switch( BlockEntity->GetBlockType() ) - { - case E_BLOCK_CHEST: - { - cChestEntity* ChestEntity = reinterpret_cast< cChestEntity* >( BlockEntity ); - ChestEntity->WriteToFile( f ); - } - break; - case E_BLOCK_FURNACE: - { - cFurnaceEntity* FurnaceEntity = reinterpret_cast< cFurnaceEntity* >( BlockEntity ); - FurnaceEntity->WriteToFile( f ); - } - break; - case E_BLOCK_SIGN_POST: - case E_BLOCK_WALLSIGN: - { - cSignEntity* SignEntity = reinterpret_cast< cSignEntity* >( BlockEntity ); - SignEntity->WriteToFile( f ); - } - break; - default: - break; - } - } - - fclose(f); - return true; - } - else - { - LOGERROR("ERROR WRITING TO FILE %s", SourceFile); - return false; - } -} - - -/****************************************** - * New format - **/ - -cChunk* cChunkLoader::LoadFormat1( int a_X, int a_Y, int a_Z ) -{ - int PakX = (int)(floorf((float)a_X / 16.f)); - int PakY = (int)(floorf((float)a_Y / 16.f)); - int PakZ = (int)(floorf((float)a_Z / 16.f)); - - ChunkPack * Pack = 0; - ChunkPackList & PackList = m_ChunkPacks->AllPacks; - for( ChunkPackList::iterator itr = PackList.begin(); itr != PackList.end(); ++itr ) - { - if( itr->x == PakX && itr->y == PakY && itr->z == PakZ ) - { - Pack = &(*itr); - break; - } - } - - - if( !Pack ) // The pack was not in memory, so try to load it from disk - { - Pack = LoadPak1( PakX, PakY, PakZ ); // Load .pak file from disk - if( Pack ) - { - PackList.push_back( *Pack ); // Add it to the loaded list - } - } - - if( Pack ) // Allright, the pack is in memory - { - ChunkData * Data = 0; - ChunkDataList & ChunkList = Pack->AllChunks; - for( ChunkDataList::iterator itr = ChunkList.begin(); itr != ChunkList.end(); ++itr ) - { - if( itr->x == a_X && itr->z == a_Z ) - { - Data = &(*itr); - break; - } - } - - if( !Data ) // Sorry, chunk does not exist (yet) - return 0; - - if( Data->LiveChunk ) // This chunk is already loaded and decoded (this should actually never happen) - return Data->LiveChunk; - - // Decompress chunk, and return brand new chunk - - // doing it... - - return 0; // actually return the chunk - } - - return 0; // .pak file didn't exist -} - -cChunkLoader::ChunkPack* cChunkLoader::LoadPak1( int PakX, int PakY, int PakZ ) -{ - AString SourceFile; - Printf(SourceFile, "world/X%i_Y%i_Z%i.pak", PakX, PakY, PakZ ); - - FILE* f = 0; - #ifdef _WIN32 - if( fopen_s(&f, SourceFile, "rb" ) == 0 ) // no error - #else - if( (f = fopen(SourceFile, "rb" )) != 0 ) // no error - #endif - { - cChunkLoader::ChunkPack * Pack = new cChunkLoader::ChunkPack; - Pack->x = PakX; - Pack->y = PakY; - Pack->z = PakZ; - - short Version = 0; - if( fread( &Version, sizeof( short ), 1, f ) != 1 ) { LOGERROR("Error reading file %s", SourceFile ); return 0; } - if( Version != 1 ) { LOGERROR("Wrong pak version! %s", SourceFile ); return 0; } - short NumChunks = 0; - if( fread( &NumChunks, sizeof( short ), 1, f ) != 1 ) { LOGERROR("Error reading file %s", SourceFile ); return 0; } - - // Load all the headers - for( short i = 0; i < NumChunks; ++i ) - { - ChunkData Data; - if( fread( &Data.x, sizeof( int ), 1, f ) != 1 ) { LOGERROR("Error reading file %s", SourceFile ); return 0; } - if( fread( &Data.z, sizeof( int ), 1, f ) != 1 ) { LOGERROR("Error reading file %s", SourceFile ); return 0; } - if( fread( &Data.DataSize, sizeof( unsigned int ), 1, f ) != 1 ) { LOGERROR("Error reading file %s", SourceFile ); return 0; } - if( fread( &Data.ChunkStart, sizeof( unsigned int ), 1, f ) != 1 ) { LOGERROR("Error reading file %s", SourceFile ); return 0; } - Pack->AllChunks.push_back( Data ); - } - - // Load all compressed chunk data in the order the headers were loaded - ChunkDataList::iterator itr = Pack->AllChunks.begin(); - for( short i = 0; i < NumChunks; ++i ) - { - itr->Data = new char[ itr->DataSize ]; - if( fread( itr->Data, sizeof( char ) * itr->DataSize, 1, f ) != 1 ) { LOGERROR("Error reading file %s", SourceFile ); return 0; } - ++itr; - } - - // And we're done :) - return Pack; - } - return 0; -} -#endif \ No newline at end of file diff --git a/source/cChunkLoader.h b/source/cChunkLoader.h deleted file mode 100644 index aa6f3806f..000000000 --- a/source/cChunkLoader.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -class cChunk; -class cChunkLoader -{ -public: - cChunkLoader(); - ~cChunkLoader(); - - cChunk* LoadChunk( int a_X, int a_Y, int a_Z ); - bool SaveChunk( const cChunk & a_Chunk ); - - struct ChunkPack; -private: - cChunk* LoadFormat1( int a_X, int a_Y, int a_Z ); - ChunkPack* LoadPak1( int PakX, int PakY, int PakZ ); // This loads a .pak file from disk and returns it, nothing more - - // Old stuffs - cChunk* LoadOldFormat( int a_X, int a_Y, int a_Z ); - bool SaveOldFormat( const cChunk & a_Chunk ); - - static void SaveThread( void* a_Param ); - - bool m_bStop; - cCriticalSection* m_CriticalSection; - cEvent* m_Event; - - struct ChunkPacks; // Defined in .cpp - ChunkPacks* m_ChunkPacks; -}; \ No newline at end of file diff --git a/source/cChunkMap.h b/source/cChunkMap.h index e43c095d0..1d3ddf6aa 100644 --- a/source/cChunkMap.h +++ b/source/cChunkMap.h @@ -27,8 +27,8 @@ public: cChunkMap(cWorld* a_World ); ~cChunkMap(); - cChunkPtr GetChunk ( int a_X, int a_Y, int a_Z ); // Also queues the chunk for loading / generating if not valid - cChunkPtr GetChunkNoGen( int a_X, int a_Y, int a_Z ); // Also queues the chunk for loading if not valid; doesn't generate + cChunkPtr GetChunk ( int a_ChunkX, int a_ChunkY, int a_ChunkZ ); // Also queues the chunk for loading / generating if not valid + cChunkPtr GetChunkNoGen( int a_ChunkX, int a_ChunkY, int a_ChunkZ ); // Also queues the chunk for loading if not valid; doesn't generate void Tick( float a_Dt, MTRand & a_TickRand ); diff --git a/source/cWorld.h b/source/cWorld.h index 146724262..b8c1391fd 100644 --- a/source/cWorld.h +++ b/source/cWorld.h @@ -82,9 +82,10 @@ public: // >> EXPORTED IN MANUALBINDINGS << unsigned int GetNumPlayers(); //tolua_export - // TODO: This interface is dangerous + // TODO: This interface is dangerous - rewrite to DoWithPlayer(playername, action) cPlayer * GetPlayer( const char * a_PlayerName ); //tolua_export + // TODO: This interface is dangerous - rewrite to DoWithClosestPlayer(pos, sight, action) cPlayer * FindClosestPlayer(const Vector3f & a_Pos, float a_SightLimit); void SendPlayerList(cPlayer * a_DestPlayer); // Sends playerlist to the player -- cgit v1.2.3