diff planner.pl @ 365:2444685c68c9

merged multi-attacks script
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 01 Mar 2009 11:10:32 +0800
parents 58351fccf619
children 95148fdc1021
line wrap: on
line diff
--- a/planner.pl	Sun Mar 01 11:10:10 2009 +0800
+++ b/planner.pl	Sun Mar 01 11:10:32 2009 +0800
@@ -4,6 +4,7 @@
 use Decision::ParseTree q{ParseTree};
 use YAML qw/LoadFile Dump DumpFile/;
 use Getopt::Std;
+use POSIX qw(strftime);
 
 Ikariam::Cities->has_a(island => 'Ikariam::Island');
 
@@ -34,6 +35,12 @@
     "DivingBoat" => 212,
 };
 
+
+sub calcTime {
+    my ($gooff, $travelTime) = @_;
+    return (sprintf("%s-%s", strftime("%H:%M", localtime(time+$gooff*60)), strftime("%H:%M", localtime(time+($gooff+$travelTime)*60))));
+}
+
 # return minutes.
 sub travelTime {
     my ($x1, $y1, $x2, $y2, $speed) = @_;
@@ -47,39 +54,59 @@
 
 
 package main;
-getopts('w');
+
 if($#ARGV < 2) {
-    die("Usage: %s targetCity navyCity armyCity wingman1.. wingman2..\n");
+    die("Usage: %s -n 1 -a 1 -v 21 targetCity navyCity armyCity wingman1.. wingman2..\n");
 }
+getopts('wa:n:v:');
+$opt_n = defined($opt_n) ? $opt_n : 1;
+$opt_a = defined($opt_a) ? $opt_a : 1;
+my $vanguard =  defined($opt_v) ? $opt_v : 21;
+
 my $targetCityId = shift(@ARGV);
-my $navyCity = shift(@ARGV);
-my $armyCity = shift(@ARGV);
-my @wingman = @ARGV;
-my $vanguard = 21;
-
 my $targetCity = Ikariam::Cities->retrieve($targetCityId);
-
+my @cities;
 # NAVY
-my $city = LoadFile(sprintf("city-%s-dump.yaml", $navyCity));
-$city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 33) + $vanguard + 8;
-$city->{type} = "navy";
-push (@cities, $city);
+foreach my $i (1..$opt_n) {
+    my $navyCity = shift(@ARGV);
+    my $city = LoadFile(sprintf("city-%s-dump.yaml", $navyCity));
+    $city->{travelTime} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 33);
+    $city->{distance}   = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 33) + $vanguard;
+    if($i == 1) {
+        $city->{distance} += 8;
+     } else {
+        $city->{distance} += 5;
+     }
+    $city->{type} = "navy";
+    $city->{queue} = "navy" . $i;
+    push (@cities, $city);
+}
 
 # ARMY
-my $city = LoadFile(sprintf("city-%s-dump.yaml", $armyCity));
-$city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60) + $vanguard + 5;
-$city->{type} = "army";
-push (@cities, $city);
+foreach my $i (1..$opt_a) {
+    my $armyCity = shift(@ARGV);
+    my $city = LoadFile(sprintf("city-%s-dump.yaml", $armyCity));
+    $city->{travelTime} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60);
+    $city->{distance}   = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60) + $vanguard;
+    if($i == 1) {
+        $city->{distance} += 5;
+    } else {
+        $city->{distance} += 2;
+    }
+    $city->{type} = "army";
+    $city->{queue} = "army" . $i;
+    push (@cities, $city);
+}
 
 # wingman, army
-foreach my $i (0..$#wingman) {
-    my $cityId = $wingman[$i];
+while(defined($cityId = shift(@ARGV))) {
     my $city = LoadFile(sprintf("city-%s-dump.yaml", $cityId));
-    $city->{type} = "wingman" . ($i+1);
+    $city->{type} = "wingman";
+    $city->{queue} = "wingman";
+    $city->{travelTime} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60);
+    $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60);
     if(defined($opt_w)) {
-        $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60) - ($i * 20 + 20);
-    } else {
-        $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60);
+            $city->{distance} -= ($i * 20 + 20);
     }
     push (@cities, $city);
 }
