annotate planner.pl @ 271:0ed1b3e72e65

new campaign planner
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 01 Dec 2008 01:02:36 +0800
parents
children 8355ec8514f9
rev   line source
271
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
1 #!/usr/bin/perl
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
2 use Ikariam;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
3 use Data::Dumper;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
4 use Decision::ParseTree q{ParseTree};
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
5 use YAML qw/LoadFile Dump DumpFile/;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
6
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
7 Ikariam::Cities->has_a(island => 'Ikariam::Island');
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
8
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
9 my $army = {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
10 "Slinger" => 301,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
11 "Swordsman" => 302,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
12 "Phalanx" => 303,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
13 "Ram" => 307,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
14 "Archer" => 313,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
15 "Catapult" => 306,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
16 "Gunsman" => 304,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
17 "Mortar" => 305,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
18 "SteamGiant" => 308,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
19 "Gyrocopter" => 312,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
20 "Bombardier" => 309,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
21 "Doctor" => 311,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
22 "Cook" => 310,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
23 };
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
24
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
25 my $fleet = {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
26 "CargoShip" => 201,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
27 "Ram-Ship" => 210,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
28 "BallistaShip" => 213,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
29 "Flamethrower" => 211,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
30 "CatapultShip" => 214,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
31 "MortarShip" => 215,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
32 "PaddleWheel Ram" => 216,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
33 "DivingBoat" => 212,
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
34 };
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
35
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
36 # return minutes.
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
37 sub travelTime {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
38 my ($x1, $y1, $x2, $y2, $speed) = @_;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
39 $speed = 20 unless(defined($speed));
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
40 return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))+1) * (400/$speed));
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
41 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
42
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
43 package main;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
44 if($#ARGV < 2) {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
45 die("Usage: %s targetCity navyCity armyCity wingman1.. wingman2..\n");
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
46 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
47 my $targetCityId = shift(@ARGV);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
48 my $navyCity = shift(@ARGV);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
49 my $armyCity = shift(@ARGV);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
50 my @wingman = @ARGV;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
51
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
52 my $targetCity = Ikariam::Cities->retrieve($targetCityId);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
53
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
54
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
55 # NAVY
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
56 my $city = LoadFile(sprintf("city-%s-dump.yaml", $navyCity));
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
57 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 8) + 45;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
58 $city->{type} = "navy";
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
59 push (@cities, $city);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
60
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
61 # ARMY
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
62 my $city = LoadFile(sprintf("city-%s-dump.yaml", $armyCity));
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
63 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 20) + 5;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
64 $city->{type} = "army";
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
65 push (@cities, $city);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
66
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
67 # wingman, army
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
68 foreach my $cityId (@wingman) {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
69 my $city = LoadFile(sprintf("city-%s-dump.yaml", $cityId));
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
70 $city->{distance} = travelTime($targetCity->island->x, $targetCity->island->y, $city->{island}->{x}, $city->{island}->{y}, 20);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
71 push (@cities, $city);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
72 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
73
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
74 @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
75
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
76
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
77 # print the code.
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
78 print <<EOF;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
79 #!/usr/bin/perl
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
80 use strict;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
81 use Ikariam;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
82 use Data::Dumper;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
83 package main;
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
84 my \$ikariam = new Ikariam(\$::server, \$::user, \$::pass);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
85 EOF
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
86
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
87 printf ("# %s %s %s %s\n", $targetCity->cityname, $targetCity->owner, $targetCity->island->x, $targetCity->island->y);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
88
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
89 for(my $i = ($#cities + 1) ; $i-- ; $i < 0 ) {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
90
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
91 if($i ne $#cities){
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
92 printf("sleep(%d*60);\n",
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
93 ($cities[$i+1]->{distance} - $cities[$i]->{distance}));
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
94 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
95 printf("\n\n\n# %s %s %dm\n", $cities[$i]->{name}, $cities[$i]->{type}, $cities[$i]->{distance});
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
96
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
97 printf('$ikariam->login;' . "\n");
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
98 if($cities[$i]->{type} eq "navy") {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
99 printf('$ikariam->changeCity(%d);' . "\n", $cities[$i]->{id});
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
100 printf('$ikariam->blockadeCity(%d, {' . "\n", $targetCity->id);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
101 printf('blockadeDuration => 8*60*60,' . "\n");
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
102 while (my ($k, $v) = each(%{$cities[$i]->{fleet}})) {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
103 if($v > 0) {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
104 printf("cargo_fleet_%s => %s, # %s \n", $fleet->{$k}, $v, $k);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
105 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
106 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
107 printf('});' . "\n", $cities[$i]->{id});
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
108 } else {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
109 printf('$ikariam->changeCity(%d);' . "\n", $cities[$i]->{id});
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
110 printf('$ikariam->plunderCity(%d, {' . "\n", $targetCity->id);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
111 while (my ($k, $v) = each(%{$cities[$i]->{army}})) {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
112 if($v > 0) {
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
113 printf("cargo_army_%s => %s, # %s \n", $army->{$k}, $v, $k);
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
114 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
115 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
116 printf('});' . "\n", $cities[$i]->{id});
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
117 }
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
118 printf('$ikariam->logout;' . "\n");
0ed1b3e72e65 new campaign planner
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
119 }