#!/usr/bin/perl -w

if($#ARGV != 5){
  print "Useage: auvgen [-s|-t|-n|-u|-f <filename>]
                <seed> <#auvs> <#waypoints> <#objectives> <#cameras> <#goals>\n";
  exit;
} else {
  $seed = shift @ARGV;
  $auvs = shift @ARGV;
  $waypoints = shift @ARGV;
  $objectives = shift @ARGV;
  $cameras = shift @ARGV;
  $goals = shift @ARGV;
  
}

open IN, "./rovergen $seed -t $auvs $waypoints $objectives $cameras $goals | ";
$at_ship = -1;

$links = {};
$visible_from = {};

srand($seed);
$in_initial = -1;
$goal = "";
while(<IN>){
  #print;

  if(s/:init/:init\n\t(ready)\n\t(= (drift-rate) 0.5)/){
    $in_initial = 1;
  }
  s/rover/auv/g;
  #s/equipped_for_rock_analysis/equipped_with_gulper/g;
  s/.*equipped_for_rock_analysis.*//g;
  s/store_of/on_board_gulper/g;
  s/store/gulper/g;
  s/Store/gulper/g;
  s/general/ship1/g;
  s/Lander/ship/g;
  s/Rover/auv/g;

  if($in_initial == -1 && m/ - auv/){
    print;
    s/auv/torch/g;
  }
 
  s/at auv(.*) waypoint(.*)\)/at auv$1 waypoint$2)\n\t(on_board_torch torch$1 auv$1)\n\t(off torch$1)\n\t(= (distance-from-waypoint auv$1) 0)\n\t(= (recharge-rate auv$1) 10)/;
  s/on_board camera/on_board_camera camera/g;
  s/channel_free (.*)/channel_free)/;
  if(m/at_lander ship1 waypoint(.*)\)/){
    print "\t(at_surface waypoint$1)\n";
    $at_ship = $1;
    $at_ship=~s/\)//;
    s/at_lander ship(\d*)/is-ship/;
  }
  if(m/Mode/ || m/in_sun/ || m/supports/ || m/visible waypoint/){
    $_ = "";
  }
  if(s/can_traverse (.*) waypoint(.*) waypoint(.*)/can_traverse waypoint$2 waypoint$3/){
    $key = "$2_$3";
    if(! (defined $links{$key}) ){
      $dist = int(rand(10)) + 1;
      $links{$key} = $dist;
      $revkey = "$3_$2";
      $links{$revkey} = $dist;
    }
    print "\t(= (distance waypoint$2 waypoint$3 $links{$key})\n";
  }
  if(m/Objective/){
      $copy = $_;
      while($copy=~s/\s*objective(\d*)(.*)/$2/){
	$additions .= "\t(= (light-level fish$1) 0)\n";
      }
    }
  s/objective/fish/g;
  s/rock_sample/water_sample/g;
  s/communicated_image_data (.*) colour/communicated_image_data $1/;
  s/communicated_image_data (.*) (.*)_res/communicated_image_data $1/;
  s/at auv(.*) waypoint(.*)/at auv$1 waypoint$at_ship)/;
  s/have_rock_sample/have_water_sample/;
  if(m/visible_from (.*) (.*)\)/){
        if(defined ($visible_from{$1})){
            $visible_from{$1} = $visible_from{$1} + 1;
        } else{
            $visible_from{$1} = 1;
        }
  }

  s/communicated_rock_data waypoint(.*)/have_water_sample waypoint$1/;
  if(m/soil/){
      $_ = "";
  }
  if(m/= \(energy auv.*\) (.*)\)/){
      #$times_ten = $1 * 10;
      $times_ten = 1000;
      s/= \(energy auv(.*)\) 50\)/= (energy auv$1) $times_ten)/;
  }
  
    if(m/Objective/){
        $objects = $_;
        #print "$objects";
        while((!($objects=~m/^ - Objective/)) && $objects=~s/\s*(\S+)\s*(.*) - Objective.*/$2 - Objective/){
            
            #print " $1 ";
            $random = int(rand(3));
            #print "$random\n";
            if($random == 1){
                $additions .= "\t(visible_from_ship $1 ship1)\n";
                if(defined ($visible_from{$1})){
                    $visible_from{$1} = $visible_from{$1}  + 1;
                } else{
                    $visible_from{$1} = 1;
                }
                #print "\t(visible_from_ship $1)\n";
            }
        }
  }
  
  if(m/Waypoint/){
    $objects = $_;
    #print "$objects";
    while((!($objects=~m/^ - Waypoint/)) && $objects=~s/\s*(\S+)\s*(.*) - Waypoint.*/$2 - Waypoint/){            
        $additions .= "\t(free $1)\n";
    }
  }
  
  if(m/^\)$/ && $in_initial == 1){
      print $additions;
      $in_initial = 0;
  }
  if($in_initial == 0){
    $goal = $goal . $_;
    if(m/communicated_image_data fish(.*)\)/){
        $goal = "\t(need_image fish$1)\n" . $goal;
        if($visible_from{"fish$1"} < 2){
            die "About to create unsolveable problem, try a different random seed, current one is $seed";
        } 
    }
    if(m/have_water_sample waypoint(.*)\)/){
        $goal = "\t(need_water_sample waypoint$1)\n" . $goal;
    }
  }else {
       print;
  }

}

print $goal;

