Ejemplo n.º 1
0
    private bool _ValidateSpawnPosition(Vector2 spawnPos, AICar aiCar)
    {
        AICar.DiagonalData dd = aiCar.ScanDiagonals(spawnPos);
        int tileType          = _GetTileType(spawnPos);

        return(_spawnTiles.Contains(tileType) && dd.forwardPoints >= 20);
    }
Ejemplo n.º 2
0
 void setAllAICarRunning(bool b)
 {
     foreach (GameObject AICar in AICars)
     {
         AICar.GetComponent <CarController>().isRunning = b;
     }
 }
Ejemplo n.º 3
0
 int CheckPosition(AICar car)
 {
     if (player.currentLap >= car.currentLap)
     {
         if (player.currentLap > car.currentLap)
         {
             return(0);
         }
         if (player.currentLap == car.currentLap && player.currentWaypoint >= car.currentWaypoint)
         {
             if (player.currentWaypoint > car.currentWaypoint)
             {
                 return(0);
             }
             if (player.currentLap == car.currentLap &&
                 player.currentWaypoint == car.currentWaypoint &&
                 player.distanceToWaypoint.magnitude <= car.distanceToWaypoint.magnitude)
             {
                 return(0);
             }
             else
             {
                 return(1);
             }
         }
         else
         {
             return(1);
         }
     }
     else
     {
         return(1);
     }
 }
Ejemplo n.º 4
0
    void UpdateCarPerformanceBehavior(bool withOptimization)
    {
        if (AICarSet == null || AICarSet.Count < 1)
        {
            return;
        }

        Debug.Log("Current AI Car Number: " + AICarSet.Count);

        if (withOptimization)
        {
            foreach (var AICar in AICarSet)
            {
                if (AICar == null)
                {
                    continue;
                }

                if (AICar != null)
                {
                    AICar.CancelInvoke(nameof(AICar.UpdateCarPerformance));
                    AICar.CancelInvoke(nameof(AICar.UpdateCarPerformanceRenderOnly));
                    AICar.CancelInvoke(nameof(AICar.UpdateCarPerformancePhysicsOnly));
                    if (optimizeDistantCarRender && optimizeDistantCarPhysics)
                    {
                        AICar.InvokeRepeating(nameof(AICar.UpdateCarPerformance), Random.Range(0.0f, performanceCheckInterval), performanceCheckInterval);
                    }
                    else if (optimizeDistantCarRender)
                    {
                        AICar.SetCarSimNormal();
                        AICar.InvokeRepeating(nameof(AICar.UpdateCarPerformanceRenderOnly), Random.Range(0.0f, performanceCheckInterval), performanceCheckInterval);
                    }
                    else if (optimizeDistantCarPhysics)
                    {
                        AICar.SetCarInRenderRange();
                        AICar.InvokeRepeating(nameof(AICar.UpdateCarPerformancePhysicsOnly), Random.Range(0.0f, performanceCheckInterval), performanceCheckInterval);
                    }
                }
            }
        }
        else
        {
            foreach (var AICar in AICarSet)
            {
                if (AICar == null)
                {
                    continue;
                }

                if (AICar != null)
                {
                    AICar.CancelInvoke(nameof(AICar.UpdateCarPerformance));
                    AICar.CancelInvoke(nameof(AICar.UpdateCarPerformanceRenderOnly));
                    AICar.CancelInvoke(nameof(AICar.UpdateCarPerformancePhysicsOnly));
                    AICar.SetCarInRenderRange();
                    AICar.SetCarSimNormal();
                }
            }
        }
    }
Ejemplo n.º 5
0
    private void _SpawnAICars()
    {
        AICar aiCar = (AICar)_aiCarScene.Instance();

        aiCar.Set("Map", _trackTileMap);
        aiCar.Set("Rng", rng);
        aiCar.Position = _GetSpawnLocation(_playerCar.Position, aiCar);
        aiCar.Connect("HitTree", this, nameof(_OnAICarHitTree));

        if (aiCar.Position == Vector2.Zero)
        {
            aiCar.CallDeferred("QueueFree");
            return;
        }
        else
        {
            _aiCars.Add(aiCar);
            aiCar.MaxSpeed = rng.RandfRange(AIMinSpeed, AIMaxSpeed);
            aiCar.Speed    = rng.RandfRange(AIMinSpeed, aiCar.MaxSpeed);
            GetParent().AddChild(aiCar);
            _canSpawn = false;
            _spawnTimer.Start(rng.RandfRange(SpawnTimeMin, SpawnTimeMax));
            _previousSpawnPos = aiCar.Position;
        }
    }
