Browse Source

use bedder dandom noise to generate trees

Kolja Strohm 3 years ago
parent
commit
9af2bf34d5

+ 2 - 2
FactoryCraft/DimensionGenerator.cpp

@@ -104,7 +104,7 @@ Chunk* DimensionGenerator::generateChunk( int seed, int centerX, int centerY )
             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));
-            int maxSurfaceHeight = (1 - (height - MIN_AIR_LEVEL) / (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
+            int maxSurfaceHeight = (int)(MAX_SURFACE_HEIGHT * (1.f - (float)(height - MIN_AIR_LEVEL) / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
             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 )));
             for( int z = 0; z < WORLD_HEIGHT; z++ )
             {
@@ -143,7 +143,7 @@ Framework::Either<Block*, int> DimensionGenerator::generateBlock( int seed, Fram
     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));
-    int maxSurfaceHeight = (1 - (height - MIN_AIR_LEVEL) / (MAX_AIR_LEVEL - MIN_AIR_LEVEL));
+    int maxSurfaceHeight = (int)(MAX_SURFACE_HEIGHT * (1.f - (float)(height - MIN_AIR_LEVEL) / (float)(MAX_AIR_LEVEL - MIN_AIR_LEVEL)));
     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 )));
     for( auto structure : *structures )
     {

+ 2 - 0
FactoryCraft/FactoryCraft.vcxproj

@@ -133,6 +133,7 @@
     <ClInclude Include="PlaceBlockUpdate.h" />
     <ClInclude Include="Player.h" />
     <ClInclude Include="PlayerHand.h" />
+    <ClInclude Include="RandNoise.h" />
     <ClInclude Include="Server.h" />
     <ClInclude Include="Dimension.h" />
     <ClInclude Include="StaticRegistry.h" />
@@ -183,6 +184,7 @@
     <ClCompile Include="PlaceBlockUpdate.cpp" />
     <ClCompile Include="Player.cpp" />
     <ClCompile Include="PlayerHand.cpp" />
+    <ClCompile Include="RandNoise.cpp" />
     <ClCompile Include="Server.cpp" />
     <ClCompile Include="Start.cpp" />
     <ClCompile Include="StaticInitializerOrder.cpp" />

+ 6 - 0
FactoryCraft/FactoryCraft.vcxproj.filters

@@ -216,6 +216,9 @@
     <ClInclude Include="TreeTemplate.h">
       <Filter>world\generator\templates\implementations</Filter>
     </ClInclude>
+    <ClInclude Include="RandNoise.h">
+      <Filter>world\generator\noise</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Server.cpp">
@@ -362,5 +365,8 @@
     <ClCompile Include="TreeTemplate.cpp">
       <Filter>world\generator\templates\implementations</Filter>
     </ClCompile>
+    <ClCompile Include="RandNoise.cpp">
+      <Filter>world\generator\noise</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 2 - 2
FactoryCraft/GeneratedStructure.cpp

@@ -40,7 +40,7 @@ void GeneratedStructure::setBlockAt( Framework::Either<Block*, int> block, Frame
 bool GeneratedStructure::isBlockAffected( Framework::Vec3<int> location ) const
 {
     Framework::Vec3<int> localPos = location - originPos;
-    if( location.x >= 0 && location.y >= 0 && location.z >= 0 && location.x < size.x && location.y < size.y && location.z < size.z )
+    if( localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0 && localPos.x < size.x && localPos.y < size.y && localPos.z < size.z )
     {
         int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
         return blocks[ index ] || blockIds[ index ];
@@ -51,7 +51,7 @@ bool GeneratedStructure::isBlockAffected( Framework::Vec3<int> location ) const
 Framework::Either<Block*, int> GeneratedStructure::generateBlockAt( Framework::Vec3<int> location ) const
 {
     Framework::Vec3<int> localPos = location - originPos;
-    if( location.x >= 0 && location.y >= 0 && location.z >= 0 && location.x < size.x && location.y < size.y && location.z < size.z )
+    if( localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0 && localPos.x < size.x && localPos.y < size.y && localPos.z < size.z )
     {
         int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
         if( blocks[ index ] )

+ 3 - 3
FactoryCraft/GrasslandBiom.cpp

@@ -9,9 +9,9 @@
 GrasslandBiom::GrasslandBiom()
     : BiomGenerator()
 {
-    addTemplateGenerator( new TreeTemplate( 0.01f, BirchBlockType::INSTANCE, LeavesBlockType::INSTANCE, 8, 15 ) );
-    addTemplateGenerator( new TreeTemplate( 0.005f, BeechBlockType::INSTANCE, LeavesBlockType::INSTANCE, 8, 13 ) );
-    addTemplateGenerator( new TreeTemplate( 0.0025f, OakBlockType::INSTANCE, LeavesBlockType::INSTANCE, 10, 15 ) );
+    addTemplateGenerator( new TreeTemplate( 0.02f, BirchBlockType::INSTANCE, LeavesBlockType::INSTANCE, 8, 15 ) );
+    addTemplateGenerator( new TreeTemplate( 0.01f, BeechBlockType::INSTANCE, LeavesBlockType::INSTANCE, 8, 13 ) );
+    addTemplateGenerator( new TreeTemplate( 0.005f, OakBlockType::INSTANCE, LeavesBlockType::INSTANCE, 10, 15 ) );
     heightNoise = 0;
 }
 

+ 3 - 6
FactoryCraft/OverworldDimension.cpp

@@ -2,6 +2,8 @@
 #include "BasicInterpolator.h"
 #include "GrasslandBiom.h"
 #include "FastNoiseWrapper.h"
+#include "RandNoise.h"
+
 
 OverworldDimension::OverworldDimension()
     : DimensionGenerator( ID )
@@ -41,11 +43,6 @@ Noise* OverworldDimension::zStructureNoise( int seed )
 {
     if( structureNoise )
         return structureNoise;
-    FastNoiseLite* noise = new FastNoiseLite( seed );
-    noise->SetNoiseType( FastNoiseLite::NoiseType::NoiseType_OpenSimplex2S );
-    noise->SetRotationType3D( FastNoiseLite::RotationType3D::RotationType3D_None );
-    noise->SetFrequency( 0.25f );
-    noise->SetFractalType( FastNoiseLite::FractalType::FractalType_None );
-    structureNoise = new FastNoiseWrapper( noise, seed );
+    structureNoise = new RandNoise( seed );
     return structureNoise;
 }

+ 39 - 0
FactoryCraft/RandNoise.cpp

@@ -0,0 +1,39 @@
+#include <Random.h>
+#include <stdlib.h>
+
+#include "RandNoise.h"
+#include "Constants.h"
+#include "FastNoiseLite.h"
+#include "FastNoiseWrapper.h"
+
+
+RandNoise::RandNoise( int seed )
+    : Noise(),
+    seed( seed )
+{
+    FastNoiseLite* n = new FastNoiseLite( seed );
+    n->SetNoiseType( FastNoiseLite::NoiseType::NoiseType_OpenSimplex2S );
+    n->SetRotationType3D( FastNoiseLite::RotationType3D::RotationType3D_None );
+    n->SetFrequency( 0.333f );
+    n->SetFractalOctaves( 1 );
+    n->SetFractalType( FastNoiseLite::FractalType::FractalType_None );
+    n->SetDomainWarpAmp( 30.f );
+    noise = new FastNoiseWrapper( n, seed );
+}
+
+RandNoise::~RandNoise()
+{
+    delete noise;
+}
+
+int RandNoise::getSeed() const
+{
+    return seed;
+}
+
+double RandNoise::getNoise( double x, double y, double z )
+{
+    double n = noise->getNoise( x * 4 + y + z * 7, 7 * x + 4 * y + z, x + 7 * y + 4 * z );
+    assert( n >= 0 && n <= 1 );
+    return n;
+}

+ 19 - 0
FactoryCraft/RandNoise.h

@@ -0,0 +1,19 @@
+#pragma once
+
+#include "Noise.h"
+
+class FastNoiseWrapper;
+
+class RandNoise : public Noise
+{
+private:
+    int seed;
+    FastNoiseWrapper* noise;
+
+public:
+    RandNoise( int seed );
+    ~RandNoise();
+
+    int getSeed() const override;
+    double getNoise( double x, double y, double z ) override;
+};

+ 1 - 1
FactoryCraft/TreeTemplate.cpp

@@ -11,7 +11,7 @@ TreeTemplate::TreeTemplate( float propability, const BlockType* zWoodType, const
 
 GeneratedStructure* TreeTemplate::generateAt( Framework::Vec3<int> location, Noise* zNoise )
 {
-    double noise = zNoise->getNoise( (double)location.x, (double)location.y, (double)location.z );
+    double noise = zNoise->getNoise( (double)location.x + 40, (double)location.y + 70, (double)location.z - 20 );
     int height = (int)(minHeight + (noise * (maxHeight - minHeight)));
     GeneratedStructure* generated = new GeneratedStructure( dynamic_cast<GenerationTemplate*>(getThis()), location, Framework::Vec3<int>( 5, 5, height ), Framework::Vec3<int>( -3, -3, 0 ) + location );
     for( int x = 1; x < 4; x++ )