@@ -90,27 +117,40 @@
 use strict;
 use Ikariam;
 use Data::Dumper;
+use POSIX qw(strftime);
+
+sub calcTime {
+    my (\$gooff, \$travelTime) = \@_;
+    return (sprintf("%s-%s", strftime("%H:%M", localtime(time+\$gooff*60)), strftime("%H:%M", localtime(time+(\$gooff+\$travelTime)*60))));
+}
+
 package main;
 my \$ikariam = new Ikariam(\$::server, \$::user, \$::pass);
 EOF
 
-foreach(@cities) {
-    printf("# %s %s (%s)\n", $_->{type}, $_->{id}, $_->{distance});
-}
-@cities = sort { $a->{distance} <=> $b->{distance}; } (@cities);
-
 printf ("# %s %s [%s:%s]\n", $targetCity->owner, $targetCity->cityname, $targetCity->island->x, $targetCity->island->y);
 printf ("# http://%s/index.php?view=island&id=%d&selectCity=%d\n", $::server, $targetCity->island->id, $targetCity->id);
+printf("#\n");
 
-for(my $i = ($#cities + 1) ; $i-- ; $i < 0 ) {
+@cities = sort { $b->{distance} <=> $a->{distance}; } (@cities);
+foreach my $i (0..$#cities) {
+    printf('printf("%%s %s %s %s %sm\n", calcTime(%s - %s, %s));',
+        $cities[$i]->{queue}, $cities[$i]->{name}, $cities[$i]->{id}, $cities[$i]->{travelTime},
+        $cities[0]->{distance}, $cities[$i]->{distance}, $cities[$i]->{travelTime});
+    printf(" # %s\n", calcTime($cities[0]->{distance} - $cities[$i]->{distance}, $cities[$i]->{travelTime}));
+}
+printf("printf('Press Enter to start...');\n");
+printf("<STDIN>;\n");
 
-    if($i ne $#cities) {
+foreach my $i (0..$#cities) {
+    if($i ne 0) {
         # TODO, 計算港口速度
         # 減掉 cities[$i+1]->buliding->port 的速度
         printf("sleep(%d*60);\n",
-                ($cities[$i+1]->{distance} - $cities[$i]->{distance}));
+                ($cities[$i-1]->{distance} - $cities[$i]->{distance}));
     }
-    printf("\n# %s %s %dm\n", $cities[$i]->{name}, $cities[$i]->{type}, $cities[$i]->{distance});
+
+    printf("\n# %s %s %dm\n", $cities[$i]->{name}, $cities[$i]->{queue}, $cities[$i]->{travelTime});
     printf("# ActionPoint %d/%d\n", $cities[$i]->{actionPoints}, $cities[$i]->{maxActionPoints});
 
     printf('$ikariam->login;' . "\n");
@@ -129,10 +169,10 @@
         printf('$ikariam->plunderCity(%d, {' . "\n", $targetCity->id);
         while (my ($k, $v) = each(%{$cities[$i]->{army}})) {
             if($v > 0) {
-                printf("\tcargo_army_%s => %s, # %s (%d)\n", $army->{$k}, 0, $k, $v);
+                printf("\tcargo_army_%s => %s, # %s (%d)\n", $army->{$k}, $v, $k, $v);
             }
         }
-        printf("\ttransporter   => %s, # transporter\n", $cities[$i]->{transporters}->{avail});
+        printf("\ttransporter   => 0, # transporter (%s)\n", $cities[$i]->{transporters}->{avail});
         printf('});' . "\n", $cities[$i]->{id});
     }
     printf('$ikariam->logout;' . "\n");