1 package Games::Wormy::TICalcLevels;
6 use experimental 'switch';
8 use List::Util qw(sum min max);
10 use Parse::Binary::Nested qw(unpackf);
12 our $VERSION = '1.00';
15 my ($self, $input, $override) = @_;
16 my ($psize, $ptype, $size, $type, $vsize, $dsize, $id, $version) = unpack q{
17 x11 x42 # file signature and comment
18 S a2 S a2 # file size, type; data size, type
20 S S # var size; content size
24 or die "Not a calculator string, thus cannot be a Wormy level file\n";
26 or warn "File size ($size) does not correspond with data size ($psize)\n";
28 or die "Not a calculator string, thus cannot be a Wormy level file\n";
29 $size == $vsize and $vsize == $dsize+2
30 or warn "Mismatch in string data size declarations\n";
31 # substr($input, -2) eq $CHECKSUM
33 $input = substr $input, 73, -2;
35 or die "Wormy level identifier not found\n";
38 warn "Override version $version to $override\n";
41 elsif ($version == 95) {
42 # level offset instead of description byte
43 $version-- if (unpack('x2Z*x2xC', $input))[1] == 0xF4;
44 warn "Ambiguous file version 95; guessing subversion $version\n";
57 offset => [1, map {$_ => 'S'} @$_], # byte location of start
58 end => [1, map {$_ => 'C'} @$_],
60 [qw/single peaworm tron deathmatch foodmatch multifood timematch race ctf/]
62 theanswer => 'x', # 42
104 my $offsetbase = 0xF080;
108 [qw'multi peaworm tron deathmatch foodmatch multifood timematch'],
117 $offsetbase = 0xF400;
119 ref $_ and splice(@$_, -8, 2) for @{ $FORMAT[11] }; # no multifood
120 splice @FORMAT, 12, 2; # no reserved byte
122 splice @FORMAT, 6, 2; # no description
125 $FORMAT[5] = 'C/a'; # length-preceding name
126 splice @FORMAT, 10, 2; # no default sprite
128 $_->[5] = $_->[7]; # no tron; deathmatch instead
129 $_->[7] = $_->[9]; # foodmatch instead
130 $_->[9] = 'linkmatch'; # replaces timematch
131 $_->[11] = $_->[13]; # race
132 $_->[13] = $_->[15]; # ctf
133 $_->[15] = 'domination';
134 } for @{ $FORMAT[9] }; # no multifood
135 splice @LEVELFORM, -2;
136 push @LEVELFORM, "objects$_" => ['C',
138 map {$_ => 'C'} qw(x1 y1 x2 y2)
140 # peaworm/tron mode do not take multiplayer levels
141 splice @VARMODES, 1, 0, ['peaworm', splice @{ $VARMODES[1] }, 1, 2];
144 die "Unsupported level version $version\n";
148 my $data = unpackf(\@FORMAT, $input);
150 $offsetbase += 1 + @{ $data->{sprite} } if $data->{sprite};
151 $data->{moderef}->{offset}->{single} == $offsetbase
152 or warn "First singleplayer level is not in front\n";
154 my $slots = sum(grep {defined}
155 $data->{moderef}->{end}->{single} > 0, # singleplayer slot if any levels
156 $data->{moderef}->{end}->{peaworm}, # one for each peaworm arena
157 $data->{moderef}->{end}->{tron}, # idem for tron
159 $data->{hinames} = [ unpack '(x2a3)*', substr($data->{leveldata}, -5 * $slots) ];
160 $data->{format} = '86s';
162 $data->{levels} = [];
163 for my $modes (@VARMODES) {
164 my $variant = shift @$modes;
165 my @modeoffsets = grep {defined} #TODO: comment
166 map { $data->{moderef}->{offset}->{$_} } @$modes;
167 @modeoffsets or next;
168 $data->{levelcount}->{$variant} = 0;
169 $offset = min(grep {$_} @modeoffsets) or next;
170 $offset -= $offsetbase;
171 my $amount = $variant eq 'single' ? 100
172 : max(grep {defined} map { $data->{moderef}->{end}->{$_} } @$modes);
174 my @varform = @LEVELFORM;
175 $varform[13]->[0] = $variant ~~ ['single', 'peaworm'] ? 1 : 4; # worms
176 unshift @varform, name => 'Z*' unless $variant eq 'single' or $version <= 91;
177 $varform[-3]->[0] = 1 if $variant eq 'race' and $version > 91;
178 $varform[-3]->[0] = 2 if $variant eq 'ctf';
179 push @varform, size => '=.';
180 my $parselevel = Parse::Binary::Nested->new(\@varform);
182 while ($offset < length $data->{leveldata}) {
183 last if substr($data->{leveldata}, $offset, 1) eq chr(255);
185 # find references to this level offset, and set start number to matching modes
186 while (my ($mode, $location) = each %{ $data->{moderef}->{offset} }) {
187 $location == $offset + $offsetbase or next;
188 $data->{moderef}->{start}->{$mode} = 1 + scalar @{ $data->{levels} };
191 my $level = $parselevel->unpackf(substr $data->{leveldata}, $offset);
192 $level->{offset} = $offset + $offsetbase;
194 # add objects until terminator
195 $level->{objects} = [];
196 if ($version <= 91) {
197 ref $_ eq 'ARRAY' and push @{ $level->{objects} }, @$_
198 for map { delete $level->{"objects$_"} } 2, 3;
201 # add parsed level and advance
202 push @{ $data->{levels} }, $level;
203 $offset += $level->{size};
204 last if ++$data->{levelcount}->{$variant} >= $amount;
207 if ($variant eq 'single') {
209 $data->{finish}->{code} =
210 my $code = substr $data->{leveldata}, $offset, -5*$slots;
215 chr 0x21, # ld hl, MESSAGE
216 pack('v', $offsetbase + $offset + 9),
218 0xCD, 0x37, 0x4A, # call _puts
219 0xC3, 0xAA, 0x55, # jp _getkey
224 0x21, 0, 0x1C, # ld hl, $POS
225 0x22, 0x7C, 0xC3, # ld (_penCol), hl
226 0x21, # ld hl, MESSAGE
228 pack('v', $offsetbase + $offset + 15),
230 0xCD, 0xA5, 0x4A, # call _vputs
231 0xC3, 0xAA, 0x55, # jp _getkey
235 while (my ($finish, $match) = each %FINISHCODE) {
236 $match eq substr $code, 0, length $match or next;
237 $data->{finish}->{type} = $finish and
238 $data->{finish}->{message} = unpack 'Z*', substr($code, length $match);
253 Games::Wormy::TICalcLevels - Read Wormy levelset from a compiled TI-86 file
257 my $levelset = Games::Wormy::TICalcLevels->read($filecontents);
258 print $levelset->{name};
262 Mischa POSLAWSKY <perl@shiar.org>