Advertisement
Huvaakoodia

Desert Strife AI - Version 3

Sep 6th, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class AI
  6. {
  7.     public class AIMove
  8.     {
  9.         public ArmyView Army;
  10.         public TileView Target;
  11.         public float Distance;
  12.  
  13.         public AIMove(ArmyView army, TileView target, float distance)
  14.         {
  15.             Army = army;
  16.             Target = target;
  17.             Distance = distance;
  18.         }
  19.     }
  20.  
  21.     public static AIMove RunAI(List<ArmyView> allies, List<ArmyView> enemies, TileView[,] world)
  22.     {
  23.         if (enemies.Count == 0) return null;
  24.  
  25.         var moves = new List<AIMove>();
  26.         foreach (var ally in allies)
  27.         {
  28.             if (ally.Disabled) continue;
  29.             if (!ally.CanMove) continue;
  30.             if (ally.TechnologyExcavationIndex > 0) continue;//always excavate to the end!
  31.  
  32.             //find closest enemy
  33.             ArmyView closestEnemy = null;
  34.             float closestEnemyDistance = float.MaxValue;
  35.             foreach (var enemy in enemies)
  36.             {
  37.                 //can find path to target?
  38.                 if (PathFinder.FindPath(world, ally.CurrentTile.Position, enemy.CurrentTile.Position, ally.TeamID) == null)
  39.                     continue;
  40.  
  41.                 //check if closest target
  42.                 float distance = Distance(ally, enemy);
  43.                 if (distance < closestEnemyDistance)
  44.                 {
  45.                     closestEnemyDistance = distance;
  46.                     closestEnemy = enemy;
  47.                 }
  48.             }
  49.  
  50.             //find closest technology
  51.             TileView closestTech = null;
  52.             float closestTechDistance = float.MaxValue;
  53.             foreach (var tile in world)
  54.             {
  55.                 //only check legal targets
  56.                 if (tile.Type != TileView.TypeID.Technology || tile.TechnologyCount == 0 || tile.CurrentArmy != null)
  57.                     continue;
  58.  
  59.                 //can find path to target?
  60.                 if (PathFinder.FindPath(world, ally.CurrentTile.Position, tile.Position, ally.TeamID) == null)
  61.                     continue;
  62.  
  63.                 //check if closest target
  64.                 float distance = Distance(ally.CurrentTile, tile);
  65.                 if (distance < closestTechDistance)
  66.                 {
  67.                     closestTechDistance = distance;
  68.                     closestTech = tile;
  69.                 }
  70.             }
  71.  
  72.             //choose target -> enemy is default target
  73.             TileView targetTile = closestEnemy.CurrentTile;
  74.             float targetDistance = closestEnemyDistance;
  75.  
  76.             //go for tech if it is far away enough from closest enemy
  77.             if (closestTech != null && closestTechDistance + 2 < closestEnemyDistance)
  78.             {
  79.                 targetTile = closestTech;
  80.                 targetDistance = closestTechDistance;
  81.             }
  82.  
  83.             moves.Add(new AIMove(ally, targetTile, targetDistance));
  84.         }
  85.  
  86.         //choose best move -> attacks first, tech second
  87.         bool bestIsAttack = false;
  88.         float bestDistance = float.MaxValue;
  89.         AIMove bestMove = null;
  90.         for (int i = 0; i < moves.Count; i++)
  91.         {
  92.             var move = moves[i];
  93.             if (moves[i].Target.CurrentArmy != null)
  94.             {
  95.                 //attack
  96.                 if (move.Distance < bestDistance)
  97.                 {
  98.                     bestIsAttack = true;
  99.                     bestDistance = move.Distance;
  100.                     bestMove = move;
  101.                 }
  102.             }
  103.             else if (!bestIsAttack)
  104.             {
  105.                 //tech
  106.                 if (move.Distance < bestDistance)
  107.                 {
  108.                     bestMove = move;
  109.                     bestDistance = move.Distance;
  110.                 }
  111.             }
  112.         }
  113.         return bestMove;
  114.     }
  115.  
  116.     private static float Distance(ArmyView army1, ArmyView army2)
  117.     {
  118.         return Distance(army1.CurrentTile, army2.CurrentTile);
  119.     }
  120.  
  121.     private static float Distance(TileView tile1, TileView tile2)
  122.     {
  123.         return Vector2.Distance(tile1.Position, tile2.Position);
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement