WIP: feat(modapi): world.ray and rayAll: the game's own ray query, what it hits by kind, which actor it hit #2422

Closed
ChloeNow wants to merge 5 commits from ChloeNow/Longshot:feat/modapi-world-ray into mod-api
Contributor

Summary

longshot/world's ray(from, to) has been in the schema since the Modding API landed, but no game build answered it:
has("world.ray") was false and a call threw NotAvailableError. The game's own ray query takes the physics world's
reader/writer lock, and nobody had shown the main thread (where mods run) could take it without hanging the game.

This makes rays work, with what other engines' raycasts offer (Unity's Raycast/RaycastAll, Unreal's line traces,
Godot's intersect_ray, Roblox's workspace:Raycast):

  • ray(from, to, options?) and rayAll(from, to, options?) (new): the nearest hit, or every hit along the ray,
    nearest first, each body once (at most 16).
  • What it hits, by kind (RayOptions): ground (terrain, floors, walls, buildings, trees, fixed furniture),
    objects (doors, chests, pots, rocks), characters (enemies, people), water (its surface) on by default;
    player (Link) off by default, so a ray cast from Link doesn't stop on him at distance 0. The kinds are the game's
    own collision layers, their names read from the binary and each checked in the game one bit at a time.
  • ignore: an actor's key; the ray passes through its bodies (the engine's collision groups, as the game's own
    helpers set them).
  • What it hit (RayHit): position, normal, distance as before, plus kind, actorKey (the actor whose body it
    is, for actors.list({ key }); for a mod with actors.read) and inside (the ray started in it).

How it stays safe, in short: the cast takes the same shared hold on the lock the game's own reader takes, but only when
it can do so without waiting (a compare-and-swap while others read, TryLockMutex from 0). If a writer (the physics
step, a body being added or moved) holds it, it retries for 50 µs, then sleeps 1 ms once a frame with the game core
given up (no CPU charged to the mod), then answers null for that frame. The game has no try-lock of its own, so this
is the only way to never block; the release is the game's own unlock.

Not offered, and why (docs/dev/reference/world.md):

  • Invisible walls. No ray query meets them: a run swept every layer, sub-layer and ray layer in both of the
    physics manager's worlds at Kokiri Forest's exit walls and met only terrain and a Kokiri. Their collision is the
    character controllers', and the game's own rays don't see them either. Detecting them would mean intersecting their
    placed shapes from map data: a separate change.
  • Shape casts and overlaps (a swept sphere, capsule or box): a separate change, stacked on this one: #2438. (An
    earlier version of this description said the game has no query class for them. It does: its AI's shape query, which
    #2438 uses.)

Read first: host/game/world.h (what was read of the game: the query, the layers, the lock), then try_read,
acquire, classify and cast in host/game/world.c, then docs/dev/reference/world.md.

Testing

Tested: Windows 11, NVIDIA GeForce RTX 4060 Laptop GPU, driver 621.24, the save kokiri-start, through
dev.py game-tests, on mod-api @ 8da9fe53 with this change (and the other open mod-api PRs, which loading a save
needs):

  • A validation mod, every check passing: the ground at Link's feet (kind: "ground"); Link not hit by default, hit
    with player: true and named by his key, passed through with ignore set to his key, inside from within him; a
    Keese spawned in front of him hit as character with its key, passed with characters: false, rayAll down
    through it giving [character, ground]; a door an object with its key; at Lake Hylia 14 of 14 rays stopping on the
    water's surface as water, and on the lake bed with water: false.
  • Probe runs with HOS_RAY_TRACE=1: more than 100,000 casts in Link's house, Kokiri Forest, Hyrule Field and Lake
    Hylia, across map changes and through loads. No stall, no crash.
  • The repository's tests/game-features/world in the game: 3 passed (the new check: the floor at Link's feet), the
    rest fake-only; with HOS_WORLD_RAY=0 the mod sees no ray, its test skips, the others pass.
  • Game-free: tests/game-features/ in the fake game (19 areas, including the new kinds and rayAll test);
    tests/mod_settings_fake.mjs; modapi/generate.py --check; the switch table's checks for the new rows.

Still to test:

  • Linux: mise run test, including tests/test_modrt.py (its stand-in game's game_world_cast, with a floor, water
    and a character; new checks for kinds, keys, ignore, rayAll, and a mod without actors.read). Not run here: the
    fixture links the POSIX way (by hand, or CI)

