Skip to content
Permalink
Browse files

Give the Mapgen on each EmergeThread its own Biome/Ore/Deco/SchemMana…

…ger copy
  • Loading branch information
sfan5 authored and nerzhul committed May 5, 2020
1 parent 2062c80 commit 3c65d1acec27366d88fc3686d3f820175673e203
@@ -109,6 +109,28 @@ class MapEditEventAreaIgnorer
VoxelArea *m_ignorevariable;
};

EmergeParams::~EmergeParams()
{
infostream << "EmergeParams: destroying " << this << std::endl;
// Delete everything that was cloned on creation of EmergeParams
delete biomemgr;
delete oremgr;
delete decomgr;
delete schemmgr;
}

EmergeParams::EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
const OreManager *oremgr, const DecorationManager *decomgr,
const SchematicManager *schemmgr) :
ndef(parent->ndef),
enable_mapgen_debug_info(parent->enable_mapgen_debug_info),
gen_notify_on(parent->gen_notify_on),
gen_notify_on_deco_ids(&parent->gen_notify_on_deco_ids),
biomemgr(biomemgr->clone()), oremgr(oremgr->clone()),
decomgr(decomgr->clone()), schemmgr(schemmgr->clone())
{
}

////
//// EmergeManager
////
@@ -182,14 +204,48 @@ EmergeManager::~EmergeManager()
}


BiomeManager *EmergeManager::getWritableBiomeManager()
{
FATAL_ERROR_IF(!m_mapgens.empty(),
"Writable managers can only be returned before mapgen init");
return biomemgr;
}

OreManager *EmergeManager::getWritableOreManager()
{
FATAL_ERROR_IF(!m_mapgens.empty(),
"Writable managers can only be returned before mapgen init");
return oremgr;
}

DecorationManager *EmergeManager::getWritableDecorationManager()
{
FATAL_ERROR_IF(!m_mapgens.empty(),
"Writable managers can only be returned before mapgen init");
return decomgr;
}

SchematicManager *EmergeManager::getWritableSchematicManager()
{
FATAL_ERROR_IF(!m_mapgens.empty(),
"Writable managers can only be returned before mapgen init");
return schemmgr;
}


void EmergeManager::initMapgens(MapgenParams *params)
{
FATAL_ERROR_IF(!m_mapgens.empty(), "Mapgen already initialised.");

mgparams = params;

for (u32 i = 0; i != m_threads.size(); i++)
m_mapgens.push_back(Mapgen::createMapgen(params->mgtype, params, this));
for (u32 i = 0; i != m_threads.size(); i++) {
EmergeParams *p = new EmergeParams(
this, biomemgr, oremgr, decomgr, schemmgr);
infostream << "EmergeManager: Created params " << p
<< " for thread " << i << std::endl;
m_mapgens.push_back(Mapgen::createMapgen(params->mgtype, params, p));
}
}


@@ -86,6 +86,30 @@ struct BlockEmergeData {
EmergeCallbackList callbacks;
};

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

BiomeManager *biomemgr;
OreManager *oremgr;
DecorationManager *decomgr;
SchematicManager *schemmgr;

private:
EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
const OreManager *oremgr, const DecorationManager *decomgr,
const SchematicManager *schemmgr);
};

