Collide sprites with rectangles that are not sprites
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m19s
akbasic CI Build / sanitizers (push) Failing after 4m33s
akbasic CI Build / coverage (push) Failing after 3m41s
akbasic CI Build / akgl_build (push) Failing after 21s
akbasic CI Build / mutation_test (push) Failing after 3m29s
Some checks failed
akbasic CI Build / cmake_build (push) Failing after 3m19s
akbasic CI Build / sanitizers (push) Failing after 4m33s
akbasic CI Build / coverage (push) Failing after 3m41s
akbasic CI Build / akgl_build (push) Failing after 21s
akbasic CI Build / mutation_test (push) Failing after 3m29s
`SOLID id, x1, y1, x2, y2` registers static collision geometry; `SOLID id` retires one and a bare `SOLID` retires them all, the way `TRAP`, `COLLISION` and `DCLOSE` all read absence. `COLLISION 2` and `BUMP(2)` stop being refused and mean *sprite met static geometry*. **This is the thing eight sprite slots made impossible.** A wall of bricks wants sixty, so until now a program could only collide with one by doing the arithmetic itself against its own array -- which is exactly what both breakout listings do, at about two hundred lines between them. A rectangle costs no sprite slot. The id is the **program's own number**, 1 to 64, not a minted handle. That is the whole trick for "which brick did I hit": the id comes back out again, so a wall built as `SOLID I#, ...` maps onto `B#(I#)` with no lookup, and retiring a broken brick is `SOLID I#`. `COLLISION 2` was refused with "sprite-to-background collision needs the screen read back every frame", which was true of the question a C128 asks -- a sprite against the bitmap's set pixels. `SOLID` gives this interpreter a background made of rectangles instead, which is the same question in a form it can answer. Same move `SPRSAV` made when it learned to take an image path. `AKBASIC_INTERRUPT_BACKGROUND` has been sitting in the interrupt table commented "COLLISION 2 -- sprite met background; refused" the whole time. Its accumulator is separate, so a sprite hitting a wall never sets a bit in `BUMP(1)`. **There is no `akgl_CollisionWorld` here, and that is deliberate.** libakgl's uniform grid keeps its cell heads, cell size and origin in file-scope statics, so it is one index per process -- and `akgl_collision_world_init()` ends in a `reset()` that memsets those heads *and* calls `akgl_heap_init_collision_cells()`. An interpreter embedded in a game with its own collision world would have destroyed every registration that game had made, on the first `SOLID` a script ran. So the geometry is indexed by an ordinary array here and pairs go straight to `akgl_collision_test()`, which needs no world. At sixty-four rectangles that is the right answer anyway; libakgl's own numbers put a naive sweep at 0.7% of a frame at sixty-four objects. **The scan now short-circuits when nothing has moved**, and that is what makes any of it affordable. Its inputs are the sprites' boxes, which slots are collidable, and the static geometry; if none changed the answer cannot have. A frame runs one full scan and 255 cached ones. Eight sprites against sixty-four rectangles is five hundred and twelve tests -- fine once a frame, ruinous 256 times. The benchmark was rewritten to say which path it is timing, because with the cache in place a loop that only calls the scan measures the short circuit and nothing else. Breakout now costs 590.6 ns for its one full scan plus 255 cached at 40.0, which is 10.8 us against a 1.19 ms frame -- **0.91%, less than the 2.0% it cost before any of this work**, with static geometry and contacts added on top. `NEW` retires the rectangles, where it cannot undefine a sprite pattern: there *is* an entry point for this one, so leaving them would be a choice, and the wrong one -- a rectangle is invisible, so one left behind by a deleted program is an unexplainable collision in the next. `CLR` leaves them alone. `tests/sprite_verbs.c` gains the whole second path against the mock and its `COLLISION 2` case is rewritten: it pinned the refusal, and now pins that type 2 arms its own handler without disturbing type 1's. `tests/akgl_backends.c` gains the end-to-end version, including a full sixty-four-rectangle wall so the proxy budget is exercised at its ceiling and the pool has to come back intact, and the sixty-fifth refused by name. A bare `SOLID` needed `akbasic_parse_optional_arglist` rather than `akbasic_parse_arglist`, which `DCLOSE` already uses for the same shape. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EwxGB6TdoVvZ11KQQME9cL
This commit is contained in:
@@ -350,9 +350,32 @@ static void test_collision_arming(void)
|
||||
"COLLISION with no target should disarm");
|
||||
harness_stop();
|
||||
|
||||
TEST_REQUIRE_OK(run_program("10 COLLISION 2, 100\n"));
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "read back every frame") != NULL,
|
||||
"COLLISION 2 should be refused with a reason, got \"%s\"", HARNESS_OUTPUT);
|
||||
/*
|
||||
* Type 2 arms a *second* handler, on its own interrupt source, and leaves
|
||||
* type 1's alone. It used to be refused -- a C128's type 2 is a sprite
|
||||
* against the bitmap screen's set pixels, which needs the render target read
|
||||
* back every frame -- and SOLID is what turned it into a question this
|
||||
* interpreter can answer, against rectangles instead of pixels.
|
||||
*/
|
||||
TEST_REQUIRE_OK(run_program("10 COLLISION 1, 100\n"
|
||||
"20 COLLISION 2, 200\n"));
|
||||
TEST_REQUIRE_STR(HARNESS_OUTPUT, "");
|
||||
TEST_REQUIRE(HARNESS_RUNTIME.interrupts[AKBASIC_INTERRUPT_SPRITE].armed,
|
||||
"COLLISION 1 should still have armed the sprite interrupt");
|
||||
TEST_REQUIRE_INT(HARNESS_RUNTIME.interrupts[AKBASIC_INTERRUPT_SPRITE].line, 100);
|
||||
TEST_REQUIRE(HARNESS_RUNTIME.interrupts[AKBASIC_INTERRUPT_BACKGROUND].armed,
|
||||
"COLLISION 2 should have armed the background interrupt");
|
||||
TEST_REQUIRE_INT(HARNESS_RUNTIME.interrupts[AKBASIC_INTERRUPT_BACKGROUND].line, 200);
|
||||
harness_stop();
|
||||
|
||||
/* And disarming one leaves the other armed. */
|
||||
TEST_REQUIRE_OK(run_program("10 COLLISION 1, 100\n"
|
||||
"20 COLLISION 2, 200\n"
|
||||
"30 COLLISION 2\n"));
|
||||
TEST_REQUIRE(HARNESS_RUNTIME.interrupts[AKBASIC_INTERRUPT_SPRITE].armed,
|
||||
"disarming type 2 should not have disarmed type 1");
|
||||
TEST_REQUIRE(!HARNESS_RUNTIME.interrupts[AKBASIC_INTERRUPT_BACKGROUND].armed,
|
||||
"COLLISION 2 with no target should disarm");
|
||||
harness_stop();
|
||||
|
||||
TEST_REQUIRE_OK(run_program("10 COLLISION 3, 100\n"));
|
||||
@@ -407,6 +430,70 @@ static void test_collision_fires(void)
|
||||
harness_stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief SOLID registers static geometry, and COLLISION 2 fires on meeting it.
|
||||
*
|
||||
* The whole second path, against the mock: the device is told about a rectangle,
|
||||
* the scan reports a sprite touching one, the service accumulates it on its own
|
||||
* accumulator and raises its own interrupt, and BUMP(2) reports and clears
|
||||
* without disturbing BUMP(1).
|
||||
*/
|
||||
static void test_solid_geometry(void)
|
||||
{
|
||||
TEST_REQUIRE_OK(harness_start(NULL));
|
||||
mock_devices_init();
|
||||
TEST_REQUIRE_OK(akbasic_runtime_set_devices(&HARNESS_RUNTIME, NULL, NULL, NULL,
|
||||
&MOCK_SPRITES));
|
||||
TEST_REQUIRE_OK(akbasic_runtime_load(&HARNESS_RUNTIME,
|
||||
"1 COLLISION 2, 5\n"
|
||||
"2 SOLID 7, 100, 100, 140, 120\n"
|
||||
"3 PRINT \"MAIN\"\n"
|
||||
"4 GOTO 3\n"
|
||||
"5 PRINT \"WALL \" + BUMP(2)\n"
|
||||
"6 PRINT \"SPRITES \" + BUMP(1)\n"
|
||||
"7 RETURN\n"));
|
||||
TEST_REQUIRE_OK(akbasic_runtime_start(&HARNESS_RUNTIME, AKBASIC_MODE_RUN));
|
||||
|
||||
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 3));
|
||||
TEST_REQUIRE(strstr(MOCK.log, "solid 7,100,100,140,120") != NULL,
|
||||
"SOLID should have reached the device, log was \"%s\"", MOCK.log);
|
||||
|
||||
/* Sprite 1 meets a wall; nothing meets another sprite. */
|
||||
mock_set_solid_collisions(0x01);
|
||||
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 2));
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "WALL 1") != NULL,
|
||||
"the type 2 handler should have reported sprite 1, got \"%s\"", HARNESS_OUTPUT);
|
||||
|
||||
mock_set_solid_collisions(0);
|
||||
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 2));
|
||||
/* The two accumulators are separate: BUMP(1) saw nothing. */
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "SPRITES 0") != NULL,
|
||||
"a static collision should not set BUMP(1), got \"%s\"", HARNESS_OUTPUT);
|
||||
TEST_REQUIRE_INT(HARNESS_RUNTIME.sprite_state.bumpedsolid, 0);
|
||||
harness_stop();
|
||||
|
||||
/* SOLID with no arguments retires every one of them. */
|
||||
TEST_REQUIRE_OK(run_program("10 SOLID 3, 0, 0, 10, 10\n"
|
||||
"20 SOLID 4, 20, 20, 30, 30\n"
|
||||
"30 SOLID\n"));
|
||||
TEST_REQUIRE(strstr(MOCK.log, "solid 3 off") != NULL,
|
||||
"bare SOLID should have retired 3, log was \"%s\"", MOCK.log);
|
||||
TEST_REQUIRE(strstr(MOCK.log, "solid 4 off") != NULL,
|
||||
"bare SOLID should have retired 4, log was \"%s\"", MOCK.log);
|
||||
harness_stop();
|
||||
|
||||
/* Out of range, and a rectangle that is not one. */
|
||||
TEST_REQUIRE_OK(run_program("10 SOLID 999, 0, 0, 10, 10\n"));
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "outside 1..") != NULL,
|
||||
"SOLID 999 should be refused, got \"%s\"", HARNESS_OUTPUT);
|
||||
harness_stop();
|
||||
|
||||
TEST_REQUIRE_OK(run_program("10 SOLID 1, 10, 10, 10, 20\n"));
|
||||
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "not a rectangle") != NULL,
|
||||
"a zero-width SOLID should be refused, got \"%s\"", HARNESS_OUTPUT);
|
||||
harness_stop();
|
||||
}
|
||||
|
||||
/** @brief The readbacks answer from interpreter state, so they need no device. */
|
||||
static void test_readbacks(void)
|
||||
{
|
||||
@@ -518,6 +605,7 @@ int main(void)
|
||||
test_sprsav_refuses_to_write();
|
||||
test_collision_arming();
|
||||
test_collision_fires();
|
||||
test_solid_geometry();
|
||||
test_readbacks();
|
||||
test_no_device();
|
||||
test_new_clears_sprites();
|
||||
|
||||
Reference in New Issue
Block a user