blib/lib/Games/RolePlay/MapGen/Exporter/XML.pm

filter: Statementstmt 100% 94 / 94 Branchbran 79.4% 27 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 92.8% 130 / 140 CC 24 SCAR 23.1
CC
24
Cov
95%
genmap
16.0
go
7.2
__ANON__
3.0
CRAP 10.0
« blib/lib/Games/RolePlay/MapGen.pm ↑ Summary blib/lib/Games/RolePlay/MapGen/Generator/Perfect.pm »
1 # vi:tw=0 syntax=perl:
2  
3 package Games::RolePlay::MapGen::Exporter::XML;
4  
5 3 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 1;
11  
12 # new {{{
13 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 4 my $class = shift;
15 4 my $this = bless {o => {@_}}, $class;
16  
17 4 return $this;
18 }
19 # }}}
20 # go {{{
21 4 ▶sub go {
Subroutine go: called CC=7 SCAR=19.7
Pod undocumented
22 4 my $this = shift;
23 4 my $opts = {@_};
24  
25 4 for my $k (keys %{ $this->{o} }) {
26 88 ▶ $opts->{$k} = $this->{o}{$k} if not exists $opts->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$k} 80 8 88
27 }
28  
29 4 ▶ croak "ERROR: fname is a required option for " . ref($this) . "::go()" unless $opts->{fname};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"fname"} 0 4 4
30 4 ▶ croak "ERROR: _the_map is a required option for " . ref($this) . "::go()" unless ref($opts->{_the_map});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts->{"_the_map"} 0 4 4
31  
32 4 my $map = $this->genmap($opts);
33 4 ▶ unless( $opts->{fname} eq "-retonly" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"fname"} eq "-retonly") 4 0 4
34 4 ▶ open my $out, ">$opts->{fname}" or die "ERROR: couldn't open $opts->{fname} for write: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, qq[>$opts->{"fname"}] or die qq[ERROR: couldn't open $opts->{"fname"} for write: $!] 4 0 4
35 4 print $out "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<!DOCTYPE MapGen SYSTEM \"MapGen.dtd\">\n";
36 4 print $out "<?xml-stylesheet type=\"text/xsl\" href=\"MapGen.xsl\"?>\n\n";
37 4 print $out "\n", $map;
38 4 close $out;
39 }
40  
41 4 return $map;
42 }
43 # }}}
44 # genmap {{{
45 4 ▶sub genmap {
Subroutine genmap: called CC=16 SCAR=27.7
Pod undocumented
46 4 my $this = shift;
47 4 my $opts = shift;
48 4 my $m = $opts->{_the_map};
49 4 my $g = $opts->{_the_groups};
50  
51 4 my $options = [];
52 4 my $groups = [];
53 4 my $map = [];
54  
55 4 ▶ my $ah = sub { my %h; tie %h, "Tie::IxHash", (@_); \%h };
Subroutine __ANON__: called CC=1 SCAR=0.0
56  
57 # options {{{
58 309 ▶ my $sort_opts = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
59 309 ▶ my ($c, $d) = map {ref $opts->{$_} ? 1:0} $a, $b;
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{$_} ? : 150 468 618
60  
61 # warn "sorting ($a, $b, $c, $d)";
62  
63 309 ▶ return $c <=> $d if ($c + $d) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $c + $d == 1 66 243 309
64 243 return $a cmp $b;
65 4 };
66  
67 4 for my $k (sort $sort_opts keys %$opts) {
68 92 ▶ unless( $k =~ m/^(?:_.+?|objs|plugins)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k =~ /^(?:_.+?|objs|plugins)$/u) 84 8 92
69 84 my $v = $opts->{$k};
70  
71 84 ▶ if( my $t = ref $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = ref $v) { } 16 68 84
72 16 ▶ next if $t ne "HASH"; # probably a r_cb => sub {} code ref from XMLImporter
Branch: 1 branch
BranchTrueFalseTotal
if $t ne "HASH" 0 16 16
73  
74 16 push @$options, $ah->( name=>$k, value=>join(" ", map { "$_: $v->{$_};" } sort keys %$v ));
75  
76 } else {
77 68 push @$options, $ah->( name=>$k, value=>$v );
78 }
79 }
80 }
81 # }}}
82 # groups {{{
83 4 for my $g (@{ $opts->{_the_groups} }) {
84 push @$groups, $ah->(
85 name => $g->{name},
86 type => $g->{type},
87 rectangle => [map {
88 $ah->(
89 13 loc => join(",", @{ $g->{loc}[$_] }),
90 13 size => join("x", @{ $g->{size}[$_] }),
91 )
92 13 } 0 .. $#{ $g->{loc} }],
93 );
94 }
95 # }}}
96  
97 4 my $iend = $#{ $opts->{_the_map} };
98 4 for my $i (0 .. $iend) {
99 95 my $jend = $#{ $opts->{_the_map}[$i] };
100 95 my $row = $ah->( ypos=>$i, tile=>[] );
101  
102 95 for my $j (0 .. $jend) {
103 2375 my $tile = $opts->{_the_map}[$i][$j];
104  
105 2375 my $h;
106 2375 ▶ if( my $t = $tile->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $tile->{'type'}) { } 1147 1228 2375
107 1147 my $closures = [];
108  
109 $h = $ah->(
110 xpos => $j,
111 type => $tile->{type},
112 1147 );
113  
114 1147 for my $dir (qw(north south east west)) {
115 4588 my $d = substr $dir, 0, 1;
116 4588 my $o = $tile->{od}{$d};
117  
118 4588 ▶ if( $o == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($o == 1) { } 3176 1412 4588
elsif ($o > 1) { } 90 1322 1412
119 # open -- so don't make it show a closure
120  
121 } elsif( $o > 1 ) {
122 90 my $door = $tile->{od}{$d};
123  
124 push @$closures, $ah->( dir => $dir, type => "door",
125 360 ▶ (map { $_ => $door->{$_} ? "yes" : "no" } qw(locked stuck secret open)),
Branch: 1 branch
BranchTrueFalseTotal
$door->{$_} ? : 70 290 360
126  
127 90 (map { $_."_open_dir" => {n=>"north", e=>"east", s=>"south", w=>"west"}->{$door->{open_dir}{$_}} } qw(major minor)),
128 );
129  
130 } else {
131 1322 push @$closures, $ah->( dir => $dir, type => "wall" );
132 }
133 }
134  
135 1147 ▶ $h->{closure} = $closures if int @$closures;
Branch: 1 branch
BranchTrueFalseTotal
if int @$closures 859 288 1147
136  
137 1147 push @{ $row->{tile} }, $h;
138  
139 } else {
140 1228 push @{ $row->{tile} }, $h = {xpos=>$j, type=>"wall"}; # this didn't used to be here... it made parsing craptastic
141 }
142  
143 2375 ▶ $opts->{t_cb}->( ($j+1,$i+1), $h ) if exists $opts->{t_cb};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"t_cb"} 0 2375 2375
144 }
145  
146 95 ▶ push @$map, $row if int @{$row->{tile}}
Branch: 1 branch
BranchTrueFalseTotal
if int @{$row->{"tile"};} 95 0 95
147 }
148  
149 4 my %main; tie %main, "Tie::IxHash", (
150 option => $options,
151 tile_group => $groups,
152 'map' => { row => $map },
153 );
154  
155 4 return XMLout(\%main,
156 RootName => "MapGen",
157 NoSort => 1, # IxHash does this, please don't help me, kthx
158 );
159 }
160 # }}}
161  
162 __END__
« blib/lib/Games/RolePlay/MapGen.pm ↑ Summary blib/lib/Games/RolePlay/MapGen/Generator/Perfect.pm »