using System.Text.Json; namespace Server.Game.Dungeon; /// /// Contains the generated dungeon layout data /// public class DungeonLayout { public int Seed { get; set; } public int Tier { get; set; } public int Width { get; set; } public int Height { get; set; } public int TileSize { get; set; } public int[,] Tiles { get; set; } = new int[0, 0]; public List Rooms { get; set; } = new(); public DungeonPoint SpawnPoint { get; set; } public DungeonPoint? ExitPosition { get; set; } // Set when dungeon is cleared public List MobSpawns { get; set; } = new(); public List ChestSpawns { get; set; } = new(); public int TotalMobs { get; set; } public int TotalChests { get; set; } // Serialize tiles to string for DB storage public string SerializeTiles() { var rows = new List(); for (int y = 0; y < Height; y++) { var row = new char[Width]; for (int x = 0; x < Width; x++) { row[x] = Tiles[x, y] == 1 ? '.' : '#'; } rows.Add(new string(row)); } 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'); Height = rows.Length; Width = rows.Length > 0 ? rows[0].Length : 0; Tiles = new int[Width, Height]; for (int y = 0; y < Height; y++) { for (int x = 0; x < Width && x < rows[y].Length; x++) { Tiles[x, y] = rows[y][x] == '.' ? 1 : 0; } } } public string ToJson() { return JsonSerializer.Serialize(new { seed = Seed, tier = Tier, width = Width, height = Height, tileSize = TileSize, tiles = SerializeTiles(), spawn = new { x = SpawnPoint.X, y = SpawnPoint.Y }, rooms = Rooms.Select(r => new { x = r.X, y = r.Y, w = r.Width, h = r.Height, type = r.Type.ToString() }), mobs = MobSpawns.Select(m => new { id = m.Id, x = m.X, y = m.Y, type = m.MobType.ToString(), roomIndex = m.RoomIndex }), chests = ChestSpawns.Select(c => new { id = c.Id, x = c.X, y = c.Y, type = c.ChestType.ToString(), roomIndex = c.RoomIndex }) }); } }