2013-02-14 04:43:15 +01:00
|
|
|
/*
|
2013-04-06 17:19:59 +02:00
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
|
2013-02-14 04:43:15 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "emerge.h"
|
2015-10-04 08:54:25 +02:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <queue>
|
2015-10-04 08:54:25 +02:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "util/container.h"
|
|
|
|
#include "util/thread.h"
|
2015-10-04 08:54:25 +02:00
|
|
|
#include "threading/event.h"
|
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
#include "config.h"
|
2015-10-04 08:54:25 +02:00
|
|
|
#include "constants.h"
|
|
|
|
#include "environment.h"
|
2013-02-14 04:43:15 +01:00
|
|
|
#include "log.h"
|
2015-10-04 08:54:25 +02:00
|
|
|
#include "map.h"
|
|
|
|
#include "mapblock.h"
|
2015-10-30 03:45:38 +01:00
|
|
|
#include "mapgen_flat.h"
|
2015-04-14 05:38:01 +02:00
|
|
|
#include "mapgen_fractal.h"
|
2014-10-16 13:45:55 +02:00
|
|
|
#include "mapgen_v5.h"
|
2013-02-14 04:43:15 +01:00
|
|
|
#include "mapgen_v6.h"
|
2013-04-06 17:19:59 +02:00
|
|
|
#include "mapgen_v7.h"
|
2016-01-14 04:57:02 +01:00
|
|
|
#include "mapgen_valleys.h"
|
2013-03-22 18:16:51 +01:00
|
|
|
#include "mapgen_singlenode.h"
|
2015-10-04 08:54:25 +02:00
|
|
|
#include "mg_biome.h"
|
|
|
|
#include "mg_ore.h"
|
|
|
|
#include "mg_decoration.h"
|
|
|
|
#include "mg_schematic.h"
|
|
|
|
#include "nodedef.h"
|
|
|
|
#include "profiler.h"
|
|
|
|
#include "scripting_game.h"
|
|
|
|
#include "server.h"
|
|
|
|
#include "serverobject.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "voxel.h"
|
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2014-12-30 03:23:51 +01:00
|
|
|
struct MapgenDesc {
|
|
|
|
const char *name;
|
|
|
|
MapgenFactory *factory;
|
2015-10-04 22:37:03 +02:00
|
|
|
bool is_user_visible;
|
2014-12-30 03:23:51 +01:00
|
|
|
};
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
class EmergeThread : public Thread {
|
2013-08-11 04:09:45 +02:00
|
|
|
public:
|
|
|
|
bool enable_mapgen_debug_info;
|
|
|
|
int id;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
EmergeThread(Server *server, int ethreadid);
|
|
|
|
~EmergeThread();
|
2013-08-11 04:09:45 +02:00
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
void *run();
|
2015-10-04 08:54:25 +02:00
|
|
|
void signal();
|
|
|
|
|
|
|
|
// Requires queue mutex held
|
|
|
|
bool pushBlock(v3s16 pos);
|
|
|
|
|
|
|
|
void cancelPendingItems();
|
|
|
|
|
|
|
|
static void runCompletionCallbacks(
|
|
|
|
v3s16 pos, EmergeAction action,
|
|
|
|
const EmergeCallbackList &callbacks);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Server *m_server;
|
|
|
|
ServerMap *m_map;
|
|
|
|
EmergeManager *m_emerge;
|
|
|
|
Mapgen *m_mapgen;
|
|
|
|
|
|
|
|
Event m_queue_event;
|
|
|
|
std::queue<v3s16> m_block_queue;
|
|
|
|
|
|
|
|
bool popBlockEmerge(v3s16 *pos, BlockEmergeData *bedata);
|
|
|
|
|
|
|
|
EmergeAction getBlockOrStartGen(
|
|
|
|
v3s16 pos, bool allow_gen, MapBlock **block, BlockMakeData *data);
|
|
|
|
MapBlock *finishGen(v3s16 pos, BlockMakeData *bmdata,
|
|
|
|
std::map<v3s16, MapBlock *> *modified_blocks);
|
|
|
|
|
|
|
|
friend class EmergeManager;
|
2013-08-11 04:09:45 +02:00
|
|
|
};
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
////
|
|
|
|
//// Built-in mapgens
|
|
|
|
////
|
|
|
|
|
|
|
|
MapgenDesc g_reg_mapgens[] = {
|
2015-10-04 22:37:03 +02:00
|
|
|
{"v5", new MapgenFactoryV5, true},
|
|
|
|
{"v6", new MapgenFactoryV6, true},
|
|
|
|
{"v7", new MapgenFactoryV7, true},
|
2016-01-10 04:27:32 +01:00
|
|
|
{"flat", new MapgenFactoryFlat, true},
|
2015-11-21 21:22:45 +01:00
|
|
|
{"fractal", new MapgenFactoryFractal, true},
|
2016-01-14 04:57:02 +01:00
|
|
|
{"valleys", new MapgenFactoryValleys, true},
|
2015-10-04 22:37:03 +02:00
|
|
|
{"singlenode", new MapgenFactorySinglenode, false},
|
2015-10-04 08:54:25 +02:00
|
|
|
};
|
2013-08-11 04:09:45 +02:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
////
|
|
|
|
//// EmergeManager
|
|
|
|
////
|
2013-02-24 04:45:12 +01:00
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
EmergeManager::EmergeManager(IGameDef *gamedef)
|
|
|
|
{
|
2014-11-13 05:01:13 +01:00
|
|
|
this->ndef = gamedef->getNodeDefManager();
|
|
|
|
this->biomemgr = new BiomeManager(gamedef);
|
|
|
|
this->oremgr = new OreManager(gamedef);
|
|
|
|
this->decomgr = new DecorationManager(gamedef);
|
|
|
|
this->schemmgr = new SchematicManager(gamedef);
|
2014-12-06 10:18:04 +01:00
|
|
|
this->gen_notify_on = 0;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-01-26 07:12:18 +01:00
|
|
|
// Note that accesses to this variable are not synchronized.
|
|
|
|
// This is because the *only* thread ever starting or stopping
|
|
|
|
// EmergeThreads should be the ServerThread.
|
2015-10-04 08:54:25 +02:00
|
|
|
this->m_threads_active = false;
|
2014-01-26 07:12:18 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
// If unspecified, leave a proc for the main thread and one for
|
2014-02-04 04:42:10 +01:00
|
|
|
// some other misc thread
|
2014-09-12 00:22:05 +02:00
|
|
|
s16 nthreads = 0;
|
2014-02-05 06:17:53 +01:00
|
|
|
if (!g_settings->getS16NoEx("num_emerge_threads", nthreads))
|
2015-04-07 12:13:12 +02:00
|
|
|
nthreads = Thread::getNumberOfProcessors() - 2;
|
2013-02-17 07:47:49 +01:00
|
|
|
if (nthreads < 1)
|
|
|
|
nthreads = 1;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
m_qlimit_total = g_settings->getU16("emergequeue_limit_total");
|
|
|
|
if (!g_settings->getU16NoEx("emergequeue_limit_diskonly", m_qlimit_diskonly))
|
|
|
|
m_qlimit_diskonly = nthreads * 5 + 1;
|
|
|
|
if (!g_settings->getU16NoEx("emergequeue_limit_generate", m_qlimit_generate))
|
|
|
|
m_qlimit_generate = nthreads + 1;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-02-05 06:17:53 +01:00
|
|
|
// don't trust user input for something very important like this
|
2015-10-04 08:54:25 +02:00
|
|
|
if (m_qlimit_total < 1)
|
|
|
|
m_qlimit_total = 1;
|
|
|
|
if (m_qlimit_diskonly < 1)
|
|
|
|
m_qlimit_diskonly = 1;
|
|
|
|
if (m_qlimit_generate < 1)
|
|
|
|
m_qlimit_generate = 1;
|
2014-02-05 06:17:53 +01:00
|
|
|
|
2014-09-12 00:22:05 +02:00
|
|
|
for (s16 i = 0; i < nthreads; i++)
|
2015-10-04 08:54:25 +02:00
|
|
|
m_threads.push_back(new EmergeThread((Server *)gamedef, i));
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-17 07:47:49 +01:00
|
|
|
infostream << "EmergeManager: using " << nthreads << " threads" << std::endl;
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
EmergeManager::~EmergeManager()
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
for (u32 i = 0; i != m_threads.size(); i++) {
|
|
|
|
EmergeThread *thread = m_threads[i];
|
|
|
|
|
|
|
|
if (m_threads_active) {
|
|
|
|
thread->stop();
|
|
|
|
thread->signal();
|
|
|
|
thread->wait();
|
2014-01-26 07:12:18 +01:00
|
|
|
}
|
2015-10-04 08:54:25 +02:00
|
|
|
|
|
|
|
delete thread;
|
|
|
|
delete m_mapgens[i];
|
2013-02-17 07:47:49 +01:00
|
|
|
}
|
2013-04-07 21:01:19 +02:00
|
|
|
|
2014-11-13 05:01:13 +01:00
|
|
|
delete biomemgr;
|
|
|
|
delete oremgr;
|
|
|
|
delete decomgr;
|
|
|
|
delete schemmgr;
|
2014-07-06 15:41:56 +02:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
delete params.sparams;
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
void EmergeManager::loadMapgenParams()
|
|
|
|
{
|
2015-01-26 12:44:49 +01:00
|
|
|
params.load(*g_settings);
|
2014-02-16 00:20:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
void EmergeManager::initMapgens()
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
if (m_mapgens.size())
|
2013-02-14 04:43:15 +01:00
|
|
|
return;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
MapgenFactory *mgfactory = getMapgenFactory(params.mg_name);
|
|
|
|
if (!mgfactory) {
|
|
|
|
errorstream << "EmergeManager: mapgen " << params.mg_name <<
|
|
|
|
" not registered; falling back to " << DEFAULT_MAPGEN << std::endl;
|
|
|
|
|
|
|
|
params.mg_name = DEFAULT_MAPGEN;
|
|
|
|
|
|
|
|
mgfactory = getMapgenFactory(params.mg_name);
|
|
|
|
FATAL_ERROR_IF(mgfactory == NULL, "Couldn't use any mapgen!");
|
|
|
|
}
|
|
|
|
|
2014-02-04 04:42:10 +01:00
|
|
|
if (!params.sparams) {
|
2015-10-04 08:54:25 +02:00
|
|
|
params.sparams = mgfactory->createMapgenParams();
|
2014-02-04 04:42:10 +01:00
|
|
|
params.sparams->readParams(g_settings);
|
2013-06-27 23:06:52 +02:00
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
for (u32 i = 0; i != m_threads.size(); i++) {
|
|
|
|
Mapgen *mg = mgfactory->createMapgen(i, ¶ms, this);
|
|
|
|
m_mapgens.push_back(mg);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
Mapgen *EmergeManager::getCurrentMapgen()
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
for (u32 i = 0; i != m_threads.size(); i++) {
|
2015-10-17 05:43:29 +02:00
|
|
|
if (m_threads[i]->isCurrentThread())
|
2015-10-04 08:54:25 +02:00
|
|
|
return m_threads[i]->m_mapgen;
|
2013-06-26 23:19:39 +02:00
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-06-26 23:19:39 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
void EmergeManager::startThreads()
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
if (m_threads_active)
|
2014-01-26 07:12:18 +01:00
|
|
|
return;
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
for (u32 i = 0; i != m_threads.size(); i++)
|
|
|
|
m_threads[i]->start();
|
2014-01-26 07:12:18 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
m_threads_active = true;
|
2014-01-26 07:12:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
void EmergeManager::stopThreads()
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
if (!m_threads_active)
|
2014-01-26 07:12:18 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Request thread stop in parallel
|
2015-10-04 08:54:25 +02:00
|
|
|
for (u32 i = 0; i != m_threads.size(); i++) {
|
|
|
|
m_threads[i]->stop();
|
|
|
|
m_threads[i]->signal();
|
2014-01-26 07:12:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then do the waiting for each
|
2015-10-04 08:54:25 +02:00
|
|
|
for (u32 i = 0; i != m_threads.size(); i++)
|
|
|
|
m_threads[i]->wait();
|
2014-01-26 07:12:18 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
m_threads_active = false;
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 23:26:08 +02:00
|
|
|
bool EmergeManager::isRunning()
|
|
|
|
{
|
|
|
|
return m_threads_active;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
bool EmergeManager::enqueueBlockEmerge(
|
|
|
|
u16 peer_id,
|
|
|
|
v3s16 blockpos,
|
|
|
|
bool allow_generate,
|
|
|
|
bool ignore_queue_limits)
|
2014-12-29 18:58:55 +01:00
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
u16 flags = 0;
|
2013-02-14 04:43:15 +01:00
|
|
|
if (allow_generate)
|
2015-10-04 08:54:25 +02:00
|
|
|
flags |= BLOCK_EMERGE_ALLOW_GEN;
|
|
|
|
if (ignore_queue_limits)
|
|
|
|
flags |= BLOCK_EMERGE_FORCE_QUEUE;
|
2013-02-15 00:32:56 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
return enqueueBlockEmergeEx(blockpos, peer_id, flags, NULL, NULL);
|
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-15 00:32:56 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
bool EmergeManager::enqueueBlockEmergeEx(
|
|
|
|
v3s16 blockpos,
|
|
|
|
u16 peer_id,
|
|
|
|
u16 flags,
|
|
|
|
EmergeCompletionCallback callback,
|
|
|
|
void *callback_param)
|
|
|
|
{
|
|
|
|
EmergeThread *thread = NULL;
|
2015-11-14 09:07:21 +01:00
|
|
|
bool entry_already_exists = false;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
{
|
|
|
|
MutexAutoLock queuelock(m_queue_mutex);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
if (!pushBlockEmergeData(blockpos, peer_id, flags,
|
2015-11-14 09:07:21 +01:00
|
|
|
callback, callback_param, &entry_already_exists))
|
2015-10-04 08:54:25 +02:00
|
|
|
return false;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-11-14 09:07:21 +01:00
|
|
|
if (entry_already_exists)
|
|
|
|
return true;
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
thread = getOptimalThread();
|
|
|
|
thread->pushBlock(blockpos);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
2015-10-04 08:54:25 +02:00
|
|
|
|
|
|
|
thread->signal();
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Mapgen-related helper functions
|
|
|
|
//
|
|
|
|
|
2015-09-23 06:31:45 +02:00
|
|
|
v3s16 EmergeManager::getContainingChunk(v3s16 blockpos)
|
|
|
|
{
|
|
|
|
return getContainingChunk(blockpos, params.chunksize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v3s16 EmergeManager::getContainingChunk(v3s16 blockpos, s16 chunksize)
|
|
|
|
{
|
|
|
|
s16 coff = -chunksize / 2;
|
|
|
|
v3s16 chunk_offset(coff, coff, coff);
|
|
|
|
|
|
|
|
return getContainerPos(blockpos - chunk_offset, chunksize)
|
|
|
|
* chunksize + chunk_offset;
|
|
|
|
}
|
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2016-02-04 02:03:31 +01:00
|
|
|
int EmergeManager::getSpawnLevelAtPoint(v2s16 p)
|
|
|
|
{
|
|
|
|
if (m_mapgens.size() == 0 || !m_mapgens[0]) {
|
|
|
|
errorstream << "EmergeManager: getSpawnLevelAtPoint() called"
|
|
|
|
" before mapgen init" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_mapgens[0]->getSpawnLevelAtPoint(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
int EmergeManager::getGroundLevelAtPoint(v2s16 p)
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
if (m_mapgens.size() == 0 || !m_mapgens[0]) {
|
2013-02-17 07:47:49 +01:00
|
|
|
errorstream << "EmergeManager: getGroundLevelAtPoint() called"
|
2015-10-04 08:54:25 +02:00
|
|
|
" before mapgen init" << std::endl;
|
2013-02-14 04:43:15 +01:00
|
|
|
return 0;
|
2013-02-17 07:47:49 +01:00
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
return m_mapgens[0]->getGroundLevelAtPoint(p);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
bool EmergeManager::isBlockUnderground(v3s16 blockpos)
|
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
#if 0
|
2013-02-14 04:43:15 +01:00
|
|
|
v2s16 p = v2s16((blockpos.X * MAP_BLOCKSIZE) + MAP_BLOCKSIZE / 2,
|
|
|
|
(blockpos.Y * MAP_BLOCKSIZE) + MAP_BLOCKSIZE / 2);
|
|
|
|
int ground_level = getGroundLevelAtPoint(p);
|
|
|
|
return blockpos.Y * (MAP_BLOCKSIZE + 1) <= min(water_level, ground_level);
|
2015-10-04 08:54:25 +02:00
|
|
|
#endif
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
// Use a simple heuristic; the above method is wildly inaccurate anyway.
|
2014-02-04 04:42:10 +01:00
|
|
|
return blockpos.Y * (MAP_BLOCKSIZE + 1) <= params.water_level;
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-04 22:37:03 +02:00
|
|
|
void EmergeManager::getMapgenNames(
|
|
|
|
std::vector<const char *> *mgnames, bool include_hidden)
|
2014-12-30 03:23:51 +01:00
|
|
|
{
|
2015-10-04 22:37:03 +02:00
|
|
|
for (u32 i = 0; i != ARRLEN(g_reg_mapgens); i++) {
|
|
|
|
if (include_hidden || g_reg_mapgens[i].is_user_visible)
|
|
|
|
mgnames->push_back(g_reg_mapgens[i].name);
|
|
|
|
}
|
2014-12-30 03:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
MapgenFactory *EmergeManager::getMapgenFactory(const std::string &mgname)
|
2014-12-29 18:58:55 +01:00
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
for (u32 i = 0; i != ARRLEN(g_reg_mapgens); i++) {
|
|
|
|
if (mgname == g_reg_mapgens[i].name)
|
|
|
|
return g_reg_mapgens[i].factory;
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
return NULL;
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
bool EmergeManager::pushBlockEmergeData(
|
|
|
|
v3s16 pos,
|
|
|
|
u16 peer_requested,
|
|
|
|
u16 flags,
|
|
|
|
EmergeCompletionCallback callback,
|
2015-11-14 09:07:21 +01:00
|
|
|
void *callback_param,
|
|
|
|
bool *entry_already_exists)
|
2014-12-29 18:58:55 +01:00
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
u16 &count_peer = m_peer_queue_count[peer_requested];
|
|
|
|
|
|
|
|
if ((flags & BLOCK_EMERGE_FORCE_QUEUE) == 0) {
|
|
|
|
if (m_blocks_enqueued.size() >= m_qlimit_total)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (peer_requested != PEER_ID_INEXISTENT) {
|
|
|
|
u16 qlimit_peer = (flags & BLOCK_EMERGE_ALLOW_GEN) ?
|
|
|
|
m_qlimit_generate : m_qlimit_diskonly;
|
|
|
|
if (count_peer >= qlimit_peer)
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
std::pair<std::map<v3s16, BlockEmergeData>::iterator, bool> findres;
|
|
|
|
findres = m_blocks_enqueued.insert(std::make_pair(pos, BlockEmergeData()));
|
|
|
|
|
|
|
|
BlockEmergeData &bedata = findres.first->second;
|
2015-11-14 09:07:21 +01:00
|
|
|
*entry_already_exists = !findres.second;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
if (callback)
|
|
|
|
bedata.callbacks.push_back(std::make_pair(callback, callback_param));
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-11-14 09:07:21 +01:00
|
|
|
if (*entry_already_exists) {
|
2015-10-04 08:54:25 +02:00
|
|
|
bedata.flags |= flags;
|
|
|
|
} else {
|
|
|
|
bedata.flags = flags;
|
|
|
|
bedata.peer_requested = peer_requested;
|
|
|
|
|
|
|
|
count_peer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
|
|
|
|
bool EmergeManager::popBlockEmergeData(
|
|
|
|
v3s16 pos,
|
|
|
|
BlockEmergeData *bedata)
|
2014-12-29 18:58:55 +01:00
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
std::map<v3s16, BlockEmergeData>::iterator it;
|
|
|
|
std::map<u16, u16>::iterator it2;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
it = m_blocks_enqueued.find(pos);
|
|
|
|
if (it == m_blocks_enqueued.end())
|
2013-02-24 04:45:12 +01:00
|
|
|
return false;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
*bedata = it->second;
|
|
|
|
|
|
|
|
it2 = m_peer_queue_count.find(bedata->peer_requested);
|
|
|
|
if (it2 == m_peer_queue_count.end())
|
|
|
|
return false;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
u16 &count_peer = it2->second;
|
|
|
|
assert(count_peer != 0);
|
|
|
|
count_peer--;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
m_blocks_enqueued.erase(it);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
EmergeThread *EmergeManager::getOptimalThread()
|
|
|
|
{
|
|
|
|
size_t nthreads = m_threads.size();
|
|
|
|
|
|
|
|
FATAL_ERROR_IF(nthreads == 0, "No emerge threads!");
|
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
size_t nitems_lowest = m_threads[0]->m_block_queue.size();
|
|
|
|
|
|
|
|
for (size_t i = 1; i < nthreads; i++) {
|
|
|
|
size_t nitems = m_threads[i]->m_block_queue.size();
|
|
|
|
if (nitems < nitems_lowest) {
|
|
|
|
index = i;
|
|
|
|
nitems_lowest = nitems;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_threads[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
//// EmergeThread
|
|
|
|
////
|
|
|
|
|
|
|
|
EmergeThread::EmergeThread(Server *server, int ethreadid) :
|
|
|
|
enable_mapgen_debug_info(false),
|
|
|
|
id(ethreadid),
|
|
|
|
m_server(server),
|
|
|
|
m_map(NULL),
|
|
|
|
m_emerge(NULL),
|
|
|
|
m_mapgen(NULL)
|
|
|
|
{
|
2015-10-17 05:43:29 +02:00
|
|
|
m_name = "Emerge-" + itos(ethreadid);
|
2015-10-04 08:54:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EmergeThread::~EmergeThread()
|
|
|
|
{
|
|
|
|
//cancelPendingItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EmergeThread::signal()
|
|
|
|
{
|
|
|
|
m_queue_event.signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EmergeThread::pushBlock(v3s16 pos)
|
|
|
|
{
|
|
|
|
m_block_queue.push(pos);
|
2013-02-24 04:45:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-14 04:43:15 +01:00
|
|
|
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
void EmergeThread::cancelPendingItems()
|
|
|
|
{
|
|
|
|
MutexAutoLock queuelock(m_emerge->m_queue_mutex);
|
|
|
|
|
|
|
|
while (!m_block_queue.empty()) {
|
|
|
|
BlockEmergeData bedata;
|
|
|
|
v3s16 pos;
|
|
|
|
|
|
|
|
pos = m_block_queue.front();
|
|
|
|
m_block_queue.pop();
|
|
|
|
|
|
|
|
m_emerge->popBlockEmergeData(pos, &bedata);
|
|
|
|
|
|
|
|
runCompletionCallbacks(pos, EMERGE_CANCELLED, bedata.callbacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EmergeThread::runCompletionCallbacks(
|
|
|
|
v3s16 pos,
|
|
|
|
EmergeAction action,
|
|
|
|
const EmergeCallbackList &callbacks)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i != callbacks.size(); i++) {
|
|
|
|
EmergeCompletionCallback callback;
|
|
|
|
void *param;
|
|
|
|
|
|
|
|
callback = callbacks[i].first;
|
|
|
|
param = callbacks[i].second;
|
|
|
|
|
|
|
|
callback(pos, action, param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EmergeThread::popBlockEmerge(v3s16 *pos, BlockEmergeData *bedata)
|
|
|
|
{
|
|
|
|
MutexAutoLock queuelock(m_emerge->m_queue_mutex);
|
|
|
|
|
|
|
|
if (m_block_queue.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*pos = m_block_queue.front();
|
|
|
|
m_block_queue.pop();
|
|
|
|
|
|
|
|
m_emerge->popBlockEmergeData(*pos, bedata);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EmergeAction EmergeThread::getBlockOrStartGen(
|
|
|
|
v3s16 pos, bool allow_gen, MapBlock **block, BlockMakeData *bmdata)
|
|
|
|
{
|
|
|
|
MutexAutoLock envlock(m_server->m_env_mutex);
|
|
|
|
|
|
|
|
// 1). Attempt to fetch block from memory
|
|
|
|
*block = m_map->getBlockNoCreateNoEx(pos);
|
|
|
|
if (*block && !(*block)->isDummy() && (*block)->isGenerated())
|
|
|
|
return EMERGE_FROM_MEMORY;
|
|
|
|
|
|
|
|
// 2). Attempt to load block from disk
|
|
|
|
*block = m_map->loadBlock(pos);
|
|
|
|
if (*block && (*block)->isGenerated())
|
|
|
|
return EMERGE_FROM_DISK;
|
|
|
|
|
|
|
|
// 3). Attempt to start generation
|
|
|
|
if (allow_gen && m_map->initBlockMake(pos, bmdata))
|
|
|
|
return EMERGE_GENERATED;
|
|
|
|
|
|
|
|
// All attempts failed; cancel this block emerge
|
|
|
|
return EMERGE_CANCELLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MapBlock *EmergeThread::finishGen(v3s16 pos, BlockMakeData *bmdata,
|
|
|
|
std::map<v3s16, MapBlock *> *modified_blocks)
|
2014-12-29 18:58:55 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock envlock(m_server->m_env_mutex);
|
2015-10-04 08:54:25 +02:00
|
|
|
ScopeProfiler sp(g_profiler,
|
|
|
|
"EmergeThread: after Mapgen::makeChunk", SPT_AVG);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Perform post-processing on blocks (invalidate lighting, queue liquid
|
|
|
|
transforms, etc.) to finish block make
|
|
|
|
*/
|
|
|
|
m_map->finishBlockMake(bmdata, modified_blocks);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
MapBlock *block = m_map->getBlockNoCreateNoEx(pos);
|
|
|
|
if (!block) {
|
|
|
|
errorstream << "EmergeThread::finishGen: Couldn't grab block we "
|
|
|
|
"just generated: " << PP(pos) << std::endl;
|
|
|
|
return NULL;
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
v3s16 minp = bmdata->blockpos_min * MAP_BLOCKSIZE;
|
|
|
|
v3s16 maxp = bmdata->blockpos_max * MAP_BLOCKSIZE +
|
|
|
|
v3s16(1,1,1) * (MAP_BLOCKSIZE - 1);
|
|
|
|
|
|
|
|
// Ignore map edit events, they will not need to be sent
|
|
|
|
// to anybody because the block hasn't been sent to anybody
|
|
|
|
MapEditEventAreaIgnorer ign(
|
|
|
|
&m_server->m_ignore_map_edit_events_area,
|
|
|
|
VoxelArea(minp, maxp));
|
|
|
|
|
|
|
|
/*
|
|
|
|
Run Lua on_generated callbacks
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
m_server->getScriptIface()->environment_OnGenerated(
|
|
|
|
minp, maxp, m_mapgen->blockseed);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
m_server->setAsyncFatalError("Lua: " + std::string(e.what()));
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
EMERGE_DBG_OUT("ended up with: " << analyze_block(block));
|
|
|
|
|
|
|
|
/*
|
|
|
|
Activate the block
|
|
|
|
*/
|
|
|
|
m_server->m_env->activateBlock(block, 0);
|
|
|
|
|
|
|
|
return block;
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
void *EmergeThread::run()
|
2014-12-29 18:58:55 +01:00
|
|
|
{
|
2015-10-14 08:39:30 +02:00
|
|
|
DSTACK(FUNCTION_NAME);
|
2013-02-14 04:43:15 +01:00
|
|
|
BEGIN_DEBUG_EXCEPTION_HANDLER
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
v3s16 pos;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
m_map = (ServerMap *)&(m_server->m_env->getMap());
|
|
|
|
m_emerge = m_server->m_emerge;
|
|
|
|
m_mapgen = m_emerge->m_mapgens[id];
|
|
|
|
enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
try {
|
2015-10-04 08:54:25 +02:00
|
|
|
while (!stopRequested()) {
|
|
|
|
std::map<v3s16, MapBlock *> modified_blocks;
|
|
|
|
BlockEmergeData bedata;
|
|
|
|
BlockMakeData bmdata;
|
|
|
|
EmergeAction action;
|
|
|
|
MapBlock *block;
|
|
|
|
|
|
|
|
if (!popBlockEmerge(&pos, &bedata)) {
|
|
|
|
m_queue_event.wait();
|
2013-02-17 08:46:08 +01:00
|
|
|
continue;
|
2013-02-15 01:22:43 +01:00
|
|
|
}
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
if (blockpos_over_limit(pos))
|
2013-02-14 04:43:15 +01:00
|
|
|
continue;
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
bool allow_gen = bedata.flags & BLOCK_EMERGE_ALLOW_GEN;
|
|
|
|
EMERGE_DBG_OUT("pos=" PP(pos) " allow_gen=" << allow_gen);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
action = getBlockOrStartGen(pos, allow_gen, &block, &bmdata);
|
|
|
|
if (action == EMERGE_GENERATED) {
|
2013-02-14 04:43:15 +01:00
|
|
|
{
|
2015-10-04 08:54:25 +02:00
|
|
|
ScopeProfiler sp(g_profiler,
|
|
|
|
"EmergeThread: Mapgen::makeChunk", SPT_AVG);
|
2013-02-14 04:43:15 +01:00
|
|
|
TimeTaker t("mapgen::make_block()");
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
m_mapgen->makeChunk(&bmdata);
|
2013-02-14 04:43:15 +01:00
|
|
|
|
|
|
|
if (enable_mapgen_debug_info == false)
|
|
|
|
t.stop(true); // Hide output
|
|
|
|
}
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
block = finishGen(pos, &bmdata, &modified_blocks);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
runCompletionCallbacks(pos, action, bedata.callbacks);
|
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
if (block)
|
2015-10-04 08:54:25 +02:00
|
|
|
modified_blocks[pos] = block;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
if (modified_blocks.size() > 0)
|
2014-01-31 00:24:00 +01:00
|
|
|
m_server->SetBlocksNotSent(modified_blocks);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
2015-10-04 08:54:25 +02:00
|
|
|
} catch (VersionMismatchException &e) {
|
2013-02-14 04:43:15 +01:00
|
|
|
std::ostringstream err;
|
2015-10-04 08:54:25 +02:00
|
|
|
err << "World data version mismatch in MapBlock " << PP(pos) << std::endl
|
2015-02-28 00:05:29 +01:00
|
|
|
<< "----" << std::endl
|
|
|
|
<< "\"" << e.what() << "\"" << std::endl
|
|
|
|
<< "See debug.txt." << std::endl
|
2015-04-27 16:17:10 +02:00
|
|
|
<< "World probably saved by a newer version of " PROJECT_NAME_C "."
|
2015-02-28 00:05:29 +01:00
|
|
|
<< std::endl;
|
2013-02-14 04:43:15 +01:00
|
|
|
m_server->setAsyncFatalError(err.str());
|
2015-10-04 08:54:25 +02:00
|
|
|
} catch (SerializationError &e) {
|
2013-02-14 04:43:15 +01:00
|
|
|
std::ostringstream err;
|
2015-10-04 08:54:25 +02:00
|
|
|
err << "Invalid data in MapBlock " << PP(pos) << std::endl
|
2015-02-28 00:05:29 +01:00
|
|
|
<< "----" << std::endl
|
|
|
|
<< "\"" << e.what() << "\"" << std::endl
|
|
|
|
<< "See debug.txt." << std::endl
|
|
|
|
<< "You can ignore this using [ignore_world_load_errors = true]."
|
|
|
|
<< std::endl;
|
2013-02-14 04:43:15 +01:00
|
|
|
m_server->setAsyncFatalError(err.str());
|
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2015-10-14 08:33:04 +02:00
|
|
|
END_DEBUG_EXCEPTION_HANDLER
|
2013-02-14 04:43:15 +01:00
|
|
|
return NULL;
|
|
|
|
}
|