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.

Celeste: Lily's shortest texture rows and zigzagged decals, tidied

Mason3631fdc73240b51
diff --git a/games/celeste/src/level.c b/games/celeste/src/level.c
index 9bc86a04..30e46ca2 100644
--- a/games/celeste/src/level.c
+++ b/games/celeste/src/level.c
@@ -110,20 +110,17 @@ static void tiles_unpack(uint8_t *dst, const uint8_t *src, int tw, int th) {
}
}
-/* CELESTE_DECAL_ZIGZAG_XY_V1: restore x/y arrays in the existing room buffer.
- * No heap or extra persistent RAM, and no per-frame decoding. */
-static void cel_decals_unzigzag(uint8_t *part) {
- unsigned n = rd16(part), nt = part[2];
- uint8_t *xs = part + 4 + 2 * nt;
- for (unsigned axis = 0; axis < 2; axis++) {
- uint8_t *p = xs + (axis ? 2 * n : 0);
- uint32_t prev = 0;
- for (unsigned i = 0; i < n; i++) {
- unsigned z = rd16(p + 2 * i);
- int32_t d = (int32_t)(z >> 1) ^ -(int32_t)(z & 1u);
- prev = (prev + d) & 65535u;
- p[2 * i] = (uint8_t)prev;
- p[2 * i + 1] = (uint8_t)(prev >> 8);
+/* a room's decals' x and y (in columns), stored as differences from the one before, zigzagged (tools/pack.py
+ * zigzag_decals): back as they are */
+static void decals_unzigzag(uint8_t *d) {
+ int n = rd16(d);
+ uint8_t *p = d + 4 + 2 * d[2];
+ for (int col = 0; col < 2; col++) {
+ uint16_t v = 0;
+ for (int i = 0; i < n; i++, p += 2) {
+ unsigned z = rd16(p);
+ v = (uint16_t)(v + ((z >> 1) ^ -(z & 1)));
+ p[0] = (uint8_t)v, p[1] = (uint8_t)(v >> 8);
}
}
}
@@ -166,9 +163,8 @@ static bool room_load(int slot, int index, int intro) {
tiles_unpack(mem + hdr[RB_FG], blob + rd16(blob + 2 * RB_FG), rm->tw, rm->th);
tiles_unpack(mem + hdr[RB_BG], blob + rd16(blob + 2 * RB_BG), rm->tw, rm->th);
memcpy(mem + hdr[RB_EXTRAS], blob + from, keep - hdr[RB_EXTRAS]);
- /* CELESTE_DECAL_ZIGZAG_XY_V1: restore decoded coordinates before exposing decal pointers. */
- cel_decals_unzigzag(mem + hdr[RB_DFG]);
- cel_decals_unzigzag(mem + hdr[RB_DBG]);
+ decals_unzigzag(mem + hdr[RB_DFG]);
+ decals_unzigzag(mem + hdr[RB_DBG]);
setup_layer(&rm->layer[0], mem + rd16(mem + 2 * RB_FG), rm->th);
setup_layer(&rm->layer[1], mem + rd16(mem + 2 * RB_BG), rm->th);
/* extras: objtiles, then scenery over fg and bg */
diff --git a/games/celeste/tools/pack.py b/games/celeste/tools/pack.py
index eb954127..d8284fd9 100644
--- a/games/celeste/tools/pack.py
+++ b/games/celeste/tools/pack.py
@@ -351,56 +351,46 @@ def direct_rows(rows, at, pool):
return bytes(lens + ops)
-# CELESTE_DIRECT_OPTIMAL_ROW_RLE_V1: shortest existing RLE instructions, no decoder changes.
-def _celeste_optimal_direct_row(row, nib):
+def shortest_row(row, nib):
+ """A row's ops (rle_direct_rows') the fewest bytes they go: of every way to cut the row into clear pixels, runs of a
+ colour and literals, the shortest (the first found of the shortest)."""
end = len(row)
while end and row[end - 1] == 0:
end -= 1
- dp = [10**9] * (end + 1)
- choice = [None] * end
- dp[end] = 0
- for i in range(end - 1, -1, -1):
- v = int(row[i])
- best = (10**9, None)
- if v == 0:
+ size = [10 ** 9] * (end + 1) # the bytes from x on
+ size[end] = 0
+ op = [None] * end # and the op at x for them: (0 clear, 1 run, 2 literals; pixels)
+ for x in range(end - 1, -1, -1):
+ v = row[x]
+ best = (10 ** 9, None)
+ kinds = ((0, 64, 1),) if v == 0 else ((1, 64, 2), (2, 128, None))
+ for kind, most, cost in kinds:
n = 0
- while n < 64 and i + n < end and row[i + n] == 0:
+ while n < most and x + n < end and (row[x + n] != 0 if kind == 2 else row[x + n] == v):
n += 1
- q = 1 + dp[i + n]
- if q < best[0]:
- best = (q, (0, n))
- else:
- n = 0
- while n < 64 and i + n < end and row[i + n] == v:
- n += 1
- q = 2 + dp[i + n]
- if q < best[0]:
- best = (q, (1, n))
- n = 0
- while n < 128 and i + n < end and row[i + n] != 0:
- n += 1
- q = 1 + ((n + 1) // 2 if nib else n) + dp[i + n]
- if q < best[0]:
- best = (q, (2, n))
- dp[i], choice[i] = best
+ total = (cost or 1 + ((n + 1) // 2 if nib else n)) + size[x + n]
+ if total < best[0]:
+ best = (total, (kind, n))
+ size[x], op[x] = best
out = bytearray()
- i = 0
- while i < end:
- kind, n = choice[i]
+ x = 0
+ while x < end:
+ kind, n = op[x]
if kind == 0:
out.append(n - 1)
elif kind == 1:
- out += bytes((0x40 + n - 1, int(row[i])))
+ out += bytes((0x40 + n - 1, int(row[x])))
else:
out.append(0x80 + n - 1)
+ lits = [int(c) for c in row[x:x + n]]
if nib:
- for j in range(0, n, 2):
- out.append(int(row[i + j]) | (int(row[i + j + 1]) << 4 if j + 1 < n else 0))
+ out += bytes(lits[i] | (lits[i + 1] << 4 if i + 1 < n else 0) for i in range(0, n, 2))
else:
- out += bytes(row[i:i + n])
- i += n
+ out += bytes(lits)
+ x += n
return bytes(out)
+
def rle_direct_rows(idx):
"""rle_direct_parts' (format flags, local palette, ops of each row)."""
flat = idx.ravel()
@@ -463,7 +453,7 @@ def rle_direct_rows(idx):
flags |= 8
colors = bytes([len(uniq)] + uniq)
if all(len(r) < ROW8_MAX for r in rows):
- rows = [_celeste_optimal_direct_row(idx[y], nib) for y in range(h)]
+ rows = [shortest_row(idx[y], nib) for y in range(h)] # (shorter still: as short as they go)
flags |= 4
return flags, colors, rows
@@ -1038,25 +1028,22 @@ BLOB_BYTES = {}
ROOM_POOL_FULL = 19092
-
-# CELESTE_DECAL_ZIGZAG_XY_V1: same-length, reversible decal x/y preconditioner.
-# Every room uses this transform; level.c undoes it once after room loading.
-def _cel_decals_zigzag_room(blob):
+def zigzag_decals(blob):
+ """The room's blob with its decals' x and y (in columns) each as its difference from the one before, zigzagged
+ (0, -1, 1, -2 as 0, 1, 2, 3): smaller packed. level.c room_load turns them back."""
out = bytearray(blob)
- for part in (3, 4): # RB_DFG, RB_DBG
- start = struct.unpack_from('<H', blob, 2 * part)[0]
- n = struct.unpack_from('<H', blob, start)[0]
- nt = blob[start + 2]
- xat = start + 4 + 2 * nt
- for at in (xat, xat + 2 * n):
- previous = 0
+ for part in (3, 4): # RB_DFG, RB_DBG
+ at = struct.unpack_from("<H", blob, 2 * part)[0]
+ n, textures = struct.unpack_from("<HB", blob, at)
+ at += 4 + 2 * textures
+ for col in (at, at + 2 * n):
+ last = 0
for i in range(n):
- value = struct.unpack_from('<H', blob, at + i * 2)[0]
- delta = (value - previous) & 65535
- signed = delta if delta < 32768 else delta - 65536
- z = ((signed << 1) ^ (signed >> 15)) & 65535
- struct.pack_into('<H', out, at + i * 2, z)
- previous = value
+ v = struct.unpack_from("<H", blob, col + 2 * i)[0]
+ d = (v - last) & 65535
+ d -= 65536 if d >= 32768 else 0
+ struct.pack_into("<H", out, col + 2 * i, (d << 1 ^ d >> 15) & 65535)
+ last = v
return bytes(out)
@@ -1097,7 +1084,7 @@ def room_blob(ch, r):
w.u16(x)
w.u16(y)
w.u16(v)
- # decals: u16 n, u8 textures, u16 ids[textures], (align 2), then in columns: s16 x[n], s16 y[n],
+ # decals: u16 n, u8 textures, u16 ids[textures], (align 2), then in columns: s16 x[n], s16 y[n] (zigzag_decals),
# u8 texture index | flip x << 6 | flip y << 7 [n]; in the map's order (it is the drawing order)
for layer in ("fgdecals", "bgdecals"):
offs.append(len(w))
@@ -1174,8 +1161,7 @@ def room_blob(ch, r):
name = ("fg", "bg", "extras", "dfg", "dbg", "spin", "need", "ents", "trigs")[k]
BLOB_PARTS[name] += b - a
BLOB_BYTES.setdefault(name, bytearray()).extend(w.b[a:b])
- # CELESTE_DECAL_ZIGZAG_XY_V1: same raw room size and same offsets.
- return _cel_decals_zigzag_room(bytes(w.b))
+ return zigzag_decals(w.b)
def room_pool_size(chapters):