Exemplo n.º 1
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.º 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;

        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.º 3
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 EuclideanHeuristic());

        //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++)
        {
            GatewayDistanceTableRow row = ScriptableObject.CreateInstance <GatewayDistanceTableRow>();
            row.entries = new GatewayDistanceTableEntry[clusterGraph.gateways.Count];
            for (int j = 0; j < clusterGraph.gateways.Count; j++)
            {
                GatewayDistanceTableEntry entry = ScriptableObject.CreateInstance <GatewayDistanceTableEntry>();

                entry.startGatewayPosition = clusterGraph.gateways[i].center;
                entry.endGatewayPosition   = clusterGraph.gateways[j].center;
                entry.shortestDistance     = (clusterGraph.gateways[i].center - clusterGraph.gateways[j].center).magnitude;
                row.entries[j]             = entry;
            }
            clusterGraph.gatewayDistanceTable[i] = row;
        }

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