# gizgame/edits

the edits of a made world. There is one agent, gizai/agent; this file gives it the tools (a shelf) that change a made world's document, and each tool answers what it really did. A person's own hand calls the same Edits. A run gives the changed document, the edits the running world applies (WorldOp; a host's session applies them), and its steps as the tools answered them: the reader sees no claim of the model's that a tool did not make.

```gizlang
import * as Edits from "gizgame/edits";
```

## Types

### `Finder`

```gizlang
type Finder = World.Finder
```

Finds a model by its NAME, of a kind, animated or not: the catalog search an edit that adds a thing asks.

### `Step`

```gizlang
type Step = World.AgentStep
```

One step of a run as its reader sees it: the TOOL, what it was asked (LABEL), and how it ended (OK, TEXT).

### `Outcome`

```gizlang
type Outcome = { reply: string; steps: Vec<Step>; changes: Vec<string>; world: World.WorldDoc; ops: Vec<World.WorldOp>; changed: bool; reload: bool; failure: string; ms: i64; tokens: i64; }
```

What a run did: the model's REPLY, its STEPS, the short CHANGES the tools made, the WORLD after them, the edits
(OPS) the running engine applies, whether the document CHANGED and whether the page must RELOAD the world (a new
camera or play), why it FAILED ("": it did not), and what it took.

- `reply`: `string`
- `steps`: `Vec<M233.AgentStep>`
- `changes`: `Vec<string>`
- `world`: `M233.WorldDoc`
- `ops`: `Vec<M233.WorldOp>`
- `changed`: `bool`
- `reload`: `bool`
- `failure`: `string`
- `ms`: `i64`
- `tokens`: `i64`

### `Shown`

```gizlang
type Shown = World.AgentPiece
```

A piece of a run for its reader, as it comes: "text" of the reply, "thinking", a "call" (STEP as asked) and its
"result" (STEP as ended).

### `ChangeThings`

```gizlang
type ChangeThings = { ids?: Vec<string>; name?: string; scale?: f64; moves?: "still" | "flies" | "sails" | "swims" | "drives" | "walks"; color?: string; sound?: "hum" | "chirp" | "sweep" | "growl" | "bell" | "none"; role?: "prop" | "enemy" | "npc" | "pickup" | "jumppad" | "hazard"; model?: string; }
```

Changes things that are in the world: those of IDS, or every thing whose name holds NAME. Only the fields given change.

- `ids`: `_gizlang_sdk_core.Option<Vec<string>>` — The ids of the things, from the list of things.
- `name`: `_gizlang_sdk_core.Option<string>` — Words of a name: every thing whose name holds them ("ufo" is every ufo).
- `scale`: `_gizlang_sdk_core.Option<f64>` — The new size in meters: 1 to 12.
- `moves`: `_gizlang_sdk_core.Option<string>` — How it moves by itself.
- `color`: `_gizlang_sdk_core.Option<string>` — A coat of paint over its model, as '#RRGGBB'; "" gives it its own colors back.
- `sound`: `_gizlang_sdk_core.Option<string>` — The sound it makes near the player; "none" takes it away.
- `role`: `_gizlang_sdk_core.Option<string>` — What it is in play.
- `model`: `_gizlang_sdk_core.Option<string>` — One to three English words of another 3D model to draw it with.

### `RemoveThings`

```gizlang
type RemoveThings = { ids?: Vec<string>; name?: string; all?: bool; }
```

Removes things from the world: those of IDS, every thing whose name holds NAME, or ALL things.

- `ids`: `_gizlang_sdk_core.Option<Vec<string>>` — The ids of the things, from the list of things.
- `name`: `_gizlang_sdk_core.Option<string>` — Words of a name: every thing whose name holds them.
- `all`: `_gizlang_sdk_core.Option<bool>` — True removes every thing.

### `Edits`

```gizlang
class Edits { find: Finder; world: World.WorldDoc; ops: Vec<World.WorldOp> = []; changes: Vec<string> = []; changed: bool = false; reload: bool = false; stand: Option<World.Stand>; constructor(find: Finder, world: World.WorldDoc, stand: Option<World.Stand> = undefined) { this.find = find; this.world = world; this.stand = stand; } private placings(): Vec<World.WorldPlacing> { return this.world.map.placings; } private keep(placings: Vec<World.WorldPlacing>): void { this.world = { ...this.world, map: { ...this.world.map, placings } }; this.changed = true; } private stood(p: World.WorldPlacing, fresh: bool): World.WorldOp { const op: World.WorldOp = { action: fresh ? "make": "update", uid: p.id ?? "", thing: p.name, role: p.kind, model: p.modelUrl, scale: p.scale, behavior: p.behavior, color: p.color ?? "", sound: p.sound ?? "" }; return fresh ? { ...op, x: p.x, z: p.z, yaw: p.yaw }: op; } private spot(n: i64): Vec<f64> { const s = this.stand; if (s === undefined) { const turn = f64(this.placings().length + n) * 1.15; const far = 12.0 + f64(n) * 7.5; return [Math.round(Math.cos(turn) * far * 10.0) / 10.0, Math.round(Math.sin(turn) * far * 10.0) / 10.0]; } const ahead = facing(s!.yaw); const right = facing(s!.yaw - Math.PI * 0.5); const far = (s!.clear ?? 9.0) + f64(n) * 4.0; const side = n === 0 ? 0.0: (f64(n % 3) - 1.0) * 6.5; return [Math.round((s!.x + ahead.x * far + right.x * side) * 10.0) / 10.0, Math.round((s!.z + ahead.z * far + right.z * side) * 10.0) / 10.0]; } put(name: string, address: string, role: string, count: i64, scale: f64, moves: string, color: string, sound: string): Vec<string> throws string { const room = World.MOST_PLACINGS - this.placings().length; if (room <count) { throw `The world holds ${World.MOST_PLACINGS } things at most and has ${ this.placings().length }. Remove some first.`; } if (color !== "" && World.hexOr(color, "") === "") { throw `The color "${color }" is no '#RRGGBB' color.`; } let list = this.placings(); let ids: Vec<string> = []; for (const n of range(0, count)) { const at = this.spot(n); const p: World.WorldPlacing = { ...World.placing(role, name, at[0], at[1], clamped(scale, 1.0, 12.0), address, 0.0, role === "enemy" ? 100: 0, role === "enemy" || role === "hazard" ? 15: 0, moves !== "" ? moves: (role === "enemy" ? "chase": "static"), ""), id: World.freeId(list, list.length + 1), color, sound }; list.push(p); ids.push(p.id!); this.ops.push(this.stood(p, true)); } this.keep(list); this.changes.push(`+${count } ${ name }`); return ids; } move(id: string, x: f64, z: f64, yaw: f64): void throws string { let list = this.placings(); const at = list.findIndex((p: World.WorldPlacing): bool => (p.id ?? "") === id); if (at <0) { throw `No thing ${id }.`; } list[at] = { ...list[at], x, z, yaw }; this.ops.push({ action: "update", uid: id, x, z, yaw }); this.keep(list); this.changes.push(`moved ${list[at].name }`); } private modelOf(name: string, animated: bool): World.Found throws string { const hit = this.find(name, "model", animated); if (hit === undefined) { throw `No 3D model named "${name }" was found. Try another name of one to three English words.`; } return hit!; } private chosen(ids: Vec<string>, name: string): Vec<i64> throws string { const want = words(name); let at: Vec<i64> = []; for (const [i, p] of this.placings().entries()) { if (ids.includes(p.id ?? "") || (want !== "" && words(p.name).includes(want))) { at.push(i); } } if (ids.length === 0 && want === "") { throw "Give ids or a name."; } if (at.length === 0) { throw `No thing matches. The world has: ${this.listed() }`; } return at; } listed(): string { let groups: Vec<string> = []; let ids: Vec<Vec<string>> = []; for (const p of this.placings()) { let line = `${p.name } (${ p.kind },  scale ${ p.scale }`; if (p.behavior !== "static") { line + = `, ${p.behavior }`; } if ((p.color ?? "") !== "") { line + = `, color ${p.color! }`; } if ((p.sound ?? "") !== "") { line + = `, sound ${p.sound! }`; } line + = ")"; const at = groups.indexOf(line); if (at <0) { groups.push(line); ids.push([p.id ?? ""]); } else { ids[at].push(p.id ?? ""); } } if (groups.length === 0) { return "no things"; } let out: Vec<string> = []; for (const [i, g] of groups.entries()) { out.push(`${ids[i].join(",") }:  ${ g }`); } return out.join("\n"); } add(q: AddThings): string throws string { const name = words(q.name); if (name === "") { throw "Give the thing's name."; } const count = Math.maxI64(1, Math.minI64(8, q.count ?? 1)); const role = q.role ?? "prop"; const hit = this.modelOf(name, role === "enemy" || role === "npc"); const ids = this.put(name, hit.url, role, count, q.scale ?? 3.4, q.moves ?? "", q.color ?? "", q.sound ?? ""); return `Added ${count } ${ name } (${ ids.join(", ") }) with the model "${ hit.title }".`; } change(q: ChangeThings): string throws string { const at = this.chosen(q.ids ?? [], q.name ?? ""); const color = q.color; if (color !== undefined && color! !== "" && World.hexOr(color!, "") === "") { throw `The color "${color! }" is no '#RRGGBB' color.`; } const model = words(q.model ?? ""); const hit = model !== "" ? this.modelOf(model, false): undefined; let list = this.placings(); let said: Vec<string> = []; if (q.scale !== undefined) { said.push(`scale ${clamped(q.scale!, 1.0, 12.0) }`); } if (q.moves !== undefined) { said.push(q.moves!); } if (color !== undefined) { said.push(color! === "" ? "its own colors": `color ${color! }`); } if (q.sound !== undefined) { said.push(q.sound! === "none" ? "no sound": `sound ${q.sound! }`); } if (q.role !== undefined) { said.push(`role ${q.role! }`); } if (hit !== undefined) { said.push(`model "${hit!.title }"`); } if (said.length === 0) { throw "Give what to change: scale, moves, color, sound, role or model."; } for (const i of at) { const was = list[i]; const p: World.WorldPlacing = { ...was, scale: q.scale !== undefined ? clamped(q.scale!, 1.0, 12.0): was.scale, behavior: q.moves ?? was.behavior, color: color ?? (was.color ?? ""), sound: q.sound === undefined ? (was.sound ?? ""): q.sound! === "none" ? "": q.sound!, kind: q.role ?? was.kind, name: hit !== undefined ? model: was.name, modelUrl: hit !== undefined ? hit!.url: was.modelUrl }; list[i] = p; this.ops.push(this.stood(p, false)); } this.keep(list); const ids = at.map((i: i64): string => list[i].id ?? ""); this.changes.push(`${at.length } ${ list[at[0]].name }:  ${ said.join(", ") }`); return `Changed ${at.length } things (${ ids.join(", ") }):  ${ said.join(", ") }.`; } remove(q: RemoveThings): string throws string { const all = q.all ?? false; if (all && this.placings().length === 0) { throw "The world has no things."; } const at = all ? this.placings().map((_: World.WorldPlacing, i: i64): i64 => i): this.chosen(q.ids ?? [], q.name ?? ""); let kept: Vec<World.WorldPlacing> = []; let gone: Vec<string> = []; for (const [i, p] of this.placings().entries()) { if (!at.includes(i)) { kept.push(p); continue; } gone.push(p.id ?? ""); this.ops.push({ action: "remove", uid: p.id ?? "" }); } const name = all ? "things": this.placings()[at[0]].name; this.keep(kept); this.changes.push(`−${gone.length } ${ name }`); return `Removed ${gone.length } things (${ gone.join(", ") }).`; } environment(q: Environment): string throws string { const theme = this.world.theme; const biome = (q.biome ?? "").trim().toLowerCase(); const weather = (q.weather ?? "").trim().toLowerCase(); if (biome !== "" && !World.BIOMES.includes(biome)) { throw `No biome "${biome }". One of:  ${ World.BIOMES.join(", ") }.`; } if (weather !== "" && !World.WEATHERS.includes(weather)) { throw `No weather "${weather }". One of:  ${ World.WEATHERS.join(", ") }.`; } let said: Vec<string> = []; const next: World.WorldTheme = { ...theme, biome: biome !== "" ? biome: theme.biome, timeOfDay: q.time ?? theme.timeOfDay, weather: weather !== "" ? weather: theme.weather }; if (next.biome !== theme.biome) { said.push(`biome ${next.biome }`); } if (next.timeOfDay !== theme.timeOfDay) { said.push(next.timeOfDay); } if (next.weather !== theme.weather) { said.push(`weather ${next.weather }`); } this.world = { ...this.world, theme: next }; let surface = ""; let normal = ""; const ground = words(q.ground ?? ""); if (ground !== "") { const hit = this.find(ground, "texture", false); if (hit === undefined) { throw `No ground surface named "${ground }" was found. Try another name.`; } surface = hit!.url; normal = hit!.normalUrl; said.push(`ground ${ground }`); } else if (next.biome !== theme.biome) { const own = World.groundPbrForBiome(next.biome); surface = own.diff; normal = own.norm; } if (surface !== "") { const pbr: World.WorldPbr = { diff: surface, norm: normal !== "" ? normal: this.world.map.ground.pbr.norm }; this.world = { ...this.world, map: { ...this.world.map, ground: { ...this.world.map.ground, pbr } } }; } if (q.terrain !== undefined) { this.ops.push({ action: "terrain", shape: q.terrain!, height: 22.0 }); said.push(`terrain ${q.terrain! }`); } if (q.sea !== undefined) { said.push(q.sea! ? `a sea of ${q.seaOf ?? "water" }`: "no sea"); } if (said.length === 0) { throw "Nothing would change: the world is so already."; } this.ops.push({ action: "environment", sea: q.sea ?? false, hasSea: q.sea !== undefined, seaKind: q.seaOf ?? "water", weather: next.weather, timeOfDay: next.timeOfDay, groundTexture: surface, groundNormal: normal }); this.changed = true; this.changes.push(said.join(", ")); return `The world now has: ${said.join(", ") }.`; } vehicle(q: Vehicle): string throws string { if (q.dismount ?? false) { this.world = { ...this.world, gameplay: { ...this.world.gameplay, vehicleKind: "none" } }; this.ops.push({ action: "dismount" }); this.changed = true; this.changes.push("on foot"); return "The player is on foot."; } const name = words(q.name ?? ""); if (name === "") { throw "Give the vehicle's name, or dismount."; } const way = q.moves ?? "ground"; const hit = this.modelOf(name, way === "air"); const said = name.split(" "); const named = World.VEHICLES.find((v: string): bool => v !== "none" && said.includes(v)); const kind = named ?? (way === "air" ? "dragon": way === "water" ? "boat": "car"); const address = hit.url; const scale = clamped(q.scale ?? (way === "water" ? 5.2: 3.4), 1.8, 7.5); this.world = { ...this.world, gameplay: { ...this.world.gameplay, vehicleKind: kind }, assets: { ...this.world.assets, vehicleQuery: name, vehicleModelUrl: address } }; this.ops.push({ action: "mount", mount: kind, fly: way === "air", locomotion: way, url: address, scale, weapon: "", label: name }); this.changed = true; this.changes.push(`riding ${name }`); return `The player rides ${name },  with the model "${ hit.title }".`; } hero(q: Hero): string throws string { let said: Vec<string> = []; const name = words(q.model ?? ""); if (name !== "") { const hit = this.modelOf(name, true); if (!hit.animated) { throw `The model "${hit.title }" has no animation,  so a hero cannot wear it. Try another name.`; } const rig = hit.url; this.world = { ...this.world, assets: { ...this.world.assets, heroQuery: name }, kinds: { ...this.world.kinds, hero: { ...this.world.kinds.hero, rig, name } } }; this.ops.push({ action: "avatar", url: rig, label: name }); said.push(`the model "${hit.title }"`); } if (q.speed !== undefined || q.jump !== undefined) { const was = this.world.kinds.hero; const speed = q.speed !== undefined ? clamped(q.speed!, 4.0, 20.0): was.speed; const jump = q.jump !== undefined ? clamped(q.jump!, 6.0, 20.0): was.jump; this.world = { ...this.world, kinds: { ...this.world.kinds, hero: { ...this.world.kinds.hero, speed, jump } } }; this.ops.push({ action: "player", speed, jump }); said.push(`speed ${speed },  jump ${ jump }`); } if (said.length === 0) { throw "Give a model, a speed or a jump."; } this.changed = true; this.changes.push(`hero: ${said.join(", ") }`); return `The hero has ${said.join(" and ") }.`; } rules(q: Rules): string throws string { const was = this.world.rules; const goal = q.goal ?? was.goalType; const target = q.target !== undefined ? Math.maxI64(1, Math.minI64(50, q.target!)): was.targetCount; const collectible = (q.collectible ?? "").trim() !== "" ? q.collectible!.trim(): was.collectible; const seconds = q.seconds !== undefined ? Math.maxI64(0, Math.minI64(600, q.seconds!)): was.timeLimitSec; const speed = q.enemySpeed !== undefined ? clamped(q.enemySpeed!, 0.3, 2.8): 0.0; const health = q.enemyHealth !== undefined ? clamped(q.enemyHealth!, 0.3, 3.5): 0.0; const damage = q.enemyDamage !== undefined ? clamped(q.enemyDamage!, 0.3, 3.0): 0.0; const gravity = q.gravity !== undefined ? clamped(q.gravity!, 0.4, 2.0): 0.0; const banner = (q.banner ?? "").trim(); let said: Vec<string> = []; if (goal !== was.goalType || target !== was.targetCount || collectible !== was.collectible) { said.push(`goal: ${goal } ${ target } ${ collectible }`); } if (seconds !== was.timeLimitSec) { said.push(seconds === 0 ? "no time limit": `${seconds } s`); } if (speed> 0.0) { said.push(`enemy speed ×${speed }`); } if (health> 0.0) { said.push(`enemy health ×${health }`); } if (damage> 0.0) { said.push(`enemy damage ×${damage }`); } if (gravity> 0.0) { said.push(`gravity ×${gravity }`); } if (banner !== "") { said.push("a banner"); } if (said.length === 0) { throw "Nothing would change: the rules are so already."; } const foes = this.world.kinds.foes.map((f: World.WorldFoe): World.WorldFoe => ({ ...f, speed: speed> 0.0 ? clamped(f.speed * speed, 1.2, 10.0): f.speed, hits: health> 0.0 ? Math.maxI64(20, Math.minI64(600, i64(f64(f.hits) * health))): f.hits, damage: damage> 0.0 ? Math.maxI64(2, Math.minI64(60, i64(f64(f.damage) * damage))): f.damage })); const verb = goal === "defeat" ? "Defeat": goal === "score" ? "Score": goal === "survive" ? "Survive": "Collect"; this.world = { ...this.world, kinds: { ...this.world.kinds, foes }, rules: { ...was, goalType: goal, targetCount: target, collectible, timeLimitSec: seconds, objectives: [{ title: `${verb } ${ target } ${ collectible }`, target }] } }; this.ops.push({ action: "rules", goalType: goal, targetCount: target, collectibleName: collectible, enemySpeedMul: speed, enemyHpMul: health, damageMul: damage, gravity, banner }); this.changed = true; this.changes.push(said.join(", ")); return `The rules now have: ${said.join(", ") }.`; } game(q: GameSet): string throws string { let said: Vec<string> = []; const title = (q.title ?? "").trim(); const help = (q.help ?? "").trim(); if (title !== "") { this.world = { ...this.world, title: World.cleanGameTitle(title) }; said.push(`title "${this.world.title }"`); } if (help !== "") { this.world = { ...this.world, help }; said.push("a new help line"); } if (q.camera !== undefined && q.camera! !== this.world.gameplay.cameraMode) { this.world = { ...this.world, gameplay: { ...this.world.gameplay, cameraMode: q.camera! } }; this.reload = true; said.push(`${q.camera! } camera`); } if (said.length === 0) { throw "Nothing would change."; } this.changed = true; this.changes.push(said.join(", ")); return `The game has ${said.join(", ") }.${ this.reload ? " The world restarts with it.": "" }`; } act(q: HeroAction): string { this.ops.push({ action: "clip", clip: q.clip }); return `The hero does: ${q.clip }.`; } say(q: SayInGame): string throws string { const line = q.text.trim(); if (line === "") { throw "Give the line."; } this.ops.push({ action: "chat", text: line }); return "The line is said in the game's chat."; } }
```

The edits of a made world's document: the document as they change it, and what they leave for the running world
(OPS) and the reader (CHANGES). The one applier of an edit to a document: the agent's tools call it, and so does a
person's own hand (put, move, change, remove).

- `find`: `(arg0: string, arg1: string, arg2: bool) => _gizlang_sdk_core.Option<M233.Found>`
- `world`: `M233.WorldDoc`
- `ops`: `Vec<M233.WorldOp>`
- `changes`: `Vec<string>`
- `changed`: `bool`
- `reload`: `bool`
- `stand`: `_gizlang_sdk_core.Option<M233.Stand>`

## Functions

### `constructor`

```gizlang
constructor(find: Finder, world: World.WorldDoc, stand: Option<World.Stand> = undefined)
```

The edits of WORLD, which start with none; a new thing stands before STAND, where the player is.

Effects: fail, read, write, alloc.

### `byHand`

```gizlang
function byHand(e: mut Edits, ask: World.StudioAsk): string throws string
```

Does the edit by hand of ASK on E, and says what it did.

Effects: fail, read, write, alloc.

### `run`

```gizlang
function run(find: Finder, model: Loop.Model, world: World.WorldDoc, message: string, history: Vec<Loop.Message>, prompt: string, stand: Option<World.Stand>, each: (s: Shown) => void): Outcome
```

One run of the game agent with MODEL on WORLD: the person's MESSAGE after HISTORY (the turns before, as text), for
a world made from PROMPT, by a player at STAND. FIND finds a model or a surface by name. EACH hears what the run
shows as it comes.

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

## Methods

### `Edits.put`

```gizlang
put(name: string, address: string, role: string, count: i64, scale: f64, moves: string, color: string, sound: string): Vec<string> throws string
```

Stands COUNT things named NAME, drawn with the model at ADDRESS, before the player: their ids.

Effects: fail, read, write, alloc.

### `Edits.move`

```gizlang
move(id: string, x: f64, z: f64, yaw: f64): void throws string
```

Moves the thing ID to X, Z, turned to YAW: a person dragged it.

Effects: fail, read, write, alloc.

### `Edits.listed`

```gizlang
listed(): string
```

The things of the world in lines, alike ones together: their ids, name, role, size, motion, color and sound.

Effects: fail, read, alloc.

### `Edits.add`

```gizlang
add(q: AddThings): string throws string
```

Puts the things Q names in the world; answers what now stands there, or throws why none could.

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

### `Edits.change`

```gizlang
change(q: ChangeThings): string throws string
```

Changes the things Q names in place (their look, place, size, way of moving, sound); answers what changed.

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

### `Edits.remove`

```gizlang
remove(q: RemoveThings): string throws string
```

Removes the things Q names; answers what left, or throws when none is so named.

Effects: fail, read, write, alloc.

### `Edits.environment`

```gizlang
environment(q: Environment): string throws string
```

Sets the biome, the weather, the hour, the sea and the ground as Q says; answers what changed.

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

### `Edits.vehicle`

```gizlang
vehicle(q: Vehicle): string throws string
```

Gives the hero the vehicle Q names, or takes it away; answers what changed.

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

### `Edits.hero`

```gizlang
hero(q: Hero): string throws string
```

Changes the hero as Q says (the model, the speed, the jump, the weapon); answers what changed.

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

### `Edits.rules`

```gizlang
rules(q: Rules): string throws string
```

Sets the goal and the difficulty of the game as Q says; answers what changed.

Effects: fail, read, write, alloc.

### `Edits.game`

```gizlang
game(q: GameSet): string throws string
```

Sets what Q names of the game itself (its title, its help); answers what changed.

Effects: fail, read, write, alloc.

### `Edits.act`

```gizlang
act(q: HeroAction): string
```

Makes the hero do the one action Q names in the running world; the document stays as it is.

Effects: fail, read, write, alloc.

### `Edits.say`

```gizlang
say(q: SayInGame): string throws string
```

Shows the words of Q in the running world; throws when it has none.

Effects: fail, read, write, alloc.

