From cb130d9158dc4e9c456d088d5e214b7d829ccc3a Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sun, 26 Jun 2011 00:03:58 +0300 Subject: [PATCH] cleaned map stuff --- src/CMakeLists.txt | 3 +- src/client.cpp | 11 ++- src/map.cpp | 44 +++++------- src/map.h | 11 ++- src/mapsector.cpp | 104 +++++---------------------- src/mapsector.h | 170 +++------------------------------------------ src/test.cpp | 9 +-- 7 files changed, 67 insertions(+), 285 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 73a960ec..ac58d1d0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -206,7 +206,8 @@ else() endif() set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG ${RELEASE_WARNING_FLAGS} ${WARNING_FLAGS} -O3 -ffast-math -Wall -fomit-frame-pointer -pipe -funroll-loops") - set(CMAKE_CXX_FLAGS_DEBUG "-g -O1 -Wall ${WARNING_FLAGS}") + #set(CMAKE_CXX_FLAGS_DEBUG "-g -O1 -Wall ${WARNING_FLAGS}") + set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall ${WARNING_FLAGS}") if(USE_GPROF) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pg") diff --git a/src/client.cpp b/src/client.cpp index abc05650..f97acfd4 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "main.h" #include #include "porting.h" +#include "mapsector.h" void * MeshUpdateThread::Thread() { @@ -715,15 +716,16 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) //TimeTaker timer("MapBlock deSerialize"); // 0ms - try{ - block = sector->getBlockNoCreate(p.Y); + block = sector->getBlockNoCreateNoEx(p.Y); + if(block) + { /* Update an existing block */ //dstream<<"Updating"<deSerialize(istr, ser_version); } - catch(InvalidPositionException &e) + else { /* Create a new block @@ -952,6 +954,8 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) } else if(command == TOCLIENT_SECTORMETA) { + dstream<<"Client received DEPRECATED TOCLIENT_SECTORMETA"<getBlockNoCreate(p3d.Y); - + MapBlock *block = sector->getBlockNoCreateNoEx(p3d.Y); return block; } -MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d) -{ - try - { - v2s16 p2d(p3d.X, p3d.Z); - MapSector * sector = getSectorNoGenerate(p2d); - MapBlock *block = sector->getBlockNoCreate(p3d.Y); - return block; - } - catch(InvalidPositionException &e) - { - return NULL; - } +MapBlock * Map::getBlockNoCreate(v3s16 p3d) +{ + MapBlock *block = getBlockNoCreateNoEx(p3d); + if(block == NULL) + throw InvalidPositionException(); + return block; } + /*MapBlock * Map::getBlockCreate(v3s16 p3d) { v2s16 p2d(p3d.X, p3d.Z); @@ -1422,9 +1415,8 @@ u32 Map::unloadUnusedData(float timeout, bool only_blocks, // Save if modified if(block->getModified() != MOD_STATE_CLEAN) saveBlock(block); - // Unload - sector->removeBlock(block); - delete block; + // Delete from memory + sector->deleteBlock(block); } else { @@ -3062,10 +3054,8 @@ void ServerMap::loadBlock(std::string sectordir, std::string blockfile, MapSecto MapBlock *block = NULL; bool created_new = false; - try{ - block = sector->getBlockNoCreate(p3d.Y); - } - catch(InvalidPositionException &e) + block = sector->getBlockNoCreateNoEx(p3d.Y); + if(block == NULL) { block = sector->createBlankBlockNoInsert(p3d.Y); created_new = true; @@ -3235,6 +3225,7 @@ MapSector * ClientMap::emergeSector(v2s16 p2d) return sector; } +#if 0 void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is) { DSTACK(__FUNCTION_NAME); @@ -3260,6 +3251,7 @@ void ClientMap::deSerializeSector(v2s16 p2d, std::istream &is) sector->deSerialize(is); } +#endif void ClientMap::OnRegisterSceneNode() { diff --git a/src/map.h b/src/map.h index 86b6b6e1..99593a58 100644 --- a/src/map.h +++ b/src/map.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define MAP_HEADER #include +#include #include #include @@ -35,12 +36,17 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #include "mapnode.h" #include "mapblock.h" -#include "mapsector.h" #include "constants.h" #include "voxel.h" #include "mapchunk.h" #include "nodemetadata.h" +class MapSector; +class ServerMapSector; +class ClientMapSector; + +class MapBlock; + namespace mapgen{ struct BlockMakeData; }; @@ -321,7 +327,6 @@ protected: core::map m_event_receivers; core::map m_sectors; - //JMutex m_sector_mutex; // Be sure to set this to NULL when the cached sector is deleted MapSector *m_sector_cache; @@ -547,7 +552,7 @@ public: */ MapSector * emergeSector(v2s16 p); - void deSerializeSector(v2s16 p2d, std::istream &is); + //void deSerializeSector(v2s16 p2d, std::istream &is); /* ISceneNode methods diff --git a/src/mapsector.cpp b/src/mapsector.cpp index 97101dd3..389714f4 100644 --- a/src/mapsector.cpp +++ b/src/mapsector.cpp @@ -21,15 +21,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "jmutexautolock.h" #include "client.h" #include "exceptions.h" +#include "mapblock.h" MapSector::MapSector(NodeContainer *parent, v2s16 pos): - differs_from_disk(true), + differs_from_disk(false), m_parent(parent), m_pos(pos), m_block_cache(NULL) { - m_mutex.Init(); - assert(m_mutex.IsInitialized()); } MapSector::~MapSector() @@ -39,8 +38,6 @@ MapSector::~MapSector() void MapSector::deleteBlocks() { - JMutexAutoLock lock(m_mutex); - // Clear cache m_block_cache = NULL; @@ -83,26 +80,12 @@ MapBlock * MapSector::getBlockBuffered(s16 y) MapBlock * MapSector::getBlockNoCreateNoEx(s16 y) { - JMutexAutoLock lock(m_mutex); - return getBlockBuffered(y); } -MapBlock * MapSector::getBlockNoCreate(s16 y) -{ - MapBlock *block = getBlockNoCreateNoEx(y); - - if(block == NULL) - throw InvalidPositionException(); - - return block; -} - MapBlock * MapSector::createBlankBlockNoInsert(s16 y) { - // There should not be a block at this position - if(getBlockBuffered(y) != NULL) - throw AlreadyExistsException("Block already exists"); + assert(getBlockBuffered(y) == NULL); v3s16 blockpos_map(m_pos.X, y, m_pos.Y); @@ -113,8 +96,6 @@ MapBlock * MapSector::createBlankBlockNoInsert(s16 y) MapBlock * MapSector::createBlankBlock(s16 y) { - JMutexAutoLock lock(m_mutex); - MapBlock *block = createBlankBlockNoInsert(y); m_blocks.insert(y, block); @@ -126,39 +107,34 @@ void MapSector::insertBlock(MapBlock *block) { s16 block_y = block->getPos().Y; - { - JMutexAutoLock lock(m_mutex); - - MapBlock *block2 = getBlockBuffered(block_y); - if(block2 != NULL){ - throw AlreadyExistsException("Block already exists"); - } - - v2s16 p2d(block->getPos().X, block->getPos().Z); - assert(p2d == m_pos); - - // Insert into container - m_blocks.insert(block_y, block); + MapBlock *block2 = getBlockBuffered(block_y); + if(block2 != NULL){ + throw AlreadyExistsException("Block already exists"); } + + v2s16 p2d(block->getPos().X, block->getPos().Z); + assert(p2d == m_pos); + + // Insert into container + m_blocks.insert(block_y, block); } -void MapSector::removeBlock(MapBlock *block) +void MapSector::deleteBlock(MapBlock *block) { s16 block_y = block->getPos().Y; - JMutexAutoLock lock(m_mutex); - // Clear from cache m_block_cache = NULL; // Remove from container m_blocks.remove(block_y); + + // Delete + delete block; } void MapSector::getBlocks(core::list &dest) { - JMutexAutoLock lock(m_mutex); - core::list ref_list; core::map::Iterator bi; @@ -184,15 +160,6 @@ ServerMapSector::~ServerMapSector() { } -f32 ServerMapSector::getGroundHeight(v2s16 p, bool generate) -{ - return GROUNDHEIGHT_NOTFOUND_SETVALUE; -} - -void ServerMapSector::setGroundHeight(v2s16 p, f32 y, bool generate) -{ -} - void ServerMapSector::serialize(std::ostream &os, u8 version) { if(!ser_ver_supported(version)) @@ -289,45 +256,6 @@ ClientMapSector::~ClientMapSector() { } -void ClientMapSector::deSerialize(std::istream &is) -{ - /* - [0] u8 serialization version - [1] s16 corners[0] - [3] s16 corners[1] - [5] s16 corners[2] - [7] s16 corners[3] - size = 9 - - In which corners are in these positions - v2s16(0,0), - v2s16(1,0), - v2s16(1,1), - v2s16(0,1), - */ - - // Read version - u8 version = SER_FMT_VER_INVALID; - is.read((char*)&version, 1); - - if(!ser_ver_supported(version)) - throw VersionMismatchException("ERROR: MapSector format not supported"); - - u8 buf[2]; - - // Dummy read corners - is.read((char*)buf, 2); - is.read((char*)buf, 2); - is.read((char*)buf, 2); - is.read((char*)buf, 2); - - /* - Set stuff in sector - */ - - // Nothing here - -} #endif // !SERVER //END diff --git a/src/mapsector.h b/src/mapsector.h index fda290cd..c5a41ca1 100644 --- a/src/mapsector.h +++ b/src/mapsector.h @@ -26,9 +26,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "common_irrlicht.h" -#include "mapblock.h" -//#include "heightmap.h" #include "exceptions.h" +#include + +class MapBlock; +class NodeContainer; /* This is an Y-wise stack of MapBlocks. @@ -37,18 +39,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #define MAPSECTOR_SERVER 0 #define MAPSECTOR_CLIENT 1 -class MapSector: public NodeContainer +class MapSector { public: MapSector(NodeContainer *parent, v2s16 pos); virtual ~MapSector(); - virtual u16 nodeContainerId() const - { - return NODECONTAINER_ID_MAPSECTOR; - } - virtual u32 getId() const = 0; void deleteBlocks(); @@ -59,167 +56,32 @@ public: } MapBlock * getBlockNoCreateNoEx(s16 y); - MapBlock * getBlockNoCreate(s16 y); MapBlock * createBlankBlockNoInsert(s16 y); MapBlock * createBlankBlock(s16 y); - //MapBlock * getBlock(s16 y, bool generate=true); void insertBlock(MapBlock *block); - // This is used to remove a dummy from the sector while generating it. - // Block is only removed from internal container, not deleted. - void removeBlock(MapBlock *block); + void deleteBlock(MapBlock *block); - /* - This might not be a thread-safe depending on the day. - See the implementation. - */ void getBlocks(core::list &dest); - /* - If all nodes in area can be accessed, returns true and - adds all blocks in area to blocks. - - If all nodes in area cannot be accessed, returns false. - - The implementation of this is quite slow - - if blocks==NULL; it is not accessed at all. - */ - bool isValidArea(v3s16 p_min_nodes, v3s16 p_max_nodes, - core::map *blocks) - { - core::map bs; - - v3s16 p_min = getNodeBlockPos(p_min_nodes); - v3s16 p_max = getNodeBlockPos(p_max_nodes); - if(p_min.X != 0 || p_min.Z != 0 - || p_max.X != 0 || p_max.Z != 0) - return false; - v3s16 y; - for(s16 y=p_min.Y; y<=p_max.Y; y++) - { - try{ - MapBlock *block = getBlockNoCreate(y); - if(block->isDummy()) - return false; - if(blocks!=NULL) - bs[y] = block; - } - catch(InvalidPositionException &e) - { - return false; - } - } - - if(blocks!=NULL) - { - for(core::map::Iterator i=bs.getIterator(); - i.atEnd()==false; i++) - { - MapBlock *block = i.getNode()->getValue(); - s16 y = i.getNode()->getKey(); - blocks->insert(y, block); - } - } - return true; - } - - void getBlocksInArea(v3s16 p_min_nodes, v3s16 p_max_nodes, - core::map &blocks) - { - v3s16 p_min = getNodeBlockPos(p_min_nodes); - v3s16 p_max = getNodeBlockPos(p_max_nodes); - v3s16 y; - for(s16 y=p_min.Y; y<=p_max.Y; y++) - { - try{ - MapBlock *block = getBlockNoCreate(y); - blocks.insert(block->getPos(), block); - } - catch(InvalidPositionException &e) - { - } - } - } - - // virtual from NodeContainer - bool isValidPosition(v3s16 p) - { - v3s16 blockpos = getNodeBlockPos(p); - - if(blockpos.X != 0 || blockpos.Z != 0) - return false; - - MapBlock *blockref; - try{ - blockref = getBlockNoCreate(blockpos.Y); - } - catch(InvalidPositionException &e) - { - return false; - } - - return true; - } - - // virtual from NodeContainer - MapNode getNode(v3s16 p) - { - v3s16 blockpos = getNodeBlockPos(p); - if(blockpos.X != 0 || blockpos.Z != 0) - throw InvalidPositionException - ("MapSector only allows Y"); - - MapBlock * blockref = getBlockNoCreate(blockpos.Y); - v3s16 relpos = p - blockpos*MAP_BLOCKSIZE; - - return blockref->getNode(relpos); - } - // virtual from NodeContainer - void setNode(v3s16 p, MapNode & n) - { - v3s16 blockpos = getNodeBlockPos(p); - if(blockpos.X != 0 || blockpos.Z != 0) - throw InvalidPositionException - ("MapSector only allows Y"); - - MapBlock * blockref = getBlockNoCreate(blockpos.Y); - v3s16 relpos = p - blockpos*MAP_BLOCKSIZE; - blockref->setNode(relpos, n); - } - - // DEPRECATED? - virtual f32 getGroundHeight(v2s16 p, bool generate=false) - { - return GROUNDHEIGHT_NOTFOUND_SETVALUE; - } - virtual void setGroundHeight(v2s16 p, f32 y, bool generate=false) - { - } - - // When true, sector metadata is changed from the one on disk - // (sector metadata = all but blocks) - // Basically, this should be changed to true in every setter method + // Always false at the moment, because sector contains no metadata. bool differs_from_disk; protected: // The pile of MapBlocks core::map m_blocks; - //JMutex m_blocks_mutex; // For public access functions NodeContainer *m_parent; // Position on parent (in MapBlock widths) v2s16 m_pos; - + + // Last-used block is cached here for quicker access. // Be sure to set this to NULL when the cached block is deleted MapBlock *m_block_cache; s16 m_block_cache_y; - // This is used for protecting m_blocks - JMutex m_mutex; - /* Private methods */ @@ -237,15 +99,12 @@ public: { return MAPSECTOR_SERVER; } - - // DEPRECATED? - f32 getGroundHeight(v2s16 p, bool generate=false); - void setGroundHeight(v2s16 p, f32 y, bool generate=false); /* These functions handle metadata. They do not handle blocks. */ + void serialize(std::ostream &os, u8 version); static ServerMapSector* deSerialize( @@ -270,16 +129,7 @@ public: return MAPSECTOR_CLIENT; } - void deSerialize(std::istream &is); - - /*s16 getCorner(u16 i) - { - return m_corners[i]; - }*/ - private: - // The ground height of the corners is stored in here - //s16 m_corners[4]; }; #endif diff --git a/src/test.cpp b/src/test.cpp index 7b86750d..9ea402b6 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "porting.h" #include "content_mapnode.h" +#include "mapsector.h" /* Asserts that the exception occurs @@ -641,13 +642,13 @@ struct TestMapSector // Create one with no heightmaps ServerMapSector sector(&parent, v2s16(1,1)); - EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0)); - EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(1)); + assert(sector.getBlockNoCreateNoEx(0) == 0); + assert(sector.getBlockNoCreateNoEx(1) == 0); MapBlock * bref = sector.createBlankBlock(-2); - EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0)); - assert(sector.getBlockNoCreate(-2) == bref); + assert(sector.getBlockNoCreateNoEx(0) == 0); + assert(sector.getBlockNoCreateNoEx(-2) == bref); //TODO: Check for AlreadyExistsException -- 2.30.2