xorshift16 PRNG; soft rock fill, hard-rock veins (random walks), 8-11 caverns away from the centre, starting cave near the centre with three dwarf start tiles on floor. Seed from CIA timer, raster and jiffy clock, or -D FIXED_SEED=n (new -Define option on build.ps1/run.ps1). Map view in plain C for now (draw_view, replaced by assembly in M1.3); written with pointer increments to avoid a cc65 -O miscompile. Smoke: row-1 'CHARSET OK' check replaced by a layout_ok byte check, and a 'SEED ' check on row 24 added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>main
| @@ -21,7 +21,8 @@ Steps: | |||||
| Expected: | Expected: | ||||
| - tools/smoke.ps1 prints `[PASS] c64`; screenshot in build/smoke/c64/. | - tools/smoke.ps1 prints `[PASS] c64`; screenshot in build/smoke/c64/. | ||||
| - Row 0: 'STONEHOLD 64'. Row 1: 'CHARSET OK' (or 'MEMORY CLASH' if the program has grown over the charset at $3800). Rows 3-21: a sample patch of map drawn with the custom glyphs: brown soft rock with grey hard-rock flecks, a dark-grey floor cave in the middle with three orange dwarves, two yellow marked rock tiles and a white cursor bracket. Row 23: a glyph legend (ROCK, HARD, FLOOR, DWARF, DIG). Row 24: 'STONE 000 FRAME nnn ANY KEY EXITS' with the frame counter changing every frame. Any key restores the normal character set and returns to BASIC. | |||||
| - (Since M1.2) Row 0: 'STONEHOLD 64'. Row 24 briefly shows 'GENERATING MAP' (about 2 seconds). Then rows 1-23 show the part of a random map around the starting cave, drawn with the custom glyphs: brown soft rock, grey hard-rock veins, dark-grey floor, three orange dwarves on floor in the starting cave. Row 24: 'STONE 000 SEED nnnnn ANY KEY EXITS'. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead. Any key restores the normal character set and returns to BASIC. | |||||
| - (M0 starter, before M1.2) Row 1 showed 'CHARSET OK', rows 3-21 a fixed sample patch, row 23 a glyph legend and row 24 a frame counter. | |||||
| - Autostarts to the Stonehold screen drawn with the custom character set: title on row 0, a map view on rows 1-23, status line (stone counter, seed, RUN/PAUSED) on row 24. Three dwarves move on their own to mine tiles the player marks with the mouse, joystick or keyboard. | - Autostarts to the Stonehold screen drawn with the custom character set: title on row 0, a map view on rows 1-23, status line (stone counter, seed, RUN/PAUSED) on row 24. Three dwarves move on their own to mine tiles the player marks with the mouse, joystick or keyboard. | ||||
| ## M1 - World and view | ## M1 - World and view | ||||
| @@ -33,12 +34,16 @@ Start: `pwsh -File tools/run.ps1 -Target c64` | |||||
| Steps: | Steps: | ||||
| 1. Start the game and note the seed on row 24. | 1. Start the game and note the seed on row 24. | ||||
| 2. Reset VICE and start the game again; note the seed. | |||||
| 2. Close VICE, run the start command again, and note the seed. (Machine > Reset only returns to BASIC; the program is not reloaded.) | |||||
| 3. Fixed seed (R2.3): run `pwsh -File tools/run.ps1 -Target c64 -Define FIXED_SEED=12345`, take a screenshot (or note the visible map), close VICE and run the same command again. | |||||
| 4. Run `pwsh -File tools/build.ps1` afterwards so the normal (random-seed) build is back in build/. | |||||
| Expected: | Expected: | ||||
| - Row 24 shows 'SEED nnnnn' each time | |||||
| - The two seeds differ and the visible maps differ | |||||
| - Three dwarves stand on floor tiles inside a cave | |||||
| - Row 24 shows 'GENERATING MAP' for about 2 seconds, then 'STONE 000 SEED nnnnn ANY KEY EXITS' | |||||
| - Steps 1 and 2: the two seeds differ and the visible maps differ | |||||
| - Step 3: both runs show 'SEED 12345' and exactly the same map | |||||
| - Three orange dwarves stand on dark-grey floor tiles inside an open cave in the middle of the view | |||||
| - The map shows brown soft rock, grey hard-rock veins and (often) part of a dark cavern; no letters or stray symbols inside the map area | |||||
| Verifies: R1, R2 | Verifies: R1, R2 | ||||
| @@ -41,6 +41,7 @@ Check them with `pwsh -File tools/check-prereqs.ps1`. Lookup order for each tool | |||||
| | Check prerequisites | `pwsh -File tools/check-prereqs.ps1` | | | Check prerequisites | `pwsh -File tools/check-prereqs.ps1` | | ||||
| | Build all targets | `pwsh -File tools/build.ps1` | | | Build all targets | `pwsh -File tools/build.ps1` | | ||||
| | Build one target | `pwsh -File tools/build.ps1 -Target <id>` | | | Build one target | `pwsh -File tools/build.ps1 -Target <id>` | | ||||
| | Build with a fixed map seed | `pwsh -File tools/build.ps1 -Define FIXED_SEED=12345` (`tools/run.ps1` takes `-Define` too) | | |||||
| | Run in VICE | `pwsh -File tools/run.ps1 -Target <id>` (add `-Video ntsc` to try NTSC) | | | Run in VICE | `pwsh -File tools/run.ps1 -Target <id>` (add `-Video ntsc` to try NTSC) | | ||||
| | Automated smoke test | `pwsh -File tools/smoke.ps1` (exit 0 pass, 1 fail, 2 missing tool, 3 manual only) | | | Automated smoke test | `pwsh -File tools/smoke.ps1` (exit 0 pass, 1 fail, 2 missing tool, 3 manual only) | | ||||
| @@ -70,6 +71,10 @@ Smoke hooks in the source: | |||||
| - ca65 programs: the `smoke_ready` label. | - ca65 programs: the `smoke_ready` label. | ||||
| - BASIC programs: `POKE 679,1` (BASIC V2) or `POKE 3071,1` (BASIC 7.0) after the first screen is drawn. | - BASIC programs: `POKE 679,1` (BASIC V2) or `POKE 3071,1` (BASIC 7.0) after the first screen is drawn. | ||||
| ## Known compiler issue | |||||
| cl65 V2.19 Git cc3c40c with `-O` miscompiled an indexed table lookup in a loop (`scr[col] = tile_glyph[t]`, src/view.c, found in M1.2). It used the low byte of the destination address as the table index. If a C lookup table gives wrong results, look at the generated assembly (`cc65 -t c64 -O -I src -o out.s src/<file>.c`) before you suspect the logic. | |||||
| ## Assumptions still to verify | ## Assumptions still to verify | ||||
| - [ ] A1: the pointer follows the grabbed host mouse and the left button is detected while the joystick in port 2 and the keyboard still work (before M2 is complete). | - [ ] A1: the pointer follows the grabbed host mouse and the left button is detected while the joystick in port 2 and the keyboard still work (before M2 is complete). | ||||
| @@ -10,7 +10,7 @@ manual = `pending`, `confirmed by user (<date>)`, `problem reported (<date>): <n | |||||
| |---|---|---| | |---|---|---| | ||||
| | M0.1 Run tools/check-prereqs.ps1, then tools/smoke.ps1 for every target. | passed (2026-11-26, `pwsh -File tools/check-prereqs.ps1` exit 0; `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64, 4/4 checks). Run on the working tree including the uncommitted src/charset.c/.h edit (glyphs at codes 128+), see Notes. | confirmed by user (2026-11-26) | | | M0.1 Run tools/check-prereqs.ps1, then tools/smoke.ps1 for every target. | passed (2026-11-26, `pwsh -File tools/check-prereqs.ps1` exit 0; `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64, 4/4 checks). Run on the working tree including the uncommitted src/charset.c/.h edit (glyphs at codes 128+), see Notes. | confirmed by user (2026-11-26) | | ||||
| | M1.1 Settle the memory map (A3): where the 16 KB map, the charset and the program live. Add a check that fails the build or shows an error if they overlap. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0, build/stonehold-64.map shows program $0801-$1702, CHARSET $3800-$3FFF, MAP $4000-$7FFF, no overlap; deliberate overflow links (1 extra byte in MAP, 10 KB extra BSS) failed with ld65 'overflows memory area', cl65 exit 1; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64 4/4. | pending | | | M1.1 Settle the memory map (A3): where the 16 KB map, the charset and the program live. Add a check that fails the build or shows an error if they overlap. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0, build/stonehold-64.map shows program $0801-$1702, CHARSET $3800-$3FFF, MAP $4000-$7FFF, no overlap; deliberate overflow links (1 extra byte in MAP, 10 KB extra BSS) failed with ld65 'overflows memory area', cl65 exit 1; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64 4/4. | pending | | ||||
| | M1.2 Map generator in C: seeded PRNG, soft rock, hard-rock veins, caverns, starting cave near the centre. Show the seed on row 24. | not run | pending | | |||||
| | M1.2 Map generator in C: seeded PRNG, soft rock, hard-rock veins, caverns, starting cave near the centre. Show the seed on row 24. | passed (2026-11-26): `pwsh -File tools/build.ps1` exit 0; `pwsh -File tools/smoke.ps1` exit 0, [PASS] c64 5/5 (title, layout_ok=01, STONE 000, 'SEED ' on row 24, KERNAL vector). Extra VICE runs with memory dumps: two runs of the normal build gave seeds 21910 and 30091 with different maps (R2.2); two runs of a FIXED_SEED=12345 build gave byte-identical 16 KB maps (R2.3); dwarves on floor tiles in the starting cave (R2.4). Generation time 133 jiffies (about 2.2 s), measured by the program. | pending | | |||||
| | M1.3 Assembly view redraw (draw_view(x, y), C prototype) copying a 40x23 window of the map to screen and colour RAM via a tile-to-glyph/colour table. | not run | pending | | | M1.3 Assembly view redraw (draw_view(x, y), C prototype) copying a 40x23 window of the map to screen and colour RAM via a tile-to-glyph/colour table. | not run | pending | | ||||
| | M1.4 Keyboard cursor (cursor keys and W/A/S/D) with edge scrolling and map-border clamping. Status line: 'STONE 000 SEED nnnnn RUN'. | not run | pending | | | M1.4 Keyboard cursor (cursor keys and W/A/S/D) with edge scrolling and map-border clamping. Status line: 'STONE 000 SEED nnnnn RUN'. | not run | pending | | ||||
| | M2.1 Mark/unmark with D on rock tiles only; marked-rock glyph. Joystick port 2 cursor movement with key repeat and fire to mark. | not run | pending | | | M2.1 Mark/unmark with D on rock tiles only; marked-rock glyph. Joystick port 2 cursor movement with key repeat and fire to mark. | not run | pending | | ||||
| @@ -23,4 +23,5 @@ manual = `pending`, `confirmed by user (<date>)`, `problem reported (<date>): <n | |||||
| ## Notes | ## Notes | ||||
| - 2026-11-26, open question (spec vs code): cc65 conio writes capital letters as screen codes 65-90. The spec's Visuals decision (codes 0-63 from the uppercase ROM, game glyphs at 64+) would put glyphs on the letters A-E and blank F-Z. The uncommitted working-tree edit to src/charset.c/.h copies ROM codes 0-127 from the lower/upper-case set and puts the game glyphs at 128-133. The M0 smoke run and screenshot used that edit. The user approved the change on 2026-11-26; docs/spec.md (Visuals), project.json and the src/main.c header now describe codes 0-127 = ROM lower/upper-case set, 128+ = game glyphs. Build and smoke re-run after the doc update (see M0.1). | - 2026-11-26, open question (spec vs code): cc65 conio writes capital letters as screen codes 65-90. The spec's Visuals decision (codes 0-63 from the uppercase ROM, game glyphs at 64+) would put glyphs on the letters A-E and blank F-Z. The uncommitted working-tree edit to src/charset.c/.h copies ROM codes 0-127 from the lower/upper-case set and puts the game glyphs at 128-133. The M0 smoke run and screenshot used that edit. The user approved the change on 2026-11-26; docs/spec.md (Visuals), project.json and the src/main.c header now describe codes 0-127 = ROM lower/upper-case set, 128+ = game glyphs. Build and smoke re-run after the doc update (see M0.1). | ||||
| - 2026-11-26, M1.2: cc65 V2.19 (Git cc3c40c) with -O miscompiled `scr[col] = tile_glyph[t]` in src/view.c: it indexed the table with the low byte of the screen address instead of t. Worked around with pointer increments (checked in the generated assembly). M1.3 replaces this loop with assembly. Inspect the generated code (`cc65 -O -t c64 -I src src/file.c`) when a C table lookup misbehaves. | |||||
| - Generated 2026-11-26. Starter verification at generation time is reported by the Foundry; re-run `pwsh -File tools/smoke.ps1` on your machine and record the result in row M0.1. | - Generated 2026-11-26. Starter verification at generation time is reported by the Foundry; re-run `pwsh -File tools/smoke.ps1` on your machine and record the result in row M0.1. | ||||
| @@ -331,7 +331,7 @@ | |||||
| }, | }, | ||||
| "starter": { | "starter": { | ||||
| "screenTitle": "STONEHOLD 64", | "screenTitle": "STONEHOLD 64", | ||||
| "expectedScreen": "Row 0: 'STONEHOLD 64'. Row 1: 'CHARSET OK' (or 'MEMORY CLASH' if the program has grown over the charset at $3800). Rows 3-21: a sample patch of map drawn with the custom glyphs: brown soft rock with grey hard-rock flecks, a dark-grey floor cave in the middle with three orange dwarves, two yellow marked rock tiles and a white cursor bracket. Row 23: a glyph legend (ROCK, HARD, FLOOR, DWARF, DIG). Row 24: 'STONE 000 FRAME nnn ANY KEY EXITS' with the frame counter changing every frame. Any key restores the normal character set and returns to BASIC.", | |||||
| "expectedScreen": "Row 0: 'STONEHOLD 64'. Rows 1-23: the part of a randomly generated 128x128 map around the starting cave, drawn with the custom glyphs: brown soft rock, grey hard-rock veins, dark-grey floor in caverns and in the starting cave, three orange dwarves standing on floor in the starting cave. Row 24: 'STONE 000 SEED nnnnn ANY KEY EXITS'. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead of the map. Any key restores the normal character set and returns to BASIC.", | |||||
| "smokeChecks": [ | "smokeChecks": [ | ||||
| { | { | ||||
| "row": 0, | "row": 0, | ||||
| @@ -339,17 +339,17 @@ | |||||
| "text": "STONEHOLD 64", | "text": "STONEHOLD 64", | ||||
| "description": "Title on row 0" | "description": "Title on row 0" | ||||
| }, | }, | ||||
| { | |||||
| "row": 1, | |||||
| "col": 0, | |||||
| "text": "CHARSET OK", | |||||
| "description": "Custom charset installed without a memory clash" | |||||
| }, | |||||
| { | { | ||||
| "row": 24, | "row": 24, | ||||
| "col": 0, | "col": 0, | ||||
| "text": "STONE 000", | "text": "STONE 000", | ||||
| "description": "Stone counter on the status line" | "description": "Stone counter on the status line" | ||||
| }, | |||||
| { | |||||
| "row": 24, | |||||
| "col": 11, | |||||
| "text": "SEED ", | |||||
| "description": "Seed on the status line" | |||||
| } | } | ||||
| ], | ], | ||||
| "filesDir": "stonehold-64-files", | "filesDir": "stonehold-64-files", | ||||
| @@ -764,6 +764,18 @@ | |||||
| "symbol": null, | "symbol": null, | ||||
| "address": "FFFC", | "address": "FFFC", | ||||
| "length": 2 | "length": 2 | ||||
| }, | |||||
| { | |||||
| "name": "layout", | |||||
| "symbol": "_layout_ok", | |||||
| "address": null, | |||||
| "length": 1 | |||||
| }, | |||||
| { | |||||
| "name": "gentime", | |||||
| "symbol": "_gen_jiffies", | |||||
| "address": null, | |||||
| "length": 2 | |||||
| } | } | ||||
| ], | ], | ||||
| "checks": [ | "checks": [ | ||||
| @@ -776,12 +788,12 @@ | |||||
| "description": "Title on row 0" | "description": "Title on row 0" | ||||
| }, | }, | ||||
| { | { | ||||
| "dump": "screen", | |||||
| "offset": 40, | |||||
| "text": "CHARSET OK", | |||||
| "encoding": "screen-mixed", | |||||
| "bytes": null, | |||||
| "description": "Custom charset installed without a memory clash" | |||||
| "dump": "layout", | |||||
| "offset": 0, | |||||
| "text": null, | |||||
| "encoding": null, | |||||
| "bytes": "01", | |||||
| "description": "Memory map as expected (layout_ok = 1; 'MEMORY CLASH' otherwise)" | |||||
| }, | }, | ||||
| { | { | ||||
| "dump": "screen", | "dump": "screen", | ||||
| @@ -791,6 +803,14 @@ | |||||
| "bytes": null, | "bytes": null, | ||||
| "description": "Stone counter on the status line" | "description": "Stone counter on the status line" | ||||
| }, | }, | ||||
| { | |||||
| "dump": "screen", | |||||
| "offset": 971, | |||||
| "text": "SEED ", | |||||
| "encoding": "screen-mixed", | |||||
| "bytes": null, | |||||
| "description": "Seed on the status line" | |||||
| }, | |||||
| { | { | ||||
| "dump": "vector", | "dump": "vector", | ||||
| "offset": 0, | "offset": 0, | ||||
| @@ -800,7 +820,7 @@ | |||||
| "description": "C64 KERNAL reset vector at $FFFC (machine is in C64 mode)" | "description": "C64 KERNAL reset vector at $FFFC (machine is in C64 mode)" | ||||
| } | } | ||||
| ], | ], | ||||
| "expectedScreen": "Row 0: 'STONEHOLD 64'. Row 1: 'CHARSET OK' (or 'MEMORY CLASH' if the program has grown over the charset at $3800). Rows 3-21: a sample patch of map drawn with the custom glyphs: brown soft rock with grey hard-rock flecks, a dark-grey floor cave in the middle with three orange dwarves, two yellow marked rock tiles and a white cursor bracket. Row 23: a glyph legend (ROCK, HARD, FLOOR, DWARF, DIG). Row 24: 'STONE 000 FRAME nnn ANY KEY EXITS' with the frame counter changing every frame. Any key restores the normal character set and returns to BASIC." | |||||
| "expectedScreen": "Row 0: 'STONEHOLD 64'. Rows 1-23: the part of a randomly generated 128x128 map around the starting cave, drawn with the custom glyphs: brown soft rock, grey hard-rock veins, dark-grey floor in caverns and in the starting cave, three orange dwarves standing on floor in the starting cave. Row 24: 'STONE 000 SEED nnnnn ANY KEY EXITS'. If the memory map is wrong, row 1 shows 'MEMORY CLASH' instead of the map. Any key restores the normal character set and returns to BASIC." | |||||
| }, | }, | ||||
| "recipeStatus": "verified", | "recipeStatus": "verified", | ||||
| "validationPlan": [] | "validationPlan": [] | ||||
| @@ -1,8 +1,7 @@ | |||||
| /* Stonehold 64 - starter (milestone M0). | |||||
| /* Stonehold 64. | |||||
| ** | ** | ||||
| ** Installs the game's own character set at $3800 (VIC bank 0, screen stays at $0400), | ** Installs the game's own character set at $3800 (VIC bank 0, screen stays at $0400), | ||||
| ** draws a sample patch of map with the custom glyphs and runs the frame-locked loop | |||||
| ** (wait_frame() from frame.s) with a visible frame counter. | |||||
| ** generates a 128x128 map from a seed and shows the part around the starting cave. | |||||
| ** | ** | ||||
| ** Character set layout: | ** Character set layout: | ||||
| ** codes 0-127 copied from the lower/upper-case ROM set (conio puts capitals at 65-90) | ** codes 0-127 copied from the lower/upper-case ROM set (conio puts capitals at 65-90) | ||||
| @@ -10,79 +9,74 @@ | |||||
| ** | ** | ||||
| ** Screen layout (40x25 text, VIC-II): | ** Screen layout (40x25 text, VIC-II): | ||||
| ** row 0 title | ** row 0 title | ||||
| ** row 1 charset / memory check | |||||
| ** rows 3-21 sample map patch | |||||
| ** row 23 glyph legend | |||||
| ** rows 1-23 map view (view.h) | |||||
| ** row 24 status line | ** row 24 status line | ||||
| ** | |||||
| ** Build with -D FIXED_SEED=n (tools/build.ps1 -Define FIXED_SEED=n) to always generate seed n. | |||||
| */ | */ | ||||
| #include <conio.h> | #include <conio.h> | ||||
| #include <string.h> | |||||
| #include <c64.h> | #include <c64.h> | ||||
| #include "smoke.h" | #include "smoke.h" | ||||
| #include "frame.h" | #include "frame.h" | ||||
| #include "charset.h" | #include "charset.h" | ||||
| #include "map.h" | #include "map.h" | ||||
| #include "view.h" | |||||
| #define SCREEN ((unsigned char *)0x0400) | #define SCREEN ((unsigned char *)0x0400) | ||||
| #define COLRAM ((unsigned char *)0xD800) | #define COLRAM ((unsigned char *)0xD800) | ||||
| #define JIFFY_LO (*(volatile unsigned char *)0xA2) | |||||
| #define JIFFY_MID (*(volatile unsigned char *)0xA1) | |||||
| /* Linker symbols. cfg/stonehold-64.cfg already stops the build if an area overflows; | /* Linker symbols. cfg/stonehold-64.cfg already stops the build if an area overflows; | ||||
| ** this run-time check also catches a config that disagrees with charset.h / map.h. */ | ** this run-time check also catches a config that disagrees with charset.h / map.h. */ | ||||
| extern unsigned char _BSS_RUN__[]; | extern unsigned char _BSS_RUN__[]; | ||||
| extern unsigned char _BSS_SIZE__[]; | extern unsigned char _BSS_SIZE__[]; | ||||
| static void put_tile(unsigned char x, unsigned char y, unsigned char code, unsigned char color) | |||||
| /* Read by tools/smoke.ps1: 1 when the memory map is as expected. */ | |||||
| unsigned char layout_ok; | |||||
| /* Read by tools/smoke.ps1 for information: map generation time in jiffies (1/60 s). */ | |||||
| unsigned gen_jiffies; | |||||
| static unsigned jiffies(void) | |||||
| { | { | ||||
| unsigned off = (unsigned)y * 40u + x; | |||||
| SCREEN[off] = code; | |||||
| COLRAM[off] = color; | |||||
| unsigned char lo, mid; | |||||
| do { | |||||
| mid = JIFFY_MID; | |||||
| lo = JIFFY_LO; | |||||
| } while (mid != JIFFY_MID); | |||||
| return ((unsigned)mid << 8) | lo; | |||||
| } | } | ||||
| static void draw_sample_map(void) | |||||
| static void draw_dwarves(unsigned char vx, unsigned char vy) | |||||
| { | { | ||||
| unsigned char x, y; | |||||
| for (y = 3; y <= 21; ++y) { | |||||
| for (x = 0; x < 40; ++x) { | |||||
| if ((unsigned char)((x * 7u + y * 13u) % 11u) == 0) { | |||||
| put_tile(x, y, GLYPH_ROCK_HARD, COLOR_GRAY2); | |||||
| } else { | |||||
| put_tile(x, y, GLYPH_ROCK_SOFT, COLOR_BROWN); | |||||
| } | |||||
| unsigned char i, sx, sy; | |||||
| unsigned off; | |||||
| for (i = 0; i < DWARF_COUNT; ++i) { | |||||
| sx = (unsigned char)(map_dwarf_x[i] - vx); | |||||
| sy = (unsigned char)(map_dwarf_y[i] - vy); | |||||
| if (sx < VIEW_W && sy < VIEW_H) { | |||||
| off = (unsigned)(sy + VIEW_TOP) * 40u + sx; | |||||
| SCREEN[off] = GLYPH_DWARF; | |||||
| COLRAM[off] = COLOR_ORANGE; | |||||
| } | } | ||||
| } | } | ||||
| /* Starting cave. */ | |||||
| for (y = 9; y <= 15; ++y) { | |||||
| for (x = 14; x <= 25; ++x) { | |||||
| put_tile(x, y, GLYPH_FLOOR, COLOR_GRAY1); | |||||
| } | |||||
| } | |||||
| put_tile(17, 12, GLYPH_DWARF, COLOR_ORANGE); | |||||
| put_tile(19, 11, GLYPH_DWARF, COLOR_ORANGE); | |||||
| put_tile(21, 13, GLYPH_DWARF, COLOR_ORANGE); | |||||
| put_tile(26, 12, GLYPH_MARKED, COLOR_YELLOW); | |||||
| put_tile(26, 13, GLYPH_MARKED, COLOR_YELLOW); | |||||
| put_tile(24, 14, GLYPH_CURSOR, COLOR_WHITE); | |||||
| } | } | ||||
| static void draw_legend(void) | |||||
| /* Top-left of a view centred on (x, y), kept inside the map. */ | |||||
| static unsigned char view_origin(unsigned char centre, unsigned char size, unsigned char limit) | |||||
| { | { | ||||
| textcolor(COLOR_GRAY3); | |||||
| put_tile(0, 23, GLYPH_ROCK_SOFT, COLOR_BROWN); | |||||
| cputsxy(2, 23, "ROCK"); | |||||
| put_tile(8, 23, GLYPH_ROCK_HARD, COLOR_GRAY2); | |||||
| cputsxy(10, 23, "HARD"); | |||||
| put_tile(16, 23, GLYPH_FLOOR, COLOR_GRAY1); | |||||
| cputsxy(18, 23, "FLOOR"); | |||||
| put_tile(25, 23, GLYPH_DWARF, COLOR_ORANGE); | |||||
| cputsxy(27, 23, "DWARF"); | |||||
| put_tile(34, 23, GLYPH_MARKED, COLOR_YELLOW); | |||||
| cputsxy(36, 23, "DIG"); | |||||
| unsigned char half = (unsigned char)(size / 2); | |||||
| if (centre < half) { | |||||
| return 0; | |||||
| } | |||||
| centre = (unsigned char)(centre - half); | |||||
| return centre > (unsigned char)(limit - size) ? (unsigned char)(limit - size) : centre; | |||||
| } | } | ||||
| int main(void) | int main(void) | ||||
| { | { | ||||
| unsigned char frames = 0; | |||||
| unsigned char charset_ok; | |||||
| unsigned seed, t0; | |||||
| unsigned char vx, vy; | |||||
| bordercolor(COLOR_BLACK); | bordercolor(COLOR_BLACK); | ||||
| bgcolor(COLOR_BLACK); | bgcolor(COLOR_BLACK); | ||||
| @@ -90,31 +84,49 @@ int main(void) | |||||
| /* Memory map (assumption A3, docs/memory-map.md): program below the charset, | /* Memory map (assumption A3, docs/memory-map.md): program below the charset, | ||||
| ** charset and map exactly where the VIC-II setup and the C code expect them. */ | ** charset and map exactly where the VIC-II setup and the C code expect them. */ | ||||
| charset_ok = (unsigned)_BSS_RUN__ + (unsigned)_BSS_SIZE__ <= CHARSET_ADDR | |||||
| && (unsigned)charset == CHARSET_ADDR | |||||
| && (unsigned)map == MAP_ADDR; | |||||
| layout_ok = (unsigned)_BSS_RUN__ + (unsigned)_BSS_SIZE__ <= CHARSET_ADDR | |||||
| && (unsigned)charset == CHARSET_ADDR | |||||
| && (unsigned)map == MAP_ADDR; | |||||
| textcolor(COLOR_WHITE); | textcolor(COLOR_WHITE); | ||||
| cputsxy(0, 0, "STONEHOLD 64"); | cputsxy(0, 0, "STONEHOLD 64"); | ||||
| if (charset_ok) { | |||||
| charset_install(); | |||||
| cputsxy(0, 1, "CHARSET OK"); | |||||
| draw_sample_map(); | |||||
| draw_legend(); | |||||
| } else { | |||||
| if (!layout_ok) { | |||||
| textcolor(COLOR_LIGHTRED); | textcolor(COLOR_LIGHTRED); | ||||
| cputsxy(0, 1, "MEMORY CLASH"); | cputsxy(0, 1, "MEMORY CLASH"); | ||||
| textcolor(COLOR_WHITE); | |||||
| cputsxy(0, 24, "STONE 000 ANY KEY EXITS"); | |||||
| smoke_capture(); | |||||
| smoke_ready(); | |||||
| cgetc(); | |||||
| return 1; | |||||
| } | } | ||||
| textcolor(COLOR_WHITE); | |||||
| cputsxy(0, 24, "STONE 000 FRAME ANY KEY EXITS"); | |||||
| charset_install(); | |||||
| view_init(); | |||||
| #ifdef FIXED_SEED | |||||
| seed = FIXED_SEED; | |||||
| #else | |||||
| seed = map_random_seed(); | |||||
| #endif | |||||
| cputsxy(0, 24, "GENERATING MAP"); | |||||
| t0 = jiffies(); | |||||
| map_generate(seed); | |||||
| gen_jiffies = jiffies() - t0; | |||||
| vx = view_origin(map_start_x, VIEW_W, MAP_W); | |||||
| vy = view_origin(map_start_y, VIEW_H, MAP_H); | |||||
| draw_view(vx, vy); | |||||
| draw_dwarves(vx, vy); | |||||
| gotoxy(0, 24); | |||||
| cprintf("STONE 000 SEED %05u ANY KEY EXITS", map_seed); | |||||
| smoke_capture(); | smoke_capture(); | ||||
| smoke_ready(); | smoke_ready(); | ||||
| while (!kbhit()) { | while (!kbhit()) { | ||||
| wait_frame(); | wait_frame(); | ||||
| gotoxy(18, 24); | |||||
| cprintf("%3u", (unsigned)frames++); | |||||
| } | } | ||||
| cgetc(); | cgetc(); | ||||
| charset_remove(); | charset_remove(); | ||||
| @@ -1,6 +1,162 @@ | |||||
| /* World map storage. Generation comes in M1.2. */ | |||||
| /* World map storage and the seeded map generator (R2). */ | |||||
| #include <string.h> | |||||
| #include <c64.h> | |||||
| #include "map.h" | #include "map.h" | ||||
| /* Generation runs once per game; static locals make cc65 code much faster here. */ | |||||
| #pragma static-locals (on) | |||||
| #pragma bss-name (push, "MAP") | #pragma bss-name (push, "MAP") | ||||
| unsigned char map[MAP_W * MAP_H]; | unsigned char map[MAP_W * MAP_H]; | ||||
| #pragma bss-name (pop) | #pragma bss-name (pop) | ||||
| unsigned map_seed; | |||||
| unsigned char map_start_x, map_start_y; | |||||
| unsigned char map_dwarf_x[DWARF_COUNT]; | |||||
| unsigned char map_dwarf_y[DWARF_COUNT]; | |||||
| /* Generation tuning. */ | |||||
| #define VEIN_COUNT 56u | |||||
| #define CAVERN_MIN 8u | |||||
| #define CAVERN_TRIES 40u | |||||
| #define START_KEEPOUT_X 24u /* caverns stay this far from the map centre ... */ | |||||
| #define START_KEEPOUT_Y 18u /* ... in x or in y, so the starting cave stays separate */ | |||||
| /* 16-bit xorshift (7, 9, 8): full period 65535, never returns 0 from a non-zero state. */ | |||||
| static unsigned rng; | |||||
| static unsigned rnd(void) | |||||
| { | |||||
| rng ^= rng << 7; | |||||
| rng ^= rng >> 9; | |||||
| rng ^= rng << 8; | |||||
| return rng; | |||||
| } | |||||
| /* Eight directions, clockwise from north. */ | |||||
| static const signed char dir_dx[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }; | |||||
| static const signed char dir_dy[8] = { -1, -1, 0, 1, 1, 1, 0, -1 }; | |||||
| unsigned map_random_seed(void) | |||||
| { | |||||
| unsigned s; | |||||
| s = (unsigned)CIA1.ta_lo | ((unsigned)CIA1.ta_hi << 8); | |||||
| s ^= (unsigned)VIC.rasterline << 5; | |||||
| s ^= (unsigned)(*(unsigned char *)0xA2) << 8; /* jiffy clock, low byte */ | |||||
| s ^= *(unsigned char *)0xA1; /* jiffy clock, middle byte */ | |||||
| return s ? s : 1u; | |||||
| } | |||||
| /* Hard-rock veins: wandering lines, sometimes two tiles wide. Rows/columns 0 and 127 stay soft rock. */ | |||||
| static void make_veins(void) | |||||
| { | |||||
| unsigned char v, len, x, y, dir, thick; | |||||
| unsigned r; | |||||
| for (v = 0; v < VEIN_COUNT; ++v) { | |||||
| r = rnd(); | |||||
| x = (unsigned char)(r & 0x7F); | |||||
| y = (unsigned char)((r >> 7) & 0x7F); | |||||
| dir = (unsigned char)((r >> 14) | ((r & 0x80) ? 4 : 0)); | |||||
| thick = (unsigned char)(rnd() & 1); | |||||
| len = (unsigned char)(24u + (rnd() & 63u)); | |||||
| while (len--) { | |||||
| if (x == 0 || x >= 127 || y == 0 || y >= 127) { | |||||
| break; | |||||
| } | |||||
| *MAP_PTR(x, y) = TILE_HARD; | |||||
| if (thick && x < 126) { | |||||
| *MAP_PTR(x + 1, y) = TILE_HARD; | |||||
| } | |||||
| r = rnd(); | |||||
| if ((r & 3) == 0) { | |||||
| dir = (unsigned char)((dir + ((r & 4) ? 1 : 7)) & 7); | |||||
| } | |||||
| x = (unsigned char)(x + dir_dx[dir]); | |||||
| y = (unsigned char)(y + dir_dy[dir]); | |||||
| } | |||||
| } | |||||
| } | |||||
| /* Carve a 2x2 block of floor with its top-left at (x, y). Caller keeps x, y in 2..124. */ | |||||
| static void carve2x2(unsigned char x, unsigned char y) | |||||
| { | |||||
| unsigned char *p = MAP_PTR(x, y); | |||||
| p[0] = TILE_FLOOR; | |||||
| p[1] = TILE_FLOOR; | |||||
| p[MAP_W] = TILE_FLOOR; | |||||
| p[MAP_W + 1] = TILE_FLOOR; | |||||
| } | |||||
| /* Caverns: random walks that carve 2x2 floor, kept away from the map centre. */ | |||||
| static void make_caverns(void) | |||||
| { | |||||
| unsigned char made = 0, tries, count, x, y, dir; | |||||
| unsigned steps; | |||||
| unsigned r; | |||||
| count = (unsigned char)(CAVERN_MIN + (rnd() & 3u)); | |||||
| for (tries = 0; tries < CAVERN_TRIES && made < count; ++tries) { | |||||
| r = rnd(); | |||||
| /* 8..119 in each axis */ | |||||
| x = (unsigned char)(8u + (((r & 0x7Fu) * 7u) >> 3)); | |||||
| y = (unsigned char)(8u + ((((r >> 7) & 0x7Fu) * 7u) >> 3)); | |||||
| if ((unsigned char)(x > 64 ? x - 64 : 64 - x) < START_KEEPOUT_X && | |||||
| (unsigned char)(y > 64 ? y - 64 : 64 - y) < START_KEEPOUT_Y) { | |||||
| continue; | |||||
| } | |||||
| ++made; | |||||
| steps = 100u + (rnd() & 127u); | |||||
| while (steps--) { | |||||
| carve2x2(x, y); | |||||
| r = rnd(); | |||||
| dir = (unsigned char)((r & 3u) << 1); /* N, E, S or W */ | |||||
| x = (unsigned char)(x + dir_dx[dir]); | |||||
| y = (unsigned char)(y + dir_dy[dir]); | |||||
| if (x < 2) x = 2; else if (x > 124) x = 124; | |||||
| if (y < 2) y = 2; else if (y > 124) y = 124; | |||||
| } | |||||
| } | |||||
| } | |||||
| /* Half-widths of the starting cave, one per row from cy-4 to cy+4. */ | |||||
| static const unsigned char start_half[9] = { 3, 5, 6, 7, 7, 7, 6, 5, 3 }; | |||||
| /* Starting cave: a rough ellipse about 15x9 near the centre, carved last so it is always open. */ | |||||
| static void make_start_cave(void) | |||||
| { | |||||
| unsigned char row, half, x, y, cx, cy; | |||||
| unsigned char *p; | |||||
| cx = (unsigned char)(60u + (rnd() & 7u)); | |||||
| cy = (unsigned char)(60u + (rnd() & 7u)); | |||||
| for (row = 0; row < 9; ++row) { | |||||
| half = (unsigned char)(start_half[row] + (rnd() & 1u)); | |||||
| y = (unsigned char)(cy - 4 + row); | |||||
| p = MAP_PTR(cx - half, y); | |||||
| for (x = 0; x <= (unsigned char)(half * 2); ++x) { | |||||
| p[x] = TILE_FLOOR; | |||||
| } | |||||
| } | |||||
| map_start_x = cx; | |||||
| map_start_y = cy; | |||||
| /* Rows cy-1..cy+1 are at least 7 tiles wide each side, so these are always floor (R2.4). */ | |||||
| map_dwarf_x[0] = (unsigned char)(cx - 2); map_dwarf_y[0] = cy; | |||||
| map_dwarf_x[1] = cx; map_dwarf_y[1] = (unsigned char)(cy - 1); | |||||
| map_dwarf_x[2] = (unsigned char)(cx + 2); map_dwarf_y[2] = (unsigned char)(cy + 1); | |||||
| } | |||||
| void map_generate(unsigned seed) | |||||
| { | |||||
| if (seed == 0) { | |||||
| seed = 1; | |||||
| } | |||||
| map_seed = seed; | |||||
| rng = seed; | |||||
| memset(map, TILE_SOFT, sizeof map); | |||||
| make_veins(); | |||||
| make_caverns(); | |||||
| make_start_cave(); | |||||
| } | |||||
| @@ -8,6 +8,36 @@ | |||||
| #define MAP_H 128u | #define MAP_H 128u | ||||
| #define MAP_ADDR 0x4000u | #define MAP_ADDR 0x4000u | ||||
| /* Tile byte: bits 0-1 tile type, bit 6 claimed by a dwarf, bit 7 marked for digging. */ | |||||
| #define TILE_FLOOR 0x00 | |||||
| #define TILE_SOFT 0x01 | |||||
| #define TILE_HARD 0x02 | |||||
| #define TILE_TYPE 0x03 | |||||
| #define TILE_CLAIMED 0x40 | |||||
| #define TILE_MARKED 0x80 | |||||
| /* Address of tile (x, y) = MAP_ADDR + y*128 + x; x and y must be 0-127. | |||||
| ** Built from bytes (high = $40 + y/2, low = (y odd ? $80 : 0) + x) because a 16-bit | |||||
| ** shift by 7 is slow in cc65. */ | |||||
| #define MAP_PTR(x, y) ((unsigned char *)( \ | |||||
| ((unsigned)(unsigned char)((MAP_ADDR >> 8) + ((unsigned char)(y) >> 1)) << 8) | \ | |||||
| (unsigned char)((((unsigned char)(y) & 1) ? 0x80 : 0) | (unsigned char)(x)))) | |||||
| #define DWARF_COUNT 3 | |||||
| extern unsigned char map[MAP_W * MAP_H]; | extern unsigned char map[MAP_W * MAP_H]; | ||||
| /* Results of map_generate(). */ | |||||
| extern unsigned map_seed; /* 1-65535 */ | |||||
| extern unsigned char map_start_x, map_start_y; /* centre of the starting cave */ | |||||
| extern unsigned char map_dwarf_x[DWARF_COUNT]; /* dwarf start tiles (floor) */ | |||||
| extern unsigned char map_dwarf_y[DWARF_COUNT]; | |||||
| /* A seed from the timers and the raster line at the moment of the call (never 0). */ | |||||
| unsigned map_random_seed(void); | |||||
| /* Fill map[] from seed: soft rock, hard-rock veins, caverns, starting cave near the centre. | |||||
| ** The same seed always gives the same map. Seed 0 is treated as 1. */ | |||||
| void map_generate(unsigned seed); | |||||
| #endif | #endif | ||||
| @@ -0,0 +1,48 @@ | |||||
| /* Map view (R3). Tile bytes are translated through two 256-entry tables, so the | |||||
| ** marked/claimed flag bits need no special case when drawing. */ | |||||
| #include <c64.h> | |||||
| #include "charset.h" | |||||
| #include "map.h" | |||||
| #include "view.h" | |||||
| #define SCREEN ((unsigned char *)0x0400) | |||||
| #define COLRAM ((unsigned char *)0xD800) | |||||
| unsigned char tile_glyph[256]; | |||||
| unsigned char tile_color[256]; | |||||
| void view_init(void) | |||||
| { | |||||
| unsigned i = 0; | |||||
| unsigned char t; | |||||
| do { | |||||
| t = (unsigned char)i; | |||||
| if (t & TILE_MARKED) { | |||||
| tile_glyph[i] = GLYPH_MARKED; tile_color[i] = COLOR_YELLOW; | |||||
| } else switch (t & TILE_TYPE) { | |||||
| case TILE_FLOOR: tile_glyph[i] = GLYPH_FLOOR; tile_color[i] = COLOR_GRAY1; break; | |||||
| case TILE_HARD: tile_glyph[i] = GLYPH_ROCK_HARD; tile_color[i] = COLOR_GRAY2; break; | |||||
| default: tile_glyph[i] = GLYPH_ROCK_SOFT; tile_color[i] = COLOR_BROWN; break; | |||||
| } | |||||
| } while (++i < 256u); | |||||
| } | |||||
| void draw_view(unsigned char vx, unsigned char vy) | |||||
| { | |||||
| unsigned char row, col, t; | |||||
| const unsigned char *src; | |||||
| unsigned char *scr = SCREEN + VIEW_TOP * 40u; | |||||
| unsigned char *clr = COLRAM + VIEW_TOP * 40u; | |||||
| for (row = 0; row < VIEW_H; ++row) { | |||||
| src = MAP_PTR(vx, vy + row); | |||||
| /* Written with pointer increments: the indexed form (scr[col] = tile_glyph[t]) | |||||
| ** is miscompiled by cc65 V2.19 cc3c40c with -O (it indexes the table with the | |||||
| ** low byte of the screen address). M1.3 replaces this loop with assembly. */ | |||||
| for (col = 0; col < VIEW_W; ++col) { | |||||
| t = *src++; | |||||
| *scr++ = tile_glyph[t]; | |||||
| *clr++ = tile_color[t]; | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,17 @@ | |||||
| #ifndef STONEHOLD_VIEW_H | |||||
| #define STONEHOLD_VIEW_H | |||||
| /* The map view: screen rows VIEW_TOP .. VIEW_TOP+VIEW_H-1, all 40 columns. */ | |||||
| #define VIEW_W 40u | |||||
| #define VIEW_H 23u | |||||
| #define VIEW_TOP 1u | |||||
| /* Build the tile-to-glyph/colour tables. Call once after charset_install(). */ | |||||
| void view_init(void); | |||||
| /* Draw the VIEW_W x VIEW_H window of the map whose top-left tile is (vx, vy). | |||||
| ** vx must be 0..MAP_W-VIEW_W and vy 0..MAP_H-VIEW_H. | |||||
| ** M1.2: plain C. M1.3 replaces it with an assembly routine with the same prototype. */ | |||||
| void draw_view(unsigned char vx, unsigned char vy); | |||||
| #endif | |||||
| @@ -12,15 +12,19 @@ | |||||
| .EXAMPLE | .EXAMPLE | ||||
| pwsh -File tools/build.ps1 | pwsh -File tools/build.ps1 | ||||
| pwsh -File tools/build.ps1 -Target c64 | pwsh -File tools/build.ps1 -Target c64 | ||||
| pwsh -File tools/build.ps1 -Define FIXED_SEED=12345 # extra cl65 -D define(s) for this build | |||||
| #> | #> | ||||
| [CmdletBinding()] | [CmdletBinding()] | ||||
| param([string]$Target) | |||||
| param([string]$Target, [string[]]$Define) | |||||
| . (Join-Path $PSScriptRoot 'lib.ps1') | . (Join-Path $PSScriptRoot 'lib.ps1') | ||||
| try { | try { | ||||
| $project = Get-ProjectConfig | $project = Get-ProjectConfig | ||||
| $done = @{} | $done = @{} | ||||
| foreach ($t in Get-ProjectTargets $project $Target) { Invoke-TargetBuild $project $t $done } | |||||
| foreach ($t in Get-ProjectTargets $project $Target) { | |||||
| if ($Define) { $t.build.defines = @($t.build.defines) + $Define } | |||||
| Invoke-TargetBuild $project $t $done | |||||
| } | |||||
| Write-Host "Build complete." | Write-Host "Build complete." | ||||
| } catch [MissingToolException] { | } catch [MissingToolException] { | ||||
| Write-Host $_.Exception.Message -ForegroundColor Yellow | Write-Host $_.Exception.Message -ForegroundColor Yellow | ||||
| @@ -10,11 +10,13 @@ | |||||
| .EXAMPLE | .EXAMPLE | ||||
| pwsh -File tools/run.ps1 | pwsh -File tools/run.ps1 | ||||
| pwsh -File tools/run.ps1 -Target c128-80 | pwsh -File tools/run.ps1 -Target c128-80 | ||||
| pwsh -File tools/run.ps1 -Target c64 -Define FIXED_SEED=12345 # extra cl65 -D define(s) | |||||
| #> | #> | ||||
| [CmdletBinding()] | [CmdletBinding()] | ||||
| param( | param( | ||||
| [string]$Target, | [string]$Target, | ||||
| [switch]$NoBuild, | [switch]$NoBuild, | ||||
| [string[]]$Define, | |||||
| [ValidateSet('pal', 'ntsc')][string]$Video | [ValidateSet('pal', 'ntsc')][string]$Video | ||||
| ) | ) | ||||
| . (Join-Path $PSScriptRoot 'lib.ps1') | . (Join-Path $PSScriptRoot 'lib.ps1') | ||||
| @@ -22,6 +24,7 @@ param( | |||||
| try { | try { | ||||
| $project = Get-ProjectConfig | $project = Get-ProjectConfig | ||||
| $t = if ($Target) { Get-ProjectTargets $project $Target } else { @($project.resolved.targets)[0] } | $t = if ($Target) { Get-ProjectTargets $project $Target } else { @($project.resolved.targets)[0] } | ||||
| if ($Define) { $t.build.defines = @($t.build.defines) + $Define } | |||||
| if (-not $NoBuild) { Invoke-TargetBuild $project $t @{} } | if (-not $NoBuild) { Invoke-TargetBuild $project $t @{} } | ||||
| if ($t.build.kind -eq 'none' -and -not $t.disk) { throw "Target $($t.id) is built on the Commodore; follow docs/toolchain.md to load it." } | if ($t.build.kind -eq 'none' -and -not $t.disk) { throw "Target $($t.id) is built on the Commodore; follow docs/toolchain.md to load it." } | ||||
| $emu = Get-ToolPath $project $t.emulator.binary | $emu = Get-ToolPath $project $t.emulator.binary | ||||
Powered by TurnKey Linux.