minetest/src/mapsector.cpp

258 lines
4.7 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
}
Cherry-pick most commits since 15c0376 Commits not directly related to network changes were cherry-picked on a best-effort basis, as some cause difficult merge conflicts. Commits skipped over: 0d1eedcccc8b83fd5f5a9a75389fe8ac97d2c697 aa474e4501f460ba853dc70ff4d97cbd061e6704 82482ecd9d45036da38335f7dd96232450338a28 b214cde5b4a833e1826ec6850b95bf1938c8b0a0 2066655aae2022384fc12a10c04dccfd2996f0ac 7e088fdfe3c77083606bce955624aef1da59bb32 40bf1d7b5f2a190ae89885c9ea99f52c476ea6be 1b2f64473ed4f222d3b7f02df853730d4382105e 7e56637ed07d9f32b4bd9049009cf7e1e4cff884 64ff966bae99813229dff6629fd9131a91ba7484 51057a56f540f4e74b424e22c94357e5cb5268b2 009149a073ac02dd412af3c203979157976c0dd8 cf4045ff0f7c96614e64b2b336e62a240f443ea6 e357577cb2c18fe29610ed406e01d8d1fda6bb89 49f84b76bcacb6de9544ebaf0a05c9f76ccf33b1 b019221c30f58ce1944d8f2421b40b31ba364716 7880ff74b65040ccb90bc30ba520d4a4215d45a0 146f77fdb750833c649de7159a0833c398e14a4d a704c04f00bfea4b77550169fa08105c2ee0dfd0 Commits with conflicts: 038d3a31dfa6c382a5a5a57f2ce367d1cd67fb9f e9eda2b0d0019890cd404e4af25b7adf349e288f 708337dfc2b3871dc6de983e781e4a4a60a1881d ~~ modified client.cpp manually; shadow changes to packethandlers/client.cpp 36e8ba9ce2d9eafb1f36e76e86113ed47afe8f7f ~~ modified main.cpp manually; add ALLOW_ZWRITE_ON_TRANSPARENT set 3b6480c5b0c968ad9f5a7cfb7ca494989be03629 ~~ modified server.cpp manually; change wrapDegrees -> modulo360f 5a5854ea9d0bc346b00f48f40b538b7a8d68b37a c09d026f0561ee3c6db821a7e193716f724a0e4a 3c91ad8fc2b7a7888503e85d31bfe286afa6560d
2015-03-07 22:25:15 +01:00
void MapSector::getBlocks(MapBlockVect &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