Ejemplo n.º 1
0
 public override void AcademyStep()
 {
     if (!IsDone() && sys.battleStarted)
     {
         GameObject[] units = inspector.getCurrentUnits();
         foreach (GameObject unit in units)
         {
             if (inspector.setScriptsFrom(unit))
             {
                 if (inspector.getScriptType() == "AgentScript" && !inspector.isDead())
                 {
                     inspector.AgentDescisionRequest();
                     inspector.AgentAlwaysUpdate();
                 }
             }
         }
         //Enviromental Upadte
         sys.Academy_Update();
         if ((sys.knightNumber <= 0 || sys.enemyNumber <= 0) && !onceInStep)
         {
             //On End
             rewardSys.RewardAtEpisodeEnds(inspector, sys);
             onceInStep = true;
             Done();
         }
     }
     else if (IsDone())
     {
         int    knightlen = inspector.getCurrentKnights().Length;
         int    enemylen  = inspector.getCurrentEnemys().Length;
         string endstring = (knightlen > 0 && enemylen <= 0)?"Knight win":(enemylen > 0 && knightlen <= 0)?"Enemy win":(enemylen <= 0 && knightlen <= 0)?"Draw":(enemylen > 0 && knightlen > 0)?"Step Max reached":"Unknown state";
         Debug.Log($"{endstring} KnightAvgHP:{inspector.AvgLives(inspector.getCurrentKnights())} / EnemyAvgHP:{inspector.AvgLives(inspector.getCurrentEnemys())}");
     }
 }
Ejemplo n.º 2
0
    public override void RewardAtEpisodeEnds(UnitInspect inspector, GameSystem sys)
    {
        float AllDamaged = (inspector.getCurrentEnemys().Length <= 0)?0:inspector.AvgLives(inspector.getCurrentEnemys());

        //Rewarding globally
        foreach (GameObject knight in inspector.getInstantiatedKnights())
        {
            inspector.setScriptsFrom(knight);
            if (inspector.isScriptValid() && inspector.getScriptType() == "AgentScript")
            {
                BagReset();
                AddReward((1 - ((AllDamaged) / sys.AllInitLives)) * 2f);
                Apply(inspector.AgentScript);
            }
        }

        /*
         * //Rewarding reamined units
         * foreach(GameObject knight in inspector.getCurrentKnights()) {
         *  inspector.AgentAddRewardDircetly(1f);
         * }
         */
    }
Ejemplo n.º 3
0
 public override void CollectObservations()
 {
     if (sys != null)
     {
         if (!sys.battleStarted)
         {
             Debug.LogWarning("Observation triggered before battle started. sending zero observation");
             for (int i = 0; i < PlannedObs; i++)
             {
                 AddVectorObs(0);
                 AddVectorObs(0);
             }
         }
         else
         {
             GameObject[] Knight = inspector.getCurrentKnights();
             GameObject[] Enemy  = inspector.getCurrentEnemys();
             for (int i = 0; i < sys.initKnightNumber; i++)
             {
                 if (i >= Knight.Length || Knight?[i] == null)
                 {
                     AddVectorObs(0);
                     AddVectorObs(0);
                     continue;
                 }
                 if (inspector.setScriptsFrom(Knight[i]))                      //returns true when it's valid
                 {
                     if (!inspector.isDead())
                     {
                         // When alive
                         AddVectorObs(Knight[i].transform.localPosition.x);
                         AddVectorObs(Knight[i].transform.localPosition.z);
                     }
                     else
                     {
                         // When dead
                         AddVectorObs(0);
                         AddVectorObs(0);
                     }
                     continue;
                 }
                 //When script is not valid but it existed as game object
                 Debug.LogWarning("Unknown game object tagged as knight and it observated.");
                 AddVectorObs(0);
                 AddVectorObs(0);
                 continue;
             }
             for (int i = 0; i < sys.initEnemyNumber; i++)
             {
                 if (i >= Enemy.Length || Enemy?[i] == null)
                 {
                     AddVectorObs(0);
                     AddVectorObs(0);
                     continue;
                 }
                 if (inspector.setScriptsFrom(Enemy[i]))                      //returns true when it's valid
                 {
                     if (!inspector.isDead())
                     {
                         AddVectorObs(Enemy[i].transform.localPosition.x);
                         AddVectorObs(Enemy[i].transform.localPosition.z);
                     }
                     else
                     {
                         AddVectorObs(0);
                         AddVectorObs(0);
                     }
                     continue;
                 }
                 Debug.LogError("Unknown game object tagged as enemy and it observated.");
                 AddVectorObs(0);
                 AddVectorObs(0);
                 continue;
             }
         }
     }
     else
     {
         Debug.LogError("Observation triggered before even game system instantiate");
         for (int i = 0; i < PlannedObs; i++)        //placeholder
         {
             AddVectorObs(0);
             AddVectorObs(0);
         }
     }
     SetTextObs($"{inspector.AvgLives(inspector.getCurrentKnights())}");
 }