Trying GPT Dungeon idea
This commit is contained in:
@@ -0,0 +1,315 @@
|
||||
// DungeonGeneratorV2.TagDrivenTiles.cs
|
||||
// Drop this file next to DungeonGeneratorV2 and wire in the 2 small call sites noted below.
|
||||
// This adds a fast tag index + robust tile picking (and avoids "Void counted as Wall" issues when DB types are messy).
|
||||
|
||||
using DataServer.Data.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Server.Game.Dungeon
|
||||
{
|
||||
/// <summary>
|
||||
/// Normalized tag info: supports comma/space separated tokens + key:value tags.
|
||||
/// Examples:
|
||||
/// - "biome:dungeon,corner_ne,north,wall" => Tokens={"corner_ne","north","wall"}, KV["biome"]="dungeon"
|
||||
/// - "frame:10,sheet:3" => KV["frame"]="10", KV["sheet"]="3"
|
||||
/// </summary>
|
||||
internal sealed class TagInfo
|
||||
{
|
||||
public readonly HashSet<string> Tokens;
|
||||
public readonly Dictionary<string, string> KV;
|
||||
|
||||
public TagInfo(HashSet<string> tokens, Dictionary<string, string> kv)
|
||||
{
|
||||
Tokens = tokens;
|
||||
KV = kv;
|
||||
}
|
||||
|
||||
public bool Has(string token) => Tokens.Contains(token);
|
||||
public bool TryGet(string key, out string value) => KV.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
internal static class TagParser
|
||||
{
|
||||
// Split on commas and whitespace. Your dataset is comma-delimited, but this makes it resilient.
|
||||
private static readonly char[] _seps = new[] { ',', ' ', '\t', '\r', '\n' };
|
||||
|
||||
public static TagInfo Parse(string? tags)
|
||||
{
|
||||
var tokens = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
var kv = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(tags))
|
||||
return new TagInfo(tokens, kv);
|
||||
|
||||
foreach (var raw in tags.Split(_seps, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(raw)) continue;
|
||||
|
||||
var s = raw.Trim();
|
||||
var idx = s.IndexOf(':');
|
||||
if (idx > 0 && idx < s.Length - 1)
|
||||
{
|
||||
var k = s[..idx].Trim();
|
||||
var v = s[(idx + 1)..].Trim();
|
||||
if (k.Length > 0 && v.Length > 0)
|
||||
kv[k] = v;
|
||||
continue;
|
||||
}
|
||||
|
||||
tokens.Add(s);
|
||||
}
|
||||
|
||||
return new TagInfo(tokens, kv);
|
||||
}
|
||||
}
|
||||
|
||||
internal enum TileKind
|
||||
{
|
||||
Unknown = 0,
|
||||
Void,
|
||||
Floor,
|
||||
Wall,
|
||||
Door,
|
||||
Decorative
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a fast index over TileDefinitions using TAGS as truth (not DB TileType),
|
||||
/// because your sample shows "Void" rows with Type=1 which can collide with "Wall".
|
||||
/// </summary>
|
||||
internal sealed class TileTagIndex
|
||||
{
|
||||
public readonly Dictionary<int, TagInfo> TagById = new();
|
||||
public readonly Dictionary<int, TileKind> KindById = new();
|
||||
public readonly List<TileDefinition> Floors = new();
|
||||
public readonly List<TileDefinition> Walls = new();
|
||||
public readonly List<TileDefinition> Doors = new();
|
||||
public readonly List<TileDefinition> Decorative = new();
|
||||
|
||||
// Convenience: cached candidates for wall variants
|
||||
private readonly List<TileDefinition> _solidWalls = new();
|
||||
private readonly List<TileDefinition> _trimWalls = new();
|
||||
private readonly List<TileDefinition> _voidWalls = new(); // wall-ish void boundary (if you ever want them)
|
||||
|
||||
public TileTagIndex(IEnumerable<TileDefinition> tiles)
|
||||
{
|
||||
foreach (var t in tiles)
|
||||
{
|
||||
var info = TagParser.Parse(t.Tags);
|
||||
TagById[t.Id] = info;
|
||||
|
||||
var kind = Classify(t, info);
|
||||
KindById[t.Id] = kind;
|
||||
|
||||
switch (kind)
|
||||
{
|
||||
case TileKind.Floor:
|
||||
Floors.Add(t);
|
||||
break;
|
||||
case TileKind.Wall:
|
||||
Walls.Add(t);
|
||||
if (info.Has("trim")) _trimWalls.Add(t);
|
||||
else _solidWalls.Add(t);
|
||||
if (info.Has("void") || info.Has("boundary") || info.Has("empty")) _voidWalls.Add(t);
|
||||
break;
|
||||
case TileKind.Door:
|
||||
Doors.Add(t);
|
||||
break;
|
||||
case TileKind.Decorative:
|
||||
Decorative.Add(t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static TileKind Classify(TileDefinition t, TagInfo info)
|
||||
{
|
||||
// Tag-driven classification (more reliable than DB enum when data is inconsistent).
|
||||
// Priority: door > void > decorative > floor > wall.
|
||||
if (info.Has("door") || info.Has("doorway") || info.Has("connector"))
|
||||
return TileKind.Door;
|
||||
|
||||
if (info.Has("void") || info.Has("empty"))
|
||||
return TileKind.Void;
|
||||
|
||||
if (info.Has("decorative") || info.Has("decor"))
|
||||
return TileKind.Decorative;
|
||||
|
||||
// Your floor tiles consistently include "floor" and/or "walkable"
|
||||
if (info.Has("floor") || info.Has("walkable") || t.IsWalkable)
|
||||
return TileKind.Floor;
|
||||
|
||||
if (info.Has("wall") || info.Has("solid") || info.Has("boundary") || info.Has("trim"))
|
||||
return TileKind.Wall;
|
||||
|
||||
return TileKind.Unknown;
|
||||
}
|
||||
|
||||
public TagInfo Tags(TileDefinition t) => TagById.TryGetValue(t.Id, out var ti) ? ti : TagParser.Parse(t.Tags);
|
||||
|
||||
public bool IsKind(int tileId, TileKind kind) => KindById.TryGetValue(tileId, out var k) && k == kind;
|
||||
|
||||
public IEnumerable<TileDefinition> WallCandidates(bool allowTrim, bool allowVoidBoundary)
|
||||
{
|
||||
// Default: prefer solid walls; optionally allow trim.
|
||||
// "allowVoidBoundary" is off by default; those are the void/boundary wall-ish tiles.
|
||||
var list = new List<TileDefinition>(_solidWalls);
|
||||
if (allowTrim) list.AddRange(_trimWalls);
|
||||
if (allowVoidBoundary) list.AddRange(_voidWalls);
|
||||
return list;
|
||||
}
|
||||
|
||||
public TileDefinition? FindBestFloor(HashSet<string> requiredTokens, string? biome = null)
|
||||
{
|
||||
if (Floors.Count == 0) return null;
|
||||
|
||||
// Hard forbids for floors
|
||||
static bool FloorForbid(TagInfo ti) =>
|
||||
ti.Has("void") || ti.Has("boundary") || ti.Has("door") || ti.Has("doorway") || ti.Has("connector") ||
|
||||
ti.Has("decorative") || ti.Has("trim") || ti.Has("wall");
|
||||
|
||||
TileDefinition? best = null;
|
||||
int bestScore = int.MinValue;
|
||||
|
||||
foreach (var f in Floors)
|
||||
{
|
||||
var ti = Tags(f);
|
||||
|
||||
if (biome != null && ti.TryGet("biome", out var b) && !b.Equals(biome, StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
if (FloorForbid(ti)) continue;
|
||||
|
||||
// Score:
|
||||
// +100 per required token present (but ignore frame/sheet metadata)
|
||||
// +10 for "stone" if required includes "stone" (already handled by required token)
|
||||
// +2 for not being "edge" unless requested
|
||||
int score = 0;
|
||||
|
||||
foreach (var req in requiredTokens)
|
||||
{
|
||||
// ignore metadata-like tokens
|
||||
if (req.StartsWith("frame:", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
if (req.StartsWith("sheet:", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
if (req.StartsWith("biome:", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
|
||||
if (ti.Has(req)) score += 100;
|
||||
else score -= 50; // soft penalty; we still allow partial matches
|
||||
}
|
||||
|
||||
// Prefer "plain" floor tiles unless you asked for edges.
|
||||
if (!requiredTokens.Contains("edge")) score += ti.Has("edge") ? -5 : +2;
|
||||
|
||||
// Prefer walkable
|
||||
if (f.IsWalkable) score += 5;
|
||||
|
||||
if (score > bestScore)
|
||||
{
|
||||
best = f;
|
||||
bestScore = score;
|
||||
}
|
||||
}
|
||||
|
||||
return best ?? Floors[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find the best wall tile matching the requested features.
|
||||
/// Typically you pass: corner_ne + (optional) void + (optional) trim/solid + (optional) north/east/west/south.
|
||||
/// </summary>
|
||||
public TileDefinition? FindBestWall(
|
||||
IEnumerable<string> required,
|
||||
IEnumerable<string>? prefer = null,
|
||||
IEnumerable<string>? forbid = null,
|
||||
bool allowTrim = false,
|
||||
bool allowVoidBoundary = false,
|
||||
string? biome = null)
|
||||
{
|
||||
var req = required?.Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()).ToArray() ?? Array.Empty<string>();
|
||||
var pref = prefer?.Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()).ToArray() ?? Array.Empty<string>();
|
||||
var forb = forbid?.Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()).ToArray() ?? Array.Empty<string>();
|
||||
|
||||
var candidates = WallCandidates(allowTrim, allowVoidBoundary);
|
||||
TileDefinition? best = null;
|
||||
int bestScore = int.MinValue;
|
||||
|
||||
foreach (var w in candidates)
|
||||
{
|
||||
var ti = Tags(w);
|
||||
|
||||
if (biome != null && ti.TryGet("biome", out var b) && !b.Equals(biome, StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
// Never pick doors/decoration as structural walls
|
||||
if (ti.Has("door") || ti.Has("doorway") || ti.Has("connector") || ti.Has("decorative"))
|
||||
continue;
|
||||
|
||||
// Forbid list
|
||||
bool bad = false;
|
||||
foreach (var f in forb)
|
||||
{
|
||||
if (ti.Has(f)) { bad = true; break; }
|
||||
}
|
||||
if (bad) continue;
|
||||
|
||||
int score = 0;
|
||||
|
||||
// Required: strong positive if present, strong negative if missing
|
||||
foreach (var r in req)
|
||||
{
|
||||
if (ti.Has(r)) score += 200;
|
||||
else score -= 200;
|
||||
}
|
||||
|
||||
// Preferred: small bump
|
||||
foreach (var p in pref)
|
||||
{
|
||||
if (ti.Has(p)) score += 20;
|
||||
}
|
||||
|
||||
// Prefer "wall"+"solid" unless trim is explicitly wanted
|
||||
if (!allowTrim) score += ti.Has("trim") ? -50 : +10;
|
||||
score += ti.Has("solid") ? +10 : 0;
|
||||
|
||||
if (score > bestScore)
|
||||
{
|
||||
best = w;
|
||||
bestScore = score;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
public TileDefinition? FindBestDoor(string direction, string? biome = null)
|
||||
{
|
||||
if (Doors.Count == 0) return null;
|
||||
|
||||
var dir = direction.Trim().ToLowerInvariant();
|
||||
TileDefinition? best = null;
|
||||
int bestScore = int.MinValue;
|
||||
|
||||
foreach (var d in Doors)
|
||||
{
|
||||
var ti = Tags(d);
|
||||
|
||||
if (biome != null && ti.TryGet("biome", out var b) && !b.Equals(biome, StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
int score = 0;
|
||||
if (ti.Has("door")) score += 50;
|
||||
if (ti.Has("doorway")) score += 20;
|
||||
if (ti.Has(dir)) score += 40; // north/south etc
|
||||
if (score > bestScore)
|
||||
{
|
||||
best = d;
|
||||
bestScore = score;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user