Esempio n. 1
0
    public Dictionary <Coordinates, Coordinates> SearchThroughCorridors(InverseGraph graph, Coordinates start, Coordinates end)
    {
        var frontier = new Priority_Queue.FastPriorityQueue <Coordinates>(10000);

        frontier.Enqueue(start, 0);
        cameFrom[start]  = start;
        costSoFar[start] = 0;
        while (frontier.Count > 0)
        {
            var current = frontier.Dequeue();
            if (current == end)
            {
                this.end = current;
                break;
            }
            foreach (var next in graph.Neighbors(current))
            {
                float newCost = costSoFar[current] + graph.Cost(next);
                if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next])
                {
                    costSoFar[next] = newCost;
                    float priority = newCost + Heuristic(next, end);
                    frontier.Enqueue(next, priority);
                    cameFrom[next] = current;
                }
            }
        }
        return(cameFrom);
    }
Esempio n. 2
0
    private bool WalkablePathBetweenPoints(int floor, Vector2 start, Vector2 end)
    {
        InverseGraph g           = new InverseGraph(floor, building.maxDimensions, building.maxDimensions, building.grid);
        Astar        astar       = new Astar();
        var          startCoords = new Coordinates((int)start.x, (int)start.y);
        var          endCoords   = new Coordinates((int)end.x, (int)end.y);
        var          values      = astar.SearchThroughCorridors(g, startCoords, endCoords);
        var          chain       = new List <Coordinates>();
        var          cursor      = endCoords;

        while (values.ContainsKey(cursor) && values[cursor] != cursor)
        {
            chain.Add(cursor);
            cursor = values[cursor];
        }
        if (values.ContainsKey(cursor))
        {
            chain.Add(values[cursor]);
        }
        if (chain.Count > 0)
        {
            chain.Reverse();
        }
        if (chain.Count == 0 || chain[0] != startCoords)
        {
            return(false);
        }
        foreach (var entry in chain)
        {
            if (entry.x < 0 || entry.y < 0 || entry.x >= 120 || entry.y >= 120)
            {
                return(false);
            }
        }
        return(true);
    }