Exemplo n.º 1
0
 public GatewayHeuristic()
 {
     //AssetDatabase.LoadAssetAtPath("Assets/Textures/texture.jpg")
     //this.graph = (ClusterGraph) AssetDatabase.LoadAssetAtPath("Assets/Resources/ClusterGraph.asset", typeof(ClusterGraph));
     //Resources.Load("ClusterGraph");
     this.graph = Resources.Load <ClusterGraph>("ClusterGraph");
 }
Exemplo n.º 2
0
    private static void CreateClusterGraph()
    {
        Cluster cluster;
        Gateway gateway;

        //get cluster game objects
        var clusters = GameObject.FindGameObjectsWithTag("Cluster");
        //get gateway game objects
        var gateways = GameObject.FindGameObjectsWithTag("Gateway");
        //get the NavMeshGraph from the current scene
        NavMeshPathGraph navMesh = GameObject.Find("Navigation Mesh").GetComponent <NavMeshRig>().NavMesh.Graph;

        ClusterGraph clusterGraph = ScriptableObject.CreateInstance <ClusterGraph>();

        //create gateway instances for each gateway game object
        for (int i = 0; i < gateways.Length; i++)
        {
            var gatewayGO = gateways[i];
            gateway = ScriptableObject.CreateInstance <Gateway>();
            gateway.Initialize(i, gatewayGO);
            clusterGraph.gateways.Add(gateway);
        }

        //create cluster instances for each cluster game object and check for connections through gateways
        foreach (var clusterGO in clusters)
        {
            cluster = ScriptableObject.CreateInstance <Cluster>();
            cluster.Initialize(clusterGO);
            clusterGraph.clusters.Add(cluster);

            //determine intersection between cluster and gateways and add connections when they intersect
            foreach (var gate in clusterGraph.gateways)
            {
                if (MathHelper.BoundingBoxIntersection(cluster.min, cluster.max, gate.min, gate.max))
                {
                    cluster.gateways.Add(gate);
                    gate.clusters.Add(cluster);
                }
            }
        }

        // Second stage of the algorithm, calculation of the Gateway table

        GlobalPath solution = null;
        //float cost;
        Gateway startGate;
        Gateway endGate;

        var pathfindingAlgorithm = new NodeArrayAStarPathFinding(navMesh, new EuclideanDistanceHeuristic());

        clusterGraph.gatewayDistanceTable = new GatewayDistanceTableRow[clusterGraph.gateways.Count];

        for (int i = 0; i < clusterGraph.gateways.Count; i++)
        {
            var row = new GatewayDistanceTableRow();
            row.entries = new GatewayDistanceTableEntry[clusterGraph.gateways.Count];
            startGate   = clusterGraph.gateways[i];
            for (int j = 0; j < clusterGraph.gateways.Count; j++)
            {
                endGate = clusterGraph.gateways[j];

                row.entries[j] = new GatewayDistanceTableEntry()
                {
                    startGatewayPosition = startGate.Localize(),
                    endGatewayPosition   = endGate.Localize()
                };

                if (i != j)
                {
                    pathfindingAlgorithm.InitializePathfindingSearch(startGate.Localize(), endGate.Localize());
                    bool finished = pathfindingAlgorithm.Search(out solution);
                    if (finished && solution != null)
                    {
                        row.entries[j].shortestDistance = solution.Length;
                    }
                    else
                    {
                        row.entries[j].shortestDistance = float.MaxValue;
                    }
                }
                else
                {
                    row.entries[j].shortestDistance = 0.0f;
                }
            }
            clusterGraph.gatewayDistanceTable[i] = row;
        }

        //Debug.Log("Distance table with: " + clusterGraph.gatewayDistanceTable.Length + " rows and " + clusterGraph.gatewayDistanceTable[0].entries.Length + " columns.");
        //string print = "[";
        //for (int i = 0; i < clusterGraph.gatewayDistanceTable.Length; i++)
        //{
        //    print += "[";
        //    for(int j= 0; j < clusterGraph.gatewayDistanceTable[i].entries.Length; j++)
        //    {
        //        print += clusterGraph.gatewayDistanceTable[i].entries[j].shortestDistance + ", ";
        //    }
        //    print += "]\n";
        //}
        //print += "]";
        //Debug.Log(print);



        //do not change this
        var nodes = GetNodesHack(navMesh);

        clusterGraph.nodesCluster = new Cluster[nodes.Count];
        foreach (var n in nodes)
        {
            var pos = n.LocalPosition;
            foreach (var c in clusterGraph.clusters)
            {
                if (pos.x >= c.min.x && pos.x <= c.max.x && pos.z >= c.min.z && pos.z <= c.max.z)
                {
                    clusterGraph.nodesCluster[n.NodeIndex] = c;
                    break;
                }
            }
        }


        //create a new asset that will contain the ClusterGraph and save it to disk (DO NOT REMOVE THIS LINE)
        clusterGraph.SaveToAssetDatabase();
    }
