From 07cdd0a46f0338fe20784bf275dd765317c98bdc Mon Sep 17 00:00:00 2001 From: Nicola Date: Mon, 11 May 2026 21:23:51 +0200 Subject: [PATCH] Changed WFC acceptance to 100% connectivity and added test case to test the layer generation. --- Scripts/Tests/TestRunner.cs | 60 +++++++++++++++++++++++++++++++++++++ Scripts/World/Layer.cs | 2 +- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/Scripts/Tests/TestRunner.cs b/Scripts/Tests/TestRunner.cs index c878239..fe5d7e9 100644 --- a/Scripts/Tests/TestRunner.cs +++ b/Scripts/Tests/TestRunner.cs @@ -37,6 +37,7 @@ public partial class TestRunner : Node Run("For node stops after configured amount", TestForNodeStopsAfterConfiguredAmount); Run("Paused world does not drain survival", TestPausedWorldDoesNotDrainSurvival); Run("Open gate hides gate content", TestOpenGateHidesGateContent); + Run("Layer generation succeeds above threshold", TestLayerGenerationSuccessRate); Run("Item data readable names are stable", TestItemDataReadableNames); Run("Resource files load core game data", TestResourceFilesLoadCoreData); @@ -502,6 +503,40 @@ public partial class TestRunner : Node AssertFalse(layer.tiles[0, 0].ContentNode.Visible, "gate content should be hidden"); } + private void TestLayerGenerationSuccessRate() + { + const int layerCount = 1000; + const float minimumSuccessRate = 0.90f; + + GameData.layerSize = 20; + WFC.FillAdjacencies(); + Dictionary tileMeshes = ResourceLoader.LoadTiles(); + + int successfulLayers = 0; + + for (int i = 0; i < layerCount; i++) + { + Layer layer = new Layer(); + layer._Ready(); + layer.SetupLayer(GameData.layerSize, 0, tileMeshes, new Vector2I()); + + if (IsGeneratedLayerValid(layer)) + { + successfulLayers++; + } + + layer.Free(); + } + + float successRate = successfulLayers / (float)layerCount; + GD.Print($"Layer generation success rate: {successfulLayers}/{layerCount} ({successRate:P2})"); + + AssertTrue( + successRate >= minimumSuccessRate, + $"layer generation success rate should be at least {minimumSuccessRate:P0}, got {successRate:P2}" + ); + } + private void TestItemDataReadableNames() { AssertEqual("Iron gear", ItemData.GetReadableName("iron_gear"), "readable name"); @@ -539,6 +574,31 @@ public partial class TestRunner : Node return layer; } + private bool IsGeneratedLayerValid(Layer layer) + { + bool hasGate = false; + + foreach (Tile tile in layer.tiles) + { + if (tile.collapsedMesh == null) + { + return false; + } + + if (tile.collapsedMesh == "gate") + { + hasGate = true; + } + } + + if (!hasGate) + { + return false; + } + + return WFC.IsMapConnected(layer.tiles, 1f); + } + private void AssertTrue(bool value, string message) { if (!value) diff --git a/Scripts/World/Layer.cs b/Scripts/World/Layer.cs index edb0730..768d713 100644 --- a/Scripts/World/Layer.cs +++ b/Scripts/World/Layer.cs @@ -198,7 +198,7 @@ public partial class Layer : Node3D if (safetyCounter == layerSize * layerSize) return false; } if (updateFailed) return false; - if (!WFC.IsMapConnected(tiles, 0.8f)) return false; + if (!WFC.IsMapConnected(tiles, 1f)) return false; return true; }