#include "DimensionGenerator.h" #include "Constants.h" #include "Noise.h" #include "NoBlock.h" #include DimensionGenerator::DimensionGenerator( int dimensionId ) : ReferenceCounter(), dimensionId( dimensionId ) { StaticRegistry::INSTANCE.registerT( this, dimensionId ); } DimensionGenerator::~DimensionGenerator() {} BiomGenerator* DimensionGenerator::zBiomGenerator( int seed, int x, int y ) { double noise = zBiomNoise( seed )->getNoise( (double)x, (double)y, 0.0 ); double border = 0; BiomGenerator* gen = 0; auto genI = biomGenerators.begin(); auto distI = biomDistribution.begin(); do { border += (double)distI++; gen = genI++; } while( border < noise && (bool)distI && (bool)genI ); return gen; } void DimensionGenerator::registerBiom( BiomGenerator* generator, double possibility ) { biomGenerators.add( generator ); biomDistribution.add( possibility ); } Chunk* DimensionGenerator::generateChunk( int seed, Game* zGame, int centerX, int centerY ) { std::cout << "generating chunk " << centerX << ", " << centerY << "\n"; Chunk* chunk = new Chunk( Framework::Punkt( centerX, centerY ), zGame, dimensionId ); for( int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++ ) { for( int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++ ) { BiomGenerator* biom = zBiomGenerator( seed + dimensionId, x + centerX, y + centerY ); // TODO: use Noise interpolator for height map between different bioms int height = MIN_AIR_LEVEL + (int)(biom->zHeightMapNoise( seed + dimensionId )->getNoise( (double)(x + centerX), (double)(y + centerY), 0.0 ) * (MAX_AIR_LEVEL - MIN_AIR_LEVEL)); for( int z = 0; z < WORLD_HEIGHT; z++ ) { Framework::Either generated = AirBlockBlockType::ID; if( z < height ) generated = biom->getBlock( x + centerX, y + centerY, z, zGame ); if( generated.isA() ) chunk->putBlockAt( Framework::Vec3( x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z ), generated ); else chunk->putBlockTypeAt( Framework::Vec3( x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z ), generated ); } } } return chunk; } Framework::Either DimensionGenerator::generateBlock( int seed, Game* zGame, Framework::Vec3 location ) { BiomGenerator* biom = zBiomGenerator( seed + dimensionId, location.x, location.y ); // TODO: use Noise interpolator for height map between different bioms int height = MIN_AIR_LEVEL + (int)(biom->zHeightMapNoise( seed + dimensionId )->getNoise( (double)(location.x), (double)(location.y), 0.0 ) * (MAX_AIR_LEVEL - MIN_AIR_LEVEL)); if( location.z < height ) return biom->getBlock( location.x, location.y, location.z, zGame ); return AirBlockBlockType::ID; } int DimensionGenerator::getDimensionId() const { return dimensionId; }