Exemplo n.º 3
0
 public GatewayHeuristic(ClusterGraph clusterGraph)
 {
     this.ClusterGraph = clusterGraph;
 }
Exemplo n.º 4
0
    private static void CreateClusterGraph()
    {
        Cluster cluster;
        Gateway gateway;

        //get cluster game objects
        var clusters = GameObject.FindGameObjectsWithTag("Cluster");
        //get gateway game objects
        var gateways = GameObject.FindGameObjectsWithTag("Gateway");
        //get the NavMeshGraph from the current scene
        NavMeshPathGraph navMesh = GameObject.Find("Navigation Mesh").GetComponent <NavMeshRig>().NavMesh.Graph;

        ClusterGraph clusterGraph = ScriptableObject.CreateInstance <ClusterGraph>();

        //create gateway instances for each gateway game object
        for (int i = 0; i < gateways.Length; i++)
        {
            var gatewayGO = gateways[i];
            gateway = ScriptableObject.CreateInstance <Gateway>();
            gateway.Initialize(i, gatewayGO);
            clusterGraph.gateways.Add(gateway);
            clusterGraph.gatewayDistanceTable = new GatewayDistanceTableRow[clusterGraph.gateways.Count];
        }

        //create cluster instances for each cluster game object and check for connections through gateways
        foreach (var clusterGO in clusters)
        {
            cluster = ScriptableObject.CreateInstance <Cluster>();
            cluster.Initialize(clusterGO);
            clusterGraph.clusters.Add(cluster);

            //determine intersection between cluster and gateways and add connections when they intersect
            foreach (var gate in clusterGraph.gateways)
            {
                if (MathHelper.BoundingBoxIntersection(cluster.min, cluster.max, gate.min, gate.max))
                {
                    cluster.gateways.Add(gate);
                    gate.clusters.Add(cluster);
                }
            }
        }

        // Second stage of the algorithm, calculation of the Gateway table

        GlobalPath solution = null;
        float      cost;
        Gateway    startGate;
        Gateway    endGate;
        Vector3    startGatewayPos;
        Vector3    endGatewayPos;

        var pathfindingManager = new PathfindingManager();

        pathfindingManager.Initialize(navMesh, new NodeArrayAStarPathFinding(navMesh, new EuclideanHeuristic()));

        AStarPathfinding pathfinding = new NodeArrayAStarPathFinding(navMesh, new EuclideanHeuristic());

        //TODO implement the rest of the algorithm here, i.e. build the GatewayDistanceTable

        //GatewayDistanceTableRow row = new GatewayDistanceTableRow();

        //OPTIMIZED VERSION
        for (int i = 0; i < clusterGraph.gateways.Count; ++i)
        {
            GatewayDistanceTableRow row = ScriptableObject.CreateInstance <GatewayDistanceTableRow>();
            row.entries    = new GatewayDistanceTableEntry[clusterGraph.gateways.Count];
            row.entries[i] = ScriptableObject.CreateInstance <GatewayDistanceTableEntry>();

            row.entries[i].startGatewayPosition = clusterGraph.gateways[i].center;
            row.entries[i].endGatewayPosition   = clusterGraph.gateways[i].center;
            row.entries[i].shortestDistance     = 0;

            for (int j = i + 1; j < clusterGraph.gateways.Count; ++j)
            {
                startGatewayPos = clusterGraph.gateways[i].center;
                endGatewayPos   = clusterGraph.gateways[j].center;
                pathfinding.InitializePathfindingSearch(startGatewayPos, endGatewayPos);
                while (solution == null)
                {
                    bool finished = pathfinding.Search(out solution, false);
                }

                List <Vector3> pathPos = solution.PathPositions;
                cost = 0;
                for (int k = 0; k < pathPos.Count - 1; ++k)
                {
                    cost += (pathPos[k + 1] - pathPos[k]).magnitude;
                }

                solution = null;

                row.entries[j] = ScriptableObject.CreateInstance <GatewayDistanceTableEntry>();

                row.entries[j].startGatewayPosition = startGatewayPos;
                row.entries[j].endGatewayPosition   = endGatewayPos;
                row.entries[j].shortestDistance     = cost;
            }
            for (int k = 0; k < i; ++k)
            {
                row.entries[k] = ScriptableObject.CreateInstance <GatewayDistanceTableEntry>();

                row.entries[k].startGatewayPosition = clusterGraph.gatewayDistanceTable[k].entries[i].endGatewayPosition;
                row.entries[k].endGatewayPosition   = clusterGraph.gatewayDistanceTable[k].entries[i].startGatewayPosition;
                row.entries[k].shortestDistance     = clusterGraph.gatewayDistanceTable[k].entries[i].shortestDistance;

                Debug.Log(row.entries[k].shortestDistance + " " + clusterGraph.gatewayDistanceTable[k].entries[i].shortestDistance);
            }
            clusterGraph.gatewayDistanceTable[i] = ScriptableObject.CreateInstance <GatewayDistanceTableRow>();
            clusterGraph.gatewayDistanceTable[i] = row;
        }

        //int j = 0;
        //foreach (Gateway gate in clusterGraph.gateways)
        //{
        //    GatewayDistanceTableRow row = ScriptableObject.CreateInstance<GatewayDistanceTableRow>();
        //    row.entries = new GatewayDistanceTableEntry[clusterGraph.gateways.Count];
        //    int i = 0;
        //    foreach (Gateway gate2 in clusterGraph.gateways)
        //    {
        //        if(!gate.center.Equals(gate2.center))
        //        {
        //            pathfinding.InitializePathfindingSearch(gate.center, gate2.center);
        //            while (solution == null)
        //            {
        //                bool finished = pathfinding.Search(out solution, false);
        //            }
        //            cost = solution.Length;
        //            solution = null;

        //            row.entries[i] = ScriptableObject.CreateInstance<GatewayDistanceTableEntry>();

        //            row.entries[i].startGatewayPosition = gate.center;
        //            row.entries[i].endGatewayPosition = gate2.center;
        //            row.entries[i].shortestDistance = cost;

        //            //Debug.Log("======================================!!!!!!!!!!!!!!!!!!!!" + row.entries[i].startGatewayPosition + gate.center);
        //            //Debug.Log("======================================\\\\\\\\\\\\\\\\\\\\" + row.entries[i].endGatewayPosition);

        //        }
        //        else
        //        {
        //            row.entries[i] = ScriptableObject.CreateInstance<GatewayDistanceTableEntry>();

        //            row.entries[i].startGatewayPosition = gate.center;
        //            row.entries[i].endGatewayPosition = gate2.center;
        //            row.entries[i].shortestDistance = 0;

        //        }
        //        i++;


        //    }
        //    clusterGraph.gatewayDistanceTable[j] = ScriptableObject.CreateInstance<GatewayDistanceTableRow>();
        //    clusterGraph.gatewayDistanceTable[j] = row;

        //    //Debug.Log("j========  " + j);
        //    if (j > 0)
        //    {
        //        Debug.Log("aaaaaaaaaaaaaaaaaaaa====== " + clusterGraph.gatewayDistanceTable[1].entries[2].startGatewayPosition + row.entries[2].startGatewayPosition);
        //    }
        //    j++;
        //}


        //foreach (var a in clusterGraph.gatewayDistanceTable)
        //{
        //    foreach(var b in a.entries)
        //    {
        //        Debug.Log("shortestDistance =        " + b.shortestDistance);
        //        Debug.Log("startGatewayPosition =        " + b.startGatewayPosition);
        //        Debug.Log("endGatewayPosition =        " + b.endGatewayPosition);

        //    }
        //    Debug.Log("ACABOU ENTRYYYYYYYYYYYYYYYY\n\n");
        //}

        //create a new asset that will contain the ClusterGraph and save it to disk (DO NOT REMOVE THIS LINE)
        clusterGraph.SaveToAssetDatabase();
    }
