cf1798d6a25e6cb787b9836577add8782ec93aed
[wormy.git] / parse-wormedit
1 #!/usr/bin/env perl
2 use 5.010;
3 use strict;
4 use warnings;
5 use experimental 'switch';
6 use lib 'lib';  # make runnable for simple cases
7
8 use Data::Dumper;
9 use Getopt::Long 2.33 qw(HelpMessage :config bundling);
10 use Games::Wormy::TICalcLevels;
11 use Games::Wormy::WormEdit;
12
13 our $VERSION = '1.07';
14
15 GetOptions(\my %opt,
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};
24
25 my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle');
26 my @ENDTYPE = ('none', 'message', 'small message');
27
28 sub objsummary {
29         my ($objects) = @_;
30         my @objtypes = map { $_->{type} } @$objects;
31         my %count;
32         $count{$_}++ for @objtypes;
33         return (@objtypes > 1 && keys %count == 1 && 'all ') . join(', ',
34                 map { $OBJTYPE[$_] ? $OBJTYPE[$_] . ($count{$_} > 1 && 's') : $_ }
35                 sort keys %count
36         );
37 }
38
39 # read and parse all input data
40 my $data;
41 local $/;
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});
46 }
47 elsif (substr($rawdata, 0, 8) eq 'WormEdit') {
48         # original wormedit source
49         $data = Games::Wormy::WormEdit->read($rawdata, $opt{version});
50 }
51 else {
52         die "Unrecognised file type\n";
53 }
54
55 if ($opt{output}) {{
56         # derive format from file extension
57         if ($opt{output} =~ /\.(yaml|txt)$/) {
58                 $opt{format} //= $1
59         }
60         else {
61                 # images are written directly to file
62                 last;
63         }
64
65         # redirect standard output to given file
66         open my $output, '>', $opt{output}
67                 or die "Cannot output to '$opt{output}': $!";
68         select $output;
69 }}
70
71 # output with user-preferred formatting
72 given ($opt{format}) {
73 when ('yaml') {
74         # full data in yaml (human-readable) formatting
75         require YAML;
76         local $YAML::CompressSeries;
77               $YAML::CompressSeries = 0;
78         my $yml = "# Wormy levelset\n" . YAML::Dump($data);
79
80         # inline format of short hashes
81         $yml =~ s{
82                 ^(\ *) - \n                          # array indicator
83                 ((?:\1\ \ [a-z0-9]{1,5}:\ *\d+\n)+)  # simple hash declaration
84                 (?!\1\ )                             # no further children
85         }[
86                 my ($indent, $value) = ($1, $2);
87                 chop $value;
88                 $value =~ s/^ +//gm;
89                 $value =~ s/\n/, /g;
90                 "$indent- {$value}\n";
91         ]egmx;
92
93         print $yml;
94 }
95 when ('txt') {
96         print $data->{name};
97         print " ($data->{description})" if defined $data->{description};
98         print "\n";
99         printf "File version: %s\n", "$data->{format} v$data->{version}";
100         printf "Defaults: %s\n", join('; ',
101                 $data->{sprite} ? 'sprite ' . scalar @{ $data->{sprite} } : (),
102                 defined $data->{hiname} ? 'hiscore by ' . $data->{hiname} : (),
103         );
104
105         my $startnr = 0;
106         for my $variant (qw/single peaworm multi race ctf/) {
107                 my $count = $data->{levelcount}->{$variant};
108                 defined $count or next;
109                 print "\n";
110                 printf '%s (%s)', ucfirst $variant, $count;
111                 $count or next;
112                 print ":";
113                 for (0 .. $count - 1) {
114                         my $level = $data->{levels}->[$_ + $startnr];
115                         printf("\n- %-22s%4s:%3s+%2s%3s %3sx%-3s%s",
116                                 $level->{id} || $level->{name} || '#'.($_+1),
117                                 @$level{qw/size bsize growth/},
118                                 $variant eq 'single' && "x$level->{peas}",
119                                 @$level{qw/width height/},
120                                 join(';', map {" $_"} grep {$_}
121                                         @{$level->{objects}} && sprintf('%2d object%s (%s)',
122                                                 scalar @{$level->{objects}}, @{$level->{objects}} != 1 && 's',
123                                                 objsummary($level->{objects}),
124                                         ),
125                                         $level->{sprite} && @{$level->{sprite}} && sprintf('sprite %d',
126                                                 scalar @{$level->{sprite}},
127                                         ),
128                                         $level->{balls} && @{$level->{balls}} && sprintf('%d bounc%s',
129                                                 scalar @{$level->{balls}}, @{$level->{balls}} == 1 ? 'y' : 'ies',
130                                         ),
131                                 ),
132                         );
133                 }
134                 $startnr += $count;
135
136                 print "\n";
137                 printf("-- %-21s%4s: %s (%s)\n",
138                         '(ending)',
139                         defined $data->{finish}->{code}
140                                 ? length $data->{finish}->{code} : '?',
141                         defined $data->{finish}->{type}
142                                 ? $ENDTYPE[$data->{finish}->{type}] || 'unknown' : 'code',
143                         $data->{finish}->{message} // '?',
144                 ) if $variant eq 'single';
145         }
146         print "\n";
147 }
148 default {
149         require Games::Wormy::Render;
150
151         my @request;
152         if ($opt{levels}) {
153                 # find all numeric values in argument
154                 @request = $opt{levels} =~ /(\d+)/g;
155         }
156         else {
157                 # default to all singleplayer levels
158                 @request = 0 .. $data->{levelcount}->{single} - 1;
159         }
160         @request or die "no levels found or specified\n";
161
162         my $img = Games::Wormy::Render->composite(
163                 map { $data->{levels}->[$_] } @request
164         ) or die "empty result for levels\n";
165         if ($opt{format} ~~ 'pbm') {
166                 $img = $img->to_paletted({make_colors => 'mono'});
167                 $opt{format} = 'pnm';
168         }
169         $img->write(
170                 $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT),
171                 type => $opt{format} // 'pnm',
172         ) or die $img->errstr;
173 }
174 }
175
176 __END__
177
178 =head1 NAME
179
180 parse-wormedit - Wormy level data parser
181
182 =head1 SYNOPSIS
183
184  parse-wormedit [--format=<type>] [--levels=<number>] [--output=<file.ext>] <input.lvl>
185
186 =head1 DESCRIPTION
187
188 Reads Wormy levels (either original WormEdit source or compiled TI-86 string)
189 from STDIN or given file, and prints parsed data to STDOUT.
190
191 If an I<output> file name is given, its extension determines the format,
192 otherwise explicitly given by the I<format> option:
193
194 =over 6
195
196 =item txt
197
198 Plain text summary of levelpack contents.
199
200 =item yaml
201
202 All parsed data in YAML syntax.
203
204 =item pnm, png, bmp, ...
205
206 Image drawing of rendered levels.
207 Unrecognised values are interpreted as file type and converted by Imager.
208
209 =back
210
211 =head1 AUTHOR
212
213 Mischa POSLAWSKY <wormy@shiar.org>
214
215 =head1 LICENSE
216
217 GPL version 3.
218