Ejemplo n.º 1
0
        List <PieceCell> pieceLoop(PieceCell cell)
        {
            var cells = new List <PieceCell>();

            var childs = cell.Divide(Random);

            if (childs == null)
            {
                cells.Add(cell);
            }
            else
            {
                foreach (var child in childs)
                {
                    cells.AddRange(pieceLoop(child));
                }
            }

            return(cells);
        }
Ejemplo n.º 2
0
        public                     PieceCell[] Divide(Random random, bool secondTry = false)
        {
            if (PieceAvailable && random.NextDouble() < 0.8f)
            {
                return(null);
            }

            var cellCount = (divideHorizontal ? Size.Y : Size.X) / info.CellSize;

            if (cellCount < 2)
            {
                if (secondTry)
                {
                    return(null);
                }

                divideHorizontal = !divideHorizontal;
                return(Divide(random, true));
            }

            var cells = new PieceCell[2];

            var cells1 = random.Next(cellCount - 1) + 1;
            var cells2 = cellCount - cells1;

            if (divideHorizontal)
            {
                cells[0] = new PieceCell(Position, new MPos(Size.X, cells1 * info.CellSize), info, !divideHorizontal);
                // Remove cell above and the path inbetween
                cells[1] = new PieceCell(Position + new MPos(0, cells1 * info.CellSize), new MPos(Size.X, cells2 * info.CellSize), info, !divideHorizontal);
            }
            else
            {
                cells[0] = new PieceCell(Position, new MPos(cells1 * info.CellSize, Size.Y), info);
                // Remove cell above and the path inbetween
                cells[1] = new PieceCell(Position + new MPos(cells1 * info.CellSize, 0), new MPos(cells2 * info.CellSize, Size.Y), info, !divideHorizontal);
            }

            return(cells);
        }
Ejemplo n.º 3
0
        public override void Generate()
        {
            var spawn = info.FillMap ? MPos.Zero : Center - new MPos(info.MinimumDimensions, info.MinimumDimensions);

            if (spawn.X < 0)
            {
                spawn = new MPos(0, spawn.Y);
            }

            if (spawn.Y < 0)
            {
                spawn = new MPos(spawn.X, 0);
            }

            var bounds = info.FillMap ? Bounds : new MPos(Random.Next(info.MaximumDimensions - info.MinimumDimensions) + info.MinimumDimensions, Random.Next(info.MaximumDimensions - info.MinimumDimensions) + info.MinimumDimensions);

            if (spawn.X + bounds.X >= Bounds.X)
            {
                bounds = new MPos(Bounds.X - spawn.X, bounds.Y);
            }

            if (spawn.Y + bounds.Y >= Bounds.Y)
            {
                bounds = new MPos(bounds.X, Bounds.Y - spawn.Y);
            }

            // If smaller than map bounds, abort
            if (bounds.X < info.MinimumDimensions || bounds.Y < info.MinimumDimensions)
            {
                return;
            }

            var baseCell = new Cell(16, spawn, bounds, info);

            cells = cellLoop(baseCell, 16);

            for (int x = spawn.X; x < spawn.X + bounds.X; x++)
            {
                for (int y = spawn.Y; y < spawn.Y + bounds.Y; y++)
                {
                    road[x, y] = true;
                }
            }

            pieceCells = new List <PieceCell>();
            foreach (var cell in cells)
            {
                var piece = new PieceCell(cell.Position, cell.Size, info);
                pieceCells.AddRange(pieceLoop(piece));
            }

            foreach (var cell in cells)
            {
                if (!Loader.CanAcquireArea(cell.Position, cell.Size, info.ID))
                {
                    continue;
                }

                for (int x = cell.Position.X; x < cell.Position.X + cell.Size.X; x++)
                {
                    for (int y = cell.Position.Y; y < cell.Position.Y + cell.Size.Y; y++)
                    {
                        if (Loader.AcquireCell(new MPos(x, y), info.ID))
                        {
                            UsedCells[x, y] = true;
                        }

                        road[x, y] = false;
                    }
                }
            }

            // Roads
            var type = Loader.Infos.FirstOrDefault(i => i.ID == info.PathGeneratorID && i is PathGeneratorInfo);

            if (type != null)
            {
                for (int x = 0; x < Bounds.X; x++)
                {
                    for (int y = 0; y < Bounds.Y; y++)
                    {
                        if (!(road[x, y] && Loader.AcquireCell(new MPos(x, y), info.PathGeneratorID)))
                        {
                            road[x, y] = false;
                        }
                    }
                }
                var generator = new PathGenerator(Random, Loader, type as PathGeneratorInfo);
                generator.Generate(road);
            }
            // Pieces
            foreach (var piece in pieceCells)
            {
                var size = piece.Size / new MPos(info.CellSize, info.CellSize);

                var fitting = info.Pieces.FirstOrDefault(p => p.Size == size);

                if (fitting == null)
                {
                    continue;
                }

                Loader.GeneratePiece(getPiece(fitting.Pieces), piece.Position, info.ID, idInclusive: true);
            }

            MapPrinter.PrintGeneratorMap(Bounds, NoiseMap.Empty, UsedCells, info.ID);
        }