Esempio n. 1
0
        private void FindPath()
        {
            // Basic
            // PathFinderRectGrid2D pathFinder = new PathFinderRectGrid2D(map);

            // Generic
            PFRG2D_Generic pathFinder = new PFRG2D_Generic(map);

            t_start = Time.realtimeSinceStartup;
            path    = pathFinder.FindPath(startX, startY, endX, endY);
            t_end   = Time.realtimeSinceStartup;
        }
Esempio n. 2
0
        private void OnGUI()
        {
            if (GUI.Button(new Rect(5, 5, 256, 32), "New map"))
            {
                GenerateMap(mapWidth, mapHeight, passabilityRate);
                path   = null;
                startX = startY = 0;
                endX   = mapWidth - 1;
                endY   = mapHeight - 1;
                FindPath();
            }

            if (map != null)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    for (int x = 0; x < mapWidth; x++)
                    {
                        GUI.DrawTexture(new Rect(100 + x * (gridPixelSize + gridGap), 100 + y * (gridPixelSize + gridGap), gridPixelSize, gridPixelSize), tex[map[x, y] ? 0 : 1]);
                    }
                }
                if (path != null && path.Path.Count > 0)
                {
                    foreach (PathNode2D node in path.Path)
                    {
                        GUI.DrawTexture(new Rect(100 + node.X * (gridPixelSize + gridGap), 100 + node.Y * (gridPixelSize + gridGap), gridPixelSize, gridPixelSize), tex[2]);
                    }
                    GUI.Label(new Rect(5, 60, 512, 32), "time elapsed: " + ((t_end - t_start) * 1000).ToString() + " ms");
                }
                if (Input.GetMouseButtonDown(0) && IsMouseInsideMapBounds())
                {
                    float mouseY = Screen.height - Input.mousePosition.y;
                    startX = (int)(Input.mousePosition.x - 100) / (gridPixelSize + gridGap);
                    startY = (int)(mouseY - 100) / (gridPixelSize + gridGap);
                    FindPath();
                }
                if (Input.GetMouseButtonDown(1) && IsMouseInsideMapBounds())
                {
                    float mouseY = Screen.height - Input.mousePosition.y;
                    endX = (int)(Input.mousePosition.x - 100) / (gridPixelSize + gridGap);
                    endY = (int)(mouseY - 100) / (gridPixelSize + gridGap);
                    FindPath();
                }
            }
            GUI.Label(new Rect(5, 40, 512, 32), "startX: " + startX + ", startY: " + startY + ", endX: " + endX + ", endY: " + endY);
        }
Esempio n. 3
0
        private static void TestPathFinderRectGrid2D(int mapMode, int testMode, int mx = 100, int my = 25, int passabilityRate = 5)
        {
            Random random = new Random();

            Console.BufferWidth  = Console.WindowWidth = mx + 1;
            Console.BufferHeight = Console.WindowHeight = my + 10;

            bool[,] map = new bool[mx, my];
            for (int y = 0; y < my; y++)
            {
                for (int x = 0; x < mx; x++)
                {
                    map[x, y] = random.Next(0, passabilityRate) > 0;
                }
            }

            switch (mapMode)
            {
            // Adding an almost full-length horizontal delimiter with one random gap
            case 1:
                int x = random.Next(0, mx);
                int y = random.Next(my / 4, my / 4 * 3 + 1);
                for (int i = 0; i < mx; i++)
                {
                    map[i, y] = x == i;
                }
                break;

            // Adding two delimiters
            case 2:
                int x1 = random.Next(0, mx);
                int x2 = random.Next(0, mx);
                int y1 = random.Next(my / 4, my / 2);
                int y2 = random.Next(y1 + 1, my / 4 * 3 + 1);
                for (int i = 0; i < mx; i++)
                {
                    map[i, y1] = x1 == i; map[i, y2] = x2 == i;
                }
                break;
            }

            PrintMap(map);
            Console.WriteLine();

            PathFinderRectGrid2D pf;
            GridPath2D           path = null;

            Stopwatch sw = new Stopwatch();

            switch (testMode)
            {
            // Only straight directions
            case 0:
                pf   = new PathFinderRectGrid2D(map);
                path = pf.FindPath(0, 0, mx - 1, my - 1, true, false);
                break;

            // All 8 directions
            case 1:
                pf   = new PathFinderRectGrid2D(map);
                path = pf.FindPath(0, 0, mx - 1, my - 1, true, true);
                break;

            // Example special usage:
            // -- eliminate zig-zagging by weighting moves based on being diagonal or not, and pre-sorting the open stack;
            // -- only allow diagonal movement if not blocked by a cornering wall.
            // Since we do not differentiate between the "cost" of a straight and a diagonal move, this will result in the pathfinder moving diagonally just before it would "hit" an obstacle,
            // to save the one extra move that would arise from not being able to move diagonally to evade the obstacle when it's next to it.
            case 2:
                // pf = new ExampleCustomPathFinder(map);
                pf = new PFRG2D_Generic(map);
                PathFinderRectGrid2D.Options options = new PathFinderRectGrid2D.Options()
                {
                    enableOpenStackSorting = false             // Not used anymore in this example since sorting-free anti-zigzag mode has been implemented.
                };
                Dictionary <string, int> passConditions = new Dictionary <string, int> {
                    { "diagonalBlocking", 1 }
                };
                sw.Start();
                path = pf.FindPath(0, 0, mx - 1, my - 1, true, true, options, null, passConditions, null);
                sw.Stop();
                break;
            }

            if (path != null && path.Path.Count > 0)
            {
                DrawPath(path.Path);
                Console.SetCursorPosition(0, my);
                Console.WriteLine();
                Console.WriteLine("Path found in " + sw.ElapsedMilliseconds + " ms (" + sw.ElapsedTicks + " ticks).");
            }
            else
            {
                Console.WriteLine("No path found!");
            }
        }