#include "game.h" int partner_owned = 0; TEST_INTERNAL int gift_conversation = 0; int ball_count = 0, balls_given = 0; int rod_owned; static int rod_gift_conversation; int caught_count; 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 void load_progress(void) { 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; if (SAVE_MEMORY[0] != 'F' || SAVE_MEMORY[1] != 'Q' || SAVE_MEMORY[2] != 'S' || SAVE_MEMORY[3] != 'P') return; /* 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; } 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 (SAVE_MEMORY[4] == 2) { if (SAVE_MEMORY[8] != 0x5a || SAVE_MEMORY[7] != (0xa5 ^ 2 ^ flags ^ count)) return; } 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; 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; 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]; } else { return; } partner_owned = !!(flags & 1); balls_given = !!(flags & 2); rod_owned = !!(flags & 4); ball_count = (int)count; active_member = version == 5 ? (int)(flags >> 3) : 0; } 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; } const char *partner_name(void) { return active_member ? species[caught_species[active_member - 1]].name : "SPROUT"; } int partner_hp(void) { return active_member ? species[caught_species[active_member - 1]].hp : SPROUT_HP; } const char *partner_move(void) { if (!active_member) return "LEAF TAP"; switch (caught_species[active_member - 1]) { case GOLDEN: return "BUMP"; case FERNFOX: return "LEAF TAP"; case EMBERWING: return "SPARK"; case PEBBLIT: return "ROLL"; default: return "WATER JET"; } } void 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; /* 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)ball_count; SAVE_MEMORY[7] = (unsigned char)caught_count; for (int i = 0; i < CAUGHT_CAPACITY; ++i) { unsigned kind = i < caught_count ? caught_species[i] : 0xff; SAVE_MEMORY[8 + i] = (unsigned char)kind; checksum ^= kind; } SAVE_MEMORY[13] = (unsigned char)checksum; SAVE_MEMORY[14] = 0x5a; SAVE_MEMORY[0] = 'F'; } int has_caught(enum Species kind) { for (int i = 0; i < caught_count; ++i) if (caught_species[i] == (unsigned)kind) return 1; return 0; } /* Change these lines to write Mila's conversation! Use CAPITAL letters. */ TEST_INTERNAL const char *const dialogue[][2] = { {"HELLO I AM MILA", "WELCOME TO OUR VILLAGE"}, {"THAT GOLDEN CREATURE IS FRIENDLY", "WALK OVER AND SAY HELLO"} }; enum { STORY_DIALOGUE_PAGES = sizeof(dialogue) / sizeof(dialogue[0]) }; #ifdef GAME_TEST const int DIALOGUE_PAGES = STORY_DIALOGUE_PAGES; #endif #define DIALOGUE_PAGES STORY_DIALOGUE_PAGES TEST_INTERNAL const char *const professor_dialogue[DIALOGUE_PAGES][2] = { {"I HAVE A PARTNER FOR YOU", "MEET SPROUT A GRASS TYPE"}, {"YOU RECEIVED SPROUT", "TAKE GOOD CARE OF YOUR NEW FRIEND"} }; TEST_INTERNAL const char *const professor_followup[DIALOGUE_PAGES][2] = { {"HOW IS SPROUT DOING", "OPEN YOUR TEAM WITH SELECT"}, {"EXPLORE SUNPETAL TRAIL TOGETHER", "I AM GLAD YOU ARE FRIENDS"} }; TEST_INTERNAL const char *const ranger_dialogue[DIALOGUE_PAGES][2] = { {"HI I AM RANGER FINN", "HERE ARE FIVE POKE BALLS FOR YOU"}, {"YOU RECEIVED FIVE POKE BALLS", "PRESS SELECT IN BATTLE TO THROW"} }; TEST_INTERNAL const char *const ranger_followup[DIALOGUE_PAGES][2] = { {"WEAKEN WILD CREATURES FIRST", "LOW HEALTH MAKES CATCHING EASIER"}, {"OPEN YOUR TEAM WITH SELECT", "USE ARROWS TO SEE YOUR NEW FRIENDS"} }; static const char *const fisher_dialogue[2][2] = { {"HI I AM FISHER CORAL", "TAKE THIS POKE ROD FOR FISHING"}, {"YOU RECEIVED A POKE ROD", "FACE THE SEA AND PRESS A"} }; static const char *const fisher_followup[2][2] = { {"FACE THE SEA AND PRESS A", "BRING SPROUT TO BATTLE TIDEFIN"}, {"WEAKEN IT THEN THROW A BALL", "PRESS SELECT TO CATCH YOUR FISH"} }; int near_npc(int x, int y) { if (current_room == BEACH) return x >= FISHER_X - 18 && x <= FISHER_X + 18 && y >= FISHER_Y - 18 && y <= FISHER_Y + 12; if (current_room == ROUTE) return x >= RANGER_X - 24 && x <= RANGER_X + 24 && y >= RANGER_Y - 22 && y <= RANGER_Y + 22; if (current_room == MELLON_HOUSE) return x >= PROF_X - 24 && x <= PROF_X + 24 && y >= PROF_Y - 22 && y <= PROF_Y + 22; return current_room == OUTSIDE && x >= NPC_X - 24 && x <= NPC_X + 24 && y >= NPC_Y - 20 && y <= NPC_Y + 20; } TEST_INTERNAL int next_dialogue_page(int page) { return page + 1 < DIALOGUE_PAGES ? page + 1 : -1; } int begin_conversation(int x, int y) { if (!near_npc(x, y)) return 0; gift_conversation = current_room == MELLON_HOUSE && !partner_owned; ball_gift_conversation = current_room == ROUTE && !balls_given; rod_gift_conversation = current_room == BEACH && !rod_owned; return 1; } 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; } TEST_INTERNAL int receive_balls(void) { if (current_room != ROUTE || !ball_gift_conversation || balls_given) return 0; ball_count += 5; if (ball_count > 99) ball_count = 99; balls_given = 1; save_progress(); return 1; } int advance_conversation(int page) { int next = next_dialogue_page(page); if (next == 1 && gift_conversation) receive_partner(); 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; return next; } void dialogue_box(int page) { u16 ink = RGB(5, 8, 11); rectangle(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H, ink); rectangle(9, 97, 222, 42, RGB(15, 20, 21)); rectangle(11, 99, 218, 38, RGB(30, 30, 26)); rectangle(12, 100, 216, 1, RGB(31, 31, 30)); rectangle(16, 110, 208, 1, RGB(23, 26, 22)); int professor = current_room == MELLON_HOUSE; int ranger = current_room == ROUTE; const char *const (*lines)[2] = current_room == BEACH ? (rod_gift_conversation ? fisher_dialogue : fisher_followup) : ranger ? (ball_gift_conversation ? ranger_dialogue : ranger_followup) : professor ? (gift_conversation ? professor_dialogue : professor_followup) : dialogue; large_text(16, 102, current_room == BEACH ? "FISHER CORAL" : ranger ? "RANGER FINN" : professor ? "PROF MELLON" : "MILA", RGB(7, 16, 12)); text(192, 103, page == DIALOGUE_PAGES - 1 ? "A CLOSE" : "A NEXT", ink); large_text(16, 115, lines[page][0], ink); large_text(16, 127, lines[page][1], ink); }