changeset 183:2362c8c8909e

implemented military builder.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 21:14:55 +0800
parents d86186ae4329
children 857a67be1609
files Ikariam.pm agent.pl city.yaml military.yaml overall.yaml
diffstat 5 files changed, 315 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Sun Nov 02 18:01:33 2008 +0800
+++ b/Ikariam.pm	Sun Nov 02 21:14:55 2008 +0800
@@ -142,7 +142,7 @@
 
 
     # if debug
-    # LWP::Debug::level('+trace');
+    LWP::Debug::level('+trace');
 
     $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1));
     $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate');
@@ -312,23 +312,18 @@
                 delete($info{'ally'}) 
             } else {
                 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/@href', $i));
-                # http://s2.ikariam.tw/index.php?view=allyPage&allyId=264&oldView=island&id=569
                 if($href =~ /&allyId=(\d+)&/) {
                     $info{'allyId'} = $1;
                 }
             }
 
             # Ally Id
-
             my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//a[@class="messageSend"]/@href', $i));
-            # ?view=sendMessage&type=0&with=20204&destinationCityId=64165&oldView=island
             if ($href =~ /with=(\d+)&destinationCityId=(\d+)/) {
                 $info{'user'} = $1;
                 $info{'cityId'} = $2;
-            # ?view=sendAllyMessage&allyId=1192&oldView=island&id=721
             } else {
-                # 聯盟
-                # this is me.
+                # 聯盟 this is me.
                 my $id = $extractor->find(sprintf('//li[@id="cityLocation%s"]/a/@id', $i));
                 if($id =~ /city_(\d+)/) {
                     $info{'user'} = undef; # FIXME
@@ -372,6 +367,7 @@
     }
 }
 
+
 # for tavern only
 sub set {
     my $self = shift;
@@ -391,13 +387,151 @@
          $self->{mech}->submit_form(
                  form_number => 1,
                  fields      => {
-                 amount => $self->{'cities'}->{$cityId}->{$type}->{maxValue},
-                 }
+                         amount => $self->{'cities'}->{$cityId}->{$type}->{maxValue},
+                     }
                  );
  
     }
 }
 
+sub is_shipyard_upgrading {
+    my $self = shift;
+    my $cityId = shift;
+    my $type = "shipyard";
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (1..2) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@class="isUpgrading"]'))) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    return 0;
+}
+
+sub is_navy_trainning {
+    my $self = shift;
+    my $cityId = shift;
+    my $type = "shipyard";
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (1..2) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@id="unitConstructionList"]//h4'))) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    # FIXME we can not found the shipyard
+    return 0;
+}
+
+sub buildShips {
+    my $self = shift;
+    my $shipType = shift;
+    my $cityId = shift;
+    my $type = 'shipyard';
+
+    warn("buildShips $shipType");
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (1..2) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        $self->{mech}->submit_form(
+                form_number => 1,
+                fields      => {
+                    $shipType => 1,
+                }
+                );
+    }
+}
+
+sub is_army_trainning {
+    my $self = shift;
+    my $cityId = shift;
+    my $type = "barracks";
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (2..$#locations) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@id="unitConstructionList"]//h4'))) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    # FIXME we can not found the shipyard
+    return 0;
+}
+
+sub is_barracks_upgrading {
+    my $self = shift;
+    my $cityId = shift;
+    my $type = 'barracks';
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (2..$#locations) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@class="isUpgrading"]'))) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    return 0;
+}
+
+sub buildUnits {
+    my $self = shift;
+    my $unitType = shift;
+    my $cityId = shift;
+    my $type = 'barracks';
+
+    warn("buildShips $unitType");
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (2..$#locations) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        $self->{mech}->submit_form(
+                form_number => 1,
+                fields      => {
+                    $unitType => 1,
+                }
+                );
+    }
+}
+
 sub build {
     my $self = shift;
     my $type = shift;
@@ -433,7 +567,7 @@
     my $self = shift;
     my $param = shift;
     # defense.
-    die("run $param not implemented yet.");
+    warn ("run $param not implemented yet.");
 }
 
 sub research
@@ -474,8 +608,8 @@
         $self->{mech}->submit_form(
                 form_number => 1,
                 fields      => {
-                cargo_army_302 => '2',
-                }
+                      cargo_army_302 => '2',
+                   }
                 );
     } else {
         warn ($treaty);
@@ -607,7 +741,6 @@
             }
         }
     }
