# gizgame/scene3d

3D: a game drawn from its kinds' figures (Kind.figure) over the world a Scene3D declares.

```gizlang
import * as Scene3d from "gizgame/scene3d";
```

## Types

### `Scene`

```gizlang
class Scene { game: Game; spec: Scene3D; private light: (game: Game, time: f64) => Environment; stage: Stage; sprites: Sprites; camera: Camera = { eye: point3(0, 10, 10), target: point3(0, 0, 0), fov: 1.05, near: 0.1, far: 300 }; private plain: Camera = { eye: point3(0, 10, 10), target: point3(0, 0, 0), fov: 1.05, near: 0.1, far: 300 }; private wide: f64 = 1.0; level: (x: f64, z: f64) => f64; private chase: Chase; private standing: Vec<Fixed> = []; private stoodAt = ""; private stoodKinds = new Set<string>(); private built: Vec<Fixed> = []; private lamps: Vec<PointLight> = []; private suns: Vec<f64> = []; private own: Vec<PointLight> = []; private look: Grade | undefined = undefined; private heaven: Gltf.Heaven | undefined = undefined; private haze: Haze | undefined = undefined; private volume: Volume | undefined = undefined; private shots: Vec<Camera> = []; private graded: Vec<Grade | undefined> = []; private chunks = new Pieces(); private streams: Streams; private layers: Vec<Layer> = []; private soil: Vec<Maps> = []; private feet: Vec<Block> = []; private swaying: Vec<Prop3D> = []; private casts = new Map<string, Cast>(); private rigs = new Map<string, Gltf.Rig>(); private coming = new Set<string>(); private placed: Vec<Fixed> = []; private cleared: Vec<WorldEdit> = []; private glows: Vec<Point3> = []; private done: i64 = 0; private back: f64 = 1; private within: i64 = -1; private stood = new Map<string, Point3>(); private seats = new Map<i64, Vec<Seated>>(); private sat = new Map<i64, i64>(); private shot: i64 = -1; private blades: Vec<Blade> = []; private met = new Set<string>(); private bladed = new Map<string, Blade>(); private clashed = new Map<i64, f64>(); private cue: Entity | undefined = undefined; private sided: f64 = 0.0; private lines: i64 = 0; private sunward: Point3 = point3(0, -1, 0); private left: { camera: Camera; at: f64; } = { camera: { eye: point3(0, 0, 0), target: point3(0, 0, -1), fov: 1, near: 0.1, far: 100 }, at: -1.0e9 }; private gazes = new Map<string, { at: Point3; weight: f64; time: f64; }>(); private worked = new Map<string, { k: f64; time: f64; }>(); private views: Vec<Follow> = []; weather: string = "clear"; constructor(game: Game, resources: Resources, screen: Screen, given: Scene3D) throws string { const spec: Scene3D = { ...given, map: staged(given.map) }; const world = worldOf(spec); let stage = new Stage(resources, game.phone() ? 512: SHADOW_SIDES[game.settings.shadows], game.phone() && screen.features.includes(LEAN_FEATURE) ? LEAN: HDR); const base = heightOf(world) ?? (game.design.three ? game.design.floor: (x, z): f64 => 0); const online = spec.online; const level = online === undefined ? base: (x: f64, z: f64): f64 => online!.ground(x, z, base(x, z)); this.light = lightOf(spec); this.game = game; this.streams = new Streams(game); this.spec = spec; this.stage = stage; this.sprites = new Sprites(faces(game), 2048, 6000, (code): Lent | undefined => game.glyph(code)); this.level = level; game.settling = (graph: Graph): f64 throws string => stage.settle(graph); this.chase = new Chase(spec.map.eye?.rig ?? { back: 5.5, up: 2.2, ahead: 2, lift: 1.6, stiffness: 9 }, level); if (online !== undefined) { const all = online!.world; for (const [i, e] of all.entries()) { if (e.kind === "map") { this.done = i+1; } } } let ahead: Vec<string> = []; let models: Vec<string> = []; for (const id of spec.map.ground?.materials ?? []) { ahead = [...ahead, ...Image.materialFiles(id, game.textureWork)]; } for (const t of spec.map.things ?? []) { if (t.file === undefined) { continue; } ahead.push(Gltf.ahead(t.file!, game.textureWork)); if (t.skin !== undefined) { ahead = [...ahead, ...this.skinFiles(t.skin!).filter(n => n !== "")]; } else if (t.file!.endsWith(".gltf")) { models.push(t.file!); } } for (const s of spec.map.sets ?? []) { ahead.push(Gltf.ahead(s.file, game.textureWork)); } for (const named of game.design.kinds) { if (named.kind.figure?.rig !== undefined) { ahead.push(Gltf.ahead(named.kind.figure!.rig!, game.textureWork)); } } game.ask(ahead); game.later((): void throws string => { const read = this.game.reader(); let beside: Vec<string> = []; for (const m of models) { beside = [...beside, ...Gltf.needs(m, read(m), this.game.textureWork)]; } this.game.ask(beside); }); for (const id of spec.map.ground?.materials ?? []) { game.later((): void throws string => { this.soil.push(Image.materials([id], this.game.reader(), this.game.textureWork)[0]); }); } const host: Host = { game, stage, streams: this.streams, height: level, cast: (name: string): Cast | undefined => this.casts.get(name), stands: (x0: f64, z0: f64, x1: f64, z1: f64): bool => reaches(this.feet, x0, z0, x1, z1) }; if (spec.map.ground?.chunk !== undefined) { this.layers.push({ casts: [], firm: (at: Point3): void throws string => { return; }, frame: (eye: Point3, feet: Point3): void throws string => this.stream(eye) }); } const city = spec.map.city; if (city !== undefined) { this.layers.push(City.layer(city!, host)); } game.later((): void throws string => this.soiled()); for (const t of spec.map.things ?? []) { game.later((): void throws string => this.thing(t)); } const capture = spec.map.capture; if (capture !== undefined) { this.layers.push(Tiles.layer(capture!, host, (file: string): void throws string => this.set({ file: file }, 0))); } const sets = spec.map.sets ?? []; for (const [at, s] of sets.entries()) { const times = sets.filter(o => o.file === s.file).length; const first = sets.findIndex(o => o.file === s.file) === at; game.later((): void throws string => this.set(s, first ? times: 0)); } if (game.walked()) { game.solidBy((): Physics.Space throws string => solid(game, world, false)); if (spec.map.sea !== undefined) { const d = game.design; d.floor = (x: f64, z: f64): f64 => Math.max(0, d.land(x, z)); } else if (spec.map.ground?.chunk !== undefined) { const height = this.level; game.design.floor = (x: f64, z: f64): f64 => height(x, z); } game.later((): void throws string => { for (const layer of this.layers) { layer.firm(spec.map.start ?? point3(0, 0, 0)); } }); game.later((): void throws string => { const space = this.game.space(); this.chase.clear = (from: Point3, to: Point3): f64 throws string => { const way = sub(to, from); const far = length(way); if (far <1e-6) { return 1.0; } const hit = space.ray(from, scale(way, 1/far), far+0.3); return hit === undefined ? 1.0: Math.max(0.05, (hit!.distance-0.3)/far); }; }); } game.later((): void throws string => this.sprayed()); this.cast(); for (const named of game.design.kinds) { for (const e of named.kind.figure?.clips?.emotes ?? []) { game.voiced(e.say ?? []); } } game.later((): void throws string => this.propped()); } private soiled(): void throws string { const spec = this.spec; const gr = spec.map.ground; if (gr !== undefined && gr!.chunk === undefined) { const at = gr!.at ?? point3(0, 0, 0); this.standing.push({ kind: "ground", model: transform(at, euler(0, 0, 0), point3(gr!.side, 1, gr!.side)), at }); } if (spec.map.sea !== undefined) { const water: Material = { color: spec.map.sea!, metallic: 0, roughness: 0.08, emissive: [0, 0, 0], alphaMode: "BLEND" }; this.stage.add("sea", plane(), water, 1, false); this.standing.push({ kind: "sea", model: transform(point3(0, 0, 0), euler(0, 0, 0), point3(900, 1, 900)), at: point3(0, 0, 0) }); } } private skinFiles(s: Skin): Vec<string> { return [s.color, s.roughness ?? "", s.normal ?? ""].map((n: string, k: i64): string => n === "" ? "": Image.mapName(n, k === 0, k === 2, this.game.textureWork)); } private thing(t: Thing3D): void throws string { const read = this.game.reader(); const work = this.game.textureWork; if (t.file !== undefined && t.file!.endsWith(".ply")) { this.stage.splats(t.name, Ply.read(read(t.file!)), t.most ?? 16); return; } if (t.file !== undefined) { const file = t.file!; const own = (path: string): bytes throws string => t.skin === undefined || path === Gltf.named(file, work) || path.startsWith(`${Gltf.named(file, work) }@`) ? read(path): b""; let parts = Gltf.model(file, own, work); if (t.skin !== undefined) { const names = this.skinFiles(t.skin!); let asked: Vec<Image.Request> = []; for (const k of range(0, 3)) { if (names[k] !== "") { asked.push({ file: read(names[k]), srgb: k === 0, normal: k === 2, blocks: work.blocks }); } } const made = Image.textureBatch(asked, work); let found: Vec<Texture | undefined> = []; let at = 0; for (const n of names) { if (n === "") { found.push(undefined); } else { found.push({ ...made[at], name: n }); at+ = 1; } } const maps: Maps = { color: found[0], metalRough: found[1], normal: found[2] }; parts = parts.map(p => ({ ...p, maps })); } const down = this.game.phone() && parts.some(p => (p.levels ?? []).length> 0); const drawn = down ? parts.map((p: Part): Part => { const held = p.levels ?? []; return held.length === 0 ? p: { ...p, geometry: held[0], levels: held.slice(1) }; }): parts; const given: Vec<f64> = t.far ?? []; const far = down ? given.slice(1): given; this.stage.model(t.name, t.thin === undefined ? drawn: thinned(drawn, t.thin!), t.most ?? 256, t.casts ?? true, far); return; } const maps = t.texture === undefined ? undefined: Image.materials([t.texture!], read, work)[0]; this.stage.add(t.name, t.geometry ?? box(), t.material ?? matte(0.5, 0.5, 0.5), t.most ?? 64, t.casts ?? true, maps); } private set(s: Set3D, times: i64): void throws string { const found = Gltf.scenery(s.file, this.game.reader(), this.game.textureWork); const base = setBase(s, this.game, levelOf(worldOf(this.spec))); let counts: Vec<i64> = []; for (const kind of found.kinds) { counts.push(0); } for (const p of found.places) { counts[p.kind]+ = 1; } for (const k of range(0, found.kinds.length)) { if (times> 0 && counts[k]> 0) { this.stage.model(`${s.file }#${ k }`, found.kinds[k], counts[k]*times, true, s.far ?? []); } } for (const p of found.places) { const model = multiply(base, p.model); const box = found.kinds[p.kind][0].geometry.bounds; this.built.push({ kind: `${s.file }#${ p.kind }`, model, at: transformed(model, scale(add(box.low, box.high), 0.5)) }); } for (const lamp of found.lamps) { const at = transformed(base, lamp.position); const a = lamp.aim; const aim = a === undefined ? undefined: sub(transformed(base, add(lamp.position, a!)), at); this.lamps.push({ ...lamp, position: at, aim }); this.suns.push(found.sun === undefined ? 0.0: Math.max(found.sun!.color[0], Math.max(found.sun!.color[1], found.sun!.color[2]))); } if (found.sun?.aim !== undefined) { const toward = sub(transformed(base, found.sun!.aim!), transformed(base, point3(0, 0, 0))); this.own.push({ position: point3(0, 0, 0), color: found.sun!.color, range: 0, aim: scale(toward, 1.0/Math.max(1.0e-9, length(toward))) }); } if (this.look === undefined) { this.look = found.grade; } if (this.heaven === undefined) { this.heaven = found.sky; } if (this.volume === undefined && found.volume !== undefined) { const v = found.volume!; const at = transformed(base, point3(v.origin[0], v.origin[1], v.origin[2])); this.volume = { ...v, origin: [at.x, at.y, at.z] }; } if (this.haze === undefined && found.haze !== undefined) { const f = found.haze!; this.haze = { ...f, height: transformed(base, point3(0, f.height, 0)).y }; } for (const v of found.views) { const at = transformed(base, point3(v.at[0], v.at[1], v.at[2])); const ahead = transformed(base, point3(v.at[0]+v.aim[0], v.at[1]+v.aim[1], v.at[2]+v.aim[2])); this.shots.push({ eye: at, target: ahead, fov: v.fov, near: 0.05, far: 2000 }); this.graded.push(v.grade); } } private sprayed(): void throws string { for (const s of this.game.sprayed()) { const c = s.spray.color ?? [1, 1, 1]; const glow = s.spray.glow ?? 0; if (s.spray.soft ?? false) { this.stage.splats(s.name, puff(c), s.spray.most); continue; } this.stage.add(s.name, box(), glow> 0 ? glowing(c[0], c[1], c[2], glow): matte(c[0], c[1], c[2], 0.7), s.spray.most, glow <= 0); } } private cast(): void throws string { const g = this.game; for (const e of g.each(Actor)) { const kind = g.design.kinds[g.design.index(this.shown(e))]; const f = kind.kind.figure; if (f?.rig !== undefined) { this.casting(kind.name, f!, f!.most ?? 8); } } for (const layer of this.layers) { for (const c of layer.casts) { const f = g.design.kinds[g.design.index(c.kind)].kind.figure; if (f?.rig === undefined) { continue; } const figure = f!; this.casting(c.name, c.rig === undefined ? figure: { ...figure, rig: c.rig }, c.most); } } } private shown(e: Entity): string throws string { const g = this.game; const name = g.design.kinds[g.read(e, Actor).kind].name; return this.spec.online !== undefined && g.has(e, Replica) && !g.ours(e) ? name: g.worn(name); } private casting(name: string, f: Figure, most: i64): void throws string { if (this.casts.has(name) || this.coming.has(name)) { return; } this.coming.add(name); const rig = f.rig!; const hide = f.hide ?? []; const feet = f.feet; const head = f.head; this.game.later((): void throws string => { let r = Gltf.rig(rig, this.game.reader(), this.game.textureWork).hide(hide); if (feet !== undefined) { r.feet = feet!; } if (head !== undefined) { r.head = head!; } this.rigs.set(name, r); }); this.game.later((): void throws string => this.rigged(name, f, most)); } private rigged(name: string, f: Figure, most: i64): void throws string { this.casts.set(name, new Cast(this.stage, name, this.rigs.get(name)!, most, f.clips?.idle ?? "Idle")); this.rigs.delete(name); } private propped(): void throws string { const spec = this.spec; let standing = this.standing; let swaying: Vec<Prop3D> = []; const found = placed(this.game, worldOf(spec)); this.feet = found.feet; for (const prop of found.props) { if ((prop.sway ?? 0)> 0) { swaying.push(prop); continue; } const model = prop.scale !== undefined ? transform(prop.at, euler(prop.yaw ?? 0, 0, 0), prop.scale!): this.stage.fit(prop.kind, prop.at, prop.size ?? 1, prop.yaw ?? 0, prop.lying ?? false); standing.push({ kind: prop.kind, model, at: prop.at }); } for (const f of this.built) { standing.push(f); } this.built = []; this.standing = standing; this.swaying = swaying; if (spec.online !== undefined) { this.stage.add("making", sphere(16, 10), glowing(0.45, 0.85, 1, 5), 24, false); } if (spec.map.ground !== undefined && spec.map.ground!.chunk === undefined) { this.ground(spec.map.ground!); } } figure(b: Figured, time: f64, pitch: f64 = 0, roll: f64 = 0, guarded: f64 = 0): void throws string { const design = this.game.design; const kinds = design.kinds; const kind = kinds[design.index(b.kind)].kind; const found = kind.figure; if (found === undefined) { return; } if (found.seat === undefined && !this.seen(b.at, found.size ?? TALL)) { return; } const seats = b.mount === undefined ? undefined: this.seats.get(b.mount!); const rank = b.mount === undefined ? 0: this.sat.get(b.mount!) ?? 0; if (b.mount !== undefined) { this.sat.set(b.mount!, rank+1); if (seats === undefined || rank> = seats!.length) { return; } } const seat = seats === undefined ? undefined: seats![rank]; const f = found; const phase = f64(b.slot); const yaw = seat === undefined ? b.yaw: seat!.yaw; if ((f.blinks ?? false) && guarded> 0 && Math.sin(time*30) <= -0.3) { return; } const pulse = f.pulse === undefined ? 1.0: 0.75+0.25*Math.sin(time*f.pulse!+phase); let lifted = seat === undefined ? add(b.at, point3(0, (f.bob ?? 0)*Math.sin(time*2.2+phase), 0)): seat!.at; const partner = this.game.partner(b.actor); let open = 0.0; if (partner !== undefined && length(b.velocity) <0.3 && seat === undefined) { const to = sub(this.game.position(partner!), b.at); if (to.x*to.x+to.z*to.z> 1.0e-6) { open = angleTo(yaw, yawOf(point3(to.x, 0, to.z))); } } const key = `${b.kind }#${ b.slot }`; const turned = yaw+open+Math.PI+(f.turn ?? 0); const tilt = pitch+(f.sway ?? 0)*Math.sin(time*1.7+phase); if (f.model !== undefined) { this.stage.put(f.model!, this.stage.fit(f.model!, lifted, f.size ?? 1, turned, f.lying ?? false, tilt)); } if (f.seat !== undefined && b.actor !== undefined && !this.casts.has(b.kind)) { this.seats.set(b.actor!.index, seated(f.seat!, b.at, yaw, point3(0, 0, 0))); } const cast = this.casts.get(b.kind); let played: Played | undefined = undefined; let swing = 0.0; if (cast !== undefined) { const c = f.clips ?? { idle: "Idle" }; const size = f.size ?? TALL; const fell = b.fell ?? -1; const feet = cast.rig.feet; let shown = chosen(c, b, Math.min(0.5, kind.attack?.every ?? 0.5)); let planted: Vec<Gltf.Plant> = []; let fall = 0.0; if ((b.clip ?? "") !== "") { shown = { clip: b.clip!, rate: b.rate ?? 1, once: b.once ?? false }; } let upper = b.upper ?? ""; let uppered = b.uppered ?? 0.0; if (seat !== undefined) { if (upper === "" && (shown.once || shown.clip === c.talk) && (b.clip ?? "") === "") { upper = shown.clip; uppered = shown.clip === c.talk ? time: (b.hurt ?? -1)> = 0 && shown.clip === c.hurt ? b.hurt!: b.struck ?? 0.0; } if ((b.clip ?? "") === "") { shown = { clip: c.ride ?? c.idle, rate: 1, once: false }; } } const over = upper === "" ? undefined: overOf(cast.rig, upper, uppered, upper === c.talk || upper === c.idle); if (fell> = 0 && c.fall === undefined) { fall = toppled(fell, size); } else if (seat !== undefined) { if (feet.length === 2 && seat!.stirrups.length === 2) { planted = [{ end: feet[0], to: seat!.stirrups[0] }, { end: feet[1], to: seat!.stirrups[1] }]; } const hands = f.hands ?? []; if (hands.length === 2 && seat!.reins !== undefined && over === undefined) { const across = scale(facing(yaw-Math.PI*0.5), size*0.06); planted.push({ end: hands[0], to: sub(seat!.reins!, across) }); planted.push({ end: hands[1], to: add(seat!.reins!, across) }); } } else if (feet.length === 2 && fell <0 && !(b.airborne ?? false)) { const side = scale(facing(yaw-Math.PI*0.5), size*0.07); const base = this.level(b.at.x, b.at.z); const right = this.level(b.at.x+side.x, b.at.z+side.z)-base; const left = this.level(b.at.x-side.x, b.at.z-side.z)-base; const sink = Math.min(0, Math.min(left, right)); lifted.y+ = sink; planted = [{ end: feet[0], lift: (left-sink)/size }, { end: feet[1], lift: (right-sink)/size }]; } if (b.aimed !== undefined && over !== undefined && (f.hands ?? []).length === 2) { const spans = Math.min(b.lasts ?? 1.0e9, cast.rig.length(upper)); const hands = f.hands!; const k = Math.max(0.0, Math.min(1.0, (b.uppered ?? 0.0)/Math.max(0.1, spans))); swing = Math.smoothstep(0.0, 0.2, k)*(1.0-Math.smoothstep(0.8, 1.0, k)); const chest = add(lifted, point3(0, size*0.3, 0)); const toward = sub(point3(b.aimed!.x, chest.y, b.aimed!.z), chest); const guards = b.guards ?? false; const raised = guards ? 0.0: size*0.33*(1.0-Math.smoothstep(0.2, 0.5, k)); const since = this.game.since()-(b.actor === undefined ? -1.0e9: this.clashed.get(b.actor!.index) ?? -1.0e9); const knock = since <0.0 ? 0.0: size*0.14*(1.0-Math.exp(-since/0.02))*Math.exp(-since/0.18); const contact = add(chest, scale(toward, 0.5)); const meet = add(add(contact, point3(0, raised+knock*0.6, 0)), scale(unitOf(toward), -knock)); const arm = cast.rig.limb(hands[1]); const hips = cast.rig.hipsJoint(); const rest = (j: string): Point3 => cast.rested(j, b.at, size, yaw); const shoulder = add(rest(arm[0]), sub(seat !== undefined && hips !== "" ? seat!.at: lifted, hips !== "" ? rest(hips): b.at)); const long = length(sub(rest(arm[1]), rest(arm[0])))+length(sub(rest(arm[2]), rest(arm[1]))); const out = sub(meet, shoulder); const reach = length(out) <= long*0.95 ? meet: add(shoulder, scale(unitOf(out), long*0.95)); const ahead = length(sub(meet, reach))> 1.0e-3 ? unitOf(sub(meet, reach)): unitOf(toward); planted.push({ end: hands[1], to: reach, weight: swing }); planted.push({ end: hands[0], to: sub(reach, scale(ahead, size*0.25)), weight: swing }); } const outfits = f.outfits ?? []; const t = (b.dressing ?? -1)> = 0 ? (b.dressing ?? 0.0)/DRESSING: -1.0; const wearing = outfits.length === 0 ? undefined: outfits[Math.maxI64(0, Math.minI64(outfits.length-1, t> = 0 && t <0.5 ? b.was ?? 0: b.outfit ?? 0))].wears; const dressed = t> = 0 && fell <0 && c.dress !== undefined; if (dressed) { shown = { clip: c.dress!, rate: cast.rig.length(c.dress!)/DRESSING, once: true }; } const hands = f.hands ?? []; const changing = t> = 0 && !dressed && hands.length === 2 && fell <0 ? dressingHands(b.at, yaw, size, t, hands): []; for (const p of changing) { planted.push(p); } const held = fell <0 ? this.holding(b, size): undefined; if (held !== undefined) { planted.push(held!); } const head = cast.rig.head; if (head !== "" && changing.length === 2) { planted.push({ end: head, look: scale(add(changing[0].to!, changing[1].to!), 0.5), weight: 0.5*(changing[0].weight ?? 1.0) }); } else if (head !== "" && b.actor !== undefined) { const want = attention(this.game, b.actor!, this.camera.eye); const was = this.gazes.get(key); const k = was === undefined ? 1.0: Math.eased(Math.max(0, time-was!.time), GAZING); const at = want === undefined ? (was?.at ?? this.camera.eye): was === undefined ? want!.at: lerp(was!.at, want!.at, k); const weight = (was?.weight ?? 0.0)+((want?.weight ?? 0.0)-(was?.weight ?? 0.0))*k; this.gazes.set(key, { at, weight, time }); if (weight> 0.01) { planted.push({ end: head, look: at, glance: want?.glance, weight }); } } const hips = cast.rig.hipsJoint(); const anchor: Anchor | undefined = seat !== undefined && hips !== "" ? { joint: hips, to: seat!.at }: undefined; played = cast.stand(b.slot, lifted, size, turned, shown.clip, shown.rate, time, shown.once, f.lying ?? false, tilt-fall, roll, planted, length(sub(b.at, this.camera.eye))/size*(this.game.phone() ? 2: 1), this.game.expression(b.actor, f.face ?? new Map<string, f64>()), wearing, { over, anchor }); if (f.seat !== undefined && played !== undefined && b.actor !== undefined) { this.seats.set(b.actor!.index, seated(f.seat!, b.at, yaw, seatBob(f.seat!, played!, cast.rig))); } const was = this.stood.get(key); this.stood.set(key, lifted); if (was !== undefined && !(f.lying ?? false)) { const move = sub(lifted, was!); const far = length(move); if (far> 1e-5 && far <1) { this.stage.mover(add(lifted, point3(0, size*0.5+0.06, 0)), size*0.2, size*0.3, move); } } } const body = transform(lifted, euler(yaw, tilt, roll), point3(1, 1, 1)); const working = (w: Works): bool throws string => b.actor !== undefined && this.game.held(w.action) && driven(this.game, b.actor!); for (const piece of f.pieces ?? []) { const copies = Math.maxI64(1, piece.ring ?? 1); const grow = (piece.pulses ?? false) ? pulse: 1; const turn = euler((piece.yaw ?? 0)+(piece.spin ?? 0)*time, piece.pitch ?? 0, piece.roll ?? 0); if (piece.joint !== undefined) { if (played === undefined || cast === undefined) { continue; } const held = multiply(played!.placed, cast!.rig.placeOf(piece.joint!, played!.posture)); const k = 1/Math.max(1e-9, length(sub(transformed(held, point3(1, 0, 0)), transformed(held, point3(0, 0, 0))))); let drawn = multiply(held, transform(scale(piece.at ?? point3(0, 0, 0), k), turn, scale(piece.size, grow*k))); if (piece.grip !== undefined) { const lead = transformed(held, point3(0, 0, 0)); const back = transformed(multiply(played!.placed, cast!.rig.placeOf(piece.grip!, played!.posture)), point3(0, 0, 0)); const span = length(sub(lead, back)); if (span> 0.15 && span <1.0) { const along = scale(sub(lead, back), 1.0/span); const r = between(point3(0, 1, 0), along); const o = piece.at ?? point3(0, 0, 0); drawn = transform(add(lead, rotate(r, point3(o.x, o.z, -o.y))), times(r, around(point3(0, 1, 0), piece.roll ?? 0)), scale(piece.size, grow)); } } this.stage.put(piece.shape, drawn); if ((piece.blade ?? false) && b.actor !== undefined && swing> 0.2) { this.blades.push({ actor: b.actor!, from: transformed(drawn, point3(0, -0.5, 0)), to: transformed(drawn, point3(0, 0.5, 0)), stroke: b.stroke ?? 0.0 }); } continue; } let at = piece.at ?? point3(0, 0, 0); let bent = piece.pitch ?? 0; for (const w of piece.works ?? []) { const part = `${key }#${ w.action }`; const was = this.worked.get(part); const now = was !== undefined && was!.time === time ? was!.k: Math.max(0.0, Math.min(1.0, (was?.k ?? 0.0)+(working(w) ? 1.0: -1.0)*(was === undefined ? 0.0: Math.min(0.25, time-was!.time)*3.0))); this.worked.set(part, { k: now, time }); const worn = w.pitch*Math.smoothstep(0.0, 1.0, now); if (w.pivot !== undefined) { const o = w.pivot!; const dy = at.y-o.y; const dz = at.z-o.z; const c = Math.cos(worn); const sn = Math.sin(worn); at = point3(at.x, o.y+dy*c-dz*sn, o.z+dz*c+dy*sn); } bent+ = worn; } const mirrored = euler(-((piece.yaw ?? 0)+(piece.spin ?? 0)*time), -bent, piece.roll ?? 0); for (const k of range(0, copies)) { const around = transform(point3(0, 0, 0), euler(f64(k)*Math.PI*2/f64(copies), 0, 0), point3(1, 1, 1)); const local = transform(point3(at.x, at.y, -at.z), mirrored, scale(piece.size, grow)); this.stage.put(piece.shape, multiply(body, multiply(around, local))); } } if (f.light !== undefined) { const l = f.light!; this.stage.light({ position: add(lifted, point3(0, 0.5, 0)), color: [l[0]*pulse, l[1]*pulse, l[2]*pulse], range: l[3] }); } } private clashes(): void throws string { let named: Vec<string> = []; let counted = new Map<i64, i64>(); for (const b of this.blades) { const n = counted.get(b.actor.index) ?? 0; counted.set(b.actor.index, n+1); named.push(`${b.actor.index }#${ n }`); } const at = (k: i64, s: f64): Vec<Point3> => { const b = this.blades[k]; const was = this.bladed.get(named[k]) ?? b; return [lerp(was.from, b.from, s), lerp(was.to, b.to, s)]; }; let now = new Set<string>(); for (const i of range(0, this.blades.length)) { for (const j of range(i+1, this.blades.length)) { const a = this.blades[i]; const b = this.blades[j]; if (a.actor.index === b.actor.index) { continue; } const first = a.actor.index <b.actor.index ? a: b; const second = a.actor.index <b.actor.index ? b: a; const key = `${first.actor.index }@${ first.stroke }: ${ second.actor.index }@${ second.stroke }`; if (this.met.has(key)) { now.add(key); continue; } let least = 1.0e9; let meeting = point3(0, 0, 0); for (const step of [0.25, 0.5, 0.75, 1.0]) { const p = at(i, step); const q = at(j, step); const near = nearestOfSegments(p[0], p[1], q[0], q[1]); const d = length(sub(near[0], near[1])); if (d <least) { least = d; meeting = scale(add(near[0], near[1]), 0.5); } } if (least> CLASH) { continue; } now.add(key); this.game.clash(a.actor, b.actor, meeting); this.clashed.set(a.actor.index, this.game.since()); this.clashed.set(b.actor.index, this.game.since()); } } this.met = now; this.bladed = new Map<string, Blade>(); for (const k of range(0, this.blades.length)) { this.bladed.set(named[k], this.blades[k]); } } private actor(e: Entity, time: f64): void throws string { const g = this.game; const spec = this.spec; const body = g.read(e, Actor); const named = g.design.kinds[g.design.index(this.shown(e))]; if (named.kind.figure === undefined) { return; } const pose = g.pose3D(e); const mount = g.mountOf(e)?.index; if (spec.online !== undefined && g.has(e, Replica)) { if ((spec.map.eye?.first ?? false) && g.ours(e)) { return; } const r = g.read(e, Replica); const seen = spec.online!.being(r.id); if (seen === undefined) { return; } const b = seen!; this.figure({ actor: e, kind: named.name, slot: r.slot, at: pose.position, yaw: pose.yaw, velocity: point3(b.body.velocity.x, 0, b.body.velocity.z), fell: b.down> 0 ? time-r.fell: -1, rose: time-r.rose, struck: time-r.struck, combo: b.combo, airborne: !b.body.grounded, talking: time <r.speaks, rolling: b.dodge> 0 }, time); return; } if (g.has(e, Standing)) { if (e.index === this.within) { return; } const s = g.read(e, Standing); const speaks = g.speaking(e); const listens = g.partner(e) !== undefined && !speaks; const flat = point3(s.velocity.x, 0, s.velocity.z); const velocity = s.aims === 1 ? flat: scale(facing(pose.yaw), length(flat)); this.figure({ actor: e, kind: named.name, slot: e.index, at: pose.position, yaw: pose.yaw, velocity, airborne: s.ground === 0 && s.air> 0.12, rolling: s.roll> 0, emote: s.emote, listens, fell: s.fell, struck: s.struck, hurt: s.hurt, combo: s.combo, talking: speaks, outfit: s.outfit, was: s.was, dressing: s.dressing, mount }, time, 0, 0, s.fell <0 && s.hurt> 0.4 ? body.guard: 0); return; } const sequence = spec.map.sequence; if (sequence !== undefined && g.has(e, Cinema.Cast)) { const p = g.read(e, Cinema.Cast).player; const t = g.phase() === PLAYING ? Cinema.now(g, sequence!): 0.0; const at = Cinema.placed(sequence!, p, t, this.level); const d = Cinema.doing(sequence!, p, t); this.figure({ actor: e, kind: named.name, slot: e.index, at: pose.position, yaw: pose.yaw, velocity: at.velocity, talking: g.speaking(e), mount, clip: d.clip, once: d.once, rate: d.rate, upper: d.upper === "" ? undefined: d.upper, uppered: d.uppered, aimed: d.aimed <0 ? undefined: Cinema.placed(sequence!, d.aimed, t, this.level).at, guards: d.guards, lasts: d.lasts <0 ? undefined: d.lasts, stroke: t-d.uppered }, time); return; } this.figure({ actor: e, kind: named.name, slot: e.index, at: pose.position, yaw: pose.yaw, velocity: g.read(e, Motion3D).linear, mount }, time, pose.pitch, pose.roll, body.guard); } private holding(b: Figured, size: f64): Gltf.Plant | undefined throws string { const g = this.game; const kinds = g.design.kinds; const hands = kinds[g.design.index(b.kind)].kind.figure?.hands ?? []; if (hands.length !== 2 || b.actor === undefined) { return undefined; } const own = kinds[g.design.index(b.kind)].kind.walker?.beside; let partner: Entity | undefined = undefined; let hand = ""; if (own !== undefined && (own!.holds ?? false)) { partner = nearest(g, own!.kind, b.at); hand = hands[own!.side> 0 ? 0: 1]; } for (const k of kinds) { const by = k.kind.walker?.beside; if (partner !== undefined || by === undefined || !(by!.holds ?? false) || by!.kind !== b.kind) { continue; } partner = nearest(g, k.name, b.at); hand = hands[by!.side> 0 ? 1: 0]; } if (partner === undefined) { return undefined; } const other = g.pose3D(partner!).position; if (length(sub(other, b.at))> 2) { return undefined; } const apart = sub(other, b.at); const along = dot(apart, facing(b.yaw)); const aside = dot(apart, facing(b.yaw-Math.PI*0.5)); if (g.partner(b.actor) !== undefined) { return undefined; } const going = Math.max(length(b.velocity), g.has(partner!, Standing) ? length(g.read(partner!, Standing).velocity): 0.0); const weight = Math.smoothstep(0.5, 0.85, dot(facing(b.yaw), facing(g.pose3D(partner!).yaw)))*Math.smoothstep(1.0, 2.0, Math.abs(aside)/Math.max(Math.abs(along), 1.0e-6)) *Math.smoothstep(0.15, 0.6, going)*(1.0-Math.smoothstep(0.3, 0.6, Math.abs(other.y-b.at.y))); if (weight <= 0.0) { return undefined; } const theirs = kinds[g.read(partner!, Actor).kind].kind.figure?.size ?? size; const middle = scale(add(other, b.at), 0.5); const ahead = add(facing(b.yaw), facing(g.pose3D(partner!).yaw)); return { end: hand, to: add(add(middle, point3(0, (size+theirs)*0.225, 0)), scale(ahead, (size+theirs)*0.015)), weight }; } private ground(gr: Ground3D): void throws string { const at = gr.at ?? point3(0, 0, 0); const height = this.level; const maps = this.soil; this.stage.ground("ground", heightfield(gr.samples, gr.samples, (u, v): f64 => height(at.x+(u-0.5)*gr.side, at.z+(v-0.5)*gr.side), gr.side/6), gr.rules, maps[0], maps.length> 2 ? maps[1]: maps[0], maps[maps.length-1]); const grass = gr.grass; if (grass !== undefined) { const n = gr.samples; const spacing = gr.side/f64(n-1); const corner = point3(at.x-gr.side*0.5, 0, at.z-gr.side*0.5); let ground: Vec<f64> = []; for (const row of range(0, n)) { for (const column of range(0, n)) { const x = corner.x+f64(column)*spacing; const z = corner.z+f64(row)*spacing; const y = height(x, z); const dx = height(x+0.5, z)-height(x-0.5, z); const dz = height(x, z+0.5)-height(x, z-0.5); const up = 1/Math.sqrt(1+dx*dx+dz*dz); const steep = 1-up; const grows = y <gr.rules.rock && steep <gr.rules.slope*0.8 && y> gr.rules.sand-gr.rules.blend && !covered(this.feet, x, z, 0.5) && !bared(grass!.bare ?? [], x, z); ground.push(y); ground.push(grows ? 1.0: 0.0); } } this.stage.lawn({ origin: corner, spacing, columns: n, ground, color: grass!.color ?? [0.07, 0.1, 0.03], height: grass!.height ?? 0.3 }); } } private edited(): void throws string { const o = this.spec.online; if (o === undefined) { return; } const all = o.world; for (const e of all.slice(this.done, all.length)) { if (e.kind === "make" && !this.stage.has(e.thing)) { const glb = e.data; try { this.stage.model(e.thing, Gltf.model(`${e.thing }.glb`, (path): bytes => glb, { ...this.game.textureWork, held: false }), 64, true, []); } catch (why) { console.error(`gizgame: the made thing ${e.thing } has no model:  ${ why }`); } } if (e.kind === "make" && e.text === "light") { for (const at of e.spots) { this.glows.push(add(at, point3(0, e.size, 0))); } } if ((e.kind === "place" || e.kind === "make") && this.stage.has(e.thing)) { for (const [i, at] of e.spots.entries()) { const ground = point3(at.x, this.level(at.x, at.z), at.z); this.placed.push({ kind: e.thing, model: this.stage.fit(e.thing, ground, e.size, i <e.yaws.length ? e.yaws[i]: 0, false), at: ground }); } } else if (e.kind === "remove") { this.placed = this.placed.filter(f => !inside(f.at, e)); this.cleared.push(e); } else if (e.kind === "terrain") { for (const key of this.chunks.at.keys()) { this.streams.drop(this.chunks, key, (name: string): void throws string => this.stage.drop(name)); } const gr = this.spec.map.ground; if (gr !== undefined && gr!.chunk === undefined) { this.stage.drop("ground"); this.ground(gr!); } } else if (e.kind === "weather") { this.weather = e.text; } } this.done = all.length; } private coming(o: Online, time: f64): void throws string { const breath = 0.75+0.25*Math.sin(time*3.0); for (const e of o.coming()) { const at = e.spots.length> 0 ? e.spots[0]: e.at; const r = Math.max(0.3, e.size*0.35)*breath; this.stage.put("making", transform(add(at, point3(0, e.size*0.5+0.25*Math.sin(time*1.7), 0)), euler(time, 0, 0), point3(r, r, r))); this.stage.put("making", transform(add(at, point3(0, 0.05, 0)), euler(-time*0.7, 0, 0), point3(e.size*0.8, 0.03, e.size*0.8))); this.stage.light({ position: add(at, point3(0, e.size*0.6, 0)), color: [0.6*breath, 1.4*breath, 2.0*breath], range: 7 }); } const mine = o.mine(); if (!o.building || mine === undefined || o.offers.length === 0) { return; } const ahead = add(mine!.body.position, scale(facing(mine!.yaw), 4)); const ground = point3(ahead.x, this.level(ahead.x, ahead.z), ahead.z); const name = `preview ${o.shown }`; if (o.preview.length> 0 && !this.stage.has(name)) { const glb = o.preview; try { this.stage.model(name, Gltf.model(`${o.shown }.glb`, (path): bytes => glb, { ...this.game.textureWork, held: false }), 1, true, []); } catch (why) { console.error(`gizgame: the model of ${o.shown } does not read:  ${ why }`); } } if (o.preview.length> 0 && this.stage.has(name)) { this.stage.put(name, this.stage.fit(name, ground, o.size, mine!.yaw, false)); } else { const r = o.size*0.3*breath; this.stage.put("making", transform(add(ground, point3(0, o.size*0.5, 0)), euler(time, 0, 0), point3(r, r, r))); } this.stage.put("making", transform(add(ground, point3(0, 0.04, 0)), euler(time, 0, 0), point3(o.size*0.9, 0.02, o.size*0.9))); this.stage.light({ position: add(ground, point3(0, o.size, 0)), color: [0.5, 1.1, 1.6], range: 6 }); } private seen(at: Point3, size: f64): bool { const c = this.camera; const to = sub(at, c.eye); const far = length(to); const reach = size+4.0; const ahead = sub(c.target, c.eye); const span = length(ahead); if (far <reach*2 || span <1e-9) { return true; } const corner = Math.atan(Math.tan(c.fov*0.5)*Math.sqrt(1+this.wide*this.wide)); const off = Math.acos(Math.max(-1.0, Math.min(1.0, dot(to, ahead)/(far*span)))); return off <= corner+0.26+Math.asin(Math.min(1.0, reach/far)); } private gone(at: Point3): bool { return this.cleared.some(e => inside(at, e)); } private stand(): void throws string { const at = `${this.standing.length } ${ this.cleared.length }`; if (at === this.stoodAt) { return; } let kinds = new Map<string, Vec<Matrix4>>(); for (const kind of this.stoodKinds) { kinds.set(kind, []); } for (const f of this.standing) { if (f.kind === "ground" || f.kind === "sea" || !this.gone(f.at)) { kinds.push(f.kind, f.model); } } for (const [kind, models] of kinds.entries()) { this.stage.fix(kind, models); } this.stoodKinds = new Set<string>([...kinds.keys()]); this.stoodAt = at; } private feet(eye: Point3): Point3 throws string { const follow = this.spec.map.eye?.follow; const who = follow === undefined ? undefined: one(this.game, this.game.led(follow!)); return who !== undefined ? this.game.pose3D(who!).position: this.game.phase() === TITLE ? this.spec.map.start ?? eye: eye; } private stream(eye: Point3): void throws string { const gr = this.spec.map.ground; if (gr === undefined || gr!.chunk === undefined) { return; } const size = gr!.chunk!; const height = this.level; const n = gr!.samples; const name = (c: Point3): string => `ground ${c.x } ${ c.z }`; this.streams.run(eye, squares(eye, gr!.at ?? point3(0, 0, 0), size, gr!.radius ?? size*3, gr!.side*0.5, this.chunks, name), this.chunks, 2, (key: string): bool => true, (key: string, c: Point3): Vec<Physics.Body> throws string => { const maps = this.soil; this.stage.ground(key, heightfield(n, n, (u, v): f64 => height(c.x+(u-0.5)*size, c.z+(v-0.5)*size), size/6), gr!.rules, maps[0], maps.length> 2 ? maps[1]: maps[0], maps[maps.length-1]); const spacing = size/f64(n-1); const origin = point3(c.x-size*0.5, 0, c.z-size*0.5); let heights: Vec<f64> = []; for (const row of range(0, n)) { for (const column of range(0, n)) { heights.push(height(origin.x+f64(column)*spacing, origin.z+f64(row)*spacing)); } } return [Physics.body(-1, Physics.field({ columns: n, rows: n, spacing, origin, heights }), point3(0, 0, 0), Physics.STATIC)]; }, (key: string): void throws string => this.stage.drop(key)); for (const [key, c] of this.chunks.at.entries()) { this.stage.put(key, transform(c, euler(0, 0, 0), point3(size, 1, size))); } } frame(p: Picture): void throws string { const g = p.game; const time = p.time; const spec = this.spec; const aspect = p.width/Math.max(1, p.height); this.back = aspect <1 ? Math.min(1.5, 1/Math.sqrt(aspect)): 1.0; this.wide = aspect; let camera = spec.view !== undefined ? spec.view!(this, p): this.eye(time); if (g.shot> = 0 && g.shot <this.shots.length) { camera = { ...this.shots[g.shot], far: camera.far }; } const sequence = spec.map.sequence; const into = Cinema.into(g, sequence); let cut: Grade | undefined = undefined; if (into> = 0) { const seen = Cinema.cameraAt(sequence!, into, this.level); if (seen !== undefined) { const left = g.fetch(Score).shake; const jolt = left*left*1.5; camera = seen!.camera; cut = seen!.grade; const kick = point3(Math.sin(time*17)*jolt, Math.cos(time*13)*jolt, Math.sin(time*19)*jolt*0.5); const shot = Cinema.shotAt(sequence!, into); const fresh = shot !== this.shot; this.shot = shot; camera = { ...camera, eye: add(camera.eye, scale(kick, 0.3)), target: add(camera.target, scale(kick, length(sub(camera.target, camera.eye))*0.25)), cut: fresh }; } } const talk = g.conversation(); if (talk.length> = 2 && spec.view === undefined && g.shot <0 && into <0) { camera = this.dialogue(talk, camera); this.left = { camera, at: time }; } else { this.sided = 0.0; this.cue = undefined; this.lines = 0; const s = Math.smoothstep(0.0, BLEND, time-this.left.at); if (s <1.0) { camera = { ...camera, eye: lerp(this.left.camera.eye, camera.eye, s), target: lerp(this.left.camera.target, camera.target, s), fov: this.left.camera.fov+(camera.fov-this.left.camera.fov)*s }; } } this.plain = camera; const settings = g.settings; this.stage.detail = settings.detail; this.stage.quality = f64(detailStep(settings)); this.stage.view = Math.maxI64(0, VIEWS.indexOf(settings.view)); this.stage.grass = (spec.map.ground?.grass?.reach ?? 25)*[0.0, 0.6, 1.0, 1.5][detailStep(settings)]; const tall = aspect <1 ? 2*Math.atan(Math.tan(camera.fov*0.5)/Math.sqrt(aspect)): camera.fov; camera = { ...camera, fov: Math.min(tall*settings.fov, 2.6), far: camera.far*settings.distance }; this.camera = camera; const eye = camera.eye; this.cast(); this.stage.begin(); for (const c of this.casts.values()) { c.begin(); } this.edited(); const feet = this.feet(eye); for (const layer of this.layers) { layer.frame(eye, feet); } this.stand(); for (const f of this.placed) { this.stage.put(f.kind, f.model); } if (spec.online !== undefined) { this.coming(spec.online!, time); } for (const at of this.glows) { if (!this.gone(at)) { this.stage.light({ position: at, color: [1.6, 1.1, 0.6], range: 9 }); } } const struck = g.fetch(Score); const bright = struck.flash> 0 ? (struck.flash/struck.flashed)**2: 0.0; if (bright> 0) { this.stage.light({ position: struck.glare, color: [60*bright, 48*bright, 32*bright], range: 16 }); } const far = spec.map.far === undefined ? undefined: spec.map.far!*g.settings.distance; this.stage.reach = far; const gust = 0.6+0.4*Math.sin(time*0.31); for (const s of this.swaying) { if ((far !== undefined && length(sub(s.at, eye))> far) || this.gone(s.at)) { continue; } const lean = (s.sway ?? 0)*gust; const phase = s.at.x*0.37+s.at.z*0.23; this.stage.put(s.kind, this.stage.fit(s.kind, s.at, s.size ?? 1, s.yaw ?? 0, s.lying ?? false, lean*Math.sin(time*1.3+phase), lean*0.6*Math.sin(time*1.7+phase*1.3))); } this.seats = new Map<i64, Vec<Seated>>(); this.sat = new Map<i64, i64>(); let riders: Vec<Entity> = []; this.blades = []; for (const e of g.each(Actor)) { if (g.has(e, Riding)) { riders.push(e); } else { this.actor(e, time); } } for (const e of riders) { this.actor(e, time); } this.clashes(); const o = spec.online; if (o !== undefined) { const goal = o.goal(); if (spec.goal !== undefined && goal !== undefined) { this.stage.place(spec.goal!, add(goal, point3(0, 3+Math.sin(time*3)*0.15, 0)), point3(0.3, 0.3, 0.3)); } } particles3D(g, this.stage); const seconds = spec.map.sky?.day ?? 300; for (const l of spec.map.lamps ?? []) { const on = lit(l.when, g, time, seconds); if (on <= 0.05) { continue; } const size = l.size ?? 0.2; for (const at of l.at ?? []) { if (l.lamp !== undefined) { this.stage.place(l.lamp!, at, point3(size, size, size)); } this.stage.light({ position: at, color: [l.color[0]*on, l.color[1]*on, l.color[2]*on], range: l.range }); } } if (spec.under !== undefined) { spec.under!(p, this); } this.ambience(time); this.rain(time); let air = graded(weathered(this.light(g, time), this.weather), settings); const peak = Math.max(air.light[0], Math.max(air.light[1], air.light[2])); const level = (spec.map.sky?.level ?? false) && this.own.length> 0; const whole = level && this.heaven !== undefined; if (whole) { const h = this.heaven!; air = { ...air, sun: this.own[0].aim!, light: this.own[0].color, ambient: h.ambient, sky: displayed(h.horizon), zenith: displayed(h.zenith), backdrop: h.backdrop, bounce: this.volume === undefined ? 1.0: 0.0, reach: this.volume === undefined ? 3.0: 0.6, occlusion: 1.0, haze: this.haze, volume: this.volume }; } else if (level) { const c = this.own[0].color; const top = Math.max(1.0e-9, Math.max(c[0], Math.max(c[1], c[2]))); air = { ...air, sun: this.own[0].aim!, light: [c[0]/top*peak, c[1]/top*peak, c[2]/top*peak] }; } if ((spec.map.sky?.level ?? false) && this.look !== undefined) { const look = this.look!; air = { ...air, grade: { ...look, exposure: air.grade?.exposure ?? look.exposure } }; } this.sunward = unitOf(air.sun); if (g.shot> = 0 && g.shot <this.graded.length && this.graded[g.shot] !== undefined) { air = { ...air, grade: over(air.grade ?? {}, this.graded[g.shot]!) }; } if (cut !== undefined) { air = { ...air, grade: over(air.grade ?? {}, cut!) }; } for (const [at, lamp] of this.lamps.entries()) { const k = whole || this.suns[at] <= 0.0 ? 1.0: peak/this.suns[at]; this.stage.light({ ...lamp, color: [lamp.color[0]*k, lamp.color[1]*k, lamp.color[2]*k] }); } p.stage(this.stage, this.camera, air); let h = new Hud(p, this.sprites); h.camera = this.camera; for (const widget of spec.hud ?? []) { widget(h); } if (into> = 0) { cinema(h, sequence!, into); } else { fingers(h); company(h); } panels(h); h.draw(); } private dialogue(talk: Vec<Entity>, base: Camera): Camera throws string { const g = this.game; const speaking = talk.find(m => g.speaking(m)); const cut = speaking !== undefined && (this.cue === undefined || !same(this.cue!, speaking!)); if (cut) { this.cue = speaking; this.lines+ = 1; } const shown = this.cue !== undefined && talk.some(m => same(m, this.cue!)) ? this.cue!: talk[0]; const listener = g.partner(shown) ?? talk[1]; const line = sub(eyesOf(this.game, talk[1]), eyesOf(this.game, talk[0])); const across = unitOf(point3(-line.z, 0, line.x)); if (this.sided === 0.0) { const lit = dot(across, this.sunward); const heard = g.listeners(); const stands = heard.length> 0 ? g.joining(heard[0]): undefined; this.sided = stands !== undefined ? (dot(sub(stands!, g.position(talk[0])), across)> 0 ? -1.0: 1.0): Math.abs(lit)> 0.2 ? (lit> 0 ? -1.0: 1.0): dot(sub(base.eye, eyesOf(this.game, talk[0])), across) <0 ? -1.0: 1.0; } const from = eyesOf(this.game, listener); const to = eyesOf(this.game, shown); const ahead = unitOf(point3(to.x-from.x, 0, to.z-from.z)); if (this.lines <= 1) { const middle = scale(add(from, to), 0.5); const span = length(sub(to, from))+0.9; const back = span*0.5/Math.tan(OVER[3]*0.5)*1.15; return { ...base, eye: add(middle, add(scale(across, back*this.sided), point3(0, 0.05, 0))), target: add(middle, point3(0, -0.15, 0)), fov: OVER[3], cut }; } const level = point3(from.x, (from.y+to.y)*0.5+OVER[2], from.z); const eye = add(add(level, scale(ahead, -OVER[0])), scale(across, OVER[1]*this.sided)); return { ...base, eye, target: add(to, point3(0, -0.08, 0)), fov: OVER[3], cut }; } private eye(time: f64): Camera throws string { const e = this.spec.map.eye; if (e === undefined) { return this.plain; } const g = this.game; const m = g.fetch(Score); let chase = this.chase; const o = this.spec.online; if (g.phase() !== TITLE && o !== undefined) { const mine = o.mine(); if (mine !== undefined) { const b = mine; const at = o.at(b); if (e.first ?? false) { const eye = o.eye(b); return { eye, target: add(eye, facing(b.yaw, b.pitch)), fov: e.fov ?? 1.2, near: 0.05, far: e.rig?.far ?? 200 }; } return chase.follow(at, b.yaw, time, m.shake*0.8, Math.max(-0.45, Math.min(0.7, b.pitch)), g.zoom*this.back); } } const who = e.follow === undefined || g.phase() === TITLE ? undefined: one(g, g.led(e.follow!)); this.within = -1; if (who !== undefined && g.has(who!, Standing)) { const pose = g.pose3D(who!); const s = g.read(who!, Standing); const rigs = [chase.rig, ...(e.views ?? [])]; if (this.views.length === 0) { this.views = rigs; } const car = g.mountOf(who!); const drives = car === undefined ? undefined: g.design.kinds[g.read(car!, Actor).kind].kind.drives; if (drives !== undefined) { const p = g.pose3D(car!); const swing = (drives!.wheels ?? false) ? m.swing: 0.0; chase.rig = DRIVING; return chase.follow(p.position, p.yaw+swing, time, m.shake, m.pitch, g.zoom*this.back); } chase.rig = this.views[s.view%this.views.length]; if (chase.rig.back <= 0) { this.within = who!.index; } return chase.follow(pose.position, s.look, time, m.shake, m.pitch, g.zoom*this.back, s.side); } if (who !== undefined) { if ((e.views ?? []).length> 0) { if (this.views.length === 0) { this.views = [chase.rig, ...e.views!]; } chase.rig = this.views[g.view%this.views.length]; if (chase.rig.back <= 0) { this.within = who!.index; } } const pose = g.pose3D(who); return chase.follow(pose.position, pose.yaw, time, m.shake, m.pitch, g.zoom*this.back); } const t = e.title; return chase.orbit(t.center, t.radius, t.height, t.speed, time); } private ambience(time: f64): void throws string { const eye = this.camera.eye; for (const a of this.spec.map.ambient ?? []) { const share = lit(a.when, this.game, time, this.spec.map.sky?.day ?? 300); const cell = a.radius*2; for (const k of range(0, i64(f64(a.count)*share))) { const x = eye.x+wrapped(Math.hash2(k, 31)*cell-eye.x, cell)-a.radius+Math.sin(time*0.7+f64(k))*0.6; const z = eye.z+wrapped(Math.hash2(k, 57)*cell-eye.z, cell)-a.radius+Math.cos(time*0.5+f64(k))*0.6; const y = this.level(x, z)+0.4+Math.hash2(k, 83)*2.2+Math.sin(time*1.3+f64(k)*0.7)*0.3; const size = a.size*(0.6+0.4*Math.sin(time*3+f64(k))); this.stage.place(a.thing, point3(x, y, z), point3(size, size, size)); } } } private rain(time: f64): void throws string { if (this.weather !== "rain" && this.weather !== "storm") { return; } const kind = "gizgame rain"; const storm = this.weather === "storm"; if (!this.stage.has(kind)) { this.stage.add(kind, box(), glowing(0.57, 0.72, 0.86, 0.35), 192, false); } const eye = this.camera.eye; const span = 10.0; const height = 7.0; const wind = storm ? 2.1: 0.7; const fall = storm ? 14.0: 9.0; for (const i of range(0, storm ? 192: 128)) { const x = eye.x+wrapped(Math.hash2(i, 11)*span+wind*time-eye.x, span)-span*0.5; const z = eye.z+wrapped(Math.hash2(i, 23)*span-eye.z, span)-span*0.5; const y = eye.y+wrapped(Math.hash2(i, 41)*height-fall*time-eye.y, height)-2.0; if (y <this.level(x, z)+0.15) { continue; } this.stage.put(kind, transform(point3(x, y, z), euler(0, 0, wind/fall), point3(0.013, 0.30, 0.013))); } } }
```

A 3D game's art: the Stage with the scene's things, the casts of its rigged figures, the props placed once (by
cell, or swaying), the HUD's sprites and the camera of the frame.

- `game`: `Ref<M201.Game>`
- `spec`: `M219.Scene3D`
- `light`: `(arg0: Ref<M201.Game>, arg1: f64) => gizlang_meshdraw.Environment`
- `stage`: `gizlang_meshdraw.Stage`
- `sprites`: `Ref<M228.Sprites>`
- `camera`: `gizlang_meshdraw.Camera`
- `plain`: `gizlang_meshdraw.Camera` — The camera of the last frame as the scene declared it, before the person's field of view and distance.
- `wide`: `f64`
- `level`: `(arg0: f64, arg1: f64) => f64` — The ground's height at x, z: the terrain, the declared ground's, or 0.
- `chase`: `Ref<M198.Chase>`
- `standing`: `Vec<M204.Fixed>`
- `stoodAt`: `string`
- `stoodKinds`: `_gizlang_sdk_collections.Set<string>`
- `built`: `Vec<M204.Fixed>`
- `lamps`: `Vec<gizlang_meshdraw.PointLight>`
- `suns`: `Vec<f64>`
- `own`: `Vec<gizlang_meshdraw.PointLight>`
- `look`: `_gizlang_sdk_core.Option<gizlang_meshdraw.Grade>`
- `heaven`: `_gizlang_sdk_core.Option<gizlang_gltf.Heaven>`
- `haze`: `_gizlang_sdk_core.Option<gizlang_meshdraw.Haze>`
- `volume`: `_gizlang_sdk_core.Option<gizlang_meshdraw.Volume>`
- `shots`: `Vec<gizlang_meshdraw.Camera>`
- `graded`: `Vec<_gizlang_sdk_core.Option<gizlang_meshdraw.Grade>>`
- `chunks`: `Ref<M229.Pieces>` — The streamed ground's chunks by name, each with its middle, and its maps (sand, grass, rock).
- `streams`: `Ref<M229.Streams>` — The one loop of the streamed pieces, and the map's layers of them (Layer: its ground's chunks, a city, a tiled place).
- `layers`: `Vec<M229.Layer>`
- `soil`: `Vec<gizlang_meshdraw.Maps>`
- `feet`: `Vec<M204.Block>`
- `swaying`: `Vec<M204.Prop3D>`
- `casts`: `_gizlang_sdk_collections.Map<string, Ref<M200.Cast>>`
- `rigs`: `_gizlang_sdk_collections.Map<string, gizlang_gltf.Rig>`
- `coming`: `_gizlang_sdk_collections.Set<string>` — The kinds that came in play (Game arrive) whose figures are being rigged.
- `placed`: `Vec<M204.Fixed>` — What the online world's edits did to the art: the things they placed, the areas they cleared, its weather; DONE
counts the edits read.
- `cleared`: `Vec<M210.WorldEdit>`
- `glows`: `Vec<gizlang_geometry.Point3>` — Where the made things that glow give light.
- `done`: `i64`
- `back`: `f64` — How much farther back the eye stands on an upright screen.
- `within`: `i64`
- `stood`: `_gizlang_sdk_collections.Map<string, gizlang_geometry.Point3>`
- `seats`: `_gizlang_sdk_collections.Map<i64, Vec<M219.Seated>>`
- `sat`: `_gizlang_sdk_collections.Map<i64, i64>`
- `shot`: `i64`
- `blades`: `Vec<M219.Blade>`
- `met`: `_gizlang_sdk_collections.Set<string>`
- `bladed`: `_gizlang_sdk_collections.Map<string, M219.Blade>`
- `clashed`: `_gizlang_sdk_collections.Map<i64, f64>`
- `cue`: `_gizlang_sdk_core.Option<M233.Entity>` — The one a conversation's eye shows (dialogue), and the side of its line the eye keeps (1 or -1; 0 none).
- `sided`: `f64`
- `lines`: `i64` — The lines a conversation's eye has shown (dialogue), and its last camera with when (the eye eases back from it).
- `sunward`: `gizlang_geometry.Point3` — The way the sun's light goes, of the last frame (dialogue: its side).
- `left`: `{ camera: gizlang_meshdraw.Camera; at: f64; }`
- `gazes`: `_gizlang_sdk_collections.Map<string, { at: gizlang_geometry.Point3; weight: f64; time: f64; }>` — Where each figure looks, how much, and when (figure: gizgame/attention).
- `worked`: `_gizlang_sdk_collections.Map<string, { k: f64; time: f64; }>` — How far each working piece is into its turn (0 to 1), and when that was (Piece works).
- `views`: `Vec<M198.Follow>`
- `weather`: `string`

### `Scene3D`

```gizlang
type Scene3D = { map: Map3D; height?: (x: f64, z: f64) => f64; light?: (game: Game, time: f64) => Environment; place?: (game: Game) => Vec<Prop3D> throws string; view?: (scene: Scene, picture: Picture) => Camera throws string; under?: (picture: Picture, scene: Scene) => void throws string; online?: Online; goal?: string; hud?: Vec<Widget>; }
```

A 3D game's world and view (play3D): its MAP (data), and its code: the ground's HEIGHT in place of the map's relief,
the LIGHT at a time in place of the map's sky, the props it PLACEs beside the map's, a VIEW (a camera of its own),
what UNDER draws of its own each frame (the kinds' STANDS stand about with it), the ONLINE
world whose beings it draws (thing GOAL over the own quest's goal), and the HUD's widgets, under the flow's panels.

- `map`: `M204.Map3D`
- `height`: `_gizlang_sdk_core.Option<(arg0: f64, arg1: f64) => f64>`
- `light`: `_gizlang_sdk_core.Option<(arg0: Ref<M201.Game>, arg1: f64) => gizlang_meshdraw.Environment>`
- `place`: `_gizlang_sdk_core.Option<(arg0: Ref<M201.Game>) => Vec<M204.Prop3D> throws string>`
- `view`: `_gizlang_sdk_core.Option<(arg0: Ref<M219.Scene>, arg1: Ref<M213.Picture>) => gizlang_meshdraw.Camera throws string>`
- `under`: `_gizlang_sdk_core.Option<(arg0: Ref<M213.Picture>, arg1: Ref<M219.Scene>) => void throws string>`
- `online`: `_gizlang_sdk_core.Option<Ref<M210.Online>>`
- `goal`: `_gizlang_sdk_core.Option<string>`
- `hud`: `_gizlang_sdk_core.Option<Vec<(arg0: Ref<M202.Hud>) => void throws string>>`

## Functions

### `weathered`

```gizlang
function weathered(sky: Environment, weather: string): Environment
```

SKY under WEATHER: rain and storms darken it and close it in, fog closes it in, snow whitens it, night darkens it.

Effects: fail.

### `constructor`

```gizlang
constructor(game: Game, resources: Resources, screen: Screen, given: Scene3D) throws string
```

SPEC's world for GAME, read from its files, on RESOURCES for SCREEN.

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

### `lightOf`

```gizlang
function lightOf(scene: Scene3D): (game: Game, time: f64) => Environment
```

The light of SCENE at a time: its code's, else its map's sky (Sky3D).

Effects: fail, alloc.

### `play3D`

```gizlang
function play3D(game: Game, scene: (map: i64) => Scene3D throws string): Play<Scene>
```

A 3D game drawn from its kinds' figures over the world that SCENE declares for the map it is on (Game.map; a game
of one map declares it for map 0); a new map builds its world again. With an online world, the host carries its
match: a game of a world's files (Game.world, gizgame/maps) reads its maps only when they are here, so it is not
online.

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

## Methods

### `Scene.figure`

```gizlang
figure(b: Figured, time: f64, pitch: f64 = 0, roll: f64 = 0, guarded: f64 = 0): void throws string
```

Figure B (Kind.figure) at TIME, tipped PITCH and ROLL; one GUARDED blinks when its figure does. What B does
chooses its rig's clip (Clips); its feet stand on the ground (two-bone IK), and a fall with no clip of its own
topples the body over.

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

### `Scene.frame`

```gizlang
frame(p: Picture): void throws string
```

One frame: the props near the eye and the swaying ones, every actor's figure, the figures and the online
beings, the particles, the lamps, what the game draws of its own, the ambient motes, the camera and the sky, then
the HUD and the panels.

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

