Compare commits

...
9 Commits
33 changed files with 2337 additions and 186 deletions

No files matched your search

+7 -4
View File
@@ -4,10 +4,11 @@ CC := $(TOOL_PREFIX)gcc
OBJCOPY := $(TOOL_PREFIX)objcopy OBJCOPY := $(TOOL_PREFIX)objcopy
CFLAGS := -mcpu=arm7tdmi -marm -O2 -g -Wall -Wextra -Werror -ffreestanding -fno-builtin -fno-common -Wa,--noexecstack CFLAGS := -mcpu=arm7tdmi -marm -O2 -g -Wall -Wextra -Werror -ffreestanding -fno-builtin -fno-common -Wa,--noexecstack
GAME_SOURCES := source/main.c source/video.c source/scenery.c source/world.c \ GAME_SOURCES := source/main.c source/video.c source/scenery.c source/world.c \
source/actors.c source/battle.c source/story.c source/hud.c source/actors.c source/battle.c source/story.c source/hud.c source/audio.c source/beach.c source/menu.c source/save.c
GAME_OBJECTS := $(GAME_SOURCES:source/%.c=build/%.o) GAME_OBJECTS := $(GAME_SOURCES:source/%.c=build/%.o)
TEST_SOURCES := tests/test_game.c tests/test_support.c tests/test_maps.c \ TEST_SOURCES := tests/test_game.c tests/test_support.c tests/test_maps.c \
tests/test_story.c tests/test_battle.c tests/test_rendering.c tests/test_movement.c tests/test_story.c tests/test_battle.c tests/test_rendering.c tests/test_movement.c tests/test_audio.c \
tests/test_catching.c tests/test_menu.c tests/test_progress.c tests/test_location.c
.PHONY: all play check check-host .PHONY: all play check check-host
all: game.gba all: game.gba
@@ -22,9 +23,11 @@ build/%.o: source/%.c source/game.h | build
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
build/actors.o: source/wild_creatures.h build/actors.o: source/wild_creatures.h
build/audio.o build/main.o: source/audio.h
build/game.elf: build/start.o $(GAME_OBJECTS) gba.ld # The bare-metal .data segment intentionally contains startup-relocated EWRAM code.
$(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-z,noexecstack -Wl,-Map,build/game.map build/start.o $(GAME_OBJECTS) -lgcc -o $@ build/game.elf: build/start.o $(GAME_OBJECTS) gba.ld Makefile
$(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-z,noexecstack -Wl,--no-warn-rwx-segments -Wl,-Map,build/game.map build/start.o $(GAME_OBJECTS) -lgcc -o $@
game.gba: build/game.elf tools/rom_header.py game.gba: build/game.elf tools/rom_header.py
$(OBJCOPY) -O binary $< $@ $(OBJCOPY) -O binary $< $@
+38 -12
View File
@@ -22,14 +22,20 @@ The game file is **game.gba**. You can also open it directly in mGBA without reb
- **X at the house door:** stand on the steps below the door and press X to open it and go inside. Walk to the bottom doorway inside and press X to return outside. - **X at the house door:** stand on the steps below the door and press X to open it and go inside. Walk to the bottom doorway inside and press X to return outside.
- **Leave Greenleaf Village:** follow the central sandy path down through the gap in the bottom fence. Keep walking down to enter **Sunpetal Trail** automatically. - **Leave Greenleaf Village:** follow the central sandy path down through the gap in the bottom fence. Keep walking down to enter **Sunpetal Trail** automatically.
- **Return to the village:** on Sunpetal Trail, follow the central path up to the top edge. Press X beside the wooden sign to read the directions. - **Return to the village:** on Sunpetal Trail, follow the central path up to the top edge. Press X beside the wooden sign to read the directions.
- **Visit Professor Mellon:** follow the village's horizontal sandy path right through the fence opening. His blue-roofed house is in the garden. Press X on its steps to enter, then walk close to the professor and press X to talk. Leave through the bottom doorway, then follow the garden path left to return to town. - **Visit Professor Mellon:** follow the village's horizontal sandy path right through the fence opening. Her blue-roofed house is in the garden. Press X on its steps to enter, then walk close to the professor and press X to talk. Leave through the bottom doorway, then follow the garden path left to return to town.
- **Get your first partner:** talk to Professor Mellon and press X to advance his conversation. He gives you **Sprout**, an original Grass-type partner at level five. This is a one-time gift; later conversations check on Sprout instead. - **Visit Seashell Beach:** from Professor Mellon's garden, follow the central sandy path down through the bottom fence gap. Walk up the beach's northern path to return to the garden. Explore the dry sand, palms, umbrella, shells, and starfish; the sea is not walkable. Sprout comes along, and the beach uses the calm trail music.
- **Backspace (GBA Select button):** open your team to see Sprout. Press Backspace, X (A), or Z (B) to return. Movement pauses while the team screen is open. - **Go fishing:** talk to **Fisher Coral**, the red-haired person in turquoise on the right side of Seashell Beach. Press X again to receive a reusable **Poké Rod** in your bag; choose Save from the Start menu to keep it after restarting. Bring Sprout, walk to the water's edge, face down toward the sea, and press **X** to hook **Tidefin**, an original Water-type fish. Weaken it and press **Backspace** to throw a Poké Ball. Catching still requires balls from Ranger Finn and space on your team. Older saves keep their gifts and catches.
- **Sprout follows you:** after receiving Sprout, walk a few steps and it follows your path. It travels through doors and map exits with you. - **Get your first partner:** talk to Professor Mellon and press X to advance her conversation. She gives you **Sprout**, an original Grass-type partner at level five. This is a one-time gift; later conversations check on Sprout instead.
- **Character artwork:** the player and Ranger Finn use boy sprites; Mila and Professor Mellon use distinct long-haired girl sprites. Mellon keeps her white coat and gray hair. Both styles have front, back, and side views.
- **Backspace (GBA Select button):** open your team directly while exploring. Use Left/Right to browse Sprout and your caught creatures, then press **X (A)** to choose the displayed creature as your following and battling partner and return. Backspace or Z (B) returns without changing your choice. Team opened directly returns to the world; Team opened from Start returns to the sidebar. Choose Save from the Start menu to retain your selection.
- **Your partner follows you:** Sprout or your selected caught creature follows your path and travels through doors and map exits with you. Each uses its own artwork. Choose Sprout again from the team screen whenever you like.
- **Battle the golden creature:** with Sprout on your team, walk close to the golden creature in Greenleaf Village and press X. Use Left/Right to choose **Tackle** or **Leaf Tap**, then X to attack. The golden creature responds with Bump. Press Z (B) to run away. At the result screen, press X or Z to return to the map. Sprout recovers fully after each battle. - **Battle the golden creature:** with Sprout on your team, walk close to the golden creature in Greenleaf Village and press X. Use Left/Right to choose **Tackle** or **Leaf Tap**, then X to attack. The golden creature responds with Bump. Press Z (B) to run away. At the result screen, press X or Z to return to the map. Sprout recovers fully after each battle.
- **Wild encounters:** with Sprout, walk around either tall-grass patch on Sunpetal Trail. **Fernfox** (Grass), **Emberwing** (Fire), or **Pebblit** (Rock) can appear automatically. Each has its own original 24×24 sprite, palette, health, and attack. Encounters only happen while moving in grass, with a short breather between battles. Leaf Tap is stronger against Pebblit and weaker against Emberwing. Fighting and running use the same keys as the golden-creature battle. - **Wild encounters:** with Sprout, walk around either tall-grass patch on Sunpetal Trail. **Fernfox** (Grass), **Emberwing** (Fire), or **Pebblit** (Rock) can appear automatically. Each has its own original 24×24 sprite, palette, health, and attack. Encounters only happen while moving in grass, with a short breather between battles. Leaf Tap is stronger against Pebblit and weaker against Emberwing. Fighting and running use the same keys as the golden-creature battle.
- **Get Poké Balls:** talk to **Ranger Finn**, the person in orange just left of Sunpetal Trail's central path. Press X again to receive **five Poké Balls**. This gift is given once. Press Backspace to see the count on the team screen, even if you do not have Sprout yet. Catching creatures is not implemented yet. - **Get Poké Balls:** talk to **Ranger Finn**, the person in orange just left of Sunpetal Trail's central path. Press X again to receive **five Poké Balls**. This gift is given once. Press Backspace to see the count on the team screen, even if you do not have Sprout yet.
- **Return (GBA Start button):** return to your starting position in the current map. - **Catch a creature:** press **Backspace during a battle** to throw one Poké Ball. Lower health improves the chance; at one-quarter health or less, the catch is guaranteed. A failed throw still uses the ball and lets the creature attack. You cannot catch a fainted creature. Successful catches join your saved team, with room for Sprout plus five catches (duplicates are allowed). A full team does not waste a ball. The village's golden creature disappears from the map once caught. Press X after the catch result, then Backspace and Left/Right to view your new friend. Press X to make it your partner.
- **Battle with caught creatures:** your selected partner fights in both land and fishing battles, using its own maximum health, Tackle, and a second move: Golden's Bump, Fernfox's Leaf Tap, Emberwing's Spark, Pebblit's Roll, or Tidefin's Water Jet. Partners recover fully after battles. Choose partners outside battle, not during a turn.
- **Return (GBA Start button):** open the **Team / Bag / Save / Close** right sidebar while exploring. Up/Down wraps through the four options and X (A) activates one. Team uses the same screen as Select. Bag shows your Poké Ball count (including zero) and rod ownership; it is read-only, with X or Z returning to the sidebar. Z (B) closes the sidebar or returns from a submenu; Return closes the entire menu from any page. Saved/failed messages wait for a fresh X, Z, or Return press. All menu pages pause walking and encounters; closing restores the same scene and position. Start no longer resets your position, and menus cannot open during conversations or battles. Music and S (R) mute continue working while menus are open.
- **S (GBA R shoulder button):** mute or unmute the music, including during battles and menus. This setting lasts until you restart the ROM.
If those keys do not match your emulator settings, check mGBA's keyboard input settings. If those keys do not match your emulator settings, check mGBA's keyboard input settings.
@@ -49,11 +55,15 @@ Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)`
- `source/game.h`: shared game types and module declarations. - `source/game.h`: shared game types and module declarations.
- `source/video.c`: frame buffer, dirty rows, text, and sprite renderer. - `source/video.c`: frame buffer, dirty rows, text, and sprite renderer.
- `source/scenery.c`: map artwork and the scenery cache. - `source/scenery.c`: map artwork and the scenery cache.
- `source/beach.c`: Seashell Beach artwork and shared shoreline boundary.
- `source/world.c`: movement, collisions, doors, map travel, and following. - `source/world.c`: movement, collisions, doors, map travel, and following.
- `source/actors.c`: character and creature artwork and drawing. - `source/actors.c`: character and creature artwork and drawing.
- `source/battle.c`: species, battle rules, and the battle screen. - `source/battle.c`: species, battle rules, and the battle screen.
- `source/story.c`: conversations, gifts, and cartridge SRAM saves. - `source/story.c`: conversations and gifts.
- `source/save.c`: versioned cartridge SRAM records and validation.
- `source/hud.c`: map status, scene drawing, and the team screen. - `source/hud.c`: map status, scene drawing, and the team screen.
- `source/menu.c`: Team / Bag / Save / Close menu input and drawing, including the Select shortcut.
- `source/audio.c` and `source/audio.h`: original chiptune scores and the GBA sound-channel sequencer.
- `source/wild_creatures.h`: original wild-creature sprites and palettes. - `source/wild_creatures.h`: original wild-creature sprites and palettes.
- `source/start.s`: code that starts the GBA hardware. - `source/start.s`: code that starts the GBA hardware.
- `gba.ld`: tells the compiler where cartridge code and working memory go. - `gba.ld`: tells the compiler where cartridge code and working memory go.
@@ -61,7 +71,7 @@ Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)`
- `Makefile`: builds the game with the installed ARM compiler. - `Makefile`: builds the game with the installed ARM compiler.
- `tests/`: cartridge header, NPC interaction, drawing, house entry/exit, and room collision tests. `make check` also generates scene previews in `build/`. - `tests/`: cartridge header, NPC interaction, drawing, house entry/exit, and room collision tests. `make check` also generates scene previews in `build/`.
Run `make check-host` to check C game behavior on your Mac using only host clang, generating scene previews in `build/`. It does not need a ROM or the ARM compiler. The C tests are grouped by maps, story, battles, rendering, and movement, with shared helpers in `tests/test_support.c`. `make check` builds the ROM, checks its header, runs the ROM-dependent Python tests, and runs these host checks. Run `make check-host` to check C game behavior on your Mac using only host clang, generating scene previews in `build/`. It does not need a ROM or the ARM compiler. The C tests are grouped by maps, story, battles, rendering, movement, and audio, with shared helpers in `tests/test_support.c`. Audio tests use fake sound registers rather than your Mac's audio device. `make check` builds the ROM, checks its header, runs the ROM-dependent Python tests, and runs these host checks.
Graphics are drawn into a RAM buffer first. Only the changed spans of each row are copied to the GBA screen during VBlank, so walking does not expose a half-erased character. Full map changes are copied while the display is temporarily blanked. Graphics are drawn into a RAM buffer first. Only the changed spans of each row are copied to the GBA screen during VBlank, so walking does not expose a half-erased character. Full map changes are copied while the display is temporarily blanked.
@@ -71,9 +81,25 @@ Sprite artwork is decoded once into cached palette indices. Character and creatu
Movement has a separate 60 Hz fixed-step clock using cascaded GBA timers 2 and 3, independent of rendering. Walking is two pixels per tick (120 pixels/second), with normalized diagonal speed. Every pixel is collision-checked and recorded for Sprout and grass encounters, so faster movement does not skip walls or map exits or double the following distance. Missed updates can catch up by at most five ticks; older backlog is dropped after a long stall. Menus, conversations, battles, and map-loading time do not build up movement backlog. The existing footstep artwork is unchanged, with its phase now clocked by simulation ticks rather than rendered frames. Movement has a separate 60 Hz fixed-step clock using cascaded GBA timers 2 and 3, independent of rendering. Walking is two pixels per tick (120 pixels/second), with normalized diagonal speed. Every pixel is collision-checked and recorded for Sprout and grass encounters, so faster movement does not skip walls or map exits or double the following distance. Missed updates can catch up by at most five ticks; older backlog is dropped after a long stall. Menus, conversations, battles, and map-loading time do not build up movement backlog. The existing footstep artwork is unchanged, with its phase now clocked by simulation ticks rather than rendered frames.
## Saving your partner and items ## Saving your adventure
Receiving Sprout or Finn's Poké Balls automatically writes a small cartridge SRAM save, stored by mGBA alongside the ROM as `game.sav`. Keep that file to retain your partner and item count after reopening the game. Older Sprout-only saves remain compatible. Partner ownership, the ball count, and whether Finn's gift was claimed are saved, but your map and position are not. The golden-creature battle is a repeatable practice battle with two moves, health bars, victory, defeat, and running away. Sprout recovers afterward. Experience and catching creatures are not implemented yet. Only choosing **Save** in the Start sidebar writes cartridge SRAM, stored by mGBA alongside the ROM as `game.sav`. Save retains Sprout ownership, Finn's gift flag, remaining balls, the rod, caught species, your selected partner, map, position and facing. Gifts, throwing balls (successful or failed, including battle losses), partner selection, walking and map changes affect the current session only until you choose Save. There are no startup, exit, or gameplay autosaves. Battle state, health, conversations, open doors, follower trail, movement timing and music mute are not saved; partners recover after battles.
The sidebar states **SAVES TEAM / ITEMS AND / LOCATION**. **GAME / SAVED** means the complete v6 cartridge record was written and read back, not that mGBA has flushed it to disk. **SAVE / FAILED** means validation or read-back failed. Invalid in-memory progress or location is rejected before writing. The single record uses CRC-16/CCITT-FALSE to detect corruption, not guarantee recovery or atomicity: interrupted writes or read-back failures can lose the previous record. Close mGBA normally, then reopen the same ROM without loading an emulator save state to check persistence. Preserve backups and use disposable ROM/save copies when testing.
Existing v1–v5 saves still load without changing their bytes; newly introduced fields receive defaults. These saves start outside in Greenleaf Village at (108,103), facing down. A v6 record with valid integrity and progress but an invalid/outdated location retains progress and uses that same default spawn. Invalid integrity, invalid progress or an unknown version rejects the whole record and starts with empty progress at the default spawn, without repairing or overwriting stored bytes. Only explicit Save writes/upgrades to v6. **Older builds cannot read v6**; do not use them with a newer save, as a subsequent save could overwrite your progress.
The v6 format is 19 bytes: `FQSP` (0–3), version 6 (4), v5 flags (5: Sprout bit 0, balls gift bit 1, rod bit 2, active member bits 3–5), ball count (6), caught count (7), five species IDs/unused `FF` (8–12), room (13), facing (14), x (15), y (16), then CRC high/low bytes (17–18). CRC covers bytes 0–16 with initial `FFFF`, polynomial `1021`, no reflection or final XOR. Byte 0 is invalidated first and committed as `F` last. Persistent room IDs are Outside 0, Cottage 1, Route 2, Mellon Garden 3, Mellon House 4, Beach 5; facing IDs are down 0, up 1, left 2, right 3. These IDs must not be reordered or reused.
The ROM uses homebrew game code `QST0`. The previous `FQ01` collided with mGBA 0.10.5's Classic NES detection, which forced EEPROM instead of SRAM.
The SRAM backend sets eight waitstates without changing other WAITCNT bits and performs byte reads from a startup-relocated EWRAM routine; writes use byte accesses from ROM. Physical cartridges remain unverified. Hot cartridge removal and its Game Pak IRQ protection are not supported; power off before removing a cartridge.
## Music
Three original four-bar chiptunes loop automatically: a bright village tune (112 BPM), a calmer trail/indoor tune (94 BPM), and a faster battle tune (160 BPM). Two hardware square-wave channels play melody and bass, with quiet noise-channel percussion in the village and battle tracks. Menus and conversations keep the current tune playing; entering a battle switches themes, and leaving it restores the location's theme.
The sequencer reads the existing hardware clock without changing the movement timers. It writes sound registers only on note events, uses no PCM audio buffers or audio DMA, and skips missed notes rather than replaying them in a long loop after a stall. These are original melodies, not music copied from Pokémon. In mGBA, make sure emulator audio is unmuted and your Mac's volume is comfortable.
### Change what Mila says ### Change what Mila says
@@ -104,6 +130,6 @@ We are making an original game. No existing Pokémon ROM or separate BIOS downlo
## Project status ## Project status
Greenleaf Village connects south to Sunpetal Trail and east to Professor Mellon's garden and house. Both houses have working doors. Mila and the professor have conversations, and Mellon gives you Sprout once. Ranger Finn gives you five Poké Balls once, with a saved inventory count. Sprout is saved to SRAM, appears on your team screen, follows your footsteps, and can battle the village's golden creature. Three wild species appear in the route's tall grass, using distinct shaded sprites rather than recolors. Battles support two moves, species-specific replies, health bars, victory, defeat, running away, and free recovery. Greenleaf Village connects south to Sunpetal Trail and east to Professor Mellon's garden and house. Seashell Beach is south of the garden, with a return path north. Both houses have working doors. Mila and the professor have conversations, and Mellon gives you Sprout once. Ranger Finn gives you five usable Poké Balls. Sprout follows your footsteps and battles the village's golden creature and three wild route species. Battles support two moves, species-specific replies, health bars, catching, victory, defeat, running away, and free recovery. The team screen lets you browse up to six members; use the Start sidebar to save your team and items.
All artwork is original; no Pokémon game assets or ROMs are included. Experience, catching creatures, sound, and full progress saving are not implemented yet. All artwork and music are original; no Pokémon game assets or ROMs are included. Experience, mid-battle partner switching and sound effects are not implemented yet.
+2
View File
@@ -14,9 +14,11 @@ SECTIONS {
. = ALIGN(4); . = ALIGN(4);
} > ROM } > ROM
/* Startup copies this range, including the leaf SRAM reader, before main. */
.data : { .data : {
__data_start = .; __data_start = .;
*(.data*) *(.data*)
*(.ewram.text.sram_read)
. = ALIGN(4); . = ALIGN(4);
__data_end = .; __data_end = .;
} > RAM AT > ROM } > RAM AT > ROM
+80 -12
View File
@@ -35,12 +35,40 @@ static void creature(void)
void scaled_creature(int x, int y, int scale, const u16 *palette) void scaled_creature(int x, int y, int scale, const u16 *palette)
{ {
rectangle(x - 7 * scale, y, 14 * scale, 2 * scale, rectangle(x - 7 * scale, y, 14 * scale, 2 * scale,
indoors() ? RGB(15, 11, 8) : RGB(8, 16, 7)); indoors() ? RGB(15, 11, 8) : current_room == BEACH ? RGB(22, 20, 12) : RGB(8, 16, 7));
blit_sprite(x - 8 * scale, y - 16 * scale, creature_art, 16, 16, palette, scale, 0); blit_sprite(x - 8 * scale, y - 16 * scale, creature_art, 16, 16, palette, scale, 0);
} }
#include "wild_creatures.h" #include "wild_creatures.h"
/* Downsample route artwork to the same 16-pixel follower footprint as Sprout. */
void draw_member(int member, int x, int y, int small)
{
static const u16 gold[] = {
0, RGB(9, 8, 6), RGB(23, 15, 5), RGB(30, 23, 7),
RGB(31, 28, 14), RGB(31, 31, 28), RGB(29, 13, 10), RGB(17, 8, 7)
};
if (!member) { draw_sprout(x, y, small ? 1 : 3); return; }
enum Species kind = (enum Species)caught_species[member - 1];
if (kind == GOLDEN) { scaled_creature(x, y, small ? 1 : 3, gold); return; }
if (!small) { draw_wild_creature(kind, x, y); return; }
static char pixels[4][16][17];
static const char *rows[4][16];
static int ready;
if (!ready) {
for (int k = 0; k < 4; ++k)
for (int row = 0; row < 16; ++row) {
rows[k][row] = pixels[k][row];
for (int col = 0; col < 16; ++col)
pixels[k][row][col] = wild_art[k].rows[row * 3 / 2][col * 3 / 2];
}
ready = 1;
}
rectangle(x - 7, y, 14, 2, indoors() ? RGB(15, 11, 8) :
current_room == BEACH ? RGB(22, 20, 12) : RGB(8, 16, 7));
sprite(x - 8, y - 16, rows[kind - FERNFOX], 16, wild_art[kind - FERNFOX].palette, 0);
}
void draw_wild_creature(enum Species enemy, int x, int y) void draw_wild_creature(enum Species enemy, int x, int y)
{ {
const struct WildArt *art = &wild_art[enemy - FERNFOX]; const struct WildArt *art = &wild_art[enemy - FERNFOX];
@@ -99,6 +127,31 @@ TEST_INTERNAL const char *const person_side_art[] = {
".....111111.....", ".....1AA1A1.....", ".....1AA1A1.....", ".....111111....." ".....111111.....", ".....1AA1A1.....", ".....1AA1A1.....", ".....111111....."
}; };
/* Distinct long-haired girl silhouette, with front, back, and side views. */
TEST_INTERNAL const char *const girl_art[] = {
".....111111.....", "....14444441....", "...1434444341...", "...1444444441...",
"..144444444441..", "..144666666441..", "..144616616441..", "..144666666441..",
"...1466776641...", "...1446666441...", "...1441881441...", "...1418998141...",
"...1168998611...", "..161889988161..", "..161888888161..", "...1618888161...",
"....18888881....", "....1AA11AA1....", "....1AA11AA1....", "....111..111...."
};
TEST_INTERNAL const char *const girl_back_art[] = {
".....111111.....", "....14444441....", "...1434444341...", "...1444444441...",
"...1444444441...", "...1444444441...", "...1244444421...", "...1244444421...",
"...1444444441...", "....14444441....", "....18444481....", "...1881441881...",
"..1618BBBB8161..", "..1618BCCB8161..", "..1618BCCB8161..", "..1668BBBB8661..",
"...1111111111...", "....1AA11AA1....", "....1AA11AA1....", "....111..111...."
};
TEST_INTERNAL const char *const girl_side_art[] = {
"......11111.....", ".....1444441....", "....143444441...", "....144444441...",
"...1444444441...", "..14444466661...", "..14444461661...", "..14444466661...",
"..14444167761...", "...144411661....", "...14441881.....", "....14418881....",
"....14188881....", ".....1881661....", ".....1881661....", ".....188161.....",
".....111111.....", ".....1AA1A1.....", ".....1AA1A1.....", ".....111111....."
};
static u16 lighter(u16 color) static u16 lighter(u16 color)
{ {
int r = color & 31, g = (color >> 5) & 31, b = (color >> 10) & 31; int r = color & 31, g = (color >> 5) & 31, b = (color >> 10) & 31;
@@ -112,22 +165,33 @@ static u16 darker(u16 color)
} }
/* x and y mark the feet. The art fits inside the original erase area. */ /* x and y mark the feet. The art fits inside the original erase area. */
TEST_INTERNAL void person(int x, int y, int step, u16 clothes, u16 hat, enum Facing facing) TEST_INTERNAL void styled_person(int x, int y, int step, u16 clothes, u16 hair_or_hat,
enum Facing facing, enum CharacterStyle style)
{ {
u16 palette[] = { u16 palette[] = {
0, RGB(5, 7, 9), darker(hat), hat, RGB(11, 8, 7), 0, RGB(5, 7, 9), darker(hair_or_hat),
style == GIRL_STYLE ? lighter(hair_or_hat) : hair_or_hat,
style == GIRL_STYLE ? hair_or_hat : RGB(11, 8, 7),
RGB(24, 16, 12), RGB(31, 25, 19), RGB(25, 13, 12), RGB(24, 16, 12), RGB(31, 25, 19), RGB(25, 13, 12),
clothes, lighter(clothes), RGB(9, 12, 17), RGB(24, 17, 8), RGB(30, 24, 12) clothes, lighter(clothes), RGB(9, 12, 17), RGB(24, 17, 8), RGB(30, 24, 12)
}; };
rectangle(x - 7, y - 1, 15, 3, rectangle(x - 7, y - 1, 15, 3,
indoors() ? RGB(15, 11, 8) : RGB(8, 16, 7)); indoors() ? RGB(15, 11, 8) : current_room == BEACH ? RGB(22, 20, 12) : RGB(8, 16, 7));
const char *const *art = facing == FACING_UP ? person_back_art : const char *const *art;
facing == FACING_DOWN ? person_art : person_side_art; if (style == GIRL_STYLE)
art = facing == FACING_UP ? girl_back_art : facing == FACING_DOWN ? girl_art : girl_side_art;
else
art = facing == FACING_UP ? person_back_art : facing == FACING_DOWN ? person_art : person_side_art;
sprite(x - 8, y - 19, art, 20, palette, facing == FACING_LEFT); sprite(x - 8, y - 19, art, 20, palette, facing == FACING_LEFT);
rectangle(x - 4, y, 3, 2 - step, palette[1]); rectangle(x - 4, y, 3, 2 - step, palette[1]);
rectangle(x + 1, y, 3, 1 + step, palette[1]); rectangle(x + 1, y, 3, 1 + step, palette[1]);
} }
TEST_INTERNAL void person(int x, int y, int step, u16 clothes, u16 hat, enum Facing facing)
{
styled_person(x, y, step, clothes, hat, facing, BOY_STYLE);
}
static void player(int x, int y, int step) static void player(int x, int y, int step)
{ {
person(x, y, step, shirt, RGB(28, 8, 9), player_facing); person(x, y, step, shirt, RGB(28, 8, 9), player_facing);
@@ -135,7 +199,7 @@ static void player(int x, int y, int step)
void actors(int x, int y, int step) void actors(int x, int y, int step)
{ {
enum { HERO, SPROUT, MILA, GOLDEN, PROFESSOR, RANGER }; enum { HERO, SPROUT, MILA, GOLDEN_ACTOR, PROFESSOR, RANGER, FISHER };
struct Actor { int x, y, kind; } list[4]; struct Actor { int x, y, kind; } list[4];
int count = 0; int count = 0;
if (partner_owned && follower_ready) if (partner_owned && follower_ready)
@@ -145,10 +209,11 @@ void actors(int x, int y, int step)
list[count++] = (struct Actor){PROF_X, PROF_Y, PROFESSOR}; list[count++] = (struct Actor){PROF_X, PROF_Y, PROFESSOR};
} }
if (current_room == OUTSIDE) { if (current_room == OUTSIDE) {
list[count++] = (struct Actor){182, 104, GOLDEN}; if (!has_caught(GOLDEN)) list[count++] = (struct Actor){182, 104, GOLDEN_ACTOR};
list[count++] = (struct Actor){NPC_X, NPC_Y, MILA}; list[count++] = (struct Actor){NPC_X, NPC_Y, MILA};
} }
if (current_room == ROUTE) list[count++] = (struct Actor){RANGER_X, RANGER_Y, RANGER}; if (current_room == ROUTE) list[count++] = (struct Actor){RANGER_X, RANGER_Y, RANGER};
if (current_room == BEACH) list[count++] = (struct Actor){FISHER_X, FISHER_Y, FISHER};
/* Stable depth ordering keeps Sprout behind the hero on equal feet. */ /* Stable depth ordering keeps Sprout behind the hero on equal feet. */
for (int i = 1; i < count; ++i) { for (int i = 1; i < count; ++i) {
struct Actor actor = list[i]; struct Actor actor = list[i];
@@ -162,12 +227,15 @@ void actors(int x, int y, int step)
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
struct Actor actor = list[i]; struct Actor actor = list[i];
if (actor.kind == HERO) player(actor.x, actor.y, step); if (actor.kind == HERO) player(actor.x, actor.y, step);
else if (actor.kind == SPROUT) draw_sprout(actor.x, actor.y, 1); else if (actor.kind == SPROUT) draw_member(active_member, actor.x, actor.y, 1);
else if (actor.kind == GOLDEN) creature(); else if (actor.kind == GOLDEN_ACTOR) creature();
else if (actor.kind == MILA) else if (actor.kind == MILA)
person(actor.x, actor.y, 0, RGB(4, 21, 13), RGB(12, 7, 4), FACING_DOWN); styled_person(actor.x, actor.y, 0, RGB(4, 21, 13), RGB(12, 7, 4), FACING_DOWN, GIRL_STYLE);
else if (actor.kind == RANGER) else if (actor.kind == RANGER)
person(actor.x, actor.y, 0, RGB(27, 16, 7), RGB(6, 15, 11), FACING_DOWN); person(actor.x, actor.y, 0, RGB(27, 16, 7), RGB(6, 15, 11), FACING_DOWN);
else person(actor.x, actor.y, 0, RGB(29, 30, 27), RGB(20, 21, 19), FACING_DOWN); else if (actor.kind == FISHER)
styled_person(actor.x, actor.y, 0, RGB(5, 23, 26), RGB(26, 12, 7), FACING_DOWN, GIRL_STYLE);
else styled_person(actor.x, actor.y, 0, RGB(29, 30, 27), RGB(20, 21, 19),
FACING_DOWN, GIRL_STYLE);
} }
} }
+171
View File
@@ -0,0 +1,171 @@
#include "audio.h"
/* Original four-bar tunes. Each position is a sixteenth note; H is a tie. */
enum {
REST = 0, H = 255,
C3 = 48, D3 = 50, E3 = 52, FS3 = 54, F3 = 53, G3 = 55, A3 = 57, B3 = 59,
C4 = 60, D4 = 62, E4 = 64, G4 = 67, A4 = 69, B4 = 71,
C5 = 72, D5 = 74, E5 = 76, F5 = 77, FS5 = 78, G5 = 79, A5 = 81, B5 = 83, C6 = 84
};
static const unsigned char village_lead[64] = {
C5,H,E5,H, G5,H,E5,D5, C5,H,E5,G5, A5,H,G5,H,
F5,H,E5,H, D5,H,G4,H, E5,H,D5,C5, D5,H,G4,H,
E5,H,G5,H, C6,H,B5,A5, G5,H,E5,H, D5,E5,G5,H,
F5,H,A5,H, G5,H,E5,H, D5,H,B4,D5, C5,H,H,REST
};
static const unsigned char village_bass[16] = {
C3,G3,E3,G3, F3,C4,G3,B3, A3,E3,F3,C4, G3,D3,G3,C3
};
static const unsigned char trail_lead[64] = {
A4,H,C5,H, E5,H,H,H, D5,H,C5,H, B4,H,E4,H,
A4,H,C5,H, D5,H,E5,H, C5,H,A4,H, G4,H,H,REST,
C5,H,E5,H, G5,H,E5,H, D5,H,C5,H, B4,H,G4,H,
A4,H,B4,C5, E5,H,D5,H, C5,H,B4,H, A4,H,H,REST
};
static const unsigned char trail_bass[16] = {
A3,E3,C4,E3, F3,C4,A3,C4, G3,D3,B3,D3, A3,E3,A3,E3
};
static const unsigned char battle_lead[64] = {
E5,B4,E5,G5, FS5,D5,FS5,A5, G5,E5,B5,G5, FS5,D5,B4,REST,
E5,B4,E5,G5, A5,G5,FS5,E5, D5,B4,D5,FS5, G5,H,FS5,REST,
C5,G4,C5,E5, G5,E5,C6,G5, D5,A4,D5,FS5, A5,FS5,D5,REST,
E5,G5,B5,A5, G5,FS5,E5,D5, B4,D5,FS5,B4, E5,H,H,REST
};
static const unsigned char battle_bass[16] = {
E3,B3,E3,G3, C3,G3,C4,G3, D3,A3,D3,FS3, E3,B3,E3,B3
};
TEST_INTERNAL const struct MusicSong music_songs[MUSIC_TRACK_COUNT] = {
{village_lead, village_bass, 112, 5, 0x80, 1},
{trail_lead, trail_bass, 94, 4, 0x80, 0},
{battle_lead, battle_bass, 160, 6, 0x40, 2}
};
TEST_INTERNAL struct MusicPlayback music_state;
/* Square-wave register = 2048 - round(131072 / frequency). MIDI C3..C6. */
static const u16 pitches[37] = {
1046,1102,1155,1205,1253,1297,1339,1379,1417,1452,1486,1517,
1547,1575,1602,1627,1650,1673,1694,1714,1732,1750,1767,1783,
1798,1812,1825,1837,1849,1860,1871,1881,1890,1899,1907,1915,1923
};
#ifdef GAME_TEST
u16 music_registers[SOUND_REGISTER_COUNT];
unsigned music_write_count;
#endif
static void sound_write(enum SoundRegister reg, u16 value)
{
#ifdef GAME_TEST
music_registers[reg] = value;
++music_write_count;
#else
static const unsigned addresses[SOUND_REGISTER_COUNT] = {
0x04000084, 0x04000080, 0x04000082, 0x04000060,
0x04000062, 0x04000064, 0x04000068, 0x0400006c,
0x04000078, 0x0400007c
};
*(volatile u16 *)addresses[reg] = value;
#endif
}
TEST_INTERNAL enum MusicTrack music_choose_track(enum Room room, enum BattlePhase phase)
{
if (phase != BATTLE_OFF) return MUSIC_BATTLE;
return room == OUTSIDE || room == MELLON_GARDEN ? MUSIC_VILLAGE : MUSIC_TRAIL;
}
TEST_INTERNAL u16 music_frequency(unsigned note)
{
return note >= C3 && note <= C6 ? pitches[note - C3] : 0;
}
static void silence_voices(void)
{
sound_write(SOUND_LEAD_ENV, 0);
sound_write(SOUND_BASS_ENV, 0);
sound_write(SOUND_NOISE_ENV, 0);
}
static unsigned resolved_lead(const struct MusicSong *song, int step)
{
for (int count = 0; count < 64; ++count) {
unsigned note = song->lead[(step - count + 64) % 64];
if (note != H) return note;
}
return REST;
}
static void play_step(int force)
{
if (music_state.muted) return;
const struct MusicSong *song = &music_songs[music_state.track];
unsigned lead = force ? resolved_lead(song, music_state.step) : song->lead[music_state.step];
if (lead != H) {
unsigned envelope = song->percussion == 2 ? 2u : song->percussion == 0 ? 5u : 3u;
sound_write(SOUND_LEAD_ENV, lead == REST ? 0 :
(u16)((song->volume << 12) | (envelope << 8) | song->duty));
if (lead != REST) sound_write(SOUND_LEAD_FREQ, (u16)(0x8000 | music_frequency(lead)));
}
if (force || music_state.step % 4 == 0) {
unsigned bass = song->bass[music_state.step / 4];
sound_write(SOUND_BASS_ENV, bass == REST ? 0 : 0x3080);
if (bass != REST) sound_write(SOUND_BASS_FREQ, (u16)(0x8000 | music_frequency(bass)));
}
int beat = song->percussion == 2 ? music_state.step % 2 == 0 :
song->percussion == 1 && music_state.step % 4 == 0;
if (beat) {
sound_write(SOUND_NOISE_ENV, 0x2138); /* Quiet 31 ms percussion hit. */
sound_write(SOUND_NOISE_FREQ, music_state.step % 8 == 4 ? 0xc045 : 0xc035);
}
}
void music_init(unsigned now)
{
music_state.track = music_choose_track(current_room, battle.phase);
music_state.last = now;
music_state.phase = 0;
music_state.step = 0;
music_state.muted = 0;
sound_write(SOUND_MASTER, 0x0080);
sound_write(SOUND_RATIO, 0x0001); /* PSG at 50%; no PCM, DMA, or timer use. */
sound_write(SOUND_SWEEP, 0x0008); /* Sweep disabled. */
silence_voices();
sound_write(SOUND_MIX, 0xbb77); /* Square 1+2 and noise, both speakers. */
play_step(1);
}
void music_update(unsigned now)
{
unsigned elapsed = now - music_state.last;
music_state.last = now;
enum MusicTrack wanted = music_choose_track(current_room, battle.phase);
if (wanted != music_state.track) {
music_state.track = wanted;
music_state.step = 0;
music_state.phase = 0;
if (!music_state.muted) { silence_voices(); play_step(1); }
return;
}
/* Skip missed notes after a stall; never replay them in a busy loop. */
if (elapsed > MOVEMENT_CLOCK_HZ * 4u) elapsed = MOVEMENT_CLOCK_HZ * 4u;
unsigned total = music_state.phase + elapsed * music_songs[wanted].bpm * 4u;
unsigned steps = total / (MOVEMENT_CLOCK_HZ * 60u);
music_state.phase = total % (MOVEMENT_CLOCK_HZ * 60u);
if (steps) {
music_state.step = (music_state.step + (int)(steps % 64)) % 64;
play_step(steps > 1);
}
}
void music_toggle_mute(void)
{
music_state.muted = !music_state.muted;
if (music_state.muted) {
sound_write(SOUND_MIX, 0);
silence_voices();
} else {
sound_write(SOUND_MIX, 0xbb77);
play_step(1);
}
}
+36
View File
@@ -0,0 +1,36 @@
#ifndef AUDIO_H
#define AUDIO_H
#include "game.h"
enum MusicTrack { MUSIC_VILLAGE, MUSIC_TRAIL, MUSIC_BATTLE, MUSIC_TRACK_COUNT };
enum SoundRegister {
SOUND_MASTER, SOUND_MIX, SOUND_RATIO, SOUND_SWEEP,
SOUND_LEAD_ENV, SOUND_LEAD_FREQ, SOUND_BASS_ENV, SOUND_BASS_FREQ,
SOUND_NOISE_ENV, SOUND_NOISE_FREQ, SOUND_REGISTER_COUNT
};
struct MusicPlayback {
enum MusicTrack track;
unsigned last, phase;
int step, muted;
};
struct MusicSong {
const unsigned char *lead, *bass;
unsigned bpm;
int volume, duty, percussion;
};
void music_init(unsigned now);
void music_update(unsigned now);
void music_toggle_mute(void);
#ifdef GAME_TEST
extern struct MusicPlayback music_state;
extern const struct MusicSong music_songs[MUSIC_TRACK_COUNT];
extern u16 music_registers[SOUND_REGISTER_COUNT];
extern unsigned music_write_count;
enum MusicTrack music_choose_track(enum Room room, enum BattlePhase phase);
u16 music_frequency(unsigned note);
#endif
#endif
+84 -23
View File
@@ -1,10 +1,11 @@
#include "game.h" #include "game.h"
TEST_INTERNAL const struct SpeciesInfo species[SPECIES_COUNT] = { const struct SpeciesInfo species[SPECIES_COUNT] = {
{"GOLDEN", "LV THREE", "WILD GOLDEN CREATURE", "GOLDEN USED BUMP", "NORMAL", GOLDEN_HP, 5, 7}, {"GOLDEN", "LV THREE", "WILD GOLDEN CREATURE", "GOLDEN USED BUMP", "NORMAL", GOLDEN_HP, 5, 7},
{"FERNFOX", "LV FOUR", "WILD FERNFOX APPEARED", "FERNFOX USED LEAF SWIPE", "GRASS", 18, 4, 7}, {"FERNFOX", "LV FOUR", "WILD FERNFOX APPEARED", "FERNFOX USED LEAF SWIPE", "GRASS", 18, 4, 7},
{"EMBERWING", "LV FOUR", "WILD EMBERWING APPEARED", "EMBERWING USED SPARK", "FIRE", 20, 4, 4}, {"EMBERWING", "LV FOUR", "WILD EMBERWING APPEARED", "EMBERWING USED SPARK", "FIRE", 20, 4, 4},
{"PEBBLIT", "LV FIVE", "WILD PEBBLIT APPEARED", "PEBBLIT USED ROLL", "ROCK", 22, 5, 9} {"PEBBLIT", "LV FIVE", "WILD PEBBLIT APPEARED", "PEBBLIT USED ROLL", "ROCK", 22, 5, 9},
{"TIDEFIN", "LV FOUR", "HOOKED A WILD TIDEFIN", "TIDEFIN USED SPLASH JET", "WATER", 24, 4, 9}
}; };
struct BattleState battle; struct BattleState battle;
unsigned random_state = 0x6d2b79f5u; unsigned random_state = 0x6d2b79f5u;
@@ -19,23 +20,25 @@ TEST_INTERNAL unsigned next_random(void)
return random_state; return random_state;
} }
TEST_INTERNAL int start_battle(enum Species enemy) int start_battle(enum Species enemy)
{ {
if (!partner_owned || battle.phase != BATTLE_OFF || (unsigned)enemy >= SPECIES_COUNT) if (!partner_owned || battle.phase != BATTLE_OFF || (unsigned)enemy >= SPECIES_COUNT)
return 0; return 0;
battle.phase = BATTLE_TURN; battle.phase = BATTLE_TURN;
battle.enemy = enemy; battle.enemy = enemy;
battle.sprout_hp = SPROUT_HP; battle.sprout_hp = partner_hp();
battle.enemy_hp = species[enemy].hp; battle.enemy_hp = species[enemy].hp;
battle.move = 0; battle.move = 0;
battle.turns = 0; battle.turns = 0;
battle.notice = NOTICE_NONE;
grass_steps = 0; grass_steps = 0;
return 1; return 1;
} }
int near_golden(int x, int y) int near_golden(int x, int y)
{ {
return current_room == OUTSIDE && x >= 163 && x <= 205 && y >= 84 && y <= 126; return current_room == OUTSIDE && !has_caught(GOLDEN) &&
x >= 163 && x <= 205 && y >= 84 && y <= 126;
} }
int begin_battle(int x, int y) int begin_battle(int x, int y)
@@ -55,7 +58,41 @@ int check_grass_encounter(int old_x, int old_y, int x, int y)
return start_battle((enum Species)(FERNFOX + next_random() % 3u)); return start_battle((enum Species)(FERNFOX + next_random() % 3u));
} }
/* One fresh button press performs one turn. The golden creature replies. */ static void enemy_reply(void)
{
battle.sprout_hp -= species[battle.enemy].attack;
if (battle.sprout_hp <= 0) {
battle.sprout_hp = 0;
battle.phase = BATTLE_LOST;
}
}
/* A weakened creature is easier to catch; quarter health guarantees it. */
static unsigned catch_chance(void)
{
int maximum = species[battle.enemy].hp;
if (battle.enemy_hp * 4 <= maximum) return 100;
return 35u + (unsigned)((maximum - battle.enemy_hp) * 50 / maximum);
}
static int throw_ball(void)
{
if (ball_count == 0) { battle.notice = NOTICE_NO_BALLS; return 1; }
if (caught_count >= CAUGHT_CAPACITY) { battle.notice = NOTICE_TEAM_FULL; return 1; }
--ball_count;
++battle.turns;
if (next_random() % 100 < catch_chance()) {
caught_species[caught_count++] = (unsigned char)battle.enemy;
battle.phase = BATTLE_CAUGHT;
battle.notice = NOTICE_NONE;
} else {
battle.notice = NOTICE_BROKE_FREE;
enemy_reply();
}
return 1;
}
/* One fresh button press performs one turn. */
int battle_input(unsigned pressed) int battle_input(unsigned pressed)
{ {
if (battle.phase == BATTLE_OFF || !pressed) return 0; if (battle.phase == BATTLE_OFF || !pressed) return 0;
@@ -68,20 +105,25 @@ int battle_input(unsigned pressed)
battle.phase = BATTLE_FLED; battle.phase = BATTLE_FLED;
return 1; return 1;
} }
if (pressed & SELECT) return throw_ball();
if (pressed & (LEFT | RIGHT | A)) battle.notice = NOTICE_NONE;
if (pressed & LEFT) battle.move = 0; if (pressed & LEFT) battle.move = 0;
if (pressed & RIGHT) battle.move = 1; if (pressed & RIGHT) battle.move = 1;
if (!(pressed & A)) return !!(pressed & (LEFT | RIGHT)); if (!(pressed & A)) return !!(pressed & (LEFT | RIGHT));
battle.enemy_hp -= battle.move ? species[battle.enemy].leaf_damage : 4; int damage = 4;
if (battle.move) {
if (!active_member || caught_species[active_member - 1] == FERNFOX)
damage = species[battle.enemy].leaf_damage;
else
damage = species[caught_species[active_member - 1]].attack + 3;
}
battle.enemy_hp -= damage;
++battle.turns; ++battle.turns;
if (battle.enemy_hp <= 0) { if (battle.enemy_hp <= 0) {
battle.enemy_hp = 0; battle.enemy_hp = 0;
battle.phase = BATTLE_WON; battle.phase = BATTLE_WON;
} else { } else {
battle.sprout_hp -= species[battle.enemy].attack; enemy_reply();
if (battle.sprout_hp <= 0) {
battle.sprout_hp = 0;
battle.phase = BATTLE_LOST;
}
} }
return 1; return 1;
} }
@@ -99,24 +141,43 @@ void draw_battle_hud(void)
{ {
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26); u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
health_bar(16, 44, battle.enemy_hp, species[battle.enemy].hp); health_bar(16, 44, battle.enemy_hp, species[battle.enemy].hp);
health_bar(136, 97, battle.sprout_hp, SPROUT_HP); health_bar(136, 97, battle.sprout_hp, partner_hp());
if (battle.phase == BATTLE_CAUGHT) {
/* Remove the creature and show its closed ball without a full-screen copy. */
rectangle(155, 18, 64, 44, RGB(23, 29, 25));
rectangle(155, 62, 64, 14, RGB(18, 26, 15));
rectangle(155, battle.enemy == GOLDEN ? 61 : 70, 64, 5, RGB(12, 21, 11));
rectangle(179, 57, 10, 12, ink);
rectangle(178, 59, 12, 8, ink);
rectangle(180, 58, 8, 5, RGB(29, 8, 9));
rectangle(180, 64, 8, 4, RGB(31, 31, 28));
rectangle(183, 62, 3, 3, ink);
rectangle(184, 63, 1, 1, RGB(31, 31, 28));
}
rectangle(8, 111, 224, 43, ink); rectangle(8, 111, 224, 43, ink);
rectangle(10, 113, 220, 39, cream); rectangle(10, 113, 220, 39, cream);
if (battle.phase == BATTLE_TURN) { if (battle.phase == BATTLE_TURN) {
large_text(22, 119, "TACKLE", ink); large_text(22, 119, "TACKLE", ink);
large_text(132, 119, "LEAF TAP", ink); large_text(132, 119, partner_move(), ink);
int arrow_x = battle.move ? 122 : 12; int arrow_x = battle.move ? 122 : 12;
rectangle(arrow_x, 119, 2, 7, RGB(7, 17, 11)); rectangle(arrow_x, 119, 2, 7, RGB(7, 17, 11));
rectangle(arrow_x + 2, 121, 2, 3, RGB(7, 17, 11)); rectangle(arrow_x + 2, 121, 2, 3, RGB(7, 17, 11));
text(16, 132, battle.turns ? species[battle.enemy].reply : text(190, 119, "BALLS", ink);
"WHAT WILL SPROUT DO", ink); char count[] = {(char)('0' + ball_count / 10), (char)('0' + ball_count % 10), 0};
text(16, 143, "LEFT RIGHT CHOOSE A ATTACK B RUN", ink); text(212, 119, count, ink);
const char *message = battle.notice == NOTICE_NO_BALLS ? "NO POKE BALLS LEFT" :
battle.notice == NOTICE_TEAM_FULL ? "YOUR TEAM IS FULL NO BALL USED" :
battle.notice == NOTICE_BROKE_FREE ? "IT BROKE FREE AND ATTACKED" :
battle.turns ? species[battle.enemy].reply : "CHOOSE YOUR PARTNERS MOVE";
text(16, 132, message, ink);
text(16, 143, "ARROWS CHOOSE A HIT SELECT BALL B RUN R MUSIC", ink);
} else { } else {
const char *result = battle.phase == BATTLE_WON ? "YOU WON" : const char *result = battle.phase == BATTLE_CAUGHT ? "CAUGHT IT" : battle.phase == BATTLE_WON ? "YOU WON" :
battle.phase == BATTLE_LOST ? "SPROUT NEEDS A REST" : "YOU GOT AWAY SAFELY"; battle.phase == BATTLE_LOST ? "YOUR PARTNER NEEDS A REST" : "YOU GOT AWAY SAFELY";
large_text(20, 120, result, ink); large_text(20, 120, result, ink);
text(20, 134, "A BACK TO ADVENTURE", ink); text(20, 134, "A BACK TO ADVENTURE", ink);
text(20, 144, "SPROUT WILL REST AND RECOVER", ink); text(20, 144, battle.phase == BATTLE_CAUGHT ? "NEW FRIEND SAVED TO YOUR TEAM" :
"YOUR PARTNER WILL REST AND RECOVER", ink);
} }
} }
@@ -134,13 +195,13 @@ void draw_battle(void)
}; };
if (battle.enemy == GOLDEN) scaled_creature(184, 62, 2, gold_palette); if (battle.enemy == GOLDEN) scaled_creature(184, 62, 2, gold_palette);
else draw_wild_creature(battle.enemy, 184, 71); else draw_wild_creature(battle.enemy, 184, 71);
draw_sprout(57, 106, 3); draw_member(active_member, 57, 106, 0);
rectangle(10, 24, 106, 29, RGB(30, 30, 26)); rectangle(10, 24, 106, 29, RGB(30, 30, 26));
large_text(16, 28, species[battle.enemy].name, RGB(5, 8, 11)); large_text(16, 28, species[battle.enemy].name, RGB(5, 8, 11));
text(16, 37, species[battle.enemy].type, RGB(5, 8, 11)); text(16, 37, species[battle.enemy].type, RGB(5, 8, 11));
text(69, 37, species[battle.enemy].level, RGB(5, 8, 11)); text(69, 37, species[battle.enemy].level, RGB(5, 8, 11));
rectangle(130, 76, 102, 31, RGB(30, 30, 26)); rectangle(130, 76, 102, 31, RGB(30, 30, 26));
large_text(136, 80, "SPROUT", RGB(5, 8, 11)); large_text(136, 80, partner_name(), RGB(5, 8, 11));
text(179, 90, "LV FIVE", RGB(5, 8, 11)); text(179, 90, active_member ? species[caught_species[active_member - 1]].level : "LV FIVE", RGB(5, 8, 11));
draw_battle_hud(); draw_battle_hud();
} }
+96
View File
@@ -0,0 +1,96 @@
#include "game.h"
int near_water(int x, int y)
{
return current_room == BEACH && x >= 17 && x <= 222 &&
y >= beach_shoreline(x) - 8 && y <= beach_shoreline(x) - 5;
}
int cast_rod(int x, int y)
{
if (!rod_owned || !near_water(x, y) || player_facing != FACING_DOWN) return 0;
return start_battle(TIDEFIN);
}
int beach_shoreline(int x)
{
return 104 + ((x / 24) % 3) * 2;
}
static u16 palm_pixel(int x, int y, int center, u16 ground)
{
int dx = x - center, dy = y - 48;
int distance = dx < 0 ? -dx : dx;
if (y >= 48 && y < 70) {
int lean = (y - 48) / 6;
if (dx >= lean - 1 && dx <= lean + 2)
ground = (dx == lean - 1 || y % 5 == 0) ? RGB(13, 9, 6) : RGB(25, 18, 10);
}
if (distance <= 15 && dy >= -5 + distance / 2 && dy <= -2 + distance / 2)
ground = dy == -2 + distance / 2 ? RGB(4, 13, 8) : RGB(8, 22, 10);
if (dy >= -12 && dy <= 1 && distance <= (dy + 13) / 4)
ground = dx < 0 ? RGB(5, 17, 9) : RGB(15, 27, 11);
if (dy >= -7 && dy <= 5 && distance >= 3 && distance <= 11 &&
dy >= distance - 11 && dy <= distance - 8)
ground = dy == distance - 11 ? RGB(17, 27, 11) : RGB(5, 18, 9);
if (dy >= -1 && dy <= 2 && (dx == -1 || dx == 2)) ground = RGB(15, 10, 6);
return ground;
}
u16 beach_scenery(int x, int y)
{
if (y < 18 || y >= 142) return RGB(29, 30, 25);
int shore = beach_shoreline(x);
u16 color = RGB(31, 28, 19);
if ((x * 3 + y * 7) % 59 < 2) color = RGB(28, 24, 15);
if ((x * 7 + y * 3) % 71 == 0) color = RGB(31, 31, 24);
/* A grassy bank separates the beach from Mellon's garden to the north. */
if (y < 36) {
color = RGB(9, 21, 10);
if ((x & 15) == 4 && (y & 7) >= 5) color = RGB(5, 15, 8);
if ((x & 15) == 10 && (y & 7) == 2) color = RGB(16, 27, 12);
if (y >= 32) color = RGB(5, 13, 8);
} else if (y < 46) {
color = (y == 36 || y == 43 || x % 16 == 0) ? RGB(18, 14, 9) : RGB(27, 21, 12);
if (y == 45) color = RGB(22, 18, 11);
}
if (x >= 97 && x < 120 && y < 70) {
color = RGB(29, 27, 18);
if (x == 97 || x == 119) color = RGB(24, 23, 14);
if ((x & 7) == 3 && (y & 7) == 5) color = RGB(26, 23, 15);
}
/* Wet sand, a foamy waterline, turquoise shallows, and deep blue sea. */
if (y >= shore - 4) color = RGB(26, 24, 17);
if (y >= shore) color = RGB(29, 31, 28);
if (y >= shore + 2) color = RGB(15, 28, 28);
if (y >= shore + 6) color = RGB(7, 23, 29);
if (y >= 128) color = RGB(5, 18, 27);
if (y >= shore + 7 && y % 11 == 0 && (x + y * 3) % 27 < 11)
color = RGB(24, 31, 30);
if (y >= shore + 8 && y % 11 == 1 && (x + y * 3) % 27 < 8)
color = RGB(12, 26, 30);
color = palm_pixel(x, y, 28, color);
color = palm_pixel(x, y, 210, color);
/* A blue-and-white towel beneath a striped sun umbrella. */
if (x >= 43 && x < 67 && y >= 89 && y < 96) {
color = y % 3 == 0 ? RGB(30, 31, 28) : RGB(8, 20, 27);
if (x == 43 || x == 66) color = RGB(23, 28, 27);
}
if (x >= 54 && x < 57 && y >= 75 && y < 94)
color = x == 54 ? RGB(15, 10, 7) : RGB(26, 20, 12);
int ux = x - 55, uy = y - 77;
if (uy <= 0 && uy >= -10 && ux * ux + uy * uy * 3 < 225) {
color = ((x / 5) & 1) ? RGB(31, 14, 12) : RGB(31, 29, 20);
if (ux * ux + uy * uy * 3 > 177 || uy == 0) color = RGB(20, 9, 9);
if (uy == -8) color = RGB(31, 24, 18);
}
/* Two tiny shells and an orange starfish, all safe to walk over. */
if (((x >= 89 && x < 95) || (x >= 172 && x < 178)) && y >= 87 && y < 91)
color = (x % 2 == 0 || y == 90) ? RGB(26, 17, 16) : RGB(31, 26, 23);
int sx = x - 142, sy = y - 94;
if ((sx >= -3 && sx <= 3 && sy == 0) || (sx == 0 && sy >= -3 && sy <= 3) ||
(sx == -2 && sy == 2) || (sx == 2 && sy == 2)) color = RGB(30, 16, 7);
if (sx == 0 && sy == 0) color = RGB(31, 24, 10);
return color;
}
+59 -8
View File
@@ -7,21 +7,30 @@ typedef unsigned short u16;
#define WIDTH 240 #define WIDTH 240
#define HEIGHT 160 #define HEIGHT 160
enum { A = 1, B = 2, SELECT = 4, START = 8, RIGHT = 16, LEFT = 32, UP = 64, DOWN = 128 }; enum { A = 1, B = 2, SELECT = 4, START = 8, RIGHT = 16, LEFT = 32, UP = 64, DOWN = 128,
R_BUTTON = 256 };
enum { MOVEMENT_HZ = 60, MOVEMENT_CLOCK_HZ = 16384, WALK_SPEED = 2, MAX_MOVEMENT_TICKS = 5 }; enum { MOVEMENT_HZ = 60, MOVEMENT_CLOCK_HZ = 16384, WALK_SPEED = 2, MAX_MOVEMENT_TICKS = 5 };
enum { NPC_X = 146, NPC_Y = 106, PROF_X = 113, PROF_Y = 92, RANGER_X = 83, RANGER_Y = 97 }; enum { NPC_X = 146, NPC_Y = 106, PROF_X = 113, PROF_Y = 92, RANGER_X = 83, RANGER_Y = 97 };
enum Room { OUTSIDE, COTTAGE, ROUTE, MELLON_GARDEN, MELLON_HOUSE }; /* Room and facing IDs are persistent: never reorder or reuse them. */
enum Facing { FACING_DOWN, FACING_UP, FACING_LEFT, FACING_RIGHT }; enum Room { OUTSIDE = 0, COTTAGE = 1, ROUTE = 2, MELLON_GARDEN = 3,
MELLON_HOUSE = 4, BEACH = 5, ROOM_COUNT = 6 };
enum Facing { FACING_DOWN = 0, FACING_UP = 1, FACING_LEFT = 2, FACING_RIGHT = 3 };
struct Location { int room, x, y, facing; };
enum CharacterStyle { BOY_STYLE, GIRL_STYLE };
enum { DIALOGUE_X = 8, DIALOGUE_Y = 96, DIALOGUE_W = 224, DIALOGUE_H = 44 }; enum { DIALOGUE_X = 8, DIALOGUE_Y = 96, DIALOGUE_W = 224, DIALOGUE_H = 44 };
enum { FOLLOW_STEPS = 14, SPROUT_HP = 24, GOLDEN_HP = 18 }; enum { FOLLOW_STEPS = 14, SPROUT_HP = 24, GOLDEN_HP = 18 };
enum BattlePhase { BATTLE_OFF, BATTLE_TURN, BATTLE_WON, BATTLE_LOST, BATTLE_FLED }; enum BattlePhase { BATTLE_OFF, BATTLE_TURN, BATTLE_WON, BATTLE_LOST, BATTLE_FLED, BATTLE_CAUGHT };
enum Species { GOLDEN, FERNFOX, EMBERWING, PEBBLIT, SPECIES_COUNT }; enum Species { GOLDEN, FERNFOX, EMBERWING, PEBBLIT, TIDEFIN, SPECIES_COUNT };
enum { FISHER_X = 178, FISHER_Y = 80 };
enum { CAUGHT_CAPACITY = 5, SAVE_RECORD_SIZE = 19 };
enum BattleNotice { NOTICE_NONE, NOTICE_NO_BALLS, NOTICE_TEAM_FULL, NOTICE_BROKE_FREE };
enum WalkResult { WALK_NORMAL, WALK_MAP_CHANGED, WALK_ENCOUNTER }; enum WalkResult { WALK_NORMAL, WALK_MAP_CHANGED, WALK_ENCOUNTER };
struct BattleState { struct BattleState {
enum BattlePhase phase; enum BattlePhase phase;
enum Species enemy; enum Species enemy;
int sprout_hp, enemy_hp, move, turns; int sprout_hp, enemy_hp, move, turns;
enum BattleNotice notice;
}; };
extern struct BattleState battle; extern struct BattleState battle;
@@ -53,6 +62,19 @@ extern unsigned random_state;
extern int grass_steps; extern int grass_steps;
extern int partner_owned; extern int partner_owned;
extern int ball_count, balls_given; extern int ball_count, balls_given;
extern int rod_owned;
int near_water(int x, int y);
int cast_rod(int x, int y);
int start_battle(enum Species enemy);
extern int caught_count;
extern int active_member;
int choose_partner(int member);
int partner_hp(void);
const char *partner_name(void);
const char *partner_move(void);
void draw_member(int member, int x, int y, int small);
extern unsigned char caught_species[CAUGHT_CAPACITY];
extern const struct SpeciesInfo species[SPECIES_COUNT];
void reset_dirty_rows(void); void reset_dirty_rows(void);
void mark_dirty(int x, int y, int w, int h); void mark_dirty(int x, int y, int w, int h);
@@ -64,6 +86,8 @@ void blit_sprite(int x, int y, const char *const *art, int width, int rows,
void sprite(int x, int y, const char *const *art, int rows, const u16 *palette, void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
int mirrored); int mirrored);
int in_tall_grass(int x, int y); int in_tall_grass(int x, int y);
int beach_shoreline(int x);
u16 beach_scenery(int x, int y);
void restore(int x, int y, int w, int h); void restore(int x, int y, int w, int h);
void pause_movement_at(unsigned now); void pause_movement_at(unsigned now);
int movement_ticks_at(unsigned now); int movement_ticks_at(unsigned now);
@@ -74,6 +98,10 @@ int near_map_exit(int x, int y);
int near_route_sign(int x, int y); int near_route_sign(int x, int y);
int use_door(int *x, int *y); int use_door(int *x, int *y);
void reset_position(int *x, int *y); void reset_position(int *x, int *y);
int room_walkable(int room, int x, int y);
int location_valid(const struct Location *location);
void default_location(struct Location *out);
void enter_location(const struct Location *location, int *x, int *y);
enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks); enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks);
void scaled_creature(int x, int y, int scale, const u16 *palette); void scaled_creature(int x, int y, int scale, const u16 *palette);
void draw_wild_creature(enum Species enemy, int x, int y); void draw_wild_creature(enum Species enemy, int x, int y);
@@ -86,6 +114,25 @@ int battle_input(unsigned pressed);
void draw_battle_hud(void); void draw_battle_hud(void);
void draw_battle(void); void draw_battle(void);
void load_progress(void); void load_progress(void);
void saved_location(struct Location *out);
int save_progress(const struct Location *location);
enum SaveMenuPhase { SAVE_MENU_CLOSED, SAVE_MENU_OPTIONS, SAVE_MENU_SAVED, SAVE_MENU_FAILED,
SAVE_MENU_TEAM, SAVE_MENU_BAG };
enum { MENU_TEAM, MENU_BAG, MENU_SAVE, MENU_CLOSE, MENU_COUNT };
enum { SAVE_MENU_X = 160, SAVE_MENU_Y = 6, SAVE_MENU_W = 74, SAVE_MENU_H = 148 };
struct SaveMenuState {
enum SaveMenuPhase phase;
int selection;
unsigned previous;
};
extern struct SaveMenuState save_menu;
int save_menu_input(unsigned keys, int can_open, const struct Location *location);
void draw_save_menu(void);
void draw_menu_scene(int x, int y);
int menu_redraw_is_panel(enum SaveMenuPhase before, enum SaveMenuPhase after);
void draw_menu_panel(enum SaveMenuPhase before);
void draw_bag(void);
int has_caught(enum Species kind);
int near_npc(int x, int y); int near_npc(int x, int y);
int begin_conversation(int x, int y); int begin_conversation(int x, int y);
int advance_conversation(int page); int advance_conversation(int page);
@@ -94,8 +141,10 @@ void status(const char *message);
void location_status(int x, int y); void location_status(int x, int y);
void draw_scene(int x, int y); void draw_scene(int x, int y);
void draw_team(void); void draw_team(void);
void draw_team_member(int member);
#ifdef GAME_TEST #ifdef GAME_TEST
unsigned save_crc(const unsigned char *bytes, int length);
extern const unsigned char large_alphabet[26][7]; extern const unsigned char large_alphabet[26][7];
extern u16 background_cache[WIDTH * HEIGHT]; extern u16 background_cache[WIDTH * HEIGHT];
extern enum Room background_room; extern enum Room background_room;
@@ -107,7 +156,7 @@ extern const char *const creature_art[16];
extern const char *const person_art[20]; extern const char *const person_art[20];
extern const char *const person_back_art[20]; extern const char *const person_back_art[20];
extern const char *const person_side_art[20]; extern const char *const person_side_art[20];
extern const struct SpeciesInfo species[SPECIES_COUNT]; extern const char *const girl_art[20], *const girl_back_art[20], *const girl_side_art[20];
extern int gift_conversation; extern int gift_conversation;
extern const char *const dialogue[][2]; extern const char *const dialogue[][2];
extern const char *const professor_dialogue[][2]; extern const char *const professor_dialogue[][2];
@@ -118,7 +167,8 @@ extern int trail_head, ball_gift_conversation;
extern unsigned rectangle_calls, sprite_decodes, sprite_row_marks; extern unsigned rectangle_calls, sprite_decodes, sprite_row_marks;
extern const int DIALOGUE_PAGES; extern const int DIALOGUE_PAGES;
extern const char *const fernfox_art[24], *const emberwing_art[24], *const pebblit_art[24]; extern const char *const fernfox_art[24], *const emberwing_art[24], *const pebblit_art[24];
extern const struct WildArt wild_art[3]; extern const char *const tidefin_art[24];
extern const struct WildArt wild_art[4];
u16 cottage_scenery(int x, int y); u16 cottage_scenery(int x, int y);
u16 route_scenery(int x, int y); u16 route_scenery(int x, int y);
u16 village_scenery(int x, int y); u16 village_scenery(int x, int y);
@@ -129,8 +179,9 @@ enum Facing facing_for_keys(unsigned keys, enum Facing previous);
int use_map_exit(int *x, int *y, unsigned keys); int use_map_exit(int *x, int *y, unsigned keys);
int can_walk(int x, int y); int can_walk(int x, int y);
void person(int x, int y, int step, u16 clothes, u16 hat, enum Facing facing); void person(int x, int y, int step, u16 clothes, u16 hat, enum Facing facing);
void styled_person(int x, int y, int step, u16 clothes, u16 hair_or_hat,
enum Facing facing, enum CharacterStyle style);
unsigned next_random(void); unsigned next_random(void);
int start_battle(enum Species enemy);
int next_dialogue_page(int page); int next_dialogue_page(int page);
int receive_partner(void); int receive_partner(void);
int receive_balls(void); int receive_balls(void);
+60 -9
View File
@@ -1,4 +1,5 @@
#include "game.h" #include "game.h"
static void draw_bag_summary(void);
void status(const char *message) void status(const char *message)
{ {
@@ -10,7 +11,9 @@ void status(const char *message)
void location_status(int x, int y) void location_status(int x, int y)
{ {
if (near_map_exit(x, y)) { if (near_map_exit(x, y)) {
if (current_room == MELLON_GARDEN) status("WALK LEFT TO GREENLEAF VILLAGE"); if (current_room == MELLON_GARDEN)
status(x < 25 ? "WALK LEFT TO GREENLEAF VILLAGE" : "WALK DOWN TO SEASHELL BEACH");
else if (current_room == BEACH) status("WALK UP TO MELLONS GARDEN");
else if (current_room == ROUTE) status("WALK UP TO GREENLEAF VILLAGE"); else if (current_room == ROUTE) status("WALK UP TO GREENLEAF VILLAGE");
else status(x >= 216 ? "WALK RIGHT TO PROFESSOR MELLON" : "WALK DOWN TO SUNPETAL TRAIL"); else status(x >= 216 ? "WALK RIGHT TO PROFESSOR MELLON" : "WALK DOWN TO SUNPETAL TRAIL");
} }
@@ -20,6 +23,9 @@ void location_status(int x, int y)
else if (near_door(x, y)) status(indoors() ? "A GO OUTSIDE" : "A OPEN DOOR"); else if (near_door(x, y)) status(indoors() ? "A GO OUTSIDE" : "A OPEN DOOR");
else if (current_room == MELLON_GARDEN) status("PROFESSOR MELLONS GARDEN"); else if (current_room == MELLON_GARDEN) status("PROFESSOR MELLONS GARDEN");
else if (current_room == MELLON_HOUSE) status("PROFESSOR MELLONS HOUSE"); else if (current_room == MELLON_HOUSE) status("PROFESSOR MELLONS HOUSE");
else if (current_room == BEACH && near_npc(x, y)) status("A TALK TO FISHER CORAL");
else if (near_water(x, y)) status(rod_owned ? "FACE DOWN A USE POKE ROD" : "ASK FISHER CORAL FOR A POKE ROD");
else if (current_room == BEACH) status("SEASHELL BEACH");
else if (partner_owned && near_golden(x, y) && !near_npc(x, y)) status("A BATTLE THE GOLDEN CREATURE"); else if (partner_owned && near_golden(x, y) && !near_npc(x, y)) status("A BATTLE THE GOLDEN CREATURE");
else status(current_room == COTTAGE ? "INSIDE THE COTTAGE" : "GREENLEAF VILLAGE"); else status(current_room == COTTAGE ? "INSIDE THE COTTAGE" : "GREENLEAF VILLAGE");
} }
@@ -28,31 +34,62 @@ void draw_scene(int x, int y)
{ {
restore(0, 0, WIDTH, HEIGHT); restore(0, 0, WIDTH, HEIGHT);
location_status(x, y); location_status(x, y);
text(8, 148, "ARROWS MOVE A USE SELECT TEAM START RESET", RGB(5, 9, 11)); text(8, 148, "ARROWS MOVE A USE SELECT TEAM START MENU R MUSIC", RGB(5, 9, 11));
actors(x, y, 0); actors(x, y, 0);
} }
void draw_team(void) void draw_team(void)
{ {
draw_team_member(0);
}
void draw_team_member(int member)
{
if (member < 0 || member > caught_count ||
(member > 0 && caught_species[member - 1] >= SPECIES_COUNT)) member = 0;
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26); u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
rectangle(0, 0, WIDTH, HEIGHT, RGB(15, 24, 20)); rectangle(0, 0, WIDTH, HEIGHT, RGB(15, 24, 20));
status("YOUR TEAM"); status("YOUR TEAM");
rectangle(8, 25, 224, 112, ink); rectangle(8, 25, 224, 112, ink);
rectangle(10, 27, 220, 108, cream); rectangle(10, 27, 220, 108, cream);
if (partner_owned) { if (partner_owned) {
draw_sprout(50, 105, 3); if (member == 0) {
large_text(94, 42, "SPROUT", ink); draw_sprout(50, 105, 3);
large_text(94, 58, "GRASS TYPE", RGB(7, 17, 11)); large_text(94, 42, "SPROUT", ink);
large_text(94, 73, "LEVEL FIVE", ink); large_text(94, 58, "GRASS TYPE", RGB(7, 17, 11));
large_text(94, 73, "LEVEL FIVE", ink);
} else {
enum Species kind = (enum Species)caught_species[member - 1];
if (kind == GOLDEN) {
static const u16 gold[] = {
0, RGB(9, 8, 6), RGB(23, 15, 5), RGB(30, 23, 7),
RGB(31, 28, 14), RGB(31, 31, 28), RGB(29, 13, 10), RGB(17, 8, 7)
};
scaled_creature(50, 105, 3, gold);
} else draw_wild_creature(kind, 50, 105);
large_text(94, 42, species[kind].name, ink);
large_text(94, 58, species[kind].type, RGB(7, 17, 11));
large_text(94, 73, species[kind].level, ink);
}
char position[] = {(char)('1' + member), ' ', 'O', 'F', ' ', (char)('1' + caught_count), 0};
text(194, 32, position, ink);
text(94, 89, "HEALTH FULL", ink); text(94, 89, "HEALTH FULL", ink);
if (member == active_member) text(94, 109, "FOLLOWING AND BATTLING", ink);
rectangle(94, 99, 106, 6, RGB(7, 14, 11)); rectangle(94, 99, 106, 6, RGB(7, 14, 11));
rectangle(95, 100, 104, 4, RGB(10, 25, 12)); rectangle(95, 100, 104, 4, RGB(10, 25, 12));
} else { } else {
large_text(28, 57, "NO PARTNER YET", ink); large_text(28, 57, "NO PARTNER YET", ink);
large_text(28, 76, "VISIT PROFESSOR MELLON", ink); large_text(28, 76, "VISIT PROFESSOR MELLON", ink);
text(28, 96, "HIS HOUSE IS EAST OF GREENLEAF VILLAGE", ink); text(28, 96, "HER HOUSE IS EAST OF GREENLEAF VILLAGE", ink);
} }
/* A small bag summary is visible even before you receive Sprout. */ draw_bag_summary();
rectangle(0, 142, WIDTH, 18, cream);
text(8, 148, "LEFT RIGHT TEAM A CHOOSE B SELECT BACK", ink);
}
static void draw_bag_summary(void)
{
u16 ink = RGB(5, 8, 11);
rectangle(24, 118, 8, 8, ink); rectangle(24, 118, 8, 8, ink);
rectangle(25, 119, 6, 3, RGB(29, 8, 9)); rectangle(25, 119, 6, 3, RGB(29, 8, 9));
rectangle(25, 122, 6, 3, RGB(31, 31, 28)); rectangle(25, 122, 6, 3, RGB(31, 31, 28));
@@ -64,6 +101,20 @@ void draw_team(void)
count_text[count_index++] = (char)('0' + ball_count % 10); count_text[count_index++] = (char)('0' + ball_count % 10);
count_text[count_index] = 0; count_text[count_index] = 0;
text(88, 119, count_text, ink); text(88, 119, count_text, ink);
if (rod_owned) text(133, 119, "POKE ROD", ink);
}
void draw_bag(void)
{
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
rectangle(0, 0, WIDTH, HEIGHT, RGB(15, 24, 20));
status("YOUR BAG");
rectangle(8, 25, 224, 112, ink);
rectangle(10, 27, 220, 108, cream);
large_text(24, 45, "ITEMS", ink);
text(24, 66, "POKE BALLS ARE USED DURING BATTLE", ink);
text(24, 80, "THIS SCREEN IS READ ONLY", ink);
draw_bag_summary();
rectangle(0, 142, WIDTH, 18, cream); rectangle(0, 142, WIDTH, 18, cream);
text(8, 148, "SELECT OR A OR B TO RETURN", ink); text(8, 148, "A B BACK START CLOSE", ink);
} }
+47 -36
View File
@@ -1,5 +1,6 @@
/* Our first GBA game. No downloaded game art or existing ROM is used. */ /* Our first GBA game. No downloaded game art or existing ROM is used. */
#include "game.h" #include "game.h"
#include "audio.h"
#ifndef GAME_TEST #ifndef GAME_TEST
#define DISPLAY (*(volatile u16 *)0x04000000) #define DISPLAY (*(volatile u16 *)0x04000000)
@@ -65,11 +66,13 @@ int main(void)
DISPLAY = 0x0080; /* Hide the screen until the scene is drawn. */ DISPLAY = 0x0080; /* Hide the screen until the scene is drawn. */
int x = 108, y = 103, frame = 0, greeting = 0; int x = 108, y = 103, frame = 0, greeting = 0;
int dialogue_page = -1; /* -1 means we are not talking. */ int dialogue_page = -1; /* -1 means we are not talking. */
int team_open = 0;
unsigned previous = 0; unsigned previous = 0;
start_movement_timer(); start_movement_timer();
load_progress(); load_progress();
reset_follower(x, y); struct Location start;
saved_location(&start);
enter_location(&start, &x, &y);
music_init(movement_time());
reset_dirty_rows(); reset_dirty_rows();
draw_scene(x, y); draw_scene(x, y);
present_frame(); present_frame();
@@ -81,32 +84,38 @@ int main(void)
unsigned pressed = keys & ~previous; unsigned pressed = keys & ~previous;
previous = keys; previous = keys;
unsigned now = movement_time(); unsigned now = movement_time();
music_update(now);
if (pressed & R_BUTTON) music_toggle_mute();
enum SaveMenuPhase menu_before = save_menu.phase;
const struct Location here = {current_room, x, y, player_facing};
int menu_changed = save_menu_input(keys,
battle.phase == BATTLE_OFF && dialogue_page < 0, &here);
if (menu_before != SAVE_MENU_CLOSED || save_menu.phase != SAVE_MENU_CLOSED) {
pause_movement_at(now);
if (menu_changed) {
int panel_only = menu_redraw_is_panel(menu_before, save_menu.phase);
if (!panel_only) DISPLAY = 0x0080;
if (panel_only) draw_menu_panel(menu_before);
else draw_menu_scene(x, y);
present_frame();
if (!panel_only) DISPLAY = 0x0403;
pause_movement_at(movement_time());
} else present_frame();
continue;
}
int movement_ticks = 0; int movement_ticks = 0;
if (team_open || battle.phase != BATTLE_OFF || dialogue_page >= 0) if (battle.phase != BATTLE_OFF || dialogue_page >= 0)
pause_movement_at(now); pause_movement_at(now);
else else
movement_ticks = movement_ticks_at(now); movement_ticks = movement_ticks_at(now);
random_state += 0x9e3779b9u; /* Mix in how long the player waits. */ random_state += 0x9e3779b9u; /* Mix in how long the player waits. */
if (team_open) {
if (pressed & (SELECT | A | B)) {
team_open = 0;
DISPLAY = 0x0080;
draw_scene(x, y);
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
} else {
present_frame();
}
continue;
}
if (battle.phase != BATTLE_OFF) { if (battle.phase != BATTLE_OFF) {
if (battle_input(pressed)) { if (battle_input(pressed)) {
if (battle.phase == BATTLE_OFF) { if (battle.phase == BATTLE_OFF) {
greeting = 180; greeting = 180;
DISPLAY = 0x0080; DISPLAY = 0x0080;
draw_scene(x, y); draw_scene(x, y);
status("SPROUT RESTED AND IS READY AGAIN"); status("YOUR PARTNER RESTED AND IS READY AGAIN");
present_frame(); present_frame();
DISPLAY = 0x0403; DISPLAY = 0x0403;
pause_movement_at(movement_time()); pause_movement_at(movement_time());
@@ -141,28 +150,11 @@ int main(void)
present_frame(); present_frame();
continue; continue;
} }
if (pressed & SELECT) {
team_open = 1;
DISPLAY = 0x0080;
draw_team();
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
continue;
}
int old_x = x, old_y = y; int old_x = x, old_y = y;
int old_follower_x = follower_x, old_follower_y = follower_y; int old_follower_x = follower_x, old_follower_y = follower_y;
enum Facing old_facing = player_facing; enum Facing old_facing = player_facing;
enum WalkResult walk = WALK_NORMAL; enum WalkResult walk = WALK_NORMAL;
if (pressed & START) { walk = walk_movement(keys, &x, &y, movement_ticks);
reset_position(&x, &y);
greeting = 0;
location_status(x, y);
pause_movement_at(now);
movement_ticks = 0;
} else {
walk = walk_movement(keys, &x, &y, movement_ticks);
}
if (walk == WALK_MAP_CHANGED) { if (walk == WALK_MAP_CHANGED) {
greeting = 0; greeting = 0;
dialogue_page = -1; dialogue_page = -1;
@@ -192,7 +184,10 @@ int main(void)
restore(170, 54, 15, 24); restore(170, 54, 15, 24);
actors(x, y, 0); actors(x, y, 0);
present_frame(); present_frame();
for (int tick = 0; tick < 12; ++tick) wait_for_frame(); for (int tick = 0; tick < 12; ++tick) {
wait_for_frame();
music_update(movement_time());
}
} }
use_door(&x, &y); use_door(&x, &y);
greeting = 0; greeting = 0;
@@ -207,6 +202,7 @@ int main(void)
dialogue_page = 0; dialogue_page = 0;
greeting = 0; greeting = 0;
status(current_room == ROUTE ? "TALKING TO RANGER FINN" : status(current_room == ROUTE ? "TALKING TO RANGER FINN" :
current_room == BEACH ? "TALKING TO FISHER CORAL" :
current_room == MELLON_HOUSE ? "TALKING TO PROFESSOR MELLON" : "TALKING TO MILA"); current_room == MELLON_HOUSE ? "TALKING TO PROFESSOR MELLON" : "TALKING TO MILA");
} else if (current_room == ROUTE) { } else if (current_room == ROUTE) {
status(near_route_sign(x, y) ? "GREENLEAF VILLAGE IS TO THE NORTH" : status(near_route_sign(x, y) ? "GREENLEAF VILLAGE IS TO THE NORTH" :
@@ -218,6 +214,20 @@ int main(void)
} else if (current_room == MELLON_GARDEN) { } else if (current_room == MELLON_GARDEN) {
status("PROFESSOR MELLON IS INSIDE THE HOUSE"); status("PROFESSOR MELLON IS INSIDE THE HOUSE");
greeting = 180; greeting = 180;
} else if (current_room == BEACH) {
if (cast_rod(x, y)) {
greeting = 0;
DISPLAY = 0x0080;
draw_battle();
present_frame();
DISPLAY = 0x0403;
pause_movement_at(movement_time());
continue;
}
status(!rod_owned ? "TALK TO FISHER CORAL FOR A POKE ROD" :
!partner_owned ? "GET SPROUT FROM PROFESSOR MELLON FIRST" :
"FACE DOWN AT THE WATER AND PRESS A");
greeting = 180;
} else if (begin_battle(x, y)) { } else if (begin_battle(x, y)) {
greeting = 0; greeting = 0;
DISPLAY = 0x0080; DISPLAY = 0x0080;
@@ -248,6 +258,7 @@ int main(void)
near_map_exit(old_x, old_y) != near_map_exit(x, y) || near_map_exit(old_x, old_y) != near_map_exit(x, y) ||
near_route_sign(old_x, old_y) != near_route_sign(x, y) || near_route_sign(old_x, old_y) != near_route_sign(x, y) ||
near_npc(old_x, old_y) != near_npc(x, y) || near_npc(old_x, old_y) != near_npc(x, y) ||
near_water(old_x, old_y) != near_water(x, y) ||
near_golden(old_x, old_y) != near_golden(x, y))) near_golden(old_x, old_y) != near_golden(x, y)))
location_status(x, y); location_status(x, y);
} }
+135
View File
@@ -0,0 +1,135 @@
#include "game.h"
struct SaveMenuState save_menu;
static int team_member, team_parent;
static u16 panel_underlay[SAVE_MENU_H][SAVE_MENU_W];
static void copy_panel_underlay(int restoring)
{
for (int y = 0; y < SAVE_MENU_H; ++y)
for (int x = 0; x < SAVE_MENU_W; ++x) {
int pixel = (SAVE_MENU_Y + y) * WIDTH + SAVE_MENU_X + x;
if (restoring) SCREEN[pixel] = panel_underlay[y][x];
else panel_underlay[y][x] = SCREEN[pixel];
}
if (restoring) mark_dirty(SAVE_MENU_X, SAVE_MENU_Y, SAVE_MENU_W, SAVE_MENU_H);
}
void draw_menu_panel(enum SaveMenuPhase before)
{
/* World stays paused; fullscreen returns recapture after rebuilding actors/HUD. */
if (before == SAVE_MENU_CLOSED) copy_panel_underlay(0);
if (save_menu.phase == SAVE_MENU_CLOSED) copy_panel_underlay(1);
else draw_save_menu();
}
int save_menu_input(unsigned keys, int can_open, const struct Location *location)
{
unsigned pressed = keys & ~save_menu.previous;
save_menu.previous = keys;
if (save_menu.phase == SAVE_MENU_CLOSED) {
if (!can_open || !(pressed & (START | SELECT))) return 0;
save_menu.phase = pressed & START ? SAVE_MENU_OPTIONS : SAVE_MENU_TEAM;
team_parent = 0;
team_member = 0;
save_menu.selection = 0;
return 1;
}
if (pressed & START) {
save_menu.phase = SAVE_MENU_CLOSED;
return 1;
}
if (save_menu.phase == SAVE_MENU_TEAM || save_menu.phase == SAVE_MENU_BAG) {
if (pressed & (B | SELECT | A)) {
if (save_menu.phase == SAVE_MENU_TEAM && (pressed & A) &&
!(pressed & (B | SELECT)) && choose_partner(team_member))
reset_follower(location->x, location->y);
save_menu.phase = team_parent ? SAVE_MENU_OPTIONS : SAVE_MENU_CLOSED;
return 1;
}
unsigned direction = pressed & (LEFT | RIGHT);
if (save_menu.phase == SAVE_MENU_TEAM && partner_owned &&
(direction == LEFT || direction == RIGHT)) {
int members = caught_count + 1;
team_member = (team_member + (direction == RIGHT ? 1 : members - 1)) % members;
return 1;
}
return 0;
}
if (pressed & B) {
save_menu.phase = SAVE_MENU_CLOSED;
return 1;
}
if (save_menu.phase != SAVE_MENU_OPTIONS) {
if (!(pressed & A)) return 0;
save_menu.phase = SAVE_MENU_CLOSED;
return 1;
}
if (pressed & A) {
if (save_menu.selection == MENU_CLOSE) save_menu.phase = SAVE_MENU_CLOSED;
else if (save_menu.selection == MENU_SAVE)
save_menu.phase = save_progress(location) ? SAVE_MENU_SAVED : SAVE_MENU_FAILED;
else {
team_parent = 1;
team_member = 0;
save_menu.phase = save_menu.selection == MENU_TEAM ? SAVE_MENU_TEAM : SAVE_MENU_BAG;
}
return 1;
}
unsigned direction = pressed & (UP | DOWN);
if (direction == UP || direction == DOWN) {
save_menu.selection = (save_menu.selection + (direction == DOWN ? 1 : MENU_COUNT - 1)) % MENU_COUNT;
return 1;
}
return 0;
}
void draw_menu_scene(int x, int y)
{
if (save_menu.phase == SAVE_MENU_TEAM) draw_team_member(team_member);
else if (save_menu.phase == SAVE_MENU_BAG) draw_bag();
else {
draw_scene(x, y);
if (save_menu.phase != SAVE_MENU_CLOSED) {
copy_panel_underlay(0);
draw_save_menu();
}
}
}
int menu_redraw_is_panel(enum SaveMenuPhase before, enum SaveMenuPhase after)
{
return before <= SAVE_MENU_FAILED && after <= SAVE_MENU_FAILED &&
(before != SAVE_MENU_CLOSED || after != SAVE_MENU_CLOSED);
}
void draw_save_menu(void)
{
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
int x = SAVE_MENU_X, y = SAVE_MENU_Y;
rectangle(x, y, SAVE_MENU_W, SAVE_MENU_H, ink);
rectangle(x + 2, y + 2, SAVE_MENU_W - 4, SAVE_MENU_H - 4, cream);
large_text(x + 16, y + 10, "MENU", ink);
rectangle(x + 6, y + 23, SAVE_MENU_W - 12, 1, RGB(15, 24, 20));
text(x + 8, y + 99, "SAVES TEAM", ink);
text(x + 8, y + 108, "ITEMS AND", ink);
text(x + 8, y + 117, "LOCATION", ink);
if (save_menu.phase == SAVE_MENU_OPTIONS) {
int selected_y = y + 28 + save_menu.selection * 16;
rectangle(x + 6, selected_y, SAVE_MENU_W - 12, 16, RGB(23, 28, 21));
rectangle(x + 10, selected_y + 5, 1, 5, ink);
rectangle(x + 11, selected_y + 6, 1, 3, ink);
rectangle(x + 12, selected_y + 7, 1, 1, ink);
static const char *const labels[] = {"TEAM", "BAG", "SAVE", "CLOSE"};
for (int i = 0; i < MENU_COUNT; ++i) large_text(x + 20, y + 33 + i * 16, labels[i], ink);
text(x + 8, y + 126, "UP DOWN", ink);
text(x + 8, y + 133, "A SELECT", ink);
text(x + 8, y + 142, "B START CLOSE", ink);
} else {
int saved = save_menu.phase == SAVE_MENU_SAVED;
large_text(x + 16, y + 37, saved ? "GAME" : "SAVE", ink);
large_text(x + 16, y + 55, saved ? "SAVED" : "FAILED", ink);
text(x + 8, y + 126, "A B START", ink);
text(x + 8, y + 136, "TO CLOSE", ink);
}
}
+150
View File
@@ -0,0 +1,150 @@
#include "game.h"
static const char save_signature[] __attribute__((used)) = "SRAM_V113";
#ifndef GAME_TEST
#define SAVE_MEMORY ((volatile unsigned char *)0x0e000000)
#endif
static void configure_sram(void)
{
#ifndef GAME_TEST
volatile u16 *const waitcnt = (volatile u16 *)0x04000204;
*waitcnt = (u16)(*waitcnt | 3u); /* SRAM: eight waitstates; preserve ROM timings. */
#endif
}
/* Startup relocates this leaf reader; SRAM reads must execute from WRAM. */
#ifndef GAME_TEST
__attribute__((section(".ewram.text.sram_read"), noinline, long_call))
#endif
static void read_save_bytes(unsigned char *bytes)
{
for (int i = 0; i < SAVE_RECORD_SIZE; ++i) bytes[i] = SAVE_MEMORY[i];
}
struct Progress {
int partner, given, rod, count, members, active;
unsigned char kinds[CAUGHT_CAPACITY];
};
static void clear_progress(struct Progress *p)
{
p->partner = p->given = p->rod = p->count = p->members = p->active = 0;
for (int i = 0; i < CAUGHT_CAPACITY; ++i) p->kinds[i] = 0xff;
}
static int valid_progress(const struct Progress *p)
{
if (p->count < 0 || p->count > 99 || (!p->given && p->count != 0) ||
p->members < 0 || p->members > CAUGHT_CAPACITY || (p->members && !p->partner) ||
p->active < 0 || p->active > p->members || (p->active && !p->partner)) return 0;
for (int i = 0; i < CAUGHT_CAPACITY; ++i)
if (i < p->members ? p->kinds[i] >= SPECIES_COUNT : p->kinds[i] != 0xff) return 0;
return 1;
}
/* CRC-16/CCITT-FALSE: init FFFF, polynomial 1021, high byte first. */
TEST_INTERNAL unsigned save_crc(const unsigned char *bytes, int length)
{
unsigned crc = 0xffff;
for (int i = 0; i < length; ++i) {
crc ^= (unsigned)bytes[i] << 8;
for (int bit = 0; bit < 8; ++bit)
crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021u : 0u)) & 0xffff;
}
return crc;
}
static int read_progress(struct Progress *p, struct Location *location)
{
unsigned char bytes[SAVE_RECORD_SIZE];
configure_sram();
read_save_bytes(bytes);
clear_progress(p);
default_location(location);
if (bytes[0] != 'F' || bytes[1] != 'Q' || bytes[2] != 'S' || bytes[3] != 'P') return 0;
/* Keep Sprout from the original version-one save format. */
if (bytes[4] == 1) {
p->partner = bytes[5] == 1 && bytes[6] == 0xa5 && bytes[7] == 0x5a;
return p->partner && valid_progress(p);
}
unsigned flags = bytes[5], count = bytes[6], version = bytes[4];
if (flags > (version >= 5 ? 47u : version == 4 ? 7u : 3u)) return 0;
if (version == 2) {
if (bytes[8] != 0x5a || bytes[7] != (0xa5 ^ 2 ^ flags ^ count)) return 0;
} else if (version >= 3 && version <= 6) {
unsigned checksum = 0xa5 ^ version ^ flags ^ count ^ bytes[7];
for (int i = 0; i < CAUGHT_CAPACITY; ++i) {
p->kinds[i] = bytes[8 + i];
checksum ^= bytes[8 + i];
}
if (version == 6) {
if (save_crc(bytes, 17) != ((unsigned)bytes[17] << 8 | bytes[18])) return 0;
location->room = bytes[13]; location->facing = bytes[14];
location->x = bytes[15]; location->y = bytes[16];
} else if (bytes[14] != 0x5a || bytes[13] != checksum) return 0;
p->members = bytes[7];
} else return 0;
p->partner = !!(flags & 1);
p->given = !!(flags & 2);
p->rod = !!(flags & 4);
p->count = (int)count;
p->active = version >= 5 ? (int)(flags >> 3) : 0;
return valid_progress(p);
}
void load_progress(void)
{
struct Progress p;
struct Location location;
if (!read_progress(&p, &location)) clear_progress(&p);
partner_owned = p.partner;
balls_given = p.given;
rod_owned = p.rod;
ball_count = p.count;
caught_count = p.members;
active_member = p.active;
for (int i = 0; i < CAUGHT_CAPACITY; ++i) caught_species[i] = p.kinds[i];
}
void saved_location(struct Location *out)
{
struct Progress p;
if (!read_progress(&p, out) || !location_valid(out)) default_location(out);
}
int save_progress(const struct Location *location)
{
struct Progress p, readback;
struct Location restored;
p.partner = !!partner_owned;
p.given = !!balls_given;
p.rod = !!rod_owned;
p.count = ball_count;
p.members = caught_count;
p.active = active_member;
for (int i = 0; i < CAUGHT_CAPACITY; ++i)
p.kinds[i] = i < caught_count ? caught_species[i] : 0xff;
if (!valid_progress(&p) || !location_valid(location)) return 0;
unsigned char bytes[SAVE_RECORD_SIZE];
bytes[0] = 'F'; bytes[1] = 'Q'; bytes[2] = 'S'; bytes[3] = 'P'; bytes[4] = 6;
bytes[5] = (unsigned char)(p.partner | (p.given << 1) | (p.rod << 2) | (p.active << 3));
bytes[6] = (unsigned char)p.count; bytes[7] = (unsigned char)p.members;
for (int i = 0; i < CAUGHT_CAPACITY; ++i) bytes[8 + i] = p.kinds[i];
bytes[13] = (unsigned char)location->room; bytes[14] = (unsigned char)location->facing;
bytes[15] = (unsigned char)location->x; bytes[16] = (unsigned char)location->y;
unsigned crc = save_crc(bytes, 17);
bytes[17] = (unsigned char)(crc >> 8); bytes[18] = (unsigned char)crc;
/* Invalidate first, commit last; a single record cannot recover interrupted writes. */
configure_sram();
SAVE_MEMORY[0] = 0;
for (int i = 1; i < SAVE_RECORD_SIZE; ++i) SAVE_MEMORY[i] = bytes[i];
SAVE_MEMORY[0] = 'F';
if (!read_progress(&readback, &restored) || p.partner != readback.partner || p.given != readback.given ||
p.rod != readback.rod || p.count != readback.count || p.members != readback.members ||
p.active != readback.active || location->room != restored.room ||
location->x != restored.x || location->y != restored.y || location->facing != restored.facing) return 0;
for (int i = 0; i < CAUGHT_CAPACITY; ++i)
if (p.kinds[i] != readback.kinds[i]) return 0;
return 1;
}
+4 -3
View File
@@ -310,10 +310,10 @@ TEST_INTERNAL u16 mellon_garden_scenery(int x, int y)
{ {
u16 color = village_scenery(x, y); u16 color = village_scenery(x, y);
if (y < 18 || y >= 142) return color; if (y < 18 || y >= 142) return color;
/* This garden connects west to town, not east or south. */ /* Connect west to town and south to the beach; the east boundary stays closed. */
if (x < 8 && y > 106 && y < 120) color = RGB(29, 27, 18); if (x < 8 && y > 106 && y < 120) color = RGB(29, 27, 18);
if ((x >= 232 && y > 106 && y < 120) || if (x >= 232 && y > 106 && y < 120) color = RGB(9, 20, 10);
(y >= 136 && x > 96 && x < 120)) color = RGB(9, 20, 10); if (x >= 97 && x < 120 && y >= 128) color = RGB(31, 28, 19);
/* Melon beds take the place of the village pond. */ /* Melon beds take the place of the village pond. */
if (x >= 22 && x < 76 && y >= 44 && y < 86) { if (x >= 22 && x < 76 && y >= 44 && y < 86) {
color = RGB(20, 15, 9); color = RGB(20, 15, 9);
@@ -364,6 +364,7 @@ TEST_INTERNAL u16 scenery(int x, int y)
if (current_room == COTTAGE) return cottage_scenery(x, y); if (current_room == COTTAGE) return cottage_scenery(x, y);
if (current_room == MELLON_GARDEN) return mellon_garden_scenery(x, y); if (current_room == MELLON_GARDEN) return mellon_garden_scenery(x, y);
if (current_room == MELLON_HOUSE) return mellon_house_scenery(x, y); if (current_room == MELLON_HOUSE) return mellon_house_scenery(x, y);
if (current_room == BEACH) return beach_scenery(x, y);
return village_scenery(x, y); return village_scenery(x, y);
} }
+59 -42
View File
@@ -3,47 +3,48 @@
int partner_owned = 0; int partner_owned = 0;
TEST_INTERNAL int gift_conversation = 0; TEST_INTERNAL int gift_conversation = 0;
int ball_count = 0, balls_given = 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; TEST_INTERNAL int ball_gift_conversation = 0;
/* mGBA detects this marker and provides cartridge SRAM for the gift record. */ int choose_partner(int member)
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 (!partner_owned || battle.phase != BATTLE_OFF || member < 0 || member > caught_count)
if (SAVE_MEMORY[0] != 'F' || SAVE_MEMORY[1] != 'Q' || return 0;
SAVE_MEMORY[2] != 'S' || SAVE_MEMORY[3] != 'P') return; active_member = member;
/* Keep Sprout from the original version-one save format. */ return 1;
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) const char *partner_name(void)
{ {
unsigned flags = (partner_owned ? 1u : 0u) | (balls_given ? 2u : 0u); return active_member ? species[caught_species[active_member - 1]].name : "SPROUT";
/* Commit the first magic byte last so an interrupted write is invalid. */ }
SAVE_MEMORY[0] = 0;
SAVE_MEMORY[1] = 'Q'; int partner_hp(void)
SAVE_MEMORY[2] = 'S'; {
SAVE_MEMORY[3] = 'P'; return active_member ? species[caught_species[active_member - 1]].hp : SPROUT_HP;
SAVE_MEMORY[4] = 2; }
SAVE_MEMORY[5] = (unsigned char)flags;
SAVE_MEMORY[6] = (unsigned char)ball_count; const char *partner_move(void)
SAVE_MEMORY[7] = (unsigned char)(0xa5 ^ 2 ^ flags ^ (unsigned)ball_count); {
SAVE_MEMORY[8] = 0x5a; if (!active_member) return "LEAF TAP";
SAVE_MEMORY[0] = 'F'; 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";
}
}
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. */ /* Change these lines to write Mila's conversation! Use CAPITAL letters. */
@@ -66,15 +67,27 @@ TEST_INTERNAL const char *const professor_followup[DIALOGUE_PAGES][2] = {
}; };
TEST_INTERNAL const char *const ranger_dialogue[DIALOGUE_PAGES][2] = { TEST_INTERNAL const char *const ranger_dialogue[DIALOGUE_PAGES][2] = {
{"HI I AM RANGER FINN", "HERE ARE FIVE POKE BALLS FOR YOU"}, {"HI I AM RANGER FINN", "HERE ARE FIVE POKE BALLS FOR YOU"},
{"YOU RECEIVED FIVE POKE BALLS", "KEEP THEM SAFE FOR YOUR ADVENTURE"} {"YOU RECEIVED FIVE POKE BALLS", "PRESS SELECT IN BATTLE TO THROW"}
}; };
TEST_INTERNAL const char *const ranger_followup[DIALOGUE_PAGES][2] = { TEST_INTERNAL const char *const ranger_followup[DIALOGUE_PAGES][2] = {
{"YOUR POKE BALLS ARE IN YOUR BAG", "OPEN YOUR TEAM WITH SELECT TO LOOK"}, {"WEAKEN WILD CREATURES FIRST", "LOW HEALTH MAKES CATCHING EASIER"},
{"ENJOY EXPLORING SUNPETAL TRAIL", "TAKE GOOD CARE OF YOUR PARTNER"} {"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) 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) if (current_room == ROUTE)
return x >= RANGER_X - 24 && x <= RANGER_X + 24 && return x >= RANGER_X - 24 && x <= RANGER_X + 24 &&
y >= RANGER_Y - 22 && y <= RANGER_Y + 22; y >= RANGER_Y - 22 && y <= RANGER_Y + 22;
@@ -95,6 +108,7 @@ int begin_conversation(int x, int y)
if (!near_npc(x, y)) return 0; if (!near_npc(x, y)) return 0;
gift_conversation = current_room == MELLON_HOUSE && !partner_owned; gift_conversation = current_room == MELLON_HOUSE && !partner_owned;
ball_gift_conversation = current_room == ROUTE && !balls_given; ball_gift_conversation = current_room == ROUTE && !balls_given;
rod_gift_conversation = current_room == BEACH && !rod_owned;
return 1; return 1;
} }
@@ -102,7 +116,6 @@ TEST_INTERNAL int receive_partner(void)
{ {
if (current_room != MELLON_HOUSE || !gift_conversation || partner_owned) return 0; if (current_room != MELLON_HOUSE || !gift_conversation || partner_owned) return 0;
partner_owned = 1; partner_owned = 1;
save_progress();
reset_follower(PROF_X, PROF_Y + 22); reset_follower(PROF_X, PROF_Y + 22);
return 1; return 1;
} }
@@ -113,7 +126,6 @@ TEST_INTERNAL int receive_balls(void)
ball_count += 5; ball_count += 5;
if (ball_count > 99) ball_count = 99; if (ball_count > 99) ball_count = 99;
balls_given = 1; balls_given = 1;
save_progress();
return 1; return 1;
} }
@@ -122,6 +134,10 @@ int advance_conversation(int page)
int next = next_dialogue_page(page); int next = next_dialogue_page(page);
if (next == 1 && gift_conversation) receive_partner(); if (next == 1 && gift_conversation) receive_partner();
if (next == 1 && ball_gift_conversation) receive_balls(); if (next == 1 && ball_gift_conversation) receive_balls();
if (next == 1 && rod_gift_conversation && current_room == BEACH && !rod_owned) {
rod_owned = 1;
}
if (next < 0) rod_gift_conversation = 0;
if (next < 0) gift_conversation = ball_gift_conversation = 0; if (next < 0) gift_conversation = ball_gift_conversation = 0;
return next; return next;
} }
@@ -136,10 +152,11 @@ void dialogue_box(int page)
rectangle(16, 110, 208, 1, RGB(23, 26, 22)); rectangle(16, 110, 208, 1, RGB(23, 26, 22));
int professor = current_room == MELLON_HOUSE; int professor = current_room == MELLON_HOUSE;
int ranger = current_room == ROUTE; int ranger = current_room == ROUTE;
const char *const (*lines)[2] = ranger ? const char *const (*lines)[2] = current_room == BEACH ?
(rod_gift_conversation ? fisher_dialogue : fisher_followup) : ranger ?
(ball_gift_conversation ? ranger_dialogue : ranger_followup) : professor ? (ball_gift_conversation ? ranger_dialogue : ranger_followup) : professor ?
(gift_conversation ? professor_dialogue : professor_followup) : dialogue; (gift_conversation ? professor_dialogue : professor_followup) : dialogue;
large_text(16, 102, ranger ? "RANGER FINN" : professor ? "PROF MELLON" : "MILA", RGB(7, 16, 12)); 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); text(192, 103, page == DIALOGUE_PAGES - 1 ? "A CLOSE" : "A NEXT", ink);
large_text(16, 115, lines[page][0], ink); large_text(16, 115, lines[page][0], ink);
large_text(16, 127, lines[page][1], ink); large_text(16, 127, lines[page][1], ink);
+2 -1
View File
@@ -105,7 +105,8 @@ void large_text(int x, int y, const char *message, u16 color)
} }
} }
enum { SPRITE_SIZE = 24, SPRITE_CACHE_SIZE = 7 }; /* Keep all artwork images resident, including both character styles and Tidefin. */
enum { SPRITE_SIZE = 24, SPRITE_CACHE_SIZE = 15 };
struct DecodedSprite { struct DecodedSprite {
const char *const *art; const char *const *art;
unsigned char width, rows; unsigned char width, rows;
+26 -1
View File
@@ -96,10 +96,35 @@ static const u16 pebblit_palette[] = {
RGB(9, 23, 26), RGB(26, 31, 30), RGB(11, 11, 15), RGB(18, 25, 28) RGB(9, 23, 26), RGB(26, 31, 30), RGB(11, 11, 15), RGB(18, 25, 28)
}; };
TEST_INTERNAL const char *const tidefin_art[] = {
"........................", "........................", "........................", "........................",
"............11..........",
"...........1331.........",
".........113331.........",
".......112333211........",
"......12334444321.......",
".....1234455444321..11..",
"....1234445144443211331.",
"....1234445144444333331.",
"....1234444444444333331.",
".....12344664443211131..",
"......12344444321...11..",
".......112333211........",
".........13331..........",
"..........131...........",
"...........1............",
"........................", "........................", "........................", "........................", "........................"
};
static const u16 tidefin_palette[] = {
0, RGB(4, 8, 14), RGB(4, 13, 23), RGB(7, 21, 29),
RGB(16, 28, 30), RGB(31, 31, 28), RGB(28, 15, 18)
};
TEST_INTERNAL const struct WildArt wild_art[] = { TEST_INTERNAL const struct WildArt wild_art[] = {
{fernfox_art, fernfox_palette}, {fernfox_art, fernfox_palette},
{emberwing_art, emberwing_palette}, {emberwing_art, emberwing_palette},
{pebblit_art, pebblit_palette} {pebblit_art, pebblit_palette},
{tidefin_art, tidefin_palette}
}; };
#endif #endif
+64 -18
View File
@@ -79,14 +79,43 @@ int near_map_exit(int x, int y)
{ {
if (current_room == OUTSIDE && x >= 216 && x <= 222 && y >= 108 && y <= 117) if (current_room == OUTSIDE && x >= 216 && x <= 222 && y >= 108 && y <= 117)
return 1; return 1;
if (current_room == MELLON_GARDEN) if (current_room == MELLON_GARDEN && x >= 17 && x <= 24 && y >= 108 && y <= 117)
return x >= 17 && x <= 24 && y >= 108 && y <= 117; return 1;
if (x < 102 || x > 114) return 0; if (x < 102 || x > 114) return 0;
if (current_room == OUTSIDE) return y >= 124 && y <= 131; if (current_room == OUTSIDE || current_room == MELLON_GARDEN) return y >= 124 && y <= 131;
if (current_room == ROUTE) return y >= 47 && y <= 54; if (current_room == ROUTE || current_room == BEACH) return y >= 47 && y <= 54;
return 0; 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. */ /* Walk across a map boundary. Arrivals are away from the return trigger. */
TEST_INTERNAL int use_map_exit(int *x, int *y, unsigned keys) TEST_INTERNAL int use_map_exit(int *x, int *y, unsigned keys)
{ {
@@ -111,13 +140,21 @@ TEST_INTERNAL int use_map_exit(int *x, int *y, unsigned keys)
*x = 210; *x = 210;
*y = 113; *y = 113;
player_facing = FACING_LEFT; 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 { } else {
return 0; return 0;
} }
door_open = 0; door_open = 0;
grass_steps = 0; reset_arrival(*x, *y);
diagonal_fraction = 0;
reset_follower(*x, *y);
return 1; return 1;
} }
@@ -149,25 +186,34 @@ int use_door(int *x, int *y)
} }
door_open = 0; door_open = 0;
player_facing = FACING_DOWN; player_facing = FACING_DOWN;
grass_steps = 0; reset_arrival(*x, *y);
diagonal_fraction = 0;
reset_follower(*x, *y);
return 1; return 1;
} }
void reset_position(int *x, int *y) void reset_position(int *x, int *y)
{ {
*x = indoors() ? 120 : current_room == MELLON_GARDEN ? 31 : 108; *x = indoors() ? 120 : current_room == MELLON_GARDEN ? 31 : 108;
*y = indoors() ? 119 : current_room == ROUTE ? 60 : current_room == MELLON_GARDEN ? 113 : 103; *y = indoors() ? 119 : (current_room == ROUTE || current_room == BEACH) ? 60 :
current_room == MELLON_GARDEN ? 113 : 103;
player_facing = FACING_DOWN; player_facing = FACING_DOWN;
grass_steps = 0; reset_arrival(*x, *y);
diagonal_fraction = 0;
reset_follower(*x, *y);
} }
TEST_INTERNAL int can_walk(int x, int y) TEST_INTERNAL int can_walk(int x, int y)
{ {
if (current_room == ROUTE) { 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 < 32 || x > 207 || y < 47 || y > 131) return 0;
if (x > 143 && x < 216 && y < 85) return 0; /* Pond. */ if (x > 143 && x < 216 && y < 85) return 0; /* Pond. */
if (x > 116 && x < 154 && y > 53 && y < 79) return 0; /* Sign. */ if (x > 116 && x < 154 && y > 53 && y < 79) return 0; /* Sign. */
@@ -176,19 +222,19 @@ TEST_INTERNAL int can_walk(int x, int y)
y > RANGER_Y - 9 && y < RANGER_Y + 9) return 0; y > RANGER_Y - 9 && y < RANGER_Y + 9) return 0;
return 1; return 1;
} }
if (indoors()) { if (room == COTTAGE || room == MELLON_HOUSE) {
if (x < 34 || x > 205 || y < 75 || y > 131) return 0; if (x < 34 || x > 205 || y < 75 || y > 131) return 0;
if (x > 26 && x < 77 && y < 105) return 0; /* Bed. */ if (x > 26 && x < 77 && y < 105) return 0; /* Bed. */
if (x > 141 && x < 196 && y < 109) return 0; /* Table. */ if (x > 141 && x < 196 && y < 109) return 0; /* Table. */
if (x > 187 && y < 85) return 0; /* Plant. */ if (x > 187 && y < 85) return 0; /* Plant. */
if (current_room == MELLON_HOUSE && x > PROF_X - 13 && x < PROF_X + 13 && if (room == MELLON_HOUSE && x > PROF_X - 13 && x < PROF_X + 13 &&
y > PROF_Y - 9 && y < PROF_Y + 9) return 0; y > PROF_Y - 9 && y < PROF_Y + 9) return 0;
return 1; return 1;
} }
if (x < 17 || x > 222 || y < 47 || y > 131) return 0; if (x < 17 || x > 222 || y < 47 || y > 131) return 0;
if (x > 16 && x < 82 && y > 42 && y < 92) return 0; if (x > 16 && x < 82 && y > 42 && y < 92) return 0;
if (x > 141 && x < 212 && y < 78) return 0; if (x > 141 && x < 212 && y < 78) return 0;
if (current_room == OUTSIDE && x > NPC_X - 13 && x < NPC_X + 13 && if (room == OUTSIDE && x > NPC_X - 13 && x < NPC_X + 13 &&
y > NPC_Y - 9 && y < NPC_Y + 9) return 0; y > NPC_Y - 9 && y < NPC_Y + 9) return 0;
return 1; return 1;
} }
+145
View File
@@ -0,0 +1,145 @@
#include "test_support.h"
#include "../source/audio.h"
static unsigned step_time(enum MusicTrack track, unsigned step)
{
unsigned numerator = step * MOVEMENT_CLOCK_HZ * 60u;
unsigned denominator = music_songs[track].bpm * 4u;
return (numerator + denominator - 1) / denominator;
}
static void assert_sound_ranges(void)
{
assert(music_state.step >= 0 && music_state.step < 64);
assert((music_registers[SOUND_LEAD_FREQ] & 0x7fff) < 2048);
assert((music_registers[SOUND_BASS_FREQ] & 0x7fff) < 2048);
assert((music_registers[SOUND_LEAD_ENV] >> 12) <= 6);
assert((music_registers[SOUND_BASS_ENV] >> 12) <= 3);
assert((music_registers[SOUND_NOISE_ENV] >> 12) <= 2);
}
void check_music(void)
{
for (unsigned note = 48; note <= 84; ++note) {
assert(music_frequency(note) > 0 && music_frequency(note) < 2048);
if (note > 48) assert(music_frequency(note) > music_frequency(note - 1));
}
assert(music_frequency(0) == 0 && music_frequency(255) == 0);
for (int track = MUSIC_VILLAGE; track < MUSIC_TRACK_COUNT; ++track) {
const struct MusicSong *song = &music_songs[track];
assert(song->bpm >= 60 && song->bpm <= 180);
for (int step = 0; step < 64; ++step) {
unsigned note = song->lead[step];
assert(note == 0 || note == 255 || (note >= 48 && note <= 84));
}
for (int step = 0; step < 16; ++step)
assert(song->bass[step] >= 48 && song->bass[step] <= 84);
}
assert(music_choose_track(OUTSIDE, BATTLE_OFF) == MUSIC_VILLAGE);
assert(music_choose_track(MELLON_GARDEN, BATTLE_OFF) == MUSIC_VILLAGE);
assert(music_choose_track(ROUTE, BATTLE_OFF) == MUSIC_TRAIL);
assert(music_choose_track(COTTAGE, BATTLE_OFF) == MUSIC_TRAIL);
assert(music_choose_track(MELLON_HOUSE, BATTLE_OFF) == MUSIC_TRAIL);
assert(music_choose_track(BEACH, BATTLE_OFF) == MUSIC_TRAIL);
for (int phase = BATTLE_TURN; phase <= BATTLE_CAUGHT; ++phase)
assert(music_choose_track(ROUTE, (enum BattlePhase)phase) == MUSIC_BATTLE);
current_room = OUTSIDE;
battle.phase = BATTLE_OFF;
music_init(0);
assert(music_registers[SOUND_MASTER] == 0x80);
assert(music_registers[SOUND_MIX] == 0xbb77 && music_registers[SOUND_RATIO] == 1);
assert(music_registers[SOUND_SWEEP] == 8);
assert(music_state.track == MUSIC_VILLAGE && !music_state.muted);
unsigned writes = music_write_count;
music_update(0);
music_update(step_time(MUSIC_VILLAGE, 1) - 1);
assert(music_state.step == 0 && music_write_count == writes);
music_update(step_time(MUSIC_VILLAGE, 1));
assert(music_state.step == 1 && music_write_count == writes); /* Tie: no retrigger. */
u16 first_pitch = music_registers[SOUND_LEAD_FREQ];
music_update(step_time(MUSIC_VILLAGE, 2));
assert(music_state.step == 2 && music_registers[SOUND_LEAD_FREQ] != first_pitch);
/* Songs keep the same tempo at different rendering/update frequencies. */
const int rates[] = {15, 30, 60, 120};
for (int track = MUSIC_VILLAGE; track < MUSIC_TRACK_COUNT; ++track) {
current_room = track == MUSIC_TRAIL ? ROUTE : OUTSIDE;
battle.phase = track == MUSIC_BATTLE ? BATTLE_TURN : BATTLE_OFF;
for (unsigned rate = 0; rate < sizeof(rates) / sizeof(rates[0]); ++rate) {
music_init(0);
for (int frame = 1; frame <= rates[rate] * 12; ++frame) {
music_update((unsigned)(frame * MOVEMENT_CLOCK_HZ / rates[rate]));
assert_sound_ranges();
}
unsigned steps = 12 * music_songs[track].bpm * 4u / 60u;
unsigned remainder = (12 * MOVEMENT_CLOCK_HZ * music_songs[track].bpm * 4u) %
(MOVEMENT_CLOCK_HZ * 60u);
assert(music_state.track == (enum MusicTrack)track);
assert(music_state.step == (int)(steps % 64) && music_state.phase == remainder);
}
}
/* Scene changes select a tune; overlays and same-theme maps keep the phrase. */
current_room = OUTSIDE;
battle.phase = BATTLE_OFF;
music_init(0);
music_update(step_time(MUSIC_VILLAGE, 5));
int step = music_state.step;
current_room = MELLON_GARDEN;
music_update(music_state.last);
assert(music_state.track == MUSIC_VILLAGE && music_state.step == step);
current_room = ROUTE;
music_update(music_state.last);
assert(music_state.track == MUSIC_TRAIL && music_state.step == 0);
battle.phase = BATTLE_TURN;
music_update(music_state.last);
assert(music_state.track == MUSIC_BATTLE && music_state.step == 0);
battle.phase = BATTLE_WON;
music_update(music_state.last);
assert(music_state.track == MUSIC_BATTLE);
battle.phase = BATTLE_OFF;
music_update(music_state.last);
assert(music_state.track == MUSIC_TRAIL && music_state.step == 0);
/* Muting stops all voices, stays muted across maps, and resumes on ties. */
music_toggle_mute();
assert(music_state.muted && music_registers[SOUND_MIX] == 0);
assert(music_registers[SOUND_LEAD_ENV] == 0 && music_registers[SOUND_BASS_ENV] == 0);
assert(music_registers[SOUND_NOISE_ENV] == 0);
writes = music_write_count;
current_room = OUTSIDE;
music_update(music_state.last);
unsigned start = music_state.last;
music_update(start + step_time(MUSIC_VILLAGE, 1));
assert(music_state.step == 1 && music_write_count == writes);
music_toggle_mute();
assert(!music_state.muted && music_registers[SOUND_MIX] == 0xbb77);
assert(music_registers[SOUND_LEAD_ENV] != 0 && music_registers[SOUND_BASS_ENV] != 0);
assert((music_registers[SOUND_LEAD_FREQ] & 2047) == music_frequency(72));
music_init(0);
for (unsigned index = 1; index <= 63; ++index) music_update(step_time(MUSIC_VILLAGE, index));
assert(music_state.step == 63 && music_registers[SOUND_LEAD_ENV] == 0); /* Rest. */
music_update(step_time(MUSIC_VILLAGE, 64));
assert(music_state.step == 0 && music_registers[SOUND_LEAD_ENV] != 0); /* Loop. */
music_init(0xffffff00u);
music_update(0x00001000u);
assert(music_state.step == 1); /* 32-bit hardware-clock rollover. */
writes = music_write_count;
music_update(music_state.last + MOVEMENT_CLOCK_HZ * 60u);
assert(music_write_count - writes <= 6); /* No missed-note replay loop. */
assert_sound_ranges();
/* The sequencer does not consume movement time or change game/save state. */
pause_movement_at(0);
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
music_update(music_state.last + 100);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == MAX_MOVEMENT_TICKS);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
current_room = OUTSIDE;
battle.phase = BATTLE_OFF;
music_init(0);
puts("Music: 3 original tunes, stable tempo, scene switching, mute, ties, loops, and clock checks passed");
}
+4 -4
View File
@@ -5,7 +5,7 @@ void check_following_and_battle(void)
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT]; static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
static const unsigned directions[] = {RIGHT, DOWN, LEFT, UP}; static const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
partner_owned = 1; partner_owned = 1;
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) { for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room; current_room = (enum Room)room;
int x, y; int x, y;
reset_position(&x, &y); reset_position(&x, &y);
@@ -78,7 +78,7 @@ void check_following_and_battle(void)
write_preview("build/battle-preview.ppm"); write_preview("build/battle-preview.ppm");
copy_pending_rows(visible); copy_pending_rows(visible);
assert(!battle_input(0)); assert(!battle_input(0));
assert(!battle_input(SELECT)); assert(battle_input(SELECT) && battle.notice == NOTICE_NO_BALLS);
assert(battle.turns == 0); assert(battle.turns == 0);
assert(battle_input(RIGHT)); assert(battle_input(RIGHT));
assert(battle.move == 1 && battle.enemy_hp == GOLDEN_HP); assert(battle.move == 1 && battle.enemy_hp == GOLDEN_HP);
@@ -126,7 +126,7 @@ void check_following_and_battle(void)
void check_wild_encounters(void) void check_wild_encounters(void)
{ {
for (int kind = 0; kind < 3; ++kind) { for (int kind = 0; kind < 4; ++kind) {
int opaque = 0; int opaque = 0;
for (int row = 0; row < 24; ++row) { for (int row = 0; row < 24; ++row) {
assert(strlen(wild_art[kind].rows[row]) == 24); assert(strlen(wild_art[kind].rows[row]) == 24);
@@ -137,7 +137,7 @@ void check_wild_encounters(void)
++opaque; ++opaque;
} }
} }
assert(opaque > 180); /* Full original sprites, not palette swaps. */ assert(opaque > (kind == TIDEFIN - FERNFOX ? 100 : 180)); /* Fish has a slimmer silhouette. */
} }
partner_owned = 1; partner_owned = 1;
current_room = OUTSIDE; current_room = OUTSIDE;
+304
View File
@@ -0,0 +1,304 @@
#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");
}
+29 -1
View File
@@ -43,6 +43,9 @@ int main(void)
check_art(person_art, 20, 11); check_art(person_art, 20, 11);
check_art(person_back_art, 20, 13); check_art(person_back_art, 20, 13);
check_art(person_side_art, 20, 13); check_art(person_side_art, 20, 13);
check_art(girl_art, 20, 13);
check_art(girl_back_art, 20, 13);
check_art(girl_side_art, 20, 13);
check_art(creature_art, 16, 8); check_art(creature_art, 16, 8);
assert(facing_for_keys(UP, FACING_DOWN) == FACING_UP); assert(facing_for_keys(UP, FACING_DOWN) == FACING_UP);
assert(facing_for_keys(DOWN, FACING_UP) == FACING_DOWN); assert(facing_for_keys(DOWN, FACING_UP) == FACING_DOWN);
@@ -76,7 +79,7 @@ int main(void)
actors(x, y, 0); actors(x, y, 0);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0); assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
} }
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) { for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room; current_room = (enum Room)room;
reset_position(&x, &y); reset_position(&x, &y);
if (current_room == OUTSIDE) x = 130; if (current_room == OUTSIDE) x = 130;
@@ -103,6 +106,23 @@ int main(void)
person(85, 116, 1, shirt, RGB(28, 8, 9), FACING_LEFT); person(85, 116, 1, shirt, RGB(28, 8, 9), FACING_LEFT);
person(130, 126, 0, shirt, RGB(28, 8, 9), FACING_RIGHT); person(130, 126, 0, shirt, RGB(28, 8, 9), FACING_RIGHT);
write_preview("build/directions-preview.ppm"); write_preview("build/directions-preview.ppm");
for (int facing = FACING_DOWN; facing <= FACING_RIGHT; ++facing) {
int gx = 45 + facing * 45;
styled_person(gx, 75, facing & 1, RGB(4, 21, 13), RGB(12, 7, 4),
(enum Facing)facing, GIRL_STYLE);
styled_person(gx, 125, facing & 1, RGB(29, 30, 27), RGB(20, 21, 19),
(enum Facing)facing, GIRL_STYLE);
}
write_preview("build/girl-directions-preview.ppm");
current_room = MELLON_HOUSE;
render_scene(108, 119);
memcpy(expected, SCREEN, sizeof(expected));
/* Replacing the professor with the girl style must leave identical pixels. */
restore(PROF_X - 8, PROF_Y - 20, 17, 24);
styled_person(PROF_X, PROF_Y, 0, RGB(29, 30, 27), RGB(20, 21, 19),
FACING_DOWN, GIRL_STYLE);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
current_room = OUTSIDE;
assert(test_pixels[0] == 0xbeef); assert(test_pixels[0] == 0xbeef);
assert(test_pixels[WIDTH * HEIGHT + 1] == 0xbeef); assert(test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
render_scene(108, 103); render_scene(108, 103);
@@ -177,6 +197,7 @@ int main(void)
check_buffered_walking(); check_buffered_walking();
check_route_travel(); check_route_travel();
check_mellon_visit(); check_mellon_visit();
check_beach_travel();
check_starter_gift(); check_starter_gift();
check_following_and_battle(); check_following_and_battle();
check_wild_encounters(); check_wild_encounters();
@@ -184,6 +205,13 @@ int main(void)
check_background_cache(); check_background_cache();
check_sprite_rows(); check_sprite_rows();
check_movement_clock_and_speed(); check_movement_clock_and_speed();
check_music();
check_catching();
check_fishing();
check_partner_selection();
check_progress();
check_location_save();
check_save_menu();
assert(test_pixels[0] == 0xbeef); assert(test_pixels[0] == 0xbeef);
assert(test_pixels[WIDTH * HEIGHT + 1] == 0xbeef); assert(test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
puts("Ball gift/inventory/save migration, wild encounters, battles, following, and map checks passed"); puts("Ball gift/inventory/save migration, wild encounters, battles, following, and map checks passed");
+142
View File
@@ -0,0 +1,142 @@
#include "test_support.h"
static void same_location(const struct Location *a, const struct Location *b)
{
assert(a->room == b->room && a->x == b->x && a->y == b->y && a->facing == b->facing);
}
static void team_loaded(void)
{
assert(partner_owned && balls_given && rod_owned && ball_count == 3 &&
caught_count == 2 && active_member == 2);
assert(caught_species[0] == FERNFOX && caught_species[1] == TIDEFIN);
for (int i = 2; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
}
void check_location_save(void)
{
assert(save_crc((const unsigned char *)"123456789", 9) == 0x29b1);
struct Location fallback, restored;
default_location(&fallback);
assert(location_valid(&fallback));
memset(test_save, 0xff, sizeof(test_save));
load_progress();
partner_owned = balls_given = rod_owned = 1;
ball_count = 3; caught_count = active_member = 2;
caught_species[0] = FERNFOX; caught_species[1] = TIDEFIN;
struct Location beach = {BEACH, 120, 100, FACING_RIGHT};
save_at_menu(&beach, SAVE_MENU_SAVED);
/* Golden bytes calculated independently with Python binascii.crc_hqx(..., 0xffff). */
const unsigned char golden[] = {'F','Q','S','P',6,0x17,3,2,1,4,255,255,255,5,3,120,100,0xa1,0xa4};
assert(memcmp(test_save, golden, sizeof(golden)) == 0);
unsigned char saved[sizeof(test_save)], fixture[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
const int positions[ROOM_COUNT][2][2] = {
{{108,103},{222,131}}, {{120,119},{80,75}}, {{108,60},{207,131}},
{{31,113},{177,83}}, {{120,119},{80,75}}, {{108,60},{120,103}}
};
for (int room = 0; room < ROOM_COUNT; ++room) {
current_room = (enum Room)room;
int x, y;
reset_position(&x, &y);
struct Location spawn = {room, x, y, FACING_DOWN};
assert(location_valid(&spawn));
for (int edge = 0; edge < 2; ++edge)
for (int facing = FACING_DOWN; facing <= FACING_RIGHT; ++facing) {
struct Location here = {room, positions[room][edge][0], positions[room][edge][1], facing};
assert(location_valid(&here));
save_at_menu(&here, SAVE_MENU_SAVED);
partner_owned = balls_given = rod_owned = caught_count = active_member = ball_count = 0;
load_progress();
team_loaded();
saved_location(&restored);
same_location(&here, &restored);
door_open = grass_steps = diagonal_fraction = 7;
follower_ready = 0;
enter_location(&restored, &x, &y);
assert((int)current_room == room && x == here.x && y == here.y && (int)player_facing == facing);
assert(!door_open && !grass_steps && !diagonal_fraction && follower_ready && !trail_head);
assert(follower_x == x && follower_y == y);
move_follower(x, y, x + 1, y);
assert(follower_x == x && follower_y == y);
}
}
/* Walking, turning, doors, map exits and unsaved inventory cannot replace A. */
save_at_menu(&fallback, SAVE_MENU_SAVED);
memcpy(fixture, test_save, sizeof(fixture));
int x, y;
enter_location(&fallback, &x, &y);
walk_movement(RIGHT, &x, &y, 2);
ball_count = 1; rod_owned = 0; active_member = 0;
x = 177; y = 83; assert(use_door(&x, &y));
y = 125; assert(use_door(&x, &y));
x = 108; y = 131; assert(use_map_exit(&x, &y, DOWN));
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
load_progress(); team_loaded(); saved_location(&restored); same_location(&fallback, &restored);
for (int version = 1; version <= 5; ++version) {
memcpy(test_save, saved, sizeof(saved));
legacy_fixture(version);
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(partner_owned && balls_given == (version >= 2) && ball_count == (version >= 2 ? 3 : 0));
assert(rod_owned == (version >= 4) && caught_count == (version >= 3 ? 2 : 0));
assert(active_member == (version == 5 ? 2 : 0));
for (int i = 0; i < CAUGHT_CAPACITY; ++i)
assert(caught_species[i] == (version >= 3 && i < 2 ? saved[8 + i] : 0xff));
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
for (int damage = 0; damage < SAVE_RECORD_SIZE * 2; ++damage) {
memcpy(test_save, saved, sizeof(saved));
if (damage < SAVE_RECORD_SIZE) test_save[damage] ^= 1;
else if (damage == SAVE_RECORD_SIZE) { test_save[4] = 7; checksum_fixture(); }
else {
int cut = damage - SAVE_RECORD_SIZE;
memset(test_save + cut, 0xff, sizeof(test_save) - (unsigned)cut);
}
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(!partner_owned && !balls_given && !rod_owned && !ball_count && !caught_count && !active_member);
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
const unsigned char bad_progress[][2] = {{5,0x57}, {6,100}, {7,6}, {8,SPECIES_COUNT}, {12,0}, {5,0x1f}};
for (unsigned i = 0; i < sizeof(bad_progress) / sizeof(bad_progress[0]); ++i) {
memcpy(test_save, saved, sizeof(saved));
test_save[bad_progress[i][0]] = bad_progress[i][1];
checksum_fixture();
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored);
assert(!partner_owned && !caught_count && !ball_count);
same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
}
const struct Location invalid[] = {
{ROOM_COUNT,108,103,0}, {-1,108,103,0}, {OUTSIDE,108,103,4}, {OUTSIDE,108,103,-1},
{256,108,103,0}, {OUTSIDE,108,103,256},
{OUTSIDE,-1,103,0}, {OUTSIDE,256,103,0}, {OUTSIDE,108,-1,0}, {OUTSIDE,108,256,0},
{OUTSIDE,255,103,0}, {OUTSIDE,108,255,0}, {COTTAGE,25,120,0}, {BEACH,120,130,0},
{ROUTE,180,60,0}, {OUTSIDE,NPC_X,NPC_Y,0}, {MELLON_HOUSE,PROF_X,PROF_Y,0}
};
for (unsigned i = 0; i < sizeof(invalid) / sizeof(invalid[0]); ++i) {
assert(!location_valid(&invalid[i]));
memcpy(test_save, saved, sizeof(saved));
test_save[13] = (unsigned char)invalid[i].room; test_save[14] = (unsigned char)invalid[i].facing;
test_save[15] = (unsigned char)invalid[i].x; test_save[16] = (unsigned char)invalid[i].y;
checksum_fixture();
memcpy(fixture, test_save, sizeof(fixture));
load_progress(); saved_location(&restored); team_loaded(); same_location(&fallback, &restored);
assert(memcmp(fixture, test_save, sizeof(fixture)) == 0);
memcpy(test_save, saved, sizeof(saved));
enter_location(&beach, &x, &y);
struct Location bad = invalid[i];
save_at_menu(&bad, SAVE_MENU_FAILED);
same_location(&bad, &invalid[i]); team_loaded();
assert(current_room == BEACH && player_facing == FACING_RIGHT && x == beach.x && y == beach.y);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
}
memset(test_save, 0xff, sizeof(test_save));
load_progress(); enter_location(&fallback, &x, &y);
puts("Location saves: v6 CRC, all rooms/facings, legacy reads, fallback and no-autosave checks passed");
}
+68 -1
View File
@@ -15,7 +15,7 @@ void check_buffered_walking(void)
mark_dirty(240, 160, 4, 4); mark_dirty(240, 160, 4, 4);
assert(pending_pixels() == 0); assert(pending_pixels() == 0);
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) { for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room; current_room = (enum Room)room;
player_facing = FACING_DOWN; player_facing = FACING_DOWN;
int x, y; int x, y;
@@ -219,3 +219,70 @@ void check_mellon_visit(void)
current_room = OUTSIDE; current_room = OUTSIDE;
player_facing = FACING_DOWN; player_facing = FACING_DOWN;
} }
void check_beach_travel(void)
{
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
current_room = MELLON_GARDEN;
partner_owned = 1;
assert(reachable(31, 113, 108, 131));
int x = 80, y = 131;
assert(!use_map_exit(&x, &y, DOWN));
x = 108;
assert(!use_map_exit(&x, &y, UP));
for (int trip = 0; trip < 5; ++trip) {
x = 108;
y = 129;
assert(walk_movement(DOWN, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == BEACH && x == 108 && y == 60);
assert(can_walk(x, y) && player_facing == FACING_DOWN);
assert(follower_x == x && follower_y == y);
assert(!use_map_exit(&x, &y, DOWN));
assert(!near_npc(RANGER_X, RANGER_Y) && !near_door(177, 83));
assert(!in_tall_grass(48, 70));
assert(!check_grass_encounter(80, 80, 81, 80));
assert(!can_walk(55, 85)); /* Umbrella. */
assert(!can_walk(28, 60) && !can_walk(210, 60)); /* Palms. */
assert(reachable(x, y, 178, 97));
assert(reachable(x, y, 30, 99));
assert(reachable(x, y, 108, 47));
for (int col = 17; col <= 222; ++col)
assert(!can_walk(col, beach_shoreline(col) - 4));
assert(!can_walk(108, 115) && !can_walk(108, 140));
render_scene(x, y);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
write_preview("build/beach-preview.ppm");
const int positions[][2] = {{108, 47}, {178, 97}, {30, 99}, {90, 82}};
for (unsigned i = 0; i < sizeof(positions) / sizeof(positions[0]); ++i) {
int nx = positions[i][0], ny = positions[i][1];
assert(can_walk(nx, ny));
render_scene(nx, ny);
memcpy(expected, SCREEN, sizeof(expected));
render_scene(x, y);
restore(x - 8, y - 20, 17, 24);
actors(nx, ny, 0);
location_status(nx, ny);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
}
x = 108;
y = beach_shoreline(x) - 6;
assert(walk_movement(DOWN, &x, &y, MAX_MOVEMENT_TICKS) == WALK_NORMAL);
assert(y == beach_shoreline(x) - 5); /* Even catch-up frames cannot enter the sea. */
reset_position(&x, &y);
assert(current_room == BEACH && 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 == MELLON_GARDEN && x == 108 && y == 125);
assert(player_facing == FACING_UP && follower_x == x && follower_y == y);
assert(!use_map_exit(&x, &y, UP));
assert(reachable(x, y, 177, 83)); /* Professor's door remains accessible. */
assert(reachable(x, y, 17, 113)); /* Town exit remains accessible. */
}
current_room = OUTSIDE;
partner_owned = follower_ready = 0;
player_facing = FACING_DOWN;
puts("Beach: garden connection, return path, shore collisions, following, and redraw checks passed");
}
+325
View File
@@ -0,0 +1,325 @@
#include "test_support.h"
static void release_keys(void) { assert(!menu_input(0, 1)); }
static void press(unsigned keys)
{
release_keys();
assert(menu_input(keys, 1));
}
static void check_submenus(void)
{
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
static u16 expected[WIDTH * HEIGHT], zero[WIDTH * HEIGHT];
current_room = OUTSIDE;
int x = 108, y = 103;
player_facing = FACING_UP;
partner_owned = caught_count = active_member = ball_count = rod_owned = 0;
for (int owned = 0; owned < 2; ++owned) {
partner_owned = owned;
caught_count = owned ? 2 : 0;
caught_species[0] = FERNFOX; caught_species[1] = TIDEFIN;
for (int parent = 0; parent < 2; ++parent) {
press(parent ? START : SELECT);
if (parent) press(A);
assert(save_menu.phase == SAVE_MENU_TEAM);
assert(!menu_input(parent ? A : SELECT, 1));
draw_menu_scene(x, y);
write_preview(owned ? "build/menu-team-owned-preview.ppm" : "build/menu-team-empty-preview.ppm");
if (owned) {
press(LEFT); assert(!menu_input(LEFT, 1));
press(RIGHT); press(LEFT);
draw_menu_scene(x, y);
write_preview("build/menu-team-caught-preview.ppm");
press(A);
assert(active_member == 2);
} else {
int fx = follower_x, fy = follower_y, head = trail_head;
press(B);
assert(follower_x == fx && follower_y == fy && trail_head == head);
}
assert(save_menu.phase == (parent ? SAVE_MENU_OPTIONS : SAVE_MENU_CLOSED));
assert(!menu_input(owned ? A : B, 1));
if (parent) {
render_scene(x, y); draw_save_menu();
memcpy(expected, SCREEN, sizeof(expected));
draw_team(); draw_menu_scene(x, y);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
press(A); press(SELECT);
assert(save_menu.phase == SAVE_MENU_OPTIONS);
press(B);
}
render_scene(x, y); memcpy(expected, SCREEN, sizeof(expected));
draw_bag(); draw_menu_scene(x, y);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
}
}
press(START);
for (int i = 1; i <= MENU_COUNT; ++i) {
press(DOWN); assert(save_menu.selection == i % MENU_COUNT);
assert(!menu_input(DOWN, 1));
}
press(DOWN); press(A);
assert(save_menu.phase == SAVE_MENU_BAG);
assert(!menu_input(A, 1));
for (int full = 0; full < 2; ++full) {
ball_count = full ? 99 : 0; rod_owned = full;
test_pixels[0] = test_pixels[WIDTH * HEIGHT + 1] = 0xbeef;
reset_dirty_rows(); draw_menu_scene(x, y);
assert(pending_pixels() == WIDTH * HEIGHT);
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
write_preview(full ? "build/bag-full-preview.ppm" : "build/bag-zero-preview.ppm");
if (!full) memcpy(zero, SCREEN, sizeof(zero));
else assert(memcmp(zero, SCREEN, sizeof(zero)) != 0);
}
press(B); assert(save_menu.phase == SAVE_MENU_OPTIONS);
assert(!menu_input(B, 1));
press(A); press(START);
assert(save_menu.phase == SAVE_MENU_CLOSED && !menu_input(START, 1));
press(SELECT); press(START);
assert(save_menu.phase == SAVE_MENU_CLOSED);
assert(x == 108 && y == 103 && player_facing == FACING_UP && current_room == OUTSIDE);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
load_progress();
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
}
static void check_highlight_redraw(void)
{
static u16 before[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
enum Room old_room = current_room;
for (int from = SAVE_MENU_CLOSED; from <= SAVE_MENU_BAG; ++from)
for (int to = SAVE_MENU_CLOSED; to <= SAVE_MENU_BAG; ++to)
assert(menu_redraw_is_panel((enum SaveMenuPhase)from, (enum SaveMenuPhase)to) ==
(from <= SAVE_MENU_FAILED && to <= SAVE_MENU_FAILED &&
(from != SAVE_MENU_CLOSED || to != SAVE_MENU_CLOSED)));
for (int room = OUTSIDE; room < ROOM_COUNT; ++room) {
current_room = (enum Room)room;
for (int up = 0; up < 2; ++up) {
save_menu = (struct SaveMenuState){SAVE_MENU_OPTIONS, 0, 0};
draw_menu_scene(108, 103);
copy_pending_rows(visible);
for (int step = 0; step < MENU_COUNT; ++step) {
memcpy(before, SCREEN, sizeof(before));
enum SaveMenuPhase previous = save_menu.phase;
press(up ? UP : DOWN);
assert(save_menu.selection == (up ? (MENU_COUNT - step - 1) % MENU_COUNT : (step + 1) % MENU_COUNT));
assert(menu_redraw_is_panel(previous, save_menu.phase));
assert(!menu_input(up ? UP : DOWN, 1));
reset_dirty_rows();
test_pixels[0] = test_pixels[WIDTH * HEIGHT + 1] = 0xbeef;
draw_save_menu();
assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H);
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x)
if (x < SAVE_MENU_X || x >= SAVE_MENU_X + SAVE_MENU_W ||
y < SAVE_MENU_Y || y >= SAVE_MENU_Y + SAVE_MENU_H)
assert(SCREEN[y * WIDTH + x] == before[y * WIDTH + x]);
assert(memcmp(before, SCREEN, sizeof(before)) != 0);
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
if (room == OUTSIDE && !up) {
const char *paths[] = {"build/highlight-team-preview.ppm", "build/highlight-bag-preview.ppm",
"build/highlight-save-preview.ppm", "build/highlight-close-preview.ppm"};
write_preview(paths[save_menu.selection]);
}
}
}
}
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
current_room = old_room;
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
}
static void check_panel_underlay(void)
{
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
partner_owned = caught_count = 1; active_member = 0;
caught_species[0] = TIDEFIN;
for (int room = OUTSIDE; room < ROOM_COUNT; ++room)
for (int facing = FACING_DOWN; facing <= FACING_RIGHT; ++facing) {
current_room = (enum Room)room;
player_facing = (enum Facing)facing;
int x = indoors() ? 200 : room == ROUTE ? 207 : 210;
int y = room == BEACH ? 85 : 119;
reset_follower(x - 14, y);
render_scene(x, y);
memcpy(expected, SCREEN, sizeof(expected));
copy_pending_rows(visible);
int fx = follower_x, fy = follower_y, head = trail_head;
for (int phase = SAVE_MENU_OPTIONS; phase <= SAVE_MENU_FAILED; ++phase) {
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
if (room == OUTSIDE && facing == FACING_DOWN && phase == SAVE_MENU_OPTIONS)
write_preview("build/panel-before-preview.ppm");
press(START);
assert(menu_redraw_is_panel(SAVE_MENU_CLOSED, save_menu.phase));
draw_menu_panel(SAVE_MENU_CLOSED);
assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H);
copy_pending_rows(visible);
assert(!menu_input(START, 1));
for (int py = 0; py < HEIGHT; ++py)
for (int px = 0; px < WIDTH; ++px)
if (px < SAVE_MENU_X || px >= SAVE_MENU_X + SAVE_MENU_W ||
py < SAVE_MENU_Y || py >= SAVE_MENU_Y + SAVE_MENU_H)
assert(SCREEN[py * WIDTH + px] == expected[py * WIDTH + px]);
save_menu.phase = (enum SaveMenuPhase)phase;
draw_menu_panel(SAVE_MENU_OPTIONS);
assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H);
copy_pending_rows(visible);
if (room == OUTSIDE && facing == FACING_DOWN && phase == SAVE_MENU_OPTIONS)
write_preview("build/panel-open-preview.ppm");
unsigned close = phase == SAVE_MENU_OPTIONS ? B : phase == SAVE_MENU_SAVED ? A : START;
press(close);
assert(save_menu.phase == SAVE_MENU_CLOSED);
assert(menu_redraw_is_panel((enum SaveMenuPhase)phase, save_menu.phase));
draw_menu_panel((enum SaveMenuPhase)phase);
assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
assert(memcmp(expected, visible, sizeof(expected)) == 0);
assert(!menu_input(close, 1));
if (room == OUTSIDE && facing == FACING_DOWN && phase == SAVE_MENU_OPTIONS)
write_preview("build/panel-closed-preview.ppm");
}
assert(player_facing == (enum Facing)facing && follower_x == fx && follower_y == fy && trail_head == head);
/* A partner change on a fullscreen page must replace, not reuse, old underlay. */
press(START); draw_menu_panel(SAVE_MENU_CLOSED);
press(A); assert(save_menu.phase == SAVE_MENU_TEAM); draw_menu_scene(x, y);
press(RIGHT); release_keys();
const struct Location here = {room, x, y, facing};
assert(save_menu_input(A, 1, &here));
assert(active_member == 1 && save_menu.phase == SAVE_MENU_OPTIONS);
render_scene(x, y); memcpy(expected, SCREEN, sizeof(expected));
draw_menu_scene(x, y); copy_pending_rows(visible);
press(B); draw_menu_panel(SAVE_MENU_OPTIONS);
assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
assert(memcmp(expected, visible, sizeof(expected)) == 0);
press(START); draw_menu_panel(SAVE_MENU_CLOSED);
press(DOWN); press(A); assert(save_menu.phase == SAVE_MENU_BAG);
draw_menu_scene(x, y);
press(B); draw_menu_scene(x, y); copy_pending_rows(visible);
press(B); draw_menu_panel(SAVE_MENU_OPTIONS);
assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
assert(memcmp(expected, visible, sizeof(expected)) == 0);
active_member = 0;
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
}
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
load_progress();
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
}
void check_save_menu(void)
{
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
for (int mode = 0; mode < 3; ++mode) {
release_keys();
assert(!menu_input(START, 0));
assert(!menu_input(START, 1));
}
release_keys();
assert(menu_input(START | A, 1));
assert(save_menu.phase == SAVE_MENU_OPTIONS && save_menu.selection == 0);
assert(!menu_input(A, 1));
release_keys();
assert(menu_input(DOWN, 1) && save_menu.selection == 1);
assert(!menu_input(DOWN, 1));
release_keys();
assert(!menu_input(UP | DOWN, 1));
release_keys();
assert(menu_input(UP, 1) && save_menu.selection == 0);
release_keys();
assert(menu_input(UP, 1) && save_menu.selection == MENU_CLOSE);
release_keys();
assert(menu_input(A, 1) && save_menu.phase == SAVE_MENU_CLOSED);
const unsigned cancel[] = {B, START};
for (int i = 0; i < 2; ++i) {
release_keys();
assert(menu_input(START, 1));
release_keys();
assert(menu_input(cancel[i], 1) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!menu_input(cancel[i], 1));
}
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
release_keys();
assert(menu_input(START, 1));
for (int i = 0; i < MENU_SAVE; ++i) {
release_keys();
assert(menu_input(DOWN, 1));
}
release_keys();
assert(menu_input(A, 1) && save_menu.phase == SAVE_MENU_SAVED);
test_save[0] = 0;
for (int i = 0; i < 10; ++i) assert(!menu_input(A, 1));
assert(test_save[0] == 0 && save_menu.phase == SAVE_MENU_SAVED);
release_keys();
assert(!menu_input(DOWN, 1));
release_keys();
assert(menu_input(A, 1) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!menu_input(A, 1));
memcpy(test_save, saved, sizeof(saved));
static u16 expected[WIDTH * HEIGHT];
enum Room old_room = current_room;
int old_fx = follower_x, old_fy = follower_y, old_ready = follower_ready;
unsigned rng = random_state;
int grass = grass_steps, door = door_open;
for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room;
render_scene(108, 103);
memcpy(expected, SCREEN, sizeof(expected));
for (int phase = SAVE_MENU_OPTIONS; phase <= SAVE_MENU_FAILED; ++phase) {
save_menu.phase = (enum SaveMenuPhase)phase;
for (int choice = 0; choice < MENU_COUNT; ++choice) {
save_menu.selection = choice;
test_pixels[0] = test_pixels[WIDTH * HEIGHT + 1] = 0xbeef;
reset_dirty_rows();
draw_save_menu();
assert(pending_pixels() == SAVE_MENU_W * SAVE_MENU_H);
for (int y = 0; y < HEIGHT; ++y) {
int row = y >= SAVE_MENU_Y && y < SAVE_MENU_Y + SAVE_MENU_H;
if (row) {
assert(dirty_first[y] == SAVE_MENU_X);
assert(dirty_end[y] == SAVE_MENU_X + SAVE_MENU_W);
} else assert(dirty_first[y] >= dirty_end[y]);
for (int x = 0; x < WIDTH; ++x)
if (!row || x < SAVE_MENU_X || x >= SAVE_MENU_X + SAVE_MENU_W)
assert(SCREEN[y * WIDTH + x] == expected[y * WIDTH + x]);
}
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
if (room == OUTSIDE && choice == 0) {
const char *paths[] = {"build/save-menu-preview.ppm",
"build/game-saved-preview.ppm", "build/save-failed-preview.ppm"};
write_preview(paths[phase - SAVE_MENU_OPTIONS]);
}
if (room == OUTSIDE && phase == SAVE_MENU_OPTIONS) {
const char *paths[] = {"build/menu-team-preview.ppm", "build/menu-bag-preview.ppm",
"build/menu-save-preview.ppm", "build/menu-close-preview.ppm"};
write_preview(paths[choice]);
}
}
render_scene(108, 103);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
}
}
assert(follower_x == old_fx && follower_y == old_fy && follower_ready == old_ready);
assert(random_state == rng && grass_steps == grass && door_open == door);
current_room = old_room;
save_menu = (struct SaveMenuState){SAVE_MENU_CLOSED, 0, 0};
check_highlight_redraw();
check_panel_underlay();
check_submenus();
}
+1 -1
View File
@@ -98,7 +98,7 @@ void check_movement_clock_and_speed(void)
static u16 snapshot[WIDTH * HEIGHT]; static u16 snapshot[WIDTH * HEIGHT];
const unsigned directions[] = {RIGHT, DOWN, LEFT, UP}; const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
const int tick_counts[] = {1, 2, 1, 3, 0}; const int tick_counts[] = {1, 2, 1, 3, 0};
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) { for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room; current_room = (enum Room)room;
reset_position(&x, &y); reset_position(&x, &y);
if (current_room == OUTSIDE) { x = 40; y = 125; } if (current_room == OUTSIDE) { x = 40; y = 125; }
+94
View File
@@ -0,0 +1,94 @@
#include "test_support.h"
static void assert_empty_progress(void)
{
assert(!partner_owned && !balls_given && !rod_owned && !ball_count && !caught_count && !active_member);
for (int i = 0; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
}
void check_progress(void)
{
memset(test_save, 0xff, sizeof(test_save));
load_progress();
assert_empty_progress();
save_from_menu();
const unsigned char empty[] = {'F','Q','S','P',6,0,0,0,255,255,255,255,255,0,0,108,103,0xde,0x32};
assert(memcmp(test_save, empty, sizeof(empty)) == 0);
partner_owned = balls_given = 1;
ball_count = 5;
save_from_menu();
/* An unsaved expanded team and rod roll back to the previous snapshot. */
rod_owned = 1;
caught_count = active_member = 1;
caught_species[0] = TIDEFIN;
load_progress();
assert(partner_owned && balls_given && ball_count == 5 && !rod_owned && !caught_count && !active_member);
for (int i = 0; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
rod_owned = 1;
caught_count = active_member = 2;
caught_species[0] = FERNFOX;
caught_species[1] = TIDEFIN;
ball_count = 3;
save_from_menu();
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
partner_owned = balls_given = rod_owned = caught_count = active_member = ball_count = 0;
memset(caught_species, 0xff, sizeof(caught_species));
load_progress();
assert(partner_owned && balls_given && rod_owned && caught_count == 2 && active_member == 2 && ball_count == 3);
assert(caught_species[0] == FERNFOX && caught_species[1] == TIDEFIN);
for (int invalid = 0; invalid < 9; ++invalid) {
load_progress();
switch (invalid) {
case 0: ball_count = -1; break;
case 1: ball_count = 100; break;
case 2: balls_given = 0; break;
case 3: caught_count = -1; break;
case 4: caught_count = CAUGHT_CAPACITY + 1; break;
case 5: partner_owned = 0; break;
case 6: active_member = -1; break;
case 7: active_member = 3; break;
default: caught_species[1] = SPECIES_COUNT; break;
}
int state[] = {partner_owned, balls_given, rod_owned, ball_count, caught_count, active_member};
unsigned char kinds[CAUGHT_CAPACITY];
memcpy(kinds, caught_species, sizeof(kinds));
assert(!menu_input(0, 1));
assert(menu_input(START, 1));
for (int i = 0; i < MENU_SAVE; ++i) {
assert(!menu_input(0, 1));
assert(menu_input(DOWN, 1));
}
assert(!menu_input(0, 1));
assert(menu_input(A, 1) && save_menu.phase == SAVE_MENU_FAILED);
int after[] = {partner_owned, balls_given, rod_owned, ball_count, caught_count, active_member};
assert(memcmp(state, after, sizeof(state)) == 0);
assert(memcmp(kinds, caught_species, sizeof(kinds)) == 0);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
assert(!menu_input(A, 1));
assert(!menu_input(0, 1));
assert(menu_input(B, 1));
}
load_progress();
partner_owned = 2; balls_given = 7; rod_owned = -1;
save_from_menu();
assert(partner_owned == 2 && balls_given == 7 && rod_owned == -1);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
for (int byte = 0; byte < SAVE_RECORD_SIZE; ++byte) {
memcpy(test_save, saved, sizeof(saved));
test_save[byte] ^= 1;
load_progress();
assert_empty_progress();
}
/* v2 ignores newer trailing bytes and defaults rod, catches and selection. */
const unsigned char v2[] = {'F','Q','S','P',2,3,5,0xa1,0x5a};
memset(test_save, 0x31, sizeof(test_save));
memcpy(test_save, v2, sizeof(v2));
load_progress();
assert(partner_owned && balls_given && ball_count == 5 && !rod_owned && !caught_count && !active_member);
for (int i = 0; i < CAUGHT_CAPACITY; ++i) assert(caught_species[i] == 0xff);
assert(memcmp(test_save, v2, sizeof(v2)) == 0 && test_save[14] == 0x31);
memset(test_save, 0xff, sizeof(test_save));
load_progress();
}
+4 -3
View File
@@ -10,7 +10,7 @@ void check_background_cache(void)
restore(240, 160, 4, 4); restore(240, 160, 4, 4);
assert(!background_valid && scenery_samples == 0 && pending_pixels() == 0); assert(!background_valid && scenery_samples == 0 && pending_pixels() == 0);
for (int room = OUTSIDE; room <= MELLON_HOUSE; ++room) { for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room; current_room = (enum Room)room;
door_open = 0; door_open = 0;
unsigned before = scenery_samples; unsigned before = scenery_samples;
@@ -87,7 +87,7 @@ void check_background_cache(void)
assert(SCREEN[y * WIDTH + x] == expected); assert(SCREEN[y * WIDTH + x] == expected);
} }
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef); assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
puts("Background cache: 5 maps match reference; warm restores use 0 scenery evaluations (was 408 each)"); puts("Background cache: 6 maps match reference; warm restores use 0 scenery evaluations (was 408 each)");
} }
/* Reference implementation of the previous per-pixel rectangle renderer. */ /* Reference implementation of the previous per-pixel rectangle renderer. */
@@ -116,8 +116,9 @@ void check_sprite_rows(void)
static const char *const zero_art[] = {"0A..B1..", "..1.....", "........"}; static const char *const zero_art[] = {"0A..B1..", "..1.....", "........"};
const struct { const char *const *art; int width, rows; } images[] = { const struct { const char *const *art; int width, rows; } images[] = {
{person_art, 16, 20}, {person_back_art, 16, 20}, {person_side_art, 16, 20}, {person_art, 16, 20}, {person_back_art, 16, 20}, {person_side_art, 16, 20},
{girl_art, 16, 20}, {girl_back_art, 16, 20}, {girl_side_art, 16, 20},
{creature_art, 16, 16}, {fernfox_art, 24, 24}, {emberwing_art, 24, 24}, {creature_art, 16, 16}, {fernfox_art, 24, 24}, {emberwing_art, 24, 24},
{pebblit_art, 24, 24}, {zero_art, 8, 3} {pebblit_art, 24, 24}, {tidefin_art, 24, 24}, {zero_art, 8, 3}
}; };
const int positions[][2] = { const int positions[][2] = {
{70, 60}, {-8, 40}, {232, 40}, {80, -12}, {80, 155}, {70, 60}, {-8, 40}, {232, 40}, {80, -12}, {80, 155},
+4
View File
@@ -22,6 +22,9 @@ class HeaderTests(unittest.TestCase):
def test_sram_save_marker_is_in_rom(self): def test_sram_save_marker_is_in_rom(self):
self.assertIn(b"SRAM_V113", self.rom) self.assertIn(b"SRAM_V113", self.rom)
def test_homebrew_code_avoids_classic_nes_override(self):
self.assertEqual(self.rom[172:176], b"QST0")
def test_bad_logo_is_rejected(self): def test_bad_logo_is_rejected(self):
self.rom[4] ^= 1 self.rom[4] ^= 1
with self.assertRaisesRegex(ValueError, "hardware header"): with self.assertRaisesRegex(ValueError, "hardware header"):
@@ -54,6 +57,7 @@ class HeaderTests(unittest.TestCase):
result = path.read_bytes() result = path.read_bytes()
self.assertEqual(len(result), 32768) self.assertEqual(len(result), 32768)
self.assertEqual(result[160:172], b"FIRSTQUEST\0\0") self.assertEqual(result[160:172], b"FIRSTQUEST\0\0")
self.assertEqual(result[172:176], b"QST0")
header.verify(result) header.verify(result)
+27 -6
View File
@@ -33,6 +33,11 @@ void check_starter_gift(void)
dialogue_box(1); dialogue_box(1);
write_preview("build/starter-gift-preview.ppm"); write_preview("build/starter-gift-preview.ppm");
assert(!receive_partner()); /* The same gift cannot add a second partner. */ assert(!receive_partner()); /* The same gift cannot add a second partner. */
for (int i = 0; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
load_progress();
assert(!partner_owned);
assert(receive_partner());
save_from_menu();
unsigned char saved[sizeof(test_save)]; unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved)); memcpy(saved, test_save, sizeof(saved));
assert(advance_conversation(1) == -1); assert(advance_conversation(1) == -1);
@@ -42,7 +47,7 @@ void check_starter_gift(void)
assert(advance_conversation(0) == 1); assert(advance_conversation(0) == 1);
assert(advance_conversation(1) == -1); assert(advance_conversation(1) == -1);
assert(memcmp(saved, test_save, sizeof(saved)) == 0); assert(memcmp(saved, test_save, sizeof(saved)) == 0);
for (int i = 9; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff); for (int i = SAVE_RECORD_SIZE; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
/* Ownership survives map changes, position reset, and loading SRAM. */ /* Ownership survives map changes, position reset, and loading SRAM. */
x = 120; x = 120;
@@ -58,6 +63,11 @@ void check_starter_gift(void)
partner_owned = 0; partner_owned = 0;
load_progress(); load_progress();
assert(partner_owned); assert(partner_owned);
current_room = MELLON_HOUSE;
assert(begin_conversation(PROF_X, PROF_Y + 22) && !gift_conversation);
assert(!receive_partner());
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
current_room = OUTSIDE;
draw_team(); draw_team();
write_preview("build/team-preview.ppm"); write_preview("build/team-preview.ppm");
static u16 expected[WIDTH * HEIGHT]; static u16 expected[WIDTH * HEIGHT];
@@ -68,7 +78,7 @@ void check_starter_gift(void)
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0); assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
/* Reject damaged, incomplete, or unknown-version save records. */ /* Reject damaged, incomplete, or unknown-version save records. */
for (int byte = 0; byte < 9; ++byte) { for (int byte = 0; byte < SAVE_RECORD_SIZE; ++byte) {
memcpy(test_save, saved, sizeof(saved)); memcpy(test_save, saved, sizeof(saved));
test_save[byte] ^= 1; test_save[byte] ^= 1;
load_progress(); load_progress();
@@ -132,7 +142,13 @@ void check_ball_gift(void)
restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H); restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H);
actors(99, 97, 0); actors(99, 97, 0);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0); assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
assert(test_save[4] == 2); assert(test_save[4] == 1 && memcmp(test_save, legacy, sizeof(legacy)) == 0);
load_progress();
assert(partner_owned && !balls_given && ball_count == 0);
assert(begin_conversation(99, 97));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
assert(test_save[4] == 6);
unsigned char saved[sizeof(test_save)]; unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved)); memcpy(saved, test_save, sizeof(saved));
for (int visit = 0; visit < 10; ++visit) { for (int visit = 0; visit < 10; ++visit) {
@@ -144,9 +160,12 @@ void check_ball_gift(void)
partner_owned = balls_given = ball_count = 0; partner_owned = balls_given = ball_count = 0;
load_progress(); load_progress();
assert(partner_owned && balls_given && ball_count == 5); assert(partner_owned && balls_given && ball_count == 5);
assert(begin_conversation(99, 97) && !ball_gift_conversation);
assert(!receive_balls());
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
draw_team(); draw_team();
write_preview("build/poke-balls-preview.ppm"); write_preview("build/poke-balls-preview.ppm");
for (int i = 9; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff); for (int i = SAVE_RECORD_SIZE; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
int x = 108, y = 47; int x = 108, y = 47;
assert(use_map_exit(&x, &y, UP)); assert(use_map_exit(&x, &y, UP));
assert(ball_count == 5 && balls_given); assert(ball_count == 5 && balls_given);
@@ -157,15 +176,17 @@ void check_ball_gift(void)
current_room = ROUTE; current_room = ROUTE;
assert(begin_conversation(99, 97)); assert(begin_conversation(99, 97));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1); assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
load_progress(); load_progress();
assert(!partner_owned && balls_given && ball_count == 5); assert(!partner_owned && balls_given && ball_count == 5);
current_room = MELLON_HOUSE; current_room = MELLON_HOUSE;
assert(begin_conversation(PROF_X, PROF_Y + 22)); assert(begin_conversation(PROF_X, PROF_Y + 22));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1); assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
load_progress(); load_progress();
assert(partner_owned && balls_given && ball_count == 5); assert(partner_owned && balls_given && ball_count == 5);
memcpy(saved, test_save, sizeof(saved)); memcpy(saved, test_save, sizeof(saved));
for (int byte = 0; byte < 9; ++byte) { for (int byte = 0; byte < SAVE_RECORD_SIZE; ++byte) {
memcpy(test_save, saved, sizeof(saved)); memcpy(test_save, saved, sizeof(saved));
test_save[byte] ^= 1; test_save[byte] ^= 1;
load_progress(); load_progress();
@@ -173,7 +194,7 @@ void check_ball_gift(void)
} }
memcpy(test_save, saved, sizeof(saved)); memcpy(test_save, saved, sizeof(saved));
test_save[6] = 100; test_save[6] = 100;
test_save[7] = (unsigned char)(0xa5 ^ 2 ^ test_save[5] ^ test_save[6]); checksum_fixture(); /* Keep CRC valid; count itself is invalid. */
load_progress(); load_progress();
assert(!partner_owned && !balls_given && ball_count == 0); assert(!partner_owned && !balls_given && ball_count == 0);
for (int page = 0; page < DIALOGUE_PAGES; ++page) for (int page = 0; page < DIALOGUE_PAGES; ++page)
+56
View File
@@ -3,6 +3,62 @@
u16 test_pixels[WIDTH * HEIGHT + 2]; u16 test_pixels[WIDTH * HEIGHT + 2];
unsigned char test_save[32]; unsigned char test_save[32];
void save_from_menu(void)
{
struct Location location;
default_location(&location);
save_at_menu(&location, SAVE_MENU_SAVED);
}
int menu_input(unsigned keys, int can_open)
{
struct Location location;
default_location(&location);
return save_menu_input(keys, can_open, &location);
}
void save_at_menu(const struct Location *location, enum SaveMenuPhase expected)
{
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(START, 1, location) && save_menu.phase == SAVE_MENU_OPTIONS);
for (int i = 0; i < MENU_SAVE; ++i) {
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(DOWN, 1, location));
}
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(A, 1, location) && save_menu.phase == expected);
assert(!save_menu_input(0, 1, location));
assert(save_menu_input(B, 1, location) && save_menu.phase == SAVE_MENU_CLOSED);
assert(!save_menu_input(0, 1, location));
}
void checksum_fixture(void)
{
if (test_save[4] == 6) {
unsigned crc = save_crc(test_save, 17);
test_save[17] = (unsigned char)(crc >> 8);
test_save[18] = (unsigned char)crc;
} else {
unsigned checksum = 0xa5 ^ test_save[4] ^ test_save[5] ^ test_save[6];
if (test_save[4] >= 3) {
checksum ^= test_save[7];
for (int i = 0; i < CAUGHT_CAPACITY; ++i) checksum ^= test_save[8 + i];
}
test_save[test_save[4] == 2 ? 7 : 13] = (unsigned char)checksum;
test_save[test_save[4] == 2 ? 8 : 14] = 0x5a;
}
}
/* Convert a fixture, never player data, retaining only fields supported by that version. */
void legacy_fixture(int version)
{
test_save[4] = (unsigned char)version;
test_save[5] &= version == 5 ? 63 : version == 4 ? 7 : 3;
if (version == 1) {
test_save[5] = 1; test_save[6] = 0xa5; test_save[7] = 0x5a;
} else checksum_fixture();
}
void render_scene(int x, int y) void render_scene(int x, int y)
{ {
draw_scene(x, y); draw_scene(x, y);
+13
View File
@@ -15,6 +15,7 @@ void copy_pending_rows(u16 *visible);
void check_buffered_walking(void); void check_buffered_walking(void);
void check_route_travel(void); void check_route_travel(void);
void check_mellon_visit(void); void check_mellon_visit(void);
void check_beach_travel(void);
void check_starter_gift(void); void check_starter_gift(void);
void check_ball_gift(void); void check_ball_gift(void);
void check_following_and_battle(void); void check_following_and_battle(void);
@@ -22,5 +23,17 @@ void check_wild_encounters(void);
void check_background_cache(void); void check_background_cache(void);
void check_sprite_rows(void); void check_sprite_rows(void);
void check_movement_clock_and_speed(void); void check_movement_clock_and_speed(void);
void check_music(void);
void check_catching(void);
void check_fishing(void);
void check_partner_selection(void);
void check_save_menu(void);
void check_progress(void);
void save_from_menu(void);
int menu_input(unsigned keys, int can_open);
void save_at_menu(const struct Location *location, enum SaveMenuPhase expected);
void legacy_fixture(int version);
void checksum_fixture(void);
void check_location_save(void);
#endif #endif
+1 -1
View File
@@ -44,7 +44,7 @@ def main():
raise ValueError("Missing cartridge header space") raise ValueError("Missing cartridge header space")
rom[4:160] = LOGO rom[4:160] = LOGO
rom[160:172] = b"FIRSTQUEST".ljust(12, b"\0") rom[160:172] = b"FIRSTQUEST".ljust(12, b"\0")
rom[172:176] = b"FQ01" rom[172:176] = b"QST0"
rom[176:178] = b"00" rom[176:178] = b"00"
rom[178:189] = bytes([0x96]) + bytes(10) rom[178:189] = bytes([0x96]) + bytes(10)
rom[189] = (-0x19 - sum(rom[160:189])) & 0xFF rom[189] = (-0x19 - sum(rom[160:189])) & 0xFF