Ejemplo n.º 6
0
    int CheckPosition(AICar car)
    {
        if(player.currentLap >= car.currentLap)
        {
            if (player.currentLap > car.currentLap)
                return 0;
            if (player.currentLap == car.currentLap && player.currentWaypoint >= car.currentWaypoint)
            {
                if (player.currentWaypoint > car.currentWaypoint)
                    return 0;
                if (player.currentLap == car.currentLap &&
            player.currentWaypoint == car.currentWaypoint &&
            player.distanceToWaypoint.magnitude <= car.distanceToWaypoint.magnitude)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            else
            {
                return 1;
            }

        }
        else
        {
            return 1;
        }
    }
Ejemplo n.º 7
0
 public void CarDead(AICar DeadCar)
 {
     DeadCar.gameObject.SetActive(false);
     CurrentPopulationCount--;
     UI.Instance.PopulationSizeText.text = "Population Size: " + CurrentPopulationCount;
     if (CurrentPopulationCount == 0)
     {
         NextGeneration();
     }
 }
Ejemplo n.º 8
0
    public void AddCar(NeuralNetwork network, Sprite sprite = null)
    {
        AICar car = Instantiate(GameManager.Instance.AICarPrefab, transform.position, transform.rotation, transform).GetComponent <AICar>();

        car.Network = network;
        if (sprite != null)
        {
            car.GetComponentInParent <SpriteRenderer>().sprite = sprite;
        }
        Population.Add(car);
    }
Ejemplo n.º 9
0
 private void CalculateFittestLivingCar()
 {
     fittestLivingCar = null;
     for (int i = 0; i < Population.Count; i++)
     {
         if (Population[i].gameObject.activeSelf && (fittestLivingCar == null || Population[i].Fitness > fittestLivingCar.Fitness))
         {
             FittestLivingCar = Population[i];
         }
     }
 }
Ejemplo n.º 10
0
 void Start()
 {
     if (gameObject.tag == "Player")
     {
         playerControl = gameObject.GetComponentInParent <CarUserControl>();
         itemMgr       = gameObject.GetComponentInParent <ItemManager>();
     }
     else
     {
         enemyControl = gameObject.GetComponentInParent <AICar>();
     }
 }
Ejemplo n.º 11
0
 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.layer == LayerMask.NameToLayer("AICarCollider"))
     {
         AICar car = other.transform.GetComponentInParent <AICar>();
         if (car != null && !HittedCars.Contains(car.ID))
         {
             HittedCars.Add(car.ID);
             car.CheckpointHit();
         }
     }
 }
Ejemplo n.º 12
0
    private void NextGeneration()
    {
        GenerationStartTime = Time.time;
        GenerationCount++;
        UI.Instance.GenerationText.text     = "Generation: " + GenerationCount;
        CurrentPopulationCount              = PopulationCount;
        UI.Instance.PopulationSizeText.text = "Population Size: " + CurrentPopulationCount;

        // Copy and order previous population
        AICar[] previousPopulation = new AICar[PopulationCount];
        Population.CopyTo(previousPopulation);
        Array.Sort(previousPopulation, ((x, y) => y.Fitness.CompareTo(x.Fitness)));

        // Destroy and clear population
        foreach (AICar car in Population)
        {
            Destroy(car.gameObject);
        }
        Population.Clear();

        // Elitism
        for (int i = 0; i < ElitismCount; i++)
        {
            AddCar(previousPopulation[i].Network, GameManager.Instance.GreenCarSprite);
        }

        // Random
        for (int i = 0; i < RandomCount; i++)
        {
            AddCar(new NeuralNetwork(NetworkTopology), GameManager.Instance.RedCarSprite);
        }

        // Mutation of fittest car
        for (int i = 0; i < MutationCount; i++)
        {
            AddCar(new NeuralNetwork(previousPopulation[0].Network).Mutate(MutationProbability, MutationAmount), GameManager.Instance.YellowCarSprite);
        }

        // Crossover
        for (int i = 0; i < CrossoverCount; i++)
        {
            // Selection
            AICar tournamentWinner1 = TournamentSelection(previousPopulation);
            AICar tournamentWinner2 = TournamentSelection(previousPopulation);

            // Crossover
            NeuralNetwork child = NeuralNetwork.Crossover(tournamentWinner1.Network, tournamentWinner2.Network, UniformRate);

            // Mutation
            AddCar(child.Mutate(MutationProbability, MutationAmount), GameManager.Instance.BlueCarSprite);
        }
    }