class EmergeManager {
public:
const NodeDefManager *ndef;
@@ -106,17 +130,22 @@ class EmergeManager {
// Environment is not created until after script initialization.
MapSettingsManager *map_settings_mgr;

// Managers of various map generation-related components
BiomeManager *biomemgr;
OreManager *oremgr;
DecorationManager *decomgr;
SchematicManager *schemmgr;

// Methods
EmergeManager(Server *server);
~EmergeManager();
DISABLE_CLASS_COPY(EmergeManager);

// 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();

void initMapgens(MapgenParams *mgparams);

void startThreads();
@@ -160,6 +189,13 @@ class EmergeManager {
u16 m_qlimit_diskonly;
u16 m_qlimit_generate;

// Managers of various map generation-related components
// Note that each Mapgen gets a copy(!) of these to work with
BiomeManager *biomemgr;
OreManager *oremgr;
DecorationManager *decomgr;
SchematicManager *schemmgr;

// Requires m_queue_mutex held
EmergeThread *getOptimalThread();

@@ -106,8 +106,8 @@ STATIC_ASSERT(
//// Mapgen
////

Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeParams *emerge) :
gennotify(emerge->gen_notify_on, emerge->gen_notify_on_deco_ids)
{
id = mapgenid;
water_level = params->water_level;
@@ -156,7 +156,7 @@ const char *Mapgen::getMapgenName(MapgenType mgtype)


Mapgen *Mapgen::createMapgen(MapgenType mgtype, MapgenParams *params,
EmergeManager *emerge)
EmergeParams *emerge)
{
switch (mgtype) {
case MAPGEN_CARPATHIAN:
@@ -585,7 +585,7 @@ void Mapgen::spreadLight(const v3s16 &nmin, const v3s16 &nmax)
//// MapgenBasic
////

MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge)
MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeParams *emerge)
: Mapgen(mapgenid, params, emerge)
{
this->m_emerge = emerge;
@@ -642,6 +642,8 @@ MapgenBasic::~MapgenBasic()
{
delete biomegen;
delete []heightmap;

delete m_emerge; // destroying EmergeParams is our responsibility
}


@@ -974,7 +976,7 @@ void MapgenBasic::generateDungeons(s16 max_stone_y)
////

GenerateNotifier::GenerateNotifier(u32 notify_on,
std::set<u32> *notify_on_deco_ids)
const std::set<u32> *notify_on_deco_ids)
{
m_notify_on = notify_on;
m_notify_on_deco_ids = notify_on_deco_ids;
@@ -987,7 +989,8 @@ void GenerateNotifier::setNotifyOn(u32 notify_on)
}


void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
void GenerateNotifier::setNotifyOnDecoIds(
const std::set<u32> *notify_on_deco_ids)
{
m_notify_on_deco_ids = notify_on_deco_ids;
}
@@ -999,7 +1002,7 @@ bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
return false;

if (type == GENNOTIFY_DECORATION &&
m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->cend())
return false;

GenNotifyEvent gne;
@@ -51,6 +51,7 @@ class Biome;
class BiomeGen;
struct BiomeParams;
class BiomeManager;
class EmergeParams;
class EmergeManager;
class MapBlock;
class VoxelManipulator;
@@ -87,18 +88,18 @@ struct GenNotifyEvent {
class GenerateNotifier {
public:
GenerateNotifier() = default;
GenerateNotifier(u32 notify_on, std::set<u32> *notify_on_deco_ids);
GenerateNotifier(u32 notify_on, const std::set<u32> *notify_on_deco_ids);

void setNotifyOn(u32 notify_on);
void setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids);
void setNotifyOnDecoIds(const std::set<u32> *notify_on_deco_ids);

bool addEvent(GenNotifyType type, v3s16 pos, u32 id=0);
void getEvents(std::map<std::string, std::vector<v3s16> > &event_map);
void clearEvents();

private:
u32 m_notify_on = 0;
std::set<u32> *m_notify_on_deco_ids;
const std::set<u32> *m_notify_on_deco_ids;
std::list<GenNotifyEvent> m_notify_events;
};

@@ -176,7 +177,7 @@ class Mapgen {
GenerateNotifier gennotify;

Mapgen() = default;
Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
Mapgen(int mapgenid, MapgenParams *params, EmergeParams *emerge);
virtual ~Mapgen() = default;
DISABLE_CLASS_COPY(Mapgen);

@@ -215,7 +216,7 @@ class Mapgen {
static MapgenType getMapgenType(const std::string &mgname);
static const char *getMapgenName(MapgenType mgtype);
static Mapgen *createMapgen(MapgenType mgtype, MapgenParams *params,
EmergeManager *emerge);
EmergeParams *emerge);
static MapgenParams *createMapgenParams(MapgenType mgtype);
static void getMapgenNames(std::vector<const char *> *mgnames, bool include_hidden);
static void setDefaultSettings(Settings *settings);
@@ -243,7 +244,7 @@ class Mapgen {
*/
class MapgenBasic : public Mapgen {
public:
MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge);
MapgenBasic(int mapgenid, MapgenParams *params, EmergeParams *emerge);
virtual ~MapgenBasic();

virtual void generateBiomes();
@@ -254,7 +255,7 @@ class MapgenBasic : public Mapgen {
virtual void generateDungeons(s16 max_stone_y);

protected:
EmergeManager *m_emerge;
EmergeParams *m_emerge;
BiomeManager *m_bmgr;

Noise *noise_filler_depth;
@@ -49,7 +49,7 @@ FlagDesc flagdesc_mapgen_carpathian[] = {
///////////////////////////////////////////////////////////////////////////////


MapgenCarpathian::MapgenCarpathian(MapgenCarpathianParams *params, EmergeManager *emerge)
MapgenCarpathian::MapgenCarpathian(MapgenCarpathianParams *params, EmergeParams *emerge)
: MapgenBasic(MAPGEN_CARPATHIAN, params, emerge)
{
base_level = params->base_level;
@@ -79,7 +79,7 @@ struct MapgenCarpathianParams : public MapgenParams
class MapgenCarpathian : public MapgenBasic
{
public:
MapgenCarpathian(MapgenCarpathianParams *params, EmergeManager *emerge);
MapgenCarpathian(MapgenCarpathianParams *params, EmergeParams *emerge);
~MapgenCarpathian();

virtual MapgenType getType() const { return MAPGEN_CARPATHIAN; }
@@ -47,7 +47,7 @@ FlagDesc flagdesc_mapgen_flat[] = {
///////////////////////////////////////////////////////////////////////////////////////


MapgenFlat::MapgenFlat(MapgenFlatParams *params, EmergeManager *emerge)
MapgenFlat::MapgenFlat(MapgenFlatParams *params, EmergeParams *emerge)
: MapgenBasic(MAPGEN_FLAT, params, emerge)
{
spflags = params->spflags;
@@ -64,7 +64,7 @@ struct MapgenFlatParams : public MapgenParams
class MapgenFlat : public MapgenBasic
{
public:
MapgenFlat(MapgenFlatParams *params, EmergeManager *emerge);
MapgenFlat(MapgenFlatParams *params, EmergeParams *emerge);
~MapgenFlat();

virtual MapgenType getType() const { return MAPGEN_FLAT; }
@@ -47,7 +47,7 @@ FlagDesc flagdesc_mapgen_fractal[] = {
///////////////////////////////////////////////////////////////////////////////////////


MapgenFractal::MapgenFractal(MapgenFractalParams *params, EmergeManager *emerge)
MapgenFractal::MapgenFractal(MapgenFractalParams *params, EmergeParams *emerge)
: MapgenBasic(MAPGEN_FRACTAL, params, emerge)
{
spflags = params->spflags;
@@ -72,7 +72,7 @@ struct MapgenFractalParams : public MapgenParams
class MapgenFractal : public MapgenBasic
{
public:
MapgenFractal(MapgenFractalParams *params, EmergeManager *emerge);
MapgenFractal(MapgenFractalParams *params, EmergeParams *emerge);
~MapgenFractal();

virtual MapgenType getType() const { return MAPGEN_FRACTAL; }
@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "emerge.h"


MapgenSinglenode::MapgenSinglenode(MapgenParams *params, EmergeManager *emerge)
MapgenSinglenode::MapgenSinglenode(MapgenParams *params, EmergeParams *emerge)
: Mapgen(MAPGEN_SINGLENODE, params, emerge)
{
const NodeDefManager *ndef = emerge->ndef;
@@ -38,7 +38,7 @@ class MapgenSinglenode : public Mapgen
content_t c_node;
u8 set_light;

MapgenSinglenode(MapgenParams *params, EmergeManager *emerge);
MapgenSinglenode(MapgenParams *params, EmergeParams *emerge);
~MapgenSinglenode() = default;

virtual MapgenType getType() const { return MAPGEN_SINGLENODE; }
@@ -44,7 +44,7 @@ FlagDesc flagdesc_mapgen_v5[] = {
};


MapgenV5::MapgenV5(MapgenV5Params *params, EmergeManager *emerge)
MapgenV5::MapgenV5(MapgenV5Params *params, EmergeParams *emerge)
: MapgenBasic(MAPGEN_V5, params, emerge)
{
spflags = params->spflags;
@@ -64,7 +64,7 @@ struct MapgenV5Params : public MapgenParams
class MapgenV5 : public MapgenBasic
{
public:
MapgenV5(MapgenV5Params *params, EmergeManager *emerge);
MapgenV5(MapgenV5Params *params, EmergeParams *emerge);
~MapgenV5();

virtual MapgenType getType() const { return MAPGEN_V5; }
@@ -54,7 +54,7 @@ FlagDesc flagdesc_mapgen_v6[] = {
/////////////////////////////////////////////////////////////////////////////


MapgenV6::MapgenV6(MapgenV6Params *params, EmergeManager *emerge)
MapgenV6::MapgenV6(MapgenV6Params *params, EmergeParams *emerge)
: Mapgen(MAPGEN_V6, params, emerge)
{
m_emerge = emerge;
@@ -160,6 +160,8 @@ MapgenV6::~MapgenV6()
delete noise_humidity;

delete[] heightmap;

delete m_emerge; // our responsibility
}


0 comments on commit 3c65d1a

Please sign in to comment.
You can’t perform that action at this time.