1 /**
2 Copyright: Copyright (c) 2013-2018 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 voxelman.log;
9 import voxelman.math : ivec3, SimplexNoise;
10 
11 import voxelman.world.block;
12 import voxelman.core.config;
13 import voxelman.thread.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 enum LAVA = 7;
24 enum SNOW = 8;
25 
26 struct ChunkGeneratorResult
27 {
28 	bool uniform;
29 	BlockId uniformBlockId;
30 }
31 
32 double noise2d(int x, int z)
33 {
34 	enum NUM_OCTAVES = 8;
35 	enum DIVIDER = 50; // bigger - smoother
36 	enum HEIGHT_MODIFIER = 4; // bigger - higher
37 
38 	double noise = 0.0;
39 	foreach(i; 1..NUM_OCTAVES+1)
40 	{
41 		// [-1; 1]
42 		noise += SimplexNoise.noise(cast(double)x/(DIVIDER*i), cast(double)z/(DIVIDER*i))*i*HEIGHT_MODIFIER;
43 	}
44 
45 	return noise;
46 }
47 
48 struct HeightmapChunkData
49 {
50 	int[CHUNK_SIZE_SQR] heightMap = void;
51 	int minHeight = int.max;
52 	int maxHeight = int.min;
53 
54 	void generate(ivec3 chunkOffset)
55 	{
56 		foreach(i, ref elem; heightMap)
57 		{
58 			int cx = i & CHUNK_SIZE_BITS;
59 			int cz = (i / CHUNK_SIZE) & CHUNK_SIZE_BITS;
60 			elem = cast(int)noise2d(chunkOffset.x + cx, chunkOffset.z + cz);
61 			if (elem > maxHeight)
62 				maxHeight = elem;
63 			if (elem < minHeight)
64 				minHeight = elem;
65 		}
66 	}
67 }