Files
pokemon-gba/tests/test_movement.c
T

135 lines
6.0 KiB
C

#include "test_support.h"
void check_movement_clock_and_speed(void)
{
const int render_rates[] = {15, 20, 30, 60, 120};
for (int following = 0; following < 2; ++following)
for (unsigned rate = 0; rate < sizeof(render_rates) / sizeof(render_rates[0]); ++rate) {
current_room = OUTSIDE;
partner_owned = following;
int x = 40, y = 125, total_ticks = 0;
reset_follower(x, y);
pause_movement_at(0);
for (int frame = 1; frame <= render_rates[rate]; ++frame) {
unsigned now = (unsigned)(frame * MOVEMENT_CLOCK_HZ / render_rates[rate]);
int ticks = movement_ticks_at(now);
total_ticks += ticks;
assert(walk_movement(RIGHT, &x, &y, ticks) == WALK_NORMAL);
}
assert(total_ticks == MOVEMENT_HZ && x == 160 && y == 125);
if (following) assert(follower_x == x - FOLLOW_STEPS && follower_y == y);
}
/* Timer rollover, idle reads, bounded catch-up, and paused menus. */
pause_movement_at(0xffffff00u);
assert(movement_ticks_at(0x00000100u) == 1);
assert(movement_ticks_at(0x00000100u) == 0);
pause_movement_at(0);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == MAX_MOVEMENT_TICKS);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == 0); /* No hidden backlog. */
pause_movement_at(123456u);
assert(movement_ticks_at(123456u) == 0);
pause_movement_at(999999u); /* Time spent in a menu is discarded. */
assert(movement_ticks_at(999999u) == 0);
assert(movement_ticks_at(999999u + MOVEMENT_CLOCK_HZ / 60 + 1) == 1);
partner_owned = 0;
current_room = OUTSIDE;
int x = 108, y = 103;
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 110 && y == 103 && player_facing == FACING_RIGHT);
assert(walk_movement(UP, &x, &y, 0) == WALK_NORMAL);
assert(x == 110 && y == 103 && player_facing == FACING_UP);
x = 132; y = 106;
assert(walk_movement(RIGHT, &x, &y, MAX_MOVEMENT_TICKS) == WALK_NORMAL);
assert(x == 133 && can_walk(x, y)); /* Never tunnel through Mila. */
x = 84; y = 70;
assert(walk_movement(LEFT, &x, &y, 2) == WALK_NORMAL);
assert(x == 82 && can_walk(x, y)); /* Stop at the pond, pixel by pixel. */
x = 177; y = 80;
assert(walk_movement(UP, &x, &y, 2) == WALK_NORMAL);
assert(y == 78 && near_door(x, y)); /* Door remains usable. */
pause_movement_at(0);
x = 160; y = 90;
for (int tick = 0; tick < 20; ++tick)
assert(walk_movement(RIGHT | DOWN, &x, &y, 1) == WALK_NORMAL);
assert(x == 188 && y == 118); /* Approximately 40 pixels, not 56 diagonally. */
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 190 && diagonal_fraction == 0);
assert(walk_movement(RIGHT | LEFT, &x, &y, 5) == WALK_NORMAL);
assert(x == 190 && y == 118);
/* Crossing an exit stops remaining substeps instead of skipping its trigger. */
partner_owned = 1;
x = 108; y = 129;
reset_follower(x, y);
assert(walk_movement(DOWN, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == ROUTE && x == 108 && y == 60);
assert(follower_x == x && follower_y == y);
y = 48;
assert(walk_movement(UP, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == OUTSIDE && x == 108 && y == 125);
x = 221; y = 113;
assert(walk_movement(RIGHT, &x, &y, 5) == WALK_MAP_CHANGED);
assert(current_room == MELLON_GARDEN && x == 31 && y == 113);
x = 18;
assert(walk_movement(LEFT, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == OUTSIDE && x == 210 && y == 113);
current_room = COTTAGE;
x = 120; y = 125;
assert(walk_movement(DOWN, &x, &y, 5) == WALK_NORMAL);
assert(current_room == COTTAGE && y == 131); /* Indoor exits still require A. */
/* Two-pixel ticks still count every traveled substep for grass and following. */
current_room = ROUTE;
x = 40; y = 70;
reset_follower(x, y);
grass_steps = 0;
random_state = 1; /* First roll does not trigger an encounter. */
for (int tick = 0; tick < 12; ++tick)
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 64 && grass_steps == 24 && follower_x == 50 && follower_y == 70);
grass_steps = 63; /* Next traveled pixel guarantees an encounter. */
assert(walk_movement(RIGHT, &x, &y, 5) == WALK_ENCOUNTER);
assert(x == 65 && y == 70 && battle.phase == BATTLE_TURN);
assert(battle_input(B) && battle_input(A) && battle.phase == BATTLE_OFF);
/* Faster and catch-up frames must erase both old sprites completely. */
static u16 snapshot[WIDTH * HEIGHT];
const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
const int tick_counts[] = {1, 2, 1, 3, 0};
for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room;
reset_position(&x, &y);
if (current_room == OUTSIDE) { x = 40; y = 125; }
if (current_room == ROUTE) y = 94;
reset_follower(x, y);
render_scene(x, y);
reset_dirty_rows();
int phase = 0;
for (int frame = 0; frame < 40; ++frame) {
int old_x = x, old_y = y, old_fx = follower_x, old_fy = follower_y;
int ticks = tick_counts[frame % 5];
assert(walk_movement(directions[frame / 10], &x, &y, ticks) == WALK_NORMAL);
restore(old_x - 8, old_y - 20, 17, 24);
restore(old_fx - 8, old_fy - 20, 17, 24);
actors(x, y, (phase >> 3) & 1);
location_status(x, y);
assert(pending_pixels() < 16500);
memcpy(snapshot, SCREEN, sizeof(snapshot));
render_scene(x, y);
restore(x - 8, y - 20, 17, 24);
actors(x, y, (phase >> 3) & 1);
assert(memcmp(snapshot, SCREEN, sizeof(snapshot)) == 0);
reset_dirty_rows();
phase += ticks;
}
}
current_room = OUTSIDE;
partner_owned = follower_ready = 0;
grass_steps = 0;
player_facing = FACING_DOWN;
pause_movement_at(0);
puts("Movement: 120 pixels/second at 15/20/30/60/120 render updates, with or without Sprout");
}