Ejemplo n.º 13
0
    private void AddAICar(string networkPath)
    {
        AICar car = null;

        using (StreamReader file = File.OpenText(networkPath))
        {
            JsonSerializer serializer = new JsonSerializer();
            NeuralNetwork  network    = (NeuralNetwork)serializer.Deserialize(file, typeof(NeuralNetwork));
            car         = Instantiate(AICarPrefab, transform.position, transform.rotation).GetComponent <AICar>();
            car.Network = network;
            Camera.main.GetComponent <CameraController>().Target = car.transform;
        }
    }
Ejemplo n.º 14
0
    void OnTriggerEnter(Collider coll)
    {
        if (coll.gameObject.tag == "LaunchableItem" && invincible)
        {
            Destroy(coll.gameObject);
        }
        else if (coll.gameObject.tag == "EnemyCollider" && invincible)
        {
            AICar ai = coll.gameObject.GetComponentInParent <AICar>();

            if (ai.isActiveAndEnabled)
            {
                ai.enabled = false;
                coll.gameObject.transform.parent.Translate(Vector3.up * power);
                StartCoroutine(ReactivateVictim(ai));
            }
        }
    }
Ejemplo n.º 15
0
    private AICar TournamentSelection(AICar[] population)
    {
        AICar[] tournament = new AICar[TournamentSize];
        for (int i = 0; i < TournamentSize; i++)
        {
            tournament[i] = population[(int)(NeuralMath.RandomDouble() * PopulationCount)];
        }
        AICar fittestCar = tournament[0];

        for (int i = 1; i < TournamentSize; i++)
        {
            if (tournament[i].Fitness > fittestCar.Fitness)
            {
                fittestCar = tournament[i];
            }
        }
        return(fittestCar);
    }
Ejemplo n.º 16
0
    private Vector2 _GetSpawnLocation(Vector2 playerPosition, AICar aiCar)
    {
        int     calcCount = 0;
        Vector2 spawnPos  = _CreateSpawnPosition(playerPosition);

        while (!_ValidateSpawnPosition(spawnPos, aiCar) && calcCount < 128)
        {
            spawnPos = _CreateSpawnPosition(playerPosition);
            calcCount++;
        }

        if (calcCount < 128)
        {
            GD.Print("Created car at: " + spawnPos);
            return(spawnPos);
        }

        return(Vector2.Zero);
    }
 // calculate the remaining distance for all the cars
 private void FetchRemainingDistance()
 {
     subham = cars[0].GetComponent <PlayerCar>();
     if (subham != null)
     {
         subhamRemainingDistance = subham.GetPathRemainingDistance();
     }
     //print("subhamRemainingDistance: " + subhamRemainingDistance);
     godspeed = cars[1].GetComponent <AICar>();
     if (godspeed != null)
     {
         godSpeedRemainingDistance = godspeed.GetPathRemainingDistance();
     }
     //print("godSpeedRemainingDistance: " + godSpeedRemainingDistance);
     pikachu = cars[2].GetComponent <AICar>();
     if (pikachu != null)
     {
         pikachuRemainingDistance = pikachu.GetPathRemainingDistance();
     }
     //print("pikachuRemainingDistance: " + pikachuRemainingDistance);
     CalculateLeaderBoard();
     CalculateGameOutcome(subham);
 }
Ejemplo n.º 18
0
 private void _RemoveAICar(AICar aiCar)
 {
     _aiCars.Remove(aiCar);
     aiCar.CallDeferred("QueueFree");
 }
Ejemplo n.º 19
0
    IEnumerator ReactivateVictim(AICar ai)
    {
        yield return(new WaitForSeconds(5f));

        ai.enabled = true;
    }
Ejemplo n.º 20
0
 public void _OnAICarHitTree(AICar aiCar)
 {
     GD.Print("Cars on track before hit: " + _aiCars.Count);
     _RemoveAICar(aiCar);
     GD.Print("Cars on track after hit: " + _aiCars.Count);
 }