void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) { VoxelArea a(nmin, nmax); bool block_is_underground = (water_level >= nmax.Y); ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG); //TimeTaker t("updateLighting"); // first, send vertical rays of sunshine downward v3s16 em = vm->m_area.getExtent(); for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) { for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) { // see if we can get a light value from the overtop u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z); if (vm->m_data[i].getContent() == CONTENT_IGNORE) { if (block_is_underground) continue; } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) { continue; } vm->m_area.add_y(em, i, -1); for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) { MapNode &n = vm->m_data[i]; if (!ndef->get(n).sunlight_propagates) break; n.param1 = LIGHT_SUN; vm->m_area.add_y(em, i, -1); } } } // now spread the sunlight and light up any sources for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) { for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) { u32 i = vm->m_area.index(a.MinEdge.X, y, z); for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) { MapNode &n = vm->m_data[i]; if (n.getContent() == CONTENT_IGNORE || !ndef->get(n).light_propagates) continue; u8 light_produced = ndef->get(n).light_source & 0x0F; if (light_produced) n.param1 = light_produced; u8 light = n.param1 & 0x0F; if (light) { lightSpread(a, v3s16(x, y, z + 1), light - 1); lightSpread(a, v3s16(x, y + 1, z ), light - 1); lightSpread(a, v3s16(x + 1, y, z ), light - 1); lightSpread(a, v3s16(x, y, z - 1), light - 1); lightSpread(a, v3s16(x, y - 1, z ), light - 1); lightSpread(a, v3s16(x - 1, y, z ), light - 1); } } } } //printf("updateLighting: %dms\n", t.stop()); }
void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) { if (light <= 1 || !a.contains(p)) return; u32 vi = vm->m_area.index(p); MapNode &nn = vm->m_data[vi]; light--; // should probably compare masked, but doesn't seem to make a difference if (light <= nn.param1 || !ndef->get(nn).light_propagates) return; nn.param1 = light; lightSpread(a, p + v3s16(0, 0, 1), light); lightSpread(a, p + v3s16(0, 1, 0), light); lightSpread(a, p + v3s16(1, 0, 0), light); lightSpread(a, p - v3s16(0, 0, 1), light); lightSpread(a, p - v3s16(0, 1, 0), light); lightSpread(a, p - v3s16(1, 0, 0), light); }