Add manual cartridge saving and a right-side menu

This commit is contained in:
LyokoAgents committed 2026-10-28 22:38:53 +00:00
1 parent cb77683a87
commit 1141765231
17 files changed
+449 -63

No files matched your search

+33 -10
View File
@@ -12,8 +12,17 @@ void check_partner_selection(void)
caught_count = CAUGHT_CAPACITY;
for (int i = 0; i < CAUGHT_CAPACITY; ++i) caught_species[i] = (unsigned char)i;
assert(!choose_partner(-1) && !choose_partner(6));
save_from_menu();
for (int member = 0; member <= caught_count; ++member) {
unsigned char previous[sizeof(test_save)];
memcpy(previous, test_save, sizeof(previous));
int previous_member = active_member;
assert(choose_partner(member));
assert(memcmp(previous, test_save, sizeof(previous)) == 0);
load_progress();
assert(active_member == previous_member);
assert(choose_partner(member));
save_from_menu();
load_progress();
assert(active_member == member && rod_owned && ball_count == 5);
assert(strcmp(partner_name(), member ? species[member - 1].name : "SPROUT") == 0);
@@ -84,11 +93,16 @@ void check_fishing(void)
assert(begin_conversation(FISHER_X - 14, FISHER_Y));
assert(!rod_owned);
assert(advance_conversation(0) == 1 && rod_owned);
assert(test_save[4] == 5);
for (int i = 0; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
dialogue_box(1);
write_preview("build/rod-gift-preview.ppm");
assert(advance_conversation(1) == -1);
load_progress();
assert(!rod_owned);
assert(begin_conversation(FISHER_X - 14, FISHER_Y));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
load_progress();
assert(rod_owned && !partner_owned);
assert(!cast_rod(x, y));
assert(begin_conversation(FISHER_X, FISHER_Y));
@@ -114,7 +128,9 @@ void check_fishing(void)
battle.enemy_hp = 1;
assert(battle_input(SELECT) && battle.phase == BATTLE_CAUGHT);
assert(ball_count == 4 && has_caught(TIDEFIN));
assert(memcmp(before, test_save, sizeof(before)) == 0);
assert(battle_input(A));
save_from_menu();
load_progress();
assert(rod_owned && has_caught(TIDEFIN) && ball_count == 4);
draw_team_member(1);
@@ -160,7 +176,7 @@ void check_catching(void)
assert(battle_input(SELECT));
assert(ball_count == 4 && caught_count == 0 && battle.turns == 1);
assert(battle.notice == NOTICE_BROKE_FREE && battle.sprout_hp == 20);
assert(test_save[4] == 5 && test_save[6] == 4);
assert(memcmp(before, test_save, sizeof(before)) == 0);
assert(!battle_input(0) && ball_count == 4); /* Holding Select cannot throw twice. */
draw_battle_hud();
assert(pending_pixels() < 16500);
@@ -168,7 +184,7 @@ void check_catching(void)
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
write_preview("build/catch-failed-preview.ppm");
load_progress();
assert(partner_owned && ball_count == 4 && caught_count == 0);
assert(partner_owned && ball_count == 5 && caught_count == 0);
assert(battle_input(RIGHT));
assert(battle_input(A) && battle.enemy_hp == 11);
@@ -178,7 +194,7 @@ void check_catching(void)
copy_pending_rows(visible);
assert(battle_input(SELECT)); /* Quarter health is a guaranteed catch. */
assert(battle.phase == BATTLE_CAUGHT && battle.sprout_hp == hp);
assert(ball_count == 3 && caught_count == 1 && caught_species[0] == FERNFOX);
assert(ball_count == 4 && caught_count == 1 && caught_species[0] == FERNFOX);
assert(has_caught(FERNFOX) && !has_caught(GOLDEN));
draw_battle_hud();
assert(pending_pixels() < 16500);
@@ -188,10 +204,12 @@ void check_catching(void)
draw_battle();
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
write_preview("build/caught-preview.ppm");
assert(!battle_input(SELECT) && ball_count == 3 && caught_count == 1);
load_progress();
assert(ball_count == 3 && caught_count == 1 && caught_species[0] == FERNFOX);
assert(!battle_input(SELECT) && ball_count == 4 && caught_count == 1);
assert(memcmp(before, test_save, sizeof(before)) == 0);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
save_from_menu();
load_progress();
assert(ball_count == 4 && caught_count == 1 && caught_species[0] == FERNFOX);
draw_team_member(1);
write_preview("build/caught-team-preview.ppm");
@@ -200,13 +218,14 @@ void check_catching(void)
assert(begin_battle(182, 113));
battle.enemy_hp = 1;
assert(battle_input(SELECT) && battle.phase == BATTLE_CAUGHT);
assert(ball_count == 2 && caught_count == 2 && caught_species[1] == GOLDEN);
assert(ball_count == 3 && caught_count == 2 && caught_species[1] == GOLDEN);
assert(!near_golden(182, 113));
assert(battle_input(A));
assert(!begin_battle(182, 113));
reset_follower(108, 103);
render_scene(108, 103);
assert(SCREEN[95 * WIDTH + 181] == scenery(181, 95));
save_from_menu();
load_progress();
assert(has_caught(GOLDEN) && !near_golden(182, 113));
@@ -221,6 +240,7 @@ void check_catching(void)
assert(battle_input(A));
}
assert(caught_count == CAUGHT_CAPACITY && ball_count == 2);
save_from_menu();
memcpy(before, test_save, sizeof(before));
assert(start_battle(FERNFOX));
rng = random_state;
@@ -259,17 +279,20 @@ void check_catching(void)
load_progress();
assert(!partner_owned && caught_count == 0); /* No captured team without Sprout. */
/* A failed attempt can end a battle, and the used ball still stays spent. */
/* Loss does not autosave a spent ball: reload restores the last menu save. */
memset(test_save, 0xff, sizeof(test_save));
load_progress();
partner_owned = balls_given = 1;
ball_count = 1;
save_from_menu();
memcpy(before, test_save, sizeof(before));
assert(start_battle(EMBERWING));
battle.sprout_hp = 1;
random_state = 1;
assert(battle_input(SELECT) && battle.phase == BATTLE_LOST && ball_count == 0);
assert(memcmp(before, test_save, sizeof(before)) == 0);
load_progress();
assert(partner_owned && balls_given && ball_count == 0 && caught_count == 0);
assert(partner_owned && balls_given && ball_count == 1 && caught_count == 0);
assert(battle_input(A));
ball_count = 1;
assert(start_battle(PEBBLIT));
+2
View File
@@ -209,6 +209,8 @@ int main(void)
check_catching();
check_fishing();
check_partner_selection();
check_progress();
check_save_menu();
assert(test_pixels[0] == 0xbeef);
assert(test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
puts("Ball gift/inventory/save migration, wild encounters, battles, following, and map checks passed");
+95
View File
@@ -0,0 +1,95 @@
#include "test_support.h"
static void release_keys(void) { assert(!save_menu_input(0, 1)); }
void check_save_menu(void)
{
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
for (int mode = 0; mode < 3; ++mode) {
release_keys();
assert(!save_menu_input(START, 0));
assert(!save_menu_input(START, 1));
}
release_keys();
assert(save_menu_input(START | A, 1));
assert(save_menu.phase == SAVE_MENU_OPTIONS && save_menu.selection == 0);
assert(!save_menu_input(A, 1));
release_keys();
assert(save_menu_input(DOWN, 1) && save_menu.selection == 1);
assert(!save_menu_input(DOWN, 1));
release_keys();
assert(!save_menu_input(UP | DOWN, 1));
release_keys();
assert(save_menu_input(UP, 1) && save_menu.selection == 0);
release_keys();
assert(save_menu_input(UP, 1) && save_menu.selection == 1);
release_keys();
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_CLOSED);
const unsigned cancel[] = {B, START};
for (int i = 0; i < 2; ++i) {
release_keys();
assert(save_menu_input(START, 1));
release_keys();
assert(save_menu_input(cancel[i], 1) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!save_menu_input(cancel[i], 1));
}
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
release_keys();
assert(save_menu_input(START, 1));
release_keys();
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_SAVED);
test_save[0] = 0;
for (int i = 0; i < 10; ++i) assert(!save_menu_input(A, 1));
assert(test_save[0] == 0 && save_menu.phase == SAVE_MENU_SAVED);
release_keys();
assert(!save_menu_input(DOWN, 1));
release_keys();
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!save_menu_input(A, 1));
memcpy(test_save, saved, sizeof(saved));
static u16 expected[WIDTH * HEIGHT];
enum Room old_room = current_room;
int old_fx = follower_x, old_fy = follower_y, old_ready = follower_ready;
unsigned rng = random_state;
int grass = grass_steps, door = door_open;
for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room;
render_scene(108, 103);
memcpy(expected, SCREEN, sizeof(expected));
for (int phase = SAVE_MENU_OPTIONS; phase <= SAVE_MENU_FAILED; ++phase) {
save_menu.phase = (enum SaveMenuPhase)phase;
for (int choice = 0; choice < 2; ++choice) {
save_menu.selection = choice;
test_pixels[0] = test_pixels[WIDTH * HEIGHT + 1] = 0xbeef;
reset_dirty_rows();
draw_save_menu();
assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H);
for (int y = 0; y < HEIGHT; ++y) {
int row = y >= SAVE_MENU_Y && y < SAVE_MENU_Y + SAVE_MENU_H;
if (row) {
assert(dirty_first[y] == SAVE_MENU_X);
assert(dirty_end[y] == SAVE_MENU_X + SAVE_MENU_W);
} else assert(dirty_first[y] >= dirty_end[y]);
for (int x = 0; x < WIDTH; ++x)
if (!row || x < SAVE_MENU_X || x >= SAVE_MENU_X + SAVE_MENU_W)
assert(SCREEN[y * WIDTH + x] == expected[y * WIDTH + x]);
}
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
if (room == OUTSIDE && choice == 0) {
const char *paths[] = {"build/save-menu-preview.ppm",
"build/game-saved-preview.ppm", "build/save-failed-preview.ppm"};
write_preview(paths[phase - SAVE_MENU_OPTIONS]);
}
}
render_scene(108, 103);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
}
}
assert(follower_x == old_fx && follower_y == old_fy && follower_ready == old_ready);
assert(random_state == rng && grass_steps == grass && door_open == door);
current_room = old_room;
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
}
+94
View File
@@ -0,0 +1,94 @@
#include "test_support.h"
static void assert_empty_progress(void)
{
assert(!partner_owned && !balls_given && !rod_owned && !ball_count && !caught_count && !active_member);
for (int i = 0; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
}
void check_progress(void)
{
memset(test_save, 0xff, sizeof(test_save));
load_progress();
assert_empty_progress();
save_from_menu();
const unsigned char empty[] = {'F','Q','S','P',5,0,0,0,255,255,255,255,255,0x5f,0x5a};
assert(memcmp(test_save, empty, sizeof(empty)) == 0);
partner_owned = balls_given = 1;
ball_count = 5;
save_from_menu();
const unsigned char gifts[] = {'F','Q','S','P',5,3,5,0,255,255,255,255,255,0x59,0x5a};
assert(memcmp(test_save, gifts, sizeof(gifts)) == 0);
/* An unsaved expanded team and rod roll back to the previous v5 snapshot. */
rod_owned = 1;
caught_count = active_member = 1;
caught_species[0] = TIDEFIN;
load_progress();
assert(partner_owned && balls_given && ball_count == 5 && !rod_owned && !caught_count && !active_member);
for (int i = 0; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
rod_owned = 1;
caught_count = active_member = 2;
caught_species[0] = FERNFOX;
caught_species[1] = TIDEFIN;
ball_count = 3;
save_from_menu();
const unsigned char team[] = {'F','Q','S','P',5,0x17,3,2,1,4,255,255,255,0x4c,0x5a};
assert(memcmp(test_save, team, sizeof(team)) == 0);
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
partner_owned = balls_given = rod_owned = caught_count = active_member = ball_count = 0;
memset(caught_species, 0xff, sizeof(caught_species));
load_progress();
assert(partner_owned && balls_given && rod_owned && caught_count == 2 && active_member == 2 && ball_count == 3);
assert(caught_species[0] == FERNFOX && caught_species[1] == TIDEFIN);
for (int invalid = 0; invalid < 9; ++invalid) {
load_progress();
switch (invalid) {
case 0: ball_count = -1; break;
case 1: ball_count = 100; break;
case 2: balls_given = 0; break;
case 3: caught_count = -1; break;
case 4: caught_count = CAUGHT_CAPACITY + 1; break;
case 5: partner_owned = 0; break;
case 6: active_member = -1; break;
case 7: active_member = 3; break;
default: caught_species[1] = SPECIES_COUNT; break;
}
int state[] = {partner_owned, balls_given, rod_owned, ball_count, caught_count, active_member};
unsigned char kinds[CAUGHT_CAPACITY];
memcpy(kinds, caught_species, sizeof(kinds));
assert(!save_menu_input(0, 1));
assert(save_menu_input(START, 1));
assert(!save_menu_input(0, 1));
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_FAILED);
int after[] = {partner_owned, balls_given, rod_owned, ball_count, caught_count, active_member};
assert(memcmp(state, after, sizeof(state)) == 0);
assert(memcmp(kinds, caught_species, sizeof(kinds)) == 0);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
assert(!save_menu_input(A, 1));
assert(!save_menu_input(0, 1));
assert(save_menu_input(B, 1));
}
load_progress();
partner_owned = 2; balls_given = 7; rod_owned = -1;
save_from_menu();
assert(partner_owned == 2 && balls_given == 7 && rod_owned == -1);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
for (int byte = 0; byte < SAVE_RECORD_SIZE; ++byte) {
memcpy(test_save, saved, sizeof(saved));
test_save[byte] ^= 1;
load_progress();
assert_empty_progress();
}
/* v2 ignores newer trailing bytes and defaults rod, catches and selection. */
const unsigned char v2[] = {'F','Q','S','P',2,3,5,0xa1,0x5a};
memset(test_save, 0x31, sizeof(test_save));
memcpy(test_save, v2, sizeof(v2));
load_progress();
assert(partner_owned && balls_given && ball_count == 5 && !rod_owned && !caught_count && !active_member);
for (int i = 0; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
assert(memcmp(test_save, v2, sizeof(v2)) == 0 && test_save[14] == 0x31);
memset(test_save, 0xff, sizeof(test_save));
load_progress();
}
+4
View File
@@ -22,6 +22,9 @@ class HeaderTests(unittest.TestCase):
def test_sram_save_marker_is_in_rom(self):
self.assertIn(b"SRAM_V113", self.rom)
def test_homebrew_code_avoids_classic_nes_override(self):
self.assertEqual(self.rom[172:176], b"QST0")
def test_bad_logo_is_rejected(self):
self.rom[4] ^= 1
with self.assertRaisesRegex(ValueError, "hardware header"):
@@ -54,6 +57,7 @@ class HeaderTests(unittest.TestCase):
result = path.read_bytes()
self.assertEqual(len(result), 32768)
self.assertEqual(result[160:172], b"FIRSTQUEST\0\0")
self.assertEqual(result[172:176], b"QST0")
header.verify(result)
+21
View File
@@ -33,6 +33,11 @@ void check_starter_gift(void)
dialogue_box(1);
write_preview("build/starter-gift-preview.ppm");
assert(!receive_partner()); /* The same gift cannot add a second partner. */
for (int i = 0; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
load_progress();
assert(!partner_owned);
assert(receive_partner());
save_from_menu();
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
assert(advance_conversation(1) == -1);
@@ -58,6 +63,11 @@ void check_starter_gift(void)
partner_owned = 0;
load_progress();
assert(partner_owned);
current_room = MELLON_HOUSE;
assert(begin_conversation(PROF_X, PROF_Y + 22) && !gift_conversation);
assert(!receive_partner());
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
current_room = OUTSIDE;
draw_team();
write_preview("build/team-preview.ppm");
static u16 expected[WIDTH * HEIGHT];
@@ -132,6 +142,12 @@ void check_ball_gift(void)
restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H);
actors(99, 97, 0);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
assert(test_save[4] == 1 && memcmp(test_save, legacy, sizeof(legacy)) == 0);
load_progress();
assert(partner_owned && !balls_given && ball_count == 0);
assert(begin_conversation(99, 97));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
assert(test_save[4] == 5);
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
@@ -144,6 +160,9 @@ void check_ball_gift(void)
partner_owned = balls_given = ball_count = 0;
load_progress();
assert(partner_owned && balls_given && ball_count == 5);
assert(begin_conversation(99, 97) && !ball_gift_conversation);
assert(!receive_balls());
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
draw_team();
write_preview("build/poke-balls-preview.ppm");
for (int i = SAVE_RECORD_SIZE; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
@@ -157,11 +176,13 @@ void check_ball_gift(void)
current_room = ROUTE;
assert(begin_conversation(99, 97));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
load_progress();
assert(!partner_owned && balls_given && ball_count == 5);
current_room = MELLON_HOUSE;
assert(begin_conversation(PROF_X, PROF_Y + 22));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
load_progress();
assert(partner_owned && balls_given && ball_count == 5);
memcpy(saved, test_save, sizeof(saved));
+11
View File
@@ -3,6 +3,17 @@
u16 test_pixels[WIDTH * HEIGHT + 2];
unsigned char test_save[32];
void save_from_menu(void)
{
assert(!save_menu_input(0, 1));
assert(save_menu_input(START, 1) && save_menu.phase == SAVE_MENU_OPTIONS);
assert(!save_menu_input(0, 1));
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_SAVED);
assert(!save_menu_input(0, 1));
assert(save_menu_input(B, 1) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!save_menu_input(0, 1));
}
void render_scene(int x, int y)
{
draw_scene(x, y);
+3
View File
@@ -27,5 +27,8 @@ void check_music(void);
void check_catching(void);
void check_fishing(void);
void check_partner_selection(void);
void check_save_menu(void);
void check_progress(void);
void save_from_menu(void);
#endif