# gizgame/stream

Streaming: the pieces of a world that come and go about the eye. A Layer is one kind of them (a ground's chunks, a city's cells, a captured place's tiles); the scene runs every layer of its map the same way and knows none of them. Streams is the one loop that makes the nearest missing pieces, keeps what a square shows until its new piece is in, and takes a piece no square wants out of the stage and the solid world.

```gizlang
import * as Stream from "gizgame/stream";
```

## Types

### `Pieces`

```gizlang
class Pieces { at = new Map<string, Point3>(); shown = new Map<string, string>(); }
```

What a layer holds: each piece that is in by its name, at its middle, and the piece each square shows (a square of a
tiled place shows one of its levels).

- `at`: `_gizlang_sdk_collections.Map<string, gizlang_geometry.Point3>`
- `shown`: `_gizlang_sdk_collections.Map<string, string>`

### `Wanted`

```gizlang
type Wanted = { square: string; name: string; middle: Point3; first?: bool; }
```

A square about the eye and the piece it wants (NAME; "" none), at its MIDDLE. One marked FIRST comes before every
one that is not, however far: a square that shows nothing yet, before one that shows a coarser piece.

- `square`: `string`
- `name`: `string`
- `middle`: `gizlang_geometry.Point3`
- `first`: `_gizlang_sdk_core.Option<bool>`

### `Casting`

```gizlang
type Casting = { name: string; kind: string; most: i64; rig?: string; }
```

A cast a layer's beings are drawn with: NAME, of the rigged figure of kind KIND (in its RIG, or another of its
Figure looks), MOST characters a frame.

- `name`: `string`
- `kind`: `string`
- `most`: `i64`
- `rig`: `_gizlang_sdk_core.Option<string>`

### `Layer`

```gizlang
type Layer = { casts: Vec<Casting>; firm: (at: Point3) => void throws string; frame: (eye: Point3, feet: Point3) => void throws string; }
```

One kind of streamed pieces of a map. The scene makes each of CASTS behind the loading screen, calls FIRM once
before play (all that must be solid about AT is, so a walker starts on firm ground) and FRAME each frame, with the
EYE and the FEET of the being it follows.

- `casts`: `Vec<M229.Casting>`
- `firm`: `(arg0: gizlang_geometry.Point3) => void throws string`
- `frame`: `(arg0: gizlang_geometry.Point3, arg1: gizlang_geometry.Point3) => void throws string`

### `Host`

```gizlang
type Host = { game: Game; stage: Stage; streams: Streams; height: (x: f64, z: f64) => f64; cast: (name: string) => Cast | undefined; stands: (x0: f64, z0: f64, x1: f64, z1: f64) => bool; }
```

What a layer works with: the GAME, the STAGE it draws on, the one loop (STREAMS), the ground's HEIGHT, the cast
of a name (CAST: undefined until it is made) and whether what the map itself sets (its sets and solid props: Map3D
placed) STANDS in the square X0..X1, Z0..Z1: a layer builds nothing over it.

- `game`: `Ref<M201.Game>`
- `stage`: `gizlang_meshdraw.Stage`
- `streams`: `Ref<M229.Streams>`
- `height`: `(arg0: f64, arg1: f64) => f64`
- `cast`: `(arg0: string) => _gizlang_sdk_core.Option<Ref<M200.Cast>>`
- `stands`: `(arg0: f64, arg1: f64, arg2: f64, arg3: f64) => bool`

### `Streams`

