Dungeon Builder. CONT

This commit is contained in:
dingo
2026-01-04 12:04:45 -05:00
parent 2ba9df6485
commit b8af3545a8
2 changed files with 662 additions and 0 deletions
@@ -0,0 +1,278 @@
@page "/admin/dungeonbuilder/testgen"
@model DungeonBuilder.TestGenModel
@{
ViewData["Title"] = "Test Generator";
}
<style>
.dungeon-grid {
display: grid;
gap: 0;
background: #0a0a0f;
border: 2px solid #374151;
border-radius: 4px;
overflow: auto;
max-height: 70vh;
}
.dungeon-cell {
width: 16px;
height: 16px;
image-rendering: pixelated;
position: relative;
}
.dungeon-cell.wall { background: #1a1a2e; }
.dungeon-cell.floor { background: #2d3748; }
.dungeon-cell.door { background: #805ad5; }
.dungeon-cell.player { background: #48bb78; }
.dungeon-cell.exit { background: #4299e1; }
.dungeon-cell.mob { background: #f56565; }
.dungeon-cell.chest { background: #ecc94b; }
.dungeon-cell.object { background: #9f7aea; }
.cell-icon {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
font-size: 12px;
}
.legend-box {
width: 16px;
height: 16px;
border-radius: 2px;
}
</style>
<div class="mb-4">
<a href="/admin/dungeonbuilder" class="text-blue-400 hover:text-blue-300 text-sm">&larr; Back to Dungeon Builder</a>
</div>
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-6">
<div>
<h1 class="text-2xl font-bold text-white">Test Generator</h1>
<p class="text-gray-400 text-sm mt-1">Preview dungeon generation with your templates</p>
</div>
</div>
@if (!string.IsNullOrEmpty(Model.Message))
{
<div class="mb-4 p-3 rounded @(Model.IsError ? "bg-red-900/50 text-red-300" : "bg-green-900/50 text-green-300")">
@Model.Message
</div>
}
<div class="grid grid-cols-1 lg:grid-cols-4 gap-6">
<!-- Left: Controls -->
<div class="space-y-4">
<form method="post" id="genForm">
@Html.AntiForgeryToken()
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Generation Settings</h3>
<div class="space-y-3">
<div>
<label class="text-xs text-gray-400">Palette</label>
<select name="paletteId" class="input w-full text-sm">
<option value="">-- Use defaults --</option>
@foreach (var p in Model.Palettes)
{
<option value="@p.Id" selected="@(Model.SelectedPaletteId == p.Id)">@p.Name</option>
}
</select>
</div>
<div>
<label class="text-xs text-gray-400">Tier</label>
<input type="number" name="tier" value="@Model.Tier" class="input w-full text-sm" min="1" max="20" />
</div>
<div>
<label class="text-xs text-gray-400">Seed (optional)</label>
<input type="number" name="seed" value="@Model.Seed" class="input w-full text-sm" placeholder="Random" />
</div>
</div>
</div>
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Size Override</h3>
<div class="space-y-3">
<div class="grid grid-cols-2 gap-2">
<div>
<label class="text-xs text-gray-400">Width</label>
<input type="number" name="width" value="@Model.Width" class="input w-full text-sm" min="20" max="200" />
</div>
<div>
<label class="text-xs text-gray-400">Height</label>
<input type="number" name="height" value="@Model.Height" class="input w-full text-sm" min="20" max="200" />
</div>
</div>
<div class="grid grid-cols-2 gap-2">
<div>
<label class="text-xs text-gray-400">Min Rooms</label>
<input type="number" name="minRooms" value="@Model.MinRooms" class="input w-full text-sm" min="1" />
</div>
<div>
<label class="text-xs text-gray-400">Max Rooms</label>
<input type="number" name="maxRooms" value="@Model.MaxRooms" class="input w-full text-sm" min="1" />
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary w-full">
🎲 Generate Dungeon
</button>
</form>
@if (Model.GeneratedDungeon != null)
{
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Results</h3>
<div class="space-y-2 text-sm">
<div class="flex justify-between">
<span class="text-gray-400">Size:</span>
<span class="text-white">@Model.GeneratedDungeon.Width × @Model.GeneratedDungeon.Height</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Rooms:</span>
<span class="text-white">@Model.GeneratedDungeon.RoomCount</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Mobs:</span>
<span class="text-white">@Model.GeneratedDungeon.MobCount</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Chests:</span>
<span class="text-white">@Model.GeneratedDungeon.ChestCount</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Objects:</span>
<span class="text-white">@Model.GeneratedDungeon.ObjectCount</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Seed:</span>
<span class="text-white font-mono text-xs">@Model.GeneratedDungeon.Seed</span>
</div>
<div class="flex justify-between">
<span class="text-gray-400">Gen Time:</span>
<span class="text-white">@Model.GeneratedDungeon.GenerationTimeMs ms</span>
</div>
</div>
</div>
}
<div class="card p-4">
<h3 class="font-semibold text-white mb-3">Legend</h3>
<div class="space-y-2">
<div class="legend-item"><div class="legend-box bg-gray-700"></div><span class="text-gray-300">Floor</span></div>
<div class="legend-item"><div class="legend-box bg-gray-900"></div><span class="text-gray-300">Wall</span></div>
<div class="legend-item"><div class="legend-box bg-purple-600"></div><span class="text-gray-300">Door</span></div>
<div class="legend-item"><div class="legend-box bg-green-500"></div><span class="text-gray-300">Player Spawn</span></div>
<div class="legend-item"><div class="legend-box bg-blue-500"></div><span class="text-gray-300">Exit Portal</span></div>
<div class="legend-item"><div class="legend-box bg-red-500"></div><span class="text-gray-300">Mob Spawn</span></div>
<div class="legend-item"><div class="legend-box bg-yellow-500"></div><span class="text-gray-300">Chest</span></div>
<div class="legend-item"><div class="legend-box bg-purple-400"></div><span class="text-gray-300">Object</span></div>
</div>
</div>
</div>
<!-- Right: Dungeon Preview -->
<div class="lg:col-span-3">
<div class="card p-4">
<div class="flex items-center justify-between mb-3">
<h3 class="font-semibold text-white">Dungeon Preview</h3>
@if (Model.GeneratedDungeon != null)
{
<div class="flex gap-2">
<button type="button" onclick="zoomIn()" class="btn btn-ghost text-sm">➕</button>
<button type="button" onclick="zoomOut()" class="btn btn-ghost text-sm">➖</button>
<span class="text-xs text-gray-500" id="zoomLevel">16px</span>
</div>
}
</div>
@if (Model.GeneratedDungeon != null)
{
<div class="dungeon-grid" id="dungeonGrid"
style="grid-template-columns: repeat(@Model.GeneratedDungeon.Width, 16px);">
@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);
<div class="dungeon-cell @cell.CssClass" title="@x,@y: @cell.Type">
@if (!string.IsNullOrEmpty(cell.Icon))
{
<span class="cell-icon">@cell.Icon</span>
}
</div>
}
}
</div>
}
else
{
<div class="flex items-center justify-center h-96 text-gray-500">
<div class="text-center">
<div class="text-4xl mb-2">🗺️</div>
<p>Click "Generate Dungeon" to preview</p>
</div>
</div>
}
</div>
@if (Model.GeneratedDungeon?.Rooms != null && Model.GeneratedDungeon.Rooms.Any())
{
<div class="card p-4 mt-4">
<h3 class="font-semibold text-white mb-3">Rooms (@Model.GeneratedDungeon.Rooms.Count)</h3>
<div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-2 max-h-48 overflow-auto">
@foreach (var room in Model.GeneratedDungeon.Rooms)
{
<div class="bg-gray-800 rounded p-2 text-xs">
<div class="font-semibold text-white">@room.Type</div>
<div class="text-gray-400">@room.X,@room.Y → @(room.X + room.Width),@(room.Y + room.Height)</div>
<div class="text-gray-500">@room.Width×@room.Height</div>
</div>
}
</div>
</div>
}
</div>
</div>
<script>
let cellSize = 16;
function zoomIn() {
cellSize = Math.min(32, cellSize + 4);
updateZoom();
}
function zoomOut() {
cellSize = Math.max(4, cellSize - 4);
updateZoom();
}
function updateZoom() {
const grid = document.getElementById('dungeonGrid');
if (!grid) return;
const width = @(Model.GeneratedDungeon?.Width ?? 0);
grid.style.gridTemplateColumns = `repeat(${width}, ${cellSize}px)`;
document.querySelectorAll('.dungeon-cell').forEach(cell => {
cell.style.width = cellSize + 'px';
cell.style.height = cellSize + 'px';
});
document.querySelectorAll('.cell-icon').forEach(icon => {
icon.style.fontSize = Math.max(8, cellSize * 0.6) + 'px';
});
document.getElementById('zoomLevel').textContent = cellSize + 'px';
}
</script>
@@ -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<DungeonPalette> 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<IActionResult> 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<RoomInfo>();
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<RoomInfo> 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";
}
}