Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

5.8 KB
Raw Blame
#!/usr/bin/env python3
"""src/data.c and src/data.h: Tomb of the Mask's 200 stages and 517 arcade sections, from the web game taken apart.

    python3 -I tools/data.py REF

REF is the reference kept outside the repository: its `text/` folder holds the game's TextAssets as files, among
them `stages__<id>` (200 records: lava speed, width, height, then the tiles, bottom row first) and `sections__<id>`
(517 records: section type, size, then the tiles of a 13-wide section). Nothing in REF is run. Each map is packed
with NumPlay's LZSS (games/common/np_lz.h), parsed optimally.
"""
import glob
import os
import sys

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def records(path, kind):
    d = open(path, 'rb').read()
    p, out = 0, []
    while p < len(d):
        if kind == 'stages':
            lava, w, h = d[p], d[p + 1], d[p + 2]
            out.append((lava, w, h, d[p + 3:p + 3 + w * h]))
            p += 3 + w * h
        else:
            t, n = d[p], d[p + 1]
            out.append((t, 13, n // 13, d[p + 2:p + 2 + n]))
            p += 2 + n
    if p != len(d):
        raise SystemExit('%s: damaged' % path)
    return out


def pack(data):
    """np_lz: a flag byte per 8 items (bit set: a literal), a match is 2 bytes (distance-1: 12 bits, length-3: 4
    bits, 3..18). Optimal parse by dynamic programming over the cost in bits."""
    n = len(data)
    inf = 1 << 60
    best = [inf] * (n + 1)
    best[n] = 0
    step = [None] * (n + 1)
    for i in range(n - 1, -1, -1):
        best[i] = 9 + best[i + 1]
        step[i] = (0, 0)
        for dist in range(1, min(i, 4096) + 1):
            length = 0
            while i + length < n and length < 18 and data[i + length] == data[i + length - dist]:
                length += 1
            for k in range(3, length + 1):
                c = 17 + best[i + k]
                if c < best[i]:
                    best[i] = c
                    step[i] = (k, dist)
    out = bytearray()
    i = 0
    flag_at, items = 0, 8
    while i < n:
        if items == 8:
            flag_at = len(out)
            out.append(0)
            items = 0
        k, dist = step[i]
        if k:
            out.append((dist - 1) & 255)
            out.append(((dist - 1) >> 8) | (k - 3) << 4)
            i += k
        else:
            out[flag_at] |= 1 << items
            out.append(data[i])
            i += 1
        items += 1
    return bytes(out)


def unpack(data, size):
    out = bytearray()
    i = 0
    while i < len(data):
        flags = data[i]
        i += 1
        for b in range(8):
            if i >= len(data):
                break
            if flags >> b & 1:
                out.append(data[i])
                i += 1
            else:
                dist = (data[i] | (data[i + 1] & 15) << 8) + 1
                k = (data[i + 1] >> 4) + 3
                i += 2
                for _ in range(k):
                    out.append(out[-dist])
    assert len(out) == size
    return bytes(out)


def c_bytes(name, data):
    lines = []
    for i in range(0, len(data), 32):
        lines.append(','.join(str(b) for b in data[i:i + 32]))
    return 'const uint8_t %s[%d] = {\n%s};\n' % (name, len(data), ',\n'.join(lines))


def main():
    if len(sys.argv) != 2:
        raise SystemExit(__doc__)
    ref = sys.argv[1]
    stages = records(glob.glob(os.path.join(ref, 'text', 'stages__*'))[0], 'stages')
    sections = records(glob.glob(os.path.join(ref, 'text', 'sections__*'))[0], 'sections')
    assert len(stages) == 200 and len(sections) == 517
    blob = bytearray()
    st_rows, se_rows = [], []
    for lava, w, h, tiles in stages:
        p = pack(tiles)
        assert unpack(p, len(tiles)) == tiles
        st_rows.append('{%d,%d,%d,%d,%d}' % (len(blob), len(p), w, h, lava))
        blob += p
    counts = [0] * 15
    first = [None] * 15
    for i, (t, w, h, tiles) in enumerate(sections):
        if first[t] is None:
            first[t] = i
        assert first[t] + counts[t] == i, 'sections not grouped by type'
        counts[t] += 1
        p = pack(tiles)
        assert unpack(p, len(tiles)) == tiles
        se_rows.append('{%d,%d,%d,%d,%d}' % (len(blob), len(p), w, h, t))
        blob += p
    h = ['/* Generated by tools/data.py from the web game\'s own stage and section caches: do not edit. */',
         '#ifndef TM_DATA_H', '#define TM_DATA_H', '#include <stdint.h>', '',
         '/* a packed map (games/common/np_lz.h): where it is in tm_maps, its size, width, height, and a byte more:',
         ' * a stage\'s lava speed (in hundredths of a unit a second, 0: none), a section\'s type */',
         'typedef struct { uint32_t at; uint16_t size; uint8_t w, h, extra; } tm_map;',
         '#define TM_STAGES 200', '#define TM_SECTIONS 517', '#define TM_MAPS_BYTES %d' % len(blob),
         'extern const uint8_t tm_maps[TM_MAPS_BYTES];', 'extern const tm_map tm_stages[TM_STAGES];',
         'extern const tm_map tm_sections[TM_SECTIONS];',
         '/* the sections of each type are together: the first of type t and how many */',
         'extern const uint16_t tm_section_first[15];', 'extern const uint8_t tm_section_count[15];',
         '#endif', '']
    c = ['/* Generated by tools/data.py from the web game\'s own stage and section caches: do not edit. */',
         '#include "data.h"', '', c_bytes('tm_maps', blob),
         'const tm_map tm_stages[TM_STAGES] = {%s};' % ','.join(st_rows),
         'const tm_map tm_sections[TM_SECTIONS] = {%s};' % ','.join(se_rows),
         'const uint16_t tm_section_first[15] = {%s};' % ','.join(str(x) for x in first),
         'const uint8_t tm_section_count[15] = {%s};' % ','.join(str(x) for x in counts), '']
    open(os.path.join(ROOT, 'src', 'data.h'), 'w').write('\n'.join(h))
    open(os.path.join(ROOT, 'src', 'data.c'), 'w').write('\n'.join(c))
    print('maps: %d bytes packed (from %d)' % (len(blob), sum(len(x[3]) for x in stages + sections)))


if __name__ == '__main__':
    main()