Example #1
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 #2
0
 /// <summary>
 /// Rebuilds the indexes of all players, by changing their index property to the correct index in the map's player list
 /// </summary>
 private static void RebuildIndexes(Bricklayer.Common.World.Map map)
 {
     for (int i = 0; i < map.Players.Count; i++)
         map.Players[i].Index = i;
 }