comparison agent.pl @ 183:2362c8c8909e

implemented military builder.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 21:14:55 +0800
parents e8a244ce5a1d
children d6dc50b64f06
comparison
equal deleted inserted replaced
182:d86186ae4329 183:2362c8c8909e
75 my ($self, $city) = @_; 75 my ($self, $city) = @_;
76 # TODO 以速率計算容納率 76 # TODO 以速率計算容納率
77 return 1; 77 return 1;
78 } 78 }
79 79
80 sub is_risk {
81 my ($self, $city) = @_;
82 # TODO 計算可搶劫比例, 城牆防護, 軍事分數
83 #
84 # my $capture = $city->{buildings}->{townHall} * ($city->{buildings}->{townHall} - 1) * $city->{resource}->gold / 10000;
85 # 計算軍人消耗
86 # 方陣兵, 1.58 軍分, 24 維護費
87 # 劍士, 4.64 軍分, 16 維護費
88 # army_score_main > (occupied * 0.3)
89 return 0;
90 }
91
92 sub is_gold_enoughforcargo { 80 sub is_gold_enoughforcargo {
93 my ($self, $city) = @_; 81 my ($self, $city) = @_;
94 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/; 82 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/;
95 83
96 if($city->{resources}->{gold} >= ($cargoCost[$city->{transporters}->{sum}]*5)) { 84 if($city->{resources}->{gold} >= ($cargoCost[$city->{transporters}->{sum}]*5)) {
170 } 158 }
171 159
172 sub is_barracks_level_enough { 160 sub is_barracks_level_enough {
173 my ($self, $city) = @_; 161 my ($self, $city) = @_;
174 return 0 if(!defined($city->{buildings}->{barracks})); 162 return 0 if(!defined($city->{buildings}->{barracks}));
175 # 方陣兵需要 level 4 163 if($city->{buildings}->{townHall} >= 10) {
176 # optimum is 5 164 # optimum is 5
177 return ($city->{buildings}->{barracks} >= 4 ? 1 : 0); 165 return ($city->{buildings}->{barracks} >= 5 ? 1 : 0);
166 } else {
167 # 方陣兵需要 level 4
168 return ($city->{buildings}->{barracks} >= 4 ? 1 : 0);
169 }
178 } 170 }
179 171
180 sub is_shipyard_level_enough { 172 sub is_shipyard_level_enough {
181 my ($self, $city) = @_; 173 my ($self, $city) = @_;
182 return 0 if(!defined($city->{buildings}->{shipyard})); 174 return 0 if(!defined($city->{buildings}->{shipyard}));
183 # optimum is 5 175 if ($self->is_greekfire_researched() eq 1) {
184 return ($city->{buildings}->{shipyard} >= 3 ? 1 : 0); 176 return ($city->{buildings}->{shipyard} >= 5 ? 1 : 0);
185 return 0; 177 } else {
178 return ($city->{buildings}->{shipyard} >= 3 ? 1 : 0);
179 }
186 } 180 }
187 181
188 sub rule_engagement 182 sub rule_engagement
189 { 183 {
190 my ($self, $city) = @_; 184 my ($self, $city) = @_;
195 sub rule_resource 189 sub rule_resource
196 { 190 {
197 # TODO 191 # TODO
198 } 192 }
199 193
194 sub is_shipyard_upgrading {
195 my ($self, $city) = @_;
196 return $::i->is_shipyard_upgrading($city->{id});
197 }
198
199 sub is_navy_trainning {
200 my ($self, $city) = @_;
201 return $::i->is_navy_trainning($city->{id});
202 }
203
204 sub train_navy {
205 my ($self, $city) = @_;
206 my $cityId = $city->{id};
207 # TODO, 依照升級比例算 CP 值最高
208 if($self->is_greekfire_researched() eq 1 && $city->{buildings}->{shipyard} >= 5) {
209 # ok, we can build Flamethrower.
210 if(($city->{'fleet'}->{BallistaShip} / $city->{'fleet'}->{Flamethrower}) <= (1.5/1)) {
211 return 213; # 強弩船
212 } else {
213 return 211; # 噴火船
214 }
215 } else {
216 return 213; # 強弩船
217 }
218 }
219
220 sub is_barracks_upgrading {
221 my ($self, $city) = @_;
222 return $::i->is_barracks_upgrading($city->{id});
223 }
224
225 sub is_army_trainning {
226 my ($self, $city) = @_;
227 return $::i->is_army_trainning($city->{id});
228 }
229
230 sub train_army {
231 my ($self, $city) = @_;
232 my $cityId = $city->{id};
233
234 # TODO, 依照升級比例算 CP 值最高
235 my $assault = 'Swordsman';
236 my $resistance = 'Phalanx';
237
238 # ok, we can build Flamethrower.
239 if(($city->{'army'}->{$assault} / $city->{'army'}->{$resistance}) <= (2/1)) {
240 return 302; # Swordsman
241 } else {
242 return 303; # Phalanx
243 }
244 }
245
200 # navy 246 # navy
201 sub rule_navyExpenditure 247 sub is_navyExpenditure_available
248 {
249 my ($self, $cityId) = @_;
250
251 # move this to somewhere else.
252 my $workersRatio = {
253 'citizens' => 0.4,
254 'specialworkers' => 0.3,
255 'woodworkers' => 0.7,
256 };
257
258 my $currentCost = $self->{'ikariam'}->getNavyExpenditure($cityId);
259 my $netincome = $self->{'ikariam'}->getNetIncome($cityId);
260
261 # 軍費為 兩成 淨收入
262 # 陸軍佔用 0.3
263 # 海軍佔用 0.7
264 my $militaryExpenditure = int($netincome * 0.2 * 0.7);
265
266 if($currentCost < $militaryExpenditure) {
267 printf("Current navy expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure);
268 return 1;
269 }
270 return 0;
271 }
272
273 # army
274 sub is_milityExpenditure_available
202 { 275 {
203 my ($self, $cityId) = @_; 276 my ($self, $cityId) = @_;
204 # move this to somewhere else. 277 # move this to somewhere else.
205 my $workersRatio = { 278 my $workersRatio = {
206 'citizens' => 0.4, 279 'citizens' => 0.4,
207 'specialworkers' => 0.3, 280 'specialworkers' => 0.3,
208 'woodworkers' => 0.7, 281 'woodworkers' => 0.7,
209 }; 282 };
210 283
211 my $currentCost = $self->{'ikariam'}->getNavyExpenditure($cityId);
212 my $netincome = $self->{'ikariam'}->getNetIncome($cityId);
213
214 # 軍費為 兩成 淨收入
215 # 陸軍佔用 0.8
216 # 海軍佔用 0.2
217 my $militaryExpenditure = int($netincome * 0.2 * 0.2);
218
219 if($currentCost < $militaryExpenditure) {
220 printf("Current navy expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure);
221 }
222 }
223
224 # army
225 sub rule_milityExpenditure
226 {
227 my ($self, $cityId) = @_;
228 # move this to somewhere else.
229 my $workersRatio = {
230 'citizens' => 0.4,
231 'specialworkers' => 0.3,
232 'woodworkers' => 0.7,
233 };
234
235 my $currentCost = $self->{'ikariam'}->getMilityExpenditure($cityId); 284 my $currentCost = $self->{'ikariam'}->getMilityExpenditure($cityId);
236 my $netincome = $self->{'ikariam'}->getNetIncome($cityId); 285 my $netincome = $self->{'ikariam'}->getNetIncome($cityId);
237 286
238 # 軍費為 兩成 淨收入 287 # 軍費為 兩成 淨收入
239 # 陸軍佔用 0.8 288 # 陸軍佔用 0.3
240 # 海軍佔用 0.2 289 # 海軍佔用 0.7
241 my $militaryExpenditure = int($netincome * 0.2 * 0.8); 290
291 my $militaryExpenditure = int($netincome * 0.2 * 0.3);
242 292
243 if($currentCost < $militaryExpenditure) { 293 if($currentCost < $militaryExpenditure) {
244 printf("Current army expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure); 294 Carp::carp("Current army expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure);
245 } 295 return 1;
296 }
297 return 0;
246 } 298 }
247 1; 299 1;
248 300
249 301
250 package main; 302 package main;
256 308
257 # Genereic rules for both overall and city level arranagement. 309 # Genereic rules for both overall and city level arranagement.
258 my $rules = Ikariam::Cities::Rules->new($i); 310 my $rules = Ikariam::Cities::Rules->new($i);
259 # blanace resources, arrange defance 311 # blanace resources, arrange defance
260 my $tree = LoadFile('overall.yaml'); 312 my $tree = LoadFile('overall.yaml');
261 my $decision; 313 my $decision; $decision->{parse_path} = []; $decision->{parse_answer} = undef;
262 $decision->{parse_path} = [];
263 $decision->{parse_answer} = undef;
264 my $action = ParseTree($tree, $rules, $decision); 314 my $action = ParseTree($tree, $rules, $decision);
265 triggerAction($action, (keys(%$cities))[0]) if(defined($action)); 315 triggerAction($action, (keys(%$cities))[0]) if(defined($action));
266 # print STDERR Dump($decision) if(defined($verbose)); 316 # print STDERR Dump($decision) if(defined($verbose));
267 317
268 # show cities. 318 # show cities.
269 foreach my $cityId (keys(%$cities)) { 319 foreach my $cityId (keys(%$cities)) {
270 printf("%s http://%s/index.php?view=city&id=%d\n", 320 printf("%s http://%s/index.php?view=city&id=%d\n",
271 $cities->{$cityId}->{name}, $::server, $cityId); 321 $cities->{$cityId}->{name}, $::server, $cityId);
272 printf("construction: %s\n", $cities->{$cityId}->{construction}); 322 printf("construction: %s\n", $cities->{$cityId}->{construction});
273 323
274 # foreach my $thing (qw/resources space wars buildings citizens army fleet/) {
275 foreach my $thing (qw/resources space buildings citizens army fleet/) { 324 foreach my $thing (qw/resources space buildings citizens army fleet/) {
276 printf("<%s>\n", uc($thing)); 325 printf("<%s>\n", uc($thing));
277 foreach my $i (keys(%{$cities->{$cityId}->{$thing}})) { 326 foreach my $i (keys(%{$cities->{$cityId}->{$thing}})) {
278 printf("%s %s, ", $i, $cities->{$cityId}->{$thing}->{$i}); 327 printf("%s %s, ", $i, $cities->{$cityId}->{$thing}->{$i});
279 } 328 }
281 } 330 }
282 print("\n"); 331 print("\n");
283 332
284 # maybe this should be moved to Rules. 333 # maybe this should be moved to Rules.
285 $i->blanceHurmanResource($cityId); 334 $i->blanceHurmanResource($cityId);
286 # milityExpenditure 335
287 $rules->rule_milityExpenditure($cityId); 336 $tree = LoadFile('military.yaml');
288 $rules->rule_navyExpenditure($cityId); 337 $cities->{$cityId}->{parse_path} = [];
338 $cities->{$cityId}->{parse_answer} = undef;
339 if (my $action = ParseTree($tree, $rules, $cities->{$cityId})) {
340 triggerAction($action, $cityId);
341 }
342 DumpFile("army-dump.yaml", $cities);
343
289 344
290 # build and upgrade for cities 345 # build and upgrade for cities
291 $tree = LoadFile('city.yaml'); 346 $tree = LoadFile('city.yaml');
292 $cities->{$cityId}->{parse_path} = []; 347 $cities->{$cityId}->{parse_path} = [];
293 $cities->{$cityId}->{parse_answer} = undef; 348 $cities->{$cityId}->{parse_answer} = undef;
294 while (my $action = ParseTree($tree, $rules, $cities->{$cityId})) { 349 if (my $action = ParseTree($tree, $rules, $cities->{$cityId})) {
295 triggerAction($action, $cityId); 350 triggerAction($action, $cityId);
296 last; 351 }
297 } 352 DumpFile("cities-dump.yaml", $cities);
298 # Debug 353 }
299 # print(Dump($cities->{$cityId}->{parse_path})) if(defined($verbose));
300 }
301 # print(Dump($cities)) if(defined($verbose));
302 DumpFile("cities-dump.yaml", $cities);
303 DumpFile("research-dump.yaml", $i->{'research'}); 354 DumpFile("research-dump.yaml", $i->{'research'});
304 DumpFile("military-dump.yaml", $i->{'military'}); 355 DumpFile("military-dump.yaml", $i->{'military'});
305 356
306 $i->logout; 357 $i->logout;
307 358