1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15 
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 
21 
22 #include "emerge.h"
23 
24 #include <iostream>
25 #include <queue>
26 
27 #include "util/container.h"
28 #include "util/thread.h"
29 #include "threading/event.h"
30 
31 #include "config.h"
32 #include "constants.h"
33 #include "environment.h"
34 #include "log.h"
35 #include "map.h"
36 #include "mapblock.h"
37 #include "mapgen/mg_biome.h"
38 #include "mapgen/mg_ore.h"
39 #include "mapgen/mg_decoration.h"
40 #include "mapgen/mg_schematic.h"
41 #include "nodedef.h"
42 #include "profiler.h"
43 #include "scripting_server.h"
44 #include "server.h"
45 #include "settings.h"
46 #include "voxel.h"
47 
48 class EmergeThread : public Thread {
49 public:
50 	bool enable_mapgen_debug_info;
51 	int id;
52 
53 	EmergeThread(Server *server, int ethreadid);
54 	~EmergeThread() = default;
55 
56 	void *run();
57 	void signal();
58 
59 	// Requires queue mutex held
60 	bool pushBlock(const v3s16 &pos);
61 
62 	void cancelPendingItems();
63 
64 	static void runCompletionCallbacks(
65 		const v3s16 &pos, EmergeAction action,
66 		const EmergeCallbackList &callbacks);
67 
68 private:
69 	Server *m_server;
70 	ServerMap *m_map;
71 	EmergeManager *m_emerge;
72 	Mapgen *m_mapgen;
73 
74 	Event m_queue_event;
75 	std::queue<v3s16> m_block_queue;
76 
77 	bool popBlockEmerge(v3s16 *pos, BlockEmergeData *bedata);
78 
79 	EmergeAction getBlockOrStartGen(
80 		const v3s16 &pos, bool allow_gen, MapBlock **block, BlockMakeData *data);
81 	MapBlock *finishGen(v3s16 pos, BlockMakeData *bmdata,
82 		std::map<v3s16, MapBlock *> *modified_blocks);
83 
84 	friend class EmergeManager;
85 };
86 
87 class MapEditEventAreaIgnorer
88 {
89 public:
MapEditEventAreaIgnorer(VoxelArea * ignorevariable,const VoxelArea & a)90 	MapEditEventAreaIgnorer(VoxelArea *ignorevariable, const VoxelArea &a):
91 		m_ignorevariable(ignorevariable)
92 	{
93 		if(m_ignorevariable->getVolume() == 0)
94 			*m_ignorevariable = a;
95 		else
96 			m_ignorevariable = NULL;
97 	}
98 
~MapEditEventAreaIgnorer()99 	~MapEditEventAreaIgnorer()
100 	{
101 		if(m_ignorevariable)
102 		{
103 			assert(m_ignorevariable->getVolume() != 0);
104 			*m_ignorevariable = VoxelArea();
105 		}
106 	}
107 
108 private:
109 	VoxelArea *m_ignorevariable;
110 };
111 
~EmergeParams()112 EmergeParams::~EmergeParams()
113 {
114 	infostream << "EmergeParams: destroying " << this << std::endl;
115 	// Delete everything that was cloned on creation of EmergeParams
116 	delete biomemgr;
117 	delete oremgr;
118 	delete decomgr;
119 	delete schemmgr;
120 }
121 
EmergeParams(EmergeManager * parent,const BiomeManager * biomemgr,const OreManager * oremgr,const DecorationManager * decomgr,const SchematicManager * schemmgr)122 EmergeParams::EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
123 	const OreManager *oremgr, const DecorationManager *decomgr,
124 	const SchematicManager *schemmgr) :
125 	ndef(parent->ndef),
126 	enable_mapgen_debug_info(parent->enable_mapgen_debug_info),
127 	gen_notify_on(parent->gen_notify_on),
128 	gen_notify_on_deco_ids(&parent->gen_notify_on_deco_ids),
129 	biomemgr(biomemgr->clone()), oremgr(oremgr->clone()),
130 	decomgr(decomgr->clone()), schemmgr(schemmgr->clone())
131 {
132 }
133 
134 ////
135 //// EmergeManager
136 ////
137 
EmergeManager(Server * server)138 EmergeManager::EmergeManager(Server *server)
139 {
140 	this->ndef      = server->getNodeDefManager();
141 	this->biomemgr  = new BiomeManager(server);
142 	this->oremgr    = new OreManager(server);
143 	this->decomgr   = new DecorationManager(server);
144 	this->schemmgr  = new SchematicManager(server);
145 
146 	// Note that accesses to this variable are not synchronized.
147 	// This is because the *only* thread ever starting or stopping
148 	// EmergeThreads should be the ServerThread.
149 
150 	enable_mapgen_debug_info = g_settings->getBool("enable_mapgen_debug_info");
151 
152 	s16 nthreads = 1;
153 	g_settings->getS16NoEx("num_emerge_threads", nthreads);
154 	// If automatic, leave a proc for the main thread and one for
155 	// some other misc thread
156 	if (nthreads == 0)
157 		nthreads = Thread::getNumberOfProcessors() - 2;
158 	if (nthreads < 1)
159 		nthreads = 1;
160 
161 	m_qlimit_total = g_settings->getU16("emergequeue_limit_total");
162 	// FIXME: these fallback values are probably not good
163 	if (!g_settings->getU16NoEx("emergequeue_limit_diskonly", m_qlimit_diskonly))
164 		m_qlimit_diskonly = nthreads * 5 + 1;
165 	if (!g_settings->getU16NoEx("emergequeue_limit_generate", m_qlimit_generate))
166 		m_qlimit_generate = nthreads + 1;
167 
168 	// don't trust user input for something very important like this
169 	if (m_qlimit_total < 1)
170 		m_qlimit_total = 1;
171 	if (m_qlimit_diskonly < 1)
172 		m_qlimit_diskonly = 1;
173 	if (m_qlimit_generate < 1)
174 		m_qlimit_generate = 1;
175 
176 	for (s16 i = 0; i < nthreads; i++)
177 		m_threads.push_back(new EmergeThread(server, i));
178 
179 	infostream << "EmergeManager: using " << nthreads << " threads" << std::endl;
180 }
181 
182 
~EmergeManager()183 EmergeManager::~EmergeManager()
184 {
185 	for (u32 i = 0; i != m_threads.size(); i++) {
186 		EmergeThread *thread = m_threads[i];
187 
188 		if (m_threads_active) {
189 			thread->stop();
190 			thread->signal();
191 			thread->wait();
192 		}
193 
194 		delete thread;
195 
196 		// Mapgen init might not be finished if there is an error during startup.
197 		if (m_mapgens.size() > i)
198 			delete m_mapgens[i];
199 	}
200 
201 	delete biomemgr;
202 	delete oremgr;
203 	delete decomgr;
204 	delete schemmgr;
205 }
206 
207 
getWritableBiomeManager()208 BiomeManager *EmergeManager::getWritableBiomeManager()
209 {
210 	FATAL_ERROR_IF(!m_mapgens.empty(),
211 		"Writable managers can only be returned before mapgen init");
212 	return biomemgr;
213 }
214 
getWritableOreManager()215 OreManager *EmergeManager::getWritableOreManager()
216 {
217 	FATAL_ERROR_IF(!m_mapgens.empty(),
218 		"Writable managers can only be returned before mapgen init");
219 	return oremgr;
220 }
221 
getWritableDecorationManager()222 DecorationManager *EmergeManager::getWritableDecorationManager()
223 {
224 	FATAL_ERROR_IF(!m_mapgens.empty(),
225 		"Writable managers can only be returned before mapgen init");
226 	return decomgr;
227 }
228 
getWritableSchematicManager()229 SchematicManager *EmergeManager::getWritableSchematicManager()
230 {
231 	FATAL_ERROR_IF(!m_mapgens.empty(),
232 		"Writable managers can only be returned before mapgen init");
233 	return schemmgr;
234 }
235 
236 
initMapgens(MapgenParams * params)237 void EmergeManager::initMapgens(MapgenParams *params)
238 {
239 	FATAL_ERROR_IF(!m_mapgens.empty(), "Mapgen already initialised.");
240 
241 	mgparams = params;
242 
243 	for (u32 i = 0; i != m_threads.size(); i++) {
244 		EmergeParams *p = new EmergeParams(
245 			this, biomemgr, oremgr, decomgr, schemmgr);
246 		infostream << "EmergeManager: Created params " << p
247 			<< " for thread " << i << std::endl;
248 		m_mapgens.push_back(Mapgen::createMapgen(params->mgtype, params, p));
249 	}
250 }
251 
252 
getCurrentMapgen()253 Mapgen *EmergeManager::getCurrentMapgen()
254 {
255 	if (!m_threads_active)
256 		return nullptr;
257 
258 	for (u32 i = 0; i != m_threads.size(); i++) {
259 		EmergeThread *t = m_threads[i];
260 		if (t->isRunning() && t->isCurrentThread())
261 			return t->m_mapgen;
262 	}
263 
264 	return nullptr;
265 }
266 
267 
startThreads()268 void EmergeManager::startThreads()
269 {
270 	if (m_threads_active)
271 		return;
272 
273 	for (u32 i = 0; i != m_threads.size(); i++)
274 		m_threads[i]->start();
275 
276 	m_threads_active = true;
277 }
278 
279 
stopThreads()280 void EmergeManager::stopThreads()
281 {
282 	if (!m_threads_active)
283 		return;
284 
285 	// Request thread stop in parallel
286 	for (u32 i = 0; i != m_threads.size(); i++) {
287 		m_threads[i]->stop();
288 		m_threads[i]->signal();
289 	}
290 
291 	// Then do the waiting for each
292 	for (u32 i = 0; i != m_threads.size(); i++)
293 		m_threads[i]->wait();
294 
295 	m_threads_active = false;
296 }
297 
298 
isRunning()299 bool EmergeManager::isRunning()
300 {
301 	return m_threads_active;
302 }
303 
304 
enqueueBlockEmerge(session_t peer_id,v3s16 blockpos,bool allow_generate,bool ignore_queue_limits)305 bool EmergeManager::enqueueBlockEmerge(
306 	session_t peer_id,
307 	v3s16 blockpos,
308 	bool allow_generate,
309 	bool ignore_queue_limits)
310 {
311 	u16 flags = 0;
312 	if (allow_generate)
313 		flags |= BLOCK_EMERGE_ALLOW_GEN;
314 	if (ignore_queue_limits)
315 		flags |= BLOCK_EMERGE_FORCE_QUEUE;
316 
317 	return enqueueBlockEmergeEx(blockpos, peer_id, flags, NULL, NULL);
318 }
319 
320 
enqueueBlockEmergeEx(v3s16 blockpos,session_t peer_id,u16 flags,EmergeCompletionCallback callback,void * callback_param)321 bool EmergeManager::enqueueBlockEmergeEx(
322 	v3s16 blockpos,
323 	session_t peer_id,
324 	u16 flags,
325 	EmergeCompletionCallback callback,
326 	void *callback_param)
327 {
328 	EmergeThread *thread = NULL;
329 	bool entry_already_exists = false;
330 
331 	{
332 		MutexAutoLock queuelock(m_queue_mutex);
333 
334 		if (!pushBlockEmergeData(blockpos, peer_id, flags,
335 				callback, callback_param, &entry_already_exists))
336 			return false;
337 
338 		if (entry_already_exists)
339 			return true;
340 
341 		thread = getOptimalThread();
342 		thread->pushBlock(blockpos);
343 	}
344 
345 	thread->signal();
346 
347 	return true;
348 }
349 
350 
351 //
352 // Mapgen-related helper functions
353 //
354 
355 
356 // TODO(hmmmm): Move this to ServerMap
getContainingChunk(v3s16 blockpos)357 v3s16 EmergeManager::getContainingChunk(v3s16 blockpos)
358 {
359 	return getContainingChunk(blockpos, mgparams->chunksize);
360 }
361 
362 // TODO(hmmmm): Move this to ServerMap
getContainingChunk(v3s16 blockpos,s16 chunksize)363 v3s16 EmergeManager::getContainingChunk(v3s16 blockpos, s16 chunksize)
364 {
365 	s16 coff = -chunksize / 2;
366 	v3s16 chunk_offset(coff, coff, coff);
367 
368 	return getContainerPos(blockpos - chunk_offset, chunksize)
369 		* chunksize + chunk_offset;
370 }
371 
372 
getSpawnLevelAtPoint(v2s16 p)373 int EmergeManager::getSpawnLevelAtPoint(v2s16 p)
374 {
375 	if (m_mapgens.empty() || !m_mapgens[0]) {
376 		errorstream << "EmergeManager: getSpawnLevelAtPoint() called"
377 			" before mapgen init" << std::endl;
378 		return 0;
379 	}
380 
381 	return m_mapgens[0]->getSpawnLevelAtPoint(p);
382 }
383 
384 
getGroundLevelAtPoint(v2s16 p)385 int EmergeManager::getGroundLevelAtPoint(v2s16 p)
386 {
387 	if (m_mapgens.empty() || !m_mapgens[0]) {
388 		errorstream << "EmergeManager: getGroundLevelAtPoint() called"
389 			" before mapgen init" << std::endl;
390 		return 0;
391 	}
392 
393 	return m_mapgens[0]->getGroundLevelAtPoint(p);
394 }
395 
396 // TODO(hmmmm): Move this to ServerMap
isBlockUnderground(v3s16 blockpos)397 bool EmergeManager::isBlockUnderground(v3s16 blockpos)
398 {
399 	// Use a simple heuristic
400 	return blockpos.Y * (MAP_BLOCKSIZE + 1) <= mgparams->water_level;
401 }
402 
pushBlockEmergeData(v3s16 pos,u16 peer_requested,u16 flags,EmergeCompletionCallback callback,void * callback_param,bool * entry_already_exists)403 bool EmergeManager::pushBlockEmergeData(
404 	v3s16 pos,
405 	u16 peer_requested,
406 	u16 flags,
407 	EmergeCompletionCallback callback,
408 	void *callback_param,
409 	bool *entry_already_exists)
410 {
411 	u16 &count_peer = m_peer_queue_count[peer_requested];
412 
413 	if ((flags & BLOCK_EMERGE_FORCE_QUEUE) == 0) {
414 		if (m_blocks_enqueued.size() >= m_qlimit_total)
415 			return false;
416 
417 		if (peer_requested != PEER_ID_INEXISTENT) {
418 			u16 qlimit_peer = (flags & BLOCK_EMERGE_ALLOW_GEN) ?
419 				m_qlimit_generate : m_qlimit_diskonly;
420 			if (count_peer >= qlimit_peer)
421 				return false;
422 		} else {
423 			// limit block enqueue requests for active blocks to 1/2 of total
424 			if (count_peer * 2 >= m_qlimit_total)
425 				return false;
426 		}
427 	}
428 
429 	std::pair<std::map<v3s16, BlockEmergeData>::iterator, bool> findres;
430 	findres = m_blocks_enqueued.insert(std::make_pair(pos, BlockEmergeData()));
431 
432 	BlockEmergeData &bedata = findres.first->second;
433 	*entry_already_exists   = !findres.second;
434 
435 	if (callback)
436 		bedata.callbacks.emplace_back(callback, callback_param);
437 
438 	if (*entry_already_exists) {
439 		bedata.flags |= flags;
440 	} else {
441 		bedata.flags = flags;
442 		bedata.peer_requested = peer_requested;
443 
444 		count_peer++;
445 	}
446 
447 	return true;
448 }
449 
450 
popBlockEmergeData(v3s16 pos,BlockEmergeData * bedata)451 bool EmergeManager::popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata)
452 {
453 	std::map<v3s16, BlockEmergeData>::iterator it;
454 	std::unordered_map<u16, u16>::iterator it2;
455 
456 	it = m_blocks_enqueued.find(pos);
457 	if (it == m_blocks_enqueued.end())
458 		return false;
459 
460 	*bedata = it->second;
461 
462 	it2 = m_peer_queue_count.find(bedata->peer_requested);
463 	if (it2 == m_peer_queue_count.end())
464 		return false;
465 
466 	u16 &count_peer = it2->second;
467 	assert(count_peer != 0);
468 	count_peer--;
469 
470 	m_blocks_enqueued.erase(it);
471 
472 	return true;
473 }
474 
475 
getOptimalThread()476 EmergeThread *EmergeManager::getOptimalThread()
477 {
478 	size_t nthreads = m_threads.size();
479 
480 	FATAL_ERROR_IF(nthreads == 0, "No emerge threads!");
481 
482 	size_t index = 0;
483 	size_t nitems_lowest = m_threads[0]->m_block_queue.size();
484 
485 	for (size_t i = 1; i < nthreads; i++) {
486 		size_t nitems = m_threads[i]->m_block_queue.size();
487 		if (nitems < nitems_lowest) {
488 			index = i;
489 			nitems_lowest = nitems;
490 		}
491 	}
492 
493 	return m_threads[index];
494 }
495 
496 
497 ////
498 //// EmergeThread
499 ////
500 
EmergeThread(Server * server,int ethreadid)501 EmergeThread::EmergeThread(Server *server, int ethreadid) :
502 	enable_mapgen_debug_info(false),
503 	id(ethreadid),
504 	m_server(server),
505 	m_map(NULL),
506 	m_emerge(NULL),
507 	m_mapgen(NULL)
508 {
509 	m_name = "Emerge-" + itos(ethreadid);
510 }
511 
512 
signal()513 void EmergeThread::signal()
514 {
515 	m_queue_event.signal();
516 }
517 
518 
pushBlock(const v3s16 & pos)519 bool EmergeThread::pushBlock(const v3s16 &pos)
520 {
521 	m_block_queue.push(pos);
522 	return true;
523 }
524 
525 
cancelPendingItems()526 void EmergeThread::cancelPendingItems()
527 {
528 	MutexAutoLock queuelock(m_emerge->m_queue_mutex);
529 
530 	while (!m_block_queue.empty()) {
531 		BlockEmergeData bedata;
532 		v3s16 pos;
533 
534 		pos = m_block_queue.front();
535 		m_block_queue.pop();
536 
537 		m_emerge->popBlockEmergeData(pos, &bedata);
538 
539 		runCompletionCallbacks(pos, EMERGE_CANCELLED, bedata.callbacks);
540 	}
541 }
542 
543 
runCompletionCallbacks(const v3s16 & pos,EmergeAction action,const EmergeCallbackList & callbacks)544 void EmergeThread::runCompletionCallbacks(const v3s16 &pos, EmergeAction action,
545 	const EmergeCallbackList &callbacks)
546 {
547 	for (size_t i = 0; i != callbacks.size(); i++) {
548 		EmergeCompletionCallback callback;
549 		void *param;
550 
551 		callback = callbacks[i].first;
552 		param    = callbacks[i].second;
553 
554 		callback(pos, action, param);
555 	}
556 }
557 
558 
popBlockEmerge(v3s16 * pos,BlockEmergeData * bedata)559 bool EmergeThread::popBlockEmerge(v3s16 *pos, BlockEmergeData *bedata)
560 {
561 	MutexAutoLock queuelock(m_emerge->m_queue_mutex);
562 
563 	if (m_block_queue.empty())
564 		return false;
565 
566 	*pos = m_block_queue.front();
567 	m_block_queue.pop();
568 
569 	m_emerge->popBlockEmergeData(*pos, bedata);
570 
571 	return true;
572 }
573 
574 
getBlockOrStartGen(const v3s16 & pos,bool allow_gen,MapBlock ** block,BlockMakeData * bmdata)575 EmergeAction EmergeThread::getBlockOrStartGen(
576 	const v3s16 &pos, bool allow_gen, MapBlock **block, BlockMakeData *bmdata)
577 {
578 	MutexAutoLock envlock(m_server->m_env_mutex);
579 
580 	// 1). Attempt to fetch block from memory
581 	*block = m_map->getBlockNoCreateNoEx(pos);
582 	if (*block && !(*block)->isDummy()) {
583 		if ((*block)->isGenerated())
584 			return EMERGE_FROM_MEMORY;
585 	} else {
586 		// 2). Attempt to load block from disk if it was not in the memory
587 		*block = m_map->loadBlock(pos);
588 		if (*block && (*block)->isGenerated())
589 			return EMERGE_FROM_DISK;
590 	}
591 
592 	// 3). Attempt to start generation
593 	if (allow_gen && m_map->initBlockMake(pos, bmdata))
594 		return EMERGE_GENERATED;
595 
596 	// All attempts failed; cancel this block emerge
597 	return EMERGE_CANCELLED;
598 }
599 
600 
finishGen(v3s16 pos,BlockMakeData * bmdata,std::map<v3s16,MapBlock * > * modified_blocks)601 MapBlock *EmergeThread::finishGen(v3s16 pos, BlockMakeData *bmdata,
602 	std::map<v3s16, MapBlock *> *modified_blocks)
603 {
604 	MutexAutoLock envlock(m_server->m_env_mutex);
605 	ScopeProfiler sp(g_profiler,
606 		"EmergeThread: after Mapgen::makeChunk", SPT_AVG);
607 
608 	/*
609 		Perform post-processing on blocks (invalidate lighting, queue liquid
610 		transforms, etc.) to finish block make
611 	*/
612 	m_map->finishBlockMake(bmdata, modified_blocks);
613 
614 	MapBlock *block = m_map->getBlockNoCreateNoEx(pos);
615 	if (!block) {
616 		errorstream << "EmergeThread::finishGen: Couldn't grab block we "
617 			"just generated: " << PP(pos) << std::endl;
618 		return NULL;
619 	}
620 
621 	v3s16 minp = bmdata->blockpos_min * MAP_BLOCKSIZE;
622 	v3s16 maxp = bmdata->blockpos_max * MAP_BLOCKSIZE +
623 				 v3s16(1,1,1) * (MAP_BLOCKSIZE - 1);
624 
625 	// Ignore map edit events, they will not need to be sent
626 	// to anybody because the block hasn't been sent to anybody
627 	MapEditEventAreaIgnorer ign(
628 		&m_server->m_ignore_map_edit_events_area,
629 		VoxelArea(minp, maxp));
630 
631 	/*
632 		Run Lua on_generated callbacks
633 	*/
634 	try {
635 		m_server->getScriptIface()->environment_OnGenerated(
636 			minp, maxp, m_mapgen->blockseed);
637 	} catch (LuaError &e) {
638 		m_server->setAsyncFatalError("Lua: finishGen" + std::string(e.what()));
639 	}
640 
641 	/*
642 		Clear generate notifier events
643 	*/
644 	m_mapgen->gennotify.clearEvents();
645 
646 	EMERGE_DBG_OUT("ended up with: " << analyze_block(block));
647 
648 	/*
649 		Activate the block
650 	*/
651 	m_server->m_env->activateBlock(block, 0);
652 
653 	return block;
654 }
655 
656 
run()657 void *EmergeThread::run()
658 {
659 	BEGIN_DEBUG_EXCEPTION_HANDLER
660 
661 	v3s16 pos;
662 
663 	m_map    = (ServerMap *)&(m_server->m_env->getMap());
664 	m_emerge = m_server->m_emerge;
665 	m_mapgen = m_emerge->m_mapgens[id];
666 	enable_mapgen_debug_info = m_emerge->enable_mapgen_debug_info;
667 
668 	try {
669 	while (!stopRequested()) {
670 		std::map<v3s16, MapBlock *> modified_blocks;
671 		BlockEmergeData bedata;
672 		BlockMakeData bmdata;
673 		EmergeAction action;
674 		MapBlock *block;
675 
676 		if (!popBlockEmerge(&pos, &bedata)) {
677 			m_queue_event.wait();
678 			continue;
679 		}
680 
681 		if (blockpos_over_max_limit(pos))
682 			continue;
683 
684 		bool allow_gen = bedata.flags & BLOCK_EMERGE_ALLOW_GEN;
685 		EMERGE_DBG_OUT("pos=" PP(pos) " allow_gen=" << allow_gen);
686 
687 		action = getBlockOrStartGen(pos, allow_gen, &block, &bmdata);
688 		if (action == EMERGE_GENERATED) {
689 			{
690 				ScopeProfiler sp(g_profiler,
691 					"EmergeThread: Mapgen::makeChunk", SPT_AVG);
692 
693 				m_mapgen->makeChunk(&bmdata);
694 			}
695 
696 			block = finishGen(pos, &bmdata, &modified_blocks);
697 		}
698 
699 		runCompletionCallbacks(pos, action, bedata.callbacks);
700 
701 		if (block)
702 			modified_blocks[pos] = block;
703 
704 		if (!modified_blocks.empty())
705 			m_server->SetBlocksNotSent(modified_blocks);
706 	}
707 	} catch (VersionMismatchException &e) {
708 		std::ostringstream err;
709 		err << "World data version mismatch in MapBlock " << PP(pos) << std::endl
710 			<< "----" << std::endl
711 			<< "\"" << e.what() << "\"" << std::endl
712 			<< "See debug.txt." << std::endl
713 			<< "World probably saved by a newer version of " PROJECT_NAME_C "."
714 			<< std::endl;
715 		m_server->setAsyncFatalError(err.str());
716 	} catch (SerializationError &e) {
717 		std::ostringstream err;
718 		err << "Invalid data in MapBlock " << PP(pos) << std::endl
719 			<< "----" << std::endl
720 			<< "\"" << e.what() << "\"" << std::endl
721 			<< "See debug.txt." << std::endl
722 			<< "You can ignore this using [ignore_world_load_errors = true]."
723 			<< std::endl;
724 		m_server->setAsyncFatalError(err.str());
725 	}
726 
727 	END_DEBUG_EXCEPTION_HANDLER
728 	return NULL;
729 }
730