Advertisement
Guest User

Untitled

a guest
Dec 30th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.49 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.MLAgents;
  5. using CardLogic;
  6. using System.Linq;
  7. using Unity.MLAgents.Sensors;
  8.  
  9. public class MonsterAgent : Agent
  10. {
  11.     private AI UnitAI;
  12.     private AI[] localUnits;
  13.     private Vector3 startPosition;
  14.     private int latestCard;
  15.  
  16.  
  17.     public override void Initialize()
  18.     {
  19.         UnitAI = GetComponent<AI>();
  20.         //Debug.Log(UnitAI);
  21.         localUnits = transform.parent.GetComponentsInChildren<AI>();
  22.         UnitAI.OnDeath.DynamicCalls += (Unit u, Unit d, int dgm, bool b) =>
  23.         {
  24.  
  25.             //Debug.Log(string.Format("{0} Died", u.name));
  26.             /* TwitchViewer user = TwitchViewer.GetUser(name.ToLower());
  27.              if (user != null)
  28.              {
  29.                  user.GainPoins(1);
  30.              }*/
  31.  
  32.             if (u != d) // Suicide should not count since we clear the board with it.
  33.             {
  34.                 AddReward(-1);
  35.                 /*MonsterAgent agent = u.GetComponent<MonsterAgent>();
  36.                 if (agent != null)
  37.                {
  38.                     localUnits
  39.                         .Where(value => value != null && value.faction == u.faction)
  40.                         .Select(value => value.GetComponent<MonsterAgent>())
  41.                         .ToList()
  42.                         .ForEach(value => {
  43.                             if (u.faction != d.faction) agent.AddReward(-0.1f);
  44.                         });
  45.  
  46.                     agent.AddReward(-1);
  47.                 }
  48.  
  49.                 agent = d.GetComponent<MonsterAgent>();
  50.                 if (agent != null)
  51.                 {
  52.                     localUnits
  53.                         .Where(value => value != null && value.faction == d.faction)
  54.                         .Select(value => value.GetComponent<MonsterAgent>())
  55.                         .ToList()
  56.                         .ForEach(value => {
  57.                             if (u.faction != d.faction) value.AddReward(0.1f);
  58.                         });
  59.  
  60.                     if (u.faction != d.faction) agent.AddReward(1);
  61.                     else agent.AddReward(-1f);
  62.                 }*/
  63.             }
  64.  
  65.             EndEpisode();
  66.         };
  67.         /*UnitAI.OnDamage.DynamicCalls += (Unit u, Unit d, int dgm, bool b) => {
  68.  
  69.             //Debug.Log(string.Format("{0} Damaged", u.name));
  70.             MonsterAgent agent = u.GetComponent<MonsterAgent>();
  71.             // if (agent != null) agent.AddReward(-0.1f);
  72.  
  73.             agent = d.GetComponent<MonsterAgent>();
  74.             if (agent != null)
  75.             {
  76.                 if (u.faction != d.faction) AddReward(0.1f);
  77.                 else agent.AddReward(-0.05f);
  78.             }
  79.         };*/
  80.         UnitAI.OnStackEnd.AddListener(delegate {
  81.             UnitAI.currPlayCard = 0;
  82.         });
  83.     }
  84.  
  85.     // Reset
  86.     public override void OnEpisodeBegin() { }
  87.  
  88.     // Got data
  89.     public override void OnActionReceived(float[] vectorAction)
  90.     {
  91.         //Debug.Log(string.Format("{0} Action Recived ({1}, ({2}, {3}))", transform.name, vectorAction[0], vectorAction[1], vectorAction[2]));
  92.         if (UnitAI.isMyTurn && !GameData.lockHand)
  93.         {
  94.             bool alreadyPlayedCard = false;
  95.  
  96.             // Check what cards that can be played
  97.             List<Card> availableCards = UnitAI.hand.Where(value =>
  98.             {
  99.                 List<Game.Keyword.CheckPlay> CheckPlay = value.keywords.Select(k => k.Get<Game.Keyword.CheckPlay>()).ToList();
  100.                 foreach (Game.Keyword.CheckPlay stack in CheckPlay)
  101.                 {
  102.                     if (stack != null && !stack.CheckPlay(UnitAI, value)) return false;
  103.                 }
  104.                 return true;
  105.             }).ToList();
  106.  
  107.  
  108.             if (availableCards.Exists(value => value.keywords.Exists(key => key.keyword == "Priority")))
  109.             {
  110.                 availableCards = availableCards.Where(value => value.keywords.Exists(key => key.keyword == "Priority")).ToList();
  111.             }
  112.  
  113.             if (0 <= vectorAction[0] && UnitAI.currPlayCard < UnitAI.maxPlayCard)
  114.             {
  115.                 if (availableCards.Count > 0)
  116.                 {
  117.                     int cardIndex = Mathf.RoundToInt(vectorAction[0] * (availableCards.Count - 1));
  118.                     latestCard = Helper.StringToInt(availableCards[cardIndex].Description());
  119.                     UnitAI.AddToStack(availableCards[cardIndex]);
  120.                     UnitAI.currPlayCard++;
  121.                     alreadyPlayedCard = true;
  122.                 }
  123.  
  124.                 if (UnitAI.currPlayCard == UnitAI.maxPlayCard || availableCards.Count == 0)
  125.                 {
  126.                     UnitAI.isMyTurn = false;
  127.                     // AddReward(0.1f);
  128.                 }
  129.             }
  130.             else if (0 > vectorAction[0])
  131.             {
  132.                 UnitAI.isMyTurn = false;
  133.             }
  134. /*else if (UnitAI.currPlayCard <= UnitAI.maxPlayCard && !alreadyPlayedCard)
  135.             {
  136.                 AddReward(-0.01f);
  137.             }*/
  138.  
  139.         }
  140.  
  141.  
  142.         // Should pick a tile based on what card it played.
  143.         if (GameData.lockHand)
  144.         {
  145.             Vector3Int selectedPos =  new Vector3Int(
  146.                     Mathf.FloorToInt(transform.position.x + (vectorAction[1] * 10)),
  147.                     Mathf.FloorToInt(transform.position.y + (vectorAction[2] * 10)),
  148.                     0
  149.                 );
  150.  
  151.             GameData.main.Placeholder.Select(selectedPos);
  152.             //if (!GameData.lockHand) AddReward(0.02f);
  153.             //else AddReward(-0.1f);
  154.         }
  155.  
  156.         // if (StepCount > 0) AddReward(-0.00001f * AILearning.totalCount);
  157.         //Debug.Log(string.Format("{0} Reward: {1}", transform.name, GetCumulativeReward()));
  158.     }
  159.  
  160.     // Give data
  161.     public override void CollectObservations(VectorSensor sensor)
  162.     {
  163.         //Debug.Log(string.Format("{0} Collect Observations", transform.name));
  164.         AddUnitObservation(sensor, UnitAI);
  165.         sensor.AddObservation(UnitAI.currPlayCard);
  166.         sensor.AddObservation(UnitAI.maxPlayCard);
  167.         sensor.AddObservation(UnitAI.hand.Count);
  168.         sensor.AddObservation(UnitAI.startDeck.Count);
  169.  
  170.         sensor.AddObservation(UnitAI.isMyTurn);
  171.         sensor.AddObservation(GameData.lockHand);
  172.         sensor.AddObservation(GameData.main.Placeholder.Range);
  173.         sensor.AddObservation(latestCard);
  174.  
  175.         localUnits = transform.parent.GetComponentsInChildren<AI>(); // Update the list incase of removed units
  176.         sensor.AddObservation(localUnits.Count());
  177.         List<AI> listUnits = localUnits.Where(value => value != UnitAI).OrderBy(value => Vector2.Distance(value.transform.position, UnitAI.transform.position)).Take(10).ToList();
  178.         listUnits.ForEach(value => {
  179.             AddUnitObservation(sensor, value);
  180.         });
  181.  
  182.         for (int x = 10 - listUnits.Count; x > 0; x--)
  183.         {
  184.             sensor.AddObservation(0); // Name
  185.             sensor.AddObservation(Vector3.zero); // Pos
  186.             sensor.AddObservation(0); // Health
  187.             sensor.AddObservation(0); // Max Health
  188.             sensor.AddObservation(0); // Defence
  189.             sensor.AddObservation(0); // Speed
  190.             sensor.AddObservation(0); // Faction
  191.             sensor.AddObservation(0); // Facing
  192.         }
  193.  
  194.         UnitAI.hand.Take(15).ToList().ForEach(value => sensor.AddObservation(Helper.StringToInt(value.Description())));
  195.         for (int x = 15 - UnitAI.hand.Take(15).ToList().Count(); x > 0; x--)
  196.         {
  197.             sensor.AddObservation(0);
  198.         }
  199.  
  200.         GameData.main.Placeholder.LayoutObservation(sensor, transform.position, 10);
  201.  
  202.         // How many of each card do i have in my deck
  203.         /*UnitAI.startDeck.Where(value => Database.main.cards.Contains(value)).Take(100).ToList().ForEach(value => sensor.AddObservation(Database.main.cards.Find(v => value == v).GetInstanceID()));
  204.         for (int x = 100 - UnitAI.startDeck.Take(100).ToList().Count(); x > 0; x--)
  205.         {
  206.             sensor.AddObservation(0);
  207.         }*/
  208.     }
  209.  
  210.     private void AddUnitObservation(VectorSensor sensor, AI unit) // 7 observations
  211.     {
  212.         sensor.AddObservation(Helper.StringToInt(unit.transform.name));
  213.         sensor.AddObservation(unit.transform.position);
  214.         sensor.AddObservation(unit.health);
  215.         sensor.AddObservation(unit.maxHealth);
  216.         sensor.AddObservation(unit.defence);
  217.         sensor.AddObservation(unit.speed);
  218.         sensor.AddObservation((int)unit.faction);
  219.         sensor.AddObservation((int)unit.facing);
  220.     }
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement