2013-04-06 17:19:59 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2013-02-14 04:43:15 +01:00
|
|
|
|
|
|
|
#include <map>
|
2017-06-06 16:29:28 +02:00
|
|
|
#include <mutex>
|
2017-09-27 19:47:36 +02:00
|
|
|
#include "network/networkprotocol.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "irr_v3d.h"
|
|
|
|
#include "util/container.h"
|
2022-05-09 21:20:58 +02:00
|
|
|
#include "util/metricsbackend.h"
|
2017-11-08 23:56:20 +01:00
|
|
|
#include "mapgen/mapgen.h" // for MapgenParams
|
2014-11-13 05:01:13 +01:00
|
|
|
#include "map.h"
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
#define BLOCK_EMERGE_ALLOW_GEN (1 << 0)
|
|
|
|
#define BLOCK_EMERGE_FORCE_QUEUE (1 << 1)
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
#define EMERGE_DBG_OUT(x) { \
|
2015-10-04 08:54:25 +02:00
|
|
|
if (enable_mapgen_debug_info) \
|
|
|
|
infostream << "EmergeThread: " x << std::endl; \
|
2017-08-20 13:30:50 +02:00
|
|
|
}
|
2013-02-17 08:46:08 +01:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
class EmergeThread;
|
2018-02-10 21:04:16 +01:00
|
|
|
class NodeDefManager;
|
2013-08-11 04:09:45 +02:00
|
|
|
class Settings;
|
2014-11-13 05:01:13 +01:00
|
|
|
|
|
|
|
class BiomeManager;
|
|
|
|
class OreManager;
|
|
|
|
class DecorationManager;
|
|
|
|
class SchematicManager;
|
2017-01-09 20:39:22 +01:00
|
|
|
class Server;
|
2020-04-10 02:43:49 +02:00
|
|
|
class ModApiMapgen;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
// Structure containing inputs/outputs for chunk generation
|
2013-02-14 04:43:15 +01:00
|
|
|
struct BlockMakeData {
|
2017-06-16 11:25:52 +02:00
|
|
|
MMVManip *vmanip = nullptr;
|
|
|
|
u64 seed = 0;
|
2013-02-14 04:43:15 +01:00
|
|
|
v3s16 blockpos_min;
|
|
|
|
v3s16 blockpos_max;
|
|
|
|
UniqueQueue<v3s16> transforming_liquid;
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *nodedef = nullptr;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2017-08-17 23:02:50 +02:00
|
|
|
BlockMakeData() = default;
|
|
|
|
|
2013-02-15 00:32:56 +01:00
|
|
|
~BlockMakeData() { delete vmanip; }
|
|
|
|
};
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
// Result from processing an item on the emerge queue
|
|
|
|
enum EmergeAction {
|
|
|
|
EMERGE_CANCELLED,
|
|
|
|
EMERGE_ERRORED,
|
|
|
|
EMERGE_FROM_MEMORY,
|
|
|
|
EMERGE_FROM_DISK,
|
|
|
|
EMERGE_GENERATED,
|
|
|
|
};
|
|
|
|
|
2022-05-09 21:20:58 +02:00
|
|
|
const static std::string emergeActionStrs[] = {
|
|
|
|
"cancelled",
|
|
|
|
"errored",
|
|
|
|
"from_memory",
|
|
|
|
"from_disk",
|
|
|
|
"generated",
|
|
|
|
};
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
// Callback
|
|
|
|
typedef void (*EmergeCompletionCallback)(
|
|
|
|
v3s16 blockpos, EmergeAction action, void *param);
|
|
|
|
|
|
|
|
typedef std::vector<
|
|
|
|
std::pair<
|
|
|
|
EmergeCompletionCallback,
|
|
|
|
void *
|
|
|
|
>
|
|
|
|
> EmergeCallbackList;
|
|
|
|
|
2013-02-15 00:32:56 +01:00
|
|
|
struct BlockEmergeData {
|
|
|
|
u16 peer_requested;
|
2015-10-04 08:54:25 +02:00
|
|
|
u16 flags;
|
|
|
|
EmergeCallbackList callbacks;
|
2013-02-14 04:43:15 +01:00
|
|
|
};
|
|
|
|
|
2020-04-10 02:05:20 +02:00
|
|
|
class EmergeParams {
|
|
|
|
friend class EmergeManager;
|
|
|
|
public:
|
|
|
|
EmergeParams() = delete;
|
|
|
|
~EmergeParams();
|
|
|
|
DISABLE_CLASS_COPY(EmergeParams);
|
|
|
|
|
|
|
|
const NodeDefManager *ndef; // shared
|
|
|
|
bool enable_mapgen_debug_info;
|
|
|
|
|
|
|
|
u32 gen_notify_on;
|
|
|
|
const std::set<u32> *gen_notify_on_deco_ids; // shared
|
|
|
|
|
2021-03-23 15:43:26 +01:00
|
|
|
BiomeGen *biomegen;
|
2020-04-10 02:05:20 +02:00
|
|
|
BiomeManager *biomemgr;
|
|
|
|
OreManager *oremgr;
|
|
|
|
DecorationManager *decomgr;
|
|
|
|
SchematicManager *schemmgr;
|
|
|
|
|
|
|
|
private:
|
2021-03-23 15:43:26 +01:00
|
|
|
EmergeParams(EmergeManager *parent, const BiomeGen *biomegen,
|
|
|
|
const BiomeManager *biomemgr,
|
2020-04-10 02:05:20 +02:00
|
|
|
const OreManager *oremgr, const DecorationManager *decomgr,
|
|
|
|
const SchematicManager *schemmgr);
|
|
|
|
};
|
|
|
|
|
2014-02-09 22:36:30 +01:00
|
|
|
class EmergeManager {
|
2020-04-10 02:43:49 +02:00
|
|
|
/* The mod API needs unchecked access to allow:
|
|
|
|
* - using decomgr or oremgr to place decos/ores
|
|
|
|
* - using schemmgr to load and place schematics
|
|
|
|
*/
|
|
|
|
friend class ModApiMapgen;
|
2013-02-14 04:43:15 +01:00
|
|
|
public:
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef;
|
2015-10-04 08:54:25 +02:00
|
|
|
bool enable_mapgen_debug_info;
|
2013-04-06 17:19:59 +02:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
// Generation Notify
|
2017-06-16 11:25:52 +02:00
|
|
|
u32 gen_notify_on = 0;
|
2014-12-06 10:18:04 +01:00
|
|
|
std::set<u32> gen_notify_on_deco_ids;
|
2013-12-14 07:52:06 +01:00
|
|
|
|
2016-06-25 00:15:56 +02:00
|
|
|
// Parameters passed to mapgens owned by ServerMap
|
|
|
|
// TODO(hmmmm): Remove this after mapgen helper methods using them
|
|
|
|
// are moved to ServerMap
|
|
|
|
MapgenParams *mgparams;
|
|
|
|
|
|
|
|
// Hackish workaround:
|
|
|
|
// For now, EmergeManager must hold onto a ptr to the Map's setting manager
|
|
|
|
// since the Map can only be accessed through the Environment, and the
|
|
|
|
// Environment is not created until after script initialization.
|
|
|
|
MapSettingsManager *map_settings_mgr;
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
// Methods
|
2022-05-09 21:20:58 +02:00
|
|
|
EmergeManager(Server *server, MetricsBackend *mb);
|
2013-02-14 04:43:15 +01:00
|
|
|
~EmergeManager();
|
2017-06-10 13:49:15 +02:00
|
|
|
DISABLE_CLASS_COPY(EmergeManager);
|
2013-02-14 04:43:15 +01:00
|
|
|
|
2021-03-23 15:43:26 +01:00
|
|
|
const BiomeGen *getBiomeGen() const { return biomegen; }
|
|
|
|
|
2020-04-10 02:05:20 +02:00
|
|
|
// no usage restrictions
|
|
|
|
const BiomeManager *getBiomeManager() const { return biomemgr; }
|
|
|
|
const OreManager *getOreManager() const { return oremgr; }
|
|
|
|
const DecorationManager *getDecorationManager() const { return decomgr; }
|
|
|
|
const SchematicManager *getSchematicManager() const { return schemmgr; }
|
|
|
|
// only usable before mapgen init
|
|
|
|
BiomeManager *getWritableBiomeManager();
|
|
|
|
OreManager *getWritableOreManager();
|
|
|
|
DecorationManager *getWritableDecorationManager();
|
|
|
|
SchematicManager *getWritableSchematicManager();
|
|
|
|
|
2019-03-25 16:38:35 +01:00
|
|
|
void initMapgens(MapgenParams *mgparams);
|
2015-10-04 08:54:25 +02:00
|
|
|
|
2014-01-26 07:12:18 +01:00
|
|
|
void startThreads();
|
|
|
|
void stopThreads();
|
2015-10-04 23:26:08 +02:00
|
|
|
bool isRunning();
|
2015-10-04 08:54:25 +02:00
|
|
|
|
|
|
|
bool enqueueBlockEmerge(
|
2017-09-27 19:47:36 +02:00
|
|
|
session_t peer_id,
|
2015-10-04 08:54:25 +02:00
|
|
|
v3s16 blockpos,
|
|
|
|
bool allow_generate,
|
|
|
|
bool ignore_queue_limits=false);
|
|
|
|
|
|
|
|
bool enqueueBlockEmergeEx(
|
|
|
|
v3s16 blockpos,
|
2017-09-27 19:47:36 +02:00
|
|
|
session_t peer_id,
|
2015-10-04 08:54:25 +02:00
|
|
|
u16 flags,
|
|
|
|
EmergeCompletionCallback callback,
|
|
|
|
void *callback_param);
|
2013-12-08 05:43:46 +01:00
|
|
|
|
2021-05-30 20:24:12 +02:00
|
|
|
bool isBlockInQueue(v3s16 pos);
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
Mapgen *getCurrentMapgen();
|
|
|
|
|
|
|
|
// Mapgen helpers methods
|
2016-02-04 02:03:31 +01:00
|
|
|
int getSpawnLevelAtPoint(v2s16 p);
|
2013-02-14 04:43:15 +01:00
|
|
|
bool isBlockUnderground(v3s16 blockpos);
|
2015-10-04 08:54:25 +02:00
|
|
|
|
|
|
|
static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Mapgen *> m_mapgens;
|
|
|
|
std::vector<EmergeThread *> m_threads;
|
2017-06-16 11:25:52 +02:00
|
|
|
bool m_threads_active = false;
|
2015-10-04 08:54:25 +02:00
|
|
|
|
2017-06-06 16:29:28 +02:00
|
|
|
std::mutex m_queue_mutex;
|
2015-10-04 08:54:25 +02:00
|
|
|
std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
|
2021-07-31 19:54:40 +02:00
|
|
|
std::unordered_map<u16, u32> m_peer_queue_count;
|
2015-10-04 08:54:25 +02:00
|
|
|
|
2021-07-31 19:54:40 +02:00
|
|
|
u32 m_qlimit_total;
|
|
|
|
u32 m_qlimit_diskonly;
|
|
|
|
u32 m_qlimit_generate;
|
2015-10-04 08:54:25 +02:00
|
|
|
|
2022-05-09 21:20:58 +02:00
|
|
|
// Emerge metrics
|
|
|
|
MetricCounterPtr m_completed_emerge_counter[5];
|
|
|
|
|
2020-04-10 02:05:20 +02:00
|
|
|
// Managers of various map generation-related components
|
|
|
|
// Note that each Mapgen gets a copy(!) of these to work with
|
2021-03-23 15:43:26 +01:00
|
|
|
BiomeGen *biomegen;
|
2020-04-10 02:05:20 +02:00
|
|
|
BiomeManager *biomemgr;
|
|
|
|
OreManager *oremgr;
|
|
|
|
DecorationManager *decomgr;
|
|
|
|
SchematicManager *schemmgr;
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
// Requires m_queue_mutex held
|
|
|
|
EmergeThread *getOptimalThread();
|
2015-11-14 09:07:21 +01:00
|
|
|
|
|
|
|
bool pushBlockEmergeData(
|
|
|
|
v3s16 pos,
|
|
|
|
u16 peer_requested,
|
|
|
|
u16 flags,
|
|
|
|
EmergeCompletionCallback callback,
|
|
|
|
void *callback_param,
|
|
|
|
bool *entry_already_exists);
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
bool popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata);
|
|
|
|
|
2022-05-09 21:20:58 +02:00
|
|
|
void reportCompletedEmerge(EmergeAction action);
|
|
|
|
|
2015-10-04 08:54:25 +02:00
|
|
|
friend class EmergeThread;
|
2013-02-14 04:43:15 +01:00
|
|
|
};
|