Exemple #1
0
    public void FindTrips(Block block)
    {
        float        distance   = float.MaxValue;
        int          foundTrips = 0;
        Node         end        = new Node(Vector3.up);
        List <Block> tmpList    = new List <Block>(city.blocks);

        //tmpList = city.blocks;
        tmpList.Remove(block);

        //Dijkstra
        Traffic.FindDistances(block.nodes[0], city.streetGraph.corners);

        //choose trip destinations
        int tripNumber = Math.Min(city.blocks.Count - 1, 10);

        if (tripNumber == city.blocks.Count - 1)
        {
            for (int i = 0; i < tmpList.Count; i++)
            {
                distance = float.MaxValue;
                //calculate trip attractiveness
                for (int j = 0; j < tmpList[i].nodes.Count; j++)
                {
                    if (tmpList[i].nodes[j].tDistance < distance)
                    {
                        distance = tmpList[i].nodes[j].tDistance;
                        end      = tmpList[i].nodes[j];
                    }
                }
                //find and add trip
                float       attractiveness = block.lut.TripAttractiveness(distance, tmpList[i].lut.getName(), tmpList[i].numInhabitants);
                List <Edge> path           = Traffic.GetPath(block.nodes[0], end);
                Trip        trip           = new Trip(path, distance, attractiveness);
                block.trips.Add(trip);
            }
        }
        else
        {
            while (foundTrips < tripNumber)
            {
                int dest = UnityEngine.Random.Range(0, tmpList.Count - 1);
                //calculate trip attractiveness
                for (int i = 0; i < tmpList[dest].nodes.Count; i++)
                {
                    if (tmpList[dest].nodes[i].tDistance < distance)
                    {
                        distance = tmpList[dest].nodes[i].tDistance;
                        end      = tmpList[dest].nodes[i];
                    }
                }

                float attractiveness = block.lut.TripAttractiveness(distance, tmpList[dest].lut.getName(), tmpList[dest].numInhabitants);
                if (UnityEngine.Random.Range(0f, 1f) <= attractiveness)
                {
                    //find and add trip
                    List <Edge> path = Traffic.GetPath(block.nodes[0], end);
                    Trip        trip = new Trip(path, distance, attractiveness);
                    block.trips.Add(trip);

                    foundTrips++;
                    tmpList.RemoveAt(dest);
                }
            }
        }
    }