Пример #1
0
        public void CalculatePaths(NeighbourMode neighbourMode, Action <object, int, int, int> onCellViewedCallback)
        {
            _pathMatrix = new IList <Vector2Int> [_transitionNodes.Count, _transitionNodes.Count];
            AStarAlgorithm aStarAlgorithm = new AStarAlgorithm();

            aStarAlgorithm.OnCellViewedEvent += onCellViewedCallback;
            CoordinateTransformer transformer = new CoordinateTransformer(this, LeftBottom);

            for (int i = 0; i < _transitionNodes.Count; i++)
            {
                for (int j = i + 1; j < _transitionNodes.Count; j++)
                {
                    var nodeA = _transitionNodes[i];
                    var nodeB = _transitionNodes[j];
                    IList <Vector2Int> path =
                        aStarAlgorithm.GetPath(transformer, nodeA.Position - LeftBottom, nodeB.Position - LeftBottom,
                                               neighbourMode);

                    _pathMatrix[i, j] = path;
                    _pathMatrix[j, i] = Utils.GetInvertedList(path);
                    if (path != null)
                    {
                        nodeA.SetWeight(nodeB, path.Count);
                        nodeB.SetWeight(nodeA, path.Count);
                    }
                }
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.BufferWidth  = 300;
            Console.BufferHeight = 240;
            Console.WindowWidth  = 180;

            Console.WriteLine("Press any key to start...");
            Console.ReadKey();

            FileStream mapFileStream = File.Open("Maps/vadim_maze.cmap", FileMode.Open, FileAccess.Read);

            ConsoleMap map = ConsoleMapGenerator.FromText(mapFileStream, 'S', 'E', 'X', '.');

            _map = map;

            InitArrays(2);

            Vector2Int start = map.DefaultStart;
            Vector2Int stop  = map.DefaultStop;

            _start = start;
            _stop  = stop;
            Vector2Int scale   = new Vector2Int(4, 2);
            int        spacing = 4;


            InitConsoleDrawer(map, scale, spacing, start, stop, 0);
            InitConsoleDrawer(map, scale, spacing, start, stop, 1);

            Console.SetCursorPosition(0, map.Height * scale.Y);

            AStarAlgorithm aStarTest = new AStarAlgorithm();
            var            path      = aStarTest.GetPath(_map, start, stop, NeighbourMode.SideOnly);

            if (path != null)
            {
                Console.WriteLine("Path was found");
                DrawPath(path, 0);
            }

            _contestants[0] = new BestFirstSearch();
            _contestants[0].OnCellViewedEvent += OnCellUpdated;

            _contestants[1] = new AStarAlgorithm();
            _contestants[1].OnCellViewedEvent += OnCellUpdated;

            StartPathFinder(0);
            StartPathFinder(1);

            while (_runningTasks > 0)
            {
                Thread.Sleep(100);
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Пример #3
0
        public void ConnectNode(IWeightedGraphNode <double> node, NeighbourMode neighbourMode)
        {
            AStarAlgorithm        aStarAlgorithm = new AStarAlgorithm();
            CoordinateTransformer transformer    = new CoordinateTransformer(this, LeftBottom);

            foreach (var transitionNode in _transitionNodes)
            {
                IList <Vector2Int> path = aStarAlgorithm.GetPath(transformer, node.Position - LeftBottom,
                                                                 transitionNode.Position - LeftBottom, neighbourMode);
                if (path != null)
                {
                    node.SetWeight(transitionNode, path.Count);
                    transitionNode.SetWeight(node, path.Count);
                    // Other mode will not be connected
                }
            }
        }
Пример #4
0
 public void GeneratePath()
 {
     path = AStarAlgorithm.GetPath(BlueSpawn, redSpawn);
 }
Пример #5
0
        public IList <Vector2Int> GetPath(ICellMap map, Vector2Int start, Vector2Int stop, NeighbourMode neighbourMode)
        {
            if (!_isInitialized)
            {
                Initialize(map);
            }

            CellCluster startContainer = HierarchicalMapGenerator.GetContainingCluster(HierarchicalGraph.ZeroLevelClusters, ClusterSizeZero, ClusterSizeZero, start);
            CellCluster stopContainer  = HierarchicalMapGenerator.GetContainingCluster(HierarchicalGraph.ZeroLevelClusters, ClusterSizeZero, ClusterSizeZero, stop);

            HierarchicalGraphNode startNode = new HierarchicalGraphNode();

            startNode.Position = start;
            startContainer.ConnectNode(startNode, neighbourMode);
            startNode.ParentCluster = startContainer;

            HierarchicalGraphNode stopNode = new HierarchicalGraphNode();

            stopNode.Position = stop;
            stopContainer.ConnectNode(stopNode, neighbourMode);
            stopNode.ParentCluster = stopContainer;

            AStarAlgorithm aStarAlgorithm = new AStarAlgorithm();

            _currentCellCluster = null;
            aStarAlgorithm.OnCellViewedEvent += OnAStarCellViewed;
            IList <IGraphNode> abstractPath = aStarAlgorithm.GetPath(null, startNode, stopNode);
            List <Vector2Int>  path         = null;

            if (abstractPath != null)
            {
                path = new List <Vector2Int>();
                for (var i = 0; i < abstractPath.Count - 1; i++)
                {
                    HierarchicalGraphNode nodeA = (HierarchicalGraphNode)abstractPath[i];
                    HierarchicalGraphNode nodeB = (HierarchicalGraphNode)abstractPath[i + 1];
                    if (nodeA.ParentCluster == nodeB.ParentCluster)
                    {
                        CoordinateTransformer transformer =
                            new CoordinateTransformer(nodeA.ParentCluster, nodeA.ParentCluster.LeftBottom);
                        _currentCellCluster = nodeA.ParentCluster;
                        Vector2Int         clusterStart = nodeA.Position - transformer.Transform;
                        Vector2Int         clusterStop  = nodeB.Position - transformer.Transform;
                        IList <Vector2Int> realPath     = aStarAlgorithm.GetPathSingleLayer(transformer, clusterStart, clusterStop, neighbourMode);

                        if (realPath == null)
                        {
                            var bitmap = CellMapToBitmap.GetBitmap(nodeA.ParentCluster, 16, clusterStart, clusterStop, null);
                            LogManager.Log($"Path in cluster not found. Start: {clusterStart}; Stop: {clusterStop}. Bitmap printed");
                            LogManager.Log(bitmap);
                            throw new InvalidOperationException();
                        }

                        TransformPath(realPath, transformer.Transform);
                        realPath = Utils.GetInvertedList(realPath);
                        path.AddRange(realPath);
                    }
                }
            }

            DestroyConnections(startNode);
            DestroyConnections(stopNode);
            DestroyData(HierarchicalGraph.ZeroLevelClusters);

            return(path);
        }
Пример #6
0
    // Use this for initialization
    void Start()
    {
        var level = new Level(GridSize);

        BSPAlgorithm.Apply(level, BSPAlgorithm.Parameters.Default, 100);

        Tile firstFloor  = null;
        Tile secondFloor = null;

        int count = 0;

        //Random.InitState(563534); // 4-5 ms
        //Random.InitState(10000); //23 ms
        // Random.InitState(2342452);  //31 ms

        Random.InitState((int)System.DateTime.Now.Ticks);
        while (true)
        {
            var tile = level.Grid.Tiles[Random.Range(0, level.Grid.Tiles.GetLength(0) - 1), Random.Range(0, level.Grid.Tiles.GetLength(1) - 1)];
            if (tile.Type == Tile.Types.Floor)
            {
                firstFloor = tile;
                break;
            }
            count++;

            if (count == 1000)
            {
                break;
            }
        }

        count = 0;
        while (true)
        {
            var tile = level.Grid.Tiles[Random.Range(0, level.Grid.Tiles.GetLength(0) - 1), Random.Range(0, level.Grid.Tiles.GetLength(1) - 1)];

            if (tile.Type == Tile.Types.Floor)
            {
                secondFloor = tile;
                break;
            }

            count++;

            if (count == 1000)
            {
                break;
            }
        }

        AStarAlgorithm pathfinding = new AStarAlgorithm();

        var path = pathfinding.GetPath(firstFloor, secondFloor);

        foreach (var item in path)
        {
            //item.GetToNode.Node.attached.GetComponent<SpriteRenderer>().color = Color.red;
        }
        render.Init(level.Grid.Tiles, GridSize);
    }