comparison planner.pl @ 334:65f45552060e

fixed travel time
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Thu, 12 Feb 2009 02:48:45 +0800
parents 0db3e2bcbd0f
children 85d60a0569bf
comparison
equal deleted inserted replaced
333:2ca2c4c49901 334:65f45552060e
35 }; 35 };
36 36
37 # return minutes. 37 # return minutes.
38 sub travelTime { 38 sub travelTime {
39 my ($x1, $y1, $x2, $y2, $speed) = @_; 39 my ($x1, $y1, $x2, $y2, $speed) = @_;
40 $speed = 20 unless(defined($speed)); 40 $speed = 60 unless(defined($speed));
41 return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))+1) * (400/$speed)); 41 if($x1 == $x2 && $y1 == $y2) {
42 return 600/$speed;
43 } else {
44 return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))) * (1200/$speed));
45 }
42 } 46 }
43 47
48 #
44 package main; 49 package main;
45 getopts('w'); 50 getopts('w');
46 if($#ARGV < 2) { 51 if($#ARGV < 2) {
47 die("Usage: %s targetCity navyCity armyCity wingman1.. wingman2..\n"); 52 die("Usage: %s targetCity navyCity armyCity wingman1.. wingman2..\n");
48 } 53 }
60 $city->{type} = "navy"; 65 $city->{type} = "navy";
61 push (@cities, $city); 66 push (@cities, $city);
62 67
63 # ARMY 68 # ARMY
64 my $city = LoadFile(sprintf("city-%s-dump.yaml", $armyCity)); 69 my $city = LoadFile(sprintf("city-%s-dump.yaml", $armyCity));
65 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 50) + 5; 70 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60) + 5;
66 $city->{type} = "army"; 71 $city->{type} = "army";
67 push (@cities, $city); 72 push (@cities, $city);
68 73
69 # wingman, army 74 # wingman, army
70 foreach my $i (0..$#wingman) { 75 foreach my $i (0..$#wingman) {
71 my $cityId = $wingman[$i]; 76 my $cityId = $wingman[$i];
72 my $city = LoadFile(sprintf("city-%s-dump.yaml", $cityId)); 77 my $city = LoadFile(sprintf("city-%s-dump.yaml", $cityId));
73 $city->{type} = "wingman" . ($i+1); 78 $city->{type} = "wingman" . ($i+1);
74 if(defined($opt_w)) { 79 if(defined($opt_w)) {
75 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 50) - ($i * 20 + 20); 80 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60) - ($i * 20 + 20);
76 } else { 81 } else {
77 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 50); 82 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 60);
78 } 83 }
79 push (@cities, $city); 84 push (@cities, $city);
80 } 85 }
81 86
82 @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities); 87 @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities);