示例#1
0
        public void LoadChunk(IChunk runtimeChunk)
        {
            using (var file = new FileStream(Path.Combine(this.m_Path, this.GetName(runtimeChunk)), FileMode.Open))
            {
                var serializer = new TychaiaDataSerializer();
                var chunk = new Chunk();
                serializer.Deserialize(file, chunk, typeof(Chunk));

                runtimeChunk.Cells = chunk.Cells;

                runtimeChunk.GeneratedIndices = chunk.Indexes;

                if (chunk.Vertexes == null)
                {
                    runtimeChunk.GeneratedVertexes = new VertexPositionTexture[0];
                }
                else
                {
                    runtimeChunk.GeneratedVertexes = new VertexPositionTexture[chunk.Vertexes.Length];
                    for (var i = 0; i < chunk.Vertexes.Length; i++)
                    {
                        runtimeChunk.GeneratedVertexes[i] =
                            new VertexPositionTexture(
                                new Vector3(chunk.Vertexes[i].X, chunk.Vertexes[i].Y, chunk.Vertexes[i].Z),
                                new Vector2(chunk.Vertexes[i].U, chunk.Vertexes[i].V));
                    }
                }

                runtimeChunk.Generated = true;
            }
        }
        public byte[] Compress(Chunk chunk)
        {
            using (var memory = new MemoryStream())
            {
                using (var writer = new BinaryWriter(memory))
                {
                    writer.Write(chunk.X);
                    writer.Write(chunk.Y);
                    writer.Write(chunk.Z);

                    if (chunk.Cells == null)
                    {
                        throw new InvalidOperationException();
                    }

                    for (var x = 0; x < this.m_ChunkSizePolicy.ChunkCellWidth; x++)
                    {
                        for (var z = 0; z < this.m_ChunkSizePolicy.ChunkCellDepth; z++)
                        {
                            var i = x + (z * this.m_ChunkSizePolicy.ChunkCellWidth * this.m_ChunkSizePolicy.ChunkCellHeight);
                            writer.Write(chunk.Cells[i].HeightMap);
                        }
                    }

                    for (var i = 0; i < this.m_ChunkCellArrayLength; i++)
                    {
                        var edgePosition = EdgePoint.Decompress(chunk.Cells[i].EdgePoint);

                        if (!edgePosition.RenderAbove && !edgePosition.RenderBelow && !edgePosition.RenderEast
                            && !edgePosition.RenderNorth && !edgePosition.RenderSouth && !edgePosition.RenderWest)
                        {
                            writer.Write(false);
                        }
                        else
                        {
                            writer.Write(true);
                            writer.Write(chunk.Cells[i].EdgePoint);
                            if (chunk.Cells[i].BlockAssetName == null)
                            {
                                writer.Write(byte.MaxValue);
                            }
                            else
                            {
                                writer.Write((byte)this.m_BlocksByName[chunk.Cells[i].BlockAssetName]);
                            }
                        }
                    }

                    var uncompressed = memory.Length;

                    using (var compressed = new MemoryStream())
                    {
                        memory.Seek(0, SeekOrigin.Begin);

                        var start = DateTime.Now;
                        LzmaHelper.Compress(memory, compressed);

                        Console.WriteLine(
                            "uncompressed " + uncompressed + " / compressed " + compressed.Position + " in "
                            + (DateTime.Now - start).TotalMilliseconds + "ms");

                        var length = compressed.Position;
                        var bytes = new byte[length];
                        compressed.Seek(0, SeekOrigin.Begin);
                        compressed.Read(bytes, 0, (int)length);
                        return bytes;
                    }
                }
            }
        }
        public Chunk Decompress(byte[] bytes)
        {
            var chunk = new Chunk { Cells = new Cell[this.m_ChunkCellArrayLength] };

            using (var compressed = new MemoryStream(bytes))
            {
                using (var memory = new MemoryStream())
                {
                    LzmaHelper.Decompress(compressed, memory);
                    memory.Seek(0, SeekOrigin.Begin);

                    using (var reader = new BinaryReader(memory))
                    {
                        chunk.X = reader.ReadInt64();
                        chunk.Y = reader.ReadInt64();
                        chunk.Z = reader.ReadInt64();

                        var lookup =
                            new int[this.m_ChunkSizePolicy.ChunkCellWidth, this.m_ChunkSizePolicy.ChunkCellDepth];
                        for (var x = 0; x < this.m_ChunkSizePolicy.ChunkCellWidth; x++)
                        {
                            for (var z = 0; z < this.m_ChunkSizePolicy.ChunkCellDepth; z++)
                            {
                                lookup[x, z] = reader.ReadInt32();
                            }
                        }

                        for (var i = 0; i < this.m_ChunkCellArrayLength; i++)
                        {
                            var x = i % this.m_ChunkSizePolicy.ChunkCellWidth;
                            var y = ((i % (this.m_ChunkSizePolicy.ChunkCellWidth * this.m_ChunkSizePolicy.ChunkCellHeight)) - x) / this.m_ChunkSizePolicy.ChunkCellWidth;
                            var z = i / (this.m_ChunkSizePolicy.ChunkCellWidth * this.m_ChunkSizePolicy.ChunkCellHeight);

                            var render = reader.ReadBoolean();

                            if (!render)
                            {
                                chunk.Cells[i].EdgePoint = 0;
                                chunk.Cells[i].BlockAssetName = null;
                                chunk.Cells[i].HeightMap = lookup[x, z];
                            }
                            else
                            {
                                var edgePoint = reader.ReadInt16();
                                var blockID = reader.ReadByte();

                                chunk.Cells[i].EdgePoint = edgePoint;
                                chunk.Cells[i].BlockAssetName = blockID == byte.MaxValue ? null : this.m_BlocksByID[blockID];
                                chunk.Cells[i].HeightMap = lookup[x, z];
                            }
                        }
                    }
                }
            }

            return chunk;
        }
