diff --git a/Services/Admin/Pages/DungeonBuilder/TestGen.cshtml b/Services/Admin/Pages/DungeonBuilder/TestGen.cshtml new file mode 100644 index 0000000..5b61653 --- /dev/null +++ b/Services/Admin/Pages/DungeonBuilder/TestGen.cshtml @@ -0,0 +1,278 @@ +@page "/admin/dungeonbuilder/testgen" +@model DungeonBuilder.TestGenModel +@{ + ViewData["Title"] = "Test Generator"; +} + + + +
+ ← Back to Dungeon Builder +
+ +
+
+

Test Generator

+

Preview dungeon generation with your templates

+
+
+ +@if (!string.IsNullOrEmpty(Model.Message)) +{ +
+ @Model.Message +
+} + +
+ +
+
+ @Html.AntiForgeryToken() + +
+

Generation Settings

+
+
+ + +
+
+ + +
+
+ + +
+
+
+ +
+

Size Override

+
+
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+
+ + +
+ + @if (Model.GeneratedDungeon != null) + { +
+

Results

+
+
+ Size: + @Model.GeneratedDungeon.Width Γ— @Model.GeneratedDungeon.Height +
+
+ Rooms: + @Model.GeneratedDungeon.RoomCount +
+
+ Mobs: + @Model.GeneratedDungeon.MobCount +
+
+ Chests: + @Model.GeneratedDungeon.ChestCount +
+
+ Objects: + @Model.GeneratedDungeon.ObjectCount +
+
+ Seed: + @Model.GeneratedDungeon.Seed +
+
+ Gen Time: + @Model.GeneratedDungeon.GenerationTimeMs ms +
+
+
+ } + +
+

Legend

+
+
Floor
+
Wall
+
Door
+
Player Spawn
+
Exit Portal
+
Mob Spawn
+
Chest
+
Object
+
+
+
+ + +
+
+
+

Dungeon Preview

