Files

151 lines
4.5 KiB
C#

using System.Text.Json;
namespace GameServer.Dungeon;
/// <summary>
/// Contains the generated dungeon layout data
/// </summary>
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; }
/// <summary>Tile IDs (for V2) or 0/1 (for V1)</summary>
public int[,] Tiles { get; set; } = new int[0, 0];
/// <summary>Walkability grid (true = can walk, false = blocked)</summary>
public bool[,]? Walkable { get; set; }
public List<DungeonRoom> Rooms { get; set; } = new();
public DungeonPoint SpawnPoint { get; set; }
public DungeonPoint? ExitPosition { get; set; } // Set when dungeon is cleared
public List<DungeonMobSpawn> MobSpawns { get; set; } = new();
public List<DungeonChestSpawn> ChestSpawns { get; set; } = new();
public List<DungeonNpcSpawn> NpcSpawns { get; set; } = new();
public int TotalMobs { get; set; }
public int TotalChests { get; set; }
/// <summary>Check if a tile position is walkable</summary>
public bool IsWalkable(int x, int y)
{
if (x < 0 || x >= Width || y < 0 || y >= Height)
return false;
// If we have explicit walkability array, use it
if (Walkable != null)
return Walkable[x, y];
// Fallback: V1 convention (0 = wall, non-zero = floor)
return Tiles[x, y] != 0;
}
// Serialize tiles to string for DB storage
public string SerializeTiles()
{
var rows = new List<string>();
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);
}
/// <summary>
/// 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
/// </summary>
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
}),
npcs = NpcSpawns.Select(n => new
{
id = n.Id,
x = n.X,
y = n.Y,
npcDefinitionId = n.NpcDefinitionId,
npcName = n.NpcName,
roomIndex = n.RoomIndex
})
});
}
}