WIP: feat(api): set, hold and speed up the game's clock, and follow its hours #3282

Draft
dyst wants to merge 2 commits from dyst/Longshot:feat/world-clock-api into main
Member

Summary

A mod can now set the game's clock, stop it, make it run slower or faster, and hear each hour as it passes. Until now the API could only read the time of day (state().timeOfDay).

  • world.setTime(hours): sets the clock. It uses the game's own clock setter, so it behaves the same as the game setting its time.
  • world.holdTime(true|false): stops the clock, or lets it run again. It does this by holding the speed at 0, not by setting the clock back every game minute.
  • world.setTimeScale(scale) and world.timeScale(): how fast the time of day runs. 1 is the game's own speed, 0.2 a day five times as long, 0 stopped, at most 60. Each mod sets its own speed and the clock runs at their product. A hold stops the clock whatever the speeds. Both end when the mod stops.
  • game.hourPassed: one event for each whole hour that passes, in order, also when several pass at once (the Sun's Song, a load that took time, a setTime), with jumped set then.
  • A content flag can be bound to hours of the clock (content.flag({ bind: { hours: [from, to] } })). A placement's when and a schedule's state then follow the clock with no script, also while a map loads.

How the speed works: the game adds 2 * step game seconds to the time of day each frame, and main's site frame.wholeTimeOfDay already cuts that to whole seconds. The speed multiplies that amount at the site. At 1 the game's own code path runs unchanged. At any other speed the fractions are carried, because a slower day's share of a frame is under a second (0.4 at a fifth at 60 fps), and the game's cut would round it to 0. There is no new hook site and no builder change.

This is also what a Day length setting can be built on: a system mod on world.setTimeScale, as a follow-up to #2413 (written for the old C side).

Read first: game/crates/game-core/src/video.rs (on_frame_whole_time_of_day, time_of_day_whole, set_clock_rate), then game/crates/api/src/services/world.rs (set_time, hold_time, set_time_scale, clock_step, hours_passed) and game.rs's hourPassed. The schema is in game/modapi/schema/world.toml and game.toml. The docs under docs/modding/ are generated from it.

See #2413.

Testing

Tested: Windows 11, RTX 3080, NVIDIA 621.47, hidden window (dev.py game-tests), over a base of main f359e2621 (before this branch was rebased onto main de6fb7b5d; the rebase changed generated docs only, and moved hourPassed to order 32 because main's pressed event now takes 31). A test mod read the clock while it set speeds, from a save at Hyrule Castle's gate (castle-gate, with the field's arrival flag set so the clock runs):

  • At speeds 1, 0.25, 2, 3 and 60, the clock ran within 1% of 2 game minutes a second times the speed (at 60: 119.7).
  • A speed of 0 and holdTime(true) stopped it, and holdTime(false) let it run again.
  • setTime jumped the clock, and hourPassed named each hour of the jump, with jumped set.
  • A speed out of range threw ArgumentError.

Checks: cargo test of game-core, api, content, schema, schema-gen and modtool, cargo clippy -D warnings, cargo fmt --check and schema-gen --check pass on the head. New unit tests cover the speed at 1, 0.2, 0, 3 and at 144 fps, the clamping, and hours passed over midnight and by a jump.

Still to test:

  • Linux (any GPU): the same speeds and holdTime, expecting the same clock rates (by hand).
  • Windows: a content flag bound to hours, with a placement's when on it, expecting the placement to appear and go at those hours with no script (by hand).

Measurements

Nothing to measure: with no mod using the clock, each frame does one relaxed atomic load at frame.wholeTimeOfDay (then the game's own path) and one check of the running content mods for flags bound to hours, which is empty.

Risks

  • A mod could make the clock run wrong (stopped, or 60 times as fast) while it runs. The speed and the hold end when the mod stops, and the clock is the game's own after that.
  • A flag bound to hours is set by the API every frame it disagrees with the clock, so a script that sets the same flag the other way loses.
  • Nothing changes for players without mods: no saves, settings or caches. A revert is enough.

Checklist

Ready for review

  • One topic, on current main; 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 main is named). The crates' cargo checks pass; the whole suite has not run on this head yet.
  • 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

  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. Windows only so far.
  • Docs updated.
  • 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 A mod can now set the game's clock, stop it, make it run slower or faster, and hear each hour as it passes. Until now the API could only read the time of day (`state().timeOfDay`). - `world.setTime(hours)`: sets the clock. It uses the game's own clock setter, so it behaves the same as the game setting its time. - `world.holdTime(true|false)`: stops the clock, or lets it run again. It does this by holding the speed at 0, not by setting the clock back every game minute. - `world.setTimeScale(scale)` and `world.timeScale()`: how fast the time of day runs. 1 is the game's own speed, 0.2 a day five times as long, 0 stopped, at most 60. Each mod sets its own speed and the clock runs at their product. A hold stops the clock whatever the speeds. Both end when the mod stops. - `game.hourPassed`: one event for each whole hour that passes, in order, also when several pass at once (the Sun's Song, a load that took time, a `setTime`), with `jumped` set then. - A content flag can be bound to hours of the clock (`content.flag({ bind: { hours: [from, to] } })`). A placement's `when` and a schedule's state then follow the clock with no script, also while a map loads. How the speed works: the game adds `2 * step` game seconds to the time of day each frame, and main's site `frame.wholeTimeOfDay` already cuts that to whole seconds. The speed multiplies that amount at the site. At 1 the game's own code path runs unchanged. At any other speed the fractions are carried, because a slower day's share of a frame is under a second (0.4 at a fifth at 60 fps), and the game's cut would round it to 0. There is no new hook site and no builder change. This is also what a Day length setting can be built on: a system mod on `world.setTimeScale`, as a follow-up to #2413 (written for the old C side). Read first: `game/crates/game-core/src/video.rs` (`on_frame_whole_time_of_day`, `time_of_day_whole`, `set_clock_rate`), then `game/crates/api/src/services/world.rs` (`set_time`, `hold_time`, `set_time_scale`, `clock_step`, `hours_passed`) and `game.rs`'s `hourPassed`. The schema is in `game/modapi/schema/world.toml` and `game.toml`. The docs under `docs/modding/` are generated from it. See #2413. ## Testing **Tested:** Windows 11, RTX 3080, NVIDIA 621.47, hidden window (`dev.py game-tests`), over a base of main f359e2621 (before this branch was rebased onto main de6fb7b5d; the rebase changed generated docs only, and moved `hourPassed` to order 32 because main's `pressed` event now takes 31). A test mod read the clock while it set speeds, from a save at Hyrule Castle's gate (`castle-gate`, with the field's arrival flag set so the clock runs): - At speeds 1, 0.25, 2, 3 and 60, the clock ran within 1% of 2 game minutes a second times the speed (at 60: 119.7). - A speed of 0 and `holdTime(true)` stopped it, and `holdTime(false)` let it run again. - `setTime` jumped the clock, and `hourPassed` named each hour of the jump, with `jumped` set. - A speed out of range threw `ArgumentError`. Checks: `cargo test` of `game-core`, `api`, `content`, `schema`, `schema-gen` and `modtool`, `cargo clippy -D warnings`, `cargo fmt --check` and `schema-gen --check` pass on the head. New unit tests cover the speed at 1, 0.2, 0, 3 and at 144 fps, the clamping, and hours passed over midnight and by a jump. **Still to test:** - Linux (any GPU): the same speeds and `holdTime`, expecting the same clock rates (by hand). - Windows: a content flag bound to hours, with a placement's `when` on it, expecting the placement to appear and go at those hours with no script (by hand). ## Measurements Nothing to measure: with no mod using the clock, each frame does one relaxed atomic load at `frame.wholeTimeOfDay` (then the game's own path) and one check of the running content mods for flags bound to hours, which is empty. ## Risks - A mod could make the clock run wrong (stopped, or 60 times as fast) while it runs. The speed and the hold end when the mod stops, and the clock is the game's own after that. - A flag bound to hours is set by the API every frame it disagrees with the clock, so a script that sets the same flag the other way loses. - Nothing changes for players without mods: no saves, settings or caches. A revert is enough. ## Checklist ### Ready for review - [x] One topic, on current `main`; 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 `main` is named). The crates' cargo checks pass; the whole suite has not run on this head yet. - [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 - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. Windows only so far. - [x] Docs updated. - [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)
world.setTime and holdTime (the game's own set of its clock), game.hourPassed (each whole hour, those of a jump and of a
load too), and a content flag bound to hours of the clock (bind: { hours }), which a placement's when and a schedule's
state follow with no script, while a map loads too.

Ported onto main from the API extensions stack without its maps commit (travelTo and exits stay there).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
feat(api): how fast the time of day runs (world.setTimeScale, timeScale), holdTime as a speed of 0
Some checks are pending
PR description / Title and description (pull_request_target) Waiting to run
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Blocked by required conditions
CI / What the change needs (pull_request) Waiting to run
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Blocked by required conditions
CI / Third-party parts and guide screenshots (pull_request) Waiting to run
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Blocked by required conditions
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Blocked by required conditions
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
6fcea6ec95
The game's own step of the time of day is multiplied at the site frame.wholeTimeOfDay, which already cuts it to whole
game seconds: 1 the game's own speed (its code path unchanged), 0.2 a day five times as long, 0 stopped, at most 60. Off
1, the fractions are always carried, as a slower day's share of a frame at 60 fps (0.4 at a fifth) would be cut to 0.
Each mod sets its own speed and the clock runs at their product; a hold stops it whatever the speeds, in place of
setting the clock back every game minute; both end with the mod's stop.

No new site and no builder change.

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

Tested in game: Windows 11, RTX 3080, NVIDIA 621.47, dev.py game-tests from a save at Hyrule Castle's gate, over a base of main f359e2621 (before the rebase onto de6fb7b5d, which changed generated docs and hourPassed's order only): speeds 1, 0.25, 2, 3 and 60 ran the clock within 1% of 2 game minutes a second times the speed; 0 and holdTime stopped it; setTime jumped it and hourPassed named each hour of the jump; a speed out of range threw ArgumentError.

Tested in game: Windows 11, RTX 3080, NVIDIA 621.47, dev.py game-tests from a save at Hyrule Castle's gate, over a base of main f359e2621 (before the rebase onto de6fb7b5d, which changed generated docs and hourPassed's order only): speeds 1, 0.25, 2, 3 and 60 ran the clock within 1% of 2 game minutes a second times the speed; 0 and holdTime stopped it; setTime jumped it and hourPassed named each hour of the jump; a speed out of range threw ArgumentError.
Some checks are pending
PR description / Title and description (pull_request_target) Waiting to run
CI / Game-free checks (run_all.py, part 2/2) (pull_request) Blocked by required conditions
CI / What the change needs (pull_request) Waiting to run
CI / Lint (pre-commit, ShellCheck, actionlint) (pull_request) Blocked by required conditions
CI / Third-party parts and guide screenshots (pull_request) Waiting to run
CI / Rust (rustfmt, clippy, cargo test, end to end) (pull_request) Blocked by required conditions
CI / Host C for Windows (llvm-mingw, compile only) (pull_request) Blocked by required conditions
CI / Renderer build check (Windows, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Renderer build check (Linux, compile --only renderer, without and with FSR 4) (pull_request) Blocked by required conditions
CI / Game-free checks (run_all.py, part 1/2) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
This pull request has changes conflicting with the target branch.
  • docs/modding/permissions.md
  • docs/modding/reference.md
  • docs/modding/scripting.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'feat/world-clock-api:dyst-feat/world-clock-api'
git switch 'dyst-feat/world-clock-api'
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!3282
No description provided.