view agent.pl @ 62:a4c364888197

add ikriam game binding lib python version
author hychen@mluna
date Wed, 22 Oct 2008 04:04:32 +0800
parents b40f87f16263
children 6eccb3a95df5
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/;

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

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

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

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

sub is_wall_enough {
    my ($self, $city) = @_;
    # TODO 應該以防禦力計算
    return ($city->{buildings}->{wall} >= $city->{buildings}->{townHall} ?  1 : 0);
}

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

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 以速率計算容納率
    # XXX: not implemented yet.
    return 1;
}

sub is_risk
{
    my ($self, $city) = @_;
    # TODO 計算可搶劫比例, 城牆防護, 軍事分數
    return 0;
}

sub is_shipyard
{
    return 1;
}

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) = @_;
    return (defined($city->{research}->{1030}) ?  1 : 0);
}

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

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

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

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

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

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

sub rule_resource
{
    # XXX
}
1; 


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

my $rules = Ikariam::Cities::Rules->new;
my $tree  = LoadFile('building.yaml');
# print Dumper($tree);
# 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 force 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");
    # print(Dumper($cities));

    # make decisions

    # for the Decision Tree
    $cities->{$cityId}->{parse_path} = [];
    $cities->{$cityId}->{parse_answer} = undef;
    while (my $action = ParseTree($tree, $rules, $cities->{$cityId}))
    {
        # TODO: remove the last rule, if the result is fallback
        triggerAction($action, $cityId);
        if($cities->{$cityId}->{parse_answer} ne "fallback") {
            last ;
        } else {

        }
    }
    # Debug
    print(Dumper($cities->{$cityId}->{parse_path}));
}

$i->logout;


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

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