This repository has been archived by the owner on Sep 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyBot.pm
434 lines (317 loc) · 11.8 KB
/
MyBot.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package MyBot;
use strict;
use warnings;
use base 'AIAnts::BotHash';
use Time::HiRes ();
=head1 NAME
MyBot for L<AIAnts> game.
=head1 SYNOPSIS
Google AI Challenge 2011 "AI Ants" game Perl bot.
=head1 METHODS
=head2 get_empty_goal_ant_nof
Number of different items counted for each hill.
=cut
sub get_empty_goal_ant_nof {
return {
food => 0,
explore => 0,
attack => 0,
defend => 0,
};
}
=head2 setup
Setup.
=cut
sub setup {
my $self = shift;
$self->SUPER::setup( @_ );
$self->dump( {@_}, 'game_config' ) if $self->{log};
$self->{ant_goal} = {};
$self->{food2ant} = {};
# Number of ants for reach goal.
$self->{goal_ant_nof} = $self->get_empty_goal_ant_nof();
$self->{hill_goal_ant_nof} = {};
}
=head2 new_hill_found
Called during 'turn_body' if new ant was spawed (found/created).
=cut
sub new_hill_found {
my ( $self, $hill_num, $x, $y ) = @_;
$self->log("new hill $hill_num found on $x,$y\n") if $self->{log};
$self->{hill_goal_ant_nof}{$hill_num} = get_empty_goal_ant_nof();
return 1;
}
=head2 ant_spawed
Called during 'turn_body' if new ant was spawed (found/created).
=cut
sub ant_spawed {
my ( $self, $ant, $x, $y, $ant_hill ) = @_;
$self->log("ant$ant spawed at $x.$y on hill $ant_hill\n") if $self->{log};
return 1;
}
sub remove_ant_goal {
my ( $self, $ant, $ant_hill ) = @_;
return 1 unless exists $self->{ant2goal}{$ant};
my $goal_name = $self->{ant2goal}{$ant}{name};
$self->{goal_ant_nof}{ $goal_name }--;
$self->{hill_goal_ant_nof}{ $ant_hill }{ $goal_name }--;
delete $self->{ant2goal}{$ant};
return 1;
}
=head2 ant_died
Called during 'turn_body' if new ant died (was not found on expected position).
=cut
sub ant_died {
my ( $self, $ant, $x, $y, $ant_hill ) = @_;
$self->log("ant$ant died at $x.$y\n") if $self->{log};
$self->remove_ant_goal( $ant, $ant_hill );
return 1;
}
sub new_defender_needed {
my ( $self, $ant_hill ) = @_;
my $ant_to_hill_ratio = $self->{chc_num}{ant_to_hill_ration};
# Too few ants -> gather for food.
return 0 if $ant_to_hill_ratio < 5;
# More than 25% defenders.
return 0 if $self->{hill_goal_ant_nof}{ $ant_hill }{defend} > ($ant_to_hill_ratio * 0.25);
return 1;
}
=head2 get_new_ant_goal
Get new goal for ant.
=cut
sub get_new_ant_goal {
my ( $self, $ant, $ant_x, $ant_y, $ant_hill, $used ) = @_;
my ( $x, $y );
my $map_obj = $self->{m};
# defend (defend hill where ant was born)
# Each 1/8 should defend own hill.
if ( $self->new_defender_needed($ant_hill) ) {
my $attemt = 1;
my ( $hill_x, $hill_y ) = @{ $self->{hill2pos}{$ant_hill} };
while ( 1 ) {
( $x, $y ) = $map_obj->pos_plus(
$hill_x, $hill_y,
int(rand ($attemt+2)),
int(rand ($attemt+2)),
);
last if $self->{m}->valid_not_used_pos( $x, $y, $used );
return undef if $attemt > 50;
$attemt++;
}
my $max_turns = int rand 10;
return {
name => 'defend',
pos => [ $x, $y ],
turns => $max_turns,
path => $map_obj->empty_path_temp(),
};
}
# attack
if ( $self->{chc_num}{ant_to_hill_ration} > 10 ) {
foreach my $e_hill_data ( values %{ $self->{pos2e_hill} } ) {
my ( $eh_x, $eh_y ) = @$e_hill_data;
$self->set_nearest_ants_to_attack( $eh_x, $eh_y, 3 );
}
}
# food
( $x, $y ) = $self->{m}->get_nearest_free_food( $ant_x, $ant_y, $self->{food2ant} );
if ( defined $x ) {
$self->{food2ant}{"$x,$y"} = $ant;
return {
name => 'food',
pos => [ $x, $y ],
turns => 20,
path => $map_obj->empty_path_temp(),
};
}
# explore
my $attemts = 100;
my ( $hill_x, $hill_y ) = @{ $self->{hill2pos}{$ant_hill} };
while ( 1 ) {
my ( $dx, $dir_x, $dy, $dir_y );
if ( rand(10) > 3 && ($hill_x != $ant_x || $hill_y != $ant_y) ) {
( $dx, $dir_x, $dy, $dir_y ) = $map_obj->dist( $hill_x, $hill_y, $ant_x, $ant_y );
} else {
$dir_x = (rand 2) ? 1 : -1;
$dir_y = (rand 2) ? 1 : -1;
}
( $x, $y ) = $map_obj->pos_plus(
$ant_x, $ant_y,
$dir_x * ( int(rand 3)+1 ),
$dir_y * ( int(rand 3)+1 ),
);
last if $self->{m}->valid_not_used_pos( $x, $y, $used );
$attemts--;
return undef if $attemts <= 0;
}
my $max_turns = int(rand(350)**0.5) + 3;
return {
name => 'explore',
pos => [ $x, $y ],
turns => $max_turns,
path => $map_obj->empty_path_temp(),
};
}
sub set_nearest_ants_to_attack {
my ( $self, $target_type, $target_x, $target_y, $ants_in_army, $re_goaled ) = @_;
$ants_in_army = 1 unless $ants_in_army;
$self->log("... computing attack to $target_type $target_x.$target_x\n") if $self->{log};
my $used = {};
my $reg_num = 0;
foreach (0..$ants_in_army) {
my ( $ant_x, $ant_y ) = $self->{m}->get_my_nearest_ant( $target_x, $target_y, $used );
last unless defined $ant_x;
my $ant_pos = "$ant_x,$ant_y";
my $ant = $self->{pos2ant}{$ant_pos};
unless ( $ant ) {
# todo
#$self->log(">>>>>>>>>> no $ant_pos\n");
#$self->dump( $self->{pos2ant} );
next;
}
my $ant_hill = $self->{ant2hill}{$ant};
$used->{$ant_pos} = 1;
my $goal = {
name => 'attack',
pos => [ $target_x, $target_y ],
turns => 20,
target_type => $target_type,
path => $self->{m}->empty_path_temp(),
};
$self->remove_ant_goal( $ant, $ant_hill );
$self->log("... ant$ant $ant_x.$ant_y set to attack\n") if $self->{log};
$self->set_ant_goal( $ant_x, $ant_y, $goal, $ant, $ant_hill );
$reg_num++;
$re_goaled->{$ant_pos} = 1 if defined $re_goaled;
}
return $reg_num;
}
=head2 set_ant_goal
Set new ant goal.
=cut
sub set_ant_goal {
my ( $self, $ant_x, $ant_y, $goal, $ant, $ant_hill) = @_;
$ant = $self->{pos2ant}{"$ant_x,$ant_y"} unless defined $ant;
$ant_hill = $self->{ant2hill}{$ant} unless defined $ant_hill;
$self->{ant2goal}{$ant} = $goal;
my $goal_name = $goal->{name};
$self->{goal_ant_nof}{ $goal_name }++;
$self->{hill_goal_ant_nof}{ $ant_hill }{ $goal_name }++;
$self->log("goal set ant$ant $ant_x.$ant_y to '$goal->{name}' pos $goal->{pos}[0].$goal->{pos}[1] in $goal->{turns} turns\n") if $self->{log};
return 1;
}
=head2 goal_still_valid
Check if ant's goal is still valid.
=cut
sub goal_still_valid {
my ( $self, $ant, $ant_x, $ant_y, $ant_hill ) = @_;
my $goal = $self->{ant2goal}{$ant};
# goal turns limit reached
if ( $goal->{turns} <= 0 ) {
$self->log("goal ant$ant turn limit reached\n") if $self->{log};
return 0;
}
my $goal_name = $goal->{name};
my ( $x, $y ) = @{ $goal->{pos} };
# food
if ( $goal_name eq 'food' ) {
return 1 if $self->{m}->food_exists($x,$y);
$self->log("goal ant$ant removed - no food on $x,$y\n") if $self->{log};
delete $self->{food2ant}{"$x,$y"};
return 0;
}
# not valid only if goal turns limit reached (above)
return 1 if $goal_name eq 'defend';
return 1 if $goal_name eq 'attack';
# explore
if ( $goal_name eq 'explore' ) {
if ( $self->{hill_goal_ant_nof}{ $ant_hill }{food} < $self->{chc_num}{ant_to_hill_ration} * 0.5 ) {
$self->log("goal ant$ant removed - too few 'food' goals \n") if $self->{log};
return 0;
}
return 1 if $x != $ant_x || $y != $ant_y;
$self->log("goal ant$ant removed - on postion on $x.$y\n") if $self->{log};
return 0;
}
$self->log("goal ant$ant unknown type '$goal_name'\n") if $self->{log};
return 1;
}
=head2 step_to_goal
Return ( $dir, $Nx, $Ny ) of next step to meat ant goal.
=cut
sub step_to_goal {
my ( $self, $ant, $ant_x, $ant_y, $used, $turn_data, $stop_turntime ) = @_;
my $goal = $self->{ant2goal}{$ant};
return () unless ref $goal;
$goal->{turns}--;
my ( $goal_x, $goal_y ) = @{ $goal->{pos} };
if ( $stop_turntime < Time::HiRes::time()+10/1000 ) {
$self->log("...using easy computation of dir_from_to ant$ant $ant_x.$ant_y to $goal_x.$goal_y ($stop_turntime < ".(Time::HiRes::time()+10/1000)."\n") if $self->{log};
return $self->{m}->dir_from_to_easy( $ant_x, $ant_y, $goal_x, $goal_y, $used, $goal->{path} );
}
# todo - turnlimit problem
$self->log("...using easy computation of dir_from_to ant$ant $ant_x.$ant_y to $goal_x.$goal_y ($stop_turntime < ".(Time::HiRes::time()+10/1000)."\n") if $self->{log};
return $self->{m}->dir_from_to_easy( $ant_x, $ant_y, $goal_x, $goal_y, $used, $goal->{path} );
$self->log("...using full computation of dir_from_to ant$ant $ant_x.$ant_y to $goal_x.$goal_y ($stop_turntime < ".(Time::HiRes::time()+10/1000)."\n") if $self->{log};
return $self->{m}->dir_from_to( $ant_x, $ant_y, $goal_x, $goal_y, $used, $goal->{path} );
}
=head2 turn_body
Main part of turn processing. Should call 'add_order' method during processing.
=cut
sub turn_body {
my ( $self, $turn_num, $turn_data, $turn_diff, $stop_turntime ) = @_;
$self->log( "turn $turn_num\n" ) if $self->{log};
#$self->log( $self->{m}->dump(1) . "\n\n" ) if $self->{log};
#$self->dump( $turn_data ) if $self->{log};
my $used = $self->get_initial_used( $turn_data );
# Attack hill just found.
my $re_goaled = {};
foreach my $pos_str ( keys %{ $turn_diff->{e_hill}{add} } ) {
my ( $e_hill_x, $e_hill_y ) = @{ $self->{pos2e_hill}{$pos_str} };
$self->set_nearest_ants_to_attack( 'hive', $e_hill_x, $e_hill_y, 5, $re_goaled );
}
foreach my $data ( values %{$turn_data->{m_ant}} ) {
my ( $ant_x, $ant_y ) = @$data;
my $ant_pos = "$ant_x,$ant_y";
my $ant = $self->{pos2ant}{$ant_pos};
my $ant_hill = $self->{ant2hill}{$ant};
if ( exists $re_goaled->{$ant_pos} ) {
# not need to set new goal
} elsif ( not exists $self->{ant2goal}{$ant} ) {
$self->log("goal ant$ant setting the first goal\n") if $self->{log};
my $goal = $self->get_new_ant_goal( $ant, $ant_x, $ant_y, $ant_hill, $used );
$self->set_ant_goal( $ant_x, $ant_y, $goal, $ant, $ant_hill, $used ) if defined $goal;
} elsif ( not $self->goal_still_valid($ant,$ant_x,$ant_y,$ant_hill) ) {
$self->log("goal ant$ant no valid, setting new goal\n") if $self->{log};
my $goal = $self->get_new_ant_goal( $ant, $ant_x, $ant_y, $ant_hill, $used );
$self->set_ant_goal( $ant_x, $ant_y, $goal, $ant, $ant_hill, $used ) if defined $goal;
}
my ( $dir, $Nx, $Ny ) = $self->step_to_goal( $ant, $ant_x, $ant_y, $used, $turn_data, $stop_turntime );
if ( (not defined $dir) || ($Nx == $ant_x && $Ny == $ant_y) ) {
#$self->log("... move ant$ant stay on $ant_x.$ant_y\n") if $self->{log};
$used->{$ant_pos} = 2;
} else {
#$self->log("... move ant$ant $ant_x.$ant_y $dir to $Nx.$Ny\n") if $self->{log};
$self->add_order( $ant, $ant_x, $ant_y, $dir, $Nx, $Ny );
delete $used->{$ant_pos};
$used->{"$Nx,$Ny"} = 2;
}
}
$self->log("\n") if $self->{log};
return 1;
}
=head2 game_over
Called when game ends.
=cut
sub game_over {
my ( $self ) = @_;
$self->dump( $self->{ant2goal} ) if $self->{log};
return $self->SUPER::game_over();
}
=head1 AUTHOR
Michal Jurosz, mj@mj41.cz
=head1 LICENSE
This is free software; you can redistribute it and/or modify it under the same
terms as the Perl 5 programming language system itself.
=cut
1;