# Racecourse — the oval and everything around it
One racecourse set, modelled in code and placed once by
`scenes/main.scene/build.luau`. It draws:
- the **apron**: one wide slab of grass the whole course sits in,
- the **turf lane**: an elliptical ring 22 m wide built from `BufferGeometry`,
mown in alternating stripes, with a crisp white line on both shoulders,
- the **rails**: white posts every couple of metres with white and crimson
horizontal bars, on the outside of the lane and on the infield side,
- the **finish**: white stripes painted across the lane at the finish line, and
the white goal post with its capped uprights and crossbar standing over it,
- the **grandstand**: a long stand outside the home straight — a concrete base,
three stepped seating tiers in cream and crimson, four rows of a faceted crowd
per tier, a colonnade, a cream slab roof and eight barrel vaults over it,
- the **infield**: low-poly pines, a pond, the tote board with a colour bar per
saddle number, and the **starting gate** with its dividers and legs.
## What it needs from the scene
Nothing but a place to stand. Placed as:
```lua
asset.resolve("Racecourse", "sceneModule"):instantiate(nil, { name = "course" })
```
## The numbers it mirrors
```
A = 92 semi-axis along X (centreline)
B = 56 semi-axis along Z
WIDTH = 22 lane width
```
These are `RaceSim.TRACK`, the ellipse the race is run on. The module declares
its own copy because a scene module compiles in its own environment and cannot
`require` a world module — **change the three numbers in both files** and the
lane, the rails, the finish and the starting gate all follow.
The finish line is at `t = 0`, world `(92, 0, 0)`; the home straight runs along
**Z** and the grandstand stands outside it at **+X**, which is the side the
broadcast camera and the goal-front camera shoot from.
# RaceDirector — one race, from the gates to the result
The component that runs ターフヴィクトリー. It owns no geometry and no screen of
its own: it drives the assets around it.
```
RaceDirector (one instance, on the entity "RaceDirector" in scenes/main.scene)
├─ RaceSim.module the rules — the oval, pace, stamina, 根性
├─ Horse1 … Horse8 Horse scene-module placements — moved every frame
├─ RaceCam the one camera, moved per mode
├─ HorseRaceUI.module the start screen, the HUD and the result screen
└─ RaceAudio.module the beds, the stings and the spoken commentary
```
Place it in a scene with:
- the eight horse placements named `Horse1` … `Horse8`,
- an entity called `RaceCam` carrying a `Camera` (any `fov`, high `priority`)
and an `AudioListener`,
- `HorseRaceUI` and `RaceAudio` present as modules.
## What it does
1. **start** — registers the UI screen and shows the start screen. The player
picks a 作戦 (`plan:nige` … `plan:oikomi`), which sets `public.plan`.
2. **countdown** — `start` begins a 3.4 s countdown with the field at the gates.
3. **running** — every frame: `RaceSim.step(race, dt)`, then each horse's
position and yaw from `RaceSim.pose(h.dist, h.lane)`, the camera for the live
mode, commentary from the race's events (`lead`, `final`, `drain`, `finish`),
and a HUD refresh at about 8 Hz. The crowd bed rises with how tightly packed
the field is.
4. **result** — placings and times, the prize for the placing, the career record
updated, and the horse sharpened a little for a top-three finish. `again`
(or the next `start`) runs a fresh race with the same horse.
## Cameras
`public.cameraMode` — `broadcast`, `chase`, `overhead`; the HUD's buttons send
`cam:broadcast`, `cam:chase`, `cam:overhead`.
- **broadcast** — a trackside camera outside the oval on the pack's own normal,
tracking it, and once the leader turns into the last 200 m it cuts to a low
goal-front angle looking back down the straight.
- **chase** — behind and just off the player's horse, low and fast.
- **overhead** — high above the pack, following it round the oval.
The camera is a single entity whose position is written each frame and whose
rotation is built as a yaw-then-pitch quaternion (the engine's models face +Z),
so no second camera and no camera behaviour is involved.
## Fields
| field | meaning |
|-------|---------|
| `public.plan` | the player's 作戦 for the next race |
| `public.cameraMode` | which camera is live |
The career (runs, wins, earnings, best time) lives in the component's own state
and survives a race, not an engine restart.
# Horse — one low-poly racehorse and its jockey
A scene module that builds a single horse, faceted and chunky, facing **+Z**:
the barrel, chest and rump as low-segment spheres, a tapered neck, a box skull
with a blaze, ears and eyes, a mane and a tail, four legs of three segments
each, and a jockey in silks — saddle cloth, breeches, vest, arms, gloves,
thighs, boots, helmet, goggles and face.
`scenes/main.scene/build.luau` places eight of them, one per saddle number, at
the starting gates.
## The gallop
There is no animation clip and no per-frame script. The four legs, the tail,
the body's bob and the jockey's rise are all **formulas of `Time`** written with
`Expr.sin`, which the engine evaluates on the device every frame:
```lua
leg.rotation.x = Expr.sin(Time * stride + phase + L.off) * 0.62
body.position.y = Expr.sin(Time * stride * 2 + phase) * 0.045
```
`phase` is an input, so the eight horses are not in step, and `L.off` puts the
front and back legs of one horse out of step with each other the way a gallop
runs. Because the pose is a formula, `RaceDirector` only ever moves the
placement — the legs keep swinging for free on every horse at once.
`math.sin(Time …)` does **not** work here: `Time` is a formula, not a number, and
the build context's trig functions are `Expr.sin` and `Expr.cos`.
## Inputs
| input | default | meaning |
|-------|---------|---------|
| `number` | 1 | the horse's saddle number (part names carry it) |
| `body` | `#a4571f` | coat colour |
| `silk` | `#c0392b` | the jockey's silks and the saddle cloth |
| `cap` | `#f2f2f2` | the jockey's helmet |
| `phase` | 0 | stride offset in radians, so eight horses do not move as one |
Set them per placement with `:instantiate(nil, { params = { … } })`.
## Notes
- A `:round(r)` on a face thinner than about `2r` fails and the part is silently
not drawn — the blaze and the number plate are deliberately unrounded.
- Every part is a shared geometry drawn as an instance, so eight of these cost a
handful of draws.
- The proportions come from `design/references/03-player-horse.png`: 1.6 m at
the withers, 2.4 m from chest to tail, jockey sitting at 2.3 m.
# RaceSim — the rules of a turf race
The whole of the game's logic, with nothing drawn and nothing scheduled. Every
other piece of the game reads it:
- `RaceDirector.component` steps it every frame and places the horses from it.
- `HorseRaceUI.module` draws the standings, clock and times it produces.
- `Racecourse.sceneModule` and `scenes/main.scene/build.luau` mirror its
`TRACK` numbers so the horses are always on the turf they model.
## The oval
```lua
RaceSim.TRACK = { A = 92, B = 56, WIDTH = 22, RAIL_OUT = 12.4, RAIL_IN = 11.6, SAMPLES = 720 }
```
An ellipse in the XZ plane. Distance is measured in **metres run from the
finish line**, and the finish line sits at `t = 0` — the point of least
curvature, world `(92, 0, 0)` — so the home straight runs along **Z** and the
stands stand outside it at **+X**. A model on the track faces **+Z**.
```lua
RaceSim.pose(distance, lane) -> x, z, yaw, nx, nz
```
`lane` is `-1` (inner) through `1` (outer); `yaw` turns a +Z-facing model down
the track; `nx, nz` is the outward normal, which is how the broadcast camera
finds the outside of the rail. `RaceSim.LENGTH` is the oval's circumference in
metres (about 472).
## The field
`RaceSim.FIELD` is the eight horses: `number`, `name`, coat `body`, `silk` and
`cap` colours, a preferred `style`, and the three hidden parameters
`speed` / `stamina` / `guts` (0–100). Horse 1 is the player's, `ヒカリノサクラ`.
`RaceSim.STYLES` are the four 作戦 — `nige` 逃げ, `senko` 先行, `sashi` 差し,
`oikomi` 追込 — with `STYLE_NAME` and `STYLE_NOTE` for the screen.
## One race
```lua
local race = RaceSim.newRace(plan, career) -- plan overrides horse 1's style
local events = RaceSim.step(race, dt) -- { kind = "lead"|"final"|"drain"|"finish", ... }
local rows = RaceSim.standings(race) -- place, number, name, colour, gap, finished, time
```
`step` is where the race is decided:
- **Pace** — each style keeps its own multiplier through 序盤 / 中盤 / 終盤
(`PACE`, ×0.92 to ×1.09 of the horse's own top speed). Front runners go early,
差し and 追込 keep something for the last stretch.
- **Stamina** — drains with `(v/16)^3`, so a horse that runs faster empties it
sooner; a full tank lasts `38 + 0.62 × stamina` seconds at racing speed. Below
14% the horse fades toward ×0.78 — that is 失速, and it happens in the last
200 m to the horses that were asked too much too early.
- **根性** — every rival within 5 m adds up to ×1.03, scaled by `guts/100` and by
how late the race is, so horses that are level in the straight fight each other
harder than horses alone.
`RaceSim.FINAL` is the last 200 m. `RaceSim.formatTime` writes `m:ss.cc`,
`RaceSim.prize` pays a placing in 万円, `RaceSim.colourOf` / `nameOf` look a
horse up by number.
## Changing the track
Change `A`, `B` and `WIDTH` here and change the same three numbers in
`Racecourse.sceneModule` (it cannot `require` this module — a scene module
compiles in its own environment) and the scene's start gate follows. Everything
else — distances, pace, finish times — works in metres and needs no edit.