/*
-Minetest-c55
-Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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
*/
+#include "emerge.h"
#include "server.h"
#include <iostream>
#include <queue>
-#include "clientserver.h"
#include "map.h"
-#include "jmutexautolock.h"
+#include "environment.h"
+#include "util/container.h"
+#include "util/thread.h"
#include "main.h"
#include "constants.h"
#include "voxel.h"
#include "mapblock.h"
#include "serverobject.h"
#include "settings.h"
-#include "script.h"
-#include "scriptapi.h"
+#include "scripting_game.h"
#include "profiler.h"
#include "log.h"
#include "nodedef.h"
#include "biome.h"
-#include "emerge.h"
#include "mapgen_v6.h"
+#include "mapgen_v7.h"
+#include "mapgen_indev.h"
+#include "mapgen_singlenode.h"
+#include "mapgen_math.h"
-EmergeManager::EmergeManager(IGameDef *gamedef, BiomeDefManager *bdef) {
+class EmergeThread : public SimpleThread
+{
+public:
+ Server *m_server;
+ ServerMap *map;
+ EmergeManager *emerge;
+ Mapgen *mapgen;
+ bool enable_mapgen_debug_info;
+ int id;
+
+ Event qevent;
+ std::queue<v3s16> blockqueue;
+
+ EmergeThread(Server *server, int ethreadid):
+ SimpleThread(),
+ m_server(server),
+ map(NULL),
+ emerge(NULL),
+ mapgen(NULL),
+ id(ethreadid)
+ {
+ }
+
+ void *Thread();
+
+ void trigger()
+ {
+ setRun(true);
+ if(IsRunning() == false)
+ {
+ Start();
+ }
+ }
+
+ bool popBlockEmerge(v3s16 *pos, u8 *flags);
+ bool getBlockOrStartGen(v3s16 p, MapBlock **b,
+ BlockMakeData *data, bool allow_generate);
+};
+
+
+/////////////////////////////// Emerge Manager ////////////////////////////////
+
+EmergeManager::EmergeManager(IGameDef *gamedef) {
//register built-in mapgens
registerMapgen("v6", new MapgenFactoryV6());
+ registerMapgen("v7", new MapgenFactoryV7());
+ registerMapgen("indev", new MapgenFactoryIndev());
+ registerMapgen("singlenode", new MapgenFactorySinglenode());
+ registerMapgen("math", new MapgenFactoryMath());
- this->biomedef = bdef ? bdef : new BiomeDefManager(gamedef);
+ this->ndef = gamedef->getNodeDefManager();
+ this->biomedef = new BiomeDefManager();
this->params = NULL;
+ this->luaoverride_params = NULL;
+ this->luaoverride_params_modified = 0;
+ this->luaoverride_flagmask = 0;
+
mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
queuemutex.Init();
if (g_settings->get("num_emerge_threads").empty()) {
int nprocs = porting::getNumberOfProcessors();
// leave a proc for the main thread and one for some other misc threads
- nthreads = (nprocs > 2) ? nthreads = nprocs - 2 : 1;
+ nthreads = (nprocs > 2) ? nprocs - 2 : 1;
} else {
nthreads = g_settings->getU16("num_emerge_threads");
}
EmergeManager::~EmergeManager() {
- for (int i = 0; i != emergethread.size(); i++) {
+ for (unsigned int i = 0; i != emergethread.size(); i++) {
emergethread[i]->setRun(false);
emergethread[i]->qevent.signal();
emergethread[i]->stop();
delete emergethread[i];
delete mapgen[i];
}
+ emergethread.clear();
+ mapgen.clear();
+
+ for (unsigned int i = 0; i < ores.size(); i++)
+ delete ores[i];
+ ores.clear();
+
+ for (unsigned int i = 0; i < decorations.size(); i++)
+ delete decorations[i];
+ decorations.clear();
+ for (std::map<std::string, MapgenFactory *>::iterator iter = mglist.begin();
+ iter != mglist.end(); iter ++) {
+ delete iter->second;
+ }
+ mglist.clear();
+
delete biomedef;
- delete params;
}
if (mapgen.size())
return;
+ // Resolve names of nodes for things that were registered
+ // (at this point, the registration period is over)
+ biomedef->resolveNodeNames(ndef);
+
+ for (size_t i = 0; i != ores.size(); i++)
+ ores[i]->resolveNodeNames(ndef);
+
+ for (size_t i = 0; i != decorations.size(); i++)
+ decorations[i]->resolveNodeNames(ndef);
+
+ // Apply mapgen parameter overrides from Lua
+ if (luaoverride_params) {
+ if (luaoverride_params_modified & MGPARAMS_SET_MGNAME)
+ mgparams->mg_name = luaoverride_params->mg_name;
+
+ if (luaoverride_params_modified & MGPARAMS_SET_SEED)
+ mgparams->seed = luaoverride_params->seed;
+
+ if (luaoverride_params_modified & MGPARAMS_SET_WATER_LEVEL)
+ mgparams->water_level = luaoverride_params->water_level;
+
+ if (luaoverride_params_modified & MGPARAMS_SET_FLAGS) {
+ mgparams->flags &= ~luaoverride_flagmask;
+ mgparams->flags |= luaoverride_params->flags;
+ }
+
+ delete luaoverride_params;
+ luaoverride_params = NULL;
+ }
+
+ // Create the mapgens
this->params = mgparams;
- for (int i = 0; i != emergethread.size(); i++) {
+ for (size_t i = 0; i != emergethread.size(); i++) {
mg = createMapgen(params->mg_name, 0, params);
if (!mg) {
infostream << "EmergeManager: falling back to mapgen v6" << std::endl;
}
+Mapgen *EmergeManager::getCurrentMapgen() {
+ for (unsigned int i = 0; i != emergethread.size(); i++) {
+ if (emergethread[i]->IsSameThread())
+ return emergethread[i]->mapgen;
+ }
+
+ return NULL;
+}
+
+
+void EmergeManager::triggerAllThreads() {
+ for (unsigned int i = 0; i != emergethread.size(); i++)
+ emergethread[i]->trigger();
+}
+
bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate) {
std::map<v3s16, BlockEmergeData *>::const_iterator iter;
BlockEmergeData *bedata;
// insert into the EmergeThread queue with the least items
int lowestitems = emergethread[0]->blockqueue.size();
- for (int i = 1; i != emergethread.size(); i++) {
+ for (unsigned int i = 1; i != emergethread.size(); i++) {
int nitems = emergethread[i]->blockqueue.size();
if (nitems < lowestitems) {
idx = i;
}
-bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) {
- std::map<v3s16, BlockEmergeData *>::iterator iter;
- JMutexAutoLock queuelock(emerge->queuemutex);
-
- if (blockqueue.empty())
- return false;
- v3s16 p = blockqueue.front();
- blockqueue.pop();
-
- *pos = p;
-
- iter = emerge->blocks_enqueued.find(p);
- if (iter == emerge->blocks_enqueued.end())
- return false; //uh oh, queue and map out of sync!!
-
- BlockEmergeData *bedata = iter->second;
- *flags = bedata->flags;
-
- emerge->peer_queue_count[bedata->peer_requested]--;
-
- delete bedata;
- emerge->blocks_enqueued.erase(iter);
-
- return true;
-}
-
-
int EmergeManager::getGroundLevelAtPoint(v2s16 p) {
if (mapgen.size() == 0 || !mapgen[0]) {
errorstream << "EmergeManager: getGroundLevelAtPoint() called"
MapgenParams *EmergeManager::getParamsFromSettings(Settings *settings) {
std::string mg_name = settings->get("mg_name");
MapgenParams *mgparams = createMapgenParams(mg_name);
+ if (!mgparams)
+ return NULL;
mgparams->mg_name = mg_name;
mgparams->seed = settings->getU64(settings == g_settings ? "fixed_map_seed" : "seed");
}
-bool EmergeManager::registerMapgen(std::string mgname, MapgenFactory *mgfactory) {
+void EmergeManager::registerMapgen(std::string mgname, MapgenFactory *mgfactory) {
mglist.insert(std::make_pair(mgname, mgfactory));
infostream << "EmergeManager: registered mapgen " << mgname << std::endl;
}
+////////////////////////////// Emerge Thread //////////////////////////////////
-class MapEditEventIgnorer
-{
-public:
- MapEditEventIgnorer(bool *flag):
- m_flag(flag)
- {
- if(*m_flag == false)
- *m_flag = true;
- else
- m_flag = NULL;
- }
-
- ~MapEditEventIgnorer()
- {
- if(m_flag)
- {
- assert(*m_flag);
- *m_flag = false;
- }
- }
-
-private:
- bool *m_flag;
-};
-
-class MapEditEventAreaIgnorer
-{
-public:
- MapEditEventAreaIgnorer(VoxelArea *ignorevariable, const VoxelArea &a):
- m_ignorevariable(ignorevariable)
- {
- if(m_ignorevariable->getVolume() == 0)
- *m_ignorevariable = a;
- else
- m_ignorevariable = NULL;
- }
+bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) {
+ std::map<v3s16, BlockEmergeData *>::iterator iter;
+ JMutexAutoLock queuelock(emerge->queuemutex);
- ~MapEditEventAreaIgnorer()
- {
- if(m_ignorevariable)
- {
- assert(m_ignorevariable->getVolume() != 0);
- *m_ignorevariable = VoxelArea();
- }
- }
+ if (blockqueue.empty())
+ return false;
+ v3s16 p = blockqueue.front();
+ blockqueue.pop();
+
+ *pos = p;
+
+ iter = emerge->blocks_enqueued.find(p);
+ if (iter == emerge->blocks_enqueued.end())
+ return false; //uh oh, queue and map out of sync!!
-private:
- VoxelArea *m_ignorevariable;
-};
+ BlockEmergeData *bedata = iter->second;
+ *flags = bedata->flags;
+
+ emerge->peer_queue_count[bedata->peer_requested]--;
+ delete bedata;
+ emerge->blocks_enqueued.erase(iter);
+
+ return true;
+}
-#if 1
bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
BlockMakeData *data, bool allow_gen) {
void *EmergeThread::Thread() {
ThreadStarted();
- log_register_thread("EmergeThread" + id);
+ log_register_thread("EmergeThread" + itos(id));
DSTACK(__FUNCTION_NAME);
BEGIN_DEBUG_EXCEPTION_HANDLER
*/
BlockMakeData data;
MapBlock *block = NULL;
- core::map<v3s16, MapBlock *> modified_blocks;
+ std::map<v3s16, MapBlock *> modified_blocks;
if (getBlockOrStartGen(p, &block, &data, allow_generate)) {
{
ign(&m_server->m_ignore_map_edit_events_area,
VoxelArea(minp, maxp));
{ // takes about 90ms with -O1 on an e3-1230v2
- scriptapi_environment_on_generated(m_server->m_lua,
+ m_server->getScriptIface()->environment_OnGenerated(
minp, maxp, emerge->getBlockSeed(minp));
}
JMutexAutoLock lock(m_server->m_con_mutex);
// Add the originally fetched block to the modified list
if (block)
- modified_blocks.insert(p, block);
+ modified_blocks[p] = block;
// Set the modified blocks unsent for all the clients
- for (core::map<u16, RemoteClient*>::Iterator
- i = m_server->m_clients.getIterator();
- i.atEnd() == false; i++) {
- RemoteClient *client = i.getNode()->getValue();
+ for (std::map<u16, RemoteClient*>::iterator
+ i = m_server->m_clients.begin();
+ i != m_server->m_clients.end(); ++i) {
+ RemoteClient *client = i->second;
if (modified_blocks.size() > 0) {
// Remove block from sent history
client->SetBlocksNotSent(modified_blocks);
log_deregister_thread();
return NULL;
}
-
-#else
-
-void *EmergeThread::Thread() {
- ThreadStarted();
- log_register_thread("EmergeThread");
- DSTACK(__FUNCTION_NAME);
- BEGIN_DEBUG_EXCEPTION_HANDLER
-
- bool enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
-
- v3s16 last_tried_pos(-32768,-32768,-32768); // For error output
- ServerMap &map = ((ServerMap&)m_server->m_env->getMap());
- EmergeManager *emerge = m_server->m_emerge;
- Mapgen *mapgen = emerge->getMapgen();
-
- while(getRun())
- try {
- QueuedBlockEmerge *qptr = m_server->m_emerge_queue.pop();
- if(qptr == NULL)
- break;
- SharedPtr<QueuedBlockEmerge> q(qptr);
-
- v3s16 &p = q->pos;
- v2s16 p2d(p.X,p.Z);
-
- last_tried_pos = p;
-
- /*
- Do not generate over-limit
- */
- if (blockpos_over_limit(p))
- continue;
-
- //infostream<<"EmergeThread::Thread(): running"<<std::endl;
-
- //TimeTaker timer("block emerge");
-
- /*
- Try to emerge it from somewhere.
-
- If it is only wanted as optional, only loading from disk
- will be allowed.
- */
-
- /*
- Check if any peer wants it as non-optional. In that case it
- will be generated.
-
- Also decrement the emerge queue count in clients.
- */
-
- bool only_from_disk = true;
- {
- core::map<u16, u8>::Iterator i;
- for (i=q->s.getIterator(); !i.atEnd(); i++) {
- u8 flags = i.getNode()->getValue();
- if (!(flags & BLOCK_EMERGE_FLAG_FROMDISK)) {
- only_from_disk = false;
- break;
- }
- }
- }
-
- if (enable_mapgen_debug_info)
- infostream<<"EmergeThread: p="
- <<"("<<p.X<<","<<p.Y<<","<<p.Z<<") "
- <<"only_from_disk="<<only_from_disk<<std::endl;
-
- MapBlock *block = NULL;
- bool got_block = true;
- core::map<v3s16, MapBlock*> modified_blocks;
-
- /*
- Try to fetch block from memory or disk.
- If not found and asked to generate, initialize generator.
- */
-
- bool started_generate = false;
- BlockMakeData data;
- {
- JMutexAutoLock envlock(m_server->m_env_mutex);
-
- // Load sector if it isn't loaded
- if(map.getSectorNoGenerateNoEx(p2d) == NULL)
- map.loadSectorMeta(p2d);
-
- // Attempt to load block
- block = map.getBlockNoCreateNoEx(p);
- if(!block || block->isDummy() || !block->isGenerated()) {
- if(enable_mapgen_debug_info)
- infostream<<"EmergeThread: not in memory, "
- <<"attempting to load from disk"<<std::endl;
-
- block = map.loadBlock(p);
- }
-
- // If could not load and allowed to generate, start generation
- // inside this same envlock
- if(only_from_disk == false &&
- (block == NULL || block->isGenerated() == false)){
- if(enable_mapgen_debug_info)
- infostream<<"EmergeThread: generating"<<std::endl;
- started_generate = true;
-
- map.initBlockMake(&data, p);
- }
- }
-
- /*
- If generator was initialized, generate now when envlock is free.
- */
- if(started_generate) {
- {
- ScopeProfiler sp(g_profiler, "EmergeThread: mapgen::make_block",
- SPT_AVG);
- TimeTaker t("mapgen::make_block()");
-
- mapgen->makeChunk(&data);
-
- if (enable_mapgen_debug_info == false)
- t.stop(true); // Hide output
- }
-
- do{ // enable break
- // Lock environment again to access the map
- JMutexAutoLock envlock(m_server->m_env_mutex);
-
- ScopeProfiler sp(g_profiler, "EmergeThread: after "
- "mapgen::make_block (envlock)", SPT_AVG);
-
- // Blit data back on map, update lighting, add mobs and
- // whatever this does
- map.finishBlockMake(&data, modified_blocks);
-
- // Get central block
- block = map.getBlockNoCreateNoEx(p);
-
- // If block doesn't exist, don't try doing anything with it
- // This happens if the block is not in generation boundaries
- if(!block)
- break;
-
- /*
- 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
- */
- MapEditEventAreaIgnorer ign(
- &m_server->m_ignore_map_edit_events_area,
- VoxelArea(minp, maxp));
- {
- TimeTaker timer("on_generated");
- scriptapi_environment_on_generated(m_server->m_lua,
- minp, maxp, emerge->getBlockSeed(minp));
- //int t = timer.stop(true);
- //dstream<<"on_generated took "<<t<<"ms"<<std::endl;
- }
-
- if (enable_mapgen_debug_info)
- infostream << "EmergeThread: ended up with: "
- << analyze_block(block) << std::endl;
-
- // Activate objects and stuff
- m_server->m_env->activateBlock(block, 0);
- }while(false);
- }
-
- if(block == NULL)
- got_block = false;
-
- /*
- Set sent status of modified blocks on clients
- */
-
- // NOTE: Server's clients are also behind the connection mutex
- JMutexAutoLock lock(m_server->m_con_mutex);
-
- /*
- Add the originally fetched block to the modified list
- */
- if(got_block)
- modified_blocks.insert(p, block);
-
- /*
- Set the modified blocks unsent for all the clients
- */
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_server->m_clients.getIterator();
- i.atEnd() == false; i++) {
- RemoteClient *client = i.getNode()->getValue();
- if(modified_blocks.size() > 0) {
- // Remove block from sent history
- client->SetBlocksNotSent(modified_blocks);
- }
- }
-
-
-niters++;
- }
- 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());
- }
-printf("emergethread iterated %d times\n", niters);
- END_DEBUG_EXCEPTION_HANDLER(errorstream)
- log_deregister_thread();
- return NULL;
-}
-
-#endif