OpenMinecraft.InfdevHandler.RegenerateLighting C# (CSharp) Method

RegenerateLighting() public method

Adapted from MineServer map.cpp
public RegenerateLighting ( long x, long z ) : bool
x long
z long
return bool
        public override bool RegenerateLighting(long x, long z)
        {
#if DEBUG
//            printf("generateLight(x=%d, z=%d, chunk=%p)\n", x, z, chunk);
#endif
            Chunk chunk = GetChunk(x, z);
            byte[,,] blocks = chunk.Blocks;
            byte[, ,] skylight = new byte[ChunkX, ChunkY, ChunkZ];//chunk.SkyLight;
            byte[, ,] blocklight = new byte[ChunkX, ChunkY, ChunkZ];//chunk.BlockLight;
            byte[,] heightmap = new byte[ChunkX, ChunkZ]; //chunk.HeightMap;

            int highest_y = 0;

            // Sky light
            int light = 0;
            bool foundheight = false;
            for(int block_x = 0; block_x < ChunkX; block_x++)
            {
                for(int block_z = 0; block_z < ChunkZ; block_z++)
                {
                    light = 15;
                    foundheight = false;

                    for(int block_y = 127; block_y > 0; block_y--)
                    {
                        int absolute_x = (int)x*ChunkX+block_x;
                        int absolute_z = (int)z*ChunkZ+block_z;
                        byte block = blocks[block_x,block_z,block_y];

                        int stopLight = Blocks.Get(block).Stop;
                        int emitLight = Blocks.Get(block).Emit;

                        light -= stopLight;
                        if (light < 0) { light = 0; }

                        // Calculate heightmap while looping this
                        if ((stopLight > 0) && (foundheight == false)) 
                        {
                            heightmap[block_x,block_z] = (byte)((block_y == 127) ? block_y : block_y + 1);
                            foundheight = true;
                        }

                        if (light < 1) 
                        {
                            if (block_y > highest_y)
                            highest_y = block_y;

                            break;
                        }

                        SetSkyLightAt(absolute_x, absolute_z, block_y, (byte)light);
                    }
                }
            }
  
            // Block light
            for (int block_x = 0; block_x < ChunkX; block_x++)
            {
                for (int block_z = 0; block_z < ChunkZ; block_z++)
                {
                    for (int block_y = highest_y; block_y >= 0; block_y--)
                    {
                        int absolute_x = (int)x*ChunkX+block_x;
                        int absolute_z = (int)z*ChunkZ+block_z;
                        byte block = blocks[block_x,block_z,block_y];

                        int stopLight = Blocks.Get(block).Stop;
                        int emitLight = Blocks.Get(block).Emit;

                        // If light emitting block
                        if (emitLight > 0)
                        {
                            //SetBlockLightAt(absolute_x, absolute_z, block_y, (byte)emitLight);
                            AddLightSourceAt(absolute_x, absolute_z, block_y, emitLight);
                        }
                    }
                }
            }
  
            /*
            // Spread light
            for (int block_x = 0; block_x < 16; block_x++)
            {
                for (int block_z = 0; block_z < 16; block_z++)
                {
                    for (int block_y = heightmap[block_x,block_z]; block_y >= 0; block_y--)
                    {
                        int absolute_x = (int)x * 16 + block_x;
                        int absolute_z = (int)z * 16 + block_z;

                        byte skylight_s, blocklight_s;

                        GetLightAt(absolute_x, absolute_z, block_y, out skylight_s, out blocklight_s);

                        if (skylight_s!=0 || blocklight_s!=0)
                            SpreadLight(absolute_x, block_y, absolute_z, skylight_s, blocklight_s);
                    }
                }
            }
            */
            SaveAll();
            return true;
        }