Exemplo 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;
        }
Exemplo n.º 2
0
        // Loads grid from file
        public void LoadFromFile(string filepath)
        {
            // Clear current grid
            for (int i = 0; i < 120; i++)
            {
                for (int j = 0; j < 160; j++)
                {
                    Cells[i, j] = new Cell(this, i, j);
                }
            }

            for (int i = 0; i < 8; i++)
            {
                hardToTraverseCenters[i] = default(Vector2);
            }

            for (int i = 0; i < 10; i++)
            {
                StartGoalPairs[i] = new StartGoalPair();
            }

            // Load grid from file
            try
            {
                // Load all lines from the file
                string[] lines = System.IO.File.ReadAllLines(filepath);
                string[] temp;
                int      xTemp;
                int      yTemp;

                // Get start locations
                temp = lines[0].Split(' ');
                for (int i = 0; i < 10; i++)
                {
                    string[] tempCoord = temp[i].Split(',');
                    xTemp = int.Parse(tempCoord[0]);
                    yTemp = int.Parse(tempCoord[1]);
                    StartGoalPairs[i].Start = new Vector2(xTemp, yTemp);
                    if (i == 0)
                    {
                        Cells[xTemp, yTemp].IsStart = true;
                    }
                }

                // Get goal locations
                temp = lines[1].Split(' ');
                for (int i = 0; i < 10; i++)
                {
                    string[] tempCoord = temp[i].Split(',');
                    xTemp = int.Parse(tempCoord[0]);
                    yTemp = int.Parse(tempCoord[1]);
                    StartGoalPairs[i].Goal = new Vector2(xTemp, yTemp);
                    if (i == 0)
                    {
                        Cells[xTemp, yTemp].IsGoal = true;
                    }
                }

                StartGoalPairIndex = 0;

                // Get 8 centers of hard to traverse areas
                for (int i = 0; i < 8; i++)
                {
                    temp  = lines[i + 2].Split(',');
                    xTemp = int.Parse(temp[0]);
                    yTemp = int.Parse(temp[1]);
                    hardToTraverseCenters[i] = new Vector2(xTemp, yTemp);
                }

                for (int i = 0; i < 120; i++)
                {
                    temp = lines[i + 10].Split(' ');
                    for (int j = 0; j < 160; j++)
                    {
                        switch (temp[j])
                        {
                        case "0":
                            Cells[i, j].TraversalType = Cell.TraversalTypes.BLOCKED;
                            break;

                        case "1":
                            Cells[i, j].TraversalType = Cell.TraversalTypes.REGULAR;
                            break;

                        case "2":
                            Cells[i, j].TraversalType = Cell.TraversalTypes.HARD;
                            break;

                        case "a":
                            Cells[i, j].TraversalType = Cell.TraversalTypes.REGULAR;
                            Cells[i, j].IsHighway     = true;
                            break;

                        case "b":
                            Cells[i, j].TraversalType = Cell.TraversalTypes.HARD;
                            Cells[i, j].IsHighway     = true;
                            break;

                        default:
                            throw new Exception("Grid file is in an invalid format");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(string.Format("Unable to load grid from file{0}{1}", System.Environment.NewLine, e.Message));
            }
        }