Mob.pm

filter: Statementstmt 17.5% 43 / 245 Branchbran 4.0% 5 / 124 Conditioncond 2.5% 1 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 32.0% 8 / 25 Podpod 89.4% 17 / 19 totaltotal 15.5% 74 / 476 CC 81 SCAR 58.9
CC
81
Cov
12%
autoex
756.0
movetoward
600.0
kbdmove
210.0
CRAP 363.1
↑ Summary Area.pm »
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Games::Roguelike::Mob;
4  
5 5 ▶use Games::Roguelike::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Games::Roguelike::Console;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Games::Roguelike::Area;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Games::Roguelike::Mob - Roguelike mobile object
14  
15 =head1 SYNOPSIS
16  
17 package myMob;
18 use base 'Games::Roguelike::Mob';
19  
20 $area = Games::Roguelike::Area->new();
21 $m = myMob->new($area, sym=>'D', x=>5,y=>6); # creates a mob at location 5, 6
22 # with symbol 'D', inside area $area
23  
24 $m->autoex() # moves the mob towards the nearest unexplored area
25 $m->kbdmove($c) # moves the mob according to keystroke '$c' using traditional roguelike semantics
26  
27 =head1 DESCRIPTION
28  
29 Mobile object used by drawing routines in Roguelke::Area
30  
31 =head2 METHODS
32  
33 =over 4
34  
35 =item new($area, %options)
36  
37 Area is an ::Area object, common options are:
38  
39 sym=>'@', # symbol to use when rendering
40 items=>[], # array ref of contained items
41 hasmem=>1, # whether the mob uses the "memory" feature
42 pov=>-1, # distance the mob can "see" (-1 = infinite, 0 = blind)
43 singleminded=>0, # whether the mob will "wander" when the movetoward function is called
44  
45 All "unknown" options are saved in the object's hash, with the assumption that they
46 will be used by the game, for example "->{MaxHp}", etc.
47  
48 =cut
49  
50 16 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
51 16 my $pkg = shift;
52 16 my $area = shift;
53 16 ▶ croak("can't create mob without area") unless $area && $area->isa('Games::Roguelike::Area');
Branch: 1 branch
BranchTrueFalseTotal
unless $area and $area->isa("Games::Roguelike::Area") 0 16 16
Condition33%1/3
$area and $area->isa("Games::Roguelike::Area")
!ll&&!rl&&r
0016
MC/DC0%0/2
$area and $area->isa("Games::Roguelike::Area")
$area $area->isa("Games::Roguelike::Area")
Truth table33%1/3
A and B
A= $areaB= $area->isa("Games::Roguelike::Area")
ABresult
0X0
100
111
54  
55 16 my $self = {};
56  
57 16 $self->{area} = $area;
58 16 $self->{sym}='@';
59 16 $self->{msglog} = [];
60 16 $self->{items} = [];
61 16 $self->{odir} = '';
62 16 $self->{hasmem} = 1;
63 16 $self->{pov} = -1;
64 16 $self->{singleminded} = 0; # whether it tries to wander around in pursuit of a goal
65  
66 16 ▶ while( my ($k, $v) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 58 16 74
67 58 $self->{$k} = $v;
68 }
69  
70 16 ▶ if (!defined($self->{x})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"x"}) 6 10 16
71 6 ($self->{x}, $self->{y}) = $area->findrandmap($area->{fsym}, 0, 1);
72 }
73  
74 16 bless $self, $pkg;
75 16 $area->addmob($self);
76 16 return $self;
77 }
78  
79 =item area([new])
80  
81 Either returns the current area (no arguments) or set the area (one argument).
82  
83 If an area is supplied, the old area has "delmob" called on it,and the new area has "addmob" called on it.
84  
85 =cut
86  
87 0 ▶sub area {
Subroutine area: not called CC=3 SCAR=24.8
Pod documented
88 0 my $self = shift;
89 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
90 0 $self->{area}->delmob($self);
91 0 $self->{area} = $_[0];
92 0 $_[0]->addmob($self);
93 0 ▶ if ($self->{area}->{world}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"area"}{"world"}) 0 0 0
94 0 $self->{area}->{world}->area($self->{area});
95 }
96 }
97 0 return $self->{area};
98 }
99  
100 =item x()
101  
102 =item y()
103  
104 Returns the location of the mob
105  
106 =cut
107  
108 0 ▶sub x {
Subroutine x: not called CC=1 SCAR=6.9
Pod documented
109 0 return $_[0]->{x};
110 }
111  
112 0 ▶sub y {
Subroutine y: not called CC=1 SCAR=6.9
Pod documented
113 0 return $_[0]->{y};
114 }
115  
116 =item on()
117  
118 Returns the map symbol from the current area at the mob's current x, y location.
119  
120 =cut
121  
122 0 ▶sub on {
Subroutine on: not called CC=1 SCAR=6.9
Pod documented
123 0 my $self = shift;
124 0 return $self->{area}->{map}->[$self->{x}][$self->{y}];
125 }
126  
127 =item movetofeature(@ARGS)
128  
129 Calls the "findfeature" function on the current area with the @ARGS, and, if one is returned, reposition with x/y coordinates to match.
130  
131 Checkmove/aftermove are not called.
132  
133 =cut
134  
135 0 ▶sub movetofeature {
Subroutine movetofeature: not called CC=2 SCAR=17.9
Pod documented
136 0 my $self = shift;
137 0 my ($cx,$cy) = $self->{area}->findfeature(@_);
138 0 ▶ if (defined($cx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cx) { } 0 0 0
139 0 $self->{x} = $cx;
140 0 $self->{y} = $cy;
141 0 return 1;
142 } else {
143 0 return 0;
144 }
145 }
146  
147 5 my %DIAGS = (
148 'nw'=>['n','w'],
149 'ne'=>['n','e'],
150 'sw'=>['s','w'],
151 'se'=>['s','e'],
152 'n',=>['nw','ne'],
153 's',=>['sw','se'],
154 'e',=>['ne','ne'],
155 'w',=>['sw','nw'],
156 );
157  
158 =item movetoward($x, $y, $error)
159  
160 Moves the mob toward the point specified. If error is specified, the destination point is "blurred" by the error radius.
161  
162 =cut
163  
164 0 ▶sub movetoward {
Subroutine movetoward: not called CC=24 SCAR=64.0
Pod documented
165 0 my $self = shift;
166 0 my ($x, $y, $err) = @_;
167 0 my ($dx, $dy) = ($x - $self->{x}, $y - $self->{y});
168
169 0 ▶ if ($err > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err > 0) 0 0 0
170 0 $dx += (randi($err*2+1)- $err);
171 0 $dy += (randi($err*2+1)- $err);
172 0 intify($dx, $dy);
173 }
174  
175 0 ▶ return 0 if $dx == 0 && $dy == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dx == 0 and $dy == 0 0 0 0
Condition0%0/3
$dx == 0 and $dy == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$dx == 0 and $dy == 0
$dx == 0 $dy == 0
Truth table0%0/3
A and B
A= $dx == 0B= $dy == 0
ABresult
0X0
100
111
176 0 my $d;
177  
178 0 ▶ if ($dy > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dy > 0) { } 0 0 0
elsif ($dy < 0) { } 0 0 0
179 0 $d = 's'
180 } elsif ($dy < 0) {
181 0 $d = 'n'
182 }
183  
184 0 ▶ if ($dx > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dx > 0) { } 0 0 0
elsif ($dx < 0) { } 0 0 0
185 0 $d .= 'e'
186 } elsif ($dx < 0) {
187 0 $d .= 'w'
188 }
189  
190 # nonzero means move happened
191 0 my $ok = $self->move($d);
192 0 ▶ return $ok if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
193  
194 # try moving orthoganally again sometimes... to range farther
195 0 ▶ if (($self->{singleminded}==0) && rand(2) > 1 && $self->{odir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"singleminded"} == 0 and rand 2 > 1 and $self->{"odir"}) 0 0 0
Condition0%0/6
$self->{"singleminded"} == 0 and rand 2 > 1 and $self->{"odir"}
!ll&&!rl&&r
000
$self->{"singleminded"} == 0 and rand 2 > 1
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"singleminded"} == 0 and rand 2 > 1 and $self->{"odir"}
$self->{"singleminded"} == 0 rand 2 > 1 $self->{"odir"}
Truth table0%0/4
A and B and C
A= $self->{"singleminded"} == 0B= rand 2 > 1C= $self->{"odir"}
ABCresult
0XX0
10X0
1100
1111
196 0 $ok = $self->move($self->{odir});
197 0 ▶ $self->{area}->dprint("ortho repeat of $self->{odir}") if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
198 0 ▶ return $ok if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
199 }
200  
201 # try moving diags of move
202 0 my @d;
203
204 0 ▶ if (abs($dy) > abs($dx)) {
Branch: 2 branches
BranchTrueFalseTotal
if (abs $dy > abs $dx) { } 0 0 0
elsif (abs $dy < abs $dx) { } 0 0 0
205 0 @d = @{$DIAGS{$d}};
206 } elsif (abs($dy) < abs($dx)) {
207 0 @d = ($DIAGS{$d}->[1],$DIAGS{$d}->[0]);
208 } else {
209 0 @d = randsort(@{$DIAGS{$d}});
210 }
211
212 0 for (@d) {
213 0 $ok = $self->move($_);
214 0 ▶ $self->{odir} = '' if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
215 0 ▶ return $ok if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
216 }
217  
218 0 ▶ return 0 if $self->{singleminded} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"singleminded"} > 1 0 0 0
219  
220 # try moving orthoganally to the way you want to go
221 0 for (randsort(orthogs($d))) {
222 0 $ok = $self->move($_);
223 0 ▶ $self->{area}->dprint("moved orthog $_") if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
224 0 ▶ $self->{odir} = $_ if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
225 0 ▶ return $ok if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
226 }
227  
228 0 return 0;
229 }
230  
231 5 my %ORTHOGS = (
232 'n'=>['e','w','ne','nw'],
233 's'=>['e','w','se','sw'],
234 'e'=>['n','s','ne','se'],
235 'w'=>['n','s','nw','sw'],
236 'ne'=>['nw','se'],
237 'nw'=>['ne','sw'],
238 'se'=>['ne','sw'],
239 'sw'=>['nw','se'],
240 );
241  
242 0 ▶sub orthogs {
Subroutine orthogs: not called CC=1 SCAR=6.9
Pod undocumented
243 0 my ($d) = @_;
244 0 return @{$ORTHOGS{$d}};
245 }
246  
247 =item kbdmove($c[, $testonly])
248  
249 Moves the mob in direction '$c': 'h' is LEFT, 'l' is RIGHT, etc.
250  
251 The testonly flag is passed to the "move" function.
252  
253 =cut
254  
255 0 ▶sub kbdmove {
Subroutine kbdmove: not called CC=14 SCAR=53.5
Pod documented
256 0 my $self = shift;
257 0 my ($c, $testonly) = @_;
258 0 ▶ if ($c eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ".") 0 0 0
259 0 return $self->move('.', $testonly);
260 }
261 0 ▶ if ($c eq 'h' || $c eq 'LEFT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "h" or $c eq "LEFT") 0 0 0
Condition0%0/3
$c eq "h" or $c eq "LEFT"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "h" or $c eq "LEFT"
$c eq "h" $c eq "LEFT"
Truth table0%0/3
A or B
A= $c eq "h"B= $c eq "LEFT"
ABresult
1X1
011
000
262 0 return $self->move('w', $testonly);
263 }
264 0 ▶ if ($c eq 'l' || $c eq 'RIGHT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "l" or $c eq "RIGHT") 0 0 0
Condition0%0/3
$c eq "l" or $c eq "RIGHT"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "l" or $c eq "RIGHT"
$c eq "l" $c eq "RIGHT"
Truth table0%0/3
A or B
A= $c eq "l"B= $c eq "RIGHT"
ABresult
1X1
011
000
265 0 return $self->move('e', $testonly);
266 }
267 0 ▶ if ($c eq 'j' || $c eq 'DOWN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "j" or $c eq "DOWN") 0 0 0
Condition0%0/3
$c eq "j" or $c eq "DOWN"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "j" or $c eq "DOWN"
$c eq "j" $c eq "DOWN"
Truth table0%0/3
A or B
A= $c eq "j"B= $c eq "DOWN"
ABresult
1X1
011
000
268 0 return $self->move('s', $testonly);
269 }
270 0 ▶ if ($c eq 'k' || $c eq 'UP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "k" or $c eq "UP") 0 0 0
Condition0%0/3
$c eq "k" or $c eq "UP"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "k" or $c eq "UP"
$c eq "k" $c eq "UP"
Truth table0%0/3
A or B
A= $c eq "k"B= $c eq "UP"
ABresult
1X1
011
000
271 0 return $self->move('n', $testonly);
272 }
273 0 ▶ if ($c eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "y") 0 0 0
274 0 return $self->move('nw', $testonly);
275 }
276 0 ▶ if ($c eq 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "b") 0 0 0
277 0 return $self->move('sw', $testonly);
278 }
279 0 ▶ if ($c eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "u") 0 0 0
280 0 return $self->move('ne', $testonly);
281 }
282 0 ▶ if ($c eq 'n') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "n") 0 0 0
283 0 return $self->move('se', $testonly);
284 }
285 0 return 0;
286 }
287  
288 =item safetomove()
289  
290 Returns true if it's safe to continue autoexploring.
291  
292 Default behavior is to return false if any mobs are in view.
293  
294 =cut
295  
296 0 ▶sub safetomove {
Subroutine safetomove: not called CC=4 SCAR=30.0
Pod documented
297 0 my $self = shift;
298 0 my $area = $self->{area};
299 0 for my $m (@{$area->{mobs}}) {
300 0 ▶ next if $m eq $self;
Branch: 1 branch
BranchTrueFalseTotal
if $m eq $self 0 0 0
301 0 ▶ if ($area->checkpov($self, $m->{x}, $m->{y})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($area->checkpov($self, $m->{"x"}, $m->{"y"})) 0 0 0
302 0 return 0;
303 }
304 }
305 0 return 1;
306 }
307  
308 =item autoex ([bool only1])
309  
310 Find closest unexplored square and move towards it until it's no longer unexplored.
311  
312 If world is specified, this loops and draws the map. Otherwise, it moves only 1 step.
313  
314 =cut
315  
316 0 ▶sub autoex {
Subroutine autoex: not called CC=27 SCAR=66.3
Pod documented
317 0 my $self = shift;
318 0 my ($world) = @_;
319  
320 # flood fill find unexplored area
321 0 my $area = $self->{area};
322 0 my ($x1, $y1) = ($self->{x}, $self->{y});
323 0 my $f;
324  
325 my @f;
326 0 push @f, [$x1, $y1, []];
327 0 my @bread;
328 0 my ($cx, $cy) = ($x1, $y1);
329 0 my $minlen = 1000000;
330  
331 0 ▶ if (!$self->safetomove()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->safetomove) 0 0 0
332 0 return 0;
333 }
334  
335 0 ▶ if (!$world && $self->{autopath} && @{$self->{autopath}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $world and $self->{"autopath"} and @{$self->{"autopath"};}) 0 0 0
Condition0%0/6
not $world and $self->{"autopath"} and @{$self->{"autopath"};}
!ll&&!rl&&r
000
not $world and $self->{"autopath"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $world and $self->{"autopath"} and @{$self->{"autopath"};}
not $world $self->{"autopath"} @{$self->{"autopath"};}
Truth table0%0/4
not(A) and B and C
A= not $worldB= $self->{"autopath"}C= @{$self->{"autopath"};}
ABCresult
0XX0
10X0
1100
1111
336 0 my $moved = $self->move(shift(@{$self->{autopath}}));
337 0 ▶ if ($self->{memory}->{$self->{area}->{name}}->[$self->{autocx}][$self->{autocy}]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memory"}{$self->{"area"}{"name"}}[$self->{"autocx"}][$self->{"autocy"}]) 0 0 0
338 0 $self->{autopath} = undef;
339 }
340 0 return $moved;
341 }
342
343 0 my $path; # path to take
344 0 ▶ while (@f) {
Branch: 1 branch
BranchTrueFalseTotal
if (@f) 0 0 0
345 0 my $c = shift @f; # breadth first
346 0 ▶ for (my $d=0;$d<8;++$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 8) 0 0 0
347 0 ▶ next unless $self->{memory}->{$self->{area}->{name}}->[$c->[0]][$c->[1]]; # has to be "moving from" a place we have seen
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"memory"}{$self->{"area"}{"name"}}[$c->[0]][$c->[1]] 0 0 0
348  
349 0 my $tx = $DD[$d]->[0]+$c->[0];
350 0 my $ty = $DD[$d]->[1]+$c->[1];
351 0 my $p = [@{$c->[2]}, $DIRS[$d]];
352  
353 # not off edge
354 0 ▶ next if $tx < 0 || $ty < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tx < 0 or $ty < 0 0 0 0
Condition0%0/3
$tx < 0 or $ty < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$tx < 0 or $ty < 0
$tx < 0 $ty < 0
Truth table0%0/3
A or B
A= $tx < 0B= $ty < 0
ABresult
1X1
011
000
355 0 ▶ next if $tx >= $area->{w} || $ty >= $area->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $tx >= $area->{"w"} or $ty >= $area->{"h"} 0 0 0
Condition0%0/3
$tx >= $area->{"w"} or $ty >= $area->{"h"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$tx >= $area->{"w"} or $ty >= $area->{"h"}
$tx >= $area->{"w"} $ty >= $area->{"h"}
Truth table0%0/3
A or B
A= $tx >= $area->{"w"}B= $ty >= $area->{"h"}
ABresult
1X1
011
000
356  
357 0 ▶ next if $bread[$tx][$ty];
Branch: 1 branch
BranchTrueFalseTotal
if $bread[$tx][$ty] 0 0 0
358 0 $bread[$tx][$ty]='.'; #been there in this algorithm
359  
360 0 my $seen = $self->{memory}->{$self->{area}->{name}}->[$tx][$ty];
361  
362 0 ▶ if (!$seen) { # not explored already;
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen) 0 0 0
363 0 $path = $p;
364 0 $cx = $tx;
365 0 $cy = $ty;
366 0 @f = ();
367 0 last;
368 }
369  
370 # not thru void
371 0 ▶ next if !defined($area->{map}->[$tx][$ty]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $area->{"map"}[$tx][$ty] 0 0 0
372 0 ▶ next if $area->{map}->[$tx][$ty] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $area->{"map"}[$tx][$ty] eq "" 0 0 0
373  
374 # not thru wall
375 0 ▶ next if index($area->{nomove}, $area->{map}->[$tx][$ty]) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($area->{"nomove"}, $area->{"map"}[$tx][$ty]) != -1 0 0 0
376
377 0 push @f, [$tx, $ty, $p]; #add to list of places can get to;
378 }
379 }
380  
381 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
382 0 ▶ if (!$world) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $world) { } 0 0 0
383 0 my $moved = $self->move(shift(@{$path}));
384 0 ▶ if (!($self->{memory}->{$self->{area}->{name}}->[$cx][$cy])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"memory"}{$self->{"area"}{"name"}}[$cx][$cy]) 0 0 0
385 0 $self->{autopath} = $path;
386 0 $self->{autocx} = $cx;
387 0 $self->{autocy} = $cy;
388 }
389 0 return $moved;
390 } else {
391 0 my $con = $world->{con};
392 0 my $stm=1;
393 0 for (@$path) {
394 0 my $bc = $con->nbgetch();
395 0 ▶ if ($bc eq 'q' || $bc eq 'ESC') {
Branch: 1 branch
BranchTrueFalseTotal
if ($bc eq "q" or $bc eq "ESC") 0 0 0
Condition0%0/3
$bc eq "q" or $bc eq "ESC"
l!l&&r!l&&!r
000
MC/DC0%0/2
$bc eq "q" or $bc eq "ESC"
$bc eq "q" $bc eq "ESC"
Truth table0%0/3
A or B
A= $bc eq "q"B= $bc eq "ESC"
ABresult
1X1
011
000
396 0 $stm = 0;
397 0 last;
398 }
399 0 my $moved = $self->move($_);
400 0 $stm = $self->safetomove();
401 0 ▶ last if !$stm;
Branch: 1 branch
BranchTrueFalseTotal
unless $stm 0 0 0
402 # explored the one we were looking for...remove this to reduce recusion at the expense of wasted moves
403 0 ▶ if (($self->{memory}->{$self->{area}->{name}}->[$cx][$cy])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"memory"}{$self->{"area"}{"name"}}[$cx][$cy]) 0 0 0
404 0 last;
405 }
406 }
407 0 ▶ if (!$stm) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $stm) { } 0 0 0
408 0 return 1;
409 } else {
410 0 $world->drawmap();
411 0 $self->autoex(@_);
412 }
413 }
414 }
415 }
416  
417  
418 =item move (direction[, testonly])
419  
420 Uses checkmove to see whether the direction is ok. If it returns > 0, then moves the mob,
421 changing its x,y position, and saving the move in "lastmove".
422  
423 Aftermove is then called if the return value of checkmove was nonzero.
424  
425 =item lastmove
426  
427 Returns the direction parameter passed to "move" that resulted in a successful move.
428  
429 =cut
430  
431 0 ▶sub move {
Subroutine move: not called CC=4 SCAR=30.0
Pod documented
432 0 my $self = shift;
433 0 my ($d, $testonly) = @_; # news direction
434 0 my $nx = $self->{x} + $DD{$d}->[0];
435 0 my $ny = $self->{y} + $DD{$d}->[1];
436 0 my $r;
437 0 $r = $self->checkmove($nx, $ny, scalar $self->{area}->mobat($nx, $ny), $testonly);
438 # less than eq zero means remain still (but move may have occurred)
439 0 ▶ if (!$testonly) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($testonly) 0 0 0
440 0 ▶ if ($r > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r > 0) { } 0 0 0
elsif ($r < 0) { } 0 0 0
441 0 $self->{area}->dprint("moved $d");
442 0 $self->{x} = $nx;
443 0 $self->{y} = $ny;
444 0 $self->{lastmove} = $d;
445 0 $self->aftermove($d);
446 } elsif ($r < 0) {
447 0 $self->aftermove(undef);
448 }
449 }
450 0 return $r;
451 }
452  
453 0 ▶sub getmovexy {
Subroutine getmovexy: not called CC=1 SCAR=6.9
Pod undocumented
454 0 my $self = shift;
455 0 my ($d, $flag) = @_; # news direction
456 0 my $nx = $self->{x} + $DD{$d}->[0];
457 0 my $ny = $self->{y} + $DD{$d}->[1];
458 0 return ($nx, $ny);
459 }
460  
461 0 ▶sub lastmove {
Subroutine lastmove: not called CC=1 SCAR=6.9
Pod documented
462 0 return $_[0]->{lastmove};
463 }
464  
465 =item aftermove (direction)
466  
467 Called after the mob moved with the direction it moved.
468  
469 If the mob attacks or otherwise moves "nowhere" it is called with 'undef' as the direction.
470  
471 =cut
472  
473 0 ▶sub aftermove {
Subroutine aftermove: not called CC=1 SCAR=0.0
Pod documented
474 }
475  
476 =item checkmove (new-x, new-y, othermob (at location x/y), testonly)
477  
478 Called before the mob moves with the direction it will move if allowed.
479  
480 Return value 0 = no move occurs
481 Return value 1 = move occurs
482 Return value -1 = attack/move occured, but keep in the same place
483  
484 =cut
485  
486 0 ▶sub checkmove {
Subroutine checkmove: not called CC=3 SCAR=24.8
Pod documented
487 0 my $self = shift;
488 0 my ($x, $y, $othermob, $testonly) = @_;
489 0 ▶ return 0 unless $self->{area}->{map}->[$x][$y] eq $self->{area}->{fsym};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"area"}{"map"}[$x][$y] eq $self->{"area"}{"fsym"} 0 0 0
490 0 ▶ return 0 unless !$othermob;
Branch: 1 branch
BranchTrueFalseTotal
if $othermob 0 0 0
491 0 return 1;
492 }
493  
494 =item additem (item)
495  
496 Adds item to inventory. Override this to add pack full messages, etc.
497  
498 Return value 0 = can't add, too full
499 Return value 1 = add ok
500 Return value -1 = move occured, but not added
501  
502 =cut
503  
504 2 ▶sub additem {
Subroutine additem: called CC=1 SCAR=0.0
Pod documented
505 2 my $self = shift;
506 2 my $item = shift;
507 # i'm never full
508 2 return $item->setcont($self);
509 }
510  
511 =item delitem (item)
512  
513 Removes item from the mob.
514  
515 =cut
516  
517 0 ▶sub delitem {
Subroutine delitem: not called CC=4 SCAR=30.0
Pod documented
518 0 my $self = shift;
519 0 my $ob = shift;
520 0 ▶ confess("not a mob") unless $self->isa('Games::Roguelike::Mob');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("Games::Roguelike::Mob") 0 0 0
521 0 $self->{area}->dprint("h1");
522 0 my $i = 0;
523 0 for (@{$self->{items}}) {
524 0 ▶ if ($_ == $ob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ == $ob) 0 0 0
525 0 splice @{$self->{items}}, $i, 1;
526 0 return $_;;
527 }
528 0 ++$i;
529 }
530 0 return undef;
531 }
532  
533 =item dropitem (item)
534  
535 Removes item, changes it's coordinates, and then tries to put it in the "area".
536  
537 Returns the result of the "additem" from the area object (which may be a failure).
538  
539 =cut
540  
541 0 ▶sub dropitem {
Subroutine dropitem: not called CC=1 SCAR=6.9
Pod documented
542 0 my $self = shift;
543 0 my $item = shift;
544 0 $item->{x} = $self->{x};
545 0 $item->{y} = $self->{y};
546 0 $self->{area}->additem($item);
547 0 return 1;
548 }
549  
550 =back
551  
552 =head1 AUTHOR
553  
554 Erik Aronesty C<earonesty@cpan.org>
555  
556 =head1 LICENSE
557  
558 This program is free software; you can redistribute it and/or
559 modify it under the same terms as Perl itself.
560  
561 See L<http://www.perl.com/perl/misc/Artistic.html> or the included LICENSE file.
562  
563 =cut
564  
565 5 1;
↑ Summary Area.pm »