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.
27 //#include "serverobject.h"
28 #include "content_sao.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
36 #include "mapgen_v6.h"
38 FlagDesc flagdesc_mapgen[] = {
41 {"dungeons", MG_DUNGEONS},
42 {"v6_jungles", MGV6_JUNGLES},
43 {"v6_biome_blend", MGV6_BIOME_BLEND},
49 ///////////////////////////////////////////////////////////////////////////////
52 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
53 bool isliquid, wasliquid;
54 v3s16 em = vm->m_area.getExtent();
56 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
57 for (s16 x = nmin.X; x <= nmax.X; x++) {
60 u32 i = vm->m_area.index(x, nmax.Y, z);
61 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
62 isliquid = ndef->get(vm->m_data[i]).isLiquid();
64 // there was a change between liquid and nonliquid, add to queue
65 if (isliquid != wasliquid)
66 trans_liquid->push_back(v3s16(x, y, z));
69 vm->m_area.add_y(em, i, -1);
76 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
77 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
78 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
79 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
81 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
82 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
83 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
84 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
85 vm->m_data[i].param1 = light;
91 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
92 if (light <= 1 || !a.contains(p))
95 u32 vi = vm->m_area.index(p);
96 MapNode &nn = vm->m_data[vi];
99 // should probably compare masked, but doesn't seem to make a difference
100 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
105 lightSpread(a, p + v3s16(0, 0, 1), light);
106 lightSpread(a, p + v3s16(0, 1, 0), light);
107 lightSpread(a, p + v3s16(1, 0, 0), light);
108 lightSpread(a, p - v3s16(0, 0, 1), light);
109 lightSpread(a, p - v3s16(0, 1, 0), light);
110 lightSpread(a, p - v3s16(1, 0, 0), light);
114 void Mapgen::updateLighting(v3s16 nmin, v3s16 nmax) {
115 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
116 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
117 bool block_is_underground = (water_level >= nmax.Y);
119 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
120 //TimeTaker t("updateLighting");
122 // first, send vertical rays of sunshine downward
123 v3s16 em = vm->m_area.getExtent();
124 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
125 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
126 // see if we can get a light value from the overtop
127 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
128 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
129 if (block_is_underground)
131 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
134 vm->m_area.add_y(em, i, -1);
136 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
137 MapNode &n = vm->m_data[i];
138 if (!ndef->get(n).sunlight_propagates)
140 n.param1 = LIGHT_SUN;
141 vm->m_area.add_y(em, i, -1);
146 // now spread the sunlight and light up any sources
147 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
148 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
149 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
150 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
151 MapNode &n = vm->m_data[i];
152 if (n.getContent() == CONTENT_IGNORE ||
153 !ndef->get(n).light_propagates)
156 u8 light_produced = ndef->get(n).light_source & 0x0F;
158 n.param1 = light_produced;
160 u8 light = n.param1 & 0x0F;
162 lightSpread(a, v3s16(x, y, z + 1), light);
163 lightSpread(a, v3s16(x, y + 1, z ), light);
164 lightSpread(a, v3s16(x + 1, y, z ), light);
165 lightSpread(a, v3s16(x, y, z - 1), light);
166 lightSpread(a, v3s16(x, y - 1, z ), light);
167 lightSpread(a, v3s16(x - 1, y, z ), light);
173 //printf("updateLighting: %dms\n", t.stop());
177 void Mapgen::updateLightingOld(v3s16 nmin, v3s16 nmax) {
178 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
180 VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
181 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
182 bool block_is_underground = (water_level > nmax.Y);
183 bool sunlight = !block_is_underground;
185 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
187 for (int i = 0; i < 2; i++) {
188 enum LightBank bank = banks[i];
189 std::set<v3s16> light_sources;
190 std::map<v3s16, u8> unlight_from;
192 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
193 light_sources, unlight_from);
194 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
196 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
197 vm->spreadLight(bank, light_sources, ndef);
202 //////////////////////// Mapgen V6 parameter read/write
204 bool MapgenV6Params::readParams(Settings *settings) {
205 freq_desert = settings->getFloat("mgv6_freq_desert");
206 freq_beach = settings->getFloat("mgv6_freq_beach");
208 np_terrain_base = settings->getNoiseParams("mgv6_np_terrain_base");
209 np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
210 np_steepness = settings->getNoiseParams("mgv6_np_steepness");
211 np_height_select = settings->getNoiseParams("mgv6_np_height_select");
212 np_mud = settings->getNoiseParams("mgv6_np_mud");
213 np_beach = settings->getNoiseParams("mgv6_np_beach");
214 np_biome = settings->getNoiseParams("mgv6_np_biome");
215 np_cave = settings->getNoiseParams("mgv6_np_cave");
216 np_humidity = settings->getNoiseParams("mgv6_np_humidity");
217 np_trees = settings->getNoiseParams("mgv6_np_trees");
218 np_apple_trees = settings->getNoiseParams("mgv6_np_apple_trees");
221 np_terrain_base && np_terrain_higher && np_steepness &&
222 np_height_select && np_trees && np_mud &&
223 np_beach && np_biome && np_cave &&
224 np_humidity && np_apple_trees;
229 void MapgenV6Params::writeParams(Settings *settings) {
230 settings->setFloat("mgv6_freq_desert", freq_desert);
231 settings->setFloat("mgv6_freq_beach", freq_beach);
233 settings->setNoiseParams("mgv6_np_terrain_base", np_terrain_base);
234 settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
235 settings->setNoiseParams("mgv6_np_steepness", np_steepness);
236 settings->setNoiseParams("mgv6_np_height_select", np_height_select);
237 settings->setNoiseParams("mgv6_np_mud", np_mud);
238 settings->setNoiseParams("mgv6_np_beach", np_beach);
239 settings->setNoiseParams("mgv6_np_biome", np_biome);
240 settings->setNoiseParams("mgv6_np_cave", np_cave);
241 settings->setNoiseParams("mgv6_np_humidity", np_humidity);
242 settings->setNoiseParams("mgv6_np_trees", np_trees);
243 settings->setNoiseParams("mgv6_np_apple_trees", np_apple_trees);
247 /////////////////////////////////// legacy static functions for farmesh
250 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
251 //just need to return something
257 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
258 double sandnoise = noise2d_perlin(
259 0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
260 seed+59420, 3, 0.50);
262 return (sandnoise > 0.15);
266 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
267 double noise = noise2d_perlin(
268 0.5+(float)p.X/125, 0.5+(float)p.Y/125,
270 double zeroval = -0.39;
274 return 0.04 * (noise-zeroval) / (1.0-zeroval);