2013-02-13 22:43:15 -05:00
|
|
|
/*
|
2013-04-06 11:19:59 -04:00
|
|
|
Minetest
|
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
|
2013-02-13 22:43:15 -05: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-10 22:09:45 -04:00
|
|
|
#include "emerge.h"
|
2013-02-13 22:43:15 -05:00
|
|
|
#include "server.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <queue>
|
2013-11-30 19:52:06 -05:00
|
|
|
#include "jthread/jevent.h"
|
2013-02-13 22:43:15 -05:00
|
|
|
#include "map.h"
|
2013-08-10 22:09:45 -04:00
|
|
|
#include "environment.h"
|
|
|
|
#include "util/container.h"
|
|
|
|
#include "util/thread.h"
|
2013-02-13 22:43:15 -05:00
|
|
|
#include "main.h"
|
|
|
|
#include "constants.h"
|
|
|
|
#include "voxel.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "mapblock.h"
|
|
|
|
#include "serverobject.h"
|
|
|
|
#include "settings.h"
|
2013-08-10 22:09:45 -04:00
|
|
|
#include "scripting_game.h"
|
2013-02-13 22:43:15 -05:00
|
|
|
#include "profiler.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "nodedef.h"
|
2014-11-01 13:16:23 -04:00
|
|
|
#include "mg_biome.h"
|
2014-11-02 11:52:30 -05:00
|
|
|
#include "mg_ore.h"
|
2014-11-12 23:01:13 -05:00
|
|
|
#include "mg_decoration.h"
|
|
|
|
#include "mg_schematic.h"
|
2014-10-16 07:45:55 -04:00
|
|
|
#include "mapgen_v5.h"
|
2013-02-13 22:43:15 -05:00
|
|
|
#include "mapgen_v6.h"
|
2013-04-06 11:19:59 -04:00
|
|
|
#include "mapgen_v7.h"
|
2013-03-22 13:16:51 -04:00
|
|
|
#include "mapgen_singlenode.h"
|
2013-02-13 22:43:15 -05:00
|
|
|
|
2014-12-29 21:23:51 -05: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-13 22:43:15 -05:00
|
|
|
|
2013-12-03 17:32:03 -05:00
|
|
|
class EmergeThread : public JThread
|
2013-08-10 22:09:45 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Server *m_server;
|
|
|
|
ServerMap *map;
|
|
|
|
EmergeManager *emerge;
|
|
|
|
Mapgen *mapgen;
|
|
|
|
bool enable_mapgen_debug_info;
|
|
|
|
int id;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-08-10 22:09:45 -04:00
|
|
|
Event qevent;
|
|
|
|
std::queue<v3s16> blockqueue;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-08-10 22:09:45 -04:00
|
|
|
EmergeThread(Server *server, int ethreadid):
|
2013-12-03 17:32:03 -05:00
|
|
|
JThread(),
|
2013-08-10 22:09:45 -04:00
|
|
|
m_server(server),
|
|
|
|
map(NULL),
|
|
|
|
emerge(NULL),
|
|
|
|
mapgen(NULL),
|
2013-12-03 17:32:03 -05:00
|
|
|
enable_mapgen_debug_info(false),
|
2013-08-10 22:09:45 -04:00
|
|
|
id(ethreadid)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Thread();
|
|
|
|
bool popBlockEmerge(v3s16 *pos, u8 *flags);
|
|
|
|
bool getBlockOrStartGen(v3s16 p, MapBlock **b,
|
|
|
|
BlockMakeData *data, bool allow_generate);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-23 22:45:12 -05:00
|
|
|
/////////////////////////////// Emerge Manager ////////////////////////////////
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
EmergeManager::EmergeManager(IGameDef *gamedef)
|
|
|
|
{
|
2014-11-12 23:01:13 -05: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 04:18:04 -05:00
|
|
|
this->gen_notify_on = 0;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-01-26 01:12:18 -05: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 01:52:06 -05:00
|
|
|
mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-02-03 22:42:10 -05:00
|
|
|
// if unspecified, leave a proc for the main thread and one for
|
|
|
|
// some other misc thread
|
2014-09-11 18:22:05 -04:00
|
|
|
s16 nthreads = 0;
|
2014-02-05 00:17:53 -05:00
|
|
|
if (!g_settings->getS16NoEx("num_emerge_threads", nthreads))
|
2014-02-03 22:42:10 -05:00
|
|
|
nthreads = porting::getNumberOfProcessors() - 2;
|
2013-02-17 01:47:49 -05:00
|
|
|
if (nthreads < 1)
|
|
|
|
nthreads = 1;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-02-03 22:42:10 -05:00
|
|
|
qlimit_total = g_settings->getU16("emergequeue_limit_total");
|
2014-02-05 00:17:53 -05:00
|
|
|
if (!g_settings->getU16NoEx("emergequeue_limit_diskonly", qlimit_diskonly))
|
2014-02-03 22:42:10 -05:00
|
|
|
qlimit_diskonly = nthreads * 5 + 1;
|
2014-02-05 00:17:53 -05:00
|
|
|
if (!g_settings->getU16NoEx("emergequeue_limit_generate", qlimit_generate))
|
2014-02-03 22:42:10 -05:00
|
|
|
qlimit_generate = nthreads + 1;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-02-05 00:17:53 -05: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-11 18:22:05 -04:00
|
|
|
for (s16 i = 0; i < nthreads; i++)
|
|
|
|
emergethread.push_back(new EmergeThread((Server *) gamedef, i));
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-17 01:47:49 -05:00
|
|
|
infostream << "EmergeManager: using " << nthreads << " threads" << std::endl;
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
EmergeManager::~EmergeManager()
|
|
|
|
{
|
2014-12-29 22:04:47 -05:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++) {
|
2014-01-26 01:12:18 -05:00
|
|
|
if (threads_active) {
|
|
|
|
emergethread[i]->Stop();
|
|
|
|
emergethread[i]->qevent.signal();
|
|
|
|
emergethread[i]->Wait();
|
|
|
|
}
|
2013-02-17 01:47:49 -05:00
|
|
|
delete emergethread[i];
|
|
|
|
delete mapgen[i];
|
|
|
|
}
|
2013-04-07 15:01:19 -04:00
|
|
|
emergethread.clear();
|
|
|
|
mapgen.clear();
|
|
|
|
|
2014-11-12 23:01:13 -05:00
|
|
|
delete biomemgr;
|
|
|
|
delete oremgr;
|
|
|
|
delete decomgr;
|
|
|
|
delete schemmgr;
|
2014-07-06 09:41:56 -04:00
|
|
|
|
|
|
|
if (params.sparams) {
|
|
|
|
delete params.sparams;
|
|
|
|
params.sparams = NULL;
|
|
|
|
}
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
void EmergeManager::loadMapgenParams()
|
|
|
|
{
|
2014-02-15 18:20:15 -05:00
|
|
|
loadParamsFromSettings(g_settings);
|
|
|
|
|
|
|
|
if (g_settings->get("fixed_map_seed").empty()) {
|
|
|
|
params.seed = (((u64)(myrand() & 0xffff) << 0)
|
|
|
|
| ((u64)(myrand() & 0xffff) << 16)
|
|
|
|
| ((u64)(myrand() & 0xffff) << 32)
|
|
|
|
| ((u64)(myrand() & 0xffff) << 48));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
void EmergeManager::initMapgens()
|
|
|
|
{
|
2013-02-17 01:47:49 -05:00
|
|
|
if (mapgen.size())
|
2013-02-13 22:43:15 -05:00
|
|
|
return;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-02-03 22:42:10 -05: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 17:06:52 -04:00
|
|
|
}
|
2014-02-03 22:42:10 -05:00
|
|
|
params.sparams->readParams(g_settings);
|
2013-06-27 17:06:52 -04:00
|
|
|
}
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-06-27 17:06:52 -04:00
|
|
|
// Create the mapgens
|
2014-12-29 22:04:47 -05:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++) {
|
2014-02-03 22:42:10 -05:00
|
|
|
Mapgen *mg = createMapgen(params.mg_name, i, ¶ms);
|
|
|
|
assert(mg);
|
2013-02-17 01:47:49 -05:00
|
|
|
mapgen.push_back(mg);
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
Mapgen *EmergeManager::getCurrentMapgen()
|
|
|
|
{
|
2014-12-29 22:04:47 -05:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++) {
|
2013-06-26 17:19:39 -04:00
|
|
|
if (emergethread[i]->IsSameThread())
|
|
|
|
return emergethread[i]->mapgen;
|
|
|
|
}
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-06-26 17:19:39 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
void EmergeManager::startThreads()
|
|
|
|
{
|
2014-01-26 01:12:18 -05:00
|
|
|
if (threads_active)
|
|
|
|
return;
|
|
|
|
|
2014-12-29 22:04:47 -05:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++)
|
2013-12-03 17:32:03 -05:00
|
|
|
emergethread[i]->Start();
|
2014-01-26 01:12:18 -05:00
|
|
|
|
|
|
|
threads_active = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
void EmergeManager::stopThreads()
|
|
|
|
{
|
2014-01-26 01:12:18 -05:00
|
|
|
if (!threads_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Request thread stop in parallel
|
2014-12-29 22:04:47 -05:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++) {
|
2014-01-26 01:12:18 -05:00
|
|
|
emergethread[i]->Stop();
|
|
|
|
emergethread[i]->qevent.signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then do the waiting for each
|
2014-12-29 22:04:47 -05:00
|
|
|
for (u32 i = 0; i != emergethread.size(); i++)
|
2014-01-26 01:12:18 -05:00
|
|
|
emergethread[i]->Wait();
|
|
|
|
|
|
|
|
threads_active = false;
|
2013-08-10 22:09:45 -04:00
|
|
|
}
|
|
|
|
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate)
|
|
|
|
{
|
2013-02-14 18:32:56 -05:00
|
|
|
std::map<v3s16, BlockEmergeData *>::const_iterator iter;
|
|
|
|
BlockEmergeData *bedata;
|
|
|
|
u16 count;
|
2013-02-13 22:43:15 -05:00
|
|
|
u8 flags = 0;
|
2013-02-17 01:47:49 -05:00
|
|
|
int idx = 0;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05:00
|
|
|
if (allow_generate)
|
|
|
|
flags |= BLOCK_EMERGE_ALLOWGEN;
|
|
|
|
|
|
|
|
{
|
|
|
|
JMutexAutoLock queuelock(queuemutex);
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-14 18:32:56 -05:00
|
|
|
count = blocks_enqueued.size();
|
2013-02-14 20:36:58 -05:00
|
|
|
if (count >= qlimit_total)
|
2013-02-14 18:32:56 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
count = peer_queue_count[peer_id];
|
2013-02-14 20:36:58 -05:00
|
|
|
u16 qlimit_peer = allow_generate ? qlimit_generate : qlimit_diskonly;
|
|
|
|
if (count >= qlimit_peer)
|
2013-02-14 18:32:56 -05:00
|
|
|
return false;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-14 18:32:56 -05:00
|
|
|
iter = blocks_enqueued.find(p);
|
2013-02-13 22:43:15 -05:00
|
|
|
if (iter != blocks_enqueued.end()) {
|
2013-02-14 18:32:56 -05:00
|
|
|
bedata = iter->second;
|
|
|
|
bedata->flags |= flags;
|
2013-02-13 22:43:15 -05:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-14 18:32:56 -05:00
|
|
|
|
|
|
|
bedata = new BlockEmergeData;
|
|
|
|
bedata->flags = flags;
|
|
|
|
bedata->peer_requested = peer_id;
|
|
|
|
blocks_enqueued.insert(std::make_pair(p, bedata));
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-14 18:32:56 -05:00
|
|
|
peer_queue_count[peer_id] = count + 1;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-21 17:44:14 -05:00
|
|
|
// insert into the EmergeThread queue with the least items
|
2013-02-17 01:47:49 -05:00
|
|
|
int lowestitems = emergethread[0]->blockqueue.size();
|
2014-12-29 22:04:47 -05:00
|
|
|
for (u32 i = 1; i != emergethread.size(); i++) {
|
2013-02-17 01:47:49 -05:00
|
|
|
int nitems = emergethread[i]->blockqueue.size();
|
|
|
|
if (nitems < lowestitems) {
|
|
|
|
idx = i;
|
|
|
|
lowestitems = nitems;
|
|
|
|
}
|
|
|
|
}
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-17 01:47:49 -05:00
|
|
|
emergethread[idx]->blockqueue.push(p);
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
2013-02-17 01:47:49 -05:00
|
|
|
emergethread[idx]->qevent.signal();
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
int EmergeManager::getGroundLevelAtPoint(v2s16 p)
|
|
|
|
{
|
2013-02-21 17:44:14 -05:00
|
|
|
if (mapgen.size() == 0 || !mapgen[0]) {
|
2013-02-17 01:47:49 -05:00
|
|
|
errorstream << "EmergeManager: getGroundLevelAtPoint() called"
|
2013-12-07 23:43:46 -05:00
|
|
|
" before mapgen initialized" << std::endl;
|
2013-02-13 22:43:15 -05:00
|
|
|
return 0;
|
2013-02-17 01:47:49 -05:00
|
|
|
}
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-17 01:47:49 -05:00
|
|
|
return mapgen[0]->getGroundLevelAtPoint(p);
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
bool EmergeManager::isBlockUnderground(v3s16 blockpos)
|
|
|
|
{
|
2013-02-13 22:43:15 -05: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-03 22:42:10 -05:00
|
|
|
return blockpos.Y * (MAP_BLOCKSIZE + 1) <= params.water_level;
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 21:23:51 -05: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 12:58:55 -05:00
|
|
|
MapgenParams *mgparams)
|
|
|
|
{
|
2014-12-29 21:23:51 -05:00
|
|
|
u32 i;
|
|
|
|
for (i = 0; i != ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
|
|
|
|
if (i == ARRLEN(reg_mapgens)) {
|
2013-02-13 22:43:15 -05:00
|
|
|
errorstream << "EmergeManager; mapgen " << mgname <<
|
2014-12-29 12:58:55 -05:00
|
|
|
" not registered" << std::endl;
|
2013-02-13 22:43:15 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-12-29 21:23:51 -05:00
|
|
|
MapgenFactory *mgfactory = reg_mapgens[i].factory;
|
2013-02-17 01:47:49 -05:00
|
|
|
return mgfactory->createMapgen(mgid, mgparams, this);
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 21:23:51 -05:00
|
|
|
MapgenSpecificParams *EmergeManager::createMapgenParams(const std::string &mgname)
|
2014-12-29 12:58:55 -05:00
|
|
|
{
|
2014-12-29 21:23:51 -05:00
|
|
|
u32 i;
|
|
|
|
for (i = 0; i != ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
|
|
|
|
if (i == ARRLEN(reg_mapgens)) {
|
|
|
|
errorstream << "EmergeManager; mapgen " << mgname <<
|
2014-12-29 12:58:55 -05:00
|
|
|
" not registered" << std::endl;
|
2013-02-13 22:43:15 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-12-29 21:23:51 -05:00
|
|
|
MapgenFactory *mgfactory = reg_mapgens[i].factory;
|
2013-02-13 22:43:15 -05:00
|
|
|
return mgfactory->createMapgenParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
void EmergeManager::loadParamsFromSettings(Settings *settings)
|
|
|
|
{
|
2014-02-03 22:42:10 -05:00
|
|
|
std::string seed_str;
|
|
|
|
const char *setname = (settings == g_settings) ? "fixed_map_seed" : "seed";
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-02-05 00:17:53 -05:00
|
|
|
if (settings->getNoEx(setname, seed_str))
|
2014-02-03 22:42:10 -05:00
|
|
|
params.seed = read_seed(seed_str.c_str());
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-02-05 00:17:53 -05:00
|
|
|
settings->getNoEx("mg_name", params.mg_name);
|
|
|
|
settings->getS16NoEx("water_level", params.water_level);
|
|
|
|
settings->getS16NoEx("chunksize", params.chunksize);
|
|
|
|
settings->getFlagStrNoEx("mg_flags", params.flags, flagdesc_mapgen);
|
2014-12-09 00:37:48 -05:00
|
|
|
settings->getNoiseParams("mg_biome_np_heat", params.np_biome_heat);
|
|
|
|
settings->getNoiseParams("mg_biome_np_humidity", params.np_biome_humidity);
|
2014-02-03 22:42:10 -05:00
|
|
|
|
|
|
|
delete params.sparams;
|
|
|
|
params.sparams = createMapgenParams(params.mg_name);
|
|
|
|
if (params.sparams)
|
|
|
|
params.sparams->readParams(settings);
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
void EmergeManager::saveParamsToSettings(Settings *settings)
|
|
|
|
{
|
2014-02-03 22:42:10 -05:00
|
|
|
settings->set("mg_name", params.mg_name);
|
|
|
|
settings->setU64("seed", params.seed);
|
|
|
|
settings->setS16("water_level", params.water_level);
|
|
|
|
settings->setS16("chunksize", params.chunksize);
|
2014-02-08 17:50:26 -05:00
|
|
|
settings->setFlagStr("mg_flags", params.flags, flagdesc_mapgen, (u32)-1);
|
2014-12-09 00:37:48 -05:00
|
|
|
settings->setNoiseParams("mg_biome_np_heat", params.np_biome_heat);
|
|
|
|
settings->setNoiseParams("mg_biome_np_humidity", params.np_biome_humidity);
|
2013-02-13 22:43:15 -05:00
|
|
|
|
2014-02-03 22:42:10 -05:00
|
|
|
if (params.sparams)
|
|
|
|
params.sparams->writeParams(settings);
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-30 19:52:06 -05:00
|
|
|
////////////////////////////// Emerge Thread //////////////////////////////////
|
2013-02-13 22:43:15 -05:00
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags)
|
|
|
|
{
|
2013-02-23 22:45:12 -05:00
|
|
|
std::map<v3s16, BlockEmergeData *>::iterator iter;
|
|
|
|
JMutexAutoLock queuelock(emerge->queuemutex);
|
2013-02-13 22:43:15 -05:00
|
|
|
|
2013-02-23 22:45:12 -05:00
|
|
|
if (blockqueue.empty())
|
|
|
|
return false;
|
|
|
|
v3s16 p = blockqueue.front();
|
|
|
|
blockqueue.pop();
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-23 22:45:12 -05:00
|
|
|
*pos = p;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-23 22:45:12 -05:00
|
|
|
iter = emerge->blocks_enqueued.find(p);
|
2013-11-30 19:52:06 -05:00
|
|
|
if (iter == emerge->blocks_enqueued.end())
|
2013-02-23 22:45:12 -05:00
|
|
|
return false; //uh oh, queue and map out of sync!!
|
2013-02-13 22:43:15 -05:00
|
|
|
|
2013-02-23 22:45:12 -05:00
|
|
|
BlockEmergeData *bedata = iter->second;
|
|
|
|
*flags = bedata->flags;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-23 22:45:12 -05:00
|
|
|
emerge->peer_queue_count[bedata->peer_requested]--;
|
2013-02-13 22:43:15 -05:00
|
|
|
|
2013-02-23 22:45:12 -05:00
|
|
|
delete bedata;
|
|
|
|
emerge->blocks_enqueued.erase(iter);
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-23 22:45:12 -05:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-13 22:43:15 -05:00
|
|
|
|
|
|
|
|
2013-11-30 19:52:06 -05:00
|
|
|
bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
|
2014-12-29 12:58:55 -05:00
|
|
|
BlockMakeData *data, bool allow_gen)
|
|
|
|
{
|
2013-02-13 22:43:15 -05:00
|
|
|
v2s16 p2d(p.X, p.Z);
|
|
|
|
//envlock: usually takes <=1ms, sometimes 90ms or ~400ms to acquire
|
2013-11-30 19:52:06 -05:00
|
|
|
JMutexAutoLock envlock(m_server->m_env_mutex);
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05: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 01:59:04 -05:00
|
|
|
if (block && block->isGenerated())
|
|
|
|
map->prepareBlock(block);
|
2013-02-13 22:43:15 -05: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 02:46:08 -05:00
|
|
|
*b = block;
|
|
|
|
return map->initBlockMake(data, p);
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05:00
|
|
|
*b = block;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 12:58:55 -05:00
|
|
|
void *EmergeThread::Thread()
|
|
|
|
{
|
2013-02-13 22:43:15 -05:00
|
|
|
ThreadStarted();
|
2013-05-19 13:58:01 -04:00
|
|
|
log_register_thread("EmergeThread" + itos(id));
|
2013-02-13 22:43:15 -05:00
|
|
|
DSTACK(__FUNCTION_NAME);
|
|
|
|
BEGIN_DEBUG_EXCEPTION_HANDLER
|
|
|
|
|
|
|
|
v3s16 last_tried_pos(-32768,-32768,-32768); // For error output
|
|
|
|
v3s16 p;
|
2014-04-15 13:49:32 -04:00
|
|
|
u8 flags = 0;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05:00
|
|
|
map = (ServerMap *)&(m_server->m_env->getMap());
|
|
|
|
emerge = m_server->m_emerge;
|
2013-02-17 02:46:08 -05:00
|
|
|
mapgen = emerge->mapgen[id];
|
|
|
|
enable_mapgen_debug_info = emerge->mapgen_debug_info;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2014-04-05 09:27:33 -04:00
|
|
|
porting::setThreadName("EmergeThread");
|
|
|
|
|
2013-12-03 17:32:03 -05:00
|
|
|
while (!StopRequested())
|
2013-02-13 22:43:15 -05:00
|
|
|
try {
|
2013-02-17 02:46:08 -05:00
|
|
|
if (!popBlockEmerge(&p, &flags)) {
|
2013-02-13 22:43:15 -05:00
|
|
|
qevent.wait();
|
2013-02-17 02:46:08 -05:00
|
|
|
continue;
|
2013-02-14 19:22:43 -05:00
|
|
|
}
|
2013-02-13 22:43:15 -05: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-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05: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 12:19:49 -05:00
|
|
|
std::map<v3s16, MapBlock *> modified_blocks;
|
2013-12-07 23:43:46 -05:00
|
|
|
|
|
|
|
if (getBlockOrStartGen(p, &block, &data, allow_generate) && mapgen) {
|
2013-02-13 22:43:15 -05: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-11-30 19:52:06 -05:00
|
|
|
JMutexAutoLock envlock(m_server->m_env_mutex);
|
2013-02-13 22:43:15 -05:00
|
|
|
ScopeProfiler sp(g_profiler, "EmergeThread: after "
|
2013-02-14 19:22:43 -05:00
|
|
|
"Mapgen::makeChunk (envlock)", SPT_AVG);
|
2013-02-13 22:43:15 -05:00
|
|
|
|
|
|
|
map->finishBlockMake(&data, modified_blocks);
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05: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-11-30 19:52:06 -05:00
|
|
|
MapEditEventAreaIgnorer
|
2013-02-13 22:43:15 -05:00
|
|
|
ign(&m_server->m_ignore_map_edit_events_area,
|
|
|
|
VoxelArea(minp, maxp));
|
2013-12-14 01:59:16 -05:00
|
|
|
try { // takes about 90ms with -O1 on an e3-1230v2
|
2013-09-16 22:52:24 -04:00
|
|
|
m_server->getScriptIface()->environment_OnGenerated(
|
2014-12-29 21:44:52 -05:00
|
|
|
minp, maxp, mapgen->blockseed);
|
2013-12-14 01:59:16 -05:00
|
|
|
} catch(LuaError &e) {
|
|
|
|
m_server->setAsyncFatalError(e.what());
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
EMERGE_DBG_OUT("ended up with: " << analyze_block(block));
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05: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 12:19:49 -05:00
|
|
|
modified_blocks[p] = block;
|
2013-02-13 22:43:15 -05:00
|
|
|
|
2014-01-30 18:24:00 -05:00
|
|
|
if (modified_blocks.size() > 0) {
|
|
|
|
m_server->SetBlocksNotSent(modified_blocks);
|
2013-02-13 22:43:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (VersionMismatchException &e) {
|
|
|
|
std::ostringstream err;
|
|
|
|
err << "World data version mismatch in MapBlock "<<PP(last_tried_pos)<<std::endl;
|
|
|
|
err << "----"<<std::endl;
|
|
|
|
err << "\""<<e.what()<<"\""<<std::endl;
|
|
|
|
err << "See debug.txt."<<std::endl;
|
|
|
|
err << "World probably saved by a newer version of Minetest."<<std::endl;
|
|
|
|
m_server->setAsyncFatalError(err.str());
|
|
|
|
}
|
|
|
|
catch (SerializationError &e) {
|
|
|
|
std::ostringstream err;
|
|
|
|
err << "Invalid data in MapBlock "<<PP(last_tried_pos)<<std::endl;
|
|
|
|
err << "----"<<std::endl;
|
|
|
|
err << "\""<<e.what()<<"\""<<std::endl;
|
|
|
|
err << "See debug.txt."<<std::endl;
|
|
|
|
err << "You can ignore this using [ignore_world_load_errors = true]."<<std::endl;
|
|
|
|
m_server->setAsyncFatalError(err.str());
|
|
|
|
}
|
2013-12-07 23:43:46 -05:00
|
|
|
|
2013-02-13 22:43:15 -05:00
|
|
|
END_DEBUG_EXCEPTION_HANDLER(errorstream)
|
|
|
|
log_deregister_thread();
|
|
|
|
return NULL;
|
|
|
|
}
|