예제 #1
0
        /**
         * Make some progress in the WaveFunctionCollapseAlgorithm
         */
        public Resolution Step()
        {
            if (status != Resolution.Undecided)
            {
                return(status);
            }

            if (backtrack)
            {
                prevWaves.Push(wave.Clone());
                prevCompatible.Push((int[, , ])compatible.Clone());
            }

            int index, pattern;

            Observe(out index, out pattern);

            if (backtrack)
            {
                prevChoices.Push(new PropagateItem {
                    Index = index, Pattern = pattern
                });
            }

restart:

            if (status == Resolution.Undecided)
            {
                status = Propagate();
            }
            if (status == Resolution.Undecided)
            {
                status = StepConstraints();
            }

            if (backtrack && status == Resolution.Contradiction)
            {
                // Actually backtrack
                while (true)
                {
                    if (prevWaves.Count == 0)
                    {
                        // We've backtracked as much as we can, but
                        // it's still not possible. That means it is imposible
                        return(Resolution.Contradiction);
                    }
                    wave       = prevWaves.Pop();
                    compatible = prevCompatible.Pop();
                    var item = prevChoices.Pop();
                    backtrackCount++;
                    toPropagate.Clear();
                    // Mark the given choice as impossible
                    if (InternalBan(item.Index, item.Pattern))
                    {
                        // Still in contradiction, need to backtrack further
                        continue;
                    }
                    status = Resolution.Undecided;
                    goto restart;
                }
            }

            return(status);
        }