minetest/src/mapsector.cpp

132 lines
2.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):
2010-11-27 00:02:21 +01:00
m_parent(parent),
m_pos(pos),
m_gamedef(gamedef)
2010-11-27 00:02:21 +01:00
{
}
MapSector::~MapSector()
{
deleteBlocks();
}
void MapSector::deleteBlocks()
{
// Clear cache
m_block_cache = nullptr;
2010-11-27 00:02:21 +01:00
// Delete all
for (auto &block : m_blocks) {
delete block.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 && y == m_block_cache_y) {
2010-11-27 00:02:21 +01:00
return m_block_cache;
}
2010-11-27 00:02:21 +01:00
// If block doesn't exist, return NULL
std::unordered_map<s16, MapBlock*>::const_iterator n = m_blocks.find(y);
block = (n != m_blocks.end() ? n->second : nullptr);
2010-11-27 00:02:21 +01:00
// Cache the last result
m_block_cache_y = y;
m_block_cache = block;
2010-11-27 00:02:21 +01:00
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)
{
assert(getBlockBuffered(y) == NULL); // Pre-condition
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) {
2011-06-25 23:03:58 +02:00
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);
2011-06-25 23:03:58 +02:00
// 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 = nullptr;
2010-11-27 00:02:21 +01:00
// 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
}
void MapSector::getBlocks(MapBlockVect &dest)
2010-11-27 00:02:21 +01:00
{
dest.reserve(dest.size() + m_blocks.size());
for (auto &block : m_blocks) {
dest.push_back(block.second);
2010-11-27 00:02:21 +01:00
}
}