277 lines
8.7 KiB
C#
277 lines
8.7 KiB
C#
namespace Server.Game.Dungeon;
|
|
|
|
/// <summary>
|
|
/// Procedural dungeon generator using BSP (Binary Space Partition) + room placement
|
|
/// </summary>
|
|
public static class DungeonGenerator
|
|
{
|
|
/// <summary>
|
|
/// Generate a dungeon layout for the given seed and tier
|
|
/// </summary>
|
|
public static DungeonLayout Generate(int seed, int tier)
|
|
{
|
|
var generator = new DungeonGeneratorInstance(seed, tier);
|
|
return generator.Generate();
|
|
}
|
|
}
|
|
|
|
internal class DungeonGeneratorInstance
|
|
{
|
|
private readonly Random _rng;
|
|
private readonly int _seed;
|
|
private readonly int _tier;
|
|
private readonly int _tileSize = 32;
|
|
|
|
// Tier-based scaling
|
|
private int MinRooms => 5 + (_tier * 2);
|
|
private int MaxRooms => 8 + (_tier * 3);
|
|
private int MinRoomSize => 5; // In tiles
|
|
private int MaxRoomSize => 12;
|
|
private int MapWidth => 50 + (_tier * 10); // In tiles
|
|
private int MapHeight => 50 + (_tier * 10);
|
|
|
|
public DungeonGeneratorInstance(int seed, int tier)
|
|
{
|
|
_seed = seed;
|
|
_tier = Math.Max(1, tier);
|
|
_rng = new Random(seed);
|
|
}
|
|
|
|
public DungeonLayout Generate()
|
|
{
|
|
var layout = new DungeonLayout
|
|
{
|
|
Seed = _seed,
|
|
Tier = _tier,
|
|
Width = MapWidth,
|
|
Height = MapHeight,
|
|
TileSize = _tileSize
|
|
};
|
|
|
|
// Initialize tile map (0 = wall, 1 = floor)
|
|
layout.Tiles = new int[MapWidth, MapHeight];
|
|
|
|
// Generate rooms
|
|
int roomCount = _rng.Next(MinRooms, MaxRooms + 1);
|
|
var rooms = new List<DungeonRoom>();
|
|
int attempts = 0;
|
|
int maxAttempts = roomCount * 20;
|
|
|
|
while (rooms.Count < roomCount && attempts < maxAttempts)
|
|
{
|
|
attempts++;
|
|
|
|
int w = _rng.Next(MinRoomSize, MaxRoomSize + 1);
|
|
int h = _rng.Next(MinRoomSize, MaxRoomSize + 1);
|
|
int x = _rng.Next(2, MapWidth - w - 2);
|
|
int y = _rng.Next(2, MapHeight - h - 2);
|
|
|
|
var newRoom = new DungeonRoom(x, y, w, h);
|
|
|
|
// Check for overlap with existing rooms (with padding)
|
|
bool overlaps = rooms.Any(r => newRoom.Intersects(r, padding: 2));
|
|
|
|
if (!overlaps)
|
|
{
|
|
rooms.Add(newRoom);
|
|
CarveRoom(layout.Tiles, newRoom);
|
|
}
|
|
}
|
|
|
|
// Sort rooms by distance from top-left for corridor generation
|
|
rooms = rooms.OrderBy(r => r.CenterX + r.CenterY).ToList();
|
|
|
|
// Connect rooms with corridors
|
|
for (int i = 0; i < rooms.Count - 1; i++)
|
|
{
|
|
ConnectRooms(layout.Tiles, rooms[i], rooms[i + 1]);
|
|
}
|
|
|
|
// Optionally add some extra connections for loops
|
|
for (int i = 0; i < rooms.Count / 3; i++)
|
|
{
|
|
int a = _rng.Next(rooms.Count);
|
|
int b = _rng.Next(rooms.Count);
|
|
if (a != b)
|
|
{
|
|
ConnectRooms(layout.Tiles, rooms[a], rooms[b]);
|
|
}
|
|
}
|
|
|
|
// Assign room types
|
|
AssignRoomTypes(rooms, layout);
|
|
|
|
// Place spawn point (first room)
|
|
layout.SpawnPoint = new DungeonPoint(rooms[0].CenterX, rooms[0].CenterY);
|
|
rooms[0].Type = DungeonRoomType.Entrance;
|
|
|
|
// Generate mob spawns
|
|
GenerateMobSpawns(rooms, layout);
|
|
|
|
// Generate chest spawns
|
|
GenerateChestSpawns(rooms, layout);
|
|
|
|
// Store rooms
|
|
layout.Rooms = rooms;
|
|
|
|
return layout;
|
|
}
|
|
|
|
private void CarveRoom(int[,] tiles, DungeonRoom room)
|
|
{
|
|
for (int x = room.X; x < room.X + room.Width; x++)
|
|
{
|
|
for (int y = room.Y; y < room.Y + room.Height; y++)
|
|
{
|
|
tiles[x, y] = 1; // Floor
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ConnectRooms(int[,] tiles, DungeonRoom a, DungeonRoom b)
|
|
{
|
|
int x = a.CenterX;
|
|
int y = a.CenterY;
|
|
int targetX = b.CenterX;
|
|
int targetY = b.CenterY;
|
|
|
|
// L-shaped corridor (horizontal then vertical, or vice versa)
|
|
if (_rng.Next(2) == 0)
|
|
{
|
|
// Horizontal first
|
|
CarveHorizontalTunnel(tiles, x, targetX, y);
|
|
CarveVerticalTunnel(tiles, y, targetY, targetX);
|
|
}
|
|
else
|
|
{
|
|
// Vertical first
|
|
CarveVerticalTunnel(tiles, y, targetY, x);
|
|
CarveHorizontalTunnel(tiles, x, targetX, targetY);
|
|
}
|
|
}
|
|
|
|
private void CarveHorizontalTunnel(int[,] tiles, 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 < tiles.GetLength(0) && y >= 0 && y < tiles.GetLength(1))
|
|
{
|
|
tiles[x, y] = 1;
|
|
// Make corridor 2 tiles wide
|
|
if (y + 1 < tiles.GetLength(1)) tiles[x, y + 1] = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CarveVerticalTunnel(int[,] tiles, 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 < tiles.GetLength(0) && y >= 0 && y < tiles.GetLength(1))
|
|
{
|
|
tiles[x, y] = 1;
|
|
// Make corridor 2 tiles wide
|
|
if (x + 1 < tiles.GetLength(0)) tiles[x + 1, y] = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AssignRoomTypes(List<DungeonRoom> rooms, DungeonLayout layout)
|
|
{
|
|
if (rooms.Count == 0) return;
|
|
|
|
// First room is entrance (already set)
|
|
// Last room is boss room
|
|
if (rooms.Count > 1)
|
|
{
|
|
rooms[^1].Type = DungeonRoomType.Boss;
|
|
}
|
|
|
|
// Assign some treasure rooms (10-20% of rooms)
|
|
int treasureCount = Math.Max(1, rooms.Count / 6);
|
|
var availableRooms = rooms.Skip(1).Take(rooms.Count - 2).ToList();
|
|
|
|
for (int i = 0; i < treasureCount && availableRooms.Count > 0; i++)
|
|
{
|
|
int idx = _rng.Next(availableRooms.Count);
|
|
availableRooms[idx].Type = DungeonRoomType.Treasure;
|
|
availableRooms.RemoveAt(idx);
|
|
}
|
|
}
|
|
|
|
private void GenerateMobSpawns(List<DungeonRoom> rooms, DungeonLayout layout)
|
|
{
|
|
foreach (var room in rooms)
|
|
{
|
|
if (room.Type == DungeonRoomType.Entrance) continue;
|
|
|
|
int mobCount = room.Type switch
|
|
{
|
|
DungeonRoomType.Boss => 1, // Just the boss
|
|
DungeonRoomType.Treasure => _rng.Next(1, 3), // Light guard
|
|
DungeonRoomType.Combat => _rng.Next(2, 5 + _tier),
|
|
_ => _rng.Next(1, 3)
|
|
};
|
|
|
|
for (int i = 0; i < mobCount; i++)
|
|
{
|
|
// Random position within room (with margin)
|
|
int x = room.X + 1 + _rng.Next(room.Width - 2);
|
|
int y = room.Y + 1 + _rng.Next(room.Height - 2);
|
|
|
|
var spawn = new DungeonMobSpawn
|
|
{
|
|
Id = layout.MobSpawns.Count + 1,
|
|
X = x,
|
|
Y = y,
|
|
MobType = room.Type == DungeonRoomType.Boss ? DungeonMobSpawnType.Boss : DungeonMobSpawnType.Normal,
|
|
RoomIndex = rooms.IndexOf(room)
|
|
};
|
|
|
|
layout.MobSpawns.Add(spawn);
|
|
}
|
|
}
|
|
|
|
layout.TotalMobs = layout.MobSpawns.Count;
|
|
}
|
|
|
|
private void GenerateChestSpawns(List<DungeonRoom> rooms, DungeonLayout layout)
|
|
{
|
|
foreach (var room in rooms)
|
|
{
|
|
int chestCount = room.Type switch
|
|
{
|
|
DungeonRoomType.Treasure => _rng.Next(2, 4),
|
|
DungeonRoomType.Boss => 1, // Guaranteed chest after boss
|
|
DungeonRoomType.Combat => _rng.NextDouble() < 0.3 ? 1 : 0, // 30% chance
|
|
_ => 0
|
|
};
|
|
|
|
for (int i = 0; i < chestCount; i++)
|
|
{
|
|
int x = room.X + 1 + _rng.Next(room.Width - 2);
|
|
int y = room.Y + 1 + _rng.Next(room.Height - 2);
|
|
|
|
var chest = new DungeonChestSpawn
|
|
{
|
|
Id = layout.ChestSpawns.Count + 1,
|
|
X = x,
|
|
Y = y,
|
|
ChestType = room.Type == DungeonRoomType.Boss ? DungeonChestType.Boss : DungeonChestType.Normal,
|
|
RoomIndex = rooms.IndexOf(room)
|
|
};
|
|
|
|
layout.ChestSpawns.Add(chest);
|
|
}
|
|
}
|
|
|
|
layout.TotalChests = layout.ChestSpawns.Count;
|
|
}
|
|
}
|