+ @if (Model.GeneratedDungeon != null) + { +
+ + + 16px +
+ } +
+ + @if (Model.GeneratedDungeon != null) + { +
+ @for (int y = 0; y < Model.GeneratedDungeon.Height; y++) + { + @for (int x = 0; x < Model.GeneratedDungeon.Width; x++) + { + var cell = Model.GeneratedDungeon.GetCell(x, y); +
+ @if (!string.IsNullOrEmpty(cell.Icon)) + { + @cell.Icon + } +
+ } + } +
+ } + else + { +
+
+
πŸ—ΊοΈ
+

Click "Generate Dungeon" to preview

+
+
+ } +
+ + @if (Model.GeneratedDungeon?.Rooms != null && Model.GeneratedDungeon.Rooms.Any()) + { +
+

Rooms (@Model.GeneratedDungeon.Rooms.Count)

+
+ @foreach (var room in Model.GeneratedDungeon.Rooms) + { +
+
@room.Type
+
@room.X,@room.Y β†’ @(room.X + room.Width),@(room.Y + room.Height)
+
@room.WidthΓ—@room.Height
+
+ } +
+
+ } +
+
+ + diff --git a/Services/Admin/Pages/DungeonBuilder/TestGen.cshtml.cs b/Services/Admin/Pages/DungeonBuilder/TestGen.cshtml.cs new file mode 100644 index 0000000..1412c84 --- /dev/null +++ b/Services/Admin/Pages/DungeonBuilder/TestGen.cshtml.cs @@ -0,0 +1,384 @@ +using DataServer.Data; +using DataServer.Data.Entities; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System.Diagnostics; + +namespace Server.Services.Admin.Pages.DungeonBuilder; + +public class TestGenModel : AdminPageModel +{ + private readonly GameDbContext _db; + + public TestGenModel(GameDbContext db) + { + _db = db; + } + + public List Palettes { get; set; } = new(); + + // Form inputs + public int? SelectedPaletteId { get; set; } + public int Tier { get; set; } = 1; + public int? Seed { get; set; } + public int Width { get; set; } = 80; + public int Height { get; set; } = 80; + public int MinRooms { get; set; } = 5; + public int MaxRooms { get; set; } = 12; + + // Output + public GeneratedDungeonPreview? GeneratedDungeon { get; set; } + + public string? Message { get; set; } + public bool IsError { get; set; } + + public async Task OnGetAsync() + { + await LoadPalettes(); + } + + public async Task OnPostAsync(int? paletteId, int tier, int? seed, int width, int height, int minRooms, int maxRooms) + { + await LoadPalettes(); + + SelectedPaletteId = paletteId; + Tier = tier; + Seed = seed; + Width = Math.Clamp(width, 20, 200); + Height = Math.Clamp(height, 20, 200); + MinRooms = Math.Max(1, minRooms); + MaxRooms = Math.Max(MinRooms, maxRooms); + + try + { + var sw = Stopwatch.StartNew(); + var actualSeed = seed ?? Random.Shared.Next(); + var rng = new Random(actualSeed); + + // Load palette if specified + DungeonPalette? palette = null; + if (paletteId.HasValue) + { + palette = await _db.DungeonPalettes.FindAsync(paletteId.Value); + } + + // Generate dungeon using simple room placement + var dungeon = GenerateSimpleDungeon(rng, palette); + + sw.Stop(); + dungeon.Seed = actualSeed; + dungeon.GenerationTimeMs = sw.ElapsedMilliseconds; + + GeneratedDungeon = dungeon; + Message = $"Dungeon generated successfully in {sw.ElapsedMilliseconds}ms!"; + } + catch (Exception ex) + { + IsError = true; + Message = $"Generation failed: {ex.Message}"; + } + + return Page(); + } + + private GeneratedDungeonPreview GenerateSimpleDungeon(Random rng, DungeonPalette? palette) + { + var dungeon = new GeneratedDungeonPreview + { + Width = Width, + Height = Height, + Cells = new CellInfo[Width, Height] + }; + + // Initialize all as walls + for (int y = 0; y < Height; y++) + { + for (int x = 0; x < Width; x++) + { + dungeon.Cells[x, y] = new CellInfo { Type = CellType.Wall }; + } + } + + // Get room count from palette or use overrides + int minRooms = palette?.MinRooms ?? MinRooms; + int maxRooms = palette?.MaxRooms ?? MaxRooms; + int roomCount = rng.Next(minRooms, maxRooms + 1); + + // Room size ranges + int minRoomW = palette?.MinRoomWidth ?? 6; + int maxRoomW = palette?.MaxRoomWidth ?? 15; + int minRoomH = palette?.MinRoomHeight ?? 6; + int maxRoomH = palette?.MaxRoomHeight ?? 15; + + var rooms = new List(); + int attempts = 0; + int maxAttempts = roomCount * 50; + + while (rooms.Count < roomCount && attempts < maxAttempts) + { + attempts++; + + int w = rng.Next(minRoomW, maxRoomW + 1); + int h = rng.Next(minRoomH, maxRoomH + 1); + int x = rng.Next(2, Width - w - 2); + int y = rng.Next(2, Height - h - 2); + + var newRoom = new RoomInfo { X = x, Y = y, Width = w, Height = h }; + + // Check overlap with existing rooms (with padding) + bool overlaps = false; + foreach (var existing in rooms) + { + if (RoomsOverlap(newRoom, existing, 3)) + { + overlaps = true; + break; + } + } + + if (!overlaps) + { + rooms.Add(newRoom); + } + } + + // Assign room types + if (rooms.Count > 0) + { + rooms[0].Type = "Entrance"; + if (rooms.Count > 1) + { + rooms[rooms.Count - 1].Type = "Exit"; + } + + // Assign other types randomly + var types = new[] { "Normal", "Normal", "Normal", "Treasure", "Boss" }; + for (int i = 1; i < rooms.Count - 1; i++) + { + rooms[i].Type = types[rng.Next(types.Length)]; + } + } + + // Carve rooms + foreach (var room in rooms) + { + for (int ry = room.Y; ry < room.Y + room.Height; ry++) + { + for (int rx = room.X; rx < room.X + room.Width; rx++) + { + dungeon.Cells[rx, ry] = new CellInfo { Type = CellType.Floor }; + } + } + } + + // Connect rooms with corridors + for (int i = 0; i < rooms.Count - 1; i++) + { + var roomA = rooms[i]; + var roomB = rooms[i + 1]; + + int ax = roomA.X + roomA.Width / 2; + int ay = roomA.Y + roomA.Height / 2; + int bx = roomB.X + roomB.Width / 2; + int by = roomB.Y + roomB.Height / 2; + + // L-shaped corridor + if (rng.Next(2) == 0) + { + CarveHorizontalCorridor(dungeon, ax, bx, ay); + CarveVerticalCorridor(dungeon, ay, by, bx); + } + else + { + CarveVerticalCorridor(dungeon, ay, by, ax); + CarveHorizontalCorridor(dungeon, ax, bx, by); + } + } + + // Place spawns + if (rooms.Count > 0) + { + // Player spawn in entrance room + var entrance = rooms[0]; + int px = entrance.X + entrance.Width / 2; + int py = entrance.Y + entrance.Height / 2; + dungeon.Cells[px, py] = new CellInfo { Type = CellType.PlayerSpawn }; + + // Exit in last room + if (rooms.Count > 1) + { + var exit = rooms[rooms.Count - 1]; + int ex = exit.X + exit.Width / 2; + int ey = exit.Y + exit.Height / 2; + dungeon.Cells[ex, ey] = new CellInfo { Type = CellType.Exit }; + } + + // Mobs in other rooms + for (int i = 1; i < rooms.Count; i++) + { + var room = rooms[i]; + int mobCount = room.Type == "Boss" ? 1 : rng.Next(1, 4); + + for (int m = 0; m < mobCount; m++) + { + int mx = room.X + rng.Next(1, room.Width - 1); + int my = room.Y + rng.Next(1, room.Height - 1); + if (dungeon.Cells[mx, my].Type == CellType.Floor) + { + dungeon.Cells[mx, my] = new CellInfo { Type = CellType.Mob }; + dungeon.MobCount++; + } + } + + // Chests in treasure rooms + if (room.Type == "Treasure" || rng.Next(3) == 0) + { + int cx = room.X + rng.Next(1, room.Width - 1); + int cy = room.Y + rng.Next(1, room.Height - 1); + if (dungeon.Cells[cx, cy].Type == CellType.Floor) + { + dungeon.Cells[cx, cy] = new CellInfo { Type = CellType.Chest }; + dungeon.ChestCount++; + } + } + } + } + + // Place some decorative objects + int decorDensity = palette?.DecorationDensity ?? 20; + int objTarget = (Width * Height * decorDensity) / 10000; + + for (int i = 0; i < objTarget && i < 50; i++) + { + int ox = rng.Next(1, Width - 1); + int oy = rng.Next(1, Height - 1); + if (dungeon.Cells[ox, oy].Type == CellType.Floor) + { + dungeon.Cells[ox, oy] = new CellInfo { Type = CellType.Object }; + dungeon.ObjectCount++; + } + } + + dungeon.Rooms = rooms; + dungeon.RoomCount = rooms.Count; + + return dungeon; + } + + private bool RoomsOverlap(RoomInfo a, RoomInfo b, int padding) + { + return a.X - padding < b.X + b.Width + padding && + a.X + a.Width + padding > b.X - padding && + a.Y - padding < b.Y + b.Height + padding && + a.Y + a.Height + padding > b.Y - padding; + } + + private void CarveHorizontalCorridor(GeneratedDungeonPreview dungeon, int x1, int x2, int y) + { + int minX = Math.Min(x1, x2); + int maxX = Math.Max(x1, x2); + + for (int x = minX; x <= maxX; x++) + { + if (x >= 0 && x < dungeon.Width && y >= 0 && y < dungeon.Height) + { + if (dungeon.Cells[x, y].Type == CellType.Wall) + dungeon.Cells[x, y] = new CellInfo { Type = CellType.Floor }; + } + } + } + + private void CarveVerticalCorridor(GeneratedDungeonPreview dungeon, int y1, int y2, int x) + { + int minY = Math.Min(y1, y2); + int maxY = Math.Max(y1, y2); + + for (int y = minY; y <= maxY; y++) + { + if (x >= 0 && x < dungeon.Width && y >= 0 && y < dungeon.Height) + { + if (dungeon.Cells[x, y].Type == CellType.Wall) + dungeon.Cells[x, y] = new CellInfo { Type = CellType.Floor }; + } + } + } + + private async Task LoadPalettes() + { + Palettes = await _db.DungeonPalettes + .Where(p => p.IsEnabled) + .OrderBy(p => p.Name) + .ToListAsync(); + } + + // Helper classes + public class GeneratedDungeonPreview + { + public int Width { get; set; } + public int Height { get; set; } + public CellInfo[,] Cells { get; set; } = new CellInfo[0, 0]; + public List Rooms { get; set; } = new(); + public int RoomCount { get; set; } + public int MobCount { get; set; } + public int ChestCount { get; set; } + public int ObjectCount { get; set; } + public int Seed { get; set; } + public long GenerationTimeMs { get; set; } + + public CellInfo GetCell(int x, int y) + { + if (x >= 0 && x < Width && y >= 0 && y < Height) + return Cells[x, y]; + return new CellInfo { Type = CellType.Wall }; + } + } + + public class CellInfo + { + public CellType Type { get; set; } + + public string CssClass => Type switch + { + CellType.Wall => "wall", + CellType.Floor => "floor", + CellType.Door => "door", + CellType.PlayerSpawn => "player", + CellType.Exit => "exit", + CellType.Mob => "mob", + CellType.Chest => "chest", + CellType.Object => "object", + _ => "wall" + }; + + public string Icon => Type switch + { + CellType.PlayerSpawn => "πŸ§‘", + CellType.Exit => "πŸŒ€", + CellType.Mob => "πŸ‘Ύ", + CellType.Chest => "πŸ“¦", + CellType.Object => "β€’", + _ => "" + }; + } + + public enum CellType + { + Wall, + Floor, + Door, + PlayerSpawn, + Exit, + Mob, + Chest, + Object + } + + public class RoomInfo + { + public int X { get; set; } + public int Y { get; set; } + public int Width { get; set; } + public int Height { get; set; } + public string Type { get; set; } = "Normal"; + } +}