minetest/src/mapsector.cpp

131 lines
2.7 KiB
C++
Raw Permalink Normal View History

/*
2013-02-24 17:40:43 +00:00
Minetest
2013-02-24 18:38:45 +00: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-26 23:02:21 +00:00
#include "mapsector.h"
#include "exceptions.h"
2011-06-25 21:03:58 +00:00
#include "mapblock.h"
#include "serialization.h"
2010-11-26 23:02:21 +00:00
2011-11-14 19:41:30 +00:00
MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
2010-11-26 23:02:21 +00:00
m_parent(parent),
m_pos(pos),
m_gamedef(gamedef)
2010-11-26 23:02:21 +00:00
{
}
MapSector::~MapSector()
{
deleteBlocks();
}
void MapSector::deleteBlocks()
{
// Clear cache
m_block_cache = nullptr;
2010-11-26 23:02:21 +00:00
// Delete all
for (auto &block : m_blocks) {
delete block.second;
2010-11-26 23:02:21 +00:00
}
// Clear container
m_blocks.clear();
}
MapBlock * MapSector::getBlockBuffered(s16 y)
{
MapBlock *block;
if (m_block_cache && y == m_block_cache_y) {
2010-11-26 23:02:21 +00:00
return m_block_cache;
}
2010-11-26 23:02:21 +00: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-26 23:02:21 +00:00
// Cache the last result
m_block_cache_y = y;
m_block_cache = block;
2010-11-26 23:02:21 +00:00
return block;
}
MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
2010-11-26 23:02:21 +00:00
{
return getBlockBuffered(y);
}
2010-11-26 23:02:21 +00:00
MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
{
assert(getBlockBuffered(y) == NULL); // Pre-condition
2010-11-26 23:02:21 +00:00
v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
2011-11-14 19:41:30 +00:00
MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
2010-11-26 23:02:21 +00:00
return block;
}
MapBlock * MapSector::createBlankBlock(s16 y)
{
MapBlock *block = createBlankBlockNoInsert(y);
2012-12-20 17:19:49 +00:00
m_blocks[y] = block;
2010-11-26 23:02:21 +00:00
return block;
}
void MapSector::insertBlock(MapBlock *block)
{
s16 block_y = block->getPos().Y;
2011-06-25 21:03:58 +00:00
MapBlock *block2 = getBlockBuffered(block_y);
if (block2) {
2011-06-25 21:03:58 +00:00
throw AlreadyExistsException("Block already exists");
2010-11-26 23:02:21 +00:00
}
2011-06-25 21:03:58 +00:00
v2s16 p2d(block->getPos().X, block->getPos().Z);
assert(p2d == m_pos);
2011-06-25 21:03:58 +00:00
// Insert into container
2012-12-20 17:19:49 +00:00
m_blocks[block_y] = block;
2010-11-26 23:02:21 +00:00
}
2011-06-25 21:03:58 +00:00
void MapSector::deleteBlock(MapBlock *block)
2010-11-26 23:02:21 +00:00
{
s16 block_y = block->getPos().Y;
// Clear from cache
m_block_cache = nullptr;
2010-11-26 23:02:21 +00:00
// Remove from container
2012-12-20 17:19:49 +00:00
m_blocks.erase(block_y);
2011-06-25 21:03:58 +00:00
// Delete
delete block;
2010-11-26 23:02:21 +00:00
}
void MapSector::getBlocks(MapBlockVect &dest)
2010-11-26 23:02:21 +00:00
{
for (auto &block : m_blocks) {
dest.push_back(block.second);
2010-11-26 23:02:21 +00:00
}
}