Split game code and tests into focused modules
This commit is contained in:
20 files changed
+2611
-2431
No files matched your search
+146
@@ -0,0 +1,146 @@
|
||||
#include "game.h"
|
||||
|
||||
int partner_owned = 0;
|
||||
TEST_INTERNAL int gift_conversation = 0;
|
||||
int ball_count = 0, balls_given = 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
|
||||
|
||||
void load_progress(void)
|
||||
{
|
||||
partner_owned = ball_count = balls_given = 0;
|
||||
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];
|
||||
if (SAVE_MEMORY[4] != 2 || flags > 3 || count > 99 ||
|
||||
(!(flags & 2) && count != 0) || SAVE_MEMORY[8] != 0x5a ||
|
||||
SAVE_MEMORY[7] != (0xa5 ^ 2 ^ flags ^ count)) return;
|
||||
partner_owned = !!(flags & 1);
|
||||
balls_given = !!(flags & 2);
|
||||
ball_count = (int)count;
|
||||
}
|
||||
|
||||
static void save_progress(void)
|
||||
{
|
||||
unsigned flags = (partner_owned ? 1u : 0u) | (balls_given ? 2u : 0u);
|
||||
/* 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] = 2;
|
||||
SAVE_MEMORY[5] = (unsigned char)flags;
|
||||
SAVE_MEMORY[6] = (unsigned char)ball_count;
|
||||
SAVE_MEMORY[7] = (unsigned char)(0xa5 ^ 2 ^ flags ^ (unsigned)ball_count);
|
||||
SAVE_MEMORY[8] = 0x5a;
|
||||
SAVE_MEMORY[0] = 'F';
|
||||
}
|
||||
|
||||
/* 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", "KEEP THEM SAFE FOR YOUR ADVENTURE"}
|
||||
};
|
||||
TEST_INTERNAL const char *const ranger_followup[DIALOGUE_PAGES][2] = {
|
||||
{"YOUR POKE BALLS ARE IN YOUR BAG", "OPEN YOUR TEAM WITH SELECT TO LOOK"},
|
||||
{"ENJOY EXPLORING SUNPETAL TRAIL", "TAKE GOOD CARE OF YOUR PARTNER"}
|
||||
};
|
||||
|
||||
int near_npc(int x, int y)
|
||||
{
|
||||
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;
|
||||
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 < 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] = ranger ?
|
||||
(ball_gift_conversation ? ranger_dialogue : ranger_followup) : professor ?
|
||||
(gift_conversation ? professor_dialogue : professor_followup) : dialogue;
|
||||
large_text(16, 102, 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);
|
||||
}
|
||||
Reference in new issue
Block a user