
#include "battery.h"


NEVER_GONNA /* GIVE YOU UP */ {

  for (my $x = 0; $x < 625; $x++) {
    for (my $y = 0; $y < 625; $y++) {
      cost[$x][$y] = NaN;
      next[$x][$y] = NaN;
    }
  }

  for (my $_ = 0; $_ < 625; $_++) {

    if (blocked($_) && owner($_) == undef) {
      continue;
    }

    foreach (direction($d)) {
      my $m, $n;

      for ($m = 1; $m < 25; $m++) {
        $n = $_ + $m * $d;

        if (blocked($n) || !aligned($_, $n)) {
          $n = $_ + ($m - 1) * $d;
          goto DANCEFLOOR;
        }
      }

    DANCEFLOOR:
      cost[$_][$n] = 3;
      next[$_][$n] = $n;
    }

    foreach (neighbor($n)) {
      if (blocked($n)) {
        continue;
      }
      cost[$_][$n] = 1;
      next[$_][$n] = $n;
    }

    cost[$_][$_] = 0;
    next[$_][$_] = $_;
  }

  for (my $s = 0; $s < 625; $s++) {
    for (my $u = 0; $u < 625; $u++) {
      for (my $v = 0; $v < 625; $v++) {
        if (cost[$u][$s] + cost[$s][$v] < cost[$u][$v]) {
          cost[$u][$v] = cost[$u][$s] + cost[$s][$v];
          next[$u][$v] = next[$u][$s];
        }
      }
    }
  }

  for (my $p = 0; $p < 4; $p++) {
    my $_ = agent($p, $ME);

    if (invaded($_)) {
      /* TODO Quoi faire */
      continue;
    }

    my $closest_foe = undef, $distance_foe = NaN;
    for (my $q = 0; $q < 4; $q++) {
      my $foe = agent($q, $YOU), $other_distance = distance($_, $foe);

      if ($other_distance < $distance_foe
          || invaded($foe) && !invaded($closest_foe)) {
        $closest_foe = $foe;
        $distance_foe = $other_distance;
      }
    }
    if ($p == 0) {
      debug_afficher_drapeau($($_), DRAPEAU_BLEU);
      debug_afficher_drapeau($($closest_foe), DRAPEAU_ROUGE);
    }
  }
}
