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"
|
2013-02-14 04:43:15 +01:00
|
|
|
#include "server.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <queue>
|
2013-12-01 01:52:06 +01:00
|
|
|
#include "jthread/jevent.h"
|
2013-02-14 04:43:15 +01:00
|
|
|
#include "map.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "environment.h"
|
|
|
|
#include "util/container.h"
|
|
|
|
#include "util/thread.h"
|
2013-02-14 04:43:15 +01:00
|
|
|
#include "constants.h"
|
|
|
|
#include "voxel.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "mapblock.h"
|
|
|
|
#include "serverobject.h"
|
|
|
|
#include "settings.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "scripting_game.h"
|
2013-02-14 04:43:15 +01:00
|
|
|
#include "profiler.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "nodedef.h"
|
2014-11-01 18:16:23 +01:00
|
|
|
#include "mg_biome.h"
|
2014-11-02 17:52:30 +01:00
|
|
|
#include "mg_ore.h"
|
2014-11-13 05:01:13 +01:00
|
|
|
#include "mg_decoration.h"
|
|
|
|
#include "mg_schematic.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"
|
2013-03-22 18:16:51 +01:00
|
|
|
#include "mapgen_singlenode.h"
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2014-12-30 03:23:51 +01:00
|
|
|
struct MapgenDesc {
|
|
|
|
const char *name;
|
|
|
|
MapgenFactory *factory;
|
|
|
|
};
|
|
|
|
|
|
|
|
MapgenDesc reg_mapgens[] = {
|
|
|
|
{"v5", new MapgenFactoryV5},
|
|
|
|
{"v6", new MapgenFactoryV6},
|
|
|
|
{"v7", new MapgenFactoryV7},
|
|
|
|
{"singlenode", new MapgenFactorySinglenode},
|
|
|
|
};
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2013-12-03 23:32:03 +01:00
|
|
|
class EmergeThread : public JThread
|
2013-08-11 04:09:45 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Server *m_server;
|
|
|
|
ServerMap *map;
|
|
|
|
EmergeManager *emerge;
|
|
|
|
Mapgen *mapgen;
|
|
|
|
bool enable_mapgen_debug_info;
|
|
|
|
int id;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
Event qevent;
|
|
|
|
std::queue<v3s16> blockqueue;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
EmergeThread(Server *server, int ethreadid):
|
2013-12-03 23:32:03 +01:00
|
|
|
JThread(),
|
2013-08-11 04:09:45 +02:00
|
|
|
m_server(server),
|
|
|
|
map(NULL),
|
|
|
|
emerge(NULL),
|
|
|
|
mapgen(NULL),
|
2013-12-03 23:32:03 +01:00
|
|
|
enable_mapgen_debug_info(false),
|
2013-08-11 04:09:45 +02:00
|
|
|
id(ethreadid)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Thread();
|
|
|
|
bool popBlockEmerge(v3s16 *pos, u8 *flags);
|
|
|
|
bool getBlockOrStartGen(v3s16 p, MapBlock **b,
|
|
|
|
BlockMakeData *data, bool allow_generate);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-24 04:45:12 +01:00
|
|
|
/////////////////////////////// Emerge Manager ////////////////////////////////
|
|
|
|
|
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.
|
|
|
|
this->threads_active = false;
|
|
|
|
|
2013-12-14 07:52:06 +01:00
|
|
|
mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-02-04 04:42:10 +01:00
|
|
|
// if unspecified, leave a proc for the main thread and one for
|
|
|
|
// 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))
|
2014-02-04 04:42:10 +01:00
|
|
|
nthreads = porting::getNumberOfProcessors() - 2;
|
2013-02-17 07:47:49 +01:00
|
|
|
if (nthreads < 1)
|
|
|
|
nthreads = 1;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-02-04 04:42:10 +01:00
|
|
|
qlimit_total = g_settings->getU16("emergequeue_limit_total");
|
2014-02-05 06:17:53 +01:00
|
|
|
if (!g_settings->getU16NoEx("emergequeue_limit_diskonly", qlimit_diskonly))
|
2014-02-04 04:42:10 +01:00
|
|
|
qlimit_diskonly = nthreads * 5 + 1;
|
2014-02-05 06:17:53 +01:00
|
|
|
if (!g_settings->getU16NoEx("emergequeue_limit_generate", qlimit_generate))
|
2014-02-04 04:42:10 +01:00
|
|
|
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
|
|
|
|
if (qlimit_total < 1)
|
|
|
|
qlimit_total = 1;
|
|
|
|
if (qlimit_diskonly < 1)
|
|
|
|
qlimit_diskonly = 1;
|
|
|
|
if (qlimit_generate < 1)
|
|
|
|
qlimit_generate = 1;
|
|
|
|
|
2014-09-12 00:22:05 +02:00
|
|
|
for (s16 i = 0; i < nthreads; i++)
|
|
|
|
emergethread.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()
|
|
|
|
{
|
2014-12-30 04:04:47 +01:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++) {
|
2014-01-26 07:12:18 +01:00
|
|
|
if (threads_active) {
|
|
|
|
emergethread[i]->Stop();
|
|
|
|
emergethread[i]->qevent.signal();
|
|
|
|
emergethread[i]->Wait();
|
|
|
|
}
|
2013-02-17 07:47:49 +01:00
|
|
|
delete emergethread[i];
|
|
|
|
delete mapgen[i];
|
|
|
|
}
|
2013-04-07 21:01:19 +02:00
|
|
|
emergethread.clear();
|
|
|
|
mapgen.clear();
|
|
|
|
|
2014-11-13 05:01:13 +01:00
|
|
|
delete biomemgr;
|
|
|
|
delete oremgr;
|
|
|
|
delete decomgr;
|
|
|
|
delete schemmgr;
|
2014-07-06 15:41:56 +02:00
|
|
|
|
|
|
|
if (params.sparams) {
|
|
|
|
delete params.sparams;
|
|
|
|
params.sparams = NULL;
|
|
|
|
}
|
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()
|
|
|
|
{
|
2013-02-17 07:47:49 +01:00
|
|
|
if (mapgen.size())
|
2013-02-14 04:43:15 +01:00
|
|
|
return;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-02-04 04:42:10 +01:00
|
|
|
if (!params.sparams) {
|
|
|
|
params.sparams = createMapgenParams(params.mg_name);
|
|
|
|
if (!params.sparams) {
|
|
|
|
params.mg_name = DEFAULT_MAPGEN;
|
|
|
|
params.sparams = createMapgenParams(params.mg_name);
|
|
|
|
assert(params.sparams);
|
2013-06-27 23:06:52 +02:00
|
|
|
}
|
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
|
|
|
|
2013-06-27 23:06:52 +02:00
|
|
|
// Create the mapgens
|
2014-12-30 04:04:47 +01:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++) {
|
2014-02-04 04:42:10 +01:00
|
|
|
Mapgen *mg = createMapgen(params.mg_name, i, ¶ms);
|
|
|
|
assert(mg);
|
2013-02-17 07:47:49 +01:00
|
|
|
mapgen.push_back(mg);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
Mapgen *EmergeManager::getCurrentMapgen()
|
|
|
|
{
|
2014-12-30 04:04:47 +01:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++) {
|
2013-06-26 23:19:39 +02:00
|
|
|
if (emergethread[i]->IsSameThread())
|
|
|
|
return emergethread[i]->mapgen;
|
|
|
|
}
|
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()
|
|
|
|
{
|
2014-01-26 07:12:18 +01:00
|
|
|
if (threads_active)
|
|
|
|
return;
|
|
|
|
|
2014-12-30 04:04:47 +01:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++)
|
2013-12-03 23:32:03 +01:00
|
|
|
emergethread[i]->Start();
|
2014-01-26 07:12:18 +01:00
|
|
|
|
|
|
|
threads_active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
void EmergeManager::stopThreads()
|
|
|
|
{
|
2014-01-26 07:12:18 +01:00
|
|
|
if (!threads_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Request thread stop in parallel
|
2014-12-30 04:04:47 +01:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++) {
|
2014-01-26 07:12:18 +01:00
|
|
|
emergethread[i]->Stop();
|
|
|
|
emergethread[i]->qevent.signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then do the waiting for each
|
2014-12-30 04:04:47 +01:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++)
|
2014-01-26 07:12:18 +01:00
|
|
|
emergethread[i]->Wait();
|
|
|
|
|
|
|
|
threads_active = false;
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate)
|
|
|
|
{
|
2013-02-15 00:32:56 +01:00
|
|
|
std::map<v3s16, BlockEmergeData *>::const_iterator iter;
|
|
|
|
BlockEmergeData *bedata;
|
|
|
|
u16 count;
|
2013-02-14 04:43:15 +01:00
|
|
|
u8 flags = 0;
|
2013-02-17 07:47:49 +01:00
|
|
|
int idx = 0;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
if (allow_generate)
|
|
|
|
flags |= BLOCK_EMERGE_ALLOWGEN;
|
|
|
|
|
|
|
|
{
|
|
|
|
JMutexAutoLock queuelock(queuemutex);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-15 00:32:56 +01:00
|
|
|
count = blocks_enqueued.size();
|
2013-02-15 02:36:58 +01:00
|
|
|
if (count >= qlimit_total)
|
2013-02-15 00:32:56 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
count = peer_queue_count[peer_id];
|
2013-02-15 02:36:58 +01:00
|
|
|
u16 qlimit_peer = allow_generate ? qlimit_generate : qlimit_diskonly;
|
|
|
|
if (count >= qlimit_peer)
|
2013-02-15 00:32:56 +01:00
|
|
|
return false;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-15 00:32:56 +01:00
|
|
|
iter = blocks_enqueued.find(p);
|
2013-02-14 04:43:15 +01:00
|
|
|
if (iter != blocks_enqueued.end()) {
|
2013-02-15 00:32:56 +01:00
|
|
|
bedata = iter->second;
|
|
|
|
bedata->flags |= flags;
|
2013-02-14 04:43:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-15 00:32:56 +01:00
|
|
|
|
|
|
|
bedata = new BlockEmergeData;
|
|
|
|
bedata->flags = flags;
|
|
|
|
bedata->peer_requested = peer_id;
|
|
|
|
blocks_enqueued.insert(std::make_pair(p, bedata));
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-15 00:32:56 +01:00
|
|
|
peer_queue_count[peer_id] = count + 1;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-21 23:44:14 +01:00
|
|
|
// insert into the EmergeThread queue with the least items
|
2013-02-17 07:47:49 +01:00
|
|
|
int lowestitems = emergethread[0]->blockqueue.size();
|
2014-12-30 04:04:47 +01:00
|
|
|
for (u32 i = 1; i != emergethread.size(); i++) {
|
2013-02-17 07:47:49 +01:00
|
|
|
int nitems = emergethread[i]->blockqueue.size();
|
|
|
|
if (nitems < lowestitems) {
|
|
|
|
idx = i;
|
|
|
|
lowestitems = nitems;
|
|
|
|
}
|
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-17 07:47:49 +01:00
|
|
|
emergethread[idx]->blockqueue.push(p);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
2013-02-17 07:47:49 +01:00
|
|
|
emergethread[idx]->qevent.signal();
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
int EmergeManager::getGroundLevelAtPoint(v2s16 p)
|
|
|
|
{
|
2013-02-21 23:44:14 +01:00
|
|
|
if (mapgen.size() == 0 || !mapgen[0]) {
|
2013-02-17 07:47:49 +01:00
|
|
|
errorstream << "EmergeManager: getGroundLevelAtPoint() called"
|
2013-12-08 05:43:46 +01:00
|
|
|
" before mapgen initialized" << 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
|
|
|
|
2013-02-17 07:47:49 +01:00
|
|
|
return mapgen[0]->getGroundLevelAtPoint(p);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
bool EmergeManager::isBlockUnderground(v3s16 blockpos)
|
|
|
|
{
|
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);
|
|
|
|
*/
|
|
|
|
|
|
|
|
//yuck, but then again, should i bother being accurate?
|
|
|
|
//the height of the nodes in a single block is quite variable
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-30 03:23:51 +01:00
|
|
|
void EmergeManager::getMapgenNames(std::list<const char *> &mgnames)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i != ARRLEN(reg_mapgens); i++)
|
|
|
|
mgnames.push_back(reg_mapgens[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Mapgen *EmergeManager::createMapgen(const std::string &mgname, int mgid,
|
2014-12-29 18:58:55 +01:00
|
|
|
MapgenParams *mgparams)
|
|
|
|
{
|
2014-12-30 03:23:51 +01:00
|
|
|
u32 i;
|
|
|
|
for (i = 0; i != ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
|
|
|
|
if (i == ARRLEN(reg_mapgens)) {
|
2013-02-14 04:43:15 +01:00
|
|
|
errorstream << "EmergeManager; mapgen " << mgname <<
|
2014-12-29 18:58:55 +01:00
|
|
|
" not registered" << std::endl;
|
2013-02-14 04:43:15 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-12-30 03:23:51 +01:00
|
|
|
MapgenFactory *mgfactory = reg_mapgens[i].factory;
|
2013-02-17 07:47:49 +01:00
|
|
|
return mgfactory->createMapgen(mgid, mgparams, this);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-30 03:23:51 +01:00
|
|
|
MapgenSpecificParams *EmergeManager::createMapgenParams(const std::string &mgname)
|
2014-12-29 18:58:55 +01:00
|
|
|
{
|
2014-12-30 03:23:51 +01:00
|
|
|
u32 i;
|
2015-01-26 12:44:49 +01:00
|
|
|
for (i = 0; i < ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
|
2014-12-30 03:23:51 +01:00
|
|
|
if (i == ARRLEN(reg_mapgens)) {
|
2015-01-26 12:44:49 +01:00
|
|
|
errorstream << "EmergeManager: Mapgen " << mgname <<
|
2014-12-29 18:58:55 +01:00
|
|
|
" not registered" << std::endl;
|
2013-02-14 04:43:15 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-12-30 03:23:51 +01:00
|
|
|
MapgenFactory *mgfactory = reg_mapgens[i].factory;
|
2013-02-14 04:43:15 +01:00
|
|
|
return mgfactory->createMapgenParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-01 01:52:06 +01:00
|
|
|
////////////////////////////// Emerge Thread //////////////////////////////////
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags)
|
|
|
|
{
|
2013-02-24 04:45:12 +01:00
|
|
|
std::map<v3s16, BlockEmergeData *>::iterator iter;
|
|
|
|
JMutexAutoLock queuelock(emerge->queuemutex);
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2013-02-24 04:45:12 +01:00
|
|
|
if (blockqueue.empty())
|
|
|
|
return false;
|
|
|
|
v3s16 p = blockqueue.front();
|
|
|
|
blockqueue.pop();
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-24 04:45:12 +01:00
|
|
|
*pos = p;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-24 04:45:12 +01:00
|
|
|
iter = emerge->blocks_enqueued.find(p);
|
2013-12-01 01:52:06 +01:00
|
|
|
if (iter == emerge->blocks_enqueued.end())
|
2013-02-24 04:45:12 +01:00
|
|
|
return false; //uh oh, queue and map out of sync!!
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2013-02-24 04:45:12 +01:00
|
|
|
BlockEmergeData *bedata = iter->second;
|
|
|
|
*flags = bedata->flags;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-24 04:45:12 +01:00
|
|
|
emerge->peer_queue_count[bedata->peer_requested]--;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2013-02-24 04:45:12 +01:00
|
|
|
delete bedata;
|
|
|
|
emerge->blocks_enqueued.erase(iter);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-24 04:45:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-14 04:43:15 +01:00
|
|
|
|
|
|
|
|
2013-12-01 01:52:06 +01:00
|
|
|
bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
|
2014-12-29 18:58:55 +01:00
|
|
|
BlockMakeData *data, bool allow_gen)
|
|
|
|
{
|
2013-02-14 04:43:15 +01:00
|
|
|
v2s16 p2d(p.X, p.Z);
|
|
|
|
//envlock: usually takes <=1ms, sometimes 90ms or ~400ms to acquire
|
2013-12-01 01:52:06 +01:00
|
|
|
JMutexAutoLock envlock(m_server->m_env_mutex);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
// Load sector if it isn't loaded
|
|
|
|
if (map->getSectorNoGenerateNoEx(p2d) == NULL)
|
|
|
|
map->loadSectorMeta(p2d);
|
|
|
|
|
|
|
|
// Attempt to load block
|
|
|
|
MapBlock *block = map->getBlockNoCreateNoEx(p);
|
|
|
|
if (!block || block->isDummy() || !block->isGenerated()) {
|
|
|
|
EMERGE_DBG_OUT("not in memory, attempting to load from disk");
|
|
|
|
block = map->loadBlock(p);
|
2013-11-17 07:59:04 +01:00
|
|
|
if (block && block->isGenerated())
|
|
|
|
map->prepareBlock(block);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If could not load and allowed to generate,
|
|
|
|
// start generation inside this same envlock
|
|
|
|
if (allow_gen && (block == NULL || !block->isGenerated())) {
|
|
|
|
EMERGE_DBG_OUT("generating");
|
2013-02-17 08:46:08 +01:00
|
|
|
*b = block;
|
|
|
|
return map->initBlockMake(data, p);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
*b = block;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 18:58:55 +01:00
|
|
|
void *EmergeThread::Thread()
|
|
|
|
{
|
2013-02-14 04:43:15 +01:00
|
|
|
ThreadStarted();
|
2013-05-19 19:58:01 +02:00
|
|
|
log_register_thread("EmergeThread" + itos(id));
|
2013-02-14 04:43:15 +01:00
|
|
|
DSTACK(__FUNCTION_NAME);
|
|
|
|
BEGIN_DEBUG_EXCEPTION_HANDLER
|
|
|
|
|
|
|
|
v3s16 last_tried_pos(-32768,-32768,-32768); // For error output
|
|
|
|
v3s16 p;
|
2014-04-15 19:49:32 +02:00
|
|
|
u8 flags = 0;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
map = (ServerMap *)&(m_server->m_env->getMap());
|
|
|
|
emerge = m_server->m_emerge;
|
2013-02-17 08:46:08 +01:00
|
|
|
mapgen = emerge->mapgen[id];
|
|
|
|
enable_mapgen_debug_info = emerge->mapgen_debug_info;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2014-04-05 15:27:33 +02:00
|
|
|
porting::setThreadName("EmergeThread");
|
|
|
|
|
2013-12-03 23:32:03 +01:00
|
|
|
while (!StopRequested())
|
2013-02-14 04:43:15 +01:00
|
|
|
try {
|
2013-02-17 08:46:08 +01:00
|
|
|
if (!popBlockEmerge(&p, &flags)) {
|
2013-02-14 04:43:15 +01:00
|
|
|
qevent.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
|
|
|
|
|
|
|
last_tried_pos = p;
|
|
|
|
if (blockpos_over_limit(p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool allow_generate = flags & BLOCK_EMERGE_ALLOWGEN;
|
|
|
|
EMERGE_DBG_OUT("p=" PP(p) " allow_generate=" << allow_generate);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
/*
|
|
|
|
Try to fetch block from memory or disk.
|
|
|
|
If not found and asked to generate, initialize generator.
|
|
|
|
*/
|
|
|
|
BlockMakeData data;
|
|
|
|
MapBlock *block = NULL;
|
2012-12-20 18:19:49 +01:00
|
|
|
std::map<v3s16, MapBlock *> modified_blocks;
|
2013-12-08 05:43:46 +01:00
|
|
|
|
|
|
|
if (getBlockOrStartGen(p, &block, &data, allow_generate) && mapgen) {
|
2013-02-14 04:43:15 +01:00
|
|
|
{
|
|
|
|
ScopeProfiler sp(g_profiler, "EmergeThread: Mapgen::makeChunk", SPT_AVG);
|
|
|
|
TimeTaker t("mapgen::make_block()");
|
|
|
|
|
|
|
|
mapgen->makeChunk(&data);
|
|
|
|
|
|
|
|
if (enable_mapgen_debug_info == false)
|
|
|
|
t.stop(true); // Hide output
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
//envlock: usually 0ms, but can take either 30 or 400ms to acquire
|
2013-12-01 01:52:06 +01:00
|
|
|
JMutexAutoLock envlock(m_server->m_env_mutex);
|
2013-02-14 04:43:15 +01:00
|
|
|
ScopeProfiler sp(g_profiler, "EmergeThread: after "
|
2013-02-15 01:22:43 +01:00
|
|
|
"Mapgen::makeChunk (envlock)", SPT_AVG);
|
2013-02-14 04:43:15 +01:00
|
|
|
|
|
|
|
map->finishBlockMake(&data, modified_blocks);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
block = map->getBlockNoCreateNoEx(p);
|
|
|
|
if (block) {
|
|
|
|
/*
|
|
|
|
Do some post-generate stuff
|
|
|
|
*/
|
|
|
|
v3s16 minp = data.blockpos_min * MAP_BLOCKSIZE;
|
|
|
|
v3s16 maxp = data.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
|
2013-12-01 01:52:06 +01:00
|
|
|
MapEditEventAreaIgnorer
|
2013-02-14 04:43:15 +01:00
|
|
|
ign(&m_server->m_ignore_map_edit_events_area,
|
|
|
|
VoxelArea(minp, maxp));
|
2013-12-14 07:59:16 +01:00
|
|
|
try { // takes about 90ms with -O1 on an e3-1230v2
|
2013-09-17 04:52:24 +02:00
|
|
|
m_server->getScriptIface()->environment_OnGenerated(
|
2014-12-30 03:44:52 +01:00
|
|
|
minp, maxp, mapgen->blockseed);
|
2013-12-14 07:59:16 +01:00
|
|
|
} catch(LuaError &e) {
|
|
|
|
m_server->setAsyncFatalError(e.what());
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EMERGE_DBG_OUT("ended up with: " << analyze_block(block));
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
m_server->m_env->activateBlock(block, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Set sent status of modified blocks on clients
|
|
|
|
*/
|
|
|
|
// Add the originally fetched block to the modified list
|
|
|
|
if (block)
|
2012-12-20 18:19:49 +01:00
|
|
|
modified_blocks[p] = block;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2014-01-31 00:24:00 +01:00
|
|
|
if (modified_blocks.size() > 0) {
|
|
|
|
m_server->SetBlocksNotSent(modified_blocks);
|
2013-02-14 04:43:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (VersionMismatchException &e) {
|
|
|
|
std::ostringstream err;
|
2015-02-28 00:05:29 +01:00
|
|
|
err << "World data version mismatch in MapBlock " << PP(last_tried_pos) << std::endl
|
|
|
|
<< "----" << 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());
|
|
|
|
}
|
|
|
|
catch (SerializationError &e) {
|
|
|
|
std::ostringstream err;
|
2015-02-28 00:05:29 +01:00
|
|
|
err << "Invalid data in MapBlock " << PP(last_tried_pos) << std::endl
|
|
|
|
<< "----" << 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-01-04 01:01:30 +01:00
|
|
|
{
|
|
|
|
JMutexAutoLock queuelock(emerge->queuemutex);
|
|
|
|
while (!blockqueue.empty())
|
|
|
|
{
|
|
|
|
v3s16 p = blockqueue.front();
|
|
|
|
blockqueue.pop();
|
|
|
|
|
|
|
|
std::map<v3s16, BlockEmergeData *>::iterator iter;
|
|
|
|
iter = emerge->blocks_enqueued.find(p);
|
|
|
|
if (iter == emerge->blocks_enqueued.end())
|
|
|
|
continue; //uh oh, queue and map out of sync!!
|
|
|
|
|
|
|
|
BlockEmergeData *bedata = iter->second;
|
|
|
|
delete bedata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 04:43:15 +01:00
|
|
|
END_DEBUG_EXCEPTION_HANDLER(errorstream)
|
|
|
|
log_deregister_thread();
|
|
|
|
return NULL;
|
|
|
|
}
|