示例#4
0
        public void SaveChunk(IChunk runtimeChunk)
        {
            using (var file = new FileStream(Path.Combine(this.m_Path, this.GetName(runtimeChunk)), FileMode.Create))
            {
                var size = this.m_ChunkSizePolicy.ChunkCellWidth * this.m_ChunkSizePolicy.ChunkCellHeight
                           * this.m_ChunkSizePolicy.ChunkCellDepth;
                var chunk = new Chunk
                {
                    X = runtimeChunk.X,
                    Y = runtimeChunk.Y,
                    Z = runtimeChunk.Z,
                    Cells = new Cell[size],
                    Indexes = runtimeChunk.GeneratedIndices,
                    Vertexes = new Vertex[runtimeChunk.GeneratedVertexes.Length]
                };

                chunk.Cells = runtimeChunk.Cells;

                for (var i = 0; i < runtimeChunk.GeneratedVertexes.Length; i++)
                {
                    chunk.Vertexes[i] = new Vertex
                    {
                        X = runtimeChunk.GeneratedVertexes[i].Position.X,
                        Y = runtimeChunk.GeneratedVertexes[i].Position.Y,
                        Z = runtimeChunk.GeneratedVertexes[i].Position.Z,
                        U = runtimeChunk.GeneratedVertexes[i].TextureCoordinate.X,
                        V = runtimeChunk.GeneratedVertexes[i].TextureCoordinate.Y,
                    };
                }

                var serializer = new TychaiaDataSerializer();
                serializer.Serialize(file, chunk);
            }
        }
示例#5
0
        public void SaveChunk(long chunkX, long chunkY, long chunkZ, Cell[,,] data)
        {
            using (
                var file = new FileStream(
                    Path.Combine(this.m_Path, this.GetName(chunkX, chunkY, chunkZ)),
                    FileMode.Create))
            {
                var size = this.m_ChunkSizePolicy.ChunkCellWidth * this.m_ChunkSizePolicy.ChunkCellHeight
                           * this.m_ChunkSizePolicy.ChunkCellDepth;
                var chunk = new Chunk
                {
                    X = chunkX,
                    Y = chunkY,
                    Z = chunkZ,
                    Cells = new Cell[size],
                    Indexes = new int[0],
                    Vertexes = new Vertex[0]
                };

                for (var x = 0; x < this.m_ChunkSizePolicy.ChunkCellWidth; x++)
                {
                    for (var y = 0; y < this.m_ChunkSizePolicy.ChunkCellHeight; y++)
                    {
                        for (var z = 0; z < this.m_ChunkSizePolicy.ChunkCellDepth; z++)
                        {
                            var idx = x;
                            idx += y * this.m_ChunkSizePolicy.ChunkCellWidth;
                            idx += z * this.m_ChunkSizePolicy.ChunkCellWidth * this.m_ChunkSizePolicy.ChunkCellHeight;
                            chunk.Cells[idx] = data[x, y, z];
                        }
                    }
                }

                var serializer = new TychaiaDataSerializer();
                serializer.Serialize(file, chunk);
            }
        }