/// <summary> /// Chose Nearest target to all army squads /// </summary> /// <param name="Army"></param> /// <param name="Targets"></param> /// <returns></returns> protected static int NearestToAll(Squad[] Army, Squad[] Targets) { int Temp = -1; double[] distances = new double[Targets.Length]; for (int i = 0; i < Targets.Length; i++) { for (int j = 0; j < Army.Length; j++) { distances[i] += DistanceAndPath.DistanceTo(Army[j].Position, Targets[i].Position); } } double min = double.MaxValue; for (int i = 0; i < distances.Length; i++) { if (distances[i] < min) { min = distances[i]; Temp = i; } } return(Temp); }
protected static int NearestToPoint(Point p1, Squad[] Army) { int Temp = -1; double minDistance = Double.MaxValue; for (int i = 0; i < Army.Length; i++) { double distance = DistanceAndPath.DistanceTo(p1, Army[i].Position); if (distance < minDistance) { minDistance = distance; Temp = i; } } return(Temp); }
static void Surround(BattleData battleData) { if (battleData.EnemyArmy.Length < 1) { return; } for (int i = 0; i < battleData.AllyArmy.Length; i++) { int TargetIndex = Strategy.NearestToAll(battleData.AllyArmy, battleData.EnemyArmy); Step[] Path = DistanceAndPath.PathTo( battleData, battleData.AllyArmy[i].Position, battleData.EnemyArmy[TargetIndex].Position, battleData.AllyArmy[i].Unit.Range); Strategy.MoveAndAttack(battleData.AllyArmy[i], battleData.EnemyArmy[TargetIndex], Path, battleData); } }
static void HitAndRun(BattleData battleData) { if (battleData.EnemyArmy.Length < 1) { return; } for (int i = 0; i < battleData.AllyArmy.Length; i++) { int TargetIndex = Strategy.NearestToPoint(battleData.AllyArmy[i].Position, battleData.EnemyArmy); Step[] Path = DistanceAndPath.PathTo( battleData, battleData.AllyArmy[i].Position, battleData.EnemyArmy[TargetIndex].Position, battleData.AllyArmy[i].Unit.Range); if (Path != null) { if (Path.Length == 0) { Point SafePoint = GetSafeFrom(battleData.AllyArmy[i].Position, battleData.EnemyArmy[TargetIndex].Position); Path = DistanceAndPath.PathTo( battleData, battleData.AllyArmy[i].Position, SafePoint, 0); Strategy.AttackAndMove(battleData.AllyArmy[i], battleData.EnemyArmy[TargetIndex], Path, battleData); } else { Strategy.MoveAndAttack(battleData.AllyArmy[i], battleData.EnemyArmy[TargetIndex], Path, battleData); } } } }
protected static Point GetSafeFrom(Point Victum, Point Enemy) { Point temp = new Point(Victum.X - Enemy.X, Victum.Y - Enemy.Y); List <Point> SafePlaces = new List <Point>(); if (temp.X <= 0) { SafePlaces.Add(new Point(0, Victum.Y)); } if (temp.X >= 0) { SafePlaces.Add(new Point(99, Victum.Y)); } if (temp.Y <= 0) { SafePlaces.Add(new Point(Victum.X, 0)); } if (temp.Y >= 0) { SafePlaces.Add(new Point(Victum.X, 99)); } double max = double.MinValue; foreach (var item in SafePlaces) { double dist = DistanceAndPath.DistanceTo(Victum, item); if (dist > max) { max = dist; temp = item; } } return(temp); }