view agent.pl @ 119:755e5f5434e4

merged for fixed inactive-map.pl and hacks for highscore search.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Thu, 30 Oct 2008 15:08:56 +0800
parents 73f0a1cafc23
children 362184763c04
line wrap: on
line source

#!/usr/bin/perl

use strict;
use Ikariam;
use Data::Dumper;
use Decision::ParseTree q{ParseTree};
use YAML qw/LoadFile Dump DumpFile/;

package Ikariam::Cities::Rules;
use strict;
use Data::Dumper;

sub new {
    my ( $class, $i ) = @_;
    my $self = {
        ikariam => $i,
    };
    return bless $self, $class;
}

sub is_attacked {
    my ($self, $city) = @_;
    return ($self->{'ikariam'}->{'military'}->{attacks} > 0 ) ? 1 : 0;
}

sub is_constructing {
    my ($self, $city) = @_;
    return ($city->{construction} > 0 ) ? 1 : 0;
}

sub is_wall_enough {
    my ($self, $city) = @_;
    # http://ikariam.wikia.com/wiki/Wall_Defense
    # Basic rule - the wall level must equale or large then townHall level.
    return ($city->{buildings}->{wall} >= $city->{buildings}->{townHall} ?  1 : 0);
}

sub is_space_enough {
    my ($self, $city) = @_;
    # TODO 應該以消耗率/時間計算
    # $self->{ikariam}->{growth}
    return 1 if($city->{buildings}->{townHall} >= 20);
    return ($city->{space}->{total} <= ($city->{space}->{occupied}+6) ?  0 : 1)
}

sub is_corruption {
    my ($self, $city) = @_;
    return 0;
}

sub is_happiness {
    my ($self, $city) = @_;
    # TODO 以 fuzzy 取出合理 happiness 值
    return ($city->{happiness} >= 2 ?  1 : 0)
}

sub is_warehouse_enough {
    my ($self, $city) = @_;
    # TODO 以速率計算容納率
    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/;

    if($city->{resources}->{gold} >= ($cargoCost[$city->{transporters}->{sum}]*5)) {
        return 1;
    } else {
        return 0;
    }
}

sub is_resource_enoghforHall {
    my ($self, $city) = @_;
    my @wood = qw/udnef 0 70 98 65 129 236 402 594 849 1176 1586 2101 3208 4937 7171 10139 14537 18420 22896 28047 33934 40623 48017 56511 226044 452088 904176 1808352 3616704 7233408 14466816 28933632/;
    my @marble = qw/undef 0 0 17 28 66 95 156 243 406 579 799 1348 2124 2951 4409 6461 8187 10176 12466 15082 18055 21381 25116 100464 200928 401856 803712 1607424 3214848 6429696 12859392/;

    my $level = $city->{buildings}->{townHall};
    if($city->{resources}->{wood} >= $wood[$level] && $city->{resources}->{marble} >= $marble[$level] ) {
        return 1;
    }
    warn(sprintf("Resource is short for build city hall. wood [%d] marble [%d] ", $wood[$level], $marble[$level]));
    return 0;
}

sub is_expansion_researched {
    my ($self, $city) = @_;
    # $self->{'ikariam'}->
    return (defined($self->{'ikariam'}->{research}->{1030}) ?  1 : 0);
}

sub is_culturalexchange_researched {
    my ($self) = @_;
    return (defined($self->{'ikariam'}->{research}->{1040}) ?  1 : 0);
}

sub is_wealth_researched {
    my ($self) = @_;
    return (defined($self->{'ikariam'}->{research}->{2030}) ?  1 : 0);
}

sub is_winepress_researched {
    my ($self) = @_;
    return (defined($self->{'ikariam'}->{research}->{2040}) ?  1 : 0);
}

sub is_paper_researched {
    my ($self) = @_;
    return (defined($self->{'ikariam'}->{research}->{3020}) ?  1 : 0);
}

sub is_espionage_researched {
    my ($self) = @_;
    return (defined($self->{'ikariam'}->{research}->{3030}) ?  1 : 0);
}

sub is_invention_researched {
    my ($self) = @_;
    return (defined($self->{'ikariam'}->{research}->{3040}) ?  1 : 0);
}

sub is_professionalarmy_researched {
    my ($self) = @_;
    return (defined($self->{'ikariam'}->{research}->{4030}) ?  1 : 0);
}

sub is_drydock_researched {
    my ($self) = @_;
    return (defined($self->{'ikariam'}->{research}->{4010}) ?  1 : 0);
}

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);
}

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;
}

sub rule_engagement
{
    my ($self, $city) = @_;
    # TODO
    # 計算距離, 可搶劫比例, 是否有聯盟, 軍事分數 (win rate)
}

sub rule_resource
{
    # TODO 
}

# navy
sub rule_navyExpenditure
{
    my ($self, $cityId) = @_;
    # move this to somewhere else.
    my $workersRatio = {
        'citizens' => 0.4,
        'specialworkers' => 0.3,
        'woodworkers' => 0.7,
    };

    my $currentCost = $self->{'ikariam'}->getNavyExpenditure($cityId);
    my $netincome = $self->{'ikariam'}->getNetIncome($cityId);
    
    # 軍費為 兩成 淨收入
    # 陸軍佔用 0.8
    # 海軍佔用 0.2
    my $militaryExpenditure = int($netincome * 0.2 * 0.2);

    if($currentCost < $militaryExpenditure) {
        printf("Current navy expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure);
    }
}

# army
sub rule_milityExpenditure
{
    my ($self, $cityId) = @_;
    # move this to somewhere else.
    my $workersRatio = {
        'citizens' => 0.4,
        'specialworkers' => 0.3,
        'woodworkers' => 0.7,
    };

    my $currentCost = $self->{'ikariam'}->getMilityExpenditure($cityId);
    my $netincome = $self->{'ikariam'}->getNetIncome($cityId);
    
    # 軍費為 兩成 淨收入
    # 陸軍佔用 0.8
    # 海軍佔用 0.2
    my $militaryExpenditure = int($netincome * 0.2 * 0.8);

    if($currentCost < $militaryExpenditure) {
        printf("Current army expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure);
    }
}
1; 


package main;
my $verbose = 1;

our $i = new Ikariam($::server, $::user, $::pass);
$i->login;
my $cities = $i->check;

# Genereic rules for both overall and city level arranagement.
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 $action = ParseTree($tree, $rules, $decision);
triggerAction($action, (keys(%$cities))[0]) if(defined($action));
print STDERR Dump($decision) if(defined($verbose));

# show cities.
foreach my $cityId (keys(%$cities)) {
    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}})) {
            printf("%s %s, ", $i, $cities->{$cityId}->{$thing}->{$i});
        }
        print("\n");
    }
    print("\n");

    # maybe this should be moved to Rules.
    $i->blanceHurmanResource($cityId);
    # milityExpenditure
    $rules->rule_milityExpenditure($cityId);
    $rules->rule_navyExpenditure($cityId);

    # 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})) {
        triggerAction($action, $cityId);
        last;
    }
    # Debug
    # print(Dump($cities->{$cityId}->{parse_path})) if(defined($verbose));
}
# print(Dump($cities)) if(defined($verbose));
DumpFile("dump.yaml", $cities);

$i->logout;

sub triggerAction {
    my ($action, $cityId) = @_;

    my ($func, $param) = split(/_/,$action);
    printf('$i->%s("%s", %s);'. "\n", $func, $param, $cityId);
    eval(sprintf('$i->%s("%s", %s);', $func, $param, $cityId));
    warn $@ if $@;
}