Exemple #1
0
        public ChunkData Load(int x, int y)
        {
            NbtFile nbt;
            if (!OpenChunk(x, y, out nbt))
                return null;

            try {
                var root = nbt.RootTag;
                var level = root["Level"] as NbtCompound;

                var blocks = level["Blocks"] as NbtByteArray;
                //var chunkX = level["xPos"] as NbtInt;
                //var chunkY = level["yPos"] as NbtInt;
                var chunk = new ChunkData(x, y, blocks.Value);
                chunk.BlockLight.Data = (level["BlockLight"] as NbtByteArray).Value;
                chunk.SkyLight.Data = (level["SkyLight"] as NbtByteArray).Value;
                chunk.MetaData.Data = (level["Data"] as NbtByteArray).Value;
                chunk.Heightmap = (level["HeightMap"] as NbtByteArray).Value;

                return chunk;
            } catch (Exception) {
                Console.WriteLine("[NBTChunkSource] Failed to load chunk {0}, {1}", x, y);
                return null;
            } finally {
                nbt.Dispose();
            }
        }
        public ChunkData GenerateNewChunk(int x, int y)
        {
            var flatChunk = new ChunkData(x, y, new byte[16 * 16 * 128]);
            for (int tx = 0; tx < 16; tx++) {
                for (int tz = 0; tz < 16; tz++) {

                    for (int ty = 0; ty < 5; ty++) {
                        flatChunk.SetBlock(tx, ty, tz, (int)BlockType.Grass);
                    }
                    for (int ty = 5; ty < 128; ty++) {
                        flatChunk.SkyLight.SetValue(tx, ty, tz, 15);
                    }
                }
            }

            lighting.RecalculateLighting(flatChunk);

            return flatChunk;
        }
Exemple #3
0
        public ChunkData Load(int x, int y)
        {
            var rand = new Random(seed + x + y * 1024);
            var chunk = new ChunkData(x, y, new byte[16 * 16 * 128]);
            for (int tx = 0; tx < 16; tx++) {
                for (int tz = 0; tz < 16; tz++) {
                    for (int ty = 0; ty < 1; ty++) {
                        chunk.SetBlock(tx, ty, tz, 1);
                    }
                    for (int ty = 1; ty < 3; ty++) {
                        if (rand.Next(10) <= 7) {
                            chunk.SetBlock(tx, ty, tz, 10);
                        } else {
                            chunk.SetBlock(tx, ty, tz, 1);
                        }
                    }

                    if (rand.Next(20) < 1) {
                        for (int ty = 0; ty < 128; ty++) {
                            chunk.SetBlock(tx, ty, tz, 8);
                        }
                    } else {
                        for (int ty = 64; ty < 100; ty++) {
                            chunk.SetBlock(tx, ty, tz, 8);
                        }
                    }

                    for (int ty = 100; ty < 101; ty++) {
                        if (rand.Next(10) <= 7)
                            chunk.SetBlock(tx, ty, tz, 2);
                    }

                    for (int ty = 0; ty < 128; ty++) {
                        var t = chunk.GetBlock(tx, ty, tz);
                        if (t == 0 || t == 10 || t == 8)
                            chunk.SkyLight.SetValue(tx, ty, tz, 15);
                        if (t == 10)
                            chunk.BlockLight.SetValue(tx, ty, tz, 15);
                    }
                }
            }
            return chunk;
        }
Exemple #4
0
        public void Save(ChunkData chunk)
        {
            var nbt = new NbtFile {
                RootTag = new NbtCompound("", new NbtTag[] {
                    new NbtCompound("Level", new NbtTag[] {
                        new NbtByteArray("Blocks", chunk.Blocks),
                        new NbtByteArray("BlockLight", chunk.BlockLight.Data),
                        new NbtByteArray("SkyLight", chunk.SkyLight.Data),
                        new NbtByteArray("Data", chunk.MetaData.Data),
                        new NbtByteArray("HeightMap", chunk.Heightmap),

                        new NbtInt("xPos", chunk.X),
                        new NbtInt("yPos", chunk.Y),
                    })
                })
            };

            var path = GetChunkPath(chunk.X, chunk.Y);
            Directory.CreateDirectory(Path.GetDirectoryName(path));
            nbt.SaveFile(path, true);
        }
Exemple #5
0
        public ChunkData Get(int x, int y)
        {
            if (lastChunk != null && lastChunkX == x && lastChunkY == y)
                return lastChunk;

            ulong hashKey = BuildKey(x, y);
            ChunkData chunk = cache[hashKey] as ChunkData;
            if (chunk == null) {
                Console.WriteLine("[ChunkCache] Loading {0}, {1}", x, y);
                chunk = source.Load(x, y);
                // Failed to load chunk, generate a new one
                if (chunk == null)
                    chunk = generator.GenerateNewChunk(x, y);
                cache[hashKey] = chunk;
            }

            lastChunkX = x;
            lastChunkY = y;
            lastChunk = chunk;

            return chunk;
        }
Exemple #6
0
        public UpdateFullChunkPacket(int chunkX, short chunkY, int chunkZ, int width, int height, int depth, ChunkData data)
        {
            ChunkX = chunkX;
            ChunkY = chunkY;
            ChunkZ = chunkZ;
            Width = width;
            Height = height;
            Depth = depth;

            Data = new byte[width * height * depth * 5 / 2];
            data.Write(Data, 0, 0, 0, width, height, depth, 0); // TODO
        }
Exemple #7
0
        protected void DoSkyLight(ChunkData chunk, int x, int ystart, int z)
        {
            var tileX = chunk.X * 16 + x;
            var tileZ = chunk.Y * 16 + z;

            var height = chunk.GetHeight(x, z);

            // From where should we start calculating
            int newHeight = Math.Max(height, ystart);

            // We need to cast light through transparent blocks
            while (newHeight > 0 && Block.LightAbsorbs[chunk.GetBlock(x, newHeight - 1, z)] == 0)
                newHeight--;

            // Nothing changed, we don't need to calculate
            if (newHeight == height)
                return;

            chunk.SetHeight(x, z, newHeight);

            /* The new height is lower than the last, that means all the tiles
             * above are fully lit */
            if (newHeight < height) {
                for (int y = newHeight; y < height; y++) {
                    chunk.SkyLight.SetValue(x, y, z, 15);
                }
            } else if (newHeight > height) {
                ScheduleUpdate(LightType.Sky, new Box(tileX, height, tileZ, tileX, newHeight, tileZ));
                for (int y = height; y < newHeight; y++) {
                    chunk.SkyLight.SetValue(x, y, z, 0);
                }
            }
        }
Exemple #8
0
 public void RecalculateLighting(ChunkData chunk)
 {
     for (int x = 0; x < 16; x++) {
         for (int z = 0; z < 16; z++) {
             chunk.SetHeight(x, z, 0);
             DoSkyLight(chunk, x, 127, z);
         }
     }
 }
Exemple #9
0
 public void Save(ChunkData chunk)
 {
 }