# game/workpieces/shapes
The four workpieces as voxel bodies, built from the reference pictures.
`build(def, seed)` takes one entry of `Config.workpieces.types` and answers
`{ nx, ny, nz, cells, tones, solid, kind, material }`, where `cells` is a flat
array indexed `(x * ny + y) * nz + z + 1` holding a tone index and `tones` is
the palette those indices name.
- `pine` — a rough-sawn 40 cm block: straight grain up the sides, sawn end
grain with a ring pattern on top, one knot on the front face, corners
knocked off, a couple of pencil marks.
- `log` — a bark-stripped trunk lying along X: round section that swells and
knots, bark ribbons down the upper flank, sawn ends, a fresh notch on top.
- `ice` — a 40 cm cold blue-green block: cloudy fractures, a refracted core,
frosted top course, chipped arrises.
- `plank` — an 80 × 8 × 14 cm sawn oak plank: lengthwise grain streaks, dark
edges, pale sawn ends.
Every build is seeded, so the same seed gives the same piece.
# config
The game's numbers and rules, in one table.
Nothing in it runs. Every other part of Chainsaw Simulator reads it: the
scene build (`scenes/main.scene/build.luau`), the yard, the saw, the
workpieces, the HUD and the audio. If a number decides how the game feels —
walk speed, how fast the chain eats ice, how long the engine takes to stall,
what the customers ordered — it lives here and nowhere else.
## What it holds
- `title` — the game's name.
- `yard` — the fenced square's size, where the operator arrives, the work
zone, the sawbuck height, and where the two sodium lamp posts stand.
- `player` — eye height and field of view.
- `saw` — the engine's whole behaviour: the idle and rev RPM band, how fast it
spins up and down, the heat rates for gassing and for cutting, the stall,
and the bar's reach and bite radius.
- `workpieces` — the four things you cut, in metres and voxel counts, with how
fast each material gives way and whether it shatters.
- `orders` — the eight orders that make a shift.
- `palette` — the reference's colours as hex, so the HUD, the screens and the
signage are one palette.
## Who calls it
`require("game.config")`. Read-only — nothing should write to the returned
table at runtime.
# game/workpieces/chips
What comes off a cut: a small tinted cube per chip, thrown from where the
chain bit, pulled down by its own weight, bounced once on the concrete and
gone in a couple of seconds.
- `burst(pos, material, amount, materialRef, floor)` — material off a bite.
- `dust(pos, material, materialRef, floor)` — the low puff pine and oak give.
- `shatter(pos, material, materialRef, floor, mass)` — a piece coming apart.
- `update(dt)` / `count()` / `clear()` — driven by the component.
Ice shears into few big cold shards, pine throws small pale splinters, oak
drops dark chips. Live chips are capped (oldest go first) and spawned on a
per-frame budget, so a long cut cannot flood the scene.
# shift
The shift's live state, and the rules that move it.
This is the one table every system shares, and the reason the game's parts do
not reach into each other. The saw writes its engine readout into it, the
workpieces report a cut into it, the HUD reads it and calls the two functions
that start and restart the shift, the audio reads the RPM. Nobody needs a
handle on anybody else's component.
## State
`Shift.state` holds the phase (`title`, `working`, `payslip`), which order is
on the card and how many of its cuts are in, the shift totals and pay, and two
blocks of live readout:
- written by the saw every frame — `rpm`, `heat`, `gassed`, `stalled`,
`cutting`, `cuttingMaterial`
- written by the workpieces — `pieceKey`, `pieceRemaining`
## Functions
- `Shift.order()` / `Shift.nextOrder()` — the order on the card, and the one
after it.
- `Shift.begin(now)` — leave the title screen and put the first order up.
- `Shift.addCut(now)` — one piece worked past its cut fraction. Ticks the
order, completes it when its cuts are in, rolls the shift over on the last
one. Returns the order that completed, or nil.
- `Shift.reset()` — back to the top of the shift.
- `Shift.summary()` — what the pay slip prints.
## Events
`Shift.on(event, fn)` / `Shift.off` / `Shift.emit`. The events are `begin`,
`order`, `cut`, `orderdone` and `payslip`.
## Who calls it
`require("game.shift")` from the saw, the workpieces, the HUD, the audio and
the scene entrypoint.
# game/workpieces/voxelMesh
Turns a voxel grid into mesh geometry: `build(grid, blockSize, colourFor)`
answers `{ positions, indices, normals, uvs, colors }` ready for
`renderer.mesh.create` / `renderer.mesh.update`.
One quad per exposed face, CCW from outside, vertex colours per quad (the
tone plus a small occlusion shade for enclosed crevices), and UVs in metres
so a tiling material reads at a constant density on every face.
It exists because the engine's `VoxelShape` wears its mesh from a GPU compute
pass that does not compile in this build (`redefinition of params`), so a
voxel shape has correct data and no picture. This mesher is the CPU path the
voxel package ships, driven from the piece's own grid.
# game/workpieces/api
The carve seam between the saw and the pieces. The saw calls this once per
punch along its bar; nothing reaches into the other slice's entity tree.
```lua
local Api = require("game.workpieces.api")
local material = Api.carve(vector.create(x, y, z), 0.085, 1.0) -- "pine" | "ice" | "oak" | nil
```
- `carve(p, radius, bite) -> materialKey?` — one bite out of whichever piece
contains `p`. Returns the material it ate, or **nil** when the bar was in
the air. It never errors: a dead piece or a bad point answers nil.
- `pieceAt(p)` — the piece containing `p`, or nil.
- `pull(key)` — bring `pine` / `log` / `ice` / `plank` into the work zone
(what keys 1–4 do; the component registers the callback).
- `inspect()` — one row per live piece: key, material, solid voxel count,
total, removed, remaining fraction, yaw, position.
`p` is a `vector` (this engine's `type` for one) or a plain `{x, y, z}` table.
The workpieces component registers and unregisters every piece; the module
holds no entities of its own.