99daaa15e8579dfa8d8a889a593ba19294122c2d
[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$/) {
58                 $opt{format} //= 'yaml';
59         }
60         elsif ($opt{output} !~ /\.txt$/) {
61                 $opt{format} //= 'pnm';
62                 last;  # images are written directly to file
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 ('pnm') {
74         require Games::Wormy::Render;
75
76         my @request;
77         if ($opt{levels}) {
78                 # find all numeric values in argument
79                 @request = $opt{levels} =~ /(\d+)/g;
80         }
81         else {
82                 # default to all singleplayer levels
83                 @request = 0 .. $data->{levelcount}->{single} - 1;
84         }
85         @request or die "no levels found or specified\n";
86
87         my $img = Games::Wormy::Render->composite(
88                 map { $data->{levels}->[$_] } @request
89         ) or die "empty result for levels\n";
90         $img->write(
91                 $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT, type => 'pnm')
92         ) or die $img->errstr;
93 }
94 when ('yaml') {
95         # full data in yaml (human-readable) formatting
96         require YAML;
97         local $YAML::CompressSeries;
98               $YAML::CompressSeries = 0;
99         my $yml = "# Wormy levelset\n" . YAML::Dump($data);
100
101         # inline format of short hashes
102         $yml =~ s{
103                 ^(\ *) - \n                          # array indicator
104                 ((?:\1\ \ [a-z0-9]{1,5}:\ *\d+\n)+)  # simple hash declaration
105                 (?!\1\ )                             # no further children
106         }[
107                 my ($indent, $value) = ($1, $2);
108                 chop $value;
109                 $value =~ s/^ +//gm;
110                 $value =~ s/\n/, /g;
111                 "$indent- {$value}\n";
112         ]egmx;
113
114         print $yml;
115 }
116 default {
117         print $data->{name};
118         print " ($data->{description})" if defined $data->{description};
119         print "\n";
120         printf "File version: %s\n", "$data->{format} v$data->{version}";
121         printf "Defaults: %s\n", join('; ',
122                 $data->{sprite} ? 'sprite ' . scalar @{ $data->{sprite} } : (),
123                 defined $data->{hiname} ? 'hiscore by ' . $data->{hiname} : (),
124         );
125
126         my $startnr = 0;
127         for my $variant (qw/single peaworm multi race ctf/) {
128                 my $count = $data->{levelcount}->{$variant};
129                 defined $count or next;
130                 print "\n";
131                 printf '%s (%s)', ucfirst $variant, $count;
132                 $count or next;
133                 print ":";
134                 for (0 .. $count - 1) {
135                         my $level = $data->{levels}->[$_ + $startnr];
136                         printf("\n- %-22s%4s:%3s+%2s%3s %3sx%-3s%s",
137                                 $level->{id} || $level->{name} || '#'.($_+1),
138                                 @$level{qw/size bsize growth/},
139                                 $variant eq 'single' && "x$level->{peas}",
140                                 @$level{qw/width height/},
141                                 join(';', map {" $_"} grep {$_}
142                                         @{$level->{objects}} && sprintf('%2d object%s (%s)',
143                                                 scalar @{$level->{objects}}, @{$level->{objects}} != 1 && 's',
144                                                 objsummary($level->{objects}),
145                                         ),
146                                         $level->{sprite} && @{$level->{sprite}} && sprintf('sprite %d',
147                                                 scalar @{$level->{sprite}},
148                                         ),
149                                         $level->{balls} && @{$level->{balls}} && sprintf('%d bounc%s',
150                                                 scalar @{$level->{balls}}, @{$level->{balls}} == 1 ? 'y' : 'ies',
151                                         ),
152                                 ),
153                         );
154                 }
155                 $startnr += $count;
156
157                 print "\n";
158                 printf("-- %-21s%4s: %s (%s)\n",
159                         '(ending)',
160                         defined $data->{finish}->{code}
161                                 ? length $data->{finish}->{code} : '?',
162                         defined $data->{finish}->{type}
163                                 ? $ENDTYPE[$data->{finish}->{type}] || 'unknown' : 'code',
164                         $data->{finish}->{message} // '?',
165                 ) if $variant eq 'single';
166         }
167         print "\n";
168 }
169 }
170
171 __END__
172
173 =head1 NAME
174
175 parse-wormedit - Wormy level data parser
176
177 =head1 SYNOPSIS
178
179  parse-wormedit [--format=<type>] [--levels=<number>] [--output=<file.ext>] <input.lvl>
180
181 =head1 DESCRIPTION
182
183 Reads Wormy levels (either original WormEdit source or compiled TI-86 string)
184 from STDIN or given file, and prints parsed data to STDOUT.
185
186 If an I<output> file name is given, its extension determines the format,
187 otherwise explicitly given by the I<format> option:
188
189 =over 6
190
191 =item txt
192
193 Plain text summary of levelpack contents.
194
195 =item yaml
196
197 All parsed data in YAML syntax.
198
199 =item pnm, png, bmp, ...
200
201 Image drawing of rendered levels.
202 Unrecognised values are interpreted as file type and converted by Imager.
203
204 =back
205
206 =head1 AUTHOR
207
208 Mischa POSLAWSKY <wormy@shiar.org>
209
210 =head1 LICENSE
211
212 GPL version 3.
213