From 67d6efb9dda9d926730df4ea625def1e8c43b234 Mon Sep 17 00:00:00 2001 From: Connor De Meyer Date: Mon, 25 Aug 2025 07:35:38 +0900 Subject: [PATCH] branch bug fix --- include/nd-wfc/wfc.hpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/include/nd-wfc/wfc.hpp b/include/nd-wfc/wfc.hpp index 9136c1c..bd525c7 100644 --- a/include/nd-wfc/wfc.hpp +++ b/include/nd-wfc/wfc.hpp @@ -212,12 +212,12 @@ public: private: void ApplyMask(size_t cellId, MaskType mask) { - if (m_wave.IsCollapsed(cellId)) return; + bool wasCollapsed = m_wave.IsCollapsed(cellId); m_wave.Collapse(cellId, mask); - assert(!m_wave.HasContradiction() && "Contradiction found"); - if (m_wave.IsCollapsed(cellId)) { + bool collapsed = m_wave.IsCollapsed(cellId); + if (!wasCollapsed && collapsed) { m_propagationQueue.push(cellId); } } @@ -277,7 +277,7 @@ public: bool Run(WorldT& world, bool propagateInitialValues = false) { //auto seed = std::random_device{}(); - auto seed = 1844803044ull; + auto seed = 1844803044ul; std::mt19937 random{ seed }; SolverState state(world, m_variables.size(), random); return Run(state, propagateInitialValues); @@ -307,12 +307,11 @@ public: constexpr size_t maxIterations = 1024; for (size_t i = 0; i < maxIterations; ++i) { - Propagate(state); + if (!Propagate(state)) + return false; if (state.wave.IsFullyCollapsed()) { return true; - } else if (state.wave.HasContradiction()) { - return false; } else { Branch(state); } @@ -414,19 +413,22 @@ private: return false; } - void Propagate(SolverState& state) + bool Propagate(SolverState& state) { while (!state.propagationQueue.empty()) { size_t cellId = state.propagationQueue.front(); state.propagationQueue.pop(); + if (state.wave.IsContradicted(cellId)) return false; + assert(state.wave.IsCollapsed(cellId) && "Cell was not collapsed"); uint16_t variableID = state.wave.GetVariableID(cellId); Constrainer constrainer(state.wave, state.propagationQueue); m_variables[variableID].constraintFunc(state.world, cellId, WorldValue{VariableIDMapT::GetValue(variableID), variableID}, constrainer); } + return true; } void PopulateWorld(SolverState& state)