2010-11-29 19:13:04 +01:00
|
|
|
/*
|
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>
|
2010-11-29 19:13:04 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 16:56:56 +02:00
|
|
|
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
|
2010-11-29 19:13:04 +01:00
|
|
|
(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
|
2012-06-05 16:56:56 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2010-11-29 19:13:04 +01:00
|
|
|
|
2012-06-05 16:56:56 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2010-11-29 19:13:04 +01:00
|
|
|
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"
|
2013-08-11 04:09:45 +02:00
|
|
|
#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),
|
2017-06-17 19:11:28 +02:00
|
|
|
m_gamedef(gamedef)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MapSector::~MapSector()
|
|
|
|
{
|
|
|
|
deleteBlocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapSector::deleteBlocks()
|
|
|
|
{
|
|
|
|
// Clear cache
|
2017-06-17 19:11:28 +02:00
|
|
|
m_block_cache = nullptr;
|
2010-11-27 00:02:21 +01:00
|
|
|
|
|
|
|
// Delete all
|
2017-08-18 18:18:25 +02:00
|
|
|
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;
|
|
|
|
|
2017-06-17 19:11:28 +02:00
|
|
|
if (m_block_cache && y == m_block_cache_y) {
|
2010-11-27 00:02:21 +01:00
|
|
|
return m_block_cache;
|
|
|
|
}
|
2015-08-10 22:24:47 +02:00
|
|
|
|
2010-11-27 00:02:21 +01:00
|
|
|
// If block doesn't exist, return NULL
|
2017-06-04 21:00:04 +02:00
|
|
|
std::unordered_map<s16, MapBlock*>::const_iterator n = m_blocks.find(y);
|
2017-06-17 19:11:28 +02:00
|
|
|
block = (n != m_blocks.end() ? n->second : nullptr);
|
2015-08-10 22:24:47 +02:00
|
|
|
|
2010-11-27 00:02:21 +01:00
|
|
|
// Cache the last result
|
|
|
|
m_block_cache_y = y;
|
|
|
|
m_block_cache = block;
|
2015-08-10 22:24:47 +02:00
|
|
|
|
2010-11-27 00:02:21 +01:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2011-01-30 00:44:54 +01:00
|
|
|
MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
2011-01-30 00:44:54 +01:00
|
|
|
return getBlockBuffered(y);
|
|
|
|
}
|
|
|
|
|
2010-11-27 00:02:21 +01:00
|
|
|
MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
|
|
|
|
{
|
2015-03-06 11:21:51 +01:00
|
|
|
assert(getBlockBuffered(y) == NULL); // Pre-condition
|
2010-11-27 00:02:21 +01:00
|
|
|
|
|
|
|
v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
|
2015-08-10 22:24:47 +02:00
|
|
|
|
2011-11-14 20:41:30 +01:00
|
|
|
MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
|
2015-08-10 22:24:47 +02:00
|
|
|
|
2010-11-27 00:02:21 +01:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
MapBlock * MapSector::createBlankBlock(s16 y)
|
|
|
|
{
|
|
|
|
MapBlock *block = createBlankBlockNoInsert(y);
|
2015-08-10 22:24:47 +02:00
|
|
|
|
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);
|
2017-06-17 19:11:28 +02:00
|
|
|
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);
|
2015-08-10 22:24:47 +02:00
|
|
|
|
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
|
2017-06-17 19:11:28 +02:00
|
|
|
m_block_cache = nullptr;
|
2015-08-10 22:24:47 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-17 15:28:49 +01:00
|
|
|
void MapSector::getBlocks(MapBlockVect &dest)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
2017-08-18 18:18:25 +02:00
|
|
|
for (auto &block : m_blocks) {
|
|
|
|
dest.push_back(block.second);
|
2010-11-27 00:02:21 +01:00
|
|
|
}
|
|
|
|
}
|