comparison enemy.pl @ 273:f81c4f9d267f

make the travel time is sort by distance
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 01 Dec 2008 14:23:08 +0800
parents 3730817e3079
children 2590bcd517a5
comparison
equal deleted inserted replaced
272:3730817e3079 273:f81c4f9d267f
14 my ($x1, $y1, $x2, $y2, $speed) = @_; 14 my ($x1, $y1, $x2, $y2, $speed) = @_;
15 $speed = 20 unless(defined($speed)); 15 $speed = 20 unless(defined($speed));
16 return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))+1) * (400/$speed)); 16 return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))+1) * (400/$speed));
17 } 17 }
18 18
19 sub travelMatrix {
20 my $userName = shift;
21 my @cities = ();
22 if(my $user = Ikariam::User->retrieve(name => $userName)) {
23
24 foreach my $target ($user->cities) {
25 my $island = Ikariam::Island->retrieve($target->island);
26 my $city;
27 $city->{id} = $target->id;
28 $city->{name} = $target->cityname;
29 $city->{island}->{id} = $island->id;
30 $city->{island}->{x} = $island->x;
31 $city->{island}->{y} = $island->y;
32
33 push (@cities, $city);
34 }
35 }
36
37 my $n = $#cities;
38
39 # Print them out.
40 # header
41 printf("Defence\n");
42 printf(" %5s ", undef);
43 for my $y (0..$n) {
44 printf ("%5s ", $cities[$y]->{id});
45 }
46 print "\n";
47
48 # body
49 for my $x (0..$n) {
50 printf(" %5s ", $cities[$x]->{id});
51 my $total = 0;
52 for my $y (0..$n) {
53 my $t = travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $cities[$y]->{island}->{x}, $cities[$y]->{island}->{y});
54 printf("%5s ", $t);
55 $total += $t;
56 }
57 printf(" %s", $cities[$x]->{name});
58 printf("\n");
59 }
60 printf("\n");
61 }
62
19 sub myTownToVicim { 63 sub myTownToVicim {
20 my $islandId = shift; 64 my $islandId = shift;
21 65
22 my @cities = (); 66 if(my $island = Ikariam::Island->retrieve($islandId)) {
23 foreach (glob("city-*-dump.yaml")) { 67 my @cities = ();
24 my $city = LoadFile($_); 68 foreach (glob("city-*-dump.yaml")) {
25 push (@cities, $city); 69 my $city = LoadFile($_);
26 } 70 $city->{distance} = travelTime($island->x, $island->y, $city->{island}->{x}, $city->{island}->{y}, 20);
27 my $n = $#cities; 71 push (@cities, $city);
72 }
73 @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities);
28 74
29 if(my $island = Ikariam::Island->retrieve($islandId)) { 75 my $n = $#cities;
30 for my $x (0..$n) { 76 for my $x (0..$n) {
31 printf("%s\t- ", $cities[$x]->{name}); 77 printf("%s (%d)\t- ", $cities[$x]->{name}, $cities[$x]->{id});
32 printf("Land %3d Ship %3d Catapult %3d Ram %3d\n", 78 printf("Land %3d Ship %3d Catapult %3d Ram %3d\n",
33 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y), 79 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y),
34 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 8), 80 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 8),
35 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 6), 81 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 6),
36 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 10), 82 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 10),
48 my ($u) = Ikariam::User->search('name' => $ARGV[0]); 94 my ($u) = Ikariam::User->search('name' => $ARGV[0]);
49 if(defined($u)) { 95 if(defined($u)) {
50 foreach my $i (qw/name score trader_score_secondary army_score_main ally/) { 96 foreach my $i (qw/name score trader_score_secondary army_score_main ally/) {
51 printf("%s: %s\n", $i, $u->get($i)); 97 printf("%s: %s\n", $i, $u->get($i));
52 } 98 }
99
100 travelMatrix($ARGV[0]);
101
53 printf("\nCities\n"); 102 printf("\nCities\n");
54 foreach my $city ($u->cities) { 103 foreach my $city ($u->cities) {
55 my $island = Ikariam::Island->retrieve($city->island); 104 my $island = Ikariam::Island->retrieve($city->island);
56 my $capture = $city->citylevel * ($city->citylevel - 1) * $u->trader_score_secondary / 10000; 105 my $capture = $city->citylevel * ($city->citylevel - 1) * $u->trader_score_secondary / 10000;
57 106