Mercurial > eagle-eye
comparison Ikariam/CitiesRules.pm @ 371:0522468991c1
added flash attacking script.
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Tue, 14 Apr 2009 16:59:14 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
370:ba1643654e6d | 371:0522468991c1 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 package Ikariam::CitiesRules; | |
4 use strict; | |
5 use Data::Dumper; | |
6 | |
7 sub new { | |
8 my ( $class, $i ) = @_; | |
9 my $self = { | |
10 ikariam => $i, | |
11 }; | |
12 return bless $self, $class; | |
13 } | |
14 | |
15 sub is_attacked { | |
16 my ($self, $city) = @_; | |
17 | |
18 return ($self->{'ikariam'}->{'military'}->{attack} > 0 ) ? 1 : 0; | |
19 } | |
20 | |
21 sub is_constructing { | |
22 my ($self, $city) = @_; | |
23 return ($city->{construction} > 0 ) ? 1 : 0; | |
24 } | |
25 | |
26 sub is_wall_enough { | |
27 my ($self, $city) = @_; | |
28 # http://ikariam.wikia.com/wiki/Wall_Defense | |
29 # Basic rule - the wall level must equale or large then townHall level. | |
30 return ($city->{buildings}->{wall} >= $city->{buildings}->{townHall} ? 1 : 0); | |
31 } | |
32 | |
33 sub is_academy_enough { | |
34 my ($self, $city) = @_; | |
35 return ($city->{buildings}->{academy} >= 12 ? 1 : 0); | |
36 } | |
37 | |
38 sub is_embassy_enough { | |
39 my ($self, $city) = @_; | |
40 return 1; | |
41 return ($city->{buildings}->{embassy} >= 1 ? 1 : 0); | |
42 return ($city->{buildings}->{embassy} >= 6 ? 1 : 0); | |
43 } | |
44 | |
45 sub is_museum_enough { | |
46 my ($self, $city) = @_; | |
47 return 1 if($city->{buildings}->{museum} >= 8); | |
48 } | |
49 | |
50 sub is_tavern_enough { | |
51 my ($self, $city) = @_; | |
52 return 0 if(!defined($city->{buildings}->{tavern})); | |
53 return ($city->{buildings}->{tavern} >= 12 ? 1 : 0); | |
54 } | |
55 | |
56 sub is_branchOffice_enough { | |
57 my ($self, $city) = @_; | |
58 return ($city->{buildings}->{branchOffice} >= 6 ? 1 : 0); | |
59 } | |
60 | |
61 sub is_port_available { | |
62 my ($self, $city) = @_; | |
63 return 0 if(!defined($city->{buildings}->{port})); | |
64 return 1; | |
65 } | |
66 | |
67 sub is_port_enough { | |
68 my ($self, $city) = @_; | |
69 return ($city->{buildings}->{port} >= 12 ? 1 : 0); | |
70 } | |
71 | |
72 sub is_space_enough { | |
73 my ($self, $city) = @_; | |
74 # The maximum town hall is level 20, then we build new town | |
75 return 1 if($city->{buildings}->{townHall} >= 15); | |
76 # TODO 應該以 成長率 * 升級所需時間計算 | |
77 # 6 hours earlier, we upgrade the twonHall. | |
78 return ($city->{space}->{total} <= ($city->{space}->{occupied} + ($city->{growth}*12)) ? 0 : 1) | |
79 } | |
80 | |
81 sub is_safehouse_enough { | |
82 my ($self, $city) = @_; | |
83 | |
84 return 1; | |
85 | |
86 return 0 if(!defined($city->{buildings}->{safehouse})); | |
87 return 1 if(($city->{buildings}->{townHall} - $city->{buildings}->{wall}) > 2); | |
88 return 1 if($city->{buildings}->{townHall} >= 20); | |
89 | |
90 # build the higgest safehouse. | |
91 # maybe we should have more then 4 towns, then we consider that we should upgrade safehouse at level 20. | |
92 # return (($city->{buildings}->{townHall} > $city->{buildings}->{safehouse}) ? 0 : 1) | |
93 # return (($city->{buildings}->{townHall} <= ($city->{buildings}->{safehouse} + 4)) ? 0 : 1); | |
94 | |
95 # Safehouse must be same level as townHall, | |
96 return (($city->{buildings}->{townHall} >= $city->{buildings}->{safehouse}) ? 0 : 1); | |
97 } | |
98 | |
99 sub is_warehouse_available { | |
100 my ($self, $city) = @_; | |
101 return 0 if(!defined($city->{buildings}->{warehouse})); | |
102 return 1; | |
103 } | |
104 | |
105 sub is_warehouse_more_then_eight { | |
106 my ($self, $city) = @_; | |
107 my $cityId = $city->{id}; | |
108 return 0 if(!defined($city->{buildings}->{warehouse})); | |
109 | |
110 my @locations = @{$city->{locations}}; | |
111 my $count = 0; | |
112 foreach (3..14) { | |
113 $count++ if($locations[$_] eq "warehouse"); | |
114 return 1 if($count >= 8); | |
115 } | |
116 return 0; | |
117 } | |
118 | |
119 sub is_warehouse_enough { | |
120 my ($self, $city) = @_; | |
121 | |
122 return 1; | |
123 # my @warehouse = (qw/undef undef 0 4 9 16 18 19 20 21 22 23 24 25/); | |
124 my @warehouse = (qw/undef undef 0 4 9 15 17 18 19 20 21 22 23 24/); | |
125 my @cities = keys(%{$self->{ikariam}->{cities}}); | |
126 my $nextCities = ($#cities + 1) + 1; | |
127 | |
128 Carp::carp(sprintf("Required warehouse level %s for next city (%s), current is %s\n", $warehouse[$nextCities], $nextCities, $city->{buildings}->{warehouse})); | |
129 return 0 if(!defined($city->{buildings}->{warehouse})); | |
130 return ($city->{buildings}->{warehouse} >= $warehouse[$nextCities]) ? 1 : 0; | |
131 } | |
132 | |
133 | |
134 sub is_warehouse_enougn_for_governorsresidence { | |
135 my ($self, $city) = @_; | |
136 return (($city->{buildings}->{warehouse} >= 5) ? 0 : 1); | |
137 | |
138 my @warehouse = (qw/undef undef 0 4 9 16 18 19 20 21 22 23 24 25/); | |
139 my @cities = keys(%{$self->{ikariam}->{cities}}); | |
140 my $citiesNumber = $#cities + 1; | |
141 | |
142 Carp::carp(sprintf("Required warehouse level %s for %s cities, current is %s\n", $warehouse[$citiesNumber], $citiesNumber, $city->{buildings}->{warehouse})); | |
143 return 0 if(!defined($city->{buildings}->{warehouse})); | |
144 return ($city->{buildings}->{warehouse} >= $warehouse[$citiesNumber]) ? 1 : 0; | |
145 } | |
146 | |
147 sub is_corruption { | |
148 my ($self, $city) = @_; | |
149 return ($city->{corruption} > 0) ? 1 : 0; | |
150 } | |
151 | |
152 sub is_any_corruption { | |
153 my ($self, $city) = @_; | |
154 | |
155 foreach (keys(%{$self->{ikariam}->{cities}})) { | |
156 return 1 if ($self->{ikariam}->{cities}->{$_}->{corruption} > 0); | |
157 } | |
158 return 0; | |
159 } | |
160 | |
161 sub is_happiness { | |
162 my ($self, $city) = @_; | |
163 | |
164 return ($city->{growth} >= 5 ? 1 : 0) | |
165 if($city->{buildings}->{townHall} <= 10); | |
166 | |
167 return ($city->{growth} >= 2 ? 1 : 0) | |
168 if($city->{buildings}->{townHall} <= 15); | |
169 | |
170 return 1 if($city->{buildings}->{townHall} >= 20); | |
171 } | |
172 | |
173 | |
174 sub is_bacchanal { | |
175 my ($self, $city) = @_; | |
176 return ($city->{tavern}->{maxValue} == $city->{tavern}->{iniValue} ? 1 : 0); | |
177 } | |
178 | |
179 sub is_gold_enoughforcargo { | |
180 my ($self, $city) = @_; | |
181 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/; | |
182 | |
183 if($city->{resources}->{gold} >= ($cargoCost[$city->{transporters}->{sum}]*5)) { | |
184 return 1; | |
185 } else { | |
186 return 0; | |
187 } | |
188 } | |
189 | |
190 sub is_resource_enoghforHall { | |
191 my ($self, $city) = @_; | |
192 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/; | |
193 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/; | |
194 | |
195 my $level = $city->{buildings}->{townHall}; | |
196 if($city->{resources}->{wood} >= $wood[$level] && $city->{resources}->{marble} >= $marble[$level] ) { | |
197 return 1; | |
198 } | |
199 warn(sprintf("Resource is short for build city hall. wood [%d] marble [%d] ", $wood[$level], $marble[$level])); | |
200 return 0; | |
201 } | |
202 | |
203 sub is_expansion_researched { | |
204 my ($self, $city) = @_; | |
205 # $self->{'ikariam'}-> | |
206 return (defined($self->{'ikariam'}->{research}->{1030}) ? 1 : 0); | |
207 } | |
208 | |
209 sub is_foreigncultures_researched { | |
210 my ($self) = @_; | |
211 return (defined($self->{'ikariam'}->{research}->{1040}) ? 1 : 0); | |
212 } | |
213 | |
214 sub is_greekfire_researched { | |
215 my ($self) = @_; | |
216 return (defined($self->{'ikariam'}->{research}->{1060}) ? 1 : 0); | |
217 } | |
218 | |
219 sub is_paddlewheelengine_researched { | |
220 my ($self) = @_; | |
221 return (defined($self->{'ikariam'}->{research}->{1100}) ? 1 : 0); | |
222 } | |
223 | |
224 sub is_conservation_researched { | |
225 my ($self) = @_; | |
226 return (defined($self->{'ikariam'}->{research}->{2010}) ? 1 : 0); | |
227 } | |
228 | |
229 sub is_wealth_researched { | |
230 my ($self) = @_; | |
231 return (defined($self->{'ikariam'}->{research}->{2030}) ? 1 : 0); | |
232 } | |
233 | |
234 sub is_winepress_researched { | |
235 my ($self) = @_; | |
236 return (defined($self->{'ikariam'}->{research}->{2040}) ? 1 : 0); | |
237 } | |
238 | |
239 sub is_paper_researched { | |
240 my ($self) = @_; | |
241 return (defined($self->{'ikariam'}->{research}->{3020}) ? 1 : 0); | |
242 } | |
243 | |
244 sub is_espionage_researched { | |
245 my ($self) = @_; | |
246 return (defined($self->{'ikariam'}->{research}->{3030}) ? 1 : 0); | |
247 } | |
248 | |
249 sub is_invention_researched { | |
250 my ($self) = @_; | |
251 return (defined($self->{'ikariam'}->{research}->{3040}) ? 1 : 0); | |
252 } | |
253 | |
254 sub is_culturalexchange_researched { | |
255 my ($self) = @_; | |
256 return (defined($self->{'ikariam'}->{research}->{3060}) ? 1 : 0); | |
257 } | |
258 | |
259 sub is_professionalarmy_researched { | |
260 my ($self) = @_; | |
261 return (defined($self->{'ikariam'}->{research}->{4030}) ? 1 : 0); | |
262 } | |
263 | |
264 sub is_drydock_researched { | |
265 my ($self) = @_; | |
266 return (defined($self->{'ikariam'}->{research}->{4010}) ? 1 : 0); | |
267 } | |
268 | |
269 sub is_robotics_researched { | |
270 my ($self) = @_; | |
271 return (defined($self->{'ikariam'}->{research}->{4110}) ? 1 : 0); | |
272 } | |
273 | |
274 sub is_barracks_available { | |
275 my ($self, $city) = @_; | |
276 return 0 if(!defined($city->{buildings}->{barracks})); | |
277 return 1; | |
278 } | |
279 | |
280 sub is_barracks_level_enough { | |
281 my ($self, $city) = @_; | |
282 return 0 if(!defined($city->{buildings}->{barracks})); | |
283 if($self->is_robotics_researched() eq 1) { | |
284 return ($city->{buildings}->{barracks} >= 24 ? 1 : 0); | |
285 } | |
286 | |
287 if($city->{buildings}->{townHall} >= 10) { | |
288 # optimum is 5 | |
289 return ($city->{buildings}->{barracks} >= 5 ? 1 : 0); | |
290 } else { | |
291 # 方陣兵需要 level 4 | |
292 return ($city->{buildings}->{barracks} >= 4 ? 1 : 0); | |
293 } | |
294 } | |
295 | |
296 sub is_shipyard_availble { | |
297 my ($self, $city) = @_; | |
298 return 0 if(!defined($city->{buildings}->{shipyard})); | |
299 return 1; | |
300 } | |
301 | |
302 sub is_shipyard_level_enough { | |
303 my ($self, $city) = @_; | |
304 return 0 if(!defined($city->{buildings}->{shipyard})); | |
305 if ($self->is_paddlewheelengine_researched() eq 1) { | |
306 return ($city->{buildings}->{shipyard} >= 16 ? 1 : 0); | |
307 } elsif ($self->is_greekfire_researched() eq 1) { | |
308 return ($city->{buildings}->{shipyard} >= 5 ? 1 : 0); | |
309 } else { | |
310 return ($city->{buildings}->{shipyard} >= 3 ? 1 : 0); | |
311 } | |
312 } | |
313 | |
314 sub is_shipyard_upgrading { | |
315 my ($self, $city) = @_; | |
316 return $self->{'ikariam'}->is_shipyard_upgrading($city->{id}); | |
317 } | |
318 | |
319 sub is_navy_trainning { | |
320 my ($self, $city) = @_; | |
321 return $self->{'ikariam'}->is_navy_trainning($city->{id}); | |
322 } | |
323 | |
324 sub train_navy { | |
325 my ($self, $city) = @_; | |
326 my $cityId = $city->{id}; | |
327 # TODO, 依照升級比例算 CP 值最高 | |
328 if($self->is_greekfire_researched() eq 1 && $city->{buildings}->{shipyard} >= 5) { | |
329 # ok, we can build Flamethrower. | |
330 if(defined($city->{'fleet'}->{Flamethrower}) && ($city->{'fleet'}->{Flamethrower} > 0) | |
331 && ($city->{'fleet'}->{BallistaShip} / $city->{'fleet'}->{Flamethrower}) <= (1.5/1)) { | |
332 return 213; # 強弩船 | |
333 } else { | |
334 return 211; # 噴火船 | |
335 } | |
336 } else { | |
337 return 213; # 強弩船 | |
338 } | |
339 } | |
340 | |
341 sub is_barracks_upgrading { | |
342 my ($self, $city) = @_; | |
343 return $self->{ikariam}->is_barracks_upgrading($city->{id}); | |
344 } | |
345 | |
346 sub is_army_trainning { | |
347 my ($self, $city) = @_; | |
348 return $self->{ikariam}->is_army_trainning($city->{id}); | |
349 } | |
350 sub train_army { | |
351 my ($self, $city) = @_; | |
352 my $cityId = $city->{id}; | |
353 | |
354 # TODO, 依照升級比例算 CP 值最高 | |
355 my $assault = 'Swordsman'; | |
356 my $resistance = 'Phalanx'; | |
357 | |
358 if(($city->{'army'}->{$assault} / $city->{'army'}->{$resistance}) <= (2/1)) { | |
359 return 302; # Swordsman | |
360 } else { | |
361 return 303; # Phalanx | |
362 } | |
363 } | |
364 | |
365 # navy | |
366 sub is_navyExpenditure_available | |
367 { | |
368 my ($self, $city) = @_; | |
369 my $cityId = $city->{id}; | |
370 | |
371 # move this to somewhere else. | |
372 my $workersRatio = { | |
373 'citizens' => 0.4, | |
374 'specialworkers' => 0.3, | |
375 'woodworkers' => 0.7, | |
376 }; | |
377 | |
378 my $currentCost = $self->{'ikariam'}->getNavyExpenditure($cityId); | |
379 my $netincome = $self->{'ikariam'}->getNetIncome($cityId); | |
380 | |
381 # 軍費為 兩成 淨收入 | |
382 # 陸軍佔用 0.3 | |
383 # 海軍佔用 0.7 | |
384 my $militaryExpenditure = int($netincome * 0.2 * 0.7); | |
385 | |
386 if($currentCost < $militaryExpenditure) { | |
387 printf("Current navy expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure); | |
388 return 1; | |
389 } | |
390 return 0; | |
391 } | |
392 | |
393 # army | |
394 sub is_milityExpenditure_available | |
395 { | |
396 my ($self, $city) = @_; | |
397 my $cityId = $city->{id}; | |
398 | |
399 # move this to somewhere else. | |
400 my $workersRatio = { | |
401 'citizens' => 0.4, | |
402 'specialworkers' => 0.3, | |
403 'woodworkers' => 0.7, | |
404 }; | |
405 | |
406 my $currentCost = $self->{'ikariam'}->getMilityExpenditure($cityId); | |
407 my $netincome = $self->{'ikariam'}->getNetIncome($cityId); | |
408 | |
409 # 軍費為 兩成 淨收入 | |
410 # 陸軍佔用 0.3 | |
411 # 海軍佔用 0.7 | |
412 | |
413 my $militaryExpenditure = int($netincome * 0.2 * 0.3); | |
414 | |
415 if($currentCost < $militaryExpenditure) { | |
416 Carp::carp("Current army expenditure total=%s, affordable %s\n", $currentCost, $militaryExpenditure); | |
417 return 1; | |
418 } | |
419 return 0; | |
420 } | |
421 | |
422 1; | |
423 |