void GetStats(System.Collections.Generic.List <float> input, out float mean, out float median, out float min, out float max)
 {
     mean   = input.Average();
     min    = input.Min();
     max    = input.Max();
     median = input.OrderBy(sample => sample).ToList().ElementAt(input.Count / 2);
 }
Beispiel #2
0
        public override void Write()
        {
            MapChunkData chunkData = GetMapChunkData(Chunk);

            int len;

            byte[] chunkCompressed = CompressChunkData(chunkData.Data, chunkData.Data.Length, out len);


            SetCapacity(18 + len);
            Writer.Write(Coords.ChunkX);
            Writer.Write(Coords.ChunkZ);
            Writer.Write(false);     // Ground Up Continous
            Writer.Write((ushort)chunkData.PrimaryBitMask);
            Writer.Write((ushort)0); // Add BitMask
            Writer.Write(len);
            Writer.Write(chunkCompressed, 0, len);

#if PROFILE_MAPCHUNK
            start = DateTime.Now;
            comp  = new byte[DataDimension];

            // Original Java compression version
            //java.util.zip.Deflater deflater = new java.util.zip.Deflater(-1);
            //try
            //{
            //    deflater.setInput(data);
            //    deflater.finish();

            //    len = deflater.deflate(comp);
            //}
            //finally
            //{
            //    deflater.end();
            //}
            end = DateTime.Now;
            TimeSpan jTime   = end - start;
            int      jLength = len;

            writeDiffsLength.Add(deflateLength - jLength);
            writeDiffsTime.Add(deflateTime.TotalMilliseconds - jTime.TotalMilliseconds);

            Console.WriteLine("Avg {0}, {1}", writeDiffsLength.Average(), writeDiffsTime.Average());
#endif
        }