Exemplo n.º 5
0
    private static void CreateClusterGraph()
    {
        Cluster cluster;
        Gateway gateway;

        //get cluster game objects
        var clusters = GameObject.FindGameObjectsWithTag("Cluster");
        //get gateway game objects
        var gateways = GameObject.FindGameObjectsWithTag("Gateway");
        //get the NavMeshGraph from the current scene
        NavMeshPathGraph navMesh = GameObject.Find("Navigation Mesh").GetComponent <NavMeshRig>().NavMesh.Graph;

        ClusterGraph clusterGraph = ScriptableObject.CreateInstance <ClusterGraph>();

        //create gateway instances for each gateway game object
        for (int i = 0; i < gateways.Length; i++)
        {
            var gatewayGO = gateways[i];
            gateway = ScriptableObject.CreateInstance <Gateway>();
            gateway.Initialize(i, gatewayGO);
            clusterGraph.gateways.Add(gateway);
        }

        //create cluster instances for each cluster game object and check for connections through gateways
        foreach (var clusterGO in clusters)
        {
            cluster = ScriptableObject.CreateInstance <Cluster>();
            cluster.Initialize(clusterGO);
            clusterGraph.clusters.Add(cluster);

            //determine intersection between cluster and gateways and add connections when they intersect
            foreach (var gate in clusterGraph.gateways)
            {
                if (MathHelper.BoundingBoxIntersection(cluster.min, cluster.max, gate.min, gate.max))
                {
                    cluster.gateways.Add(gate);
                    gate.clusters.Add(cluster);
                }
            }
        }

        // Second stage of the algorithm, calculation of the Gateway table

        GlobalPath solution = null;
        float      cost;

        var pathfindingManager = new PathfindingManager();

        pathfindingManager.Initialize(navMesh, new NodeArrayAStarPathFinding(navMesh, new EuclideanDistanceHeuristic()));

        //TODO implement the rest of the algorithm here, i.e. build the GatewayDistanceTable
        clusterGraph.gatewayDistanceTable = new GatewayDistanceTableRow[clusterGraph.CountGateways()];

        foreach (Gateway startGate in clusterGraph.gateways)
        {
            Vector3 startGatePosition = startGate.Localize();
            GatewayDistanceTableRow startGateToOthers = new GatewayDistanceTableRow();
            startGateToOthers.entries = new GatewayDistanceTableEntry[clusterGraph.CountGateways()];
            foreach (Gateway endGate in clusterGraph.gateways)
            {
                Vector3 endGatePosition        = endGate.Localize();
                GatewayDistanceTableEntry cell = new GatewayDistanceTableEntry();
                cell.startGatewayPosition = startGatePosition;
                cell.endGatewayPosition   = endGatePosition;

                if (startGatePosition == endGatePosition)
                {
                    cost = 0;
                }
                else
                {
                    pathfindingManager.AStarPathFinding.InitializePathfindingSearch(startGatePosition, endGatePosition);
                    while (pathfindingManager.AStarPathFinding.InProgress)
                    {
                        var finished = pathfindingManager.AStarPathFinding.Search(out solution);
                        if (finished)
                        {
                            pathfindingManager.AStarPathFinding.InProgress = false;
                        }
                    }
                    cost = solution.Length;
                }
                cell.shortestDistance = cost;
                startGateToOthers.entries[endGate.id] = cell;
            }
            clusterGraph.gatewayDistanceTable[startGate.id] = startGateToOthers;
        }

        //create a new asset that will contain the ClusterGraph and save it to disk (DO NOT REMOVE THIS LINE)
        clusterGraph.SaveToAssetDatabase();
    }
