1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

cleaned map stuff

This commit is contained in:
Perttu Ahola 2011-06-26 00:03:58 +03:00
parent a80025c352
commit cb130d9158
7 changed files with 67 additions and 285 deletions

View file

@ -26,9 +26,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <jmutex.h>
#include "common_irrlicht.h"
#include "mapblock.h"
//#include "heightmap.h"
#include "exceptions.h"
#include <ostream>
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<MapBlock*> &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<s16, MapBlock*> *blocks)
{
core::map<s16, MapBlock*> 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<s16, MapBlock*>::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<v3s16, MapBlock*> &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<s16, MapBlock*> 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