Zgragselus

AI

Sep 15th, 2025
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using Maporino;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class PlayerAI : Player
  7. {
  8.     // Units to operate
  9.     public List<UnitController> units = new List<UnitController>();
  10.  
  11.     // Trade posts
  12.     public List<TradePostController> tradePosts = new List<TradePostController>();
  13.  
  14.     // AI's buildings
  15.     public List<BuildingController> buildings = new List<BuildingController>();
  16.  
  17.     // Enemy buildings
  18.     public List<BuildingController> enemyBuildings = new List<BuildingController>();
  19.  
  20.     // Time between issuing new commands
  21.     public float timeout = 1.0f;
  22.     public float current = 1.0f;
  23.  
  24.     // Is AI awake?
  25.     public bool awake = false;
  26.  
  27.     // Start is called once before the first execution of Update after the MonoBehaviour is created
  28.     void Start()
  29.     {
  30.        
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.         if (awake == false)
  37.         {
  38.             return;
  39.         }
  40.  
  41.         current -= Time.deltaTime;
  42.         if (current < 0.0f)
  43.         {
  44.             current = timeout;
  45.  
  46.             // Check if AI buildings are producing
  47.             foreach (BuildingController building in buildings)
  48.             {
  49.                 if (building != null && building.spawning == false && gold >= 200)
  50.                 {
  51.                     try
  52.                     {
  53.                         gold -= 200;
  54.                         building.spawning = true;
  55.                     }
  56.                     catch
  57.                     {
  58.                         buildings.Remove(building);
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             List<Transform> targets = new List<Transform>();
  64.  
  65.             // Check for trade posts not owned by AI
  66.             foreach (TradePostController tradePost in tradePosts)
  67.             {
  68.                 if (tradePost.owner != this)
  69.                 {
  70.                     targets.Add(tradePost.transform);
  71.                 }
  72.             }
  73.  
  74.             // Add each enemy building to targets (if there are no trade posts anymore)
  75.             if (targets.Count == 0)
  76.             {
  77.                 foreach (BuildingController building in enemyBuildings)
  78.                 {
  79.                     if (building != null)
  80.                     {
  81.                         try
  82.                         {
  83.                             targets.Add(building.transform);
  84.                         }
  85.                         catch
  86.                         {
  87.                             enemyBuildings.Remove(building);
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.  
  93.             // Gather all idle untis
  94.             List<UnitController> idleUnits = new List<UnitController>();
  95.             foreach (UnitController unit in units)
  96.             {
  97.                 try
  98.                 {
  99.                     if (unit != null &&
  100.                         unit.commanded == false &&
  101.                         unit.GetComponent<AttackController>().target == null &&
  102.                         unit.GetComponent<NavMeshAgent>().remainingDistance < 1.0f)
  103.                     {
  104.                         idleUnits.Add(unit);
  105.                     }
  106.                 }
  107.                 catch
  108.                 {
  109.                     units.Remove(unit);
  110.                 }
  111.             }
  112.  
  113.             // Pick a random group of idle units
  114.             int groupSize = Random.Range(1, idleUnits.Count);
  115.  
  116.             // Select a single random target
  117.             if (targets.Count > 0 && idleUnits.Count > 0)
  118.             {
  119.                 Transform target = targets[Random.Range(0, targets.Count)];
  120.                 for (int i = 0; i < groupSize; i++)
  121.                 {
  122.                     if (idleUnits.Count == 0)
  123.                     {
  124.                         break;
  125.                     }
  126.                     int unitIndex = Random.Range(0, idleUnits.Count);
  127.                     UnitController unit = idleUnits[unitIndex];
  128.                     idleUnits.RemoveAt(unitIndex);
  129.                     if (unit != null)
  130.                     {
  131.                         unit.SetTarget(target.position);
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.     }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment