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())}"); } }
///<summary>set battleStarted to be True</summary> public void startBattle() { AgentPreset preset; initEnemyNumber = inspector.getCurrentEnemys().Length; initKnightNumber = inspector.getCurrentKnights().Length; int numberOfSpace = inspector.getCurrentUnits().Length *2; foreach (GameObject unit in inspector.getCurrentUnits()) { if (unit.TryGetComponent <AgentPreset>(out preset)) { if (preset.AgentType == "Unit") { dynamic obj = preset.refer(numberOfSpace, AcademyInner.resetParameters); unit.SetActive(true); unit.SendMessage("initRefer", obj, SendMessageOptions.RequireReceiver); } else { Type anytype = Type.GetType(preset.AgentType); if (anytype != null) { Component script = unit.AddComponent(anytype); dynamic obj = preset.refer(numberOfSpace, AcademyInner.resetParameters); Debug.Log($"{anytype.Name} / {script.name} / {obj}"); unit.SetActive(true); script.SendMessage("initRefer", obj, SendMessageOptions.RequireReceiver); } else { Debug.LogError("There was invalid Agent type string which couldn't converted to type object "); } } } } print($"Start:{knightUnits.Length}/{enemyUnits.Length}"); //show the new UI cam.onStart(); StartCoroutine(battleUI()); battleStarted = true; //Hardcoded rewarding algorithm, need to be adjusted AllInitLives = 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); * } */ }
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())}"); }