Beispiel #1
0
    public unsafe void SaveChunk(BlockTerrain.Chunk chunk)
    {
        if (!chunk.isEdited)
        {
            return;
        }
        lock (locker)
        {
            Point2 p = new Point2(chunk.chunkx, chunk.chunky);
            long   value;
            if (chunkOffsets.TryGetValue(p, out value))
            {
                stream.Seek(value, SeekOrigin.Begin);
                WriteChunkHeader(stream, chunk.chunkx, chunk.chunky);

                fixed(byte *bptr = &buffer[0])
                {
                    int *iptr = (int *)bptr;

                    for (int x = 0; x < 16; x++)
                    {
                        for (int y = 0; y < 16; y++)
                        {
                            int index = BlockTerrain.GetCellIndex(15 - x, 0, y);
                            int h     = 0;
                            while (h < 128)
                            {
                                *iptr = chunk.GetCellValue(index);
                                iptr++;
                                h++;
                                index++;
                            }
                        }
                    }
                }

                stream.Write(buffer, 0, 131072);

                fixed(byte *bptr = &buffer[0])
                {
                    int *iptr = (int *)bptr;

                    for (int x = 0; x < 16; x++)
                    {
                        int index = BlockTerrain.GetShiftIndex(15 - x, 0);
                        int h     = 0;
                        while (h < 16)
                        {
                            *iptr = chunk.GetShiftValue(index);
                            iptr++;
                            h++;
                            index++;
                        }
                    }
                }

                stream.Write(buffer, 0, 1024);
            }
        }
    }
Beispiel #2
0
    //	public void SaveChunkEntries ()
    //	{
    //		Point2[] entries = new Point2[chunkOffsets.Count];
    //		foreach (KeyValuePair<Point2, long> p in chunkOffsets) {
    //			entries [(int)((p.Value - 786444L) / 132112L)] = p.Key;
    //		}
    //
    //		stream.Seek (0, SeekOrigin.Begin);
    //		for (int i = 0; i < entries.Length; i++) {
    //			WriteChunkEntry (stream, -entries [i].X, entries [i].Y, i);
    //		}
    //	}

    public unsafe void ReadChunk(int chunkx, int chunky, BlockTerrain.Chunk chunk)
    {
        lock (locker)
        {
            Point2 p = new Point2(chunkx, chunky);
            long   value;
            if (chunkOffsets.TryGetValue(p, out value))
            {
                stream.Seek(value, SeekOrigin.Begin);
                ReadChunkHeader(stream);

                stream.Read(buffer, 0, 131072);

                fixed(byte *bptr = &buffer[0])
                {
                    int *iptr = (int *)bptr;

                    for (int x = 0; x < 16; x++)
                    {
                        for (int y = 0; y < 16; y++)
                        {
                            int index = BlockTerrain.GetCellIndex(15 - x, 0, y);
                            int h     = 0;
                            while (h < 128)
                            {
                                chunk.SetCellValue(index, *iptr);
                                iptr++;
                                h++;
                                index++;
                            }
                        }
                    }
                }

                stream.Read(buffer, 0, 1024);

                fixed(byte *bptr = &buffer[0])
                {
                    int *iptr = (int *)bptr;

                    for (int x = 0; x < 16; x++)
                    {
                        int index = BlockTerrain.GetShiftIndex(15 - x, 0);
                        int h     = 0;
                        while (h < 16)
                        {
                            chunk.SetShiftValue(index, *iptr);
                            iptr++;
                            h++;
                            index++;
                        }
                    }
                }
            }
        }
    }