Example #1
0
    // Use this for initialization
    void Start()
    {
        Node a = new Node("A");
        Node b = new Node("B");
        Node c = new Node("C");
        Node d = new Node("D");
        Node e = new Node("E");
        Node f = new Node("F");

        a.AddOutgoingEdge(new Edge(a, b, 4));
        a.AddOutgoingEdge(new Edge(a, c, 2));
        a.AddOutgoingEdge(new Edge(a, f, 50));
        b.AddOutgoingEdge(new Edge(b, c, 5));
        b.AddOutgoingEdge(new Edge(b, d, 10));
        c.AddOutgoingEdge(new Edge(c, e, 3));
        e.AddOutgoingEdge(new Edge(e, d, 4));
        d.AddOutgoingEdge(new Edge(d, f, 11));

        var pathfinder = new AstarPathfinder <Node>(Heuristic);
        var timer      = new SystemExecutionTimer();
        var path       = pathfinder.FindPath(a, f);

        print(string.Format("In {1} seconds found the following path with cost {0} from A to F:", path.Cost, timer.ElapsedSeconds));
        print(path.Edges.Aggregate("", (soFar, edge) => soFar + (soFar.Length > 0 ? " -> " : "") + edge.ToString()));
    }
Example #2
0
        public HashSet <HotRoute> Run(RoadGraph roadGraph, int eps, int minTraffic)
        {
            this.eps        = eps;
            this.minTraffic = minTraffic;
            this.roadGraph  = roadGraph;

            hotRoutes      = new HashSet <HotRoute>();
            hotRouteStarts = new HashSet <Connection>();

            //var test = roadGraph.Connections.Where(c => c.Traffic.Count() >= minTraffic);
            //Console.WriteLine(test.ToList().Count);

            this.aStart = new AstarPathfinder(this.roadGraph);
            FindHotRouteStarts();
            Console.WriteLine("Return FindHotRouteStars(): " + hotRouteStarts.Count);

            foreach (var hrs in hotRouteStarts)
            {
                var r = new HotRoute(hrs);
                ExtendHotRoutes(ref r);
                hotRoutes.Add(r);
            }

            return(hotRoutes);
        }
Example #3
0
    void SecondPassCorridorsStage()
    {
        List <int>            impassableList = new List <int>();
        Dictionary <int, int> costModList    = new Dictionary <int, int>();

        costModList.Add(0, 1000);
        costModList.Add(1, 10);
        costModList.Add(2, 10);
        costModList.Add(3, 10);
        costModList.Add(4, 10);
        AstarPathfinder pathfinder = new AstarPathfinder(map, costModList, impassableList);

        // Pick random Room
        // Pick Random tile in that room
        UpStairs = BSPMap[0][0][UnityEngine.Random.Range(0, BSPMap[0][0].Count)].segmentRoom.roomCache.position;// GetRandomPointInRoom(BSPMap[0][0][UnityEngine.Random.Range(0, BSPMap[0][0].Count)].segmentRoom);
        // Assign 'Up Stairs' to that tile
        map[UpStairs.x, UpStairs.y] = 3;

        int longestPathLength = 0;

        // loop through all Segments
        for (int i = 0; i < BSPMap[0][0].Count; i++)
        {
            // Ensure there is a path from the 'Up Stairs' to each room
            Vector2Int startNode  = UpStairs;
            Vector2Int targetNode = BSPMap[0][0][i].segmentRoom.roomCache.position;// GetRandomPointInRoom(BSPMap[0][0][i].segmentRoom);

            List <Node> path = pathfinder.StartPathfinder(startNode, targetNode);
            foreach (var node in path)
            {
                // If the path passes through walls
                // change walls to floor tiles
                if (map[node.xPos, node.yPos] == 0)
                {
                    map[node.xPos, node.yPos] = 1;
                }
            }
            if (path.Count > longestPathLength)
            {
                longestPathLength = path.Count;
                // The 'Down Stairs' tile is replaced with the new longestPath target
                DownStairs = new Vector2Int(targetNode.x, targetNode.y);
            }
        }

        // Use longest path from 2nd pass to place 'Down Stairs'
        map[DownStairs.x, DownStairs.y] = 4;
    }
Example #4
0
    // First pass of corridor connections - Pair up a number of the rooms previously created
    // Use A* pathfinding to create a path between each pair.
    #region
    void FirstPassCorridorsStage()
    {
        // Get List of Random indices for segments to use
        // Pairs up all the rooms (currently)
        // Make sure it is a even number
        List <int> segmentIndices = new List <int>();
        int        maxIndex       = BSPMap[0][0].Count;

        segmentIndices = GetRandomIndexList(maxIndex, BSPMap[0][0].Count);

        List <int>            impassableList = new List <int>();
        Dictionary <int, int> costModList    = new Dictionary <int, int>();

        costModList.Add(0, 40);
        costModList.Add(1, 10);
        costModList.Add(2, 10);
        costModList.Add(3, 10);
        costModList.Add(4, 10);
        AstarPathfinder pathfinder = new AstarPathfinder(map, costModList, impassableList);

        // loop through randomly selected segments
        for (int i = 0; i < segmentIndices.Count; i += 2)
        {
            // Start node will be in BSPMap[0][0][i]
            Vector2Int startNode = BSPMap[0][0][segmentIndices[i]].segmentRoom.roomCache.position;
            // Target node will be in BSPMap[0][0][i + 1]
            Vector2Int targetNode = BSPMap[0][0][segmentIndices[i + 1]].segmentRoom.roomCache.position;

            List <Node> path = pathfinder.StartPathfinder(startNode, targetNode);
            foreach (var node in path)
            {
                // If the path passes through walls
                // change walls to floor tiles
                if (map[node.xPos, node.yPos] == 0)
                {
                    map[node.xPos, node.yPos] = 1;
                }
            }
        }
    }