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.

Champion Island: the ending's petals and glow, every sprite drawn, houses in the layer

Mason Chen273e03821a23
diff --git a/games/championisland/src/data.bin b/games/championisland/src/data.bin
index 5cba9328..3368a7ab 100644
Binary files a/games/championisland/src/data.bin and b/games/championisland/src/data.bin differ
diff --git a/games/championisland/src/gfx.c b/games/championisland/src/gfx.c
index dff8b7a0..65a04465 100644
--- a/games/championisland/src/gfx.c
+++ b/games/championisland/src/gfx.c
@@ -110,15 +110,20 @@ void gfx_rect(int x, int y, int w, int h, uint16_t c, uint8_t alpha) {
}
/* one pattern a frame: a small 4-bit image at n places (gfx_tiles) */
-static struct { const uint8_t *px; const uint16_t *pal; const uint8_t *al; const int16_t *pos; int n, w, h, ox, oy; } tl;
+static struct { const uint8_t *px, *pts; const uint16_t *pal; const uint8_t *al; const int16_t *pos; int n, w, h, ox, oy; } tl;
void gfx_tiles(const uint8_t *px, int w, int h, const uint16_t *pal, const uint8_t *al, const int16_t *pos, int n, int ox, int oy, uint8_t alpha) {
if (!alpha || n <= 0 || w <= 0 || h <= 0) return;
Item *it = add();
if (!it) return;
- tl.px = px; tl.pal = pal; tl.al = al; tl.pos = pos; tl.n = n; tl.w = w; tl.h = h; tl.ox = ox; tl.oy = oy;
+ tl.px = px; tl.pts = NULL; tl.pal = pal; tl.al = al; tl.pos = pos; tl.n = n; tl.w = w; tl.h = h; tl.ox = ox; tl.oy = oy;
*it = (Item){DI_TILES, alpha, 0, 0, 0, 0, VIEW_W, VIEW_H, (uint16_t)(uintptr_t)px, (uint16_t)((ox & 0xFF) | (oy & 0xFF) << 8)};
}
+void gfx_points(const uint8_t *pts, int w, int h, const uint16_t *pal, const uint8_t *al, const int16_t *pos, int n, int ox, int oy, uint8_t alpha) {
+ gfx_tiles(pts, w, h, pal, al, pos, n, ox, oy, alpha);
+ if (nitems && items[nitems - 1].kind == DI_TILES) { tl.px = NULL; tl.pts = pts; }
+}
+
void gfx_shape(const uint8_t *shape, Mat m, uint8_t alpha) {
if (!alpha || !shape[0]) return;
/* bounds from the points */
@@ -605,8 +610,19 @@ static void draw_stream(const Item *it, int y0, int y1, int by, unsigned ga) {
}
}
+/* A sprite the cache let go before its band (a frame needing more than the
+ * cache holds, such as a menu over the island): decoded now, unless a stream
+ * holds the decoder (then it comes next frame). */
+static const uint8_t *band_sprite(uint16_t sp) {
+ const uint8_t *r = spr_peek(sp);
+ if (r || streaming) return r;
+ r = spr_get(sp);
+ line_spr = NULL; /* the cache may have moved */
+ return r;
+}
+
static __attribute__((noinline)) void draw_sprite_item(const Item *it, int y0, int y1, int by, int rows, unsigned ga) {
- const uint8_t *r = spr_peek(it->ref);
+ const uint8_t *r = band_sprite(it->ref);
if (!r) return;
Sprite s;
sprite_info(it->ref, &s);
@@ -626,7 +642,7 @@ static __attribute__((noinline)) void draw_sprite_item(const Item *it, int y0, i
}
static __attribute__((noinline)) void draw_affine_item(const Item *it, int y0, int y1, int by, int rows, unsigned ga) {
- const uint8_t *r = spr_peek(it->color);
+ const uint8_t *r = band_sprite(it->color);
if (!r) return;
Sprite s;
sprite_info(it->color, &s);
@@ -737,6 +753,7 @@ static __attribute__((noinline)) void draw_shape_item(const Item *it, int y0, in
static __attribute__((noinline)) void draw_mask_item(const Item *it, int y0, int y1, int by) {
const uint8_t *m = spr_peek_mask(it->ref);
+ if (!m && !streaming) { m = spr_mask(it->ref); line_spr = NULL; }
if (!m || !bgmem) return;
const uint16_t *pal = pal565(bgsheet);
const uint8_t *al = palalpha(bgsheet);
@@ -777,6 +794,16 @@ static __attribute__((noinline)) void draw_tiles_item(int y0, int y1, int by, un
int tx = tl.ox + tl.pos[2 * i], ty = tl.oy + tl.pos[2 * i + 1];
int a = ty > y0 ? ty : y0, b = ty + tl.h < y1 ? ty + tl.h : y1;
if (a >= b || tx >= VIEW_W || tx + tl.w <= 0) continue;
+ if (tl.pts) { /* a few pixels: u16 count, then (x, y, colour) */
+ int np = rd16(tl.pts);
+ for (const uint8_t *q = tl.pts + 2; np--; q += 3) {
+ int x = tx + q[0], y = ty + q[1];
+ if (y < a || y >= b || x < 0 || x >= VIEW_W) continue;
+ uint16_t *d = band + (y - by) * VIEW_W + x;
+ *d = blend(tl.pal[q[2]], *d, (tl.al[q[2]] * ga + 16) >> 5);
+ }
+ continue;
+ }
int x0 = tx < 0 ? -tx : 0, x1 = tx + tl.w > VIEW_W ? VIEW_W - tx : tl.w;
for (int y = a; y < b; y++) {
const uint8_t *row = tl.px + (y - ty) * (tl.w >> 1);
@@ -866,6 +893,16 @@ void gfx_letterbox(uint16_t c) {
letter_color = c;
}
+/* sprites whose items all end above y: the cache may let them go first */
+static void release_done(int y) {
+ for (int i = 0; i < nitems; i++) {
+ const Item *it = &items[i];
+ if (it->y1 <= y - BAND || it->y1 > y) continue;
+ if (it->kind == DI_SPRITE || it->kind == DI_MASK || it->kind == DI_STREAM) spr_release(it->ref);
+ else if (it->kind == DI_AFFINE) spr_release(it->color);
+ }
+}
+
void gfx_end(void) {
if (letter_dirty) {
plat_fill(0, 0, SCREEN_W, VIEW_Y, letter_color);
@@ -893,7 +930,10 @@ void gfx_end(void) {
fprintf(stderr, "item %d kind %d a %d f %d box %d %d %d %d ref %u col %u\n", i, items[i].kind, items[i].alpha, items[i].flags,
items[i].x0, items[i].y0, items[i].x1, items[i].y1, items[i].ref, items[i].color);
#endif
- /* decode what the frame needs before drawing, so bands only read the cache */
+ /* decode what the frame needs before drawing, so bands only read the cache;
+ * a frame needing more than the cache holds (a menu over the island, a busy
+ * street) is decoded band by band instead, from the top, each sprite let go
+ * once its last band is drawn (release_done) */
streaming = NULL;
line_spr = NULL;
uint8_t seen[(SPRITE_COUNT + 7) / 8];
@@ -909,11 +949,16 @@ void gfx_end(void) {
need += (t.rle + 3) & ~3u;
seen[sp >> 3] |= (uint8_t)(1 << (sp & 7));
}
- spr_get(sp);
}
+ bool over = need + 1024 > spr_capacity();
#ifdef HOST
- if (getenv("CI_NEED")) fprintf(stderr, "need %u of %u\n", (unsigned)need, (unsigned)spr_capacity());
+ if (getenv("CI_NEED")) fprintf(stderr, "need %u of %u%s\n", (unsigned)need, (unsigned)spr_capacity(), over ? " (by band)" : "");
#endif
+ for (int i = 0; i < nitems && !over; i++) {
+ Item *it = &items[i];
+ if (it->kind == DI_SPRITE || it->kind == DI_AFFINE) spr_get(it->kind == DI_SPRITE ? it->ref : it->color);
+ else if (it->kind == DI_MASK) spr_mask(it->ref);
+ }
/* big sprites are cached too while everything fits; otherwise the first
* one keeps the decoder while the bands go down */
for (int i = 0; i < nitems; i++) {
@@ -922,15 +967,14 @@ void gfx_end(void) {
Sprite t;
sprite_info(it->ref, &t);
uint32_t sz = (t.rle + 3) & ~3u;
- if (need + sz + 1024 <= spr_capacity() && spr_get(it->ref)) {
+ if (!over && need + sz + 1024 <= spr_capacity() && spr_get(it->ref)) {
it->kind = DI_SPRITE;
need += sz;
} else if (!streaming) stream_begin(it);
}
- for (int i = 0; i < nitems; i++)
- if (items[i].kind == DI_MASK) spr_mask(items[i].ref);
- /* a sprite pushed out of the cache by a later one is drawn next frame */
- for (int i = 0; i < nitems; i++) {
+ /* with a stream holding the decoder nothing is decoded while the bands go
+ * down: what is not cached is drawn next frame */
+ for (int i = 0; i < nitems && (streaming || !over); i++) {
Item *it = &items[i];
if ((it->kind == DI_SPRITE || it->kind == DI_AFFINE || (it->kind == DI_STREAM && it != streaming)) &&
!spr_peek(it->kind == DI_AFFINE ? it->color : it->ref))
@@ -946,7 +990,7 @@ void gfx_end(void) {
const Item *it = &items[i];
if (it->y1 > by && it->y0 < by + rows) h = item_hash(h, it);
}
- if (!redraw_all && h == band_hash[by / BAND]) continue;
+ if (!redraw_all && h == band_hash[by / BAND]) { if (over) release_done(by + rows); continue; }
band_hash[by / BAND] = h;
band_bg(by, rows);
for (int i = 0; i < nitems; i++) {
@@ -954,6 +998,7 @@ void gfx_end(void) {
if (it->y1 > by && it->y0 < by + rows) draw_item(it, by, rows);
}
plat_push(0, VIEW_Y + by, VIEW_W, rows, band);
+ if (over) release_done(by + rows);
}
redraw_all = false;
spr_tick();
diff --git a/games/championisland/src/gfx.h b/games/championisland/src/gfx.h
index 8adec55a..edc521b9 100644
--- a/games/championisland/src/gfx.h
+++ b/games/championisland/src/gfx.h
@@ -19,6 +19,7 @@ void gfx_rect(int x, int y, int w, int h, uint16_t c, uint8_t alpha);
/* one small image at n places (pos: x, y pairs, moved by ox, oy): 4-bit pixels,
* index 0 clear, pal/al: RGB565 and alpha (0..32) of each index (the island's rain) */
void gfx_tiles(const uint8_t *px, int w, int h, const uint16_t *pal, const uint8_t *al, const int16_t *pos, int n, int ox, int oy, uint8_t alpha);
+void gfx_points(const uint8_t *pts, int w, int h, const uint16_t *pal, const uint8_t *al, const int16_t *pos, int n, int ox, int oy, uint8_t alpha); /* the same with pixels as (x, y, colour) */
/* text: font size in px of the doodle's layout, colour RGB565 */
void gfx_text(const char *s, Mat m, uint16_t color, uint8_t align, int16_t line_w, int16_t line_h, uint8_t alpha);
void gfx_text_k(const char *s, Mat m, uint16_t color, uint8_t align, int16_t line_w, int16_t line_h, uint8_t alpha, int scale);
diff --git a/games/championisland/src/main.c b/games/championisland/src/main.c
index b5661bf5..9e96369f 100644
--- a/games/championisland/src/main.c
+++ b/games/championisland/src/main.c
@@ -25,6 +25,13 @@ void ci_init(void) {
hud_init();
/* the doodle opens on its intro film the first time, then on the island */
#ifdef START_SCENE
+#ifdef START_TUTORIAL_DONE
+ store_set_bool("TUTORIAL_DONE", true); /* recordings of the island after the tutorial */
+ store_set_bool("intro_VIDEO_SEEN", true);
+#endif
+#ifdef START_ENDING
+ store_set_bool("outro_VIDEO_SEEN", true); /* the island after the ending */
+#endif
game_go(START_SCENE); /* test builds: make EXTRA='-DSTART_SCENE=\"pingpong:hard\"' */
#else
game_go(store_bool("intro_VIDEO_SEEN", false) ? "overworld" : "video:intro:skippable");
diff --git a/games/championisland/src/spr.c b/games/championisland/src/spr.c
index 9c8cf7c6..1eeb0150 100644
--- a/games/championisland/src/spr.c
+++ b/games/championisland/src/spr.c
@@ -239,8 +239,26 @@ static bool add_from_decoder(uint16_t sp, const Sprite *s, bool force) {
return true;
}
+/* sprites kept outside the cache by a scene (the ending's glow: small runs,
+ * but a whole screen to decode again each time the cache lets it go) */
+#define PINS 2
+static struct { uint16_t sp; const uint8_t *rle; } pins[PINS];
+void spr_pin(uint16_t sp, const uint8_t *rle) {
+ for (int i = 0; i < PINS; i++)
+ if (pins[i].rle && pins[i].sp == sp) pins[i].rle = NULL;
+ for (int i = 0; rle && i < PINS; i++)
+ if (!pins[i].rle) { pins[i].sp = sp; pins[i].rle = rle; return; }
+}
+static const uint8_t *pinned(uint16_t sp) {
+ for (int i = 0; i < PINS; i++)
+ if (pins[i].rle && pins[i].sp == sp) return pins[i].rle;
+ return NULL;
+}
+
const uint8_t *spr_get(uint16_t sp) {
if (sp >= SPRITE_COUNT || !mem) return NULL;
+ const uint8_t *pn = pinned(sp);
+ if (pn) return pn;
unsigned f = find(sp);
if (f) {
Entry *e = &ent[f - 1];
@@ -282,6 +300,8 @@ const uint8_t *spr_get(uint16_t sp) {
/* the cached runs if the sprite is in the cache now (never decodes) */
const uint8_t *spr_peek(uint16_t sp) {
+ const uint8_t *pn = pinned(sp);
+ if (pn) return pn;
unsigned f = sp < SPRITE_COUNT && mem ? find(sp) : 0;
if (!f) return NULL;
Entry *e = &ent[f - 1];
@@ -379,5 +399,13 @@ const uint8_t *spr_peek_mask(uint16_t sp) {
return f ? mem + 4u * ent[f - 1].off4 : NULL;
}
+/* a sprite this frame needs no more (its last band is drawn): the first to go */
+void spr_release(uint16_t sp) {
+ unsigned f = sp < SPRITE_COUNT && mem ? find(sp) : 0;
+ if (f) ent[f - 1].used = (uint16_t)(tick - 2);
+ f = sp < SPRITE_COUNT && mem ? find((uint16_t)(sp | MASK)) : 0;
+ if (f) ent[f - 1].used = (uint16_t)(tick - 2);
+}
+
uint32_t spr_cache_used(void) { return top; }
uint32_t spr_capacity(void) { return cache_bytes; }
diff --git a/games/championisland/src/spr.h b/games/championisland/src/spr.h
index 77eee072..a549e541 100644
--- a/games/championisland/src/spr.h
+++ b/games/championisland/src/spr.h
@@ -16,6 +16,8 @@ const uint8_t *spr_peek(uint16_t sprite); /* only if cached */
const uint8_t *spr_stream(uint16_t sprite); /* 14-byte stream record or NULL */
const uint8_t *spr_mask(uint16_t sprite); /* its opaque pixels as spans (decodes it if needed), or NULL */
const uint8_t *spr_peek_mask(uint16_t sprite);
+void spr_pin(uint16_t sprite, const uint8_t *rle);
+void spr_release(uint16_t sprite); /* not needed again this frame */ /* its runs kept elsewhere by the scene (NULL: no more) */
void spr_setup(uint8_t *mem, uint32_t size); /* where the cache lives (game.c's arena) */
void spr_reset(void);
uint8_t *z_scratch(uint32_t *size); /* the decoder's ring when it is idle */
diff --git a/games/championisland/src/world.c b/games/championisland/src/world.c
index 70200048..8c23d823 100644
--- a/games/championisland/src/world.c
+++ b/games/championisland/src/world.c
@@ -127,6 +127,7 @@ static int statics_in(float x0, float y0, float x1, float y1, uint8_t *bits, int
#define FRONT_MAX 40
#define SOLID_MAX 40
#define RAIN_MAX 48
+#define PETAL_MAX 80
typedef struct {
uint8_t water[WATER * WATER]; /* the sea's tile now (w9a: frames 0-15, 16-31) */
int water_look; /* which, -1 before the first */
@@ -135,6 +136,9 @@ typedef struct {
uint16_t rain_cond; /* its conditionallyVisible */
int16_t rain_pos[2 * RAIN_MAX]; /* its tiles */
int rain_n;
+ bool ending; /* the outro was seen: the petals and the glow */
+ int16_t petal_pos[2 * PETAL_MAX];
+ int npetal;
uint32_t tick;
int roll; /* Bi.Jaa: ticks left in a roll */
float stream_x, stream_y; /* camera when the live children were last streamed */
@@ -684,13 +688,52 @@ static bool rain_slot(unsigned slot, const SlotInfo *si, void *ctx) {
W->rain_n++;
return true;
}
+static uint16_t glow_sprite(void) {
+ SymInfo glow;
+ sym_info(S_overworld_nAa, &glow);
+ return glow.type == SYM_BITMAP ? (uint16_t)glow.v : NONE16;
+}
+
+/* a pattern set of the data (pack.py: rain, petals) */
+static const uint8_t *pattern(unsigned i) {
+ if (!HDR(H_TILES)) return NULL;
+ const uint8_t *blk = ci_data + HDR(H_TILES);
+ return i < rd16(blk) ? blk + rd32(blk + 4 + 4 * i) : NULL;
+}
+
+/* oAa, mAa: after the ending (outro seen), a glow over the top of the view and
+ * the petals falling over the island (repeatable tiles of 144 x 62, the 20
+ * looks of a petal taking turns every 3 ticks) */
+static bool petal_slot(unsigned slot, const SlotInfo *si, void *ctx) {
+ if (W->npetal >= PETAL_MAX) return false;
+ W->petal_pos[2 * W->npetal] = (int16_t)floorf(si->x + .5f);
+ W->petal_pos[2 * W->npetal + 1] = (int16_t)floorf(si->y + .5f);
+ W->npetal++;
+ return true;
+}
+static void draw_ending(float mx, float my) {
+ uint16_t gs = glow_sprite();
+ if (gs != NONE16) gfx_sprite(gs, MAT_ID, 255);
+ const uint8_t *t = pattern(1);
+ if (!t || !W->npetal) return;
+ unsigned looks = rd16(t + 2), look = W->tick / 3 % (looks ? looks : 1);
+ const uint8_t *offs = t + 56;
+ int ox = (int)floorf((fmodf(fmodf(mx, 432) + 432, 432) - 432) / 3 + .5f);
+ int oy = (int)floorf((fmodf(fmodf(my, 186) + 186, 186) - 186) / 3 + .5f);
+ gfx_points(t + rd32(offs + 4 * look), rd16(t + 4), rd16(t + 6), (const uint16_t *)(t + 8), t + 40, W->petal_pos, W->npetal,
+ ox, oy, 255);
+}
+
static void draw_over_overworld(void) {
- if (!W || !W->map || W->rain < .01f || !W->rain_n || !HDR(H_TILES)) return;
+ if (!W || !W->map) return;
+ float mx = nodes[W->map].x, my = nodes[W->map].y;
+ if (W->ending) draw_ending(mx, my);
+ if (W->rain < .01f || !W->rain_n) return;
if (W->rain_cond != NONE16 && str(W->rain_cond)[0] && !cond_eval(W->rain_cond)) return;
- const uint8_t *t = ci_data + HDR(H_TILES);
- int w = rd16(t), h = rd16(t + 2), looks = rd16(t + 4);
+ const uint8_t *t = pattern(0);
+ if (!t) return;
+ int looks = rd16(t + 2), w = rd16(t + 4), h = rd16(t + 6);
unsigned look = (W->tick / 3) % 2 % (unsigned)(looks ? looks : 1);
- float mx = nodes[W->map].x, my = nodes[W->map].y;
int ox = (int)floorf((fmodf(fmodf(mx, 144) + 144, 144) - 144) / 3 + .5f);
int oy = (int)floorf((fmodf(fmodf(my, 144) + 144, 144) - 144) / 3 + .5f);
gfx_tiles(t + 56 + look * (unsigned)(w * h / 2), w, h, (const uint16_t *)(t + 8), t + 40, W->rain_pos, W->rain_n, ox, oy,
@@ -749,6 +792,7 @@ static NodeId child_of_sym(NodeId n, uint16_t sym) {
}
static void start_overworld(void) {
+ bool ending = store_bool("outro_VIDEO_SEEN", false);
W = scene_state(sizeof(World));
W->region_rain = -1;
world_load(S_overworld_jqa);
@@ -765,10 +809,16 @@ static void start_overworld(void) {
W->rain_cond = rain && nodes[rain].T != NONE16 ? comp_str(nodes[rain].T, C_conditionallyVisible, F_condition) : NONE16;
if (rain) { node_remove(rain); node_free(rain); }
clip_each_slot(S_overworld_bca, 0, rain_slot, NULL);
- NodeId sky = child_of_sym(root, S_overworld_mAa);
- if (sky) node_free(sky);
+ /* the ending's glow and petals are drawn by code too (draw_over) */
+ node_free(child_of_sym(root, S_overworld_oAa));
+ node_free(child_of_sym(root, S_overworld_mAa));
+ W->ending = ending;
+ if (W->ending) clip_each_slot(S_overworld_mAa, 0, petal_slot, NULL);
if (!W->map) return;
mem_layout(VIEW_W + 8, VIEW_H + 8, SHEET_OVERWORLD, paint);
+ /* after the ending, the glow is read from its runs in the data (pack.py) */
+ const uint8_t *glow = pattern(2);
+ if (ending && glow && rd16(glow + 2) == glow_sprite()) spr_pin(glow_sprite(), glow + 4);
bg_blend_lut(wd.lut);
ent_register_tree(root);
node_stream_hook = streamed_in;
@@ -795,6 +845,7 @@ static void start_overworld(void) {
}
static void end_overworld(void) {
+ spr_pin(glow_sprite(), NULL);
node_stream_hook = NULL;
node_draw_hook = NULL;
node_draw_hook_id = 0;
diff --git a/games/championisland/tools/pack.py b/games/championisland/tools/pack.py
index ae24592f..780186e7 100644
--- a/games/championisland/tools/pack.py
+++ b/games/championisland/tools/pack.py
@@ -459,6 +459,71 @@ def tr_of(M, tr):
return out
+# property names the game code reads (b.title, b.Jw.zK...): Closure renamed most
+# of Animate's instance properties, so only those the code uses are kept
+_code = KITSUNE.split('\n')
+USED_PROPS = set(re.findall(r'\.([A-Za-z_$][\w$]*)', '\n'.join(_code[2780:4830] + _code[24600:]))) - {'shape'}
+
+
+def child_name(c):
+ """The names code can find a child by: its instance name and/or the property
+ it is kept in, as "name|property" when both."""
+ nm, pk = c['tr'].get('nm'), c.get('pk')
+ if nm == 'hitArea': # touch areas: the calculator has keys
+ nm = None
+ if pk not in USED_PROPS or pk == nm or pk == 'hitArea':
+ pk = None
+ if nm and pk:
+ return nm + '|' + pk
+ return nm or pk
+
+
+# live children whose first pictures never change (a house under its door):
+# those pictures are baked into the layer, and the symbol loses them
+LEAD_COMPS = {'boundable', 'collidable', 'jumpToFrameOnTrigger', 'trigger', 'scenePortal', 'untraversable'}
+LEAD_BAKED = {}
+
+
+def _world_only_uses():
+ """(lib, sym) used as a child only by maps and their regions."""
+ ok = {}
+ for lib_, _ in LIBS:
+ L = LIB[lib_]['syms']
+ for k, s_ in L.items():
+ T = s_.get('T') or {}
+ is_world = 'map' in T or 'region' in T
+ for fr in s_.get('frames') or []:
+ for c in fr:
+ if c.get('sym'):
+ key = (lib_, c['sym'])
+ ok[key] = ok.get(key, True) and is_world
+ return ok
+
+
+WORLD_ONLY = _world_only_uses()
+
+
+def static_lead(lib, g):
+ """How many of a live child's first children are pictures that never change."""
+ L = LIB[lib]['syms']
+ s_ = L.get(g.get('sym'), {})
+ T = s_.get('T') or {}
+ tr = g.get('tr', {})
+ if not T or not set(T) <= LEAD_COMPS or not WORLD_ONLY.get((lib, g['sym'])) or tr.get('a', 1) < 1 or tr.get('v', 1) == 0:
+ return 0
+ frames = s_.get('frames') or []
+ if not frames:
+ return 0
+ n = 0
+ for i, c in enumerate(frames[0]):
+ if not c.get('sym') or c['t'] not in ('b', 'm') or child_name(c) or not content_static(lib, c['sym']):
+ break
+ if c['tr'].get('v', 1) == 0 or any(i >= len(f) or f[i] != c for f in frames):
+ break
+ n += 1
+ return n
+
+
def classify(lib, g, Mg, statics, kids, order_i, walls_only=False):
"""A child of a map (or of one of its regions) placed by Mg: baked scenery, or kept live.
In rooms only invisible walls are taken out (the rest is small and drawn from the clip)."""
@@ -474,6 +539,17 @@ def classify(lib, g, Mg, statics, kids, order_i, walls_only=False):
d = dict(g)
d['tr'] = tr_of(Mg, g.get('tr', {}))
kids.append(d)
+ n = static_lead(lib, g) if not walls_only else 0
+ if n:
+ # the pictures under a door (the house itself) never change: they go into
+ # the layer at the door's depth, and the door keeps the rest
+ draws = []
+ for c in gs['frames'][0][:n]:
+ flatten_draws(lib, c['sym'], mat_mul(Mg, matrix(c.get('tr', {}))), c.get('tr', {}).get('a', 1), 0, draws)
+ key = float(dro) if dro is not None else Mg[5]
+ statics.append({'key': key, 'order': order_i, 'draws': draws, 'coll': None, 'sym': g.get('sym')})
+ LEAD_BAKED[(lib, g['sym'])] = n
+ return order_i + 1
return order_i
draws = []
flatten_draws(lib, g['sym'], Mg, g.get('tr', {}).get('a', 1), 0, draws)
@@ -715,23 +791,6 @@ CK_SYM, CK_SHAPE, CK_TEXT = 0, 1, 2
anon_shapes = {}
-# property names the game code reads (b.title, b.Jw.zK...): Closure renamed most
-# of Animate's instance properties, so only those the code uses are kept
-_code = KITSUNE.split('\n')
-USED_PROPS = set(re.findall(r'\.([A-Za-z_$][\w$]*)', '\n'.join(_code[2780:4830] + _code[24600:]))) - {'shape'}
-
-
-def child_name(c):
- """The names code can find a child by: its instance name and/or the property
- it is kept in, as "name|property" when both."""
- nm, pk = c['tr'].get('nm'), c.get('pk')
- if nm == 'hitArea': # touch areas: the calculator has keys
- nm = None
- if pk not in USED_PROPS or pk == nm or pk == 'hitArea':
- pk = None
- if nm and pk:
- return nm + '|' + pk
- return nm or pk
SLOT_CONFLICTS = []
@@ -933,6 +992,8 @@ for (lib, k) in SYMS:
sym_table.append((SYM_SHAPE, PAYLOADS.setdefault(enc_shape(s.get('g') or []), len(PAYLOADS))))
elif t == 'm':
frames = s['frames']
+ if (lib, k) in LEAD_BAKED:
+ frames = [f[LEAD_BAKED[(lib, k)]:] for f in frames]
nf = len(frames)
slots = defaultdict(lambda: [None] * nf)
for fi, fr in enumerate(frames):
@@ -1281,37 +1342,128 @@ for i in range(len(SHEETS)):
pal_bin += bytes(sheet_alpha[i])
off_pal = blob.add(bytes(pal_bin))
-# the island's rain (bca): its two looks (Yi shows u_a, then v_a) drawn as a
-# pattern over the view straight from here, 4 bits a pixel with their own 15
-# colours (u16 w, h, looks, colours; u16 rgb565[16]; u8 alpha 0..32 [16]; pixels)
+# Patterns drawn over the island straight from here, with their own colours
+# (u16 rgb565[16], u8 alpha 0..32 [16], index 0 clear):
+# - the rain (bca): its two looks (Yi shows u_a, then v_a), 4 bits a pixel;
+# - the petals after the ending (mAa): the 20 looks of a petal (Ue), each as
+# its few pixels (x, y, colour), where Ue shows it.
+# Block: u16 sets, u16 0, u32 set offsets; a set: u16 kind (0 pixels, 1 points),
+# u16 looks, u16 w, u16 h, colours, then the pixels, or u32 look offsets and per
+# look u16 points, then (x, y, colour) bytes.
+def pattern_colours(sh, vals):
+ cols = []
+ for v in vals:
+ if sheet_alpha[sh][v] and v not in cols:
+ cols.append(v)
+ assert len(cols) <= 15, cols
+ pal, al = [0] * 16, [0] * 16
+ for i, v in enumerate(cols):
+ r, g, b = pal_rgb[sh][v]
+ pal[i + 1] = (r >> 3) << 11 | (g >> 2) << 5 | (b >> 3)
+ al[i + 1] = (int(sheet_alpha[sh][v]) * 32 + 127) // 255
+ return cols, struct.pack('<16H', *pal) + bytes(al)
+
+
def tiles4(lib, names):
looks = [LIB[lib]['syms'][n]['sheet'] for n in names]
sh, _, _, w, h = looks[0]
assert all(l[0] == sh and l[3:] == (w, h) for l in looks) and w % 2 == 0
- cols, px = [], bytearray()
+ cols, colours = pattern_colours(sh, [v for _, x, y, _, _ in looks for v in sheet_px[sh][y:y + h, x:x + w].flatten().tolist()])
+ px = bytearray()
for _, x, y, _, _ in looks:
- a = sheet_px[sh][y:y + h, x:x + w]
- for row in a:
- nib = []
- for v in row.tolist():
- if sheet_alpha[sh][v] == 0:
- nib.append(0)
- continue
- if v not in cols:
- cols.append(v)
- nib.append(cols.index(v) + 1)
+ for row in sheet_px[sh][y:y + h, x:x + w]:
+ nib = [cols.index(v) + 1 if sheet_alpha[sh][v] else 0 for v in row.tolist()]
px += bytes(nib[i] | nib[i + 1] << 4 for i in range(0, w, 2))
- assert len(cols) <= 15, cols
- pal = [0] * 16
- al = [0] * 16
- for i, v in enumerate(cols):
- r, g, b = pal_rgb[sh][v]
- pal[i + 1] = (r >> 3) << 11 | (g >> 2) << 5 | (b >> 3)
- al[i + 1] = (int(sheet_alpha[sh][v]) * 32 + 127) // 255
- return struct.pack('<HHHH', w, h, len(looks), len(cols)) + struct.pack('<16H', *pal) + bytes(al) + bytes(px)
+ return struct.pack('<HHHH', 0, len(looks), w, h) + colours + bytes(px)
-off_tiles = blob.add(tiles4('overworld', ['u_a', 'v_a']), 2)
+def points(lib, clip):
+ """Each frame of `clip` (one bitmap child) as its visible pixels, where it stands."""
+ L = LIB[lib]['syms']
+ frames = L[clip]['frames']
+ step = 3
+ shots = []
+ for f in range(0, len(frames), step):
+ c = frames[f][0]
+ sh, x, y, w, h = L[c['sym']]['sheet']
+ dx, dy = int(round(c['tr'].get('x', 0))), int(round(c['tr'].get('y', 0)))
+ shots.append((sh, x, y, w, h, dx, dy))
+ sh = shots[0][0]
+ cols, colours = pattern_colours(sh, [v for s_, x, y, w, h, _, _ in shots for v in sheet_px[s_][y:y + h, x:x + w].flatten().tolist()])
+ bodies = []
+ for s_, x, y, w, h, dx, dy in shots:
+ pts = [(dx + i, dy + j, cols.index(v) + 1) for j, row in enumerate(sheet_px[s_][y:y + h, x:x + w].tolist())
+ for i, v in enumerate(row) if sheet_alpha[s_][v]]
+ assert all(0 <= a < 256 and 0 <= b < 256 for a, b, _ in pts)
+ bodies.append(struct.pack('<H', len(pts)) + b''.join(bytes(p) for p in pts))
+ mw = max(dx + w for _, _, _, w, h, dx, dy in shots)
+ mh = max(dy + h for _, _, _, w, h, dx, dy in shots)
+ head = struct.pack('<HHHH', 1, len(bodies), mw, mh) + colours
+ offs, at = [], len(head) + 4 * len(bodies)
+ for b_ in bodies:
+ offs.append(at)
+ at += len(b_)
+ return head + b''.join(struct.pack('<I', o) for o in offs) + b''.join(bodies)
+
+
+def rle_bytes(sp):
+ """The cache's run-length form of a sprite (spr.c: rle_row), to be read from here."""
+ sh, x, y, w, h = SPR[sp]['key']
+ al = sheet_alpha[sh]
+ rows = []
+ for row in sprite_pixels(sp).tolist():
+ out, n, xx = bytearray(), 0, 0
+ while xx < w:
+ skip = 0
+ while xx < w and not al[row[xx]]:
+ xx += 1
+ skip += 1
+ if xx >= w:
+ break
+ while skip > 255:
+ out += bytes([255, 0])
+ skip -= 255
+ n += 1
+ run = 1
+ while xx + run < w and run < 128 and row[xx + run] == row[xx]:
+ run += 1
+ if run >= 3:
+ out += bytes([skip, 0x80 | (run - 1), row[xx]])
+ xx += run
+ else:
+ ln = 0
+ while xx + ln < w and ln < 127 and al[row[xx + ln]] and not (
+ xx + ln + 2 < w and row[xx + ln] == row[xx + ln + 1] and row[xx + ln] == row[xx + ln + 2]):
+ ln += 1
+ ln = max(ln, 1)
+ out += bytes([skip, ln]) + bytes(row[xx:xx + ln])
+ xx += ln
+ n += 1
+ rows.append(struct.pack('<H', n) + out)
+ offs, at = [], 4 + 2 * h
+ for r in rows:
+ offs.append(at)
+ at += len(r)
+ data = struct.pack('<HH', w, h) + b''.join(struct.pack('<H', o) for o in offs) + b''.join(rows)
+ assert len(data) == rle_size(sp), (len(data), rle_size(sp))
+ return data
+
+
+# the ending's glow (nAa): a whole screen, but little once in runs
+_glow = sym_sprite[('overworld', 'nAa')]
+_sets = [tiles4('overworld', ['u_a', 'v_a']), points('overworld', 'Ue'), struct.pack('<HH', 2, _glow) + rle_bytes(_glow)]
+_blk = bytearray(struct.pack('<HH', len(_sets), 0))
+_at = 4 + 4 * len(_sets)
+for _s in _sets:
+ _at = (_at + 3) & ~3
+ _blk += struct.pack('<I', _at)
+ _at += len(_s)
+for _s in _sets:
+ while len(_blk) % 4:
+ _blk += b'\0'
+ _blk += _s
+off_tiles = blob.add(bytes(_blk), 4)
+print('patterns: %d bytes' % len(_blk))
pay_offs = [blob.add(p_, 2, cat='payload') for p_ in PAYLOADS]
off_payloads = blob.add(b''.join(struct.pack('<I', o) for o in pay_offs))