#!/usr/bin/perl

die "usage: $0 <cities|plotfile> <population>" unless @ARGV == 2;
my ($points, $population) = @ARGV;

use AI::NaturalSelection::Plot2;

my $plot = AI::NaturalSelection::Plot2->new();
if ($points =~ /^\d+$/) {
  $plot->createPlot($points, $points + 10, $points + 10); # create a random plot
}
else {
  $plot->setPlot(&readPlot($points));
}

$plot->drawPlot();
print "\n";

$plot->setTour(0);
$plot->localOptimizeTour(0);
$plot->drawTour(0);
print "\n";
sub readPlot {
  my $file = shift;
  my @points;
  open(FILE, "<$file") || die "Can't read $file: $!";
      
  my $x = my $maxx = my $maxy = 0;
  while (<FILE>) {
    s/\s//g;
    next unless /[.X]/;
        
    for (my $y = 0; s/^(.)//; $y++) {
      if ($1 eq "X") {
        push(@points, [$x, $y]);
        $maxy++ if $x == 0;
      }
      elsif ($1 eq ".") {
        $maxy++ if $x == 0;
      }
    }
 
    $x++;
    $maxx++;
  }
  close(FILE);
  return (\@points, $maxx, $maxy);
}
