#include "game.h" static struct { unsigned last, fraction; } movement_clock; TEST_INTERNAL unsigned diagonal_fraction; void pause_movement_at(unsigned now) { movement_clock.last = now; movement_clock.fraction = 0; diagonal_fraction = 0; } /* Fixed 60 Hz simulation, independent of how often a frame is drawn. */ int movement_ticks_at(unsigned now) { unsigned elapsed = now - movement_clock.last; movement_clock.last = now; if (elapsed > MOVEMENT_CLOCK_HZ / 4) elapsed = MOVEMENT_CLOCK_HZ / 4; unsigned total = movement_clock.fraction + elapsed * MOVEMENT_HZ; unsigned ticks = total / MOVEMENT_CLOCK_HZ; movement_clock.fraction = total % MOVEMENT_CLOCK_HZ; /* Drop excess backlog after a long stall rather than teleporting. */ return ticks > MAX_MOVEMENT_TICKS ? MAX_MOVEMENT_TICKS : (int)ticks; } enum Room current_room = OUTSIDE; int door_open = 0; enum Facing player_facing = FACING_DOWN; static short trail_x[FOLLOW_STEPS], trail_y[FOLLOW_STEPS]; TEST_INTERNAL int trail_head; int follower_x, follower_y, follower_ready; void reset_follower(int x, int y) { for (int i = 0; i < FOLLOW_STEPS; ++i) { trail_x[i] = (short)x; trail_y[i] = (short)y; } trail_head = 0; follower_x = x; follower_y = y; follower_ready = 1; } TEST_INTERNAL void move_follower(int old_x, int old_y, int x, int y) { if (!partner_owned || (old_x == x && old_y == y)) return; if (!follower_ready) reset_follower(old_x, old_y); trail_x[trail_head] = (short)old_x; trail_y[trail_head] = (short)old_y; trail_head = (trail_head + 1) % FOLLOW_STEPS; follower_x = trail_x[trail_head]; follower_y = trail_y[trail_head]; } int indoors(void) { return current_room == COTTAGE || current_room == MELLON_HOUSE; } TEST_INTERNAL enum Facing facing_for_keys(unsigned keys, enum Facing previous) { if (keys & RIGHT) return FACING_RIGHT; if (keys & LEFT) return FACING_LEFT; if (keys & DOWN) return FACING_DOWN; if (keys & UP) return FACING_UP; return previous; } int near_door(int x, int y) { if (indoors()) return x >= 110 && x <= 130 && y >= 125 && y <= 131; return (current_room == OUTSIDE || current_room == MELLON_GARDEN) && x >= 169 && x <= 185 && y >= 78 && y <= 89; } int near_map_exit(int x, int y) { if (current_room == OUTSIDE && x >= 216 && x <= 222 && y >= 108 && y <= 117) return 1; if (current_room == MELLON_GARDEN && x >= 17 && x <= 24 && y >= 108 && y <= 117) return 1; if (x < 102 || x > 114) return 0; if (current_room == OUTSIDE || current_room == MELLON_GARDEN) return y >= 124 && y <= 131; if (current_room == ROUTE || current_room == BEACH) return y >= 47 && y <= 54; 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) { if (!near_map_exit(*x, *y)) return 0; if (current_room == OUTSIDE && *y == 131 && (keys & DOWN)) { current_room = ROUTE; *x = 108; *y = 60; player_facing = FACING_DOWN; } else if (current_room == ROUTE && *y == 47 && (keys & UP)) { current_room = OUTSIDE; *x = 108; *y = 125; player_facing = FACING_UP; } else if (current_room == OUTSIDE && *x == 222 && (keys & RIGHT)) { current_room = MELLON_GARDEN; *x = 31; *y = 113; player_facing = FACING_RIGHT; } else if (current_room == MELLON_GARDEN && *x == 17 && (keys & LEFT)) { current_room = OUTSIDE; *x = 210; *y = 113; player_facing = FACING_LEFT; } else if (current_room == MELLON_GARDEN && *y == 131 && (keys & DOWN)) { current_room = BEACH; *x = 108; *y = 60; player_facing = FACING_DOWN; } else if (current_room == BEACH && *y == 47 && (keys & UP)) { current_room = MELLON_GARDEN; *x = 108; *y = 125; player_facing = FACING_UP; } else { return 0; } door_open = 0; reset_arrival(*x, *y); return 1; } int near_route_sign(int x, int y) { return current_room == ROUTE && x >= 121 && x <= 151 && y >= 77 && y <= 87; } /* Only the doorway can change maps; both arrivals are on safe floor. */ int use_door(int *x, int *y) { if (!near_door(*x, *y)) return 0; if (current_room == OUTSIDE) { current_room = COTTAGE; *x = 120; *y = 119; } else if (current_room == COTTAGE) { current_room = OUTSIDE; *x = 177; *y = 83; } else if (current_room == MELLON_GARDEN) { current_room = MELLON_HOUSE; *x = 120; *y = 119; } else { current_room = MELLON_GARDEN; *x = 177; *y = 83; } door_open = 0; player_facing = FACING_DOWN; reset_arrival(*x, *y); return 1; } void reset_position(int *x, int *y) { *x = indoors() ? 120 : current_room == MELLON_GARDEN ? 31 : 108; *y = indoors() ? 119 : (current_room == ROUTE || current_room == BEACH) ? 60 : current_room == MELLON_GARDEN ? 113 : 103; player_facing = FACING_DOWN; reset_arrival(*x, *y); } TEST_INTERNAL int can_walk(int x, int y) { 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 (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. */ if (x > 28 && x < 72 && y > 104 && y < 130) return 0; /* Boulder. */ if (x > RANGER_X - 13 && x < RANGER_X + 13 && y > RANGER_Y - 9 && y < RANGER_Y + 9) return 0; return 1; } 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 (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 (room == OUTSIDE && x > NPC_X - 13 && x < NPC_X + 13 && y > NPC_Y - 9 && y < NPC_Y + 9) return 0; return 1; } enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks) { player_facing = facing_for_keys(keys, player_facing); int dx = !!(keys & RIGHT) - !!(keys & LEFT); int dy = !!(keys & DOWN) - !!(keys & UP); if (!dx && !dy) { diagonal_fraction = 0; return WALK_NORMAL; } if (ticks > MAX_MOVEMENT_TICKS) ticks = MAX_MOVEMENT_TICKS; for (int tick = 0; tick < ticks; ++tick) { int pixels = WALK_SPEED; if (dx && dy) { /* 181/256 approximates 1/sqrt(2): no diagonal speed boost. */ diagonal_fraction += WALK_SPEED * 181; pixels = (int)(diagonal_fraction / 256); diagonal_fraction %= 256; } else { diagonal_fraction = 0; } for (int pixel = 0; pixel < pixels; ++pixel) { int old_x = *x, old_y = *y; if (can_walk(*x + dx, *y)) *x += dx; if (can_walk(*x, *y + dy)) *y += dy; /* Record every substep, preserving Sprout's existing path gap. */ move_follower(old_x, old_y, *x, *y); if (use_map_exit(x, y, keys)) return WALK_MAP_CHANGED; if (check_grass_encounter(old_x, old_y, *x, *y)) return WALK_ENCOUNTER; } } return WALK_NORMAL; }