daily pastebin goal
48%
SHARE
TWEET

Desert Strife AI - Version 2

Huvaakoodia Sep 6th, 2016 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  12.         public AIMove(ArmyView army, TileView target)
  13.         {
  14.             Army = army;
  15.             Target = target;
  16.         }
  17.     }
  18.  
  19.     public static AIMove RunAI(List<ArmyView> allies, List<ArmyView> enemies, TileView[,] world)
  20.     {
  21.         foreach (var ally in allies)
  22.         {
  23.             if (!ally.CanMove) continue;
  24.             if (ally.TechnologyExcavationIndex > 0) continue;//always excavate to the end!
  25.  
  26.             //find closest enemy
  27.             ArmyView closestEnemy = null;
  28.             float closestEnemyDistance = float.MaxValue;
  29.             foreach (var enemy in enemies)
  30.             {
  31.                 //can find path to target?
  32.                 if (PathFinder.FindPath(world, ally.CurrentTile.Position, enemy.CurrentTile.Position, ally.TeamID) == null)
  33.                     continue;
  34.  
  35.                 //check if closest target
  36.                 float distance = Distance(ally, enemy);
  37.                 if (distance < closestEnemyDistance)
  38.                 {
  39.                     closestEnemyDistance = distance;
  40.                     closestEnemy = enemy;
  41.                 }
  42.             }
  43.  
  44.             //find closest technology
  45.             TileView closestTech = null;
  46.             float closestTechDistance = float.MaxValue;
  47.             foreach (var tile in world)
  48.             {
  49.                 //only check legal targets
  50.                 if (tile.Type != TileView.TypeID.Technology || tile.TechnologyCount == 0 || tile.CurrentArmy != null) continue;
  51.  
  52.                 //can find path to target?
  53.                 if (PathFinder.FindPath(world, ally.CurrentTile.Position, tile.Position, ally.TeamID) == null)
  54.                     continue;
  55.  
  56.                 //check if closest target
  57.                 float distance = Distance(ally.CurrentTile, tile);
  58.                 if (distance < closestTechDistance)
  59.                 {
  60.                     closestTechDistance = distance;
  61.                     closestTech = tile;
  62.                 }
  63.             }
  64.  
  65.             //choose target -> enemy is default target
  66.             TileView targetTile = closestEnemy.CurrentTile;
  67.             //go for tech if it is far away enough from closest enemy
  68.             if (closestTech != null && closestTechDistance + 2 < closestEnemyDistance) targetTile = closestTech;
  69.  
  70.             return new AIMove(ally, targetTile);
  71.         }
  72.         return null;
  73.     }
  74.  
  75.     private static float Distance(ArmyView army1, ArmyView army2)
  76.     {
  77.         return Distance(army1.CurrentTile, army2.CurrentTile);
  78.     }
  79.  
  80.     private static float Distance(TileView tile1, TileView tile2)
  81.     {
  82.         return Vector2.Distance(tile1.Position, tile2.Position);
  83.     }
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top