Unbreak the three example programs the RND merge left behind
All checks were successful
akbasic CI Build / cmake_build (push) Successful in 3m46s
akbasic CI Build / coverage (push) Successful in 4m16s
akbasic CI Build / sanitizers (push) Successful in 8m10s
akbasic CI Build / akgl_build (push) Successful in 8m10s
akbasic CI Build / mutation_test (push) Successful in 17m55s
All checks were successful
akbasic CI Build / cmake_build (push) Successful in 3m46s
akbasic CI Build / coverage (push) Successful in 4m16s
akbasic CI Build / sanitizers (push) Successful in 8m10s
akbasic CI Build / akgl_build (push) Successful in 8m10s
akbasic CI Build / mutation_test (push) Successful in 17m55s
Adding native RND and ASC (ae2c702) made RND a function name, and a suffixed
identifier that collides with one is refused -- "SYNTAX ERROR Reserved word in
variable name". Three example programs held their PRNG output in a variable
called RND#, or a host field called RND%, and none of them had run since:
- examples/galaga/ bound RND% as a host field on both ENEMY and GAME. The
BASIC-visible name is ROLL% now; the C member stays `rnd`. This one was
caught by example_galaga and example_galaga_interop, which have been
failing.
- examples/breakout/characters/breakout.bas and examples/megademo/
megademo.bas both use RND# for their LCG output, renamed to ROLL#. Neither
is in any test, so neither failure was visible.
examples/breakout/sprites/breakout.bas was broken a second way: seven REM lines
the reader refuses. Worth recording that the ceiling is not the one the message
names -- src/sink_stdio.c fails when the read filled the buffer without seeing
a terminator, so with AKBASIC_MAX_LINE_LENGTH at 80 the message says "79
character limit" and the real maximum is 78, because a 79-character line leaves
no room for the newline. The sweeps that fixed the corpus and the megademo for
this did not reach this file. The seven comments are reflowed.
The prose went stale with the code. Chapter 21 said "there is no RND verb in
this dialect; issue #16 tracks adding one", chapter 17's historical aside
offered an LCG that no longer parses, and four REM blocks across the two games
said the same thing. All of them now say RND exists, and say why these programs
keep their own generator anyway: the sequence has to be reproducible for a
headless run to be the same game on every machine, which is what lets
interop_test.c assert exact counts.
Chapter 21 also gains the rule that bit them, since a reader writing a host
type will hit it: a host field name is a bare word and shares a namespace with
every verb and function.
None of this came from the submodule bump -- all three were already broken on
main. It was found by running the tutorial games, which nothing else does;
that gap is akbasic issue #58.
Verified: all three run clean under the dummy drivers, and 114/114 default,
116/116 with akgl.
Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
Co-Authored-By: Claude Code (Claude Opus 5, claude-opus-5[1m]) <noreply@anthropic.com>
This commit is contained in:
@@ -20,7 +20,7 @@ whole development loop; the engine never rebuilds.
|
||||
- **[Step 2](#step-2-bind-the-engines-own-actor)** — bind the engine's own
|
||||
actor as the second type, which is the point of the whole exercise
|
||||
- **[Step 3](#step-3-share-the-frame-and-the-dice)** — share the frame state,
|
||||
and give the script randomness it cannot make itself
|
||||
and hand the script dice the engine controls
|
||||
- **[Step 4](#step-4-why-bindings-and-not-arguments)** — see why the structures
|
||||
are bindings rather than function arguments
|
||||
- **[Step 5](#step-5-the-shape-of-the-script)** — learn the three language
|
||||
@@ -75,7 +75,7 @@ typedef struct galaga_Enemy
|
||||
float t; /* parametric clock for the current maneuver */
|
||||
int32_t hp;
|
||||
int32_t fire; /* outbox: script sets 1, engine consumes */
|
||||
float rnd; /* inbox: engine writes fresh 0..1 each call */
|
||||
float rnd; /* inbox: fresh 0..1 each call; ROLL% in BASIC */
|
||||
} galaga_Enemy;
|
||||
```
|
||||
|
||||
@@ -107,7 +107,7 @@ static const akbasic_HostField ENEMY_FIELDS[] = {
|
||||
AKBASIC_HOST_FIELD( galaga_Enemy, t, "T%", AKBASIC_HOSTFIELD_FLOAT ),
|
||||
AKBASIC_HOST_FIELD( galaga_Enemy, hp, "HP#", AKBASIC_HOSTFIELD_INT32 ),
|
||||
AKBASIC_HOST_FIELD( galaga_Enemy, fire, "FIRE#", AKBASIC_HOSTFIELD_INT32 ),
|
||||
AKBASIC_HOST_FIELD( galaga_Enemy, rnd, "RND%", AKBASIC_HOSTFIELD_FLOAT )
|
||||
AKBASIC_HOST_FIELD( galaga_Enemy, rnd, "ROLL%", AKBASIC_HOSTFIELD_FLOAT )
|
||||
};
|
||||
static const akbasic_HostType ENEMY_TYPE = {
|
||||
"ENEMY", sizeof(galaga_Enemy), ENEMY_FIELDS, 8
|
||||
@@ -194,7 +194,7 @@ typedef struct galaga_Shared
|
||||
float playerx; /* the player actor's position, this frame */
|
||||
float playery;
|
||||
int32_t wave;
|
||||
float rnd; /* fresh 0..1 each frame; the issue #16 route */
|
||||
float rnd; /* fresh 0..1 each frame; ROLL% to the script */
|
||||
} galaga_Shared;
|
||||
```
|
||||
|
||||
@@ -203,13 +203,24 @@ the engine refreshes it at the top of every frame. The boss reads
|
||||
`GAME@.PLAYERX%` to lead its dive; the fire decision reads it to know whether
|
||||
anything is worth shooting at.
|
||||
|
||||
The `rnd` fields — one here per frame, one on each enemy per call — exist
|
||||
because the engine's PRNG is the script's **only** source of randomness: write
|
||||
`SELF@.RND% < DT% * 1.5` and an enemy's trigger finger is a dice roll. There
|
||||
is no `RND` verb in this dialect; issue #16 tracks adding one, and Chapter
|
||||
17's breakout hand-rolls a linear congruential generator in BASIC as the other
|
||||
route. Here the engine fills the field, which also keeps a headless run the
|
||||
same game on every machine — the PRNG is the example's own, not libc's.
|
||||
The `rnd` fields — one here per frame, one on each enemy per call — carry the
|
||||
engine's PRNG into the script: write `SELF@.ROLL% < DT% * 1.5` and an enemy's
|
||||
trigger finger is a dice roll.
|
||||
|
||||
The dialect does now have a native `RND` function — issue #16 closed, and
|
||||
[Chapter 12](12-function-reference.md) documents it — so this is no longer the
|
||||
*only* route; Chapter 17's breakout hand-rolls a linear congruential generator
|
||||
in BASIC as a third. The engine keeps filling the field here on purpose,
|
||||
because it buys something `RND` cannot: the numbers come from the example's own
|
||||
PRNG rather than libc's, so a headless run is the same game on every machine,
|
||||
which is what makes `example_galaga` a test and not just a demo.
|
||||
|
||||
**The BASIC name is `ROLL%`, not `RND%`.** A host field is a bare word and
|
||||
shares a namespace with every verb and function, so once `RND` became a
|
||||
function name a field could no longer be called that — the scanner refuses it
|
||||
with *"Reserved word in variable name"*. The C member stays `rnd`; only the
|
||||
name the script sees had to move. [Chapter 16](16-structures.md) has the same
|
||||
rule for `TYPE` declarations.
|
||||
|
||||
## Step 4: Why bindings, and not arguments
|
||||
|
||||
@@ -351,7 +362,7 @@ DEF DECIDEFIRE(DT%)
|
||||
DX% = GAME@.PLAYERX% - ACTOR@.X%
|
||||
IF ABS(DX%) > 140 THEN RETURN 0
|
||||
IF ACTOR@.Y% > GAME@.PLAYERY% THEN RETURN 0
|
||||
IF SELF@.RND% < DT% * 1.5 THEN SELF@.FIRE# = 1
|
||||
IF SELF@.ROLL% < DT% * 1.5 THEN SELF@.FIRE# = 1
|
||||
RETURN 0
|
||||
END
|
||||
```
|
||||
@@ -386,7 +397,7 @@ DEF UPDATEBEE(DT%)
|
||||
IF (S# AND 2) > 0 THEN BEGIN
|
||||
ACTOR@.X% = SELF@.HOMEX% + SIN(SELF@.T% * 1.7) * 16
|
||||
ACTOR@.Y% = SELF@.HOMEY%
|
||||
IF SELF@.RND% < DT% * 0.04 THEN SELF@.STATE# = 4 : SELF@.T% = 0
|
||||
IF SELF@.ROLL% < DT% * 0.04 THEN SELF@.STATE# = 4 : SELF@.T% = 0
|
||||
BEND
|
||||
IF (S# AND 4) > 0 THEN BEGIN
|
||||
R# = DIVESTEP(DT%, 130, 0.2)
|
||||
|
||||
Reference in New Issue
Block a user