1 /** 2 Copyright: Copyright (c) 2013-2016 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 module voxelman.world.gen.utils; 7 8 import std.experimental.logger; 9 import voxelman.math : ivec3, SimplexNoise; 10 11 import voxelman.block.utils; 12 import voxelman.core.config; 13 import voxelman.utils.worker; 14 import voxelman.world.storage.chunk; 15 import voxelman.world.storage.coordinates; 16 17 enum AIR = 1; 18 enum GRASS = 2; 19 enum DIRT = 3; 20 enum STONE = 4; 21 enum SAND = 5; 22 enum WATER = 6; 23 24 struct ChunkGeneratorResult 25 { 26 bool uniform; 27 BlockId uniformBlockId; 28 } 29 30 double noise2d(int x, int z) 31 { 32 enum NUM_OCTAVES = 8; 33 enum DIVIDER = 50; // bigger - smoother 34 enum HEIGHT_MODIFIER = 4; // bigger - higher 35 36 double noise = 0.0; 37 foreach(i; 1..NUM_OCTAVES+1) 38 { 39 // [-1; 1] 40 noise += SimplexNoise.noise(cast(double)x/(DIVIDER*i), cast(double)z/(DIVIDER*i))*i*HEIGHT_MODIFIER; 41 } 42 43 return noise; 44 } 45 46 struct HeightmapChunkData 47 { 48 int[CHUNK_SIZE_SQR] heightMap = void; 49 int minHeight = int.max; 50 int maxHeight = int.min; 51 52 void generate(ivec3 chunkOffset) 53 { 54 foreach(i, ref elem; heightMap) 55 { 56 int cx = i & CHUNK_SIZE_BITS; 57 int cz = (i / CHUNK_SIZE) & CHUNK_SIZE_BITS; 58 elem = cast(int)noise2d(chunkOffset.x + cx, chunkOffset.z + cz); 59 if (elem > maxHeight) 60 maxHeight = elem; 61 if (elem < minHeight) 62 minHeight = elem; 63 } 64 } 65 }