WIP: feat(modapi): world.ray and rayAll: the game's own ray query, what it hits by kind, which actor it hit #2422
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/mods
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2422
Loading…
Reference in a new issue
No description provided.
Delete branch "ChloeNow/Longshot:feat/modapi-world-ray"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
longshot/world'sray(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 threwNotAvailableError. The game's own ray query takes the physics world'sreader/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'sworkspace:Raycast):ray(from, to, options?)andrayAll(from, to, options?)(new): the nearest hit, or every hit along the ray,nearest first, each body once (at most 16).
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'sown 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 ownhelpers set them).
RayHit): position, normal, distance as before, pluskind,actorKey(the actor whose body itis, for
actors.list({ key }); for a mod withactors.read) andinside(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,
TryLockMutexfrom 0). If a writer (the physicsstep, 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
nullfor that frame. The game has no try-lock of its own, so thisis the only way to never block; the release is the game's own unlock.
Not offered, and why (docs/dev/reference/world.md):
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.
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), thentry_read,acquire,classifyandcastinhost/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, throughdev.py game-tests, onmod-api@8da9fe53with this change (and the other open mod-api PRs, which loading a saveneeds):
kind: "ground"); Link not hit by default, hitwith
player: trueand named by his key, passed through withignoreset to his key,insidefrom within him; aKeese spawned in front of him hit as
characterwith its key, passed withcharacters: false,rayAlldownthrough it giving
[character, ground]; a door anobjectwith its key; at Lake Hylia 14 of 14 rays stopping on thewater's surface as
water, and on the lake bed withwater: false.HOS_RAY_TRACE=1: more than 100,000 casts in Link's house, Kokiri Forest, Hyrule Field and LakeHylia, across map changes and through loads. No stall, no crash.
tests/game-features/worldin the game: 3 passed (the new check: the floor at Link's feet), therest fake-only; with
HOS_WORLD_RAY=0the mod sees no ray, its test skips, the others pass.tests/game-features/in the fake game (19 areas, including the new kinds andrayAlltest);tests/mod_settings_fake.mjs;modapi/generate.py --check; the switch table's checks for the new rows.Still to test:
mise run test, includingtests/test_modrt.py(its stand-in game'sgame_world_cast, with a floor, waterand a character; new checks for kinds, keys,
ignore,rayAll, and a mod withoutactors.read). Not run here: thefixture 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,400requests in Kokiri Forest and Lake Hylia, 4 rays a frame of stress on top).
raywith its kindnull(busy past the sleep)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
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.
lock and its condition-variable wait, or a low-priority reader mid-cast. Both are short, and neither waits for the
main thread.
slots, the filter's vtable). Any other build keeps
has("world.ray")false.actorKeycan be null for an object of several bodies hit on a part the host doesn't know (its kind is stillright): seen once in Link's house.
generate.pynow emits the list type for a nullable list result (list<RayHit>?); no othergenerated output changes.
tests/test_mod_host_features.pydidn't compile onmod-api(
hos_game_is_mkwundeclared in its cut ofservices_game.c); it gets the stub here and still fails onmod-apifor
files.*andcontent.change(10 features no cut function answers), unchanged.tests/test_switch_table.pyfails on
mod-api's order of a fewRX_VK_*rows; the newHOS_rows are in order and match their reads.Checklist
Ready for review
mod-api; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmod-apiis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
gpu/vk/switches.def; its default ships on (the kill switch'son).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)Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
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]>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.
Pull request closed