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