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: compact scenery tables, walls as boxes, rooms drawn like the doodle

Mason Chenf78045ea98f4
diff --git a/games/championisland/src/node.c b/games/championisland/src/node.c
index 4161e7bc..4fcc91f0 100644
--- a/games/championisland/src/node.c
+++ b/games/championisland/src/node.c
@@ -241,6 +241,7 @@ NodeId node_new_sym_frame(uint16_t sym, int frame) {
static void instantiate_children(NodeId n) {
Clip c;
if (!clip_get(nodes[n].sym, &c)) return;
+ bool map_kids = c.T != NONE16 && comp_has(c.T, C_map);
const uint8_t *p = c.slots;
NodeId last = 0;
for (unsigned s = 0; s < c.nslots; s++) {
@@ -256,7 +257,8 @@ static void instantiate_children(NodeId n) {
if (k.start <= nodes[n].frame) { cur = k; has_cur = true; cur_idx = (uint16_t)i; }
}
p = q;
- if (!has || !needs_node(&first_present)) continue;
+ /* a map's children are all nodes: the map sorts them by depth */
+ if (!has || !(needs_node(&first_present) || map_kids)) continue;
if (only_current && (!has_cur || (cur.flags & K_ABSENT))) continue;
if (only_current) first_present = cur;
NodeId ch = new_child_for(&first_present);
@@ -890,6 +892,7 @@ void node_stream(NodeId n, float x0, float y0, float x1, float y1) {
}
const uint8_t *q = c.slots;
int hi = 0;
+ bool map_kids = c.T != NONE16 && comp_has(c.T, C_map);
for (unsigned s = 0; s < c.nslots; s++) {
Key k;
uint16_t idx;
@@ -898,7 +901,7 @@ void node_stream(NodeId n, float x0, float y0, float x1, float y1) {
NodeId got = hi < nh && nodes[have[hi]].slot == s ? have[hi] : 0;
bool present = idx != NONE16 && !(k.flags & K_ABSENT);
float bx, by, bw, bh;
- bool near = present && needs_node(&k) && key_bounds(&k, &bx, &by, &bw, &bh) && bx < x1 && by < y1 && bx + bw > x0 && by + bh > y0;
+ bool near = present && (needs_node(&k) || map_kids) && key_bounds(&k, &bx, &by, &bw, &bh) && bx < x1 && by < y1 && bx + bw > x0 && by + bh > y0;
if (near && !got) {
if (nh >= HAVE_MAX) continue;
NodeId nc = new_child_for(&k);
diff --git a/games/championisland/src/spr.c b/games/championisland/src/spr.c
index ec3e4709..9052984f 100644
--- a/games/championisland/src/spr.c
+++ b/games/championisland/src/spr.c
@@ -226,7 +226,8 @@ static bool add_from_decoder(uint16_t sp, const Sprite *s, bool force) {
top -= (bound + 3) & ~3u; /* give back what the runs did not need */
if (!r.ok) return false;
top += (used + 3) & ~3u;
- ent[nent++] = (Entry){sp, 0, (uint32_t)(p - mem), used, tick};
+ /* sprites decoded on the way (bank neighbours) count as older than any in use */
+ ent[nent++] = (Entry){sp, 0, (uint32_t)(p - mem), used, force || !tick ? tick : tick - 1};
return true;
}
diff --git a/games/championisland/src/world.c b/games/championisland/src/world.c
index bff317ab..fec3c0a9 100644
--- a/games/championisland/src/world.c
+++ b/games/championisland/src/world.c
@@ -20,8 +20,8 @@
/* ---------------------------------------------------------------- the world table (pack.py) */
typedef struct {
- const uint8_t *cells, *list, *stoffs, *regions, *lut;
- uint16_t map_sym, nstatic, nregions, sheet, gw, gh, cell;
+ const uint8_t *cells, *list, *wcells, *wlist, *stoffs, *stbase, *walls, *regions, *lut;
+ uint16_t map_sym, nstatic, nwalls, nregions, sheet, cell, gw, gh;
int16_t gx0, gy0;
} WorldData;
static WorldData wd;
@@ -36,29 +36,28 @@ static bool world_load(uint16_t map_sym) {
if (rd16(w) != map_sym) continue;
wd.map_sym = map_sym;
wd.nstatic = rd16(w + 2);
- wd.nregions = rd16(w + 4);
- wd.sheet = rd16(w + 6);
- wd.gx0 = rds16(w + 8);
- wd.gy0 = rds16(w + 10);
- wd.gw = rd16(w + 12);
- wd.gh = rd16(w + 14);
- wd.cell = rd16(w + 16);
- wd.cells = ci_data + rd32(w + 20);
- wd.list = ci_data + rd32(w + 24);
- wd.stoffs = ci_data + rd32(w + 28);
- wd.regions = ci_data + rd32(w + 32);
- wd.lut = ci_data + rd32(w + 36);
+ wd.nwalls = rd16(w + 4);
+ wd.nregions = rd16(w + 6);
+ wd.sheet = rd16(w + 8);
+ wd.cell = rd16(w + 10);
+ wd.gx0 = rds16(w + 12);
+ wd.gy0 = rds16(w + 14);
+ wd.gw = rd16(w + 16);
+ wd.gh = rd16(w + 18);
+ const uint8_t **ptrs[] = {&wd.cells, &wd.list, &wd.wcells, &wd.wlist, &wd.stoffs, &wd.stbase, &wd.walls, &wd.regions, &wd.lut};
+ for (int k = 0; k < 9; k++) *ptrs[k] = ci_data + rd32(w + 20 + 4 * k);
return true;
}
return false;
}
-/* a static: f32 key, i16 box x0 y0 x1 y1, u8 draws, u8 flags (1: solid), [i16 solid box], draws */
+/* a static: f32 key, i16 x0 y0, u16 w h (its box), u8 draws, u8 flags (1: solid), [i16 solid box], draws */
typedef struct { float key; int16_t x0, y0, x1, y1; uint8_t ndraws, flags; int16_t c[4]; const uint8_t *draws; } Static;
static void static_get(unsigned i, Static *s) {
- const uint8_t *p = ci_data + rd32(wd.stoffs + 4 * i);
+ const uint8_t *p = wd.stbase + 4u * rd16(wd.stoffs + 2 * i);
s->key = rdf(p);
- s->x0 = rds16(p + 4); s->y0 = rds16(p + 6); s->x1 = rds16(p + 8); s->y1 = rds16(p + 10);
+ s->x0 = rds16(p + 4); s->y0 = rds16(p + 6);
+ s->x1 = (int16_t)(s->x0 + rd16(p + 8)); s->y1 = (int16_t)(s->y0 + rd16(p + 10));
s->ndraws = p[12];
s->flags = p[13];
p += 14;
@@ -69,9 +68,27 @@ static void static_get(unsigned i, Static *s) {
s->draws = p;
}
+/* a draw of a static: u16 sprite, u8 flags (0-2 BD_*, 8: alpha follows, 16: 16-bit offsets), offsets from the box, [alpha] */
+typedef struct { uint16_t sp; int16_t x, y; uint8_t flags, alpha; } Draw;
+static const uint8_t *draw_next(const uint8_t *p, const Static *s, Draw *d) {
+ d->sp = rd16(p);
+ uint8_t f = p[2];
+ p += 3;
+ if (f & 16) { d->x = (int16_t)(s->x0 + rd16(p)); d->y = (int16_t)(s->y0 + rd16(p + 2)); p += 4; }
+ else { d->x = (int16_t)(s->x0 + p[0]); d->y = (int16_t)(s->y0 + p[1]); p += 2; }
+ d->alpha = 255;
+ if (f & 8) d->alpha = *p++;
+ d->flags = f & 7;
+ return p;
+}
+
+static void wall_get(unsigned i, int16_t c[4]) {
+ for (int k = 0; k < 4; k++) c[k] = rds16(wd.walls + 8 * i + 2 * k);
+}
+
#define STATIC_BITS 2048
-/* marks the statics whose cells meet the world rect (ids are in draw order) */
-static int statics_in(float x0, float y0, float x1, float y1, uint8_t *bits, int *lo, int *hi) {
+/* marks the ids of a grid (statics or walls) whose cells meet the world rect */
+static int grid_in(const uint8_t *cells, const uint8_t *list, float x0, float y0, float x1, float y1, uint8_t *bits, int *lo, int *hi) {
int cx0 = (int)floorf((x0 - wd.gx0) / wd.cell), cy0 = (int)floorf((y0 - wd.gy0) / wd.cell);
int cx1 = (int)floorf((x1 - wd.gx0) / wd.cell), cy1 = (int)floorf((y1 - wd.gy0) / wd.cell);
if (cx0 < 0) cx0 = 0;
@@ -81,11 +98,12 @@ static int statics_in(float x0, float y0, float x1, float y1, uint8_t *bits, int
int n = 0;
*lo = STATIC_BITS;
*hi = -1;
+ if (!wd.cell) return 0;
for (int cy = cy0; cy <= cy1; cy++)
for (int cx = cx0; cx <= cx1; cx++) {
unsigned c = (unsigned)(cy * wd.gw + cx);
- for (uint32_t k = rd32(wd.cells + 4 * c); k < rd32(wd.cells + 4 * c + 4); k++) {
- int id = rd16(wd.list + 2 * k);
+ for (unsigned k = rd16(cells + 2 * c); k < rd16(cells + 2 * c + 2); k++) {
+ int id = rd16(list + 2 * k);
if (id >= STATIC_BITS || (bits[id >> 3] & (1 << (id & 7)))) continue;
bits[id >> 3] |= (uint8_t)(1 << (id & 7));
if (id < *lo) *lo = id;
@@ -96,10 +114,14 @@ static int statics_in(float x0, float y0, float x1, float y1, uint8_t *bits, int
return n;
}
+static int statics_in(float x0, float y0, float x1, float y1, uint8_t *bits, int *lo, int *hi) {
+ return grid_in(wd.cells, wd.list, x0, y0, x1, y1, bits, lo, hi);
+}
+
/* ---------------------------------------------------------------- scene state */
#define WATER 48
#define FRONT_MAX 40
-#define SOLID_MAX 36
+#define SOLID_MAX 40
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 */
@@ -132,9 +154,11 @@ static void paint(int x0, int y0, int w, int h) {
Static s;
static_get((unsigned)id, &s);
if (s.x1 <= x0 || s.y1 <= y0 || s.x0 >= x0 + w || s.y0 >= y0 + h) continue;
+ const uint8_t *p = s.draws;
for (int d = 0; d < s.ndraws; d++) {
- const uint8_t *p = s.draws + 8 * d;
- bg_draw(rd16(p), rds16(p + 2), rds16(p + 4), p[6], p[7]);
+ Draw dr;
+ p = draw_next(p, &s, &dr);
+ bg_draw(dr.sp, dr.x, dr.y, dr.flags, dr.alpha);
}
}
}
@@ -159,15 +183,16 @@ static float draw_key(NodeId c) {
static void draw_static_over(unsigned id, Mat m, uint8_t alpha) {
Static s;
static_get(id, &s);
+ const uint8_t *p = s.draws;
for (int d = 0; d < s.ndraws; d++) {
- const uint8_t *p = s.draws + 8 * d;
- if (p[7] != 255) continue; /* see-through parts (shadows) are already in the layer */
- uint16_t sp = rd16(p);
+ Draw dr;
+ p = draw_next(p, &s, &dr);
+ if (dr.alpha != 255) continue; /* see-through parts (shadows) are already in the layer */
Sprite si;
- sprite_info(sp, &si);
+ sprite_info(dr.sp, &si);
if (si.kind > 1) continue; /* big ground images never stand in front */
- int x = rds16(p + 2), y = rds16(p + 4);
- uint8_t f = p[6];
+ int x = dr.x, y = dr.y;
+ uint8_t f = dr.flags;
/* the drawn rectangle's corner, back to a matrix */
Mat dm;
if (f & BD_TRANSPOSE) {
@@ -177,7 +202,7 @@ static void draw_static_over(unsigned id, Mat m, uint8_t alpha) {
float a = f & BD_FLIPX ? -1.f : 1.f, dd = f & BD_FLIPY ? -1.f : 1.f;
dm = (Mat){a, 0, 0, dd, a < 0 ? x + si.w : x, dd < 0 ? y + si.h : y};
}
- gfx_sprite_ex(sp, mat_mul(m, dm), alpha, true);
+ gfx_sprite_ex(dr.sp, mat_mul(m, dm), alpha, true);
}
}
@@ -312,46 +337,61 @@ void overworld_teleport(const char *location) {
}
/* ---------------------------------------------------------------- physics near the player */
+/* the walls and solid statics near the player are bodies (ids: statics as is, walls + 0x8000) */
+static bool solid_box(uint16_t id, int16_t c[4]) {
+ if (id & 0x8000) { wall_get(id & 0x7FFF, c); return true; }
+ Static s;
+ static_get(id, &s);
+ memcpy(c, s.c, sizeof s.c);
+ return s.flags & 1;
+}
+
+static void solid_add(uint16_t id, int type) {
+ for (int i = 0; i < W->nsolid; i++) if (W->solid_id[i] == id) return;
+ if (W->nsolid >= SOLID_MAX) return;
+ int16_t c[4];
+ if (!solid_box(id, c)) return;
+ float hw = (c[2] - c[0]) * .5f, hh = (c[3] - c[1]) * .5f;
+ int b = phys_add(type, SH_BOX, c[0] + hw, c[1] + hh, 50, hw, hh, 50);
+ if (b < 0) return;
+ bodies[b].user = 0;
+ W->solid_id[W->nsolid] = id;
+ W->solid_body[W->nsolid++] = (int16_t)b;
+}
+
static void solids_update(void) {
if (!W->player) return;
float px, py;
ent_pos(W->player, &px, &py);
- if (W->solid_valid && fabsf(px - W->solid_x) < 32 && fabsf(py - W->solid_y) < 32) return;
+ if (W->solid_valid && fabsf(px - W->solid_x) < 16 && fabsf(py - W->solid_y) < 16) return;
W->solid_valid = true;
W->solid_x = px;
W->solid_y = py;
- const float R = 112;
+ const float R = 56;
/* drop the far ones */
for (int i = 0; i < W->nsolid; i++) {
- Static s;
- static_get(W->solid_id[i], &s);
- float cx = (s.c[0] + s.c[2]) * .5f, cy = (s.c[1] + s.c[3]) * .5f;
- if (fabsf(cx - px) > R + 48 + (s.c[2] - s.c[0]) * .5f || fabsf(cy - py) > R + 48 + (s.c[3] - s.c[1]) * .5f) {
+ int16_t c[4];
+ solid_box(W->solid_id[i], c);
+ if (c[2] < px - R - 32 || c[0] > px + R + 32 || c[3] < py - R - 32 || c[1] > py + R + 32) {
phys_remove(W->solid_body[i]);
W->solid_id[i] = W->solid_id[--W->nsolid];
W->solid_body[i] = W->solid_body[W->nsolid];
i--;
}
}
+ int type = phys_type("prop");
uint8_t bits[STATIC_BITS / 8];
- memset(bits, 0, sizeof bits);
int lo, hi;
- if (!statics_in(px - R, py - R, px + R, py + R, bits, &lo, &hi)) return;
- int type = phys_type("prop");
- for (int id = lo; id <= hi && W->nsolid < SOLID_MAX; id++) {
- if (!(bits[id >> 3] & (1 << (id & 7)))) continue;
- Static s;
- static_get((unsigned)id, &s);
- if (!(s.flags & 1) || s.c[2] <= px - R || s.c[0] >= px + R || s.c[3] <= py - R || s.c[1] >= py + R) continue;
- bool have = false;
- for (int i = 0; i < W->nsolid; i++) if (W->solid_id[i] == id) have = true;
- if (have) continue;
- float hw = (s.c[2] - s.c[0]) * .5f, hh = (s.c[3] - s.c[1]) * .5f;
- int b = phys_add(type, SH_BOX, s.c[0] + hw, s.c[1] + hh, 50, hw, hh, 50);
- if (b < 0) break;
- bodies[b].user = 0;
- W->solid_id[W->nsolid] = (uint16_t)id;
- W->solid_body[W->nsolid++] = (int16_t)b;
+ for (int pass = 0; pass < 2; pass++) {
+ memset(bits, 0, sizeof bits);
+ if (!grid_in(pass ? wd.wcells : wd.cells, pass ? wd.wlist : wd.list, px - R, py - R, px + R, py + R, bits, &lo, &hi)) continue;
+ for (int id = lo; id <= hi; id++) {
+ if (!(bits[id >> 3] & (1 << (id & 7)))) continue;
+ uint16_t sid = (uint16_t)(pass ? id | 0x8000 : id);
+ int16_t c[4];
+ if (!solid_box(sid, c) || c[2] <= px - R || c[0] >= px + R || c[3] <= py - R || c[1] >= py + R) continue;
+ solid_add(sid, type);
+ }
}
}
@@ -759,6 +799,7 @@ static void start_interior(void) {
ent_register_tree(root);
translate_tree(root);
W->map = ent_map;
+ if (W->map) world_load(nodes[W->map].sym);
for (int i = 0; i < ent_count(); i++) {
NodeId n = ent_at(i);
if (n && comp_has(nodes[n].T, C_overworldPlayer)) W->player = n;
@@ -782,6 +823,7 @@ static void tick_interior(void) {
sys_walk_idle();
sys_storage_sprites();
sys_sprite_dirs();
+ solids_update();
sys_physics();
sys_ground_height();
sys_portals();
diff --git a/games/championisland/tools/pack.py b/games/championisland/tools/pack.py
index fa443120..6b2f87d9 100644
--- a/games/championisland/tools/pack.py
+++ b/games/championisland/tools/pack.py
@@ -459,6 +459,48 @@ def tr_of(M, tr):
return out
+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)."""
+ L = LIB[lib]['syms']
+ gs = L.get(g.get('sym'), {}) if g.get('sym') else {}
+ T = gs.get('T') or {}
+ dro = (T.get('drawOrderOverride') or {}).get('drawOrder')
+ ground = 'tileBackground' in T or (dro is not None and dro <= -1000)
+ bake = g['t'] == 'b' or ground or (g['t'] == 'm' and set(T) <= BAKE_COMPS and content_static(lib, g['sym']))
+ if g['t'] in ('s', 'x') or g.get('tr', {}).get('nm'):
+ bake = False
+ if not bake:
+ d = dict(g)
+ d['tr'] = tr_of(Mg, g.get('tr', {}))
+ kids.append(d)
+ return order_i
+ draws = []
+ flatten_draws(lib, g['sym'], Mg, g.get('tr', {}).get('a', 1), 0, draws)
+ if walls_only and (draws or 'collidable' not in T):
+ d = dict(g)
+ d['tr'] = tr_of(Mg, g.get('tr', {}))
+ kids.append(d)
+ return order_i
+ coll = None
+ if 'collidable' in T and 'boundable' in T:
+ for bc in gs['frames'][0]:
+ bs = L.get(bc.get('sym'), {}) if bc.get('sym') else {}
+ if 'bounds' in (bs.get('T') or {}):
+ r = sym_bounds(lib, bc['sym'])
+ if r:
+ coll = aabb(mat_mul(Mg, matrix(bc.get('tr', {}))), r)
+ break
+ key = float(dro) if dro is not None else Mg[5]
+ statics.append({'key': key, 'order': order_i, 'draws': draws, 'coll': coll, 'sym': g.get('sym')})
+ return order_i + 1
+
+
+# the rooms: each frame of the interior library's root shows one room's map
+for c in [c for fr in LIB['interior']['syms']['mbb']['frames'] for c in fr if c.get('sym')]:
+ if 'map' in (LIB['interior']['syms'][c['sym']].get('T') or {}) and ('interior', c['sym']) not in WORLDS:
+ WORLDS.append(('interior', c['sym']))
+
for lib, mapsym in WORLDS:
L = LIB[lib]['syms']
m = L[mapsym]
@@ -468,42 +510,15 @@ for lib, mapsym in WORLDS:
for c in m['frames'][0]:
rs = L.get(c.get('sym'), {}) if c.get('sym') else {}
if 'region' not in (rs.get('T') or {}):
- kids.append(dict(c))
+ order_i = classify(lib, c, matrix(c.get('tr', {})), statics, kids, order_i, lib == 'interior')
continue
Mr = matrix(c['tr'])
nb = rs.get('nb')
regions.append((SYM_ID[(lib, c['sym'])], aabb(Mr, nb) if nb else (0, 0, 0, 0)))
world_drop.add((lib, c['sym']))
for g in rs['frames'][0]:
- Mg = mat_mul(Mr, matrix(g.get('tr', {})))
- gs = L.get(g.get('sym'), {}) if g.get('sym') else {}
- T = gs.get('T') or {}
- dro = (T.get('drawOrderOverride') or {}).get('drawOrder')
- ground = 'tileBackground' in T or (dro is not None and dro <= -1000)
- bake = g['t'] == 'b' or ground or (g['t'] == 'm' and set(T) <= BAKE_COMPS and content_static(lib, g['sym']))
- if g['t'] in ('s', 'x'):
- world_warn['shape/text in region'] += 1
- bake = False
- if not bake:
- d = dict(g)
- d['tr'] = tr_of(Mg, g.get('tr', {}))
- kids.append(d)
- continue
- draws = []
- flatten_draws(lib, g['sym'], Mg, g.get('tr', {}).get('a', 1), 0, draws)
- coll = None
- if 'collidable' in T and 'boundable' in T:
- for bc in gs['frames'][0]:
- bs = L.get(bc.get('sym'), {}) if bc.get('sym') else {}
- if 'bounds' in (bs.get('T') or {}):
- r = sym_bounds(lib, bc['sym'])
- if r:
- coll = aabb(mat_mul(Mg, matrix(bc.get('tr', {}))), r)
- break
- key = float(dro) if dro is not None else Mg[5]
- statics.append({'key': key, 'order': order_i, 'draws': draws, 'coll': coll, 'sym': g.get('sym')})
- order_i += 1
- # the map keeps its own children and gets the regions' live ones
+ order_i = classify(lib, g, mat_mul(Mr, matrix(g.get('tr', {}))), statics, kids, order_i)
+ # the map keeps its live children
for i, c in enumerate(kids):
c['i'] = i
m['frames'] = [kids]
@@ -1175,68 +1190,93 @@ def lut444(sheet):
world_offs = []
+LUT_OFF = {}
+
+
+def grid_of(boxes, gx0, gy0, gw, gh):
+ cells = [[] for _ in range(gw * gh)]
+ for i, b in enumerate(boxes):
+ for cy in range((b[1] - gy0) // WORLD_CELL, (b[3] - 1 - gy0) // WORLD_CELL + 1):
+ for cx in range((b[0] - gx0) // WORLD_CELL, (b[2] - 1 - gx0) // WORLD_CELL + 1):
+ cells[cy * gw + cx].append(i)
+ starts, lst = [], []
+ for c in cells:
+ starts.append(len(lst))
+ lst += c
+ starts.append(len(lst))
+ assert len(lst) < 65536
+ return (blob.add(b''.join(struct.pack('<H', v) for v in starts), 2, cat='world'),
+ blob.add(b''.join(struct.pack('<H', v) for v in lst), 2, cat='world'), len(lst))
+
+
for w in WORLD:
- sts = w['statics']
- recs = []
- boxes = []
+ recs, boxes, walls = [], [], []
sheets = Counter()
- for st in sts:
+ for st in w['statics']:
draws = []
- bx0 = by0 = 1e9
- bx1 = by1 = -1e9
+ coll = st['coll']
+ cb = (int(math.floor(coll[0])), int(math.floor(coll[1])), int(math.ceil(coll[2])), int(math.ceil(coll[3]))) if coll else None
for sp, M, a in st['draws']:
- sh, _, _, sw, shh = SPR[sp]['key']
- sheets[sh] += 1
fl = orientation(M)
if fl is None:
continue
- x0_, y0_, x1_, y1_ = aabb(M, (0, 0, sw, shh))
- x = int(math.floor(x0_ + 0.5))
- y = int(math.floor(y0_ + 0.5))
+ sh, _, _, sw, shh = SPR[sp]['key']
+ sheets[sh] += 1
+ x0_, y0_, _, _ = aabb(M, (0, 0, sw, shh))
dw, dh = (shh, sw) if fl & 4 else (sw, shh)
- draws.append(struct.pack('<HhhBB', sp, x, y, fl, int(round(min(1, a) * 255))))
- bx0, by0, bx1, by1 = min(bx0, x), min(by0, y), max(bx1, x + dw), max(by1, y + dh)
- coll = st['coll']
- if coll: # invisible walls are found by their solid box
- cx0, cy0, cx1, cy1 = (int(math.floor(coll[0])), int(math.floor(coll[1])), int(math.ceil(coll[2])), int(math.ceil(coll[3])))
- bx0, by0, bx1, by1 = min(bx0, cx0), min(by0, cy0), max(bx1, cx1), max(by1, cy1)
- if bx0 > bx1:
- bx0 = by0 = bx1 = by1 = 0
- rec = struct.pack('<fhhhhBB', st['key'], int(bx0), int(by0), int(bx1), int(by1), len(draws), 1 if coll else 0)
- if coll:
- rec += struct.pack('<hhhh', *(int(round(v)) for v in coll))
- rec += b''.join(draws)
- recs.append(rec)
- boxes.append((bx0, by0, bx1, by1) if draws or coll else None)
- gx0 = int(min(b[0] for b in boxes if b)) // WORLD_CELL * WORLD_CELL
- gy0 = int(min(b[1] for b in boxes if b)) // WORLD_CELL * WORLD_CELL
- gw = (int(max(b[2] for b in boxes if b)) - gx0) // WORLD_CELL + 1
- gh = (int(max(b[3] for b in boxes if b)) - gy0) // WORLD_CELL + 1
- cells = [[] for _ in range(gw * gh)]
- for i, b in enumerate(boxes):
- if not b:
+ draws.append((sp, int(math.floor(x0_ + 0.5)), int(math.floor(y0_ + 0.5)), dw, dh, fl, int(round(min(1, a) * 255))))
+ if not draws:
+ if cb:
+ walls.append(cb)
continue
- for cy in range((int(b[1]) - gy0) // WORLD_CELL, (int(b[3]) - 1 - gy0) // WORLD_CELL + 1):
- for cx in range((int(b[0]) - gx0) // WORLD_CELL, (int(b[2]) - 1 - gx0) // WORLD_CELL + 1):
- cells[cy * gw + cx].append(i)
- starts, lst = [], []
- for c in cells:
- starts.append(len(lst))
- lst += c
- starts.append(len(lst))
- off_cells = blob.add(b''.join(struct.pack('<I', v) for v in starts), cat='world')
- off_list = blob.add(b''.join(struct.pack('<H', v) for v in lst), cat='world')
- st_offs = [blob.add(r, 4, cat='world') for r in recs]
- off_st = blob.add(b''.join(struct.pack('<I', o) for o in st_offs), cat='world')
- off_reg = blob.add(b''.join(struct.pack('<Hhhhh', sym, *(int(round(v)) for v in r)) for sym, r in w['regions']), cat='world')
- sheet = sheets.most_common(1)[0][0]
+ bx0, by0 = min(d[1] for d in draws), min(d[2] for d in draws)
+ bx1, by1 = max(d[1] + d[3] for d in draws), max(d[2] + d[4] for d in draws)
+ if cb:
+ bx0, by0, bx1, by1 = min(bx0, cb[0]), min(by0, cb[1]), max(bx1, cb[2]), max(by1, cb[3])
+ rec = struct.pack('<fhhHHBB', st['key'], bx0, by0, bx1 - bx0, by1 - by0, len(draws), 1 if cb else 0)
+ if cb:
+ rec += struct.pack('<hhhh', *cb)
+ for sp, x, y, dw, dh, fl, al in draws:
+ dx, dy = x - bx0, y - by0
+ wide = dx > 255 or dy > 255
+ rec += struct.pack('<HB', sp, fl | (8 if al != 255 else 0) | (16 if wide else 0))
+ rec += struct.pack('<HH', dx, dy) if wide else struct.pack('<BB', dx, dy)
+ if al != 255:
+ rec += bytes([al])
+ while len(rec) % 4:
+ rec += b'\0'
+ recs.append(rec)
+ boxes.append((bx0, by0, bx1, by1))
+ allb = boxes + walls
+ if allb:
+ gx0 = min(b[0] for b in allb) // WORLD_CELL * WORLD_CELL
+ gy0 = min(b[1] for b in allb) // WORLD_CELL * WORLD_CELL
+ gw = (max(b[2] for b in allb) - gx0) // WORLD_CELL + 1
+ gh = (max(b[3] for b in allb) - gy0) // WORLD_CELL + 1
+ else:
+ gx0 = gy0 = 0
+ gw = gh = 1
+ off_cells, off_list, nrefs = grid_of(boxes, gx0, gy0, gw, gh)
+ off_wcells, off_wlist, nwrefs = grid_of(walls, gx0, gy0, gw, gh)
+ base = blob.add(b'', 4, cat='world')
+ rel = []
+ for r in recs:
+ rel.append((blob.add(r, 4, cat='world') - base) // 4)
+ assert not rel or max(rel) < 65536
+ off_stoffs = blob.add(b''.join(struct.pack('<H', v) for v in rel), 2, cat='world')
+ off_walls = blob.add(b''.join(struct.pack('<hhhh', *b) for b in walls), 2, cat='world')
+ off_reg = blob.add(b''.join(struct.pack('<Hhhhh', sym, *(int(round(v)) for v in r)) for sym, r in w['regions']), 2, cat='world')
+ sheet = sheets.most_common(1)[0][0] if sheets else SHEETS.index('overworld-sprite.png')
if len(sheets) > 1:
print('world %s: images from sheets %s' % (w['map'], dict(sheets)))
- off_lut = blob.add(lut444(sheet), cat='world')
- world_offs.append(blob.add(struct.pack('<HHHHhhHHHHIIIII', SYM_ID[(w['lib'], w['map'])], len(sts), len(w['regions']), sheet,
- gx0, gy0, gw, gh, WORLD_CELL, 0, off_cells, off_list, off_st, off_reg, off_lut)))
- print('world %s: %d statics, grid %dx%d, %d refs, %d KB' % (w['map'], len(sts), gw, gh, len(lst),
- (sum(len(r) for r in recs) + 2 * len(lst) + 4 * len(starts) + 4096) // 1024))
+ if sheet not in LUT_OFF:
+ LUT_OFF[sheet] = blob.add(lut444(sheet), cat='world')
+ world_offs.append(blob.add(struct.pack('<HHHHHHhhHHIIIIIIIII', SYM_ID[(w['lib'], w['map'])], len(recs), len(walls), len(w['regions']),
+ sheet, WORLD_CELL, gx0, gy0, gw, gh, off_cells, off_list, off_wcells, off_wlist,
+ off_stoffs, base, off_walls, off_reg, LUT_OFF[sheet])))
+ if w['lib'] == 'overworld' or os.environ.get('WORLD_DEBUG'):
+ print('world %s: %d statics, %d walls, grid %dx%d, %d+%d refs, %d KB' % (w['map'], len(recs), len(walls), gw, gh, nrefs, nwrefs,
+ (sum(len(r) for r in recs) + 2 * (nrefs + nwrefs + 2 * gw * gh + len(recs)) + 8 * len(walls)) // 1024))
off_worlds = blob.add(struct.pack('<I', len(world_offs)) + b''.join(struct.pack('<I', o) for o in world_offs))
# strings last (everything above may have added some)