Compare commits

..
14 Commits
Author SHA1 Message Date
rhett f7a3dad3d0 Merge pull request 'Add Team and Bag pages to the Start menu' (#13) from feature/3-team-and-bag-menu into main
Reviewed-on: #13
2026-10-29 01:34:16 +00:00
LyokoAgents 1e1e753be1 Avoid blanking when opening and closing the sidebar 2026-10-29 00:47:57 +00:00
LyokoAgents 4bc31ea1fc Avoid blanking the display for menu highlight changes 2026-10-29 00:37:08 +00:00
LyokoAgents 79a27ccbae Add Team and Bag pages to the Start menu 2026-10-29 00:30:06 +00:00
rhett dd6b92300e Merge pull request 'Add manual adventure saves and a Start sidebar' (#12) from feature/manual-save-sidebar into main
Reviewed-on: #12
2026-10-29 00:21:42 +00:00
LyokoAgents a6937448c0 Read cartridge SRAM from RAM with safe wait states 2026-10-29 00:04:08 +00:00
LyokoAgents 59c44c2b45 Save adventure location with team and inventory 2026-10-28 23:59:14 +00:00
LyokoAgents 1141765231 Add manual cartridge saving and a right-side menu 2026-10-28 22:38:53 +00:00
rory cb77683a87 Add music, beach fishing, catching, and selectable creature partners 2026-10-28 22:22:27 +00:00
rhett 3469f2e6dd Merge pull request 'Split game code and tests into focused modules' (#1) from chore/split-game-modules into main
Reviewed-on: #1
2026-10-28 16:22:45 +00:00
LyokoAgents 2dbfec3163 Split game code and tests into focused modules 2026-10-28 16:11:09 +00:00
rory a557c43e15 Optimize rendering and add faster fixed-step walking 2026-10-28 14:50:28 +00:00
rory 4beed3bd5c Add village exploration, starter companion, wild battles, and saved item gifts 2026-10-28 14:13:29 +00:00
rory 1307ff3d19 Create GBA adventure with walkable map and NPC dialogue 2026-10-26 00:37:22 +00:00
33 changed files with 4811 additions and 2480 deletions

No files matched your search

+18 -5
View File
@@ -3,8 +3,14 @@ PYTHON ?= /usr/bin/python3
CC := $(TOOL_PREFIX)gcc 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 \
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)
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_audio.c \
tests/test_catching.c tests/test_menu.c tests/test_progress.c tests/test_location.c
.PHONY: all play check .PHONY: all play check check-host
all: game.gba all: game.gba
build: build:
@@ -13,11 +19,15 @@ build:
build/start.o: source/start.s | build build/start.o: source/start.s | build
$(CC) -mcpu=arm7tdmi -marm -c $< -o $@ $(CC) -mcpu=arm7tdmi -marm -c $< -o $@
build/main.o: source/main.c source/wild_creatures.h | build build/%.o: source/%.c source/game.h | build
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
build/game.elf: build/start.o build/main.o gba.ld build/actors.o: source/wild_creatures.h
$(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-z,noexecstack -Wl,-Map,build/game.map build/start.o build/main.o -lgcc -o $@ build/audio.o build/main.o: source/audio.h
# The bare-metal .data segment intentionally contains startup-relocated EWRAM code.
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 $< $@
@@ -26,7 +36,10 @@ game.gba: build/game.elf tools/rom_header.py
check: game.gba check: game.gba
$(PYTHON) tools/rom_header.py --check game.gba $(PYTHON) tools/rom_header.py --check game.gba
$(PYTHON) -m unittest discover -s tests -v $(PYTHON) -m unittest discover -s tests -v
/usr/bin/clang -Wall -Wextra -Werror tests/test_game.c -o build/test_game $(MAKE) check-host
check-host: | build
/usr/bin/clang -Wall -Wextra -Werror -DGAME_TEST $(TEST_SOURCES) $(GAME_SOURCES) -o build/test_game
./build/test_game ./build/test_game
play: game.gba play: game.gba
+51 -15
View File
@@ -9,7 +9,7 @@ Double-click **Build and Play.command** in this folder. It builds your ROM, runs
Or open Terminal and run: Or open Terminal and run:
```sh ```sh
cd ~/gba-game cd ~/Developer/pokemon-gba
make play make play
``` ```
@@ -22,30 +22,48 @@ 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.
### Your first change ### Your first change
Open `source/main.c` in a text editor. Find: Open `source/actors.c` in a text editor. Find:
```c ```c
static const u16 shirt = RGB(5, 14, 29); TEST_INTERNAL const u16 shirt = RGB(5, 14, 29);
``` ```
Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)` for a red shirt. Save, then double-click **Build and Play.command** again. Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)` for a red shirt. Save, then double-click **Build and Play.command** again.
## Project files ## Project files
- `source/main.c`: map, character, creature, and game rules. - `source/main.c`: GBA hardware, timers, and the main game loop.
- `source/game.h`: shared game types and module declarations.
- `source/video.c`: frame buffer, dirty rows, text, and sprite renderer.
- `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/actors.c`: character and creature artwork and drawing.
- `source/battle.c`: species, battle rules, and the battle screen.
- `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/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.
@@ -53,6 +71,8 @@ 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, 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.
The active map's scenery is cached in a separate 76,800-byte RAM buffer. Walking and dialogue cleanup copy background rows from that cache instead of recalculating every pixel. The cache rebuilds when maps change; opening or closing an exterior door updates only its 13×18-pixel patch. Actors and UI are never stored in the scenery cache. The active map's scenery is cached in a separate 76,800-byte RAM buffer. Walking and dialogue cleanup copy background rows from that cache instead of recalculating every pixel. The cache rebuilds when maps change; opening or closing an exterior door updates only its 13×18-pixel patch. Actors and UI are never stored in the scenery cache.
@@ -61,13 +81,29 @@ 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
Find `dialogue` near the top of `source/main.c`. Each pair of strings is one page of her conversation. Use CAPITAL letters and spaces, with no more than 34 characters on each line. Save and rebuild to try your new conversation. Find `dialogue` in `source/story.c`. Each pair of strings is one page of her conversation. Use CAPITAL letters and spaces, with no more than 34 characters on each line. Save and rebuild to try your new conversation.
## Mac setup ## Mac setup
@@ -94,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
+241
View File
@@ -0,0 +1,241 @@
#include "game.h"
/* These are good first things to change! Colors use numbers from 0 to 31. */
TEST_INTERNAL const u16 shirt = RGB(5, 14, 29);
TEST_INTERNAL const char *const creature_art[] = {
"..11........11..",
".1221......1221.",
".1231......1321.",
"..123111111321..",
"..123333333321..",
".12344444443321.",
".12341334143321.",
".12341534153321.",
".12643333443621.",
"..123337733321..",
"...1233333321...",
"...1234444321...",
"..123444444321..",
"..123344443321..",
"...1223113221...",
"....111..111...."
};
static void creature(void)
{
static const u16 palette[] = {
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)
};
rectangle(175, 102, 14, 3, RGB(8, 16, 7));
sprite(174, 88, creature_art, 16, palette, 0);
}
void scaled_creature(int x, int y, int scale, const u16 *palette)
{
rectangle(x - 7 * scale, y, 14 * scale, 2 * scale,
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);
}
#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)
{
const struct WildArt *art = &wild_art[enemy - FERNFOX];
rectangle(x - 20, y, 40, 3, RGB(8, 16, 7));
blit_sprite(x - 24, y - 48, art->rows, 24, 24, art->palette, 2, 0);
}
void draw_sprout(int x, int y, int scale)
{
static const u16 palette[] = {
0, RGB(5, 10, 7), RGB(6, 16, 8), RGB(12, 24, 10),
RGB(24, 30, 14), RGB(31, 31, 28), RGB(30, 17, 12), RGB(12, 9, 7)
};
scaled_creature(x, y, scale, palette);
rectangle(x - scale, y - 16 * scale, 2 * scale, 3 * scale, RGB(5, 17, 8));
rectangle(x, y - 17 * scale, 4 * scale, 2 * scale, RGB(13, 24, 9));
rectangle(x + scale, y - 17 * scale, 2 * scale, scale, RGB(24, 30, 14));
}
TEST_INTERNAL const char *const person_art[] = {
".....111111.....",
"....12222221....",
"...1223333221...",
"...1233333321...",
"...1222222221...",
"..111111111111..",
"...1444444441...",
"...1466666641...",
"....16166161....",
"....16677661....",
".....166661.....",
"....11888811....",
"...1888998881...",
"..161889988161..",
"..161888888161..",
"..166188881661..",
"...1111111111...",
"....1AA11AA1....",
"....1AA11AA1....",
"....111..111...."
};
TEST_INTERNAL const char *const person_back_art[] = {
".....111111.....", "....12222221....", "...1223333221...", "...1233333321...",
"...1233333321...", "...1222222221...", "...1444444441...", "....14444441....",
"....14444441....", ".....166661.....", "....11888811....", "...1888888881...",
"..1618BBBB8161..", "..1618BCCB8161..", "..1618BCCB8161..", "..1668BBBB8661..",
"...1111111111...", "....1AA11AA1....", "....1AA11AA1....", "....111..111...."
};
TEST_INTERNAL const char *const person_side_art[] = {
"......11111.....", ".....1222221....", "....123333321...", "....123333321...",
"....1222222221..", "....1111111111..", "....144466661...", "....144661661...",
".....14666661...", ".....1467761....", "......16661.....", ".....188881.....",
"....1BB88881....", "....1BC81661....", "....1BB81661....", ".....188161.....",
".....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)
{
int r = color & 31, g = (color >> 5) & 31, b = (color >> 10) & 31;
return RGB(r + (31 - r) / 3, g + (31 - g) / 3, b + (31 - b) / 3);
}
static u16 darker(u16 color)
{
return RGB((color & 31) * 2 / 3, ((color >> 5) & 31) * 2 / 3,
((color >> 10) & 31) * 2 / 3);
}
/* x and y mark the feet. The art fits inside the original erase area. */
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[] = {
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),
clothes, lighter(clothes), RGB(9, 12, 17), RGB(24, 17, 8), RGB(30, 24, 12)
};
rectangle(x - 7, y - 1, 15, 3,
indoors() ? RGB(15, 11, 8) : current_room == BEACH ? RGB(22, 20, 12) : RGB(8, 16, 7));
const char *const *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);
rectangle(x - 4, y, 3, 2 - 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)
{
person(x, y, step, shirt, RGB(28, 8, 9), player_facing);
}
void actors(int x, int y, int step)
{
enum { HERO, SPROUT, MILA, GOLDEN_ACTOR, PROFESSOR, RANGER, FISHER };
struct Actor { int x, y, kind; } list[4];
int count = 0;
if (partner_owned && follower_ready)
list[count++] = (struct Actor){follower_x, follower_y, SPROUT};
list[count++] = (struct Actor){x, y, HERO};
if (current_room == MELLON_HOUSE) {
list[count++] = (struct Actor){PROF_X, PROF_Y, PROFESSOR};
}
if (current_room == OUTSIDE) {
if (!has_caught(GOLDEN)) list[count++] = (struct Actor){182, 104, GOLDEN_ACTOR};
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 == BEACH) list[count++] = (struct Actor){FISHER_X, FISHER_Y, FISHER};
/* Stable depth ordering keeps Sprout behind the hero on equal feet. */
for (int i = 1; i < count; ++i) {
struct Actor actor = list[i];
int j = i;
while (j > 0 && list[j - 1].y > actor.y) {
list[j] = list[j - 1];
--j;
}
list[j] = actor;
}
for (int i = 0; i < count; ++i) {
struct Actor actor = list[i];
if (actor.kind == HERO) player(actor.x, actor.y, step);
else if (actor.kind == SPROUT) draw_member(active_member, actor.x, actor.y, 1);
else if (actor.kind == GOLDEN_ACTOR) creature();
else if (actor.kind == MILA)
styled_person(actor.x, actor.y, 0, RGB(4, 21, 13), RGB(12, 7, 4), FACING_DOWN, GIRL_STYLE);
else if (actor.kind == RANGER)
person(actor.x, actor.y, 0, RGB(27, 16, 7), RGB(6, 15, 11), 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
+207
View File
@@ -0,0 +1,207 @@
#include "game.h"
const struct SpeciesInfo species[SPECIES_COUNT] = {
{"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},
{"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},
{"TIDEFIN", "LV FOUR", "HOOKED A WILD TIDEFIN", "TIDEFIN USED SPLASH JET", "WATER", 24, 4, 9}
};
struct BattleState battle;
unsigned random_state = 0x6d2b79f5u;
int grass_steps;
TEST_INTERNAL unsigned next_random(void)
{
if (!random_state) random_state = 0x6d2b79f5u;
random_state ^= random_state << 13;
random_state ^= random_state >> 17;
random_state ^= random_state << 5;
return random_state;
}
int start_battle(enum Species enemy)
{
if (!partner_owned || battle.phase != BATTLE_OFF || (unsigned)enemy >= SPECIES_COUNT)
return 0;
battle.phase = BATTLE_TURN;
battle.enemy = enemy;
battle.sprout_hp = partner_hp();
battle.enemy_hp = species[enemy].hp;
battle.move = 0;
battle.turns = 0;
battle.notice = NOTICE_NONE;
grass_steps = 0;
return 1;
}
int near_golden(int x, int y)
{
return current_room == OUTSIDE && !has_caught(GOLDEN) &&
x >= 163 && x <= 205 && y >= 84 && y <= 126;
}
int begin_battle(int x, int y)
{
if (!near_golden(x, y)) return 0;
return start_battle(GOLDEN);
}
int check_grass_encounter(int old_x, int old_y, int x, int y)
{
if (!partner_owned || !in_tall_grass(x, y)) { grass_steps = 0; return 0; }
if (battle.phase != BATTLE_OFF || (old_x == x && old_y == y)) return 0;
++grass_steps;
/* No encounters while idle, and a 24-pixel breather after every battle. */
if (grass_steps < 24 || (grass_steps - 24) % 8) return 0;
if ((next_random() & 3u) && grass_steps < 64) return 0;
return start_battle((enum Species)(FERNFOX + next_random() % 3u));
}
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)
{
if (battle.phase == BATTLE_OFF || !pressed) return 0;
if (battle.phase != BATTLE_TURN) {
if (!(pressed & (A | B))) return 0;
battle.phase = BATTLE_OFF;
return 1;
}
if (pressed & B) {
battle.phase = BATTLE_FLED;
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 & RIGHT) battle.move = 1;
if (!(pressed & A)) return !!(pressed & (LEFT | RIGHT));
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;
if (battle.enemy_hp <= 0) {
battle.enemy_hp = 0;
battle.phase = BATTLE_WON;
} else {
enemy_reply();
}
return 1;
}
static void health_bar(int x, int y, int hp, int maximum)
{
rectangle(x, y, 92, 6, RGB(7, 12, 10));
rectangle(x + 1, y + 1, 90, 4, RGB(21, 22, 18));
u16 color = hp * 4 <= maximum ? RGB(29, 9, 8) :
hp * 2 <= maximum ? RGB(29, 23, 7) : RGB(10, 25, 12);
rectangle(x + 1, y + 1, hp * 90 / maximum, 4, color);
}
void draw_battle_hud(void)
{
u16 ink = RGB(5, 8, 11), cream = RGB(30, 30, 26);
health_bar(16, 44, battle.enemy_hp, species[battle.enemy].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(10, 113, 220, 39, cream);
if (battle.phase == BATTLE_TURN) {
large_text(22, 119, "TACKLE", ink);
large_text(132, 119, partner_move(), ink);
int arrow_x = battle.move ? 122 : 12;
rectangle(arrow_x, 119, 2, 7, RGB(7, 17, 11));
rectangle(arrow_x + 2, 121, 2, 3, RGB(7, 17, 11));
text(190, 119, "BALLS", ink);
char count[] = {(char)('0' + ball_count / 10), (char)('0' + ball_count % 10), 0};
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 {
const char *result = battle.phase == BATTLE_CAUGHT ? "CAUGHT IT" : battle.phase == BATTLE_WON ? "YOU WON" :
battle.phase == BATTLE_LOST ? "YOUR PARTNER NEEDS A REST" : "YOU GOT AWAY SAFELY";
large_text(20, 120, result, ink);
text(20, 134, "A BACK TO ADVENTURE", ink);
text(20, 144, battle.phase == BATTLE_CAUGHT ? "NEW FRIEND SAVED TO YOUR TEAM" :
"YOUR PARTNER WILL REST AND RECOVER", ink);
}
}
void draw_battle(void)
{
rectangle(0, 0, WIDTH, HEIGHT, RGB(28, 29, 23));
status(species[battle.enemy].intro);
rectangle(0, 18, WIDTH, 44, RGB(23, 29, 25));
rectangle(0, 62, WIDTH, 49, RGB(18, 26, 15));
rectangle(155, battle.enemy == GOLDEN ? 61 : 70, 64, 5, RGB(12, 21, 11));
rectangle(18, 104, 78, 6, RGB(12, 21, 11));
static const u16 gold_palette[] = {
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 (battle.enemy == GOLDEN) scaled_creature(184, 62, 2, gold_palette);
else draw_wild_creature(battle.enemy, 184, 71);
draw_member(active_member, 57, 106, 0);
rectangle(10, 24, 106, 29, RGB(30, 30, 26));
large_text(16, 28, species[battle.enemy].name, 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));
rectangle(130, 76, 102, 31, RGB(30, 30, 26));
large_text(136, 80, partner_name(), 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();
}
+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;
}
+190
View File
@@ -0,0 +1,190 @@
#ifndef GAME_H
#define GAME_H
typedef unsigned short u16;
#define RGB(r, g, b) ((u16)((r) | ((g) << 5) | ((b) << 10)))
#define WIDTH 240
#define HEIGHT 160
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 { NPC_X = 146, NPC_Y = 106, PROF_X = 113, PROF_Y = 92, RANGER_X = 83, RANGER_Y = 97 };
/* Room and facing IDs are persistent: never reorder or reuse them. */
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 { FOLLOW_STEPS = 14, SPROUT_HP = 24, GOLDEN_HP = 18 };
enum BattlePhase { BATTLE_OFF, BATTLE_TURN, BATTLE_WON, BATTLE_LOST, BATTLE_FLED, BATTLE_CAUGHT };
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 };
struct BattleState {
enum BattlePhase phase;
enum Species enemy;
int sprout_hp, enemy_hp, move, turns;
enum BattleNotice notice;
};
extern struct BattleState battle;
struct SpeciesInfo {
const char *name, *level, *intro, *reply, *type;
int hp, attack, leaf_damage;
};
struct WildArt { const char *const *rows; const u16 *palette; };
#ifdef GAME_TEST
#define TEST_INTERNAL
extern u16 test_pixels[WIDTH * HEIGHT + 2];
extern unsigned char test_save[32];
#define SCREEN (test_pixels + 1)
#define SAVE_MEMORY test_save
#else
#define TEST_INTERNAL static
extern u16 frame_buffer[WIDTH * HEIGHT];
#define SCREEN frame_buffer
#endif
extern u16 dirty_first[HEIGHT];
extern u16 dirty_end[HEIGHT];
extern enum Room current_room;
extern int door_open;
extern enum Facing player_facing;
extern int follower_x, follower_y, follower_ready;
extern unsigned random_state;
extern int grass_steps;
extern int partner_owned;
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 mark_dirty(int x, int y, int w, int h);
void rectangle(int x, int y, int w, int h, u16 color);
void text(int x, int y, const char *message, u16 color);
void large_text(int x, int y, const char *message, u16 color);
void blit_sprite(int x, int y, const char *const *art, int width, int rows,
const u16 *palette, int scale, int mirrored);
void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
int mirrored);
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 pause_movement_at(unsigned now);
int movement_ticks_at(unsigned now);
void reset_follower(int x, int y);
int indoors(void);
int near_door(int x, int y);
int near_map_exit(int x, int y);
int near_route_sign(int x, int y);
int use_door(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);
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_sprout(int x, int y, int scale);
void actors(int x, int y, int step);
int near_golden(int x, int y);
int begin_battle(int x, int y);
int check_grass_encounter(int old_x, int old_y, int x, int y);
int battle_input(unsigned pressed);
void draw_battle_hud(void);
void draw_battle(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 begin_conversation(int x, int y);
int advance_conversation(int page);
void dialogue_box(int page);
void status(const char *message);
void location_status(int x, int y);
void draw_scene(int x, int y);
void draw_team(void);
void draw_team_member(int member);
#ifdef GAME_TEST
unsigned save_crc(const unsigned char *bytes, int length);
extern const unsigned char large_alphabet[26][7];
extern u16 background_cache[WIDTH * HEIGHT];
extern enum Room background_room;
extern int background_valid, background_door_open;
extern unsigned scenery_samples;
extern unsigned diagonal_fraction;
extern const u16 shirt;
extern const char *const creature_art[16];
extern const char *const person_art[20];
extern const char *const person_back_art[20];
extern const char *const person_side_art[20];
extern const char *const girl_art[20], *const girl_back_art[20], *const girl_side_art[20];
extern int gift_conversation;
extern const char *const dialogue[][2];
extern const char *const professor_dialogue[][2];
extern const char *const professor_followup[][2];
extern const char *const ranger_dialogue[][2];
extern const char *const ranger_followup[][2];
extern int trail_head, ball_gift_conversation;
extern unsigned rectangle_calls, sprite_decodes, sprite_row_marks;
extern const int DIALOGUE_PAGES;
extern const char *const fernfox_art[24], *const emberwing_art[24], *const pebblit_art[24];
extern const char *const tidefin_art[24];
extern const struct WildArt wild_art[4];
u16 cottage_scenery(int x, int y);
u16 route_scenery(int x, int y);
u16 village_scenery(int x, int y);
u16 mellon_garden_scenery(int x, int y);
u16 scenery(int x, int y);
void move_follower(int old_x, int old_y, int x, int y);
enum Facing facing_for_keys(unsigned keys, enum Facing previous);
int use_map_exit(int *x, int *y, unsigned keys);
int can_walk(int x, int y);
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);
int next_dialogue_page(int page);
int receive_partner(void);
int receive_balls(void);
#endif
#endif
+120
View File
@@ -0,0 +1,120 @@
#include "game.h"
static void draw_bag_summary(void);
void status(const char *message)
{
rectangle(0, 0, WIDTH, 18, RGB(29, 30, 25));
rectangle(0, 16, WIDTH, 2, RGB(10, 18, 14));
text(8, 6, message, RGB(5, 9, 11));
}
void location_status(int x, int y)
{
if (near_map_exit(x, y)) {
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 status(x >= 216 ? "WALK RIGHT TO PROFESSOR MELLON" : "WALK DOWN TO SUNPETAL TRAIL");
}
else if (near_route_sign(x, y)) status("A READ SIGN");
else if (current_room == ROUTE && near_npc(x, y)) status("A TALK TO RANGER FINN");
else if (current_room == ROUTE) status("SUNPETAL TRAIL");
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_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 status(current_room == COTTAGE ? "INSIDE THE COTTAGE" : "GREENLEAF VILLAGE");
}
void draw_scene(int x, int y)
{
restore(0, 0, WIDTH, HEIGHT);
location_status(x, y);
text(8, 148, "ARROWS MOVE A USE SELECT TEAM START MENU R MUSIC", RGB(5, 9, 11));
actors(x, y, 0);
}
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);
rectangle(0, 0, WIDTH, HEIGHT, RGB(15, 24, 20));
status("YOUR TEAM");
rectangle(8, 25, 224, 112, ink);
rectangle(10, 27, 220, 108, cream);
if (partner_owned) {
if (member == 0) {
draw_sprout(50, 105, 3);
large_text(94, 42, "SPROUT", 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);
if (member == active_member) text(94, 109, "FOLLOWING AND BATTLING", ink);
rectangle(94, 99, 106, 6, RGB(7, 14, 11));
rectangle(95, 100, 104, 4, RGB(10, 25, 12));
} else {
large_text(28, 57, "NO PARTNER YET", ink);
large_text(28, 76, "VISIT PROFESSOR MELLON", ink);
text(28, 96, "HER HOUSE IS EAST OF GREENLEAF VILLAGE", ink);
}
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(25, 119, 6, 3, RGB(29, 8, 9));
rectangle(25, 122, 6, 3, RGB(31, 31, 28));
rectangle(27, 121, 2, 2, ink);
text(38, 119, "POKE BALLS", ink);
char count_text[3];
int count_index = 0;
if (ball_count >= 10) count_text[count_index++] = (char)('0' + ball_count / 10);
count_text[count_index++] = (char)('0' + ball_count % 10);
count_text[count_index] = 0;
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);
text(8, 148, "A B BACK START CLOSE", ink);
}
+51 -1407
View File
File diff suppressed because it is too large. Load diff
+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;
}
+404
View File
@@ -0,0 +1,404 @@
#include "game.h"
static const u16 grass = RGB(15, 26, 12);
/* One scenery-only cache; the frame buffer still holds actors and UI. */
TEST_INTERNAL u16 background_cache[WIDTH * HEIGHT];
TEST_INTERNAL enum Room background_room;
TEST_INTERNAL int background_valid, background_door_open;
#ifdef GAME_TEST
TEST_INTERNAL unsigned scenery_samples;
#endif
int in_tall_grass(int x, int y)
{
return current_room == ROUTE &&
((x >= 32 && x < 81 && y >= 53 && y < 87) ||
(x >= 151 && x < 208 && y >= 112 && y < 133));
}
/* Little shaded trees, made from pixels rather than downloaded artwork. */
static u16 tree_pixel(int x, int y, u16 ground)
{
int dx = x - 9, dy = y - 8;
int distance = dx * dx + dy * dy;
if (y >= 15 && y < 21 && x >= 7 && x <= 11)
ground = x < 9 ? RGB(11, 8, 5) : RGB(17, 12, 6);
if (distance < 76) {
if (distance > 54) return RGB(3, 10, 7);
if (y > 10 || x > 12) return RGB(5, 15, 8);
if ((x + y * 2) % 7 < 2) return RGB(13, 24, 10);
return RGB(8, 20, 9);
}
return ground;
}
/* A cozy room: wooden floor, a bed, bookcase, dresser, plant, and table. */
TEST_INTERNAL u16 cottage_scenery(int x, int y)
{
u16 color = RGB(3, 7, 10);
if (y < 18 || y >= 142) return RGB(29, 30, 25);
if (x < 18 || x >= 222 || y < 24) return color;
color = RGB(13, 9, 7);
if (x >= 24 && x < 216 && y >= 28 && y < 54) {
color = y < 43 ? RGB(29, 27, 21) : RGB(16, 21, 18);
if (y == 42 || y == 51) color = RGB(10, 13, 12);
if (y > 43 && x % 16 == 0) color = RGB(12, 17, 15);
}
if (x >= 26 && x < 214 && y >= 54 && y < 135) {
color = ((y / 8) & 1) ? RGB(24, 18, 12) : RGB(26, 20, 13);
if (y % 8 == 0 || (x + (y / 8) * 23) % 47 == 0) color = RGB(17, 12, 9);
if (y % 8 == 1) color = RGB(29, 23, 16);
if (y % 8 == 5 && (x * 3 + y) % 41 < 8) color = RGB(22, 16, 11);
}
/* Window and sill above the bed. */
if (x >= 43 && x < 70 && y >= 30 && y < 49) {
color = RGB(11, 10, 9);
if (x >= 45 && x < 68 && y >= 32 && y < 46) color = RGB(12, 25, 29);
if (x >= 46 && x < 54 && y >= 33 && y < 38) color = RGB(25, 31, 30);
if (x == 56 || y == 39) color = RGB(27, 25, 19);
if (y >= 47) color = RGB(25, 20, 14);
}
/* A bookcase fixed to the back wall. */
if (x >= 150 && x < 184 && y >= 29 && y < 54) {
color = RGB(11, 8, 6);
if (x >= 153 && x < 181 && y >= 32 && y < 51) {
int book = (x - 153) / 4;
color = book % 3 == 0 ? RGB(24, 10, 10) :
book % 3 == 1 ? RGB(9, 18, 21) : RGB(25, 21, 12);
if (x % 4 == 0) color = RGB(30, 27, 18);
}
if (y == 41 || y == 52) color = RGB(24, 16, 10);
}
/* Bed: headboard, pillow, blue blanket, and wooden feet. */
if (x >= 34 && x < 69 && y >= 59 && y < 98) {
color = RGB(10, 8, 7);
if (x >= 37 && x < 66 && y >= 62 && y < 94) color = RGB(29, 27, 21);
if (x >= 40 && x < 63 && y >= 64 && y < 72)
color = y == 71 ? RGB(23, 24, 23) : RGB(31, 31, 28);
if (x >= 37 && x < 66 && y >= 75 && y < 94) {
color = RGB(7, 15, 24);
if (x < 41 || x > 62) color = RGB(5, 11, 20);
if (y == 76 || y == 89) color = RGB(15, 25, 29);
if (y > 78 && y < 87 && (x + y) % 10 == 0) color = RGB(22, 29, 30);
}
if (y < 62 || y == 95) color = RGB(20, 13, 8);
}
/* Dresser with two little brass handles. */
if (x >= 96 && x < 132 && y >= 54 && y < 69) {
color = RGB(13, 9, 6);
if (x > 98 && x < 129 && y > 56 && y < 67) color = RGB(23, 16, 10);
if (y == 61) color = RGB(15, 10, 7);
if (x >= 111 && x < 116 && (y == 59 || y == 64)) color = RGB(31, 25, 10);
if (y == 54) color = RGB(29, 22, 14);
}
/* Red woven rug, clear of the furniture. */
if (x >= 86 && x < 140 && y >= 83 && y < 113) {
color = RGB(22, 9, 12);
if (x < 89 || x >= 137 || y < 86 || y >= 110) color = RGB(29, 23, 15);
else if ((x + y) % 12 == 0 || (x - y + 240) % 12 == 0) color = RGB(28, 15, 16);
}
/* Dining table, shaded edge, legs, and a little cup. */
if (x >= 150 && x < 188 && y >= 76 && y < 102) {
color = RGB(12, 8, 6);
if (y < 97 && x > 151 && x < 186) color = RGB(25, 18, 11);
if (y == 78) color = RGB(30, 24, 16);
if (y >= 97 && x >= 156 && x < 182) return RGB(18, 13, 9);
if (x >= 168 && x < 174 && y >= 83 && y < 89) color = RGB(30, 29, 24);
if (x >= 169 && x < 173 && y == 83) color = RGB(12, 9, 7);
}
/* Potted plant tucked into the top-right corner. */
if (x >= 197 && x < 207 && y >= 65 && y < 77)
color = y < 68 ? RGB(28, 17, 10) : RGB(21, 11, 8);
int px = x - 201, py = y - 60;
if (px * px + py * py < 51)
color = (x + y) % 4 == 0 ? RGB(15, 25, 10) : RGB(5, 17, 9);
/* Entry mat and a bright opening through the bottom wall. */
if (x >= 108 && x < 132 && y >= 120 && y < 134)
color = y % 3 == 0 ? RGB(21, 17, 11) : RGB(29, 25, 17);
if (x >= 108 && x < 132 && y >= 134) {
color = x < 111 || x >= 129 ? RGB(25, 18, 11) : RGB(12, 23, 11);
if (x >= 113 && x < 127) color = RGB(27, 25, 16);
}
return color;
}
/* Sunpetal Trail, the first area outside Greenleaf Village. */
TEST_INTERNAL u16 route_scenery(int x, int y)
{
if (y < 18 || y >= 142) return RGB(29, 30, 25);
u16 color = ((x / 16 + y / 16) & 1) ? RGB(16, 27, 13) : grass;
if ((x & 15) == 4 && (y & 7) >= 5) color = RGB(11, 22, 10);
if ((x & 15) == 11 && (y & 15) == 3) color = RGB(21, 29, 15);
if (x < 24 || x >= 216 || y >= 136) color = RGB(7, 18, 9);
if (y < 40 && (x < 88 || x >= 128))
color = tree_pixel((x - 8) % 20, y - 19, color);
if (x < 24) color = tree_pixel(x - 2, (y - 20) % 22, color);
if (x >= 216) color = tree_pixel(x - 217, (y - 20) % 22, color);
if (y >= 128) color = tree_pixel((x - 8) % 20, y - 127, color);
/* A central trail and a short branch to the pond lookout. */
if ((x >= 97 && x < 120 && y < 136) ||
(x >= 97 && x < 210 && y >= 89 && y < 104)) {
color = RGB(29, 27, 18);
if ((x == 97 || x == 119) && (y < 89 || y >= 104)) color = RGB(20, 23, 12);
if ((y == 89 || y == 103) && x >= 120) color = RGB(20, 23, 12);
if ((x & 7) == 3 && (y & 7) == 5) color = RGB(25, 23, 15);
if ((x & 15) == 10 && (y & 7) == 2) color = RGB(31, 29, 21);
}
/* The encounter check uses these exact same grass boundaries. */
if (in_tall_grass(x, y)) {
int tx = x & 7, ty = y & 7;
if ((tx == 1 && ty >= 2 && ty <= 5) || (tx == 3 && ty >= 1 && ty <= 5) ||
(tx == 5 && ty >= 3 && ty <= 6)) color = RGB(4, 15, 8);
if ((tx == 2 && ty >= 3 && ty <= 6) || (tx == 4 && ty >= 2 && ty <= 6))
color = RGB(9, 22, 9);
if ((tx == 1 && ty == 1) || (tx == 3 && ty == 0)) color = RGB(22, 29, 12);
}
if (x >= 152 && x < 208 && y >= 46 && y < 79) {
color = RGB(27, 25, 16);
if (x >= 155 && x < 205 && y >= 49 && y < 76) {
color = y < 54 ? RGB(6, 15, 25) : RGB(8, 20, 29);
if (y % 9 == 0 && (x + y * 3) % 19 < 7) color = RGB(17, 28, 30);
if (y == 75) color = RGB(13, 25, 28);
}
if (x == 154 || y == 48) color = RGB(31, 29, 20);
}
/* A readable sign pointing north, on two wooden posts. */
if ((x == 128 || x == 142) && y >= 64 && y < 74) color = RGB(16, 11, 7);
if (x >= 124 && x < 147 && y >= 55 && y < 68) {
color = RGB(13, 9, 7);
if (x > 125 && x < 145 && y > 56 && y < 66) color = RGB(29, 24, 15);
if ((x == 135 && y >= 59 && y <= 64) ||
(y == 60 && x >= 134 && x <= 136) ||
(y == 61 && x >= 133 && x <= 137)) color = RGB(10, 15, 11);
}
/* A small boulder beside the lower trail. */
int rx = x - 49, ry = y - 114;
if (rx * rx + ry * ry * 2 < 190) {
color = RGB(10, 13, 14);
if (rx * rx + ry * ry * 2 < 145) color = RGB(19, 22, 22);
if (y < 112 && x < 53 && rx * rx + ry * ry * 2 < 130) color = RGB(26, 28, 26);
if (y > 118) color = RGB(13, 17, 17);
}
return color;
}
/* Calculate scenery at one pixel so moving never leaves sprite trails. */
TEST_INTERNAL u16 village_scenery(int x, int y)
{
if (y < 18 || y >= 142) return RGB(29, 30, 25);
u16 color = grass;
if (((x >> 4) + (y >> 4)) & 1) color = RGB(16, 27, 13);
int gx = x & 15, gy = y & 15;
if ((gx == 4 && gy >= 9 && gy <= 11) ||
(gx == 3 && gy == 8) || (gx == 6 && gy == 9))
color = RGB(11, 22, 10);
if (gx == 11 && gy == 4) color = RGB(20, 29, 15);
if (x < 8 || x >= 232 || y >= 136) {
/* Wooden boundary fences, with a gap where the path leaves town. */
int post = (x < 8 || x >= 232) ? y % 16 : x % 16;
color = RGB(9, 20, 10);
if (post < 4) color = post == 0 ? RGB(13, 10, 7) : RGB(27, 22, 14);
else if (y >= 138 || (x < 8 && x >= 3) || (x >= 232 && x < 237))
color = RGB(22, 17, 11);
if (y >= 136 && x > 96 && x < 120) color = RGB(28, 26, 18);
if (x >= 232 && y > 106 && y < 120) color = RGB(29, 27, 18);
return color;
}
if (y < 40 && (x < 88 || x >= 128))
color = tree_pixel((x - 8) % 20, y - 19, color);
/* Warm sand, darker edges, and little scattered pebbles. */
if ((x > 96 && x < 120) || (y > 106 && y < 120)) {
color = RGB(29, 27, 18);
if (((x == 97 || x == 119) && (y <= 106 || y >= 120)) ||
((y == 107 || y == 119) && (x <= 96 || x >= 120)))
color = RGB(20, 23, 12);
if ((x & 7) == 3 && (y & 7) == 5) color = RGB(25, 23, 15);
if ((x & 15) == 10 && (y & 7) == 2) color = RGB(31, 29, 21);
}
/* Rounded pond, shoreline, wavelets, reeds, and a flowering lily. */
if (x >= 22 && x < 76 && y >= 44 && y < 86) {
int corner = (x < 26 || x > 71) && (y < 48 || y > 81);
if (!corner) {
color = RGB(22, 22, 13);
if (x >= 24 && x < 74 && y >= 46 && y < 84) color = RGB(29, 27, 18);
if (x >= 26 && x < 72 && y >= 48 && y < 82) {
color = y < 53 ? RGB(6, 15, 25) : RGB(8, 20, 29);
if (y >= 78) color = RGB(10, 23, 26);
if (y % 9 == 0 && (x + y * 3) % 19 < 7) color = RGB(17, 28, 29);
if (y % 9 == 1 && (x + y * 3) % 19 < 5) color = RGB(8, 21, 27);
int lx = x - 61, ly = y - 59;
if (lx * lx + ly * ly < 19 && !(lx > 0 && ly < 0))
color = ly < 0 ? RGB(16, 26, 10) : RGB(8, 20, 10);
if ((x == 59 && y >= 55 && y <= 57) ||
(y == 56 && x >= 58 && x <= 60)) color = RGB(31, 19, 24);
}
}
if ((x == 25 || x == 29 || x == 31) && y >= 76 && y <= 81)
color = RGB(7, 17, 7);
}
/* Cottage: chimney, individual roof tiles, beams, glass, and steps. */
if (x >= 154 && x < 204 && y >= 70 && y < 76) color = RGB(8, 17, 9);
if (x >= 170 && x < 185 && y >= 71 && y < 78)
color = y % 3 == 0 ? RGB(15, 16, 14) : RGB(24, 24, 19);
if (x >= 191 && x < 198 && y >= 27 && y < 39) {
color = ((y % 4 == 0) || (x == 194 && y % 8 < 4)) ? RGB(14, 10, 9) : RGB(22, 16, 13);
if (y == 27) color = RGB(10, 8, 8);
}
if (x >= 152 && x < 201 && y >= 49 && y < 72) {
color = RGB(29, 27, 20);
if (y < 52) color = RGB(21, 19, 14);
if (x < 155 || x >= 198 || y == 69 || y == 71) color = RGB(13, 10, 8);
if (x >= 171 && x < 184 && y >= 54) {
color = RGB(12, 8, 6);
if (door_open) {
color = RGB(3, 5, 7);
if (x == 171) color = RGB(23, 16, 10);
} else {
if (x >= 173 && x < 182 && y > 55) color = RGB(20, 13, 8);
if (x == 177) color = RGB(16, 10, 7);
if (x == 180 && y == 63) color = RGB(31, 25, 10);
}
}
if (((x >= 157 && x < 167) || (x >= 186 && x < 196)) && y >= 54 && y < 65) {
int wx = x >= 186 ? x - 186 : x - 157;
color = RGB(12, 11, 10);
if (wx > 0 && wx < 9 && y > 54 && y < 63) color = RGB(10, 21, 27);
if (wx > 1 && wx < 4 && y > 55 && y < 59) color = RGB(23, 30, 30);
if (wx == 4 || y == 59) color = RGB(21, 19, 14);
if (y == 64) color = RGB(26, 24, 18);
}
}
if (y >= 32 && y < 49 && x >= 147 + (48 - y) / 2 && x < 206 - (48 - y) / 2) {
color = RGB(25, 10, 10);
if ((y - 32) % 5 == 0 || (x + ((y - 32) / 5) * 4) % 9 == 0)
color = RGB(16, 7, 8);
else if ((y - 32) % 5 == 1) color = RGB(30, 15, 12);
if (y == 48) color = RGB(10, 8, 8);
}
/* Flowers have stems, petals, and golden centers. */
if ((x > 126 && x < 146 && y > 77 && y < 98) ||
(x > 28 && x < 72 && y > 95 && y < 104)) {
int fx = x % 7, fy = y % 8;
if (fx == 3 && fy >= 4 && fy <= 6) color = RGB(5, 16, 7);
if ((fx == 2 && fy == 5) || (fx == 4 && fy == 6)) color = RGB(7, 20, 8);
if ((fx == 3 && (fy == 1 || fy == 3)) || (fy == 2 && (fx == 2 || fx == 4)))
color = (x / 7) & 1 ? RGB(31, 20, 24) : RGB(30, 30, 25);
if (fx == 3 && fy == 2) color = RGB(31, 24, 6);
}
/* Decorative tall-grass tiles: walk through them, no encounters yet. */
if (x >= 24 && x < 80 && y >= 123 && y < 134) {
int tx = x & 7, ty = (y - 123) & 7;
if ((tx == 1 && ty >= 2 && ty <= 5) || (tx == 3 && ty >= 1 && ty <= 5) ||
(tx == 5 && ty >= 3 && ty <= 6)) color = RGB(4, 15, 8);
if ((tx == 2 && ty >= 3 && ty <= 6) || (tx == 4 && ty >= 2 && ty <= 6))
color = RGB(9, 22, 9);
if ((tx == 1 && ty == 1) || (tx == 3 && ty == 0)) color = RGB(22, 29, 12);
}
return color;
}
TEST_INTERNAL u16 mellon_garden_scenery(int x, int y)
{
u16 color = village_scenery(x, y);
if (y < 18 || y >= 142) return color;
/* 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 >= 232 && y > 106 && y < 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. */
if (x >= 22 && x < 76 && y >= 44 && y < 86) {
color = RGB(20, 15, 9);
if (x == 22 || x == 75 || y == 44 || y == 85) color = RGB(29, 23, 14);
if (y % 12 == 0) color = RGB(13, 10, 7);
int mx = (x - 24) % 16 - 7, my = (y - 45) % 18 - 8;
if (mx * mx + my * my < 30) {
color = x % 4 < 2 ? RGB(9, 19, 8) : RGB(19, 28, 12);
if (my > 3) color = RGB(6, 14, 7);
}
if (my == -6 && mx == 0) color = RGB(6, 15, 7);
}
/* A blue roof makes the professor's house easy to recognize. */
if (x >= 147 && x < 206 && y >= 32 && y < 49) {
if (color == RGB(25, 10, 10)) color = RGB(8, 16, 24);
else if (color == RGB(16, 7, 8)) color = RGB(5, 10, 17);
else if (color == RGB(30, 15, 12)) color = RGB(14, 23, 29);
}
return color;
}
static u16 mellon_house_scenery(int x, int y)
{
u16 color = cottage_scenery(x, y);
if (color == RGB(22, 9, 12)) color = RGB(7, 17, 15);
else if (color == RGB(28, 15, 16)) color = RGB(12, 23, 20);
/* A little research computer on the desk at the back of the room. */
if (x >= 104 && x < 124 && y >= 36 && y < 54) {
color = RGB(10, 13, 16);
if (x > 106 && x < 121 && y > 38 && y < 49) color = RGB(9, 23, 26);
if (x > 108 && x < 118 && (y == 41 || y == 45)) color = RGB(23, 30, 26);
if (y >= 50 && (x < 112 || x > 116)) color = cottage_scenery(x, y);
}
/* An open notebook on the table. */
if (x >= 157 && x < 166 && y >= 83 && y < 91) {
color = x == 161 ? RGB(18, 19, 16) : RGB(31, 30, 25);
if ((y == 85 || y == 88) && x != 161) color = RGB(18, 23, 23);
}
return color;
}
TEST_INTERNAL u16 scenery(int x, int y)
{
#ifdef GAME_TEST
++scenery_samples;
#endif
if (current_room == ROUTE) return route_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_HOUSE) return mellon_house_scenery(x, y);
if (current_room == BEACH) return beach_scenery(x, y);
return village_scenery(x, y);
}
static void ensure_background(void)
{
if (!background_valid || background_room != current_room) {
for (int row = 0; row < HEIGHT; ++row)
for (int col = 0; col < WIDTH; ++col)
background_cache[row * WIDTH + col] = scenery(col, row);
background_room = current_room;
background_door_open = door_open;
background_valid = 1;
} else if (background_door_open != door_open) {
/* Only these door pixels change; keep the rest of the map cached. */
if (current_room == OUTSIDE || current_room == MELLON_GARDEN)
for (int row = 54; row < 72; ++row)
for (int col = 171; col < 184; ++col)
background_cache[row * WIDTH + col] = scenery(col, row);
background_door_open = door_open;
}
}
void restore(int x, int y, int w, int h)
{
int left = x < 0 ? 0 : x;
int right = x + w > WIDTH ? WIDTH : x + w;
int top = y < 0 ? 0 : y;
int bottom = y + h > HEIGHT ? HEIGHT : y + h;
if (left >= right || top >= bottom) return;
ensure_background();
mark_dirty(left, top, right - left, bottom - top);
for (int row = top; row < bottom; ++row) {
const u16 *source = &background_cache[row * WIDTH + left];
u16 *destination = &SCREEN[row * WIDTH + left];
for (int col = 0; col < right - left; ++col) destination[col] = source[col];
}
}
+163
View File
@@ -0,0 +1,163 @@
#include "game.h"
int partner_owned = 0;
TEST_INTERNAL int gift_conversation = 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;
int choose_partner(int member)
{
if (!partner_owned || battle.phase != BATTLE_OFF || member < 0 || member > caught_count)
return 0;
active_member = member;
return 1;
}
const char *partner_name(void)
{
return active_member ? species[caught_species[active_member - 1]].name : "SPROUT";
}
int partner_hp(void)
{
return active_member ? species[caught_species[active_member - 1]].hp : SPROUT_HP;
}
const char *partner_move(void)
{
if (!active_member) return "LEAF TAP";
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. */
TEST_INTERNAL const char *const dialogue[][2] = {
{"HELLO I AM MILA", "WELCOME TO OUR VILLAGE"},
{"THAT GOLDEN CREATURE IS FRIENDLY", "WALK OVER AND SAY HELLO"}
};
enum { STORY_DIALOGUE_PAGES = sizeof(dialogue) / sizeof(dialogue[0]) };
#ifdef GAME_TEST
const int DIALOGUE_PAGES = STORY_DIALOGUE_PAGES;
#endif
#define DIALOGUE_PAGES STORY_DIALOGUE_PAGES
TEST_INTERNAL const char *const professor_dialogue[DIALOGUE_PAGES][2] = {
{"I HAVE A PARTNER FOR YOU", "MEET SPROUT A GRASS TYPE"},
{"YOU RECEIVED SPROUT", "TAKE GOOD CARE OF YOUR NEW FRIEND"}
};
TEST_INTERNAL const char *const professor_followup[DIALOGUE_PAGES][2] = {
{"HOW IS SPROUT DOING", "OPEN YOUR TEAM WITH SELECT"},
{"EXPLORE SUNPETAL TRAIL TOGETHER", "I AM GLAD YOU ARE FRIENDS"}
};
TEST_INTERNAL const char *const ranger_dialogue[DIALOGUE_PAGES][2] = {
{"HI I AM RANGER FINN", "HERE ARE FIVE POKE BALLS FOR YOU"},
{"YOU RECEIVED FIVE POKE BALLS", "PRESS SELECT IN BATTLE TO THROW"}
};
TEST_INTERNAL const char *const ranger_followup[DIALOGUE_PAGES][2] = {
{"WEAKEN WILD CREATURES FIRST", "LOW HEALTH MAKES CATCHING EASIER"},
{"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)
{
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)
return x >= RANGER_X - 24 && x <= RANGER_X + 24 &&
y >= RANGER_Y - 22 && y <= RANGER_Y + 22;
if (current_room == MELLON_HOUSE)
return x >= PROF_X - 24 && x <= PROF_X + 24 &&
y >= PROF_Y - 22 && y <= PROF_Y + 22;
return current_room == OUTSIDE && x >= NPC_X - 24 && x <= NPC_X + 24 &&
y >= NPC_Y - 20 && y <= NPC_Y + 20;
}
TEST_INTERNAL int next_dialogue_page(int page)
{
return page + 1 < DIALOGUE_PAGES ? page + 1 : -1;
}
int begin_conversation(int x, int y)
{
if (!near_npc(x, y)) return 0;
gift_conversation = current_room == MELLON_HOUSE && !partner_owned;
ball_gift_conversation = current_room == ROUTE && !balls_given;
rod_gift_conversation = current_room == BEACH && !rod_owned;
return 1;
}
TEST_INTERNAL int receive_partner(void)
{
if (current_room != MELLON_HOUSE || !gift_conversation || partner_owned) return 0;
partner_owned = 1;
reset_follower(PROF_X, PROF_Y + 22);
return 1;
}
TEST_INTERNAL int receive_balls(void)
{
if (current_room != ROUTE || !ball_gift_conversation || balls_given) return 0;
ball_count += 5;
if (ball_count > 99) ball_count = 99;
balls_given = 1;
return 1;
}
int advance_conversation(int page)
{
int next = next_dialogue_page(page);
if (next == 1 && gift_conversation) receive_partner();
if (next == 1 && ball_gift_conversation) receive_balls();
if (next == 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;
return next;
}
void dialogue_box(int page)
{
u16 ink = RGB(5, 8, 11);
rectangle(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H, ink);
rectangle(9, 97, 222, 42, RGB(15, 20, 21));
rectangle(11, 99, 218, 38, RGB(30, 30, 26));
rectangle(12, 100, 216, 1, RGB(31, 31, 30));
rectangle(16, 110, 208, 1, RGB(23, 26, 22));
int professor = current_room == MELLON_HOUSE;
int ranger = current_room == ROUTE;
const char *const (*lines)[2] = current_room == BEACH ?
(rod_gift_conversation ? fisher_dialogue : fisher_followup) : ranger ?
(ball_gift_conversation ? ranger_dialogue : ranger_followup) : professor ?
(gift_conversation ? professor_dialogue : professor_followup) : dialogue;
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);
large_text(16, 115, lines[page][0], ink);
large_text(16, 127, lines[page][1], ink);
}
+207
View File
@@ -0,0 +1,207 @@
#include "game.h"
#ifndef GAME_TEST
/* Never erase a character on the visible screen. Finish drawing in RAM. */
u16 frame_buffer[WIDTH * HEIGHT];
#endif
u16 dirty_first[HEIGHT];
u16 dirty_end[HEIGHT];
void reset_dirty_rows(void)
{
for (int y = 0; y < HEIGHT; ++y) {
dirty_first[y] = WIDTH;
dirty_end[y] = 0;
}
}
void mark_dirty(int x, int y, int w, int h)
{
int left = x < 0 ? 0 : x;
int right = x + w > WIDTH ? WIDTH : x + w;
int top = y < 0 ? 0 : y;
int bottom = y + h > HEIGHT ? HEIGHT : y + h;
if (left >= right || top >= bottom) return;
for (int row = top; row < bottom; ++row) {
if (left < dirty_first[row]) dirty_first[row] = left;
if (right > dirty_end[row]) dirty_end[row] = right;
}
}
#ifdef GAME_TEST
unsigned rectangle_calls, sprite_decodes, sprite_row_marks;
#endif
void rectangle(int x, int y, int w, int h, u16 color)
{
#ifdef GAME_TEST
++rectangle_calls;
#endif
mark_dirty(x, y, w, h);
for (int row = y; row < y + h; ++row) {
for (int col = x; col < x + w; ++col) {
if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT)
SCREEN[row * WIDTH + col] = color;
}
}
}
/* Tiny original 3-by-5 alphabet: one row of bits per letter. */
static const unsigned char alphabet[26][5] = {
{2,5,7,5,5}, {6,5,6,5,6}, {3,4,4,4,3}, {6,5,5,5,6},
{7,4,6,4,7}, {7,4,6,4,4}, {3,4,5,5,3}, {5,5,7,5,5},
{7,2,2,2,7}, {1,1,1,5,2}, {5,5,6,5,5}, {4,4,4,4,7},
{5,7,7,5,5}, {5,7,7,7,5}, {2,5,5,5,2}, {6,5,6,4,4},
{2,5,5,3,1}, {6,5,6,5,5}, {3,4,2,1,6}, {7,2,2,2,2},
{5,5,5,5,7}, {5,5,5,5,2}, {5,5,7,7,5}, {5,5,2,5,5},
{5,5,2,2,2}, {7,1,2,4,7}
};
static const unsigned char digit_font[10][5] = {
{7,5,5,5,7}, {2,6,2,2,7}, {6,1,2,4,7}, {6,1,2,1,6}, {5,5,7,1,1},
{7,4,6,1,6}, {3,4,7,5,7}, {7,1,2,2,2}, {7,5,7,5,7}, {7,5,7,1,6}
};
void text(int x, int y, const char *message, u16 color)
{
for (; *message; ++message, x += 4) {
const unsigned char *letter;
if (*message >= 'A' && *message <= 'Z') letter = alphabet[*message - 'A'];
else if (*message >= '0' && *message <= '9') letter = digit_font[*message - '0'];
else continue;
for (int row = 0; row < 5; ++row)
for (int col = 0; col < 3; ++col)
if (letter[row] & (4 >> col))
rectangle(x + col, y + row, 1, 1, color);
}
}
/* Larger, easy-to-read letters for the RPG conversation window. */
TEST_INTERNAL const unsigned char large_alphabet[26][7] = {
{14,17,17,31,17,17,17}, {30,17,17,30,17,17,30},
{14,17,16,16,16,17,14}, {30,17,17,17,17,17,30},
{31,16,16,30,16,16,31}, {31,16,16,30,16,16,16},
{14,17,16,23,17,17,14}, {17,17,17,31,17,17,17},
{14,4,4,4,4,4,14}, {7,2,2,2,18,18,12},
{17,18,20,24,20,18,17}, {16,16,16,16,16,16,31},
{17,27,21,21,17,17,17}, {17,25,21,19,17,17,17},
{14,17,17,17,17,17,14}, {30,17,17,30,16,16,16},
{14,17,17,17,21,18,13}, {30,17,17,30,20,18,17},
{15,16,16,14,1,1,30}, {31,4,4,4,4,4,4},
{17,17,17,17,17,17,14}, {17,17,17,17,17,10,4},
{17,17,17,21,21,21,10}, {17,17,10,4,10,17,17},
{17,17,10,4,4,4,4}, {31,1,2,4,8,16,31}
};
void large_text(int x, int y, const char *message, u16 color)
{
for (; *message; ++message, x += 6) {
if (*message < 'A' || *message > 'Z') continue;
const unsigned char *letter = large_alphabet[*message - 'A'];
for (int row = 0; row < 7; ++row)
for (int col = 0; col < 5; ++col)
if (letter[row] & (16 >> col))
rectangle(x + col, y + row, 1, 1, color);
}
}
/* Keep all artwork images resident, including both character styles and Tidefin. */
enum { SPRITE_SIZE = 24, SPRITE_CACHE_SIZE = 15 };
struct DecodedSprite {
const char *const *art;
unsigned char width, rows;
unsigned char first[SPRITE_SIZE], end[SPRITE_SIZE];
unsigned char pixels[SPRITE_SIZE * SPRITE_SIZE];
};
static struct DecodedSprite sprite_cache[SPRITE_CACHE_SIZE];
static unsigned sprite_cache_next;
static const struct DecodedSprite *decode_sprite(const char *const *art, int width, int rows)
{
for (int i = 0; i < SPRITE_CACHE_SIZE; ++i)
if (sprite_cache[i].art == art && sprite_cache[i].width == width &&
sprite_cache[i].rows == rows) return &sprite_cache[i];
struct DecodedSprite *decoded = &sprite_cache[sprite_cache_next];
sprite_cache_next = (sprite_cache_next + 1) % SPRITE_CACHE_SIZE;
decoded->art = art;
decoded->width = (unsigned char)width;
decoded->rows = (unsigned char)rows;
for (int row = 0; row < rows; ++row) {
decoded->first[row] = (unsigned char)width;
decoded->end[row] = 0;
for (int col = 0; col < width; ++col) {
unsigned char encoded = 0;
if (art[row][col] != '.') {
char pixel = art[row][col];
int index = pixel <= '9' ? pixel - '0' : pixel - 'A' + 10;
/* Zero means transparent; palette entry zero is still usable. */
encoded = (unsigned char)(index + 1);
if (decoded->first[row] == width) decoded->first[row] = (unsigned char)col;
decoded->end[row] = (unsigned char)(col + 1);
}
decoded->pixels[row * SPRITE_SIZE + col] = encoded;
}
}
#ifdef GAME_TEST
++sprite_decodes;
#endif
return decoded;
}
/* Decode once, then write clipped rows directly into the RAM frame buffer. */
void blit_sprite(int x, int y, const char *const *art, int width, int rows,
const u16 *palette, int scale, int mirrored)
{
if (width <= 0 || width > SPRITE_SIZE || rows <= 0 || rows > SPRITE_SIZE || scale <= 0)
return;
if (x >= WIDTH || y >= HEIGHT || x + width * scale <= 0 || y + rows * scale <= 0) return;
const struct DecodedSprite *decoded = decode_sprite(art, width, rows);
for (int row = 0; row < rows; ++row) {
int first = decoded->first[row], end = decoded->end[row];
if (first >= end) continue;
int top = y + row * scale, bottom = top + scale;
if (bottom <= 0 || top >= HEIGHT) continue;
int left = x + (mirrored ? width - end : first) * scale;
int right = x + (mirrored ? width - first : end) * scale;
if (left < 0) left = 0;
if (right > WIDTH) right = WIDTH;
if (top < 0) top = 0;
if (bottom > HEIGHT) bottom = HEIGHT;
if (left >= right) continue;
mark_dirty(left, top, right - left, bottom - top);
#ifdef GAME_TEST
++sprite_row_marks;
#endif
const unsigned char *source = &decoded->pixels[row * SPRITE_SIZE];
if (scale == 1) {
u16 *destination = &SCREEN[top * WIDTH + left];
int col = mirrored ? width - 1 - (left - x) : left - x;
int advance = mirrored ? -1 : 1;
for (int offset = 0; offset < right - left; ++offset, col += advance) {
unsigned char pixel = source[col];
if (pixel) destination[offset] = palette[pixel - 1];
}
} else {
for (int col = first; col < end; ++col) {
unsigned char pixel = source[col];
if (!pixel) continue;
u16 color = palette[pixel - 1];
int start = x + (mirrored ? width - 1 - col : col) * scale;
int finish = start + scale;
if (start < left) start = left;
if (finish > right) finish = right;
for (int screen_y = top; screen_y < bottom; ++screen_y) {
u16 *destination = &SCREEN[screen_y * WIDTH];
for (int screen_x = start; screen_x < finish; ++screen_x)
destination[screen_x] = color;
}
}
}
}
}
void sprite(int x, int y, const char *const *art, int rows, const u16 *palette,
int mirrored)
{
blit_sprite(x, y, art, 16, rows, palette, 1, mirrored);
}
+30 -6
View File
@@ -2,7 +2,7 @@
#define WILD_CREATURES_H #define WILD_CREATURES_H
/* Original 24x24 pixel art. Dots are transparent; digits select colors. */ /* Original 24x24 pixel art. Dots are transparent; digits select colors. */
static const char *const fernfox_art[] = { TEST_INTERNAL const char *const fernfox_art[] = {
"........................", "........................",
"....1.............1.....", "....1.............1.....",
"...131...........131....", "...131...........131....",
@@ -29,7 +29,7 @@ static const char *const fernfox_art[] = {
".......1...1...1........" ".......1...1...1........"
}; };
static const char *const emberwing_art[] = { TEST_INTERNAL const char *const emberwing_art[] = {
".......11.......11......", ".......11.......11......",
"......1411.....1141.....", "......1411.....1141.....",
"....1121111...111211....", "....1121111...111211....",
@@ -56,7 +56,7 @@ static const char *const emberwing_art[] = {
"........................" "........................"
}; };
static const char *const pebblit_art[] = { TEST_INTERNAL const char *const pebblit_art[] = {
"........................", "........................",
"..............1.........", "..............1.........",
".......1.....161........", ".......1.....161........",
@@ -96,11 +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)
}; };
struct WildArt { const char *const *rows; const u16 *palette; }; TEST_INTERNAL const char *const tidefin_art[] = {
static const struct WildArt wild_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[] = {
{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
+270
View File
@@ -0,0 +1,270 @@
#include "game.h"
static struct { unsigned last, fraction; } movement_clock;
TEST_INTERNAL unsigned diagonal_fraction;
void pause_movement_at(unsigned now)
{
movement_clock.last = now;
movement_clock.fraction = 0;
diagonal_fraction = 0;
}
/* Fixed 60 Hz simulation, independent of how often a frame is drawn. */
int movement_ticks_at(unsigned now)
{
unsigned elapsed = now - movement_clock.last;
movement_clock.last = now;
if (elapsed > MOVEMENT_CLOCK_HZ / 4) elapsed = MOVEMENT_CLOCK_HZ / 4;
unsigned total = movement_clock.fraction + elapsed * MOVEMENT_HZ;
unsigned ticks = total / MOVEMENT_CLOCK_HZ;
movement_clock.fraction = total % MOVEMENT_CLOCK_HZ;
/* Drop excess backlog after a long stall rather than teleporting. */
return ticks > MAX_MOVEMENT_TICKS ? MAX_MOVEMENT_TICKS : (int)ticks;
}
enum Room current_room = OUTSIDE;
int door_open = 0;
enum Facing player_facing = FACING_DOWN;
static short trail_x[FOLLOW_STEPS], trail_y[FOLLOW_STEPS];
TEST_INTERNAL int trail_head;
int follower_x, follower_y, follower_ready;
void reset_follower(int x, int y)
{
for (int i = 0; i < FOLLOW_STEPS; ++i) {
trail_x[i] = (short)x;
trail_y[i] = (short)y;
}
trail_head = 0;
follower_x = x;
follower_y = y;
follower_ready = 1;
}
TEST_INTERNAL void move_follower(int old_x, int old_y, int x, int y)
{
if (!partner_owned || (old_x == x && old_y == y)) return;
if (!follower_ready) reset_follower(old_x, old_y);
trail_x[trail_head] = (short)old_x;
trail_y[trail_head] = (short)old_y;
trail_head = (trail_head + 1) % FOLLOW_STEPS;
follower_x = trail_x[trail_head];
follower_y = trail_y[trail_head];
}
int indoors(void)
{
return current_room == COTTAGE || current_room == MELLON_HOUSE;
}
TEST_INTERNAL enum Facing facing_for_keys(unsigned keys, enum Facing previous)
{
if (keys & RIGHT) return FACING_RIGHT;
if (keys & LEFT) return FACING_LEFT;
if (keys & DOWN) return FACING_DOWN;
if (keys & UP) return FACING_UP;
return previous;
}
int near_door(int x, int y)
{
if (indoors())
return x >= 110 && x <= 130 && y >= 125 && y <= 131;
return (current_room == OUTSIDE || current_room == MELLON_GARDEN) &&
x >= 169 && x <= 185 && y >= 78 && y <= 89;
}
int near_map_exit(int x, int y)
{
if (current_room == OUTSIDE && x >= 216 && x <= 222 && y >= 108 && y <= 117)
return 1;
if (current_room == MELLON_GARDEN && x >= 17 && x <= 24 && y >= 108 && y <= 117)
return 1;
if (x < 102 || x > 114) return 0;
if (current_room == OUTSIDE || current_room == MELLON_GARDEN) return y >= 124 && y <= 131;
if (current_room == ROUTE || current_room == BEACH) return y >= 47 && y <= 54;
return 0;
}
static void reset_arrival(int x, int y)
{
grass_steps = 0;
diagonal_fraction = 0;
reset_follower(x, y);
}
void default_location(struct Location *out)
{
out->room = OUTSIDE; out->x = 108; out->y = 103; out->facing = FACING_DOWN;
}
int location_valid(const struct Location *location)
{
return location && location->room >= OUTSIDE && location->room < ROOM_COUNT &&
location->facing >= FACING_DOWN && location->facing <= FACING_RIGHT &&
location->x >= 0 && location->x < WIDTH && location->y >= 0 && location->y < HEIGHT &&
room_walkable(location->room, location->x, location->y);
}
void enter_location(const struct Location *location, int *x, int *y)
{
current_room = (enum Room)location->room;
*x = location->x; *y = location->y;
player_facing = (enum Facing)location->facing;
door_open = 0;
reset_arrival(*x, *y);
}
/* Walk across a map boundary. Arrivals are away from the return trigger. */
TEST_INTERNAL int use_map_exit(int *x, int *y, unsigned keys)
{
if (!near_map_exit(*x, *y)) return 0;
if (current_room == OUTSIDE && *y == 131 && (keys & DOWN)) {
current_room = ROUTE;
*x = 108;
*y = 60;
player_facing = FACING_DOWN;
} else if (current_room == ROUTE && *y == 47 && (keys & UP)) {
current_room = OUTSIDE;
*x = 108;
*y = 125;
player_facing = FACING_UP;
} else if (current_room == OUTSIDE && *x == 222 && (keys & RIGHT)) {
current_room = MELLON_GARDEN;
*x = 31;
*y = 113;
player_facing = FACING_RIGHT;
} else if (current_room == MELLON_GARDEN && *x == 17 && (keys & LEFT)) {
current_room = OUTSIDE;
*x = 210;
*y = 113;
player_facing = FACING_LEFT;
} else if (current_room == MELLON_GARDEN && *y == 131 && (keys & DOWN)) {
current_room = BEACH;
*x = 108;
*y = 60;
player_facing = FACING_DOWN;
} else if (current_room == BEACH && *y == 47 && (keys & UP)) {
current_room = MELLON_GARDEN;
*x = 108;
*y = 125;
player_facing = FACING_UP;
} else {
return 0;
}
door_open = 0;
reset_arrival(*x, *y);
return 1;
}
int near_route_sign(int x, int y)
{
return current_room == ROUTE && x >= 121 && x <= 151 && y >= 77 && y <= 87;
}
/* Only the doorway can change maps; both arrivals are on safe floor. */
int use_door(int *x, int *y)
{
if (!near_door(*x, *y)) return 0;
if (current_room == OUTSIDE) {
current_room = COTTAGE;
*x = 120;
*y = 119;
} else if (current_room == COTTAGE) {
current_room = OUTSIDE;
*x = 177;
*y = 83;
} else if (current_room == MELLON_GARDEN) {
current_room = MELLON_HOUSE;
*x = 120;
*y = 119;
} else {
current_room = MELLON_GARDEN;
*x = 177;
*y = 83;
}
door_open = 0;
player_facing = FACING_DOWN;
reset_arrival(*x, *y);
return 1;
}
void reset_position(int *x, int *y)
{
*x = indoors() ? 120 : current_room == MELLON_GARDEN ? 31 : 108;
*y = indoors() ? 119 : (current_room == ROUTE || current_room == BEACH) ? 60 :
current_room == MELLON_GARDEN ? 113 : 103;
player_facing = FACING_DOWN;
reset_arrival(*x, *y);
}
TEST_INTERNAL int can_walk(int x, int y)
{
return room_walkable(current_room, x, y);
}
int room_walkable(int room, int x, int y)
{
if (room < OUTSIDE || room >= ROOM_COUNT) return 0;
if (room == BEACH) {
if (x < 17 || x > 222 || y < 47 || y > beach_shoreline(x) - 5) return 0;
if ((x < 42 || x > 194) && y < 72) return 0; /* Palm trunks. */
if (x > 35 && x < 75 && y > 68 && y < 98) return 0; /* Umbrella and towel. */
return 1;
}
if (room == ROUTE) {
if (x < 32 || x > 207 || y < 47 || y > 131) return 0;
if (x > 143 && x < 216 && y < 85) return 0; /* Pond. */
if (x > 116 && x < 154 && y > 53 && y < 79) return 0; /* Sign. */
if (x > 28 && x < 72 && y > 104 && y < 130) return 0; /* Boulder. */
if (x > RANGER_X - 13 && x < RANGER_X + 13 &&
y > RANGER_Y - 9 && y < RANGER_Y + 9) return 0;
return 1;
}
if (room == COTTAGE || room == MELLON_HOUSE) {
if (x < 34 || x > 205 || y < 75 || y > 131) return 0;
if (x > 26 && x < 77 && y < 105) return 0; /* Bed. */
if (x > 141 && x < 196 && y < 109) return 0; /* Table. */
if (x > 187 && y < 85) return 0; /* Plant. */
if (room == MELLON_HOUSE && x > PROF_X - 13 && x < PROF_X + 13 &&
y > PROF_Y - 9 && y < PROF_Y + 9) return 0;
return 1;
}
if (x < 17 || x > 222 || y < 47 || y > 131) return 0;
if (x > 16 && x < 82 && y > 42 && y < 92) return 0;
if (x > 141 && x < 212 && y < 78) return 0;
if (room == OUTSIDE && x > NPC_X - 13 && x < NPC_X + 13 &&
y > NPC_Y - 9 && y < NPC_Y + 9) return 0;
return 1;
}
enum WalkResult walk_movement(unsigned keys, int *x, int *y, int ticks)
{
player_facing = facing_for_keys(keys, player_facing);
int dx = !!(keys & RIGHT) - !!(keys & LEFT);
int dy = !!(keys & DOWN) - !!(keys & UP);
if (!dx && !dy) { diagonal_fraction = 0; return WALK_NORMAL; }
if (ticks > MAX_MOVEMENT_TICKS) ticks = MAX_MOVEMENT_TICKS;
for (int tick = 0; tick < ticks; ++tick) {
int pixels = WALK_SPEED;
if (dx && dy) {
/* 181/256 approximates 1/sqrt(2): no diagonal speed boost. */
diagonal_fraction += WALK_SPEED * 181;
pixels = (int)(diagonal_fraction / 256);
diagonal_fraction %= 256;
} else {
diagonal_fraction = 0;
}
for (int pixel = 0; pixel < pixels; ++pixel) {
int old_x = *x, old_y = *y;
if (can_walk(*x + dx, *y)) *x += dx;
if (can_walk(*x, *y + dy)) *y += dy;
/* Record every substep, preserving Sprout's existing path gap. */
move_follower(old_x, old_y, *x, *y);
if (use_map_exit(x, y, keys)) return WALK_MAP_CHANGED;
if (check_grass_encounter(old_x, old_y, *x, *y)) return WALK_ENCOUNTER;
}
}
return WALK_NORMAL;
}
+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");
}
+223
View File
@@ -0,0 +1,223 @@
#include "test_support.h"
void check_following_and_battle(void)
{
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
static const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
partner_owned = 1;
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); }
assert(follower_x == x && follower_y == y);
render_scene(x, y);
copy_pending_rows(visible);
for (int frame = 0; frame < 100; ++frame) {
int old_x = x, old_y = y;
int old_fx = follower_x, old_fy = follower_y;
int old_head = trail_head;
unsigned keys = directions[(frame / 10) % 4];
player_facing = facing_for_keys(keys, player_facing);
int nx = x + !!(keys & RIGHT) - !!(keys & LEFT);
int ny = y + !!(keys & DOWN) - !!(keys & UP);
if (can_walk(nx, y)) x = nx;
if (can_walk(x, ny)) y = ny;
move_follower(old_x, old_y, x, y);
assert(can_walk(follower_x, follower_y));
if (old_x == x && old_y == y) assert(trail_head == old_head);
restore(old_x - 8, old_y - 20, 17, 24);
restore(old_fx - 8, old_fy - 20, 17, 24);
actors(x, y, 0);
location_status(x, y);
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
memcpy(expected, SCREEN, sizeof(expected));
render_scene(x, y);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
}
}
current_room = OUTSIDE;
reset_follower(108, 103);
for (int step = 0; step < 20; ++step) move_follower(108 + step, 103, 109 + step, 103);
assert(follower_x == 114 && follower_y == 103); /* Exactly 14 steps behind. */
int head = trail_head;
move_follower(128, 103, 128, 103);
assert(trail_head == head);
int x = 108, y = 131;
assert(use_map_exit(&x, &y, DOWN));
assert(follower_x == x && follower_y == y && current_room == ROUTE);
current_room = OUTSIDE;
reset_follower(108, 103);
x = 108;
y = 103;
for (int step = 0; step < 20; ++step) {
move_follower(x, y, x + 1, y);
++x;
}
render_scene(x, y);
write_preview("build/follower-preview.ppm");
partner_owned = 0;
assert(!begin_battle(182, 113));
partner_owned = 1;
current_room = ROUTE;
assert(!begin_battle(182, 113));
current_room = OUTSIDE;
assert(!begin_battle(108, 103));
assert(!near_golden(162, 113));
assert(begin_battle(182, 113));
assert(!begin_battle(182, 113));
assert(battle.phase == BATTLE_TURN && battle.sprout_hp == SPROUT_HP &&
battle.enemy_hp == GOLDEN_HP);
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
draw_battle();
write_preview("build/battle-preview.ppm");
copy_pending_rows(visible);
assert(!battle_input(0));
assert(battle_input(SELECT) && battle.notice == NOTICE_NO_BALLS);
assert(battle.turns == 0);
assert(battle_input(RIGHT));
assert(battle.move == 1 && battle.enemy_hp == GOLDEN_HP);
assert(battle_input(A));
assert(battle.enemy_hp == 11 && battle.sprout_hp == 19 && battle.turns == 1);
draw_battle_hud();
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
memcpy(expected, SCREEN, sizeof(expected));
draw_battle();
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
assert(!battle_input(0)); /* A held key without a new press adds no turn. */
assert(battle.turns == 1);
assert(battle_input(A));
assert(battle.enemy_hp == 4 && battle.sprout_hp == 14);
assert(battle_input(A));
assert(battle.phase == BATTLE_WON && battle.enemy_hp == 0 && battle.sprout_hp == 14);
draw_battle_hud();
assert(pending_pixels() < 16500);
write_preview("build/battle-win-preview.ppm");
assert(!battle_input(RIGHT));
assert(battle_input(A) && battle.phase == BATTLE_OFF);
assert(begin_battle(182, 113));
assert(battle_input(B) && battle.phase == BATTLE_FLED);
assert(battle.sprout_hp == SPROUT_HP && battle.enemy_hp == GOLDEN_HP);
assert(battle_input(B) && battle.phase == BATTLE_OFF);
assert(begin_battle(182, 113));
battle.sprout_hp = 1;
assert(battle_input(A) && battle.phase == BATTLE_LOST);
assert(battle.sprout_hp == 0);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
assert(begin_battle(182, 113));
assert(battle.sprout_hp == SPROUT_HP); /* Free recovery after each battle. */
for (int turn = 0; turn < 5; ++turn) assert(battle_input(A));
assert(battle.phase == BATTLE_WON && battle.sprout_hp == 4 && battle.enemy_hp == 0);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
assert(partner_owned && memcmp(saved, test_save, sizeof(saved)) == 0);
partner_owned = 0;
follower_ready = 0;
player_facing = FACING_DOWN;
}
void check_wild_encounters(void)
{
for (int kind = 0; kind < 4; ++kind) {
int opaque = 0;
for (int row = 0; row < 24; ++row) {
assert(strlen(wild_art[kind].rows[row]) == 24);
for (int col = 0; col < 24; ++col) {
char pixel = wild_art[kind].rows[row][col];
if (pixel == '.') continue;
assert(pixel >= '1' && pixel <= '8');
++opaque;
}
}
assert(opaque > (kind == TIDEFIN - FERNFOX ? 100 : 180)); /* Fish has a slimmer silhouette. */
}
partner_owned = 1;
current_room = OUTSIDE;
assert(!in_tall_grass(48, 70));
assert(!check_grass_encounter(48, 70, 49, 70));
current_room = ROUTE;
assert(in_tall_grass(32, 53) && in_tall_grass(80, 86));
assert(in_tall_grass(151, 112) && in_tall_grass(207, 132));
assert(!in_tall_grass(31, 70) && !in_tall_grass(81, 70));
assert(!in_tall_grass(48, 87) && !in_tall_grass(108, 94));
partner_owned = 0;
for (int i = 0; i < 100; ++i) assert(!check_grass_encounter(48, 70, 49, 70));
assert(grass_steps == 0);
partner_owned = 1;
for (int i = 0; i < 100; ++i) assert(!check_grass_encounter(48, 70, 48, 70));
assert(grass_steps == 0); /* Waiting in the grass never starts a battle. */
for (int i = 0; i < 100; ++i) assert(!check_grass_encounter(108, 94, 109, 94));
assert(grass_steps == 0);
unsigned seen = 0;
for (unsigned trial = 1; trial <= 64; ++trial) {
random_state = trial * 719u;
grass_steps = 0;
int steps;
for (steps = 1; steps <= 64; ++steps) {
int x = 48 + (steps & 1), old_x = 48 + ((steps + 1) & 1);
if (check_grass_encounter(old_x, 70, x, 70)) break;
assert(battle.phase == BATTLE_OFF);
}
assert(steps >= 24 && steps <= 64);
assert(battle.enemy >= FERNFOX && battle.enemy <= PEBBLIT);
assert(battle.phase == BATTLE_TURN && battle.enemy_hp == species[battle.enemy].hp);
assert(grass_steps == 0);
seen |= 1u << battle.enemy;
assert(!check_grass_encounter(48, 70, 49, 70)); /* No nested battle. */
assert(battle_input(B) && battle.phase == BATTLE_FLED);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
for (int i = 0; i < 23; ++i) assert(!check_grass_encounter(48, 70, 49, 70));
assert(!check_grass_encounter(80, 70, 81, 70));
assert(grass_steps == 0); /* Leaving the grass resets the meter. */
}
assert(seen == ((1u << FERNFOX) | (1u << EMBERWING) | (1u << PEBBLIT)));
int right_steps = 0;
while (++right_steps <= 64)
if (check_grass_encounter(180, 121, 181, 121)) break;
assert(right_steps <= 64 && battle.phase == BATTLE_TURN);
assert(battle_input(B) && battle_input(A));
assert(battle.phase == BATTLE_OFF);
unsigned char saved[sizeof(test_save)];
memcpy(saved, test_save, sizeof(saved));
static u16 visible[WIDTH * HEIGHT];
const char *previews[] = {
"build/wild-fernfox-preview.ppm", "build/wild-emberwing-preview.ppm", "build/wild-pebblit-preview.ppm"
};
for (int kind = FERNFOX; kind <= PEBBLIT; ++kind) {
assert(start_battle((enum Species)kind));
assert(battle.enemy == (enum Species)kind);
draw_battle();
write_preview(previews[kind - FERNFOX]);
copy_pending_rows(visible);
assert(battle_input(RIGHT));
int turns = 0;
while (battle.phase == BATTLE_TURN && turns < 10) {
assert(battle_input(A));
++turns;
assert(battle.enemy_hp >= 0 && battle.enemy_hp <= species[kind].hp);
assert(battle.sprout_hp >= 0 && battle.sprout_hp <= SPROUT_HP);
draw_battle_hud();
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
assert(battle.phase == BATTLE_WON && battle.sprout_hp > 0);
assert(battle_input(A) && battle.phase == BATTLE_OFF);
}
assert(!start_battle(SPECIES_COUNT));
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
random_state = 0;
assert(next_random() != 0);
partner_owned = 0;
grass_steps = 0;
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");
}
+30 -1046
View File
File diff suppressed because it is too large. Load diff
+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");
}
+288
View File
@@ -0,0 +1,288 @@
#include "test_support.h"
void check_buffered_walking(void)
{
static u16 visible[WIDTH * HEIGHT], before[WIDTH * HEIGHT];
static const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
reset_dirty_rows();
mark_dirty(-2, -2, 4, 4);
assert(pending_pixels() == 4);
reset_dirty_rows();
mark_dirty(239, 159, 4, 4);
assert(pending_pixels() == 1);
reset_dirty_rows();
mark_dirty(-8, 0, 2, 2);
mark_dirty(240, 160, 4, 4);
assert(pending_pixels() == 0);
for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room;
player_facing = FACING_DOWN;
int x, y;
reset_position(&x, &y);
if (current_room == ROUTE) y = 94;
render_scene(x, y);
assert(pending_pixels() == WIDTH * HEIGHT);
copy_pending_rows(visible);
for (int frame = 0; frame < 80; ++frame) {
memcpy(before, visible, sizeof(before));
unsigned keys = directions[(frame / 10) % 4];
int old_x = x, old_y = y;
player_facing = facing_for_keys(keys, player_facing);
int nx = x + !!(keys & RIGHT) - !!(keys & LEFT);
int ny = y + !!(keys & DOWN) - !!(keys & UP);
if (can_walk(nx, y)) x = nx;
if (can_walk(x, ny)) y = ny;
restore(old_x - 8, old_y - 20, 17, 24);
actors(x, y, (frame >> 3) & 1);
if (near_door(old_x, old_y) != near_door(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)) location_status(x, y);
assert(pending_pixels() < 16500);
/* Neither erasing nor drawing the head may touch the display. */
assert(memcmp(before, visible, sizeof(before)) == 0);
copy_pending_rows(visible);
assert(pending_pixels() == 0);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
status("TALKING TO MILA");
dialogue_box(0);
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H);
actors(x, y, 0);
location_status(x, y);
assert(pending_pixels() < 16500);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
current_room = OUTSIDE;
player_facing = FACING_DOWN;
}
void check_route_travel(void)
{
static u16 visible[WIDTH * HEIGHT];
current_room = OUTSIDE;
assert(reachable(108, 103, 108, 131));
int x = 80, y = 131;
assert(!use_map_exit(&x, &y, DOWN)); /* No exit through the fence. */
x = 108;
assert(!use_map_exit(&x, &y, UP)); /* Must walk out, not back in. */
y = 130;
assert(!use_map_exit(&x, &y, DOWN)); /* Not across the edge yet. */
y = 131;
assert(!use_map_exit(&x, &y, A)); /* No button press needed. */
for (int trip = 0; trip < 5; ++trip) {
x = 108;
y = 131;
assert(use_map_exit(&x, &y, DOWN));
assert(current_room == ROUTE && x == 108 && y == 60);
assert(player_facing == FACING_DOWN && can_walk(x, y));
assert(!use_map_exit(&x, &y, DOWN)); /* Held direction cannot bounce. */
assert(!near_door(177, 83));
assert(!near_npc(NPC_X, NPC_Y));
assert(!use_door(&x, &y));
assert(!can_walk(175, 60)); /* Pond. */
assert(!can_walk(135, 65)); /* Sign. */
assert(!can_walk(49, 114)); /* Boulder. */
assert(!can_walk(20, 94)); /* Forest boundary. */
assert(!can_walk(108, 132)); /* No unfinished south exit. */
assert(can_walk(48, 70)); /* Grass patches are walkable. */
assert(can_walk(183, 121));
assert(near_route_sign(135, 80));
assert(!near_route_sign(108, 54));
assert(reachable(x, y, 135, 80));
assert(reachable(x, y, 183, 121));
assert(reachable(x, y, 108, 47));
render_scene(x, y);
assert(pending_pixels() == WIDTH * HEIGHT);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
assert(SCREEN[NPC_Y * WIDTH + NPC_X] == route_scenery(NPC_X, NPC_Y));
assert(SCREEN[95 * WIDTH + 181] == route_scenery(181, 95));
write_preview("build/route-preview.ppm");
x = 180;
y = 100;
reset_position(&x, &y);
assert(current_room == ROUTE && x == 108 && y == 60);
assert(player_facing == FACING_DOWN);
for (; y > 47; --y) assert(can_walk(x, y));
assert(!use_map_exit(&x, &y, DOWN));
assert(use_map_exit(&x, &y, UP));
assert(current_room == OUTSIDE && x == 108 && y == 125);
assert(player_facing == FACING_UP && can_walk(x, y));
assert(!use_map_exit(&x, &y, UP));
assert(reachable(x, y, 108, 103));
assert(reachable(x, y, 177, 83)); /* House still reachable on return. */
render_scene(x, y);
copy_pending_rows(visible);
assert(memcmp(visible, SCREEN, sizeof(visible)) == 0);
}
current_room = COTTAGE;
x = 108;
y = 131;
assert(!use_map_exit(&x, &y, DOWN)); /* Cottage keeps its own exit. */
current_room = OUTSIDE;
player_facing = FACING_DOWN;
}
void check_mellon_visit(void)
{
static u16 expected[WIDTH * HEIGHT];
current_room = OUTSIDE;
assert(reachable(108, 103, 222, 113));
int x = 222, y = 100;
assert(!use_map_exit(&x, &y, RIGHT)); /* No passage through the fence. */
y = 113;
assert(!use_map_exit(&x, &y, LEFT));
for (int visit = 0; visit < 5; ++visit) {
x = 222;
y = 113;
assert(use_map_exit(&x, &y, RIGHT));
assert(current_room == MELLON_GARDEN && x == 31 && y == 113);
assert(player_facing == FACING_RIGHT && can_walk(x, y));
assert(!use_map_exit(&x, &y, RIGHT));
assert(!near_npc(NPC_X, NPC_Y));
assert(reachable(x, y, 177, 83));
assert(reachable(x, y, 17, 113));
assert(!can_walk(40, 65)); /* Melon beds. */
reset_position(&x, &y);
assert(x == 31 && y == 113);
render_scene(x, y);
write_preview("build/mellon-garden-preview.ppm");
assert(village_scenery(160, 41) != mellon_garden_scenery(160, 41));
x = 177;
y = 83;
assert(near_door(x, y));
assert(use_door(&x, &y));
assert(current_room == MELLON_HOUSE && x == 120 && y == 119);
assert(can_walk(x, y) && !near_door(x, y));
assert(!can_walk(PROF_X, PROF_Y));
assert(near_npc(PROF_X, PROF_Y + 22));
assert(!near_npc(PROF_X, PROF_Y + 23));
assert(reachable(x, y, PROF_X, PROF_Y + 22));
assert(reachable(x, y, 120, 125));
assert(!near_map_exit(222, 113));
reset_position(&x, &y);
assert(x == 120 && y == 119 && current_room == MELLON_HOUSE);
render_scene(x, y);
write_preview("build/mellon-house-preview.ppm");
/* Repainting near either side of the professor must not leave trails. */
const int positions[][2] = {{100, 92}, {126, 92}, {113, 101}, {113, 83}};
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);
dialogue_box(0);
restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H);
actors(nx, ny, 0);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
}
render_scene(PROF_X, PROF_Y + 22);
dialogue_box(0);
write_preview("build/mellon-dialogue-preview.ppm");
for (int page = 0; page < DIALOGUE_PAGES; ++page)
for (int line = 0; line < 2; ++line) {
const char *message = professor_dialogue[page][line];
assert(strlen(message) <= 34);
for (; *message; ++message)
assert(*message == ' ' || (*message >= 'A' && *message <= 'Z'));
}
x = 120;
y = 125;
assert(use_door(&x, &y));
assert(current_room == MELLON_GARDEN && x == 177 && y == 83);
assert(can_walk(x, y) && reachable(x, y, 17, 113));
x = 17;
y = 113;
assert(!use_map_exit(&x, &y, RIGHT));
assert(use_map_exit(&x, &y, LEFT));
assert(current_room == OUTSIDE && x == 210 && y == 113);
assert(player_facing == FACING_LEFT && can_walk(x, y));
assert(!use_map_exit(&x, &y, LEFT));
assert(reachable(x, y, 108, 131)); /* South route still accessible. */
assert(reachable(x, y, 177, 83)); /* Original cottage still accessible. */
}
current_room = OUTSIDE;
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();
}
+134
View File
@@ -0,0 +1,134 @@
#include "test_support.h"
void check_movement_clock_and_speed(void)
{
const int render_rates[] = {15, 20, 30, 60, 120};
for (int following = 0; following < 2; ++following)
for (unsigned rate = 0; rate < sizeof(render_rates) / sizeof(render_rates[0]); ++rate) {
current_room = OUTSIDE;
partner_owned = following;
int x = 40, y = 125, total_ticks = 0;
reset_follower(x, y);
pause_movement_at(0);
for (int frame = 1; frame <= render_rates[rate]; ++frame) {
unsigned now = (unsigned)(frame * MOVEMENT_CLOCK_HZ / render_rates[rate]);
int ticks = movement_ticks_at(now);
total_ticks += ticks;
assert(walk_movement(RIGHT, &x, &y, ticks) == WALK_NORMAL);
}
assert(total_ticks == MOVEMENT_HZ && x == 160 && y == 125);
if (following) assert(follower_x == x - FOLLOW_STEPS && follower_y == y);
}
/* Timer rollover, idle reads, bounded catch-up, and paused menus. */
pause_movement_at(0xffffff00u);
assert(movement_ticks_at(0x00000100u) == 1);
assert(movement_ticks_at(0x00000100u) == 0);
pause_movement_at(0);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == MAX_MOVEMENT_TICKS);
assert(movement_ticks_at(MOVEMENT_CLOCK_HZ) == 0); /* No hidden backlog. */
pause_movement_at(123456u);
assert(movement_ticks_at(123456u) == 0);
pause_movement_at(999999u); /* Time spent in a menu is discarded. */
assert(movement_ticks_at(999999u) == 0);
assert(movement_ticks_at(999999u + MOVEMENT_CLOCK_HZ / 60 + 1) == 1);
partner_owned = 0;
current_room = OUTSIDE;
int x = 108, y = 103;
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 110 && y == 103 && player_facing == FACING_RIGHT);
assert(walk_movement(UP, &x, &y, 0) == WALK_NORMAL);
assert(x == 110 && y == 103 && player_facing == FACING_UP);
x = 132; y = 106;
assert(walk_movement(RIGHT, &x, &y, MAX_MOVEMENT_TICKS) == WALK_NORMAL);
assert(x == 133 && can_walk(x, y)); /* Never tunnel through Mila. */
x = 84; y = 70;
assert(walk_movement(LEFT, &x, &y, 2) == WALK_NORMAL);
assert(x == 82 && can_walk(x, y)); /* Stop at the pond, pixel by pixel. */
x = 177; y = 80;
assert(walk_movement(UP, &x, &y, 2) == WALK_NORMAL);
assert(y == 78 && near_door(x, y)); /* Door remains usable. */
pause_movement_at(0);
x = 160; y = 90;
for (int tick = 0; tick < 20; ++tick)
assert(walk_movement(RIGHT | DOWN, &x, &y, 1) == WALK_NORMAL);
assert(x == 188 && y == 118); /* Approximately 40 pixels, not 56 diagonally. */
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 190 && diagonal_fraction == 0);
assert(walk_movement(RIGHT | LEFT, &x, &y, 5) == WALK_NORMAL);
assert(x == 190 && y == 118);
/* Crossing an exit stops remaining substeps instead of skipping its trigger. */
partner_owned = 1;
x = 108; y = 129;
reset_follower(x, y);
assert(walk_movement(DOWN, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == ROUTE && x == 108 && y == 60);
assert(follower_x == x && follower_y == y);
y = 48;
assert(walk_movement(UP, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == OUTSIDE && x == 108 && y == 125);
x = 221; y = 113;
assert(walk_movement(RIGHT, &x, &y, 5) == WALK_MAP_CHANGED);
assert(current_room == MELLON_GARDEN && x == 31 && y == 113);
x = 18;
assert(walk_movement(LEFT, &x, &y, 1) == WALK_MAP_CHANGED);
assert(current_room == OUTSIDE && x == 210 && y == 113);
current_room = COTTAGE;
x = 120; y = 125;
assert(walk_movement(DOWN, &x, &y, 5) == WALK_NORMAL);
assert(current_room == COTTAGE && y == 131); /* Indoor exits still require A. */
/* Two-pixel ticks still count every traveled substep for grass and following. */
current_room = ROUTE;
x = 40; y = 70;
reset_follower(x, y);
grass_steps = 0;
random_state = 1; /* First roll does not trigger an encounter. */
for (int tick = 0; tick < 12; ++tick)
assert(walk_movement(RIGHT, &x, &y, 1) == WALK_NORMAL);
assert(x == 64 && grass_steps == 24 && follower_x == 50 && follower_y == 70);
grass_steps = 63; /* Next traveled pixel guarantees an encounter. */
assert(walk_movement(RIGHT, &x, &y, 5) == WALK_ENCOUNTER);
assert(x == 65 && y == 70 && battle.phase == BATTLE_TURN);
assert(battle_input(B) && battle_input(A) && battle.phase == BATTLE_OFF);
/* Faster and catch-up frames must erase both old sprites completely. */
static u16 snapshot[WIDTH * HEIGHT];
const unsigned directions[] = {RIGHT, DOWN, LEFT, UP};
const int tick_counts[] = {1, 2, 1, 3, 0};
for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room;
reset_position(&x, &y);
if (current_room == OUTSIDE) { x = 40; y = 125; }
if (current_room == ROUTE) y = 94;
reset_follower(x, y);
render_scene(x, y);
reset_dirty_rows();
int phase = 0;
for (int frame = 0; frame < 40; ++frame) {
int old_x = x, old_y = y, old_fx = follower_x, old_fy = follower_y;
int ticks = tick_counts[frame % 5];
assert(walk_movement(directions[frame / 10], &x, &y, ticks) == WALK_NORMAL);
restore(old_x - 8, old_y - 20, 17, 24);
restore(old_fx - 8, old_fy - 20, 17, 24);
actors(x, y, (phase >> 3) & 1);
location_status(x, y);
assert(pending_pixels() < 16500);
memcpy(snapshot, SCREEN, sizeof(snapshot));
render_scene(x, y);
restore(x - 8, y - 20, 17, 24);
actors(x, y, (phase >> 3) & 1);
assert(memcmp(snapshot, SCREEN, sizeof(snapshot)) == 0);
reset_dirty_rows();
phase += ticks;
}
}
current_room = OUTSIDE;
partner_owned = follower_ready = 0;
grass_steps = 0;
player_facing = FACING_DOWN;
pause_movement_at(0);
puts("Movement: 120 pixels/second at 15/20/30/60/120 render updates, with or without Sprout");
}
+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();
}
+188
View File
@@ -0,0 +1,188 @@
#include "test_support.h"
void check_background_cache(void)
{
static u16 reference[WIDTH * HEIGHT];
background_valid = 0;
scenery_samples = 0;
reset_dirty_rows();
restore(-10, -10, 2, 2);
restore(240, 160, 4, 4);
assert(!background_valid && scenery_samples == 0 && pending_pixels() == 0);
for (int room = OUTSIDE; room <= BEACH; ++room) {
current_room = (enum Room)room;
door_open = 0;
unsigned before = scenery_samples;
restore(0, 0, WIDTH, HEIGHT);
assert(scenery_samples - before == WIDTH * HEIGHT);
assert(background_valid && background_room == current_room);
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x)
reference[y * WIDTH + x] = scenery(x, y);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
/* Reference cost: every 17x24 restore previously evaluated 408 pixels. */
before = scenery_samples;
for (int y = 63; y < 87; ++y)
for (int x = 100; x < 117; ++x) (void)scenery(x, y);
assert(scenery_samples - before == 408);
before = scenery_samples;
for (int frame = 0; frame < 500; ++frame) {
restore(100, 63, 17, 24);
restore(120, 63, 17, 24); /* Same restoration work with Sprout. */
}
assert(scenery_samples == before); /* Zero procedural work while walking. */
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
/* Actors and menu graphics must never leak into the scenery cache. */
int x, y;
reset_position(&x, &y);
actors(x, y, 0);
draw_team();
restore(0, 0, WIDTH, HEIGHT);
assert(scenery_samples == before);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
}
const enum Room houses[] = {OUTSIDE, MELLON_GARDEN};
for (unsigned i = 0; i < sizeof(houses) / sizeof(houses[0]); ++i) {
current_room = houses[i];
door_open = 0;
restore(0, 0, WIDTH, HEIGHT);
for (int open = 1; open >= 0; --open) {
door_open = open;
unsigned before = scenery_samples;
restore(170, 54, 15, 24);
assert(scenery_samples - before == 13 * 18); /* Patch only the door. */
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x)
reference[y * WIDTH + x] = scenery(x, y);
assert(memcmp(SCREEN, reference, sizeof(reference)) == 0);
assert(memcmp(background_cache, reference, sizeof(reference)) == 0);
before = scenery_samples;
restore(170, 54, 15, 24);
assert(scenery_samples == before);
}
}
/* Clip once before copying, without altering pixels outside the request. */
current_room = OUTSIDE;
restore(0, 0, WIDTH, HEIGHT);
const u16 marker = RGB(1, 2, 3);
rectangle(0, 0, WIDTH, HEIGHT, marker);
reset_dirty_rows();
unsigned before = scenery_samples;
restore(-3, -2, 5, 4);
restore(239, 159, 5, 4);
restore(0, 0, 0, 10);
restore(0, 0, 10, 0);
assert(scenery_samples == before && pending_pixels() == 5);
for (int y = 0; y < HEIGHT; ++y)
for (int x = 0; x < WIDTH; ++x) {
u16 expected = ((x < 2 && y < 2) || (x == 239 && y == 159)) ?
background_cache[y * WIDTH + x] : marker;
assert(SCREEN[y * WIDTH + x] == expected);
}
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
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. */
static void reference_sprite(int x, int y, const char *const *art, int width, int rows,
const u16 *palette, int scale, int mirrored)
{
for (int row = 0; row < rows; ++row)
for (int col = 0; col < width; ++col) {
char pixel = art[row][col];
if (pixel == '.') continue;
int index = pixel <= '9' ? pixel - '0' : pixel - 'A' + 10;
rectangle(x + (mirrored ? width - 1 - col : col) * scale, y + row * scale,
scale, scale, palette[index]);
}
}
static void sprite_test_canvas(void)
{
for (int i = 0; i < WIDTH * HEIGHT; ++i) SCREEN[i] = (u16)((i * 3 ^ 0x5a5a) & 0x7fff);
reset_dirty_rows();
}
void check_sprite_rows(void)
{
static u16 expected[WIDTH * HEIGHT], visible[WIDTH * HEIGHT];
static const char *const zero_art[] = {"0A..B1..", "..1.....", "........"};
const struct { const char *const *art; int width, rows; } images[] = {
{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},
{pebblit_art, 24, 24}, {tidefin_art, 24, 24}, {zero_art, 8, 3}
};
const int positions[][2] = {
{70, 60}, {-8, 40}, {232, 40}, {80, -12}, {80, 155},
{-5, -7}, {236, 156}, {-100, -100}, {240, 30}, {50, 160}
};
u16 palette[13], alternate[13];
for (int i = 0; i < 13; ++i) {
palette[i] = RGB(i, i * 2, 31 - i);
alternate[i] = RGB(31 - i, i, i * 2);
}
palette[0] = 0; /* Palette entry zero is opaque black, not transparency. */
for (unsigned image = 0; image < sizeof(images) / sizeof(images[0]); ++image)
for (int scale = 1; scale <= 3; ++scale)
for (int mirror = 0; mirror < 2; ++mirror)
for (unsigned p = 0; p < sizeof(positions) / sizeof(positions[0]); ++p) {
int x = positions[p][0], y = positions[p][1];
const u16 *colors = p & 1 ? alternate : palette;
sprite_test_canvas();
reference_sprite(x, y, images[image].art, images[image].width,
images[image].rows, colors, scale, mirror);
memcpy(expected, SCREEN, sizeof(expected));
sprite_test_canvas();
memcpy(visible, SCREEN, sizeof(visible));
unsigned before_rectangles = rectangle_calls;
unsigned before_rows = sprite_row_marks;
blit_sprite(x, y, images[image].art, images[image].width,
images[image].rows, colors, scale, mirror);
assert(rectangle_calls == before_rectangles);
assert(sprite_row_marks - before_rows <= (unsigned)images[image].rows);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
copy_pending_rows(visible);
assert(memcmp(expected, visible, sizeof(expected)) == 0);
}
/* Palette changes reuse decoded indices, rather than cached colors. */
sprite_test_canvas();
blit_sprite(20, 20, zero_art, 8, 3, palette, 1, 0);
assert(SCREEN[20 * WIDTH + 20] == palette[0]);
unsigned before_decodes = sprite_decodes;
blit_sprite(20, 20, zero_art, 8, 3, alternate, 1, 0);
assert(sprite_decodes == before_decodes);
assert(SCREEN[20 * WIDTH + 20] == alternate[0]);
sprite_test_canvas();
unsigned before_rectangles = rectangle_calls;
reference_sprite(80, 60, person_art, 16, 20, palette, 1, 0);
unsigned old_rectangles = rectangle_calls - before_rectangles;
assert(old_rectangles > 100);
sprite_test_canvas();
before_rectangles = rectangle_calls;
unsigned before_rows = sprite_row_marks;
blit_sprite(80, 60, person_art, 16, 20, palette, 1, 0);
assert(rectangle_calls == before_rectangles);
assert(sprite_row_marks - before_rows == 20);
before_decodes = sprite_decodes;
for (int frame = 0; frame < 1000; ++frame)
blit_sprite(80, 60, person_art, 16, 20, palette, 1, frame & 1);
assert(sprite_decodes == before_decodes && rectangle_calls == before_rectangles);
sprite_test_canvas();
blit_sprite(0, 0, zero_art, 0, 3, palette, 1, 0);
blit_sprite(0, 0, zero_art, 8, 25, palette, 1, 0);
blit_sprite(0, 0, zero_art, 8, 3, palette, 0, 0);
assert(pending_pixels() == 0);
assert(test_pixels[0] == 0xbeef && test_pixels[WIDTH * HEIGHT + 1] == 0xbeef);
printf("Sprite rows: character body uses 0 rectangle calls (was %u), 20 row marks, no repeat decoding\n",
old_rectangles);
}
+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)
+212
View File
@@ -0,0 +1,212 @@
#include "test_support.h"
void check_starter_gift(void)
{
memset(test_save, 0xff, sizeof(test_save));
load_progress();
assert(!partner_owned);
draw_team();
assert(pending_pixels() == WIDTH * HEIGHT);
write_preview("build/team-empty-preview.ppm");
current_room = OUTSIDE;
assert(!begin_conversation(108, 103));
assert(begin_conversation(NPC_X - 13, NPC_Y));
assert(!gift_conversation);
assert(advance_conversation(0) == 1);
assert(!partner_owned && test_save[0] == 0xff);
assert(advance_conversation(1) == -1);
assert(!receive_partner());
current_room = MELLON_HOUSE;
int x = PROF_X, y = PROF_Y + 22;
assert(can_walk(x, y));
assert(!begin_conversation(200, 120));
assert(begin_conversation(x, y));
assert(gift_conversation && !partner_owned);
render_scene(x, y);
dialogue_box(0);
write_preview("build/starter-intro-preview.ppm");
assert(advance_conversation(0) == 1);
assert(partner_owned && gift_conversation);
dialogue_box(1);
write_preview("build/starter-gift-preview.ppm");
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)];
memcpy(saved, test_save, sizeof(saved));
assert(advance_conversation(1) == -1);
assert(!gift_conversation);
assert(begin_conversation(x, y));
assert(!gift_conversation && partner_owned); /* Follow-up, not another gift. */
assert(advance_conversation(0) == 1);
assert(advance_conversation(1) == -1);
assert(memcmp(saved, test_save, sizeof(saved)) == 0);
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. */
x = 120;
y = 125;
assert(use_door(&x, &y));
assert(current_room == MELLON_GARDEN && partner_owned);
x = 17;
y = 113;
assert(use_map_exit(&x, &y, LEFT));
assert(current_room == OUTSIDE && partner_owned);
reset_position(&x, &y);
assert(partner_owned);
partner_owned = 0;
load_progress();
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();
write_preview("build/team-preview.ppm");
static u16 expected[WIDTH * HEIGHT];
render_scene(x, y);
memcpy(expected, SCREEN, sizeof(expected));
draw_team();
render_scene(x, y);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
/* Reject damaged, incomplete, or unknown-version save records. */
for (int byte = 0; byte < SAVE_RECORD_SIZE; ++byte) {
memcpy(test_save, saved, sizeof(saved));
test_save[byte] ^= 1;
load_progress();
assert(!partner_owned);
}
memcpy(test_save, saved, sizeof(saved));
test_save[0] = 0;
load_progress();
assert(!partner_owned);
memcpy(test_save, saved, sizeof(saved));
load_progress();
assert(partner_owned);
for (int page = 0; page < DIALOGUE_PAGES; ++page)
for (int line = 0; line < 2; ++line) {
const char *message = professor_followup[page][line];
assert(strlen(message) <= 34);
for (; *message; ++message)
assert(*message == ' ' || (*message >= 'A' && *message <= 'Z'));
}
partner_owned = 0;
gift_conversation = 0;
current_room = OUTSIDE;
player_facing = FACING_DOWN;
}
void check_ball_gift(void)
{
static u16 expected[WIDTH * HEIGHT];
/* The original Sprout-only save must survive the inventory upgrade. */
const unsigned char legacy[] = {'F', 'Q', 'S', 'P', 1, 1, 0xa5, 0x5a};
memset(test_save, 0xff, sizeof(test_save));
memcpy(test_save, legacy, sizeof(legacy));
load_progress();
assert(partner_owned && !balls_given && ball_count == 0);
assert(test_save[4] == 1); /* Loading alone does not rewrite the save. */
current_room = ROUTE;
assert(!can_walk(RANGER_X, RANGER_Y));
assert(can_walk(RANGER_X + 13, RANGER_Y));
assert(near_npc(99, 97) && !near_npc(108, 60));
assert(reachable(108, 60, 99, 97));
assert(reachable(108, 60, 108, 47));
assert(reachable(108, 60, 183, 121));
assert(!begin_conversation(108, 60));
assert(!receive_balls());
reset_follower(108, 103);
render_scene(99, 97);
write_preview("build/ranger-preview.ppm");
memcpy(expected, SCREEN, sizeof(expected));
assert(begin_conversation(99, 97));
assert(ball_gift_conversation && !gift_conversation);
dialogue_box(0);
assert(ball_count == 0); /* The gift arrives on its receipt page. */
assert(advance_conversation(0) == 1);
assert(ball_count == 5 && balls_given && partner_owned);
dialogue_box(1);
write_preview("build/ball-gift-preview.ppm");
assert(!receive_balls());
assert(advance_conversation(1) == -1);
restore(DIALOGUE_X, DIALOGUE_Y, DIALOGUE_W, DIALOGUE_H);
actors(99, 97, 0);
assert(memcmp(expected, SCREEN, sizeof(expected)) == 0);
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)];
memcpy(saved, test_save, sizeof(saved));
for (int visit = 0; visit < 10; ++visit) {
assert(begin_conversation(99, 97) && !ball_gift_conversation);
assert(advance_conversation(0) == 1);
assert(advance_conversation(1) == -1);
assert(ball_count == 5 && memcmp(saved, test_save, sizeof(saved)) == 0);
}
partner_owned = balls_given = ball_count = 0;
load_progress();
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();
write_preview("build/poke-balls-preview.ppm");
for (int i = SAVE_RECORD_SIZE; i < (int)sizeof(test_save); ++i) assert(test_save[i] == 0xff);
int x = 108, y = 47;
assert(use_map_exit(&x, &y, UP));
assert(ball_count == 5 && balls_given);
/* Getting the balls before Sprout is allowed and must also persist. */
memset(test_save, 0xff, sizeof(test_save));
load_progress();
current_room = ROUTE;
assert(begin_conversation(99, 97));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
load_progress();
assert(!partner_owned && balls_given && ball_count == 5);
current_room = MELLON_HOUSE;
assert(begin_conversation(PROF_X, PROF_Y + 22));
assert(advance_conversation(0) == 1 && advance_conversation(1) == -1);
save_from_menu();
load_progress();
assert(partner_owned && balls_given && ball_count == 5);
memcpy(saved, test_save, sizeof(saved));
for (int byte = 0; byte < SAVE_RECORD_SIZE; ++byte) {
memcpy(test_save, saved, sizeof(saved));
test_save[byte] ^= 1;
load_progress();
assert(!partner_owned && !balls_given && ball_count == 0);
}
memcpy(test_save, saved, sizeof(saved));
test_save[6] = 100;
checksum_fixture(); /* Keep CRC valid; count itself is invalid. */
load_progress();
assert(!partner_owned && !balls_given && ball_count == 0);
for (int page = 0; page < DIALOGUE_PAGES; ++page)
for (int line = 0; line < 2; ++line) {
const char *messages[] = {ranger_dialogue[page][line], ranger_followup[page][line]};
for (int i = 0; i < 2; ++i) {
assert(strlen(messages[i]) <= 34);
for (const char *p = messages[i]; *p; ++p)
assert(*p == ' ' || (*p >= 'A' && *p <= 'Z'));
}
}
current_room = OUTSIDE;
gift_conversation = ball_gift_conversation = 0;
partner_owned = balls_given = ball_count = follower_ready = 0;
}
+140
View File
@@ -0,0 +1,140 @@
#include "test_support.h"
u16 test_pixels[WIDTH * HEIGHT + 2];
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)
{
draw_scene(x, y);
}
int reachable(int start_x, int start_y, int end_x, int end_y)
{
static unsigned char visited[HEIGHT][WIDTH];
static int queue[WIDTH * HEIGHT][2];
static const int directions[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
memset(visited, 0, sizeof(visited));
int head = 0, tail = 1;
queue[0][0] = start_x;
queue[0][1] = start_y;
visited[start_y][start_x] = 1;
while (head < tail) {
int x = queue[head][0], y = queue[head][1];
++head;
if (x == end_x && y == end_y) return 1;
for (int direction = 0; direction < 4; ++direction) {
int nx = x + directions[direction][0], ny = y + directions[direction][1];
if (nx < 0 || nx >= WIDTH || ny < 0 || ny >= HEIGHT) continue;
if (visited[ny][nx] || !can_walk(nx, ny)) continue;
visited[ny][nx] = 1;
queue[tail][0] = nx;
queue[tail][1] = ny;
++tail;
}
}
return 0;
}
void check_art(const char *const *art, int rows, int colors)
{
for (int row = 0; row < rows; ++row) {
assert(strlen(art[row]) == 16);
for (const char *p = art[row]; *p; ++p) {
if (*p == '.') continue;
int index = *p <= '9' ? *p - '0' : *p - 'A' + 10;
assert(index > 0 && index < colors);
}
}
}
void write_preview(const char *path)
{
FILE *file = fopen(path, "wb");
assert(file);
fprintf(file, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
for (int i = 0; i < WIDTH * HEIGHT; ++i) {
u16 color = SCREEN[i];
fputc((color & 31) * 255 / 31, file);
fputc(((color >> 5) & 31) * 255 / 31, file);
fputc(((color >> 10) & 31) * 255 / 31, file);
}
assert(fclose(file) == 0);
}
/* Simulate the same row copies as hardware DMA, into a separate display. */
int pending_pixels(void)
{
int total = 0;
for (int y = 0; y < HEIGHT; ++y) {
assert(dirty_first[y] <= WIDTH && dirty_end[y] <= WIDTH);
if (dirty_first[y] < dirty_end[y]) total += dirty_end[y] - dirty_first[y];
}
return total;
}
void copy_pending_rows(u16 *visible)
{
for (int y = 0; y < HEIGHT; ++y) {
int first = dirty_first[y], end = dirty_end[y];
if (first < end)
memcpy(&visible[y * WIDTH + first], &SCREEN[y * WIDTH + first],
(size_t)(end - first) * sizeof(u16));
}
reset_dirty_rows();
}
+39
View File
@@ -0,0 +1,39 @@
#ifndef TEST_SUPPORT_H
#define TEST_SUPPORT_H
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "../source/game.h"
void render_scene(int x, int y);
int reachable(int start_x, int start_y, int end_x, int end_y);
void check_art(const char *const *art, int rows, int colors);
void write_preview(const char *path);
int pending_pixels(void);
void copy_pending_rows(u16 *visible);
void check_buffered_walking(void);
void check_route_travel(void);
void check_mellon_visit(void);
void check_beach_travel(void);
void check_starter_gift(void);
void check_ball_gift(void);
void check_following_and_battle(void);
void check_wild_encounters(void);
void check_background_cache(void);
void check_sprite_rows(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
+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