Ejemplo n.º 1
0
    void initCarPheremoneMaps()
    {
        carPheremoneMapsList = new List <CarPheremonMap> ();

        foreach (GameObject car in CarGenerator.carList)
        {
            CarPheremonMap cpm = new CarPheremonMap();
            cpm.car           = car;
            cpm.pheremoneList = new List <Pheremone> ();

            Color color = Random.ColorHSV(0, 1, 1, 1, 1, 1);

            float height = UnityEngine.Random.Range(0.05f, 0.7f);

            List <Pheremone> pheremoneList = new List <Pheremone> ();
            foreach (Road road in MPACColonyRoadList)
            {
                Pheremone p = new Pheremone(road.End_A, road.End_B, road, color, height);
                cpm.pheremoneList.Add(p);
            }

            carPheremoneMapsList.Add(cpm);
        }
    }
Ejemplo n.º 2
0
    private static List <Road> getRoadParams()
    {
        float          MaxSum  = 0;
        CarPheremonMap bestCPM = null;

        foreach (CarPheremonMap cpm in carPheremoneMapsList)
        {
            float Sum = 0;
            foreach (Pheremone p in cpm.pheremoneList)
            {
                Sum = Sum + p.pheremone;
            }
            if (Sum > MaxSum)
            {
                MaxSum  = Sum;
                bestCPM = cpm;
            }
        }

        // start from the car and go until the car and find all the parameters.
        List <Road>       path           = new List <Road>();
        carController_new carCtrl        = bestCPM.car.GetComponent <carController_new> ();
        Pheremone         startPheremone = bestCPM.pheremoneList.Find(
            p => (
                (p.EndA == bestCPM.car.transform.position && p.EndB == carCtrl.targetNode.location) ||
                (p.EndB == bestCPM.car.transform.position && p.EndA == carCtrl.targetNode.location)));

        path.Add(startPheremone.road);

        Vector3 previousLocation = bestCPM.car.transform.position;
        Vector3 currentLocation  = ((previousLocation == path [0].End_A) ? path [0].End_B : path [0].End_A);

        // keep on moving until we reach the user.
        int safeexit = 0;

        while (currentLocation != UserSelector.selectedNode.transform.position)
        {
            safeexit++;
            if (safeexit >= 1000)
            {
                break;
            }

            // get current node
            Node      thisNode          = MPACColonyNodeList.Find(node => node.location == currentLocation);
            Pheremone maxPheremone      = null;
            float     maxPheremoneFound = 0;
            foreach (Vector3 newLocation in thisNode.ways)
            {
                // for each path check the pheremone
                if (newLocation != previousLocation)
                {
                    Pheremone thisPheremone = bestCPM.pheremoneList.Find(p =>
                                                                         (p.EndA == newLocation && p.EndB == currentLocation) ||
                                                                         (p.EndB == newLocation && p.EndA == currentLocation));
                    // check for maximum pheremone
                    if (thisPheremone.pheremone >= maxPheremoneFound)
                    {
                        maxPheremoneFound = thisPheremone.pheremone;
                        maxPheremone      = thisPheremone;
                    }
                }
            }
            path.Add(maxPheremone.road);
            previousLocation = currentLocation;
            currentLocation  = ((previousLocation == maxPheremone.road.End_A) ? maxPheremone.road.End_B : maxPheremone.road.End_A);
        }
        return(path);
    }