Exemplo n.º 1
0
        protected void ChangeCell(MazeGenerationResults results, Point point, ICell cell, CellState state, CellDisplayState displayState)
        {
            cell.State        = state;
            cell.DisplayState = displayState;
            var result = new MazeGenerationResult(point, state, displayState);

            results?.Add(result);
        }
        private void A(MazeGenerationResults results, int x, int y, CellState state = CellState.Empty, CellDisplayState displayState = CellDisplayState.Path)
        {
            var point = new Point(x, y);
            //var cell = Map.GetCell(point);
            //cell.State = state;
            //cell.DisplayState = displayState;
            var result = new MazeGenerationResult(point, state, displayState);

            results.Add(result);
        }
Exemplo n.º 3
0
        public override MazeGenerationResults Generate()
        {
            var results = new MazeGenerationResults();

            if (!Map.Infinite)
            {
                // TODO: Ending generation sometimes ends prematurely. Find out why and fix.
                if (RemainingPoints.Count == 0)
                {
                    results.ResultsType = GenerationResultsType.GenerationCompleted;
                    return(results);
                }
            }

            if (CurrentPoint == null)
            {
                CurrentPoint = MazeGenerationUtils.PickStartingPoint(Map, RNG);
                var cell = Map.GetCell(CurrentPoint);

                cell.State        = CellState.Empty;
                cell.DisplayState = CellDisplayState.Path;

                results.Add(new MazeGenerationResult(CurrentPoint, cell.State, cell.DisplayState));
                return(results);
            }

            var doLooping = RNG.NextDouble() < Looping;
            var offsets   = CurrentPoint.GetAxisOffsets();

            Point pathToCellPoint = null;
            Point otherCellPoint  = null;

            while (offsets.Count > 0)
            {
                var offsetIndex = RNG.Next(0, offsets.Count);
                var offset      = offsets[offsetIndex];
                pathToCellPoint = CurrentPoint + offset;
                otherCellPoint  = CurrentPoint + offset * 2;
                var cellExists = Map.CellExists(otherCellPoint);
                if (cellExists)
                {
                    break;
                }
                offsets.RemoveAt(offsetIndex);
            }

            var otherCell  = Map.GetCell(otherCellPoint);
            var pathToCell = Map.GetCell(pathToCellPoint);

            if (doLooping || otherCell.State == CellState.Filled)
            {
                otherCell.State  = CellState.Empty;
                pathToCell.State = CellState.Empty;
            }

            RemainingPoints.Remove(otherCellPoint);

            otherCell.DisplayState  = otherCell.State == CellState.Empty ? CellDisplayState.Path : CellDisplayState.Wall;
            pathToCell.DisplayState = pathToCell.State == CellState.Empty ? CellDisplayState.Path : CellDisplayState.Wall;

            var otherResult = new MazeGenerationResult(otherCellPoint, otherCell.State, otherCell.DisplayState);
            var pathResult  = new MazeGenerationResult(pathToCellPoint, pathToCell.State, pathToCell.DisplayState);

            results.Results.Add(otherResult);
            results.Results.Add(pathResult);

            CurrentPoint = otherCellPoint;

            return(results);
        }
        public override MazeGenerationResults Generate()
        {
            var results = new MazeGenerationResults();

            if (CurrentIteration == 0)
            {
                InitializeMap();
            }

            var treeIndex = CurrentIteration % RunningTrees.Count;
            var tree      = RunningTrees[treeIndex];
            var path      = tree.Path;

            CurrentIteration++;
            if (path.Count == 0)
            {
                return(InitializeTree(path, results, tree));
            }

            var doBreadth              = RNG.NextDouble() < Breadth;
            var doFirstChanceLooping   = RNG.NextDouble() < FirstChanceLooping;
            var doLastChanceLooping    = RNG.NextDouble() < LastChanceLooping;
            var dontGoBackAfterLooping = RNG.NextDouble() < DontGoBackAfterLooping;
            var doBlocking             = RNG.NextDouble() < Blocking;

            var currentCoordinateIndex = doBreadth && path.Count > 1 ? RNG.Next(1, path.Count / Sparseness + 1) * Sparseness : path.Count - 1;
            var currentPoint           = path[currentCoordinateIndex];

            var offsets = currentPoint.GetAxisOffsets();
            var biases  = GetCurrentOffsetProbabilities(tree.LastOffset);

            var lastChanceLooping = false;

            while (offsets.Count > 0)
            {
                var offsetIndex = PickNextDirection(biases, offsets);
                var offset      = offsets[offsetIndex];
                var points      = new List <Point>(Sparseness);
                {
                    for (var i = 1; i <= Sparseness; i++)
                    {
                        var point = currentPoint + (offset * i);
                        points.Add(point);
                    }
                }
                var firstPoint     = points[0];
                var lastPoint      = points[points.Count - 1];
                var lastCellExists = Map.CellExists(lastPoint);
                var testCell       = lastCellExists ? Map.GetCell(lastChanceLooping ? firstPoint : lastPoint) : null;

                if (testCell == null || doBlocking || (!doFirstChanceLooping && testCell.State != CellState.Filled))
                {
                    offsets.RemoveAt(offsetIndex);
                    biases.RemoveAt(offsetIndex);
                    if (!lastChanceLooping && offsets.Count == 0)
                    {
                        offsets           = currentPoint.GetAxisOffsets();
                        biases            = Biases.ToList();
                        lastChanceLooping = true;
                    }
                    continue;
                }

                var cells    = points.Select(x => Map.GetCell(x)).ToList();
                var lastCell = cells[cells.Count - 1];

                var wouldLoop = lastCell.State == CellState.Empty;

                Tree otherTree;
                var  treeJoinForceConnect = CellsTreeDict.TryGetValue(lastPoint, out otherTree) && TreeTree.Connect(tree, otherTree);

                if (wouldLoop)
                {
                    if (!doLastChanceLooping && !treeJoinForceConnect)
                    {
                        break;
                    }
                }

                tree.LastOffset = offset;

                if (!dontGoBackAfterLooping && LastLooped && wouldLoop)
                {
                    // TODO: Fix going back with first chance looping.
                    break;
                }

                if (!treeJoinForceConnect && !wouldLoop)
                {
                    CellsTreeDict.Add(lastPoint, tree);
                }

                LastLooped = wouldLoop;

                for (var i = 0; i < points.Count; i++)
                {
                    var point = points[i];
                    var cell  = cells[i];
                    cell.State = CellState.Empty;
                    if (wouldLoop)
                    {
                        cell.DisplayState = CellDisplayState.Path;
                    }
                    else
                    {
                        cell.DisplayState = CellDisplayState.PathWillReturn;
                        path.Push(point);
                    }
                    var result = new MazeGenerationResult(point, cell.State, cell.DisplayState);
                    results.Add(result);
                }

                return(results);
            }

            if (path.Count <= 1)
            {
                return(CompleteTree(treeIndex, results));
            }

            if (currentCoordinateIndex != 0)
            {
                for (var i = 0; i < Sparseness; i++)
                {
                    var coord = path[currentCoordinateIndex - i];
                    path.RemoveAt(currentCoordinateIndex - i);
                    var lastCell = Map.GetCell(coord);
                    lastCell.DisplayState = CellDisplayState.Path;
                    var lastResult = new MazeGenerationResult(coord, lastCell.State, lastCell.DisplayState);
                    results.Results.Add(lastResult);
                }
            }
            return(results);
        }
        public override MazeGenerationResults Generate()
        {
            var results = new MazeGenerationResults(GenerationResultsType.Success);

            if (Walls.Count == 0)
            {
                results.ResultsType = GenerationResultsType.GenerationCompleted;
                return(results);
            }

            var wall = Walls.First.Value;

            Walls.RemoveFirst();
            var x = 0;
            var y = 0;

            if (wall[0] % 2 == 0 && wall[1] % 2 == 1)
            {
                x = 1;
            }
            else if (wall[0] % 2 == 1 && wall[1] % 2 == 0)
            {
                y = 1;
            }

            var wallA     = new Point(wall[0] - x, wall[1] - y);
            var wallB     = new Point(wall[0] + x, wall[1] + y);
            var connected = Tree.AreConnected(wallA, wallB);

            var loop = RNG.NextDouble() < Looping;

            var wallPoint = new Point(wall[0], wall[1]);
            var wallCell  = Map.GetCell(wallPoint);

            if (loop || !connected)
            {
                var sidePoint1 = new Point(wall[0] - x, wall[1] - y);
                var sidePoint2 = new Point(wall[0] + x, wall[1] + y);


                var sideCell1 = Map.GetCell(sidePoint1);
                var sideCell2 = Map.GetCell(sidePoint2);

                wallCell.State  = CellState.Empty;
                sideCell1.State = CellState.Empty;
                sideCell2.State = CellState.Empty;

                wallCell.DisplayState  = CellDisplayState.Path;
                sideCell1.DisplayState = CellDisplayState.Path;
                sideCell2.DisplayState = CellDisplayState.Path;

                results.Add(new MazeGenerationResult(wallPoint, wallCell.State, wallCell.DisplayState));
                results.Add(new MazeGenerationResult(sidePoint1, sideCell1.State, sideCell1.DisplayState));
                results.Add(new MazeGenerationResult(sidePoint2, sideCell2.State, sideCell2.DisplayState));

                if (!connected)
                {
                    Tree.Connect(wallA, wallB);
                }
            }
            else
            {
                if (ShowAllWallChecking)
                {
                    results.Add(new MazeGenerationResult(wallPoint, wallCell.State, wallCell.DisplayState));
                }
                else
                {
                    return(Generate());
                }
            }

            return(results);
        }