AIBot.pm

filter: Statementstmt 10.2% 15 / 146 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 17.3% 4 / 23 Podpod 0.0% 0 / 8 totaltotal 5.9% 19 / 322 CC 61 SCAR 56.6
CC
61
Cov
6%
tick
812.0
loadfile
42.0
_turnto
42.0
CRAP 286.8
« AIBots.pm ↑ Summary
1 # $File: //member/autrijus/AIBots/lib/Games/AIBot.pm $ $Author: autrijus $
2 # $Revision: #2 $ $Change: 692 $ $DateTime: 2002/08/17 09:29:13 $
3  
4 1 require 5.005;
5 package Games::AIBot;
6 1 $Games::AIBot::VERSION = '0.01';
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Games::AIBot - An AI Bot object
14  
15 =head1 VERSION
16  
17 This document describes version 0.01 of Locale::Maketext::Fuzzy.
18  
19 =head1 SYNOPSIS
20  
21 use Games::AIBot;
22 my $bot = Games::AIBot->new($botfile);
23 $bot->tick;
24  
25 =head1 DESCRIPTION
26  
27 This module exists exclusively for the purpose of the F<aibots>
28 script bundled in the distribution. Please see L<aibots> for
29 an explanation of the game's mechanics, rules and tips.
30  
31 =cut
32  
33 1 ▶use fields qw/max_fuel max_ammo max_life
Subroutine BEGIN: called CC=1 SCAR=0.0
34 fuel ammo life
35 x y h score
36 enemy_x enemy_y enemy_h enemy_l
37 snode_x snode_y
38 friend_x friend_y friend_h friend_l
39 bumped_x bumped_y
40 shield cloak laymine
41 bumped found burn
42 id dead botcount
43 lastcmd state var pic
44 name author team stack
45 queue missiles cmds line
46 1 lineidx stateidx condidx/;
47  
48  
49 # ===========
50 # Constructor
51 # ===========
52  
53 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=35.1
Pod undocumented
54 0 my $class = shift;
55 my $bot = ($] > 5.00562) ? fields::new($class)
56 0 ▶ : do { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
$] > 5.00562 ? : 0 0 0
57 0 bless [\%{$class.'::FIELDS'}], $class };
58 0 $bot->loadfile($_[0]);
59 0 push @{$bot->{'cmds'}}, 'attempt destruct';
60  
61 0 my $count;
62 0 foreach my $line (@{$bot->{'cmds'}}) {
63 0 $count++;
64 0 my $condflag = int($line !~ m/^(?:\$|if|else|elsif|unless|print)/);
65 0 ▶ $line =~ s/\$(\w+)/exists($bot->[0]->{$1}) ? "\${\$bot}{$1}" : "\${\$bot}{'var'}{$1}"/eg;
Branch: 1 branch
BranchTrueFalseTotal
exists $bot->[0]{$1} ? : 0 0 0
66 0 $line =~ s/\&(?=\w+)/\$bot->_/g;
67 0 $bot->{'lineidx'} .= $condflag; # and (index($line, '$') > -1));
68 0 ▶ $bot->{'stateidx'}{$1} = $count
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^sub[\s\t]+(.+)[\s\t]+{/ or $line =~ /^(.+):[\s\t]*{/ 0 0 0
Condition0%0/3
$line =~ /^sub[\s\t]+(.+)[\s\t]+{/ or $line =~ /^(.+):[\s\t]*{/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^sub[\s\t]+(.+)[\s\t]+{/ or $line =~ /^(.+):[\s\t]*{/
$line =~ /^sub[\s\t]+(.+)[\s\t]+{/ $line =~ /^(.+):[\s\t]*{/
Truth table0%0/3
A or B
A= $line =~ /^sub[\s\t]+(.+)[\s\t]+{/B= $line =~ /^(.+):[\s\t]*{/
ABresult
1X1
011
000
69 if $line =~ /^sub[\s\t]+(.+)[\s\t]+{/ or $line =~ /^(.+):[\s\t]*{/;
70 0 $bot->{'condidx'} .= int($line ne '}') +
71 ($line =~ /^(?:if|unless|elsif|else|sub|.+:)[\s\t]/);
72 }
73  
74 0 $bot->{'queue'} = [];
75 0 $bot->{'missiles'} = [];
76 0 $bot->{'line'} = 0;
77  
78 0 return $bot;
79 }
80  
81 0 ▶sub loadfile {
Subroutine loadfile: not called CC=6 SCAR=37.4
Pod undocumented
82 0 my ($bot, $file) = @_;
83 0 my @include;
84  
85 0 ▶ open _, $file or die "Cannot load bot $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open _, $file or die "Cannot load bot ${file}: $!" 0 0 0
86  
87 0 ▶ while (<_>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline _)) 0 0 0
88 0 chomp;
89 0 s/#[\s\t].+//;
90 0 s/^[\s\t]+//;
91 0 s/[\s\t\;]+$//;
92 0 s/^(.+)[\s\t]+(if|unless)[\s\t]+(.+)$/$2 ($3) {\n$1\n}\n/g;
93 0 ▶ if (/^require[\s\t]+\'?([^\']+)\'?/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^require[\s\t]+\'?([^\']+)\'?/) { } 0 0 0
94 0 push @include, substr($file, 0, rindex($file, '/')+1).$1;
95 }
96 else {
97 0 ▶ push (@{$bot->{'cmds'}}, split("\n", $_)) if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
98 }
99 }
100  
101 0 close _;
102  
103 0 $bot->loadfile($_) foreach @include;
104 }
105  
106 0 ▶sub cond {
Subroutine cond: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my $bot = $_[0];
108 0 my $cmd = eval($_[1]);
109 0 ▶ if ($@) { die "[Cond $_[1] :".$bot->{'name'}.':'.$bot->{'state'}.'] '.$@ };
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
110 0 return $cmd;
111 }
112  
113 0 ▶sub tick {
Subroutine tick: not called CC=28 SCAR=67.0
Pod undocumented
114 0 my $bot = shift;
115 0 my $count;
116  
117 0 ▶ while (my $line = $bot->nextline()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $bot->nextline) 0 0 0
118 0 ▶ next if $line eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "}" 0 0 0
119 0 ▶ if ($count++ > 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count++ > 100) 0 0 0
120 0 warn "recursion too deep";
121 0 return;
122 }
123  
124 0 ▶ if ($line =~ /^\$[{\w]/) {
Branch: 10 branches
BranchTrueFalseTotal
if ($line =~ /^\$[{\w]/) { } 0 0 0
elsif ($line =~ /^(?:else|elsif)[\s\t]/) { } 0 0 0
elsif ($line =~ /^sub[\s\t]+(.+)[\s\t]+{$/ or $line =~ /^(.+):[\s\t]*{$/) { } 0 0 0
elsif ($line =~ /^goto[\s\t]+(.+)/) { } 0 0 0
elsif ($line =~ /^call[\s\t]+(.+)/ or $line =~ /^(.+)\(\)$/) { } 0 0 0
elsif ($line eq 'redo') { } 0 0 0
elsif ($line eq 'return') { } 0 0 0
elsif ($line =~ /^(if|unless)[\s\t]+(.+){$/) { } 0 0 0
elsif ($line =~ /^(e|d)(?:nable|isable)[\s\t]+(\w+)/) { } 0 0 0
elsif ($line =~ /^print[\s\t]+/) { } 0 0 0
125 0 $bot->cond($line);
126 }
127 elsif ($line =~ /^(?:else|elsif)[\s\t]/) {
128 0 $bot->endif();
129 }
130 ▶ elsif ($line =~ /^sub[\s\t]+(.+)[\s\t]+{$/ or $line =~ /^(.+):[\s\t]*{$/) {
Condition0%0/3
$line =~ /^sub[\s\t]+(.+)[\s\t]+{$/ or $line =~ /^(.+):[\s\t]*{$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^sub[\s\t]+(.+)[\s\t]+{$/ or $line =~ /^(.+):[\s\t]*{$/
$line =~ /^sub[\s\t]+(.+)[\s\t]+{$/ $line =~ /^(.+):[\s\t]*{$/
Truth table0%0/3
A or B
A= $line =~ /^sub[\s\t]+(.+)[\s\t]+{$/B= $line =~ /^(.+):[\s\t]*{$/
ABresult
1X1
011
000
131 0 $bot->{'state'} = $1;
132 }
133 elsif ($line =~ /^goto[\s\t]+(.+)/) {
134 0 pop @{$bot->{'stack'}};
135 0 $bot->gotostate($1);
136 }
137 ▶ elsif ($line =~ /^call[\s\t]+(.+)/ or $line =~ /^(.+)\(\)$/) {
Condition0%0/3
$line =~ /^call[\s\t]+(.+)/ or $line =~ /^(.+)\(\)$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^call[\s\t]+(.+)/ or $line =~ /^(.+)\(\)$/
$line =~ /^call[\s\t]+(.+)/ $line =~ /^(.+)\(\)$/
Truth table0%0/3
A or B
A= $line =~ /^call[\s\t]+(.+)/B= $line =~ /^(.+)\(\)$/
ABresult
1X1
011
000
138 0 push @{$bot->{'stack'}}, [@{$bot}{'state', 'line'}];
139 # print "call from line ",$bot->{'line'},"\n";
140 0 $bot->gotostate($1);
141 }
142 elsif ($line eq 'redo') {
143 0 $bot->gotostate($bot->{'state'});
144 }
145 elsif ($line eq 'return') {
146 warn $bot->{'name'}." cannot return from state ".$bot->{'state'}
147 0 ▶ unless ($bot->{'stack'} and @{$bot->{'stack'}});
Branch: 1 branch
BranchTrueFalseTotal
unless $bot->{"stack"} and @{$bot->{"stack"};} 0 0 0
Condition0%0/3
$bot->{"stack"} and @{$bot->{"stack"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$bot->{"stack"} and @{$bot->{"stack"};}
$bot->{"stack"} @{$bot->{"stack"};}
Truth table0%0/3
A and B
A= $bot->{"stack"}B= @{$bot->{"stack"};}
ABresult
0X0
100
111
148 0 eval{@{$bot}{'state', 'line'} = @{pop(@{$bot->{'stack'}})}};
149 # print "return to line ",$bot->{'line'},"\n";
150 }
151 elsif ($line =~ /^(if|unless)[\s\t]+(.+){$/) {
152 0 ▶ if ($1 eq 'if' xor $bot->cond($2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "if" xor $bot->cond($2)) 0 0 0
Condition0%0/4
$1 eq "if" xor $bot->cond($2)
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$1 eq "if" xor $bot->cond($2)
$1 eq "if" $bot->cond($2)
Truth table0%0/4
A xor B
A= $1 eq "if"B= $bot->cond($2)
ABresult
110
101
011
000
153 0 ▶ while (my $cond = $bot->elseif()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cond = $bot->elseif) 0 0 0
154 0 ▶ ($bot->{'line'}++, last) if ($bot->cond($cond));
Branch: 1 branch
BranchTrueFalseTotal
if $bot->cond($cond) 0 0 0
155 }
156 0 $bot->{'line'}--; # end all blocks
157 }
158 }
159 elsif ($line =~ /^(e|d)(?:nable|isable)[\s\t]+(\w+)/) {
160 0 ▶ return $line if ($1 eq 'e' xor $bot->{$2});
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "e" xor $bot->{$2} 0 0 0
Condition0%0/4
$1 eq "e" xor $bot->{$2}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$1 eq "e" xor $bot->{$2}
$1 eq "e" $bot->{$2}
Truth table0%0/4
A xor B
A= $1 eq "e"B= $bot->{$2}
ABresult
110
101
011
000
161 }
162 elsif ($line =~ /^print[\s\t]+/) {
163 0 $bot->cond($line);
164 0 print "\n";
165 }
166 else {
167 # command
168 0 ▶ my $times = ((int($1) eq $1) ? $1 : $bot->cond($1))
Branch: 2 branches
BranchTrueFalseTotal
if $line =~ s/\s*\*\s*(.+)$// 0 0 0
int $1 eq $1 ? : 0 0 0
169 if ($line =~ s/\s*\*\s*(.+)$//);
170 0 my @cmds;
171  
172 0 ▶ push @cmds, $line for (1..($times || 1));
Condition0%0/2
$times || 1
l!l
00
MC/DC0%0/1
$times || 1
$times
Truth table0%0/2
A
A= $times
Aresult
11
00
173 0 return @cmds;
174 }
175 }
176 }
177  
178 0 ▶sub endif {
Subroutine endif: not called CC=3 SCAR=24.8
Pod undocumented
179 0 my $bot = shift;
180 0 my $depth = 1;
181  
182 0 ▶ while ($bot->{'line'}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bot->{"line"}++) 0 0 0
183 0 $depth += substr($bot->{'condidx'}, $bot->{'line'} - 1, 1) - 1;
184 0 ▶ return unless $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless $depth 0 0 0
185 }
186  
187 0 die ("Unterminated condition block from ".$bot->{'state'});
188 }
189  
190 0 ▶sub elseif {
Subroutine elseif: not called CC=3 SCAR=24.8
Pod undocumented
191 0 my $bot = shift;
192 0 my $depth = 1;
193  
194 0 $bot->endif();
195 0 my $line = $bot->nextline();
196  
197 0 ▶ if ($line eq 'else {') {
Branch: 2 branches
BranchTrueFalseTotal
if ($line eq 'else {') { } 0 0 0
elsif ($line =~ /^(?:elsif[\s\t])(.+){/) { } 0 0 0
198 0 return 1;
199 }
200 elsif ($line =~ /^(?:elsif[\s\t])(.+){/) {
201 0 return $1;
202 }
203  
204 0 return;
205 }
206  
207 0 ▶sub gotostate {
Subroutine gotostate: not called CC=2 SCAR=17.9
Pod undocumented
208 0 my $bot = shift;
209 0 my $state = shift;
210  
211 # print "=>$state\n";
212  
213 defined($bot->{'line'} = $bot->{'stateidx'}{$state})
214 0 ▶ or die ($bot->{'name'}.": cannot goto state $state");
Branch: 1 branch
BranchTrueFalseTotal
defined($bot->{"line"} = $bot->{"stateidx"}{$state}) or die $bot->{"name"} . ": cannot goto state $state" 0 0 0
215  
216 0 $bot->{'state'} = $state;
217 }
218  
219 0 ▶sub nextline {
Subroutine nextline: not called CC=2 SCAR=17.9
Pod undocumented
220 0 my $bot = shift;
221 0 my $lineflag = substr($bot->{'lineidx'}, $bot->{'line'}, 1);
222 0 my $line = $bot->{'cmds'}[$bot->{'line'}++];
223 0 ▶ return $lineflag ? eval("\"$line\"") : $line;
Branch: 1 branch
BranchTrueFalseTotal
$lineflag ? : 0 0 0
224 }
225  
226  
227 # ===================
228 # Utility Subroutines
229 # ===================
230  
231 0 ▶sub _nearst {
Subroutine _nearst: not called CC=2 SCAR=17.9
232 0 my ($bot, $rel) = @_;
233  
234 0 ▶ return 99999 unless defined $bot->{"${rel}_x"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$bot{"${rel}_x"} 0 0 0
235 return abs($bot->{"${rel}_x"} - $bot->{'x'}) +
236 0 abs($bot->{"${rel}_y"} - $bot->{'y'});
237 }
238  
239 0 ▶sub _onnode {
Subroutine _onnode: not called CC=1 SCAR=6.9
240 0 my $bot = shift;
241  
242 0 return not $bot->_nearst('snode');
243 }
244  
245 0 ▶sub _inperim {
Subroutine _inperim: not called CC=4 SCAR=30.0
246 0 my ($bot, $rel) = @_;
247  
248 return ($bot->{"${rel}_x"} and $bot->{"${rel}_y"} and
249 abs($bot->{"${rel}_x"} - $bot->{'x'}) <= 1 and
250 0 ▶ abs($bot->{"${rel}_y"} - $bot->{'y'}) <= 1);
Condition0%0/9
$$bot{"${rel}_x"} && $$bot{"${rel}_y"} && abs $$bot{"${rel}_x"} - $bot->{'x'} <= 1 && abs $$bot{"${rel}_y"} - $bot->{'y'} <= 1
!ll&&!rl&&r
000
$$bot{"${rel}_x"} and $$bot{"${rel}_y"} and abs $$bot{"${rel}_x"} - $bot->{"x"} <= 1
!ll&&!rl&&r
000
$$bot{"${rel}_x"} and $$bot{"${rel}_y"}
!ll&&!rl&&r
000
MC/DC0%0/4
$$bot{"${rel}_x"} && $$bot{"${rel}_y"} && abs $$bot{"${rel}_x"} - $bot->{'x'} <= 1 && abs $$bot{"${rel}_y"} - $bot->{'y'} <= 1
$$bot{"${rel}_x"} $$bot{"${rel}_y"} abs $$bot{"${rel}_x"} - $bot->{"x"} <= 1 abs $$bot{"${rel}_y"} - $bot->{'y'} <= 1
Truth table0%0/5
A and B and C && D
A= $$bot{"${rel}_x"}B= $$bot{"${rel}_y"}C= abs $$bot{"${rel}_x"} - $bot->{"x"} <= 1D= abs $$bot{"${rel}_y"} - $bot->{'y'} <= 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
251 }
252  
253 0 ▶sub _distance {
Subroutine _distance: not called CC=1 SCAR=6.9
254 0 my ($bot, $x, $y) = @_;
255  
256 0 return abs($x - $bot->{'x'}) + abs($y - $bot->{'y'});
257 }
258  
259 0 ▶sub _ready {
Subroutine _ready: not called CC=1 SCAR=6.9
260 0 return Games::AIBots::bot_ready(@_);
261 }
262  
263 0 ▶sub _damaged {
Subroutine _damaged: not called CC=1 SCAR=6.9
264 0 my $bot = shift;
265 0 return 100 - int($bot->{'life'} / $bot->{'max_life'} * 100);
266 }
267  
268 0 ▶sub _turnto {
Subroutine _turnto: not called CC=6 SCAR=37.4
269 0 my ($bot, $head) = @_;
270 0 ▶ return if !$head or $bot->{'h'} eq $head;
Branch: 1 branch
BranchTrueFalseTotal
if not $head or $bot->{"h"} eq $head 0 0 0
Condition0%0/3
not $head or $bot->{"h"} eq $head
l!l&&r!l&&!r
000
MC/DC0%0/2
not $head or $bot->{"h"} eq $head
not $head $bot->{"h"} eq $head
Truth table0%0/3
not(A) or B
A= not $headB= $bot->{"h"} eq $head
ABresult
1X1
011
000
271  
272 0 my $delta = (index('8624', $bot->{'h'})
273 - index('8624', $head) + 4) % 4;
274  
275 0 ▶ return ('left') if $delta == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $delta == 1 0 0 0
276 0 ▶ return ('left * 2') if $delta == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $delta == 2 0 0 0
277 0 ▶ return ('right') if $delta == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $delta == 3 0 0 0
278 }
279  
280 0 ▶sub _headto {
Subroutine _headto: not called CC=4 SCAR=30.0
281 0 my ($bot, $rel) = @_;
282  
283 0 ▶ return unless defined $bot->{"${rel}_x"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$bot{"${rel}_x"} 0 0 0
284  
285 0 ▶ if ($bot->{"${rel}_x"} == $bot->{'x'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$bot{"${rel}_x"} == $bot->{'x'}) { } 0 0 0
elsif ($$bot{"${rel}_y"} == $bot->{'y'}) { } 0 0 0
286 0 return ('2', '8')[$bot->{"${rel}_y"} < $bot->{'y'}];
287 }
288 elsif ($bot->{"${rel}_y"} == $bot->{'y'}) {
289 0 return ('6', '4')[$bot->{"${rel}_x"} < $bot->{'x'}];
290 }
291 }
292  
293 0 ▶sub _toggle {
Subroutine _toggle: not called CC=1 SCAR=6.9
294 0 my $bot = shift;
295 $bot->{'var'}{'_'.$bot->{'state'}}
296 0 = !$bot->{'var'}{'_'.$bot->{'state'}};
297 0 return !$bot->{'var'}{'_'.$bot->{'state'}};
298 }
299  
300 0 ▶sub _found {
Subroutine _found: not called CC=3 SCAR=24.8
301 0 my $bot = shift;
302  
303 return (
304 @_ ? (index($_[0], '|') > -1)
305 ? $bot->{'found'} =~ /^(?:$_[0])/
306 : $bot->{'found'} eq $_[0]
307 0 ▶ : $bot->{'found'}
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
(index($_[0], '|') != -1) ? : 0 0 0
308 );
309 }
310  
311 0 ▶sub _bumped {
Subroutine _bumped: not called CC=3 SCAR=24.8
312 0 my $bot = shift;
313  
314 return (
315 @_ ? (index($_[0], '|') > -1)
316 ? $bot->{'bumped'} =~ /^(?:$_[0])/
317 : $bot->{'bumped'} eq $_[0]
318 0 ▶ : $bot->{'bumped'}
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
(index($_[0], '|') != -1) ? : 0 0 0
319 );
320 }
321  
322 1 1;
323  
324 =head1 SEE ALSO
325  
326 L<aibots>, L<Games::AIBots>
327  
328 =head1 AUTHORS
329  
330 Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>
331  
332 Files under the F<bots/> directory was contributed by students in
333 the autonomous learning experimnetal class, Bei'zheng junior high
334 school, Taipei, Taiwan.
335  
336 =head1 COPYRIGHT
337  
338 Copyright 2001, 2002 by Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>.
339  
340 This program is free software; you can redistribute it and/or
341 modify it under the same terms as Perl itself.
342  
343 See L<http://www.perl.com/perl/misc/Artistic.html>
344  
345 =cut
« AIBots.pm ↑ Summary