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
|
|
|
|
2023-03-03 15:20:29 +01:00
|
|
|
// Delete all blocks
|
2010-11-27 00:02:21 +01:00
|
|
|
m_blocks.clear();
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:20:29 +01:00
|
|
|
MapBlock *MapSector::getBlockBuffered(s16 y)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
|
|
|
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
|
2023-03-03 15:20:29 +01:00
|
|
|
auto it = m_blocks.find(y);
|
|
|
|
block = it != m_blocks.end() ? it->second.get() : 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;
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:20:29 +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);
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:20:29 +01:00
|
|
|
std::unique_ptr<MapBlock> MapSector::createBlankBlockNoInsert(s16 y)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
2023-03-03 15:20:29 +01:00
|
|
|
assert(getBlockBuffered(y) == nullptr); // Pre-condition
|
2010-11-27 00:02:21 +01:00
|
|
|
|
2024-01-30 21:51:51 +01:00
|
|
|
if (blockpos_over_max_limit(v3s16(0, y, 0)))
|
|
|
|
throw InvalidPositionException("createBlankBlockNoInsert(): pos over max mapgen limit");
|
|
|
|
|
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
|
|
|
|
2023-12-12 15:43:39 +01:00
|
|
|
return std::make_unique<MapBlock>(blockpos_map, m_gamedef);
|
2010-11-27 00:02:21 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 15:20:29 +01:00
|
|
|
MapBlock *MapSector::createBlankBlock(s16 y)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
2023-03-03 15:20:29 +01:00
|
|
|
std::unique_ptr<MapBlock> block_u = createBlankBlockNoInsert(y);
|
|
|
|
MapBlock *block = block_u.get();
|
2015-08-10 22:24:47 +02:00
|
|
|
|
2023-03-03 15:20:29 +01:00
|
|
|
m_blocks[y] = std::move(block_u);
|
2010-11-27 00:02:21 +01:00
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:20:29 +01:00
|
|
|
void MapSector::insertBlock(std::unique_ptr<MapBlock> block)
|
2010-11-27 00:02:21 +01:00
|
|
|
{
|
|
|
|
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
|
2023-03-03 15:20:29 +01:00
|
|
|
m_blocks[block_y] = std::move(block);
|
2010-11-27 00:02:21 +01:00
|
|
|
}
|
|
|
|
|
2011-06-25 23:03:58 +02:00
|
|
|
void MapSector::deleteBlock(MapBlock *block)
|
2023-03-24 12:34:44 +01:00
|
|
|
{
|
|
|
|
detachBlock(block);
|
2023-03-03 15:20:29 +01:00
|
|
|
// returned smart-ptr is dropped
|
2023-03-24 12:34:44 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 15:20:29 +01:00
|
|
|
std::unique_ptr<MapBlock> MapSector::detachBlock(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
|
2023-03-03 15:20:29 +01:00
|
|
|
auto it = m_blocks.find(block_y);
|
|
|
|
assert(it != m_blocks.end());
|
|
|
|
std::unique_ptr<MapBlock> ret = std::move(it->second);
|
|
|
|
assert(ret.get() == block);
|
|
|
|
m_blocks.erase(it);
|
2011-06-25 23:03:58 +02:00
|
|
|
|
2023-03-24 12:34:44 +01:00
|
|
|
// Mark as removed
|
|
|
|
block->makeOrphan();
|
2023-03-03 15:20:29 +01:00
|
|
|
|
|
|
|
return ret;
|
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
|
|
|
{
|
2023-03-12 17:37:47 +01:00
|
|
|
dest.reserve(dest.size() + m_blocks.size());
|
2017-08-18 18:18:25 +02:00
|
|
|
for (auto &block : m_blocks) {
|
2023-03-03 15:20:29 +01:00
|
|
|
dest.push_back(block.second.get());
|
2010-11-27 00:02:21 +01:00
|
|
|
}
|
|
|
|
}
|