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;
22 use Parse::Binary::Nested;
25 "WormEdit053\000LVL" => 53,
26 "WormEdit\34195\000LVL" => 95,
27 "WormEdit\34194\000LVL" => 94,
28 "WormEdit\34193\000LVL" => 93,
35 description => 'Ca64x256',
44 map { (start => $_, end => $_) } [1,
67 levels => ['*', # levelcount->total actually
105 my ($self, $input) = @_;
106 my ($id, $version) = (substr($input, 0, 15), ord substr($input, 15, 1));
107 my $fileversion = $MAGICID{$id}
108 or die "File does not match any known WormEdit level header\n";
111 warn "Override version $version to $opt{version}\n";
112 $version = $opt{version};
114 elsif ($version != $fileversion) {
115 warn "Unexpected version $version (expecting $fileversion)\n";
117 elsif ($version == 95) {
118 # auto-detect exact variant
119 if (ord substr($input, 70, 1) ~~ [1 .. 8]) {
120 # valid sprite length instead of description byte
121 # (which is usually a letter or nul)
124 elsif (ord substr($input, 147, 1) == 0) {
125 # nul of finish type is 2 bytes later (unlike first char of message)
128 warn "Ambiguous file version 95; guessing subversion $version\n";
131 $fileversion += 100 if $fileversion < 90; # 93..95 came before 50..53
132 given ($fileversion) {
133 when (153) { } # current @FORMAT
134 $FORMAT[7] = 'Ca64'; # no reserved space after description
135 splice @{ $FORMAT[15] }, -2; # finish reserve
136 $FORMAT[-1]->[-1]->[0] = '32C'; # less objects
137 ref $_ and pop @$_ for @{ $FORMAT[11] }; # 9 moderefs
138 when ($version == 96) { }
139 ref $_ and pop @$_ for @{ $FORMAT[11] }; # only 8 moderefs (no ctf)
140 splice @FORMAT, 6, 2 if $version <= 94; # earlier version without description
142 splice @{ $FORMAT[7] }, 4, 2; # no race
143 splice @{ $FORMAT[13] }, 4, 2; # no enddata
144 splice @{ $FORMAT[-1] }, 1, 2; # no name
146 splice @FORMAT, 14, 2; # no hiname
147 $FORMAT[-1]->[0] = 64; # constant amount of levels
150 die "Cannot parse data for Wormedit $fileversion/$version\n";
154 # convert to an easily accessible hash
155 push @FORMAT, -trail => 'a*';
156 my $data = Parse::Binary::Nested->new(\@FORMAT)->unpackf($input);
157 warn "Trailing data left unparsed\n" if length delete $data->{-trail};
158 $data->{format} = 'WormEdit';
163 package Shiar_Parse::WormyLevel;
168 use List::Util qw(sum min max);
170 use Parse::Binary::Nested;
173 my ($self, $input) = @_;
174 my ($psize, $ptype, $size, $type, $vsize, $dsize, $id, $version) = unpack q{
175 x11 x42 # file signature and comment
176 S a2 S a2 # file size, type; data size, type
178 S S # var size; content size
182 or die "Not a calculator string, thus cannot be a Wormy level file\n";
184 or warn "File size ($size) does not correspond with data size ($psize)\n";
186 or die "Not a calculator string, thus cannot be a Wormy level file\n";
187 $size == $vsize and $vsize == $dsize+2
188 or warn "Mismatch in string data size declarations\n";
189 # substr($input, -2) eq $CHECKSUM
191 $input = substr $input, 73, -2;
193 or die "Wormy level identifier not found\n";
196 warn "Override version $version to $opt{version}\n";
197 $version = $opt{version};
199 elsif ($version == 95) {
200 # level offset instead of description byte
201 $version-- if (unpack('x2Z*x2xC', $input))[1] == 0xF4;
202 warn "Ambiguous file version 95; guessing subversion $version\n";
215 offset => [1, map {$_ => 'S'} @$_], # byte location of start
216 end => [1, map {$_ => 'C'} @$_],
218 [qw/single peaworm tron deathmatch foodmatch multifood timematch race ctf/]
220 theanswer => 'x', # 42
262 my $offsetbase = 0xF080;
268 $offsetbase = 0xF400;
270 ref $_ and splice(@$_, -8, 2) for @{ $FORMAT[11] }; # no multifood
271 splice @FORMAT, 12, 2; # no reserved byte
273 splice @FORMAT, 6, 2; # no description
276 $FORMAT[5] = 'C/a'; # length-preceding name
277 splice @FORMAT, 10, 2; # no default sprite
279 $_->[5] = $_->[7]; # no tron; deathmatch instead
280 $_->[7] = $_->[9]; # foodmatch instead
281 $_->[9] = 'linkmatch'; # replaces timematch
282 $_->[11] = $_->[13]; # race
283 $_->[13] = $_->[15]; # ctf
284 $_->[15] = 'domination';
285 } for @{ $FORMAT[9] }; # no multifood
286 push @LEVELFORM, "object$_" => ['C',
287 map {$_ => 'C'} qw(x1 y1 x2 y2)
288 ] for qw(lines boxes);
291 die "Unsupported level version $version\n";
295 my $data = Parse::Binary::Nested->new(\@FORMAT)->unpackf($input);
297 $offsetbase += 1 + @{ $data->{sprite} } if $data->{sprite};
298 $data->{moderef}->{offset}->{single} == $offsetbase
299 or warn "First singleplayer level is not in front\n";
302 $data->{moderef}->{end}->{single} > 0, # singleplayer slot if any levels
303 $data->{moderef}->{end}->{peaworm}, # one for each peaworm arena
304 $data->{moderef}->{end}->{tron}, # idem for tron
306 $data->{hinames} = [ unpack '(x2a3)*', substr($data->{leveldata}, -5 * $slots) ];
307 $data->{format} = '86s';
311 [qw'multi peaworm tron deathmatch foodmatch multifood timematch'],
316 $data->{levels} = [];
317 for my $modes (@VARMODES) {
318 my $variant = shift @$modes;
319 my @modeoffsets = grep {defined} #TODO: comment
320 map { $data->{moderef}->{offset}->{$_} } @$modes;
321 @modeoffsets or next;
322 $data->{levelcount}->{$variant} = 0;
323 $offset = min(grep {$_} @modeoffsets) or next;
324 $offset -= $offsetbase;
325 my $amount = $variant eq 'single' ? 100
326 : max(grep {defined} map { $data->{moderef}->{end}->{$_} } @$modes);
328 my @varform = @LEVELFORM;
329 $varform[13]->[0] = $variant eq 'single' ? 1 : 4;
330 unshift @varform, name => 'Z*' unless $variant eq 'single' or $version <= 91;
331 $varform[-1]->[0] = 1 if $variant eq 'race' and $version > 91;
332 $varform[-1]->[0] = 2 if $variant eq 'ctf';
333 my $parselevel = Parse::Binary::Nested->new(\@varform);
335 while ($offset < length $data->{leveldata}) {
336 last if substr($data->{leveldata}, $offset, 1) eq chr(255);
338 # find references to this level offset, and set start number to matching modes
339 while (my ($mode, $location) = each %{ $data->{moderef}->{offset} }) {
340 $location == $offset + $offsetbase or next;
341 $data->{moderef}->{start}->{$mode} = 1 + scalar @{ $data->{levels} };
344 my $level = $parselevel->unpackf(substr $data->{leveldata}, $offset);
345 my $size = 8 # unpack length (ugh, ugly recalculation)
346 + (defined $level->{name} ? 1 + length $level->{name} : 0)
347 + 3 * (ref $level->{worms} eq 'ARRAY' ? scalar @{$level->{worms}} : 1)
348 + 2 * ($level->{flags} ? ref $level->{flags} eq 'ARRAY' ? scalar @{$level->{flags}} : 1 : 0)
349 + ($level->{sprite} ? scalar @{$level->{sprite}} : 0)
350 + ($level->{balls} ? 3 * scalar @{$level->{balls}} : 0);
351 $level->{size} = $size;
352 $level->{offset} = $offset + $offsetbase;
354 # add objects until terminator
355 $level->{objects} = [];
356 if ($version <= 91) {
357 push @{ $level->{objects} }, { %$_, type => 2 } for map { $level->{$_} ? @{ $level->{$_} } : () } qw(objectlines);
358 push @{ $level->{objects} }, { %$_, type => 3 } for map { $level->{$_} ? @{ $level->{$_} } : () } qw(objectboxes);
359 $size += 1 + 4 * scalar @{ $level->{objects} };
362 while (my $object = ord substr($data->{leveldata}, $offset+$size, 1)) {
363 push @{ $level->{objects} }, Parse::Binary::Nested->new([@OBJECTFORM])->unpackf(
364 substr $data->{leveldata}, $offset+$size, 5
370 # add parsed level and advance
371 push @{ $data->{levels} }, $level;
373 last if ++$data->{levelcount}->{$variant} >= $amount;
376 if ($variant eq 'single') {
378 $data->{finish}->{code} =
379 my $code = substr $data->{leveldata}, $offset, -5*$slots;
384 chr 0x21, # ld hl, MESSAGE
385 pack('v', $offsetbase + $offset + 9),
387 0xCD, 0x37, 0x4A, # call _puts
388 0xC3, 0xAA, 0x55, # jp _getkey
393 0x21, 0, 0x1C, # ld hl, $POS
394 0x22, 0x7C, 0xC3, # ld (_penCol), hl
395 0x21, # ld hl, MESSAGE
397 pack('v', $offsetbase + $offset + 15),
399 0xCD, 0xA5, 0x4A, # call _vputs
400 0xC3, 0xAA, 0x55, # jp _getkey
404 while (my ($finish, $match) = each %FINISHCODE) {
405 $match eq substr $code, 0, length $match or next;
406 $data->{finish}->{type} = $finish and
407 $data->{finish}->{message} = unpack 'Z*', substr($code, length $match);
419 my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle');
420 my @ENDTYPE = ('none', 'message', 'small message');
424 my @objtypes = map { $_->{type} } @$objects;
426 $count{$_}++ for @objtypes;
427 return (@objtypes > 1 && keys %count == 1 && 'all ') . join(', ',
428 map { $OBJTYPE[$_] ? $OBJTYPE[$_] . ($count{$_} > 1 && 's') : $_ }
433 # read and parse all input data
436 my $rawdata = readline;
437 if (substr($rawdata, 0, 11) eq "**TI86**\032\012\000") {
438 # compiled calculator file
439 $data = Shiar_Parse::WormyLevel->read($rawdata);
441 elsif (substr($rawdata, 0, 8) eq 'WormEdit') {
442 # original wormedit source
443 $data = Shiar_Parse::WormEdit->read($rawdata);
446 die "Unrecognised file type\n";
449 # output with user-preferred formatting
451 # full data in yaml (human-readable) formatting
453 local $YAML::CompressSeries;
454 $YAML::CompressSeries = 0;
455 my $yml = "# Wormy levelset\n" . YAML::Dump($data);
457 # inline format of short hashes
459 ^(\ *) - \n # array indicator
460 ((?:\1\ \ [a-z0-9]{1,5}:\ *\d+\n)+) # simple hash declaration
461 (?!\1\ ) # no further children
463 my ($indent, $value) = ($1, $2);
467 "$indent- {$value}\n";
474 print " ($data->{description})" if defined $data->{description};
476 printf "File version: %s\n", "$data->{format} v$data->{version}";
477 printf "Defaults: %s\n", join('; ',
478 $data->{sprite} ? 'sprite ' . scalar @{ $data->{sprite} } : (),
479 defined $data->{hiname} ? 'hiscore by ' . $data->{hiname} : (),
483 for my $variant (qw/single multi race ctf/) {
484 my $count = $data->{levelcount}->{$variant};
486 printf '%s (%s)', ucfirst $variant, $count // 'invalid';
489 for (0 .. $count - 1) {
490 my $level = $data->{levels}->[$_ + $startnr];
491 printf("\n- %-22s%4s:%3s+%2s%3s %3sx%-3s%s",
492 $level->{id} || $level->{name} || '#'.($_+1),
493 @$level{qw/size bsize growth/},
494 $variant eq 'single' && "x$level->{peas}",
495 @$level{qw/width height/},
496 join(';', map {" $_"} grep {$_}
497 @{$level->{objects}} && sprintf('%2d object%s (%s)',
498 scalar @{$level->{objects}}, @{$level->{objects}} != 1 && 's',
499 objsummary($level->{objects}),
501 $level->{sprite} && @{$level->{sprite}} && sprintf('sprite %d',
502 scalar @{$level->{sprite}},
504 $level->{balls} && @{$level->{balls}} && sprintf('%d bounc%s',
505 scalar @{$level->{balls}}, @{$level->{balls}} == 1 ? 'y' : 'ies',
514 printf("-- %-21s%4s: %s (%s)\n",
516 defined $data->{finish}->{code}
517 ? length $data->{finish}->{code} : '?',
518 defined $data->{finish}->{type}
519 ? $ENDTYPE[$data->{finish}->{type}] || 'unknown' : 'code',
520 $data->{finish}->{message} // '?',
521 ) if $variant eq 'single';
529 parse-wormedit - Wormy level data parser
533 parse-wormedit [--raw] <input.lvl>
537 Reads Wormy levels (either original WormEdit source or compiled TI-86 string)
538 from STDIN or given file, and outputs contents, summarised or in full.
542 Mischa POSLAWSKY <wormy@shiar.org>