Files
pokemon-gba/tests/test_catching.c
T

305 lines
13 KiB
C

#include "test_support.h"
void check_partner_selection(void)
{
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
memset(test_save, 0xff, sizeof(test_save));
load_progress();
battle.phase = BATTLE_OFF;
assert(!choose_partner(0));
partner_owned = balls_given = rod_owned = 1;
ball_count = 5;
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);
assert(partner_hp() == (member ? species[member - 1].hp : SPROUT_HP));
for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room;
int x, y;
reset_position(&x, &y);
if (current_room == ROUTE) { y = 94; reset_follower(x, y); }
for (int step = 0; step < 20; ++step) {
render_scene(x, y);
copy_pending_rows(visible);
int old_x = x, old_y = y, fx = follower_x, fy = follower_y;
walk_movement(RIGHT, &x, &y, 1);
restore(old_x - 8, old_y - 20, 17, 24);
restore(fx - 8, fy - 20, 17, 24);
actors(x, y, 0);
location_status(x, y);
copy_pending_rows(visible);
memcpy(expected, SCREEN, sizeof(expected));
render_scene(x, y);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
}
assert(start_battle(TIDEFIN));
assert(battle.sprout_hp == partner_hp());
assert(!choose_partner(0)); /* No changing partners mid-turn. */
draw_battle();
if (member == 3) write_preview("build/emberwing-partner-preview.ppm");
assert(battle_input(RIGHT));
assert(battle_input(A) && battle.enemy_hp < species[TIDEFIN].hp);
assert(battle.sprout_hp == partner_hp() - species[TIDEFIN].attack);
assert(battle_input(B) && battle_input(A));
assert(start_battle(GOLDEN) && battle.sprout_hp == partner_hp());
assert(battle_input(B) && battle_input(A));
}
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
/* Reject an out-of-range selection even with a correct checksum. */
test_save[5] ^= (5 ^ 6) << 3;
checksum_fixture();
load_progress();
assert(!partner_owned && active_member == 0);
memcpy(test_save, saved, sizeof(saved));
legacy_fixture(4);
load_progress();
assert(partner_owned && rod_owned && caught_count == 5 && active_member == 0);
memset(test_save, 0xff, sizeof(test_save));
load_progress();
current_room = OUTSIDE;
follower_ready = 0;
puts("Partners: selection, saving, old saves, follower redraws, moves and battle recovery passed");
}
void check_fishing(void)
{
memset(test_save, 0xff, sizeof(test_save));
load_progress();
battle.phase = BATTLE_OFF;
current_room = BEACH;
player_facing = FACING_DOWN;
int x = 120, y = beach_shoreline(120) - 5;
assert(near_water(x, y) && !cast_rod(x, y));
assert(!begin_conversation(x, y));
assert(begin_conversation(FISHER_X - 14, FISHER_Y));
assert(!rod_owned);
assert(advance_conversation(0) == 1 && rod_owned);
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));
unsigned char before[sizeof(test_save)];
memcpy(before, test_save, sizeof(before));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
assert(memcmp(before, test_save, sizeof(before)) == 0);
partner_owned = balls_given = 1;
ball_count = 5;
for (int facing = FACING_UP; facing <= FACING_RIGHT; ++facing) {
player_facing = (enum Facing)facing;
assert(!cast_rod(x, y));
}
player_facing = FACING_DOWN;
assert(!cast_rod(x, 75));
current_room = ROUTE;
assert(!cast_rod(x, y));
current_room = BEACH;
assert(cast_rod(x, y) && battle.enemy == TIDEFIN);
assert(!cast_rod(x, y));
draw_battle();
write_preview("build/fishing-battle-preview.ppm");
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);
write_preview("build/tidefin-team-preview.ppm");
assert(cast_rod(x, y));
assert(battle_input(B) && battle_input(A));
/* An old save keeps catches and balls without granting a rod. */
legacy_fixture(3);
load_progress();
assert(!rod_owned && has_caught(TIDEFIN) && ball_count == 4);
memset(test_save, 0xff, sizeof(test_save));
load_progress();
current_room = OUTSIDE;
follower_ready = 0;
puts("Fishing: rod gift, save migration, shoreline use, water battles and catches passed");
}
void check_catching(void)
{
const unsigned char old_save[] = {'F','Q','S','P',2,3,5,0xa1,0x5a};
memset(test_save, 0xff, sizeof(test_save));
memcpy(test_save, old_save, sizeof(old_save));
load_progress();
assert(partner_owned && balls_given && ball_count == 5 && caught_count == 0);
assert(test_save[4] == 2); /* Preserve an existing save until an action writes it. */
current_room = ROUTE;
assert(start_battle(FERNFOX));
ball_count = 0;
unsigned rng = random_state;
unsigned char before[sizeof(test_save)];
memcpy(before, test_save, sizeof(before));
assert(battle_input(SELECT) && battle.notice == NOTICE_NO_BALLS);
assert(battle.phase == BATTLE_TURN && battle.turns == 0 && battle.sprout_hp == SPROUT_HP);
assert(random_state == rng && memcmp(before, test_save, sizeof(before)) == 0);
ball_count = 5;
random_state = 1; /* Full-health roll 69 fails the 35% catch chance. */
draw_battle();
static u16 visible[WIDTH * HEIGHT], expected[WIDTH * HEIGHT];
copy_pending_rows(visible);
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(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);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
write_preview("build/catch-failed-preview.ppm");
load_progress();
assert(partner_owned && ball_count == 5 && caught_count == 0);
assert(battle_input(RIGHT));
assert(battle_input(A) && battle.enemy_hp == 11);
assert(battle_input(A) && battle.enemy_hp == 4);
int hp = battle.sprout_hp;
draw_battle_hud();
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 == 4 && caught_count == 1 && caught_species[0] == FERNFOX);
assert(has_caught(FERNFOX) && !has_caught(GOLDEN));
draw_battle_hud();
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
memcpy(expected, SCREEN, sizeof(expected));
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
draw_battle();
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
write_preview("build/caught-preview.ppm");
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");
/* The village's single golden creature leaves the map after being caught. */
current_room = OUTSIDE;
assert(begin_battle(182, 113));
battle.enemy_hp = 1;
assert(battle_input(SELECT) && battle.phase == BATTLE_CAUGHT);
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));
/* Fill a six-member team, including duplicate wild species. */
current_room = ROUTE;
ball_count = 5;
const enum Species additions[] = {EMBERWING, PEBBLIT, FERNFOX};
for (unsigned i = 0; i < sizeof(additions) / sizeof(additions[0]); ++i) {
assert(start_battle(additions[i]));
battle.enemy_hp = 1;
assert(battle_input(SELECT) && battle.phase == BATTLE_CAUGHT);
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;
assert(battle_input(SELECT) && battle.notice == NOTICE_TEAM_FULL);
assert(ball_count == 2 && battle.turns == 0 && battle.sprout_hp == SPROUT_HP);
assert(random_state == rng && memcmp(before, test_save, sizeof(before)) == 0);
assert(battle_input(B) && battle_input(A));
for (int member = 0; member <= caught_count; ++member) draw_team_member(member);
draw_team();
memcpy(expected, SCREEN, sizeof(expected));
draw_team_member(99);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
load_progress();
assert(caught_count == 5 && ball_count == 2 && partner_owned);
for (int i = SAVE_RECORD_SIZE; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
for (int byte = 0; byte < SAVE_RECORD_SIZE; ++byte) {
memcpy(test_save, before, sizeof(before));
test_save[byte] ^= 1;
load_progress();
assert(!partner_owned && !balls_given && ball_count == 0 && caught_count == 0);
}
memcpy(test_save, before, sizeof(before));
test_save[7] = 6;
checksum_fixture(); /* Valid CRC, invalid party length. */
load_progress();
assert(!partner_owned && caught_count == 0);
memcpy(test_save, before, sizeof(before));
test_save[8] = SPECIES_COUNT;
checksum_fixture();
load_progress();
assert(!partner_owned && caught_count == 0);
memcpy(test_save, before, sizeof(before));
test_save[5] &= (unsigned char)~1;
checksum_fixture();
load_progress();
assert(!partner_owned && caught_count == 0); /* No captured team without Sprout. */
/* 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 == 1 && caught_count == 0);
assert(battle_input(A));
ball_count = 1;
assert(start_battle(PEBBLIT));
battle.enemy_hp = 1;
assert(battle_input(A) && battle.phase == BATTLE_WON);
assert(!battle_input(SELECT) && ball_count == 1); /* Cannot catch a fainted creature. */
assert(battle_input(A));
memset(test_save, 0xff, sizeof(test_save));
load_progress();
current_room = OUTSIDE;
follower_ready = 0;
puts("Catching: ball use, failed/successful catches, team limits, browsing, and save migration passed");
}