blib/lib/Game/HexDescribe/Utils.pm

filter: Statementstmt 50.2% 343 / 683 Branchbran 34.2% 117 / 342 Conditioncond 34.1% 56 / 164 MC/DCmcdc 19.7% 19 / 96 Subroutinesub 68.0% 32 / 47 Podpod 97.3% 37 / 38 totaltotal 44.0% 604 / 1370 SCAR 94.8
CC
266
Cov
43%
describe
2389.6
parse_table
226.0
same_direction
156.0
CRAP 13097.1
↑ Summary blib/lib/Game/HexDescribe.pm »
1 #!/usr/bin/env perl
2 # Copyright (C) 2018–2022 Alex Schroeder <alex@gnu.org>
3 #
4 # This program is free software: you can redistribute it and/or modify it under
5 # the terms of the GNU Affero General Public License as published by the Free
6 # Software Foundation, either version 3 of the License, or (at your option) any
7 # later version.
8 #
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU Affero General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Game::HexDescribe::Utils - utilities to use the Hex Describe data
22  
23 =head1 DESCRIPTION
24  
25 L<Hex::Describe> is a web application which uses recursive random tables to
26 create the description of a map. This package contains the functions used to
27 access the information outside the web application framework.
28  
29 =cut
30  
31 package Game::HexDescribe::Utils;
32 6 require Exporter;
33 6 our @ISA = qw(Exporter);
34 6 our @EXPORT_OK = qw(init markdown describe_text list_tables parse_table load_table
35 describe_map parse_map load_map);
36 6 ▶use Text::Autoformat;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 6 ▶use Game::HexDescribe::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use Array::Utils qw(intersect);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 6 ▶use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 6 my $log = Game::HexDescribe::Log->get;
47  
48 6 our $face_generator_url;
49 6 our $text_mapper_url;
50  
51 =item list_tables($dir)
52  
53 This function returns the table names in $dir. These are based on the following
54 filename convention: "$dir/hex-describe-$name-table.txt".
55  
56 =cut
57  
58 sub list_tables {
59 0 ▶ my $dir = shift;
Subroutine list_tables: not called CC=2 SCAR=17.9
Pod documented
60 0 $log->debug("Looking for maps in the contrib directory: $dir");
61 0 my @names = map { $_->basename('.txt') } Mojo::File->new($dir)->list->each;
62 0 ▶ return grep { $_ } map { $1 if /^hex-describe-(.*)-table$/ } @names;
Branch: 1 branch
BranchTrueFalseTotal
if /^hex-describe-(.*)-table$/u 0 0 0
63 }
64  
65 =item load_table($name, $dir)
66  
67 This function returns the unparsed table from the filename
68 "$dir/hex-describe-$name-table.txt".
69  
70 =cut
71  
72 sub load_table {
73 7 ▶ my ($name, $dir) = @_;
Subroutine load_table: called CC=2 SCAR=7.4
Pod documented
74 7 $log->debug("Looking for table '$name' in the contrib directory '$dir'");
75 7 my $file = Mojo::File->new("$dir/hex-describe-$name-table.txt");
76 7 ▶ return decode_utf8($file->slurp) if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 7 0 7
77 0 return '';
78 }
79  
80 =item load_map($name, $dir)
81  
82 This function returns the unparsed map from the filename
83 "$dir/hex-describe-$name-map.txt".
84  
85 =cut
86  
87 sub load_map {
88 1 ▶ my ($name, $dir) = @_;
Subroutine load_map: called CC=2 SCAR=7.0
Pod documented
89 1 $log->debug("Looking for map in the contrib directory: $dir");
90 1 my $file = Mojo::File->new("$dir/hex-describe-$name-map.txt");
91 1 ▶ return decode_utf8($file->slurp) if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 1 0 1
92 }
93  
94 =item parse_table
95  
96 This parses the random tables. This is also where *bold* gets translated to
97 HTML. We also do some very basic checking of references. If we refer to another
98 table in square brackets we check whether we've seen such a table.
99  
100 Table data is a reference to a hash of hashes. The key to the first hash is the
101 name of the table; the key to the second hash is "total" for the number of
102 options and "lines" for a reference to a list of hashes with two keys, "count"
103 (the weight of this lines) and "text" (the text of this line).
104  
105 A table like the following:
106  
107 ;tab
108 1,a
109 2,b
110  
111 Would be:
112  
113 $table_data->{tab}->{total} == 3
114 $table_data->{tab}->{lines}->[0]->{count} == 1
115 $table_data->{tab}->{lines}->[0]->{text} eq "a"
116 $table_data->{tab}->{lines}->[1]->{count} == 2
117 $table_data->{tab}->{lines}->[1]->{text} eq "b"
118  
119 =cut
120  
121 6 my $dice_re = qr/^(save )?(?:(\d+)d(\d+)(?:x(\d+))?(?:([+-]\d+))?(?:>=(-?\d+))?(?:<=(-?\d+))?|(\d+))(?: as (.+))?$/;
122 6 my $math_re = qr/^(save )?([-+*\/%<>=()0-9]+)(?: as (.+))?$/;
123  
124 sub parse_table {
125 7 ▶ my $text = shift;
Subroutine parse_table: called CC=31 SCAR=54.2
Pod documented
126 7 $log->debug("parse_table: parsing " . length($text) . " characters");
127 7 my $data = {};
128 7 my $words = "[^\[\]\n]*";
129 7 my (%aliases, $key, $c, $t);
130 7 for my $line (split(/\r?\n/, $text)) {
131 285040 ▶ if ($line =~ /^;([^#\r\n]+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^;([^#\r\n]+)/u) { } 14553 270487 285040
elsif ($key and () = $line =~ /^(\d+),(.*)/u) { } 253309 17178 270487
elsif ($line ne '' and not $line =~ /^\s*#/u) { } 0 32053 32053
132 14553 $key = $1;
133 14553 ▶ $log->warn("parse_table: reset '$key'") if exists $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$key} 0 14553 14553
134 14553 $data->{$key} = {}; # reset, don't merge
135 ▶ } elsif ($key and ($c, $t) = $line =~ /^(\d+),(.*)/) {
Condition100%3/3
$key and () = $line =~ /^(\d+),(.*)/u
!ll&&!rl&&r
2117157253309
MC/DC100%2/2
$key and () = $line =~ /^(\d+),(.*)/u
$key () = $line =~ /^(\d+),(.*)/u
Truth table100%3/3
A and B
A= $keyB= () = $line =~ /^(\d+),(.*)/u
ABresult
0X0
100
111
136 253309 $t =~ s/\*\*(.*?)\*\*/<strong>$1<\/strong>/g;
137 253309 $t =~ s/\*(.*?)\*/<em>$1<\/em>/g;
138 253309 my %h = (text => $t);
139 253309 ▶ if ($c == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c == 0) 42 253267 253309
140 42 $h{unique} = 1;
141 42 $c = 1;
142 }
143 253309 $h{count} = $c;
144 253309 $data->{$key}->{total} += $c;
145 253309 push(@{$data->{$key}->{lines}}, \%h);
146 # [foo as bar]
147 253309 for my $alias ($h{text} =~ /\[$words as ($words)\]/g) {
148 0 $aliases{$alias} = 1;
149 }
150 # [foo [baz] quux as bar] (one level of indirection allowed
151 253309 for my $alias ($h{text} =~ /\[$words\[$words\]$words as ($words)\]/g) {
152 0 $aliases{$alias} = 1;
153 }
154 ▶ } elsif ($line ne '' and $line !~ /^\s*#/) {
Condition66%2/3
$line ne "" and not $line =~ /^\s*#/u
!ll&&!rl&&r
1487523030
MC/DC0%0/2
$line ne "" and not $line =~ /^\s*#/u
$line ne "" not $line =~ /^\s*#/u
Truth table66%2/3
A and not(B)
A= $line ne ""B= not $line =~ /^\s*#/u
ABresult
0X0
100
111
155 0 $log->warn("unknown line type: '$line'");
156 }
157 }
158 # check tables
159 7 for my $table (keys %$data) {
160 14553 for my $line (@{$data->{$table}->{lines}}) {
161 253309 for my $subtable ($line->{text} =~ /\[($words)\]/g) {
162 0 ▶ next if index($subtable, '|') != -1;
Branch: 1 branch
BranchTrueFalseTotal
if index($subtable, "|") != -1 0 0 0
163 0 ▶ next if $subtable =~ /$dice_re/;
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /$dice_re/u 0 0 0
164 0 ▶ next if $subtable =~ /$math_re/;
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /$math_re/u 0 0 0
165 0 ▶ next if $subtable =~ /^redirect https?:/;
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^redirect https?:/u 0 0 0
166 0 ▶ next if $subtable =~ /^names for (.*)/ and $data->{"name for $1"};
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^names for (.*)/u and $$data{"name for $1"} 0 0 0
Condition0%0/3
$subtable =~ /^names for (.*)/u and $$data{"name for $1"}
!ll&&!rl&&r
000
MC/DC0%0/2
$subtable =~ /^names for (.*)/u and $$data{"name for $1"}
$subtable =~ /^names for (.*)/u $$data{"name for $1"}
Truth table0%0/3
A and B
A= $subtable =~ /^names for (.*)/uB= $$data{"name for $1"}
ABresult
0X0
100
111
167 0 ▶ next if $subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/ and $data->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/u and $data->{$1} 0 0 0
Condition0%0/3
$subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/u and $data->{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/u and $data->{$1}
$subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/u $data->{$1}
Truth table0%0/3
A and B
A= $subtable =~ /^(?:capitalize|titlecase|highlightcase|normalize-elvish) (.*)/uB= $data->{$1}
ABresult
0X0
100
111
168 0 ▶ next if $subtable =~ /^adjacent hex$/; # experimental
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^adjacent hex$/u 0 0 0
169 0 ▶ next if $subtable =~ /^same (.*)/ and ($data->{$1} or $aliases{$1} or $1 eq 'adjacent hex');
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^same (.*)/u and $data->{$1} || $aliases{$1} || $1 eq "adjacent hex" 0 0 0
Condition0%0/9
$subtable =~ /^same (.*)/u and $data->{$1} || $aliases{$1} || $1 eq "adjacent hex"
!ll&&!rl&&r
000
$data->{$1} or $aliases{$1} or $1 eq "adjacent hex"
l!l&&r!l&&!r
000
$data->{$1} or $aliases{$1}
l!l&&r!l&&!r
000
MC/DC0%0/4
$subtable =~ /^same (.*)/u and $data->{$1} || $aliases{$1} || $1 eq "adjacent hex"
$subtable =~ /^same (.*)/u $data->{$1} $aliases{$1} $1 eq "adjacent hex"
Truth table0%0/5
A and B or C or D
A= $subtable =~ /^same (.*)/uB= $data->{$1}C= $aliases{$1}D= $1 eq "adjacent hex"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
170 0 ▶ next if $subtable =~ /^(?:here|nearby|other|append|later|with|and|save|store) (.+?)( as (.+))?$/ and $data->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^(?:here|nearby|other|append|later|with|and|save|store) (.+?)( as (.+))?$/u and $data->{$1} 0 0 0
Condition0%0/3
$subtable =~ /^(?:here|nearby|other|append|later|with|and|save|store) (.+?)( as (.+))?$/u and $data->{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$subtable =~ /^(?:here|nearby|other|append|later|with|and|save|store) (.+?)( as (.+))?$/u and $data->{$1}
$subtable =~ /^(?:here|nearby|other|append|later|with|and|save|store) (.+?)( as (.+))?$/u $data->{$1}
Truth table0%0/3
A and B
A= $subtable =~ /^(?:here|nearby|other|append|later|with|and|save|store) (.+?)( as (.+))?$/uB= $data->{$1}
ABresult
0X0
100
111
171 0 ▶ $subtable = $1 if $subtable =~ /^(.+) as (.+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $subtable =~ /^(.+) as (.+)/u 0 0 0
172 $log->error("Error in table $table: subtable $subtable is missing")
173 0 ▶ unless $data->{$subtable};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$subtable} 0 0 0
174 }
175 }
176 }
177 7 return $data;
178 }
179  
180 =item init
181  
182 When starting a description, we need to initialize our data. There are two
183 global data structures beyond the map.
184  
185 B<$extra> is a reference to a hash of lists of hashes used to keep common data
186 per line. In this context, lines are linear structures like rivers or trails on
187 the map. The first hash uses the hex coordinates as a key. This gets you the
188 list of hashes, one per line going through this hex. Each of these hashes uses
189 the key "type" to indicate the type of line, "line" for the raw data (for
190 debugging), and later "name" will be used to name these lines.
191  
192 $extra->{"0101"}->[0]->{"type"} eq "river"
193  
194 B<%names> is just a hash of names. It is used for all sorts of things. When
195 using the reference C<name for a bugbear band1>, then "name for a bugbear band1"
196 will be a key in this hash. When using the reference C<name for forest foo>,
197 then "name for forest foo: 0101" and will be set for every hex sharing that
198 name.
199  
200 $names{"name for a bugbear band1"} eq "Long Fangs"
201 $names{"name for forest foo: 0101"} eq "Dark Wood"
202  
203 Note that for C</describe/text>, C<init> is called for every paragraph.
204  
205 B<%locals> is a hash of all the "normal" table lookups encountered so far. It is
206 is reset for every paragraph. To refer to a previous result, start a reference
207 with the word "same". This doesn't work for references to adjacent hexes, dice
208 rolls, or names. Here's an example:
209  
210 ;village boss
211 1,[man] is the village boss. They call him Big [same man].
212 1,[woman] is the village boss. They call her Big [same woman].
213  
214 Thus:
215  
216 $locals{man} eq "Alex"
217  
218 B<%globals> is a hash of hashes of all the table lookups beginning with the word
219 "here" per hex. In a second phase, all the references starting with the word
220 "nearby" will be resolved using these. Here's an example:
221  
222 ;ingredient
223 1,fey moss
224 1,blue worms
225 ;forest
226 3,There is nothing here but trees.
227 1,You find [here ingredient].
228 ;village
229 1,The alchemist needs [nearby ingredient].
230  
231 Some of the forest hexes will have one of the two possible ingredients and the
232 village alchemist will want one of the nearby ingredients. Currently, there is a
233 limitation in place: we can only resolve the references starting with the word
234 "nearby" when everything else is done. This means that at that point, references
235 starting with the word "same" will no longer work since C<%locals> will no
236 longer be set.
237  
238 Thus:
239  
240 $globals->{ingredient}->{"0101"} eq "fey moss"
241  
242 =cut
243  
244 6 my $extra;
245 my %names;
246 6 my %locals;
247 6 my $globals;
248  
249 sub init {
250 6 ▶ %names = ();
Subroutine init: called CC=1 SCAR=0.0
Pod documented
251 6 %locals = ();
252 6 $globals = undef;
253 6 $extra = undef;
254 }
255  
256 =item parse_map_data
257  
258 This does basic parsing of hexes on the map as produced by Text Mapper, for
259 example:
260  
261 0101 dark-green trees village
262  
263 =cut
264  
265 sub parse_map_data {
266 1 ▶ my $map = shift;
Subroutine parse_map_data: called CC=8 SCAR=22.4
Pod documented
267 1 my $map_data;
268 1 ▶ if ($map and $map->isa('Mojo::Upload')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map and $map->isa("Mojo::Upload")) 0 1 1
Condition33%1/3
$map and $map->isa("Mojo::Upload")
!ll&&!rl&&r
010
MC/DC0%0/2
$map and $map->isa("Mojo::Upload")
$map $map->isa("Mojo::Upload")
Truth table33%1/3
A and B
A= $mapB= $map->isa("Mojo::Upload")
ABresult
0X0
100
111
269 0 $map = $map->slurp();
270 };
271 1 for my $hex (split(/\r?\n/, $map)) {
272 1 ▶ if (my ($x, $y) = $hex =~ /^(\d\d)(\d\d)\s*empty$/cg) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = $hex =~ /^(\d\d)(\d\d)\s*empty$/cgu) { } 0 1 1
elsif (() = $hex =~ /^(\d\d)(\d\d)\s+/cgu) { } 1 0 1
273 # skip
274 } elsif (($x, $y) = $hex =~ /^(\d\d)(\d\d)\s+/cg) {
275 1 my @types = ("system"); # Traveller
276 1 ▶ while($hex =~ /\G([a-z]="[^"]+")\s*/cg or $hex =~ /(\S+)/cg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hex =~ /\G([a-z]="[^"]+")\s*/cgu or $hex =~ /(\S+)/cgu) 1 1 2
Condition66%2/3
$hex =~ /\G([a-z]="[^"]+")\s*/cgu or $hex =~ /(\S+)/cgu
l!l&&r!l&&!r
011
MC/DC50%1/2
$hex =~ /\G([a-z]="[^"]+")\s*/cgu or $hex =~ /(\S+)/cgu
$hex =~ /\G([a-z]="[^"]+")\s*/cgu $hex =~ /(\S+)/cgu
Truth table66%2/3
A or B
A= $hex =~ /\G([a-z]="[^"]+")\s*/cguB= $hex =~ /(\S+)/cgu
ABresult
1X1
011
000
277 1 push(@types, $1);
278 }
279 1 $map_data->{"$x$y"} = \@types;
280 }
281 }
282 1 return $map_data;
283 }
284  
285 =item parse_map_lines
286  
287 This does basic parsing of linear structures on the map as produced by Text
288 Mapper, for example:
289  
290 0302-0101 trail
291  
292 We use C<compute_missing_points> to find all the missing points on the line.
293  
294 =cut
295  
296 6 my $line_re = qr/^(\d\d\d\d(?:-\d\d\d\d)+)\s+(\S+)/m;
297  
298 sub parse_map_lines {
299 1 ▶ my $map = shift;
Subroutine parse_map_lines: called CC=2 SCAR=8.5
Pod documented
300 1 my @lines;
301 1 ▶ while ($map =~ /$line_re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map =~ /$line_re/gu) 0 1 1
302 0 my ($line, $type) = ($1, $2);
303 0 my @points = compute_missing_points(split(/-/, $line));
304 0 push(@lines, [$type, @points]);
305 }
306 1 return \@lines;
307 }
308  
309 =item process_map_merge_lines
310  
311 As we process lines, we want to do two things: if a hex is part of a linear
312 structure, we want to add the B<type> to the terrain features. Thus, given the
313 following hex and river, we want to add "river" to the terrain features of 0101:
314  
315 0801-0802-0703-0602-0503-0402-0302-0201-0101-0100 river
316  
317 The (virtual) result:
318  
319 0101 dark-green trees village river
320  
321 Furthermore, given another river like the following, we want to merge these
322 where they meet (at 0302):
323  
324 0701-0601-0501-0401-0302-0201-0101-0100 river
325  
326 Again, the (virtual) result:
327  
328 0302 dark-green trees town river river-merge
329  
330 If you look at the default map, here are some interesting situations:
331  
332 A river starts at 0906 but it immediately merges with the river starting at 1005
333 thus it should be dropped entirely.
334  
335 A trail starts at 0206 and passes through 0305 on the way to 0404 but it
336 shouldn't end at 0305 just because there's also a trail starting at 0305 going
337 north to 0302.
338  
339 =cut
340  
341 sub process_map_merge_lines {
342 1 ▶ my $map_data = shift;
Subroutine process_map_merge_lines: called CC=11 SCAR=47.1
Pod documented
343 1 my $lines = shift;
344 1 for my $line (@$lines) {
345 0 my $type = $line->[0];
346 0 my %data = (type => $type, line => $line);
347 # $log->debug("New $type...");
348 0 my $start = 1;
349 COORD:
350 0 for my $i (1 .. $#$line) {
351 0 my $coord = $line->[$i];
352 # don't add data for hexes outside the map
353 0 ▶ last unless $map_data->{$coord};
Branch: 1 branch
BranchTrueFalseTotal
unless $map_data->{$coord} 0 0 0
354 # don't start a line going in the same direction as an existing line in
355 # the same hex (e.g. 0906) but also don't stop a line if it runs into a
356 # merge and continues (e.g. 0305)
357 0 my $same_dir = 0;
358 0 for my $line2 (grep { $_->{type} eq $type } @{$extra->{$coord}}) {
359 0 ▶ if (same_direction($coord, $line, $line2->{line})) {
Branch: 1 branch
BranchTrueFalseTotal
if (same_direction($coord, $line, $line2->{"line"})) 0 0 0
360 # $log->debug("... at $coord, @$line and @{$line2->{line}} go in the same direction");
361 0 $same_dir = 1;
362 0 last;
363 }
364 }
365 0 ▶ if ($start and $same_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start and $same_dir) 0 0 0
Condition0%0/3
$start and $same_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$start and $same_dir
$start $same_dir
Truth table0%0/3
A and B
A= $startB= $same_dir
ABresult
0X0
100
111
366 # $log->debug("... skipping");
367 0 last COORD;
368 }
369 # add type to the hex description, add "$type-merge" when
370 # running into an existing one
371 0 my $merged;
372 0 ▶ if (not grep { $_ eq $type } @{$map_data->{$coord}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not grep({$_ eq $type;} @{$$map_data{$coord};})) { } 0 0 0
elsif (not grep({$_ eq "$type-merge";} @{$$map_data{$coord};})) { } 0 0 0
373 # $log->debug("...$type leading into $coord");
374 0 push(@{$map_data->{$coord}}, $type);
375 0 } elsif (not grep { $_ eq "$type-merge" } @{$map_data->{$coord}}) {
376 0 $merged = $same_dir; # skip the rest of the line, if same dir
377 # $log->debug("...noted merge into existing $type at $coord");
378 0 push(@{$map_data->{$coord}}, "$type-merge");
379 } else {
380 0 $merged = $same_dir; # skip the rest of the line, if same dir
381 # $log->debug("...leads into existing $type merge at $coord");
382 }
383 0 $start = 0;
384 # all hexes along a line share this hash
385 0 push(@{$extra->{$coord}}, \%data);
386 # if a river merges into another, don't add any hexes downriver
387 0 ▶ last if $merged;
Branch: 1 branch
BranchTrueFalseTotal
if $merged 0 0 0
388 }
389 }
390 }
391  
392 =item process_map_start_lines
393  
394 As we process lines, we also want to note the start of lines: sources of rivers,
395 the beginning of trails. Thus, given the following hex and river, we want to add
396 "river-start" to the terrain features of 0801:
397  
398 0801-0802-0703-0602-0503-0402-0302-0201-0101-0100 river
399  
400 Adds a river to the hex:
401  
402 0801 light-grey mountain river river-start
403  
404 But note that we don't want to do this where linear structures have merged. If a
405 trail ends at a town and merges with other trails there, it doesn't "start"
406 there. It can only be said to start somewhere if no other linear structure
407 starts there.
408  
409 In case we're not talking about trails and rivers but things like routes from A
410 to B, it might be important to note the fact. Therefore, both ends of the line
411 get a "river-end" (if a river).
412  
413 =cut
414  
415 sub process_map_start_lines {
416 1 ▶ my $map_data = shift;
Subroutine process_map_start_lines: called CC=6 SCAR=33.6
Pod documented
417 1 my $lines = shift;
418 # add "$type-start" to the first and last hex of a line, unless it is a merge
419 1 for my $line (@$lines) {
420 0 my $type = $line->[0];
421 0 for my $coord ($line->[1], $line->[$#$line]) {
422 # ends get marked either way
423 0 ▶ push(@{$map_data->{$coord}}, "$type-end") unless grep { $_ eq "$type-end" } @{$map_data->{$coord}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "$type-end";} @{$$map_data{$coord};} 0 0 0
424 # skip hexes outside the map
425 0 ▶ last unless $map_data->{$coord};
Branch: 1 branch
BranchTrueFalseTotal
unless $map_data->{$coord} 0 0 0
426 # skip merges
427 0 ▶ last if grep { $_ eq "$type-merge" } @{$map_data->{$coord}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "$type-merge";} @{$$map_data{$coord};} 0 0 0
428 # add start
429 0 push(@{$map_data->{$coord}}, "$type-start");
430 }
431 }
432 }
433  
434 =item parse_map
435  
436 This calls all the map parsing and processing functions we just talked about.
437  
438 =cut
439  
440 sub parse_map {
441 1 ▶ my $map = shift;
Subroutine parse_map: called CC=1 SCAR=0.0
Pod documented
442 1 my $map_data = parse_map_data($map);
443 1 my $lines = parse_map_lines($map);
444 # longest rivers first
445 1 @$lines = sort { @$b <=> @$a } @$lines;
446 # for my $line (@$lines) {
447 # $log->debug("@$line");
448 # }
449 1 process_map_merge_lines($map_data, $lines);
450 1 process_map_start_lines($map_data, $lines);
451 # for my $coord (sort keys %$map_data) {
452 # $log->debug(join(" ", $coord, @{$map_data->{$coord}}));
453 # }
454 1 return $map_data;
455 }
456  
457 =item pick_description
458  
459 Pick a description from a given table. In the example above, pick a random
460 number between 1 and 3 and then go through the list, addin up counts until you
461 hit that number.
462  
463 If the result picked is unique, remove it from the list. That is, set it's count
464 to 0 such that it won't ever get picked again.
465  
466 =cut
467  
468 sub pick_description {
469 363 ▶ my $h = shift;
Subroutine pick_description: called CC=4 SCAR=14.7
Pod documented
470 363 my $total = $h->{total};
471 363 my $lines = $h->{lines};
472 363 my $roll = int(rand($total)) + 1;
473 363 my $i = 0;
474 363 for my $line (@$lines) {
475 12707 $i += $line->{count};
476 12707 ▶ if ($i >= $roll) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $roll) 363 12344 12707
477 363 ▶ if ($line->{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"unique"}) 0 363 363
478 0 $h->{total} -= $line->{count};
479 0 $line->{count} = 0;
480 }
481 363 return $line->{text};
482 }
483 }
484 0 $log->error("picked nothing");
485 0 return '';
486 }
487  
488 =item resolve_redirect
489  
490 This handles the special redirect syntax: request an URL and if the response
491 code is a 301 or 302, take the location header in the response and return it.
492  
493 If the environment variable C<HEX_DESCRIBE_OFFLINE> is set, these do not get
494 resolved and the empty string is returned.
495  
496 =cut
497  
498 sub resolve_redirect {
499 # If you install this tool on a server using HTTPS, then some browsers will
500 # make sure that including resources from other servers will not work.
501 11 ▶ my $url = shift;
Subroutine resolve_redirect: called CC=8 SCAR=37.5
Pod documented
502 11 my $redirects = shift;
503 11 ▶ return '' unless $redirects and not $ENV{HEX_DESCRIBE_OFFLINE};
Branch: 1 branch
BranchTrueFalseTotal
unless $redirects and not $ENV{"HEX_DESCRIBE_OFFLINE"} 11 0 11
Condition66%2/3
$redirects and not $ENV{"HEX_DESCRIBE_OFFLINE"}
!ll&&!rl&&r
1100
MC/DC0%0/2
$redirects and not $ENV{"HEX_DESCRIBE_OFFLINE"}
$redirects not $ENV{"HEX_DESCRIBE_OFFLINE"}
Truth table66%2/3
A and not(B)
A= $redirectsB= not $ENV{"HEX_DESCRIBE_OFFLINE"}
ABresult
0X0
100
111
504 # Special case because table writers probably used the default face generator URL
505 0 ▶ $url =~ s!^https://campaignwiki\.org/face!$face_generator_url! if $face_generator_url;
Branch: 1 branch
BranchTrueFalseTotal
if $Game::HexDescribe::Utils::face_generator_url 0 0 0
506 0 ▶ $url =~ s!^https://campaignwiki\.org/text-mapper!$text_mapper_url! if $text_mapper_url;
Branch: 1 branch
BranchTrueFalseTotal
if $Game::HexDescribe::Utils::text_mapper_url 0 0 0
507 0 my $ua = Mojo::UserAgent->new;
508 0 my $res = eval { $ua->get($url)->result };
509 0 ▶ if (not $res) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $res) { } 0 0 0
elsif ($res->code == 301 or $res->code == 302) { } 0 0 0
510 0 my $warning = $@;
511 0 chomp($warning);
512 0 $log->warn("connecting to $url: $warning");
513 0 return "";
514 ▶ } elsif ($res->code == 301 or $res->code == 302) {
Condition0%0/3
$res->code == 301 or $res->code == 302
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->code == 301 or $res->code == 302
$res->code == 301 $res->code == 302
Truth table0%0/3
A or B
A= $res->code == 301B= $res->code == 302
ABresult
1X1
011
000
515 0 return Mojo::URL->new($res->headers->location)
516 ->base(Mojo::URL->new($url))
517 ->to_abs;
518 }
519 0 $log->info("resolving redirect for $url did not result in a redirection");
520 0 return $url;
521 }
522  
523 =item pick
524  
525 This function picks the appropriate table given a particular word (usually a map
526 feature such as "forest" or "river").
527  
528 This is where I<context> is implemented. Let's start with this hex:
529  
530 0101 dark-green trees village river trail
531  
532 Remember that parsing the map added more terrain than was noted on the map
533 itself. Our function will get called for each of these words, Let's assume it
534 will get called for "dark-green". Before checking whether a table called
535 "dark-green" exists, we want to check whether any of the other words provide
536 enough context to pick a more specific table. Thus, we will check "trees
537 dark-green", "village dark-green", "river dark-green" and "trail dark-green"
538 before checking for "dark-green".
539  
540 If such a table exists in C<$table_data>, we call C<pick_description> to pick a
541 text from the table and then we go through the text and call C<describe> to
542 resolve any table references in square brackets.
543  
544 Remember that rules for the remaining words are still being called. Thus, if you
545 write a table for "trees dark-green" (which is going to be picked in preference
546 to "dark-green"), then there should be no table for "trees" because that's the
547 next word that's going to be processed!
548  
549 =cut
550  
551 sub pick {
552 364 ▶ my $map_data = shift;
Subroutine pick: called CC=6 SCAR=18.0
Pod documented
553 364 my $table_data = shift;
554 364 my $level = shift;
555 364 my $coordinates = shift;
556 364 my $words = shift;
557 364 my $word = shift;
558 364 my $redirects = shift;
559 364 my $text;
560 # Make sure we're testing all the context combinations first. Thus, if $words
561 # is [ "mountains" white" "chaos"] and $word is "mountains", we want to test
562 # "white mountains", "cold mountains" and "mountains", in this order.
563 364 for my $context (grep( { $_ ne $word } @$words), $word) {
564 378 ▶ my $key = ($context eq $word ? $word : "$context $word");
Branch: 1 branch
BranchTrueFalseTotal
$context eq $word ? : 364 14 378
565 # $log->debug("$coordinates: looking for a $key table") if $coordinates eq "0109";
566 378 ▶ if ($table_data->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_data->{$key}) 363 15 378
567 363 $text = pick_description($table_data->{$key});
568 # $log->debug("$coordinates → $key → $text");
569 363 my $seed = int(rand(~0)); # maxint
570 363 $text =~ s/\[\[redirect (https:.*?)\]\]/my $url = $1; $url =~ s!\$seed!$seed!; resolve_redirect($url, $redirects)/ge;
571 # this makes sure we recursively resolve all references, in order, because
572 # we keep rescanning from the beginning
573 363 my $last = $text;
574 363 ▶ while ($text =~ s/\[([^][]*)\]/describe($map_data,$table_data,$level+1,$coordinates,[$1], $redirects)/e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/\[([^][]*)\]/describe($map_data, $table_data, $level + 1, $coordinates, [$1], $redirects);/eu) 510 363 873
575 510 ▶ if ($last eq $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last eq $text) 0 510 510
576 0 $log->error("Infinite loop: $text");
577 0 last;
578 }
579 510 $last = $text;
580 };
581 363 last;
582 }
583 }
584 # $log->debug("$word → $text ") if $text;
585 364 return $text;
586 }
587  
588 =item describe
589  
590 This is where all the references get resolved. We handle references to dice
591 rolls, the normal recursive table lookup, and all the special rules for names
592 that get saved once they have been determined both globally or per terrain
593 features. Please refer to the tutorial on the help page for the various
594 features.
595  
596 =cut
597  
598 sub describe {
599 529 ▶ my $map_data = shift;
Subroutine describe: called CC=126 SCAR=77.8
Pod documented
600 529 my $table_data = shift;
601 529 my $level = shift;
602 529 my $coordinates = shift;
603 529 my $words = shift;
604 529 my $redirects = shift;
605 529 ▶ $log->error("Recursion level $level exceeds 20 in $coordinates (@$words)!") if $level > 20;
Branch: 1 branch
BranchTrueFalseTotal
if $level > 20 0 529 529
606 529 ▶ return '' if $level > 20;
Branch: 1 branch
BranchTrueFalseTotal
if $level > 20 0 529 529
607 529 ▶ if ($level == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 1) 3 526 529
608 3 %locals = (hex => $coordinates); # reset once per paragraph
609 3 for my $word (@$words) {
610 4 ▶ if ($word =~ /^([a-z]+)="(.*)"/ or
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /^([a-z]+)="(.*)"/u or $word =~ /(.*)-(\d+)$/u) { } 0 4 4
Condition33%1/3
$word =~ /^([a-z]+)="(.*)"/u or $word =~ /(.*)-(\d+)$/u
l!l&&r!l&&!r
004
MC/DC0%0/2
$word =~ /^([a-z]+)="(.*)"/u or $word =~ /(.*)-(\d+)$/u
$word =~ /^([a-z]+)="(.*)"/u $word =~ /(.*)-(\d+)$/u
Truth table33%1/3
A or B
A= $word =~ /^([a-z]+)="(.*)"/uB= $word =~ /(.*)-(\d+)$/u
ABresult
1X1
011
000
611 $word =~ /(.*)-(\d+)$/) {
612 # assigments in the form uwp=“777777” assign “777777” to “uwp”
613 # words in the form law-5 assign “5” to “law”
614 0 $locals{$1} = $2;
615 } else {
616 4 $locals{$word} = 1;
617 }
618 }
619 }
620 529 my @descriptions;
621 529 for my $word (@$words) {
622 # valid dice rolls: 1d6, 1d6+1, 1d6x10, 1d6x10+1
623 530 ▶ if (my ($just_save, $n, $d, $m, $p, $min, $max, $c, $save_as) = $word =~ /$dice_re/) {
Branch: 22 branches
BranchTrueFalseTotal
if (() = $word =~ /$dice_re/u) { } 38 492 530
elsif (() = $word =~ /$math_re/u) { } 0 492 492
elsif ($word =~ /^(\S+)\?\|\|(.*)/u) { } 0 492 492
elsif ($word =~ /^!(\S+)\|\|(.*)/u) { } 0 492 492
elsif (index($word, '||') != -1) { } 0 492 492
elsif (index($word, '|') != -1) { } 0 492 492
elsif ($word =~ /^name for an? /u) { } 4 488 492
elsif ($word =~ /^names for (\S+)/u) { } 0 488 488
elsif ($word =~ /^name for (\S+)/u) { } 0 488 488
elsif ($word eq 'adjacent hex') { } 0 488 488
elsif ($word =~ /^(?:nearby|other|later) ./u) { } 2 486 488
elsif ($word =~ /^append (.*)/u) { } 0 486 486
elsif ($word =~ /^same (.+)/u) { } 95 391 486
elsif ($word =~ /^(?:(here|global) )?with (.+?)(?: as (.+))?$/u) { } 3 388 391
elsif ($word =~ /^(?:(here|global) )?and (.+?)(?: as (.+))?$/u) { } 2 386 388
elsif ($word =~ /^capitalize (.+)/u) { } 1 385 386
elsif ($word =~ /^titlecase (.+)/u) { } 0 385 385
elsif ($word =~ /^highlightcase (.+)/u) { } 0 385 385
elsif ($word =~ /^normalize-elvish (.+)/u) { } 2 383 385
elsif ($word =~ /^(?:(here|global) )?(?:(save|store|quote) )?(.+?)(?: as (.+))?$/u) { } 383 0 383
elsif ($level > 1 and not exists $table_data->{$word} and not $locals{$word}) { } 0 0 0
elsif ($level > 1 and not $table_data->{$word} and not $locals{$word}) { } 0 0 0
624 38 my $r = 0;
625 38 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 0 38 38
626 0 $r = $c;
627 } else {
628 38 ▶ for(my $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 53 38 91
629 53 $r += int(rand($d)) + 1;
630 }
631 38 ▶ $r *= $m||1;
Condition100%2/2
$m || 1
l!l
1028
MC/DC100%1/1
$m || 1
$m
Truth table100%2/2
A
A= $m
Aresult
11
00
632 38 ▶ $r += $p||0;
Condition100%2/2
$p || 0
l!l
1028
MC/DC100%1/1
$p || 0
$p
Truth table100%2/2
A
A= $p
Aresult
11
00
633 38 ▶ $r = $min if defined $min and $r < $min;
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and $r < $min 0 38 38
Condition33%1/3
defined $min and $r < $min
!ll&&!rl&&r
3800
MC/DC0%0/2
defined $min and $r < $min
defined $min $r < $min
Truth table33%1/3
A and B
A= defined $minB= $r < $min
ABresult
0X0
100
111
634 38 ▶ $r = $max if defined $max and $r > $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and $r > $max 0 38 38
Condition33%1/3
defined $max and $r > $max
!ll&&!rl&&r
3800
MC/DC0%0/2
defined $max and $r > $max
defined $max $r > $max
Truth table33%1/3
A and B
A= defined $maxB= $r > $max
ABresult
0X0
100
111
635 }
636 # $log->debug("rolling dice: $word = $r");
637 38 ▶ $locals{$save_as} = $r if $save_as;
Branch: 1 branch
BranchTrueFalseTotal
if $save_as 10 28 38
638 38 ▶ push(@descriptions, $r) unless $just_save;
Branch: 1 branch
BranchTrueFalseTotal
unless $just_save 38 0 38
639 } elsif (my ($save, $expression, $as) = $word =~ /$math_re/) {
640 0 my $r = eval($expression);
641 0 ▶ $locals{$as} = $r if $as;
Branch: 1 branch
BranchTrueFalseTotal
if $as 0 0 0
642 0 ▶ push(@descriptions, $r) unless $save;
Branch: 1 branch
BranchTrueFalseTotal
unless $save 0 0 0
643 } elsif ($word =~ /^(\S+)\?\|\|(.*)/) {
644 # [a?||b] return b if a is defined, or nothing
645 0 ▶ push(@descriptions, $2) if $locals{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $locals{$1} 0 0 0
646 } elsif ($word =~ /^!(\S+)\|\|(.*)/) {
647 # [!a||b] return b if a is undefined
648 0 ▶ push(@descriptions, $2) if not $locals{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless $locals{$1} 0 0 0
649 } elsif (index($word, "||") != -1) {
650 # [a||b] returns a if defined, otherwise b
651 0 for my $html (split(/\|\|/, $word)) {
652 0 my $copy = $html;
653 0 $copy =~ s/<.*?>|…//g; # strip tags, e.g. span elements, and ellipsis
654 0 ▶ if ($copy =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copy =~ /\S/u) 0 0 0
655 0 push(@descriptions, $html);
656 0 last;
657 }
658 }
659 } elsif (index($word, "|") != -1) {
660 # [a|b] returns one of a or b
661 0 push(@descriptions, one(split(/\|/, $word)));
662 } elsif ($word =~ /^name for an? /) {
663 # for global things like factions, dukes
664 4 my $name = $names{$word};
665 # $log->debug("memoized: $word is $name") if $name;
666 4 ▶ return $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 4 4
667 4 $name = pick($map_data, $table_data, $level, $coordinates, $words, $word, $redirects);
668 4 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
669 4 $names{$word} = $name;
670 # $log->debug("$word is $name");
671 4 push(@descriptions, $name);
672 } elsif ($word =~ /^names for (\S+)/) {
673 0 my $key = $1; # "river"
674 # $log->debug("Looking at $key for $coordinates...");
675 0 ▶ if (my @lines = grep { $_->{type} eq $key } @{$extra->{$coordinates}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@lines) = grep({$_->{"type"} eq $key;} @{$$extra{$coordinates};})) 0 0 0
676 # $log->debug("...@lines");
677 # make sure all the lines (rivers, trails) are named
678 0 my @names = ();
679 0 for my $line (@lines) {
680 0 my $name = $line->{name};
681 0 ▶ if (not $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
682 0 ▶ $name ||= pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects);
Condition0%0/3
$name ||= pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects)
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects)
$name pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects)
Truth table0%0/3
A ||= B
A= $nameB= pick($map_data, $table_data, $level, $coordinates, $words, "name for $key", $redirects)
ABresult
1X1
011
000
683 0 $line->{name} = $name;
684 }
685 0 push(@names, $name);
686 }
687 0 my $list;
688 0 ▶ if (@names > 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@names > 2) { } 0 0 0
elsif (@names == 2) { } 0 0 0
689 0 $list = join(", ", @names[0 .. $#names-1], "and " . $names[-1]);
690 } elsif (@names == 2) {
691 0 $list = join(" and ", @names);
692 } else {
693 0 $log->error("$coordinates has merge but just one line (@lines)");
694 0 $list = shift(@names);
695 }
696 0 ▶ $log->error("$coordinates uses merging rule without names") unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
697 0 ▶ next unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 0 0 0
698 0 push(@descriptions, $list);
699 }
700 } elsif ($word =~ /^name for (\S+)/) {
701 0 my $key = $1; # "white" or "river"
702 # $log->debug("Looking at $key for $coordinates...");
703 0 ▶ if (my @lines = grep { $_->{type} eq $key } @{$extra->{$coordinates}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@lines) = grep({$_->{'type'} eq $key;} @{$$extra{$coordinates};})) { } 0 0 0
704 # for rivers and the like: "name for river"
705 0 for my $line (@lines) {
706 # $log->debug("Looking at $word for $coordinates...");
707 0 my $name = $line->{name};
708 # $log->debug("... we already have a name: $name") if $name;
709 # if a type appears twice for a hex, this returns the same name for all of them
710 0 ▶ return $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
711 0 $name = pick($map_data, $table_data, $level, $coordinates, $words, $word, $redirects);
712 # $log->debug("... we picked a new name: $name") if $name;
713 0 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
714 0 push(@descriptions, $name);
715 0 $line->{name} = $name;
716 0 $globals->{$key}->{$_} = $name for @{$line->{line}}[1..$#{$line->{line}}];
717 # name the first one without a name, don't keep adding names
718 0 last;
719 }
720 } else {
721 # regular features: "name for white big mountain"
722 0 my $name = $names{"$word: $coordinates"}; # "name for white big mountain: 0101"
723 # $log->debug("$word for $coordinates is $name") if $name;
724 0 ▶ return $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
725 0 $name = pick($map_data, $table_data, $level, $coordinates, $words, $word, $redirects);
726 # $log->debug("new $word for $coordinates is $name") if $name;
727 0 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
728 0 $names{"$word: $coordinates"} = $name;
729 0 push(@descriptions, $name);
730 0 ▶ spread_name($map_data, $coordinates, $word, $key, $name) if %$map_data;
Branch: 1 branch
BranchTrueFalseTotal
if %$map_data 0 0 0
731 }
732 } elsif ($word eq 'adjacent hex') {
733 # experimental
734 0 ▶ my $location = $coordinates eq 'no map' ? 'somewhere' : one(neighbours($map_data, $coordinates));
Branch: 1 branch
BranchTrueFalseTotal
$coordinates eq 'no map' ? : 0 0 0
735 0 $locals{$word} = $location;
736 0 return $location;
737 } elsif ($word =~ /^(?:nearby|other|later) ./) {
738 # skip on the first pass
739 2 return "「$word」";
740 } elsif ($word =~ /^append (.*)/) {
741 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $1, $redirects);
742 # remember it's legitimate to have no result for a table
743 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
744 0 $locals{$word} = $text;
745 0 push(@descriptions, "「append $text」");
746 } elsif ($word =~ /^same (.+)/) {
747 95 my $key = $1;
748 95 ▶ return $locals{$key}->[0] if exists $locals{$key} and ref($locals{$key}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if exists $locals{$key} and ref $locals{$key} eq "ARRAY" 0 95 95
Condition33%1/3
exists $locals{$key} and ref $locals{$key} eq "ARRAY"
!ll&&!rl&&r
0950
MC/DC0%0/2
exists $locals{$key} and ref $locals{$key} eq "ARRAY"
exists $locals{$key} ref $locals{$key} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $locals{$key}B= ref $locals{$key} eq "ARRAY"
ABresult
0X0
100
111
749 95 ▶ return $locals{$key} if exists $locals{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $locals{$key} 95 0 95
750 0 ▶ return $globals->{$key}->{global} if $globals->{$key} and $globals->{$key}->{global};
Branch: 1 branch
BranchTrueFalseTotal
if $globals->{$key} and $globals->{$key}{"global"} 0 0 0
Condition0%0/3
$globals->{$key} and $globals->{$key}{"global"}
!ll&&!rl&&r
000
MC/DC0%0/2
$globals->{$key} and $globals->{$key}{"global"}
$globals->{$key} $globals->{$key}{"global"}
Truth table0%0/3
A and B
A= $globals->{$key}B= $globals->{$key}{"global"}
ABresult
0X0
100
111
751 0 $log->warn("[same $key] is undefined for $coordinates, attempt picking a new one");
752 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
753 0 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) { } 0 0 0
754 0 $locals{$key} = $text;
755 0 push(@descriptions, $text . "*");
756 } else {
757 0 $log->error("[$key] is undefined for $coordinates");
758 0 push(@descriptions, "…");
759 }
760 } elsif ($word =~ /^(?:(here|global) )?with (.+?)(?: as (.+))?$/) {
761 3 my ($where, $key, $alias) = ($1, $2, $3);
762 3 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
763 3 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 3 3
764 3 $locals{$key} = [$text]; # start a new list
765 3 ▶ $locals{$alias} = $text if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 3 3
766 3 ▶ $globals->{$key}->{$coordinates} = $text if $where and $where eq 'here';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" 0 3 3
Condition33%1/3
$where and $where eq "here"
!ll&&!rl&&r
300
MC/DC0%0/2
$where and $where eq "here"
$where $where eq "here"
Truth table33%1/3
A and B
A= $whereB= $where eq "here"
ABresult
0X0
100
111
767 3 ▶ $globals->{$alias}->{$coordinates} = $text if $where and $where eq 'here' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" and $alias 0 3 3
Condition33%2/6
$where and $where eq "here" and $alias
!ll&&!rl&&r
300
$where and $where eq "here"
!ll&&!rl&&r
300
MC/DC0%0/3
$where and $where eq "here" and $alias
$where $where eq "here" $alias
Truth table25%1/4
A and B and C
A= $whereB= $where eq "here"C= $alias
ABCresult
0XX0
10X0
1100
1111
768 3 ▶ $globals->{$key}->{global} = $text if $where and $where eq 'global';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" 0 3 3
Condition33%1/3
$where and $where eq "global"
!ll&&!rl&&r
300
MC/DC0%0/2
$where and $where eq "global"
$where $where eq "global"
Truth table33%1/3
A and B
A= $whereB= $where eq "global"
ABresult
0X0
100
111
769 3 ▶ $globals->{$alias}->{global} = $text if $where and $where eq 'global' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" and $alias 0 3 3
Condition33%2/6
$where and $where eq "global" and $alias
!ll&&!rl&&r
300
$where and $where eq "global"
!ll&&!rl&&r
300
MC/DC0%0/3
$where and $where eq "global" and $alias
$where $where eq "global" $alias
Truth table25%1/4
A and B and C
A= $whereB= $where eq "global"C= $alias
ABCresult
0XX0
10X0
1100
1111
770 3 push(@descriptions, $text);
771 } elsif ($word =~ /^(?:(here|global) )?and (.+?)(?: as (.+))?$/) {
772 2 my ($where, $key, $alias) = ($1, $2, $3);
773 2 my $found = 0;
774 # limited attempts to find a unique entry for an existing list (instead of
775 # modifying the data structures)
776 2 for (1 .. 20) {
777 3 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
778 3 ▶ $log->warn("[and $key] is used before [with $key] is done in $coordinates") if ref $locals{$key} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $locals{$key} ne "ARRAY" 0 3 3
779 3 ▶ $locals{$key} = [$text] if ref $locals{$key} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $locals{$key} ne "ARRAY" 0 3 3
780 3 ▶ next if not $text or grep { $text eq $_ } @{$locals{$key}};
Branch: 1 branch
BranchTrueFalseTotal
if not $text or grep {$text eq $_;} @{$locals{$key};} 1 2 3
Condition66%2/3
not $text or grep {$text eq $_;} @{$locals{$key};}
l!l&&r!l&&!r
012
MC/DC50%1/2
not $text or grep {$text eq $_;} @{$locals{$key};}
not $text grep {$text eq $_;} @{$locals{$key};}
Truth table66%2/3
not(A) or B
A= not $textB= grep {$text eq $_;} @{$locals{$key};}
ABresult
1X1
011
000
781 2 push(@{$locals{$key}}, $text);
782 2 push(@descriptions, $text);
783 2 ▶ $locals{$alias} = $text if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 2 2
784 2 ▶ $globals->{$key}->{$coordinates} = $text if $where and $where eq 'here';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" 0 2 2
Condition33%1/3
$where and $where eq "here"
!ll&&!rl&&r
200
MC/DC0%0/2
$where and $where eq "here"
$where $where eq "here"
Truth table33%1/3
A and B
A= $whereB= $where eq "here"
ABresult
0X0
100
111
785 2 ▶ $globals->{$alias}->{$coordinates} = $text if $where and $where eq 'here' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" and $alias 0 2 2
Condition33%2/6
$where and $where eq "here" and $alias
!ll&&!rl&&r
200
$where and $where eq "here"
!ll&&!rl&&r
200
MC/DC0%0/3
$where and $where eq "here" and $alias
$where $where eq "here" $alias
Truth table25%1/4
A and B and C
A= $whereB= $where eq "here"C= $alias
ABCresult
0XX0
10X0
1100
1111
786 2 ▶ $globals->{$key}->{global} = $text if $where and $where eq 'global';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" 0 2 2
Condition33%1/3
$where and $where eq "global"
!ll&&!rl&&r
200
MC/DC0%0/2
$where and $where eq "global"
$where $where eq "global"
Truth table33%1/3
A and B
A= $whereB= $where eq "global"
ABresult
0X0
100
111
787 2 ▶ $globals->{$alias}->{global} = $text if $where and $where eq 'global' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" and $alias 0 2 2
Condition33%2/6
$where and $where eq "global" and $alias
!ll&&!rl&&r
200
$where and $where eq "global"
!ll&&!rl&&r
200
MC/DC0%0/3
$where and $where eq "global" and $alias
$where $where eq "global" $alias
Truth table25%1/4
A and B and C
A= $whereB= $where eq "global"C= $alias
ABCresult
0XX0
10X0
1100
1111
788 2 $found = 1;
789 2 last;
790 }
791 2 ▶ if (not $found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 2 2
792 0 $log->warn("[and $key] not unique in $coordinates");
793 0 push(@descriptions, "…");
794 }
795 } elsif ($word =~ /^capitalize (.+)/) {
796 1 my $key = $1;
797 1 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
798 1 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 1 1
799 1 $locals{$key} = $text;
800 1 push(@descriptions, ucfirst $text);
801 } elsif ($word =~ /^titlecase (.+)/) {
802 0 my $key = $1;
803 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
804 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
805 0 $locals{$key} = $text;
806 0 push(@descriptions, autoformat($text, { case => 'titlecase' }));
807 } elsif ($word =~ /^highlightcase (.+)/) {
808 0 my $key = $1;
809 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
810 0 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 0 0
811 0 $locals{$key} = $text;
812 0 push(@descriptions, autoformat($text, { case => 'highlight' }));
813 } elsif ($word =~ /^normalize-elvish (.+)/) {
814 2 my $key = $1;
815 2 my $text = normalize_elvish($key);
816 2 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 0 2 2
817 2 $locals{$key} = $text;
818 2 push(@descriptions, $text);
819 } elsif ($word =~ /^(?:(here|global) )?(?:(save|store|quote) )?(.+?)(?: as (.+))?$/) {
820 383 my ($where, $action, $key, $alias) = ($1, $2, $3, $4);
821 383 my $text;
822 383 ▶ if (not $action or $action eq "save") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $action or $action eq 'save') { } 353 30 383
Condition100%3/3
not $action or $action eq "save"
l!l&&r!l&&!r
350330
MC/DC100%2/2
not $action or $action eq "save"
not $action $action eq "save"
Truth table100%3/3
not(A) or B
A= not $actionB= $action eq "save"
ABresult
1X1
011
000
823 # no action and save are with lookup
824 353 $text = pick($map_data, $table_data, $level, $coordinates, $words, $key, $redirects);
825 } else {
826 # quote and store are without lookup
827 30 $text = $key;
828 }
829 383 ▶ next unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 86 297 383
830 297 $locals{$key} = $text;
831 297 ▶ $locals{$alias} = $text if $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $alias 34 263 297
832 297 ▶ $globals->{$key}->{$coordinates} = $text if $where and $where eq 'here';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" 4 293 297
Condition100%3/3
$where and $where eq "here"
!ll&&!rl&&r
28764
MC/DC100%2/2
$where and $where eq "here"
$where $where eq "here"
Truth table100%3/3
A and B
A= $whereB= $where eq "here"
ABresult
0X0
100
111
833 297 ▶ $globals->{$alias}->{$coordinates} = $text if $where and $where eq 'here' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "here" and $alias 3 294 297
Condition100%6/6
$where and $where eq "here" and $alias
!ll&&!rl&&r
29313
$where and $where eq "here"
!ll&&!rl&&r
28764
MC/DC100%3/3
$where and $where eq "here" and $alias
$where $where eq "here" $alias
Truth table100%4/4
A and B and C
A= $whereB= $where eq "here"C= $alias
ABCresult
0XX0
10X0
1100
1111
834 297 ▶ $globals->{$key}->{global} = $text if $where and $where eq 'global';
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" 6 291 297
Condition100%3/3
$where and $where eq "global"
!ll&&!rl&&r
28746
MC/DC100%2/2
$where and $where eq "global"
$where $where eq "global"
Truth table100%3/3
A and B
A= $whereB= $where eq "global"
ABresult
0X0
100
111
835 297 ▶ $globals->{$alias}->{global} = $text if $where and $where eq 'global' and $alias;
Branch: 1 branch
BranchTrueFalseTotal
if $where and $where eq "global" and $alias 6 291 297
Condition83%5/6
$where and $where eq "global" and $alias
!ll&&!rl&&r
29106
$where and $where eq "global"
!ll&&!rl&&r
28746
MC/DC66%2/3
$where and $where eq "global" and $alias
$where $where eq "global" $alias
Truth table75%3/4
A and B and C
A= $whereB= $where eq "global"C= $alias
ABCresult
0XX0
10X0
1100
1111
836 297 ▶ push(@descriptions, $text) if not $action or $action eq "quote";
Branch: 1 branch
BranchTrueFalseTotal
if not $action or $action eq "quote" 264 33 297
Condition66%2/3
not $action or $action eq "quote"
l!l&&r!l&&!r
264033
MC/DC50%1/2
not $action or $action eq "quote"
not $action $action eq "quote"
Truth table66%2/3
not(A) or B
A= not $actionB= $action eq "quote"
ABresult
1X1
011
000
837 ▶ } elsif ($level > 1 and not exists $table_data->{$word} and not $locals{$word}) {
Condition0%0/6
$level > 1 and not exists $table_data->{$word} and not $locals{$word}
!ll&&!rl&&r
000
$level > 1 and not exists $table_data->{$word}
!ll&&!rl&&r
000
MC/DC0%0/3
$level > 1 and not exists $table_data->{$word} and not $locals{$word}
$level > 1 not exists $table_data->{$word} not $locals{$word}
Truth table0%0/4
A and not(B) and not(C)
A= $level > 1B= not exists $table_data->{$word}C= not $locals{$word}
ABCresult
0XX0
10X0
1100
1111
838 # on level one, many terrain types do not exist (e.g. river-start)
839 0 $log->error("unknown table for $coordinates/$level: $word");
840 ▶ } elsif ($level > 1 and not $table_data->{$word} and not $locals{$word}) {
Condition0%0/6
$level > 1 and not $table_data->{$word} and not $locals{$word}
!ll&&!rl&&r
000
$level > 1 and not $table_data->{$word}
!ll&&!rl&&r
000
MC/DC0%0/3
$level > 1 and not $table_data->{$word} and not $locals{$word}
$level > 1 not $table_data->{$word} not $locals{$word}
Truth table0%0/4
A and not(B) and not(C)
A= $level > 1B= not $table_data->{$word}C= not $locals{$word}
ABCresult
0XX0
10X0
1100
1111
841 # on level one, many terrain types do not exist (e.g. river-start)
842 0 $log->error("empty table for $coordinates/$level: $word");
843 } else {
844 0 my $text = pick($map_data, $table_data, $level, $coordinates, $words, $word, $redirects);
845 # remember it's legitimate to have no result for a table, and remember we
846 # cannot use a local with the same name that's defined because sometimes
847 # locals are simply defined as "1" since they start out as "words" and I
848 # don't want to make "1" a special case to ignore, here
849 0 ▶ next unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
850 0 $locals{$word} = $text;
851 0 push(@descriptions, $text);
852 }
853 }
854 432 return join(' ', @descriptions);
855 }
856  
857 =item describe_text
858  
859 This function does what C<describe> does, but for simple text without hex
860 coordinates.
861  
862 =cut
863  
864 sub describe_text {
865 5 ▶ my $input = shift;
Subroutine describe_text: called CC=2 SCAR=6.9
Pod documented
866 5 my $table_data = shift;
867 5 my $redirects = shift;
868 5 my @descriptions;
869 5 init();
870 5 for my $text (split(/\r?\n/, $input)) {
871 # recusion level 2 makes sure we don't reset %locals
872 16 $text =~ s/\[(.*?)\]/describe({},$table_data,2,"no map",[$1],$redirects)/ge;
873 16 push(@descriptions, process($text, $redirects));
874 16 %locals = (); # reset once per paragraph
875 }
876 5 return \@descriptions;
877 }
878  
879 =item normalize_elvish
880  
881 We do some post-processing of words, inspired by these two web pages, but using
882 our own replacements.
883 http://sindarinlessons.weebly.com/37---how-to-make-names-1.html
884 http://sindarinlessons.weebly.com/38---how-to-make-names-2.html
885  
886 =cut
887  
888 sub normalize_elvish {
889 2 ▶ my $original = shift;
Subroutine normalize_elvish: called CC=1 SCAR=0.0
Pod documented
890 2 my $name = $original;
891  
892 2 $name =~ s/(.) \1/$1/g;
893 2 $name =~ s/d t/d/g;
894 2 $name =~ s/a ui/au/g;
895 2 $name =~ s/nd m/dhm/g;
896 2 $name =~ s/n?d w/dhw/g;
897 2 $name =~ s/r gw/rw/g;
898 2 $name =~ s/^nd/d/;
899 2 $name =~ s/^ng/g/;
900 2 $name =~ s/th n?d/d/g;
901 2 $name =~ s/dh dr/dhr/g;
902 2 $name =~ s/ //g;
903  
904 2 $name =~ tr/âêîôûŷ/aeioúi/;
905 2 $name =~ s/ll$/l/;
906 2 $name =~ s/ben$/wen/g;
907 2 $name =~ s/bwi$/wi/;
908 2 $name =~ s/[^aeiouúi]ndil$/dil/g;
909 2 $name =~ s/ae/aë/g;
910 2 $name =~ s/ea/ëa/g;
911  
912 2 $name = ucfirst($name);
913  
914 # $log->debug("Elvish normalize: $original → $name");
915 2 return $name;
916 }
917  
918 =item process
919  
920 We do some post-processing after the description has been assembled: we move all
921 the IMG tags in a SPAN element with class "images". This makes it easier to lay
922 out the result using CSS.
923  
924 =cut
925  
926 sub process {
927 19 ▶ my $text = shift;
Subroutine process: called CC=2 SCAR=6.9
Pod documented
928 19 my $images = shift;
929 19 ▶ if ($images) {
Branch: 1 branch
BranchTrueFalseTotal
if ($images) { } 10 9 19
930 10 $text =~ s/(<img[^>]+?>)/<span class="images">$1<\/span>/g;
931 } else {
932 9 $text =~ s/(<img[^>]+?>)//g;
933 }
934 # fix whilespace at the end of spans
935 19 $text =~ s/\s+<\/span>/<\/span> /g;
936 # strip empty paragraphs
937 19 $text =~ s/<p>\s*<\/p>//g;
938 19 $text =~ s/<p>\s*<p>/<p>/g;
939 # strip other empty elements
940 19 $text =~ s/<em><\/em>//g;
941 19 return $text;
942 }
943  
944 =item resolve_appends
945  
946 This removes text marked for appending and adds it at the end of a hex
947 description. This modifies the third parameter, C<$descriptions>.
948  
949 =cut
950  
951 sub resolve_appends {
952 1 ▶ my $map_data = shift;
Subroutine resolve_appends: called CC=3 SCAR=12.0
Pod documented
953 1 my $table_data = shift;
954 1 my $descriptions = shift;
955 1 my $redirects = shift;
956 1 my $text;
957 1 for my $coord (keys %$descriptions) {
958 3 ▶ while ($descriptions->{$coord} =~ s/「append ([^][」]*)」/$text = $1; ""/e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descriptions->{$coord} =~ s/\x{ff62}append ([^][\x{ff63}]*)\x{ff63}/$text = $1; "";/eu) 0 3 3
959 0 $descriptions->{$coord} .= ' ' . $text;
960 }
961 }
962 }
963  
964 =item resolve_nearby
965  
966 We have nearly everything resolved except for references starting with the word
967 "nearby" because these require all of the other data to be present. This
968 modifies the third parameter, C<$descriptions>.
969  
970 =cut
971  
972 sub resolve_nearby {
973 1 ▶ my $map_data = shift;
Subroutine resolve_nearby: called CC=4 SCAR=14.9
Pod documented
974 1 my $table_data = shift;
975 1 my $descriptions = shift;
976 1 my $redirects = shift;
977 1 for my $coord (keys %$descriptions) {
978 $descriptions->{$coord} =~
979 0 ▶ s/「nearby ([^][」]*)」/closest($map_data,$table_data,$coord,$1, $redirects) or '…'/ge
Branch: 1 branch
BranchTrueFalseTotal
closest($map_data, $table_data, $coord, $1, $redirects) or "\x{2026}" 0 0 0
980 3 for 1 .. 2; # two levels deep of 「nearby ...」
981 3 $descriptions->{$coord} =~ s!( \(<a href="#desc\d+">\d+</a>\))</em>!</em>$1!g; # fixup
982 }
983 }
984  
985 =item closest
986  
987 This picks the closest instance of whatever we're looking for, but not from the
988 same coordinates, obviously.
989  
990 =cut
991  
992 sub closest {
993 0 ▶ my $map_data = shift;
Subroutine closest: not called CC=4 SCAR=30.0
Pod documented
994 0 my $table_data = shift;
995 0 my $coordinates = shift;
996 0 my $key = shift;
997 0 my $redirects = shift;
998 0 ▶ my @coordinates = grep { $_ ne $coordinates and $_ ne 'global' } keys %{$globals->{$key}};
Branch: 1 branch
BranchTrueFalseTotal
$_ ne $coordinates and $_ ne "global" 0 0 0
999 0 ▶ if (not @coordinates) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@coordinates) 0 0 0
1000 0 $log->info("Did not find any hex with $key ($coordinates)");
1001 0 return "…";
1002 }
1003 0 ▶ if ($coordinates !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $coordinates =~ /^\d+$/u) { } 0 0 0
1004 # if $coordinates is "TOP" or "END" or something like that, we cannot get
1005 # the closest one and we need to return a random one
1006 0 my $random = one(@coordinates);
1007 0 return $globals->{$key}->{$random}
1008 . qq{ (<a href="#desc$random">$random</a>)}; # see resolve_later!
1009 } else {
1010 0 @coordinates = sort { distance($coordinates, $a) <=> distance($coordinates, $b) } @coordinates;
1011 # the first one is the closest
1012 0 return $globals->{$key}->{$coordinates[0]}
1013 . qq{ (<a href="#desc$coordinates[0]">$coordinates[0]</a>)}; # see resolve_later!
1014 }
1015 }
1016  
1017 =item distance
1018  
1019 Returns the distance between two hexes. Either provide two coordinates (strings
1020 in the form "0101", "0102") or four numbers (1, 1, 1, 2).
1021  
1022 =cut
1023  
1024 sub distance {
1025 0 ▶ my ($x1, $y1, $x2, $y2) = @_;
Subroutine distance: not called CC=5 SCAR=34.0
Pod documented
1026 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 0 0
1027 0 ($x1, $y1, $x2, $y2) = map { xy($_) } @_;
1028 }
1029 # transform the coordinate system into a decent system with one axis tilted by
1030 # 60°
1031 0 $y1 = $y1 - POSIX::ceil($x1/2);
1032 0 $y2 = $y2 - POSIX::ceil($x2/2);
1033 0 ▶ if ($x1 > $x2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 0 0 0
1034 # only consider moves from left to right and transpose start and
1035 # end point to make it so
1036 0 my ($t1, $t2) = ($x1, $y1);
1037 0 ($x1, $y1) = ($x2, $y2);
1038 0 ($x2, $y2) = ($t1, $t2);
1039 }
1040 0 ▶ if ($y2>=$y1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 >= $y1) { } 0 0 0
1041 # if it the move has a downwards component add Δx and Δy
1042 0 return $x2-$x1 + $y2-$y1;
1043 } else {
1044 # else just take the larger of Δx and Δy
1045 0 ▶ return $x2-$x1 > $y1-$y2 ? $x2-$x1 : $y1-$y2;
Branch: 1 branch
BranchTrueFalseTotal
$x2 - $x1 > $y1 - $y2 ? : 0 0 0
1046 }
1047 }
1048  
1049 =item resolve_other
1050  
1051 This is a second phase. We have nearly everything resolved except for references
1052 starting with the word "other" because these require all of the other data to
1053 be present. This modifies the third parameter, C<$descriptions>.
1054  
1055 =cut
1056  
1057 sub resolve_other {
1058 1 ▶ my $map_data = shift;
Subroutine resolve_other: called CC=3 SCAR=11.8
Pod documented
1059 1 my $table_data = shift;
1060 1 my $descriptions = shift;
1061 1 my $redirects = shift;
1062 1 for my $coord (keys %$descriptions) {
1063 3 $descriptions->{$coord} =~
1064 0 ▶ s/「other ([^][」]*)」/some_other($map_data,$table_data,$coord,$1, $redirects) or '…'/ge;
Branch: 1 branch
BranchTrueFalseTotal
some_other($map_data, $table_data, $coord, $1, $redirects) or "\x{2026}" 0 0 0
1065 3 $descriptions->{$coord} =~ s!( \(<a href="#desc\d+">\d+</a>\))</em>!</em>$1!g; # fixup
1066 }
1067 }
1068  
1069 =item some_other
1070  
1071 This picks some other instance of whatever we're looking for, irrespective of distance.
1072  
1073 =cut
1074  
1075 sub some_other {
1076 0 ▶ my $map_data = shift;
Subroutine some_other: not called CC=2 SCAR=17.9
Pod documented
1077 0 my $table_data = shift;
1078 0 my $coordinates = shift;
1079 0 my $key = shift;
1080 0 my $redirects = shift;
1081 # make sure we don't pick the same location!
1082 0 my @coordinates = grep !/$coordinates/, keys %{$globals->{$key}};
1083 0 ▶ if (not @coordinates) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@coordinates) 0 0 0
1084 0 $log->info("Did not find any other hex with $key");
1085 0 return "…";
1086 }
1087 # just pick a random one
1088 0 my $other = one(@coordinates);
1089 0 return $globals->{$key}->{$other}
1090 . qq{ (<a href="#desc$other">$other</a>)}; # see resolve_later!
1091 }
1092  
1093  
1094 =item resolve_later
1095  
1096 This is a second phase. We have nearly everything resolved except for references
1097 starting with the word "later" because these require all of the other data to be
1098 present. This modifies the third parameter, C<$descriptions>. Use this for
1099 recursive lookup involving "nearby" and "other".
1100  
1101 This also takes care of hex references introduced by "nearby" and "other". This
1102 is also why we need to take extra care to call C<quotemeta> on various strings
1103 we want to search and replace: these hex references contain parenthesis!
1104  
1105 =cut
1106  
1107 sub resolve_later {
1108 1 ▶ my $map_data = shift;
Subroutine resolve_later: called CC=7 SCAR=33.2
Pod documented
1109 1 my $table_data = shift;
1110 1 my $descriptions = shift;
1111 1 my $redirects = shift;
1112 1 for my $coord (keys %$descriptions) {
1113 3 ▶ while ($descriptions->{$coord} =~ /「later ([^][」]*)」/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($descriptions->{$coord} =~ /\x{ff62}later ([^][\x{ff63}]*)\x{ff63}/u) 0 3 3
1114 0 my $words = $1;
1115 0 my ($ref) = $words =~ m!( \(<a href=".*">.*</a>\))!;
1116 0 ▶ $ref //= ''; # but why should it ever be empty?
Condition0%0/2
$ref //= ""
l!l
00
MC/DC0%0/1
$ref //= ""
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
1117 0 my $key = $words;
1118 0 my $re = quotemeta($ref);
1119 0 ▶ $key =~ s/$re// if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 0 0 0
1120 0 $re = quotemeta($words);
1121 0 my $result = $descriptions->{$coord} =~
1122 0 ▶ s/「later $re」/describe($map_data,$table_data,1,$coord,[$key], $redirects) . $ref or '…'/ge;
Branch: 1 branch
BranchTrueFalseTotal
describe($map_data, $table_data, 1, $coord, [$key], $redirects) . $ref or "\x{2026}" 0 0 0
1123 0 ▶ if (not $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
1124 0 $log->error("Could not resolve later reference in '$words'");
1125 0 last; # avoid infinite loops!
1126 }
1127 }
1128 }
1129 }
1130  
1131 =item describe_map
1132  
1133 This is one of the top entry points: it simply calls C<describe> for every hex
1134 in C<$map_data> and calls C<process> on the result. All the texts are collected
1135 into a new hash where the hex coordinates are the key and the generated
1136 description is the value.
1137  
1138 =cut
1139  
1140 sub describe_map {
1141 1 ▶ my $map_data = shift;
Subroutine describe_map: called CC=5 SCAR=16.2
Pod documented
1142 1 my $table_data = shift;
1143 1 my $redirects = shift;
1144 1 my %descriptions;
1145 # first, add special rule for TOP and END keys which the description template knows
1146 1 for my $coords (qw(TOP END)) {
1147 # with redirects means we keep images
1148 2 my $description =
1149 process(describe($map_data, $table_data, 1,
1150 $coords, [$coords], $redirects), $redirects);
1151 # only set the TOP and END key if there is a description
1152 2 ▶ $descriptions{$coords} = $description if $description;
Branch: 1 branch
BranchTrueFalseTotal
if $description 2 0 2
1153 }
1154 # shuffle sort the coordinates so that it is reproducibly random
1155 1 for my $coord (shuffle sort keys %$map_data) {
1156 # with redirects means we keep images
1157 my $description =
1158 process(describe($map_data, $table_data, 1,
1159 1 $coord, $map_data->{$coord}, $redirects), $redirects);
1160 # only set the description if there is one (empty hexes are not listed)
1161 1 ▶ $descriptions{$coord} = $description if $description;
Branch: 1 branch
BranchTrueFalseTotal
if $description 1 0 1
1162 }
1163 1 resolve_nearby($map_data, $table_data, \%descriptions, $redirects);
1164 1 resolve_other($map_data, $table_data, \%descriptions, $redirects);
1165 1 resolve_later($map_data, $table_data, \%descriptions, $redirects);
1166 # as append might include the items above, it must come last
1167 1 resolve_appends($map_data, $table_data, \%descriptions, $redirects);
1168 1 return \%descriptions;
1169 }
1170  
1171 =item add_labels
1172  
1173 This function is used after generating the descriptions to add the new names of
1174 rivers and trails to the existing map.
1175  
1176 =cut
1177  
1178 sub add_labels {
1179 0 ▶ my $map = shift;
Subroutine add_labels: not called CC=1 SCAR=6.9
Pod documented
1180 0 $map =~ s/$line_re/get_label($1,$2)/ge;
1181 0 return $map;
1182 }
1183  
1184 =item get_label
1185  
1186 This function returns the name of a line.
1187  
1188 =cut
1189  
1190 sub get_label {
1191 0 ▶ my $map_line = shift;
Subroutine get_label: not called CC=5 SCAR=34.0
Pod documented
1192 0 my $type = shift;
1193 0 my @hexes = split(/-/, $map_line);
1194 LINE:
1195 0 for my $line (@{$extra->{$hexes[0]}}) {
1196 0 ▶ next unless $line->{type} eq $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $line->{"type"} eq $type 0 0 0
1197 0 for my $hex (@hexes) {
1198 0 my @line = @{$line->{line}};
1199 0 ▶ next LINE unless grep(/$hex/, @line);
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$hex/u, @line 0 0 0
1200 }
1201 0 my $label = $line->{name};
1202 0 return qq{$map_line $type "$label"};
1203 }
1204 0 return qq{$map_line $type};
1205 }
1206  
1207 =item xy
1208  
1209 This is a helper function to turn "0101" into ("01", "01") which is equivalent
1210 to (1, 1).
1211  
1212 =cut
1213  
1214 sub xy {
1215 0 ▶ my $coordinates = shift;
Subroutine xy: not called CC=1 SCAR=6.9
Pod documented
1216 0 return (substr($coordinates, 0, 2), substr($coordinates, 2));
1217 }
1218  
1219 =item coordinates
1220  
1221 This is a helper function to turn (1, 1) back into "0101".
1222  
1223 =cut
1224  
1225 sub coordinates {
1226 0 ▶ my ($x, $y) = @_;
Subroutine coordinates: not called CC=1 SCAR=6.9
Pod documented
1227 0 return sprintf("%02d%02d", $x, $y);
1228 }
1229  
1230 =item neighbour
1231  
1232 This is a helper function that takes the coordinates of a hex, a reference like
1233 [1,1] or regular coordinates like "0101", and a direction from 0 to 5, and
1234 returns the coordinates of the neighbouring hex in that direction.
1235  
1236 =cut
1237  
1238 6 my $delta = [[[-1, 0], [ 0, -1], [+1, 0], [+1, +1], [ 0, +1], [-1, +1]], # x is even
1239 [[-1, -1], [ 0, -1], [+1, -1], [+1, 0], [ 0, +1], [-1, 0]]]; # x is odd
1240  
1241 sub neighbour {
1242 # $hex is [x,y] or "0101" and $i is a number 0 .. 5
1243 0 ▶ my ($hex, $i) = @_;
Subroutine neighbour: not called CC=2 SCAR=17.9
Pod documented
1244 0 ▶ $hex = [xy($hex)] unless ref $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hex 0 0 0
1245 # return is a string like "0102"
1246 0 return coordinates(
1247 $hex->[0] + $delta->[$hex->[0] % 2]->[$i]->[0],
1248 $hex->[1] + $delta->[$hex->[0] % 2]->[$i]->[1]);
1249 }
1250  
1251 =item neighbours
1252  
1253 This is a helper function that takes map_data and the coordinates of a hex, a
1254 reference like [1,1] or regular coordinates like "0101", and returns a list of
1255 existing neighbours, or the string "[…]". This makes a difference at the edge of
1256 the map.
1257  
1258 =cut
1259  
1260 sub neighbours {
1261 0 ▶ my $map_data = shift;
Subroutine neighbours: not called CC=5 SCAR=34.0
Pod documented
1262 0 my $hex = shift;
1263 0 my @neighbours;
1264 0 ▶ $hex = [xy($hex)] unless ref $hex;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hex 0 0 0
1265 0 for my $i (0 .. 5) {
1266 0 my $neighbour = neighbour($hex, $i);
1267 # $log->debug($neighbour);
1268 0 ▶ push(@neighbours, $neighbour) if $map_data->{$neighbour};
Branch: 1 branch
BranchTrueFalseTotal
if $map_data->{$neighbour} 0 0 0
1269 }
1270 0 ▶ return "..." unless @neighbours;
Branch: 1 branch
BranchTrueFalseTotal
unless @neighbours 0 0 0
1271 0 return @neighbours;
1272 }
1273  
1274 =item one
1275  
1276 This is a helper function that picks a random element from a list. This works
1277 both for actual lists and for references to lists.
1278  
1279 =cut
1280  
1281 sub one {
1282 0 ▶ my @arr = @_;
Subroutine one: not called CC=3 SCAR=24.8
Pod documented
1283 0 ▶ @arr = @{$arr[0]} if @arr == 1 and ref $arr[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if @arr == 1 and ref $arr[0] eq "ARRAY" 0 0 0
Condition0%0/3
@arr == 1 and ref $arr[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@arr == 1 and ref $arr[0] eq "ARRAY"
@arr == 1 ref $arr[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @arr == 1B= ref $arr[0] eq "ARRAY"
ABresult
0X0
100
111
1284 0 return $arr[int(rand(scalar @arr))];
1285 }
1286  
1287 =item one_step_to
1288  
1289 Given a hex to start from, check all directions and figure out which neighbour
1290 is closer to your destination. Return the coordinates of this neighbour.
1291  
1292 =cut
1293  
1294 sub one_step_to {
1295 0 ▶ my $from = shift;
Subroutine one_step_to: not called CC=4 SCAR=30.0
Pod documented
1296 0 my $to = shift;
1297 0 my ($min, $best);
1298 0 for my $i (0 .. 5) {
1299 # make a new guess
1300 0 my ($x, $y) = ($from->[0] + $delta->[$from->[0] % 2]->[$i]->[0],
1301 $from->[1] + $delta->[$from->[0] % 2]->[$i]->[1]);
1302 0 my $d = ($to->[0] - $x) * ($to->[0] - $x)
1303 + ($to->[1] - $y) * ($to->[1] - $y);
1304 0 ▶ if (!defined($min) || $d < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $min or $d < $min) 0 0 0
Condition0%0/3
not defined $min or $d < $min
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $min or $d < $min
not defined $min $d < $min
Truth table0%0/3
not(A) or B
A= not defined $minB= $d < $min
ABresult
1X1
011
000
1305 0 $min = $d;
1306 0 $best = [$x, $y];
1307 }
1308 }
1309 0 return $best;
1310 }
1311  
1312 =item compute_missing_points
1313  
1314 Return a list of coordinates in string form. Thus, given a list like ("0302",
1315 "0101") it will return ("0302", "0201", "0101").
1316  
1317 =cut
1318  
1319 sub compute_missing_points {
1320 0 ▶ my @result = ($_[0]); # "0101" not [01,02]
Subroutine compute_missing_points: not called CC=4 SCAR=30.0
Pod documented
1321 0 my @points = map { [xy($_)] } @_;
1322 # $log->debug("Line: " . join(", ", map { coordinates(@$_) } @points));
1323 0 my $from = shift(@points);
1324 0 ▶ while (@points) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points) 0 0 0
1325 # $log->debug("Going from " . coordinates(@$from) . " to " . coordinates(@{$points[0]}));
1326 0 $from = one_step_to($from, $points[0]);
1327 0 ▶ shift(@points) if $from->[0] == $points[0]->[0] and $from->[1] == $points[0]->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $from->[0] == $points[0][0] and $from->[1] == $points[0][1] 0 0 0
Condition0%0/3
$from->[0] == $points[0][0] and $from->[1] == $points[0][1]
!ll&&!rl&&r
000
MC/DC0%0/2
$from->[0] == $points[0][0] and $from->[1] == $points[0][1]
$from->[0] == $points[0][0] $from->[1] == $points[0][1]
Truth table0%0/3
A and B
A= $from->[0] == $points[0][0]B= $from->[1] == $points[0][1]
ABresult
0X0
100
111
1328 0 push(@result, coordinates(@$from));
1329 }
1330 0 return @result;
1331 }
1332  
1333 =item same_direction
1334  
1335 Given two linear structures and a point of contact, return 1 if the these
1336 objects go in the same direction on way or the other.
1337  
1338 =cut
1339  
1340 sub same_direction {
1341 0 ▶ my $coord = shift;
Subroutine same_direction: not called CC=12 SCAR=50.5
Pod documented
1342 0 my $line1 = shift;
1343 0 my $line2 = shift;
1344 # $log->debug("same_direction: $coord, @$line1 and @$line2");
1345 # this code assumes that a line starts with $type at index 0
1346 0 my $j;
1347 0 for my $i (1 .. $#$line1) {
1348 0 ▶ if ($line1->[$i] eq $coord) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line1->[$i] eq $coord) 0 0 0
1349 0 $j = $i;
1350 0 last;
1351 }
1352 }
1353 # $log->debug("same_direction: $coord has index $j in @$line1");
1354 0 for my $i1 ($j - 1, $j + 1) {
1355 0 ▶ next if $i1 == 0 or $i1 > $#$line1;
Branch: 1 branch
BranchTrueFalseTotal
if $i1 == 0 or $i1 > $line1->$#* 0 0 0
Condition0%0/3
$i1 == 0 or $i1 > $line1->$#*
l!l&&r!l&&!r
000
MC/DC0%0/2
$i1 == 0 or $i1 > $line1->$#*
$i1 == 0 $i1 > $line1->$#*
Truth table0%0/3
A or B
A= $i1 == 0B= $i1 > $line1->$#*
ABresult
1X1
011
000
1356 0 my $next = $line1->[$i1];
1357 0 for my $i2 (1 .. $#$line2) {
1358 0 ▶ if ($line2->[$i2] eq $coord) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line2->[$i2] eq $coord) 0 0 0
1359 0 ▶ if ($line2->[$i2-1] and $next eq $line2->[$i2-1]
Branch: 1 branch
BranchTrueFalseTotal
if ($$line2[$i2 - 1] and $next eq $$line2[$i2 - 1] or $$line2[$i2 + 1] and $next eq $$line2[$i2 + 1]) 0 0 0
Condition0%0/9
$$line2[$i2 - 1] and $next eq $$line2[$i2 - 1] or $$line2[$i2 + 1] and $next eq $$line2[$i2 + 1]
l!l&&r!l&&!r
000
$$line2[$i2 - 1] and $next eq $$line2[$i2 - 1]
!ll&&!rl&&r
000
$$line2[$i2 + 1] and $next eq $$line2[$i2 + 1]
!ll&&!rl&&r
000
MC/DC0%0/4
$$line2[$i2 - 1] and $next eq $$line2[$i2 - 1] or $$line2[$i2 + 1] and $next eq $$line2[$i2 + 1]
$$line2[$i2 - 1] $next eq $$line2[$i2 - 1] $$line2[$i2 + 1] $next eq $$line2[$i2 + 1]
Truth table0%0/7
A and B or C and D
A= $$line2[$i2 - 1]B= $next eq $$line2[$i2 - 1]C= $$line2[$i2 + 1]D= $next eq $$line2[$i2 + 1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1360 or $line2->[$i2+1] and $next eq $line2->[$i2+1]) {
1361 # $log->debug("same direction at $coord: @$line1 and @$line2");
1362 0 return 1;
1363 }
1364 }
1365 }
1366 }
1367 0 return 0;
1368 }
1369  
1370 =item spread_name
1371  
1372 This function is used to spread a name along terrain features.
1373  
1374 =cut
1375  
1376 sub spread_name {
1377 0 ▶ my $map_data = shift;
Subroutine spread_name: not called CC=7 SCAR=40.3
Pod documented
1378 0 my $coordinates = shift;
1379 0 my $word = shift; # "name for white big mountain"
1380 0 my $key = shift; # "white"
1381 0 my @keys = split(/\//, $key); # ("white")
1382 0 my $name = shift; # "Vesuv"
1383 0 my %seen = ($coordinates => 1);
1384 0 $globals->{$key}->{$coordinates} = $name;
1385 # $log->debug("$word: $coordinates = $name");
1386 0 my @queue = map { neighbour($coordinates, $_) } 0..5;
1387 0 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 0 0 0
1388 # $log->debug("Working on the first item of @queue");
1389 0 my $coord = shift(@queue);
1390 0 ▶ next if $seen{$coord} or not $map_data->{$coord};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$coord} or not $map_data->{$coord} 0 0 0
Condition0%0/3
$seen{$coord} or not $map_data->{$coord}
l!l&&r!l&&!r
000
MC/DC0%0/2
$seen{$coord} or not $map_data->{$coord}
$seen{$coord} not $map_data->{$coord}
Truth table0%0/3
A or not(B)
A= $seen{$coord}B= not $map_data->{$coord}
ABresult
1X1
011
000
1391 0 $seen{$coord} = 1;
1392 0 ▶ if (intersect(@keys, @{$map_data->{$coord}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&intersect(\@keys, \@{$$map_data{$coord};})) 0 0 0
1393 $log->error("$word for $coord is already something else")
1394 0 ▶ if $names{"$word for $coord"};
Branch: 1 branch
BranchTrueFalseTotal
if $names{"$word for $coord"} 0 0 0
1395 0 $names{"$word: $coord"} = $name; # "name for white big mountain: 0102"
1396 # $log->debug("$coord: $name for @keys");
1397 0 $globals->{$_}->{$coord} = $name for @keys;
1398 # $log->debug("$word: $coord = $name");
1399 0 push(@queue, map { neighbour($coord, $_) } 0..5);
1400 }
1401 }
1402 }
1403  
1404 =item markdown
1405  
1406 This allows us to generate Markdown output.
1407  
1408 =cut
1409  
1410 sub markdown {
1411 5 ▶ my $descriptions = shift;
Subroutine markdown: called CC=2 SCAR=7.0
Pod documented
1412 5 ▶ my $separator = shift || "\n\n---\n\n";
Condition100%2/2
shift() || "\n\n---\n\n"
l!l
14
MC/DC100%1/1
shift() || "\n\n---\n\n"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1413 my @paragraphs = map {
1414 # remove inline images
1415 5 s!<img[^>]*>!!g;
1416 # empty spans left after img has been removed
1417 9 s!<span[^>]*>\s*</span>!!g;
1418 # remaining spans result in Japanese brackets around their text
1419 9 s!<span[^>]*>\s*!「!g;
1420 9 s!\s*</span>!」!g;
1421 # emphasis
1422 9 s!</?(strong|b)>!**!g;
1423 9 s!</?(em|i)>!*!g;
1424 9 s!</?u>!_!g;
1425 # remove links but leave their text
1426 9 s!</?a\b[^>]*>!!g;
1427 # closing paragraph tags are optional
1428 9 s!</p>!!g;
1429 # paragraph breaks
1430 9 s!<p.*?>!\n\n!g;
1431 # blockquotes
1432 9 s!<blockquote>(.*?)</blockquote>!local $_ = $1; s/^/\n> /g; $_!gem;
1433 # unreplaced references (nearby, other, later)
1434 9 s!(.*?)!$1!g;
1435 # return what's left
1436 9 markdown_lists($_);
1437 } @$descriptions;
1438 5 return join($separator, @paragraphs);
1439 }
1440  
1441 sub markdown_lists {
1442 9 ▶ $_ = shift;
Subroutine markdown_lists: called CC=8 SCAR=29.6
Pod undocumented
1443 9 my ($str, @list);
1444 9 for (split(/(<.*?>)/)) {
1445 128 ▶ if (/^<ol.*>$/) { unshift(@list, '1.'); $str .= "\n" }
Branch: 5 branches
BranchTrueFalseTotal
if (/^<ol.*>$/u) { } 0 128 128
elsif (/^<ul.*>$/u) { } 0 128 128
elsif (/^<li>$/u) { } 0 128 128
elsif (m[^</(ol|ul)>$]u) { } 0 128 128
elsif (m[^</li>$]u) { } 0 128 128
1446 0 elsif (/^<ul.*>$/) { unshift(@list, '*'); $str .= "\n" }
1447 0 elsif (/^<li>$/) { $str .= " " x (4 * @list) . $list[0] . " " }
1448 0 elsif (/^<\/(ol|ul)>$/) { shift(@list) }
1449 0 ▶ elsif (/^<\/li>$/) { $str .= "\n" unless $str =~ /\n$/ }
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\n$/u 0 0 0
1450 128 else { $str .= $_ }
1451 }
1452 9 return $str;
1453 }
1454  
1455 6 1;
↑ Summary blib/lib/Game/HexDescribe.pm »