1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-11-15 13:55:11 -05:00
minetest/src/mapsector.cpp

262 lines
4.7 KiB
C++
Raw Normal View History

/*
Minetest-c55
Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2010-11-26 18:02:21 -05:00
#include "mapsector.h"
#include "jmutexautolock.h"
#include "client.h"
#include "exceptions.h"
2011-06-25 17:03:58 -04:00
#include "mapblock.h"
2010-11-26 18:02:21 -05:00
2011-06-25 18:31:43 -04:00
MapSector::MapSector(Map *parent, v2s16 pos):
2011-06-25 17:03:58 -04:00
differs_from_disk(false),
2010-11-26 18:02:21 -05:00
m_parent(parent),
m_pos(pos),
m_block_cache(NULL)
{
}
MapSector::~MapSector()
{
deleteBlocks();
}
void MapSector::deleteBlocks()
{
// Clear cache
m_block_cache = NULL;
// Delete all
core::map<s16, MapBlock*>::Iterator i = m_blocks.getIterator();
for(; i.atEnd() == false; i++)
{
delete i.getNode()->getValue();
}
// Clear container
m_blocks.clear();
}
MapBlock * MapSector::getBlockBuffered(s16 y)
{
MapBlock *block;
if(m_block_cache != NULL && y == m_block_cache_y){
return m_block_cache;
}
// If block doesn't exist, return NULL
core::map<s16, MapBlock*>::Node *n = m_blocks.find(y);
if(n == NULL)
{
block = NULL;
}
// If block exists, return it
else{
block = n->getValue();
}
// Cache the last result
m_block_cache_y = y;
m_block_cache = block;
return block;
}
MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
2010-11-26 18:02:21 -05:00
{
return getBlockBuffered(y);
}
2010-11-26 18:02:21 -05:00
MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
{
2011-06-25 17:03:58 -04:00
assert(getBlockBuffered(y) == NULL);
2010-11-26 18:02:21 -05:00
v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
MapBlock *block = new MapBlock(m_parent, blockpos_map);
return block;
}
MapBlock * MapSector::createBlankBlock(s16 y)
{
MapBlock *block = createBlankBlockNoInsert(y);
m_blocks.insert(y, block);
return block;
}
void MapSector::insertBlock(MapBlock *block)
{
s16 block_y = block->getPos().Y;
2011-06-25 17:03:58 -04:00
MapBlock *block2 = getBlockBuffered(block_y);
if(block2 != NULL){
throw AlreadyExistsException("Block already exists");
2010-11-26 18:02:21 -05:00
}
2011-06-25 17:03:58 -04:00
v2s16 p2d(block->getPos().X, block->getPos().Z);
assert(p2d == m_pos);
// Insert into container
m_blocks.insert(block_y, block);
2010-11-26 18:02:21 -05:00
}
2011-06-25 17:03:58 -04:00
void MapSector::deleteBlock(MapBlock *block)
2010-11-26 18:02:21 -05:00
{
s16 block_y = block->getPos().Y;
// Clear from cache
m_block_cache = NULL;
// Remove from container
m_blocks.remove(block_y);
2011-06-25 17:03:58 -04:00
// Delete
delete block;
2010-11-26 18:02:21 -05:00
}
void MapSector::getBlocks(core::list<MapBlock*> &dest)
{
core::list<MapBlock*> ref_list;
core::map<s16, MapBlock*>::Iterator bi;
bi = m_blocks.getIterator();
for(; bi.atEnd() == false; bi++)
{
MapBlock *b = bi.getNode()->getValue();
dest.push_back(b);
}
}
/*
ServerMapSector
*/
2011-06-25 18:31:43 -04:00
ServerMapSector::ServerMapSector(Map *parent, v2s16 pos):
2011-02-05 07:55:16 -05:00
MapSector(parent, pos)
2010-11-26 18:02:21 -05:00
{
}
ServerMapSector::~ServerMapSector()
{
}
void ServerMapSector::serialize(std::ostream &os, u8 version)
{
if(!ser_ver_supported(version))
throw VersionMismatchException("ERROR: MapSector format not supported");
/*
[0] u8 serialization version
+ heightmap data
*/
// Server has both of these, no need to support not having them.
2011-02-05 07:55:16 -05:00
//assert(m_objects != NULL);
2010-11-26 18:02:21 -05:00
// Write version
os.write((char*)&version, 1);
/*
2011-02-05 07:55:16 -05:00
Add stuff here, if needed
2010-11-26 18:02:21 -05:00
*/
}
ServerMapSector* ServerMapSector::deSerialize(
std::istream &is,
2011-06-25 18:31:43 -04:00
Map *parent,
2010-11-26 18:02:21 -05:00
v2s16 p2d,
core::map<v2s16, MapSector*> & sectors
)
{
/*
[0] u8 serialization version
+ heightmap data
*/
/*
Read stuff
*/
// 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");
/*
2011-02-05 07:55:16 -05:00
Add necessary reading stuff here
2010-11-26 18:02:21 -05:00
*/
/*
Get or create sector
*/
ServerMapSector *sector = NULL;
core::map<v2s16, MapSector*>::Node *n = sectors.find(p2d);
if(n != NULL)
{
dstream<<"WARNING: deSerializing existent sectors not supported "
2010-11-26 18:02:21 -05:00
"at the moment, because code hasn't been tested."
<<std::endl;
MapSector *sector = n->getValue();
assert(sector->getId() == MAPSECTOR_SERVER);
return (ServerMapSector*)sector;
2010-11-26 18:02:21 -05:00
}
else
{
2011-02-05 07:55:16 -05:00
sector = new ServerMapSector(parent, p2d);
2010-11-26 18:02:21 -05:00
sectors.insert(p2d, sector);
}
/*
Set stuff in sector
*/
2011-02-05 07:55:16 -05:00
// Nothing here
2010-11-26 18:02:21 -05:00
return sector;
}
#ifndef SERVER
2010-11-26 18:02:21 -05:00
/*
ClientMapSector
*/
2011-06-25 18:31:43 -04:00
ClientMapSector::ClientMapSector(Map *parent, v2s16 pos):
2010-11-26 18:02:21 -05:00
MapSector(parent, pos)
{
}
ClientMapSector::~ClientMapSector()
{
}
#endif // !SERVER
2010-11-26 18:02:21 -05:00
//END