```gizlang
class Streams { private game: Game; private bodied = new Map<string, i64>(); private made: i64 = 0; constructor(game: Game) { this.game = game; } run(eye: Point3, wanted: Vec<Wanted>, held: Pieces, most: i64, ready: (name: string) => bool throws string, make: (name: string, middle: Point3) => Vec<Physics.Body> throws string, leave: (name: string) => void throws string): bool throws string { let made = 0; let settled = true; let kept = new Set<string>(); let tried = 0; const until = Task.now()+WORK_MS; const before = (a: Wanted, b: Wanted): bool => { const fa = a.first ?? false; return fa !== (b.first ?? false) ? fa: groundDistance(a.middle, eye) <groundDistance(b.middle, eye); }; for (const w of wanted.toSorted(before)) { const shown = held.shown.get(w.square) ?? ""; const may = w.name !== "" && !held.at.has(w.name) && made <most && tried <most+STREAM_AHEAD && (made === 0 || Task.now() <until); if (may) { tried+ = 1; } if (may && ready(w.name)) { const bodies = make(w.name, w.middle); held.at.set(w.name, w.middle); made+ = 1; if (bodies.length> 0 && this.game.walked()) { const id = STREAMED-i64(this.bodied.size)-1-this.made; this.made+ = 1; this.bodied.set(w.name, id); let space = this.game.space(); for (const b of bodies) { space.add({ ...b, id }); } } } if (w.name !== "" && !held.at.has(w.name)) { settled = false; } if (w.name === "") { held.shown.delete(w.square); } else if (held.at.has(w.name)) { held.shown.set(w.square, w.name); kept.add(w.name); } else if (shown !== "") { kept.add(shown); } } for (const name of held.at.keys().filter(name => !kept.has(name))) { this.drop(held, name, leave); settled = false; } return settled; } drop(held: Pieces, key: string, leave: (name: string) => void throws string): void throws string { leave(key); held.at.delete(key); for (const square of held.shown.keys().filter(s => held.shown.get(s) === key)) { held.shown.delete(square); } const id = this.bodied.get(key); if (id !== undefined) { this.game.space().keep(b => b.id !== id!); this.bodied.delete(key); } } }
```

The one loop of every layer, and the solid bodies its pieces added.

- `game`: `Ref<M201.Game>`
- `bodied`: `_gizlang_sdk_collections.Map<string, i64>` — The bodies of the solid world each piece added, by the piece's name.
- `made`: `i64`

## Functions

### `squares`

```gizlang
function squares(eye: Point3, at: Point3, size: f64, reach: f64, half: f64, held: Pieces, name: (c: Point3) => string): Vec<Wanted>
```

The squares of SIZE meters (their corners on AT's grid) about EYE, each wanting the piece NAME gives it within
REACH (and within HALF of AT, when HALF is not 0); one a size farther keeps what it shows and makes nothing (no
piece comes and goes at the edge).

Effects: fail, read, write, console, io, alloc.

### `constructor`

```gizlang
constructor(game: Game)
```

The loop of GAME's streamed pieces.

Effects: fail, read, write, alloc.

## Methods

### `Streams.run`

```gizlang
run(eye: Point3, wanted: Vec<Wanted>, held: Pieces, most: i64, ready: (name: string) => bool throws string, make: (name: string, middle: Point3) => Vec<Physics.Body> throws string, leave: (name: string) => void throws string): bool throws string
```

Runs the loop for a frame, for every streamed piece (the ground's chunks, the city's cells, a tiled place's levels): each square of
WANTED shows the piece it wants. The nearest MOST missing ones that are READY are given to MAKE, which draws one and
gives the solid bodies it adds (those marked first before the others); a square keeps what it showed until its new piece is in (no hole), and a piece no
square wants any more leaves the stage, HELD and the solid world. True when every square shows what it wants and
nothing left: the same WANTED asks nothing more.

Effects: fail, read, write, console, io, alloc.

### `Streams.drop`

```gizlang
drop(held: Pieces, key: string, leave: (name: string) => void throws string): void throws string
```

Piece KEY of HELD taken away: LEAVE takes it from the stage, then it leaves HELD and its bodies the solid world.

Effects: fail, read, write, console, io, alloc.

## Constants

### `STREAMED`

```gizlang
const STREAMED: i64 = -1000000000000
```

The ids of the streamed pieces' solid bodies (Streams run) count down from here, below online's PLACED ones.

