Improve maze start/end placement with dead-end detection

Refactored PlaceStartAndEnd to intelligently place start and end points at dead ends using random selection and distance-based fallbacks. Added EnforceSingleConnection to ensure start/end points have exactly one connection, compatible with U-turn prefabs. Improved MazeReworkSpawner with object hierarchy grouping (Floors, Categories) and made RefreshSingleCell public. Updated maze config and cleaned up scene hierarchy.
This commit is contained in:
2026-07-04 06:51:32 +07:00
parent 6b4a5a8e12
commit 531e28409a
6 changed files with 279 additions and 339 deletions

View File

@@ -129,7 +129,7 @@ namespace Baba_yaga.GameSetup.MazeRework
CarveLoops(grid, rng, width, depth);
// 8. Place Start & End points
PlaceStartAndEnd(grid, rooms, width, depth, forcedStart, forcedDirection, null);
PlaceStartAndEnd(grid, rooms, width, depth, rng, forcedStart, forcedDirection, null);
return grid;
}
@@ -219,7 +219,7 @@ namespace Baba_yaga.GameSetup.MazeRework
// 8. Place Start & End points (no highlighting needed for this step in animation)
currentPhase = MazeAnimationPhase.StartEnd;
PlaceStartAndEnd(grid, rooms, width, depth, forcedStart, forcedDirection, recordNoHighlight);
PlaceStartAndEnd(grid, rooms, width, depth, rng, forcedStart, forcedDirection, recordNoHighlight);
return (grid, history);
}
@@ -469,81 +469,149 @@ namespace Baba_yaga.GameSetup.MazeRework
}
}
private void PlaceStartAndEnd(MazeReworkCellType[,] grid, List<Room> rooms, int width, int depth, Vector2Int? forcedStart, Vector2Int? forcedDirection,
private void PlaceStartAndEnd(MazeReworkCellType[,] grid, List<Room> rooms, int width, int depth, System.Random rng, Vector2Int? forcedStart, Vector2Int? forcedDirection,
Action<int, int, MazeReworkCellType> onCellChanged = null)
{
Vector2Int startPt;
Vector2Int startPt = new Vector2Int(-1, -1);
if (forcedStart.HasValue)
{
startPt = forcedStart.Value;
}
else if (rooms != null && rooms.Count > 0)
{
startPt = rooms[0].GetCenter();
}
else
{
int sx = _config.startLocation.x, sz = _config.startLocation.y;
EnsureValidOddCoordinates(width, depth, ref sx, ref sz);
startPt = new Vector2Int(sx, sz);
startPt = FindDeadEnd(grid, width, depth, rng, new Vector2Int(-1, -1));
if (startPt.x == -1)
{
// Fallback to config start if no corridors exist
int sx = _config.startLocation.x, sz = _config.startLocation.y;
EnsureValidOddCoordinates(width, depth, ref sx, ref sz);
startPt = new Vector2Int(sx, sz);
}
}
grid[startPt.x, startPt.y] = MazeReworkCellType.Start;
onCellChanged?.Invoke(startPt.x, startPt.y, MazeReworkCellType.Start);
Vector2Int endPt = startPt;
if (rooms != null && rooms.Count > 0)
Vector2Int endPt = FindDeadEnd(grid, width, depth, rng, startPt);
if (endPt.x == -1)
{
var endRoom = rooms[rooms.Count - 1];
endPt = endRoom.GetCenter();
if (endPt == startPt && rooms.Count > 1) { endPt = rooms[1].GetCenter(); }
}
else
{
int ex = width - 2, ez = depth - 2;
EnsureValidOddCoordinates(width, depth, ref ex, ref ez);
if (grid[ex, ez] == MazeReworkCellType.Wall)
// Fallback: just pick the furthest corridor cell from startPt
endPt = FindFurthestCorridor(grid, width, depth, startPt);
if (endPt.x == -1)
{
bool found = false;
for (int r = 1; r < Mathf.Max(width, depth) && !found; r++)
for (int ddx = -r; ddx <= r && !found; ddx++)
for (int ddz = -r; ddz <= r && !found; ddz++)
{
int tx = ex + ddx, tz = ez + ddz;
if (tx >= 0 && tx < width && tz >= 0 && tz < depth && IsPathCell(grid[tx, tz]))
{ ex = tx; ez = tz; found = true; }
}
// Extreme fallback
int ex = width - 2, ez = depth - 2;
EnsureValidOddCoordinates(width, depth, ref ex, ref ez);
endPt = new Vector2Int(ex, ez);
}
endPt = new Vector2Int(ex, ez);
}
if (endPt == startPt)
{
// Fallback: find any path cell that isn't the start point, starting from the opposite corner
int ex = 1, ez = 1;
bool found = false;
for (int r = 0; r < Mathf.Max(width, depth) && !found; r++)
{
for (int ddx = -r; ddx <= r && !found; ddx++)
{
for (int ddz = -r; ddz <= r && !found; ddz++)
{
int tx = ex + ddx, tz = ez + ddz;
if (tx >= 0 && tx < width && tz >= 0 && tz < depth && IsPathCell(grid[tx, tz]))
{
if (tx != startPt.x || tz != startPt.y)
{
ex = tx; ez = tz; found = true;
}
}
}
}
}
endPt = new Vector2Int(ex, ez);
}
grid[endPt.x, endPt.y] = MazeReworkCellType.End;
onCellChanged?.Invoke(endPt.x, endPt.y, MazeReworkCellType.End);
// To guarantee they fit the U-turn prefab, enforce a single connection on them
// (this only affects Corridors now, so it will never ruin a Room!)
EnforceSingleConnection(grid, startPt.x, startPt.y, forcedDirection);
EnforceSingleConnection(grid, endPt.x, endPt.y, null);
}
private Vector2Int FindDeadEnd(MazeReworkCellType[,] grid, int width, int depth, System.Random rng, Vector2Int exclude)
{
List<Vector2Int> deadEnds = new List<Vector2Int>();
List<Vector2Int> corridors = new List<Vector2Int>();
for (int z = 1; z < depth - 1; z++)
{
for (int x = 1; x < width - 1; x++)
{
if (grid[x, z] == MazeReworkCellType.Corridor)
{
if (x == exclude.x && z == exclude.y) continue;
corridors.Add(new Vector2Int(x, z));
int connections = 0;
if (IsPathCell(grid[x, z + 1])) connections++;
if (IsPathCell(grid[x + 1, z])) connections++;
if (IsPathCell(grid[x, z - 1])) connections++;
if (IsPathCell(grid[x - 1, z])) connections++;
if (connections == 1)
{
deadEnds.Add(new Vector2Int(x, z));
}
}
}
}
if (deadEnds.Count > 0) return deadEnds[rng.Next(deadEnds.Count)];
if (corridors.Count > 0) return corridors[rng.Next(corridors.Count)];
return new Vector2Int(-1, -1);
}
private Vector2Int FindFurthestCorridor(MazeReworkCellType[,] grid, int width, int depth, Vector2Int from)
{
Vector2Int best = new Vector2Int(-1, -1);
float maxDist = -1f;
for (int z = 1; z < depth - 1; z++)
{
for (int x = 1; x < width - 1; x++)
{
if (grid[x, z] == MazeReworkCellType.Corridor)
{
if (x == from.x && z == from.y) continue;
float dist = Vector2Int.Distance(new Vector2Int(x, z), from);
if (dist > maxDist)
{
maxDist = dist;
best = new Vector2Int(x, z);
}
}
}
}
return best;
}
private void EnforceSingleConnection(MazeReworkCellType[,] grid, int cx, int cz, Vector2Int? forcedDirection)
{
int width = grid.GetLength(0);
int depth = grid.GetLength(1);
Vector2Int[] dirs = new Vector2Int[]
{
new Vector2Int(0, 1),
new Vector2Int(1, 0),
new Vector2Int(0, -1),
new Vector2Int(-1, 0)
};
bool foundFirst = false;
if (forcedDirection.HasValue)
{
int fx = cx + forcedDirection.Value.x;
int fz = cz + forcedDirection.Value.y;
if (fx >= 0 && fx < width && fz >= 0 && fz < depth && IsPathCell(grid[fx, fz]))
{
foundFirst = true;
}
}
foreach (var d in dirs)
{
int nx = cx + d.x;
int nz = cz + d.y;
if (nx >= 0 && nx < width && nz >= 0 && nz < depth && IsPathCell(grid[nx, nz]))
{
if (forcedDirection.HasValue && d == forcedDirection.Value) continue;
if (!foundFirst) foundFirst = true;
else grid[nx, nz] = MazeReworkCellType.Wall;
}
}
}
#endregion

View File

@@ -171,7 +171,7 @@ namespace Baba_yaga.GameSetup.MazeRework
}
}
private void RefreshSingleCell(MazeReworkCellType[,] grid, MazeCellHighlight[,] highlights, int x, int z, int width, int depth, float yOffset, Transform container)
public void RefreshSingleCell(MazeReworkCellType[,] grid, MazeCellHighlight[,] highlights, int x, int z, int width, int depth, float yOffset, Transform container)
{
if (x < 0 || x >= width || z < 0 || z >= depth) return;
@@ -179,6 +179,10 @@ namespace Baba_yaga.GameSetup.MazeRework
MazeReworkCellType type = grid[x, z];
MazeCellHighlight hType = highlights != null ? highlights[x, z] : MazeCellHighlight.None;
Transform rootContainer = container != null ? container : transform;
int floorIndex = Mathf.RoundToInt(yOffset / (stairHeightOffset > 0 ? stairHeightOffset : 5f)); // Approximation for group naming
Transform floorContainer = GetOrCreateGroup(rootContainer, $"Floor_{yOffset}");
// 1. Process Highlights
if (_spawnedHighlights.TryGetValue(pos, out GameObject existingHighlight))
{
@@ -198,8 +202,9 @@ namespace Baba_yaga.GameSetup.MazeRework
if (hPrefab != null)
{
Transform highlightContainer = GetOrCreateGroup(floorContainer, "Highlights");
Vector3 localPos = new Vector3(x * spacing, yOffset, z * spacing);
GameObject spawnedH = Instantiate(hPrefab, container != null ? container : transform);
GameObject spawnedH = Instantiate(hPrefab, highlightContainer);
spawnedH.transform.localPosition = localPos;
spawnedH.name = $"Highlight_{hType}_{x}_{z}";
// Highlights should pop instantly without animation since they are short-lived cursors
@@ -297,8 +302,17 @@ namespace Baba_yaga.GameSetup.MazeRework
else DestroyImmediate(existingData.Instance);
}
// Determine category for clean grouping
string categoryName = "Paths";
if (type == MazeReworkCellType.Start || type == MazeReworkCellType.End || type == MazeReworkCellType.StairsUp || type == MazeReworkCellType.StairsDown)
categoryName = "Special";
else if (isPreviewMode)
categoryName = "Preview";
Transform categoryContainer = GetOrCreateGroup(floorContainer, categoryName);
Vector3 localPosition = new Vector3(x * spacing, yOffset, z * spacing);
GameObject spawnedObj = Instantiate(targetPrefab, container != null ? container : transform);
GameObject spawnedObj = Instantiate(targetPrefab, categoryContainer);
spawnedObj.transform.localPosition = localPosition;
spawnedObj.transform.localRotation = Quaternion.Euler(0f, targetRot, 0f);
spawnedObj.name = $"{targetName}_{x}_{z}";
@@ -307,6 +321,20 @@ namespace Baba_yaga.GameSetup.MazeRework
_spawnedGridCells[pos] = new SpawnedCellData { Instance = spawnedObj, Prefab = targetPrefab, Rotation = targetRot };
}
private Transform GetOrCreateGroup(Transform parent, string groupName)
{
if (parent == null) return null;
Transform group = parent.Find(groupName);
if (group == null)
{
GameObject go = new GameObject(groupName);
go.transform.SetParent(parent, false);
go.transform.localPosition = Vector3.zero;
group = go.transform;
}
return group;
}
private (GameObject, float) GetModularPrefabAndRotation(MazeReworkCellType[,] grid, int x, int z, int width, int depth)
{
bool top = IsPath(grid, x, z + 1, width, depth);