12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #include "DimensionGenerator.h"
- #include "Constants.h"
- #include "Noise.h"
- #include "NoBlock.h"
- #include <iostream>
- DimensionGenerator::DimensionGenerator( int dimensionId )
- : ReferenceCounter(),
- dimensionId( dimensionId )
- {
- StaticRegistry<DimensionGenerator>::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, int centerX, int centerY )
- {
- std::cout << "generating chunk " << centerX << ", " << centerY << "\n";
- Chunk* chunk = new Chunk( Framework::Punkt( centerX, centerY ), 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<Block*, int> generated = AirBlockBlockType::ID;
- if( z < height )
- generated = biom->getBlock( x + centerX, y + centerY, z );
- if( generated.isA() )
- chunk->putBlockAt( Framework::Vec3<int>( x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z ), generated );
- else
- chunk->putBlockTypeAt( Framework::Vec3<int>( x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z ), generated );
- }
- }
- }
- return chunk;
- }
- Framework::Either<Block*, int> DimensionGenerator::generateBlock( int seed, Framework::Vec3<int> 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 );
- return AirBlockBlockType::ID;
- }
- int DimensionGenerator::getDimensionId() const
- {
- return dimensionId;
- }
|