-    # print(Dumper($self->{'military'}));
 }
 
 sub checkMilitaryAdvisorReportView {
@@ -1002,6 +1135,7 @@
     }
     foreach(@cities) {
         $self->{'cities'}->{$_} = {};
+        $self->{'cities'}->{$_}->{id} = $_;
     }
 }
 
--- a/agent.pl	Sun Nov 02 18:01:33 2008 +0800
+++ b/agent.pl	Sun Nov 02 21:14:55 2008 +0800
@@ -77,18 +77,6 @@
     return 1;
 }
 
-sub is_risk {
-    my ($self, $city) = @_;
-    # TODO 計算可搶劫比例, 城牆防護, 軍事分數
-    # 
-    # my $capture = $city->{buildings}->{townHall} * ($city->{buildings}->{townHall} - 1) * $city->{resource}->gold / 10000;
-    # 計算軍人消耗
-    # 方陣兵, 1.58 軍分, 24 維護費
-    # 劍士, 4.64 軍分, 16 維護費
-    # army_score_main > (occupied * 0.3)
-    return 0;
-}
-
 sub is_gold_enoughforcargo {
     my ($self, $city) = @_;
     my @cargoCost = qw/160 244 396 812 1240 1272 1832 1888 3848 3972 5204 5384 6868 7120 8864 9200 11268 11712 14108 14680 23320 24288 28664 29880 34956 36468 42348 44212 51024 53308 61236 64024 73096 76468 87020 91088 103224 116524 122072 137432 180060 202132 211964 237444 249108 278276 292076 306623 321963 338138 355198 373191 392171 412195 433320 455612 479135 503962 530166 557828 587031 617863 650420 684802 721113 759466 799981 842783 888005 935790 986286 1039654 1096062 1155689 1218724 1285369 1355837 1430353 1509159 1592508 1680670 1773932 1872597 1976989 2087448 2204338 2328045 2458976 2597567 2744276 2899594 3064040 3238163 3422550 3617820 3824635 4043693 4275738 4521561 4782000 5057946 5350345 5660202 5988585 6336630 6705540 7096598 7511164 7950683 8416694 8910828 9434823 9990523 10579889 11205006 11868090 12571498 13317734 14109462 14949514/;
@@ -172,17 +160,23 @@
 sub is_barracks_level_enough {
     my ($self, $city) = @_;
     return 0 if(!defined($city->{buildings}->{barracks}));
-    # 方陣兵需要 level 4
-    # optimum is 5
-    return ($city->{buildings}->{barracks} >= 4 ? 1 : 0);
+    if($city->{buildings}->{townHall} >= 10) {
+        # optimum is 5
+        return ($city->{buildings}->{barracks} >= 5 ? 1 : 0);
+    } else {
+        # 方陣兵需要 level 4
+        return ($city->{buildings}->{barracks} >= 4 ? 1 : 0);
+    }
 }
 
 sub is_shipyard_level_enough {
     my ($self, $city) = @_;
     return 0 if(!defined($city->{buildings}->{shipyard}));
-    # optimum is 5
-    return ($city->{buildings}->{shipyard} >= 3 ? 1 : 0);
-    return 0;
+    if ($self->is_greekfire_researched() eq 1) {
+        return ($city->{buildings}->{shipyard} >= 5 ? 1 : 0);
+    } else {
+        return ($city->{buildings}->{shipyard} >= 3 ? 1 : 0);
+    }
 }
 
 sub rule_engagement
@@ -197,10 +191,63 @@
     # TODO 
 }
 
+sub is_shipyard_upgrading {
+    my ($self, $city) = @_;
+    return $::i->is_shipyard_upgrading($city->{id});
+}
+
+sub is_navy_trainning {
+    my ($self, $city) = @_;
+    return $::i->is_navy_trainning($city->{id});
+}
+
+sub train_navy {
+    my ($self, $city) = @_;
+    my $cityId = $city->{id};
+    # TODO, 依照升級比例算 CP 值最高
+    if($self->is_greekfire_researched() eq 1 && $city->{buildings}->{shipyard} >= 5) {
+        # ok, we can build Flamethrower.
+        if(($city->{'fleet'}->{BallistaShip} / $city->{'fleet'}->{Flamethrower}) <= (1.5/1)) {
+            return 213; # 強弩船
+        } else {
+            return 211; # 噴火船
+        }
+    } else {
+        return 213; # 強弩船
+    }
+}
+
+sub is_barracks_upgrading {
+    my ($self, $city) = @_;
+    return $::i->is_barracks_upgrading($city->{id});
+}
+
+sub is_army_trainning {
+    my ($self, $city) = @_;
+    return $::i->is_army_trainning($city->{id});
+}
+
+sub train_army {
+    my ($self, $city) = @_;
+    my $cityId = $city->{id};
+
+    # TODO, 依照升級比例算 CP 值最高
+    my $assault = 'Swordsman';
+    my $resistance = 'Phalanx';
+
+    # ok, we can build Flamethrower.
+    if(($city->{'army'}->{$assault} / $city->{'army'}->{$resistance}) <= (2/1)) {
+        return 302; # Swordsman
+    } else {
+        return 303; # Phalanx
+    }
+}
+
 # navy
