Skip to content
Permalink
Browse files

Fix broken BiomeGen abstraction (#11107)

  • Loading branch information
sfan5 committed Mar 23, 2021
1 parent c9eba84 commit 2da1eee394554879bf1cee6bc0f7b77acf0b6c43
Showing with 90 additions and 185 deletions.
  1. +13 −3 src/emerge.cpp
  2. +6 −1 src/emerge.h
  3. +2 −2 src/mapgen/mapgen.cpp
  4. +2 −1 src/mapgen/mapgen_valleys.cpp
  5. +17 −73 src/mapgen/mg_biome.cpp
  6. +17 −10 src/mapgen/mg_biome.h
  7. +3 −3 src/noise.cpp
  8. +3 −3 src/noise.h
  9. +27 −89 src/script/lua_api/l_mapgen.cpp
@@ -113,13 +113,15 @@ EmergeParams::~EmergeParams()
{
infostream << "EmergeParams: destroying " << this << std::endl;
// Delete everything that was cloned on creation of EmergeParams
delete biomegen;
delete biomemgr;
delete oremgr;
delete decomgr;
delete schemmgr;
}

EmergeParams::EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
EmergeParams::EmergeParams(EmergeManager *parent, const BiomeGen *biomegen,
const BiomeManager *biomemgr,
const OreManager *oremgr, const DecorationManager *decomgr,
const SchematicManager *schemmgr) :
ndef(parent->ndef),
@@ -129,6 +131,7 @@ EmergeParams::EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
biomemgr(biomemgr->clone()), oremgr(oremgr->clone()),
decomgr(decomgr->clone()), schemmgr(schemmgr->clone())
{
this->biomegen = biomegen->clone(this->biomemgr);
}

////
@@ -143,6 +146,10 @@ EmergeManager::EmergeManager(Server *server)
this->decomgr = new DecorationManager(server);
this->schemmgr = new SchematicManager(server);

// initialized later
this->mgparams = nullptr;
this->biomegen = nullptr;

// Note that accesses to this variable are not synchronized.
// This is because the *only* thread ever starting or stopping
// EmergeThreads should be the ServerThread.
@@ -240,9 +247,12 @@ void EmergeManager::initMapgens(MapgenParams *params)

mgparams = params;

v3s16 csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
biomegen = biomemgr->createBiomeGen(BIOMEGEN_ORIGINAL, params->bparams, csize);

for (u32 i = 0; i != m_threads.size(); i++) {
EmergeParams *p = new EmergeParams(
this, biomemgr, oremgr, decomgr, schemmgr);
EmergeParams *p = new EmergeParams(this, biomegen,
biomemgr, oremgr, decomgr, schemmgr);
infostream << "EmergeManager: Created params " << p
<< " for thread " << i << std::endl;
m_mapgens.push_back(Mapgen::createMapgen(params->mgtype, params, p));
@@ -99,13 +99,15 @@ class EmergeParams {
u32 gen_notify_on;
const std::set<u32> *gen_notify_on_deco_ids; // shared

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

private:
EmergeParams(EmergeManager *parent, const BiomeManager *biomemgr,
EmergeParams(EmergeManager *parent, const BiomeGen *biomegen,
const BiomeManager *biomemgr,
const OreManager *oremgr, const DecorationManager *decomgr,
const SchematicManager *schemmgr);
};
@@ -140,6 +142,8 @@ class EmergeManager {
~EmergeManager();
DISABLE_CLASS_COPY(EmergeManager);

const BiomeGen *getBiomeGen() const { return biomegen; }

// no usage restrictions
const BiomeManager *getBiomeManager() const { return biomemgr; }
const OreManager *getOreManager() const { return oremgr; }
@@ -196,6 +200,7 @@ class EmergeManager {

// Managers of various map generation-related components
// Note that each Mapgen gets a copy(!) of these to work with
BiomeGen *biomegen;
BiomeManager *biomemgr;
OreManager *oremgr;
DecorationManager *decomgr;
@@ -595,7 +595,8 @@ MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeParams *emerg
this->heightmap = new s16[csize.X * csize.Z];

//// Initialize biome generator
biomegen = m_bmgr->createBiomeGen(BIOMEGEN_ORIGINAL, params->bparams, csize);
biomegen = emerge->biomegen;
biomegen->assertChunkSize(csize);
biomemap = biomegen->biomemap;

//// Look up some commonly used content
@@ -621,7 +622,6 @@ MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeParams *emerg

MapgenBasic::~MapgenBasic()
{
delete biomegen;
delete []heightmap;

delete m_emerge; // destroying EmergeParams is our responsibility
@@ -57,7 +57,8 @@ FlagDesc flagdesc_mapgen_valleys[] = {
MapgenValleys::MapgenValleys(MapgenValleysParams *params, EmergeParams *emerge)
: MapgenBasic(MAPGEN_VALLEYS, params, emerge)
{
// NOTE: MapgenValleys has a hard dependency on BiomeGenOriginal
FATAL_ERROR_IF(biomegen->getType() != BIOMEGEN_ORIGINAL,
"MapgenValleys has a hard dependency on BiomeGenOriginal");
m_bgen = (BiomeGenOriginal *)biomegen;

spflags = params->spflags;
@@ -101,71 +101,6 @@ BiomeManager *BiomeManager::clone() const
return mgr;
}


// For BiomeGen type 'BiomeGenOriginal'
float BiomeManager::getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
NoiseParams &np_heat_blend, u64 seed) const
{
return
NoisePerlin2D(&np_heat, pos.X, pos.Z, seed) +
NoisePerlin2D(&np_heat_blend, pos.X, pos.Z, seed);
}


// For BiomeGen type 'BiomeGenOriginal'
float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
NoiseParams &np_humidity_blend, u64 seed) const
{
return
NoisePerlin2D(&np_humidity, pos.X, pos.Z, seed) +
NoisePerlin2D(&np_humidity_blend, pos.X, pos.Z, seed);
}


// For BiomeGen type 'BiomeGenOriginal'
const Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat,
float humidity, v3s16 pos) const
{
Biome *biome_closest = nullptr;
Biome *biome_closest_blend = nullptr;
float dist_min = FLT_MAX;
float dist_min_blend = FLT_MAX;

for (size_t i = 1; i < getNumObjects(); i++) {
Biome *b = (Biome *)getRaw(i);
if (!b ||
pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
continue;

float d_heat = heat - b->heat_point;
float d_humidity = humidity - b->humidity_point;
float dist = (d_heat * d_heat) + (d_humidity * d_humidity);

if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
if (dist < dist_min) {
dist_min = dist;
biome_closest = b;
}
} else if (dist < dist_min_blend) { // Blend area above biome b
dist_min_blend = dist;
biome_closest_blend = b;
}
}

const u64 seed = pos.Y + (heat + humidity) * 0.9f;
PcgRandom rng(seed);

if (biome_closest_blend && dist_min_blend <= dist_min &&
rng.range(0, biome_closest_blend->vertical_blend) >=
pos.Y - biome_closest_blend->max_pos.Y)
return biome_closest_blend;

return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE);
}


////////////////////////////////////////////////////////////////////////////////

void BiomeParamsOriginal::readParams(const Settings *settings)
@@ -189,7 +124,7 @@ void BiomeParamsOriginal::writeParams(Settings *settings) const
////////////////////////////////////////////////////////////////////////////////

BiomeGenOriginal::BiomeGenOriginal(BiomeManager *biomemgr,
BiomeParamsOriginal *params, v3s16 chunksize)
const BiomeParamsOriginal *params, v3s16 chunksize)
{
m_bmgr = biomemgr;
m_params = params;
@@ -224,17 +159,26 @@ BiomeGenOriginal::~BiomeGenOriginal()
delete noise_humidity_blend;
}

// Only usable in a mapgen thread
Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
BiomeGen *BiomeGenOriginal::clone(BiomeManager *biomemgr) const
{
return new BiomeGenOriginal(biomemgr, m_params, m_csize);
}

float BiomeGenOriginal::calcHeatAtPoint(v3s16 pos) const
{
float heat =
NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) +
return NoisePerlin2D(&m_params->np_heat, pos.X, pos.Z, m_params->seed) +
NoisePerlin2D(&m_params->np_heat_blend, pos.X, pos.Z, m_params->seed);
float humidity =
NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
}

float BiomeGenOriginal::calcHumidityAtPoint(v3s16 pos) const
{
return NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
}

return calcBiomeFromNoise(heat, humidity, pos);
Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
{
return calcBiomeFromNoise(calcHeatAtPoint(pos), calcHumidityAtPoint(pos), pos);
}


@@ -97,6 +97,15 @@ class BiomeGen {

virtual BiomeGenType getType() const = 0;

// Clone this BiomeGen and set a the new BiomeManager to be used by the copy
virtual BiomeGen *clone(BiomeManager *biomemgr) const = 0;

// Check that the internal chunk size is what the mapgen expects, just to be sure.
inline void assertChunkSize(v3s16 expect) const
{
FATAL_ERROR_IF(m_csize != expect, "Chunk size mismatches");
}

// Calculates the biome at the exact position provided. This function can
// be called at any time, but may be less efficient than the latter methods,
// depending on implementation.
@@ -158,12 +167,18 @@ struct BiomeParamsOriginal : public BiomeParams {
class BiomeGenOriginal : public BiomeGen {
public:
BiomeGenOriginal(BiomeManager *biomemgr,
BiomeParamsOriginal *params, v3s16 chunksize);
const BiomeParamsOriginal *params, v3s16 chunksize);
virtual ~BiomeGenOriginal();

BiomeGenType getType() const { return BIOMEGEN_ORIGINAL; }

BiomeGen *clone(BiomeManager *biomemgr) const;

// Slower, meant for Script API use
float calcHeatAtPoint(v3s16 pos) const;
float calcHumidityAtPoint(v3s16 pos) const;
Biome *calcBiomeAtPoint(v3s16 pos) const;

void calcBiomeNoise(v3s16 pmin);

biome_t *getBiomes(s16 *heightmap, v3s16 pmin);
@@ -176,7 +191,7 @@ class BiomeGenOriginal : public BiomeGen {
float *humidmap;

private:
BiomeParamsOriginal *m_params;
const BiomeParamsOriginal *m_params;

Noise *noise_heat;
Noise *noise_humidity;
@@ -229,14 +244,6 @@ class BiomeManager : public ObjDefManager {

virtual void clear();

// For BiomeGen type 'BiomeGenOriginal'
float getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
NoiseParams &np_heat_blend, u64 seed) const;
float getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
NoiseParams &np_humidity_blend, u64 seed) const;
const Biome *getBiomeFromNoiseOriginal(float heat, float humidity,
v3s16 pos) const;

private:
BiomeManager() {};

@@ -369,7 +369,7 @@ float contour(float v)
///////////////////////// [ New noise ] ////////////////////////////


float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed)
float NoisePerlin2D(const NoiseParams *np, float x, float y, s32 seed)
{
float a = 0;
float f = 1.0;
@@ -395,7 +395,7 @@ float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed)
}


float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed)
float NoisePerlin3D(const NoiseParams *np, float x, float y, float z, s32 seed)
{
float a = 0;
float f = 1.0;
@@ -422,7 +422,7 @@ float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed)
}


Noise::Noise(NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz)
Noise::Noise(const NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz)
{
np = *np_;
this->seed = seed;
@@ -146,7 +146,7 @@ class Noise {
float *persist_buf = nullptr;
float *result = nullptr;

Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
Noise(const NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1);
~Noise();

void setSize(u32 sx, u32 sy, u32 sz=1);
@@ -192,8 +192,8 @@ class Noise {

};

float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed);
float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed);
float NoisePerlin2D(const NoiseParams *np, float x, float y, s32 seed);
float NoisePerlin3D(const NoiseParams *np, float x, float y, float z, s32 seed);

inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff,
float y, float yoff, s32 seed)

0 comments on commit 2da1eee

Please sign in to comment.