Area.pm

filter: Statementstmt 59.8% 512 / 856 Branchbran 43.3% 214 / 494 Conditioncond 41.8% 93 / 222 MC/DCmcdc 24.2% 31 / 128 Subroutinesub 62.8% 44 / 70 Podpod 63.2% 31 / 49 totaltotal 50.8% 925 / 1819 CC 349 SCAR 51.4
CC
349
Cov
52%
genroom
1406.0
genmaze3
386.6
dump
182.0
CRAP 170.3
« Mob.pm ↑ Summary Area/Wilderness.pm »
1 package Games::Roguelike::Area;
2  
3 # puposely don't use much of the curses windowing stuff since curses doesn't port well
4 # purpose of library:
5 #
6 # keep track of map/location
7 # convenience for collision, line of sight, path-finding
8 # assume some roguelike concepts (mobs/items)
9 # allow me to make 7-day rl's in 7-days
10  
11 =head1 NAME
12  
13 Games::Roguelike::Area - Roguelike area map
14  
15 =head1 SYNOPSIS
16  
17 package myArea;
18 use base 'Games::Roguelike::Area';
19  
20 $a = myArea->new(w=>80,h=>50); # creates an area with specified width/height
21 $a->generate('cavelike'); # make a cavelike maze
22 $char = Games::Roguelike::Mob->new($a, sym=>'@'); # add a mobile object with symbol '@'
23  
24 =head1 DESCRIPTION
25  
26 Library for loading or generating mazes, managing items/mobs
27  
28 * assumes the user will be using overridden Games::Roguelike::Mob's as characters in the game
29 * provides a flexible load() function
30 * contains an array of 'features', which can be named, searched for and positioned
31  
32 =head2 METHODS
33  
34 =over 4
35  
36 =cut
37  
38 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 ▶use Games::Roguelike::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 5 ▶use Games::Roguelike::Mob;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶use Carp qw(croak confess carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 5 our $OKINLINEPOV;
46 5 our $AUTOLOAD;
47  
48 5 our $VERSION = '0.5.' . [qw$Revision: 259 $]->[1];
49  
50 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.6
51 0 ▶ eval('use Games::Roguelike::Utils::Pov_C;');
Subroutine BEGIN: called CC=1 SCAR=1.2
52 5 $OKINLINEPOV = !$@;
53 }
54  
55 =item new(OPT1=>VAL1, OPT2=>VAL2...)
56
57 Options can also all be set/get as class accessors:
58  
59 world => undef, # world this area belongs to (optional container which can "addarea")
60 name => '', # name of this level/area (required if world is specified)
61 map => [] # double-indexed array of map symbols
62 color => [] # double-indexed array of strings (used to color map symbols)
63 mobs => [], # list of mobs
64 items => [], # list of items
65  
66 # These will default to the world defaults, if world is set
67  
68 w=>80, h=>40, # width/height of this area
69 debugmap => 0, # turn on map coordinate display
70 memcolor => 'gray', # color drawn when an area is not in sight
71  
72 # These vars, or the world defaults (if a world is defined)
73 # are used by map-making, pathfinding and field-of view, rather than using hooks
74 # specifically because function calling seems to slow things down
75  
76 wsym => '#', # default wall symbol
77 fsym => '.', # default floor symbol
78 dsym => '+', # default door symbol
79 noview => '#+', # list of symbols that block view
80 nomove => '#', # list of symbols that block movement
81
82 =cut
83  
84 16 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
85 16 my $pkg = shift;
86 16 ▶ croak "usage: Games::Roguelike::Area->new()" unless $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 16 16
87  
88 16 my $self = bless {}, $pkg;
89 16 $self->init(@_);
90 16 return $self;
91 }
92  
93 16 ▶sub init {
Subroutine init: called CC=11 SCAR=27.6
Pod undocumented
94 16 my $self = shift;
95 16 my %opts = @_;
96  
97 16 ▶ croak("need to specify a name for this area") if $opts{world} && !$opts{name};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"world"} and not $opts{"name"} 0 16 16
Condition33%1/3
$opts{"world"} and not $opts{"name"}
!ll&&!rl&&r
1600
MC/DC0%0/2
$opts{"world"} and not $opts{"name"}
$opts{"world"} not $opts{"name"}
Truth table33%1/3
A and not(B)
A= $opts{"world"}B= not $opts{"name"}
ABresult
0X0
100
111
98  
99 # set defaults
100  
101 16 $self->{map} = [];
102 16 $self->{color} = [];
103 16 $self->{mobs} = [];
104 16 $self->{items} = [];
105  
106 16 ▶ if (!$opts{world}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'world'}) { } 16 0 16
107 16 $self->{h} = 40;
108 16 $self->{w} = 80;
109 16 $self->{wsym} = '#';
110 16 $self->{fsym} = '.';
111 16 $self->{dsym} = '+';
112 16 $self->{debugmap} = 0;
113 } else {
114 # really, should create functions for each one which reads from world member
115 0 for (qw(h w noview nomove wsym fsym dsym debugmap memcolor)) {
116 0 $self->{$_} = $opts{world}->$_;
117 }
118 }
119  
120 # override defaults
121 16 for (keys(%opts)) {
122 16 $self->{$_} = $opts{$_};
123 }
124
125 16 ▶ $self->{memcolor} = 'gray' unless defined $self->{memcolor};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"memcolor"} 16 0 16
126 16 ▶ $self->{nomove} = $self->{wsym} unless $self->{nomove};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"nomove"} 16 0 16
127 16 ▶ $self->{noview} = $self->{wsym}.$self->{dsym} unless $self->{noview};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"noview"} 16 0 16
128 16 ▶ if ($self->{world}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"world"}) 0 16 16
129 0 $self->{world}->addarea($self);
130 0 ▶ $self->{world}->area($self) if !$self->{world}->area;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"world"}->area 0 0 0
131 }
132 }
133  
134 0 ▶sub setworld {
Subroutine setworld: not called CC=6 SCAR=37.4
Pod undocumented
135 0 my $self = shift;
136 0 my $world = shift;
137  
138 0 ▶ croak("need to specify a name for this area") if !$self->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"name"} 0 0 0
139  
140 0 ▶ if ($self->{world} != $world) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"world"} != $world) 0 0 0
141 0 ▶ $self->{world}->delarea($self) if ($self->{world});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"world"} 0 0 0
142 0 $self->{world} = $world;
143 0 $world->addarea($self);
144 0 for (qw(h w noview nomove wsym fsym dsym debugmap)) {
145 0 ▶ $self->{$_} = $self->{world}->$_ if !defined($self->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$_} 0 0 0
146 }
147 }
148 }
149  
150 # perl accessors are slow compared to just accessing the hash directly
151 # autoload is even slower
152 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=6 SCAR=37.4
153 0 my $self = shift;
154 0 ▶ my $pkg = ref($self) or croak "$self is not an object";
Branch: 1 branch
BranchTrueFalseTotal
my $pkg = ref $self or croak("$self is not an object") 0 0 0
155  
156 0 my $name = $AUTOLOAD;
157 0 $name =~ s/.*://; # strip fully-qualified portion
158  
159 0 ▶ $name =~ s/^set// if @_ && !exists $self->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if @_ and not exists $self->{$name} 0 0 0
Condition0%0/3
@_ and not exists $self->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and not exists $self->{$name}
@_ not exists $self->{$name}
Truth table0%0/3
A and not(B)
A= @_B= not exists $self->{$name}
ABresult
0X0
100
111
160  
161 0 ▶ unless (exists $self->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$name}) 0 0 0
162 0 croak "Can't access `$name' field in class $pkg";
163 }
164  
165 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
166 0 return $self->{$name} = $_[0];
167 } else {
168 0 return $self->{$name};
169 }
170 }
171  
172 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
173 }
174  
175 =item getmapsym ($x,$y)
176  
177 =item getmapcolor ($x,$y)
178  
179 =item setmap ($x,$y, $sym [,$color])
180  
181 Raw accessors may be subject to change in future versions, but are much faster.
182  
183 $area->{map}[$x][$y] = $sym,
184 $area->{color}[$x][$y] = $color
185  
186 Function accessors will always work, but are slower:
187  
188 $sym=$area->getmapsym($x,$y)
189 $color=$area->getmapcolor($x,$y)
190 $area->setmap($x,$y, $sym [,$color]);
191  
192 If you're looping over the whole map, you can use foreachmap().
193  
194 etcetera.
195  
196 =cut
197  
198 0 ▶sub getmapsym {
Subroutine getmapsym: not called CC=1 SCAR=6.9
Pod documented
199 0 my $self = shift;
200 0 my ($x, $y) = @_;
201 0 return $self->{map}->[$x][$y];
202 }
203  
204 0 ▶sub setmapsym {
Subroutine setmapsym: not called CC=1 SCAR=6.9
Pod undocumented
205 0 my $self = shift;
206 0 my ($x, $y, $sym) = @_;
207 0 $self->{map}->[$x][$y] = $sym;
208 }
209  
210 0 ▶sub setmap {
Subroutine setmap: not called CC=4 SCAR=30.0
Pod documented
211 0 my $self = shift;
212 0 my ($x, $y, $sym, $color) = @_;
213 0 ▶ if (ref($sym)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sym) { } 0 0 0
214 0 $self->{map}->[$x][$y] = $sym->{sym};
215 0 ▶ $self->{color}->[$x][$y] = $sym->{color} if defined $sym->{color};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sym->{"color"} 0 0 0
216 } else {
217 0 $self->{map}->[$x][$y] = $sym;
218 0 ▶ $self->{color}->[$x][$y] = $color if defined $color;
Branch: 1 branch
BranchTrueFalseTotal
if defined $color 0 0 0
219 }
220 }
221  
222 0 ▶sub mapsym {
Subroutine mapsym: not called CC=2 SCAR=17.9
Pod undocumented
223 0 my $self = shift;
224 0 my ($x, $y, $s) = @_;
225 0 ▶ if ($s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s) { } 0 0 0
226 0 return $self->{map}->[$x][$y] = $s;
227 } else {
228 0 return $self->{map}->[$x][$y];
229 }
230 }
231  
232 0 ▶sub mapcolor {
Subroutine mapcolor: not called CC=2 SCAR=17.9
Pod undocumented
233 0 my $self = shift;
234 0 my ($x, $y, $c) = @_;
235 0 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 0 0 0
236 0 return $self->{color}->[$x][$y] = $c;
237 } else {
238 0 return $self->{color}->[$x][$y];
239 }
240 }
241  
242 1 ▶sub map {
Subroutine map: called CC=2 SCAR=7.4
Pod undocumented
243 1 my $self = shift;
244 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 0 1
245 1 my ($x, $y) = (shift, shift);
246 1 return $self->{map}->[$x][$y];
247 } else {
248 0 return $self->{map};
249 }
250 }
251  
252 =item rpoint ()
253  
254 Returns a random point as an X/Y array, that's within the map, and not on the edge..
255  
256 =cut
257  
258 32 ▶sub rpoint {
Subroutine rpoint: called CC=2 SCAR=7.1
Pod documented
259 32 my $self = shift;
260 32 ▶ croak unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 32 32
261 32 return (1+int(rand()*($self->{w}-2)), 1+int(rand()*($self->{h}-2)));
262 }
263  
264 =item rpoint_empty ()
265  
266 Returns a random point that's empty (eiher the symbol is ''' or is undefined)
267  
268 =cut
269  
270 0 ▶sub rpoint_empty {
Subroutine rpoint_empty: not called CC=3 SCAR=24.8
Pod documented
271 0 my $self = shift;
272 0 ▶ croak unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
273 # well, there's better ways to do this, but this has worked so far
274 0 while (1) {
275 0 my ($x, $y) = (1+int(rand()*($self->{w}-2)), 1+int(rand()*($self->{h}-2)));
276 0 ▶ return ($x, $y) if $self->{map}->[$x][$y] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"map"}[$x][$y] eq "" 0 0 0
277 }
278 }
279  
280 ############ part of genmaze1 ############
281  
282 0 ▶sub genroom {
Subroutine genroom: not called CC=37 SCAR=72.5
Pod undocumented
283 # make a room around x/y with options
284 # options are: minx, miny, maxx, maxy, nooverlap, withdoor
285
286 0 my $self = shift;
287 0 my ($x, $y, %opts) = @_;
288
289 0 ▶ if ($x =~ /^[a-z]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ /^[a-z]/i) 0 0 0
290 0 %opts = @_;
291 0 $x = $opts{x};
292 0 $y = $opts{y};
293 0 ▶ if (! defined $x) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $x) 0 0 0
294 0 ($x, $y) = $self->rpoint_empty();
295 }
296 }
297
298 0 my $m = $self->{map};
299  
300 # room width/height
301 0 my $rw = max(1,2+($self->{w}/2-2)*rand()*rand());
302 0 my $rh = max(1,2+($self->{h}/2-2)*rand()*rand());
303  
304 0 ▶ $rw=$opts{minw} if $opts{minw} && $rw < $opts{minw};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"minw"} and $rw < $opts{"minw"} 0 0 0
Condition0%0/3
$opts{"minw"} and $rw < $opts{"minw"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"minw"} and $rw < $opts{"minw"}
$opts{"minw"} $rw < $opts{"minw"}
Truth table0%0/3
A and B
A= $opts{"minw"}B= $rw < $opts{"minw"}
ABresult
0X0
100
111
305 0 ▶ $rh=$opts{minh} if $opts{minh} && $rh < $opts{minh};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"minh"} and $rh < $opts{"minh"} 0 0 0
Condition0%0/3
$opts{"minh"} and $rh < $opts{"minh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"minh"} and $rh < $opts{"minh"}
$opts{"minh"} $rh < $opts{"minh"}
Truth table0%0/3
A and B
A= $opts{"minh"}B= $rh < $opts{"minh"}
ABresult
0X0
100
111
306 0 ▶ $rw=$opts{maxw} if $opts{maxw} && $rw > $opts{maxw};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"maxw"} and $rw > $opts{"maxw"} 0 0 0
Condition0%0/3
$opts{"maxw"} and $rw > $opts{"maxw"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"maxw"} and $rw > $opts{"maxw"}
$opts{"maxw"} $rw > $opts{"maxw"}
Truth table0%0/3
A and B
A= $opts{"maxw"}B= $rw > $opts{"maxw"}
ABresult
0X0
100
111
307 0 ▶ $rh=$opts{maxh} if $opts{maxh} && $rh > $opts{maxh};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"maxh"} and $rh > $opts{"maxh"} 0 0 0
Condition0%0/3
$opts{"maxh"} and $rh > $opts{"maxh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"maxh"} and $rh > $opts{"maxh"}
$opts{"maxh"} $rh > $opts{"maxh"}
Truth table0%0/3
A and B
A= $opts{"maxh"}B= $rh > $opts{"maxh"}
ABresult
0X0
100
111
308
309 # top left corner of room (not including walls)
310 0 my $rx = min($x, max(1,int(rand() + $x - ($rw-1)/2)));
311 0 my $ry = min($y, max(1,int(rand() + $y - ($rh-1)/2)));
312  
313 0 $rw = min($rw, $self->{w}-$rx-1);
314 0 $rh = min($rh, $self->{h}-$ry-1);
315  
316 0 ▶ if (!$rh || !$rw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rh and $rw) 0 0 0
Condition0%0/3
$rh and $rw
!ll&&!rl&&r
000
MC/DC0%0/2
$rh and $rw
$rh $rw
Truth table0%0/3
A and B
A= $rhB= $rw
ABresult
0X0
100
111
317 #push @{$self->{f}}, [$rx, $ry, 'NULLROOM'];
318 0 return 0;
319 }
320  
321 0 ▶ if ($opts{nooverlap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"nooverlap"}) 0 0 0
322 0 my $ov = 0;
323 0 ▶ for (my $i = -1; $i <= $rw; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $rw) 0 0 0
324 0 ▶ if ($m->[$rx+$i][int($ry+($i * $rw/$rh))]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$m[$rx + $i][int $ry + $i * $rw / $rh]) 0 0 0
325 0 $rw = $i-1;
326 0 $rh = int(($i * $rw/$rh)) - 1;
327 }
328 }
329 0 ▶ if ($rw <= 1 || $rh <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rw <= 1 or $rh <= 1) 0 0 0
Condition0%0/3
$rw <= 1 or $rh <= 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$rw <= 1 or $rh <= 1
$rw <= 1 $rh <= 1
Truth table0%0/3
A or B
A= $rw <= 1B= $rh <= 1
ABresult
1X1
011
000
330 0 return 0;
331 }
332 }
333  
334 0 intify($rx, $ry, $rw, $rh);
335  
336 0 ▶ if ($x > ($rx+$rw) || $y > ($ry+$rh)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > $rx + $rw or $y > $ry + $rh) 0 0 0
Condition0%0/3
$x > $rx + $rw or $y > $ry + $rh
l!l&&r!l&&!r
000
MC/DC0%0/2
$x > $rx + $rw or $y > $ry + $rh
$x > $rx + $rw $y > $ry + $rh
Truth table0%0/3
A or B
A= $x > $rx + $rwB= $y > $ry + $rh
ABresult
1X1
011
000
337 0 return 0;
338 }
339  
340 0 ▶ for (my $i = -1; $i <= $rw; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $rw) 0 0 0
341 0 ▶ next if ($rx+$i) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rx + $i < 0 0 0 0
342 0 ▶ $m->[$rx+$i][$ry-1] = $self->{wsym} unless $m->[$rx+$i][$ry-1] || ($ry == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $$m[$rx + $i][$ry - 1] or $ry == 0 0 0 0
Condition0%0/3
$$m[$rx + $i][$ry - 1] or $ry == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$$m[$rx + $i][$ry - 1] or $ry == 0
$$m[$rx + $i][$ry - 1] $ry == 0
Truth table0%0/3
A or B
A= $$m[$rx + $i][$ry - 1]B= $ry == 0
ABresult
1X1
011
000
343 0 ▶ $m->[$rx+$i][$ry+$rh] = $self->{wsym} unless $m->[$rx+$i][$ry+$rh];
Branch: 1 branch
BranchTrueFalseTotal
unless $$m[$rx + $i][$ry + $rh] 0 0 0
344 }
345 0 ▶ for (my $i = 0; $i < $rw; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $rw) 0 0 0
346 0 ▶ for (my $j = 0; $j < $rh; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $rh) 0 0 0
347 0 ▶ $m->[$rx+$i][$ry+$j] = $self->{fsym} unless $m->[$rx+$i][$ry+$j];
Branch: 1 branch
BranchTrueFalseTotal
unless $$m[$rx + $i][$ry + $j] 0 0 0
348 }
349 }
350 0 ▶ for (my $i = -1; $i <= $rh; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $rh) 0 0 0
351 0 ▶ next if ($ry+$i) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ry + $i < 0 0 0 0
352 0 ▶ $m->[$rx-1][$ry+$i] = $self->{wsym} unless $m->[$rx-1][$ry+$i] || ($rx == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $$m[$rx - 1][$ry + $i] or $rx == 0 0 0 0
Condition0%0/3
$$m[$rx - 1][$ry + $i] or $rx == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$$m[$rx - 1][$ry + $i] or $rx == 0
$$m[$rx - 1][$ry + $i] $rx == 0
Truth table0%0/3
A or B
A= $$m[$rx - 1][$ry + $i]B= $rx == 0
ABresult
1X1
011
000
353 0 ▶ $m->[$rx+$rw][$ry+$i] = $self->{wsym} unless $m->[$rx+$rw][$ry+$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $$m[$rx + $rw][$ry + $i] 0 0 0
354 }
355 0 ▶ if ($opts{door}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"door"}) 0 0 0
356 0 ▶ if (rand() > .5) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() > 0.5) { } 0 0 0
357 0 ▶ if (rand() > .5) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() > 0.5) { } 0 0 0
358 0 $m->[$rx+rand()*$rw][$ry-1] = $self->{dsym};
359 } else {
360 0 $m->[$rx+rand()*$rw][$ry+$rh] = $self->{dsym};
361 }
362 0 --$opts{door};
363 } else {
364 0 ▶ if (rand() > .5) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() > 0.5) { } 0 0 0
365 0 $m->[$rx-1][$ry+rand()*$rh] = $self->{dsym};
366 } else {
367 0 $m->[$rx+$rw][$ry+rand()*$rh] = $self->{dsym};
368 }
369 0 --$opts{door};
370 }
371 }
372  
373 0 if (0) {
374 my $sig = chr(64 + int($rw));
375 print "$sig:$rx, $ry / $rw,$rh\n";
376 $m->[$x][$y] = $sig;
377 $m->[$rx][$ry] = 'C';
378 }
379  
380 0 push @{$self->{f}}, [$x, $y, 'ROOM'];
381  
382 0 return 1;
383 }
384  
385 =item findpath(x1, y1, x2, y2)
386  
387 Returns 1 if there is a path from x1,y2 to x2,y2. Uses "nomove" member as the list of symbols that cannot be moved through.
388  
389 =cut
390  
391 31 ▶sub findpath {
Subroutine findpath: called CC=15 SCAR=28.3
Pod documented
392 # flood fill 'path exists' search
393  
394 31 my $self = shift;
395 31 my ($x1, $y1, $x2, $y2) = @_;
396 31 my $f;
397  
398 31 ▶ return 1 if ($x1 == $x2 && $y1 == $y2);
Branch: 1 branch
BranchTrueFalseTotal
if $x1 == $x2 and $y1 == $y2 0 31 31
Condition33%1/3
$x1 == $x2 and $y1 == $y2
!ll&&!rl&&r
3100
MC/DC0%0/2
$x1 == $x2 and $y1 == $y2
$x1 == $x2 $y1 == $y2
Truth table33%1/3
A and B
A= $x1 == $x2B= $y1 == $y2
ABresult
0X0
100
111
399  
400 31 my @f;
401 my @bread;
402 31 push @f, [$x1, $y1];
403 31 ▶ while (@f) {
Branch: 1 branch
BranchTrueFalseTotal
if (@f) 3381 13 3394
404 3381 my $c = pop @f;
405 3381 ▶ for (my $d=0;$d<8;++$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 8) 26995 3363 30358
406 26995 my $tx = $DD[$d]->[0]+$c->[0];
407 26995 my $ty = $DD[$d]->[1]+$c->[1];
408  
409 # not off edge
410 26995 ▶ next if $tx < 0 || $ty < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tx < 0 or $ty < 0 0 26995 26995
Condition33%1/3
$tx < 0 or $ty < 0
l!l&&r!l&&!r
0026995
MC/DC0%0/2
$tx < 0 or $ty < 0
$tx < 0 $ty < 0
Truth table33%1/3
A or B
A= $tx < 0B= $ty < 0
ABresult
1X1
011
000
411 26995 ▶ next if $tx >= $self->{w} || $ty >= $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $tx >= $self->{"w"} or $ty >= $self->{"h"} 0 26995 26995
Condition33%1/3
$tx >= $self->{"w"} or $ty >= $self->{"h"}
l!l&&r!l&&!r
0026995
MC/DC0%0/2
$tx >= $self->{"w"} or $ty >= $self->{"h"}
$tx >= $self->{"w"} $ty >= $self->{"h"}
Truth table33%1/3
A or B
A= $tx >= $self->{"w"}B= $ty >= $self->{"h"}
ABresult
1X1
011
000
412  
413 # not thru void
414 26995 ▶ next if !defined($self->{map}->[$tx][$ty]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"map"}[$tx][$ty] 0 26995 26995
415 26995 ▶ next if $self->{map}->[$tx][$ty] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"map"}[$tx][$ty] eq "" 0 26995 26995
416  
417 # not thru wall
418 26995 ▶ next if index($self->{nomove}, $self->{map}->[$tx][$ty]) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($self->{"nomove"}, $self->{"map"}[$tx][$ty]) != -1 10660 16335 26995
419  
420 16335 ▶ next if $bread[$tx][$ty];
Branch: 1 branch
BranchTrueFalseTotal
if $bread[$tx][$ty] 11794 4541 16335
421 4541 $bread[$tx][$ty] = '.'; #been there
422  
423 4541 ▶ return 1 if ($tx == $x2 && $ty == $y2);
Branch: 1 branch
BranchTrueFalseTotal
if $tx == $x2 and $ty == $y2 18 4523 4541
Condition100%3/3
$tx == $x2 and $ty == $y2
!ll&&!rl&&r
44398418
MC/DC100%2/2
$tx == $x2 and $ty == $y2
$tx == $x2 $ty == $y2
Truth table100%3/3
A and B
A= $tx == $x2B= $ty == $y2
ABresult
0X0
100
111
424  
425 4523 push @f, [$tx, $ty]; #add to list;
426 }
427 }
428  
429 13 return 0;
430 }
431  
432 =item findclose(x1, y1, x2, y2)
433  
434 Returns the closest you can get to x2,y2 from x1,y2 without going through a "nomove" symbol.
435  
436 Return value is (X,Y) array, or undefined if there's no path.
437  
438 =cut
439  
440 #if (!defined(&findclose)) {
441 44 ▶sub findclose {
Subroutine findclose: called CC=14 SCAR=27.0
Pod documented
442 # flood fill return closest you can get to x2/y2 without going thru a barrier
443 44 my $self = shift;
444 44 my ($x1, $y1, $x2, $y2) = @_;
445 44 my $f;
446 my @f;
447 44 push @f, [$x1, $y1];
448 44 my @bread;
449 44 my ($cx, $cy) = ($x1, $y1);
450 44 my $mindist = ($self->{w} + $self->{h}) * 2;
451  
452 44 ▶ while (@f) {
Branch: 1 branch
BranchTrueFalseTotal
if (@f) 3591 31 3622
453 3591 my $c = pop @f;
454 3591 ▶ for (my $d=0;$d<8;++$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 8) 28683 3578 32261
455 28683 my $tx = $DD[$d]->[0]+$c->[0];
456 28683 my $ty = $DD[$d]->[1]+$c->[1];
457  
458 # not off edge
459 28683 ▶ next if $tx < 0 || $ty < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tx < 0 or $ty < 0 0 28683 28683
Condition33%1/3
$tx < 0 or $ty < 0
l!l&&r!l&&!r
0028683
MC/DC0%0/2
$tx < 0 or $ty < 0
$tx < 0 $ty < 0
Truth table33%1/3
A or B
A= $tx < 0B= $ty < 0
ABresult
1X1
011
000
460 28683 ▶ next if $tx >= $self->{w} || $ty >= $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $tx >= $self->{"w"} or $ty >= $self->{"h"} 0 28683 28683
Condition33%1/3
$tx >= $self->{"w"} or $ty >= $self->{"h"}
l!l&&r!l&&!r
0028683
MC/DC0%0/2
$tx >= $self->{"w"} or $ty >= $self->{"h"}
$tx >= $self->{"w"} $ty >= $self->{"h"}
Truth table33%1/3
A or B
A= $tx >= $self->{"w"}B= $ty >= $self->{"h"}
ABresult
1X1
011
000
461  
462 # not thru void
463 28683 ▶ next if !defined($self->{map}->[$tx][$ty]) || $self->{map}->[$tx][$ty] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"map"}[$tx][$ty] or $self->{"map"}[$tx][$ty] eq "" 0 28683 28683
Condition33%1/3
not defined $self->{"map"}[$tx][$ty] or $self->{"map"}[$tx][$ty] eq ""
l!l&&r!l&&!r
0028683
MC/DC0%0/2
not defined $self->{"map"}[$tx][$ty] or $self->{"map"}[$tx][$ty] eq ""
not defined $self->{"map"}[$tx][$ty] $self->{"map"}[$tx][$ty] eq ""
Truth table33%1/3
not(A) or B
A= not defined $self->{"map"}[$tx][$ty]B= $self->{"map"}[$tx][$ty] eq ""
ABresult
1X1
011
000
464  
465 # not thru wall
466 28683 ▶ next if index($self->{nomove}, $self->{map}->[$tx][$ty]) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($self->{"nomove"}, $self->{"map"}[$tx][$ty]) != -1 11244 17439 28683
467  
468 17439 ▶ next if $bread[$tx][$ty];
Branch: 1 branch
BranchTrueFalseTotal
if $bread[$tx][$ty] 13413 4026 17439
469 4026 $bread[$tx][$ty] = '.'; #been there
470  
471 4026 ▶ return ($tx, $ty, 0) if ($tx == $x2 && $ty == $y2);
Branch: 1 branch
BranchTrueFalseTotal
if $tx == $x2 and $ty == $y2 13 4013 4026
Condition100%3/3
$tx == $x2 and $ty == $y2
!ll&&!rl&&r
39585513
MC/DC100%2/2
$tx == $x2 and $ty == $y2
$tx == $x2 $ty == $y2
Truth table100%3/3
A and B
A= $tx == $x2B= $ty == $y2
ABresult
0X0
100
111
472  
473 4013 ▶ if ((my $tdist = distance($tx, $ty, $x2, $y2)) < $mindist) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $tdist = distance($tx, $ty, $x2, $y2)) < $mindist) 446 3567 4013
474 446 $cx = $tx;
475 446 $cy = $ty;
476 446 $mindist = $tdist;
477 }
478  
479 4013 push @f, [$tx, $ty]; #add to list of places can get to;
480 }
481 }
482  
483 # my ($ax,$ay,$ad) = findclose_c($self, $x1, $y1, $x2,$y2);
484 # if ($ax!=$cx||$ay!=$cy) {
485 # croak "C and perl disagree: ($ax,$ay <> $cx,$cy);"
486 # }
487  
488 # $self->{color}->[$cx][$cy] = 'green';
489 31 return ($cx, $cy, $mindist);
490 }#}
491  
492  
493 =item maxcardinal ()
494  
495 Maximum point someone can walk from x/y in each of 4 cardinal directions, returned as an array of XY arrays.
496  
497 Array is sorted n,s,e,w.
498  
499 =cut
500  
501 14 ▶sub maxcardinal {
Subroutine maxcardinal: called CC=9 SCAR=23.5
Pod documented
502 14 my $self = shift;
503 14 my ($x, $y) = @_;
504  
505 # maximum direction you can walk from x/y in each of the 4 directions, returned as a nsew array of points
506 14 my @r;
507 14 ▶ for (my $d=0;$d<4;++$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 4) 56 14 70
508 56 my ($cx, $cy) = ($x,$y);
509 56 while (1) {
510 146 my $tx = $DD[$d]->[0]+$cx;
511 146 my $ty = $DD[$d]->[1]+$cy;
512  
513 # not thru void
514 146 ▶ last if !defined($self->{map}->[$tx][$ty]) || $self->{map}->[$tx][$ty] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"map"}[$tx][$ty] or $self->{"map"}[$tx][$ty] eq "" 0 146 146
Condition33%1/3
not defined $self->{"map"}[$tx][$ty] or $self->{"map"}[$tx][$ty] eq ""
l!l&&r!l&&!r
00146
MC/DC0%0/2
not defined $self->{"map"}[$tx][$ty] or $self->{"map"}[$tx][$ty] eq ""
not defined $self->{"map"}[$tx][$ty] $self->{"map"}[$tx][$ty] eq ""
Truth table33%1/3
not(A) or B
A= not defined $self->{"map"}[$tx][$ty]B= $self->{"map"}[$tx][$ty] eq ""
ABresult
1X1
011
000
515  
516 # not thru wall
517 146 ▶ last if index($self->{nomove}, $self->{map}->[$tx][$ty]) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($self->{"nomove"}, $self->{"map"}[$tx][$ty]) != -1 56 90 146
518  
519 # not off edge
520 90 ▶ last if $tx < 0 || $ty < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tx < 0 or $ty < 0 0 90 90
Condition33%1/3
$tx < 0 or $ty < 0
l!l&&r!l&&!r
0090
MC/DC0%0/2
$tx < 0 or $ty < 0
$tx < 0 $ty < 0
Truth table33%1/3
A or B
A= $tx < 0B= $ty < 0
ABresult
1X1
011
000
521 90 ▶ last if $tx >= $self->{w} || $ty > $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $tx >= $self->{"w"} or $ty > $self->{"h"} 0 90 90
Condition33%1/3
$tx >= $self->{"w"} or $ty > $self->{"h"}
l!l&&r!l&&!r
0090
MC/DC0%0/2
$tx >= $self->{"w"} or $ty > $self->{"h"}
$tx >= $self->{"w"} $ty > $self->{"h"}
Truth table33%1/3
A or B
A= $tx >= $self->{"w"}B= $ty > $self->{"h"}
ABresult
1X1
011
000
522  
523 # record
524 90 ($cx, $cy) = ($tx,$ty);
525 };
526  
527 56 push @r, [$cx, $cy];
528 }
529  
530 14 return @r;
531 }
532  
533 =item digone (x, y, $ws, $fs)
534  
535 "Digs" one square of the map, at position x,y - turning it into a "floor",
536 while also turning the surrounding areas into "walls", if they are currently
537 not assigned.
538  
539 Optionally can specify wall & floor symbols (or uses area defaults).
540  
541 Does nothing if the map symbol at x,y is not a wall or void.
542  
543 Returns the number of walls that the new point is surrounded by or -1 if the point was off the map.
544  
545 =cut
546  
547 2908 ▶sub digone {
Subroutine digone: called CC=13 SCAR=27.0
Pod documented
548 2908 my $self = shift;
549 2908 my ($x, $y, $ws, $fs) = @_;
550  
551 2908 ▶ $ws = $self->{wsym} unless $ws;
Branch: 1 branch
BranchTrueFalseTotal
unless $ws 2908 0 2908
552 2908 ▶ $fs = $self->{fsym} unless $fs;
Branch: 1 branch
BranchTrueFalseTotal
unless $fs 2908 0 2908
553
554 2908 ▶ return -1 if ($x <=0 || $y <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $x <= 0 or $y <= 0 0 2908 2908
Condition33%1/3
$x <= 0 or $y <= 0
l!l&&r!l&&!r
002908
MC/DC0%0/2
$x <= 0 or $y <= 0
$x <= 0 $y <= 0
Truth table33%1/3
A or B
A= $x <= 0B= $y <= 0
ABresult
1X1
011
000
555 2908 ▶ return -1 if ($x >=($self->{w}-1) || $y >= ($self->{h}-1));
Branch: 1 branch
BranchTrueFalseTotal
if $x >= $self->{"w"} - 1 or $y >= $self->{"h"} - 1 0 2908 2908
Condition33%1/3
$x >= $self->{"w"} - 1 or $y >= $self->{"h"} - 1
l!l&&r!l&&!r
002908
MC/DC0%0/2
$x >= $self->{"w"} - 1 or $y >= $self->{"h"} - 1
$x >= $self->{"w"} - 1 $y >= $self->{"h"} - 1
Truth table33%1/3
A or B
A= $x >= $self->{"w"} - 1B= $y >= $self->{"h"} - 1
ABresult
1X1
011
000
556  
557 2908 my $c = $self->{map}->[$x][$y];
558 2908 ▶ return unless !defined($c) || ($c eq $ws) || ($c eq '');
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $c or $c eq $ws or $c eq "" 125 2783 2908
Condition83%5/6
not defined $c or $c eq $ws or $c eq ""
l!l&&r!l&&!r
27830125
not defined $c or $c eq $ws
l!l&&r!l&&!r
302753125
MC/DC66%2/3
not defined $c or $c eq $ws or $c eq ""
not defined $c $c eq $ws $c eq ""
Truth table75%3/4
not(A) or B or C
A= not defined $cB= $c eq $wsC= $c eq ""
ABCresult
1XX1
01X1
0011
0000
559 2783 $self->{map}->[$x][$y] = $fs;
560  
561 2783 ▶ for (my $d=0;$d<8;++$d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 8) 22264 2783 25047
562 22264 my $tx = $DD[$d]->[0]+$x;
563 22264 my $ty = $DD[$d]->[1]+$y;
564 22264 my $c = $self->{map}->[$tx][$ty];
565 22264 ▶ next unless !defined($c) || $c eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $c or $c eq "" 16543 5721 22264
Condition66%2/3
not defined $c or $c eq ""
l!l&&r!l&&!r
5721016543
MC/DC50%1/2
not defined $c or $c eq ""
not defined $c $c eq ""
Truth table66%2/3
not(A) or B
A= not defined $cB= $c eq ""
ABresult
1X1
011
000
566 5721 $self->{map}->[$tx][$ty] = $ws;
567 }
568  
569 #$self->drawmap();
570
571 2783 return 1;
572 }
573  
574 21 ▶sub debug {
Subroutine debug: called CC=1 SCAR=0.0
Pod undocumented
575 21 return 1;
576 }
577  
578  
579 =item nexttosym(x, y, sym)
580  
581 Returns a direction if x,y are adjacent to sym, otherwise returns undef.
582  
583 =cut
584  
585 0 ▶sub nexttosym {
Subroutine nexttosym: not called CC=3 SCAR=24.8
Pod documented
586 0 my $self = shift;
587 0 my ($x, $y, $sym) = @_;
588 0 my $dn = 0;
589 0 for (@DD) {
590 0 my $tx = $x + $_->[0];
591 0 my $ty = $y + $_->[1];
592 0 ▶ return $DIRS[$dn] if index($sym, $self->{map}[$tx][$ty]) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($sym, $self->{"map"}[$tx][$ty]) != -1 0 0 0
593 0 ++$dn;
594 }
595 0 return undef;
596 }
597  
598  
599 =item okdoor(x, y)
600  
601 Looks at the clockwize adjacent symbols for an alternating pattern of walls and floors, to see if a door
602 would be meaningful.
603  
604 Uses {nomove} as the list of "wall" symbols.
605  
606 =cut
607  
608 25 ▶sub okdoor {
Subroutine okdoor: called CC=7 SCAR=19.5
Pod documented
609 25 my $self = shift;
610 25 my ($x, $y) = @_;
611 25 my $cnt = 0;
612 25 my $cur;
613 my $first;
614 25 my $flip = 0;
615 25 for (@CWDIRS) {
616 200 my $tx = $x + $DD{$_}->[0];
617 200 my $ty = $y + $DD{$_}->[1];
618 200 my $sym = $self->{map}[$tx][$ty];
619 200 ▶ my $wl = !defined($sym) || (index($self->{nomove}, $sym) >= 0) || $sym eq $self->{dsym};
Condition83%5/6
!defined($sym) || (index($self->{'nomove'}, $sym) != -1) || $sym eq $self->{'dsym'}
l!l&&r!l&&!r
108191
not defined $sym or (index($self->{"nomove"}, $sym) != -1)
l!l&&r!l&&!r
010892
MC/DC66%2/3
!defined($sym) || (index($self->{'nomove'}, $sym) != -1) || $sym eq $self->{'dsym'}
not defined $sym (index($self->{"nomove"}, $sym) != -1) $sym eq $self->{'dsym'}
Truth table75%3/4
not(A) or B || C
A= not defined $symB= (index($self->{"nomove"}, $sym) != -1)C= $sym eq $self->{'dsym'}
ABCresult
1XX1
01X1
0011
0000
620 200 ▶ if (! defined $cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cur) { } 25 175 200
621 25 $cur = $wl;
622 25 $first = $wl;
623 } else {
624 175 ▶ if ($cur != $wl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur != $wl) 84 91 175
625 84 ++$flip;
626 84 $cur = $wl;
627 }
628 }
629
630 }
631 25 ▶ if ($cur != $first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur != $first) 12 13 25
632 12 ++$flip;
633 }
634  
635 25 return $flip == 4;
636 }
637  
638 =item makepath(x1, y1, x2, y2)
639  
640 Drill a right-angled corridor between 2 nonempty points using digone().
641  
642 ** Notably the whole auto-door upon breaking into an open area doesn't always work right, and should.
643  
644 =cut
645  
646 14 ▶sub placedoors {
Subroutine placedoors: called CC=4 SCAR=13.9
Pod undocumented
647 14 my $self=shift;
648 14 my @doors = @_;
649 14 for (@doors) {
650 25 my ($x, $y) = @$_;
651 25 ▶ if ($self->okdoor($x, $y) && $self->{dsym}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->okdoor($x, $y) and $self->{"dsym"}) 23 2 25
Condition66%2/3
$self->okdoor($x, $y) and $self->{"dsym"}
!ll&&!rl&&r
2023
MC/DC50%1/2
$self->okdoor($x, $y) and $self->{"dsym"}
$self->okdoor($x, $y) $self->{"dsym"}
Truth table66%2/3
A and B
A= $self->okdoor($x, $y)B= $self->{"dsym"}
ABresult
0X0
100
111
652 23 $self->{map}->[$x][$y] = $self->{dsym};
653 }
654 }
655 }
656  
657 23 ▶sub makepath {
Subroutine makepath: called CC=30 SCAR=34.9
Pod documented
658 23 my $self = shift;
659 23 my ($ox, $oy, $x2, $y2) = @_;
660  
661 croak "can't make a path without floor and wall symbols set"
662 23 ▶ unless $self->{wsym} && $self->{fsym};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"wsym"} and $self->{"fsym"} 0 23 23
Condition33%1/3
$self->{"wsym"} and $self->{"fsym"}
!ll&&!rl&&r
0023
MC/DC0%0/2
$self->{"wsym"} and $self->{"fsym"}
$self->{"wsym"} $self->{"fsym"}
Truth table33%1/3
A and B
A= $self->{"wsym"}B= $self->{"fsym"}
ABresult
0X0
100
111
663  
664 # $self->{map}->[$x][$y] = chr(64 + ++$self->{dseq}) if $self->debug;
665 # $self->{map}->[$x2][$y2] = chr(64 + ++$->{dseq}) if $self->debug;
666  
667 23 my ($x, $y) = ($ox, $oy);
668  
669 23 ▶ if ($self->{map}->[$x][$y] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"map"}[$x][$y] eq "") 0 23 23
670 0 return;
671 }
672 23 ▶ if ($self->{map}->[$x2][$y2] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"map"}[$x2][$y2] eq "") 0 23 23
673 0 return;
674 }
675  
676 23 my $d;
677 23 ▶ if ($y < $y2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y < $y2) { } 9 14 23
elsif ($y > $y2) { } 5 9 14
678 9 $d = 's';
679 } elsif ($y > $y2) {
680 5 $d = 'n';
681 }
682  
683 23 ▶ if ($x < $x2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x < $x2) { } 5 18 23
elsif ($x > $x2) { } 9 9 18
684 5 $d .= 'e';
685 } elsif ($x > $x2) {
686 9 $d .= 'w';
687 }
688  
689 23 ▶ return if !$d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 9 14 23
690  
691 # 2 directions, randomly sorted
692
693 14 my @d;
694 14 $d[1] = $d;
695 14 $d[0] = substr($d, rand()*2, 1);
696 14 $d[1] =~ s/$d[0]//;
697  
698 # closest can get now
699 14 ($x, $y) = $self->findclose($x, $y, $x2, $y2);
700  
701 # $self->dprint "($x, $y) closest from $ox $oy to $x2, $y2";
702  
703 # choose a random square among maximum wall range from closest point
704 14 my @mc = $self->maxcardinal($x, $y);
705 14 $d=$d[0];
706 14 my $len;
707 14 ▶ if ($d =~ /^n|s$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /^n|s$/) { } 10 4 14
708 10 $x = $mc[$DI{w}]->[0] + rand() * ($mc[$DI{e}]->[0] - $mc[$DI{w}]->[0]);
709 } else {
710 4 $y = $mc[$DI{n}]->[1] + rand() * ($mc[$DI{s}]->[1] - $mc[$DI{n}]->[1]);
711 }
712 14 intify($x, $y);
713  
714 14 my @doors;
715  
716 14 my $firstdig = 1; # first dig out of an area gets a door
717 14 for my $d (@d) {
718 21 ▶ next unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 21 21
719  
720 21 ▶ $self->{map}->[$x][$y] = $d if $self->debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 21 21
721  
722 21 ▶ if ($d =~ /^n|s$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d =~ /^n|s$/) { } 11 10 21
723 11 $len = abs($y-$y2);
724 } else {
725 10 $len = abs($x-$x2);
726 }
727  
728 21 my $already_dug = 0;
729 21 my $waswall = 0;
730
731 21 ▶ for (my $i = 0; $i < $len; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 251 7 258
732 251 my ($px,$py) = ($x, $y);
733 251 $x += $DD{$d}->[0];
734 251 $y += $DD{$d}->[1];
735  
736 251 my $iswall = $self->{map}->[$x][$y] eq $self->{wsym};
737 251 ▶ if ($iswall || $self->{map}->[$x][$y] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($iswall or $self->{'map'}[$x][$y] eq '') { } 236 15 251
Condition66%2/3
$iswall or $self->{"map"}[$x][$y] eq ""
l!l&&r!l&&!r
236015
MC/DC50%1/2
$iswall or $self->{"map"}[$x][$y] eq ""
$iswall $self->{"map"}[$x][$y] eq ""
Truth table66%2/3
A or B
A= $iswallB= $self->{"map"}[$x][$y] eq ""
ABresult
1X1
011
000
738 236 $self->digone($x,$y);
739 236 ▶ if ($firstdig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstdig) 13 223 236
740 13 push @doors, [$x, $y];
741 13 $firstdig = 0;
742 }
743 } else {
744 # not a wall, already dug
745 15 $already_dug = 1;
746 15 ▶ if ($waswall) {
Branch: 1 branch
BranchTrueFalseTotal
if ($waswall) { } 12 3 15
747 12 push @doors, [$px, $py];
748 } else {
749 # 2 non walls in a row = reset firstdig
750 3 $firstdig = 1;
751 }
752 }
753 251 $waswall = $iswall;
754  
755
756 251 ▶ if ( $already_dug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($already_dug) 30 221 251
757 30 my ($fx, $fy, $dist) = $self->findclose($x, $y, $x2, $y2);
758 30 ▶ if (&distance($fx, $fy, $x, $y) >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&distance($fx, $fy, $x, $y) >= 2) 10 20 30
759 10 my $res = $self->makepath($fx, $fy, $x2, $y2);
760 10 $self->placedoors(@doors);
761 10 return $res;
762 }
763 20 ▶ last if $dist == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dist == 0 4 16 20
764 }
765  
766 #$self->{world}->drawmap();
767 # too close to edge
768 237 ▶ last if $x<=1 || $y<=1 || $x>=($self->{w}-1) || $y>=($self->{h}-1);
Branch: 1 branch
BranchTrueFalseTotal
if $x <= 1 or $y <= 1 or $x >= $self->{"w"} - 1 or $y >= $self->{"h"} - 1 0 237 237
Condition33%3/9
$x <= 1 or $y <= 1 or $x >= $self->{"w"} - 1 or $y >= $self->{"h"} - 1
l!l&&r!l&&!r
00237
$x <= 1 or $y <= 1 or $x >= $self->{"w"} - 1
l!l&&r!l&&!r
00237
$x <= 1 or $y <= 1
l!l&&r!l&&!r
00237
MC/DC0%0/4
$x <= 1 or $y <= 1 or $x >= $self->{"w"} - 1 or $y >= $self->{"h"} - 1
$x <= 1 $y <= 1 $x >= $self->{"w"} - 1 $y >= $self->{"h"} - 1
Truth table20%1/5
A or B or C or D
A= $x <= 1B= $y <= 1C= $x >= $self->{"w"} - 1D= $y >= $self->{"h"} - 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
769 }
770 11 ▶ last if ($x == $x2 && $y == $y2);
Branch: 1 branch
BranchTrueFalseTotal
if $x == $x2 and $y == $y2 3 8 11
Condition100%3/3
$x == $x2 and $y == $y2
!ll&&!rl&&r
713
MC/DC100%2/2
$x == $x2 and $y == $y2
$x == $x2 $y == $y2
Truth table100%3/3
A and B
A= $x == $x2B= $y == $y2
ABresult
0X0
100
111
771 }
772  
773
774 4 ▶ if (!$self->findpath($ox, $oy, $x2, $y2)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->findpath($ox, $oy, $x2, $y2)) 0 4 4
775 0 $self->makepath2($ox, $oy, $x2, $y2);
776 }
777  
778 4 $self->placedoors(@doors);
779  
780 4 return 1;
781 }
782  
783 =item makepath2 (x1, y1, x2, y2)
784  
785 Like makepath, but diagonal and with no doors.
786  
787 =cut
788  
789  
790 0 ▶sub makepath2 {
Subroutine makepath2: not called CC=9 SCAR=45.0
Pod documented
791 0 my $self = shift;
792 0 my ($ox, $oy, $x2, $y2) = @_;
793  
794 croak "can't make a path without floor and wall symbols set"
795 0 ▶ unless $self->{wsym} && $self->{fsym};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"wsym"} and $self->{"fsym"} 0 0 0
Condition0%0/3
$self->{"wsym"} and $self->{"fsym"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"wsym"} and $self->{"fsym"}
$self->{"wsym"} $self->{"fsym"}
Truth table0%0/3
A and B
A= $self->{"wsym"}B= $self->{"fsym"}
ABresult
0X0
100
111
796  
797 0 my ($x, $y) = ($ox, $oy);
798  
799 0 ($x, $y) = $self->findclose($x, $y, $x2, $y2);
800  
801 0 ▶ while (($x != $x2) || ($y != $y2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x != $x2 or $y != $y2) 0 0 0
Condition0%0/3
$x != $x2 or $y != $y2
l!l&&r!l&&!r
000
MC/DC0%0/2
$x != $x2 or $y != $y2
$x != $x2 $y != $y2
Truth table0%0/3
A or B
A= $x != $x2B= $y != $y2
ABresult
1X1
011
000
802 0 $self->digone($x, $y);
803 0 my $d;
804 0 ▶ if ($y < $y2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y < $y2) { } 0 0 0
elsif ($y > $y2) { } 0 0 0
805 0 $d = 's';
806 } elsif ($y > $y2) {
807 0 $d = 'n';
808 }
809 0 ▶ if ($x < $x2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x < $x2) { } 0 0 0
elsif ($x > $x2) { } 0 0 0
810 0 $d .= 'e';
811 } elsif ($x > $x2) {
812 0 $d .= 'w';
813 }
814  
815 0 $x += $DD{$d}->[0];
816 0 $y += $DD{$d}->[1];
817 }
818 }
819  
820  
821 =item findfeature (symbol)
822  
823 Searches "map feature list" for the given symbol, returns coordinates if found.
824  
825 =cut
826  
827 0 ▶sub findfeature {
Subroutine findfeature: not called CC=3 SCAR=24.8
Pod documented
828 0 my $self = shift;
829 0 my ($sym) = @_;
830  
831 0 for (@{$self->{f}}) {
832 0 my ($fx, $fy) = @$_;
833 0 ▶ if ($self->{map}->[$fx][$fy] eq $sym) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"map"}[$fx][$fy] eq $sym) 0 0 0
834 0 return ($fx, $fy);
835 }
836 }
837 }
838  
839 =item addfeature (symbol [, x, y])
840  
841 Adds a symbol to the map (to a random open floor point if one is not specified), and also adds it to the searchable "feature list".
842  
843 =cut
844  
845 0 ▶sub addfeature {
Subroutine addfeature: not called CC=2 SCAR=17.9
Pod documented
846 0 my $self = shift;
847 0 my ($sym, $x, $y) = @_;
848  
849 0 ▶ if (!defined($x)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $x) 0 0 0
850 0 ($x, $y) = $self->findrandmap($self->{fsym}, 0, 1);
851 }
852 0 $self->{map}->[$x][$y] = $sym;
853 0 push @{$self->{f}}, [$x, $y];
854 }
855  
856 =item inbound(x, y)
857  
858 Returns true if x >= 0 and x < $self->{w} and y >= 0 and y < $self->{h}.
859  
860 =cut
861  
862 0 ▶sub inbound {
Subroutine inbound: not called CC=4 SCAR=30.0
Pod documented
863 0 my $self = shift;
864 0 ▶ return ($_[0]>=0)&&($_[0]<($self->{w}))&&($_[1]>=0)&&($_[1]<($self->{h}));
Condition0%0/9
$_[0] >= 0 && $_[0] < $self->{'w'} && $_[1] >= 0 && $_[1] < $self->{'h'}
!ll&&!rl&&r
000
$_[0] >= 0 and $_[0] < $self->{"w"} and $_[1] >= 0
!ll&&!rl&&r
000
$_[0] >= 0 and $_[0] < $self->{"w"}
!ll&&!rl&&r
000
MC/DC0%0/4
$_[0] >= 0 && $_[0] < $self->{'w'} && $_[1] >= 0 && $_[1] < $self->{'h'}
$_[0] >= 0 $_[0] < $self->{"w"} $_[1] >= 0 $_[1] < $self->{'h'}
Truth table0%0/5
A and B and C && D
A= $_[0] >= 0B= $_[0] < $self->{"w"}C= $_[1] >= 0D= $_[1] < $self->{'h'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
865 }
866  
867  
868 =item generate ($type [, options ... ])
869  
870 This calls "$type::generate" if it is defined, perhaps in the caller's package, etc.
871  
872 Predefined types are "Rooms", "Cavelike" and "Maze" (see below).
873  
874 If no "::" is present, 'Games::Roguelike::Area::' is prepended, and the first character is propercased.
875  
876 If &{"$type::generate"} is not defined, it will require "$type" and then try again.
877  
878 Assuming there is no function named "wilderness::generate", then the following are roughly equivalent:
879  
880 $self->generate('Games::Roguelike::Area::Rooms');
881 or
882 $self->generate('rooms');
883 or
884 Games::Roguelike::Area::Rooms::generate($self);
885 or
886 $self = new Games::Roguelike::Area::Rooms; $self->generate();
887  
888 Additonal options are passed to the generate function. Generate functions should support named parameters..
889  
890 Common option is a "with=>[feature1, feature2]" feature list, that get added to empty floor map locations,
891 and appended to the map's feature list.
892  
893 Height, width are taken from the area object.
894  
895 =cut
896  
897 2 ▶sub generate {
Subroutine generate: called CC=4 SCAR=14.2
Pod documented
898 2 my $self = shift;
899 2 my $type = shift;
900  
901 2 my $path = $type . '::generate';
902 2 ▶ if (defined &$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$path) 0 2 2
903 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
904 0 return &$type($self, @_);
905 }
906  
907 2 ▶ if ($type !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /::/) 2 0 2
908 2 $type =~ s/^(\w)/\U$1/;
909 2 $type = 'Games::Roguelike::Area::' . $type;
910 2 $path = $type . '::generate';
911 2 ▶ if (defined &$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$path) 2 0 2
912 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
913 2 return &$path($self, @_);
914 }
915 }
916 0 eval "require $type";
917 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
918 0 return &$path($self, @_);
919 }
920  
921  
922 # this is intended as *example* of making a map that i got to work in a few hours
923 # it is not intended as a good map
924 # if map-making isn't what you want to work on in the beginning, you can start here
925  
926  
927 =item generate('cavelike', [with=>[sym1[,sym2...]])
928  
929 Makes a random map with a bunch of cave-like rooms connected by corridors.
930  
931 Can specify a list of symbols to be added as "features" of the map.
932  
933 =cut
934  
935 2712 ▶sub diginbound {
Subroutine diginbound: called CC=4 SCAR=13.9
Pod undocumented
936 2712 my $self = shift;
937 2712 ▶ return ($_[0]>0)&&($_[0]<($self->{w}-2))&&($_[1]>0)&&($_[1]<($self->{h}-2));
Condition88%8/9
$_[0] > 0 && $_[0] < $self->{'w'} - 2 && $_[1] > 0 && $_[1] < $self->{'h'} - 2
!ll&&!rl&&r
51212640
$_[0] > 0 and $_[0] < $self->{"w"} - 2 and $_[1] > 0
!ll&&!rl&&r
25262661
$_[0] > 0 and $_[0] < $self->{"w"} - 2
!ll&&!rl&&r
0252687
MC/DC75%3/4
$_[0] > 0 && $_[0] < $self->{'w'} - 2 && $_[1] > 0 && $_[1] < $self->{'h'} - 2
$_[0] > 0 $_[0] < $self->{"w"} - 2 $_[1] > 0 $_[1] < $self->{'h'} - 2
Truth table80%4/5
A and B and C && D
A= $_[0] > 0B= $_[0] < $self->{"w"} - 2C= $_[1] > 0D= $_[1] < $self->{'h'} - 2
ABCDresult
0XXX0
10XX0
110X0
11100
11111
938 }
939  
940 5 ▶sub genmaze2 {
Subroutine genmaze2: called CC=11 SCAR=24.0
Pod undocumented
941 5 my $self = shift;
942 5 my %opts = @_;
943  
944 5 my ($m, $fx, $fy);
945  
946 5 my $digc = 0;
947  
948 do {
949 32 my ($cx, $cy) = $self->rpoint();
950 32 $self->digone($cx, $cy);
951 32 ▶ if (my $feature = shift @{$opts{with}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $feature = shift @{$opts{'with'};}) { } 0 32 32
952 0 $self->{map}->[$cx][$cy]=$feature;
953 0 push @{$self->{f}}, [$cx, $cy, 'FEATURE'];
954 } else {
955 32 push @{$self->{f}}, [$cx, $cy, 'ROOM'];
956 }
957 32 my @v;
958 32 $v[$cx][$cy]=1;
959 32 my $dug = 0;
960 32 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$dug and do { do { my $o = randi(4); $dug = 0; for (my $i = 0; $i < 4; ++$i) { () = ($cx + $Games::Roguelike::Area::DD[($i + $o) % 4][0], $cy + $Games::Roguelike::Area::DD[($i + $o) % 4][1]); if (not $v[$tx][$ty] and $self->diginbound($tx, $ty)) { () = ; ++$digc if $self->digone($cx, $cy); $v[$cx][$cy] = 1; $dug = 1; last; } ; } } } 2640 32 2672
961 2672 my $o = randi(4);
962 2672 $dug = 0;
963 2672 ▶ for (my $i=0;$i<4;++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 3931 32 3963
964 3931 my ($tx, $ty) = ($cx+$DD[($i+$o)%4]->[0], $cy+$DD[($i+$o)%4]->[1]);
965 3931 ▶ if ((!$v[$tx][$ty]) && $self->diginbound($tx, $ty)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $v[$tx][$ty] and $self->diginbound($tx, $ty)) 2640 1291 3931
Condition100%3/3
not $v[$tx][$ty] and $self->diginbound($tx, $ty)
!ll&&!rl&&r
1219722640
MC/DC100%2/2
not $v[$tx][$ty] and $self->diginbound($tx, $ty)
not $v[$tx][$ty] $self->diginbound($tx, $ty)
Truth table100%3/3
not(A) and B
A= not $v[$tx][$ty]B= $self->diginbound($tx, $ty)
ABresult
0X0
100
111
966 2640 ($cx, $cy) = ($tx, $ty);
967 2640 ▶ ++$digc if $self->digone($cx, $cy);
Branch: 1 branch
BranchTrueFalseTotal
if $self->digone($cx, $cy) 2517 123 2640
968 #print "dig at $cx, $cy $v[$cx][$cy]\n";
969 2640 $v[$cx][$cy] = 1;
970 2640 $dug = 1;
971 2640 last;
972 }
973 }
974 } while ($dug);
975  
976 5 ▶ } while ($digc < (($self->{w}*$self->{h})/8));
Branch: 1 branch
BranchTrueFalseTotal
$digc < $self->{"w"} * $self->{"h"} / 8 and do { do { () = $self->rpoint; $self->digone($cx, $cy); if (my $feature = shift @{$opts{"with"};}) { $self->{"map"}[$cx][$cy] = $feature; push @{$$self{"f"};}, [$cx, $cy, "FEATURE"]; } else { push @{$$self{"f"};}, [$cx, $cy, "ROOM"]; } ; my @v; $v[$cx][$cy] = 1; my $dug = 0; do { my $o = randi(4); $dug = 0; for (my $i = 0; $i < 4; ++$i) { () = ($cx + $Games::Roguelike::Area::DD[($i + $o) % 4][0], $cy + $Games::Roguelike::Area::DD[($i + $o) % 4][1]); if (not $v[$tx][$ty] and $self->diginbound($tx, $ty)) { () = ; ++$digc if $self->digone($cx, $cy); $v[$cx][$cy] = 1; $dug = 1; last; } ; } } while $dug } } 27 5 32
977  
978 # dig out paths
979 5 my ($px, $py);
980 5 for (randsort(@{$self->{f}})) {
981 32 my ($x, $y, $reason) = @{$_};
982 32 ▶ if ($px) {
Branch: 1 branch
BranchTrueFalseTotal
if ($px) 27 5 32
983 27 ▶ if (!$self->findpath($x, $y, $px, $py)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->findpath($x, $y, $px, $py)) 13 14 27
984 13 $self->makepath($x, $y, $px, $py);
985 #$self->drawmap();
986 #$self->getch();
987 }
988 }
989 32 ($px, $py) = ($x, $y);
990 }
991 }
992  
993 =item generate ('rooms', [with=>[sym1[,sym2...]])
994  
995 Makes a random nethack-style map with a bunch of rectangle rooms connected by corridors
996  
997 If you specify a "with" list, it puts those symbols on the map in random rooms, and calls "addfeature" on them.
998  
999 =cut
1000  
1001 0 ▶sub genmaze1 {
Subroutine genmaze1: not called CC=9 SCAR=45.0
Pod undocumented
1002 0 my $self = shift;
1003 0 my %opts = @_;
1004  
1005 0 my ($m, $fx, $fy);
1006  
1007 0 my $rooms = 0;
1008  
1009 0 for my $feature (@{$opts{with}}) {
1010 0 ($fx, $fy) = $self->rpoint_empty();
1011 0 $self->{map}->[$fx][$fy]=$feature;
1012 0 push @{$self->{f}}, [$fx, $fy, 'FEATURE'];
1013 0 ▶ ++$rooms if $self->genroom($fx, $fy); # put rooms around features
Branch: 1 branch
BranchTrueFalseTotal
if $self->genroom($fx, $fy) 0 0 0
1014 }
1015  
1016 # some extra rooms
1017 0 ▶ while($rooms < ($self->{w}*$self->{h}/600)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rooms < $self->{"w"} * $self->{"h"} / 600) 0 0 0
1018 0 ▶ ++$rooms if $self->genroom(($fx, $fy) = $self->rpoint_empty(), nooverlap=>1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->genroom(() = $self->rpoint_empty, "nooverlap", 1) 0 0 0
1019 }
1020  
1021 # dig out paths
1022 0 my ($px, $py);
1023 0 for (randsort(@{$self->{f}})) {
1024 0 my ($x, $y, $reason) = @{$_};
1025 0 ▶ if ($px) {
Branch: 1 branch
BranchTrueFalseTotal
if ($px) 0 0 0
1026 0 ▶ if (!$self->findpath($x, $y, $px, $py)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->findpath($x, $y, $px, $py)) 0 0 0
1027 0 $self->makepath($x, $y, $px, $py);
1028 0 ▶ if (!$self->findpath($x, $y, $px, $py)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->findpath($x, $y, $px, $py)) 0 0 0
1029 0 $self->{map}->[$px][$py] = '1';
1030 0 $self->{map}->[$x][$y] = '2';
1031 0 $self->dump();
1032 0 die "makepath failed!!!\n";
1033 0 $self->makepath($x, $y, $px, $py);
1034 }
1035 #$self->drawmap();
1036 }
1037 }
1038 0 ($px, $py) = ($x, $y);
1039 }
1040 }
1041  
1042 =item generate('maze', rand=>number, with=>feature-list)
1043  
1044 Generate a tight, difficult maze. Rand defaults to 5 (higher numbers are less random).
1045  
1046 =cut
1047  
1048 0 ▶sub genmaze3 {
Subroutine genmaze3: not called CC=21 SCAR=59.6
Pod undocumented
1049 0 my $self = shift;
1050 0 my %opts = @_;
1051  
1052 0 ▶ $opts{w} = $self->{w} if !$opts{w};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"w"} 0 0 0
1053 0 ▶ $opts{h} = $self->{h} if !$opts{h};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"h"} 0 0 0
1054  
1055 0 my ($cNx,$cNy, $cSx, $cSy);
1056 0 my $intDir;
1057 0 my $intDone = 0;
1058  
1059 0 my $blnBlocked;
1060  
1061 5 ▶ use constant X => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
1062 5 ▶ use constant Y => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1063  
1064 0 ▶ $opts{rand} = 5 if !$opts{rand};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"rand"} 0 0 0
1065  
1066 0 ▶ $opts{w} -= 1 if !($opts{w}%2);
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"w"} % 2 0 0 0
1067 0 ▶ $opts{h} -= 1 if !($opts{h}%2);
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"h"} % 2 0 0 0
1068  
1069 # stores the directions that corridors go in
1070 0 my @cDir;
1071 my @blnMaze;
1072  
1073 do {
1074 # this code is used to make sure the numbers are odd
1075 0 $cSx = 1 + (int((($opts{w} - 1) * rand()) / 2) * 2);
1076 0 $cSy = 1 + (int((($opts{h} - 1) * rand()) / 2) * 2);
1077  
1078 # first opening is free!
1079 0 ▶ $blnMaze[$cSx][$cSy] = 1 if !$intDone;
Branch: 1 branch
BranchTrueFalseTotal
unless $intDone 0 0 0
1080  
1081 0 ▶ if ($blnMaze[$cSx][$cSy]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blnMaze[$cSx][$cSy]) 0 0 0
1082 # randomize directions to start
1083 0 @cDir = &getRandomDirections();
1084 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$blnBlocked or do { do { @cDir = &getRandomDirections() unless int $opts{"rand"} * (rand()); $blnBlocked = 1; for ($intDir = 0; $intDir < 4; ++$intDir) { $cNx = $cSx + $cDir[$intDir][0] * 2; $cNy = $cSy + $cDir[$intDir][1] * 2; my $isFree; if ($cNx < $opts{"w"} - 1 and $cNx >= 1 and $cNy < $opts{"h"} - 1 and $cNy >= 1) { $isFree = !$blnMaze[$cNx][$cNy]; } ; if ($isFree) { $blnMaze[$cNx][$cNy] = 1; $blnMaze[$cSx + $cDir[$intDir][0]][$cSy + $cDir[$intDir][1]] = 1; $cSx = $cNx; $cSy = $cNy; $blnBlocked = 0; $intDone = $intDone + 1; last; } ; } } } 0 0 0
1085 # only randomisation directions, based on the constant
1086 0 ▶ @cDir = &getRandomDirections() if !int($opts{rand} * rand());
Branch: 1 branch
BranchTrueFalseTotal
unless int $opts{"rand"} * (rand()) 0 0 0
1087 0 $blnBlocked = 1;
1088 # loop through order of directions
1089 0 ▶ for ($intDir = 0; $intDir < 4; ++$intDir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intDir < 4) 0 0 0
1090 # work out where this direction is
1091 0 $cNx = $cSx + ($cDir[$intDir][X] * 2);
1092 0 $cNy = $cSy + ($cDir[$intDir][Y] * 2);
1093 # check if the tile can be used
1094 0 my $isFree;
1095 0 ▶ if ($cNx < ($opts{w}-1) && $cNx >= 1 && $cNy < ($opts{h}-1) && $cNy >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cNx < $opts{"w"} - 1 and $cNx >= 1 and $cNy < $opts{"h"} - 1 and $cNy >= 1) 0 0 0
Condition0%0/9
$cNx < $opts{"w"} - 1 and $cNx >= 1 and $cNy < $opts{"h"} - 1 and $cNy >= 1
!ll&&!rl&&r
000
$cNx < $opts{"w"} - 1 and $cNx >= 1 and $cNy < $opts{"h"} - 1
!ll&&!rl&&r
000
$cNx < $opts{"w"} - 1 and $cNx >= 1
!ll&&!rl&&r
000
MC/DC0%0/4
$cNx < $opts{"w"} - 1 and $cNx >= 1 and $cNy < $opts{"h"} - 1 and $cNy >= 1
$cNx < $opts{"w"} - 1 $cNx >= 1 $cNy < $opts{"h"} - 1 $cNy >= 1
Truth table0%0/5
A and B and C and D
A= $cNx < $opts{"w"} - 1B= $cNx >= 1C= $cNy < $opts{"h"} - 1D= $cNy >= 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1096 # true if it hasn't been used yet
1097 0 $isFree = !$blnMaze[$cNx][$cNy];
1098 }
1099 0 ▶ if ($isFree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isFree) 0 0 0
1100 # create a path
1101 0 $blnMaze[$cNx][$cNy] = 1;
1102 # and the square inbetween
1103 0 $blnMaze[$cSx + $cDir[$intDir][X]][$cSy + $cDir[$intDir][Y]] = 1;
1104 # this is now the current square
1105 0 $cSx = $cNx;
1106 0 $cSy = $cNy;
1107 0 $blnBlocked = 0;
1108 # increment paths created
1109 0 $intDone = $intDone + 1;
1110 0 last;
1111 }
1112 }
1113 # loop until a path was created
1114 } while (!$blnBlocked)
1115 }
1116 0 ▶ } while ( $intDone + 1 < ( (($opts{w} - 1) * ($opts{h} - 1)) / 4 ) );
Branch: 1 branch
BranchTrueFalseTotal
$intDone + 1 < ($opts{"w"} - 1) * ($opts{"h"} - 1) / 4 and do { do { $cSx = 1 + int(($opts{"w"} - 1) * (rand()) / 2) * 2; $cSy = 1 + int(($opts{"h"} - 1) * (rand()) / 2) * 2; $blnMaze[$cSx][$cSy] = 1 unless $intDone; if ($blnMaze[$cSx][$cSy]) { @cDir = &getRandomDirections(); do { @cDir = &getRandomDirections() unless int $opts{"rand"} * (rand()); $blnBlocked = 1; for ($intDir = 0; $intDir < 4; ++$intDir) { $cNx = $cSx + $cDir[$intDir][0] * 2; $cNy = $cSy + $cDir[$intDir][1] * 2; my $isFree; if ($cNx < $opts{"w"} - 1 and $cNx >= 1 and $cNy < $opts{"h"} - 1 and $cNy >= 1) { $isFree = !$blnMaze[$cNx][$cNy]; } ; if ($isFree) { $blnMaze[$cNx][$cNy] = 1; $blnMaze[$cSx + $cDir[$intDir][0]][$cSy + $cDir[$intDir][1]] = 1; $cSx = $cNx; $cSy = $cNy; $blnBlocked = 0; $intDone = $intDone + 1; last; } ; } } until $blnBlocked; } } } 0 0 0
1117 # create enough paths to fill the whole grid
1118  
1119  
1120 # this changes the direction to go from the current square, to the next available
1121 0 ▶ sub getRandomDirections {
Subroutine getRandomDirections: not called CC=2 SCAR=17.9
Pod undocumented
1122 # clear the array
1123 0 my @a = ([-1,0],[1,0],[0,-1],[0,1]);
1124 0 my @b;
1125 0 ▶ while (@a) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 0 0 0
1126 0 push @b, splice(@a, rand()*scalar(@a), 1);
1127 }
1128 0 return @b;
1129 }
1130  
1131 0 $self->{map} = [];
1132  
1133 0 ▶ for (my $y = 0; $y < $opts{h}; ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $opts{"h"}) 0 0 0
1134 0 ▶ for (my $x = 0; $x < $opts{w}; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $opts{"w"}) 0 0 0
1135 0 ▶ $self->{map}->[$x][$y] = ($blnMaze[$x][$y] ? $self->{fsym} : $self->{wsym});
Branch: 1 branch
BranchTrueFalseTotal
$blnMaze[$x][$y] ? : 0 0 0
1136 }
1137 }
1138  
1139  
1140 0 for my $feature (@{$opts{with}}) {
1141 0 my ($fx, $fy) = $self->findrandmap('.');
1142 0 $self->{map}->[$fx][$fy]=$feature;
1143 0 push @{$self->{f}}, [$fx, $fy, 'FEATURE'];
1144 }
1145 }
1146  
1147 =item foreachmap (code-reference [,noborder=>1] [,border=>1])
1148  
1149 Loops over each map location, and calls the code with parameters x, y and map-symbol.
1150  
1151 Option "border" calls the code only for border (edge) squares.
1152  
1153 Option "noborder" calls the code for everything but border (edge) squares.
1154  
1155 =cut
1156  
1157 0 ▶sub foreachmap {
Subroutine foreachmap: not called CC=9 SCAR=45.0
Pod documented
1158 0 my $self = shift;
1159 0 my $code = shift;
1160 0 my %opts = @_;
1161
1162 0 ▶ if ($opts{border}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'border'}) { } 0 0 0
1163 0 for my $y ((0,$self->{h}-1)) {
1164 0 ▶ for (my $x = 0; $x < $self->{w}; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"w"}) 0 0 0
1165 0 &{$code}($x, $y, $self->{map}->[$x][$y]);
1166 }
1167 }
1168 0 for my $x ((0,$self->{w}-1)) {
1169 0 ▶ for (my $y = 0; $y < $self->{h}; ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $self->{"h"}) 0 0 0
1170 0 &{$code}($x, $y, $self->{map}->[$x][$y]);
1171 }
1172 }
1173 } else {
1174 0 my ($x1, $y1, $x2, $y2) = (0, 0, $self->{w},$self->{h});
1175  
1176 0 ▶ if ($opts{nobodrder}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"nobodrder"}) 0 0 0
1177 0 $x1++; $y1++;
1178 0 $x2--; $y2--;
1179 }
1180  
1181 0 ▶ for (my $y = $y1; $y < $y2; ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $y2) 0 0 0
1182 0 ▶ for (my $x = $x1; $x < $x2; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $x2) 0 0 0
1183 0 &{$code}($x, $y, $self->{map}->[$x][$y]);
1184 }
1185 }
1186 }
1187 }
1188  
1189 =item draw ({dispx=>, dispy=>, vp=>, con=>});
1190  
1191 Draws the map using offset params dispx, dispy,disph,dispw,
1192  
1193 Uses the perspective of $vp (viewpoint), if specified. $vp can contain an x, y coordinate
1194 and a "pov" integer which is the maximum sight distance.
1195  
1196 Uses the console $con to draw the map.
1197  
1198 Uses the algorithm provided by the "checkpov" function for field of view calculations.
1199  
1200 Usually done after each move.
1201  
1202 =cut
1203  
1204 3 ▶sub draw {
Subroutine draw: called CC=21 SCAR=37.7
Pod documented
1205 3 my $self = shift;
1206 3 my ($opts) = @_;
1207  
1208 3 my $dispx = $opts->{dispx};
1209 3 my $dispy = $opts->{dispy};
1210 3 my $dispw = $opts->{dispw};
1211 3 my $disph = $opts->{disph};
1212 3 my $vp = $opts->{vp};
1213 3 my $con = $opts->{con};
1214  
1215 3 my $debugx = $dispx;
1216 3 my $debugy = $dispy;
1217 3 ▶ if ($self->{debugmap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debugmap"}) 0 3 3
1218 0 $dispx += 3; $dispw -= 3;
1219 0 $dispy += 3; $disph -= 3;
1220 }
1221  
1222 3 my $ox = 0;
1223 3 my $oy = 0;
1224 3 ▶ if ($vp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vp) 3 0 3
1225 3 $ox = $vp->{x}-($dispw/2); #substract offsets from actual
1226 3 $oy = $vp->{y}-($disph/2);
1227 3 ▶ $ox = 0 if $ox < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ox < 0 0 3 3
1228 3 ▶ $oy = 0 if $oy < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $oy < 0 3 0 3
1229 3 ▶ $ox = $self->{w}-$dispw if ($ox+$dispw) > $self->{w};
Branch: 1 branch
BranchTrueFalseTotal
if $ox + $dispw > $self->{"w"} 1 2 3
1230 3 ▶ $oy = $self->{h}-$disph if ($oy+$disph) > $self->{h};
Branch: 1 branch
BranchTrueFalseTotal
if $oy + $disph > $self->{"h"} 0 3 3
1231 }
1232 3 intify($ox, $oy);
1233  
1234 3 ▶ if ($self->{debugmap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debugmap"}) 0 3 3
1235 # show labels to help debuggin map routines
1236 0 $con->addstr($debugy,$debugx," " x 3);
1237 0 ▶ for (my $x = $ox; $x < $dispw+$ox; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $dispw + $ox) 0 0 0
1238 0 $con->addstr(substr(sprintf("%03.0d", $x),-2,1));
1239 }
1240 0 $con->addstr($debugy+1,$debugx," " x 3);
1241 0 ▶ for (my $x = $ox; $x < $dispw+$ox; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $dispw + $ox) 0 0 0
1242 0 $con->addstr(substr(sprintf("%03.0d", $x),-1,1));
1243 }
1244 0 $con->addstr($debugy+2,$debugx," " x 3);
1245 0 ▶ for (my $x = $ox; $x < $dispw+$ox; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $dispw + $ox) 0 0 0
1246 0 $con->addstr("-");
1247 }
1248 }
1249  
1250 #$self->dprint("OXY: $ox, $oy DXY: $dispx,$dispy");
1251
1252 #actual map drawn at user-requested location/virtual window
1253 3 ▶ for (my $y = $oy; $y < ($disph+$oy); ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $disph + $oy) 54 3 57
1254 # x/y is the game map-coord, not drawn location
1255 54 ▶ if ($self->{debugmap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debugmap"}) 0 54 54
1256 0 $con->addstr($y-$oy+$dispy, $debugx, sprintf("%02.0d|", $y));
1257 }
1258 54 ▶ for (my $x = $ox; $x < $dispw+$ox; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $dispw + $ox) 2160 54 2214
1259 2160 ▶ if (my $memtyp = $self->checkmap($vp, $x, $y, $self->{map}->[$x][$y])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $memtyp = $self->checkmap($vp, $x, $y, $self->{'map'}[$x][$y])) { } 181 1979 2160
1260 181 my ($color) = $self->{color}->[$x][$y];
1261 181 ▶ my $sym = ($memtyp == 2) ? $vp->{memory}->{$self->{name}}->[$x][$y] : $self->{map}->[$x][$y] ? $self->{map}->[$x][$y] : ' ';
Branch: 2 branches
BranchTrueFalseTotal
$memtyp == 2 ? : 0 181 181
$self->{'map'}[$x][$y] ? : 181 0 181
1262 181 ▶ $color = $self->{memcolor} if $memtyp == 2 && $self->{memcolor}; # if the area is memorized, then draw as gray
Branch: 1 branch
BranchTrueFalseTotal
if $memtyp == 2 and $self->{"memcolor"} 0 181 181
Condition33%1/3
$memtyp == 2 and $self->{"memcolor"}
!ll&&!rl&&r
18100
MC/DC0%0/2
$memtyp == 2 and $self->{"memcolor"}
$memtyp == 2 $self->{"memcolor"}
Truth table33%1/3
A and B
A= $memtyp == 2B= $self->{"memcolor"}
ABresult
0X0
100
111
1263 181 $con->attrch($color, $y-$oy+$dispy,$x-$ox+$dispx,$sym);
1264 } else {
1265 1979 $con->addch($y-$oy+$dispy,$x-$ox+$dispx,' ');
1266 }
1267 }
1268 }
1269  
1270 # drawitems
1271 3 for my $i (@{$self->{items}}) {
1272 0 $self->drawob($i, $opts, $ox, $oy, $dispx, $dispy);
1273 }
1274  
1275 # drawmobs on top
1276 3 for my $m (@{$self->{mobs}}) {
1277 3 $self->drawob($m, $opts, $ox, $oy, $dispx, $dispy);
1278 }
1279  
1280 3 $con->refresh();
1281 }
1282  
1283 #
1284 # This draws a thing that has a symbol, a color, an x and a y
1285 # It is called by draw() above
1286 #
1287  
1288 3 ▶sub drawob {
Subroutine drawob: called CC=9 SCAR=28.5
Pod undocumented
1289 3 my $self = shift;
1290 3 my ($ob, $opts, $ox, $oy, $xoff, $yoff) = @_;
1291  
1292 3 my $vp = $opts->{vp};
1293 3 my $con = $opts->{con};
1294  
1295 # $ox, $oy must be subtracted to get display coords (relative to display box, don't draw if outside box)
1296 # $xoff, $yoff musy be ADDED to get absolute coords (relative to console box)
1297  
1298 3 ▶ if ($self->checkpov($vp, $ob->{x}, $ob->{y})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->checkpov($vp, $ob->{"x"}, $ob->{"y"})) 3 0 3
1299 3 ▶ if ( (($ob->{y}-$oy) >= 0) && (($ob->{x}-$ox) >= 0) && (($ob->{x}-$ox) < $opts->{dispw}) && (($ob->{y}-$oy) < $opts->{disph}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ob->{"y"} - $oy >= 0 and $ob->{"x"} - $ox >= 0 and $ob->{"x"} - $ox < $opts->{"dispw"} and $ob->{"y"} - $oy < $opts->{"disph"}) 3 0 3
Condition33%3/9
$ob->{"y"} - $oy >= 0 and $ob->{"x"} - $ox >= 0 and $ob->{"x"} - $ox < $opts->{"dispw"} and $ob->{"y"} - $oy < $opts->{"disph"}
!ll&&!rl&&r
003
$ob->{"y"} - $oy >= 0 and $ob->{"x"} - $ox >= 0 and $ob->{"x"} - $ox < $opts->{"dispw"}
!ll&&!rl&&r
003
$ob->{"y"} - $oy >= 0 and $ob->{"x"} - $ox >= 0
!ll&&!rl&&r
003
MC/DC0%0/4
$ob->{"y"} - $oy >= 0 and $ob->{"x"} - $ox >= 0 and $ob->{"x"} - $ox < $opts->{"dispw"} and $ob->{"y"} - $oy < $opts->{"disph"}
$ob->{"y"} - $oy >= 0 $ob->{"x"} - $ox >= 0 $ob->{"x"} - $ox < $opts->{"dispw"} $ob->{"y"} - $oy < $opts->{"disph"}
Truth table20%1/5
A and B and C and D
A= $ob->{"y"} - $oy >= 0B= $ob->{"x"} - $ox >= 0C= $ob->{"x"} - $ox < $opts->{"dispw"}D= $ob->{"y"} - $oy < $opts->{"disph"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1300 3 $con->attrch($ob->{color},$ob->{y}-$oy+$yoff, $ob->{x}-$ox+$xoff, $ob->{sym});
1301 }
1302 #if the object is not the char, and the object is novel then memorize it and set the "saw something new this turn" flag
1303 3 ▶ if ($vp && $ob != $vp && !($vp->{memory}->{$self->{name}}->[$ob->{x}][$ob->{y}] eq $ob->{sym})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vp and $ob != $vp and not $vp->{"memory"}{$self->{"name"}}[$ob->{"x"}][$ob->{"y"}] eq $ob->{"sym"}) 0 3 3
Condition33%2/6
$vp and $ob != $vp and not $vp->{"memory"}{$self->{"name"}}[$ob->{"x"}][$ob->{"y"}] eq $ob->{"sym"}
!ll&&!rl&&r
300
$vp and $ob != $vp
!ll&&!rl&&r
030
MC/DC0%0/3
$vp and $ob != $vp and not $vp->{"memory"}{$self->{"name"}}[$ob->{"x"}][$ob->{"y"}] eq $ob->{"sym"}
$vp $ob != $vp not $vp->{"memory"}{$self->{"name"}}[$ob->{"x"}][$ob->{"y"}] eq $ob->{"sym"}
Truth table25%1/4
A and B and not(C)
A= $vpB= $ob != $vpC= not $vp->{"memory"}{$self->{"name"}}[$ob->{"x"}][$ob->{"y"}] eq $ob->{"sym"}
ABCresult
0XX0
10X0
1100
1111
1304 0 $vp->{memory}->{$self->{name}}->[$ob->{x}][$ob->{y}] = $ob->{sym};
1305 0 $vp->{sawnew} = 1;
1306 }
1307 3 return 1;
1308 }
1309 }
1310  
1311  
1312 # These can be easily optimized also storing items/mobs at {m-items}[x][y] and {m-mobs}[x][y]
1313 # But a list approach is simpler for now, and reduces some overhead on sets
1314  
1315 =item mobat (x, y)
1316  
1317 Returns a single mob located at x/y, or undef if none is there.
1318  
1319 =cut
1320  
1321 2767 ▶sub mobat {
Subroutine mobat: called CC=4 SCAR=14.6
Pod documented
1322 2767 my $self = shift;
1323 2767 my ($x, $y) = @_;
1324 #$self->dprint("mobat $x, $y");
1325 2767 my @r;
1326 2767 for my $m (@{$self->{mobs}}) {
1327 1 ▶ return $m if ($m->{x} == $x) && ($m->{y} == $y);
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"x"} == $x and $m->{"y"} == $y 1 0 1
Condition33%1/3
$m->{"x"} == $x and $m->{"y"} == $y
!ll&&!rl&&r
001
MC/DC0%0/2
$m->{"x"} == $x and $m->{"y"} == $y
$m->{"x"} == $x $m->{"y"} == $y
Truth table33%1/3
A and B
A= $m->{"x"} == $xB= $m->{"y"} == $y
ABresult
0X0
100
111
1328 }
1329 }
1330  
1331 =item items ([x, y])
1332  
1333 Returns reference to array of items located at x/y, or reference to array of all items if no x/y is supplied.
1334  
1335 =cut
1336  
1337 0 ▶sub items {
Subroutine items: not called CC=5 SCAR=34.0
Pod documented
1338 0 my $self = shift;
1339 0 ▶ if (!@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_) { } 0 0 0
1340 return $self->{items}
1341 0 } else {
1342 0 my ($x, $y) = @_;
1343 0 my @r;
1344 0 for my $i (@{$self->{items}}) {
1345 0 ▶ push @r, $i if ($i->{x} == $x) && ($i->{y} == $y);
Branch: 1 branch
BranchTrueFalseTotal
if $i->{"x"} == $x and $i->{"y"} == $y 0 0 0
Condition0%0/3
$i->{"x"} == $x and $i->{"y"} == $y
!ll&&!rl&&r
000
MC/DC0%0/2
$i->{"x"} == $x and $i->{"y"} == $y
$i->{"x"} == $x $i->{"y"} == $y
Truth table0%0/3
A and B
A= $i->{"x"} == $xB= $i->{"y"} == $y
ABresult
0X0
100
111
1346 }
1347 0 return \@r;
1348 }
1349 }
1350  
1351 =item mobs ([x, y])
1352  
1353 Returns reference to array of all mobs located at x/y, or all mobs if no x/y is supplied.
1354  
1355 =cut
1356  
1357 0 ▶sub mobs {
Subroutine mobs: not called CC=5 SCAR=34.0
Pod documented
1358 0 my $self = shift;
1359 0 ▶ if (!@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_) { } 0 0 0
1360 return $self->{mobs}
1361 0 } else {
1362 0 my ($x, $y) = @_;
1363 0 my @r;
1364 0 for my $m (@{$self->{mobs}}) {
1365 0 ▶ push @r, $m if ($m->{x} == $x) && ($m->{y} == $y);
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"x"} == $x and $m->{"y"} == $y 0 0 0
Condition0%0/3
$m->{"x"} == $x and $m->{"y"} == $y
!ll&&!rl&&r
000
MC/DC0%0/2
$m->{"x"} == $x and $m->{"y"} == $y
$m->{"x"} == $x $m->{"y"} == $y
Truth table0%0/3
A and B
A= $m->{"x"} == $xB= $m->{"y"} == $y
ABresult
0X0
100
111
1366 }
1367 0 return \@r;
1368 }
1369 }
1370  
1371 =item checkpov (vp, x, y)
1372  
1373 Returns 1 if the $vp object (something that has an x, a y and a pov) can see x/y.
1374  
1375 Uses the "noview" area variable to determine what can be seen through.
1376  
1377 =cut
1378  
1379 # this is used to show monster at the current location
1380 2171 ▶sub checkpov {
Subroutine checkpov: called CC=32 SCAR=35.9
Pod documented
1381 2171 my $self = shift;
1382 2171 my ($vp, $x, $y) = @_;
1383 2171 ▶ return 1 if (!$vp); # no viewpoint, draw everything
Branch: 1 branch
BranchTrueFalseTotal
unless $vp 0 2171 2171
1384 2171 ▶ return 1 if ($vp->{pov}<0 || !defined($vp->{pov})); # see all
Branch: 1 branch
BranchTrueFalseTotal
if $vp->{"pov"} < 0 or not defined $vp->{"pov"} 0 2171 2171
Condition33%1/3
$vp->{"pov"} < 0 or not defined $vp->{"pov"}
l!l&&r!l&&!r
002171
MC/DC0%0/2
$vp->{"pov"} < 0 or not defined $vp->{"pov"}
$vp->{"pov"} < 0 not defined $vp->{"pov"}
Truth table33%1/3
A or not(B)
A= $vp->{"pov"} < 0B= not defined $vp->{"pov"}
ABresult
1X1
011
000
1385 2171 ▶ return 0 if ($vp->{pov}==0); # blind
Branch: 1 branch
BranchTrueFalseTotal
if $vp->{"pov"} == 0 0 2171 2171
1386 2171 my $vx = $vp->{x};
1387 2171 my $vy = $vp->{y};
1388  
1389 2171 my $dist = distance($vx, $vy, $x, $y);
1390
1391 2171 ▶ return 0 unless $dist <= $vp->{pov};
Branch: 1 branch
BranchTrueFalseTotal
unless $dist <= $vp->{"pov"} 1683 488 2171
1392  
1393 488 ▶ return 1 if $dist <= 1; # always see close
Branch: 1 branch
BranchTrueFalseTotal
if $dist <= 1 18 470 488
1394  
1395 470 ▶ print "---FOV2: $vx, $vy, $x, $y D:$dist\n" if $self->{debugfov};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debugfov"} 0 470 470
1396 470 ▶ if ($OKINLINEPOV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Games::Roguelike::Area::OKINLINEPOV) 0 470 470
1397 0 ▶ print "using inline pov\n" if $self->{debugfov};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debugfov"} 0 0 0
1398 0 ▶ return checkpov_c($vx, $vy, $x, $y, $self->{map}, $self->{noview}, $self->{debugfov} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'debugfov'} ? : 0 0 0
1399 }
1400  
1401 # here's where we need to actually do some field of view calculations
1402  
1403 470 my $dx = $x-$vx;
1404 470 my $dy = $y-$vy;
1405  
1406 # trace 4 parallel rays from corner to corner
1407 # without cosines!
1408 # this code allows diagonal blocking pillars
1409  
1410 470 my @ok = (1,1,1,1);
1411 470 ▶ for (my $i = 1; $i <= $dist; $i+=0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $dist) 2365 169 2534
1412 2365 my $tx = $vx+($i/$dist)*$dx; # delta-fraction of distance
1413 2365 my $ty = $vy+($i/$dist)*$dy;
1414  
1415 2365 my (@x, @y);
1416 2365 $x[0] = (0.1+$tx); # not quite the corners
1417 2365 $y[0] = (0.1+$ty);
1418 2365 $x[1] = (0.9+$tx);
1419 2365 $y[1] = (0.9+$ty);
1420 2365 $x[2] = (0.9+$tx);
1421 2365 $y[2] = (0.1+$ty);
1422 2365 $x[3] = (0.1+$tx);
1423 2365 $y[3] = (0.9+$ty);
1424  
1425 2365 my $ok = 0;
1426 2365 ▶ for (my $j = 0; $j < 4; ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 4) 9460 2365 11825
1427 9460 ▶ next if !$ok[$j];
Branch: 1 branch
BranchTrueFalseTotal
unless $ok[$j] 2326 7134 9460
1428 7134 ▶ if (int($x[$j]) eq $x && int($y[$j]) eq $y) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $x[$j] eq $x and int $y[$j] eq $y) 433 6701 7134
Condition100%3/3
int $x[$j] eq $x and int $y[$j] eq $y
!ll&&!rl&&r
56791022433
MC/DC100%2/2
int $x[$j] eq $x and int $y[$j] eq $y
int $x[$j] eq $x int $y[$j] eq $y
Truth table100%3/3
A and B
A= int $x[$j] eq $xB= int $y[$j] eq $y
ABresult
0X0
100
111
1429 433 ▶ print "$i: sub $j: $x[$j],$y[$j] SAME ($self->{map}->[$x[$j]][$y[$j]])\n" if $self->{debugfov};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debugfov"} 0 433 433
1430 433 next;
1431 }
1432 6701 ▶ if ($dx != 0 && $dy != 0 && (abs($dx/$dy) > 0.1) && (abs($dy/$dx) > 0.1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dx != 0 and $dy != 0 and abs $dx / $dy > "0.1" and abs $dy / $dx > "0.1") 5693 1008 6701
Condition77%7/9
$dx != 0 and $dy != 0 and abs $dx / $dy > "0.1" and abs $dy / $dx > "0.1"
!ll&&!rl&&r
100805693
$dx != 0 and $dy != 0 and abs $dx / $dy > "0.1"
!ll&&!rl&&r
100805693
$dx != 0 and $dy != 0
!ll&&!rl&&r
5124965693
MC/DC50%2/4
$dx != 0 and $dy != 0 and abs $dx / $dy > "0.1" and abs $dy / $dx > "0.1"
$dx != 0 $dy != 0 abs $dx / $dy > "0.1" abs $dy / $dx > "0.1"
Truth table60%3/5
A and B and C and D
A= $dx != 0B= $dy != 0C= abs $dx / $dy > "0.1"D= abs $dy / $dx > "0.1"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1433 # allow peeking around corners if target is near the edge
1434 5693 ▶ if (round($x[$j]) eq $x && round($y[$j]) eq $y && $i >= ($dist -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (round($x[$j]) eq $x and round($y[$j]) eq $y and $i >= $dist - 1) 59 5634 5693
Condition100%6/6
round($x[$j]) eq $x and round($y[$j]) eq $y and $i >= $dist - 1
!ll&&!rl&&r
56082659
round($x[$j]) eq $x and round($y[$j]) eq $y
!ll&&!rl&&r
510750185
MC/DC100%3/3
round($x[$j]) eq $x and round($y[$j]) eq $y and $i >= $dist - 1
round($x[$j]) eq $x round($y[$j]) eq $y $i >= $dist - 1
Truth table100%4/4
A and B and C
A= round($x[$j]) eq $xB= round($y[$j]) eq $yC= $i >= $dist - 1
ABCresult
0XX0
10X0
1100
1111
1435 59 ▶ print "$i: sub $j: $x[$j],$y[$j] PEEK ($self->{map}->[$x[$j]][$y[$j]])\n" if $self->{debugfov};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debugfov"} 0 59 59
1436 59 next;
1437 }
1438 }
1439 6642 ▶ if (($self->{map}->[$x[$j]][$y[$j]] =~ /^(#|\+)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'map'}[$x[$j]][$y[$j]] =~ /^(#|\+)$/) { } 1424 5218 6642
1440 1424 $ok[$j] = 0;
1441 1424 ▶ print "$i: sub $j: $x[$j],$y[$j] WALL ($self->{map}->[$x[$j]][$y[$j]])\n" if $self->{debugfov};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debugfov"} 0 1424 1424
1442 } else {
1443 5218 ▶ print "$i: sub $j: $x[$j],$y[$j] OK ($self->{map}->[$x[$j]][$y[$j]])\n" if $self->{debugfov};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debugfov"} 0 5218 5218
1444 }
1445 }
1446 2365 ▶ return 0 if !$ok[0] && !$ok[1] && !$ok[2] && !$ok[3];
Branch: 1 branch
BranchTrueFalseTotal
unless $ok[0] or $ok[1] or $ok[2] or $ok[3] 301 2064 2365
Condition100%9/9
$ok[0] or $ok[1] or $ok[2] or $ok[3]
l!l&&r!l&&!r
199668301
$ok[0] or $ok[1] or $ok[2]
l!l&&r!l&&!r
1878118369
$ok[0] or $ok[1]
l!l&&r!l&&!r
1598280487
MC/DC100%4/4
$ok[0] or $ok[1] or $ok[2] or $ok[3]
$ok[0] $ok[1] $ok[2] $ok[3]
Truth table100%5/5
A or B or C or D
A= $ok[0]B= $ok[1]C= $ok[2]D= $ok[3]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1447 }
1448 169 return 1;
1449 }
1450  
1451 =item checkmap (vp, x, y, sym)
1452  
1453 Returns 1 if the $vp mob can see x/y, 2 if $vp has a memory of x/y,
1454 and also memorizes x/y if it can be seen by adding it to the vp's "memory" variable..
1455  
1456 Uses the "noview" area variable to determine what can be seen through.
1457  
1458 =cut
1459  
1460 # this is used to show the map
1461 2160 ▶sub checkmap {
Subroutine checkmap: called CC=5 SCAR=17.1
Pod documented
1462 2160 my $self = shift;
1463 2160 my ($vp, $x, $y, $sym) = @_;
1464 2160 ▶ if ($vp && $vp->{hasmem}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vp and $vp->{'hasmem'}) { } 2160 0 2160
Condition33%1/3
$vp and $vp->{"hasmem"}
!ll&&!rl&&r
002160
MC/DC0%0/2
$vp and $vp->{"hasmem"}
$vp $vp->{"hasmem"}
Truth table33%1/3
A and B
A= $vpB= $vp->{"hasmem"}
ABresult
0X0
100
111
1465 2160 ▶ if ($self->checkpov($vp, $x, $y)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->checkpov($vp, $x, $y)) 181 1979 2160
1466 181 $vp->{memory}->{$self->{name}}->[$x][$y]=$sym;
1467 181 return 1;
1468 }
1469 # $self->dprint("mem $self->{name}: $x,$y") if $vp->{memory}->{$self->{name}}->[$x][$y];
1470 1979 ▶ return 2 if $vp->{memory}->{$self->{name}}->[$x][$y];
Branch: 1 branch
BranchTrueFalseTotal
if $vp->{"memory"}{$self->{"name"}}[$x][$y] 0 1979 1979
1471 1979 return 0;
1472 } else {
1473 0 return $self->checkpov($vp, $x, $y);
1474 }
1475 }
1476  
1477 =item addmob (mob)
1478  
1479 Adds a mob to the area, unless it's already in it.
1480  
1481 =cut
1482  
1483 16 ▶sub addmob {
Subroutine addmob: called CC=3 SCAR=11.0
Pod documented
1484 16 my $self = shift;
1485 16 my $m = shift;
1486 16 for (@{$self->{mobs}}) {
1487 1 ▶ return 0 if $_ eq $m;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $m 0 1 1
1488 }
1489 16 push @{$self->{mobs}}, $m;
1490 16 return $m;
1491 }
1492  
1493 =item delmob (mob)
1494  
1495 Removes mob from the area.
1496  
1497 =cut
1498  
1499 1 ▶sub delmob {
Subroutine delmob: called CC=3 SCAR=11.0
Pod documented
1500 1 my $self = shift;
1501 1 my $m = shift;
1502 1 my $i = 0;
1503 1 for (@{$self->{mobs}}) {
1504 1 ▶ splice @{$self->{mobs}}, $i, 1 if $_ == $m;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $m 1 0 1
1505 1 ++$i;
1506 }
1507 }
1508  
1509 =item findrandmap (symbol[, mobok=0])
1510  
1511 Finds a random map location containing symbol (if mobok is not set, then it won't returns locations that have mobs).
1512  
1513 =cut
1514  
1515 6 ▶sub findrandmap {
Subroutine findrandmap: called CC=7 SCAR=19.6
Pod documented
1516 6 my $self = shift;
1517 6 my $sym = shift;
1518 6 my $mobok = shift;
1519  
1520 6 my $index = [];
1521  
1522 6 ▶ for (my $x = 0; $x < $self->{w}; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"w"}) 407 6 413
1523 407 ▶ for (my $y = 0; $y < $self->{h}; ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $self->{"h"}) 17621 407 18028
1524 2766 push @{$index}, [$x, $y]
1525 if defined($self->{map}->[$x][$y])
1526 17621 ▶ && ($self->{map}->[$x][$y] eq $sym && ($mobok || !$self->mobat($x,$y)));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"map"}[$x][$y] and ($self->{"map"}[$x][$y] eq $sym and $mobok || !$self->mobat($x, $y)) 2766 14855 17621
Condition66%6/9
defined $self->{"map"}[$x][$y] and ($self->{"map"}[$x][$y] eq $sym and $mobok || !$self->mobat($x, $y))
!ll&&!rl&&r
1184930062766
$self->{"map"}[$x][$y] eq $sym and $mobok || !$self->mobat($x, $y)
!ll&&!rl&&r
300602766
$mobok or not $self->mobat($x, $y)
l!l&&r!l&&!r
027660
MC/DC50%2/4
defined $self->{"map"}[$x][$y] and ($self->{"map"}[$x][$y] eq $sym and $mobok || !$self->mobat($x, $y))
defined $self->{"map"}[$x][$y] $self->{"map"}[$x][$y] eq $sym $mobok not $self->mobat($x, $y)
Truth table60%3/5
A and B and C or not(D)
A= defined $self->{"map"}[$x][$y]B= $self->{"map"}[$x][$y] eq $symC= $mobokD= not $self->mobat($x, $y)
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1527 }
1528 }
1529 6 $self->{index}->{$sym} = $index;
1530  
1531 6 my $i = int(rand() * scalar(@{$index}));
1532  
1533 6 return $index->[$i]->[0], $index->[$i]->[1];
1534 }
1535  
1536 =item dump
1537  
1538 Prints map to stdout, without mobs and items. For a more flexible approach, create a ::Console::Dump object and call draw() using it.
1539  
1540 =cut
1541  
1542 0 ▶sub dump {
Subroutine dump: not called CC=13 SCAR=52.0
Pod documented
1543 0 my $self = shift;
1544  
1545 0 my $ox = 0;
1546 0 my $oy = 0;
1547  
1548 0 my ($xx, $xy, $mx, $my) = (0, 0, $self->{w}, $self->{h});
1549 0 ▶ for (my $y = 0; $y < $self->{h}; ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $self->{"h"}) 0 0 0
1550 0 ▶ for (my $x = 0; $x < $self->{w}; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"w"}) 0 0 0
1551 0 ▶ if ($self->{map}->[$x][$y]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"map"}[$x][$y]) 0 0 0
1552 0 ▶ $mx = $x if ($x < $mx);
Branch: 1 branch
BranchTrueFalseTotal
if $x < $mx 0 0 0
1553 0 ▶ $my = $y if ($y < $my);
Branch: 1 branch
BranchTrueFalseTotal
if $y < $my 0 0 0
1554 0 ▶ $xx = $x if ($x > $xx);
Branch: 1 branch
BranchTrueFalseTotal
if $x > $xx 0 0 0
1555 0 ▶ $xy = $y if ($y > $xy);
Branch: 1 branch
BranchTrueFalseTotal
if $y > $xy 0 0 0
1556 }
1557 }
1558 }
1559  
1560 0 $ox=max($ox, $mx);
1561 0 $oy=max($oy, $my);
1562  
1563 #actual map drawn at user-requested location/virtual window
1564 0 ▶ for (my $y = $oy; $y < $self->{h} && $y <= $xy; ++$y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < $self->{"h"} and $y <= $xy) 0 0 0
Condition0%0/3
$y < $self->{"h"} and $y <= $xy
!ll&&!rl&&r
000
MC/DC0%0/2
$y < $self->{"h"} and $y <= $xy
$y < $self->{"h"} $y <= $xy
Truth table0%0/3
A and B
A= $y < $self->{"h"}B= $y <= $xy
ABresult
0X0
100
111
1565 0 ▶ for (my $x = $ox; $x < $self->{w} && $x <= $xx; ++$x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"w"} and $x <= $xx) 0 0 0
Condition0%0/3
$x < $self->{"w"} and $x <= $xx
!ll&&!rl&&r
000
MC/DC0%0/2
$x < $self->{"w"} and $x <= $xx
$x < $self->{"w"} $x <= $xx
Truth table0%0/3
A and B
A= $x < $self->{"w"}B= $x <= $xx
ABresult
0X0
100
111
1566 0 ▶ print $self->{map}->[$x][$y] ? $self->{map}->[$x][$y] : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'map'}[$x][$y] ? : 0 0 0
1567 }
1568 0 print "\n";
1569 }
1570 }
1571  
1572 =item additem (item)
1573  
1574 Adds item to floor. Override this to add floor full messages, etc.
1575  
1576 Return value 0 = can't add, too full
1577 Return value 1 = add ok
1578 Return value -1 = move occured, but not added
1579  
1580 If the item doesn't have an {x} and {y} value then the item is added to a random location.
1581  
1582 =cut
1583  
1584 1 ▶sub additem {
Subroutine additem: called CC=3 SCAR=11.8
Pod documented
1585 1 my $self = shift;
1586 1 my $item = shift;
1587 1 ▶ if ($item->setcont($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->setcont($self)) 1 0 1
1588 1 ▶ if (!defined($item->{x})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item->{"x"}) 0 1 1
1589 0 ($item->{x}, $item->{y}) = $self->findrandmap($self->{fsym}, 0, 1);
1590 }
1591 }
1592 1 return 1; # i'm never full
1593 }
1594  
1595 =item delitem (item)
1596  
1597 Removes item from the area.
1598  
1599 =cut
1600  
1601 0 ▶sub delitem {
Subroutine delitem: not called CC=3 SCAR=24.8
Pod documented
1602 0 my $self = shift;
1603 0 my $ob = shift;
1604 0 my $i = 0;
1605 0 for (@{$self->{items}}) {
1606 0 ▶ splice @{$self->{items}}, $i, 1 if $_ == $ob;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $ob 0 0 0
1607 0 ++$i;
1608 }
1609 }
1610  
1611 =item load (file | options)
1612  
1613 Loads an area from a file, which is a perl program that sets these vars:
1614  
1615 $map : 2d map as one big string
1616 or $yxarray : 2d map as y then x indexed array
1617 %key : for each symbol in the map *optionally* provide:
1618 color - color of that symbol
1619 sym - real symbol to use
1620 feature - name of feature for feature table, don't specify with class!
1621 class - optional package to use for "new", passed the area itself as the first argument to new
1622 lib - look up item or mob from library
1623  
1624 %lib : hash of hashes, used to populate items or monsters - as needed
1625  
1626 Alternatively, these can be passed as named options to the load function.
1627  
1628 The map system knows very little about game semantics. It's merely a way of loading maps
1629 made of symbols - some of which may correlate to perl objects. The tictactoe example script
1630 uses the map load system.
1631  
1632 '>', and '<' are assumed to be "stair features" unless otherwise specified.
1633  
1634 Objects can be looked up by name from the item library instead of specified in full.
1635  
1636 At a minimum, classes must add themselves, somehow, to the area object when new is called.
1637  
1638 The example below loads a standard map, with blue doors, 2 mobs and 1 item
1639  
1640 One mob is loaded via a package "mymonster", and is passed "hd" & "name" parameters,
1641 in addition to the "x", "y" and "sym" parameters which are derived from its location.
1642  
1643 The other mob is loaded from the library named "blue dragon", and has it's "name" and "hp"
1644 parameters modified.
1645  
1646 About lib entries:
1647  
1648 If a key entry has a "lib", it's assumed to the be the name of an entry in the lib hash.
1649  
1650 The lib hash is then looked up and has its values copied into the key entry before using the key entry.
1651  
1652 "lib" entries can be recursive.
1653  
1654 The "lib" can be loaded from an external shared file, so multiple maps can use the same "lib".
1655  
1656 About items:
1657  
1658 The "items" member of an object (mob or backpack), if it is an array reference, will be auto-expanded
1659 by creating an item object for each array member with the parent object set as the first, unnamed, argument to new.
1660  
1661 If a member of the items array is a hash ref, it's treated like a key entry. If it's a scalar string, it's
1662 equivalent to {lib=>'string'}.
1663  
1664 If there's no class set within an item, a warning is emitted.
1665  
1666 EXAMPLE 1:
1667  
1668 $map = '
1669 ##########
1670 #k <+ ! #
1671 ######## #
1672 #> D #
1673 ##########
1674 ';
1675  
1676 %key = (
1677 'k'=>{class=>'mymonster', type='kobold', name=>'Harvey', hd=>12,
1678 items=>['potion of healing',
1679 {class=>'myweapon', name=>'Blue Sword', hd=>9, dd=>4, drain=>1, glow=>1}
1680 ]
1681 },
1682 '!'=>{lib=>'potion of speed'},
1683 'D'=>{lib=>'blue dragon', name=>'Charlie', hp=>209},
1684 '+'=>{color=>'blue'}
1685 );
1686  
1687 %lib = (
1688 'potion of speed'=>{class=>'myitem', type=>'potion', effect=>'speed', power=>1},
1689 'blue dragon'=>{class=>'mymob', type=>'dragon', breath=>'lightning', hp=>180, hd=>12, at=>[10,5], dm=>[5,10], speed=>5, loot=>4},
1690 );
1691  
1692 $area->load(map=>$map, key=>%key, lib=>\%lib);
1693  
1694 EXAMPLE 2:
1695
1696 use Games::Roguelike::Caves;
1697 my $yx = generate_cave($r->{w},$r->{h}, 12, .46, '#', '.');
1698 $area->load(yxarray=>$yx);
1699  
1700 =cut
1701  
1702 2 ▶sub load {
Subroutine load: called CC=21 SCAR=31.9
Pod documented
1703 2 my $self = shift;
1704  
1705 2 ▶ confess("cannot call load without a filename or a map/key and lib")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
1706 if (!@_);
1707  
1708 2 my $map;
1709 my %key;
1710 2 my %lib;
1711  
1712 2 my ($fn);
1713 2 my %opts;
1714  
1715 2 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 2 2
1716 0 ($fn) = @_;
1717 } else {
1718 2 %opts = @_;
1719 2 $fn = $opts{file};
1720 }
1721  
1722 2 ▶ if ($fn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn) { } 0 2 2
1723 0 eval {
1724 5 ▶ use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
1725 0 my $in = new Safe;
1726 0 $in->permit_only(':base_core');
1727 0 $in->rdo($fn);
1728 0 $map = $in->reval('$map');
1729 0 %key = $in->reval('%key');
1730 0 %lib = $in->reval('%lib');
1731 };
1732 } else {
1733 2 $map = $opts{map};
1734 2 ▶ %key = %{$opts{key}} if $opts{key};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"key"} 1 1 2
1735 2 ▶ %lib = %{$opts{lib}} if $opts{lib};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"lib"} 1 1 2
1736 }
1737  
1738 2 my $mapyx;
1739  
1740 2 ▶ if ($opts{yxarray} && ref($opts{yxarray})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'yxarray'} and ref $opts{'yxarray'}) { } 0 2 2
Condition33%1/3
$opts{"yxarray"} and ref $opts{"yxarray"}
!ll&&!rl&&r
200
MC/DC0%0/2
$opts{"yxarray"} and ref $opts{"yxarray"}
$opts{"yxarray"} ref $opts{"yxarray"}
Truth table33%1/3
A and B
A= $opts{"yxarray"}B= ref $opts{"yxarray"}
ABresult
0X0
100
111
1741 0 $mapyx = $opts{yxarray};
1742 } else {
1743 2 ▶ if (!$map) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($map) 0 2 2
1744 0 cluck("no 'map' or 'xyarray' found in parameters");
1745 0 return 0;
1746 }
1747 2 $map =~ s/^[\r\n]+//;
1748 2 $map =~ s/[\r\n]+$//;
1749 2 my @ylines = split(/[\r\n]/,$map);
1750 2 my $y = 0;
1751 2 for (@ylines) {
1752 8 my @l = split(//, $_);
1753 8 $mapyx->[$y++]= \@l;
1754 }
1755 }
1756  
1757 2 $self->{map} = [];
1758 2 $self->{color} = [];
1759  
1760 2 my $y = 0;
1761 2 for (@{$mapyx}) {
1762 8 my $x = 0;
1763 8 for (@{$mapyx->[$y]}) {
1764 71 my $sym = $mapyx->[$y][$x];
1765 71 expandkey($sym, \%key, \%lib);
1766 71 my $opt = $key{$sym};
1767 71 ▶ if ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt) { } 4 67 71
1768 4 ▶ if ($opt->{sym}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"sym"}) 2 2 4
1769 2 $sym = $self->{map}->[$x][$y] = $opt->{sym};
1770 }
1771 4 ▶ if ($opt->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'class'}) { } 3 1 4
1772 3 $self->{map}->[$x][$y] = $self->{fsym};
1773 3 my $ob;
1774 3 my ($cpack) = caller;
1775 3 eval {$ob = $opt->{class}->new($self, x=>$x, y=>$y, sym=>$sym, %{$opt});};
1776 3 ▶ carp "failed to create $opt->{class}: $@" if !$ob;
Branch: 1 branch
BranchTrueFalseTotal
unless $ob 0 3 3
1777 3 ▶ if (ref($opt->{items}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt->{"items"} eq "ARRAY") 1 2 3
1778 1 for(@{$opt->{items}}) {
1779 3 ▶ if (!ref($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_) { } 1 2 3
1780 1 expandkey($_, \%lib, \%lib);
1781 1 $_ = $lib{$_};
1782 } else {
1783 2 expandhash($_, \%lib);
1784 }
1785 3 my $it;
1786 3 eval {$it = $_->{class}->new($ob, %{$_});};
1787 3 ▶ carp "failed to create $_->{class}: $@" if !$ob;
Branch: 1 branch
BranchTrueFalseTotal
unless $ob 0 3 3
1788 }
1789 }
1790 } else {
1791 1 ▶ if ($opt->{feature}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"feature"}) 0 1 1
1792 0 push @{$self->{f}}, [$x, $y];
1793 }
1794 1 $self->{map}->[$x][$y] = $sym;
1795 1 $self->{color}->[$x][$y] = $opt->{color};
1796 }
1797 } else {
1798 67 $self->{map}->[$x][$y] = $mapyx->[$y][$x];
1799 }
1800 71 $x++;
1801 }
1802 8 $y++;
1803 }
1804  
1805 2 $self->{w} = @{$self->{map}};
1806 2 $self->{h} = @{$self->{map}->[0]};
1807 }
1808  
1809 # this looks in the hash "key" for an entry called "lib", which should be a string
1810 # it then looks in the hash "lib" for that string
1811 # finally it copied the keys from "lib" to the hash "key"
1812 # really no reason for 2 hashes... 1 would suffice (for map keys and lib entries),
1813 # but i think it's easier to keep track of for the users that way
1814  
1815 9 ▶sub expandhash {
Subroutine expandhash: called CC=7 SCAR=20.0
Pod undocumented
1816 9 my ($hash, $lib) = @_;
1817  
1818 9 ▶ return if !$hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $hash 0 9 9
1819 9 ▶ return if $hash->{__lib__};
Branch: 1 branch
BranchTrueFalseTotal
if $hash->{"__lib__"} 0 9 9
1820 9 ▶ return if !(my $libname = $hash->{lib});
Branch: 1 branch
BranchTrueFalseTotal
unless my $libname = $hash->{"lib"} 7 2 9
1821  
1822 croak "no entry for '$libname'"
1823 2 ▶ if !$lib->{$libname};
Branch: 1 branch
BranchTrueFalseTotal
unless $lib->{$libname} 0 2 2
1824  
1825 # allow recursion
1826 2 expandhash($lib, $lib);
1827  
1828 2 for (keys(%{$lib->{$libname}})) {
1829 15 ▶ next if $_ eq 'lib';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "lib" 0 15 15
1830 15 $hash->{$_}=$lib->{$libname}->{$_};
1831 }
1832  
1833 }
1834  
1835 72 ▶sub expandkey {
Subroutine expandkey: called CC=2 SCAR=6.9
Pod undocumented
1836 72 my ($index, $key, $lib) = @_;
1837 72 ▶ return if !$key->{$index};
Branch: 1 branch
BranchTrueFalseTotal
unless $key->{$index} 67 5 72
1838 5 expandhash($key->{$index}, $lib);
1839 }
1840  
1841  
1842 0 ▶sub dprint {
Subroutine dprint: not called CC=2 SCAR=17.9
Pod undocumented
1843 0 my $self=shift;
1844 0 ▶ if ($self->{world}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'world'}) { } 0 0 0
1845 0 $self->{world}->dprint(@_)
1846 } else {
1847 0 print @_, "\n";
1848 }
1849 }
1850  
1851 =back
1852  
1853 =cut
1854  
1855 # this allows the "generate" function to work and support the old interface
1856  
1857 package Games::Roguelike::Area::Rooms;
1858 5 ▶use base 'Games::Roguelike::Area';
Subroutine BEGIN: called CC=1 SCAR=0.0
1859  
1860 0 ▶sub generate {
Subroutine generate: not called CC=1 SCAR=6.9
1861 0 Games::Roguelike::Area::genmaze1(@_);
1862 }
1863  
1864 package Games::Roguelike::Area::Cavelike;
1865 5 ▶use base 'Games::Roguelike::Area';
Subroutine BEGIN: called CC=1 SCAR=0.0
1866  
1867 2 ▶sub generate {
Subroutine generate: called CC=1 SCAR=0.0
1868 2 Games::Roguelike::Area::genmaze2(@_);
1869 }
1870  
1871 package Games::Roguelike::Area::Maze;
1872 5 ▶use base 'Games::Roguelike::Area';
Subroutine BEGIN: called CC=1 SCAR=0.0
1873  
1874 0 ▶sub generate {
Subroutine generate: not called CC=1 SCAR=6.9
1875 0 Games::Roguelike::Area::genmaze3(@_);
1876 }
1877  
1878 =head1 SEE ALSO
1879  
1880 L<Games::Roguelike::World>, L<Games::Roguelike::Mob>, L<Games::Roguelike::Console>
1881  
1882 =head1 AUTHOR
1883  
1884 Erik Aronesty C<earonesty@cpan.org>
1885  
1886 =head1 LICENSE
1887  
1888 This program is free software; you can redistribute it and/or
1889 modify it under the same terms as Perl itself.
1890  
1891 See L<http://www.perl.com/perl/misc/Artistic.html> or the included LICENSE file.
1892  
1893 =cut
1894  
1895 5 1;
1896  
« Mob.pm ↑ Summary Area/Wilderness.pm »