int[] findRoute(int roomFrom, int roomTo)
    {
        BuildingGraph graph = building.GetComponent <BuildingGraph>();

        int[]  vertShortestAccess    = new int[graph.vertices.Length];
        bool[] vertPotentiallyViable = new bool[graph.vertices.Length]; //is set to false when all connecting verts return an unsuccessful path
        for (int n = graph.vertices.Length - 1; n >= 0; n--)
        {
            vertShortestAccess[n]    = int.MaxValue; //Still calculated in Vertex-passes, not actual physical distance
            vertPotentiallyViable[n] = true;
        }

        List <int> l = new List <int>();

        l.Add(roomFrom);

        List <int> routeList = routeRun(graph, l, roomTo, vertShortestAccess, vertPotentiallyViable);
        string     s         = arrayToString(routeList.ToArray());

        Debug.Log("ROute is: " + s);

        if (routeList != null)
        {
            int[] routeListArray = routeList.ToArray();
            return(routeListArray);
        }
        else
        {
            return(null);
        }
    }
Esempio n. 2
0
 // Use this for initialization
 void Start()
 {
     if (building == null)
     {
         building = GameObject.Find("Building").GetComponent <BuildingGraph>();
     }
 }
    List <int> routeRun(BuildingGraph graph, List <int> route, int roomTo, int[] vertShortestAccess, bool[] viability)
    {
        BuildingVertex v = graph.vertices[route[route.Count - 1]];

        vertShortestAccess[v.id] = route.Count;
        for (int n = 0; n < v.floorIDs.Length; n++)
        {
            if (graph.floorsRoomIds[v.floorIDs[n]] == roomTo) //If in target room
            {
                return(route);
            }
        }

        List <int>[] lists = new List <int> [v.accessibleVertices.Length];

        for (int n = 0; n < v.accessibleVertices.Length; n++)
        {
            if (vertShortestAccess[v.accessibleVertices[n]] > route.Count - 1 && viability[v.accessibleVertices[n]])
            {
                List <int> copy = listCopy(route);
                copy.Add(v.accessibleVertices[n]);
                lists[n] = routeRun(graph, copy, roomTo, vertShortestAccess, viability);
            }
            else
            {
                lists[n] = null;
            }
        }

        int shortestLength = int.MaxValue;
        int shortestIndex  = -1;

        for (int n = 0; n < lists.Length; n++)
        {
            List <int> rCurrent = lists[n];
            if (rCurrent != null)
            {
                if (rCurrent.Count < shortestLength)
                {
                    shortestIndex  = n;
                    shortestLength = rCurrent.Count;
                }
            }
        }
        if (shortestIndex >= 0)
        {
            return(lists[shortestIndex]);
        }
        else
        {
            viability[v.id] = false; //DONT GO HERE EVER AGAIN!
            return(null);
        }
    }
    Vector3[] routeToPositions(int[] route, Vector3 start)
    {
        Vector3[] positions = new Vector3[route.Length + 1];
        positions[0] = start;

        BuildingGraph graph = building.GetComponent <BuildingGraph>();


        for (int n = 0; n < route.Length; n++)
        {
            positions[n + 1] = graph.vertices[route[n]].position;
        }

        return(positions);
    }
    int findVertex(int floor)
    {
        BuildingGraph graph = building.GetComponent <BuildingGraph>();

        for (int n = 0; n < graph.vertices.Length; n++)
        {
            BuildingVertex v = graph.vertices[n];

            for (int n2 = 0; n2 < v.floorIDs.Length; n2++)
            {
                if (v.floorIDs[n2] == floor)
                {
                    return(n);
                }
            }
        }
        return(-1);
    }