diff --git a/Game/Dungeon/DungeonLayout.cs b/Game/Dungeon/DungeonLayout.cs index 06d0e54..bb48cff 100644 --- a/Game/Dungeon/DungeonLayout.cs +++ b/Game/Dungeon/DungeonLayout.cs @@ -39,7 +39,39 @@ public class DungeonLayout } return string.Join("\n", rows); } - + /// + /// Serialize tiles with run-length encoding for network transmission + /// Format: "tile,count;tile,count;..." where tile is 0 or 1 + /// Much more compact for large dungeons + /// + public string SerializeTilesRLE() + { + var sb = new System.Text.StringBuilder(); + int lastTile = Tiles[0, 0]; + int count = 0; + + for (int y = 0; y < Height; y++) + { + for (int x = 0; x < Width; x++) + { + int tile = Tiles[x, y]; + if (tile == lastTile) + { + count++; + } + else + { + sb.Append(lastTile).Append(',').Append(count).Append(';'); + lastTile = tile; + count = 1; + } + } + } + // Don't forget the last run + sb.Append(lastTile).Append(',').Append(count); + + return sb.ToString(); + } public void DeserializeTiles(string data) { var rows = data.Split('\n');