Measurements

Verdict: a ray costs 80 to 100 µs on the main thread (a cast for the hit and one for its kind); nothing runs when
no mod casts.

Setup: Windows 11, RTX 4060 Laptop GPU, driver 621.24; HOS_RAY_TRACE=1's counts over the validation run (8,400
requests in Kokiri Forest and Lake Hylia, 4 rays a frame of stress on top).

a cast about 60 µs
a ray with its kind 80 to 93 µs on average
the slowest request about 2.2 ms (one that slept on a busy lock)
busy at the first try 17 in 8,400 requests (around Lake Hylia's load)
slept on (1 ms, no CPU) 4
null (busy past the sleep) 1

An earlier version spun for 2 ms on a busy lock: one cast used most of a mod callback's 5 ms of CPU, and the runtime
faulted the mod. Hence the short spin and the sleep.

Risks

  • The lock's protocol is the game's, copied for the acquire: the same state word, the same increments for the
    main thread's priority, the mutex only from 0, the game's own unlock at the end. A misreading would corrupt the lock
    for the game's own readers and writers. The reading is in world.md; the runs saw no stall or crash in more than
    100,000 casts.
  • The release can wait on the mutex (the game's own unlock, for the last reader): only for a writer between its
    lock and its condition-variable wait, or a low-priority reader mid-cast. Both are short, and neither waits for the
    main thread.
  • Build-specific addresses are checked before use (the build id, the functions in the lift, the world's vtable
    slots, the filter's vtable). Any other build keeps has("world.ray") false.
  • actorKey can be null for an object of several bodies hit on a part the host doesn't know (its kind is still
    right): seen once in Link's house.
  • Generator: generate.py now emits the list type for a nullable list result (list<RayHit>?); no other
    generated output changes.
  • Pre-existing, not from this change: tests/test_mod_host_features.py didn't compile on mod-api
    (hos_game_is_mkw undeclared in its cut of services_game.c); it gets the stub here and still fails on mod-api
    for files.* and content.change (10 features no cut function answers), unchanged. tests/test_switch_table.py
    fails on mod-api's order of a few RX_VK_* rows; the new HOS_ rows are in order and match their reads.

Checklist

Ready for review

  • One topic, on current mod-api; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on mod-api is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A switch has its row in gpu/vk/switches.def; its default ships on (the kill switch's on).
  • A check under tests/ that reads changed source is updated, not weakened. (tests/modrt_test.c's stand-in game, test_mod_host_features.py's ABSENT, the fake game)
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.

🤖 Generated with Claude Code

## Summary `longshot/world`'s `ray(from, to)` has been in the schema since the Modding API landed, but no game build answered it: `has("world.ray")` was false and a call threw `NotAvailableError`. The game's own ray query takes the physics world's reader/writer lock, and nobody had shown the main thread (where mods run) could take it without hanging the game. This makes rays work, with what other engines' raycasts offer (Unity's `Raycast`/`RaycastAll`, Unreal's line traces, Godot's `intersect_ray`, Roblox's `workspace:Raycast`): - **`ray(from, to, options?)`** and **`rayAll(from, to, options?)`** (new): the nearest hit, or every hit along the ray, nearest first, each body once (at most 16). - **What it hits, by kind** (`RayOptions`): `ground` (terrain, floors, walls, buildings, trees, fixed furniture), `objects` (doors, chests, pots, rocks), `characters` (enemies, people), `water` (its surface) on by default; `player` (Link) off by default, so a ray cast from Link doesn't stop on him at distance 0. The kinds are the game's own collision layers, their names read from the binary and each checked in the game one bit at a time. - **`ignore`**: an actor's key; the ray passes through its bodies (the engine's collision groups, as the game's own helpers set them). - **What it hit** (`RayHit`): position, normal, distance as before, plus `kind`, `actorKey` (the actor whose body it is, for `actors.list({ key })`; for a mod with `actors.read`) and `inside` (the ray started in it). How it stays safe, in short: the cast takes the same shared hold on the lock the game's own reader takes, but only when it can do so without waiting (a compare-and-swap while others read, `TryLockMutex` from 0). If a writer (the physics step, a body being added or moved) holds it, it retries for 50 µs, then sleeps 1 ms once a frame with the game core given up (no CPU charged to the mod), then answers `null` for that frame. The game has no try-lock of its own, so this is the only way to never block; the release is the game's own unlock. Not offered, and why (docs/dev/reference/world.md): - **Invisible walls.** No ray query meets them: a run swept every layer, sub-layer and ray layer in both of the physics manager's worlds at Kokiri Forest's exit walls and met only terrain and a Kokiri. Their collision is the character controllers', and the game's own rays don't see them either. Detecting them would mean intersecting their placed shapes from map data: a separate change. - **Shape casts and overlaps** (a swept sphere, capsule or box): a separate change, stacked on this one: #2438. (An earlier version of this description said the game has no query class for them. It does: its AI's shape query, which #2438 uses.) Read first: `host/game/world.h` (what was read of the game: the query, the layers, the lock), then `try_read`, `acquire`, `classify` and `cast` in `host/game/world.c`, then docs/dev/reference/world.md. ## Testing **Tested:** Windows 11, NVIDIA GeForce RTX 4060 Laptop GPU, driver 621.24, the save `kokiri-start`, through `dev.py game-tests`, on `mod-api` @ 8da9fe53 with this change (and the other open mod-api PRs, which loading a save needs): - A validation mod, every check passing: the ground at Link's feet (`kind: "ground"`); Link not hit by default, hit with `player: true` and named by his key, passed through with `ignore` set to his key, `inside` from within him; a Keese spawned in front of him hit as `character` with its key, passed with `characters: false`, `rayAll` down through it giving `[character, ground]`; a door an `object` with its key; at Lake Hylia 14 of 14 rays stopping on the water's surface as `water`, and on the lake bed with `water: false`. - Probe runs with `HOS_RAY_TRACE=1`: more than 100,000 casts in Link's house, Kokiri Forest, Hyrule Field and Lake Hylia, across map changes and through loads. No stall, no crash. - The repository's `tests/game-features/world` in the game: 3 passed (the new check: the floor at Link's feet), the rest fake-only; with `HOS_WORLD_RAY=0` the mod sees no ray, its test skips, the others pass. - Game-free: `tests/game-features/` in the fake game (19 areas, including the new kinds and `rayAll` test); `tests/mod_settings_fake.mjs`; `modapi/generate.py --check`; the switch table's checks for the new rows. **Still to test:** - Linux: `mise run test`, including `tests/test_modrt.py` (its stand-in game's `game_world_cast`, with a floor, water and a character; new checks for kinds, keys, `ignore`, `rayAll`, and a mod without `actors.read`). Not run here: the fixture links the POSIX way (by hand, or CI) ## Measurements **Verdict:** a ray costs 80 to 100 µs on the main thread (a cast for the hit and one for its kind); nothing runs when no mod casts. Setup: Windows 11, RTX 4060 Laptop GPU, driver 621.24; `HOS_RAY_TRACE=1`'s counts over the validation run (8,400 requests in Kokiri Forest and Lake Hylia, 4 rays a frame of stress on top). | | | |---|---| | a cast | about 60 µs | | a `ray` with its kind | 80 to 93 µs on average | | the slowest request | about 2.2 ms (one that slept on a busy lock) | | busy at the first try | 17 in 8,400 requests (around Lake Hylia's load) | | slept on (1 ms, no CPU) | 4 | | `null` (busy past the sleep) | 1 | An earlier version spun for 2 ms on a busy lock: one cast used most of a mod callback's 5 ms of CPU, and the runtime faulted the mod. Hence the short spin and the sleep. ## Risks - **The lock's protocol is the game's, copied** for the acquire: the same state word, the same increments for the main thread's priority, the mutex only from 0, the game's own unlock at the end. A misreading would corrupt the lock for the game's own readers and writers. The reading is in world.md; the runs saw no stall or crash in more than 100,000 casts. - **The release can wait on the mutex** (the game's own unlock, for the last reader): only for a writer between its lock and its condition-variable wait, or a low-priority reader mid-cast. Both are short, and neither waits for the main thread. - **Build-specific addresses** are checked before use (the build id, the functions in the lift, the world's vtable slots, the filter's vtable). Any other build keeps `has("world.ray")` false. - **`actorKey` can be null** for an object of several bodies hit on a part the host doesn't know (its kind is still right): seen once in Link's house. - **Generator**: `generate.py` now emits the list type for a nullable list result (`list<RayHit>?`); no other generated output changes. - **Pre-existing, not from this change**: `tests/test_mod_host_features.py` didn't compile on `mod-api` (`hos_game_is_mkw` undeclared in its cut of `services_game.c`); it gets the stub here and still fails on `mod-api` for `files.*` and `content.change` (10 features no cut function answers), unchanged. `tests/test_switch_table.py` fails on `mod-api`'s order of a few `RX_VK_*` rows; the new `HOS_` rows are in order and match their reads. ## Checklist ### Ready for review - [x] One topic, on current `mod-api`; the title is a Conventional Commit. - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [ ] `mise run test` passes (any failure that also fails on `mod-api` is named). <!-- Windows half run; the Linux half is CI's --> - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [x] A switch has its row in `gpu/vk/switches.def`; its default ships on (the kill switch's `on`). - [x] A check under `tests/` that reads changed source is updated, not weakened. (`tests/modrt_test.c`'s stand-in game, `test_mod_host_features.py`'s ABSENT, the fake game) - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. <!-- Windows built and played; Linux: CI --> - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
feat(modapi): world.ray and rayAll: the game's own ray query, what it hits by kind, which actor it hit
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 5s
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
Website / Website on Cloudflare (pull_request) Has been cancelled
76da61dd95
longshot/world's ray was in the schema with no game build answering it. It now casts through the game's own
synchronous ray query (f_80e86730), built as the game builds its own inline (f_80e865e0's layout), under the physics
world's reader/writer lock taken as the game's own reader would take it but never with a wait that could hang: a
compare-and-swap while others read, or under nn::os TryLockMutex from 0; busy, 50 us of retries, then once a frame a
1 ms sleep with the core given up (no CPU of the mod's), then null. The game's own unlock lets the hold go.

What a ray hits is chosen by kind (RayOptions: ground, objects, characters, player, water), the game's collision layers
read from the binary and checked one bit at a time in runs; by default everything but Link, so a ray cast from him does
not stop on him. ignore passes through an actor's collision group (f_803f2dd0, f_80e86ac0). A hit says its kind (the
same ray against one kind at a time), the key of the actor whose body it is (a rigid body's handle, [[body+0x78]+8],
matched to the listed actors' bodies; for a mod with actors.read) and whether the ray started inside it. rayAll gives
every hit along the ray, each body once. The game's invisible walls are no kind: no ray query meets them (a run swept
every layer, sub-layer and ray layer in both of the manager's worlds).

has("world.ray") and has("world.rayAll") are true in build e0f3ccfa; HOS_WORLD_RAY=0 turns them off. In the game
(more than 100,000 casts across map changes): no stall; about 60 us a cast, 80 to 100 us a ray with its kind; busy
about once in 500 requests, slept on 4 times in 8,400, one null. The checks: the ground at Link's feet, Link hit and
named, passed through with ignore, a spawned Keese hit and named, passed with characters off, rayAll through it, a
door an object with its key, Lake Hylia's surface water and the bed under it with water off.
docs/dev/reference/world.md has the lock, the layers, the runs and what is not offered (shape casts, the walls).

The fake game (mod-tools) filters a test's hits by the options as the game does, and has rayAll. The generator now
emits the list type for a nullable list result (list<RayHit>?). Tests: test_modrt.py's world checks over the stand-in's
floor, water and a character (kinds, keys, ignore, rayAll, a mod without actors.read); test_mod_host_features.py no
longer lists world.ray as absent and gets the hos_game_is_mkw stub services_game.c already called; the world feature
suite's game test checks the floor at Link's feet, its fake tests the kinds and rayAll.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

Superseded by #2662: ray and rayAll with kinds, ignore, actorKey and inside, made again on the Rust mod API's own step-thread rays. Closing this C one.

Superseded by #2662: ray and rayAll with kinds, ignore, actorKey and inside, made again on the Rust mod API's own step-thread rays. Closing this C one.
ChloeNow closed this pull request 2026-11-27 11:18:24 +00:00
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
PR description / Description follows the template (pull_request_target) Successful in 5s
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
Website / Website on Cloudflare (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2422
No description provided.