Esempio n. 1
0
        public void RandomlyGenerateGrid()
        {
            /// TODO: Delete this
            GlobalLogger.ClearLog();

            // Create cell array
            for (int i = 0; i < 120; i++)
            {
                for (int j = 0; j < 160; j++)
                {
                    Cells[i, j] = new Cell(this, i, j);
                }
            }

            // Create 8 unique random locations
            Random  random = new Random(System.Guid.NewGuid().GetHashCode());
            Vector2 temp;
            int     numLoc = 0;

            while (numLoc < 8)
            {
                temp = new Vector2(random.Next(0, 120), random.Next(0, 160));
                if (!hardToTraverseCenters.Contains(temp))
                {
                    hardToTraverseCenters[numLoc++] = temp;
                }
            }

            // Populate with a 50/50 ratio, hard to traverse and regular to traverse cells
            foreach (Vector2 location in hardToTraverseCenters)
            {
                for (int i = (int)location.X - 15; i < (int)location.X + 15; i++)
                {
                    for (int j = (int)location.Y - 15; j < (int)location.Y + 15; j++)
                    {
                        if (i > -1 && i < 120 && j > -1 && j < 160)
                        {
                            if (random.NextDouble() > 0.49f)
                            {
                                Cells[i, j].TraversalType = Cell.TraversalTypes.HARD;
                            }
                        }
                    }
                }
            }

            // Select a random cell along the boundary
            int numHighwaysCreated = 0;

            while (numHighwaysCreated < 4)
            {
                //GlobalLogger.Log(string.Format("{0}Creating highway: {1}{0}", System.Environment.NewLine, numHighwaysCreated + 1));

                double edgeRand = random.NextDouble();

                //GlobalLogger.Log(string.Format("Random number chosen for highway start direction: {0}", edgeRand));

                Vector2?          startCoord     = null;
                HighwayDirections?startDirection = null;

                if (edgeRand < 0.25f)
                {
                    //GlobalLogger.Log("Highway start direction: Down");
                    startDirection = HighwayDirections.DOWN;
                    startCoord     = new Vector2(0, random.Next(1, 159));
                }
                else if (edgeRand < 0.50f)
                {
                    //GlobalLogger.Log("Highway start direction: Left");
                    startDirection = HighwayDirections.LEFT;
                    startCoord     = new Vector2(random.Next(1, 119), 159);
                }
                else if (edgeRand < 0.75f)
                {
                    //GlobalLogger.Log("Highway start direction: Up");
                    startDirection = HighwayDirections.UP;
                    startCoord     = new Vector2(119, random.Next(1, 159));
                }
                else
                {
                    //GlobalLogger.Log("Highway start direction: Right");
                    startDirection = HighwayDirections.RIGHT;
                    startCoord     = new Vector2(random.Next(1, 119), 0);
                }

                if (CreateHighway(startCoord.Value, startDirection.Value))
                {
                    numHighwaysCreated++;
                }

                //GlobalLogger.Log(string.Format("{0}HIGHWAY FAILED{0}", System.Environment.NewLine));
            }

            // Flag 20% of all cells as blocked as long as they are not a highway
            int numBlockedCells = 0;

            while (numBlockedCells < 3840)
            {
                int x = random.Next(0, 120);
                int y = random.Next(0, 160);

                if (!Cells[x, y].IsHighway)
                {
                    Cells[x, y].TraversalType = Cell.TraversalTypes.BLOCKED;
                    numBlockedCells++;
                }
            }

            // Choose 10 start/goal locations
            int  a     = 0;
            bool found = false;
            Cell tempCell;

            while (a < 10)
            {
                StartGoalPair sgpair = new StartGoalPair();

                // get start
                List <Vector2> borderCells = GetBorderCells(borderThickness: 20);
                found = false;
                do
                {
                    int     rand   = random.Next(0, borderCells.Count);
                    Vector2 startV = borderCells[rand];
                    tempCell = Cells[(int)startV.X, (int)startV.Y];

                    if (tempCell.TraversalType != Cell.TraversalTypes.BLOCKED)
                    {
                        sgpair.Start = startV;
                        found        = true;
                    }
                } while (!found);

                // get goal
                found = false;
                do
                {
                    int     rand  = random.Next(0, borderCells.Count);
                    Vector2 goalV = borderCells[rand];
                    tempCell = Cells[(int)goalV.X, (int)goalV.Y];

                    if (tempCell.TraversalType != Cell.TraversalTypes.BLOCKED &&
                        CalculateDistance(goalV, sgpair.Start) > 100)
                    {
                        sgpair.Goal = goalV;
                        found       = true;
                    }
                } while (!found);

                if (!StartGoalPairs.Contains(sgpair))
                {
                    StartGoalPairs[a] = sgpair;
                    a++;
                }
            }

            StartGoalPairIndex = 0;
        }