Mercurial > eagle-eye
comparison agent.pl @ 52:d2ac1e198ce4
implement a new agent based on Decision Tree (Decision::ParseTree)
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Mon, 20 Oct 2008 19:07:53 +0800 |
parents | |
children | 2d3c394b7940 |
comparison
equal
deleted
inserted
replaced
51:bc31729f29f4 | 52:d2ac1e198ce4 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 use strict; | |
4 use Ikariam; | |
5 use Data::Dumper; | |
6 use Decision::ParseTree q{ParseTree}; | |
7 use YAML qw/LoadFile Dump/; | |
8 | |
9 package Ikariam::Cities::Rules; | |
10 use strict; | |
11 use Data::Dumper; | |
12 | |
13 sub new { | |
14 my ( $class ) = @_; | |
15 my $self = {}; | |
16 return bless $self, $class; | |
17 } | |
18 | |
19 sub is_Attacked { | |
20 my ($self, $city) = @_; | |
21 return ($city->{force}->{attacks} > 0 ) ? 1 : 0; | |
22 } | |
23 | |
24 sub is_constructing { | |
25 my ($self, $city) = @_; | |
26 return ($city->{construction} > 0 ) ? 1 : 0; | |
27 } | |
28 | |
29 sub is_wall_enough { | |
30 my ($self, $city) = @_; | |
31 # TODO 應該以防禦力計算 | |
32 return ($city->{buildings}->{wall} >= $city->{buildings}->{townHall} ? 1 : 0); | |
33 } | |
34 | |
35 sub is_space_enough { | |
36 my ($self, $city) = @_; | |
37 # TODO 應該以消耗率/時間計算 | |
38 return ($city->{space}->{total} <= ($city->{space}->{occupied}+6) ? 1 : 0) | |
39 } | |
40 | |
41 sub is_corruption { | |
42 my ($self, $city) = @_; | |
43 return 0; | |
44 } | |
45 | |
46 sub is_happiness | |
47 { | |
48 my ($self, $city) = @_; | |
49 # TODO 以 fuzzy 取出合理 happiness 值 | |
50 return ($city->{happiness} >= 2 ? 1 : 0) | |
51 } | |
52 | |
53 sub is_warehouse_enough | |
54 { | |
55 my ($self, $city) = @_; | |
56 # TODO 以速率計算容納率 | |
57 # XXX: not implemented yet. | |
58 return 1; | |
59 } | |
60 | |
61 sub is_risk | |
62 { | |
63 my ($self, $city) = @_; | |
64 # TODO 計算可搶劫比例, 城牆防護, 軍事分數 | |
65 return 0; | |
66 } | |
67 | |
68 sub is_shipyard | |
69 { | |
70 return 1; | |
71 } | |
72 | |
73 sub is_drydock_researched { | |
74 my ($self, $city) = @_; | |
75 return (defined($city->{research}->{4010}) ? 1 : 0); | |
76 } | |
77 | |
78 sub is_winepress_researched { | |
79 my ($self, $city) = @_; | |
80 return (defined($city->{research}->{2040}) ? 1 : 0); | |
81 } | |
82 | |
83 sub rule_engagement | |
84 { | |
85 my ($self, $city) = @_; | |
86 # XXX | |
87 # 計算距離, 可搶劫比例, 是否有聯盟, 軍事分數 (win rate) | |
88 } | |
89 | |
90 sub rule_resource | |
91 { | |
92 # XXX | |
93 } | |
94 1; | |
95 | |
96 | |
97 package main; | |
98 our $i = new Ikariam($::server, $::user, $::pass); | |
99 $i->login; | |
100 my $cities = $i->check; | |
101 | |
102 my $rules = Ikariam::Cities::Rules->new; | |
103 my $tree = LoadFile('building.yaml'); | |
104 # print Dumper($tree); | |
105 # show cities. | |
106 foreach my $cityId (keys(%$cities)) { | |
107 printf("%s http://%s/index.php?view=city&id=%d\n", | |
108 $cities->{$cityId}->{name}, $::server, $cityId); | |
109 printf("construction: %s\n", $cities->{$cityId}->{construction}); | |
110 | |
111 foreach my $thing (qw/resources space force buildings citizens army fleet/) { | |
112 printf("<%s>\n", uc($thing)); | |
113 foreach my $i (keys(%{$cities->{$cityId}->{$thing}})) { | |
114 printf("%s %s, ", $i, $cities->{$cityId}->{$thing}->{$i}); | |
115 } | |
116 print("\n"); | |
117 } | |
118 print("\n"); | |
119 # print(Dumper($cities)); | |
120 | |
121 # make decisions | |
122 | |
123 # for the Decision Tree | |
124 $cities->{$cityId}->{parse_path} = []; | |
125 $cities->{$cityId}->{parse_answer} = undef; | |
126 while (my $action = ParseTree($tree, $rules, $cities->{$cityId})) | |
127 { | |
128 # TODO: remove the last rule, if the result is fallback | |
129 triggerAction($action, $cityId); | |
130 last; | |
131 } | |
132 # Debug | |
133 print(Dumper($cities->{$cityId}->{parse_path})); | |
134 } | |
135 | |
136 $i->logout; | |
137 | |
138 sub triggerAction { | |
139 my ($action, $cityId) = @_; | |
140 | |
141 my ($func, $param) = split(/_/,$action); | |
142 # printf('$i->%s("%s", %s);\n\n', $func, $param, $cityId); | |
143 eval(sprintf('$i->%s("%s", %s);', $func, $param, $cityId)); | |
144 warn $@ if $@; | |
145 } |