Add manual adventure saves and a Start sidebar #12

Merged
rhett merged 3 commits from feature/manual-save-sidebar into main 2026-10-29 00:21:42 +00:00
16 changed files with 444 additions and 203 deletions
Showing only changes of commit 59c44c2b45 - Show all commits

No files matched your search

+2 -2
View File
@@ -4,11 +4,11 @@ CC := $(TOOL_PREFIX)gcc
OBJCOPY := $(TOOL_PREFIX)objcopy OBJCOPY := $(TOOL_PREFIX)objcopy
CFLAGS := -mcpu=arm7tdmi -marm -O2 -g -Wall -Wextra -Werror -ffreestanding -fno-builtin -fno-common -Wa,--noexecstack 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 \ 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/menu.c source/actors.c source/battle.c source/story.c source/hud.c source/audio.c source/beach.c source/menu.c source/save.c
GAME_OBJECTS := $(GAME_SOURCES:source/%.c=build/%.o) GAME_OBJECTS := $(GAME_SOURCES:source/%.c=build/%.o)
TEST_SOURCES := tests/test_game.c tests/test_support.c tests/test_maps.c \ 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_story.c tests/test_battle.c tests/test_rendering.c tests/test_movement.c tests/test_audio.c \
tests/test_catching.c tests/test_menu.c tests/test_progress.c tests/test_catching.c tests/test_menu.c tests/test_progress.c tests/test_location.c
.PHONY: all play check check-host .PHONY: all play check check-host
all: game.gba all: game.gba
+10 -5
View File
@@ -59,7 +59,8 @@ Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)`
- `source/world.c`: movement, collisions, doors, map travel, and following. - `source/world.c`: movement, collisions, doors, map travel, and following.
- `source/actors.c`: character and creature artwork and drawing. - `source/actors.c`: character and creature artwork and drawing.
- `source/battle.c`: species, battle rules, and the battle screen. - `source/battle.c`: species, battle rules, and the battle screen.
- `source/story.c`: conversations, gifts, and cartridge SRAM saves. - `source/story.c`: conversations and gifts.
- `source/save.c`: versioned cartridge SRAM records and validation.
- `source/hud.c`: map status, scene drawing, and the team screen. - `source/hud.c`: map status, scene drawing, and the team screen.
- `source/menu.c`: Save / Close sidebar input and drawing. - `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/audio.c` and `source/audio.h`: original chiptune scores and the GBA sound-channel sequencer.
@@ -80,11 +81,15 @@ Sprite artwork is decoded once into cached palette indices. Character and creatu
Movement has a separate 60 Hz fixed-step clock using cascaded GBA timers 2 and 3, independent of rendering. Walking is two pixels per tick (120 pixels/second), with normalized diagonal speed. Every pixel is collision-checked and recorded for Sprout and grass encounters, so faster movement does not skip walls or map exits or double the following distance. Missed updates can catch up by at most five ticks; older backlog is dropped after a long stall. Menus, conversations, battles, and map-loading time do not build up movement backlog. The existing footstep artwork is unchanged, with its phase now clocked by simulation ticks rather than rendered frames. Movement has a separate 60 Hz fixed-step clock using cascaded GBA timers 2 and 3, independent of rendering. Walking is two pixels per tick (120 pixels/second), with normalized diagonal speed. Every pixel is collision-checked and recorded for Sprout and grass encounters, so faster movement does not skip walls or map exits or double the following distance. Missed updates can catch up by at most five ticks; older backlog is dropped after a long stall. Menus, conversations, battles, and map-loading time do not build up movement backlog. The existing footstep artwork is unchanged, with its phase now clocked by simulation ticks rather than rendered frames.
## Saving your partner and items ## Saving your adventure
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. Only choosing **Save** in the Start sidebar writes cartridge SRAM, stored by mGBA alongside the ROM as `game.sav`. Save retains Sprout ownership, Finn's gift flag, remaining balls, the rod, caught species, your selected partner, map, position and facing. Gifts, throwing balls (successful or failed, including battle losses), partner selection, walking and map changes affect the current session only until you choose Save. There are no startup, exit, or gameplay autosaves. Battle state, health, conversations, open doors, follower trail, movement timing and music mute 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 sidebar states **SAVES TEAM / ITEMS AND / LOCATION**. **GAME / SAVED** means the complete v6 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 progress or location is rejected before writing. The single record uses CRC-16/CCITT-FALSE to detect corruption, not guarantee recovery or atomicity: interrupted writes or read-back failures can lose the previous record. Close mGBA normally, then reopen the same ROM without loading an emulator save state to check persistence. Preserve backups and use disposable ROM/save copies when testing.
Existing v1–v5 saves still load without changing their bytes; newly introduced fields receive defaults. These saves start outside in Greenleaf Village at (108,103), facing down. A v6 record with valid integrity and progress but an invalid/outdated location retains progress and uses that same default spawn. Invalid integrity, invalid progress or an unknown version rejects the whole record and starts with empty progress at the default spawn, without repairing or overwriting stored bytes. Only explicit Save writes/upgrades to v6. **Older builds cannot read v6**; do not use them with a newer save, as a subsequent save could overwrite your progress.
The v6 format is 19 bytes: `FQSP` (0–3), version 6 (4), v5 flags (5: Sprout bit 0, balls gift bit 1, rod bit 2, active member bits 3–5), ball count (6), caught count (7), five species IDs/unused `FF` (8–12), room (13), facing (14), x (15), y (16), then CRC high/low bytes (17–18). CRC covers bytes 0–16 with initial `FFFF`, polynomial `1021`, no reflection or final XOR. Byte 0 is invalidated first and committed as `F` last. Persistent room IDs are Outside 0, Cottage 1, Route 2, Mellon Garden 3, Mellon House 4, Beach 5; facing IDs are down 0, up 1, left 2, right 3. These IDs must not be reordered or reused.
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. 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.
@@ -125,4 +130,4 @@ We are making an original game. No existing Pokémon ROM or separate BIOS downlo
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. 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. All artwork and music are original; no Pokémon game assets or ROMs are included. Experience, mid-battle partner switching and sound effects are not implemented yet.
+14 -5
View File
@@ -11,15 +11,18 @@ enum { A = 1, B = 2, SELECT = 4, START = 8, RIGHT = 16, LEFT = 32, UP = 64, DOWN
R_BUTTON = 256 }; R_BUTTON = 256 };
enum { MOVEMENT_HZ = 60, MOVEMENT_CLOCK_HZ = 16384, WALK_SPEED = 2, MAX_MOVEMENT_TICKS = 5 }; enum { MOVEMENT_HZ = 60, MOVEMENT_CLOCK_HZ = 16384, WALK_SPEED = 2, MAX_MOVEMENT_TICKS = 5 };
enum { NPC_X = 146, NPC_Y = 106, PROF_X = 113, PROF_Y = 92, RANGER_X = 83, RANGER_Y = 97 }; enum { NPC_X = 146, NPC_Y = 106, PROF_X = 113, PROF_Y = 92, RANGER_X = 83, RANGER_Y = 97 };
enum Room { OUTSIDE, COTTAGE, ROUTE, MELLON_GARDEN, MELLON_HOUSE, BEACH }; /* Room and facing IDs are persistent: never reorder or reuse them. */
enum Facing { FACING_DOWN, FACING_UP, FACING_LEFT, FACING_RIGHT }; enum Room { OUTSIDE = 0, COTTAGE = 1, ROUTE = 2, MELLON_GARDEN = 3,
MELLON_HOUSE = 4, BEACH = 5, ROOM_COUNT = 6 };
enum Facing { FACING_DOWN = 0, FACING_UP = 1, FACING_LEFT = 2, FACING_RIGHT = 3 };
struct Location { int room, x, y, facing; };
enum CharacterStyle { BOY_STYLE, GIRL_STYLE }; enum CharacterStyle { BOY_STYLE, GIRL_STYLE };
enum { DIALOGUE_X = 8, DIALOGUE_Y = 96, DIALOGUE_W = 224, DIALOGUE_H = 44 }; enum { DIALOGUE_X = 8, DIALOGUE_Y = 96, DIALOGUE_W = 224, DIALOGUE_H = 44 };
enum { FOLLOW_STEPS = 14, SPROUT_HP = 24, GOLDEN_HP = 18 }; enum { FOLLOW_STEPS = 14, SPROUT_HP = 24, GOLDEN_HP = 18 };
enum BattlePhase { BATTLE_OFF, BATTLE_TURN, BATTLE_WON, BATTLE_LOST, BATTLE_FLED, BATTLE_CAUGHT }; enum BattlePhase { BATTLE_OFF, BATTLE_TURN, BATTLE_WON, BATTLE_LOST, BATTLE_FLED, BATTLE_CAUGHT };
enum Species { GOLDEN, FERNFOX, EMBERWING, PEBBLIT, TIDEFIN, SPECIES_COUNT }; enum Species { GOLDEN, FERNFOX, EMBERWING, PEBBLIT, TIDEFIN, SPECIES_COUNT };
enum { FISHER_X = 178, FISHER_Y = 80 }; enum { FISHER_X = 178, FISHER_Y = 80 };
enum { CAUGHT_CAPACITY = 5, SAVE_RECORD_SIZE = 15 }; enum { CAUGHT_CAPACITY = 5, SAVE_RECORD_SIZE = 19 };
enum BattleNotice { NOTICE_NONE, NOTICE_NO_BALLS, NOTICE_TEAM_FULL, NOTICE_BROKE_FREE }; enum BattleNotice { NOTICE_NONE, NOTICE_NO_BALLS, NOTICE_TEAM_FULL, NOTICE_BROKE_FREE };
enum WalkResult { WALK_NORMAL, WALK_MAP_CHANGED, WALK_ENCOUNTER }; enum WalkResult { WALK_NORMAL, WALK_MAP_CHANGED, WALK_ENCOUNTER };
@@ -95,6 +98,10 @@ int near_map_exit(int x, int y);
int near_route_sign(int x, int y); int near_route_sign(int x, int y);
int use_door(int *x, int *y); int use_door(int *x, int *y);
void reset_position(int *x, int *y); void reset_position(int *x, int *y);
int room_walkable(int room, int x, int y);
int location_valid(const struct Location *location);
void default_location(struct Location *out);
void enter_location(const struct Location *location, int *x, int *y);
enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks); enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks);
void scaled_creature(int x, int y, int scale, const u16 *palette); void scaled_creature(int x, int y, int scale, const u16 *palette);
void draw_wild_creature(enum Species enemy, int x, int y); void draw_wild_creature(enum Species enemy, int x, int y);
@@ -107,7 +114,8 @@ int battle_input(unsigned pressed);
void draw_battle_hud(void); void draw_battle_hud(void);
void draw_battle(void); void draw_battle(void);
void load_progress(void); void load_progress(void);
int save_progress(void); void saved_location(struct Location *out);
int save_progress(const struct Location *location);
enum SaveMenuPhase { SAVE_MENU_CLOSED, SAVE_MENU_OPTIONS, SAVE_MENU_SAVED, SAVE_MENU_FAILED }; 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 }; enum { SAVE_MENU_X = 160, SAVE_MENU_Y = 6, SAVE_MENU_W = 74, SAVE_MENU_H = 148 };
struct SaveMenuState { struct SaveMenuState {
@@ -116,7 +124,7 @@ struct SaveMenuState {
unsigned previous; unsigned previous;
}; };
extern struct SaveMenuState save_menu; extern struct SaveMenuState save_menu;
int save_menu_input(unsigned keys, int can_open); int save_menu_input(unsigned keys, int can_open, const struct Location *location);
void draw_save_menu(void); void draw_save_menu(void);
int has_caught(enum Species kind); int has_caught(enum Species kind);
int near_npc(int x, int y); int near_npc(int x, int y);
@@ -130,6 +138,7 @@ void draw_team(void);
void draw_team_member(int member); void draw_team_member(int member);
#ifdef GAME_TEST #ifdef GAME_TEST
unsigned save_crc(const unsigned char *bytes, int length);
extern const unsigned char large_alphabet[26][7]; extern const unsigned char large_alphabet[26][7];
extern u16 background_cache[WIDTH * HEIGHT]; extern u16 background_cache[WIDTH * HEIGHT];
extern enum Room background_room; extern enum Room background_room;
+6 -3
View File
@@ -70,9 +70,11 @@ int main(void)
int team_member = 0; int team_member = 0;
unsigned previous = 0; unsigned previous = 0;
start_movement_timer(); start_movement_timer();
music_init(movement_time());
load_progress(); load_progress();
reset_follower(x, y); struct Location start;
saved_location(&start);
enter_location(&start, &x, &y);
music_init(movement_time());
reset_dirty_rows(); reset_dirty_rows();
draw_scene(x, y); draw_scene(x, y);
present_frame(); present_frame();
@@ -87,8 +89,9 @@ int main(void)
music_update(now); music_update(now);
if (pressed & R_BUTTON) music_toggle_mute(); if (pressed & R_BUTTON) music_toggle_mute();
int menu_was_open = save_menu.phase != SAVE_MENU_CLOSED; int menu_was_open = save_menu.phase != SAVE_MENU_CLOSED;
const struct Location here = {current_room, x, y, player_facing};
int menu_changed = save_menu_input(keys, int menu_changed = save_menu_input(keys,
!team_open && battle.phase == BATTLE_OFF && dialogue_page < 0); !team_open && battle.phase == BATTLE_OFF && dialogue_page < 0, &here);
if (menu_was_open || save_menu.phase != SAVE_MENU_CLOSED) { if (menu_was_open || save_menu.phase != SAVE_MENU_CLOSED) {
pause_movement_at(now); pause_movement_at(now);
if (menu_changed) { if (menu_changed) {
+5 -5
View File
@@ -2,7 +2,7 @@
struct SaveMenuState save_menu; struct SaveMenuState save_menu;
int save_menu_input(unsigned keys, int can_open) int save_menu_input(unsigned keys, int can_open, const struct Location *location)
{ {
unsigned pressed = keys & ~save_menu.previous; unsigned pressed = keys & ~save_menu.previous;
save_menu.previous = keys; save_menu.previous = keys;
@@ -23,7 +23,7 @@ int save_menu_input(unsigned keys, int can_open)
} }
if (pressed & A) { if (pressed & A) {
if (save_menu.selection) save_menu.phase = SAVE_MENU_CLOSED; if (save_menu.selection) save_menu.phase = SAVE_MENU_CLOSED;
else save_menu.phase = save_progress() ? SAVE_MENU_SAVED : SAVE_MENU_FAILED; else save_menu.phase = save_progress(location) ? SAVE_MENU_SAVED : SAVE_MENU_FAILED;
return 1; return 1;
} }
unsigned direction = pressed & (UP | DOWN); unsigned direction = pressed & (UP | DOWN);
@@ -42,9 +42,9 @@ void draw_save_menu(void)
rectangle(x + 2, y + 2, SAVE_MENU_W - 4, SAVE_MENU_H - 4, cream); rectangle(x + 2, y + 2, SAVE_MENU_W - 4, SAVE_MENU_H - 4, cream);
large_text(x + 16, y + 10, "MENU", ink); large_text(x + 16, y + 10, "MENU", ink);
rectangle(x + 6, y + 23, SAVE_MENU_W - 12, 1, RGB(15, 24, 20)); 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 + 85, "SAVES TEAM", ink);
text(x + 8, y + 95, "ITEMS ONLY", ink); text(x + 8, y + 95, "ITEMS AND", ink);
text(x + 8, y + 105, "NO LOCATION", ink); text(x + 8, y + 105, "LOCATION", ink);
if (save_menu.phase == SAVE_MENU_OPTIONS) { if (save_menu.phase == SAVE_MENU_OPTIONS) {
int selected_y = y + 32 + save_menu.selection * 20; 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 + 6, selected_y, SAVE_MENU_W - 12, 16, RGB(23, 28, 21));
+131
View File
@@ -0,0 +1,131 @@
#include "game.h"
static const char save_signature[] __attribute__((used)) = "SRAM_V113";
#ifndef GAME_TEST
#define SAVE_MEMORY ((volatile unsigned char *)0x0e000000)
#endif
struct Progress {
int partner, given, rod, count, members, active;
unsigned char kinds[CAUGHT_CAPACITY];
};
static void clear_progress(struct Progress *p)
{
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;
}
/* CRC-16/CCITT-FALSE: init FFFF, polynomial 1021, high byte first. */
TEST_INTERNAL unsigned save_crc(const unsigned char *bytes, int length)
{
unsigned crc = 0xffff;
for (int i = 0; i < length; ++i) {
crc ^= (unsigned)bytes[i] << 8;
for (int bit = 0; bit < 8; ++bit)
crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021u : 0u)) & 0xffff;
}
return crc;
}
static int read_progress(struct Progress *p, struct Location *location)
{
unsigned char bytes[SAVE_RECORD_SIZE];
for (int i = 0; i < SAVE_RECORD_SIZE; ++i) bytes[i] = SAVE_MEMORY[i];
clear_progress(p);
default_location(location);
if (bytes[0] != 'F' || bytes[1] != 'Q' || bytes[2] != 'S' || bytes[3] != 'P') return 0;
/* Keep Sprout from the original version-one save format. */
if (bytes[4] == 1) {
p->partner = bytes[5] == 1 && bytes[6] == 0xa5 && bytes[7] == 0x5a;
return p->partner && valid_progress(p);
}
unsigned flags = bytes[5], count = bytes[6], version = bytes[4];
if (flags > (version >= 5 ? 47u : version == 4 ? 7u : 3u)) return 0;
if (version == 2) {
if (bytes[8] != 0x5a || bytes[7] != (0xa5 ^ 2 ^ flags ^ count)) return 0;
} else if (version >= 3 && version <= 6) {
unsigned checksum = 0xa5 ^ version ^ flags ^ count ^ bytes[7];
for (int i = 0; i < CAUGHT_CAPACITY; ++i) {
p->kinds[i] = bytes[8 + i];
checksum ^= bytes[8 + i];
}
if (version == 6) {
if (save_crc(bytes, 17) != ((unsigned)bytes[17] << 8 | bytes[18])) return 0;
location->room = bytes[13]; location->facing = bytes[14];
location->x = bytes[15]; location->y = bytes[16];
} else if (bytes[14] != 0x5a || bytes[13] != checksum) return 0;
p->members = bytes[7];
} else return 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;
struct Location location;
if (!read_progress(&p, &location)) 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];
}
void saved_location(struct Location *out)
{
struct Progress p;
if (!read_progress(&p, out) || !location_valid(out)) default_location(out);
}
int save_progress(const struct Location *location)
{
struct Progress p, readback;
struct Location restored;
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) || !location_valid(location)) return 0;
unsigned char bytes[SAVE_RECORD_SIZE];
bytes[0] = 'F'; bytes[1] = 'Q'; bytes[2] = 'S'; bytes[3] = 'P'; bytes[4] = 6;
bytes[5] = (unsigned char)(p.partner | (p.given << 1) | (p.rod << 2) | (p.active << 3));
bytes[6] = (unsigned char)p.count; bytes[7] = (unsigned char)p.members;
for (int i = 0; i < CAUGHT_CAPACITY; ++i) bytes[8 + i] = p.kinds[i];
bytes[13] = (unsigned char)location->room; bytes[14] = (unsigned char)location->facing;
bytes[15] = (unsigned char)location->x; bytes[16] = (unsigned char)location->y;
unsigned crc = save_crc(bytes, 17);
bytes[17] = (unsigned char)(crc >> 8); bytes[18] = (unsigned char)crc;
/* Invalidate first, commit last; a single record cannot recover interrupted writes. */
SAVE_MEMORY[0] = 0;
for (int i = 1; i < SAVE_RECORD_SIZE; ++i) SAVE_MEMORY[i] = bytes[i];
SAVE_MEMORY[0] = 'F';
if (!read_progress(&readback, &restored) || p.partner != readback.partner || p.given != readback.given ||
p.rod != readback.rod || p.count != readback.count || p.members != readback.members ||
p.active != readback.active || location->room != restored.room ||
location->x != restored.x || location->y != restored.y || location->facing != restored.facing) return 0;
for (int i = 0; i < CAUGHT_CAPACITY; ++i)
if (p.kinds[i] != readback.kinds[i]) return 0;
return 1;
}
-117
View File
@@ -10,83 +10,6 @@ int active_member;
unsigned char caught_species[CAUGHT_CAPACITY]; unsigned char caught_species[CAUGHT_CAPACITY];
TEST_INTERNAL int ball_gift_conversation = 0; TEST_INTERNAL int ball_gift_conversation = 0;
/* mGBA detects this marker and provides cartridge SRAM for the gift record. */
static const char save_signature[] __attribute__((used)) = "SRAM_V113";
#ifndef GAME_TEST
#define SAVE_MEMORY ((volatile unsigned char *)0x0e000000)
#endif
struct Progress {
int partner, given, rod, count, members, active;
unsigned char kinds[CAUGHT_CAPACITY];
};
static void clear_progress(struct Progress *p)
{
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 0;
/* Keep Sprout from the original version-one save format. */
if (SAVE_MEMORY[4] == 1) {
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)) return 0;
if (SAVE_MEMORY[4] == 2) {
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 (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];
p->kinds[i] = (unsigned char)kind;
checksum ^= kind;
}
if (SAVE_MEMORY[13] != checksum) return 0;
p->members = (int)members;
} else {
return 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) int choose_partner(int member)
{ {
if (!partner_owned || battle.phase != BATTLE_OFF || member < 0 || member > caught_count) if (!partner_owned || battle.phase != BATTLE_OFF || member < 0 || member > caught_count)
@@ -117,46 +40,6 @@ const char *partner_move(void)
} }
} }
int save_progress(void)
{
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';
SAVE_MEMORY[2] = 'S';
SAVE_MEMORY[3] = 'P';
SAVE_MEMORY[4] = 5;
SAVE_MEMORY[5] = (unsigned char)flags;
SAVE_MEMORY[6] = (unsigned char)p.count;
SAVE_MEMORY[7] = (unsigned char)p.members;
for (int i = 0; i < CAUGHT_CAPACITY; ++i) {
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) int has_caught(enum Species kind)
{ {
for (int i = 0; i < caught_count; ++i) for (int i = 0; i < caught_count; ++i)
+43 -14
View File
@@ -87,6 +87,35 @@ int near_map_exit(int x, int y)
return 0; return 0;
} }
static void reset_arrival(int x, int y)
{
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(x, y);
}
void default_location(struct Location *out)
{
out->room = OUTSIDE; out->x = 108; out->y = 103; out->facing = FACING_DOWN;
}
int location_valid(const struct Location *location)
{
return location && location->room >= OUTSIDE && location->room < ROOM_COUNT &&
location->facing >= FACING_DOWN && location->facing <= FACING_RIGHT &&
location->x >= 0 && location->x < WIDTH && location->y >= 0 && location->y < HEIGHT &&
room_walkable(location->room, location->x, location->y);
}
void enter_location(const struct Location *location, int *x, int *y)
{
current_room = (enum Room)location->room;
*x = location->x; *y = location->y;
player_facing = (enum Facing)location->facing;
door_open = 0;
reset_arrival(*x, *y);
}
/* Walk across a map boundary. Arrivals are away from the return trigger. */ /* Walk across a map boundary. Arrivals are away from the return trigger. */
TEST_INTERNAL int use_map_exit(int *x, int *y, unsigned keys) TEST_INTERNAL int use_map_exit(int *x, int *y, unsigned keys)
{ {
@@ -125,9 +154,7 @@ TEST_INTERNAL int use_map_exit(int *x, int *y, unsigned keys)
return 0; return 0;
} }
door_open = 0; door_open = 0;
grass_steps = 0; reset_arrival(*x, *y);
diagonal_fraction = 0;
reset_follower(*x, *y);
return 1; return 1;
} }
@@ -159,9 +186,7 @@ int use_door(int *x, int *y)
} }
door_open = 0; door_open = 0;
player_facing = FACING_DOWN; player_facing = FACING_DOWN;
grass_steps = 0; reset_arrival(*x, *y);
diagonal_fraction = 0;
reset_follower(*x, *y);
return 1; return 1;
} }
@@ -171,20 +196,24 @@ void reset_position(int *x, int *y)
*y = indoors() ? 119 : (current_room == ROUTE || current_room == BEACH) ? 60 : *y = indoors() ? 119 : (current_room == ROUTE || current_room == BEACH) ? 60 :
current_room == MELLON_GARDEN ? 113 : 103; current_room == MELLON_GARDEN ? 113 : 103;
player_facing = FACING_DOWN; player_facing = FACING_DOWN;
grass_steps = 0; reset_arrival(*x, *y);
diagonal_fraction = 0;
reset_follower(*x, *y);
} }
TEST_INTERNAL int can_walk(int x, int y) TEST_INTERNAL int can_walk(int x, int y)
{ {
if (current_room == BEACH) { return room_walkable(current_room, x, y);
}
int room_walkable(int room, int x, int y)
{
if (room < OUTSIDE || room >= ROOM_COUNT) return 0;
if (room == BEACH) {
if (x < 17 || x > 222 || y < 47 || y > beach_shoreline(x) - 5) return 0; if (x < 17 || x > 222 || y < 47 || y > beach_shoreline(x) - 5) return 0;
if ((x < 42 || x > 194) && y < 72) return 0; /* Palm trunks. */ if ((x < 42 || x > 194) && y < 72) return 0; /* Palm trunks. */
if (x > 35 && x < 75 && y > 68 && y < 98) return 0; /* Umbrella and towel. */ if (x > 35 && x < 75 && y > 68 && y < 98) return 0; /* Umbrella and towel. */
return 1; return 1;
} }
if (current_room == ROUTE) { if (room == ROUTE) {
if (x < 32 || x > 207 || y < 47 || y > 131) return 0; if (x < 32 || x > 207 || y < 47 || y > 131) return 0;
if (x > 143 && x < 216 && y < 85) return 0; /* Pond. */ if (x > 143 && x < 216 && y < 85) return 0; /* Pond. */
if (x > 116 && x < 154 && y > 53 && y < 79) return 0; /* Sign. */ if (x > 116 && x < 154 && y > 53 && y < 79) return 0; /* Sign. */
@@ -193,19 +222,19 @@ TEST_INTERNAL int can_walk(int x, int y)
y > RANGER_Y - 9 && y < RANGER_Y + 9) return 0; y > RANGER_Y - 9 && y < RANGER_Y + 9) return 0;
return 1; return 1;
} }
if (indoors()) { if (room == COTTAGE || room == MELLON_HOUSE) {
if (x < 34 || x > 205 || y < 75 || y > 131) return 0; if (x < 34 || x > 205 || y < 75 || y > 131) return 0;
if (x > 26 && x < 77 && y < 105) return 0; /* Bed. */ if (x > 26 && x < 77 && y < 105) return 0; /* Bed. */
if (x > 141 && x < 196 && y < 109) return 0; /* Table. */ if (x > 141 && x < 196 && y < 109) return 0; /* Table. */
if (x > 187 && y < 85) return 0; /* Plant. */ if (x > 187 && y < 85) return 0; /* Plant. */
if (current_room == MELLON_HOUSE && x > PROF_X - 13 && x < PROF_X + 13 && if (room == MELLON_HOUSE && x > PROF_X - 13 && x < PROF_X + 13 &&
y > PROF_Y - 9 && y < PROF_Y + 9) return 0; y > PROF_Y - 9 && y < PROF_Y + 9) return 0;
return 1; return 1;
} }
if (x < 17 || x > 222 || y < 47 || y > 131) return 0; if (x < 17 || x > 222 || y < 47 || y > 131) return 0;
if (x > 16 && x < 82 && y > 42 && y < 92) return 0; if (x > 16 && x < 82 && y > 42 && y < 92) return 0;
if (x > 141 && x < 212 && y < 78) return 0; if (x > 141 && x < 212 && y < 78) return 0;
if (current_room == OUTSIDE && x > NPC_X - 13 && x < NPC_X + 13 && if (room == OUTSIDE && x > NPC_X - 13 && x < NPC_X + 13 &&
y > NPC_Y - 9 && y < NPC_Y + 9) return 0; y > NPC_Y - 9 && y < NPC_Y + 9) return 0;
return 1; return 1;
} }
+6 -10
View File
@@ -64,13 +64,11 @@ void check_partner_selection(void)
memcpy(saved, test_save, sizeof(saved)); memcpy(saved, test_save, sizeof(saved));
/* Reject an out-of-range selection even with a correct checksum. */ /* Reject an out-of-range selection even with a correct checksum. */
test_save[5] ^= (5 ^ 6) << 3; test_save[5] ^= (5 ^ 6) << 3;
test_save[13] ^= (5 ^ 6) << 3; checksum_fixture();
load_progress(); load_progress();
assert(!partner_owned && active_member == 0); assert(!partner_owned && active_member == 0);
memcpy(test_save, saved, sizeof(saved)); memcpy(test_save, saved, sizeof(saved));
test_save[4] = 4; legacy_fixture(4);
test_save[5] &= 7;
test_save[13] ^= 5 ^ 4 ^ (5 << 3);
load_progress(); load_progress();
assert(partner_owned && rod_owned && caught_count == 5 && active_member == 0); assert(partner_owned && rod_owned && caught_count == 5 && active_member == 0);
memset(test_save, 0xff, sizeof(test_save)); memset(test_save, 0xff, sizeof(test_save));
@@ -138,9 +136,7 @@ void check_fishing(void)
assert(cast_rod(x, y)); assert(cast_rod(x, y));
assert(battle_input(B) && battle_input(A)); assert(battle_input(B) && battle_input(A));
/* An old save keeps catches and balls without granting a rod. */ /* An old save keeps catches and balls without granting a rod. */
test_save[4] = 3; legacy_fixture(3);
test_save[5] &= (unsigned char)~4;
test_save[13] ^= 5 ^ 3 ^ 4;
load_progress(); load_progress();
assert(!rod_owned && has_caught(TIDEFIN) && ball_count == 4); assert(!rod_owned && has_caught(TIDEFIN) && ball_count == 4);
memset(test_save, 0xff, sizeof(test_save)); memset(test_save, 0xff, sizeof(test_save));
@@ -265,17 +261,17 @@ void check_catching(void)
} }
memcpy(test_save, before, sizeof(before)); memcpy(test_save, before, sizeof(before));
test_save[7] = 6; test_save[7] = 6;
test_save[13] ^= 5 ^ 6; /* Valid checksum, invalid party length. */ checksum_fixture(); /* Valid CRC, invalid party length. */
load_progress(); load_progress();
assert(!partner_owned && caught_count == 0); assert(!partner_owned && caught_count == 0);
memcpy(test_save, before, sizeof(before)); memcpy(test_save, before, sizeof(before));
test_save[8] = SPECIES_COUNT; test_save[8] = SPECIES_COUNT;
test_save[13] ^= FERNFOX ^ SPECIES_COUNT; checksum_fixture();
load_progress(); load_progress();
assert(!partner_owned && caught_count == 0); assert(!partner_owned && caught_count == 0);
memcpy(test_save, before, sizeof(before)); memcpy(test_save, before, sizeof(before));
test_save[5] &= (unsigned char)~1; test_save[5] &= (unsigned char)~1;
test_save[13] ^= 1; checksum_fixture();
load_progress(); load_progress();
assert(!partner_owned && caught_count == 0); /* No captured team without Sprout. */ assert(!partner_owned && caught_count == 0); /* No captured team without Sprout. */
+1
View File
@@ -210,6 +210,7 @@ int main(void)
check_fishing(); check_fishing();
check_partner_selection(); check_partner_selection();
check_progress(); check_progress();
check_location_save();
check_save_menu(); check_save_menu();
assert(test_pixels[0] == 0xbeef); assert(test_pixels[0] == 0xbeef);
assert(test_pixels[WIDTH * HEIGHT + 1] == 0xbeef); assert(test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
+142
View File
@@ -0,0 +1,142 @@
#include "test_support.h"
static void same_location(const struct Location *a, const struct Location *b)
{
assert(a->room == b->room && a->x == b->x && a->y == b->y && a->facing == b->facing);
}
static void team_loaded(void)
{
assert(partner_owned && balls_given && rod_owned && ball_count == 3 &&
caught_count == 2 && active_member == 2);
assert(caught_species[0] == FERNFOX && caught_species[1] == TIDEFIN);
for (int i = 2; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
}
void check_location_save(void)
{
assert(save_crc((const unsigned char *)"123456789", 9) == 0x29b1);
struct Location fallback, restored;
default_location(&fallback);
assert(location_valid(&fallback));
memset(test_save, 0xff, sizeof(test_save));
load_progress();
partner_owned = balls_given = rod_owned = 1;
ball_count = 3; caught_count = active_member = 2;
caught_species[0] = FERNFOX; caught_species[1] = TIDEFIN;
struct Location beach = {BEACH, 120, 100, FACING_RIGHT};
save_at_menu(&beach, SAVE_MENU_SAVED);
/* Golden bytes calculated independently with Python binascii.crc_hqx(..., 0xffff). */
const unsigned char golden[] = {'F','Q','S','P',6,0x17,3,2,1,4,255,255,255,5,3,120,100,0xa1,0xa4};
assert(memcmp(test_save, golden, sizeof(golden)) == 0);
unsigned char saved[sizeof(test_save)], fixture[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
const int positions[ROOM_COUNT][2][2] = {
{{108,103},{222,131}}, {{120,119},{80,75}}, {{108,60},{207,131}},
{{31,113},{177,83}}, {{120,119},{80,75}}, {{108,60},{120,103}}
};
for (int room = 0; room < ROOM_COUNT; ++room) {
current_room = (enum Room)room;
int x, y;
reset_position(&x, &y);
struct Location spawn = {room, x, y, FACING_DOWN};
assert(location_valid(&spawn));
for (int edge = 0; edge < 2; ++edge)
for (int facing = FACING_DOWN; facing <= FACING_RIGHT; ++facing) {
struct Location here = {room, positions[room][edge][0], positions[room][edge][1], facing};
assert(location_valid(&here));
save_at_menu(&here, SAVE_MENU_SAVED);
partner_owned = balls_given = rod_owned = caught_count = active_member = ball_count = 0;
load_progress();
team_loaded();
saved_location(&restored);
same_location(&here, &restored);
door_open = grass_steps = diagonal_fraction = 7;
follower_ready = 0;
enter_location(&restored, &x, &y);
assert((int)current_room == room && x == here.x && y == here.y && (int)player_facing == facing);
assert(!door_open && !grass_steps && !diagonal_fraction && follower_ready && !trail_head);
assert(follower_x == x && follower_y == y);
move_follower(x, y, x + 1, y);
assert(follower_x == x && follower_y == y);
}
}
/* Walking, turning, doors, map exits and unsaved inventory cannot replace A. */
save_at_menu(&fallback, SAVE_MENU_SAVED);
memcpy(fixture, test_save, sizeof(fixture));
int x, y;
enter_location(&fallback, &x, &y);
walk_movement(RIGHT, &x, &y, 2);
ball_count = 1; rod_owned = 0; active_member = 0;
x = 177; y = 83; assert(use_door(&x, &y));
y = 125; assert(use_door(&x, &y));
x = 108; y = 131; assert(use_map_exit(&x, &y, DOWN));
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
load_progress(); team_loaded(); saved_location(&restored); same_location(&fallback, &restored);
for (int version = 1; version <= 5; ++version) {
memcpy(test_save, saved, sizeof(saved));
legacy_fixture(version);
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(partner_owned && balls_given == (version >= 2) && ball_count == (version >= 2 ? 3 : 0));
assert(rod_owned == (version >= 4) && caught_count == (version >= 3 ? 2 : 0));
assert(active_member == (version == 5 ? 2 : 0));
for (int i = 0; i < CAUGHT_CAPACITY; ++i)
assert(caught_species[i] == (version >= 3 && i < 2 ? saved[8 + i] : 0xff));
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
for (int damage = 0; damage < SAVE_RECORD_SIZE * 2; ++damage) {
memcpy(test_save, saved, sizeof(saved));
if (damage < SAVE_RECORD_SIZE) test_save[damage] ^= 1;
else if (damage == SAVE_RECORD_SIZE) { test_save[4] = 7; checksum_fixture(); }
else {
int cut = damage - SAVE_RECORD_SIZE;
memset(test_save + cut, 0xff, sizeof(test_save) - (unsigned)cut);
}
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(!partner_owned && !balls_given && !rod_owned && !ball_count && !caught_count && !active_member);
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
const unsigned char bad_progress[][2] = {{5,0x57}, {6,100}, {7,6}, {8,SPECIES_COUNT}, {12,0}, {5,0x1f}};
for (unsigned i = 0; i < sizeof(bad_progress) / sizeof(bad_progress[0]); ++i) {
memcpy(test_save, saved, sizeof(saved));
test_save[bad_progress[i][0]] = bad_progress[i][1];
checksum_fixture();
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(!partner_owned && !caught_count && !ball_count);
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
const struct Location invalid[] = {
{ROOM_COUNT,108,103,0}, {-1,108,103,0}, {OUTSIDE,108,103,4}, {OUTSIDE,108,103,-1},
{256,108,103,0}, {OUTSIDE,108,103,256},
{OUTSIDE,-1,103,0}, {OUTSIDE,256,103,0}, {OUTSIDE,108,-1,0}, {OUTSIDE,108,256,0},
{OUTSIDE,255,103,0}, {OUTSIDE,108,255,0}, {COTTAGE,25,120,0}, {BEACH,120,130,0},
{ROUTE,180,60,0}, {OUTSIDE,NPC_X,NPC_Y,0}, {MELLON_HOUSE,PROF_X,PROF_Y,0}
};
for (unsigned i = 0; i < sizeof(invalid) / sizeof(invalid[0]); ++i) {
assert(!location_valid(&invalid[i]));
memcpy(test_save, saved, sizeof(saved));
test_save[13] = (unsigned char)invalid[i].room; test_save[14] = (unsigned char)invalid[i].facing;
test_save[15] = (unsigned char)invalid[i].x; test_save[16] = (unsigned char)invalid[i].y;
checksum_fixture();
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored); team_loaded(); same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
memcpy(test_save, saved, sizeof(saved));
enter_location(&beach, &x, &y);
struct Location bad = invalid[i];
save_at_menu(&bad, SAVE_MENU_FAILED);
same_location(&bad, &invalid[i]); team_loaded();
assert(current_room == BEACH && player_facing == FACING_RIGHT && x == beach.x && y == beach.y);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
}
memset(test_save, 0xff, sizeof(test_save));
load_progress(); enter_location(&fallback, &x, &y);
puts("Location saves: v6 CRC, all rooms/facings, legacy reads, fallback and no-autosave checks passed");
}
+20 -20
View File
@@ -1,6 +1,6 @@
#include "test_support.h" #include "test_support.h"
static void release_keys(void) { assert(!save_menu_input(0, 1)); } static void release_keys(void) { assert(!menu_input(0, 1)); }
void check_save_menu(void) void check_save_menu(void)
{ {
@@ -9,45 +9,45 @@ void check_save_menu(void)
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0}; save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
for (int mode = 0; mode < 3; ++mode) { for (int mode = 0; mode < 3; ++mode) {
release_keys(); release_keys();
assert(!save_menu_input(START, 0)); assert(!menu_input(START, 0));
assert(!save_menu_input(START, 1)); assert(!menu_input(START, 1));
} }
release_keys(); release_keys();
assert(save_menu_input(START | A, 1)); assert(menu_input(START | A, 1));
assert(save_menu.phase == SAVE_MENU_OPTIONS && save_menu.selection == 0); assert(save_menu.phase == SAVE_MENU_OPTIONS && save_menu.selection == 0);
assert(!save_menu_input(A, 1)); assert(!menu_input(A, 1));
release_keys(); release_keys();
assert(save_menu_input(DOWN, 1) && save_menu.selection == 1); assert(menu_input(DOWN, 1) && save_menu.selection == 1);
assert(!save_menu_input(DOWN, 1)); assert(!menu_input(DOWN, 1));
release_keys(); release_keys();
assert(!save_menu_input(UP | DOWN, 1)); assert(!menu_input(UP | DOWN, 1));
release_keys(); release_keys();
assert(save_menu_input(UP, 1) && save_menu.selection == 0); assert(menu_input(UP, 1) && save_menu.selection == 0);
release_keys(); release_keys();
assert(save_menu_input(UP, 1) && save_menu.selection == 1); assert(menu_input(UP, 1) && save_menu.selection == 1);
release_keys(); release_keys();
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_CLOSED); assert(menu_input(A, 1) && save_menu.phase == SAVE_MENU_CLOSED);
const unsigned cancel[] = {B, START}; const unsigned cancel[] = {B, START};
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
release_keys(); release_keys();
assert(save_menu_input(START, 1)); assert(menu_input(START, 1));
release_keys(); release_keys();
assert(save_menu_input(cancel[i], 1) && save_menu.phase == SAVE_MENU_CLOSED); assert(menu_input(cancel[i], 1) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!save_menu_input(cancel[i], 1)); assert(!menu_input(cancel[i], 1));
} }
assert(memcmp(saved, test_save, sizeof(saved)) == 0); assert(memcmp(saved, test_save, sizeof(saved)) == 0);
release_keys(); release_keys();
assert(save_menu_input(START, 1)); assert(menu_input(START, 1));
release_keys(); release_keys();
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_SAVED); assert(menu_input(A, 1) && save_menu.phase == SAVE_MENU_SAVED);
test_save[0] = 0; test_save[0] = 0;
for (int i = 0; i < 10; ++i) assert(!save_menu_input(A, 1)); for (int i = 0; i < 10; ++i) assert(!menu_input(A, 1));
assert(test_save[0] == 0 && save_menu.phase == SAVE_MENU_SAVED); assert(test_save[0] == 0 && save_menu.phase == SAVE_MENU_SAVED);
release_keys(); release_keys();
assert(!save_menu_input(DOWN, 1)); assert(!menu_input(DOWN, 1));
release_keys(); release_keys();
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_CLOSED); assert(menu_input(A, 1) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!save_menu_input(A, 1)); assert(!menu_input(A, 1));
memcpy(test_save, saved, sizeof(saved)); memcpy(test_save, saved, sizeof(saved));
static u16 expected[WIDTH * HEIGHT]; static u16 expected[WIDTH * HEIGHT];
+9 -13
View File
@@ -12,14 +12,12 @@ void check_progress(void)
load_progress(); load_progress();
assert_empty_progress(); assert_empty_progress();
save_from_menu(); save_from_menu();
const unsigned char empty[] = {'F','Q','S','P',5,0,0,0,255,255,255,255,255,0x5f,0x5a}; const unsigned char empty[] = {'F','Q','S','P',6,0,0,0,255,255,255,255,255,0,0,108,103,0xde,0x32};
assert(memcmp(test_save, empty, sizeof(empty)) == 0); assert(memcmp(test_save, empty, sizeof(empty)) == 0);
partner_owned = balls_given = 1; partner_owned = balls_given = 1;
ball_count = 5; ball_count = 5;
save_from_menu(); save_from_menu();
const unsigned char gifts[] = {'F','Q','S','P',5,3,5,0,255,255,255,255,255,0x59,0x5a}; /* An unsaved expanded team and rod roll back to the previous snapshot. */
assert(memcmp(test_save, gifts, sizeof(gifts)) == 0);
/* An unsaved expanded team and rod roll back to the previous v5 snapshot. */
rod_owned = 1; rod_owned = 1;
caught_count = active_member = 1; caught_count = active_member = 1;
caught_species[0] = TIDEFIN; caught_species[0] = TIDEFIN;
@@ -32,8 +30,6 @@ void check_progress(void)
caught_species[1] = TIDEFIN; caught_species[1] = TIDEFIN;
ball_count = 3; ball_count = 3;
save_from_menu(); 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)]; unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved)); memcpy(saved, test_save, sizeof(saved));
partner_owned = balls_given = rod_owned = caught_count = active_member = ball_count = 0; partner_owned = balls_given = rod_owned = caught_count = active_member = ball_count = 0;
@@ -58,17 +54,17 @@ void check_progress(void)
int state[] = {partner_owned, balls_given, rod_owned, ball_count, caught_count, active_member}; int state[] = {partner_owned, balls_given, rod_owned, ball_count, caught_count, active_member};
unsigned char kinds[CAUGHT_CAPACITY]; unsigned char kinds[CAUGHT_CAPACITY];
memcpy(kinds, caught_species, sizeof(kinds)); memcpy(kinds, caught_species, sizeof(kinds));
assert(!save_menu_input(0, 1)); assert(!menu_input(0, 1));
assert(save_menu_input(START, 1)); assert(menu_input(START, 1));
assert(!save_menu_input(0, 1)); assert(!menu_input(0, 1));
assert(save_menu_input(A, 1) && save_menu.phase == SAVE_MENU_FAILED); assert(menu_input(A, 1) && save_menu.phase == SAVE_MENU_FAILED);
int after[] = {partner_owned, balls_given, rod_owned, ball_count, caught_count, active_member}; int after[] = {partner_owned, balls_given, rod_owned, ball_count, caught_count, active_member};
assert(memcmp(state, after, sizeof(state)) == 0); assert(memcmp(state, after, sizeof(state)) == 0);
assert(memcmp(kinds, caught_species, sizeof(kinds)) == 0); assert(memcmp(kinds, caught_species, sizeof(kinds)) == 0);
assert(memcmp(saved, test_save, sizeof(saved)) == 0); assert(memcmp(saved, test_save, sizeof(saved)) == 0);
assert(!save_menu_input(A, 1)); assert(!menu_input(A, 1));
assert(!save_menu_input(0, 1)); assert(!menu_input(0, 1));
assert(save_menu_input(B, 1)); assert(menu_input(B, 1));
} }
load_progress(); load_progress();
partner_owned = 2; balls_given = 7; rod_owned = -1; partner_owned = 2; balls_given = 7; rod_owned = -1;
+2 -2
View File
@@ -148,7 +148,7 @@ void check_ball_gift(void)
assert(begin_conversation(99, 97)); assert(begin_conversation(99, 97));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1); assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu(); save_from_menu();
assert(test_save[4] == 5); assert(test_save[4] == 6);
unsigned char saved[sizeof(test_save)]; unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved)); memcpy(saved, test_save, sizeof(saved));
for (int visit = 0; visit < 10; ++visit) { for (int visit = 0; visit < 10; ++visit) {
@@ -194,7 +194,7 @@ void check_ball_gift(void)
} }
memcpy(test_save, saved, sizeof(saved)); memcpy(test_save, saved, sizeof(saved));
test_save[6] = 100; test_save[6] = 100;
test_save[13] ^= (unsigned char)(5 ^ 100); /* Keep checksum valid; count itself is invalid. */ checksum_fixture(); /* Keep CRC valid; count itself is invalid. */
load_progress(); load_progress();
assert(!partner_owned && !balls_given && ball_count == 0); assert(!partner_owned && !balls_given && ball_count == 0);
for (int page = 0; page < DIALOGUE_PAGES; ++page) for (int page = 0; page < DIALOGUE_PAGES; ++page)
+48 -7
View File
@@ -5,13 +5,54 @@ unsigned char test_save[32];
void save_from_menu(void) void save_from_menu(void)
{ {
assert(!save_menu_input(0, 1)); struct Location location;
assert(save_menu_input(START, 1) && save_menu.phase == SAVE_MENU_OPTIONS); default_location(&location);
assert(!save_menu_input(0, 1)); save_at_menu(&location, SAVE_MENU_SAVED);
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); int menu_input(unsigned keys, int can_open)
assert(!save_menu_input(0, 1)); {
struct Location location;
default_location(&location);
return save_menu_input(keys, can_open, &location);
}
void save_at_menu(const struct Location *location, enum SaveMenuPhase expected)
{
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(START, 1, location) && save_menu.phase == SAVE_MENU_OPTIONS);
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(A, 1, location) && save_menu.phase == expected);
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(B, 1, location) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!save_menu_input(0, 1, location));
}
void checksum_fixture(void)
{
if (test_save[4] == 6) {
unsigned crc = save_crc(test_save, 17);
test_save[17] = (unsigned char)(crc >> 8);
test_save[18] = (unsigned char)crc;
} else {
unsigned checksum = 0xa5 ^ test_save[4] ^ test_save[5] ^ test_save[6];
if (test_save[4] >= 3) {
checksum ^= test_save[7];
for (int i = 0; i < CAUGHT_CAPACITY; ++i) checksum ^= test_save[8 + i];
}
test_save[test_save[4] == 2 ? 7 : 13] = (unsigned char)checksum;
test_save[test_save[4] == 2 ? 8 : 14] = 0x5a;
}
}
/* Convert a fixture, never player data, retaining only fields supported by that version. */
void legacy_fixture(int version)
{
test_save[4] = (unsigned char)version;
test_save[5] &= version == 5 ? 63 : version == 4 ? 7 : 3;
if (version == 1) {
test_save[5] = 1; test_save[6] = 0xa5; test_save[7] = 0x5a;
} else checksum_fixture();
} }
void render_scene(int x, int y) void render_scene(int x, int y)
+5
View File
@@ -30,5 +30,10 @@ void check_partner_selection(void);
void check_save_menu(void); void check_save_menu(void);
void check_progress(void); void check_progress(void);
void save_from_menu(void); void save_from_menu(void);
int menu_input(unsigned keys, int can_open);
void save_at_menu(const struct Location *location, enum SaveMenuPhase expected);
void legacy_fixture(int version);
void checksum_fixture(void);
void check_location_save(void);
#endif #endif