private static ProxyStarcraft.Unit GetTarget(ProxyStarcraft.Unit unit, GameState gameState) { // Only valid for units with exactly one weapon var range = gameState.UnitTypes[unit.Raw.UnitType].Weapons[0].Range; ProxyStarcraft.Unit target = null; foreach (var enemyUnit in gameState.EnemyUnits) { var distance = unit.GetDistance(enemyUnit); if (distance < range) { if (target == null || enemyUnit.Raw.Health < target.Raw.Health || (enemyUnit.Raw.Health == target.Raw.Health && distance < unit.GetDistance(target))) { target = enemyUnit; } } } return(target); }
/// <summary> /// Determines which of the specified units is the closest. /// In the event of a tie, the earliest one in the enumeration is returned. /// Uses GetDistance, and the same question about edge-to-edge distance therefore applies. /// </summary> public static Unit GetClosest(this Unit self, IEnumerable <Unit> others) { Unit closest = null; var minDistance = 99999f; foreach (var other in others) { var distance = self.GetDistance(other); if (distance < minDistance) { closest = other; minDistance = distance; } } return(closest); }