Inheritance: Bricklayer.Common.World.Map
Example #1
0
        public static Bricklayer.Server.World.Map LoadMap(string name)
        {
            Bricklayer.Server.World.Map map;
            using (BinaryReader binaryReader = new BinaryReader( //Create a new binary writer to write to the file
                new BufferedStream(
                File.Open(Path.Combine(mapFolder, name + mapSuffix), FileMode.Open))))
            {
                //Read general data
                map = new Bricklayer.Server.World.Map(binaryReader.ReadString(), binaryReader.ReadString(),
                    binaryReader.ReadInt16(), binaryReader.ReadInt16(), Server.Maps.Count)
                    { Rating = binaryReader.ReadDouble() };

                for (int y = 0; y < map.Height; y++)
                {
                    for (int x = 0; x < map.Width; x++)
                    {
                        Tile fg;
                        Tile bg;
                        SaveFlags sf = (SaveFlags)binaryReader.ReadByte();

                        int RLE = 0;
                        if (sf.HasFlag(SaveFlags.RLE))
                            RLE = binaryReader.ReadInt16();
                        if (sf.HasFlag(SaveFlags.Foreground))
                            fg = new Tile(BlockType.FromID(binaryReader.ReadByte()));
                        else
                            fg = new Tile(BlockType.Empty);
                        if (sf.HasFlag(SaveFlags.Background))
                            bg = new Tile(BlockType.FromID(binaryReader.ReadByte()));
                        else
                            bg = new Tile(BlockType.Empty);

                        map.Tiles[x, y, 1] = fg;
                        map.Tiles[x, y, 0] = bg;

                        if (RLE > 0) //RLE enabled
                        {
                            for (int i = 1; i <= RLE; i++)
                            {
                                 map.Tiles[x + i, y, 1] = fg;
                                 map.Tiles[x + i, y, 0] = bg;
                            }
                            x += RLE;
                        }
                    }
                }
            }
            return map;
        }
Example #2
0
        public static void SaveMap(Bricklayer.Server.World.Map map)
        {
            using (BinaryWriter binaryWriter = new BinaryWriter( //Create a new binary writer to write to the file
                       new BufferedStream(
                           File.Open(Path.Combine(mapFolder, map.Name + mapSuffix), FileMode.Create))))
            {
                //Write general data
                binaryWriter.Write(map.Name);
                binaryWriter.Write(map.Description);
                binaryWriter.Write((short)map.Width);
                binaryWriter.Write((short)map.Height);
                binaryWriter.Write(map.Rating);

                for (int y = 0; y < map.Height; y++)
                {
                    for (int x = 0; x < map.Width; x++)
                    {
                        BlockType fg = map.Tiles[x, y, 1].Block;
                        BlockType bg = map.Tiles[x, y, 0].Block;
                        SaveFlags sf = SaveFlags.None;

                        //Calculate Run-Length Encoding
                        int i = 0;
                        while (i + 1 + x < map.Width && fg.ID == map.Tiles[x + i + 1, y, 1].Block.ID && bg.ID == map.Tiles[x + i + 1, y, 0].Block.ID)
                        {
                            i++; //If next block is the same, record the amount of same blocks in i
                        }
                        //Calculate flags
                        if (i > 0)
                        {
                            sf |= SaveFlags.RLE;
                        }
                        if (fg.ID != BlockType.Empty.ID)
                        {
                            sf |= SaveFlags.Foreground;
                        }
                        if (bg.ID != BlockType.Empty.ID)
                        {
                            sf |= SaveFlags.Background;
                        }
                        binaryWriter.Write((byte)sf);

                        //Save data
                        if (i > 0)
                        {
                            binaryWriter.Write((short)i);
                        }
                        if (fg.ID != BlockType.Empty.ID)
                        {
                            binaryWriter.Write(fg.ID);
                        }
                        if (bg.ID != BlockType.Empty.ID)
                        {
                            binaryWriter.Write(bg.ID);
                        }

                        x += i;
                    }
                }
            }
        }
Example #3
0
        public static Bricklayer.Server.World.Map LoadMap(string name)
        {
            Bricklayer.Server.World.Map map;
            using (BinaryReader binaryReader = new BinaryReader( //Create a new binary writer to write to the file
                       new BufferedStream(
                           File.Open(Path.Combine(mapFolder, name + mapSuffix), FileMode.Open))))
            {
                //Read general data
                map = new Bricklayer.Server.World.Map(binaryReader.ReadString(), binaryReader.ReadString(),
                                                      binaryReader.ReadInt16(), binaryReader.ReadInt16(), Server.Maps.Count)
                {
                    Rating = binaryReader.ReadDouble()
                };

                for (int y = 0; y < map.Height; y++)
                {
                    for (int x = 0; x < map.Width; x++)
                    {
                        Tile      fg;
                        Tile      bg;
                        SaveFlags sf = (SaveFlags)binaryReader.ReadByte();

                        int RLE = 0;
                        if (sf.HasFlag(SaveFlags.RLE))
                        {
                            RLE = binaryReader.ReadInt16();
                        }
                        if (sf.HasFlag(SaveFlags.Foreground))
                        {
                            fg = new Tile(BlockType.FromID(binaryReader.ReadByte()));
                        }
                        else
                        {
                            fg = new Tile(BlockType.Empty);
                        }
                        if (sf.HasFlag(SaveFlags.Background))
                        {
                            bg = new Tile(BlockType.FromID(binaryReader.ReadByte()));
                        }
                        else
                        {
                            bg = new Tile(BlockType.Empty);
                        }

                        map.Tiles[x, y, 1] = fg;
                        map.Tiles[x, y, 0] = bg;

                        if (RLE > 0) //RLE enabled
                        {
                            for (int i = 1; i <= RLE; i++)
                            {
                                map.Tiles[x + i, y, 1] = fg;
                                map.Tiles[x + i, y, 0] = bg;
                            }
                            x += RLE;
                        }
                    }
                }
            }
            return(map);
        }