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.
C calculatorcalculator-gamescasiochesscrossy-roaddrive-madfx-cg50gamesgeometry-dashgraphing-calculatorndlessnumworksnumworks-appsnumworks-calculatornumworks-gamenumworks-gamestetristi-84-plus-ceti-nspire
Hollow Knight: 178 KB less data, the same pictures
Mason Chen44022af4460d
diff --git a/games/hollowknight/src/data.bin b/games/hollowknight/src/data.binindex 3c807e3d..a7cec365 100644Binary files a/games/hollowknight/src/data.bin and b/games/hollowknight/src/data.bin differdiff --git a/games/hollowknight/src/data.h b/games/hollowknight/src/data.hindex a3ccfc25..e91b32f8 100644--- a/games/hollowknight/src/data.h+++ b/games/hollowknight/src/data.h@@ -1,24 +1,23 @@/* made by tools/pack.py */#pragma once#define SEC_TEX 0-#define SEC_TMAP 1-#define SEC_PAL 2-#define SEC_BIDX 3-#define SEC_BLK 4-#define SEC_ROOMS 5-#define SEC_RBLOB 6-#define SEC_PRIOR 7-#define SEC_SOFT 8-#define SEC_SPR 9-#define SEC_CLIP 10-#define SEC_STR 11-#define SEC_TEXT 12-#define SEC_FONT 13-#define SEC_PHASH 14-#define SEC_VM 15-#define SEC_VMDEF 16-#define SEC_SCOL 17-#define SEC_RVAR 18+#define SEC_PAL 1+#define SEC_TDAT 2+#define SEC_ROOMS 3+#define SEC_RBLOB 4+#define SEC_PRIOR 5+#define SEC_SOFT 6+#define SEC_SPR 7+#define SEC_CLIP 8+#define SEC_STR 9+#define SEC_TEXT 10+#define SEC_FONT 11+#define SEC_PHASH 12+#define SEC_SCOL 13+#define SEC_RVAR 14+#define SEC_HEADS 15+#define SEC_VM 16+#define SEC_VMDEF 17#define NUM_ROOMS 38#define ROOM_TUTORIAL_01 0#define ROOM_TOWN 1diff --git a/games/hollowknight/src/gfx.c b/games/hollowknight/src/gfx.cindex 96f91f2e..31e20242 100644--- a/games/hollowknight/src/gfx.c+++ b/games/hollowknight/src/gfx.c@@ -145,6 +145,27 @@ static inline uint32_t inst_alpha(const Inst *in) {return in->group ? (a * g_group_alpha[in->group & (MAX_GROUPS - 1)] + 127) / 255 : a;}+/* a palette as tools/pack.py codes it (pal_code): per color, Rice codes of its steps from the one before (green, then+ * red and blue less half the green: SEC_PAL's first bytes say how many low bits each has), then a bit: opaque, else 8+ * bits of alpha */+typedef struct {+ const uint8_t *p;+ uint32_t buf;+ int n;+} Bits;+static uint32_t bits_get(Bits *b, int k) {+ while (b->n < k) b->buf |= (uint32_t)*b->p++ << b->n, b->n += 8;+ uint32_t v = b->buf & ((1u << k) - 1);+ b->buf >>= k, b->n -= k;+ return v;+}+static int bits_step(Bits *b, int k) {+ uint32_t q = 0;+ while (bits_get(b, 1)) q++;+ uint32_t u = q << k | bits_get(b, k);+ return (int)(u >> 1) ^ -(int)(u & 1);+}+static int pal_get(uint16_t tex, uint8_t tint, uint8_t flags, uint32_t alpha) {/* (tex << 17: solid colors' TEX_NONE comes out as 0xFFFE....; black textures' keys below those) */uint32_t key = (uint32_t)tex << 17 | alpha << 9 | (uint32_t)tint << 1 | ((flags & F_LIT) ? 1 : 0);@@ -188,15 +209,18 @@ static int pal_get(uint16_t tex, uint8_t tint, uint8_t flags, uint32_t alpha) {if (r->fmt == FMT_ALPHA2) { /* (drawn black whatever the tint) */for (int i = 1; i < 4; i++) p->c[i] = (uint32_t)(ta * i / 3.0f * 255 + 0.5f) << 24;} else {- const uint8_t *pc = section(SEC_PAL) + r->pal_off;/* (a flash: towards its color by its amount, the alpha kept) */float fk = tint >= DYN_TINT ? t[7] / 255.0f : 0, fr = fk ? t[4] / 255.0f * fk : 0, fg = fk ? t[5] / 255.0f * fk : 0,fb = fk ? t[6] / 255.0f * fk : 0;+ const uint8_t *ks = section(SEC_PAL);+ Bits bits = {ks + 2 * r->pal, 0, 0};+ int g = 0, ro = 0, bo = 0;for (int i = 0; i < 15; i++) {- /* (RGB565, then alpha) */- unsigned v = pc[3 * i] | pc[3 * i + 1] << 8;- p->c[i + 1] = grade((float)(v >> 11) / 31 * tr * (1 - fk) + fr, (float)(v >> 5 & 63) / 63 * tg * (1 - fk) + fg,- (float)(v & 31) / 31 * tb * (1 - fk) + fb, pc[3 * i + 2] / 255.0f * ta);+ /* (red 5 bits, green 6, blue 5) */+ g += bits_step(&bits, ks[0]), ro += bits_step(&bits, ks[1]), bo += bits_step(&bits, ks[2]);+ int a = bits_get(&bits, 1) ? 255 : (int)bits_get(&bits, 8);+ p->c[i + 1] = grade((float)(ro + (g >> 1)) / 31 * tr * (1 - fk) + fr, (float)g / 63 * tg * (1 - fk) + fg,+ (float)(bo + (g >> 1)) / 31 * tb * (1 - fk) + fb, a / 255.0f * ta);}}return s;@@ -228,10 +252,13 @@ static void pals_forget_tint(int tint) {static void soft_reset(void) { nsoft = 0, soft_top = 0; }+/* (FMT_SOFTA: its data is its color, then its alpha texels) */+static uint32_t softa_color(const TexRec *r) { return rd32(section(SEC_SOFT) + r->off); }+uint32_t g_soft_misses;static const uint32_t *soft_get(uint16_t tex, uint8_t tint, uint8_t flags, uint32_t alpha) {const TexRec *rr = tex_rec(tex);- if (rr->fmt == FMT_SOFTA) return (const uint32_t *)(const void *)(section(SEC_SOFT) + rr->pal_off); /* (alpha, in place) */+ if (rr->fmt == FMT_SOFTA) return (const uint32_t *)(const void *)(section(SEC_SOFT) + rr->off + 4); /* (alpha, in place) */uint32_t key = (uint32_t)tex << 17 | alpha << 9 | (uint32_t)tint << 1 | ((flags & F_LIT) ? 1 : 0);for (int i = 0; i < nsoft; i++)if (soft_ent[i].key == key) {@@ -256,7 +283,7 @@ static const uint32_t *soft_get(uint16_t tex, uint8_t tint, uint8_t flags, uint3if (soft_top + n > SOFT_POOL || nsoft == NSOFT) return NULL;}uint32_t *px = soft_pool + soft_top;- const uint8_t *src = section(SEC_SOFT) + r->pal_off;+ const uint8_t *src = section(SEC_SOFT) + r->off;lz_decode(src + 4, rd32(src), (uint8_t *)px, (uint32_t)n * 4);const uint8_t *t = tint_rgba(tint);float tr = t[0] / 255.0f, tg = t[1] / 255.0f, tb = t[2] / 255.0f, ta = alpha / 255.0f;@@ -785,7 +812,7 @@ __attribute__((noinline)) static void draw_item(int idx, Item *it, int sy0, intconst uint8_t *t = tint_rgba(in->tint);float tr = t[0] / 255.0f, tg = t[1] / 255.0f, tb = t[2] / 255.0f;if (in->flags & F_LIT) tr *= g_room.h->ambient[0], tg *= g_room.h->ambient[1], tb *= g_room.h->ambient[2];- uint32_t base = c.r->map_off;+ uint32_t base = softa_color(c.r);grade_off = in->tint >= HUD_TINT;uint32_t col = grade((base & 255) / 255.0f * tr, (base >> 8 & 255) / 255.0f * tg, (base >> 16 & 255) / 255.0f * tb, 1);crb = col & 0xFF00FF, cg = col & 0xFF00, ta = inst_alpha(in) + 1u;@@ -902,7 +929,7 @@ static void draw_bg_item(Item *it) {const uint8_t *t = tint_rgba(in->tint);float tr = t[0] / 255.0f, tg = t[1] / 255.0f, tb = t[2] / 255.0f;if (in->flags & F_LIT) tr *= g_room.h->ambient[0], tg *= g_room.h->ambient[1], tb *= g_room.h->ambient[2];- uint32_t base = r->map_off;+ uint32_t base = softa_color(r);grade_off = in->tint >= HUD_TINT;uint32_t c = grade((base & 255) / 255.0f * tr, (base >> 8 & 255) / 255.0f * tg, (base >> 16 & 255) / 255.0f * tb, 1);crb = c & 0xFF00FF, cg = c & 0xFF00, ta = inst_alpha(in) + 1u;diff --git a/games/hollowknight/src/hk.h b/games/hollowknight/src/hk.hindex 2b2f147d..91df8e66 100644--- a/games/hollowknight/src/hk.h+++ b/games/hollowknight/src/hk.h@@ -49,10 +49,13 @@ const char *str_at(int id); /* the shared string table (SEC_STR) *//* ---------------------------------------------------------------- textures (tex.c) */typedef struct {uint16_t w, h;- uint8_t tw, th, fmt, flags; /* flags: TEX_SMOOTH (drawn interpolated: the blurred background) */- uint32_t pal_off, map_off, blk_first;+ uint8_t tw, th;+ uint16_t pal; /* its colors (in SEC_PAL, at 2 x pal: gfx.c) */+ uint32_t off : 27, fmt : 2, flags : 3; /* its data: SEC_TDAT (tiles: tex.c), SEC_SOFT (smooth: gfx.c) */} TexRec;-#define TEX_SMOOTH 1+#define TEX_SMOOTH 1 /* drawn interpolated: the blurred background */+#define TEX_FULL 2 /* every tile kept, none opaque: no tile map */+#define TEX_WIDE 4 /* tile ranks of 2 bytes */enum { FMT_PAL4 = 0, FMT_ALPHA2 = 1, FMT_SOFT = 2, FMT_SOFTA = 3 };#define TILE 16#define TILE_SHIFT(fmt) ((fmt) == FMT_ALPHA2 ? 5 : 4) /* a tile's width, log 2 */diff --git a/games/hollowknight/src/room.c b/games/hollowknight/src/room.cindex f021c3ed..8fa173c8 100644--- a/games/hollowknight/src/room.c+++ b/games/hollowknight/src/room.c@@ -81,22 +81,33 @@ static inline int32_t svar(const uint8_t **pp) {}/* the stream's next instance (into s->in), false at its end */+static const uint8_t *heads; /* (SEC_HEADS: the common heads, flags | aux << 8 | rank step << 16) */static bool advance(Stream *s) {if (!s->left) return false;s->left--;const uint8_t *p = s->p;Inst *in = &s->in;- uint8_t flags = *p++, aux = *p++;+ uint32_t h = *p++;+ if (h < 255) h = ((const uint32_t *)(const void *)heads)[h];+ else h = p[0] | p[1] << 8, p += 2;+ uint8_t flags = (uint8_t)h, aux = (uint8_t)(h >> 8);in->flags = flags;- s->rank = (uint16_t)(s->rank + ((aux & 64) ? 1 : uvar(&p) + 2));+ s->rank = (uint16_t)(s->rank + ((h >> 16) ? (h >> 16) : uvar(&p) + 1));if (aux & 128) {uint8_t layer = *p++;s->group = SORT_KEY(layer, svar(&p));}if (!(aux & 2)) in->tex = (uint16_t)(in->tex + svar(&p));in->tint = (aux & 1) ? *p++ : 0;- in->ax = (int16_t)(in->ax + svar(&p));- in->ay = (int16_t)(in->ay + svar(&p));+ if (aux & 64) { /* (x and y steps in 12 bits each) */+ uint32_t v = p[0] | p[1] << 8 | (uint32_t)p[2] << 16;+ p += 3;+ in->ax = (int16_t)(in->ax + ((int32_t)(v << 20) >> 20));+ in->ay = (int16_t)(in->ay + ((int32_t)(v << 8) >> 20));+ } else {+ in->ax = (int16_t)(in->ax + svar(&p));+ in->ay = (int16_t)(in->ay + svar(&p));+ }if (!(aux & 4)) in->z = (int16_t)(in->z + svar(&p));if (!(aux & 32)) in->a = rd16(p), p += 2;switch (aux >> 3 & 3) {@@ -114,6 +125,7 @@ static bool advance(Stream *s) {void room_first(void) {const uint8_t *blob = section(SEC_RBLOB);+ heads = section(SEC_HEADS);nstreams = 0;for (int j = 0; j < g_room.nnear; j++) {const SectorRec *r = &g_room.secs[g_room.near[j]];diff --git a/games/hollowknight/src/tex.c b/games/hollowknight/src/tex.cindex 88176db5..b58e5b1e 100644--- a/games/hollowknight/src/tex.c+++ b/games/hollowknight/src/tex.c@@ -136,16 +136,31 @@ static bool inited;uint32_t g_tex_decodes, g_tex_misses, g_tex_calls;bool g_tex_overload;-static const uint8_t *sec_tex, *sec_tmap, *sec_bidx, *sec_blk;+static const uint8_t *sec_tex, *sec_tdat;const TexRec *tex_rec(uint16_t t) {if (!sec_tex) sec_tex = section(SEC_TEX) + 4;- return (const TexRec *)(sec_tex + sizeof(TexRec) * t);+ return (const TexRec *)(const void *)(sec_tex + sizeof(TexRec) * t);}+_Static_assert(sizeof(TexRec) == 12, "TexRec: as tools/pack.py writes it");-static const uint8_t *row_base(const TexRec *r, int ty) { return sec_tmap + r->map_off + ty * (2 + (r->tw + 3) / 4); }+/* (tools/pack.py) a texture's data: its tile map, where its blocks after the first start (2 bytes each, from its data),+ * its blocks. The map: per row, the rank of its first tile (a byte, 2 if TEX_WIDE), then 2 bits a tile; none for+ * TEX_FULL (every row as full_row) */+static const uint8_t full_row[64] = {[0 ... 63] = 0x55};+static int row_stride(const TexRec *r) { return ((r->flags & TEX_WIDE) ? 2 : 1) + (r->tw + 3) / 4; }+static const uint8_t *row_base(const TexRec *r, int ty) { return sec_tdat + r->off + ty * row_stride(r); }+static int row_rank(const TexRec *r, int ty) {+ if (r->flags & TEX_FULL) return ty * r->tw;+ const uint8_t *b = row_base(r, ty);+ return (r->flags & TEX_WIDE) ? rd16(b) : b[0];+}+static const uint8_t *row_codes(const TexRec *r, int ty) {+ if (r->flags & TEX_FULL) return full_row;+ return row_base(r, ty) + ((r->flags & TEX_WIDE) ? 2 : 1);+}const uint8_t *tex_row_codes(uint16_t t, int ty) {- if (!sec_tmap) sec_tmap = section(SEC_TMAP);- return row_base(tex_rec(t), ty) + 2;+ if (!sec_tdat) sec_tdat = section(SEC_TDAT);+ return row_codes(tex_rec(t), ty);}static int code_at(const uint8_t *codes, int tx) { return codes[tx >> 2] >> ((tx & 3) * 2) & 3; }@@ -158,22 +173,24 @@ static void init_kept4(void) {/* the tile's number among the texture's kept tiles */static int tile_rank(const TexRec *r, int tx, int ty) {- const uint8_t *b = row_base(r, ty), *c = b + 2;- int n = rd16(b), i = 0;+ if (r->flags & TEX_FULL) return ty * r->tw + tx;+ const uint8_t *c = row_codes(r, ty);+ int n = row_rank(r, ty), i = 0;for (; i + 4 <= tx; i += 4) n += kept4[c[i >> 2]];for (; i < tx; i++) n += code_at(c, i) != 0;return n;}static int tex_tiles(const TexRec *r) {- const uint8_t *b = row_base(r, r->th - 1), *c = b + 2;- int n = rd16(b);+ if (r->flags & TEX_FULL) return r->tw * r->th;+ const uint8_t *c = row_codes(r, r->th - 1);+ int n = row_rank(r, r->th - 1);for (int i = 0; i < r->tw; i++) n += code_at(c, i) != 0;return n;}static void init(void) {init_kept4();- sec_tex = section(SEC_TEX) + 4, sec_tmap = section(SEC_TMAP), sec_bidx = section(SEC_BIDX), sec_blk = section(SEC_BLK);+ sec_tex = section(SEC_TEX) + 4, sec_tdat = section(SEC_TDAT);for (int i = 0; i < NHASH; i++) head[i] = NO;for (int i = 0; i < NSLOTS; i++) slot_key[i] = 0xFFFFFFFF, slot_next[i] = NO, slot_frame[i] = 0;inited = true;@@ -240,10 +257,10 @@ const uint8_t *tex_slot_px(int s) { return slot_px[s]; }static void block_geometry(const TexRec *r, int first, int n, uint8_t *left, uint8_t *up) {uint8_t tx[BLOCK_TILES], ty[BLOCK_TILES];int y = 0, i = 0;- while (y + 1 < r->th && rd16(row_base(r, y + 1)) <= first) y++;- for (int k = rd16(row_base(r, y)); i < n; y++)+ while (y + 1 < r->th && row_rank(r, y + 1) <= first) y++;+ for (int k = row_rank(r, y); i < n; y++)for (int x = 0; x < r->tw && i < n; x++)- if (code_at(row_base(r, y) + 2, x) && k++ >= first) tx[i] = (uint8_t)x, ty[i] = (uint8_t)y, i++;+ if (code_at(row_codes(r, y), x) && k++ >= first) tx[i] = (uint8_t)x, ty[i] = (uint8_t)y, i++;for (i = 0; i < n; i++) {left[i] = i && ty[i - 1] == ty[i] && tx[i - 1] + 1 == tx[i] ? (uint8_t)(i - 1) : NONE_T;up[i] = NONE_T;@@ -262,7 +279,7 @@ int tex_slot(uint16_t t, int tx, int ty) {g_tex_calls++;const TexRec *r = tex_rec(t);if ((unsigned)tx >= r->tw || (unsigned)ty >= r->th) return -1;- if (!code_at(row_base(r, ty) + 2, tx)) return -1;+ if (!code_at(row_codes(r, ty), tx)) return -1;int rank = tile_rank(r, tx, ty);uint32_t key = (uint32_t)t << 16 | (uint32_t)rank;int s = find(key);@@ -274,12 +291,14 @@ int tex_slot(uint16_t t, int tx, int ty) {/* decode the block */int bytes = 128;int blk = rank / BLOCK_TILES, first = blk * BLOCK_TILES;- int ntiles = tex_tiles(r) - first;+ int all = tex_tiles(r), ntiles = all - first;if (ntiles > BLOCK_TILES) ntiles = BLOCK_TILES;- const uint8_t *bidx = sec_bidx + 4 * (r->blk_first + blk);+ /* (the first block after the map and where the others start) */+ const uint8_t *d = sec_tdat + r->off, *starts = d + ((r->flags & TEX_FULL) ? 0 : r->th * row_stride(r));+ const uint8_t *src = blk ? d + rd16(starts + 2 * (blk - 1)) : starts + 2 * ((all - 1) / BLOCK_TILES);uint8_t left[BLOCK_TILES], up[BLOCK_TILES];block_geometry(r, first, ntiles, left, up);- decode_block(sec_blk + rd32(bidx), ntiles, r->fmt == FMT_ALPHA2, left, up, block_buf);+ decode_block(src, ntiles, r->fmt == FMT_ALPHA2, left, up, block_buf);g_tex_decodes++;int want = insert((uint32_t)t << 16 | (uint32_t)rank, block_buf + (rank - first) * bytes, bytes);slot_frame[want] = frame;diff --git a/games/hollowknight/tools/pack.py b/games/hollowknight/tools/pack.pyindex 117d4ea0..4c0b76e3 100644--- a/games/hollowknight/tools/pack.py+++ b/games/hollowknight/tools/pack.py@@ -54,9 +54,8 @@ def room_data(name, variant=0):ROOM_DATA[(name, variant)] = ((solid, segs, cols, owners), (recs, groups), spans)return ROOM_DATA[(name, variant)]-SECTIONS = ["TEX", "TMAP", "PAL", "BIDX", "BLK", "ROOMS", "RBLOB", "PRIOR", "SOFT", "SPR", "CLIP", "STR", "TEXT", "FONT", "PHASH",- "VM", "VMDEF",- "SCOL", "RVAR"]+SECTIONS = ["TEX", "PAL", "TDAT", "ROOMS", "RBLOB", "PRIOR", "SOFT", "SPR", "CLIP", "STR", "TEXT", "FONT", "PHASH",+ "SCOL", "RVAR", "HEADS", "VM", "VMDEF"]def _pdf_flags():@@ -162,12 +161,14 @@ def svar(v):return uvar(v << 1 if v >= 0 else ((-v) << 1) - 1)-def sector_stream(recs):- """A sector's instances in draw order, each relative to the one before (read in place by room.c):- flags, aux (1 tint, 2 same texture, 4 same z, 8|16 b: 0 its own, 1 = a, 2 = -a, 3 same as before, 32 same a,- 64 next rank, 128 another sorting layer or order), [rank step - 2], [layer, order], [texture step], [tint],- x step, y step, [z step], [a], [b], [angle], [render group (F_DYN)]."""- out = bytearray()+A_XY12 = 64 # (aux: x and y steps in 12 bits each)+MAX_STEP = 3 # (rank steps a head can hold)+++def stream_insts(recs):+ """A sector's instances as sector_stream codes them: (flags, aux, rank step, whether x and y fit 12 bits, the+ bytes before x and y, x and y steps, the bytes after)."""+ out = []pr, pt, px, py, pz, pa, pb, pg = -1, 0, 0, 0, 0, 0, 0, Nonefor rank, (ax, ay, z, tex, tint, flags, a, b, rot, rgroup), group in recs:X, Y, Z, A, B = i16(ax * 64), i16(ay * 64), i16(z * 128), f16s(a) & 0xFFFF, f16s(b) & 0xFFFF@@ -182,31 +183,70 @@ def sector_stream(recs):aux |= bm << 3if A == pa:aux |= 32- if rank == pr + 1:- aux |= 64if group != pg:aux |= 128- out += bytes([flags, aux])- if not aux & 64:- out += uvar(rank - pr - 2)+ pre = bytearray()if aux & 128:- out += bytes([group[0]]) + svar(group[1])+ pre += bytes([group[0]]) + svar(group[1])if not aux & 2:- out += svar(tex - pt)+ pre += svar((tex - pt + 32768 & 0xFFFF) - 32768) # (the game's sum wraps at 16 bits)if tint:- out.append(tint)- out += svar(X - px) + svar(Y - py)+ pre.append(tint)+ post = bytearray()if not aux & 4:- out += svar(Z - pz)+ post += svar(Z - pz)if not aux & 32:- out += struct.pack("<H", A)+ post += struct.pack("<H", A)if bm == 0:- out += struct.pack("<H", B)+ post += struct.pack("<H", B)if flags & F_ROT:- out += struct.pack("<h", rot)+ post += struct.pack("<h", rot)if flags & F_DYN:- out.append(rgroup)+ post.append(rgroup)+ dx, dy = X - px, Y - py+ out.append((flags, aux, rank - pr, -2048 <= dx < 2048 and -2048 <= dy < 2048, bytes(pre), dx, dy, bytes(post)))pr, pt, px, py, pz, pa, pb, pg = rank, tex, X, Y, Z, A, B, group+ return out+++def head_symbol(inst):+ """The head an instance would rather have: (flags, aux, rank step or 0 if it is given after)"""+ flags, aux, step, xy12 = inst[:4]+ return flags, aux | (A_XY12 if xy12 else 0), step if step <= MAX_STEP else 0+++def head_table(streams):+ """The 255 most common heads (sector_stream) of all the rooms' instances: (bytes, symbol -> code)"""+ import collections+ count = collections.Counter(head_symbol(i) for s in streams for i in s)+ top = [k for k, _ in count.most_common(255)]+ return b"".join(struct.pack("<I", f | a << 8 | s << 16) for f, a, s in top), {k: i for i, k in enumerate(top)}+++def sector_stream(insts, heads):+ """A sector's instances in draw order, each relative to the one before (read in place by room.c): a head (a code+ of the HEADS table: flags, aux, rank step; 255: flags and aux follow), [rank step - 1 (none in the head)], [layer,+ order], [texture step], [tint], x step, y step (12 bits each if A_XY12, else on their own), [z step], [a], [b],+ [angle], [render group (F_DYN)]. aux: 1 tint, 2 same texture, 4 same z, 8|16 b: 0 its own, 1 = a, 2 = -a, 3 same as+ before, 32 same a, 64 A_XY12, 128 another sorting layer or order."""+ out = bytearray()+ for flags, aux, step, xy12, pre, dx, dy, post in insts:+ xy = struct.pack("<I", (dx & 0xFFF) | (dy & 0xFFF) << 12)[:3] if xy12 else svar(dx) + svar(dy)+ best = None+ # (the cheapest head: the one wanted, else one with the steps on their own)+ for x12 in ((True, False) if xy12 else (False,)):+ for st in ((step, 0) if step <= MAX_STEP else (0,)):+ code = heads.get((flags, aux | (A_XY12 if x12 else 0), st))+ if code is None:+ continue+ b = bytes([code]) + (uvar(step - 1) if not st else b"") + pre + \+ (xy if x12 or not xy12 else svar(dx) + svar(dy)) + post+ if best is None or len(b) < len(best):+ best = b+ if best is None:+ a = aux | (A_XY12 if xy12 else 0)+ best = bytes([255, flags, a]) + uvar(step - 1) + pre + xy + post+ out += bestreturn bytes(out)@@ -245,7 +285,7 @@ def room_blobs(name, keep, st, tex_id, variant=0):table = bytearray()for k in order:recs, va, vb = secs[k]- blobs.append((sector_stream(recs), len(recs)))+ blobs.append((stream_insts(recs), len(recs)))if k < 0:va, vb = -1e9, 1e9table += struct.pack("<IIHHff", 0, 0, len(recs), 0, va, vb) # (offsets filled in by main)@@ -292,6 +332,76 @@ def room_blobs(name, keep, st, tex_id, variant=0):return body, o_sec, blobs, bytes(cb), b"".join(erecs)+def pal_colors(pal):+ """A palette's 15 colors as the game has them: (red 5 bits, green 6, blue 5, alpha 8)"""+ return tuple(((int(r) * 31 + 127) // 255, (int(g) * 63 + 127) // 255, (int(b) * 31 + 127) // 255, int(a))+ for r, g, b, a in pal)+++def pal_steps(cols):+ """What pal_code codes of a palette's colors: per color, the steps (zigzag) from the one before in green, then in+ red and blue less half the green."""+ def zz(v):+ return v << 1 if v >= 0 else (-v << 1) - 1+ out, pg, pr, pb = [], 0, 0, 0+ for r, g, b, a in cols:+ out.append((zz(g - pg), zz(r - (g >> 1) - pr), zz(b - (g >> 1) - pb)))+ pg, pr, pb = g, r - (g >> 1), b - (g >> 1)+ return out+++def pal_ks(pals):+ """The Rice codes' parameters that code the steps of all the palettes best (green, red, blue)."""+ steps = [st for c in pals for st in pal_steps(c)]+ return [min(range(9), key=lambda k: sum((st[ch] >> k) + 1 + k for st in steps)) for ch in range(3)]+++def pal_code(cols, ks):+ """A palette (src/gfx.c: pal_get): per color, its steps (pal_steps: a Rice code each, ks: green, red, blue), then+ a bit: 1 opaque, 0 its alpha follows (8 bits); bits from the low end of each byte."""+ bits = []+ def put(v, n):+ bits.extend(v >> i & 1 for i in range(n))+ for (r, g, b, a), st in zip(cols, pal_steps(cols)):+ for ch in range(3):+ bits.extend([1] * (st[ch] >> ks[ch]) + [0])+ put(st[ch], ks[ch])+ put(a == 255, 1)+ if a != 255:+ put(a, 8)+ bits += [0] * (-len(bits) % 8)+ return bytes(sum(bits[i + j] << j for j in range(8)) for i in range(0, len(bits), 8))+++TEX_SMOOTH, TEX_FULL, TEX_WIDE = 1, 2, 4+++def tex_record(w, h, tw, th, pal, off, fmt, flags):+ """A texture's record (src/hk.h: TexRec): its size, tiles across and down, palette, then where its data is (27+ bits), its format (2) and flags (3)."""+ assert off < 1 << 27 and fmt < 4 and flags < 8+ return struct.pack("<HHBBHI", w, h, tw, th, pal, off | fmt << 27 | flags << 29)+++def tile_map(t):+ """A texture's tile map: per row, the rank of its first tile (a byte, 2 if TEX_WIDE), then 2 bits a tile (0 empty, 1+ tile, 3 opaque tile); none at all (TEX_FULL) if every tile is kept and none is opaque. -> (bytes, flags)"""+ codes = [[0 if t.empty[ty, tx] else (3 if t.opaque[ty, tx] else 1) for tx in range(t.tw)] for ty in range(t.th)]+ if all(c == 1 for row in codes for c in row):+ return b"", TEX_FULL+ wide = t.ntiles > 255+ m = bytearray()+ rank = 0+ for row in codes:+ m += struct.pack("<H" if wide else "<B", rank)+ b = bytearray((t.tw + 3) // 4)+ for tx, code in enumerate(row):+ b[tx >> 2] |= code << ((tx & 3) * 2)+ rank += code != 0+ m += b+ return bytes(m), TEX_WIDE if wide else 0++def tile_blocks(t):"""A texture's tiles in blocks: (bits, tiles, per tile the one to its left and the one above in the block (255:none), as src/tex.c finds them)"""@@ -406,48 +516,43 @@ def main():# the starting probabilities as the game uses them (tilecode.h)secs["PRIOR"].b += priortex_id = {}- pal_at = {} # (palettes shared: once each)- nblk = 0+ # the palettes (shared: once each), coded: where each one is, 2 bytes a unit+ pal_at = {pal_colors(t.pal): 0 for t in texs if t.pal is not None}+ ks = pal_ks(pal_at)+ secs["PAL"].b += bytes(ks) + bytes(1)+ for c in pal_at:+ secs["PAL"].align(2)+ pal_at[c] = len(secs["PAL"].b) // 2+ secs["PAL"].b += pal_code(c, ks)+ assert len(secs["PAL"].b) < 2 << 16texrec = bytearray()for i, (v, t) in enumerate(zip(variants, texs)):tex_id[id(v)] = (i, t.w, t.h)- pal_off = 0xFFFFFFFF- if t.pal is not None:- # (15 colors: RGB565, then alpha; read a byte at a time)- pb = b"".join(struct.pack("<HB", (int(r) * 31 + 127) // 255 << 11 | (int(g) * 63 + 127) // 255 << 5 |- (int(b) * 31 + 127) // 255, int(a)) for r, g, b, a in t.pal)- if pb not in pal_at:- pal_at[pb] = secs["PAL"].add(pb, 1)- pal_off = pal_at[pb]- if t.fmt == art.FMT_SOFTA: # (alpha only: read in place)- pal_off = secs["SOFT"].add(t.soft)- texrec += struct.pack("<HHBBBBIII", t.w, t.h, 0, 0, t.fmt, 0, pal_off, t.base, 0)+ pal = pal_at[pal_colors(t.pal)] if t.pal is not None else 0+ if t.fmt == art.FMT_SOFTA: # (its color, then its alpha: read in place)+ off = secs["SOFT"].add(struct.pack("<I", t.base) + t.soft)+ texrec += tex_record(t.w, t.h, 0, 0, pal, off, t.fmt, 0)continueif t.fmt == art.FMT_SOFT:c = lz(t.soft)- pal_off = secs["SOFT"].add(struct.pack("<I", len(c)) + c)- texrec += struct.pack("<HHBBBBIII", t.w, t.h, 0, 0, t.fmt, 0, pal_off, t.base, 0)+ off = secs["SOFT"].add(struct.pack("<I", len(c)) + c)+ texrec += tex_record(t.w, t.h, 0, 0, pal, off, t.fmt, 0)continue- # tile map: per row, the rank of its first tile, then 2 bits a tile (0 empty, 1 tile, 3 opaque tile)- m = bytearray()- rank = 0- for ty in range(t.th):- m += struct.pack("<H", rank)- row = bytearray((t.tw + 3) // 4)- for tx in range(t.tw):- code = 0 if t.empty[ty, tx] else (3 if t.opaque[ty, tx] else 1)- row[tx >> 2] |= code << ((tx & 3) * 2)- rank += code != 0- m += row- map_off = secs["TMAP"].add(bytes(m))- bidx = secs["BIDX"].b- first = len(bidx) // 4- for bl in coded[t.first_block:t.first_block + (t.ntiles + art.BLOCK_TILES - 1) // art.BLOCK_TILES]:- bidx += struct.pack("<I", len(secs["BLK"].b))- secs["BLK"].b += bl- bidx += struct.pack("<I", len(secs["BLK"].b))- texrec += struct.pack("<HHBBBBIII", t.w, t.h, t.tw, t.th, t.fmt, 1 if v.blur else 0, pal_off, map_off, first)- secs["BLK"].b += bytes(3) # (the game reads up to 3 bytes past a block)+ bl = coded[t.first_block:t.first_block + (t.ntiles + art.BLOCK_TILES - 1) // art.BLOCK_TILES]+ m, flags = tile_map(t)+ if v.blur:+ flags |= TEX_SMOOTH+ # its data: the tile map, where its blocks after the first start (from its data), its blocks+ d = bytearray(m)+ at = len(d) + 2 * (len(bl) - 1)+ for b in bl[:-1]:+ at += len(b)+ d += struct.pack("<H", at)+ for b in bl:+ d += b+ assert at < 65536, (i, at)+ texrec += tex_record(t.w, t.h, t.tw, t.th, pal, secs["TDAT"].add(bytes(d), 1), t.fmt, flags)+ secs["TDAT"].b += bytes(3) # (the game reads up to 3 bytes past a block)if os.environ.get("ACTSTATS"):import collectionstot = collections.Counter()@@ -496,14 +601,23 @@ def main():rooms = bytearray(struct.pack("<I", len(ROOMS) + len(VARIANT_ROOMS)))sector_offs, rvar = {}, bytearray(struct.pack("<I", len(VARIANT_ROOMS)))pdf = _pdf_flags()+ made = {}+ for r in ROOMS + [r + "@1" for r in VARIANT_ROOMS]:+ base, variant = (r[:-2], 1) if r.endswith("@1") else (r, 0)+ keep, st = per_room[base]+ made[r] = room_blobs(base, keep, st, tex_id, variant)+ # the instances' heads: the most common of all the rooms' (a variant's sectors are its room's)+ table, heads = head_table([s for r in ROOMS for s, n in made[r][2]])+ secs["HEADS"].b += tablefor r in ROOMS + [r + "@1" for r in VARIANT_ROOMS]:base, variant = (r[:-2], 1) if r.endswith("@1") else (r, 0)keep, st = per_room[base]- body, o_sec, blobs, ground, recs = room_blobs(base, keep, st, tex_id, variant)+ body, o_sec, blobs, ground, recs = made[r]struct.pack_into("<I", body, 76, secs["RBLOB"].add(ground))struct.pack_into("<I", body, 80, secs["RBLOB"].add(recs))packed = 0- for i, (c, n) in enumerate(blobs):+ for i, (insts, n) in enumerate(blobs):+ c = sector_stream(insts, heads)# (a room's variant: the same sectors)off = sector_offs[(base, i)] if variant else secs["RBLOB"].add(c)sector_offs[(base, i)] = off