diff --git a/Makefile b/Makefile index 37f8f26..065c6cb 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,11 @@ CC := $(TOOL_PREFIX)gcc OBJCOPY := $(TOOL_PREFIX)objcopy CFLAGS := -mcpu=arm7tdmi -marm -O2 -g -Wall -Wextra -Werror -ffreestanding -fno-builtin -fno-common -Wa,--noexecstack GAME_SOURCES := source/main.c source/video.c source/scenery.c source/world.c \ - source/actors.c source/battle.c source/story.c source/hud.c source/audio.c source/beach.c + source/actors.c source/battle.c source/story.c source/hud.c source/audio.c source/beach.c source/menu.c GAME_OBJECTS := $(GAME_SOURCES:source/%.c=build/%.o) TEST_SOURCES := tests/test_game.c tests/test_support.c tests/test_maps.c \ tests/test_story.c tests/test_battle.c tests/test_rendering.c tests/test_movement.c tests/test_audio.c \ - tests/test_catching.c + tests/test_catching.c tests/test_menu.c tests/test_progress.c .PHONY: all play check check-host all: game.gba diff --git a/README.md b/README.md index 52d1768..36ff40d 100644 --- a/README.md +++ b/README.md @@ -24,17 +24,17 @@ The game file is **game.gba**. You can also open it directly in mGBA without reb - **Return to the village:** on Sunpetal Trail, follow the central path up to the top edge. Press X beside the wooden sign to read the directions. - **Visit Professor Mellon:** follow the village's horizontal sandy path right through the fence opening. Her blue-roofed house is in the garden. Press X on its steps to enter, then walk close to the professor and press X to talk. Leave through the bottom doorway, then follow the garden path left to return to town. - **Visit Seashell Beach:** from Professor Mellon's garden, follow the central sandy path down through the bottom fence gap. Walk up the beach's northern path to return to the garden. Explore the dry sand, palms, umbrella, shells, and starfish; the sea is not walkable. Sprout comes along, and the beach uses the calm trail music. -- **Go fishing:** talk to **Fisher Coral**, the red-haired person in turquoise on the right side of Seashell Beach. Press X again to receive a **Poké Rod**, saved permanently in your bag. Bring Sprout, walk to the water's edge, face down toward the sea, and press **X** to hook **Tidefin**, an original Water-type fish. Weaken it and press **Backspace** to throw a Poké Ball. The rod is reusable; catching still requires balls from Ranger Finn and space on your team. Older saves keep their gifts and catches. +- **Go fishing:** talk to **Fisher Coral**, the red-haired person in turquoise on the right side of Seashell Beach. Press X again to receive a reusable **Poké Rod** in your bag; choose Save from the Start menu to keep it after restarting. Bring Sprout, walk to the water's edge, face down toward the sea, and press **X** to hook **Tidefin**, an original Water-type fish. Weaken it and press **Backspace** to throw a Poké Ball. Catching still requires balls from Ranger Finn and space on your team. Older saves keep their gifts and catches. - **Get your first partner:** talk to Professor Mellon and press X to advance her conversation. She gives you **Sprout**, an original Grass-type partner at level five. This is a one-time gift; later conversations check on Sprout instead. - **Character artwork:** the player and Ranger Finn use boy sprites; Mila and Professor Mellon use distinct long-haired girl sprites. Mellon keeps her white coat and gray hair. Both styles have front, back, and side views. -- **Backspace (GBA Select button):** open your team. Use Left/Right to browse Sprout and your caught creatures, then press **X (A)** to choose the displayed creature as your following and battling partner. Backspace or Z (B) returns without changing your choice. Your selection is saved automatically. Movement pauses while the team screen is open. +- **Backspace (GBA Select button):** open your team. Use Left/Right to browse Sprout and your caught creatures, then press **X (A)** to choose the displayed creature as your following and battling partner. Backspace or Z (B) returns without changing your choice. Choose Save from the Start menu to retain your selection. Movement pauses while the team screen is open. - **Your partner follows you:** Sprout or your selected caught creature follows your path and travels through doors and map exits with you. Each uses its own artwork. Choose Sprout again from the team screen whenever you like. - **Battle the golden creature:** with Sprout on your team, walk close to the golden creature in Greenleaf Village and press X. Use Left/Right to choose **Tackle** or **Leaf Tap**, then X to attack. The golden creature responds with Bump. Press Z (B) to run away. At the result screen, press X or Z to return to the map. Sprout recovers fully after each battle. - **Wild encounters:** with Sprout, walk around either tall-grass patch on Sunpetal Trail. **Fernfox** (Grass), **Emberwing** (Fire), or **Pebblit** (Rock) can appear automatically. Each has its own original 24×24 sprite, palette, health, and attack. Encounters only happen while moving in grass, with a short breather between battles. Leaf Tap is stronger against Pebblit and weaker against Emberwing. Fighting and running use the same keys as the golden-creature battle. - **Get Poké Balls:** talk to **Ranger Finn**, the person in orange just left of Sunpetal Trail's central path. Press X again to receive **five Poké Balls**. This gift is given once. Press Backspace to see the count on the team screen, even if you do not have Sprout yet. - **Catch a creature:** press **Backspace during a battle** to throw one Poké Ball. Lower health improves the chance; at one-quarter health or less, the catch is guaranteed. A failed throw still uses the ball and lets the creature attack. You cannot catch a fainted creature. Successful catches join your saved team, with room for Sprout plus five catches (duplicates are allowed). A full team does not waste a ball. The village's golden creature disappears from the map once caught. Press X after the catch result, then Backspace and Left/Right to view your new friend. Press X to make it your partner. - **Battle with caught creatures:** your selected partner fights in both land and fishing battles, using its own maximum health, Tackle, and a second move: Golden's Bump, Fernfox's Leaf Tap, Emberwing's Spark, Pebblit's Roll, or Tidefin's Water Jet. Partners recover fully after battles. Choose partners outside battle, not during a turn. -- **Return (GBA Start button):** return to your starting position in the current map. +- **Return (GBA Start button):** open the **Save / Close** right sidebar while exploring. The world stays visible and paused. Up/Down chooses an option, X (A) activates it, and Z (B) or Return closes it. Saved/failed messages wait for a fresh X, Z, or Return press. Start no longer resets your position; the menu cannot open during conversations, battles, or the team screen. Music and S (R) mute continue working while it is open. - **S (GBA R shoulder button):** mute or unmute the music, including during battles and menus. This setting lasts until you restart the ROM. If those keys do not match your emulator settings, check mGBA's keyboard input settings. @@ -61,6 +61,7 @@ Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)` - `source/battle.c`: species, battle rules, and the battle screen. - `source/story.c`: conversations, gifts, and cartridge SRAM saves. - `source/hud.c`: map status, scene drawing, and the team screen. +- `source/menu.c`: Save / Close sidebar input and drawing. - `source/audio.c` and `source/audio.h`: original chiptune scores and the GBA sound-channel sequencer. - `source/wild_creatures.h`: original wild-creature sprites and palettes. - `source/start.s`: code that starts the GBA hardware. @@ -81,7 +82,11 @@ Movement has a separate 60 Hz fixed-step clock using cascaded GBA timers 2 and 3 ## Saving your partner and items -Receiving gifts, throwing Poké Balls, and choosing partners automatically write a small cartridge SRAM save, stored by mGBA alongside the ROM as `game.sav`. Keep that file to retain your gifts, caught creatures, remaining balls, and selected partner after reopening the game. Older saves remain compatible and default to Sprout as the active partner. Your map and position are not saved. Partners recover after battles. Experience is not implemented yet. +Only choosing **Save** in the Start sidebar writes cartridge SRAM, stored by mGBA alongside the ROM as `game.sav`. Gifts, throwing balls (successful or failed, including battle losses), and partner selection change the current session only. Save manually before closing or resetting the emulator; otherwise reload restores your last saved state, including previously unspent balls and unclaimed gifts. Keep `game.sav` to retain Sprout ownership, Finn's gift flag, remaining balls, the rod, caught species and your selected partner. There are no startup, exit, or gameplay autosaves. Your map, position and battle state are not saved; partners recover after battles. + +The sidebar states **TEAM AND / ITEMS ONLY / NO LOCATION**. **GAME / SAVED** means the complete v5 cartridge record was written and read back, not that mGBA has flushed it to disk. **SAVE / FAILED** means validation or read-back failed. Invalid in-memory state is rejected before writing; a read-back failure or interrupted write does not restore the previous record. Close mGBA normally. Existing v1/v2/v3/v4 saves still load; older formats default newly introduced fields appropriately. Save writes the unchanged v5 format. Do not reopen that save with an old v1/v2-only ROM: it cannot load v5 and could overwrite your newer progress. + +The ROM uses homebrew game code `QST0`. The previous `FQ01` collided with mGBA 0.10.5's Classic NES detection, which forced EEPROM instead of SRAM. ## Music @@ -118,6 +123,6 @@ We are making an original game. No existing Pokémon ROM or separate BIOS downlo ## Project status -Greenleaf Village connects south to Sunpetal Trail and east to Professor Mellon's garden and house. Seashell Beach is south of the garden, with a return path north. Both houses have working doors. Mila and the professor have conversations, and Mellon gives you Sprout once. Ranger Finn gives you five usable Poké Balls. Sprout follows your footsteps and battles the village's golden creature and three wild route species. Battles support two moves, species-specific replies, health bars, catching, victory, defeat, running away, and free recovery. Caught creatures and spent balls are saved, and the team screen lets you browse up to six members. +Greenleaf Village connects south to Sunpetal Trail and east to Professor Mellon's garden and house. Seashell Beach is south of the garden, with a return path north. Both houses have working doors. Mila and the professor have conversations, and Mellon gives you Sprout once. Ranger Finn gives you five usable Poké Balls. Sprout follows your footsteps and battles the village's golden creature and three wild route species. Battles support two moves, species-specific replies, health bars, catching, victory, defeat, running away, and free recovery. The team screen lets you browse up to six members; use the Start sidebar to save your team and items. All artwork and music are original; no Pokémon game assets or ROMs are included. Experience, mid-battle partner switching, sound effects, and full progress saving are not implemented yet. diff --git a/source/battle.c b/source/battle.c index da93cee..8b5cd60 100644 --- a/source/battle.c +++ b/source/battle.c @@ -89,7 +89,6 @@ static int throw_ball(void) battle.notice = NOTICE_BROKE_FREE; enemy_reply(); } - save_progress(); /* Save spent balls as well as successful catches. */ return 1; } diff --git a/source/game.h b/source/game.h index b8a802a..e3ad35b 100644 --- a/source/game.h +++ b/source/game.h @@ -107,7 +107,17 @@ int battle_input(unsigned pressed); void draw_battle_hud(void); void draw_battle(void); void load_progress(void); -void save_progress(void); +int save_progress(void); +enum SaveMenuPhase { SAVE_MENU_CLOSED, SAVE_MENU_OPTIONS, SAVE_MENU_SAVED, SAVE_MENU_FAILED }; +enum { SAVE_MENU_X = 160, SAVE_MENU_Y = 6, SAVE_MENU_W = 74, SAVE_MENU_H = 148 }; +struct SaveMenuState { + enum SaveMenuPhase phase; + int selection; + unsigned previous; +}; +extern struct SaveMenuState save_menu; +int save_menu_input(unsigned keys, int can_open); +void draw_save_menu(void); int has_caught(enum Species kind); int near_npc(int x, int y); int begin_conversation(int x, int y); diff --git a/source/hud.c b/source/hud.c index 6117a52..5c1afb6 100644 --- a/source/hud.c +++ b/source/hud.c @@ -33,7 +33,7 @@ void draw_scene(int x, int y) { restore(0, 0, WIDTH, HEIGHT); location_status(x, y); - text(8, 148, "ARROWS MOVE A USE SELECT TEAM START RESET R MUSIC", RGB(5, 9, 11)); + text(8, 148, "ARROWS MOVE A USE SELECT TEAM START MENU R MUSIC", RGB(5, 9, 11)); actors(x, y, 0); } diff --git a/source/main.c b/source/main.c index 45c9090..d804d10 100644 --- a/source/main.c +++ b/source/main.c @@ -86,6 +86,25 @@ int main(void) unsigned now = movement_time(); music_update(now); if (pressed & R_BUTTON) music_toggle_mute(); + int menu_was_open = save_menu.phase != SAVE_MENU_CLOSED; + int menu_changed = save_menu_input(keys, + !team_open && battle.phase == BATTLE_OFF && dialogue_page < 0); + if (menu_was_open || save_menu.phase != SAVE_MENU_CLOSED) { + pause_movement_at(now); + if (menu_changed) { + if (save_menu.phase == SAVE_MENU_CLOSED) { + DISPLAY = 0x0080; + draw_scene(x, y); + present_frame(); + DISPLAY = 0x0403; + } else { + draw_save_menu(); + present_frame(); + } + pause_movement_at(movement_time()); + } else present_frame(); + continue; + } int movement_ticks = 0; if (team_open || battle.phase != BATTLE_OFF || dialogue_page >= 0) pause_movement_at(now); @@ -169,15 +188,7 @@ int main(void) int old_follower_x = follower_x, old_follower_y = follower_y; enum Facing old_facing = player_facing; enum WalkResult walk = WALK_NORMAL; - if (pressed & START) { - reset_position(&x, &y); - greeting = 0; - location_status(x, y); - pause_movement_at(now); - movement_ticks = 0; - } else { - walk = walk_movement(keys, &x, &y, movement_ticks); - } + walk = walk_movement(keys, &x, &y, movement_ticks); if (walk == WALK_MAP_CHANGED) { greeting = 0; dialogue_page = -1; diff --git a/source/menu.c b/source/menu.c new file mode 100644 index 0000000..c96bd9c --- /dev/null +++ b/source/menu.c @@ -0,0 +1,66 @@ +#include "game.h" + +struct SaveMenuState save_menu; + +int save_menu_input(unsigned keys, int can_open) +{ + unsigned pressed = keys & ~save_menu.previous; + save_menu.previous = keys; + if (save_menu.phase == SAVE_MENU_CLOSED) { + if (!can_open || !(pressed & START)) return 0; + save_menu.phase = SAVE_MENU_OPTIONS; + save_menu.selection = 0; + return 1; + } + if (pressed & (B | START)) { + save_menu.phase = SAVE_MENU_CLOSED; + return 1; + } + if (save_menu.phase != SAVE_MENU_OPTIONS) { + if (!(pressed & A)) return 0; + save_menu.phase = SAVE_MENU_CLOSED; + return 1; + } + if (pressed & A) { + if (save_menu.selection) save_menu.phase = SAVE_MENU_CLOSED; + else save_menu.phase = save_progress() ? SAVE_MENU_SAVED : SAVE_MENU_FAILED; + return 1; + } + unsigned direction = pressed & (UP | DOWN); + if (direction == UP || direction == DOWN) { + save_menu.selection ^= 1; + return 1; + } + return 0; +} + +void draw_save_menu(void) +{ + u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26); + int x = SAVE_MENU_X, y = SAVE_MENU_Y; + rectangle(x, y, SAVE_MENU_W, SAVE_MENU_H, ink); + rectangle(x + 2, y + 2, SAVE_MENU_W - 4, SAVE_MENU_H - 4, cream); + large_text(x + 16, y + 10, "MENU", ink); + rectangle(x + 6, y + 23, SAVE_MENU_W - 12, 1, RGB(15, 24, 20)); + text(x + 8, y + 85, "TEAM AND", ink); + text(x + 8, y + 95, "ITEMS ONLY", ink); + text(x + 8, y + 105, "NO LOCATION", ink); + if (save_menu.phase == SAVE_MENU_OPTIONS) { + int selected_y = y + 32 + save_menu.selection * 20; + rectangle(x + 6, selected_y, SAVE_MENU_W - 12, 16, RGB(23, 28, 21)); + rectangle(x + 10, selected_y + 5, 1, 5, ink); + rectangle(x + 11, selected_y + 6, 1, 3, ink); + rectangle(x + 12, selected_y + 7, 1, 1, ink); + large_text(x + 20, y + 37, "SAVE", ink); + large_text(x + 20, y + 57, "CLOSE", ink); + text(x + 8, y + 124, "UP DOWN", ink); + text(x + 8, y + 133, "A SELECT", ink); + text(x + 8, y + 142, "B START CLOSE", ink); + } else { + int saved = save_menu.phase == SAVE_MENU_SAVED; + large_text(x + 16, y + 37, saved ? "GAME" : "SAVE", ink); + large_text(x + 16, y + 55, saved ? "SAVED" : "FAILED", ink); + text(x + 8, y + 126, "A B START", ink); + text(x + 8, y + 136, "TO CLOSE", ink); + } +} diff --git a/source/story.c b/source/story.c index 518711a..9f34fc3 100644 --- a/source/story.c +++ b/source/story.c @@ -16,46 +16,75 @@ static const char save_signature[] __attribute__((used)) = "SRAM_V113"; #define SAVE_MEMORY ((volatile unsigned char *)0x0e000000) #endif -void load_progress(void) +struct Progress { + int partner, given, rod, count, members, active; + unsigned char kinds[CAUGHT_CAPACITY]; +}; + +static void clear_progress(struct Progress *p) { - partner_owned = ball_count = balls_given = caught_count = 0; - rod_owned = 0; - active_member = 0; - for (int i = 0; i < CAUGHT_CAPACITY; ++i) caught_species[i] = 0xff; + p->partner = p->given = p->rod = p->count = p->members = p->active = 0; + for (int i = 0; i < CAUGHT_CAPACITY; ++i) p->kinds[i] = 0xff; +} + +static int valid_progress(const struct Progress *p) +{ + if (p->count < 0 || p->count > 99 || (!p->given && p->count != 0) || + p->members < 0 || p->members > CAUGHT_CAPACITY || (p->members && !p->partner) || + p->active < 0 || p->active > p->members || (p->active && !p->partner)) return 0; + for (int i = 0; i < CAUGHT_CAPACITY; ++i) + if (i < p->members ? p->kinds[i] >= SPECIES_COUNT : p->kinds[i] != 0xff) return 0; + return 1; +} + +static int read_progress(struct Progress *p) +{ + clear_progress(p); if (SAVE_MEMORY[0] != 'F' || SAVE_MEMORY[1] != 'Q' || - SAVE_MEMORY[2] != 'S' || SAVE_MEMORY[3] != 'P') return; + SAVE_MEMORY[2] != 'S' || SAVE_MEMORY[3] != 'P') return 0; /* Keep Sprout from the original version-one save format. */ if (SAVE_MEMORY[4] == 1) { - partner_owned = SAVE_MEMORY[5] == 1 && SAVE_MEMORY[6] == 0xa5 && SAVE_MEMORY[7] == 0x5a; - return; + p->partner = SAVE_MEMORY[5] == 1 && SAVE_MEMORY[6] == 0xa5 && SAVE_MEMORY[7] == 0x5a; + return p->partner && valid_progress(p); } unsigned flags = SAVE_MEMORY[5], count = SAVE_MEMORY[6]; unsigned version = SAVE_MEMORY[4]; - if (flags > (version == 5 ? 47u : version == 4 ? 7u : 3u) || count > 99 || (!(flags & 2) && count != 0)) return; + if (flags > (version == 5 ? 47u : version == 4 ? 7u : 3u)) return 0; if (SAVE_MEMORY[4] == 2) { - if (SAVE_MEMORY[8] != 0x5a || SAVE_MEMORY[7] != (0xa5 ^ 2 ^ flags ^ count)) return; + if (SAVE_MEMORY[8] != 0x5a || SAVE_MEMORY[7] != (0xa5 ^ 2 ^ flags ^ count)) return 0; } else if (version == 3 || version == 4 || version == 5) { unsigned members = SAVE_MEMORY[7]; - if (members > CAUGHT_CAPACITY || (members && !(flags & 1)) || SAVE_MEMORY[14] != 0x5a) - return; - if (version == 5 && ((flags >> 3) > members || ((flags >> 3) && !(flags & 1)))) return; + if (SAVE_MEMORY[14] != 0x5a) return 0; unsigned checksum = 0xa5 ^ version ^ flags ^ count ^ members; for (unsigned i = 0; i < CAUGHT_CAPACITY; ++i) { unsigned kind = SAVE_MEMORY[8 + i]; - if (i < members ? kind >= SPECIES_COUNT : kind != 0xff) return; + p->kinds[i] = (unsigned char)kind; checksum ^= kind; } - if (SAVE_MEMORY[13] != checksum) return; - caught_count = (int)members; - for (int i = 0; i < caught_count; ++i) caught_species[i] = SAVE_MEMORY[8 + i]; + if (SAVE_MEMORY[13] != checksum) return 0; + p->members = (int)members; } else { - return; + return 0; } - partner_owned = !!(flags & 1); - balls_given = !!(flags & 2); - rod_owned = !!(flags & 4); - ball_count = (int)count; - active_member = version == 5 ? (int)(flags >> 3) : 0; + p->partner = !!(flags & 1); + p->given = !!(flags & 2); + p->rod = !!(flags & 4); + p->count = (int)count; + p->active = version == 5 ? (int)(flags >> 3) : 0; + return valid_progress(p); +} + +void load_progress(void) +{ + struct Progress p; + if (!read_progress(&p)) clear_progress(&p); + partner_owned = p.partner; + balls_given = p.given; + rod_owned = p.rod; + ball_count = p.count; + caught_count = p.members; + active_member = p.active; + for (int i = 0; i < CAUGHT_CAPACITY; ++i) caught_species[i] = p.kinds[i]; } int choose_partner(int member) @@ -63,7 +92,6 @@ int choose_partner(int member) if (!partner_owned || battle.phase != BATTLE_OFF || member < 0 || member > caught_count) return 0; active_member = member; - save_progress(); return 1; } @@ -89,10 +117,21 @@ const char *partner_move(void) } } -void save_progress(void) +int save_progress(void) { - unsigned flags = (partner_owned ? 1u : 0u) | (balls_given ? 2u : 0u) | (rod_owned ? 4u : 0u) | ((unsigned)active_member << 3); - unsigned checksum = 0xa5 ^ 5 ^ flags ^ (unsigned)ball_count ^ (unsigned)caught_count; + struct Progress p, readback; + p.partner = !!partner_owned; + p.given = !!balls_given; + p.rod = !!rod_owned; + p.count = ball_count; + p.members = caught_count; + p.active = active_member; + for (int i = 0; i < CAUGHT_CAPACITY; ++i) + p.kinds[i] = i < caught_count ? caught_species[i] : 0xff; + if (!valid_progress(&p)) return 0; + unsigned flags = (unsigned)p.partner | ((unsigned)p.given << 1) | + ((unsigned)p.rod << 2) | ((unsigned)p.active << 3); + unsigned checksum = 0xa5 ^ 5 ^ flags ^ (unsigned)p.count ^ (unsigned)p.members; /* Commit the first magic byte last so an interrupted write is invalid. */ SAVE_MEMORY[0] = 0; SAVE_MEMORY[1] = 'Q'; @@ -100,16 +139,22 @@ void save_progress(void) SAVE_MEMORY[3] = 'P'; SAVE_MEMORY[4] = 5; SAVE_MEMORY[5] = (unsigned char)flags; - SAVE_MEMORY[6] = (unsigned char)ball_count; - SAVE_MEMORY[7] = (unsigned char)caught_count; + SAVE_MEMORY[6] = (unsigned char)p.count; + SAVE_MEMORY[7] = (unsigned char)p.members; for (int i = 0; i < CAUGHT_CAPACITY; ++i) { - unsigned kind = i < caught_count ? caught_species[i] : 0xff; + unsigned kind = p.kinds[i]; SAVE_MEMORY[8 + i] = (unsigned char)kind; checksum ^= kind; } SAVE_MEMORY[13] = (unsigned char)checksum; SAVE_MEMORY[14] = 0x5a; SAVE_MEMORY[0] = 'F'; + if (!read_progress(&readback) || p.partner != readback.partner || p.given != readback.given || + p.rod != readback.rod || p.count != readback.count || p.members != readback.members || + p.active != readback.active) return 0; + for (int i = 0; i < CAUGHT_CAPACITY; ++i) + if (p.kinds[i] != readback.kinds[i]) return 0; + return 1; } int has_caught(enum Species kind) @@ -188,7 +233,6 @@ TEST_INTERNAL int receive_partner(void) { if (current_room != MELLON_HOUSE || !gift_conversation || partner_owned) return 0; partner_owned = 1; - save_progress(); reset_follower(PROF_X, PROF_Y + 22); return 1; } @@ -199,7 +243,6 @@ TEST_INTERNAL int receive_balls(void) ball_count += 5; if (ball_count > 99) ball_count = 99; balls_given = 1; - save_progress(); return 1; } @@ -210,7 +253,6 @@ int advance_conversation(int page) if (next == 1 && ball_gift_conversation) receive_balls(); if (next == 1 && rod_gift_conversation && current_room == BEACH && !rod_owned) { rod_owned = 1; - save_progress(); } if (next < 0) rod_gift_conversation = 0; if (next < 0) gift_conversation = ball_gift_conversation = 0; diff --git a/tests/test_catching.c b/tests/test_catching.c index 72aaab4..e7bd5f0 100644 --- a/tests/test_catching.c +++ b/tests/test_catching.c @@ -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)); diff --git a/tests/test_game.c b/tests/test_game.c index 0ffe498..62e1b2f 100644 --- a/tests/test_game.c +++ b/tests/test_game.c @@ -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"); diff --git a/tests/test_menu.c b/tests/test_menu.c new file mode 100644 index 0000000..d9c797a --- /dev/null +++ b/tests/test_menu.c @@ -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}; +} diff --git a/tests/test_progress.c b/tests/test_progress.c new file mode 100644 index 0000000..97fd1a5 --- /dev/null +++ b/tests/test_progress.c @@ -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(); +} diff --git a/tests/test_rom_header.py b/tests/test_rom_header.py index fdd4a16..603c068 100644 --- a/tests/test_rom_header.py +++ b/tests/test_rom_header.py @@ -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) diff --git a/tests/test_story.c b/tests/test_story.c index 63246b4..fa85757 100644 --- a/tests/test_story.c +++ b/tests/test_story.c @@ -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)); diff --git a/tests/test_support.c b/tests/test_support.c index e389878..7d9e515 100644 --- a/tests/test_support.c +++ b/tests/test_support.c @@ -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); diff --git a/tests/test_support.h b/tests/test_support.h index cc32cc7..66164d3 100644 --- a/tests/test_support.h +++ b/tests/test_support.h @@ -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 diff --git a/tools/rom_header.py b/tools/rom_header.py index d8c28c1..62a04d9 100644 --- a/tools/rom_header.py +++ b/tools/rom_header.py @@ -44,7 +44,7 @@ def main(): raise ValueError("Missing cartridge header space") rom[4:160] = LOGO rom[160:172] = b"FIRSTQUEST".ljust(12, b"\0") - rom[172:176] = b"FQ01" + rom[172:176] = b"QST0" rom[176:178] = b"00" rom[178:189] = bytes([0x96]) + bytes(10) rom[189] = (-0x19 - sum(rom[160:189])) & 0xFF