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: what a room loads, as in the game

Mason Chen4b64c929345d
diff --git a/games/celeste/src/berry.c b/games/celeste/src/berry.c
index e9d2dda3..141d7690 100644
--- a/games/celeste/src/berry.c
+++ b/games/celeste/src/berry.c
@@ -627,11 +627,20 @@ static void berry_new(const EData *d, bool golden, bool memorial) {
}
}
}
+/* Level.LoadLevel: a golden strawberry once the side is completed with the C-sides unlocked (or in cheat mode), in the
+ * room seen last or before any death; the memorial's winged one without a dash since the chapter's start */
bool berry_create(const EData *d) {
+ Session *s = &g_session;
switch (d->type) {
case ET_strawberry: berry_new(d, false, false); return true;
- case ET_goldenBerry: berry_new(d, true, false); return true;
- case ET_memorialTextController: berry_new(d, false, true); return true;
+ case ET_goldenBerry:
+ if ((g_save.cheat_mode || (save_unlocked_modes() >= 3 && (save_mode()->flags & MS_COMPLETED))) &&
+ (s->furthest_seen == s->level + 1 || !s->deaths))
+ berry_new(d, true, false);
+ return true;
+ case ET_memorialTextController:
+ if (!s->dashes && s->started_from_beginning) berry_new(d, false, true);
+ return true;
}
return false;
}
diff --git a/games/celeste/src/ch3.c b/games/celeste/src/ch3.c
index 7c89967f..9df3098f 100644
--- a/games/celeste/src/ch3.c
+++ b/games/celeste/src/ch3.c
@@ -2316,9 +2316,7 @@ static Ent *make_key(float x, float y, uint32_t id) {
return e;
}
static void new_key(const EData *d) {
- uint32_t id = level_entity_hash(d);
- if (level_do_not_load(id)) return;
- make_key(d->x, d->y, id);
+ make_key(d->x, d->y, level_entity_hash(d));
}
/* Level.LoadLevel: the session's keys follow the new player */
static void restore_keys(void) {
@@ -2456,7 +2454,6 @@ static const EntClass LOCK = {.name = "lockBlock", .size = sizeof(Lock), .update
.kind = KIND_SOLID};
static void new_lock(const EData *d) {
uint32_t id = level_entity_hash(d);
- if (level_do_not_load(id)) return;
Ent *e = ent_new(&LOCK, d->x, d->y);
if (!e) return;
ent_box(e, 32, 32, 0, 0);
diff --git a/games/celeste/src/ch5.c b/games/celeste/src/ch5.c
index 9c0e7897..76fab553 100644
--- a/games/celeste/src/ch5.c
+++ b/games/celeste/src/ch5.c
@@ -181,15 +181,16 @@ static void death_draw(V2 pos, uint16_t color, float ease) {
#define MAX_TORCH 100
typedef struct {
int16_t x, y;
- uint16_t eid, frames; /* frames: updates since the sprite started playing */
- uint8_t slot, room, lit, lit_bank; /* lit_bank: startLit, the "litTorch" sprite */
+ uint16_t bit, frames; /* bit: in Session.torches; frames: updates since the sprite started playing */
+ uint8_t slot, lit, lit_bank; /* lit_bank: startLit, the "litTorch" sprite */
uint8_t on_mode, tween; /* on_mode: the sprite plays "on" (lit when added), else "turnOn" first;
* tween: the light's tween, in updates (60: done) */
} Torch;
enum { OFF_torches = 0, END_torches = OFF_torches + (int)sizeof(Torch[MAX_TORCH]) };
#define torches (*(Torch(*)[MAX_TORCH])(void *)(g_chram[5] + OFF_torches))
static int ntorches;
-typedef struct { uint8_t slot; } TorchMgr;
+typedef struct { uint8_t slot, n; } TorchMgr; /* n: the room's torches unlit at first, so far */
+#define TORCH_BITS (8 * (int)sizeof g_session.torches_lit)
static uint16_t torch_tex[2][9];
static void torch_update(Ent *e) {
@@ -208,8 +209,7 @@ static void torch_update(Ent *e) {
t->lit = 1;
t->frames = 0;
t->tween = 0;
- char flag[40];
- level_set_flag(id_flag(flag, "torch_", t->room, t->eid), true);
+ if (t->bit < TORCH_BITS) g_session.torches_lit[t->bit >> 3] |= (uint8_t)(1 << (t->bit & 7)); /* Session.SetFlag */
particles_emit(PL_FG, &P_Torch_P_OnLight, 12, v2(t->x, t->y), v2(3, 3), 0);
}
}
@@ -265,6 +265,8 @@ static void new_torch(const EData *d) {
m->collidable = 0;
ST(m, TorchMgr)->slot = (uint8_t)slot;
}
+ bool start_lit = EAB(d, torch, startLit);
+ int bit = start_lit ? 0xFFFF : g_level.room->info[RR_TORCHES] * 2 + ST(m, TorchMgr)->n++;
if (ntorches >= MAX_TORCH) return;
if (torch_tex[0][0] == 0) {
tex_family("objects/temple/torch", torch_tex[0], 9);
@@ -273,13 +275,11 @@ static void new_torch(const EData *d) {
Torch *t = &torches[ntorches++];
memset(t, 0, sizeof *t);
t->x = (int16_t)d->x, t->y = (int16_t)d->y;
- t->eid = (uint16_t)d->id;
+ t->bit = (uint16_t)bit;
t->slot = (uint8_t)slot;
- t->room = (uint8_t)d->room;
- t->lit_bank = EAB(d, torch, startLit);
+ t->lit_bank = start_lit;
t->tween = 60;
- char flag[40];
- if (t->lit_bank || level_get_flag(id_flag(flag, "torch_", d->room, d->id))) {
+ if (start_lit || (bit < TORCH_BITS && (g_session.torches_lit[bit >> 3] >> (bit & 7) & 1))) { /* Session.GetFlag */
t->lit = 1; /* Added: lit, sprite.Play("on") */
t->on_mode = 1;
}
@@ -988,7 +988,6 @@ static const EntClass CRACKED = {.name = "templeCrackedBlock", .size = sizeof(Cr
.render = cracked_render, .awake = cracked_awake, .kind = KIND_SOLID};
static void new_cracked(const EData *d) {
uint32_t h = level_entity_hash(d);
- if (level_do_not_load(h)) return;
float w = EA(d, templeCrackedBlock, width), ht = EA(d, templeCrackedBlock, height);
Ent *e = ent_new(&CRACKED, d->x, d->y);
if (!e) return;
@@ -4189,7 +4188,6 @@ static const EntClass MINITRIG = {.name = "minitextboxTrigger", .size = sizeof(M
.awake = minitrig_awake, .on_enter = minitrig_enter, .kind = KIND_TRIGGER};
static void new_minitrig(const EData *d) {
uint32_t h = level_entity_hash(d);
- if (level_do_not_load(h)) return;
Ent *e = ent_new(&MINITRIG, d->x, d->y);
if (!e) return;
ent_box(e, EA(d, minitextboxTrigger, width), EA(d, minitextboxTrigger, height), 0, 0);
diff --git a/games/celeste/src/ch7.c b/games/celeste/src/ch7.c
index b307eac0..43373ba3 100644
--- a/games/celeste/src/ch7.c
+++ b/games/celeste/src/ch7.c
@@ -200,7 +200,6 @@ static const EntClass GEM = {.name = "summitgem", .size = sizeof(Gem), .update =
.on_player = gem_on_player, .kind = KIND_PCOLLIDE};
static void new_gem(const EData *d) {
uint32_t gid = level_entity_hash(d);
- if (level_do_not_load(gid)) return;
Ent *e = ent_new(&GEM, d->x, d->y);
if (!e) return;
ent_box(e, 12, 12, -6, -6);
diff --git a/games/celeste/src/data.bin b/games/celeste/src/data.bin
index 97d8e4cb..ab87c530 100644
Binary files a/games/celeste/src/data.bin and b/games/celeste/src/data.bin differ
diff --git a/games/celeste/src/entities.c b/games/celeste/src/entities.c
index 9fab7516..158fb1ef 100644
--- a/games/celeste/src/entities.c
+++ b/games/celeste/src/entities.c
@@ -23,7 +23,7 @@ V2 ed_node(const EData *d, int i) {
if (i >= d->nnodes) return v2(d->x, d->y);
return v2(d->rx + rds16(d->nodes + 4 * i), d->ry + rds16(d->nodes + 4 * i + 2));
}
-uint32_t level_entity_hash(const EData *d) { return level_entity_id(d->room, d->id); }
+uint32_t level_entity_hash(const EData *d) { return level_entity_id(d->room, d->id) | (d->trig ? 1u << 31 : 0); }
const char *area_jumpthru(void) {
/* AreaData.Jumpthru */
@@ -1180,8 +1180,6 @@ bool ent_create(const EData *d) {
return true;
}
case ET_dashBlock: {
- uint32_t h = level_entity_hash(d);
- if (level_do_not_load(h)) return true;
char t = TILE_ATTR(d, dashBlock, tiletype);
Ent *e = new_block(&DASHBLOCK, d, EA(d, dashBlock, width), EA(d, dashBlock, height), t);
if (!e) return true;
@@ -1196,8 +1194,6 @@ bool ent_create(const EData *d) {
return true;
}
case ET_fakeWall: {
- uint32_t h = level_entity_hash(d);
- if (level_do_not_load(h)) return true;
char t = TILE_ATTR(d, fakeWall, tiletype);
Ent *e = new_block(&FAKEWALL, d, EA(d, fakeWall, width), EA(d, fakeWall, height), t);
if (!e) return true;
@@ -1231,7 +1227,9 @@ bool ent_create(const EData *d) {
if (e) ent_box(e, EA(d, killbox, width), 32, 0, 0), e->visible = 0;
return true;
}
- case ET_checkpoint: {
+ case ET_checkpoint: { /* Level.LoadLevel: not falling into a room (but the first) */
+ if (g_level.last_intro == INTRO_FALL && strcmp(level_room_name(), "0")) return true;
+ g_level.start_position = ed_node(d, 0), g_level.has_start_position = true; /* + SpawnOffset */
Ent *e = ent_new(&CHECKPOINT, d->x, d->y);
if (!e) return true;
e->depth = 9990;
@@ -1284,9 +1282,12 @@ void entities_load(Room *rm, const uint8_t *blob, int slot) {
int size = pass ? trig_attr_size(d.type) : ent_attr_size(d.type);
d.nodes = d.attrs + size;
g_new_eid = (uint16_t)d.id;
- if (pass) trig_create(&d);
- else if (!leader_has_id(level_entity_id(rm->index, d.id))) /* what follows the player is there already */
- ent_create(&d);
+ d.trig = pass;
+ uint32_t id = level_entity_hash(&d);
+ if (!level_do_not_load(id) && !leader_has_id(id)) { /* what follows the player is there already */
+ if (pass) trig_create(&d);
+ else ent_create(&d);
+ }
g_new_eid = 0xFFFF;
p = d.nodes + 4 * d.nnodes;
if ((uintptr_t)(p - blob) & 1) p++;
diff --git a/games/celeste/src/entities.h b/games/celeste/src/entities.h
index 069dd737..20f5dcd6 100644
--- a/games/celeste/src/entities.h
+++ b/games/celeste/src/entities.h
@@ -11,6 +11,7 @@ typedef struct {
const uint8_t *attrs, *nodes;
int room; /* the room's index in the chapter */
float rx, ry; /* the room's offset */
+ bool trig; /* a trigger: its EntityID is apart from the entities' (ID + 10000000) */
} EData;
float ea_num(const EData *d, int off, char kind);
@@ -26,7 +27,7 @@ V2 ed_node(const EData *d, int i);
/* the area's look (AreaData): jumpthru, spikes, crumble blocks */
const char *area_jumpthru(void);
const char *area_crumble(void);
-uint32_t level_entity_hash(const EData *d);
+uint32_t level_entity_hash(const EData *d); /* its EntityID (DoNotLoad) */
/* factories: entities.c and the per-chapter files */
bool ent_create(const EData *d);
diff --git a/games/celeste/src/heart.c b/games/celeste/src/heart.c
index 267eed47..8cf905dc 100644
--- a/games/celeste/src/heart.c
+++ b/games/celeste/src/heart.c
@@ -396,6 +396,7 @@ Ent *heart_spawn(V2 at) {
return e;
}
static bool heart_new(const EData *d) {
+ if (g_session.heart && g_session.mode == M_A) return true; /* Level.LoadLevel */
Ent *e = heart_spawn(v2(d->x, d->y));
if (e) ST(e, Heart)->remove_cam = EAB(d, blackGem, removeCameraTriggers);
return true;
diff --git a/games/celeste/src/level.c b/games/celeste/src/level.c
index 971a035c..c83e3d2b 100644
--- a/games/celeste/src/level.c
+++ b/games/celeste/src/level.c
@@ -21,7 +21,7 @@ static const uint8_t *room_rec(int i) {
return c + rd32(c + CH_ROOMS + 4 * i);
}
/* room record: u16 name, u16 pack, u32 off, u32 size, s32 x, s32 y, u16 w, u16 h, u8 flags, u8 wind,
- * s8 camx, s8 camy, u32 style mask lo, hi, u16 music, u8 enforce dashes, u8 pad, u16 nneed, u16 need[] */
+ * s8 camx, s8 camy, u32 style mask lo, hi, u16 music, u8 enforce dashes, u8 first torch bit / 2 (RR_TORCHES) */
#define RR_NAME 0
#define RR_PACK 2
#define RR_OFF 4
@@ -110,7 +110,8 @@ static void tiles_unpack(uint8_t *dst, const uint8_t *src, int tw, int th) {
}
}
-static bool room_load(int slot, int index) {
+static void session_enter(int index, int intro);
+static bool room_load(int slot, int index, int intro) {
Room *rm = &g_level.rooms[slot];
rm->index = -1;
uint32_t size;
@@ -185,13 +186,18 @@ static bool room_load(int slot, int index) {
rm->index = index;
/* entities and triggers */
int was = g_level.room_slot;
- g_level.room_slot = slot;
+ Room *was_room = g_level.room;
+ g_level.room_slot = slot, g_level.room = rm; /* the room being loaded is the level's (Session.Level) */
g_res_can_load = false; /* the room's data is in the cache's free space */
g_level.has_cassette_blocks = false, g_level.cassette_beats = 2;
g_level.cam_lock = 0, g_level.cam_locker = NULL; /* Level.LoadLevel: CameraLockMode = None */
+ g_level.last_intro = (uint8_t)intro;
+ g_level.has_start_position = false;
+ g_level.in_space = (r[RR_FLAGS] & 2) != 0; /* InSpace (LevelData.Space) */
+ session_enter(index, intro);
entities_load(rm, blob, slot);
g_res_can_load = true;
- g_level.room_slot = was;
+ g_level.room_slot = was, g_level.room = was_room;
return true;
}
@@ -289,13 +295,14 @@ bool level_get_flag(const char *flag) {
return false;
}
uint32_t level_entity_id(int room, int eid) { return (uint32_t)room << 16 | (uint16_t)eid; }
+static uint32_t *dnl_at(int i) { return i < 32 ? &g_session.dnl[i] : &g_session.dnl_more[i - 32]; }
bool level_do_not_load(uint32_t id) {
for (int i = 0; i < g_session.ndnl; i++)
- if (g_session.dnl[i] == id) return true;
+ if (*dnl_at(i) == id) return true;
return false;
}
void level_set_do_not_load(uint32_t id) {
- if (!level_do_not_load(id) && g_session.ndnl < 32) g_session.dnl[g_session.ndnl++] = id;
+ if (!level_do_not_load(id) && g_session.ndnl < 48) *dnl_at(g_session.ndnl++) = id;
}
/* ---------------------------------------------------------------- the player */
@@ -348,17 +355,20 @@ static int room_at(V2 p) {
}
/* ---------------------------------------------------------------- level start / reload */
-/* Level.LoadLevel's bookkeeping in the session for room `index` */
+/* Level.LoadLevel's bookkeeping in the session for room `index`, before its entities */
static void session_enter(int index, int intro) {
Session *ss = &g_session;
ss->level = (uint8_t)index;
if (intro != INTRO_FALL || !strcmp(str(rd16(room_rec(index) + RR_NAME)), "0")) {
- ss->visited[index >> 3] |= (uint8_t)(1 << (index & 7)); /* LevelFlags */
- ss->dashes_at_level_start = ss->dashes; /* UpdateLevelStartDashes */
+ uint8_t *v = &ss->visited[index >> 3], bit = (uint8_t)(1 << (index & 7));
+ if (!(*v & bit)) ss->furthest_seen = (uint8_t)(index + 1); /* FurthestSeenLevel */
+ *v |= bit; /* LevelFlags */
+ ss->dashes_at_level_start = ss->dashes; /* UpdateLevelStartDashes */
}
}
static float spawn_fx = 0, spawn_fy = 1; /* LoadLevel's spawn: the one nearest this point of the room (fractions) */
+static bool just_started; /* Session.JustStarted: the session's first room is loading */
static void load_into_current(int index, int intro) {
ents_clear(false);
g_level.rooms[0].index = g_level.rooms[1].index = -1;
@@ -367,17 +377,18 @@ static void load_into_current(int index, int intro) {
level_tiles_entities();
spinners_entities();
dust_entities();
- if (!room_load(0, index)) { /* no room to read it: everything loaded goes first */
+ if (!room_load(0, index, intro)) { /* no room to read it: everything loaded goes first */
res_flush();
- room_load(0, index);
+ room_load(0, index, intro);
}
need_textures();
g_level.room = &g_level.rooms[0];
Room *rm = g_level.room;
g_level.core_mode = g_session.core_mode;
- session_enter(index, intro);
- if (!g_session.has_respawn) { /* DefaultSpawnPoint, or the one LoadLevel was given */
- V2 at = player_spawn_near(v2(rm->x + rm->w * spawn_fx, rm->y + rm->h * spawn_fy));
+ if (!g_session.has_respawn) { /* DefaultSpawnPoint, or the one LoadLevel was given; the checkpoint's at a start there */
+ V2 from = v2(rm->x + rm->w * spawn_fx, rm->y + rm->h * spawn_fy);
+ if (just_started && !g_session.started_from_beginning && g_level.has_start_position) from = g_level.start_position;
+ V2 at = player_spawn_near(from);
spawn_fx = 0, spawn_fy = 1;
g_session.rx = (int32_t)at.x, g_session.ry = (int32_t)at.y;
g_session.has_respawn = 1;
@@ -489,7 +500,9 @@ void level_start(int intro) {
g_level.zoom_focus = v2(160, 90);
int idx = g_session.level;
if (idx >= g_level.nrooms) idx = g_session.level = (uint8_t)level_start_room(); /* MapData.StartLevel */
+ just_started = !g_session.has_respawn;
load_into_current(idx, intro);
+ just_started = false;
style_init();
}
@@ -514,14 +527,18 @@ void level_reload(void) {
static void transition_to(int index, V2 dir) {
int slot = g_level.room_slot ^ 1;
ents_remove_room(slot);
- if (!room_load(slot, index)) return;
+ uint8_t first = g_session.first_level;
+ uint32_t deaths = g_session.deaths_in_current_level;
+ g_session.first_level = 0;
+ g_session.deaths_in_current_level = 0;
+ if (!room_load(slot, index, INTRO_TRANSITION)) {
+ g_session.first_level = first, g_session.deaths_in_current_level = deaths;
+ return;
+ }
Room *old = g_level.room;
g_level.room_slot = slot;
g_level.room = &g_level.rooms[slot];
need_textures();
- g_session.first_level = 0;
- g_session.deaths_in_current_level = 0;
- session_enter(index, INTRO_TRANSITION);
ents_awake_new();
cassette_level_start(true);
Room *rm = g_level.room;
@@ -723,6 +740,12 @@ void level_update(void) {
transition_update();
ents_update(); /* TransitionUpdate entities only */
} else {
+ if (g_level.in_space && pp) { /* SpaceController: out of view at the top or bottom, back in at the other end */
+ Ent *pe = pp->ent;
+ float top = g_level.cam.y, bottom = top + 180;
+ if (e_top(pe) > bottom + 12) pe->y += top - 4 - e_bottom(pe);
+ else if (e_bottom(pe) < top - 4) pe->y += bottom + 12 - e_top(pe);
+ }
ents_update();
}
player_after_update(&g_player);
diff --git a/games/celeste/src/level.h b/games/celeste/src/level.h
index c928deff..adca1e17 100644
--- a/games/celeste/src/level.h
+++ b/games/celeste/src/level.h
@@ -95,6 +95,9 @@ typedef struct {
float tr_duration;
/* respawn */
V2 respawn;
+ V2 start_position; /* the room's checkpoint (LoadLevel's startPosition), if has_start_position */
+ bool has_start_position;
+ uint8_t last_intro; /* LastIntroType: the INTRO_ the room was loaded with */
bool dead_reload;
bool in_cutscene;
bool skipping_cutscene;
@@ -162,6 +165,11 @@ typedef struct {
uint8_t visited[32]; /* LevelFlags: a bit per room */
struct { uint32_t key; int32_t value; } counters[3];
ModeStats old_stats; /* OldStats: the side's stats before this go */
+ /* since 1.6.1 (a 1.6.0 save has zeros here) */
+ uint8_t furthest_seen; /* FurthestSeenLevel: the room + 1, 0 if none */
+ uint8_t pad_[3];
+ uint32_t dnl_more[16]; /* DoNotLoad past dnl's 32 */
+ uint8_t torches_lit[56]; /* the Torches lit (the game's "torch_" flags): a bit each, from the room's RR_TORCHES * 2 */
} Session;
typedef struct {
@@ -175,6 +183,10 @@ typedef struct {
uint32_t total_deaths, total_golden, total_jumps, total_wall_jumps, total_dashes, time;
uint8_t bind[4]; /* jump, dash, grab, talk: EADK keys */
Session session; /* CurrentSession, and the one being played */
+ /* since 1.6.1 */
+ uint8_t cheat_mode; /* CheatMode */
+ uint8_t binds_set; /* 1: bind holds 1.6.1's keys or the player's own (1.6.0's defaults are replaced) */
+ uint8_t pad_[2];
uint32_t checksum;
} SaveData;
extern SaveData g_save;
@@ -284,6 +296,7 @@ void style_update(void);
void style_render(bool fg);
void level_tiles_entities(void);
+#define RR_TORCHES 39 /* in a room record (Room.info): its torches' first bit in Session.torches, halved */
/* lights (light.c): call while rendering */
void light_add(float x, float y, uint32_t color, float alpha, float start_fade, float end_fade);
diff --git a/games/celeste/src/save.c b/games/celeste/src/save.c
index f7261493..6a7c8c05 100644
--- a/games/celeste/src/save.c
+++ b/games/celeste/src/save.c
@@ -9,16 +9,33 @@
#define SAVE_NAME "celeste.sav"
#define SAVE_MAGIC 0x43454C53u /* "CELS" */
-#define SAVE_VERSION 1
+#define SAVE_VERSION 2
+/* 1.6.0's saves (version 1): the same up to its checksum, at the end; what came after goes after it */
+#define V1_SIZE 1552
+#define V1_CHECKSUM 1544
SaveData g_save;
-static uint32_t checksum(void) {
+static uint32_t checksum(uint32_t n) { /* of the first n bytes */
const uint8_t *p = (const uint8_t *)&g_save;
uint32_t h = 2166136261u;
- for (uint32_t i = 0; i < offsetof(SaveData, checksum); i++) h = (h ^ p[i]) * 16777619u;
+ for (uint32_t i = 0; i < n; i++) h = (h ^ p[i]) * 16777619u;
return h;
}
+/* g_save's first n bytes hold a save of this version or 1.6.0's: true if it is whole (then it is of this version, what
+ * 1.6.0 had not zero) */
+static bool whole(uint32_t n) {
+ uint8_t *b = (uint8_t *)&g_save;
+ uint32_t at = n == sizeof g_save ? offsetof(SaveData, checksum) : n == V1_SIZE ? V1_CHECKSUM : 0, ck;
+ if (!at || g_save.magic != SAVE_MAGIC || g_save.size != n || g_save.version != (n == V1_SIZE ? 1 : SAVE_VERSION))
+ return false;
+ memcpy(&ck, b + at, 4);
+ if (ck != checksum(at)) return false;
+ memset(b + at, 0, sizeof g_save - at);
+ g_save.version = SAVE_VERSION;
+ g_save.size = sizeof g_save;
+ return true;
+}
static void save_new(void) {
memset(&g_save, 0, sizeof g_save);
@@ -76,33 +93,33 @@ static bool restore_copy(void) {
if (!l) return false;
uint32_t len = 0, o = 0;
while (l + len < c + n && l[len] != '\n' && l[len]) len++;
- if (!len || len % 4 || len / 4 * 3 < sizeof g_save) return false;
+ if (!len || len % 4) return false;
+ uint32_t size = len / 4 * 3 - (l[len - 1] == '=') - (l[len - 2] == '=');
+ if (size > sizeof g_save) return false;
uint8_t *out = (uint8_t *)&g_save;
- for (uint32_t i = 0; i < len && o < sizeof g_save; i += 4) {
+ memset(out, 0, sizeof g_save);
+ for (uint32_t i = 0; i < len; i += 4) {
int v[4];
for (int k = 0; k < 4; k++) v[k] = l[i + k] == '=' ? 0 : b64_value(l[i + k]);
if (v[0] < 0 || v[1] < 0 || v[2] < 0 || v[3] < 0) return false;
uint32_t w = (uint32_t)(v[0] << 18 | v[1] << 12 | v[2] << 6 | v[3]);
- for (int k = 0; k < 3 && o < sizeof g_save; k++) out[o++] = (uint8_t)(w >> (16 - 8 * k));
+ for (int k = 0; k < 3 && o < size; k++) out[o++] = (uint8_t)(w >> (16 - 8 * k));
}
- return true;
+ return whole(size);
}
void save_load(void) {
uint32_t len = 0;
const uint8_t *p = plat_load(SAVE_NAME, &len);
- save_new();
- if (p && len == sizeof g_save) memcpy(&g_save, p, sizeof g_save); /* the record is not aligned: copy it */
- else if (!restore_copy()) save_new();
- if (g_save.magic != SAVE_MAGIC || g_save.version != SAVE_VERSION || g_save.size != sizeof g_save ||
- g_save.checksum != checksum())
- save_new();
+ memset(&g_save, 0, sizeof g_save);
+ if (p && len <= sizeof g_save) memcpy(&g_save, p, len); /* the record is not aligned: copy it */
+ if (!(p && whole(len)) && !restore_copy()) save_new();
if (g_save.bind[0] && g_save.bind[1] && g_save.bind[2] && g_save.bind[3]) memcpy(g_bind, g_save.bind, 4);
else plat_default_binds(), memcpy(g_save.bind, g_bind, 4);
}
bool save_write(void) {
- g_save.checksum = checksum();
+ g_save.checksum = checksum(offsetof(SaveData, checksum));
bool ok = plat_save(SAVE_NAME, &g_save, sizeof g_save);
save_copy();
return ok;
@@ -141,8 +158,9 @@ bool save_set_checkpoint(int index) {
void save_register_heart(void) { save_mode()->flags |= MS_HEART; }
bool save_flag(int bit) { return g_save.flags >> bit & 1; }
void save_set_flag(int bit) { g_save.flags |= (uint8_t)(1 << bit); }
-/* SaveData.UnlockedModes: B-sides after a cassette, C-sides after 16 hearts */
+/* SaveData.UnlockedModes: B-sides after a cassette, C-sides after 16 hearts (or in cheat mode) */
int save_unlocked_modes(void) {
+ if (g_save.cheat_mode) return 3;
int hearts = 0;
for (int a = 0; a < AREAS; a++)
for (int m = 0; m < 3; m++) hearts += (g_save.modes[a][m].flags & MS_HEART) != 0;
diff --git a/games/celeste/tools/pack.py b/games/celeste/tools/pack.py
index 1c41ff5b..da704d58 100644
--- a/games/celeste/tools/pack.py
+++ b/games/celeste/tools/pack.py
@@ -27,6 +27,7 @@ import entities as E # noqa: E402
HERE = os.path.dirname(os.path.abspath(__file__))
OUT_BIN = os.path.join(HERE, "..", "src", "data.bin")
OUT_H = os.path.join(HERE, "..", "src", "data.h")
+TORCH_BITS = 448 # Session.torches (src/level.h)
MAP_AREA = {"0-Intro": 0, "1-ForsakenCity": 1, "1H-ForsakenCity": 1, "1X-ForsakenCity": 1, "2-OldSite": 2,
"2H-OldSite": 2, "2X-OldSite": 2, "3-CelestialResort": 3, "3H-CelestialResort": 3,
@@ -2104,6 +2105,13 @@ def main():
body.u16(STR(a.get("fadex", "")))
body.u16(STR(a.get("fadey", "")))
body.u16(STR(a.get("only", "")))
+ # Torch: the game keeps the ones lit as session flags; the port as bits, a room's from its first (in twos)
+ torch_base, tb = [], 0
+ for r in ch.rooms:
+ torch_base.append(tb)
+ ents = r.e.child("entities").children if r.e.child("entities") else []
+ tb += (sum(1 for e in ents if e.name == "torch" and not E.attr(e, "startLit")) + 1) // 2
+ assert tb <= TORCH_BITS // 2, ch.name
for ri, r in enumerate(ch.rooms):
body.align(4)
struct.pack_into("<I", body.b, rooms_at + 4 * ri, len(body) - ch_start)
@@ -2128,7 +2136,7 @@ def main():
body.u32(r.style_mask >> 32)
body.u16(STR(r.e.get("music", "") or ""))
body.u8(int(r.e.get("enforceDashNumber", 0) or 0))
- body.u8(0)
+ body.u8(torch_base[ri])
body.align(2)
struct.pack_into("<H", body.b, extra_at, len(body) - ch_start)
for golden, cp, order, ri, eid, fl in berries: