summaryrefslogtreecommitdiff
path: root/terrain_generator.cpp
blob: fe9ddd02fb8a8ff9c52e01103699625a6fbbf81f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "terrain_generator.h"

#include <noise/noise.h>
#include "noiseutils/noiseutils.h"
#include <boost/random.hpp>
#include <boost/random/variate_generator.hpp>

using namespace noise;

TerrainGenerator::TerrainGenerator(int seed, fs::path root) : TerrainLoader(root) {
	this->seed = seed;
}

float *TerrainGenerator::generate_heights(int64_t x, int64_t y, unsigned int width, unsigned int height) {
	const double scale_factor = 0.004;
	module::Perlin mod;
	mod.SetSeed(seed);

	utils::NoiseMap heightmap;
	utils::NoiseMapBuilderPlane heightmap_builder;

	heightmap_builder.SetSourceModule(mod);
	heightmap_builder.SetDestNoiseMap(heightmap);

	heightmap_builder.SetDestSize(width, height);
	// subtract by one due to chunk overlapping
	heightmap_builder.SetBounds((double)(x-1) * scale_factor, (double)(x-1+width) * scale_factor,
			(double)(y-1) * scale_factor, (double)(y-1+height) * scale_factor);
	heightmap_builder.Build();

	float *heights = new float[width*height];
	for(unsigned int i = 0; i < width; i++) {
		for(unsigned int j = 0; j < height; j++) {
			heights[i*height + j] = 50*(1+heightmap.GetValue(i, j));
		}
	}

	save_chunk(heights, x, y, width, height);

	return heights;
}

float *TerrainGenerator::get_chunk(int64_t x, int64_t y, unsigned int width, unsigned int height) {
	float *h;
	if(has_chunk(x, y))
		h = load_chunk(x, y, width, height);
	else
		h = generate_heights(x, y, width, height);
	return h;
}

// TODO: merge this into terrain height generation above somehow
std::list<Vector3> TerrainGenerator::generate_objects(int64_t x, int64_t y, unsigned int width, unsigned int height) {
	std::list<Vector3> trees;

	const double scale_factor = 0.004;
	// "base" needs to match the terrain heightmap noise
	module::Perlin base;
	base.SetSeed(seed);

	module::Const low;
	low.SetConstValue(0);

	module::Select final;
	final.SetSourceModule(0, low);
	final.SetSourceModule(1, base);
	final.SetControlModule(base);
	const float lower = -.24;
	const float upper = .19;
	final.SetBounds(lower, upper);

	utils::NoiseMap heightmap;
	utils::NoiseMapBuilderPlane heightmap_builder;

	heightmap_builder.SetSourceModule(final);
	heightmap_builder.SetDestNoiseMap(heightmap);

	heightmap_builder.SetDestSize(width, height);
	heightmap_builder.SetBounds((double)x * scale_factor, (double)(x+33) * scale_factor,
			(double)y * scale_factor, (double)(y+33) * scale_factor);
	heightmap_builder.Build();

	boost::mt19937 rng(seed);
	boost::normal_distribution<float> nd(-2, .7);
	boost::variate_generator<boost::mt19937&, boost::normal_distribution<float> > r(rng, nd);

	for(unsigned int i = 0; i < 32; i++) {
		for(unsigned int j = 0; j < 32; j++) {
			if(heightmap.GetValue(i, j) > 0 && r() > 0)
				trees.push_back(Vector3(x + i, 0, y + j));
		}
	}

	return trees;
}

std::list<Vector3> TerrainGenerator::get_objects(int64_t x, int64_t y, unsigned int width, unsigned int height) {
	if(has_objects(x, y))
		return load_objects(x, y);
	else
		return generate_objects(x, y, width, height);
}