-sub rule_navyExpenditure
+sub is_navyExpenditure_available
 {
     my ($self, $cityId) = @_;
+
     # move this to somewhere else.
     my $workersRatio = {
         'citizens' => 0.4,
@@ -212,17 +259,19 @@
     my $netincome = $self->{'ikariam'}->getNetIncome($cityId);
     
     # 軍費為 兩成 淨收入
-    # 陸軍佔用 0.8
-    # 海軍佔用 0.2
-    my $militaryExpenditure = int($netincome * 0.2 * 0.2);
+    # 陸軍佔用 0.3
+    # 海軍佔用 0.7
+    my $militaryExpenditure = int($netincome * 0.2 * 0.7);
 
     if($currentCost < $militaryExpenditure) {
         printf("Current navy expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure);
+        return 1;
     }
+    return 0;
 }
 
 # army
-sub rule_milityExpenditure
+sub is_milityExpenditure_available
 {
     my ($self, $cityId) = @_;
     # move this to somewhere else.
@@ -236,13 +285,16 @@
     my $netincome = $self->{'ikariam'}->getNetIncome($cityId);
     
     # 軍費為 兩成 淨收入
-    # 陸軍佔用 0.8
-    # 海軍佔用 0.2
-    my $militaryExpenditure = int($netincome * 0.2 * 0.8);
+    # 陸軍佔用 0.3
+    # 海軍佔用 0.7
+
+    my $militaryExpenditure = int($netincome * 0.2 * 0.3);
 
     if($currentCost < $militaryExpenditure) {
-        printf("Current army expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure);
+        Carp::carp("Current army expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure);
+        return 1;
     }
+    return 0;
 }
 1; 
 
@@ -258,9 +310,7 @@
 my $rules = Ikariam::Cities::Rules->new($i);
 # blanace resources, arrange defance 
 my $tree  = LoadFile('overall.yaml');
-my $decision;
-$decision->{parse_path} = [];
-$decision->{parse_answer} = undef;
+my $decision; $decision->{parse_path} = []; $decision->{parse_answer} = undef;
 my $action = ParseTree($tree, $rules, $decision);
 triggerAction($action, (keys(%$cities))[0]) if(defined($action));
 # print STDERR Dump($decision) if(defined($verbose));
@@ -270,8 +320,7 @@
     printf("%s http://%s/index.php?view=city&id=%d\n", 
         $cities->{$cityId}->{name}, $::server, $cityId);
     printf("construction: %s\n", $cities->{$cityId}->{construction});
-    
-    # foreach my $thing (qw/resources space wars buildings citizens army fleet/) {
+
     foreach my $thing (qw/resources space buildings citizens army fleet/) {
         printf("<%s>\n", uc($thing));
         foreach my $i (keys(%{$cities->{$cityId}->{$thing}})) {
@@ -283,23 +332,25 @@
 
     # maybe this should be moved to Rules.
     $i->blanceHurmanResource($cityId);
-    # milityExpenditure
-    $rules->rule_milityExpenditure($cityId);
-    $rules->rule_navyExpenditure($cityId);
+    
+    $tree  = LoadFile('military.yaml');
+    $cities->{$cityId}->{parse_path} = [];
+    $cities->{$cityId}->{parse_answer} = undef;
+    if (my $action = ParseTree($tree, $rules, $cities->{$cityId})) {
+        triggerAction($action, $cityId);
+    }
+    DumpFile("army-dump.yaml", $cities);
+
 
     # build and upgrade for cities
     $tree  = LoadFile('city.yaml');
     $cities->{$cityId}->{parse_path} = [];
     $cities->{$cityId}->{parse_answer} = undef;
-    while (my $action = ParseTree($tree, $rules, $cities->{$cityId})) {
+    if (my $action = ParseTree($tree, $rules, $cities->{$cityId})) {
         triggerAction($action, $cityId);
-        last;
     }
-    # Debug
-    # print(Dump($cities->{$cityId}->{parse_path})) if(defined($verbose));
+    DumpFile("cities-dump.yaml", $cities);
 }
-# print(Dump($cities)) if(defined($verbose));
-DumpFile("cities-dump.yaml", $cities);
 DumpFile("research-dump.yaml", $i->{'research'});
 DumpFile("military-dump.yaml", $i->{'military'});
 
--- a/city.yaml	Sun Nov 02 18:01:33 2008 +0800
+++ b/city.yaml	Sun Nov 02 21:14:55 2008 +0800
@@ -5,7 +5,7 @@
 - is_attacked:
     # we are in Peace :D
     0:
-      # adjust human resources
+      # TODO adjust human resources
       - is_constructing:
          0:
             # XXX: 這樣會一口氣買同等城市數量的船耶
@@ -32,36 +32,40 @@
                               - is_bacchanal:
                                  0: set_tavern
                                  1: build_tavern
-            # TODO
-            # http://ikariam.wikia.com/wiki/List_of_buildings
-            # http://ikariam.wikia.com/wiki/Technology_Tree
-            # is_conservation_researched
-            # -build_warehouse
-            # build_academy
-            # build_palace (Expansion, Capital only)
-            # build_embassy (副城, 不建)
-            - is_risk:
-               1: 
+            - is_navyExpenditure_available:
+               1:
                   - is_professionalarmy_researched:
                      1:
                         - is_shipyard_level_enough:
                            0: build_shipyard
+                           1: 
+                              - train_navy:
+            - is_milityExpenditure_available:
+               1:
+                  - is_professionalarmy_researched:
+                     1:
                         - is_barracks_level_enough:
                            0: build_barracks
-                  - is_shipyard_level_enough:
-                     0: 
-                        - is_drydock_researched: 
-                           # requireed more army ?
-                           1: build_shipyard
-                  #    - build one boat
-# 應該把 Barracks 建到最佳等級
-            # 要蓋到何種程度?
+                           1:
+                              - train_army:
+            # TODO
+            # build_safehouse
+            # is_espionage_researched
+            #    1: 
+            #       - build_hideout
+
+            # 水晶島應該建設 
             # is_invention_researched
             #    1: 
             #       - build_workshop 
-            # is_espionage_researched
-            #    1: 
-            #       - build_hideout
+            # build_academy
+
+            # build_palace (Expansion, Capital only)
+
+            # 大使館
+            # build_embassy 
+
+            # 要蓋到何種程度?
             # is_culturalexchange_resaerched
             #    1: 
             #       - build_museum
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/military.yaml	Sun Nov 02 21:14:55 2008 +0800
@@ -0,0 +1,50 @@
+---
+- is_attacked:
+   0:
+      - is_navyExpenditure_available:
+         1:
+            - is_professionalarmy_researched:
+               1:
+                  - is_shipyard_level_enough:
+                     0: 
+                        - is_constructing:
+                           0: build_shipyard
+                     1:
+                        - is_shipyard_upgrading:
+                           0:
+                              - is_navy_trainning:
+                                 0:
+                                    - train_navy:
+                                       211: buildShips_211
+                                       212: buildShips_212
+                                       213: buildShips_213
+                                       214: buildShips_214
+                                       215: buildShips_215
+                                       216: buildShips_216
+      - is_milityExpenditure_available:
+         1:
+            - is_professionalarmy_researched:
+               1:
+                  - is_barracks_level_enough:
+                     0:
+                        - is_constructing:
+                           0: build_barracks
+                     1:
+                        - is_barracks_upgrading:
+                           0:
+                              - is_army_trainning:
+                                 0:
+                                    - train_army:
+                                       301: buildUnits_301
+                                       302: buildUnits_302
+                                       303: buildUnits_303
+                                       304: buildUnits_304
+                                       305: buildUnits_305
+                                       306: buildUnits_306
+                                       307: buildUnits_307
+                                       308: buildUnits_308
+                                       309: buildUnits_309
+                                       310: buildUnits_310
+                                       311: buildUnits_311
+                                       312: buildUnits_312
+                                       313: buildUnits_313
--- a/overall.yaml	Sun Nov 02 18:01:33 2008 +0800
+++ b/overall.yaml	Sun Nov 02 21:14:55 2008 +0800
@@ -30,4 +30,3 @@
         0: research_seafaring
         
    1: run_defence
-   # we are being attacked!!!!!!!!!!!!!