Mapgen: Deduplicate common constructor code

This commit is contained in:
kwolekr 2016-05-22 02:17:19 -04:00
parent 0df5c01a8c
commit 0810901766
12 changed files with 128 additions and 221 deletions

@ -377,9 +377,60 @@ void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
MapgenBasic::MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge)
: Mapgen(mapgenid, params, emerge)
{
this->m_emerge = emerge;
this->m_bmgr = emerge->biomemgr;
//// Here, 'stride' refers to the number of elements needed to skip to index
//// an adjacent element for that coordinate in noise/height/biome maps
//// (*not* vmanip content map!)
// Note there is no X stride explicitly defined. Items adjacent in the X
// coordinate are assumed to be adjacent in memory as well (i.e. stride of 1).
// Number of elements to skip to get to the next Y coordinate
this->ystride = csize.X;
// Number of elements to skip to get to the next Z coordinate
this->zstride = csize.X * csize.Y;
// Z-stride value for maps oversized for 1-down overgeneration
this->zstride_1d = csize.X * (csize.Y + 1);
// Z-stride value for maps oversized for 1-up 1-down overgeneration
this->zstride_1u1d = csize.X * (csize.Y + 2);
//// Allocate heightmap
this->heightmap = new s16[csize.X * csize.Z];
//// Initialize biome generator
// TODO(hmmmm): should we have a way to disable biomemanager biomes?
biomegen = m_bmgr->createBiomeGen(BIOMEGEN_ORIGINAL, params->bparams, csize);
biomemap = biomegen->biomemap;
//// Look up some commonly used content
c_stone = ndef->getId("mapgen_stone");
c_water_source = ndef->getId("mapgen_water_source");
c_desert_stone = ndef->getId("mapgen_desert_stone");
c_sandstone = ndef->getId("mapgen_sandstone");
c_river_water_source = ndef->getId("mapgen_river_water_source");
//// Fall back to more basic content if not defined
if (c_desert_stone == CONTENT_IGNORE)
c_desert_stone = c_stone;
if (c_sandstone == CONTENT_IGNORE)
c_sandstone = c_stone;
if (c_river_water_source == CONTENT_IGNORE)
c_river_water_source = c_water_source;
}
MapgenBasic::~MapgenBasic()
{
delete biomegen;
delete []heightmap;
}
MgStoneType MapgenBasic::generateBiomes()
{
v3s16 em = vm->m_area.getExtent();
@ -499,7 +550,7 @@ void MapgenBasic::dustTopNodes()
for (s16 z = node_min.Z; z <= node_max.Z; z++)
for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
Biome *biome = (Biome *)bmgr->getRaw(biomemap[index]);
Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index]);
if (biome->c_dust == CONTENT_IGNORE)
continue;
@ -544,7 +595,7 @@ void MapgenBasic::generateCaves(s16 max_stone_y, s16 large_cave_depth)
if (max_stone_y < node_min.Y)
return;
CavesNoiseIntersection caves_noise(ndef, bmgr, csize,
CavesNoiseIntersection caves_noise(ndef, m_bmgr, csize,
&np_cave1, &np_cave2, seed, cave_width);
caves_noise.generateCaves(vm, node_min, node_max, biomemap);

@ -216,8 +216,16 @@ private:
*/
class MapgenBasic : public Mapgen {
public:
MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge);
virtual ~MapgenBasic();
virtual MgStoneType generateBiomes();
virtual void dustTopNodes();
virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth);
protected:
EmergeManager *m_emerge;
BiomeManager *bmgr;
BiomeManager *m_bmgr;
Noise *noise_filler_depth;
@ -233,15 +241,15 @@ public:
content_t c_sandstone;
int ystride;
int zstride;
int zstride_1d;
int zstride_1u1d;
u32 spflags;
NoiseParams np_cave1;
NoiseParams np_cave2;
float cave_width;
MapgenBasic(int mapgenid, MapgenParams *params, EmergeManager *emerge);
virtual MgStoneType generateBiomes();
virtual void dustTopNodes();
virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth);
};
struct MapgenFactory {

@ -52,15 +52,6 @@ FlagDesc flagdesc_mapgen_flat[] = {
MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge)
: MapgenBasic(mapgenid, params, emerge)
{
this->m_emerge = emerge;
this->bmgr = emerge->biomemgr;
//// amount of elements to skip for the next index
//// for noise/height/biome maps (not vmanip)
this->ystride = csize.X;
this->heightmap = new s16[csize.X * csize.Z];
MapgenFlatParams *sp = (MapgenFlatParams *)params->sparams;
this->spflags = sp->spflags;
@ -79,27 +70,14 @@ MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge
MapgenBasic::np_cave1 = sp->np_cave1;
MapgenBasic::np_cave2 = sp->np_cave2;
//// Initialize biome generator
biomegen = emerge->biomemgr->createBiomeGen(
BIOMEGEN_ORIGINAL, params->bparams, csize);
biomemap = biomegen->biomemap;
//// Resolve nodes to be used
c_stone = ndef->getId("mapgen_stone");
c_water_source = ndef->getId("mapgen_water_source");
c_lava_source = ndef->getId("mapgen_lava_source");
c_desert_stone = ndef->getId("mapgen_desert_stone");
c_ice = ndef->getId("mapgen_ice");
c_sandstone = ndef->getId("mapgen_sandstone");
// Content used for dungeon generation
c_cobble = ndef->getId("mapgen_cobble");
c_stair_cobble = ndef->getId("mapgen_stair_cobble");
c_mossycobble = ndef->getId("mapgen_mossycobble");
c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
if (c_ice == CONTENT_IGNORE)
c_ice = CONTENT_AIR;
// Fall back to more basic content if not defined
if (c_mossycobble == CONTENT_IGNORE)
c_mossycobble = c_cobble;
if (c_stair_cobble == CONTENT_IGNORE)
@ -115,10 +93,6 @@ MapgenFlat::~MapgenFlat()
{
delete noise_terrain;
delete noise_filler_depth;
delete biomegen;
delete[] heightmap;
}

@ -55,7 +55,14 @@ struct MapgenFlatParams : public MapgenSpecificParams {
class MapgenFlat : public MapgenBasic {
public:
u32 spflags;
MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge);
~MapgenFlat();
virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
s16 generateTerrain();
private:
s16 ground_level;
s16 large_cave_depth;
float lake_threshold;
@ -64,21 +71,11 @@ public:
float hill_steepness;
Noise *noise_terrain;
content_t c_lava_source;
content_t c_ice;
content_t c_cobble;
content_t c_stair_cobble;
content_t c_mossycobble;
content_t c_sandstonebrick;
content_t c_stair_sandstonebrick;
MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge);
~MapgenFlat();
virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
s16 generateTerrain();
};
struct MapgenFactoryFlat : public MapgenFactory {

@ -50,15 +50,6 @@ FlagDesc flagdesc_mapgen_fractal[] = {
MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge)
: MapgenBasic(mapgenid, params, emerge)
{
this->m_emerge = emerge;
this->bmgr = emerge->biomemgr;
//// amount of elements to skip for the next index
//// for noise/height/biome maps (not vmanip)
this->ystride = csize.X;
this->heightmap = new s16[csize.X * csize.Z];
MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams;
this->spflags = sp->spflags;
@ -80,30 +71,17 @@ MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *
MapgenBasic::np_cave1 = sp->np_cave1;
MapgenBasic::np_cave2 = sp->np_cave2;
//// Initialize biome generator
biomegen = emerge->biomemgr->createBiomeGen(
BIOMEGEN_ORIGINAL, params->bparams, csize);
biomemap = biomegen->biomemap;
this->formula = fractal / 2 + fractal % 2;
this->julia = fractal % 2 == 0;
//// Resolve nodes to be used
c_stone = ndef->getId("mapgen_stone");
c_water_source = ndef->getId("mapgen_water_source");
c_lava_source = ndef->getId("mapgen_lava_source");
c_desert_stone = ndef->getId("mapgen_desert_stone");
c_ice = ndef->getId("mapgen_ice");
c_sandstone = ndef->getId("mapgen_sandstone");
// Content used for dungeon generation
c_cobble = ndef->getId("mapgen_cobble");
c_stair_cobble = ndef->getId("mapgen_stair_cobble");
c_mossycobble = ndef->getId("mapgen_mossycobble");
c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
if (c_ice == CONTENT_IGNORE)
c_ice = CONTENT_AIR;
// Fall back to more basic content if not defined
if (c_mossycobble == CONTENT_IGNORE)
c_mossycobble = c_cobble;
if (c_stair_cobble == CONTENT_IGNORE)
@ -119,10 +97,6 @@ MapgenFractal::~MapgenFractal()
{
delete noise_seabed;
delete noise_filler_depth;
delete biomegen;
delete[] heightmap;
}

@ -59,10 +59,18 @@ struct MapgenFractalParams : public MapgenSpecificParams {
class MapgenFractal : public MapgenBasic {
public:
MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge);
~MapgenFractal();
virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
bool getFractalAtPoint(s16 x, s16 y, s16 z);
s16 generateTerrain();
private:
u16 formula;
bool julia;
u32 spflags;
u16 fractal;
u16 iterations;
v3f scale;
@ -74,22 +82,11 @@ public:
float julia_w;
Noise *noise_seabed;
content_t c_lava_source;
content_t c_ice;
content_t c_cobble;
content_t c_stair_cobble;
content_t c_mossycobble;
content_t c_sandstonebrick;
content_t c_stair_sandstonebrick;
MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge);
~MapgenFractal();
virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
bool getFractalAtPoint(s16 x, s16 y, s16 z);
s16 generateTerrain();
};
struct MapgenFactoryFractal : public MapgenFactory {

@ -48,15 +48,6 @@ FlagDesc flagdesc_mapgen_v5[] = {
MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge)
: MapgenBasic(mapgenid, params, emerge)
{
this->m_emerge = emerge;
this->bmgr = emerge->biomemgr;
// amount of elements to skip for the next index
// for noise/height/biome maps (not vmanip)
this->ystride = csize.X;
this->heightmap = new s16[csize.X * csize.Z];
MapgenV5Params *sp = (MapgenV5Params *)params->sparams;
this->spflags = sp->spflags;
@ -74,27 +65,14 @@ MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge)
MapgenBasic::np_cave1 = sp->np_cave1;
MapgenBasic::np_cave2 = sp->np_cave2;
//// Initialize biome generator
biomegen = emerge->biomemgr->createBiomeGen(
BIOMEGEN_ORIGINAL, params->bparams, csize);
biomemap = biomegen->biomemap;
//// Resolve nodes to be used
c_stone = ndef->getId("mapgen_stone");
c_water_source = ndef->getId("mapgen_water_source");
c_lava_source = ndef->getId("mapgen_lava_source");
c_desert_stone = ndef->getId("mapgen_desert_stone");
c_ice = ndef->getId("mapgen_ice");
c_sandstone = ndef->getId("mapgen_sandstone");
// Content used for dungeon generation
c_cobble = ndef->getId("mapgen_cobble");
c_stair_cobble = ndef->getId("mapgen_stair_cobble");
c_mossycobble = ndef->getId("mapgen_mossycobble");
c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
if (c_ice == CONTENT_IGNORE)
c_ice = CONTENT_AIR;
// Fall back to more basic content if not defined
if (c_mossycobble == CONTENT_IGNORE)
c_mossycobble = c_cobble;
if (c_stair_cobble == CONTENT_IGNORE)
@ -112,10 +90,6 @@ MapgenV5::~MapgenV5()
delete noise_factor;
delete noise_height;
delete noise_ground;
delete biomegen;
delete[] heightmap;
}

@ -50,26 +50,23 @@ struct MapgenV5Params : public MapgenSpecificParams {
class MapgenV5 : public MapgenBasic {
public:
u32 spflags;
Noise *noise_factor;
Noise *noise_height;
Noise *noise_ground;
content_t c_lava_source;
content_t c_ice;
content_t c_cobble;
content_t c_stair_cobble;
content_t c_mossycobble;
content_t c_sandstonebrick;
content_t c_stair_sandstonebrick;
MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge);
~MapgenV5();
virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
int generateBaseTerrain();
private:
Noise *noise_factor;
Noise *noise_height;
Noise *noise_ground;
content_t c_cobble;
content_t c_stair_cobble;
content_t c_mossycobble;
content_t c_sandstonebrick;
content_t c_stair_sandstonebrick;
};

@ -53,17 +53,6 @@ FlagDesc flagdesc_mapgen_v7[] = {
MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge)
: MapgenBasic(mapgenid, params, emerge)
{
this->m_emerge = emerge;
this->bmgr = emerge->biomemgr;
//// amount of elements to skip for the next index
//// for noise/height/biome maps (not vmanip)
this->ystride = csize.X;
// 1-up 1-down overgeneration
this->zstride_1u1d = csize.X * (csize.Y + 2);
this->heightmap = new s16[csize.X * csize.Z];
MapgenV7Params *sp = (MapgenV7Params *)params->sparams;
this->spflags = sp->spflags;
@ -86,28 +75,14 @@ MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge)
MapgenBasic::np_cave1 = sp->np_cave1;
MapgenBasic::np_cave2 = sp->np_cave2;
// TODO(hmmmm): should we have a way to disable biomemanager biomes?
//// Initialize biome generator
biomegen = emerge->biomemgr->createBiomeGen(
BIOMEGEN_ORIGINAL, params->bparams, csize);
biomemap = biomegen->biomemap;
//// Resolve nodes to be used
c_stone = ndef->getId("mapgen_stone");
c_water_source = ndef->getId("mapgen_water_source");
c_lava_source = ndef->getId("mapgen_lava_source");
c_desert_stone = ndef->getId("mapgen_desert_stone");
c_ice = ndef->getId("mapgen_ice");
c_sandstone = ndef->getId("mapgen_sandstone");
// Content used for dungeon generation
c_cobble = ndef->getId("mapgen_cobble");
c_stair_cobble = ndef->getId("mapgen_stair_cobble");
c_mossycobble = ndef->getId("mapgen_mossycobble");
c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
if (c_ice == CONTENT_IGNORE)
c_ice = CONTENT_AIR;
// Fall back to more basic content if not defined
if (c_mossycobble == CONTENT_IGNORE)
c_mossycobble = c_cobble;
if (c_stair_cobble == CONTENT_IGNORE)
@ -130,10 +105,6 @@ MapgenV7::~MapgenV7()
delete noise_ridge_uwater;
delete noise_mountain;
delete noise_ridge;
delete biomegen;
delete[] heightmap;
}

@ -56,27 +56,6 @@ struct MapgenV7Params : public MapgenSpecificParams {
class MapgenV7 : public MapgenBasic {
public:
int zstride_1u1d;
u32 spflags;
Noise *noise_terrain_base;
Noise *noise_terrain_alt;
Noise *noise_terrain_persist;
Noise *noise_height_select;
Noise *noise_mount_height;
Noise *noise_ridge_uwater;
Noise *noise_mountain;
Noise *noise_ridge;
content_t c_lava_source;
content_t c_ice;
content_t c_cobble;
content_t c_stair_cobble;
content_t c_mossycobble;
content_t c_sandstonebrick;
content_t c_stair_sandstonebrick;
MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge);
~MapgenV7();
@ -89,6 +68,22 @@ public:
bool getMountainTerrainFromMap(int idx_xyz, int idx_xz, s16 y);
int generateTerrain();
void generateRidgeTerrain();
private:
Noise *noise_terrain_base;
Noise *noise_terrain_alt;
Noise *noise_terrain_persist;
Noise *noise_height_select;
Noise *noise_mount_height;
Noise *noise_ridge_uwater;
Noise *noise_mountain;
Noise *noise_ridge;
content_t c_cobble;
content_t c_stair_cobble;
content_t c_mossycobble;
content_t c_sandstonebrick;
content_t c_stair_sandstonebrick;
};
struct MapgenFactoryV7 : public MapgenFactory {

@ -67,17 +67,8 @@ static FlagDesc flagdesc_mapgen_valleys[] = {
MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *emerge)
: MapgenBasic(mapgenid, params, emerge)
{
this->m_emerge = emerge;
this->bmgr = emerge->biomemgr;
//// amount of elements to skip for the next index
//// for noise/height/biome maps (not vmanip)
this->ystride = csize.X;
this->zstride = csize.X * (csize.Y + 2);
// 1-down overgeneration
this->zstride_1d = csize.X * (csize.Y + 1);
this->heightmap = new s16[csize.X * csize.Z];
// NOTE: MapgenValleys has a hard dependency on BiomeGenOriginal
this->m_bgen = (BiomeGenOriginal *)biomegen;
this->map_gen_limit = MYMIN(MAX_MAP_GENERATION_LIMIT,
g_settings->getU16("map_generation_limit"));
@ -111,13 +102,6 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *
noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z);
noise_massive_caves = new Noise(&sp->np_massive_caves, seed, csize.X, csize.Y + 1, csize.Z);
//// Initialize biome generator
// NOTE: valleys mapgen can only use BiomeGenOriginal
biomegen = emerge->biomemgr->createBiomeGen(
BIOMEGEN_ORIGINAL, params->bparams, csize);
biomemap = biomegen->biomemap;
m_bgen = (BiomeGenOriginal *)biomegen;
this->humid_rivers = (spflags & MGVALLEYS_HUMID_RIVERS);
this->use_altitude_chill = (spflags & MGVALLEYS_ALT_CHILL);
this->humidity_adjust = bp->np_humidity.offset - 50.f;
@ -128,25 +112,18 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *
tcave_cache = new float[csize.Y + 2];
//// Resolve nodes to be used
// Resolve content to be used
c_cobble = ndef->getId("mapgen_cobble");
c_desert_stone = ndef->getId("mapgen_desert_stone");
c_dirt = ndef->getId("mapgen_dirt");
c_lava_source = ndef->getId("mapgen_lava_source");
c_mossycobble = ndef->getId("mapgen_mossycobble");
c_river_water_source = ndef->getId("mapgen_river_water_source");
c_sand = ndef->getId("mapgen_sand");
c_sandstonebrick = ndef->getId("mapgen_sandstonebrick");
c_sandstone = ndef->getId("mapgen_sandstone");
c_stair_cobble = ndef->getId("mapgen_stair_cobble");
c_stair_sandstonebrick = ndef->getId("mapgen_stair_sandstonebrick");
c_stone = ndef->getId("mapgen_stone");
c_water_source = ndef->getId("mapgen_water_source");
// Fall back to more basic content if not defined
if (c_mossycobble == CONTENT_IGNORE)
c_mossycobble = c_cobble;
if (c_river_water_source == CONTENT_IGNORE)
c_river_water_source = c_water_source;
if (c_sand == CONTENT_IGNORE)
c_sand = c_stone;
if (c_sandstonebrick == CONTENT_IGNORE)
@ -171,9 +148,6 @@ MapgenValleys::~MapgenValleys()
delete noise_valley_depth;
delete noise_valley_profile;
delete biomegen;
delete[] heightmap;
delete[] tcave_cache;
}
@ -289,13 +263,13 @@ void MapgenValleys::makeChunk(BlockMakeData *data)
// Generate biome noises. Note this must be executed strictly before
// generateTerrain, because generateTerrain depends on intermediate
// biome-related noises.
biomegen->calcBiomeNoise(node_min);
m_bgen->calcBiomeNoise(node_min);
// Generate base terrain with initial heightmaps
s16 stone_surface_max_y = generateTerrain();
// Build biomemap
biomegen->getBiomes(heightmap);
m_bgen->getBiomes(heightmap);
// Place biome-specific nodes
MgStoneType stone_type = generateBiomes();
@ -606,7 +580,7 @@ int MapgenValleys::generateTerrain()
}
}
u32 index_3d = (z - node_min.Z) * zstride + (x - node_min.X);
u32 index_3d = (z - node_min.Z) * zstride_1u1d + (x - node_min.X);
u32 index_data = vm->m_area.index(x, node_min.Y - 1, z);
// Mapgens concern themselves with stone and water.
@ -755,7 +729,7 @@ void MapgenValleys::generateCaves(s16 max_stone_y, s16 large_cave_depth)
u32 index_2d = 0;
for (s16 z = node_min.Z; z <= node_max.Z; z++)
for (s16 x = node_min.X; x <= node_max.X; x++, index_2d++) {
Biome *biome = (Biome *)bmgr->getRaw(biomemap[index_2d]);
Biome *biome = (Biome *)m_bmgr->getRaw(biomemap[index_2d]);
bool tunnel_air_above = false;
bool underground = false;
u32 index_data = vm->m_area.index(x, node_max.Y, z);

@ -99,9 +99,6 @@ public:
private:
BiomeGenOriginal *m_bgen;
int zstride;
int zstride_1d;
float map_gen_limit;
bool humid_rivers;
@ -110,7 +107,6 @@ private:
s16 cave_water_max_height;
s16 lava_max_height;
u32 spflags;
float altitude_chill;
s16 lava_features_lim;
s16 massive_cave_depth;
@ -129,7 +125,6 @@ private:
Noise *noise_valley_profile;
content_t c_cobble;
content_t c_dirt;
content_t c_lava_source;
content_t c_mossycobble;
content_t c_sand;