Beispiel #3
0
        public override void Write()
        {
            int dataDim = (Chunk.SectionsNum * Section.BYTESIZE) + 256; // (Number of sections * (Section dimension + Add array) + Biome array

            byte[] data = new byte[dataDim];

            /*byte[] types = new byte[0];
             * byte[] metadata = new byte[0];
             * byte[] blockLight = new byte[0];
             * byte[] skyLight = new byte[0];
             *
             * byte[] tempBlockLight = new byte[2048];
             * byte[] tempSkyLight = new byte[2048];
             *
             * int primaryBitMask = 0;
             * ushort mask = 1;
             *
             * for(int i = 0; i < 16; ++i)
             * {
             *  Section currentSection = Chunk.Sections[i];
             *  if(currentSection != null && currentSection.NonAirBlocks > 0)
             *  {
             *      types = currentSection.Types.Concat(types).ToArray();
             *      metadata = currentSection.Data.Data.Concat(metadata).ToArray();
             *
             *      Buffer.BlockCopy(Chunk.Light.Data, i * Section.HALFSIZE, tempBlockLight, 0, Section.HALFSIZE);
             *      Buffer.BlockCopy(Chunk.SkyLight.Data, i * Section.HALFSIZE, tempSkyLight, 0, Section.HALFSIZE);
             *
             *      blockLight = tempBlockLight.Concat(blockLight).ToArray();
             *      skyLight = tempSkyLight.Concat(skyLight).ToArray();
             *
             *      primaryBitMask |= mask;
             *  }
             *
             *
             *  mask <<= 1;
             * }
             *
             * byte[] data = types.Concat(metadata).Concat(blockLight).Concat(skyLight).ToArray();*/

            int halfSize       = Chunk.SectionsNum * Section.HALFSIZE;
            int offsetData     = Chunk.SectionsNum * Section.SIZE;
            int offsetLight    = offsetData + halfSize;
            int offsetSkyLight = offsetLight + halfSize;

            int    primaryBitMask = 0;
            ushort mask           = 1;
            int    sectionIndex   = 0;

            for (int i = 0; i < 16; ++i)
            {
                Section currentSection = Chunk.Sections[i];

                //int typeIndex = i*Section.SIZE;

                if (currentSection != null && currentSection.NonAirBlocks > 0)
                {
                    Buffer.BlockCopy(currentSection.Types, 0, data, sectionIndex * Section.SIZE, Section.SIZE);
                    Buffer.BlockCopy(currentSection.Data.Data, 0, data, offsetData + (sectionIndex * Section.HALFSIZE),
                                     Section.HALFSIZE);

                    Buffer.BlockCopy(Chunk.Light.Data, i * Section.HALFSIZE, data, offsetLight + (sectionIndex * Section.HALFSIZE),
                                     Section.HALFSIZE);
                    Buffer.BlockCopy(Chunk.SkyLight.Data, i * Section.HALFSIZE, data, offsetSkyLight + (sectionIndex * Section.HALFSIZE),
                                     Section.HALFSIZE);


                    primaryBitMask |= mask;
                    ++sectionIndex;
                }



                mask <<= 1;

                // TODO: we leave add array and biome array to 0 (ocean), we need to change the chunk generator accordingly
            }

            /*for (int j = 0; j < Chunk.SkyLight.Data.Length; ++j)
             * {
             *  Logger.Log(LogLevel.Info, "-" + Chunk.SkyLight.Data[j]);
             * }
             * Console.WriteLine(" ");*/
            /*Chunk.Types.CopyTo(data, i);
             * i += Chunk.Types.Length;
             *
             * Chunk.Data.Data.CopyTo(data, i);
             * i += Chunk.Data.Data.Length;
             *
             * Chunk.Light.Data.CopyTo(data, i);
             * i += Chunk.Light.Data.Length;
             *
             * Chunk.SkyLight.Data.CopyTo(data, i);*/

            byte[] comp = new byte[data.Length];
            int    len;

#if PROFILE_MAPCHUNK
            DateTime start = DateTime.Now;
#endif
            // The ZlibStream gives up to 3ms faster with approx. 10bytes larger, but currently has a bug
            // need to reproduce and post on codeplex DotNetZip site.

            //comp = ZlibStream.CompressBuffer(data);
            //len = comp.Length;
            //try
            //{
            //    byte[] testout = ZlibStream.UncompressBuffer(comp);
            //}
            //catch (Exception)
            //{
            //    // TODO: add some code to generate test case for DotNetZip issue
            //    throw;
            //}

            // Compression level 5 gives 0.3ms faster than the Java version, and exact same size
            // Using the static instance gives another 0.3ms

            Deflater deflater;
            DeflaterPool.TryPop(out deflater);

            if (deflater == null)
            {
                deflater = new Deflater(5);
            }

            deflater.SetInput(data);
            deflater.Finish();
            len = deflater.Deflate(comp);
            deflater.Reset();

            DeflaterPool.Push(deflater);

#if PROFILE_MAPCHUNK
            DateTime end           = DateTime.Now;
            TimeSpan deflateTime   = end - start;
            int      deflateLength = len;
            Console.WriteLine("1: {0}->{1}bytes in {2}ms", data.Length, deflateLength, deflateTime);
#endif

            SetCapacity(22 + len);
            Writer.Write(Coords.ChunkX);
            Writer.Write(Coords.ChunkZ);
            Writer.Write(false);     // Ground Up Continous
            Writer.Write((ushort)primaryBitMask);
            Writer.Write((ushort)0); // Add BitMask
            Writer.Write(len);
            Writer.Write(0);         // Unused
            Writer.Write(comp, 0, len);

#if PROFILE_MAPCHUNK
            start = DateTime.Now;
            comp  = new byte[DataDimension];

            // Original Java compression version
            //java.util.zip.Deflater deflater = new java.util.zip.Deflater(-1);
            //try
            //{
            //    deflater.setInput(data);
            //    deflater.finish();

            //    len = deflater.deflate(comp);
            //}
            //finally
            //{
            //    deflater.end();
            //}
            end = DateTime.Now;
            TimeSpan jTime   = end - start;
            int      jLength = len;

            writeDiffsLength.Add(deflateLength - jLength);
            writeDiffsTime.Add(deflateTime.TotalMilliseconds - jTime.TotalMilliseconds);

            Console.WriteLine("Avg {0}, {1}", writeDiffsLength.Average(), writeDiffsTime.Average());
#endif
        }