# HG changeset patch # User "Rex Tsai " # Date 1228112588 -28800 # Node ID f81c4f9d267fe6729136e8a3bdbb3743bde2c3e9 # Parent 3730817e3079d77fb3fa54868d75389619045cc9 make the travel time is sort by distance diff -r 3730817e3079 -r f81c4f9d267f enemy.pl --- a/enemy.pl Mon Dec 01 14:15:01 2008 +0800 +++ b/enemy.pl Mon Dec 01 14:23:08 2008 +0800 @@ -16,19 +16,65 @@ return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))+1) * (400/$speed)); } +sub travelMatrix { + my $userName = shift; + my @cities = (); + if(my $user = Ikariam::User->retrieve(name => $userName)) { + + foreach my $target ($user->cities) { + my $island = Ikariam::Island->retrieve($target->island); + my $city; + $city->{id} = $target->id; + $city->{name} = $target->cityname; + $city->{island}->{id} = $island->id; + $city->{island}->{x} = $island->x; + $city->{island}->{y} = $island->y; + + push (@cities, $city); + } + } + + my $n = $#cities; + + # Print them out. + # header + printf("Defence\n"); + printf(" %5s ", undef); + for my $y (0..$n) { + printf ("%5s ", $cities[$y]->{id}); + } + print "\n"; + + # body + for my $x (0..$n) { + printf(" %5s ", $cities[$x]->{id}); + my $total = 0; + for my $y (0..$n) { + my $t = travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $cities[$y]->{island}->{x}, $cities[$y]->{island}->{y}); + printf("%5s ", $t); + $total += $t; + } + printf(" %s", $cities[$x]->{name}); + printf("\n"); + } + printf("\n"); +} + sub myTownToVicim { my $islandId = shift; - my @cities = (); - foreach (glob("city-*-dump.yaml")) { - my $city = LoadFile($_); - push (@cities, $city); - } - my $n = $#cities; + if(my $island = Ikariam::Island->retrieve($islandId)) { + my @cities = (); + foreach (glob("city-*-dump.yaml")) { + my $city = LoadFile($_); + $city->{distance} = travelTime($island->x, $island->y, $city->{island}->{x}, $city->{island}->{y}, 20); + push (@cities, $city); + } + @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities); - if(my $island = Ikariam::Island->retrieve($islandId)) { + my $n = $#cities; for my $x (0..$n) { - printf("%s\t- ", $cities[$x]->{name}); + printf("%s (%d)\t- ", $cities[$x]->{name}, $cities[$x]->{id}); printf("Land %3d Ship %3d Catapult %3d Ram %3d\n", travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y), travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 8), @@ -50,6 +96,9 @@ foreach my $i (qw/name score trader_score_secondary army_score_main ally/) { printf("%s: %s\n", $i, $u->get($i)); } + + travelMatrix($ARGV[0]); + printf("\nCities\n"); foreach my $city ($u->cities) { my $island = Ikariam::Island->retrieve($city->island);