blib/lib/Games/RolePlay/MapGen.pm

filter: Statementstmt 75.6% 137 / 181 Branchbran 56.5% 52 / 92 Conditioncond 72.2% 13 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 77.7% 14 / 18 Podpod 9.0% 1 / 11 totaltotal 67.2% 222 / 330 CC 59 SCAR 23.6
CC
59
Cov
69%
sub_map
30.0
generate
15.6
_check_mod_path
12.0
CRAP 10.6
« blib/lib/Games/RolePlay/MapGen/GeneratorPlugin/BasicDoors.pm ↑ Summary blib/lib/Games/RolePlay/MapGen/Exporter/XML.pm »
1 # vi:tw=0 syntax=perl:
2  
3 package Games::RolePlay::MapGen;
4  
5 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 our $VERSION = '1.5008';
10  
11 22 our $AUTOLOAD;
12  
13 22 our %opp = (n=>"s", e=>"w", s=>"n", w=>"e");
14 22 our %full = (n=>"north", e=>"east", s=>"south", w=>"west");
15  
16 # known_opts {{{
17 22 our %known_opts = (
18 generator => "Basic",
19 exporter => "Text",
20 bounding_box => "50x50",
21 tile_size => 10,
22 cell_size => "20x20",
23  
24 nocolor => 0, # for the text map generator
25  
26 num_rooms => "1d4+1",
27 min_room_size => "2x2",
28 max_room_size => "7x7",
29  
30 sparseness => 10,
31 same_way_percent => 90,
32 same_node_percent => 30,
33 remove_deadend_percent => 60,
34 );
35 # }}}
36  
37 # _check_mod_path {{{
38 0 ▶sub _check_mod_path {
Subroutine _check_mod_path: not called CC=3 SCAR=24.8
39 0 my $this = shift;
40 0 my $omod = shift;
41 0 $omod =~ s/\:\:/\//g;
42  
43 0 for my $mod ($omod, map {$_ . "::$omod"} "Games::RolePlay::MapGen::Generator", "Games::RolePlay::MapGen::GeneratorPlugin",
44 "Games::RolePlay::MapGen::Exporter", "Games::RolePlay::MapGen::ExporterPlugin") {
45  
46 0 ▶ return $mod if eval "require $mod";
Branch: 1 branch
BranchTrueFalseTotal
if eval "require $mod" 0 0 0
47 }
48  
49 0 return;
50 }
51 # }}}
52 # _check_opts {{{
53 33 ▶sub _check_opts {
Subroutine _check_opts: called CC=9 SCAR=22.0
54 33 my $this = shift;
55 33 my @e = ();
56  
57 # warn "checking known_opts";
58 33 for my $k (keys %known_opts) {
59 485 ▶ "set_$k"->($this, $known_opts{$k} ) unless defined $this->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{$k} 257 228 485
60 }
61  
62 33 for my $k ( keys %$this ) {
63 552 ▶ unless( exists $known_opts{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Games::RolePlay::MapGen::known_opts{$k}) 67 485 552
64 67 ▶ next if $k eq "objs";
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "objs" 33 34 67
65 34 ▶ next if $k eq "_the_map";
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "_the_map" 5 29 34
66 29 ▶ next if $k eq "_the_groups";
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "_the_groups" 5 24 29
67  
68 24 push @e, "unrecognized option: '$k'";
69 }
70 }
71  
72 33 ▶ return "ERROR:\n\t" . join("\n\t", @e) . "\n" if @e;
Branch: 1 branch
BranchTrueFalseTotal
if @e 14 19 33
73 19 return;
74 }
75 # }}}
76  
77 # AUTOLOAD {{{
78 284 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=10 SCAR=23.1
79 284 my $this = shift;
80 284 my $sub = $AUTOLOAD;
81  
82 # sub set_generator
83 # sub set_exporter
84  
85 284 ▶ if( $sub =~ m/MapGen\:\:set_(generator|exporter)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sub =~ /MapGen\:\:set_(generator|exporter)$/u) { } 55 229 284
elsif ($sub =~ /MapGen\:\:add_(generator|exporter)_plugin$/u) { } 7 222 229
elsif ($sub =~ /MapGen\:\:set_([\w\d\_]+)$/u) { } 222 0 222
86 55 my $type = $1;
87 55 my $modu = shift;
88  
89 55 ▶ delete $this->{objs}{$type} if $this->{objs}{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"objs"}{$type} 2 53 55
90  
91 55 my $module = "Games::RolePlay::MapGen::" . (ucfirst $type) . "::$modu";
92 55 ▶ croak "Couldn't locate module \"$modu\" during execution of $sub() $@" unless eval { "require $module" };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { "require $module" } } 0 55 55
93  
94 # NOTE: why does this not have {objs}? 5/12/8
95 55 $this->{$type} = $module;
96  
97 55 return;
98  
99 } elsif( $sub =~ m/MapGen\:\:add_(generator|exporter)_plugin$/ ) {
100 7 my $type = $1;
101 7 my $plug = shift;
102  
103 7 my $newn = "Games::RolePlay::MapGen::" . (ucfirst $type) . "Plugin::$plug";
104 7 ▶ croak "Couldn't locate module \"$plug\" during execution of $sub()" unless eval { "require $newn" };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { "require $newn" } } 0 7 7
105  
106 7 push @{ $this->{plugins}{$type} }, $newn;
107  
108 7 return;
109  
110 } elsif( $sub =~ m/MapGen\:\:set_([\w\d\_]+)$/ ) {
111 222 my $n = $1;
112  
113 222 ▶ croak "ERROR: set_$n() unknown setting during execution of $sub()" unless exists $known_opts{$n};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Games::RolePlay::MapGen::known_opts{$n} 1 221 222
114  
115 221 $this->{$n} = shift;
116  
117 221 for my $o (qw(generator exporter)) {
118 442 ▶ if( my $oo = $this->{objs}{$o} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $oo = $this->{"objs"}{$o}) 25 417 442
119  
120 # NOTE: how does this->{$n} relate to this->{objs}{$n} ... 5/12/8
121 25 $oo->{o}{$n} = $this->{$n};
122 }
123 }
124  
125 221 return;
126 }
127  
128 0 croak "ERROR: function $sub() not found";
129 }
130 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
131 # }}}
132 # new {{{
133 19 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
134 19 my $class = shift;
135 19 my @opts = @_;
136 19 ▶ my $opts = ( (@opts == 1 and ref($opts[0]) eq "HASH") ? {%{$opts[0]}} : {@opts} );
Branch: 1 branch
BranchTrueFalseTotal
@opts == 1 && ref $opts[0] eq 'HASH' ? : 11 8 19
Condition66%2/3
@opts == 1 and ref $opts[0] eq "HASH"
!ll&&!rl&&r
8011
MC/DC50%1/2
@opts == 1 and ref $opts[0] eq "HASH"
@opts == 1 ref $opts[0] eq "HASH"
Truth table66%2/3
A and B
A= @opts == 1B= ref $opts[0] eq "HASH"
ABresult
0X0
100
111
137 19 my $this = bless $opts, $class;
138  
139 0 ▶ if( my $e = $this->_check_opts ) { croak $e }
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $this->_check_opts) 0 19 19
140  
141 19 return $this;
142 }
143 # }}}
144  
145 # save_map {{{
146 13 ▶sub save_map {
Subroutine save_map: called CC=2 SCAR=6.9
Pod undocumented
147 13 my $this = shift;
148 13 my $filename = shift;
149  
150 13 $this->{_the_map}->disconnect_map;
151  
152 13 my $str;
153 13 ▶ if( $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) { } 5 8 13
154 5 Storable::store($this, $filename);
155  
156 } else {
157 8 $str = Storable::freeze($this);
158 }
159  
160 13 $this->{_the_map}->interconnect_map;
161  
162 13 return $str;
163 }
164 # }}}
165 # load_map {{{
166 4 ▶sub load_map {
Subroutine load_map: called CC=4 SCAR=18.5
Pod undocumented
167 4 my $this = shift;
168 4 my $filename = shift;
169  
170 4 ▶ if( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) { } 1 0 1
171 1 ▶ eval { %$this = %{ Storable::retrieve( $filename ) } }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { %$this = %{Storable::retrieve($filename);} } } or die "ERROR while evaluating saved map from file: $@" 0 1 1
172 or die "ERROR while evaluating saved map from file: $@";
173  
174 } else {
175 0 ▶ eval { %$this = %{ Storable::thaw( $filename ) } }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { %$this = %{Storable::thaw($filename);} } } or die "ERROR while evaluating saved map from string: $@" 0 0 0
176 or die "ERROR while evaluating saved map from string: $@";
177 }
178  
179 0 require Games::RolePlay::MapGen::Tools; # This would already be loaded if we were the blessed ref that did the saving
180 0 $this->{_the_map}->interconnect_map; # bit it wouldn't be loaded otherwise!
181 }
182 # }}}
183 # legacy_load_map {{{
184 0 ▶sub legacy_load_map {
Subroutine legacy_load_map: not called CC=3 SCAR=24.8
Pod undocumented
185 0 my $this = shift;
186 0 my $filename = shift;
187  
188 0 ▶ open my $load, "$filename" or die "couldn't open $filename for read: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $load, "$filename" or die "couldn't open $filename for read: $!" 0 0 0
189 0 local $/ = undef;
190 0 my $entire_file = <$load>;
191 0 close $load;
192  
193 0 eval $entire_file;
194 0 ▶ die "ERROR while evaluating saved map: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
195  
196 0 require Games::RolePlay::MapGen::Tools; # This would already be loaded if we were the blessed ref that did the saving
197 0 $this->{_the_map}->interconnect_map; # bit it wouldn't be loaded otherwise!
198 }
199 # }}}
200 # generate {{{
201 16 ▶sub generate {
Subroutine generate: called CC=13 SCAR=27.5
Pod undocumented
202 16 my $this = shift;
203 16 my $err;
204  
205 __MADE_GEN_OBJ:
206 25 ▶ if( my $gen = $this->{objs}{generator} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gen = $this->{'objs'}{'generator'}) { } 9 16 25
207 9 my $new_opts;
208  
209 9 ($this->{_the_map}, $this->{_the_groups}, $new_opts) = $gen->go( @_ );
210  
211 9 ▶ if( $new_opts and keys %$new_opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_opts and keys %$new_opts) 9 0 9
Condition33%1/3
$new_opts and keys %$new_opts
!ll&&!rl&&r
009
MC/DC0%0/2
$new_opts and keys %$new_opts
$new_opts keys %$new_opts
Truth table33%1/3
A and B
A= $new_optsB= keys %$new_opts
ABresult
0X0
100
111
212 9 for my $k (keys %$new_opts) {
213 160 $this->{$k} = $new_opts->{$k};
214 }
215 }
216  
217 9 return;
218  
219 } else {
220 16 ▶ die "ERROR: problem creating new generator object" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 16 16
221 }
222  
223 16 eval qq( require $this->{generator} );
224 16 ▶ croak "ERROR locating generator module:\n\t$@\n " if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 7 9 16
225  
226 9 my $obj;
227 9 ▶ my @opts = map(($_=>$this->{$_}), grep {defined $this->{$_} and $_ ne "objs" and $_ ne "plugins" } keys %$this);
Condition83%5/6
defined $this->{$_} && $_ ne 'objs' && $_ ne 'plugins'
!ll&&!rl&&r
95117
defined $this->{$_} and $_ ne "objs"
!ll&&!rl&&r
09122
MC/DC66%2/3
defined $this->{$_} && $_ ne 'objs' && $_ ne 'plugins'
defined $this->{$_} $_ ne "objs" $_ ne 'plugins'
Truth table75%3/4
A and B && C
A= defined $this->{$_}B= $_ ne "objs"C= $_ ne 'plugins'
ABCresult
0XX0
10X0
1100
1111
228  
229 9 eval qq( \$obj = new $this->{generator} (\@opts); );
230 9 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
231 0 ▶ die "ERROR generating generator:\n\t$@\n " if $@ =~ m/ERROR/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /ERROR/u 0 0 0
232 0 ▶ croak "ERROR generating generator:\n\t$@\n " if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
233 }
234  
235 9 $obj->add_plugin( $_ ) for @{ $this->{plugins}{generator} };
236  
237 9 $this->{objs}{generator} = $obj;
238 9 $err = 1;
239  
240 9 $this->_check_opts; # plugins, generators and exporters can add default options
241  
242 9 goto __MADE_GEN_OBJ;
243 }
244 # }}}
245 # export {{{
246 6 ▶sub export {
Subroutine export: called CC=10 SCAR=24.7
Pod undocumented
247 6 my $this = shift;
248 6 my $err;
249  
250 __MADE_VIS_OBJ:
251 11 ▶ if( my $vis = $this->{objs}{exporter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vis = $this->{'objs'}{'exporter'}) { } 6 5 11
252  
253 6 ▶ return $vis->go( _the_map => $this->{_the_map}, _the_groups => $this->{_the_groups}, (@_==1 ? (fname=>$_[0]) : @_) );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 5 1 6
254  
255 } else {
256 5 ▶ die "problem creating new exporter object" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 5 5
257 }
258  
259 5 eval qq( require $this->{exporter} );
260 5 ▶ croak "ERROR locating exporter module:\n\t$@\n " if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
261  
262 5 my $obj;
263 5 ▶ my @opts = map(($_=>$this->{$_}), grep {defined $this->{$_} and $_ ne "objs" and $_ ne "plugins" } keys %$this);
Condition83%5/6
defined $this->{$_} && $_ ne 'objs' && $_ ne 'plugins'
!ll&&!rl&&r
55110
defined $this->{$_} and $_ ne "objs"
!ll&&!rl&&r
05115
MC/DC66%2/3
defined $this->{$_} && $_ ne 'objs' && $_ ne 'plugins'
defined $this->{$_} $_ ne "objs" $_ ne 'plugins'
Truth table75%3/4
A and B && C
A= defined $this->{$_}B= $_ ne "objs"C= $_ ne 'plugins'
ABCresult
0XX0
10X0
1100
1111
264  
265 5 eval qq( \$obj = new $this->{exporter} (\@opts); );
266 5 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
267 0 ▶ die "ERROR generating exporter:\n\t$@\n " if $@ =~ m/ERROR/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /ERROR/u 0 0 0
268 0 ▶ croak "ERROR generating exporter:\n\t$@\n " if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
269 }
270  
271 5 $this->{objs}{exporter} = $obj;
272 5 $err = 1;
273  
274 5 $this->_check_opts; # plugins, generators and exporters can add default options
275  
276 5 goto __MADE_VIS_OBJ;
277 }
278 # }}}
279  
280 # import_xml {{{
281 3 ▶sub import_xml {
Subroutine import_xml: called CC=4 SCAR=15.2
Pod undocumented
282 3 my $this = shift;
283 3 ▶ my $that = shift; croak "no such file that=$that" unless -f $that;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $that 0 3 3
284  
285 3 ▶ $this = $this->new unless ref $this;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this 3 0 3
286  
287 3 $this->set_generator( "XMLImport" );
288 3 ▶ if( -f $that ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $that) { } 3 0 3
289 3 $this->generate( xml_input_file => $that, @_ );
290  
291 } else {
292 0 $this->generate( xml_input => $that, @_ );
293 }
294 0 $this;
295 }
296 # }}}
297 # sub_map {{{
298 0 ▶sub sub_map {
Subroutine sub_map: not called CC=5 SCAR=34.0
Pod undocumented
299 0 my $this = shift;
300 0 ▶ my $that = shift; croak "that's not a map" unless ref $that;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $that 0 0 0
301 0 ▶ my $ul = shift; croak "upper left should be an arrayref two tuple" unless 2==eval {@$ul};
Branch: 1 branch
BranchTrueFalseTotal
unless 2 == eval { do { @$ul } } 0 0 0
302 0 ▶ my $lr = shift; croak "lower right should be an arrayref two tuple" unless 2==eval {@$lr};
Branch: 1 branch
BranchTrueFalseTotal
unless 2 == eval { do { @$lr } } 0 0 0
303  
304 0 ▶ $this = $this->new unless ref $this;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $this 0 0 0
305 0 $this->set_generator( "SubMap" );
306 0 $this->generate( map_input => $that, upper_left=>$ul, lower_right=>$lr );
307 0 $this;
308 }
309 # }}}
310 # size {{{
311 3 ▶sub size {
Subroutine size: called CC=2 SCAR=7.1
Pod undocumented
312 3 my $this = shift;
313 3 my $map = $this->{_the_map};
314  
315 3 my $x = @{$map->[0]};
316 3 my $y = @$map;
317  
318 3 ▶ return ($x, $y) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 3 0 3
319 0 return [$x, $y];
320 }
321 # }}}
322  
323 # {{{ FREEZE_THAW_HOOKS
324 FREEZE_THAW_HOOKS: {
325 22 my $going;
326 14 ▶ sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=2 SCAR=6.9
Pod undocumented
327 14 ▶ return if $going;
Branch: 1 branch
BranchTrueFalseTotal
if $going 7 7 14
328 7 my $this = shift;
329 7 $going = 1;
330 7 my $str = $this->save_map;
331 7 $going = 0;
332 7 return $str;
333 }
334  
335 2 ▶ sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
336 2 my $this = shift;
337 2 $this->load_map($_[1]);
338 }
339 }
340  
341 # }}}
342  
343 22 1;
344  
345 __END__
« blib/lib/Games/RolePlay/MapGen/GeneratorPlugin/BasicDoors.pm ↑ Summary blib/lib/Games/RolePlay/MapGen/Exporter/XML.pm »