Skip to content

Commit

Permalink
Add automated biomes
Browse files Browse the repository at this point in the history
  • Loading branch information
Janorico committed Oct 2, 2024
1 parent 288b2b8 commit bf84b8c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
8 changes: 4 additions & 4 deletions modules/voxelfun2/worldgen/biomes/biomes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Biome {
public:
Biome() {};
~Biome() {};
int generate_surface_block(int y, int surface_y) {
virtual int generate_surface_block(int y, int surface_y) {
return (y == 0) ? VoxelFun2Misc::STONE : VoxelFun2Misc::AIR;
};

Expand All @@ -27,15 +27,15 @@ class Biome {

class Meadow : public Biome {
public:
int generate_surface_block(int y, int surface_y);
int generate_surface_block(int y, int surface_y) override;
};

class SnowyMeadow : public Biome {
public:
int generate_surface_block(int y, int surface_y);
int generate_surface_block(int y, int surface_y) override;
};

class Desert : public Biome {
public:
int generate_surface_block(int y, int surface_y);
int generate_surface_block(int y, int surface_y) override;
};
16 changes: 15 additions & 1 deletion modules/voxelfun2/worldgen/voxel_generator_voxelfun2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ VoxelGeneratorVoxelFun2::VoxelGeneratorVoxelFun2() {
selector_noise.set_period(512.0);
// Caves
cheese_cave_noise.set_period(128.0);
// Biomes
humidity_noise.set_period(2048.0);
temperature_noise.set_period(2048.0);
// Other stuff
update_seed(_parameters.seed);
}
Expand Down Expand Up @@ -79,11 +82,12 @@ VoxelGenerator::Result VoxelGeneratorVoxelFun2::generate_block(VoxelBlockRequest
float mountain_y = (mountain_noise.get_noise_2d(gx, gz) * params.mountain_height) + params.mountain_height / 2;
float selector = selector_noise.get_noise_2d(gx, gz) * 0.5f + 0.5f;
int surface_y = int(Math::lerp(hill_y, mountain_y, selector));
Biome *biome = select_biome(humidity_noise.get_noise_2d(gx, gz), temperature_noise.get_noise_2d(gx, gz));
for (int y = 0; y < bs.y; ++y) {
int gy = origin.y + y;
bool cheese_cave = ((selector > 0.4) ? gy <= surface_y : gy < surface_y) && (cheese_cave_noise.get_noise_3d(gx, gy, gz)) > 0.6;
if (!cheese_cave && gy <= surface_y) {
out_buffer.set_voxel(biome.generate_surface_block(gy, surface_y), x, y, z);
out_buffer.set_voxel(biome->generate_surface_block(gy, surface_y), x, y, z);
}
}
} // for z
Expand All @@ -92,6 +96,16 @@ VoxelGenerator::Result VoxelGeneratorVoxelFun2::generate_block(VoxelBlockRequest
return result;
}

Biome *VoxelGeneratorVoxelFun2::select_biome(float humidity, float temperature) {
if (temperature < -0.4) {
return biomes.snowy_meadow;
} else if (temperature > 0.4) {
return biomes.desert;
} else {
return biomes.meadow;
}
}

void VoxelGeneratorVoxelFun2::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_hill_height", "h"), &VoxelGeneratorVoxelFun2::set_hill_height);
ClassDB::bind_method(D_METHOD("get_hill_height"), &VoxelGeneratorVoxelFun2::get_hill_height);
Expand Down
13 changes: 11 additions & 2 deletions modules/voxelfun2/worldgen/voxel_generator_voxelfun2.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,27 @@ class VoxelGeneratorVoxelFun2 : public VoxelGenerator {
int seed = 0;
};

struct Biomes {
Meadow *meadow = new Meadow();
SnowyMeadow *snowy_meadow = new SnowyMeadow();
Desert *desert = new Desert();
};

Parameters _parameters;
RWLock _parameters_lock;
// Heightmap
FastNoiseLite hill_noise;
FastNoiseLite mountain_noise;
FastNoiseLite selector_noise;
// Biomes
FastNoiseLite humidity_noise;
FastNoiseLite temperature_noise;
Biomes biomes;
// Caves
FastNoiseLite cheese_cave_noise;

Meadow biome;

void update_seed(int s);
Biome *select_biome(float humidity, float temperature);
};

#endif // VOXEL_GENERATOR_VOXELFUN2_H

0 comments on commit bf84b8c

Please sign in to comment.