DimensionGenerator.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "DimensionGenerator.h"
  2. #include "Constants.h"
  3. #include "Noise.h"
  4. #include "NoBlock.h"
  5. #include <iostream>
  6. DimensionGenerator::DimensionGenerator( int dimensionId )
  7. : ReferenceCounter(),
  8. dimensionId( dimensionId ),
  9. minTemplateAffectedPosition( 0, 0, 0 ),
  10. maxTemplateAffectedPosition( 0, 0, 0 )
  11. {
  12. StaticRegistry<DimensionGenerator>::INSTANCE.registerT( this, dimensionId );
  13. }
  14. DimensionGenerator::~DimensionGenerator()
  15. {}
  16. BiomGenerator* DimensionGenerator::zBiomGenerator( int seed, int x, int y )
  17. {
  18. double noise = zBiomNoise( seed )->getNoise( (double)x, (double)y, 0.0 );
  19. double border = 0;
  20. BiomGenerator* gen = 0;
  21. auto genI = biomGenerators.begin();
  22. auto distI = biomDistribution.begin();
  23. do
  24. {
  25. border += (double)distI++;
  26. gen = genI++;
  27. } while( border < noise && (bool)distI && (bool)genI );
  28. return gen;
  29. }
  30. void DimensionGenerator::registerBiom( BiomGenerator* generator, double possibility )
  31. {
  32. biomGenerators.add( generator );
  33. biomDistribution.add( possibility );
  34. for( auto t : generator->getTemplates() )
  35. {
  36. minTemplateAffectedPosition.x = MIN( minTemplateAffectedPosition.x, t->getMinAffectedOffset().x );
  37. minTemplateAffectedPosition.y = MIN( minTemplateAffectedPosition.y, t->getMinAffectedOffset().y );
  38. minTemplateAffectedPosition.z = MIN( minTemplateAffectedPosition.z, t->getMinAffectedOffset().z );
  39. maxTemplateAffectedPosition.x = MAX( maxTemplateAffectedPosition.x, t->getMaxAffectedOffset().x );
  40. maxTemplateAffectedPosition.y = MAX( maxTemplateAffectedPosition.y, t->getMaxAffectedOffset().y );
  41. maxTemplateAffectedPosition.z = MAX( maxTemplateAffectedPosition.z, t->getMaxAffectedOffset().z );
  42. }
  43. }
  44. Framework::RCArray<GeneratedStructure>* DimensionGenerator::getGeneratedStructoresForArea( int seed, Framework::Vec3<int> minPos, Framework::Vec3<int> maxPos )
  45. {
  46. Framework::RCArray<GeneratedStructure>* result = new Framework::RCArray<GeneratedStructure>();
  47. int minSearchX = minPos.x - maxTemplateAffectedPosition.x;
  48. int minSearchY = minPos.y - maxTemplateAffectedPosition.y;
  49. int minSearchZ = MAX( minPos.z - maxTemplateAffectedPosition.z, 0 );
  50. int maxSearchX = maxPos.x - minTemplateAffectedPosition.x;
  51. int maxSearchY = maxPos.y - minTemplateAffectedPosition.y;
  52. int maxSearchZ = MIN( maxPos.z - minTemplateAffectedPosition.z, WORLD_HEIGHT - 1 );
  53. Noise* structureNoise = zStructureNoise( seed + dimensionId );
  54. for( int x = minSearchX; x <= maxSearchX; x++ )
  55. {
  56. for( int y = minSearchY; y <= maxSearchY; y++ )
  57. {
  58. BiomGenerator* biom = zBiomGenerator( seed + dimensionId, x, y );
  59. int height = MIN_AIR_LEVEL + (int)(biom->zHeightMapNoise( seed + dimensionId )->getNoise( (double)(x), (double)(y), 0.0 ) * (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
  60. for( int z = minSearchZ; z <= maxSearchZ; z++ )
  61. {
  62. if( z < height )
  63. {
  64. double rValue = structureNoise->getNoise( (double)x, (double)y, (double)z );
  65. double probSum = 0;
  66. for( auto t : biom->getTemplates() )
  67. {
  68. if( t->isGenerationPossable( Framework::Vec3<int>( x, y, z ), height - z ) )
  69. {
  70. if( rValue - probSum <= t->getPropability() )
  71. {
  72. result->add( t->generateAt( Framework::Vec3<int>( x, y, z ), structureNoise ) );
  73. break;
  74. }
  75. }
  76. probSum += t->getPropability();
  77. }
  78. }
  79. }
  80. }
  81. }
  82. return result;
  83. }
  84. Chunk* DimensionGenerator::generateChunk( int seed, int centerX, int centerY )
  85. {
  86. Framework::RCArray<GeneratedStructure>* structures = getGeneratedStructoresForArea( seed, Framework::Vec3<int>( centerX - CHUNK_SIZE / 2, centerY - CHUNK_SIZE / 2, 0 ), Framework::Vec3<int>( centerX + CHUNK_SIZE / 2, centerY + CHUNK_SIZE / 2, WORLD_HEIGHT - 1 ) );
  87. std::cout << "generating chunk " << centerX << ", " << centerY << "\n";
  88. Chunk* chunk = new Chunk( Framework::Punkt( centerX, centerY ), dimensionId );
  89. for( int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++ )
  90. {
  91. for( int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++ )
  92. {
  93. BiomGenerator* biom = zBiomGenerator( seed + dimensionId, x + centerX, y + centerY );
  94. // TODO: use Noise interpolator for height map between different bioms
  95. 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));
  96. int maxSurfaceHeight = (int)(MAX_SURFACE_HEIGHT * (1.f - (float)(height - MIN_AIR_LEVEL) / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
  97. int actualSurfaceHeight = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART) + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART * (float)biom->zHeightMapNoise( seed + dimensionId )->getNoise( (double)(x + centerX), (double)(y + centerY), 10.0 )));
  98. for( int z = 0; z < WORLD_HEIGHT; z++ )
  99. {
  100. Framework::Either<Block*, int> generated = AirBlockBlockType::ID;
  101. bool structureAffected = 0;
  102. for( auto structure : *structures )
  103. {
  104. if( structure->isBlockAffected( Framework::Vec3<int>( x + centerX, y + centerY, z ) ) )
  105. {
  106. generated = structure->generateBlockAt( Framework::Vec3<int>( x + centerX, y + centerY, z ) );
  107. structureAffected = 1;
  108. break;
  109. }
  110. }
  111. if( !structureAffected )
  112. {
  113. if( z < height && z >= height - actualSurfaceHeight )
  114. generated = biom->generateSurfaceBlock( x + centerX, y + centerY, z );
  115. else if( z < height )
  116. generated = biom->generateBelowSurfaceBlock( x + centerX, y + centerY, z );
  117. }
  118. if( generated.isA() )
  119. chunk->putBlockAt( Framework::Vec3<int>( x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z ), generated );
  120. else
  121. chunk->putBlockTypeAt( Framework::Vec3<int>( x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2, z ), generated );
  122. }
  123. }
  124. }
  125. structures->release();
  126. return chunk;
  127. }
  128. Framework::Either<Block*, int> DimensionGenerator::generateBlock( int seed, Framework::Vec3<int> location )
  129. {
  130. Framework::RCArray<GeneratedStructure>* structures = getGeneratedStructoresForArea( seed, location, location );
  131. BiomGenerator* biom = zBiomGenerator( seed + dimensionId, location.x, location.y );
  132. // TODO: use Noise interpolator for height map between different bioms
  133. 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));
  134. int maxSurfaceHeight = (int)(MAX_SURFACE_HEIGHT * (1.f - (float)(height - MIN_AIR_LEVEL) / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
  135. int actualSurfaceHeight = (int)((float)maxSurfaceHeight * (1.f - VARIABLE_SURFACE_PART) + ((float)maxSurfaceHeight * VARIABLE_SURFACE_PART * (float)biom->zHeightMapNoise( seed + dimensionId )->getNoise( (double)(location.x), (double)(location.y), 10.0 )));
  136. for( auto structure : *structures )
  137. {
  138. if( structure->isBlockAffected( location ) )
  139. {
  140. auto generated = structure->generateBlockAt( location );
  141. structures->release();
  142. return generated;
  143. }
  144. }
  145. structures->release();
  146. if( location.z < height && location.z >= height - actualSurfaceHeight )
  147. return biom->generateSurfaceBlock( location.x, location.y, location.z );
  148. else if( location.z < height )
  149. return biom->generateBelowSurfaceBlock( location.x, location.y, location.z );
  150. return AirBlockBlockType::ID;
  151. }
  152. int DimensionGenerator::getDimensionId() const
  153. {
  154. return dimensionId;
  155. }