Пример #1
0
    public IEnumerator TestTrack()
    {
        ResetPlayers();

        Genome genome = SaveableObjects.SaveableNeuralNetwork.LoadNetwork("Best Overall ");

        aiPlayers.Add(new AIPlayer("Tester", genome.CreateNetwork()));
        carsLocker    = new object[1];
        carsLocker[0] = new object();

        CreateCars(true);
        AddPlayers();
        SetCarsReady(true, true, 0);

        ResetRaceParameters();
        CreateThreadActions((500));

        GA_Parameters.fps         = 60;
        GA_Parameters.laps        = 3;
        GA_Parameters.stopAtCrash = false;

        while (MyThreadPool.GetWaitingThreads() != MyThreadPool.workers.Length || cars[0].positions.Count > 0)
        {
            for (int i = 0; i < 40; i++)
            {
                cars[0].UpdateCar();
            }

            yield return(null);
        }
    }
Пример #2
0
    IEnumerator ThreadedRace(bool forceCompleteRace)
    {
        if (forceCompleteRace)
        {
            // Make sure the camera is at the right position when counting down
            cameraController.UpdateTransform();

            // Start the countdown coroutine and make sure it is added to the coroutines list
            Coroutine routine = StartCoroutine(cameraController.raceCanvas.CountDown());
            UIController.instance.activeRoutines.Add(routine);

            // Show a frame
            yield return(routine);
        }

        ResetRaceParameters();
        simulationTime = GA_Parameters.simulationTime;
        CreateThreadActions((int)(simulationTime));
        //ThreadedRace((int)simulationTime);

        while (true)
        {
            ingameTimePassed += (1f / GA_Parameters.fps);

            // Show a frame if the framerate drops under the training framerate
            if (Time.realtimeSinceStartup - curTime > 1f / GA_Parameters.fps)
            {
                yield return(null);

                if (countSinceFrame != 0)
                {
                    // Calculate how fast the simulation currently is
                    curSpeed = countSinceFrame / (GA_Parameters.fps * (Time.realtimeSinceStartup - curTime));
                }

                // reset these values
                countSinceFrame = 0;
                curTime         = Time.realtimeSinceStartup;
            }
            stopCurRace = true;

            // for each car in the group
            for (int carControllerindex = 0; carControllerindex < cars.Count; carControllerindex++)
            {
                // Get the cars controller

                if (cars[carControllerindex].IsDone() && carDone[carControllerindex])
                {
                    continue;
                }

                int counter = 0;
                while (!cars[carControllerindex].UpdateCar())
                {
                    counter++;
                    if (counter > 10)
                    {
                        cars[carControllerindex].SetActive(false);
                    }
                    if (!cars[carControllerindex].GetActive())
                    {
                        break;
                    }


                    if (!carDone[carControllerindex])
                    {
                        yield return(null);
                    }

                    if (countSinceFrame != 0)
                    {
                        curSpeed = countSinceFrame / (GA_Parameters.fps * (Time.realtimeSinceStartup - curTime));
                    }

                    // reset these values
                    countSinceFrame = 0;
                    curTime         = Time.realtimeSinceStartup;
                }

                if (cars[carControllerindex].IsDone() && !carDone[carControllerindex])
                {
                    if (!cars[carControllerindex].aIPlayer.network.bestOfAll && !cars[carControllerindex].aIPlayer.network.leader)
                    {
                        cars[carControllerindex].trackSphereMat.color    = Color.red;
                        cars[carControllerindex].trailren.material.color = Color.red;
                    }

                    carDone[carControllerindex] = true;
                }
                else if (cars[carControllerindex].IsDone() && carControllerindex == 0 && !carDone[carControllerindex])
                {
                    carDone[carControllerindex] = true;
                }

                if (carDone[carControllerindex])
                {
                    continue;
                }

                stopCurRace = false;
            }

            countSinceFrame++;


            // If a camera is following a car update its transform
            CameraController.instance.UpdateTransform();

            // If simulating to fast a pause is set up here
            Coroutine limitRoutine = StartCoroutine(LimitPlaybackSpeed());


            if (stopCurRace /*|| MyThreadPool.GetWaitingThreads() == MyThreadPool.workers.Length*/)
            {
                lock (curDoneLocker)
                    curDone = true;

                while (MyThreadPool.GetWaitingThreads() != MyThreadPool.workers.Length)
                {
                    yield return(null);
                }

                break;
            }

            if (limitRoutine != null)
            {
                yield return(limitRoutine);
            }
        }

        while (MyThreadPool.GetWaitingThreads() != MyThreadPool.workers.Length)
        {
            yield return(null);
        }
    }