parse-wormedit: detect common finish code in 86s data
authorMischa Poslawsky <wormy@shiar.org>
Sun, 1 Mar 2009 16:18:24 +0000 (17:18 +0100)
committerMischa Poslawsky <wormy@shiar.org>
Mon, 2 Mar 2009 21:42:36 +0000 (22:42 +0100)
parse-wormedit

index 8136c58..4fd22e7 100755 (executable)
@@ -329,9 +329,39 @@ sub read {
                }
 
                if ($variant eq 'single') {
-                       $data->{finish}->{code} = substr(
-                               delete($data->{leveldata}), $offset, -5 * $slots
+                       $offset++;
+                       $data->{finish}->{code} =
+                       my $code = substr delete($data->{leveldata}), $offset, -5*$slots;
+
+                       my %FINISHCODE = (
+                               0 => chr 0xC9, # ret
+                               1 => join('',
+                                       chr 0x21,  # ld hl, MESSAGE
+                                       pack('v', $offsetbase + $offset + 9),
+                                       (map {chr}
+                                               0xCD, 0x37, 0x4A,  # call _puts
+                                               0xC3, 0xAA, 0x55,  # jp _getkey
+                                       ),
+                               ),
+                               2 => join('',
+                                       (map {chr}
+                                               0x21, 0, 0x1C,  # ld hl, $POS
+                                               0x22, 0x7C, 0xC3, # ld (_penCol), hl
+                                               0x21,  # ld hl, MESSAGE
+                                       ),
+                                       pack('v', $offsetbase + $offset + 15),
+                                       (map {chr}
+                                               0xCD, 0xA5, 0x4A, # call _vputs
+                                               0xC3, 0xAA, 0x55, # jp _getkey
+                                       ),
+                               ),
                        );
+                       while (my ($finish, $match) = each %FINISHCODE) {
+                               $match eq substr $code, 0, length $match or next;
+                               $data->{finish}->{type} = $finish or last;
+                               $data->{finish}->{message} = unpack 'Z*', substr($code, length $match);
+                               last;
+                       }
                }
        }