3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "mapgen_indev.h"
21 #include "constants.h"
24 #include "util/numeric.h"
27 ///////////////////////////////////////////////////////////////////////////////
29 void NoiseIndev::init(NoiseIndevParams *np, int seed, int sx, int sy, int sz) {
30 Noise::init((NoiseParams*)np, seed, sx, sy, sz);
34 NoiseIndev::NoiseIndev(NoiseIndevParams *np, int seed, int sx, int sy) : Noise(np, seed, sx, sy) {
35 init(np, seed, sx, sy, 1);
38 NoiseIndev::NoiseIndev(NoiseIndevParams *np, int seed, int sx, int sy, int sz) : Noise(np, seed, sx, sy, sz) {
39 init(np, seed, sx, sy, sz);
42 float farscale(float scale, float z) {
43 return ( 1 + ( 1 - (MAP_GENERATION_LIMIT * 1 - (fabs(z)) ) / (MAP_GENERATION_LIMIT * 1) ) * (scale - 1) );
46 float farscale(float scale, float x, float z) {
47 return ( 1 + ( 1 - (MAP_GENERATION_LIMIT * 2 - (fabs(x) + fabs(z)) ) / (MAP_GENERATION_LIMIT * 2) ) * (scale - 1) );
50 float farscale(float scale, float x, float y, float z) {
51 return ( 1 + ( 1 - (MAP_GENERATION_LIMIT * 3 - (fabs(x) + fabs(y) + fabs(z)) ) / (MAP_GENERATION_LIMIT * 3) ) * (scale - 1) );
54 void NoiseIndev::transformNoiseMapFarScale(float xx, float yy, float zz) {
56 for (int z = 0; z != sz; z++) {
57 for (int y = 0; y != sy; y++) {
58 for (int x = 0; x != sx; x++) {
59 result[i] = result[i] * npindev->scale * farscale(npindev->farscale, xx, yy, zz) + npindev->offset;
66 MapgenIndev::MapgenIndev(int mapgenid, MapgenParams *params, EmergeManager *emerge)
67 : MapgenV6(mapgenid, params, emerge)
69 MapgenIndevParams *sp = (MapgenIndevParams *)params->sparams;
71 float_islands = sp->float_islands;
73 noiseindev_terrain_base = new NoiseIndev(&sp->npindev_terrain_base, seed, csize.X, csize.Z);
74 noiseindev_terrain_higher = new NoiseIndev(&sp->npindev_terrain_higher, seed, csize.X, csize.Z);
75 noiseindev_steepness = new NoiseIndev(&sp->npindev_steepness, seed, csize.X, csize.Z);
76 noiseindev_mud = new NoiseIndev(&sp->npindev_mud, seed, csize.X, csize.Z);
77 noiseindev_float_islands1 = new NoiseIndev(&sp->npindev_float_islands1, seed, csize.X, csize.Y, csize.Z);
78 noiseindev_float_islands2 = new NoiseIndev(&sp->npindev_float_islands2, seed, csize.X, csize.Y, csize.Z);
79 noiseindev_float_islands3 = new NoiseIndev(&sp->npindev_float_islands3, seed, csize.X, csize.Z);
80 noiseindev_biome = new NoiseIndev(&sp->npindev_biome, seed, csize.X, csize.Z);
83 MapgenIndev::~MapgenIndev() {
84 delete noiseindev_terrain_base;
85 delete noiseindev_terrain_higher;
86 delete noiseindev_steepness;
87 delete noiseindev_mud;
88 delete noiseindev_float_islands1;
89 delete noiseindev_float_islands2;
90 delete noiseindev_float_islands3;
91 delete noiseindev_biome;
94 void MapgenIndev::calculateNoise() {
98 // Need to adjust for the original implementation's +.5 offset...
99 if (!(flags & MG_FLAT)) {
100 noiseindev_terrain_base->perlinMap2D(
101 x + 0.5 * noiseindev_terrain_base->npindev->spread.X * farscale(noiseindev_terrain_base->npindev->farspread, x, z),
102 z + 0.5 * noiseindev_terrain_base->npindev->spread.Z * farscale(noiseindev_terrain_base->npindev->farspread, x, z));
103 noiseindev_terrain_base->transformNoiseMapFarScale(x, y, z);
105 noiseindev_terrain_higher->perlinMap2D(
106 x + 0.5 * noiseindev_terrain_higher->npindev->spread.X * farscale(noiseindev_terrain_higher->npindev->farspread, x, z),
107 z + 0.5 * noiseindev_terrain_higher->npindev->spread.Z * farscale(noiseindev_terrain_higher->npindev->farspread, x, z));
108 noiseindev_terrain_higher->transformNoiseMapFarScale(x, y, z);
110 noiseindev_steepness->perlinMap2D(
111 x + 0.5 * noiseindev_steepness->npindev->spread.X * farscale(noiseindev_steepness->npindev->farspread, x, z),
112 z + 0.5 * noiseindev_steepness->npindev->spread.Z * farscale(noiseindev_steepness->npindev->farspread, x, z));
113 noiseindev_steepness->transformNoiseMapFarScale(x, y, z);
115 noise_height_select->perlinMap2D(
116 x + 0.5 * noise_height_select->np->spread.X,
117 z + 0.5 * noise_height_select->np->spread.Z);
119 noiseindev_float_islands1->perlinMap3D(
120 x + 0.33 * noiseindev_float_islands1->npindev->spread.X * farscale(noiseindev_float_islands1->npindev->farspread, x, y, z),
121 y + 0.33 * noiseindev_float_islands1->npindev->spread.Y * farscale(noiseindev_float_islands1->npindev->farspread, x, y, z),
122 z + 0.33 * noiseindev_float_islands1->npindev->spread.Z * farscale(noiseindev_float_islands1->npindev->farspread, x, y, z)
124 noiseindev_float_islands1->transformNoiseMapFarScale(x, y, z);
126 noiseindev_float_islands2->perlinMap3D(
127 x + 0.33 * noiseindev_float_islands2->npindev->spread.X * farscale(noiseindev_float_islands2->npindev->farspread, x, y, z),
128 y + 0.33 * noiseindev_float_islands2->npindev->spread.Y * farscale(noiseindev_float_islands2->npindev->farspread, x, y, z),
129 z + 0.33 * noiseindev_float_islands2->npindev->spread.Z * farscale(noiseindev_float_islands2->npindev->farspread, x, y, z)
131 noiseindev_float_islands2->transformNoiseMapFarScale(x, y, z);
133 noiseindev_float_islands3->perlinMap2D(
134 x + 0.5 * noiseindev_float_islands3->npindev->spread.X * farscale(noiseindev_float_islands3->npindev->farspread, x, z),
135 z + 0.5 * noiseindev_float_islands3->npindev->spread.Z * farscale(noiseindev_float_islands3->npindev->farspread, x, z));
136 noiseindev_float_islands3->transformNoiseMapFarScale(x, y, z);
138 noiseindev_mud->perlinMap2D(
139 x + 0.5 * noiseindev_mud->npindev->spread.X * farscale(noiseindev_mud->npindev->farspread, x, y, z),
140 z + 0.5 * noiseindev_mud->npindev->spread.Z * farscale(noiseindev_mud->npindev->farspread, x, y, z));
141 noiseindev_mud->transformNoiseMapFarScale(x, y, z);
143 noise_beach->perlinMap2D(
144 x + 0.2 * noise_beach->np->spread.X,
145 z + 0.7 * noise_beach->np->spread.Z);
147 noise_biome->perlinMap2D(
148 x + 0.6 * noiseindev_biome->npindev->spread.X * farscale(noiseindev_biome->npindev->farspread, x, z),
149 z + 0.2 * noiseindev_biome->npindev->spread.Z * farscale(noiseindev_biome->npindev->farspread, x, z));
152 MapgenIndevParams::MapgenIndevParams() {
154 npindev_terrain_base = NoiseIndevParams(-4, 20, v3f(250, 250, 250), 82341, 5, 0.6, 10, 10);
155 npindev_terrain_higher = NoiseIndevParams(20, 16, v3f(500, 500, 500), 85039, 5, 0.6, 10, 10);
156 npindev_steepness = NoiseIndevParams(0.85, 0.5, v3f(125, 125, 125), -932, 5, 0.7, 2, 10);
157 npindev_mud = NoiseIndevParams(4, 2, v3f(200, 200, 200), 91013, 3, 0.55, 1, 1);
158 npindev_biome = NoiseIndevParams(0, 1, v3f(250, 250, 250), 9130, 3, 0.50, 1, 10);
159 npindev_float_islands1 = NoiseIndevParams(0, 1, v3f(256, 256, 256), 3683, 6, 0.6, 1, 1.5);
160 npindev_float_islands2 = NoiseIndevParams(0, 1, v3f(8, 8, 8 ), 9292, 2, 0.5, 1, 1.5);
161 npindev_float_islands3 = NoiseIndevParams(0, 1, v3f(256, 256, 256), 6412, 2, 0.5, 1, 0.5);
164 void MapgenIndevParams::readParams(Settings *settings) {
165 MapgenV6Params::readParams(settings);
167 settings->getS16NoEx("mgindev_float_islands", float_islands);
169 settings->getNoiseIndevParams("mgindev_np_terrain_base", npindev_terrain_base);
170 settings->getNoiseIndevParams("mgindev_np_terrain_higher", npindev_terrain_higher);
171 settings->getNoiseIndevParams("mgindev_np_steepness", npindev_steepness);
172 settings->getNoiseIndevParams("mgindev_np_mud", npindev_mud);
173 settings->getNoiseIndevParams("mgindev_np_biome", npindev_biome);
174 settings->getNoiseIndevParams("mgindev_np_float_islands1", npindev_float_islands1);
175 settings->getNoiseIndevParams("mgindev_np_float_islands2", npindev_float_islands2);
176 settings->getNoiseIndevParams("mgindev_np_float_islands3", npindev_float_islands3);
179 void MapgenIndevParams::writeParams(Settings *settings) {
180 MapgenV6Params::writeParams(settings);
182 settings->setS16("mgindev_float_islands", float_islands);
184 settings->setNoiseIndevParams("mgindev_np_terrain_base", npindev_terrain_base);
185 settings->setNoiseIndevParams("mgindev_np_terrain_higher", npindev_terrain_higher);
186 settings->setNoiseIndevParams("mgindev_np_steepness", npindev_steepness);
187 settings->setNoiseIndevParams("mgindev_np_mud", npindev_mud);
188 settings->setNoiseIndevParams("mgindev_np_biome", npindev_biome);
189 settings->setNoiseIndevParams("mgindev_np_float_islands1", npindev_float_islands1);
190 settings->setNoiseIndevParams("mgindev_np_float_islands2", npindev_float_islands2);
191 settings->setNoiseIndevParams("mgindev_np_float_islands3", npindev_float_islands3);
195 float MapgenIndev::baseTerrainLevelFromNoise(v2s16 p) {
199 float terrain_base = NoisePerlin2DPosOffset(noiseindev_terrain_base->npindev,
200 p.X, 0.5, p.Y, 0.5, seed);
201 float terrain_higher = NoisePerlin2DPosOffset(noiseindev_terrain_higher->npindev,
202 p.X, 0.5, p.Y, 0.5, seed);
203 float steepness = NoisePerlin2DPosOffset(noiseindev_steepness->npindev,
204 p.X, 0.5, p.Y, 0.5, seed);
205 float height_select = NoisePerlin2DNoTxfmPosOffset(noise_height_select->np,
206 p.X, 0.5, p.Y, 0.5, seed);
208 return baseTerrainLevel(terrain_base, terrain_higher,
209 steepness, height_select);
212 float MapgenIndev::baseTerrainLevelFromMap(int index) {
216 float terrain_base = noiseindev_terrain_base->result[index];
217 float terrain_higher = noiseindev_terrain_higher->result[index];
218 float steepness = noiseindev_steepness->result[index];
219 float height_select = noise_height_select->result[index];
221 return baseTerrainLevel(terrain_base, terrain_higher,
222 steepness, height_select);
225 float MapgenIndev::getMudAmount(int index) {
227 return AVERAGE_MUD_AMOUNT;
229 /*return ((float)AVERAGE_MUD_AMOUNT + 2.0 * noise2d_perlin(
230 0.5+(float)p.X/200, 0.5+(float)p.Y/200,
231 seed+91013, 3, 0.55));*/
233 return noiseindev_mud->result[index];
236 void MapgenIndev::generateCaves(int max_stone_y) {
237 float cave_amount = NoisePerlin2D(np_cave, node_min.X, node_min.Y, seed);
238 int volume_nodes = (node_max.X - node_min.X + 1) *
239 (node_max.Y - node_min.Y + 1) * MAP_BLOCKSIZE;
240 cave_amount = MYMAX(0.0, cave_amount);
241 u32 caves_count = cave_amount * volume_nodes / 50000;
242 u32 bruises_count = 1;
243 PseudoRandom ps(blockseed + 21343);
244 PseudoRandom ps2(blockseed + 1032);
246 if (ps.range(1, 6) == 1)
247 bruises_count = ps.range(0, ps.range(0, 2));
249 if (getBiome(v2s16(node_min.X, node_min.Z)) == BT_DESERT) {
254 for (u32 i = 0; i < caves_count + bruises_count; i++) {
255 bool large_cave = (i >= caves_count);
256 CaveIndev cave(this, &ps, &ps2, node_min, large_cave);
258 cave.makeCave(node_min, node_max, max_stone_y);
262 CaveIndev::CaveIndev(MapgenIndev *mg, PseudoRandom *ps, PseudoRandom *ps2,
263 v3s16 node_min, bool is_large_cave) {
266 this->ndef = mg->ndef;
267 this->water_level = mg->water_level;
268 this->large_cave = is_large_cave;
271 this->c_water_source = mg->c_water_source;
272 this->c_lava_source = mg->c_lava_source;
274 min_tunnel_diameter = 2;
275 max_tunnel_diameter = ps->range(2,6);
276 dswitchint = ps->range(1,14);
277 flooded = large_cave && ps->range(0,4);
279 part_max_length_rs = ps->range(2,4);
280 float scale = farscale(0.2, node_min.X, node_min.Y, node_min.Z);
281 if (node_min.Y < -100 && !ps->range(0, scale * 30)) { //huge
282 flooded = !ps->range(0, 3);
283 tunnel_routepoints = ps->range(5, 30);
284 min_tunnel_diameter = 30;
285 max_tunnel_diameter = ps->range(40, ps->range(80, 150));
287 tunnel_routepoints = ps->range(5, ps->range(15,30));
288 min_tunnel_diameter = 5;
289 max_tunnel_diameter = ps->range(7, ps->range(8,24));
292 part_max_length_rs = ps->range(2,9);
293 tunnel_routepoints = ps->range(10, ps->range(15,30));
295 large_cave_is_flat = (ps->range(0,1) == 0);
299 // version with one perlin3d. use with good params like
300 settings->setDefault("mgindev_np_float_islands1", "-9.5, 10, (20, 50, 50 ), 45123, 5, 0.6, 1.5, 5");
301 void MapgenIndev::generateFloatIslands(int min_y) {
302 if (node_min.Y < min_y) return;
303 v3s16 p0(node_min.X, node_min.Y, node_min.Z);
304 MapNode n1(c_stone), n2(c_desert_stone);
305 int xl = node_max.X - node_min.X;
306 int yl = node_max.Y - node_min.Y;
307 int zl = node_max.Z - node_min.Z;
309 for (int x1 = 0; x1 <= xl; x1++)
311 //int x = x1 + node_min.Y;
312 for (int z1 = 0; z1 <= zl; z1++)
314 //int z = z1 + node_min.Z;
315 for (int y1 = 0; y1 <= yl; y1++, index++)
317 //int y = y1 + node_min.Y;
318 float noise = noiseindev_float_islands1->result[index];
319 //dstream << " y1="<<y1<< " x1="<<x1<<" z1="<<z1<< " noise="<<noise << std::endl;
321 v3s16 p = p0 + v3s16(x1, y1, z1);
322 u32 i = vm->m_area.index(p);
323 if (!vm->m_area.contains(i))
326 if (vm->m_data[i].getContent() != CONTENT_AIR)
328 vm->m_data[i] = noise > 1 ? n1 : n2;
336 void MapgenIndev::generateFloatIslands(int min_y) {
337 if (node_min.Y < min_y) return;
338 PseudoRandom pr(blockseed + 985);
339 // originally from http://forum.minetest.net/viewtopic.php?id=4776
340 float RAR = 0.8 * farscale(0.4, node_min.Y); // 0.4; // Island rarity in chunk layer. -0.4 = thick layer with holes, 0 = 50%, 0.4 = desert rarity, 0.7 = very rare.
341 float AMPY = 24; // 24; // Amplitude of island centre y variation.
342 float TGRAD = 24; // 24; // Noise gradient to create top surface. Tallness of island top.
343 float BGRAD = 24; // 24; // Noise gradient to create bottom surface. Tallness of island bottom.
345 v3s16 p0(node_min.X, node_min.Y, node_min.Z);
348 float xl = node_max.X - node_min.X;
349 float yl = node_max.Y - node_min.Y;
350 float zl = node_max.Z - node_min.Z;
351 u32 zstride = xl + 1;
352 float midy = node_min.Y + yl * 0.5;
354 for (int z1 = 0; z1 <= zl; ++z1)
355 for (int y1 = 0; y1 <= yl; ++y1)
356 for (int x1 = 0; x1 <= xl; ++x1, ++index) {
357 int y = y1 + node_min.Y;
358 u32 index2d = z1 * zstride + x1;
359 float noise3 = noiseindev_float_islands3->result[index2d];
360 float pmidy = midy + noise3 / 1.5 * AMPY;
361 float noise1 = noiseindev_float_islands1->result[index];
362 float offset = y > pmidy ? (y - pmidy) / TGRAD : (pmidy - y) / BGRAD;
363 float noise1off = noise1 - offset - RAR;
364 if (noise1off > 0 && noise1off < 0.7) {
365 float noise2 = noiseindev_float_islands2->result[index];
366 if (noise2 - noise1off > -0.7) {
367 v3s16 p = p0 + v3s16(x1, y1, z1);
368 u32 i = vm->m_area.index(p);
369 if (!vm->m_area.contains(i))
372 if (vm->m_data[i].getContent() != CONTENT_AIR)
380 void MapgenIndev::generateExperimental() {
382 generateFloatIslands(float_islands);