7 use Getopt::Long 2.33 qw(HelpMessage :config bundling);
12 'raw|r', # full output
13 'version=i', # force version
14 ) or HelpMessage(-exitval => 2);
17 package Shiar_Parse::WormEdit;
23 "WormEdit053\000LVL" => 53,
24 "WormEdit\34195\000LVL" => 95,
25 "WormEdit\34194\000LVL" => 94,
26 "WormEdit\34193\000LVL" => 93,
33 description => 'Ca64x256',
42 map { (start => $_, end => $_) } [1,
63 levels => ['*', # levelcount->total actually
101 my ($self, $input) = @_;
102 my ($id, $version) = (substr($input, 0, 15), ord substr($input, 15, 1));
103 my $fileversion = $MAGICID{$id}
104 or die "File does not match any known WormEdit level header\n";
107 warn "Override version $version to $opt{version}\n";
108 $version = $opt{version};
110 elsif ($version != $fileversion) {
111 warn "Unexpected version $version (expecting $fileversion)\n";
113 elsif ($version == 95) {
114 # auto-detect exact variant
115 if (ord substr($input, 70, 1) ~~ [1 .. 8]) {
116 # valid sprite length instead of description byte
117 # (which is usually a letter or nul)
120 elsif (ord substr($input, 147, 1) == 0) {
121 # nul of finish type is 2 bytes later (unlike first char of message)
124 warn "Ambiguous file version 95; guessing subversion $version\n";
127 $fileversion += 100 if $fileversion < 90; # 93..95 came before 50..53
128 given ($fileversion) {
129 when (153) { } # current @FORMAT
130 $FORMAT[7] = 'Ca64'; # no reserved space after description
131 $FORMAT[15]->[-1] = 'Ca255'; # enddata
132 $FORMAT[-1]->[-1]->[0] = '32C'; # less objects
133 when ($version == 96) {
134 ref $_ and $_->[-1] = 'C' for @{ $FORMAT[11] }; # 9 moderefs
136 ref $_ and pop @$_ for @{ $FORMAT[11] }; # only 8 moderefs
137 splice @FORMAT, 6, 2 if $version <= 94; # earlier version without description
139 splice @{ $FORMAT[7] }, 4, 2; # no race
140 splice @{ $FORMAT[13] }, 4, 2; # no enddata
141 splice @{ $FORMAT[-1] }, 1, 2; # no name
143 splice @FORMAT, 14, 2; # no hiname
144 $FORMAT[-1]->[0] = 64; # constant amount of levels
147 die "Cannot parse data for Wormedit $fileversion/$version\n";
151 # convert to an easily accessible hash
152 my @values = unpack Shiar_Parse::Nested->template(\@FORMAT).'a*', $input;
153 my $data = Shiar_Parse::Nested->convert(\@FORMAT, \@values);
154 warn "Trailing data left unparsed\n" if grep {length} @values;
155 $data->{format} = 'WormEdit';
160 package Shiar_Parse::WormyLevel;
165 use List::Util qw(sum min max);
168 my ($self, $input) = @_;
169 my ($psize, $ptype, $size, $type, $vsize, $dsize, $id, $subid) = unpack q{
170 x11 x42 # file signature and comment
171 S a2 S a2 # file size, type; data size, type
173 S S # var size; content size
177 or die "Not a calculator string, thus cannot be a Wormy level file\n";
179 or warn "File size ($size) does not correspond with data size ($psize)\n";
181 or die "Not a calculator string, thus cannot be a Wormy level file\n";
182 $size == $vsize and $vsize == $dsize+2
183 or warn "Mismatch in string data size declarations\n";
184 # substr($input, -2) eq $CHECKSUM
186 $input = substr $input, 73, -2;
188 or die "Wormy level identifier not found\n";
199 offset => [1, map {$_ => 'S'} @$_], # byte location of start
200 end => [1, map {$_ => 'C'} @$_],
202 [qw/single peaworm tron deathmatch foodmatch multifood timematch race ctf/]
204 theanswer => 'C', # 42
252 ref $_ and splice @$_, -2 for @{ $FORMAT[11] }; # only 8 moderefs
253 splice @FORMAT, 12, 2; # no reserved byte
256 die "Unsupported level version $subid\n";
260 my $data = Shiar_Parse::Nested->unpack(\@FORMAT, $input);
262 my $offsetbase = 0xF080 + @{ $data->{sprite} } + 1;
263 $data->{moderef}->{offset}->{single} == $offsetbase
264 or warn "First singleplayer level is not in front\n";
267 $data->{moderef}->{end}->{single} > 0, # singleplayer slot if any levels
268 $data->{moderef}->{end}->{peaworm}, # one for each peaworm arena
269 $data->{moderef}->{end}->{tron}, # idem for tron
271 $data->{hinames} = [ unpack '(x2a3)*', substr($data->{leveldata}, -5 * $slots) ];
272 $data->{format} = '86s';
276 [qw'multi peaworm tron deathmatch foodmatch multifood timematch'],
281 $data->{levels} = [];
282 for my $modes (@VARMODES) {
283 my $variant = shift @$modes;
284 $offset = min(grep {$_} map { $data->{moderef}->{offset}->{$_} } @$modes)
286 $offset -= $offsetbase;
287 my $amount = $variant eq 'single' ? 100 : max(map { $data->{moderef}->{end}->{$_} } @$modes);
289 my @varform = @LEVELFORM;
290 $varform[13]->[0] = $variant eq 'single' ? 1 : 4;
291 unshift @varform, name => 'Z*' unless $variant eq 'single';
292 $varform[-1]->[0] = 1 if $variant eq 'race';
293 $varform[-1]->[0] = 2 if $variant eq 'ctf';
295 while ($offset < length $data->{leveldata}) {
296 last if substr($data->{leveldata}, $offset, 1) eq chr(255);
298 # find references to this level offset, and set start number to matching modes
299 while (my ($mode, $location) = each %{ $data->{moderef}->{offset} }) {
300 $location == $offset + $offsetbase or next;
301 $data->{moderef}->{start}->{$mode} = 1 + scalar @{ $data->{levels} };
304 my $level = Shiar_Parse::Nested->unpack(
305 [@varform], substr $data->{leveldata}, $offset
307 my $size = 8 # unpack length (ugh, ugly recalculation)
308 + (defined $level->{name} ? 1 + length $level->{name} : 0)
309 + 3 * (ref $level->{worms} eq 'ARRAY' ? scalar @{$level->{worms}} : 1)
310 + 2 * ($level->{flags} ? ref $level->{flags} eq 'ARRAY' ? scalar @{$level->{flags}} : 1 : 0)
311 + ($level->{sprite} ? scalar @{$level->{sprite}} : 0)
312 + ($level->{balls} ? 3 * scalar @{$level->{balls}} : 0);
313 $level->{size} = $size;
314 $level->{offset} = $offset + $offsetbase;
316 # add objects until terminator
317 $level->{objects} = [];
318 while (my $object = ord substr($data->{leveldata}, $offset+$size, 1)) {
319 push @{ $level->{objects} }, Shiar_Parse::Nested->unpack(
320 [@OBJECTFORM], substr($data->{leveldata}, $offset+$size, 5)
325 # add parsed level and advance
326 push @{ $data->{levels} }, $level;
328 last if ++$data->{levelcount}->{$variant} >= $amount;
331 if ($variant eq 'single') {
333 $data->{finish}->{code} =
334 my $code = substr delete($data->{leveldata}), $offset, -5*$slots;
339 chr 0x21, # ld hl, MESSAGE
340 pack('v', $offsetbase + $offset + 9),
342 0xCD, 0x37, 0x4A, # call _puts
343 0xC3, 0xAA, 0x55, # jp _getkey
348 0x21, 0, 0x1C, # ld hl, $POS
349 0x22, 0x7C, 0xC3, # ld (_penCol), hl
350 0x21, # ld hl, MESSAGE
352 pack('v', $offsetbase + $offset + 15),
354 0xCD, 0xA5, 0x4A, # call _vputs
355 0xC3, 0xAA, 0x55, # jp _getkey
359 while (my ($finish, $match) = each %FINISHCODE) {
360 $match eq substr $code, 0, length $match or next;
361 $data->{finish}->{type} = $finish or last;
362 $data->{finish}->{message} = unpack 'Z*', substr($code, length $match);
372 package Shiar_Parse::Nested;
375 my ($self, $format) = @_;
376 # total (flattened) unpack template from nested format definitions
377 return join '', map {
378 my $value = $format->[-($_ << 1) - 1];
379 if (ref $value eq 'ARRAY') {
380 my $count = $value->[0];
381 $value = $self->template($value);
382 $value = $count =~ s/^([*\d]+)// ? "$count($value)$1"
383 : $count."X[$count]$count/($value)";
386 $value =~ s/^C(a)(\d+)/$1 . ($2 + 1)/e; # length prefix
389 } reverse 0 .. ($#$format - 1) >> 1;
393 my ($self, $format, $data) = @_;
394 # map flat results into a named and nested hash
396 while (my ($field, $template) = splice @$format, 0, 2) {
397 if (ref $template eq 'ARRAY') {
398 my ($count, @subformat) = @$template;
399 my $max = $count =~ s/^(\d+)// ? $1 : 0;
400 $count = !$count ? $max
401 : $count eq '*' ? $res{levelcount}->{total} : shift @$data;
402 $res{$field}->[$_] = $self->convert([@subformat], $data) for 0 .. ($max || $count)-1;
403 splice @{ $res{$field} }, $count if $max > $count;
404 $res{$field} = $res{$field}->[0] if $max == 1;
407 elsif ($template =~ /^Ca/) {
408 $data->[0] = CORE::unpack 'C/a', $data->[0];
410 $res{$field} = shift @$data;
416 my ($self, $format, $input) = @_;
417 my @data = CORE::unpack $self->template($format), $input;
418 return $self->convert($format, \@data);
424 my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle');
425 my @ENDTYPE = ('none', 'message', 'small message');
429 my @objtypes = map { $_->{type} } @$objects;
431 $count{$_}++ for @objtypes;
432 return (@objtypes > 1 && keys %count == 1 && 'all ') . join(', ',
433 map { $OBJTYPE[$_] ? $OBJTYPE[$_] . ($count{$_} > 1 && 's') : $_ }
438 # read and parse all input data
441 my $rawdata = readline;
442 if (substr($rawdata, 0, 11) eq "**TI86**\032\012\000") {
443 # compiled calculator file
444 $data = Shiar_Parse::WormyLevel->read($rawdata);
446 elsif (substr($rawdata, 0, 8) eq 'WormEdit') {
447 # original wormedit source
448 $data = Shiar_Parse::WormEdit->read($rawdata);
451 die "Unrecognised file type\n";
454 # output with user-preferred formatting
456 # full data in yaml (human-readable) formatting
458 local $YAML::CompressSeries;
459 $YAML::CompressSeries = 0;
460 my $yml = "# Wormy levelset\n" . YAML::Dump($data);
462 # inline format of short hashes
464 ^(\ *) - \n # array indicator
465 ((?:\1\ \ [a-z0-9]{1,5}:\ *\d+\n)+) # simple hash declaration
466 (?!\1\ ) # no further children
468 my ($indent, $value) = ($1, $2);
472 "$indent- {$value}\n";
479 print " ($data->{description})" if defined $data->{description};
481 printf "File version: %s\n", "$data->{format} v$data->{version}";
482 printf "Defaults: %s\n", join('; ',
483 'sprite ' . scalar @{ $data->{sprite} },
484 defined $data->{hiname} ? 'hiscore by ' . $data->{hiname} : (),
488 for my $variant (qw/single multi race ctf/) {
489 my $count = $data->{levelcount}->{$variant};
491 printf '%s (%s)', ucfirst $variant, $count // 'invalid';
494 for (0 .. $count - 1) {
495 my $level = $data->{levels}->[$_ + $startnr];
496 printf("\n- %-22s%4s:%3s+%2s%3s %3sx%-3s%s",
497 $level->{id} || $level->{name} || '#'.($_+1),
498 @$level{qw/size bsize growth/},
499 $variant eq 'single' && "x$level->{peas}",
500 @$level{qw/width height/},
501 join(';', map {" $_"} grep {$_}
502 @{$level->{objects}} && sprintf('%2d object%s (%s)',
503 scalar @{$level->{objects}}, @{$level->{objects}} != 1 && 's',
504 objsummary($level->{objects}),
506 $level->{sprite} && @{$level->{sprite}} && sprintf('sprite %d',
507 scalar @{$level->{sprite}},
509 $level->{balls} && @{$level->{balls}} && sprintf('%d bounc%s',
510 scalar @{$level->{balls}}, @{$level->{balls}} == 1 ? 'y' : 'ies',
519 printf("-- %-21s%4s: %s (%s)\n",
521 defined $data->{finish}->{code}
522 ? length $data->{finish}->{code} : '?',
523 defined $data->{finish}->{type}
524 ? $ENDTYPE[$data->{finish}->{type}] || 'unknown' : 'code',
525 $data->{finish}->{message} // '?',
526 ) if $variant eq 'single';
534 parse-wormedit - Wormy level data parser
538 parse-wormedit [--raw] <input.lvl>
542 Reads Wormy levels (either original WormEdit source or compiled TI-86 string)
543 from STDIN or given file, and outputs contents, summarised or in full.
547 Mischa POSLAWSKY <wormy@shiar.org>