/*
Minetest
Copyright (C) 2010-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.
*/
#include "map.h"
#include "mapsector.h"
#include "mapblock.h"
#include "filesys.h"
#include "voxel.h"
#include "voxelalgorithms.h"
#include "porting.h"
#include "serialization.h"
#include "nodemetadata.h"
#include "settings.h"
#include "log.h"
#include "profiler.h"
#include "nodedef.h"
#include "gamedef.h"
#include "util/directiontables.h"
#include "util/basic_macros.h"
#include "rollback_interface.h"
#include "environment.h"
#include "reflowscan.h"
#include "emerge.h"
#include "mapgen/mapgen_v6.h"
#include "mapgen/mg_biome.h"
#include "config.h"
#include "server.h"
#include "database/database.h"
#include "database/database-dummy.h"
#include "database/database-sqlite3.h"
#include "script/scripting_server.h"
#include <deque>
#include <queue>
#if USE_LEVELDB
#include "database/database-leveldb.h"
#endif
#if USE_REDIS
#include "database/database-redis.h"
#endif
#if USE_POSTGRESQL
#include "database/database-postgresql.h"
#endif
/*
Map
*/
Map::Map(IGameDef *gamedef):
m_gamedef(gamedef),
m_nodedef(gamedef->ndef())
{
}
Map::~Map()
{
/*
Free all MapSectors
*/
for (auto §or : m_sectors) {
delete sector.second;
}
}
void Map::addEventReceiver(MapEventReceiver *event_receiver)
{
m_event_receivers.insert(event_receiver);
}
void Map::removeEventReceiver(MapEventReceiver *event_receiver)
{
m_event_receivers.erase(event_receiver);
}
void Map::dispatchEvent(const MapEditEvent &event)
{
for (MapEventReceiver *event_receiver : m_event_receivers) {
event_receiver->onMapEditEvent(event);
}
}
MapSector * Map::getSectorNoGenerateNoLock(v2s16 p)
{
if(m_sector_cache != NULL && p == m_sector_cache_p){
MapSector * sector = m_sector_cache;
return sector;
}
auto n = m_sectors.find(p);
if (n == m_sectors.end())
return NULL;
MapSector *sector = n->second;
// Cache the last result
m_sector_cache_p = p;
m_sector_cache = sector;
return sector;
}
MapSector * Map::getSectorNoGenerate(v2s16 p)
{
return getSectorNoGenerateNoLock(p);
}
MapBlock * Map::getBlockNoCreateNoEx(v3s16 p3d)
{
v2s16 p2d(p3d.X, p3d.Z);
MapSector * sector = getSectorNoGenerate(p2d);
if(sector == NULL)
return NULL;
MapBlock *block = sector->getBlockNoCreateNoEx(p3d.Y);
return block;
}
MapBlock * Map::getBlockNoCreate(v3s16 p3d)
{
MapBlock *block = getBlockNoCreateNoEx(p3d);
if(block == NULL)
throw InvalidPositionException();
return block;
}
bool Map::isValidPosition(v3s16 p)
{
v3s16 blockpos = getNodeBlockPos(p);
MapBlock *block = getBlockNoCreateNoEx(blockpos);
return (block != NULL);
}
// Returns a CONTENT_IGNORE node if not found
MapNode Map::getNode(v3s16 p, bool *is_valid_position)
{
v3s16 blockpos = getNodeBlockPos(p);
MapBlock *block = getBlockNoCreateNoEx(blockpos);
if (block == NULL) {
if (is_valid_position != NULL)
*is_valid_position = false;
return {CONTENT_IGNORE};
}
v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
bool is_valid_p;
MapNode node = block->getNodeNoCheck(relpos, &is_valid_p);
if (is_valid_position != NULL)
*is_valid_position = is_valid_p;
return node;
}
|