Save adventure location with team and inventory

This commit is contained in:
LyokoAgents committed 2026-10-28 23:59:14 +00:00
1 parent 1141765231
commit 59c44c2b45
16 files changed
+444 -203

No files matched your search

+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 };
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 Room { OUTSIDE, COTTAGE, ROUTE, MELLON_GARDEN, MELLON_HOUSE, BEACH };
enum Facing { FACING_DOWN, FACING_UP, FACING_LEFT, FACING_RIGHT };
/* Room and facing IDs are persistent: never reorder or reuse them. */
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 { DIALOGUE_X = 8, DIALOGUE_Y = 96, DIALOGUE_W = 224, DIALOGUE_H = 44 };
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 Species { GOLDEN, FERNFOX, EMBERWING, PEBBLIT, TIDEFIN, SPECIES_COUNT };
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 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 use_door(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);
void scaled_creature(int x, int y, int scale, const u16 *palette);
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(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 { SAVE_MENU_X = 160, SAVE_MENU_Y = 6, SAVE_MENU_W = 74, SAVE_MENU_H = 148 };
struct SaveMenuState {
@@ -116,7 +124,7 @@ struct SaveMenuState {
unsigned previous;
};
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);
int has_caught(enum Species kind);
int near_npc(int x, int y);
@@ -130,6 +138,7 @@ void draw_team(void);
void draw_team_member(int member);
#ifdef GAME_TEST
unsigned save_crc(const unsigned char *bytes, int length);
extern const unsigned char large_alphabet[26][7];
extern u16 background_cache[WIDTH * HEIGHT];
extern enum Room background_room;
+6 -3
View File
@@ -70,9 +70,11 @@ int main(void)
int team_member = 0;
unsigned previous = 0;
start_movement_timer();
music_init(movement_time());
load_progress();
reset_follower(x, y);
struct Location start;
saved_location(&start);
enter_location(&start, &x, &y);
music_init(movement_time());
reset_dirty_rows();
draw_scene(x, y);
present_frame();
@@ -87,8 +89,9 @@ int main(void)
music_update(now);
if (pressed & R_BUTTON) music_toggle_mute();
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,
!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) {
pause_movement_at(now);
if (menu_changed) {
+5 -5
View File
@@ -2,7 +2,7 @@
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;
save_menu.previous = keys;
@@ -23,7 +23,7 @@ int save_menu_input(unsigned keys, int can_open)
}
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;
else save_menu.phase = save_progress(location) ? SAVE_MENU_SAVED : SAVE_MENU_FAILED;
return 1;
}
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);
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);
text(x + 8, y + 85, "SAVES TEAM", ink);
text(x + 8, y + 95, "ITEMS AND", ink);
text(x + 8, y + 105, "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));
+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];
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)
{
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)
{
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;
}
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. */
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;
}
door_open = 0;
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(*x, *y);
reset_arrival(*x, *y);
return 1;
}
@@ -159,9 +186,7 @@ int use_door(int *x, int *y)
}
door_open = 0;
player_facing = FACING_DOWN;
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(*x, *y);
reset_arrival(*x, *y);
return 1;
}
@@ -171,20 +196,24 @@ void reset_position(int *x, int *y)
*y = indoors() ? 119 : (current_room == ROUTE || current_room == BEACH) ? 60 :
current_room == MELLON_GARDEN ? 113 : 103;
player_facing = FACING_DOWN;
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(*x, *y);
reset_arrival(*x, *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 < 42 || x > 194) && y < 72) return 0; /* Palm trunks. */
if (x > 35 && x < 75 && y > 68 && y < 98) return 0; /* Umbrella and towel. */
return 1;
}
if (current_room == ROUTE) {
if (room == ROUTE) {
if (x < 32 || x > 207 || y < 47 || y > 131) return 0;
if (x > 143 && x < 216 && y < 85) return 0; /* Pond. */
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;
return 1;
}
if (indoors()) {
if (room == COTTAGE || room == MELLON_HOUSE) {
if (x < 34 || x > 205 || y < 75 || y > 131) return 0;
if (x > 26 && x < 77 && y < 105) return 0; /* Bed. */
if (x > 141 && x < 196 && y < 109) return 0; /* Table. */
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;
return 1;
}
if (x < 17 || x > 222 || y < 47 || y > 131) return 0;
if (x > 16 && x < 82 && y > 42 && y < 92) 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;
return 1;
}