Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class AI
- {
- public class AIMove
- {
- public ArmyView Army;
- public TileView Target;
- public float Distance;
- public AIMove(ArmyView army, TileView target, float distance)
- {
- Army = army;
- Target = target;
- Distance = distance;
- }
- }
- public static AIMove RunAI(List<ArmyView> allies, List<ArmyView> enemies, TileView[,] world)
- {
- if (enemies.Count == 0) return null;
- var moves = new List<AIMove>();
- foreach (var ally in allies)
- {
- if (ally.Disabled) continue;
- if (!ally.CanMove) continue;
- if (ally.TechnologyExcavationIndex > 0) continue;//always excavate to the end!
- //find closest enemy
- ArmyView closestEnemy = null;
- float closestEnemyDistance = float.MaxValue;
- foreach (var enemy in enemies)
- {
- //can find path to target?
- if (PathFinder.FindPath(world, ally.CurrentTile.Position, enemy.CurrentTile.Position, ally.TeamID) == null)
- continue;
- //check if closest target
- float distance = Distance(ally, enemy);
- if (distance < closestEnemyDistance)
- {
- closestEnemyDistance = distance;
- closestEnemy = enemy;
- }
- }
- //find closest technology
- TileView closestTech = null;
- float closestTechDistance = float.MaxValue;
- foreach (var tile in world)
- {
- //only check legal targets
- if (tile.Type != TileView.TypeID.Technology || tile.TechnologyCount == 0 || tile.CurrentArmy != null)
- continue;
- //can find path to target?
- if (PathFinder.FindPath(world, ally.CurrentTile.Position, tile.Position, ally.TeamID) == null)
- continue;
- //check if closest target
- float distance = Distance(ally.CurrentTile, tile);
- if (distance < closestTechDistance)
- {
- closestTechDistance = distance;
- closestTech = tile;
- }
- }
- //choose target -> enemy is default target
- TileView targetTile = closestEnemy.CurrentTile;
- float targetDistance = closestEnemyDistance;
- //go for tech if it is far away enough from closest enemy
- if (closestTech != null && closestTechDistance + 2 < closestEnemyDistance)
- {
- targetTile = closestTech;
- targetDistance = closestTechDistance;
- }
- moves.Add(new AIMove(ally, targetTile, targetDistance));
- }
- //choose best move -> attacks first, tech second
- bool bestIsAttack = false;
- float bestDistance = float.MaxValue;
- AIMove bestMove = null;
- for (int i = 0; i < moves.Count; i++)
- {
- var move = moves[i];
- if (moves[i].Target.CurrentArmy != null)
- {
- //attack
- if (move.Distance < bestDistance)
- {
- bestIsAttack = true;
- bestDistance = move.Distance;
- bestMove = move;
- }
- }
- else if (!bestIsAttack)
- {
- //tech
- if (move.Distance < bestDistance)
- {
- bestMove = move;
- bestDistance = move.Distance;
- }
- }
- }
- return bestMove;
- }
- private static float Distance(ArmyView army1, ArmyView army2)
- {
- return Distance(army1.CurrentTile, army2.CurrentTile);
- }
- private static float Distance(TileView tile1, TileView tile2)
- {
- return Vector2.Distance(tile1.Position, tile2.Position);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement