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 AIMove(ArmyView army, TileView target)
- {
- Army = army;
- Target = target;
- }
- }
- public static AIMove RunAI(List<ArmyView> allies, List<ArmyView> enemies, TileView[,] world)
- {
- foreach (var ally in allies)
- {
- 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;
- //go for tech if it is far away enough from closest enemy
- if (closestTech != null && closestTechDistance + 2 < closestEnemyDistance) targetTile = closestTech;
- return new AIMove(ally, targetTile);
- }
- return null;
- }
- 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