Exemplo n.º 6
0
    private static void CreateClusterGraph()
    {
        Cluster cluster;
        Gateway gateway;

        //get cluster game objects
        var clusters = GameObject.FindGameObjectsWithTag("Cluster");

        //sort by name to be able to manually create the master clusters
        Array.Sort(clusters, CompareObNames);


        //get gateway game objects
        var gateways = GameObject.FindGameObjectsWithTag("Gateway");
        //get the NavMeshGraph from the current scene
        NavMeshPathGraph navMesh = GameObject.Find("Navigation Mesh").GetComponent <NavMeshRig>().NavMesh.Graph;

        ClusterGraph clusterGraph = ScriptableObject.CreateInstance <ClusterGraph>();

        //create gateway instances for each gateway game object
        for (int i = 0; i < gateways.Length; i++)
        {
            var gatewayGO = gateways[i];
            gateway = ScriptableObject.CreateInstance <Gateway>();
            gateway.Initialize(i, gatewayGO);
            clusterGraph.gateways.Add(gateway);
        }

        //create cluster instances for each cluster game object and check for connections through gateways
        foreach (var clusterGO in clusters)
        {
            cluster = ScriptableObject.CreateInstance <Cluster>();
            cluster.Initialize(clusterGO);
            clusterGraph.clusters.Add(cluster);

            //determine intersection between cluster and gateways and add connections when they intersect
            foreach (var gate in clusterGraph.gateways)
            {
                if (MathHelper.BoundingBoxIntersection(cluster.min, cluster.max, gate.min, gate.max))
                {
                    cluster.gateways.Add(gate);
                    gate.clusters.Add(cluster);
                }
            }
        }
        //Creating master clusters
        clusterGraph.MasterClusters.Add(new MasterCluster(clusterGraph.clusters[1], clusterGraph.clusters[2]));
        clusterGraph.MasterClusters.Add(new MasterCluster(clusterGraph.clusters[3], clusterGraph.clusters[4], clusterGraph.clusters[5], clusterGraph.clusters[18]));
        clusterGraph.MasterClusters.Add(new MasterCluster(clusterGraph.clusters[19], clusterGraph.clusters[20], clusterGraph.clusters[21], clusterGraph.clusters[22], clusterGraph.clusters[23]));
        clusterGraph.MasterClusters.Add(new MasterCluster(clusterGraph.clusters[24], clusterGraph.clusters[25], clusterGraph.clusters[26], clusterGraph.clusters[27], clusterGraph.clusters[28], clusterGraph.clusters[29]));
        clusterGraph.MasterClusters.Add(new MasterCluster(clusterGraph.clusters[7], clusterGraph.clusters[8], clusterGraph.clusters[9], clusterGraph.clusters[10], clusterGraph.clusters[11], clusterGraph.clusters[12], clusterGraph.clusters[13], clusterGraph.clusters[14]));
        clusterGraph.MasterClusters.Add(new MasterCluster(clusterGraph.clusters[6], clusterGraph.clusters[16], clusterGraph.clusters[17]));
        clusterGraph.MasterClusters.Add(new MasterCluster(clusterGraph.clusters[0]));
        clusterGraph.MasterClusters.Add(new MasterCluster(clusterGraph.clusters[15]));

        // Second stage of the algorithm, calculation of the Gateway table

        GlobalPath solution = null;
        float      cost;
        Gateway    startGate;
        Gateway    endGate;
        int        gatewaysCount = gateways.Length;
        int        k, j;
        var        pathfindingAlgorithm = new NodeArrayAStarPathFinding(navMesh, new EuclidianHeuristic());

        GatewayDistanceTableRow[] distanceTable = new GatewayDistanceTableRow[gatewaysCount];

        for (k = 0; k < gatewaysCount; k++)
        {
            GatewayDistanceTableRow distanceTableRow = ScriptableObject.CreateInstance <GatewayDistanceTableRow>();
            distanceTableRow.Initialize(gatewaysCount);

            startGate = clusterGraph.gateways[k];

            for (j = 0; j < gatewaysCount; j++)
            {
                endGate = clusterGraph.gateways[j];

                if (startGate == endGate)
                {
                    cost = 0;
                }
                else
                {
                    pathfindingAlgorithm.InitializePathfindingSearch(startGate.Localize(), endGate.Localize());
                    pathfindingAlgorithm.Search(out solution);
                    solution.CalculateLength();
                    cost = solution.Length;
                }
                GatewayDistanceTableEntry entry = ScriptableObject.CreateInstance <GatewayDistanceTableEntry>();
                entry.Initialize(startGate.Localize(), endGate.Localize(), cost);

                distanceTableRow.AddEntry(entry, j);
            }
            distanceTable[k] = distanceTableRow;
        }

        clusterGraph.gatewayDistanceTable = distanceTable;

        //create a new asset that will contain the ClusterGraph and save it to disk (DO NOT REMOVE THIS LINE)
        clusterGraph.SaveToAssetDatabase();
    }
Exemplo n.º 7
0
    private static void CreateClusterGraph()
    {
        Cluster cluster;
        Gateway gateway;

        //get cluster game objects
        var clusters = GameObject.FindGameObjectsWithTag("Cluster");
        //get gateway game objects
        var gateways = GameObject.FindGameObjectsWithTag("Gateway");
        //get the NavMeshGraph from the current scene
        NavMeshPathGraph navMesh = GameObject.Find("Navigation Mesh").GetComponent <NavMeshRig>().NavMesh.Graph;

        ClusterGraph clusterGraph = ScriptableObject.CreateInstance <ClusterGraph>();

        //create gateway instances for each gateway game object
        for (int i = 0; i < gateways.Length; i++)
        {
            var gatewayGO = gateways[i];
            gateway = ScriptableObject.CreateInstance <Gateway>();
            gateway.Initialize(i, gatewayGO);
            clusterGraph.gateways.Add(gateway);
        }

        //create cluster instances for each cluster game object and check for connections through gateways
        foreach (var clusterGO in clusters)
        {
            cluster = ScriptableObject.CreateInstance <Cluster>();
            cluster.Initialize(clusterGO);
            clusterGraph.clusters.Add(cluster);

            //determine intersection between cluster and gateways and add connections when they intersect
            foreach (var gate in clusterGraph.gateways)
            {
                if (MathHelper.BoundingBoxIntersection(cluster.min, cluster.max, gate.min, gate.max))
                {
                    cluster.gateways.Add(gate);
                    gate.clusters.Add(cluster);
                }
            }
        }

        // Second stage of the algorithm, calculation of the Gateway table

        GlobalPath solution = null;
        float      cost;
        Gateway    startGate;
        Gateway    endGate;

        var pathfindingAlgorithm = new NodeArrayAStarPathFinding(navMesh, new EuclideanDistanceHeuristic());

        //TODO implement the rest of the algorithm here, i.e. build the GatewayDistanceTable
        clusterGraph.gatewayDistanceTable = new GatewayDistanceTableRow[clusterGraph.gateways.Count];

        for (int i = 0; i < clusterGraph.gateways.Count; i++)
        {
            startGate = clusterGraph.gateways[i];
            clusterGraph.gatewayDistanceTable[i]         = ScriptableObject.CreateInstance <GatewayDistanceTableRow>();
            clusterGraph.gatewayDistanceTable[i].entries = new GatewayDistanceTableEntry[clusterGraph.gateways.Count];
            for (int j = 0; j < clusterGraph.gateways.Count; j++)
            {
                endGate = clusterGraph.gateways[j];
                pathfindingAlgorithm.InitializePathfindingSearch(startGate.center, endGate.center);
                pathfindingAlgorithm.Search(out solution);

                cost = solution.Length;
                clusterGraph.gatewayDistanceTable[i].entries[j] = ScriptableObject.CreateInstance <GatewayDistanceTableEntry>();
                clusterGraph.gatewayDistanceTable[i].entries[j].startGatewayPosition = startGate.center;
                clusterGraph.gatewayDistanceTable[i].entries[j].endGatewayPosition   = endGate.center;
                clusterGraph.gatewayDistanceTable[i].entries[j].shortestDistance     = cost;
            }
        }

        //create a new asset that will contain the ClusterGraph and save it to disk (DO NOT REMOVE THIS LINE)
        clusterGraph.SaveToAssetDatabase();
    }