minetest/src/mapsector.cpp

258 lines
4.8 KiB
C++
Raw Normal View History

/*
2013-02-24 18:40:43 +01:00
Minetest
2013-02-24 19:38:45 +01:00
Copyright (C) 2013 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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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-27 00:02:21 +01:00
#include "mapsector.h"
#include "exceptions.h"
2011-06-25 23:03:58 +02:00
#include "mapblock.h"
#include "serialization.h"
2010-11-27 00:02:21 +01:00
2011-11-14 20:41:30 +01:00
MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
2011-06-25 23:03:58 +02:00
differs_from_disk(false),
2010-11-27 00:02:21 +01:00
m_parent(parent),
m_pos(pos),
2011-11-14 20:41:30 +01:00
m_gamedef(gamedef),
2010-11-27 00:02:21 +01:00
m_block_cache(NULL)
{
}
MapSector::~MapSector()
{
deleteBlocks();
}
void MapSector::deleteBlocks()
{
// Clear cache
m_block_cache = NULL;
// Delete all
2012-12-20 18:19:49 +01:00
for(std::map<s16, MapBlock*>::iterator i = m_blocks.begin();
i != m_blocks.end(); ++i)
2010-11-27 00:02:21 +01:00
{
2012-12-20 18:19:49 +01:00
delete i->second;
2010-11-27 00:02:21 +01:00
}
// 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
2012-12-20 18:19:49 +01:00
std::map<s16, MapBlock*>::iterator n = m_blocks.find(y);
if(n == m_blocks.end())
2010-11-27 00:02:21 +01:00
{
block = NULL;
}
// If block exists, return it
else{
2012-12-20 18:19:49 +01:00
block = n->second;
2010-11-27 00:02:21 +01:00
}
// Cache the last result
m_block_cache_y = y;
m_block_cache = block;
return block;
}
MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
2010-11-27 00:02:21 +01:00
{
return getBlockBuffered(y);
}
2010-11-27 00:02:21 +01:00
MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
{
2011-06-25 23:03:58 +02:00
assert(getBlockBuffered(y) == NULL);
2010-11-27 00:02:21 +01:00
v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
2011-11-14 20:41:30 +01:00
MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
2010-11-27 00:02:21 +01:00
return block;
}
MapBlock * MapSector::createBlankBlock(s16 y)
{
MapBlock *block = createBlankBlockNoInsert(y);
2012-12-20 18:19:49 +01:00
m_blocks[y] = block;
2010-11-27 00:02:21 +01:00
return block;
}
void MapSector::insertBlock(MapBlock *block)
{
s16 block_y = block->getPos().Y;
2011-06-25 23:03:58 +02:00
MapBlock *block2 = getBlockBuffered(block_y);
if(block2 != NULL){
throw AlreadyExistsException("Block already exists");
2010-11-27 00:02:21 +01:00
}
2011-06-25 23:03:58 +02:00
v2s16 p2d(block->getPos().X, block->getPos().Z);
assert(p2d == m_pos);
// Insert into container
2012-12-20 18:19:49 +01:00
m_blocks[block_y] = block;
2010-11-27 00:02:21 +01:00
}
2011-06-25 23:03:58 +02:00
void MapSector::deleteBlock(MapBlock *block)
2010-11-27 00:02:21 +01:00
{
s16 block_y = block->getPos().Y;
// Clear from cache
m_block_cache = NULL;
// Remove from container
2012-12-20 18:19:49 +01:00
m_blocks.erase(block_y);
2011-06-25 23:03:58 +02:00
// Delete
delete block;
2010-11-27 00:02:21 +01:00
}
2012-12-20 18:19:49 +01:00
void MapSector::getBlocks(std::list<MapBlock*> &dest)
2010-11-27 00:02:21 +01:00
{
2012-12-20 18:19:49 +01:00
for(std::map<s16, MapBlock*>::iterator bi = m_blocks.begin();
bi != m_blocks.end(); ++bi)
2010-11-27 00:02:21 +01:00
{
2012-12-20 18:19:49 +01:00
dest.push_back(bi->second);
2010-11-27 00:02:21 +01:00
}
}
/*
ServerMapSector
*/
2011-11-14 20:41:30 +01:00
ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
MapSector(parent, pos, gamedef)
2010-11-27 00:02:21 +01: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 13:55:16 +01:00
//assert(m_objects != NULL);
2010-11-27 00:02:21 +01:00
// Write version
os.write((char*)&version, 1);
/*
2011-02-05 13:55:16 +01:00
Add stuff here, if needed
2010-11-27 00:02:21 +01:00
*/
}
ServerMapSector* ServerMapSector::deSerialize(
std::istream &is,
2011-06-26 00:31:43 +02:00
Map *parent,
2010-11-27 00:02:21 +01:00
v2s16 p2d,
2012-12-20 18:19:49 +01:00
std::map<v2s16, MapSector*> & sectors,
2011-11-14 20:41:30 +01:00
IGameDef *gamedef
2010-11-27 00:02:21 +01:00
)
{
/*
[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 13:55:16 +01:00
Add necessary reading stuff here
2010-11-27 00:02:21 +01:00
*/
/*
Get or create sector
*/
ServerMapSector *sector = NULL;
2012-12-20 18:19:49 +01:00
std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
2010-11-27 00:02:21 +01:00
2012-12-20 18:19:49 +01:00
if(n != sectors.end())
2010-11-27 00:02:21 +01:00
{
dstream<<"WARNING: deSerializing existent sectors not supported "
2010-11-27 00:02:21 +01:00
"at the moment, because code hasn't been tested."
<<std::endl;
2012-12-20 18:19:49 +01:00
MapSector *sector = n->second;
assert(sector->getId() == MAPSECTOR_SERVER);
return (ServerMapSector*)sector;
2010-11-27 00:02:21 +01:00
}
else
{
2011-11-14 20:41:30 +01:00
sector = new ServerMapSector(parent, p2d, gamedef);
2012-12-20 18:19:49 +01:00
sectors[p2d] = sector;
2010-11-27 00:02:21 +01:00
}
/*
Set stuff in sector
*/
2011-02-05 13:55:16 +01:00
// Nothing here
2010-11-27 00:02:21 +01:00
return sector;
}
#ifndef SERVER
2010-11-27 00:02:21 +01:00
/*
ClientMapSector
*/
2011-11-14 20:41:30 +01:00
ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
MapSector(parent, pos, gamedef)
2010-11-27 00:02:21 +01:00
{
}
ClientMapSector::~ClientMapSector()
{
}
#endif // !SERVER
2010-11-27 00:02:21 +01:00
//END