Example #1
0
 private GameArea(int width, int height)
 {
     tiles  = new GameTile[width, height];
     Width  = width;
     Height = height;
 }
Example #2
0
        /// <summary>
        /// Generates new room
        /// </summary>
        /// <param name="rnd">Random generator to use</param>
        /// <param name="walls">Bit mask for determining where generator must provide walls and where not.</param>
        /// <returns></returns>
        public static GameArea Generate(Random rnd, GeneratorParameters parameters)
        {
            int   initialChance = parameters.InitialChance;
            Walls walls         = parameters.Walls;

            int w = parameters.Width, h = parameters.Height;

            log.Info("Generaing new Area...");
            bool[,] sp = new bool[w, h], dp = new bool[w, h];
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    sp[i, j] = rnd.Next(1000) < initialChance;
                }
            }

            bool[,] mask = new bool[w, h];
            int wallWidth      = parameters.WallWidth;
            int maskPercentage = parameters.MaskChance;

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    if ((walls & Walls.North) != Walls.None && j < wallWidth)
                    {
                        if (j == 0 || rnd.Next(100) < maskPercentage)
                        {
                            mask[i, j] = true;
                        }
                    }
                    if ((walls & Walls.East) != Walls.None && i > w - 1 - wallWidth)
                    {
                        if (i == w - 1 || rnd.Next(100) < maskPercentage)
                        {
                            mask[i, j] = true;
                        }
                    }
                    if ((walls & Walls.South) != Walls.None && j > h - 1 - wallWidth)
                    {
                        if (j == h - 1 || rnd.Next(100) < maskPercentage)
                        {
                            mask[i, j] = true;
                        }
                    }
                    if ((walls & Walls.West) != Walls.None && i < wallWidth)
                    {
                        if (i == 0 || rnd.Next(100) < maskPercentage)
                        {
                            mask[i, j] = true;
                        }
                    }
                }
            }

            //var inp = w / 2 - 2;
            for (int i = 0; i < parameters.PassCount; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    for (int k = 0; k < h; k++)
                    {
                        int nwc = GetNeighbourgs(new Vector2(j, k)).Select((_) =>
                                                                           GetTileFromBoolMap(sp, _, parameters.WallForcing ? walls : Walls.All)).Where((_) => _).Count();
                        if (nwc >= 5 || (parameters.Smoothing && i == parameters.PassCount - 1 && (nwc >= 2)))
                        {
                            dp[j, k] = true;
                        }
                        else
                        {
                            dp[j, k] = false;
                        }
                    }
                }
                if (i != parameters.PassCount - 1)
                {
                    for (int j = 0; j < w; j++)
                    {
                        for (int k = 0; k < h; k++)
                        {
                            dp[j, k] |= mask[j, k];
                        }
                    }
                }
                sp = dp;
                dp = new bool[w, h];
            }

            var r = new GameArea(w, h);

            int wallNumber = 0;

            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    bool wall = sp[i, j];
                    if (wall)
                    {
                        wallNumber++;
                    }

                    r[i, j] = new GameTile(wall);
                }
            }

            log.DebugFormat("wallRatio: {0}", (double)wallNumber / (w * h));

            /*
             * int c = 12+rnd.Next(4);
             * for (int i = 0; i < c; i++)
             * {
             * Vector2 p = new Vector2(rnd.Next(r.Width), rnd.Next(r.Height));
             * int rad = 2 + rnd.Next(10);
             * foreach (var t in r.EnumTiles().Where((_) => (p - _).Length() <= rad))
             * r[t] = new GameTile(true);
             * }*/

            log.Info("Done generating");
            return(r);
        }