5 use experimental 'switch';
6 use lib 'lib'; # make runnable for simple cases
9 use Getopt::Long 2.33 qw(HelpMessage :config bundling);
10 use Games::Wormy::TICalcLevels;
11 use Games::Wormy::WormEdit;
13 our $VERSION = '1.07';
16 'format|f=s', # output type
17 'raw|r!', # compatibility for yaml format
18 'version=i', # force version
19 'levels|render:i', # image of level(s)
20 'output|o=s', # output file
21 ) or HelpMessage(-exitval => 2);
22 $opt{format} //= 'yaml' if $opt{raw};
23 $opt{format} //= 'pnm' if defined $opt{levels};
25 my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle');
26 my @ENDTYPE = ('none', 'message', 'small message');
30 my @objtypes = map { $_->{type} } @$objects;
32 $count{$_}++ for @objtypes;
33 return (@objtypes > 1 && keys %count == 1 && 'all ') . join(', ',
34 map { $OBJTYPE[$_] ? $OBJTYPE[$_] . ($count{$_} > 1 && 's') : $_ }
39 # read and parse all input data
42 my $rawdata = readline;
43 if (substr($rawdata, 0, 11) eq "**TI86**\032\012\000") {
44 # compiled calculator file
45 $data = Games::Wormy::TICalcLevels->read($rawdata, $opt{version});
47 elsif (substr($rawdata, 0, 8) eq 'WormEdit') {
48 # original wormedit source
49 $data = Games::Wormy::WormEdit->read($rawdata, $opt{version});
52 die "Unrecognised file type\n";
56 # derive format from file extension
57 if ($opt{output} =~ /\.(yaml|json|txt)$/) {
61 # images are written directly to file
65 # redirect standard output to given file
66 open my $output, '>', $opt{output}
67 or die "Cannot output to '$opt{output}': $!";
71 # output with user-preferred formatting
72 given ($opt{format}) {
75 say JSON->new->encode($data);
78 # full data in yaml (human-readable) formatting
80 local $YAML::CompressSeries;
81 $YAML::CompressSeries = 0;
82 my $yml = "# Wormy levelset\n" . YAML::Dump($data);
84 # inline format of short hashes
86 ^(\ *) - \n # array indicator
87 ((?:\1\ \ [a-z0-9]{1,5}:\ *\d+\n)+) # simple hash declaration
88 (?!\1\ ) # no further children
90 my ($indent, $value) = ($1, $2);
94 "$indent- {$value}\n";
101 print " ($data->{description})" if defined $data->{description};
103 printf "File version: %s\n", "$data->{format} v$data->{version}";
104 printf "Defaults: %s\n", join('; ',
105 $data->{sprite} ? 'sprite ' . scalar @{ $data->{sprite} } : (),
106 defined $data->{hiname} ? 'hiscore by ' . $data->{hiname} : (),
110 for my $variant (qw/single peaworm multi race ctf/) {
111 my $count = $data->{levelcount}->{$variant};
112 defined $count or next;
114 printf '%s (%s)', ucfirst $variant, $count;
117 for (0 .. $count - 1) {
118 my $level = $data->{levels}->[$_ + $startnr];
119 printf("\n- %-22s%4s:%3s+%2s%3s %3sx%-3s%s",
120 $level->{id} || $level->{name} || '#'.($_+1),
121 @$level{qw/size bsize growth/},
122 $variant eq 'single' && "x$level->{peas}",
123 @$level{qw/width height/},
124 join(';', map {" $_"} grep {$_}
125 @{$level->{objects}} && sprintf('%2d object%s (%s)',
126 scalar @{$level->{objects}}, @{$level->{objects}} != 1 && 's',
127 objsummary($level->{objects}),
129 $level->{sprite} && @{$level->{sprite}} && sprintf('sprite %d',
130 scalar @{$level->{sprite}},
132 $level->{balls} && @{$level->{balls}} && sprintf('%d bounc%s',
133 scalar @{$level->{balls}}, @{$level->{balls}} == 1 ? 'y' : 'ies',
141 printf("-- %-21s%4s: %s (%s)\n",
143 defined $data->{finish}->{code}
144 ? length $data->{finish}->{code} : '?',
145 defined $data->{finish}->{type}
146 ? $ENDTYPE[$data->{finish}->{type}] || 'unknown' : 'code',
147 $data->{finish}->{message} // '?',
148 ) if $variant eq 'single';
153 require Games::Wormy::Render;
157 # find all numeric values in argument
158 @request = $opt{levels} =~ /(\d+)/g;
161 # default to all singleplayer levels
162 @request = 0 .. $data->{levelcount}->{single} - 1;
164 @request or die "no levels found or specified\n";
166 my $img = Games::Wormy::Render->composite(
167 map { $data->{levels}->[$_] } @request
168 ) or die "empty result for levels\n";
169 if ($opt{format} ~~ 'pbm') {
170 $img = $img->to_paletted({make_colors => 'mono'});
171 $opt{format} = 'pnm';
174 $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT),
175 type => $opt{format} // 'pnm',
176 ) or die $img->errstr;
184 parse-wormedit - Wormy level data parser
188 parse-wormedit [--format=<type>] [--levels=<number>] [--output=<file.ext>] <input.lvl>
192 Reads Wormy levels (either original WormEdit source or compiled TI-86 string)
193 from STDIN or given file, and prints parsed data to STDOUT.
195 If an I<output> file name is given, its extension determines the format,
196 otherwise explicitly given by the I<format> option:
202 Plain text summary of levelpack contents.
206 All parsed data in YAML syntax.
210 Parsed data in JSON syntax.
212 =item pnm, png, bmp, ...
214 Image drawing of rendered levels.
215 Unrecognised values are interpreted as file type and converted by Imager.
221 Mischa POSLAWSKY <wormy@shiar.org>