+ v3s16 em = vm->m_area.getExtent();
+ u32 index = 0;
+ MgStoneType stone_type = MGSTONE_STONE;
+
+ noise_filler_depth->perlinMap2D(node_min.X, node_min.Z);
+
+ for (s16 z = node_min.Z; z <= node_max.Z; z++)
+ for (s16 x = node_min.X; x <= node_max.X; x++, index++) {
+ Biome *biome = NULL;
+ u16 depth_top = 0;
+ u16 base_filler = 0;
+ u16 depth_water_top = 0;
+ u16 depth_riverbed = 0;
+ u32 vi = vm->m_area.index(x, node_max.Y, z);
+
+ // Check node at base of mapchunk above, either a node of a previously
+ // generated mapchunk or if not, a node of overgenerated base terrain.
+ content_t c_above = vm->m_data[vi + em.X].getContent();
+ bool air_above = c_above == CONTENT_AIR;
+ bool river_water_above = c_above == c_river_water_source;
+ bool water_above = c_above == c_water_source || river_water_above;
+
+ biomemap[index] = BIOME_NONE;
+
+ // If there is air or water above enable top/filler placement, otherwise force
+ // nplaced to stone level by setting a number exceeding any possible filler depth.
+ u16 nplaced = (air_above || water_above) ? 0 : U16_MAX;
+
+ for (s16 y = node_max.Y; y >= node_min.Y; y--) {
+ content_t c = vm->m_data[vi].getContent();
+
+ // Biome is recalculated each time an upper surface is detected while
+ // working down a column. The selected biome then remains in effect for
+ // all nodes below until the next surface and biome recalculation.
+ // Biome is recalculated:
+ // 1. At the surface of stone below air or water.
+ // 2. At the surface of water below air.
+ // 3. When stone or water is detected but biome has not yet been calculated.
+ bool is_stone_surface = (c == c_stone) &&
+ (air_above || water_above || !biome);
+
+ bool is_water_surface =
+ (c == c_water_source || c == c_river_water_source) &&
+ (air_above || !biome);
+
+ if (is_stone_surface || is_water_surface) {
+ biome = biomegen->getBiomeAtIndex(index, y);
+
+ if (biomemap[index] == BIOME_NONE && is_stone_surface)
+ biomemap[index] = biome->index;
+
+ depth_top = biome->depth_top;
+ base_filler = MYMAX(depth_top +
+ biome->depth_filler +
+ noise_filler_depth->result[index], 0.f);
+ depth_water_top = biome->depth_water_top;
+ depth_riverbed = biome->depth_riverbed;
+
+ // Detect stone type for dungeons during every biome calculation.
+ // This is more efficient than detecting per-node and will not
+ // miss any desert stone or sandstone biomes.
+ if (biome->c_stone == c_desert_stone)
+ stone_type = MGSTONE_DESERT_STONE;
+ else if (biome->c_stone == c_sandstone)
+ stone_type = MGSTONE_SANDSTONE;
+ }