A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D
Split the page into a kit, panels and screens behind a map port
SandroMaglioneca624bc5f73f
diff --git a/.oxlintrc.json b/.oxlintrc.jsonindex f35a61f..b3156f0 100644--- a/.oxlintrc.json+++ b/.oxlintrc.json@@ -19,7 +19,33 @@"ritmo/use-random-for-randomness": "error","ritmo/use-ops-for-math": "error","ritmo/keep-simulation-headless": "error",- "ritmo/no-infinity-in-simulation-data": "error"+ "ritmo/no-infinity-in-simulation-data": "error",+ "no-restricted-imports": ["error", {"patterns": [{"regex": "^(\\.\\./)+(view|ui|render|platform|app|labs)/", "message": "The simulation imports only itself: hand the page and the renderer what they need through edits, events, and the view."}]}]+ }+ },+ {+ "files": ["src/view/**"],+ "rules": {+ "ritmo/keep-simulation-headless": "error",+ "no-restricted-imports": ["error", {"patterns": [{"regex": "^(\\.\\./)+(ui|render|platform|app|labs)/", "message": "The view reads the world into plain data (view/contracts.ts): any UI and any renderer read that, so the view imports neither."}]}]+ }+ },+ {+ "files": ["src/ui/**"],+ "rules": {+ "no-restricted-imports": ["error", {"patterns": [{"regex": "^(\\.\\./)+(render|app|labs|game/systems)/", "message": "The page's UI reaches the map only through MapPort, and the world only through the view and edits."}]}]+ }+ },+ {+ "files": ["src/render/**"],+ "rules": {+ "no-restricted-imports": ["error", {"patterns": [{"regex": "^(\\.\\./)+(ui|app|labs|game/systems)/", "message": "A renderer draws the world and the SceneView: it never reaches into a UI or the simulation's systems."}]}]+ }+ },+ {+ "files": ["src/platform/**"],+ "rules": {+ "no-restricted-imports": ["error", {"patterns": [{"regex": "^(\\.\\./)+(view|ui|render|app|labs)/", "message": "Platform adapters (storage, sharing) know the game's data, not the page or the renderer."}]}]}}],diff --git a/DEVELOPMENT.md b/DEVELOPMENT.mdindex 310b4bf..2c1827c 100644--- a/DEVELOPMENT.md+++ b/DEVELOPMENT.md@@ -40,7 +40,7 @@ go off the map (over the tool bar, say) is dropped and shows red while itwould be, as are a right click or Esc mid-drag. On touch the tooltip showsonly for what is about to be built, never while panning or picking, in athin full-width strip under the top line. This lives entirely in `src/render/three/input.ts`-(and the bar in `host.ts`); the game still sees one ordinary press and+(and the bar in `src/ui/dom/index.ts`); the game still sees one ordinary press andrelease.Phones get their layout from `src/ui/dom/styles/phone.css`, loaded after `factory.css`. On@@ -48,7 +48,7 @@ a narrow or short screen the HUD folds away: one top line (clock, treasury,people, and a chip of planks, stone and tools), one tool bar along the bottom(town, the tools, pause (a switch: play while paused), a speed button that cycles, and a ⋯ menu holdingshare, guide, save, load, new town and recentre), and two buttons top right-for the guide and the map overlays. `host.ts` sets `body.fold-goods`,+for the guide and the map overlays. `src/ui/dom/folds.ts` sets `body.fold-goods`,`fold-guide`, `fold-views` or `fold-more` while one is open; a tap on the mapfolds it, a pan or pinch does not. Once the town has a minute of goods(`body.flow-ready`), the goods chip opens the made/used table straight away,@@ -199,33 +199,87 @@ The README shows the film as an animated AVIF, because GitHub plays no## Code-- `src/game/world/iso.ts`: the grid and the unit everything is measured in (1/16 cell = 2×1 px).-- `src/game/content/index.ts`: goods, building kinds, home tiers, costs, upkeep, taxes: plain data.-- `src/game/world/archetypes.ts`: content to components.-- `src/game/world/mapgen.ts`: the land of a new town, from its charter.-- `src/game/schema.ts`: the ECS world: components, relations, resources, services.-- `src/game/world/appeal.ts`: desirability: what buildings and the land do to the cells around them, the whole grid, and one cell explained.-- `src/game/world/places.ts`: street names for the news ("Mill Lane"), from the nearest landmark.-- `src/game/world/grid.ts`: occupancy, entrances, reach, and road paths.-- `src/sim/`: the fixed-step simulation (no renderer access): edits, work (staffing by crew and commute, tools,- pace), logistics, walkers (services, mending), town (households and tiers), hazards (desirability, risk- meters, fire and firefighters, sickness, theft), decay (wear, collapse, spoilage, the pantry), and the term- (seasons and the almanac, the crown, approval, the end of the term, history); orphans drops a walker or field- whose building went before it could join it.-- `src/view/present.ts`: per-frame interaction, panel, and view sync.-- `src/render/art.ts`: all drawing: buildings and their yards, homes, the land, roads, carts, and figures.-- `src/render3d/`: live world renderer, model instances, camera and map input,- overlays, ruins, surface shaders and roof particle emitters.-- `src/render/three/assets/town-props.ts`: parks (ground and furnishings, drawn cell by cell), stone paving and ruins.-- `src/render/three/assets/services.ts`: grain hoist, fish racks, watch tower, herbs,- medical signs, street noticeboard and builder's equipment.-- `src/share/`: sharing a town: the card it is shared as (`card.ts`), the format and limits the page and the- server agree on (`format.ts`), and the page's side: uploading, fetching, and the device's share sheet (`client.ts`).+The source is split into layers, each a folder under `src/`, and each may+import only the layers below it (`.oxlintrc.json` enforces it):++```+app/ start-up: the one module that knows every layer+ui/dom/ the page's UI render/three/ the Three.js renderer+ \ /+ view/ the world as plain data for any UI and any renderer+ |+ game/ the simulation: headless, replays exactly+platform/ storage and sharing (knows game data only) labs/ dev-only pages+```++The layers talk through small typed contracts, so a new UI (a native app)+or a new renderer (Pixi) is a new folder, not a change to the others:++- **Edits** (`src/game/edits.ts`): what the player asks of the town. Every UI, bot and test sends them;+ only the simulation applies them, at the start of its next step (`systems/edits`), and replays record them.+- **`Interaction`** (`src/view/contracts.ts`): what the player is doing with the map. The UI sets the tool,+ the menu, the speed and the map view; the map input sets the pointer and queues gestures.+- **`TownView`** and **`SceneView`** (`src/view/contracts.ts`): what the view shows, filled in once a frame.+ The UI reads the first (panel, numbers, tooltip, toasts), the renderer the second (preview, map view,+ tints, the upgrades' arrows).++One frame runs three schedules: `simulate` (at the chosen speed), then `project` (the view), then `draw`+(the renderer). While the town waits, `settle` keeps the guide's books without a step of time.++- `src/game/`: the simulation, with no page or renderer in it.+ - `schema.ts`: the ECS world: components, relations, resources, the `Edits` and `Rules` services, the events.+ - `edits.ts`: the player's edits, and the queue the page and the view push them to.+ - `content/`: plain data, one module per closed set: `goods.ts`, `kinds/` (one file per category,+ merged in `kinds/index.ts`), `homes.ts`, `parks.ts`, `lands.ts`, `town.ts` (ranks, the crown, fate,+ costs), and the rest. A table every kind must fill is typed `satisfies Record<KindId, …>`, so a new+ kind fails to compile until it is in each.+ - `rules/`: pure calculations: production, households, taxes, approval, the guide's steps, explanations.+ - `world/`: the grid (`iso.ts`, `grid.ts`), the land (`mapgen.ts`), desirability (`appeal.ts`), street+ names (`places.ts`), and content to components (`archetypes.ts`).+ - `systems/<area>/`: one file per system, each area's `index.ts` its public names: edits, work (staffing+ by crew and commute, tools, pace), logistics, walkers (services, mending), town (households and tiers),+ hazards (risk meters, fire and firefighters, sickness, theft), decay (wear, collapse, spoilage, the+ pantry), the term (seasons and the almanac, the crown, approval, the end of the term, history), and+ orphans (a walker or field whose building went before it could join it).+ - `schedule.ts`: `simulate`, `settle`, and the world's resources before a town is founded.+ - `headless.ts`: a town with no page, stepped from code: what the tests, `check`, `balance` and replays play.+- `src/view/`: the world read once a frame (`frame.ts`) into the contracts above, by the `Project` system+ (`project.ts`). It never writes the world.+ - `interaction/`: the pointer's gestures as a selection, drags, and edits.+ - `preview.ts`, `tooltip.ts`, `overlay.ts` (the map views and their legends), `alerts.ts`, `upgrades.ts`,+ `guide.ts`, `stats.ts`, and `panels/` (a building, a home, a park, a ruin): each fills its part.+ - `tools.ts`: every command, tool and map view, defined once: the tool bar, the keys, and the `[homes]`+ chips in messages all come from it.+ - `icons.ts`: every icon name the view can use; each UI must draw them all.+- `src/ui/dom/`: the page. `index.ts` composes it and wires its actions (`controls.ts`); it reaches the map+ only through the `MapPort` the app hands it.+ - `kit/`: the building blocks (rows, lines, sections, sliders, cost chips, pictures, text, icons), each in+ every state on `/ui-kit.html` (`kit/examples.ts`). A new state of one goes there, not into a panel's CSS.+ - `hud.ts`, `toolbar.ts`, `map-views.ts`, `ticker.ts` (the guide, alerts, news), `system-menu.ts`,+ `folds.ts` (a phone's folded HUD), `keys.ts`, `toast.ts`.+ - `panels/`: the side panel's frame, the build menu, the town panel, and what is picked.+ - `screens/`: the charter a town is founded from, sharing (and its card), a visit to a shared town, the+ reckoning at the end of a term, and the confirm dialog.+ - `styles/`: `factory.css`, and `phone.css` after it.+- `src/render/three/`: the live renderer: `services.ts` (its scene and view registries), `sync.ts` (a view+ for each entity), `draw/` (the land, the ground, the map view, the preview, animation, the townsfolk),+ `schedule.ts` (`draw`), `scene.ts` (camera and frame source), `input.ts` (map input), `views.ts`,+ `pool.ts`, `thumbnails.ts` (the pictures the page's menus show), effects and shaders.+ - `assets/`: the models, drawn in code: buildings, homes, people, the land, parks (`town-props.ts`),+ services (`services.ts`: grain hoist, fish racks, watch tower, herbs, medical signs, noticeboard, builder's+ equipment), and `appearance.ts` (material state metadata for catalogue presets; wear, neighbourhood,+ hazards and winter are per-instance attributes, so a state change does not change the geometry cache key).+- `src/render/art.ts`: the town's look shared by every picture of it: ink, dress colours, measures, and the+ 2D drawings the gallery shows.+- `src/platform/`: `save.ts` (saving and loading as [Effect](https://effect.website) programs: a versioned file+ in a slot, the mayor's own or the autosave, typed errors for each way a load can fail) and `share/` (the format+ and limits the page and the server agree on, and the page's side: uploading, fetching, the share sheet).- `src/server/`: the only server code: the routes behind shared towns (`handler.ts`), the Cloudflare Worker thatruns them (`worker.ts`), and the same routes under `pnpm dev`, over a bucket in memory (`dev.ts`).-- `src/render/three/assets/appearance.ts`: material state metadata for catalogue presets.- Wear, neighbourhood, hazards and winter are per-instance attributes in the- live renderer. A state change does not change the geometry cache key.+- `src/app/`: `browser.ts` (start-up: the runtime, the scene, the map input, the page, the loop, saves),+ `lint.ts` (what `pnpm lint:game` checks), and `devtools.ts` (the dev monitor and replay recorder).+- `src/labs/`: the dev-only pages: the art gallery, the asset lab, the map, park, effects and model reviews,+ the service study, and the UI kit.### Reviewing the new town models@@ -264,7 +318,7 @@ and house geometry is checked against its plate bounds. Natural rock depositsuse six seeded silhouettes while retaining their simulation cell footprints.`pnpm check:revisions` renders every catalogue entry, checks the exclamationmark from both sides and verifies animated smoke through rendered pixels.-The chimney plume uses `render3d/smoke-effects.ts` in both the catalogue and+The chimney plume uses `src/render/three/smoke-effects.ts` in both the catalogue andworking buildings. Smoke's GLB stores emitter metadata; particles run at runtime.Open [`/effects-lab.html`](http://localhost:5232/effects-lab.html) to inspect@@ -305,18 +359,11 @@ road setts and the park simulation rules are unchanged.Open `/park-lab.html` for a park assembled from 25 cells using the currentpark layout system, with two street entrances. The three retained furnishingsare framed lanterns, tiered fountains and simple stone busts;-`render3d/fountain-effects.ts` supplies moving droplets and impact ripples in+`src/render/three/fountain-effects.ts` supplies moving droplets and impact ripples inthe actual basin. Park rendering in the game, catalogue, map and preview sharesthe same emitter metadata and water renderer. `pnpm check:park` verifies therendered animation, pause, camera controls and phone layout, and captures stillsand a five-second MP4 in `output/inkline-3d/park-review/`.-- `src/render/three/townsfolk.ts`: residents out walking, for looking at only: not simulated, not saved.-- `src/ui/dom/kit/icons.ts`: the page's line-drawn icons.-- `src/labs/gallery/main.ts`: the dev-only art gallery (see above).-- `src/game/headless.ts`: a town with no page, stepped from code: what the tests, `check`, and `balance` play.-- `src/platform/save.ts`: saving and loading as [Effect](https://effect.website) programs: a versioned file in a slot- (the mayor's own, or the autosave), typed errors for each way a load can fail.-- `src/view/contracts.ts`, `src/ui/dom/host.ts`, `src/app/browser.ts`: the page bridge, camera, input, panels, and start-up.The simulation runs on Ritmo's `FixedLoop` at 60 steps a second (times thechosen speed), with integer state and a seeded random resource: `Town/Rng`,@@ -330,8 +377,8 @@ snapshot the whole world, history and chronicle included, to localStorage;the page also autosaves every two minutes of game time, and **load** takeswhichever is newer.-In development the console has `town.ui`, `town.runtime`, and-`town.advance(seconds)` to fast-forward the simulation.+In development the console has `town.interaction`, `town.shown`, `town.edits`,+`town.runtime`, and `town.advance(seconds)` to fast-forward the simulation.### Simulation rules and lint@@ -342,10 +389,8 @@ In every file, a `runtime.tick(...)` or `runtime.restore(...)` result ischecked (`void` ignores one on purpose), descriptor and system names carrythe `Town/` prefix, and an optional query cell is read with `cell.value()`.-The simulation files, listed in the `overrides` of `.oxlintrc.json`-(`src/sim/`, `src/rules/`, and the pure modules they run: `grid.ts`,-`mapgen.ts`, `places.ts`, `appeal.ts`, `content.ts`, `schema.ts`,-`archetypes.ts`, `iso.ts`), must replay exactly and run headless. They read+The simulation, everything under `src/game/` (one glob in the `overrides` of+`.oxlintrc.json`), must replay exactly and run headless. They readtime from the `Town/Time` clock and randomness from `Town/Rng` or a seeded`Random.make`, never `Date.now()` or `Math.random()`; they never touch thepage, host I/O or the renderer; they keep `Infinity` and `NaN` out of data,@@ -353,8 +398,11 @@ which would save but not load; and they do engine-approximated math(`sin`, `cos`, `atan2`, `exp`, `hypot`, `a ** b`) with `Ops` from`@typeonce/ritmo-math`, which returns the same bits on every engine.`Math.sqrt`, `min`, `max`, `floor`, `abs` and `x ** 2` are exact everywhere-and stay. A new module the simulation runs joins that list. `pnpm lint:game`-then checks the schedules themselves with Ritmo's devtools lints.+and stay. A module the simulation runs belongs under `src/game/`, which is+all the lint needs. The same file keeps each layer to its imports (see+**Code**): the view, for one, imports no UI and no renderer, and is linted+headless too. `pnpm lint:game` then checks the schedules themselves with+Ritmo's devtools lints (`src/app/lint.ts`).## Balancingdiff --git a/package.json b/package.jsonindex c982217..d53e7cf 100644--- a/package.json+++ b/package.json@@ -13,7 +13,7 @@"balance:worth": "node scripts/worth.ts","balance:furniture": "node scripts/furniture.ts","balance:model:check": "node --test test/balance-model.test.ts",- "lint:game": "ritmo-devtools lint src/game/headless.ts",+ "lint:game": "ritmo-devtools lint src/app/lint.ts","replay": "node scripts/replay.ts","check": "pnpm lint && pnpm lint:game && node scripts/check.ts","check:browser": "node scripts/check-browser.ts",diff --git a/src/app/browser.ts b/src/app/browser.tsindex 31f0a91..62676d9 100644--- a/src/app/browser.ts+++ b/src/app/browser.ts@@ -18,10 +18,12 @@ import { blankResources, settle, simulate } from "../game/schedule.ts"import { Edits, Game, Rules } from "../game/schema.ts"import { browserStorage, describe, fileOf, latest, loadLatest, loadText, save, Storage, World } from "../platform/save.ts"import { download, sharedId } from "../platform/share/client.ts"-import type { CameraView } from "../render/three/scene.ts"+import { MapInput } from "../render/three/input.ts"+import { GameScene, type CameraView } from "../render/three/scene.ts"import { draw } from "../render/three/schedule.ts"import { BuildingNodes, FieldNodes, HouseNodes, RockNodes, RuinNodes, StageService, TreeNodes, WalkerNodes } from "../render/three/services.ts"-import { createHost, type Persistence } from "../ui/dom/host.ts"+import { thumbnails } from "../render/three/thumbnails.ts"+import { createDomUi, type Persistence } from "../ui/dom/index.ts"import { blankInteraction, blankShown } from "../view/contracts.ts"import { project } from "../view/schedule.ts"import { Clock, Interaction, Outbox, Shown } from "../view/services.ts"@@ -39,7 +41,7 @@ const loaded = () => {const start = async () => {const mount = document.getElementById("app")!- let persistence: Persistence = { save: () => "Not ready", load: () => "Not ready", shareFile: () => "" }+ let persistence: Persistence<CameraView> = { save: () => "Not ready", load: () => "Not ready", shareFile: () => "" }// A shared town's link (`/t/<id>`): fetch it while the page starts, and load it once the town is up.const shared = sharedId(location.pathname)let arriving: string | null = null@@ -50,7 +52,29 @@ const start = async () => {const interaction = blankInteraction()const shown = blankShown()const edits = editQueue()- const { stage, syncPage, resetPage, toast, visit, visitFailed, visiting } = createHost(mount, () => persistence, { interaction, shown, send: edits.push }, { shared: shared !== null, saved })+ // The renderer draws into `mount`; the page's UI goes around it, and reaches the map only through `MapPort`.+ const stage = new GameScene(mount)+ let input: MapInput | null = null+ const page = createDomUi<CameraView>(+ { interaction, shown, send: edits.push },+ {+ surface: mount,+ focus: (x, y) => stage.focus(x, y),+ resetCamera: () => stage.resetCamera(),+ camera: () => stage.view(),+ picture: (width, height, scale) => stage.picture(width, height, scale),+ pointer: () => input?.tip ?? null,+ confirmStroke: () => input?.confirmStroke(),+ cancelStroke: () => input?.cancelStroke(),+ reset: () => input?.reset(),+ sync: () => input?.sync()+ },+ thumbnails(),+ () => persistence,+ { shared: shared !== null, saved }+ )+ input = new MapInput(stage, interaction, shown, { toast: page.toast, blocked: page.blocked })+ const { syncPage, resetPage, toast, visit, visitFailed, visiting } = pageif (shared)download(shared).then((text) => (arriving = text),diff --git a/src/app/lint.ts b/src/app/lint.tsnew file mode 100644index 0000000..c663762--- /dev/null+++ b/src/app/lint.ts@@ -0,0 +1,29 @@+/**+ * What `ritmo-devtools lint src/app/lint.ts` checks (`pnpm lint:game`, part+ * of `pnpm check`): the simulation on a debug runtime, and the view's+ * `project` and the renderer's `draw` schedules described without running+ * (they need a page). It lives with the app because it is the one place that+ * knows every layer; the simulation's headless entry stays free of the view+ * and the renderer.+ */+import { HostClock } from "@typeonce/ritmo"++import type { CharterValue } from "../game/content/index.ts"+import { founding } from "../game/headless.ts"+import { resolveRules } from "../game/rules/config.ts"+import { blankResources, simulate } from "../game/schedule.ts"+import { Edits, Game, Rules } from "../game/schema.ts"+import { draw } from "../render/three/schedule.ts"+import { project } from "../view/schedule.ts"++const lintCharter: CharterValue = { size: "small", term: 45, land: "fair", seed: 7 }++export const lintTarget = () => {+ const made = Game.Runtime.make({+ services: Game.Runtime.services(Game.Runtime.service(Edits, founding(lintCharter)), Game.Runtime.service(Rules, resolveRules())),+ resources: blankResources(),+ debug: { hostClock: HostClock.test() }+ })+ if (!made.ok) throw new Error(`The town could not start: ${JSON.stringify(made.error)}`)+ return { runtime: made.value, schedules: { simulate }, describe: { project, draw } }+}diff --git a/src/game/headless.ts b/src/game/headless.tsindex b9f4129..131f041 100644--- a/src/game/headless.ts+++ b/src/game/headless.ts@@ -24,14 +24,12 @@ export interface Snapshot {}/** The edit queue of a town about to be founded: one edit founds it on the first step. */-const founding = (charter: CharterValue) => {+export const founding = (charter: CharterValue) => {const edits = editQueue()edits.push({ type: "newTown", charter })return edits}-const lintCharter: CharterValue = { size: "small", term: 45, land: "fair", seed: 7 }-/** The toasts the last step emitted (an inspector: it reads, and does not hold, the stream). */const Toasts = Game.Inspector("Town/Toasts", { events: { toast: Game.System.readEvent(Toast) } }, ({ events }) => events.toast.all())@@ -51,25 +49,6 @@ export const replayTarget = (playback: Replay.Playback) => {return { runtime: made.value, schedules: { simulate }, hostClock }}-/**- * What `ritmo-devtools lint src/game/headless.ts` checks (`pnpm lint:game`,- * part of `pnpm check`): the simulation on a debug runtime, and the view's- * `project` and the renderer's `draw` schedules described without running- * (they need a page).- */-export const lintTarget = async () => {- // Loaded here only: tests and bots that use `headless` never load the view or the renderer.- const { project } = await import("../view/schedule.ts")- const { draw } = await import("../render/three/schedule.ts")- const made = Game.Runtime.make({- services: Game.Runtime.services(Game.Runtime.service(Edits, founding(lintCharter)), Game.Runtime.service(Rules, resolveRules())),- resources: blankResources(),- debug: { hostClock: HostClock.test() }- })- if (!made.ok) throw new Error(`The town could not start: ${JSON.stringify(made.error)}`)- return { runtime: made.value, schedules: { simulate }, describe: { project, draw } }-}-export const headless = (charter: CharterValue, options: { readonly toast?: (message: string) => void; readonly rules?: RuleInputs; readonly hostClock?: HostClock.HostClock } = {}) => {const edits = founding(charter)const made = Game.Runtime.make({ services: Game.Runtime.services(Game.Runtime.service(Edits, edits), Game.Runtime.service(Rules, options.rules ?? resolveRules())), resources: blankResources(), debug: { hostClock: options.hostClock ?? HostClock.test() } })diff --git a/src/labs/ui-kit/main.ts b/src/labs/ui-kit/main.tsnew file mode 100644index 0000000..0cdff3e--- /dev/null+++ b/src/labs/ui-kit/main.ts@@ -0,0 +1,8 @@+/** The page's building blocks in each of their states (see `ui/dom/kit/examples.ts`). */+import { kitExamples } from "../../ui/dom/kit/examples.ts"++/** A placeholder picture: the sheet needs no renderer. */+const placeholder = async (what: unknown) =>+ `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="64" height="56"><rect width="64" height="56" rx="6" fill="#e2e5de"/><text x="32" y="32" font-size="10" text-anchor="middle" fill="#3d4943">${String(what)}</text></svg>`)}`++document.getElementById("kit")!.append(kitExamples(placeholder))diff --git a/src/render/three/thumbnails.ts b/src/render/three/thumbnails.tsnew file mode 100644index 0000000..674a7c2--- /dev/null+++ b/src/render/three/thumbnails.ts@@ -0,0 +1,30 @@+/**+ * Pictures of buildings, homes, and park pieces for the page's menus and+ * panel: each is its 3D model, as the town draws it, turned to the town's+ * camera (see `portrait`), so the picture matches what stands on the map.+ * One model a frame, so a menu of them does not stall the page; each picture+ * is made once and kept. Make one of these for the page, and share it.+ */+import type { GroundId, KindId, PropId, Tier } from "../../game/content/index.ts"+import { pictureAsset } from "./models.ts"+import { portrait } from "./portrait.ts"++export const thumbnails = () => {+ const pictures = new Map<string, Promise<string>>()+ let portraits: Promise<unknown> = Promise.resolve()+ const modelPicture = (make: () => Parameters<typeof portrait>[0]): Promise<string> => {+ const url = portraits.then(() => new Promise<string>((done) => requestAnimationFrame(() => done(portrait(make())))))+ portraits = url.catch(() => undefined)+ return url+ }+ return (what: KindId | Tier | `park:${GroundId | PropId}`, level: number): Promise<string> => {+ const id = `${String(what)}${level > 0 ? `/upgraded${level}` : ""}`+ let url = pictures.get(id)+ if (!url) {+ // The model the map draws, as it stands: one source for the map and every picture of it.+ url = modelPicture(() => pictureAsset(what, level))+ pictures.set(id, url)+ }+ return url+ }+}diff --git a/src/ui/dom/controls.ts b/src/ui/dom/controls.tsnew file mode 100644index 0000000..870e1a0--- /dev/null+++ b/src/ui/dom/controls.ts@@ -0,0 +1,41 @@+/**+ * What the page can do, as named actions: pick a tool, open a panel, place a+ * building, work the clock, show a map view, point the camera somewhere, open+ * a sheet. Each part of the page takes only the actions it uses (a `Pick` of+ * these); `index.ts` wires them to the parts that carry them out.+ */+import type { GroundId, KindId, PropId } from "../../game/content/index.ts"+import type { Tool, View } from "../../view/contracts.ts"++/** What a phone keeps folded away, one at a time: the goods, the guide, the map views, the system menu. */+export type Fold = "goods" | "guide" | "views" | "more"++export interface Controls {+ /** Says something to the player for a few seconds. */+ readonly toast: (message: string) => void+ readonly choose: (tool: Tool) => void+ readonly toggleMenu: (menu: "build" | "town") => void+ /** Picks the tool that places a building, lays park ground, or sets a furnishing. */+ readonly place: (kind: KindId) => void+ readonly layGround: (ground: GroundId) => void+ readonly furnish: (prop: PropId) => void+ readonly setSpeed: (speed: number) => void+ /** Runs a paused town at its last speed, or pauses a running one. */+ readonly togglePause: () => void+ /** Runs a paused town, else goes on to the next speed. */+ readonly cycleSpeed: () => void+ readonly setView: (view: View) => void+ readonly setFold: (fold: Fold | null) => void+ readonly toggleFold: (fold: Fold) => void+ /** Points the page at a place: the camera goes there, it is outlined for a moment, and (when it is a thing) it is selected. */+ readonly goTo: (at: { readonly x: number; readonly y: number }, size: number, id: number | null) => void+ /** What a "[homes]"-style chip does: picks the tool, opens the panel, works the clock, or shows the map view (see `view/tools.ts`). */+ readonly useRef: (name: string) => void+ /** Shows which tool and panel are picked. */+ readonly refreshTools: () => void+ readonly openCharter: (cancellable: boolean) => void+ readonly openShare: () => void+ readonly showReckoning: (open: boolean) => void+ /** Asks before something that replaces the town: save first, go ahead, or stay. */+ readonly confirmLeave: (title: string, text: string, go: string, run: () => void) => void+}diff --git a/src/ui/dom/folds.ts b/src/ui/dom/folds.tsnew file mode 100644index 0000000..89d37d9--- /dev/null+++ b/src/ui/dom/folds.ts@@ -0,0 +1,33 @@+/**+ * Phones: the HUD folds away. On a small screen only the top line and the+ * tool bar stay out; the goods, the guide, the map views, and the system+ * buttons each open on a tap, one at a time, and a tap on the map folds them+ * again.+ */+import type { Interaction } from "../../view/contracts.ts"+import type { Fold } from "./controls.ts"++export const phone = window.matchMedia("(max-width: 760px), (max-height: 500px)")++/** `buttons`: what opens each fold; `covered`: a fold opened over the panel, which closes it. */+export const createFolds = (ui: Interaction, buttons: Readonly<Record<Fold, HTMLElement>>, ticker: HTMLElement, covered: () => void) => {+ let fold: Fold | null = null+ const setFold = (next: Fold | null) => {+ fold = next+ for (const f of ["goods", "guide", "views", "more"] as const) document.body.classList.toggle(`fold-${f}`, f === next)+ buttons.goods.classList.toggle("on", next === "goods")+ buttons.guide.classList.toggle("on", next === "guide")+ buttons.views.classList.toggle("on", next === "views" || ui.view !== "none")+ buttons.more.classList.toggle("on", next === "more")+ if (next !== null) {+ ticker.classList.remove("peek")+ // The panel and a fold would cover each other: the fold wins.+ if (ui.menu !== null || ui.selected !== null) covered()+ }+ }+ return {+ fold: () => fold,+ setFold,+ toggleFold: (f: Fold) => setFold(fold === f ? null : f)+ }+}diff --git a/src/ui/dom/host.ts b/src/ui/dom/host.tsdeleted file mode 100644index 984bdb7..0000000--- a/src/ui/dom/host.ts+++ /dev/null@@ -1,2264 +0,0 @@-/**- * The page around the town: the 3D scene, camera and pointer gestures, and the- * top bar, toolbar, build menu, panel, tooltip, and toasts; the dialog a new- * town is founded from, and the reckoning at the end of its term.- */-import {- categoryColor,- categoryOrder,- costText,- defaultCharter,- goodIds,- goods,- groundIds,- grounds,- home,- kindOrder,- park as parkRules,- propIds,- props,- PAVE_COST,- PAVED_PACE,- tierHome,- winters,- kinds,- landIds,- lands,- mood as moodRules,- needTitle,- LEDGER_TICKS,- RESERVE_MAX,- step,- TRADE_LOAD,- ROAD_COST,- sizeIds,- sizes,- TAX_STEPS,- TERMS,- YEARS_PER_TERM,- type Category,- type CharterValue,- type GoodId,- type GroundId,- type Kind,- type KindId,- type ParkPiece,- type PropId,- type Tier-} from "../../game/content/index.ts"-import { GRID } from "../../game/world/iso.ts"-import { goodIcon, icon, iconEl, type IconName } from "./kit/icons.ts"-import { generate } from "../../game/world/mapgen.ts"-import { askOf, priceOf } from "../../game/schedule.ts"-import type { Edit, TradeMode } from "../../game/edits.ts"-import type { Group, Interaction, Line, Panel, Shown, Stats, Tool, View } from "../../view/contracts.ts"-import { GameScene, type CameraView } from "../../render/three/scene.ts"-import { blobOf, cardFacts, drawCard, metaOf, scaled, verdictOf } from "../../platform/share/card.ts"-import { handheld, offerLink, offerPicture, upload } from "../../platform/share/client.ts"-import { PICTURE_HEIGHT, PICTURE_QUALITY, PICTURE_WIDTH } from "../../platform/share/format.ts"-import { MapInput } from "../../render/three/input.ts"-import { portrait } from "../../render/three/portrait.ts"-import { pictureAsset } from "../../render/three/models.ts"--const el = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, text = "") => {- const node = document.createElement(tag)- node.className = className- node.textContent = text- return node-}-/**- * Sets a node's markup only when it changed. Rewriting it every frame would- * replace the element under the pointer between press and release, and the- * browser then drops the click.- */-const setHtml = (node: HTMLElement, html: string) => {- if (node.dataset.html === html) return- node.dataset.html = html- node.innerHTML = html-}-const escapeHtml = (text: string) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """)--/** Words in the town's messages that name a building or a good, longest first, so "smoked fish" wins over "fish". */-const namedThings: ReadonlyArray<{ readonly word: string; readonly ref: { readonly kind: KindId } | { readonly good: GoodId } }> = [- ...kindOrder.flatMap((id) => {- const title = kinds[id].title.toLowerCase()- return [title, `${title}s`].map((word) => ({ word, ref: { kind: id } }))- }),- ...goodIds.map((id) => ({ word: goods[id].title, ref: { good: id } })),- { word: "loaves", ref: { good: "bread" as GoodId } },- { word: "loaf", ref: { good: "bread" as GoodId } }-].sort((a, b) => b.word.length - a.word.length)-/**- * Tools, panels, and map views a message can name in brackets ("[homes]",- * "[view:fire]"): a chip that picks the tool, opens the panel, or shows the- * view. The key goes beside it, for a keyboard (touch screens hide it).- */-const toolRefs: Readonly<Record<string, { readonly label: string; readonly icon: IconName; readonly key?: string; readonly title: string }>> = {- select: { label: "select", icon: "select", key: "S", title: "Select: pick a building or home to see what it does" },- road: { label: "road", icon: "road", key: "R", title: "Road: drag to lay one" },- pave: { label: "pave", icon: "pave", key: "P", title: "Pave: drag along a road to pave it" },- homes: { label: "homes", icon: "homes", key: "H", title: "Homes: drag along a road to lay a row" },- build: { label: "build", icon: "build", key: "B", title: "Open the build menu" },- bulldoze: { label: "bulldoze", icon: "bulldoze", key: "X", title: "Bulldoze: remove what stands on a cell" },- town: { label: "town", icon: "town", key: "T", title: "Open the town panel: goals, taxes, and trade" },- speed: { label: "speed", icon: "clock", title: "Run time faster: 1×, 2×, 4×" },- pause: { label: "pause", icon: "pause", key: "space", title: "Pause the town, or run it again" },- "view:appeal": { label: "desirability map", icon: "appeal", key: "D", title: "Colour the map by desirability" },- "view:workers": { label: "workers map", icon: "workers", key: "W", title: "Colour the map by workers" },- "view:fire": { label: "fire map", icon: "fire", key: "F", title: "Colour the map by fire risk" },- "view:health": { label: "health map", icon: "health", key: "A", title: "Colour the map by health risk" },- "view:order": { label: "order map", icon: "order", key: "O", title: "Colour the map by the risk of theft" },- "view:condition": { label: "condition map", icon: "condition", key: "C", title: "Colour the map by the condition of buildings" }-}-const namedPattern = new RegExp(- `\\b(${namedThings.map((t) => t.word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})\\b|\\(([A-Z])\\)|\\[([a-z]+(?::[a-z]+)?)\\]`,- "gi"-)-/** Goods as numbers name them ("2 planks", "1 plank"), and as recipes do ("flour → bread"), by title. */-const goodWords: ReadonlyArray<readonly [string, GoodId]> = [- ...goodIds.map((id) => [goods[id].title, id] as const),- ...goodIds.filter((id) => goods[id].title.endsWith("s")).map((id) => [goods[id].title.slice(0, -1), id] as const)-].sort((a, b) => b[0].length - a[0].length)-const goodAlternatives = goodWords.map(([w]) => w).join("|")-const goodOf = (word: string) => goodWords.find(([w]) => w === word)![1]-/**- * Amounts in a line of (escaped) text as icons, where the icon says it as- * well as the word: "3 coins a minute" as a coin and 3/min, "2 planks" as a- * plank and 2, "flour → bread" with each good's icon.- */-const iconize = (html: string): string =>- html- .replace(/([+−-]?)(\d+) coins a minute/g, (_, sign: string, n: string) => `<span class="amount">${sign}${icon("coins", 13)}${n}/min</span>`)- .replace(/(\d+) coins\b/g, (_, n: string) => `<span class="amount">${icon("coins", 13)}${n}</span>`)- .replace(new RegExp(`(\\d+) (${goodAlternatives})\\b`, "g"), (_, n: string, w: string) => `<span class="amount">${goodIcon(goodOf(w), 13)}${n}</span>`)- .replace(new RegExp(`\\b(${goodAlternatives}) → (${goodAlternatives})\\b`, "g"), (_, a: string, b: string) => `<span class="amount">${goodIcon(goodOf(a), 13)}${a}</span> → <span class="amount">${goodIcon(goodOf(b), 13)}${b}</span>`)- // A cost as icons side by side: the dots between them only crowd it.- .replace(/<\/span> · <span class="amount">/g, '</span> <span class="amount">')--/** A finger rather than a mouse: messages say "tap", and keys are left out. */-const touchScreen = () => matchMedia("(pointer: coarse)").matches-/** `{tap|click}`: the first on a touch screen, the second with a mouse. */-const forDevice = (text: string) => text.replace(/\{([^{}|]*)\|([^{}|]*)\}/g, (_, touch: string, mouse: string) => (touchScreen() ? touch : mouse))--/**- * A message as markup: each building it names becomes a chip to click (to- * build one), each good a chip with its icon, each key in round brackets a- * key, and each tool in square brackets a chip that picks it.- * `standing`: buildings the line is about, which stand already; their chips go to them rather than placing another.- * `asks`: when given, only these buildings become chips to place one; others it names are plain words.- */-const callouts = (text: string, standing: ReadonlySet<KindId> = new Set(), asks?: ReadonlySet<KindId>): string =>- escapeHtml(forDevice(text)).replace(namedPattern, (match, word: string | undefined, key: string | undefined, tool: string | undefined) => {- if (key) return `<kbd class="key">${key}</kbd>`- if (tool) {- const ref = toolRefs[tool]- if (!ref) return match- return `<button class="ref tool" data-tool="${tool}" title="${escapeHtml(ref.title)}">${icon(ref.icon, 13)}${ref.label}${ref.key ? `<kbd>${ref.key}</kbd>` : ""}</button>`- }- const thing = namedThings.find((t) => t.word === word!.toLowerCase())- if (!thing) return match- if ("good" in thing.ref) return `<span class="ref ware" title="${goods[thing.ref.good].title}">${goodIcon(thing.ref.good, 13)}${match}</span>`- const kind = kinds[thing.ref.kind]- if (standing.has(thing.ref.kind)) return `<button class="ref kind go" data-go="${thing.ref.kind}" title="Go to the ${kind.title.toLowerCase()}">${icon("locate", 13)}${match}</button>`- return kind.buildable && (!asks || asks.has(thing.ref.kind))- ? `<button class="ref kind" data-kind="${thing.ref.kind}" title="Build a ${kind.title.toLowerCase()}: ${escapeHtml(kind.blurb)}">${icon("build", 13)}${match}</button>`- : `<span class="ref kind" title="${escapeHtml(kind.blurb)}">${icon("town", 13)}${match}</span>`- })--const hex = (color: number) => `#${color.toString(16).padStart(6, "0")}`-/** Seconds as m:ss. */-const clockText = (seconds: number) => `${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`-const signed = (n: number) => `${n > 0 ? "+" : n < 0 ? "−" : ""}${Math.abs(n)}`--export interface Persistence {- readonly save: () => string | null- readonly load: () => string | null- /** The town as a save file (as text), with where the camera stands, to share. */- readonly shareFile: (camera: CameraView) => string-}--/**- * `shared`: the page opened on a shared town's link, so it loads that rather than asking for a charter.- * `saved`: the newest town saved in this browser, which the first charter offers to carry on with.- */-export const createHost = (- mount: HTMLElement,- persistence: () => Persistence,- view: { readonly interaction: Interaction; readonly shown: Shown; readonly send: (edit: Edit) => void },- options: { readonly shared?: boolean; readonly saved?: { readonly title: string; readonly savedAt: number } | null } = {}-) => {- const stage = new GameScene(mount)- const ui = view.interaction- const townView = view.shown.town- const queueEdit = view.send-- // ── DOM ──- // Top left: the term's clock and the treasury, the goods in stock below, then the crown's request and any alarm.- // Top right: the people and their mood, which opens the town's panel.- const hud = el("div", "hud left")- const bar = el("div", "card bar")- const clock = el("div", "cell clock")- const coins = el("div", "cell coins")- bar.append(clock, coins)- const stock = el("div", "card stock")- /** What each good is for, for the stock bar's tooltips: who makes it, who takes it. */- const goodHelp: Readonly<Record<GoodId, string>> = {- wood: "lumberjacks fell it; a sawmill saws it into planks",- planks: "what homes and buildings are built from: a sawmill makes them from wood",- stone: "for building and paving: quarries cut it from rocks, which run out",- ore: "only for tools: once the town has 10 citizens, a mine digs it from the rust-red deposits, and a smithy forges it into tools",- tools: "for building, and workshops wear one out every 50 or 60 jobs: a smithy forges them, or buy them",- fish: "food for settlers: fisheries catch it by the water; it rots in about three minutes",- smoked: "fish that keeps all winter: a smokehouse smokes it",- wheat: "farms grow it; a mill grinds it into flour (not in winter)",- flour: "a mill's; a bakery bakes it into bread",- bread: "food that citizens need, and a cottage with bread pays half again: it rots in about five minutes",- wool: "sheep farms shear it; a weaver weaves it into cloth",- cloth: "citizens need it: only brought to streets with desirability 5 or more",- furniture: "burghers need it in their townhouses: a carpenter makes it from planks; only brought to streets fine enough for townhouses"- }- const groups: ReadonlyArray<ReadonlyArray<GoodId>> = [- ["wood", "planks", "stone", "ore", "tools"],- ["fish", "smoked", "wheat", "flour", "bread"],- ["wool", "cloth", "furniture"]- ]- stock.title = "Goods in the town's storage, and which way each is going · click for what the town makes and uses"- const goodCells = new Map<GoodId, HTMLElement>()- const goodTrends = new Map<GoodId, HTMLElement>()- for (const group of groups) {- const cell = el("div", "group")- for (const g of group) {- const item = el("span", "good")- item.title = `${goods[g].title}: ${goodHelp[g]}`- item.setAttribute("aria-label", goods[g].title)- const glyph = el("span", "glyph")- glyph.innerHTML = goodIcon(g, 18)- const count = el("span", "count", "0")- const trend = el("span", "trend")- item.append(glyph, count, trend)- cell.append(item)- goodCells.set(g, count)- goodTrends.set(g, trend)- }- stock.append(cell)- }- // The crown's request, when there is one, and what threatens the term (or is about to raise the town a rank).- const request = el("div", "card request")- const requestText = el("div", "cell")- const send = el("button", "send", "send")- send.onclick = (event) => {- event.stopPropagation()- queueEdit({ type: "deliver" })- }- request.append(requestText, send)- const alarm = el("div", "card alarm")- // The town ticker: what needs looking at now, and the latest news.- const ticker = el("div", "card ticker")- // The top line: the clock and treasury (on a phone, the people and a chip of the building goods beside them).- const topline = el("div", "topline")- const goodsChip = el("button", "card goods-chip")- goodsChip.title = "The goods in storage"- topline.append(bar, goodsChip)- // Under the goods, on a click: each good's last minute, made against used, and where it is held.- const flowCard = el("div", "card flow")- stock.onclick = (event) => {- event.stopPropagation()- flowCard.classList.toggle("open")- }- flowCard.onclick = () => flowCard.classList.remove("open")- hud.append(topline, stock, flowCard, request, alarm, ticker)- const people = el("div", "hud right")- const folk = el("div", "card bar folk")- const approval = el("div", "cell")- const settlerCell = el("div", "cell")- const citizenCell = el("div", "cell")- const jobs = el("div", "cell jobs")- folk.append(approval, settlerCell, citizenCell, jobs)- people.append(folk)- folk.title = "Open the town's numbers, goals, and levers (T)"- folk.onclick = () => toggleMenu("town")- /** Points the page at a place: the camera goes there, it is outlined for a moment, and (when it is a thing) it is selected. */- const goTo = (at: { readonly x: number; readonly y: number }, size: number, id: number | null) => {- setFold(null)- stage.focus(at.x + size / 2, at.y + size / 2)- ui.flash = { cell: { x: at.x, y: at.y }, size, until: Date.now() + 4000 }- if (id !== null) {- ui.selected = id- ui.menu = null- refreshTools()- }- }- let shortCursor = 0- jobs.onclick = (event) => {- event.stopPropagation()- const short = townView.stats?.shortStaffed ?? []- if (short.length === 0) return toggleMenu("town")- const next = short[shortCursor % short.length]!- shortCursor += 1- goTo(next.cell, next.size, next.id)- }- const tooltip = el("div", "tooltip")- const toastBox = el("div", "toast")- const toolbar = el("div", "toolbar")- const system = el("div", "system")- const panel = el("aside", "panel")- const dialog = el("div", "dialog")- const confirmBox = el("div", "dialog")- const reckoning = el("div", "dialog")- const shareBox = el("div", "dialog share")- const visitBar = el("div", "card visit")- const result = el("button", "result", "")- result.onclick = () => showReckoning(true)- // Map views, and the legend of the one shown.- const views = el("div", "views")- const legend = el("div", "card legend")- // A phone's buttons for what stays folded away: the guide and the map views.- const foldButtons = el("div", "fold-buttons")- const guideBulb = el("button", "card")- guideBulb.title = "The guide, and what needs looking at now"- const guideBadge = el("span", "badge")- guideBulb.append(iconEl("hint", 16), guideBadge)- const layersButton = el("button", "card")- layersButton.title = "Map overlays"- layersButton.append(iconEl("layers", 16))- foldButtons.append(guideBulb, layersButton)- document.body.append(hud, people, tooltip, toastBox, toolbar, system, panel, result, dialog, confirmBox, reckoning, shareBox, visitBar, views, legend, foldButtons)-- // ── Phones: the HUD folds away ──- // On a small screen only the top line and the tool bar stay out; the goods,- // the guide, the map views, and the system buttons each open on a tap, one- // at a time, and a tap on the map folds them again.- const phone = window.matchMedia("(max-width: 760px), (max-height: 500px)")- type Fold = "goods" | "guide" | "views" | "more"- let fold: Fold | null = null- const setFold = (next: Fold | null) => {- fold = next- for (const f of ["goods", "guide", "views", "more"] as const) document.body.classList.toggle(`fold-${f}`, f === next)- goodsChip.classList.toggle("on", next === "goods")- guideBulb.classList.toggle("on", next === "guide")- layersButton.classList.toggle("on", next === "views" || ui.view !== "none")- moreButton.classList.toggle("on", next === "more")- if (next !== null) {- ticker.classList.remove("peek")- // The panel and a fold would cover each other: the fold wins.- if (ui.menu !== null || ui.selected !== null) {- ui.menu = null- ui.selected = null- refreshTools()- }- }- }- const toggleFold = (f: Fold) => setFold(fold === f ? null : f)- goodsChip.onclick = (event) => {- event.stopPropagation()- toggleFold("goods")- }- // The treasury opens the same minute the goods do.- coins.onclick = (event) => {- event.stopPropagation()- if (phone.matches) toggleFold("goods")- else flowCard.classList.toggle("open")- }- guideBulb.onclick = () => toggleFold("guide")- layersButton.onclick = () => toggleFold("views")- // A tap on the map folds them; a pan or a pinch, to look around with the goods or the guide open, does not.- let mapPress: { id: number; x: number; y: number; moved: boolean } | null = null- mount.addEventListener("pointerdown", (e) => {- mapPress = mapPress === null && e.isPrimary ? { id: e.pointerId, x: e.clientX, y: e.clientY, moved: false } : null- })- mount.addEventListener("pointermove", (e) => {- if (mapPress?.id === e.pointerId && Math.hypot(e.clientX - mapPress.x, e.clientY - mapPress.y) > 8) mapPress.moved = true- })- mount.addEventListener("pointerup", (e) => {- // A tap on the map puts a toast away (one it raised itself stays): a pan or a pinch, to look for the spot, does not.- if (mapPress?.id === e.pointerId && !mapPress.moved && toastBox.classList.contains("show") && Date.now() - toastAt > 250) {- window.clearTimeout(toastTimer)- toastBox.classList.remove("show")- }- if (mapPress?.id === e.pointerId && !mapPress.moved && fold !== null) setFold(null)- if (mapPress?.id === e.pointerId) mapPress = null- })- mount.addEventListener("pointercancel", () => (mapPress = null))- // The people's card joins the top line on a phone, and goes back to the top right on a wider screen.- const placeFolk = () => {- if (phone.matches) topline.insertBefore(folk, goodsChip)- else people.append(folk)- if (!phone.matches) setFold(null)- }-- let toastTimer = 0- /** When the toast showing went up: a press on the map just after does not put it away. */- let toastAt = 0- // Long enough to read (about 15 letters a second, and a moment to look), and gone as soon as the mayor- // touches the map again: by then they have read it, or are acting on it.- const toast = (message: string) => {- toastBox.innerHTML = iconize(escapeHtml(message))- toastBox.classList.add("show")- toastAt = Date.now()- window.clearTimeout(toastTimer)- toastTimer = window.setTimeout(() => toastBox.classList.remove("show"), Math.min(8000, Math.max(2200, 1200 + message.length * 65)))- }- // A town is founded behind the dialog, so there is land to look at while choosing.- ui.founding = true- queueEdit({ type: "newTown", charter: defaultCharter((Date.now() & 0x7fffffff) >>> 0) })-- const tools: ReadonlyArray<{- readonly tool: Tool | "build"- readonly label: string- readonly key: string- readonly icon: IconName- readonly tip: string- }> = [- {- tool: "select",- label: "select",- key: "S",- icon: "select",- tip: "Click a building or home to see what it does · drag to pan · wheel to zoom"- },- { tool: "road", label: "road", key: "R", icon: "road", tip: `Drag to lay a road · ${ROAD_COST} coins a cell` },- {- tool: "pave",- label: "pave",- key: "P",- icon: "pave",- tip: `Drag along a road to pave it: carriers and walkers go ${PAVED_PACE}× as fast on it, and homes beside it like it · ${PAVE_COST.coins} coins and ${PAVE_COST.stone} stone a cell`- },- { tool: "home", label: "homes", key: "H", icon: "homes", tip: `Drag to lay a row of homes along a road · ${costText(home.cost)} each` },- { tool: "build", label: "build", key: "B", icon: "build", tip: "Choose a building, then click to place it · Esc to stop" },- { tool: "bulldoze", label: "bulldoze", key: "X", icon: "bulldoze", tip: "Click a building, home, road, tree, or field to remove it" }- ]- // The town's panel is opened most: it has a button of its own, first and larger.- const townButton = el("button", "town-button")- townButton.title = "The town's goals, numbers, taxes, and trade (T)"- townButton.append(iconEl("town", 22), el("span", "", "town"), el("kbd", "", "T"))- townButton.onclick = () => toggleMenu("town")- // Everything the bottom-right buttons do, folded into one menu on a phone.- const moreButton = el("button", "more")- moreButton.title = "Share, the guide, save, load, new town"- moreButton.append(iconEl("more"))- moreButton.onclick = () => toggleFold("more")- const toolGroup = el("div", "group")- toolbar.append(townButton, toolGroup)- const buttons = tools.map((tool) => {- const button = el("button", "")- button.title = tool.tip- button.append(iconEl(tool.icon), document.createTextNode(tool.label), el("kbd", "", tool.key))- button.onclick = () => (tool.tool === "build" ? toggleMenu("build") : choose(tool.tool))- toolGroup.append(button)- return { tool, button }- })- const speedGroup = el("div", "group speed")- toolbar.append(speedGroup)- const speeds = [- { speed: 0, label: "", key: "space" },- { speed: 1, label: "1×", key: "1" },- { speed: 2, label: "2×", key: "2" },- { speed: 4, label: "4×", key: "3" }- ].map(({ speed, label, key }) => {- const button = el("button", speed === 0 ? "pause" : "", label)- if (speed === 0) button.append(iconEl("pause", 14))- button.title = speed === 0 ? "Pause the town, or run it again (space)" : `Run the town at ${label} (${key})`- // Pause is a switch: pressed again it runs the town at its last speed.- button.onclick = () => setSpeed(speed === 0 && ui.speed === 0 ? lastSpeed : speed)- speedGroup.append(button)- return { speed, button }- })- // A phone has room for one speed button: it runs a paused town, else goes on to the next speed.- const cycle = el("button", "cycle")- cycle.title = "Change speed: 1×, 2×, 4×"- cycle.onclick = () => setSpeed(ui.speed === 0 ? lastSpeed : ui.speed === 4 ? 1 : ui.speed * 2)- speedGroup.append(cycle)- toolbar.append(moreButton)- let lastSpeed = 1- const setSpeed = (speed: number) => {- if (speed > 0) lastSpeed = speed- ui.speed = speed- for (const s of speeds) s.button.classList.toggle("on", s.speed === speed)- // Paused, the pause button shows play.- const pause = speeds[0]!.button- pause.replaceChildren(iconEl(speed === 0 ? "play" : "pause", 14))- pause.setAttribute("aria-label", speed === 0 ? "Run the town" : "Pause the town")- cycle.textContent = `${speed === 0 ? lastSpeed : speed}×`- cycle.title = speed === 0 ? `Run the town at ${lastSpeed}×` : "Change speed: 1×, 2×, 4×"- }- setSpeed(1)-- // ── Map views ──- const viewButtons: ReadonlyArray<{ readonly view: Exclude<View, "none">; readonly label: string; readonly key: string; readonly icon: IconName }> = [- { view: "appeal", label: "desirability", key: "D", icon: "appeal" },- { view: "workers", label: "workers", key: "W", icon: "workers" },- { view: "fire", label: "fire", key: "F", icon: "fire" },- { view: "health", label: "health", key: "A", icon: "health" },- { view: "order", label: "order", key: "O", icon: "order" },- { view: "condition", label: "condition", key: "C", icon: "condition" }- ]- const viewsTitle = el("div", "views-title", "map overlays")- viewsTitle.title = "Colour the map by one of these, to see where the town is doing well or badly. Press the key again (or click) to hide it."- views.append(viewsTitle)- const viewEls = viewButtons.map((v) => {- const button = el("button", "")- button.title = `Show ${v.label} over the map (${v.key}); again to hide it`- button.append(iconEl(v.icon, 15), document.createTextNode(v.label), el("kbd", "", v.key))- button.onclick = () => setView(ui.view === v.view ? "none" : v.view)- views.append(button)- return { ...v, button }- })- const setView = (view: View) => {- ui.view = view- for (const v of viewEls) v.button.classList.toggle("on", v.view === view)- // On a phone the list folds away once one is chosen, so the map shows.- if (fold === "views") setFold(null)- layersButton.classList.toggle("on", view !== "none")- }- // Share, the guide, save, load, and new town: a row of icons in the bottom- // right. Founding or loading replaces the town, so each asks first.- const action = (name: IconName, label: string, tip: string, run: () => void) => {- const button = el("button", name === "share" ? "labelled" : "")- button.title = tip- button.append(iconEl(name), document.createTextNode(label))- button.onclick = run- system.append(button)- }- action("share", "share", "Share a picture of the town, or a link anyone can open to watch it and play on", () => void openShare())- // The guide, on or off (it is on for a new mayor).- const guideButton = el("button", "guide-toggle")- guideButton.title = "Turn the guide on or off: it tells a new mayor what to do next, and why"- guideButton.append(iconEl("hint"), document.createTextNode("guide"))- guideButton.onclick = () => ui.guide.push({ type: townView.stats?.guide.off ? "on" : "off" })- system.append(guideButton)- action("save", "save", "Save the town in this browser (it also autosaves every two minutes)", () =>- toast(persistence().save() ?? "Saved")- )- action("load", "load", "Load the latest save: yours or the autosave, whichever is newer", () =>- confirmLeave("load the latest save?", "The town as it is now is replaced by the latest save (yours or the autosave).", "Load", () =>- toast(persistence().load() ?? "Loaded")- )- )- action("newTown", "new town", "Found a new town on new land", () =>- confirmLeave("found a new town?", "This town is left behind for good unless it is saved first.", "Choose new land", () =>- openCharter(true)- )- )- // A phone has no camera bar: this puts the camera back.- action("view", "recentre", "Back to the original angle and centre", () => stage.resetCamera())- system.lastElementChild!.classList.add("phone-only")- // A choice from the menu folds it.- system.addEventListener("click", () => {- if (fold === "more") setFold(null)- })- placeFolk()- phone.addEventListener("change", placeFolk)-- /** Asks before something that replaces the town: save first, go ahead, or stay. */- const confirmLeave = (title: string, text: string, go: string, run: () => void) => {- const wasSpeed = ui.speed- setSpeed(0)- confirmBox.replaceChildren()- const card = el("div", "sheet narrow")- card.append(el("h2", "", title), el("p", "sub", text))- const actions = el("div", "actions")- const close = (resume: boolean) => {- confirmBox.classList.remove("open")- if (resume) setSpeed(wasSpeed)- }- const stay = el("button", "frame primary", "Keep playing")- stay.onclick = () => close(true)- const saveFirst = el("button", "frame", `Save, then ${go.toLowerCase()}`)- saveFirst.onclick = () => {- const failed = persistence().save()- if (failed) return toast(failed)- close(true)- run()- }- const leave = el("button", "frame danger", `${go} without saving`)- leave.onclick = () => {- close(true)- run()- }- actions.append(stay, saveFirst, leave)- card.append(actions)- confirmBox.append(card)- confirmBox.classList.add("open")- stay.focus()- }-- const refreshTools = () => {- for (const { tool, button } of buttons) {- const on = tool.tool === "build" ? ui.menu === "build" || ui.tool === "place" || ui.tool === "ground" || ui.tool === "prop" || ui.tool === "upgrade" : tool.tool === ui.tool- button.classList.toggle("on", on)- }- townButton.classList.toggle("on", ui.menu === "town")- }- const choose = (tool: Tool) => {- ui.tool = tool- ui.dragFrom = null- if (tool !== "place") ui.placing = null- if (tool !== "upgrade") ui.upgrading = null- if (tool !== "select") ui.selected = null- refreshTools()- }- const toggleMenu = (menu: "build" | "town") => {- setFold(null)- ui.menu = ui.menu === menu ? null : menu- if (ui.menu) ui.selected = null- refreshTools()- }- const place = (kind: KindId) => {- ui.placing = kind- ui.menu = null- choose("place")- const title = kinds[kind].title.toLowerCase()- toast(touchScreen() ? `Tap a spot for the ${title}, then tap it again to build it` : `Click to place a ${title} · Esc to stop`)- }- const layGround = (ground: GroundId) => {- ui.ground = ground- ui.menu = null- choose("ground")- const title = grounds[ground].title.toLowerCase()- toast(touchScreen() ? `Drag a rectangle to lay ${title}, then tap Build` : `Drag a rectangle to lay ${title} · Esc to stop`)- }- const furnish = (prop: PropId) => {- ui.prop = prop- ui.menu = null- choose("prop")- const title = props[prop].title.toLowerCase()- toast(touchScreen() ? `Tap a cell of park for the ${title}, then tap it again to set it` : `Click a cell of park to set a ${title} · Esc to stop`)- }- choose("select")-- window.addEventListener("keydown", (event) => {- if (event.target instanceof HTMLElement && event.target.closest("input,select,textarea,[contenteditable=true]")) return- if (event.key === " " && event.target instanceof HTMLButtonElement) return- if (confirmBox.classList.contains("open") && event.key === "Escape") confirmBox.querySelector<HTMLButtonElement>(".primary")?.click()- if (shareBox.classList.contains("open") && event.key === "Escape") closeShare()- if (ui.founding || reckoning.classList.contains("open") || confirmBox.classList.contains("open") || shareBox.classList.contains("open") || visiting) return- if (event.key === " ") {- event.preventDefault()- setSpeed(ui.speed === 0 ? lastSpeed : 0)- }- if (event.key === "1") setSpeed(1)- if (event.key === "2") setSpeed(2)- if (event.key === "3") setSpeed(4)- if (event.key === "Escape") {- // A stroke being drawn or held is dropped first; the tool stays.- if (ui.dragFrom) input.cancelStroke()- else if (ui.tool !== "select") choose("select")- else if (ui.menu) ui.menu = null- else ui.selected = null- refreshTools()- }- const key = event.key.toLowerCase()- if (key === "s") choose("select")- if (key === "r") choose("road")- if (key === "p") choose("pave")- const viewKey = viewButtons.find((v) => v.key.toLowerCase() === key)- if (viewKey) setView(ui.view === viewKey.view ? "none" : viewKey.view)- if (key === "h") choose("home")- if (key === "x") choose("bulldoze")- if (key === "b") toggleMenu("build")- if (key === "t") toggleMenu("town")- })-- // ── History, for the town panel's lines ──- interface Sample {- readonly coins: number- readonly residents: number- readonly stock: Readonly<Record<GoodId, number>>- }- /** One sample every `SAMPLE_SECONDS`, the last ten minutes of them. */- const history: Array<Sample> = []- const SAMPLE_SECONDS = 4- const HISTORY = 150- window.setInterval(() => {- const s = townView.stats- if (!s) return- history.push({ coins: s.coins, residents: s.settlers + s.citizens, stock: s.stock })- if (history.length > HISTORY) history.shift()- }, SAMPLE_SECONDS * 1000)-- /** A thin ink line of `values`, scaled to its own range. */- const sparkline = (values: ReadonlyArray<number>): string => {- if (values.length < 2) return ""- const w = 300- const h = 36- const lo = Math.min(...values)- const hi = Math.max(...values)- const span = hi - lo || 1- const points = values- .map((v, i) => `${((i / (values.length - 1)) * w).toFixed(1)},${(h - 2 - ((v - lo) / span) * (h - 4)).toFixed(1)}`)- .join(" ")- return `<svg viewBox="0 0 ${w} ${h}" preserveAspectRatio="none"><polyline points="${points}" fill="none" stroke="#3d4943" stroke-width="1.5" vector-effect="non-scaling-stroke"/></svg>`- }-- // ── Pictures of buildings, for the build menu and the panel ──- const pictures = new Map<string, Promise<string>>()- /**- * A building (or a home of a tier) drawn as the town draws it, as an image.- * An upgraded market or chapel has no 2D drawing: its model is pictured- * from the town's camera instead (see `portrait`).- */- /**- * Pictures for the build menu and the panel. A building or a home is its 3D model, as the town draws it,- * turned to the town's camera (see `portrait`), so the picture matches what stands on the map; one model- * a frame, so a menu of them does not stall the page. A park piece is its drawing.- */- let portraits: Promise<unknown> = Promise.resolve()- const modelPicture = (make: () => Parameters<typeof portrait>[0]): Promise<string> => {- const url = portraits.then(() => new Promise<string>((done) => requestAnimationFrame(() => done(portrait(make())))))- portraits = url.catch(() => undefined)- return url- }- const both = { right: true, left: true }- const picture = (what: KindId | Tier | `park:${GroundId | PropId}`, level: number | boolean = 0): HTMLImageElement => {- const img = document.createElement("img")- img.className = "thumb"- img.alt = ""- const lvl = Number(level), upgraded = lvl > 0- const id = `${String(what)}${upgraded ? `/upgraded${lvl}` : ""}`- let url = pictures.get(id)- if (!url) {- // The model the map draws, as it stands: one source for the map and every picture of it.- url = modelPicture(() => pictureAsset(what, lvl))- pictures.set(id, url)- }- void url.then((src) => (img.src = src))- return img- }-- // ── Panel ──- /** Volatile parts of the panel, updated in place without rebuilding it. */- let live: Array<() => void> = []- let shape = ""-- /** A section of the panel, its heading with an icon; `help` is kept behind an ⓘ rather than spelled out. */- const section = (heading: string, glyph?: IconName, help?: string) => {- const box = el("section", "")- const h3 = el("h3", "")- if (glyph) h3.append(iconEl(glyph))- h3.append(document.createTextNode(heading))- box.append(h3)- if (help) {- const info = el("button", "help")- info.append(iconEl("info", 15))- info.title = "What this means"- info.setAttribute("aria-expanded", "false")- const text = el("p", "help-text", help)- info.onclick = () => {- const open = !text.classList.contains("open")- text.classList.toggle("open", open)- info.setAttribute("aria-expanded", String(open))- }- h3.append(info)- box.append(text)- }- return box- }- /**- * A slider for one of the town's levers. While it is held, the town's- * numbers do not move it back; `sync` sets it otherwise.- */- const slider = (min: number, max: number, step: number, onInput: (value: number) => void) => {- const input = el("input", "slider")- input.type = "range"- input.min = String(min)- input.max = String(max)- input.step = String(step)- let held = false- input.addEventListener("pointerdown", () => (held = true))- input.addEventListener("pointerup", () => (held = false))- input.addEventListener("blur", () => (held = false))- const paint = () => input.style.setProperty("--at", `${((Number(input.value) - min) / (max - min)) * 100}%`)- input.oninput = () => {- paint()- onInput(Number(input.value))- }- const sync = (value: number) => {- if (held || input.value === String(value)) return- input.value = String(value)- paint()- }- paint()- return { input, sync }- }- const header = (title: string, lead: HTMLElement, below?: HTMLElement) => {- const head = el("header", "")- const text = el("div", "title")- text.append(el("h2", "", title))- if (below) text.append(below)- const closeButton = el("button", "close")- closeButton.title = "Close (Esc)"- closeButton.append(iconEl("close", 18))- closeButton.onclick = () => {- ui.selected = null- ui.menu = null- refreshTools()- }- head.append(lead, text, closeButton)- return head- }-- const lockText = (kind: Pick<Kind, "unlock"> | ParkPiece): string | null => {- const s = townView.stats- if (!s) return null- const residents = s.settlers + s.citizens- if (residents < kind.unlock.residents) return `needs ${kind.unlock.residents} residents`- if (s.citizens < kind.unlock.citizens) return `needs ${kind.unlock.citizens} citizens`- return null- }-- /** A cost as icon chips, each marked when the town is short of it. */- const costChips = (cost: Kind["cost"]) => {- const box = el("span", "chips")- const chips: Array<{ chip: HTMLElement; short: () => boolean }> = []- if (cost.coins > 0) {- const chip = el("span", "chip")- chip.innerHTML = `${icon("coins", 14)}${cost.coins}`- chips.push({ chip, short: () => (townView.stats?.coins ?? 0) < cost.coins })- }- for (const [g, n] of Object.entries(cost.materials) as Array<[GoodId, number]>) {- const chip = el("span", "chip")- chip.title = goods[g].title- chip.innerHTML = `${goodIcon(g, 14)}${n}`- chips.push({ chip, short: () => (townView.stats?.stock[g] ?? 0) < n })- }- box.append(...chips.map((c) => c.chip))- const refresh = () => {- let short = false- for (const c of chips) {- const now = c.short()- c.chip.classList.toggle("short", now)- short ||= now- }- return short- }- return { box, refresh }- }-- const categoryIcon: Readonly<Record<Category, IconName>> = {- storage: "jobs",- gathering: "wood",- crafts: "tools",- food: "bread",- services: "faith"- }-- const buildMenu = () => {- panel.append(header("build", iconEl("build", 30), el("p", "sub", touchScreen() ? "Pick one, then tap the map where it goes" : "Pick one, then click the map · Esc to stop")))- for (const category of categoryOrder) {- const options = kindOrder.filter((id) => kinds[id].category === category && kinds[id].buildable)- if (options.length === 0) continue- const box = section(category, categoryIcon[category])- box.style.borderTop = `3px solid ${hex(categoryColor[category])}`- for (const id of options) {- const kind = kinds[id]- const option = el("button", "option")- const cost = costChips(kind.cost)- cost.box.prepend(el("span", "cap", "cost"))- const terms = el("span", "chips")- const r = kind.recipe- const does = [- r ? `<span class="chip" title="${r.input ? `turns ${r.inputs} ${goods[r.input].title} into ${r.batch} ${goods[r.output].title}` : `makes ${r.batch} ${goods[r.output].title}`} every ${r.seconds} s">${r.input ? `${goodIcon(r.input, 14)}<i>→</i>` : ""}${goodIcon(r.output, 14)}${r.batch > 1 ? r.batch : ""}</span>` : "",- kind.service ? `<span class="chip" title="its walkers serve the homes within ${kind.service.range} road cells of its door">${icon(kind.service.service === "goods" ? "trade" : kind.service.service === "faith" ? "faith" : kind.service.service === "repair" ? "repair" : kind.service.service, 14)}${kind.service.range}</span>` : "",- kind.holds && !kind.recipe && !kind.service ? `<span class="chip" title="holds up to ${kind.holds.cap} of each good${kind.keeps ? `, and keeps food ${kind.keeps} times as long` : ""}">${icon("jobs", 14)}${kind.holds.cap}</span>` : "",- kind.workers > 0 ? `<span class="chip dim" title="${kind.workers} ${kind.crew} to work it, from homes within 15 road cells">${icon(kind.crew === "citizens" ? "citizens" : "settlers", 14)}${kind.workers}</span>` : "",- kind.appeal ? `<span class="chip ${kind.appeal.value < 0 ? "bad" : "good"}" title="desirability ${kind.appeal.value > 0 ? "+" : ""}${kind.appeal.value} beside it, out to ${kind.appeal.reach} cells${kind.appeal.value < 0 ? ": keep homes away" : ": homes like it"}">${icon("appeal", 14)}${kind.appeal.value > 0 ? "+" : ""}${kind.appeal.value}</span>` : "",- `<span class="chip dim" title="upkeep: coins a minute while it stands (a quarter when paused)">${icon("upkeep", 14)}${kind.upkeep}/min</span>`- ].join("")- terms.innerHTML = `<span class="cap">does</span>${does}`- const blurb = el("span", "blurb", kind.blurb)- option.append(picture(id), el("span", "name", kind.title.toLowerCase()), cost.box, terms, blurb)- option.title = `${kind.title}: ${kind.blurb}${kind.workers > 0 ? ` · ${kind.workers} ${kind.crew === "citizens" ? "citizen" : "settler"} workers from within 15 road cells` : ""}${kind.service ? ` · walkers reach ${kind.service.range} road cells` : ""}${kind.appeal ? ` · desirability ${kind.appeal.value > 0 ? "+" : ""}${kind.appeal.value} out to ${kind.appeal.reach} cells` : ""} · ${kind.upkeep} coins a minute`- option.onclick = () => place(id)- live.push(() => {- const lock = lockText(kind)- const short = cost.refresh()- option.disabled = lock !== null- option.classList.toggle("short", lock === null && short)- blurb.textContent = lock ?? kind.blurb- })- box.append(option)- }- panel.append(box)- }- // Parks are laid out piece by piece: ground first, then what stands on it.- const parks = section(- "parks",- "decor",- `Lay ground, then set furnishings on it. Ground that touches is one park: the more it holds the nicer the streets around it (citizens need ${home.tiers[2].appeal}, townhouses ${home.tiers[3].appeal}). One furnishing counts for every ${parkRules.cellsPerProp} cells; where parks meet, the finest counts in full.`- )- parks.style.borderTop = "3px solid #4f9a6a"- const pieces: ReadonlyArray<{ readonly id: GroundId | PropId; readonly piece: ParkPiece; readonly pick: () => void; readonly ground: boolean }> = [- ...groundIds.map((id) => ({ id, piece: grounds[id], pick: () => layGround(id), ground: true })),- ...propIds.map((id) => ({ id, piece: props[id], pick: () => furnish(id), ground: false }))- ]- for (const { id, piece, pick, ground } of pieces) {- if (ground && id === "lawn") parks.append(el("p", "empty", "ground · drag a rectangle"))- if (!ground && id === "lamp") parks.append(el("p", "empty", "furnishings · click a cell of park"))- const option = el("button", "option")- const cost = costChips(piece.cost)- cost.box.prepend(el("span", "cap", "cost"))- const terms = el("span", "chips")- terms.innerHTML = `<span class="cap">does</span><span class="chip good" title="beauty it adds to its park">${icon("appeal", 14)}+${piece.beauty}</span><span class="chip dim" title="upkeep, coins a minute">${icon("upkeep", 14)}${piece.upkeep}/min</span>`- const blurb = el("span", "blurb", piece.blurb)- option.append(picture(`park:${id}`), el("span", "name", piece.title.toLowerCase()), cost.box, terms, blurb)- option.title = `${piece.title}: ${piece.blurb} · beauty +${piece.beauty}${ground ? " a cell" : ""} · ${piece.upkeep} coins a minute`- option.onclick = pick- live.push(() => {- const lock = lockText(piece)- const short = cost.refresh()- option.disabled = lock !== null- option.classList.toggle("short", lock === null && short)- option.classList.toggle("on", (ground ? ui.tool === "ground" && ui.ground === id : ui.tool === "prop" && ui.prop === id))- blurb.textContent = lock ?? piece.blurb- })- parks.append(option)- }- panel.append(parks)- }-- const row = (box: HTMLElement, label: string, glyph?: IconName | GoodId) => {- const line = el("div", "row")- const value = el("span", "count")- const name = el("span", "label")- if (glyph) name.innerHTML = icon(glyph, 16)- name.append(document.createTextNode(label))- line.append(name, value)- box.append(line)- return value- }- /** Sets a number with its sign, coloured by it. */- const money = (node: HTMLElement, n: number, suffix = "") => {- node.textContent = `${signed(n)}${suffix}`- node.classList.toggle("up", n > 0)- node.classList.toggle("down", n < 0)- }-- const townPanel = () => {- const sub = el("p", "sub")- panel.append(header("town", iconEl("town", 30), sub))-- // The town's rank, the goals that raise it to the next, and how near each one is.- const goals = section(- "goals",- "goal",- "The town rises from a hamlet to a village, a town, and a city. Meet every goal of the next rank at once and hold them for two minutes: the crown sends a reward, and the game goes on. A rank is never lost."- )- const goalRows: Array<{ line: HTMLElement; mark: HTMLElement; text: HTMLElement; value: HTMLElement }> = []- const goalNote = el("p", "empty")- panel.append(goals)-- const mood = section(- "approval",- "approval",- `Homes hold more residents when approval is high and fewer when it is low. At −50 for two minutes, the town turns the mayor out.`- )- const moodLine = el("div", "row")- const moodValue = el("span", "count total")- moodLine.append(el("span", "label", "now → heading for"), moodValue)- const moodTrack = el("div", "meter")- const moodFill = el("span", "fill")- moodTrack.append(moodFill)- const moodParts = el("div", "")- mood.append(moodLine, moodTrack, moodParts)- panel.append(mood)-- const treasury = section(- "treasury",- "treasury",- "Coins a minute. Taxes come only from homes that are fed, in proportion to the needs they have met. Administration is reckoned by the home and grows faster than the town: in a large town another cottage costs about what it pays, while a citizens' home or a townhouse holds two or three times the people for the same cost. Three minutes in debt and the town is bankrupt."- )- const taxes = row(treasury, "taxes", "taxes")- const supplied = row(treasury, "if every home were supplied", "homes")- supplied.classList.add("dim")- const suppliedNote = el("p", "empty")- treasury.append(suppliedNote)- const traded = row(treasury, "trade", "trade")- const upkeep = row(treasury, "upkeep", "upkeep")- const admin = row(treasury, "administration", "town")- const net = row(treasury, "a minute", "coins")- net.classList.add("total")- const coinLine = el("div", "spark")- treasury.append(coinLine)- panel.append(treasury)-- const levers = section(- "tax rates",- "taxes",- `Lower taxes raise approval, higher ones lower it, weighed by how many of each tier there are. Above 100% some residents also move away, settlers sooner than citizens, so a higher rate does not always bring in more: under each rate is what the homes would pay at each step once they settle there. Above ${moodRules.grudge.above}% the town also keeps a grudge that outlasts the rate: cutting taxes at the end does not undo a term of squeezing.`- )- for (const tier of ["settlers", "citizens"] as const) {- const line = el("div", "row lever")- const value = el("span", "rate")- const name = el("span", "label")- name.innerHTML = icon(tier, 16)- name.append(document.createTextNode(tier))- line.append(name, value)- const rate = slider(TAX_STEPS[0], TAX_STEPS.at(-1)!, 5, (to) => queueEdit({ type: "tax", tier, rate: to }))- rate.input.classList.add("tax")- rate.input.title = "Drag to set the rate; double-click for the usual 100%"- rate.input.ondblclick = () => queueEdit({ type: "tax", tier, rate: 100 })- const effect = el("p", "effect")- const outlook = el("p", "effect outlook")- outlook.title = "Taxes a minute from every home if this rate stayed there and the homes settled: residents move away above 100%, and approval follows the rate"- levers.append(line, rate.input, effect, outlook)- live.push(() => {- const now = townView.stats?.policy[tier] ?? 100- rate.sync(now)- value.textContent = `${now}%`- const sway = Math.round((100 - now) * moodRules.perTaxPoint)- const grudge = now > moodRules.grudge.above ? " · a grudge builds while it stays this high, and fades slowly" : ""- effect.textContent = (sway === 0 ? "the usual rate" : `${signed(sway)} approval`) + grudge- effect.className = `effect ${sway > 0 ? "up" : sway < 0 ? "down" : ""}`- const steps = townView.stats?.taxOutlook[tier] ?? []- const best = steps.reduce<{ rate: number; taxes: number } | null>((b, o) => (b === null || o.taxes > b.taxes ? o : b), null)- const text = steps.length === 0 || best === null- ? ""- : `taxes a minute, once settled: ${steps.map((o) => `${o.rate}% ${o.taxes}`).join(" · ")} · most at ${best.rate}%`- if (outlook.textContent !== text) outlook.textContent = text- outlook.style.display = text ? "" : "none"- })- }- panel.append(levers)-- const town = section("people", "people")- const settlers = row(town, "settlers", "settlers")- const citizens = row(town, "citizens", "citizens")- const burghers = row(town, "of them, burghers", "burghers")- const homes = row(town, "homes", "homes")- const work = row(town, "jobs filled", "jobs")- const workNote = el("p", "empty")- town.append(workNote)- const peopleLine = el("div", "spark")- town.append(peopleLine)- panel.append(town)-- const needs = section(- "homes supplied",- "homes",- "A hungry home pays no taxes. Settlers need food (fish, smoked fish, or bread) from a market, and a cottage with bread besides pays half again; a visit feeds a home a fish or a loaf for every four residents, so a citizens' home eats twice what a cottage does and a townhouse three times; citizens and burghers need bread, cloth, and faith, and burghers furniture from a carpenter besides. Cloth is only brought to streets with desirability 5 or more, furniture to streets of 12 or more. Fish rots in about three minutes and bread in five; a granary keeps them five times as long."- )- const needIcons: Readonly<Record<"food" | "bread" | "cloth" | "faith" | "furniture", IconName>> = {- food: "fish",- bread: "bread",- cloth: "cloth",- faith: "faith",- furniture: "furniture"- }- const bars = (["food", "bread", "cloth", "faith", "furniture"] as const).map((n) => {- const line = el("div", "need")- // The icon alone says which need: the name is its tooltip, so no word runs under the bar.- const label = el("span", "label")- label.innerHTML = icon(needIcons[n], 16)- label.title = needTitle[n]- label.setAttribute("aria-label", needTitle[n])- const track = el("span", "track")- const fill = el("span", "fill")- track.append(fill)- const value = el("span", "count")- line.append(label, track, value)- needs.append(line)- return { n, fill, value }- })- const larderLine = row(needs, "food in store", "flour")- const eatenLine = row(needs, "eaten · rotted last minute", "fish")- const troubleLine = row(needs, "sick homes · on fire", "health")- panel.append(needs)-- const crown = section(- "the crown",- "crown",- `Sending what the crown asks for earns coins. A missed request is a fine, and the town remembers the snub for a while; too many missed in a row and the crown dismisses the mayor.`- )- const crownLine = row(crown, "requests met · missed", "goal")- const crownNext = el("p", "empty")- crown.append(crownNext)- panel.append(crown)-- const store = section(- "goods and trade",- "trade",- "Traders call every few seconds. Drag a good's slider left to sell it beyond what the town keeps, right to buy it up to that (at half again the price), the further the more each call; the middle neither sells nor buys. Prices fall as the town sells and rise, three times as fast, as it buys; a dear price calms only slowly, so buying tops up a shortfall but a town fed on bought food pays more every minute."- )- const reserveLine = el("div", "row lever")- const reserveValue = el("span", "rate")- reserveLine.append(el("span", "label", "keep in store"), reserveValue)- const reserve = slider(0, RESERVE_MAX, 5, (amount) => queueEdit({ type: "reserve", amount }))- reserve.input.title = "How much of each good the town keeps: it sells only beyond this, and buys only up to it"- store.append(reserveLine, reserve.input, el("p", "effect", "of each good: sold only above this, bought only up to it"))- /** Collections a minute: the traders come at every ledger. */- const perMinute = 60 / (LEDGER_TICKS * step.dt)- const goodRows = goodIds.map((g) => {- const line = el("div", "trade-row")- const name = el("span", "label")- name.innerHTML = goodIcon(g, 16)- name.append(document.createTextNode(goods[g].title))- const value = el("span", "count")- const price = el("span", "price")- const top = el("div", "row")- const change = el("small", "change")- const right = el("span", "label")- right.append(change, value, price)- top.append(name, right)- // Left of the middle sells, right of it buys, the middle neither; how far out is how much a collection.- const deal = slider(-TRADE_LOAD, TRADE_LOAD, 1, (v) =>- queueEdit(v === 0 ? { type: "trade", good: g, mode: "none" } : { type: "trade", good: g, mode: v < 0 ? "sell" : "buy", amount: Math.abs(v) })- )- deal.input.classList.add("deal")- deal.input.title = `Drag left to sell ${goods[g].title}, right to buy it; the further, the more each time the traders call. Double-click to stop trading it.`- deal.input.ondblclick = () => queueEdit({ type: "trade", good: g, mode: "none" })- const mode = el("span", "mode")- const bottom = el("div", "deal-line")- bottom.append(deal.input, mode)- line.append(top, bottom)- store.append(line)- return { g, value, change, mode, price, deal, line }- })- panel.append(store)-- const chronicle = section("chronicle", "chronicle")- const lines = el("div", "chronicle")- chronicle.append(lines)- panel.append(chronicle)-- live.push(() => {- const s = townView.stats- if (!s) return- sub.textContent = `${sizes[s.charter.size].title}, ${lands[s.charter.land].title} land · a ${s.fate.rankTitle} · term ${s.fate.term}, ${s.charter.term} minutes`- sub.title = `seed ${s.charter.seed}`- // Goals.- while (goalRows.length < s.goals.length) {- const line = el("div", "row goal")- const mark = el("span", "mark", "✓")- const text = el("span", "")- const value = el("span", "count")- const left = el("span", "label")- left.append(mark, text)- line.append(left, value)- goals.append(line)- goalRows.push({ line, mark, text, value })- }- goals.append(goalNote)- goalRows.forEach((r, i) => (r.line.style.display = i < s.goals.length ? "" : "none"))- s.goals.forEach((g, i) => {- const r = goalRows[i]!- r.line.classList.toggle("met", g.met)- r.text.textContent = g.text- r.value.textContent = g.value- })- goalNote.textContent = s.fate.nextTitle === null- ? `A ${s.fate.rankTitle}, the highest rank: keep it standing, term after term.`- : s.fate.rises !== null- ? `All met: hold them ${clockText(s.fate.rises)} more and the ${s.fate.rankTitle} is a ${s.fate.nextTitle}.`- : `A ${s.fate.rankTitle} now: these make it a ${s.fate.nextTitle}.`- goalNote.style.display = goalNote.textContent ? "" : "none"- // Approval.- moodValue.textContent = `${signed(s.mood.approval)} → ${signed(s.mood.target)}`- moodValue.classList.toggle("down", s.mood.approval < 0)- moodFill.style.left = `${Math.min(50, 50 + s.mood.approval / 2)}%`- moodFill.style.width = `${Math.abs(s.mood.approval) / 2}%`- moodFill.classList.toggle("low", s.mood.approval < 0)- const parts = s.mood.parts- .map(- (p) =>- `<div class="row"><span class="label">${p.label}</span><span class="count ${p.value > 0 ? "up" : p.value < 0 ? "down" : ""}">${signed(p.value)}</span></div>`- )- .join("")- if (moodParts.dataset.html !== parts) {- moodParts.dataset.html = parts- moodParts.innerHTML = parts- }- money(taxes, s.taxes)- supplied.textContent = `+${s.suppliedTaxes}`- const gap = s.suppliedTaxes - s.taxes- suppliedNote.textContent = gap > Math.max(10, s.taxes * 0.1) ? `${gap} coins a minute left on the table: a hungry home pays nothing, a cottage with bread pays half again, a fine street a quarter more` : ""- suppliedNote.style.display = suppliedNote.textContent ? "" : "none"- money(traded, s.trade)- money(upkeep, -s.upkeep)- money(admin, -s.admin)- money(net, s.net, " coins")- settlers.textContent = String(s.settlers)- citizens.textContent = String(s.citizens)- burghers.textContent = String(s.burghers)- homes.textContent = String(s.homes)- work.textContent = `${s.employed} of ${s.jobs}`- workNote.textContent = s.shortStaffed.length === 0- ? "every workplace has its crew"- : `${s.shortStaffed.length} short of workers: ${s.shortStaffed.slice(0, 4).map((w) => `${w.title} ${w.assigned}/${w.needed}${w.nobodyNear ? " (nobody within reach)" : ""}`).join(", ")}${s.shortStaffed.length > 4 ? ", …" : ""} · click the jobs counter to visit them`- for (const b of bars) {- // Furniture is counted against the townhouses: no other home keeps it.- const of = b.n === "furniture" ? Math.max(s.townhouses, s.needs.furniture) : s.homes- const share = of > 0 ? s.needs[b.n] / of : 0- b.fill.style.width = `${Math.round(share * 100)}%`- b.fill.className = `fill ${of === 0 ? "" : share < 0.34 ? "none" : share < 0.67 ? "low" : ""}`- b.value.textContent = `${s.needs[b.n]}/${of}`- }- larderLine.textContent = `${s.larder}`- eatenLine.textContent = `${s.eaten} · ${s.rotted}`- troubleLine.textContent = `${s.sick} · ${s.burning}`- crownLine.textContent = `${s.crown.met} · ${s.crown.missed}`- crownNext.innerHTML = s.crown.open- ? `Wants <b>${s.crown.amount} ${goods[s.crown.good].title}</b> · ${clockText(s.crown.left)} left · +${s.crown.reward} coins`- : `Next request in ${clockText(s.crown.next)} · ${s.crown.patience} missed in a row ends the term`- // Goods' change over the last minute of samples.- const ago = history[Math.max(0, history.length - 1 - Math.round(60 / SAMPLE_SECONDS))]- reserveValue.textContent = String(s.policy.reserve)- reserve.sync(s.policy.reserve)- for (const r of goodRows) {- const change = ago ? s.stock[r.g] - ago.stock[r.g] : 0- r.change.textContent = change !== 0 ? `${change > 0 ? "+" : ""}${change}/min` : ""- r.change.className = `change ${change > 0 ? "up" : "down"}`- r.value.textContent = String(s.stock[r.g])- const now: TradeMode = s.policy.sells.includes(r.g) ? "sell" : s.policy.buys.includes(r.g) ? "buy" : "none"- const load = s.policy.loads[r.g]- r.deal.sync(now === "sell" ? -load : now === "buy" ? load : 0)- r.mode.textContent = now === "none" ? "no trade" : `${now} ≤${Math.round(load * perMinute)}/min`- r.mode.className = `mode ${now}`- r.line.className = `trade-row ${now}`- const price = now === "buy" ? askOf(r.g, s.prices[r.g]) : Math.round(priceOf(r.g, s.prices[r.g]) * 10) / 10- r.price.textContent = `${price}c${s.prices[r.g] !== 100 ? ` (${s.prices[r.g]}%)` : ""}`- r.price.title = now === "buy" ? "what traders ask for one" : "what traders pay for one"- }- const samples = s.history- const coinsSvg = sparkline(samples.map((h) => h.coins))- if (coinLine.dataset.svg !== coinsSvg) {- coinLine.dataset.svg = coinsSvg- coinLine.innerHTML = coinsSvg- }- const peopleSvg = sparkline(samples.map((h) => h.residents))- if (peopleLine.dataset.svg !== peopleSvg) {- peopleLine.dataset.svg = peopleSvg- peopleLine.innerHTML = peopleSvg- }- const recent = s.chronicle- .slice(-10)- .reverse()- .map((l) => `<p><b>${clockText(Math.round(l.tick / 60))}</b> ${l.text}</p>`)- .join("")- if (lines.dataset.html !== recent) {- lines.dataset.html = recent- lines.innerHTML = recent- }- })- }-- /** A line of a description as a row: its icon and what it is about, its value, any goods as chips, and a note underneath saying why. */- const renderLine = (row: HTMLElement, l: Line) => {- // Every row the same way: what on the left, its value on the right, any note under both.- row.className = `line ${l.tone ?? ""}`- const chips = l.goods ? `<span class="chips">${l.goods.map((g) => `<span class="chip" title="${goods[g.good].title}">${goodIcon(g.good, 14)}${g.count}</span>`).join("")}</span>` : ""- // Goods take the value's place: chips on the right.- row.innerHTML = `<span class="mark">${icon(l.icon, 16)}</span><span class="what"></span>${chips || `<span class="count"></span>`}${l.note ? `<small class="note"></small>` : ""}`- row.querySelector<HTMLElement>(".what")!.textContent = l.label- if (!chips) row.querySelector<HTMLElement>(".count")!.innerHTML = iconize(escapeHtml(l.value ?? ""))- if (l.note) row.querySelector<HTMLElement>(".note")!.innerHTML = iconize(escapeHtml(l.note))- // What to do about it, on a row of its own: a building to place shows its picture, and the row places one.- if (l.fix) {- const build = l.fix.build && kinds[l.fix.build].buildable ? l.fix.build : undefined- const fix = el(build ? "button" : "div", "fix")- if (build) {- fix.dataset.build = build- fix.title = `Build a ${kinds[build].title.toLowerCase()}`- fix.append(picture(build))- } else fix.append(iconEl("hint", 14))- const text = el("span", "do")- text.innerHTML = iconize(callouts(l.fix.text))- fix.append(text)- row.append(fix)- }- }- /** Keeps a box of rows in step with its lines, touching only the rows that changed. */- const syncLines = (box: HTMLElement, rows: Array<HTMLElement>, lines: ReadonlyArray<Line>) => {- while (rows.length < lines.length) {- const line = el("div", "line")- rows.push(line)- box.append(line)- }- rows.forEach((row, i) => {- const l = lines[i]- const text = l ? JSON.stringify(l) : ""- if (row.dataset.text === text) return- row.dataset.text = text- row.style.display = l ? "" : "none"- if (l) renderLine(row, l)- })- }-- /** What upgrading a building would give and cost, said in full before the mayor pays for it, with the button that begins it. */- /** An upgrade's gain as a row: what changes, on the left; from what to what, on the right. */- const gainLine = (change: string): Line => {- const at = change.search(/[+−-]?\d/)- const label = at > 0 ? change.slice(0, at).trim() : change, rest = at > 0 ? change.slice(at) : undefined- // "+6 → +8, out to 6 cells": the change on the right, what qualifies it under it.- const [value, note] = rest?.includes(", ") ? [rest.slice(0, rest.indexOf(", ")), rest.slice(rest.indexOf(", ") + 2)] : [rest, undefined]- const glyph: IconName =- /^holds/.test(change) ? "build" : /^carriers/.test(change) ? "trade" : /^walker|^a walker/.test(change) ? "people"- : /^administration/.test(change) ? "taxes" : /^desirability/.test(change) ? "appeal" : /^workers/.test(change) ? "workers"- : /^firefighters/.test(change) ? "fire" : /rock/.test(change) ? "stone" : /a minute/.test(change) ? "jobs" : "goal"- return { icon: glyph, label, value, note, tone: "ok" }- }- /**- * What upgrading a building would cost and give, said in full before the mayor pays: what is paid now, what- * carriers bring later and the upkeep after; then what it gives. The button that begins it comes last, alone.- */- const upgradeBox = (offer: NonNullable<Extract<Panel, { type: "building" }>["upgrade"]>) => {- const box = section(`upgrade to ${offer.title.toLowerCase()}`, "build", offer.blurb)- box.classList.add("lines", "upgrade")- // Why the town needs it now (the gold arrow over it), or that it can wait.- const needed = el("p", "needed", "")- const pays = el("div", "part"), gets = el("div", "part")- const payRows: Array<HTMLElement> = [], getRows: Array<HTMLElement> = []- const upkeep = offer.changes.find((c) => c.startsWith("upkeep"))- syncLines(gets, getRows, offer.changes.filter((c) => c !== upkeep).map(gainLine))- const begin = el("button", "frame primary", `Begin the ${offer.title.toLowerCase()}`)- begin.onclick = () => {- const now = townView.panel- if (now?.type === "building") queueEdit({ type: "upgrade", cell: now.cell })- }- const why = el("p", "sub", "")- live.push(() => {- const now = townView.panel- if (now?.type !== "building" || !now.upgrade) return- needed.textContent = now.upgrade.needed ? `needed now: ${now.upgrade.needed}` : "not needed yet: the gold arrow shows when it is"- needed.classList.toggle("now", now.upgrade.needed !== null)- const coins = townView.stats?.coins ?? 0, stock = townView.stats?.stock- const materials = (Object.entries(offer.cost.materials) as Array<[GoodId, number]>).filter(([, n]) => n > 0)- const lacking = materials.filter(([g, n]) => (stock?.[g] ?? 0) < n).map(([g, n]) => `${n - (stock?.[g] ?? 0)} ${goods[g].title}`)- syncLines(pays, payRows, [- { icon: "coins", label: "now, to begin", value: `${offer.cost.coins} coins`, tone: coins < offer.cost.coins ? "bad" : undefined,- note: coins < offer.cost.coins ? `${offer.cost.coins - coins} coins short` : undefined },- ...(materials.length > 0- ? [{ icon: "trade", label: "later, carried in", goods: materials.map(([good, count]) => ({ good, count })), tone: lacking.length > 0 ? "warn" : undefined,- note: lacking.length > 0 ? `the store lacks ${lacking.join(", ")}: the works wait for them` : "from the store: the works go on as they come" } as Line]- : []),- ...(upkeep ? [{ icon: "upkeep", label: "upkeep after", value: upkeep.replace(/^upkeep /, "") } as Line] : [])- ])- begin.disabled = now.upgrade.blocked !== null || now.upgrade.short !== null- why.textContent = now.upgrade.blocked ?? now.upgrade.short ?? "it goes on working while the works go on, and scaffolding rises inside its plot"- })- const footer = el("div", "begin")- footer.append(begin, why)- box.append(needed, el("p", "cap", "you pay"), pays, el("p", "cap", "you get"), gets, footer)- return box- }-- const buildPanel = () => {- const p = townView.panel- live = []- panel.replaceChildren()- if (!p) return- if (p.type === "build") return buildMenu()- if (p.type === "town") return townPanel()-- const title = p.type === "building" ? p.title.toLowerCase() : p.type === "house" ? tierHome[p.tier] : p.type === "park" ? "park" : "ruin"- const status = el("span", "pill")- const about = el(- "p",- "sub",- p.type === "building"- ? p.blurb- : p.type === "ruin"- ? "bulldoze it to clear the plot"- : p.type === "park"- ? "every cell of ground that touches this one: lay more, or set furnishings on it, to make the streets around it nicer"- : p.tier === 1- ? "settlers live here and do the rough work"- : p.tier === 2- ? "citizens live here, do the skilled work, and pay twice as much"- : "burghers live here, and pay the best taxes"- )- const below = el("div", "")- // A building says how it is in a box of its own, under the header; a home, a park, or a ruin in a word here.- if (p.type === "building") below.append(about)- else below.append(status, about)- panel.append(header(title, p.type === "ruin" ? iconEl("condition", 30) : p.type === "park" ? iconEl("decor", 30) : picture(p.type === "building" ? p.kind : p.tier, p.type === "building" ? p.level : 0), below))- // A building: how it is first, and what to do about it; then its upgrade, when it has one to offer.- if (p.type === "building") {- const box = section("status", p.problem !== null ? "warning" : "jobs")- box.classList.add("lines", "state")- const rows: Array<HTMLElement> = []- live.push(() => {- const now = townView.panel- if (now?.type !== "building") return- box.classList.toggle("trouble", now.problem !== null)- syncLines(box, rows, [now.state])- })- panel.append(box)- }- if (p.type === "building" && p.upgrade) panel.append(upgradeBox(p.upgrade))- // A building or a home: its rows in boxes under headings, kept in step with what the town says each frame.- // A park or a ruin: one box.- const holder = el("div", "")- panel.append(holder)- const slots: Array<{ box: HTMLElement; rows: Array<HTMLElement>; key: string }> = []- const syncGroups = (groups: ReadonlyArray<Group>) => {- groups.forEach((g, i) => {- const key = `${g.title}|${g.icon}|${g.help ?? ""}`- let slot = slots[i]- if (!slot || slot.key !== key) {- const box = section(g.title, g.icon, g.help)- box.classList.add("lines")- const fresh = { box, rows: [] as Array<HTMLElement>, key }- if (slot) slot.box.replaceWith(box)- else holder.append(box)- slots[i] = fresh- slot = fresh- }- slot.box.style.display = ""- syncLines(slot.box, slot.rows, g.lines)- })- for (const extra of slots.slice(groups.length)) extra.box.style.display = "none"- }- live.push(() => {- const now = townView.panel- if (!now || now.type === "build" || now.type === "town" || now.type !== p.type) return- if (now.type === "building") {- status.textContent = now.problem ?? now.status- status.className = `pill ${now.problem !== null ? "problem" : now.status === "working" ? "working" : ""}`- syncGroups(now.groups)- } else if (now.type === "house") {- status.textContent = now.summary- status.className = `pill ${now.trouble ? "problem" : "working"}`- syncGroups(now.groups)- } else if (now.type === "park") {- status.textContent = `${now.value > 0 ? "+" : ""}${now.value} out to ${now.reach} cells`- status.className = "pill working"- syncGroups([{ title: "what makes it", icon: "appeal", lines: now.lines }])- } else {- status.textContent = now.cause- status.className = "pill problem"- syncGroups([{ title: "what is left", icon: "condition", lines: now.lines }])- }- })- if (p.type === "building" && p.kind === "townhall") return- {- const actions = el("div", "actions")- if (p.type === "building") {- const pause = el("button", "frame", "Pause work")- pause.title = "A paused building has no workers and pays a quarter of its upkeep"- pause.onclick = () => {- const now = townView.panel- if (now?.type === "building") queueEdit({ type: "pause", cell: now.cell, on: !now.paused })- }- live.push(() => {- const now = townView.panel- if (now?.type !== "building") return- pause.textContent = now.paused ? "Resume work" : "Pause work"- pause.style.display = now.pausable ? "" : "none"- })- actions.append(pause)- }- if (p.type === "house") {- const close = el("button", "frame", "Close home")- close.title = "A closed home stands empty: no taxes, and no cost to the town hall's administration. Reopen it and settlers move back in."- close.onclick = () => {- const now = townView.panel- if (now?.type === "house") queueEdit({ type: "pause", cell: now.cell, on: !now.closed })- }- live.push(() => {- const now = townView.panel- if (now?.type !== "house") return- close.textContent = now.closed ? "Reopen home" : "Close home"- close.style.display = now.closable ? "" : "none"- })- actions.append(close)- }- const remove = el("button", "frame danger", "Remove")- remove.onclick = () => queueEdit({ type: "remove", cell: p.cell })- live.push(() => {- const now = townView.panel- remove.title = now?.type === "building" || now?.type === "house" ? now.salvage : ""- })- actions.append(remove)- panel.append(actions)- }- }-- /** Rebuilds the panel when what it shows changes, and refreshes its numbers every frame. */- const syncPanel = () => {- const p = townView.panel- const next =- p === null- ? ""- : p.type === "build" || p.type === "town"- ? p.type- : `${p.type}|${p.id}|${p.type === "house" ? p.tier : p.type === "building" ? `${p.level}|${p.upgrade?.title ?? ""}|${p.works !== null}` : ""}`- if (next !== shape) {- shape = next- // Keep the old contents while the closed panel slides away.- if (p !== null) buildPanel()- panel.classList.toggle("open", p !== null)- document.body.classList.toggle("panel-open", p !== null)- refreshTools()- }- for (const update of live) update()- }-- // ── Founding a town ──- const savedTown = options.saved ?? null- const choice: { -readonly [K in keyof CharterValue]: CharterValue[K] } = defaultCharter((Date.now() & 0x7fffffff) >>> 0)- /** The land a charter would found, drawn small: a dot a cell. */- const drawLand = (canvas: HTMLCanvasElement) => {- const land = generate(choice)- const water = new Set(land.water.map((c) => c.y * GRID + c.x))- const trees = new Set(land.trees.map((t) => t.cell.y * GRID + t.cell.x))- const rocks = new Map(land.rocks.map((r) => [r.cell.y * GRID + r.cell.x, r.ore]))- const scale = 3- canvas.width = GRID * scale- canvas.height = GRID * scale- const g = canvas.getContext("2d")!- g.clearRect(0, 0, canvas.width, canvas.height)- for (let y = 0; y < GRID; y++) {- for (let x = 0; x < GRID; x++) {- const k = y * GRID + x- if (land.mask[k] !== "1") continue- g.fillStyle = water.has(k)- ? "#b9cfcb"- : trees.has(k)- ? "#9bb08a"- : rocks.has(k)- ? rocks.get(k)- ? "#5a6479"- : "#8f9891"- : "#f7f8f4"- g.fillRect(x * scale, y * scale, scale, scale)- }- }- return land- }- const openCharter = (cancellable: boolean) => {- ui.founding = true- dialog.replaceChildren()- const card = el("div", "sheet wide")- const title = el("h2", "title", "Inkline Town ")- const by = el("small", "by", "by ")- const author = el("a", "", "Sandro Maglione") as HTMLAnchorElement- author.href = "https://www.sandromaglione.com/"- author.target = "_blank"- author.rel = "noopener"- by.append(author)- title.append(by)- card.append(title, el("p", "sub", "Pick the land, then grow the town to its goal before the term runs out."))- const body = el("div", "charter")- const options = el("div", "")- const preview = el("div", "preview")- const mapBox = el("div", "map")- const map = document.createElement("canvas")- mapBox.append(map)- const facts = el("p", "note facts")- const cells = el("span", "ref ware cells")- mapBox.append(cells)- preview.append(mapBox, facts)- body.append(options, preview)- /** A figure of the charter as the town's messages name things: an icon and a few words. */- const chip = (glyph: IconName, text: string) => `<span class="ref ware">${icon(glyph, 13)}${escapeHtml(text)}</span>`- const group = <T extends string | number>(- title: string,- glyph: IconName,- values: ReadonlyArray<T>,- label: (v: T) => string,- note: (v: T) => string,- tip: (v: T) => string,- get: () => T,- set: (v: T) => void- ) => {- const box = section(title, glyph)- const row = el("div", "choices")- const said = el("p", "note")- const buttons = values.map((v) => {- const b = el("button", "frame choice", label(v))- b.title = tip(v)- b.onclick = () => {- set(v)- refresh()- }- row.append(b)- return { v, b }- })- box.append(row, said)- options.append(box)- return () => {- for (const { v, b } of buttons) b.classList.toggle("on", v === get())- setHtml(said, note(get()))- }- }- const refreshers = [- group(- "land",- "pave",- sizeIds,- (v) => sizes[v].title,- (v) => `a town at ${chip("people", `${sizes[v].goal.residents} residents`)}${chip("coins", `+${sizes[v].goal.income}/min`)}`,- (v) => `The town rank asks ${sizes[v].goal.residents} residents and an income of ${sizes[v].goal.income} coins a minute; a village less, a city more`,- () => choice.size,- (v) => (choice.size = v)- ),- group(- "term",- "clock",- TERMS,- (v) => `${v} min`,- (v) => `${chip("clock", `${YEARS_PER_TERM} years of ${Number((v / YEARS_PER_TERM).toFixed(1))} min`)}${chip("winter", `${YEARS_PER_TERM - 1} winters`)}`,- (v) => `${v} minutes at 1×: ${YEARS_PER_TERM} years, a winter at the end of each after the first`,- () => choice.term,- (v) => (choice.term = v)- ),- group(- "richness",- "wood",- landIds,- (v) => lands[v].title,- (v) => chip("coins", `${lands[v].coins} coins to start`),- (v) => lands[v].blurb,- () => choice.land,- (v) => (choice.land = v)- )- ]- // The land is drawn from a seed: a fresh one on a click; the number itself only on hover, for whoever wants it.- const reroll = el("button", "frame reroll", "↻ other land")- reroll.onclick = () => {- choice.seed = (Math.random() * 0x7fffffff) >>> 0- refresh()- }- mapBox.append(reroll)- const refresh = () => {- for (const r of refreshers) r()- const land = drawLand(map)- reroll.title = `Draw other land (this is seed ${choice.seed})`- const rocks = land.rocks.filter((r) => !r.ore).length- setHtml(cells, `${icon("pave", 13)}${land.cells} cells`)- setHtml(facts, chip("wood", `${land.trees.length} trees`) + chip("stone", `${rocks} stone`) + chip("ore", `${land.rocks.length - rocks} ore`))- }- refresh()- const actions = el("div", "actions")- const found = el("button", "frame primary", "Start new")- found.onclick = () => {- if (visiting) endVisit()- queueEdit({ type: "newTown", charter: { ...choice } })- history.length = 0- ui.selected = null- ui.menu = null- closeCharter()- showReckoning(false)- choose("select")- setSpeed(1)- }- actions.append(found)- // On the way in, a town saved in this browser: carry on with it instead.- if (!cancellable && savedTown) {- const saved = savedTown- const resume = el("button", "frame", "Continue saved")- resume.title = `Carry on with the town saved in this browser: ${saved.title}${saved.savedAt > 0 ? `, saved ${ago(saved.savedAt)}` : ""}`- resume.onclick = () => {- const said = persistence().load() ?? "Loaded"- toast(said)- if (!said.startsWith("Loaded")) return- history.length = 0- ui.selected = null- ui.menu = null- closeCharter()- choose("select")- setSpeed(1)- }- actions.append(resume)- }- if (cancellable) {- const cancel = el("button", "frame", "Cancel")- cancel.onclick = () => closeCharter()- actions.append(cancel)- }- card.append(body, actions)- dialog.append(card)- dialog.classList.add("open")- }- const closeCharter = () => {- ui.founding = false- dialog.classList.remove("open")- }- if (!options.shared) openCharter(false)-- // ── Sharing ──- // A picture of the town (the card), to post anywhere; and, on asking, a link- // that opens the town itself, running, for anyone to watch and play on.- let shareSpeed = 1- const closeShare = () => {- if (!shareBox.classList.contains("open")) return- shareBox.classList.remove("open")- for (const img of shareBox.querySelectorAll("img")) URL.revokeObjectURL(img.src)- if (!visiting) setSpeed(shareSpeed)- }- shareBox.onclick = (event) => {- if (event.target === shareBox) closeShare()- }- const openShare = async () => {- const s = townView.stats- if (!s || shareBox.classList.contains("open")) return- shareSpeed = ui.speed- setSpeed(0)- const facts = cardFacts(s)- let card: HTMLCanvasElement, png: Blob- try {- card = await drawCard(stage.picture(PICTURE_WIDTH, PICTURE_HEIGHT, 2), facts, location.host)- png = await blobOf(card, "image/png")- } catch (error) {- setSpeed(shareSpeed)- return toast(error instanceof Error ? error.message : "The picture could not be made")- }- const name = `inkline-town-${s.fate.state === "playing" ? `year-${s.season.year}` : `term-${s.fate.term}`}.png`- const caption = `${metaOf(facts).title} · ${metaOf(facts).description}`- shareBox.replaceChildren()- const sheet = el("div", "sheet")- sheet.append(el("h2", "", "share the town"))- const preview = document.createElement("img")- preview.className = "card-preview"- preview.alt = caption- preview.src = URL.createObjectURL(png)- sheet.append(preview)- sheet.append(el("p", "sub", "Post the picture anywhere, or make a link: whoever opens it sees this town running, from where you look at it now, and can play on from here."))- const actions = el("div", "actions")- const picture = el("button", "frame primary", "")- picture.append(iconEl("picture", 15), document.createTextNode(handheld() ? "Share the picture" : "Save the picture"))- picture.onclick = async () => toast(await offerPicture(png, name, caption))- const link = el("button", "frame", "")- link.append(iconEl("link", 15), document.createTextNode("Make a link"))- const linkRow = el("div", "link-row")- link.onclick = async () => {- link.disabled = true- link.lastChild!.textContent = "Making a link…"- try {- const small = await blobOf(scaled(card, PICTURE_WIDTH, PICTURE_HEIGHT), "image/jpeg", PICTURE_QUALITY)- const made = await upload(persistence().shareFile(stage.view()), small, metaOf(facts))- link.remove()- const field = document.createElement("input")- field.readOnly = true- field.value = made.url- field.onfocus = () => field.select()- const copy = el("button", "frame primary", handheld() ? "Share the link" : "Copy the link")- copy.onclick = async () => toast(await offerLink(made.url, metaOf(facts).title))- const open = el("a", "frame", "Open")- open.setAttribute("href", made.url)- open.setAttribute("target", "_blank")- open.setAttribute("rel", "noopener")- linkRow.replaceChildren(field, copy, open)- linkRow.classList.add("show")- copy.focus()- } catch (error) {- link.disabled = false- link.lastChild!.textContent = "Try the link again"- toast(error instanceof Error ? error.message : "Could not share the town")- }- }- const close = el("button", "frame", "Close")- close.onclick = () => closeShare()- actions.append(picture, link, close)- sheet.append(actions, linkRow)- shareBox.append(sheet)- shareBox.classList.add("open")- picture.focus()- }-- // ── Visiting a shared town ──- // The town runs as its mayor left it, with the building tools put away,- // until the visitor plays on from here (it becomes theirs, and autosaves)- // or founds a town of their own.- let visiting = false- const endVisit = () => {- visiting = false- document.body.classList.remove("visiting")- visitBar.classList.remove("show")- window.history.replaceState(null, "", "/")- }- /** The shared town is loaded: show it running. */- const visit = (title: string) => {- visiting = true- closeCharter()- resetPage()- document.body.classList.add("visiting")- visitBar.replaceChildren()- const text = el("div", "cell")- text.append(iconEl("town", 18))- const words = el("span", "stack")- words.append(el("b", "", "a shared town"), el("small", "", title))- text.append(words)- const play = el("button", "frame primary", "Play from here")- play.title = "Make this town yours: build, govern, and it autosaves in this browser"- play.onclick = () => {- endVisit()- toast("The town is yours: it autosaves as you play")- }- const own = el("button", "frame", "Found your own")- own.onclick = () => openCharter(true)- const buttons = el("div", "cell")- buttons.append(play, own)- visitBar.append(text, buttons)- visitBar.classList.add("show")- setSpeed(1)- }- /** The shared town could not be opened: say why, and found a town instead. */- const visitFailed = (message: string) => {- window.history.replaceState(null, "", "/")- openCharter(false)- toast(message)- }-- // ── The end of the term ──- /** Lines of residents, coins, and approval over the term, on the term's own time axis. */- const chart = (s: Stats): string => {- const w = 520- const h = 150- const from = s.fate.began * 3600- const span = s.charter.term * 3600- const history = s.history.filter((p) => p.tick >= from)- const line = (values: ReadonlyArray<number>, lo: number, hi: number, color: string, dash = "") => {- if (values.length < 2) return ""- const range = hi - lo || 1- const points = history- .map((p, i) => `${(((p.tick - from) / span) * w).toFixed(1)},${(h - 4 - ((values[i]! - lo) / range) * (h - 8)).toFixed(1)}`)- .join(" ")- return `<polyline points="${points}" fill="none" stroke="${color}" stroke-width="1.5" stroke-dasharray="${dash}" vector-effect="non-scaling-stroke"/>`- }- const residents = history.map((p) => p.residents)- const coinsLine = history.map((p) => p.coins)- const approvalLine = history.map((p) => p.approval)- const target = s.goals.find((g) => g.id === "residents")?.target ?? sizes[s.charter.size].goal.residents- const top = Math.max(1, ...residents, target)- const goal = h - 4 - (target / top) * (h - 8)- // The first term spares its first year a winter; later terms do not.- const winters = Array.from({ length: 6 }, (_, y) => {- if (y === 0 && s.fate.term === 1) return ""- const at = ((y + 0.7) / 6) * w- return `<rect x="${at.toFixed(1)}" y="0" width="${((w * 0.3) / 6).toFixed(1)}" height="${h}" fill="#3d6db5" opacity="0.06"/>`- }).join("")- return `<svg viewBox="0 0 ${w} ${h}" preserveAspectRatio="none">${winters}<line x1="0" x2="${w}" y1="${goal}" y2="${goal}" stroke="#77817a" stroke-dasharray="3 3" vector-effect="non-scaling-stroke"/>${line(coinsLine, Math.min(0, ...coinsLine), Math.max(1, ...coinsLine), "#d6ae2e")}${line(approvalLine, -100, 100, "#3d6db5", "4 3")}${line(residents, 0, top, "#3d4943")}</svg>`- }- let reckoned = ""- let reckoningOpen = false- const showReckoning = (open: boolean) => {- reckoningOpen = open- reckoning.classList.toggle("open", open && townView.stats !== null && townView.stats.fate.state !== "playing")- }- const syncReckoning = (s: Stats) => {- const over = s.fate.state !== "playing"- result.classList.toggle("show", over && !reckoningOpen && !ui.founding)- if (!over) {- reckoned = ""- reckoning.classList.remove("open")- return- }- const id = `${s.charter.seed}|${s.fate.state}|${s.fate.at}`- result.textContent = s.fate.state === "lost" ? "the term is lost · see how" : `term ${s.fate.term} is over · see how`- if (id === reckoned) return- reckoned = id- const title = s.fate.state === "lost" ? s.fate.reason.split(":")[0]!.toLowerCase() : `term ${s.fate.term} is over: ${verdictOf(s)}`- reckoning.replaceChildren()- const card = el("div", "sheet wide")- card.append(el("h2", "", title), el("p", "sub", s.fate.reason))- const facts = el("div", "facts")- const fact = (label: string, value: string) => {- const f = el("div", "fact")- f.append(el("b", "", value), el("span", "", label))- facts.append(f)- }- fact("rank", s.fate.rankTitle)- fact("minutes in office", `${Math.round(s.fate.at)}`)- fact("residents", `${s.settlers + s.citizens}`)- fact("citizens · burghers", `${s.citizens} · ${s.burghers}`)- fact("coins", `${s.coins}`)- fact("approval", signed(s.mood.approval))- fact("crown met · missed", `${s.crown.met} · ${s.crown.missed}`)- fact("score", `${s.fate.score}`)- card.append(facts)- const graph = el("div", "chart")- graph.innerHTML = chart(s)- card.append(graph, el("p", "legend", "— residents (dashed: the next rank) · coins · - - approval · shaded: winters"))- const story = el("div", "chronicle")- story.innerHTML = s.chronicle- .filter((l) => l.tick >= s.fate.began * 3600)- // Timed from the start of this term, as its chart is.- .map((l) => `<p><b>${clockText(Math.round((l.tick - s.fate.began * 3600) / 60))}</b> ${l.text}</p>`)- .join("")- card.append(story)- const actions = el("div", "actions")- if (s.fate.state === "over") {- const stand = el("button", "frame primary", "Next term")- stand.title = `Stand for another ${s.charter.term} minutes: the same town, new winters, and the crown's requests start small again`- stand.onclick = () => {- queueEdit({ type: "nextTerm" })- showReckoning(false)- }- actions.append(stand)- }- const again = el("button", "frame" + (s.fate.state === "over" ? "" : " primary"), s.fate.state === "over" ? "Retire: found a new town" : "Found a new town")- again.onclick = () => {- showReckoning(false)- openCharter(true)- }- const look = el("button", "frame", "Look at the town")- look.onclick = () => showReckoning(false)- const share = el("button", "frame", "Share the town")- share.title = "A picture of the town and its verdict, or a link anyone can open"- share.onclick = () => void openShare()- actions.append(again, look, share)- card.append(actions)- reckoning.append(card)- showReckoning(true)- }-- const input = new MapInput(stage, ui, view.shown, { toast, blocked: () => document.querySelector(".dialog.open") !== null })- // A road, a row of homes, or a patch of park let go of on a touch screen is held here until built or- // dropped; the tooltip says what it lays and costs.- const strokeBar = el("div", "stroke-bar")- const strokeBuild = el("button", "primary", "Build")- strokeBuild.onclick = () => input.confirmStroke()- const strokeDrop = el("button", "", "Cancel")- strokeDrop.onclick = () => input.cancelStroke()- strokeBar.append(strokeDrop, strokeBuild)- document.body.append(strokeBar)- const resetPage = () => {- choose("select")- ui.menu = null- ui.selected = null- input.reset()- }-- /** What a tool chip in a message does (see `toolRefs`): picks the tool, opens the panel, or shows the map view. */- const useTool = (id: string) => {- setFold(null)- if (id.startsWith("view:")) {- const view = id.slice(5) as View- return setView(ui.view === view ? "none" : view)- }- if (id === "build" || id === "town") return ui.menu === id ? undefined : toggleMenu(id)- if (id === "speed") return setSpeed(ui.speed === 0 ? lastSpeed : ui.speed === 4 ? 1 : ui.speed * 2)- if (id === "pause") return setSpeed(ui.speed === 0 ? lastSpeed : 0)- ui.menu = null- choose(id === "homes" ? "home" : (id as Tool))- }- // A panel row's fix: place the building it names, or pick the tool or panel it names.- panel.addEventListener("click", (event) => {- const target = event.target as HTMLElement- const tool = target.closest<HTMLElement>(".fix [data-tool]")- if (tool) return useTool(tool.dataset.tool!)- const build = target.closest<HTMLElement>("[data-build], .fix [data-kind]")- const kind = (build?.dataset.build ?? build?.dataset.kind) as KindId | undefined- if (kind && kinds[kind].buildable) {- ui.selected = null- place(kind)- }- })- /** Ticker lines the mayor has put away, by what they say (and, for news, when). */- const dismissed = new Set<string>()- let tickerLines: { guide: Stats["guide"]["step"]; alerts: Stats["alerts"]; news: Stats["chronicle"] } = { guide: null, alerts: [], news: [] }- ticker.onclick = (event) => {- event.stopPropagation()- const target = event.target as HTMLElement- // A tool, panel, or map view the line names: pick it, with the map in view.- const tool = target.closest<HTMLElement>("[data-tool]")- if (tool) return useTool(tool.dataset.tool!)- // On a phone: tuck the step away into the bulb, or open the guide in full.- if (target.closest("[data-tuck]")) {- tucked = stepNow- if (!toldWhere) toast("The guide is in the bulb at the top right")- toldWhere = true- return- }- if (ticker.classList.contains("peek") && !target.closest("[data-kind]")) return setFold("guide")- const off = target.closest<HTMLElement>("[data-dismiss]")- if (off) {- dismissed.add(off.dataset.dismiss!)- return- }- // The guide: put this step away, or turn the guide off (the bottom-right button turns it back on).- const skip = target.closest<HTMLElement>("[data-skip]")- if (skip) {- ui.guide.push({ type: "skip", id: skip.dataset.skip! })- return- }- if (target.closest("[data-guide-off]")) {- ui.guide.push({ type: "off" })- toast("Guide off · turn it back on from the bottom right")- return- }- // A building the line is about: go to it.- const go = target.closest<HTMLElement>("[data-go]")- if (go) {- const row = go.closest<HTMLElement>("[data-alert]")- const there = row ? tickerLines.alerts[Number(row.dataset.alert)]?.places?.[go.dataset.go as KindId] : undefined- if (there) goTo(there.at, there.size, there.id)- // The guide's step names the building it points at.- const guide = tickerLines.guide- if (!row && go.closest("[data-guide]") && guide?.at) goTo(guide.at, guide.size, guide.id)- return- }- const ref = target.closest<HTMLElement>("[data-kind]")- if (ref) {- const kind = ref.dataset.kind as KindId- // Straight to placing it, with the map in view: the placement itself says if the town cannot build it yet.- if (kinds[kind].buildable) {- setFold(null)- place(kind)- }- return- }- const line = target.closest<HTMLElement>(".cell.place")- if (!line) return- if (line.dataset.guide !== undefined && tickerLines.guide?.at) return goTo(tickerLines.guide.at, tickerLines.guide.size, tickerLines.guide.id)- const alert = line.dataset.alert !== undefined ? tickerLines.alerts[Number(line.dataset.alert)] : undefined- if (alert?.at) return goTo(alert.at, alert.size, alert.id)- const news = line.dataset.news !== undefined ? tickerLines.news[Number(line.dataset.news)] : undefined- if (news?.at) goTo(news.at, 1, null)- }-- /** The last chronicle line announced. */- let lastNews = ""- /**- * A phone's guide: the step stays above the tool bar until the mayor tucks it away (`tucked`: the step and- * state they tucked; a new step, or the same one under way, comes out again). Tucked away, the bulb pulses,- * and the first time a toast says where the guide went.- */- let stepNow = ""- let tucked = ""- let toldWhere = false- /** Copies the town's numbers and preview into the page, once a frame. */- const syncPage = () => {- for (const message of townView.toasts.splice(0)) toast(message)- syncPanel()- const s = townView.stats- if (s) {- // The treasury as the goods are shown: which way it went over the last minute, and by how much; the steady rate until a minute is counted.- const cf = s.flow?.coins ?? null- const change = cf ? cf.made + cf.traded - cf.used : s.net- const way = cf ? trendOf(cf.made, cf.used, cf.traded) : 0- setHtml(- coins,- `${icon("coins", 20)}<span class="stack"><b>${s.coins}${cf ? `<span class="trend ${trendClass(way)}">${trendMark(way)}</span>` : ""}</b>` +- `<small class="${change >= 0 ? "up" : "down"}">${change >= 0 ? "+" : ""}${change}/min</small></span>`- )- coins.title = cf- ? `Coins in the treasury · click for the town's last minute\nLast minute: ${signed(change)} (made ${cf.made}, used ${cf.used}${cf.traded !== 0 ? `, traded ${signed(cf.traded)}` : ""})\nAt the current rates: ${signed(s.net)}/min (taxes, trade, upkeep, administration)`- : `Coins in the treasury, and what it gains (or loses) a minute at the current rates · click for the town's last minute`- coins.classList.toggle("debt", s.coins < 0)- const left = Math.max(0, s.charter.term * 60 - Math.floor((s.minute - s.fate.began) * 60))- const severity = (w: typeof s.season.coming) => (w === null ? "" : ` (${winters[w].title})`)- const until = s.season.winter- ? `${winters[s.season.severity ?? "normal"].title} winter · spring in ${clockText(s.season.left)}`- : s.season.coming === null- ? "no more winters this term"- : `winter in ${clockText(s.season.left)}${severity(s.season.coming)}`- setHtml(clock, `${icon(s.season.winter ? "winter" : "clock", 20)}<span class="stack"><b>${clockText(left)}${ui.speed === 0 ? `<span class="paused-tag">paused</span>` : ""}</b><small class="${s.season.winter ? "winter" : s.season.coming === "hard" ? "down" : ""}">year ${s.season.year} · ${until}</small></span>`)- clock.title = `${s.season.winter ? "Winter: fields lie still, fishing is slow, and homes eat faster" : "Time left in the term"}\nThe almanac: the coming winter is ${s.season.coming === null ? "beyond the term" : winters[s.season.coming].title}, the one after ${s.season.after === null ? "beyond the term" : winters[s.season.after].title}.\nMild: fishing at ${Math.round(winters.mild.fishing * 100)}%. Normal: ${Math.round(winters.normal.fishing * 100)}%. Hard: half as long again, fishing at ${Math.round(winters.hard.fishing * 100)}%, and homes heating are a fire risk.`- const trend = s.mood.target > s.mood.approval ? "↑" : s.mood.target < s.mood.approval ? "↓" : ""- setHtml(approval, `${icon("approval", 18)}<span class="stack"><b class="${s.mood.approval < 0 ? "down" : ""}">${signed(s.mood.approval)} <span class="${s.mood.target >= s.mood.approval ? "up" : "down"}">${trend}</span></b><small>approval</small></span>`)- setHtml(settlerCell, `${icon("settlers", 18)}<span class="stack"><b>${s.settlers}</b><small>settlers</small></span>`)- setHtml(citizenCell, `${icon("citizens", 18)}<span class="stack"><b>${s.citizens}</b><small>citizens${s.burghers > 0 ? ` · ${s.burghers} burghers` : ""}</small></span>`)- const short = s.shortStaffed.length- setHtml(jobs, `${icon("jobs", 18)}<span class="stack"><b>${s.employed}<span class="of">/${s.jobs}</span></b><small class="${short > 0 ? "down" : ""}">${short > 0 ? `${short} short · click` : "jobs filled"}</small></span>`)- jobs.title = short > 0- ? `Workers at work over workers needed. ${short} workplace${short > 1 ? "s are" : " is"} short: ${s.shortStaffed.slice(0, 5).map((w) => `${w.title} ${w.assigned}/${w.needed}${w.nobodyNear ? " (no homes within 15 road cells)" : ""}`).join(", ")}. Click to visit them in turn.`- : "Workers at work over workers needed: every workplace has its crew. Residents walk at most 15 road cells to work."- const chipHtml = (["planks", "stone", "tools"] as const)- .map((g) => `<span class="${s.stock[g] === 0 ? "short" : ""}">${goodIcon(g, 14)}${s.stock[g]}</span>`)- .join("")- if (goodsChip.dataset.html !== chipHtml) {- goodsChip.dataset.html = chipHtml- goodsChip.innerHTML = chipHtml- }- for (const [g, count] of goodCells) {- count.textContent = String(s.stock[g])- const way = s.flow ? flowTrend(s.flow, g) : 0- const trend = goodTrends.get(g)!- trend.textContent = s.flow ? trendMark(way) : ""- trend.className = `trend ${trendClass(way)}`- // Empty storage is only worth dimming when nothing is held anywhere else either.- count.parentElement!.classList.toggle("empty", s.stock[g] === 0 && (s.flow?.elsewhere[g] ?? 0) === 0)- count.parentElement!.title = `${goods[g].title}: ${goodHelp[g]}` + (s.flow- ? `\nLast minute: made ${s.flow.made[g]}, used ${s.flow.used[g]}${s.flow.traded[g] !== 0 ? `, ${s.flow.traded[g] > 0 ? "bought" : "sold"} ${Math.abs(s.flow.traded[g])}` : ""}. ${s.stock[g]} in storage, ${s.flow.elsewhere[g]} about town (in workshops and markets, and on the way).`- : "")- }- // On a phone the goods chip opens the table itself, once there is a minute to show; the plain goods until then.- document.body.classList.toggle("flow-ready", s.flow !== null)- if (flowCard.classList.contains("open") || (phone.matches && fold === "goods")) setHtml(flowCard, flowHtml(s))- // The crown's request.- request.classList.toggle("show", s.crown.open && s.fate.state === "playing")- if (s.crown.open) {- const have = s.stock[s.crown.good]- // What is asked on top, in one line; the terms under it.- setHtml(- requestText,- `${icon("crown", 18)}<span class="lines"><span class="ask">the crown asks for <b>${s.crown.amount}</b> ${goodIcon(s.crown.good, 14)} <b>${goods[s.crown.good].title}</b></span>` +- `<span class="terms"><span class="${have < s.crown.amount ? "down" : "up"}">${have} in store</span> · due in <b>${clockText(s.crown.left)}</b> · +${s.crown.reward} coins</span></span>`- )- send.title = have < s.crown.amount ? `Not enough ${goods[s.crown.good].title} in store yet` : `Send ${s.crown.amount} ${goods[s.crown.good].title} to the crown for ${s.crown.reward} coins`- send.disabled = have < s.crown.amount- }- // What threatens the term, or is about to raise the town a rank.- const warnings: Array<string> = []- if (s.fate.state === "playing") {- if (s.fate.bankrupt !== null)- warnings.push(`<b>In debt:</b> bankrupt in ${clockText(s.fate.bankrupt)} unless the treasury recovers`)- else if (s.coins < 0) warnings.push("<b>In debt:</b> wages are going unpaid")- if (s.fate.revolt !== null) warnings.push(`<b>Uproar:</b> the town turns the mayor out in ${clockText(s.fate.revolt)}`)- else if (s.mood.approval < -30) warnings.push("<b>Restless:</b> approval is falling towards revolt")- if (s.fate.peak >= 40 && s.settlers + s.citizens < s.fate.peak * 0.4) warnings.push("<b>Emptying:</b> people are leaving the town")- if (s.crown.streak > 0 && s.crown.streak >= s.crown.patience - 1) {- warnings.push(`<b>The crown's patience:</b> ${s.crown.streak} requests missed in a row; one more and the mayor is dismissed`)- }- if (s.fate.rises !== null && s.fate.nextTitle !== null) warnings.push(`<b>Every goal met:</b> the town is a ${s.fate.nextTitle} in ${clockText(s.fate.rises)}`)- }- const html = warnings- .map((w) => `<div class="cell">${icon(w.startsWith("<b>Every goal") ? "goal" : "warning", 16)}<span>${w}</span></div>`)- .join("")- if (alarm.dataset.html !== html) {- alarm.dataset.html = html- alarm.innerHTML = html- }- alarm.classList.toggle("show", warnings.length > 0)- alarm.classList.toggle("good", warnings.length === 1 && s.fate.rises !== null)- // The ticker: what needs looking at now, then the news of the last minute and a half; click a line to go there.- const minute = Math.round(s.minute * 3600)- const news = s.chronicle.filter((l) => l.alarm && minute - l.tick < 90 * 60).slice(-3).reverse()- const ago = (tick: number) => {- const sec = Math.max(0, Math.round((minute - tick) / 60))- return sec < 5 ? "just now" : sec < 60 ? `${sec} s ago` : `${Math.round(sec / 60)} min ago`- }- const alerts = s.alerts.filter((a) => !dismissed.has(`a:${a.text}`))- // The crown's request has its own card while it is open, and is settled after: its news line would only repeat it.- const fresh = news.filter((l) => !dismissed.has(`n:${l.tick}:${l.text}`) && !l.text.startsWith("The crown asks"))- const toneIcon = { hint: "hint", warn: "caution", bad: "warning" } as const- const row = (kind: string, tone: string, glyph: IconName, text: string, place: boolean, key: string, data: string) =>- `<div class="cell ${kind} ${tone}${place ? " place" : ""}" ${data}>${icon(glyph, 16, "mark")}<span class="text">${text}</span><span class="acts">${place ? `<button class="act locate" title="Show me where">${icon("locate", 14)}</button>` : ""}<button class="act dismiss" data-dismiss="${escapeHtml(key)}" title="Dismiss">${icon("close", 12)}</button></span></div>`- // The guide's step first: what to do next, and why.- const g = s.guide.step- const guideHtml = g- ? (() => {- const asks = new Set(g.asks)- const say = (text: string) => callouts(text, new Set(g.near ? [g.near] : []), asks)- const count = g.count ? `step ${g.count.at} of ${g.count.of}` : ""- return [- `<div class="head guide"><span>guide · ${escapeHtml(g.chapter)}</span><span class="progress" title="${g.count ? "The first steps of a new town" : ""}">${count}</span><button class="quiet" data-guide-off title="Turn the guide off (the guide button in the bottom right turns it back on)">hide guide</button></div>`,- `<div class="cell alert hint guide${g.waiting ? " waiting" : g.locked ? " locked" : ""}${g.at !== null ? " place" : ""}" data-guide>${icon(g.waiting ? "clock" : g.locked ? "caution" : "hint", 16, "mark")}<span class="text">` +- `<span class="do">${say(g.text)}</span>` +- (g.waiting ? `<span class="why now">${say(g.waiting)}</span>` : g.locked ? `<span class="why now">${say(g.locked)}</span>` : `<span class="why">${say(g.why)}</span>`) +- `<span class="goal">${icon("goal", 12)}<span>done when ${say(g.goal)}</span></span>` +- (g.next ? `<span class="goal next">${icon("clock", 12)}<span>${say(g.next)}</span></span>` : "") +- `</span>` +- `<span class="acts">${g.at !== null ? `<button class="act locate" title="Show me where">${icon("locate", 14)}</button>` : ""}<button class="act dismiss" data-skip="${escapeHtml(g.key)}" title="Skip this step">${icon("close", 12)}</button></span>` +- `<button class="act tuck" data-tuck title="Hide: the guide stays in the bulb at the top right">${icon("close", 12)}</button></div>`- ]- })()- : []- const tickerHtml = [- ...guideHtml,- ...(alerts.length > 0 ? [`<div class="head">now</div>`] : []),- ...alerts.map((a) => row("alert", a.tone, toneIcon[a.tone], callouts(a.text, new Set(Object.keys(a.places ?? {}) as Array<KindId>)), a.at !== null, `a:${a.text}`, `data-alert="${s.alerts.indexOf(a)}"`)),- ...(fresh.length > 0 ? [`<div class="head">lately</div>`] : []),- ...fresh.map((l) => row("news", "", "chronicle", `${callouts(l.text)} <em data-tick="${l.tick}"></em>`, l.at !== null, `n:${l.tick}:${l.text}`, `data-news="${news.indexOf(l)}"`))- ].join("")- if (ticker.dataset.html !== tickerHtml) {- ticker.dataset.html = tickerHtml- ticker.innerHTML = tickerHtml- }- for (const em of ticker.querySelectorAll<HTMLElement>("em[data-tick]")) {- const text = ago(Number(em.dataset.tick))- if (em.textContent !== text) em.textContent = text- }- tickerLines = { guide: g, alerts: s.alerts, news }- guideButton.classList.toggle("on", !s.guide.off)- guideButton.title = s.guide.off ? "Turn the guide back on: a next step to take, and why, at the top of the ticker" : "Turn the guide off"- ticker.classList.toggle("show", tickerHtml !== "" && s.fate.state === "playing")- // The phone's guide button: the step it is on, or how many things need looking at.- const bad = alerts.some((a) => a.tone === "bad")- const things = alerts.length + (g ? 1 : 0)- const badge = g?.count ? `${g.count.at}/${g.count.of}` : things > 0 ? String(things) : ""- if (guideBadge.textContent !== badge) guideBadge.textContent = badge- guideBulb.classList.toggle("alert", bad)- // On a phone the step stays out above the tool bar, until tucked away; not behind the town picker or an open sheet.- const stepKey = g ? `${g.key}${g.waiting ? ":under way" : g.locked ? ":locked" : ""}` : ""- stepNow = stepKey- const peeking = stepKey !== "" && phone.matches && fold === null && tucked !== stepKey && !ui.founding && s.fate.state === "playing"- if (ticker.classList.contains("peek") !== peeking) ticker.classList.toggle("peek", peeking)- document.body.classList.toggle("peeking", peeking)- const nudge = stepKey !== "" && phone.matches && tucked === stepKey && fold !== "guide"- if (guideBulb.classList.contains("nudge") !== nudge) guideBulb.classList.toggle("nudge", nudge)- // News as it happens: a toast for each new alarm line.- const latest = s.chronicle.at(-1)- const seen = `${s.charter.seed}|${latest?.tick ?? -1}|${s.chronicle.length}`- if (seen !== lastNews) {- if (lastNews !== "" && latest?.alarm && minute - latest.tick < 120) toast(latest.text)- lastNews = seen- }- // The map view's legend.- const l = townView.legend- const legendHtml = l- ? `<b>${l.title}</b>${l.keys.map((k) => `<span class="key"><i style="background:${hex(k.color)}"></i>${k.label}</span>`).join("")}<small>${l.note}</small>`- : ""- if (legend.dataset.html !== legendHtml) {- legend.dataset.html = legendHtml- legend.innerHTML = legendHtml- }- legend.classList.toggle("show", l !== null)- syncReckoning(s)- }- input.sync()- const screenPointer = input.tip- // A stroke being drawn says what it lays and costs.- const text = ui.dragFrom && ui.stroke !== "cancel" && townView.stroke ? townView.stroke : townView.tooltip- // A held touch stroke waits for its build button.- const held = ui.stroke === "held" && townView.stroke !== null- strokeBar.classList.toggle("open", held)- document.body.classList.toggle("stroke-held", held)- if (text && screenPointer) {- setHtml(tooltip, iconize(escapeHtml(text)))- tooltip.style.display = "block"- tooltip.style.left = `${Math.min(screenPointer.x, window.innerWidth - tooltip.offsetWidth - 20)}px`- tooltip.style.top = `${screenPointer.y}px`- } else tooltip.style.display = "none"- }-- return { stage, syncPage, resetPage, toast, input, visit, visitFailed, visiting: () => visiting }-}--/** Which way a good (or the treasury) went over the last minute: made and bought against used and sold, past a tenth of the traffic either way. */-const trendOf = (made: number, used: number, traded: number): number => {- const net = made + traded - used- const room = Math.max(1, Math.round((made + used + Math.abs(traded)) / 10))- return net > room ? 1 : net < -room ? -1 : 0-}-const flowTrend = (flow: NonNullable<Stats["flow"]>, g: GoodId): number => trendOf(flow.made[g], flow.used[g], flow.traded[g])--/** A good's way over the last minute: up, down, or holding steady. */-const trendMark = (way: number) => (way > 0 ? "↑" : way < 0 ? "↓" : "→")-const trendClass = (way: number) => (way > 0 ? "up" : way < 0 ? "down" : "steady")--/** The goods' last minute, side by side: only the goods the town has touched. */-const flowHtml = (s: Stats): string => {- const f = s.flow- if (f === null) return `<p class="sub">Counting the town's goods: the first numbers come in half a minute…</p>`- const rows = goodIds.filter((g) => f.made[g] + f.used[g] + Math.abs(f.traded[g]) + s.stock[g] + f.elsewhere[g] > 0)- const head = `<span></span><b title="Made by workshops and gatherers over the last minute">made</b><b title="Eaten, worked up, built with, rotted, or sent away over the last minute">used</b><b title="Bought (+) or sold (−) over the last minute">traded</b><b title="In the town hall, warehouses, and granaries">storage</b><b title="In workshops and markets, walkers' baskets, and carriers' crates">about town</b>`- const cell = (n: number) => `<span>${n === 0 ? `<i>–</i>` : n}</span>`- const signedCell = (n: number) => `<span>${n === 0 ? `<i>–</i>` : n > 0 ? `+${n}` : `−${-n}`}</span>`- // The treasury first: taxes and rewards made, upkeep and building used, trade's coins; it is all in storage.- const c = f.coins- const way = trendOf(c.made, c.used, c.traded)- const treasury = `<span class="name" title="coins">${icon("coins", 14)}<span class="label">coins</span><span class="trend ${trendClass(way)}">${trendMark(way)}</span></span>` +- `${cell(c.made)}${cell(c.used)}${signedCell(c.traded)}${cell(s.coins)}<span><i>–</i></span>`- const body = treasury + rows.map((g) => {- const way = flowTrend(f, g)- const traded = f.traded[g] === 0 ? `<i>–</i>` : f.traded[g] > 0 ? `+${f.traded[g]}` : `−${-f.traded[g]}`- return `<span class="name" title="${goods[g].title}">${goodIcon(g, 14)}<span class="label">${goods[g].title}</span><span class="trend ${trendClass(way)}">${trendMark(way)}</span></span>` +- `${cell(f.made[g])}${cell(f.used[g])}<span>${traded}</span>${cell(s.stock[g])}${cell(f.elsewhere[g])}`- }).join("")- return `<div class="flow-grid">${head}${body}</div>`-}--/** How long ago a save was made, roughly: "just now", "12 min ago", "3 h ago", "2 days ago". */-const ago = (at: number): string => {- const minutes = Math.floor((Date.now() - at) / 60000)- if (minutes < 1) return "just now"- if (minutes < 60) return `${minutes} min ago`- const hours = Math.floor(minutes / 60)- if (hours < 24) return `${hours} h ago`- const days = Math.floor(hours / 24)- return days === 1 ? "yesterday" : `${days} days ago`-}diff --git a/src/ui/dom/hud.ts b/src/ui/dom/hud.tsnew file mode 100644index 0000000..358b908--- /dev/null+++ b/src/ui/dom/hud.ts@@ -0,0 +1,259 @@+/**+ * The heads-up display. Top left: the term's clock and the treasury, the+ * goods in stock below, then the crown's request and any alarm (the ticker+ * goes under them). Top right: the people and their mood, which opens the+ * town's panel. On a phone the people join the top line, and a chip of the+ * building goods stands in for the stock.+ */+import { goodIds, goods, winters, type GoodId } from "../../game/content/index.ts"+import type { Edit } from "../../game/edits.ts"+import type { Stats } from "../../view/contracts.ts"+import type { Controls, Fold } from "./controls.ts"+import { phone } from "./folds.ts"+import { el, setHtml } from "./kit/dom.ts"+import { goodIcon, icon } from "./kit/icons.ts"+import { clockText, signed } from "./kit/text.ts"++/** What each good is for, for the stock bar's tooltips: who makes it, who takes it. */+const goodHelp: Readonly<Record<GoodId, string>> = {+ wood: "lumberjacks fell it; a sawmill saws it into planks",+ planks: "what homes and buildings are built from: a sawmill makes them from wood",+ stone: "for building and paving: quarries cut it from rocks, which run out",+ ore: "only for tools: once the town has 10 citizens, a mine digs it from the rust-red deposits, and a smithy forges it into tools",+ tools: "for building, and workshops wear one out every 50 or 60 jobs: a smithy forges them, or buy them",+ fish: "food for settlers: fisheries catch it by the water; it rots in about three minutes",+ smoked: "fish that keeps all winter: a smokehouse smokes it",+ wheat: "farms grow it; a mill grinds it into flour (not in winter)",+ flour: "a mill's; a bakery bakes it into bread",+ bread: "food that citizens need, and a cottage with bread pays half again: it rots in about five minutes",+ wool: "sheep farms shear it; a weaver weaves it into cloth",+ cloth: "citizens need it: only brought to streets with desirability 5 or more",+ furniture: "burghers need it in their townhouses: a carpenter makes it from planks; only brought to streets fine enough for townhouses"+}+const groups: ReadonlyArray<ReadonlyArray<GoodId>> = [+ ["wood", "planks", "stone", "ore", "tools"],+ ["fish", "smoked", "wheat", "flour", "bread"],+ ["wool", "cloth", "furniture"]+]++/** `ticker`: the ticker's card, which goes under the alarms. */+export const createHud = (+ ticker: HTMLElement,+ send: (edit: Edit) => void,+ stats: () => Stats | null,+ controls: Pick<Controls, "toggleMenu" | "toggleFold" | "setFold" | "goTo">+) => {+ const hud = el("div", "hud left")+ const bar = el("div", "card bar")+ const clock = el("div", "cell clock")+ const coins = el("div", "cell coins")+ bar.append(clock, coins)+ const stock = el("div", "card stock")+ stock.title = "Goods in the town's storage, and which way each is going · click for what the town makes and uses"+ const goodCells = new Map<GoodId, HTMLElement>()+ const goodTrends = new Map<GoodId, HTMLElement>()+ for (const group of groups) {+ const cell = el("div", "group")+ for (const g of group) {+ const item = el("span", "good")+ item.title = `${goods[g].title}: ${goodHelp[g]}`+ item.setAttribute("aria-label", goods[g].title)+ const glyph = el("span", "glyph")+ glyph.innerHTML = goodIcon(g, 18)+ const count = el("span", "count", "0")+ const trend = el("span", "trend")+ item.append(glyph, count, trend)+ cell.append(item)+ goodCells.set(g, count)+ goodTrends.set(g, trend)+ }+ stock.append(cell)+ }+ // The crown's request, when there is one, and what threatens the term (or is about to raise the town a rank).+ const request = el("div", "card request")+ const requestText = el("div", "cell")+ const deliver = el("button", "send", "send")+ deliver.onclick = (event) => {+ event.stopPropagation()+ send({ type: "deliver" })+ }+ request.append(requestText, deliver)+ const alarm = el("div", "card alarm")+ // The top line: the clock and treasury (on a phone, the people and a chip of the building goods beside them).+ const topline = el("div", "topline")+ const goodsChip = el("button", "card goods-chip")+ goodsChip.title = "The goods in storage"+ topline.append(bar, goodsChip)+ // Under the goods, on a click: each good's last minute, made against used, and where it is held.+ const flowCard = el("div", "card flow")+ stock.onclick = (event) => {+ event.stopPropagation()+ flowCard.classList.toggle("open")+ }+ flowCard.onclick = () => flowCard.classList.remove("open")+ hud.append(topline, stock, flowCard, request, alarm, ticker)+ const people = el("div", "hud right")+ const folk = el("div", "card bar folk")+ const approval = el("div", "cell")+ const settlerCell = el("div", "cell")+ const citizenCell = el("div", "cell")+ const jobs = el("div", "cell jobs")+ folk.append(approval, settlerCell, citizenCell, jobs)+ people.append(folk)+ folk.title = "Open the town's numbers, goals, and levers (T)"+ folk.onclick = () => controls.toggleMenu("town")+ let shortCursor = 0+ jobs.onclick = (event) => {+ event.stopPropagation()+ const short = stats()?.shortStaffed ?? []+ if (short.length === 0) return controls.toggleMenu("town")+ const next = short[shortCursor % short.length]!+ shortCursor += 1+ controls.goTo(next.cell, next.size, next.id)+ }+ goodsChip.onclick = (event) => {+ event.stopPropagation()+ controls.toggleFold("goods")+ }+ // The treasury opens the same minute the goods do.+ coins.onclick = (event) => {+ event.stopPropagation()+ if (phone.matches) controls.toggleFold("goods")+ else flowCard.classList.toggle("open")+ }+ // The people's card joins the top line on a phone, and goes back to the top right on a wider screen.+ const placeFolk = () => {+ if (phone.matches) topline.insertBefore(folk, goodsChip)+ else people.append(folk)+ if (!phone.matches) controls.setFold(null)+ }++ /** `speed`: 0 shows the clock paused; `fold`: what a phone has open. */+ const update = (s: Stats, speed: number, fold: Fold | null) => {+ // The treasury as the goods are shown: which way it went over the last minute, and by how much; the steady rate until a minute is counted.+ const cf = s.flow?.coins ?? null+ const change = cf ? cf.made + cf.traded - cf.used : s.net+ const way = cf ? trendOf(cf.made, cf.used, cf.traded) : 0+ setHtml(+ coins,+ `${icon("coins", 20)}<span class="stack"><b>${s.coins}${cf ? `<span class="trend ${trendClass(way)}">${trendMark(way)}</span>` : ""}</b>` ++ `<small class="${change >= 0 ? "up" : "down"}">${change >= 0 ? "+" : ""}${change}/min</small></span>`+ )+ coins.title = cf+ ? `Coins in the treasury · click for the town's last minute\nLast minute: ${signed(change)} (made ${cf.made}, used ${cf.used}${cf.traded !== 0 ? `, traded ${signed(cf.traded)}` : ""})\nAt the current rates: ${signed(s.net)}/min (taxes, trade, upkeep, administration)`+ : `Coins in the treasury, and what it gains (or loses) a minute at the current rates · click for the town's last minute`+ coins.classList.toggle("debt", s.coins < 0)+ const left = Math.max(0, s.charter.term * 60 - Math.floor((s.minute - s.fate.began) * 60))+ const severity = (w: typeof s.season.coming) => (w === null ? "" : ` (${winters[w].title})`)+ const until = s.season.winter+ ? `${winters[s.season.severity ?? "normal"].title} winter · spring in ${clockText(s.season.left)}`+ : s.season.coming === null+ ? "no more winters this term"+ : `winter in ${clockText(s.season.left)}${severity(s.season.coming)}`+ setHtml(clock, `${icon(s.season.winter ? "winter" : "clock", 20)}<span class="stack"><b>${clockText(left)}${speed === 0 ? `<span class="paused-tag">paused</span>` : ""}</b><small class="${s.season.winter ? "winter" : s.season.coming === "hard" ? "down" : ""}">year ${s.season.year} · ${until}</small></span>`)+ clock.title = `${s.season.winter ? "Winter: fields lie still, fishing is slow, and homes eat faster" : "Time left in the term"}\nThe almanac: the coming winter is ${s.season.coming === null ? "beyond the term" : winters[s.season.coming].title}, the one after ${s.season.after === null ? "beyond the term" : winters[s.season.after].title}.\nMild: fishing at ${Math.round(winters.mild.fishing * 100)}%. Normal: ${Math.round(winters.normal.fishing * 100)}%. Hard: half as long again, fishing at ${Math.round(winters.hard.fishing * 100)}%, and homes heating are a fire risk.`+ const trend = s.mood.target > s.mood.approval ? "↑" : s.mood.target < s.mood.approval ? "↓" : ""+ setHtml(approval, `${icon("approval", 18)}<span class="stack"><b class="${s.mood.approval < 0 ? "down" : ""}">${signed(s.mood.approval)} <span class="${s.mood.target >= s.mood.approval ? "up" : "down"}">${trend}</span></b><small>approval</small></span>`)+ setHtml(settlerCell, `${icon("settlers", 18)}<span class="stack"><b>${s.settlers}</b><small>settlers</small></span>`)+ setHtml(citizenCell, `${icon("citizens", 18)}<span class="stack"><b>${s.citizens}</b><small>citizens${s.burghers > 0 ? ` · ${s.burghers} burghers` : ""}</small></span>`)+ const short = s.shortStaffed.length+ setHtml(jobs, `${icon("jobs", 18)}<span class="stack"><b>${s.employed}<span class="of">/${s.jobs}</span></b><small class="${short > 0 ? "down" : ""}">${short > 0 ? `${short} short · click` : "jobs filled"}</small></span>`)+ jobs.title = short > 0+ ? `Workers at work over workers needed. ${short} workplace${short > 1 ? "s are" : " is"} short: ${s.shortStaffed.slice(0, 5).map((w) => `${w.title} ${w.assigned}/${w.needed}${w.nobodyNear ? " (no homes within 15 road cells)" : ""}`).join(", ")}. Click to visit them in turn.`+ : "Workers at work over workers needed: every workplace has its crew. Residents walk at most 15 road cells to work."+ const chipHtml = (["planks", "stone", "tools"] as const)+ .map((g) => `<span class="${s.stock[g] === 0 ? "short" : ""}">${goodIcon(g, 14)}${s.stock[g]}</span>`)+ .join("")+ if (goodsChip.dataset.html !== chipHtml) {+ goodsChip.dataset.html = chipHtml+ goodsChip.innerHTML = chipHtml+ }+ for (const [g, count] of goodCells) {+ count.textContent = String(s.stock[g])+ const way = s.flow ? flowTrend(s.flow, g) : 0+ const trend = goodTrends.get(g)!+ trend.textContent = s.flow ? trendMark(way) : ""+ trend.className = `trend ${trendClass(way)}`+ // Empty storage is only worth dimming when nothing is held anywhere else either.+ count.parentElement!.classList.toggle("empty", s.stock[g] === 0 && (s.flow?.elsewhere[g] ?? 0) === 0)+ count.parentElement!.title = `${goods[g].title}: ${goodHelp[g]}` + (s.flow+ ? `\nLast minute: made ${s.flow.made[g]}, used ${s.flow.used[g]}${s.flow.traded[g] !== 0 ? `, ${s.flow.traded[g] > 0 ? "bought" : "sold"} ${Math.abs(s.flow.traded[g])}` : ""}. ${s.stock[g]} in storage, ${s.flow.elsewhere[g]} about town (in workshops and markets, and on the way).`+ : "")+ }+ // On a phone the goods chip opens the table itself, once there is a minute to show; the plain goods until then.+ document.body.classList.toggle("flow-ready", s.flow !== null)+ if (flowCard.classList.contains("open") || (phone.matches && fold === "goods")) setHtml(flowCard, flowHtml(s))+ // The crown's request.+ request.classList.toggle("show", s.crown.open && s.fate.state === "playing")+ if (s.crown.open) {+ const have = s.stock[s.crown.good]+ // What is asked on top, in one line; the terms under it.+ setHtml(+ requestText,+ `${icon("crown", 18)}<span class="lines"><span class="ask">the crown asks for <b>${s.crown.amount}</b> ${goodIcon(s.crown.good, 14)} <b>${goods[s.crown.good].title}</b></span>` ++ `<span class="terms"><span class="${have < s.crown.amount ? "down" : "up"}">${have} in store</span> · due in <b>${clockText(s.crown.left)}</b> · +${s.crown.reward} coins</span></span>`+ )+ deliver.title = have < s.crown.amount ? `Not enough ${goods[s.crown.good].title} in store yet` : `Send ${s.crown.amount} ${goods[s.crown.good].title} to the crown for ${s.crown.reward} coins`+ deliver.disabled = have < s.crown.amount+ }+ // What threatens the term, or is about to raise the town a rank.+ const warnings: Array<string> = []+ if (s.fate.state === "playing") {+ if (s.fate.bankrupt !== null)+ warnings.push(`<b>In debt:</b> bankrupt in ${clockText(s.fate.bankrupt)} unless the treasury recovers`)+ else if (s.coins < 0) warnings.push("<b>In debt:</b> wages are going unpaid")+ if (s.fate.revolt !== null) warnings.push(`<b>Uproar:</b> the town turns the mayor out in ${clockText(s.fate.revolt)}`)+ else if (s.mood.approval < -30) warnings.push("<b>Restless:</b> approval is falling towards revolt")+ if (s.fate.peak >= 40 && s.settlers + s.citizens < s.fate.peak * 0.4) warnings.push("<b>Emptying:</b> people are leaving the town")+ if (s.crown.streak > 0 && s.crown.streak >= s.crown.patience - 1) {+ warnings.push(`<b>The crown's patience:</b> ${s.crown.streak} requests missed in a row; one more and the mayor is dismissed`)+ }+ if (s.fate.rises !== null && s.fate.nextTitle !== null) warnings.push(`<b>Every goal met:</b> the town is a ${s.fate.nextTitle} in ${clockText(s.fate.rises)}`)+ }+ const html = warnings+ .map((w) => `<div class="cell">${icon(w.startsWith("<b>Every goal") ? "goal" : "warning", 16)}<span>${w}</span></div>`)+ .join("")+ if (alarm.dataset.html !== html) {+ alarm.dataset.html = html+ alarm.innerHTML = html+ }+ alarm.classList.toggle("show", warnings.length > 0)+ alarm.classList.toggle("good", warnings.length === 1 && s.fate.rises !== null)+ }++ return { left: hud, right: people, goodsChip, placeFolk, update }+}++/** Which way a good (or the treasury) went over the last minute: made and bought against used and sold, past a tenth of the traffic either way. */+const trendOf = (made: number, used: number, traded: number): number => {+ const net = made + traded - used+ const room = Math.max(1, Math.round((made + used + Math.abs(traded)) / 10))+ return net > room ? 1 : net < -room ? -1 : 0+}+const flowTrend = (flow: NonNullable<Stats["flow"]>, g: GoodId): number => trendOf(flow.made[g], flow.used[g], flow.traded[g])++/** A good's way over the last minute: up, down, or holding steady. */+const trendMark = (way: number) => (way > 0 ? "↑" : way < 0 ? "↓" : "→")+const trendClass = (way: number) => (way > 0 ? "up" : way < 0 ? "down" : "steady")++/** The goods' last minute, side by side: only the goods the town has touched. */+const flowHtml = (s: Stats): string => {+ const f = s.flow+ if (f === null) return `<p class="sub">Counting the town's goods: the first numbers come in half a minute…</p>`+ const rows = goodIds.filter((g) => f.made[g] + f.used[g] + Math.abs(f.traded[g]) + s.stock[g] + f.elsewhere[g] > 0)+ const head = `<span></span><b title="Made by workshops and gatherers over the last minute">made</b><b title="Eaten, worked up, built with, rotted, or sent away over the last minute">used</b><b title="Bought (+) or sold (−) over the last minute">traded</b><b title="In the town hall, warehouses, and granaries">storage</b><b title="In workshops and markets, walkers' baskets, and carriers' crates">about town</b>`+ const cell = (n: number) => `<span>${n === 0 ? `<i>–</i>` : n}</span>`+ const signedCell = (n: number) => `<span>${n === 0 ? `<i>–</i>` : n > 0 ? `+${n}` : `−${-n}`}</span>`+ // The treasury first: taxes and rewards made, upkeep and building used, trade's coins; it is all in storage.+ const c = f.coins+ const way = trendOf(c.made, c.used, c.traded)+ const treasury = `<span class="name" title="coins">${icon("coins", 14)}<span class="label">coins</span><span class="trend ${trendClass(way)}">${trendMark(way)}</span></span>` ++ `${cell(c.made)}${cell(c.used)}${signedCell(c.traded)}${cell(s.coins)}<span><i>–</i></span>`+ const body = treasury + rows.map((g) => {+ const way = flowTrend(f, g)+ const traded = f.traded[g] === 0 ? `<i>–</i>` : f.traded[g] > 0 ? `+${f.traded[g]}` : `−${-f.traded[g]}`+ return `<span class="name" title="${goods[g].title}">${goodIcon(g, 14)}<span class="label">${goods[g].title}</span><span class="trend ${trendClass(way)}">${trendMark(way)}</span></span>` ++ `${cell(f.made[g])}${cell(f.used[g])}<span>${traded}</span>${cell(s.stock[g])}${cell(f.elsewhere[g])}`+ }).join("")+ return `<div class="flow-grid">${head}${body}</div>`+}diff --git a/src/ui/dom/index.ts b/src/ui/dom/index.tsnew file mode 100644index 0000000..6fadac3--- /dev/null+++ b/src/ui/dom/index.ts@@ -0,0 +1,283 @@+/**+ * The page's UI: one way to show the town, on the DOM. It reads what the+ * view shows (`TownView`), writes what the player does with the map+ * (`Interaction`), and sends edits; it knows nothing of how the map is drawn,+ * only the narrow `MapPort` the app hands it. This module composes the parts+ * and wires the page's controls (`controls.ts`) to them.+ */+import { defaultCharter, grounds, kinds, props, type GroundId, type KindId, type PropId } from "../../game/content/index.ts"+import type { Edit } from "../../game/edits.ts"+import type { Point } from "../../game/world/iso.ts"+import type { Interaction, Shown, Tool, View } from "../../view/contracts.ts"+import { commands, refOf } from "../../view/tools.ts"+import type { Controls } from "./controls.ts"+import { createFolds, phone } from "./folds.ts"+import { createHud } from "./hud.ts"+import { bindKeys } from "./keys.ts"+import { el, escapeHtml, setHtml } from "./kit/dom.ts"+import { iconEl } from "./kit/icons.ts"+import type { Pictures } from "./kit/picture.ts"+import { iconize, touchScreen } from "./kit/text.ts"+import { createMapViews } from "./map-views.ts"+import { createPanel } from "./panels/frame.ts"+import { townHistory } from "./panels/town.ts"+import { createCharter } from "./screens/charter.ts"+import { createConfirm } from "./screens/confirm.ts"+import { createReckoning } from "./screens/reckoning.ts"+import { createShare } from "./screens/share.ts"+import { createVisit } from "./screens/visit.ts"+import { createSystemMenu } from "./system-menu.ts"+import { createTicker } from "./ticker.ts"+import { createToast } from "./toast.ts"+import { createToolbar } from "./toolbar.ts"++export interface Persistence<Camera> {+ readonly save: () => string | null+ readonly load: () => string | null+ /** The town as a save file (as text), with where the camera stands, to share. */+ readonly shareFile: (camera: Camera) => string+}++/** What the page needs of the map, whatever draws it. */+export interface MapPort<Camera> {+ /** The element the map is drawn in: a tap on it folds a phone's HUD and puts a toast away. */+ readonly surface: HTMLElement+ readonly focus: (x: number, y: number) => void+ readonly resetCamera: () => void+ readonly camera: () => Camera+ readonly picture: (width: number, height: number, scale: number) => HTMLCanvasElement+ /** Where the pointer is on the screen, for the tooltip; `null` off the map. */+ readonly pointer: () => Point | null+ /** A touch stroke let go of and held: lay it, or drop it. */+ readonly confirmStroke: () => void+ readonly cancelStroke: () => void+ /** Puts the map input back as it starts: no stroke, the camera's mode reset. */+ readonly reset: () => void+ /** Follows the tool once a frame (the cursor, a held stroke). */+ readonly sync: () => void+}++/**+ * `shared`: the page opened on a shared town's link, so it loads that rather than asking for a charter.+ * `saved`: the newest town saved in this browser, which the first charter offers to carry on with.+ */+export const createDomUi = <Camera>(+ view: { readonly interaction: Interaction; readonly shown: Shown; readonly send: (edit: Edit) => void },+ map: MapPort<Camera>,+ pictures: Pictures,+ persistence: () => Persistence<Camera>,+ options: { readonly shared?: boolean; readonly saved?: { readonly title: string; readonly savedAt: number } | null } = {}+) => {+ const ui = view.interaction+ const town = view.shown.town+ const send = view.send+ const stats = () => town.stats++ // The page's controls, carried out by the parts below once they exist.+ let lastSpeed = 1+ const controls: Controls = {+ toast: (message) => toast.show(message),+ choose: (tool: Tool) => {+ ui.tool = tool+ ui.dragFrom = null+ if (tool !== "place") ui.placing = null+ if (tool !== "upgrade") ui.upgrading = null+ if (tool !== "select") ui.selected = null+ controls.refreshTools()+ },+ toggleMenu: (menu) => {+ folds.setFold(null)+ ui.menu = ui.menu === menu ? null : menu+ if (ui.menu) ui.selected = null+ controls.refreshTools()+ },+ place: (kind: KindId) => {+ ui.placing = kind+ ui.menu = null+ controls.choose("place")+ const title = kinds[kind].title.toLowerCase()+ controls.toast(touchScreen() ? `Tap a spot for the ${title}, then tap it again to build it` : `Click to place a ${title} · Esc to stop`)+ },+ layGround: (ground: GroundId) => {+ ui.ground = ground+ ui.menu = null+ controls.choose("ground")+ const title = grounds[ground].title.toLowerCase()+ controls.toast(touchScreen() ? `Drag a rectangle to lay ${title}, then tap Build` : `Drag a rectangle to lay ${title} · Esc to stop`)+ },+ furnish: (prop: PropId) => {+ ui.prop = prop+ ui.menu = null+ controls.choose("prop")+ const title = props[prop].title.toLowerCase()+ controls.toast(touchScreen() ? `Tap a cell of park for the ${title}, then tap it again to set it` : `Click a cell of park to set a ${title} · Esc to stop`)+ },+ setSpeed: (speed) => {+ if (speed > 0) lastSpeed = speed+ ui.speed = speed+ toolbar.showSpeed(speed, lastSpeed)+ },+ togglePause: () => controls.setSpeed(ui.speed === 0 ? lastSpeed : 0),+ cycleSpeed: () => controls.setSpeed(ui.speed === 0 ? lastSpeed : ui.speed === 4 ? 1 : ui.speed * 2),+ setView: (shown: View) => {+ ui.view = shown+ mapViews.show(shown)+ // On a phone the list folds away once one is chosen, so the map shows.+ if (folds.fold() === "views") folds.setFold(null)+ layersButton.classList.toggle("on", shown !== "none")+ },+ setFold: (fold) => folds.setFold(fold),+ toggleFold: (fold) => folds.toggleFold(fold),+ goTo: (at, size, id) => {+ folds.setFold(null)+ map.focus(at.x + size / 2, at.y + size / 2)+ ui.flash = { cell: { x: at.x, y: at.y }, size, until: Date.now() + 4000 }+ if (id !== null) {+ ui.selected = id+ ui.menu = null+ controls.refreshTools()+ }+ },+ useRef: (name) => {+ folds.setFold(null)+ const ref = refOf(name)+ if (!ref) return+ if ("view" in ref) return controls.setView(ui.view === ref.view ? "none" : ref.view)+ const action = commands[ref.command].action+ if ("menu" in action) return ui.menu === action.menu ? undefined : controls.toggleMenu(action.menu)+ if ("clock" in action) return action.clock === "speed" ? controls.cycleSpeed() : controls.togglePause()+ ui.menu = null+ controls.choose(action.tool)+ },+ refreshTools: () => toolbar.refresh(),+ openCharter: (cancellable) => charter.open(cancellable),+ openShare: () => void share.open(),+ showReckoning: (open) => reckoning.show(open),+ confirmLeave: (title, text, go, run) => confirm.ask(title, text, go, run)+ }++ const toast = createToast()+ const history = townHistory(stats)+ const ticker = createTicker(ui, () => folds.fold(), controls)+ const hud = createHud(ticker.element, send, stats, controls)+ const toolbar = createToolbar(ui, controls)+ const mapViews = createMapViews(ui, controls)+ const panel = createPanel(ui, town, send, pictures, history, controls)+ const confirm = createConfirm(() => persistence().save(), () => ui.speed, controls)+ const reckoning = createReckoning(ui, stats, send, controls)+ const visit = createVisit({ ...controls, closeCharter: () => charter.close(), resetPage: () => resetPage() })+ const charter = createCharter(ui, send, () => persistence().load(), history, options.saved ?? null, () => {+ if (visit.visiting()) visit.endVisit()+ }, controls)+ const share = createShare(stats, () => ui.speed, visit.visiting, map, (camera) => persistence().shareFile(camera), controls)+ const tooltip = el("div", "tooltip")+ // A phone's buttons for what stays folded away: the guide and the map views.+ const foldButtons = el("div", "fold-buttons")+ const layersButton = el("button", "card")+ layersButton.title = "Map overlays"+ layersButton.append(iconEl("layers", 16))+ layersButton.onclick = () => folds.toggleFold("views")+ foldButtons.append(ticker.bulb, layersButton)+ const folds = createFolds(ui, { goods: hud.goodsChip, guide: ticker.bulb, views: layersButton, more: toolbar.moreButton }, ticker.element, () => {+ ui.menu = null+ ui.selected = null+ controls.refreshTools()+ })+ const system = createSystemMenu(ui, persistence, map.resetCamera, folds.fold, controls)+ document.body.append(+ hud.left, hud.right, tooltip, toast.element, toolbar.element, system.element, panel.element, reckoning.result, charter.element, confirm.element,+ reckoning.element, share.element, visit.element, mapViews.list, mapViews.legend, foldButtons+ )++ // A tap on the map folds the HUD and puts a toast away; a pan or a pinch, to look around with the goods or the guide open, does not.+ let mapPress: { id: number; x: number; y: number; moved: boolean } | null = null+ map.surface.addEventListener("pointerdown", (e) => {+ mapPress = mapPress === null && e.isPrimary ? { id: e.pointerId, x: e.clientX, y: e.clientY, moved: false } : null+ })+ map.surface.addEventListener("pointermove", (e) => {+ if (mapPress?.id === e.pointerId && Math.hypot(e.clientX - mapPress.x, e.clientY - mapPress.y) > 8) mapPress.moved = true+ })+ map.surface.addEventListener("pointerup", (e) => {+ if (mapPress?.id === e.pointerId && !mapPress.moved) toast.tapped()+ if (mapPress?.id === e.pointerId && !mapPress.moved && folds.fold() !== null) folds.setFold(null)+ if (mapPress?.id === e.pointerId) mapPress = null+ })+ map.surface.addEventListener("pointercancel", () => (mapPress = null))+ hud.placeFolk()+ phone.addEventListener("change", hud.placeFolk)++ // A town is founded behind the dialog, so there is land to look at while choosing.+ ui.founding = true+ send({ type: "newTown", charter: defaultCharter((Date.now() & 0x7fffffff) >>> 0) })+ controls.setSpeed(1)+ controls.choose("select")+ bindKeys(+ ui,+ {+ escape: () => {+ if (confirm.open()) confirm.dismiss()+ if (share.isOpen()) share.close()+ },+ blocked: () => ui.founding || reckoning.isOpen() || confirm.open() || share.isOpen() || visit.visiting()+ },+ map.cancelStroke,+ controls+ )+ if (!options.shared) charter.open(false)++ // A road, a row of homes, or a patch of park let go of on a touch screen is held here until built or+ // dropped; the tooltip says what it lays and costs.+ const strokeBar = el("div", "stroke-bar")+ const strokeBuild = el("button", "primary", "Build")+ strokeBuild.onclick = () => map.confirmStroke()+ const strokeDrop = el("button", "", "Cancel")+ strokeDrop.onclick = () => map.cancelStroke()+ strokeBar.append(strokeDrop, strokeBuild)+ document.body.append(strokeBar)++ const resetPage = () => {+ controls.choose("select")+ ui.menu = null+ ui.selected = null+ map.reset()+ }++ /** Copies the town's numbers and preview into the page, once a frame. */+ const syncPage = () => {+ for (const message of town.toasts.splice(0)) toast.show(message)+ panel.sync()+ const s = town.stats+ if (s) {+ hud.update(s, ui.speed, folds.fold())+ ticker.update(s)+ system.update(s)+ mapViews.showLegend(town.legend)+ reckoning.sync(s)+ }+ map.sync()+ const screenPointer = map.pointer()+ // A stroke being drawn says what it lays and costs.+ const text = ui.dragFrom && ui.stroke !== "cancel" && town.stroke ? town.stroke : town.tooltip+ // A held touch stroke waits for its build button.+ const held = ui.stroke === "held" && town.stroke !== null+ strokeBar.classList.toggle("open", held)+ document.body.classList.toggle("stroke-held", held)+ if (text && screenPointer) {+ setHtml(tooltip, iconize(escapeHtml(text)))+ tooltip.style.display = "block"+ tooltip.style.left = `${Math.min(screenPointer.x, window.innerWidth - tooltip.offsetWidth - 20)}px`+ tooltip.style.top = `${screenPointer.y}px`+ } else tooltip.style.display = "none"+ }++ return {+ syncPage,+ resetPage,+ toast: controls.toast,+ visit: visit.visit,+ visitFailed: visit.visitFailed,+ visiting: visit.visiting,+ /** A dialog is open over the map: the keyboard is not the map's. */+ blocked: () => document.querySelector(".dialog.open") !== null+ }+}diff --git a/src/ui/dom/keys.ts b/src/ui/dom/keys.tsnew file mode 100644index 0000000..81d81f3--- /dev/null+++ b/src/ui/dom/keys.ts@@ -0,0 +1,48 @@+/**+ * The keyboard: each command's and map view's key from the tool table+ * (`view/tools.ts`), the speeds on 1, 2, and 3, space to pause, and Esc to+ * step back (drop a stroke, put the tool down, close the panel).+ */+import type { Interaction } from "../../view/contracts.ts"+import { commands, mapViews, type Command, type MapViewId } from "../../view/tools.ts"+import type { Controls } from "./controls.ts"++/** `sheet.escape`: Esc closes the sheet open over the map, if any; `sheet.blocked`: the keys are not the map's now. */+export const bindKeys = (+ ui: Interaction,+ sheet: { readonly escape: () => void; readonly blocked: () => boolean },+ cancelStroke: () => void,+ controls: Pick<Controls, "choose" | "toggleMenu" | "setSpeed" | "togglePause" | "setView" | "refreshTools">+) => {+ const letters = new Map<string, () => void>()+ for (const command of Object.values(commands) as Array<Command>) {+ const action = command.action+ if (!command.key || command.key.length !== 1) continue+ letters.set(command.key.toLowerCase(), () => ("tool" in action ? controls.choose(action.tool) : "menu" in action ? controls.toggleMenu(action.menu) : undefined))+ }+ for (const view of Object.keys(mapViews) as Array<MapViewId>)+ letters.set(mapViews[view].key.toLowerCase(), () => controls.setView(ui.view === view ? "none" : view))++ window.addEventListener("keydown", (event) => {+ if (event.target instanceof HTMLElement && event.target.closest("input,select,textarea,[contenteditable=true]")) return+ if (event.key === " " && event.target instanceof HTMLButtonElement) return+ if (event.key === "Escape") sheet.escape()+ if (sheet.blocked()) return+ if (event.key === " ") {+ event.preventDefault()+ controls.togglePause()+ }+ if (event.key === "1") controls.setSpeed(1)+ if (event.key === "2") controls.setSpeed(2)+ if (event.key === "3") controls.setSpeed(4)+ if (event.key === "Escape") {+ // A stroke being drawn or held is dropped first; the tool stays.+ if (ui.dragFrom) cancelStroke()+ else if (ui.tool !== "select") controls.choose("select")+ else if (ui.menu) ui.menu = null+ else ui.selected = null+ controls.refreshTools()+ }+ letters.get(event.key.toLowerCase())?.()+ })+}diff --git a/src/ui/dom/kit/cost-chips.ts b/src/ui/dom/kit/cost-chips.tsnew file mode 100644index 0000000..32f2d53--- /dev/null+++ b/src/ui/dom/kit/cost-chips.ts@@ -0,0 +1,34 @@+/** A cost as icon chips, each marked when the town is short of it. */+import { goods, type GoodId, type Kind } from "../../../game/content/index.ts"+import { el } from "./dom.ts"+import { goodIcon, icon } from "./icons.ts"++/** What the town has, for the chips to compare against; `null` before the first frame. */+export type Holdings = () => { readonly coins: number; readonly stock: Readonly<Record<GoodId, number>> } | null++export const costChips = (cost: Kind["cost"], has: Holdings) => {+ const box = el("span", "chips")+ const chips: Array<{ chip: HTMLElement; short: () => boolean }> = []+ if (cost.coins > 0) {+ const chip = el("span", "chip")+ chip.innerHTML = `${icon("coins", 14)}${cost.coins}`+ chips.push({ chip, short: () => (has()?.coins ?? 0) < cost.coins })+ }+ for (const [g, n] of Object.entries(cost.materials) as Array<[GoodId, number]>) {+ const chip = el("span", "chip")+ chip.title = goods[g].title+ chip.innerHTML = `${goodIcon(g, 14)}${n}`+ chips.push({ chip, short: () => (has()?.stock[g] ?? 0) < n })+ }+ box.append(...chips.map((c) => c.chip))+ const refresh = () => {+ let short = false+ for (const c of chips) {+ const now = c.short()+ c.chip.classList.toggle("short", now)+ short ||= now+ }+ return short+ }+ return { box, refresh }+}diff --git a/src/ui/dom/kit/dom.ts b/src/ui/dom/kit/dom.tsnew file mode 100644index 0000000..84890ac--- /dev/null+++ b/src/ui/dom/kit/dom.ts@@ -0,0 +1,23 @@+/** The few DOM helpers every part of the page builds with. */++export const el = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, text = "") => {+ const node = document.createElement(tag)+ node.className = className+ node.textContent = text+ return node+}++/**+ * Sets a node's markup only when it changed. Rewriting it every frame would+ * replace the element under the pointer between press and release, and the+ * browser then drops the click.+ */+export const setHtml = (node: HTMLElement, html: string) => {+ if (node.dataset.html === html) return+ node.dataset.html = html+ node.innerHTML = html+}++export const escapeHtml = (text: string) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """)++export const hex = (color: number) => `#${color.toString(16).padStart(6, "0")}`diff --git a/src/ui/dom/kit/examples.ts b/src/ui/dom/kit/examples.tsnew file mode 100644index 0000000..f008f53--- /dev/null+++ b/src/ui/dom/kit/examples.ts@@ -0,0 +1,80 @@+/**+ * Every building block of the page in each of its states, on one sheet: the+ * place to look before adding a variant, and the sheet `ui-kit.html` shows+ * (`pnpm dev`, then /ui-kit.html). A new state of a block gets an example here.+ */+import type { Line } from "../../../view/contracts.ts"+import { costChips } from "./cost-chips.ts"+import { el } from "./dom.ts"+import { iconEl, iconNames, icon } from "./icons.ts"+import { syncLines } from "./lines.ts"+import type { Pictures } from "./picture.ts"+import { money, row } from "./rows.ts"+import { section } from "./section.ts"+import { slider } from "./slider.ts"+import { sparkline } from "./sparkline.ts"+import { callouts, iconize } from "./text.ts"++/** Every tone a line can take, with a value, goods, a note, and a fix. */+const lines: ReadonlyArray<Line> = [+ { icon: "workers", label: "workers", value: "4 of 4", tone: "ok" },+ { icon: "workers", label: "workers", value: "2 of 4", tone: "warn", note: "everyone nearby has a job: lay more homes within 15 road cells" },+ { icon: "tools", label: "tools", value: "none", tone: "bad", note: "half pace until one comes", fix: { text: "build a smithy, or buy tools in the [town] panel", build: "smithy" } },+ { icon: "jobs", label: "holds", value: "nothing", tone: "dim" },+ { icon: "people", label: "handed out, last minute", goods: [{ good: "fish", count: 6 }, { good: "bread", count: 2 }] },+ { icon: "upkeep", label: "upkeep", value: "3 coins a minute" }+]++/** `pictures`: where the examples' building pictures come from (the sheet draws a placeholder). */+export const kitExamples = (pictures: Pictures): HTMLElement => {+ const sheet = el("aside", "panel open")+ sheet.style.position = "static"+ sheet.style.width = "380px"+ sheet.style.maxWidth = "none"++ const tones = section("lines", "info", "Every row a panel shows: a value, goods as chips, a note when something wants doing, and what to do about it.")+ tones.classList.add("lines")+ syncLines(pictures, tones, [], lines)+ sheet.append(tones)++ const numbers = section("rows and sums", "coins")+ row(numbers, "settlers", "settlers").textContent = "42"+ money(row(numbers, "taxes", "taxes"), 18)+ money(row(numbers, "upkeep", "upkeep"), -7)+ money(row(numbers, "a minute", "coins"), 0, " coins")+ const spark = el("div", "spark")+ spark.innerHTML = sparkline([3, 5, 4, 8, 7, 11, 10, 14])+ numbers.append(spark)+ sheet.append(numbers)++ const costs = section("costs", "build")+ const enough = costChips({ coins: 40, materials: { planks: 4 } }, () => ({ coins: 100, stock: { wood: 0, planks: 10, stone: 0, ore: 0, tools: 0, fish: 0, smoked: 0, wheat: 0, flour: 0, bread: 0, wool: 0, cloth: 0, furniture: 0 } }))+ const short = costChips({ coins: 120, materials: { stone: 6 } }, () => ({ coins: 100, stock: { wood: 0, planks: 0, stone: 2, ore: 0, tools: 0, fish: 0, smoked: 0, wheat: 0, flour: 0, bread: 0, wool: 0, cloth: 0, furniture: 0 } }))+ enough.refresh()+ short.refresh()+ costs.append(el("p", "empty", "affordable"), enough.box, el("p", "empty", "short of coins and stone"), short.box)+ sheet.append(costs)++ const levers = section("sliders", "taxes")+ const rate = slider(50, 150, 5, () => {})+ rate.sync(120)+ levers.append(rate.input)+ sheet.append(levers)++ const words = section("messages", "chronicle")+ const said = el("p", "")+ said.innerHTML = iconize(callouts("Lay [homes] by a road, build a fishery for 2 planks, then open the [view:workers]. Press (S) to select."))+ words.append(said)+ sheet.append(words)++ const glyphs = section("icons", "picture")+ const grid = el("div", "chips")+ grid.innerHTML = iconNames.map((name) => `<span class="chip" title="${name}">${icon(name, 16)}</span>`).join("")+ glyphs.append(grid)+ sheet.append(glyphs)++ const help = section("a section's help", "info", "A section's help sits behind the ⓘ: one line of what the numbers mean.")+ help.append(iconEl("hint"))+ sheet.append(help)+ return sheet+}diff --git a/src/ui/dom/kit/index.ts b/src/ui/dom/kit/index.tsnew file mode 100644index 0000000..cce0d0e--- /dev/null+++ b/src/ui/dom/kit/index.ts@@ -0,0 +1,15 @@+/**+ * The page's building blocks. Panels and screens compose these; a new state+ * of one is added here (and shown in `examples.ts`), not styled in place.+ */+export { el, escapeHtml, hex, setHtml } from "./dom.ts"+export { costChips, type Holdings } from "./cost-chips.ts"+export { goodIcon, icon, iconEl, iconNames } from "./icons.ts"+export { kitExamples } from "./examples.ts"+export { renderLine, syncLines } from "./lines.ts"+export { thumb, type Pictured, type Pictures } from "./picture.ts"+export { money, row } from "./rows.ts"+export { section } from "./section.ts"+export { slider } from "./slider.ts"+export { sparkline } from "./sparkline.ts"+export { ago, callouts, clockText, iconize, signed, touchScreen } from "./text.ts"diff --git a/src/ui/dom/kit/lines.ts b/src/ui/dom/kit/lines.tsnew file mode 100644index 0000000..a5ad102--- /dev/null+++ b/src/ui/dom/kit/lines.ts@@ -0,0 +1,53 @@+/**+ * The view's lines (see `Line`) as rows: what on the left, its value on the+ * right, any goods as chips, a note underneath, and what to do about it.+ */+import { goods, kinds } from "../../../game/content/index.ts"+import type { Line } from "../../../view/contracts.ts"+import { el, escapeHtml } from "./dom.ts"+import { goodIcon, icon, iconEl } from "./icons.ts"+import { thumb, type Pictures } from "./picture.ts"+import { callouts, iconize } from "./text.ts"++/** A line of a description as a row: its icon and what it is about, its value, any goods as chips, and a note underneath saying why. */+export const renderLine = (pictures: Pictures, row: HTMLElement, l: Line) => {+ // Every row the same way: what on the left, its value on the right, any note under both.+ row.className = `line ${l.tone ?? ""}`+ const chips = l.goods ? `<span class="chips">${l.goods.map((g) => `<span class="chip" title="${goods[g.good].title}">${goodIcon(g.good, 14)}${g.count}</span>`).join("")}</span>` : ""+ // Goods take the value's place: chips on the right.+ row.innerHTML = `<span class="mark">${icon(l.icon, 16)}</span><span class="what"></span>${chips || `<span class="count"></span>`}${l.note ? `<small class="note"></small>` : ""}`+ row.querySelector<HTMLElement>(".what")!.textContent = l.label+ if (!chips) row.querySelector<HTMLElement>(".count")!.innerHTML = iconize(escapeHtml(l.value ?? ""))+ if (l.note) row.querySelector<HTMLElement>(".note")!.innerHTML = iconize(escapeHtml(l.note))+ // What to do about it, on a row of its own: a building to place shows its picture, and the row places one.+ if (l.fix) {+ const build = l.fix.build && kinds[l.fix.build].buildable ? l.fix.build : undefined+ const fix = el(build ? "button" : "div", "fix")+ if (build) {+ fix.dataset.build = build+ fix.title = `Build a ${kinds[build].title.toLowerCase()}`+ fix.append(thumb(pictures, build))+ } else fix.append(iconEl("hint", 14))+ const text = el("span", "do")+ text.innerHTML = iconize(callouts(l.fix.text))+ fix.append(text)+ row.append(fix)+ }+}++/** Keeps a box of rows in step with its lines, touching only the rows that changed. */+export const syncLines = (pictures: Pictures, box: HTMLElement, rows: Array<HTMLElement>, lines: ReadonlyArray<Line>) => {+ while (rows.length < lines.length) {+ const line = el("div", "line")+ rows.push(line)+ box.append(line)+ }+ rows.forEach((row, i) => {+ const l = lines[i]+ const text = l ? JSON.stringify(l) : ""+ if (row.dataset.text === text) return+ row.dataset.text = text+ row.style.display = l ? "" : "none"+ if (l) renderLine(pictures, row, l)+ })+}diff --git a/src/ui/dom/kit/picture.ts b/src/ui/dom/kit/picture.tsnew file mode 100644index 0000000..a1a67c1--- /dev/null+++ b/src/ui/dom/kit/picture.ts@@ -0,0 +1,21 @@+/**+ * Pictures of buildings, homes, and park pieces for the build menu and the+ * panel. The page does not draw them: whatever renders the town provides+ * them (see `render/three/thumbnails.ts`), so a picture matches what stands+ * on the map.+ */+import type { GroundId, KindId, PropId, Tier } from "../../../game/content/index.ts"++/** What can be pictured: a building, a home of a tier, or a park piece. */+export type Pictured = KindId | Tier | `park:${GroundId | PropId}`+/** A picture's URL, at a building's level of upgrades. */+export type Pictures = (what: Pictured, level: number) => Promise<string>++/** A picture as an image, filled in when it is ready. */+export const thumb = (pictures: Pictures, what: Pictured, level: number | boolean = 0): HTMLImageElement => {+ const img = document.createElement("img")+ img.className = "thumb"+ img.alt = ""+ void pictures(what, Number(level)).then((src) => (img.src = src))+ return img+}diff --git a/src/ui/dom/kit/rows.ts b/src/ui/dom/kit/rows.tsnew file mode 100644index 0000000..5876c66--- /dev/null+++ b/src/ui/dom/kit/rows.ts@@ -0,0 +1,25 @@+/** A labelled number in a panel, and a signed sum coloured by its sign. */+import type { GoodId } from "../../../game/content/index.ts"+import type { IconName } from "../../../view/icons.ts"+import { el } from "./dom.ts"+import { icon } from "./icons.ts"+import { signed } from "./text.ts"++/** Appends a row to `box` and returns the node its value goes in. */+export const row = (box: HTMLElement, label: string, glyph?: IconName | GoodId) => {+ const line = el("div", "row")+ const value = el("span", "count")+ const name = el("span", "label")+ if (glyph) name.innerHTML = icon(glyph, 16)+ name.append(document.createTextNode(label))+ line.append(name, value)+ box.append(line)+ return value+}++/** Sets a number with its sign, coloured by it. */+export const money = (node: HTMLElement, n: number, suffix = "") => {+ node.textContent = `${signed(n)}${suffix}`+ node.classList.toggle("up", n > 0)+ node.classList.toggle("down", n < 0)+}diff --git a/src/ui/dom/kit/section.ts b/src/ui/dom/kit/section.tsnew file mode 100644index 0000000..a0b48a9--- /dev/null+++ b/src/ui/dom/kit/section.ts@@ -0,0 +1,28 @@+/** A box of a panel or a sheet, under a heading with an icon. */+import type { IconName } from "../../../view/icons.ts"+import { el } from "./dom.ts"+import { iconEl } from "./icons.ts"++/** `help` is kept behind an ⓘ rather than spelled out. */+export const section = (heading: string, glyph?: IconName, help?: string) => {+ const box = el("section", "")+ const h3 = el("h3", "")+ if (glyph) h3.append(iconEl(glyph))+ h3.append(document.createTextNode(heading))+ box.append(h3)+ if (help) {+ const info = el("button", "help")+ info.append(iconEl("info", 15))+ info.title = "What this means"+ info.setAttribute("aria-expanded", "false")+ const text = el("p", "help-text", help)+ info.onclick = () => {+ const open = !text.classList.contains("open")+ text.classList.toggle("open", open)+ info.setAttribute("aria-expanded", String(open))+ }+ h3.append(info)+ box.append(text)+ }+ return box+}diff --git a/src/ui/dom/kit/slider.ts b/src/ui/dom/kit/slider.tsnew file mode 100644index 0000000..b90f8f5--- /dev/null+++ b/src/ui/dom/kit/slider.ts@@ -0,0 +1,29 @@+/**+ * A slider for one of the town's levers. While it is held, the town's+ * numbers do not move it back; `sync` sets it otherwise.+ */+import { el } from "./dom.ts"++export const slider = (min: number, max: number, step: number, onInput: (value: number) => void) => {+ const input = el("input", "slider")+ input.type = "range"+ input.min = String(min)+ input.max = String(max)+ input.step = String(step)+ let held = false+ input.addEventListener("pointerdown", () => (held = true))+ input.addEventListener("pointerup", () => (held = false))+ input.addEventListener("blur", () => (held = false))+ const paint = () => input.style.setProperty("--at", `${((Number(input.value) - min) / (max - min)) * 100}%`)+ input.oninput = () => {+ paint()+ onInput(Number(input.value))+ }+ const sync = (value: number) => {+ if (held || input.value === String(value)) return+ input.value = String(value)+ paint()+ }+ paint()+ return { input, sync }+}diff --git a/src/ui/dom/kit/sparkline.ts b/src/ui/dom/kit/sparkline.tsnew file mode 100644index 0000000..a3447e1--- /dev/null+++ b/src/ui/dom/kit/sparkline.ts@@ -0,0 +1,13 @@+/** A thin ink line of `values`, scaled to its own range. */+export const sparkline = (values: ReadonlyArray<number>): string => {+ if (values.length < 2) return ""+ const w = 300+ const h = 36+ const lo = Math.min(...values)+ const hi = Math.max(...values)+ const span = hi - lo || 1+ const points = values+ .map((v, i) => `${((i / (values.length - 1)) * w).toFixed(1)},${(h - 2 - ((v - lo) / span) * (h - 4)).toFixed(1)}`)+ .join(" ")+ return `<svg viewBox="0 0 ${w} ${h}" preserveAspectRatio="none"><polyline points="${points}" fill="none" stroke="#3d4943" stroke-width="1.5" vector-effect="non-scaling-stroke"/></svg>`+}diff --git a/src/ui/dom/kit/text.ts b/src/ui/dom/kit/text.tsnew file mode 100644index 0000000..4561ad0--- /dev/null+++ b/src/ui/dom/kit/text.ts@@ -0,0 +1,91 @@+/**+ * The town's messages as markup: amounts as icons, the buildings and goods+ * they name as chips, keys as keys, and "[homes]"-style references as chips+ * that pick what they name (see `view/tools.ts`).+ */+import { goodIds, goods, kindOrder, kinds, type GoodId, type KindId } from "../../../game/content/index.ts"+import { refLook, refOf } from "../../../view/tools.ts"+import { escapeHtml } from "./dom.ts"+import { goodIcon, icon } from "./icons.ts"++/** Words in the town's messages that name a building or a good, longest first, so "smoked fish" wins over "fish". */+const namedThings: ReadonlyArray<{ readonly word: string; readonly ref: { readonly kind: KindId } | { readonly good: GoodId } }> = [+ ...kindOrder.flatMap((id) => {+ const title = kinds[id].title.toLowerCase()+ return [title, `${title}s`].map((word) => ({ word, ref: { kind: id } }))+ }),+ ...goodIds.map((id) => ({ word: goods[id].title, ref: { good: id } })),+ { word: "loaves", ref: { good: "bread" as GoodId } },+ { word: "loaf", ref: { good: "bread" as GoodId } }+].sort((a, b) => b.word.length - a.word.length)+const namedPattern = new RegExp(+ `\\b(${namedThings.map((t) => t.word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})\\b|\\(([A-Z])\\)|\\[([a-z]+(?::[a-z]+)?)\\]`,+ "gi"+)+/** Goods as numbers name them ("2 planks", "1 plank"), and as recipes do ("flour → bread"), by title. */+const goodWords: ReadonlyArray<readonly [string, GoodId]> = [+ ...goodIds.map((id) => [goods[id].title, id] as const),+ ...goodIds.filter((id) => goods[id].title.endsWith("s")).map((id) => [goods[id].title.slice(0, -1), id] as const)+].sort((a, b) => b[0].length - a[0].length)+const goodAlternatives = goodWords.map(([w]) => w).join("|")+const goodOf = (word: string) => goodWords.find(([w]) => w === word)![1]++/**+ * Amounts in a line of (escaped) text as icons, where the icon says it as+ * well as the word: "3 coins a minute" as a coin and 3/min, "2 planks" as a+ * plank and 2, "flour → bread" with each good's icon.+ */+export const iconize = (html: string): string =>+ html+ .replace(/([+−-]?)(\d+) coins a minute/g, (_, sign: string, n: string) => `<span class="amount">${sign}${icon("coins", 13)}${n}/min</span>`)+ .replace(/(\d+) coins\b/g, (_, n: string) => `<span class="amount">${icon("coins", 13)}${n}</span>`)+ .replace(new RegExp(`(\\d+) (${goodAlternatives})\\b`, "g"), (_, n: string, w: string) => `<span class="amount">${goodIcon(goodOf(w), 13)}${n}</span>`)+ .replace(new RegExp(`\\b(${goodAlternatives}) → (${goodAlternatives})\\b`, "g"), (_, a: string, b: string) => `<span class="amount">${goodIcon(goodOf(a), 13)}${a}</span> → <span class="amount">${goodIcon(goodOf(b), 13)}${b}</span>`)+ // A cost as icons side by side: the dots between them only crowd it.+ .replace(/<\/span> · <span class="amount">/g, '</span> <span class="amount">')++/** A finger rather than a mouse: messages say "tap", and keys are left out. */+export const touchScreen = () => matchMedia("(pointer: coarse)").matches+/** `{tap|click}`: the first on a touch screen, the second with a mouse. */+const forDevice = (text: string) => text.replace(/\{([^{}|]*)\|([^{}|]*)\}/g, (_, touch: string, mouse: string) => (touchScreen() ? touch : mouse))++/**+ * A message as markup: each building it names becomes a chip to click (to+ * build one), each good a chip with its icon, each key in round brackets a+ * key, and each tool in square brackets a chip that picks it.+ * `standing`: buildings the line is about, which stand already; their chips go to them rather than placing another.+ * `asks`: when given, only these buildings become chips to place one; others it names are plain words.+ */+export const callouts = (text: string, standing: ReadonlySet<KindId> = new Set(), asks?: ReadonlySet<KindId>): string =>+ escapeHtml(forDevice(text)).replace(namedPattern, (match, word: string | undefined, key: string | undefined, tool: string | undefined) => {+ if (key) return `<kbd class="key">${key}</kbd>`+ if (tool) {+ const ref = refOf(tool)+ if (!ref) return match+ const look = refLook(ref)+ return `<button class="ref tool" data-tool="${tool}" title="${escapeHtml(look.title)}">${icon(look.icon, 13)}${look.label}${look.key ? `<kbd>${look.key}</kbd>` : ""}</button>`+ }+ const thing = namedThings.find((t) => t.word === word!.toLowerCase())+ if (!thing) return match+ if ("good" in thing.ref) return `<span class="ref ware" title="${goods[thing.ref.good].title}">${goodIcon(thing.ref.good, 13)}${match}</span>`+ const kind = kinds[thing.ref.kind]+ if (standing.has(thing.ref.kind)) return `<button class="ref kind go" data-go="${thing.ref.kind}" title="Go to the ${kind.title.toLowerCase()}">${icon("locate", 13)}${match}</button>`+ return kind.buildable && (!asks || asks.has(thing.ref.kind))+ ? `<button class="ref kind" data-kind="${thing.ref.kind}" title="Build a ${kind.title.toLowerCase()}: ${escapeHtml(kind.blurb)}">${icon("build", 13)}${match}</button>`+ : `<span class="ref kind" title="${escapeHtml(kind.blurb)}">${icon("town", 13)}${match}</span>`+ })++/** Seconds as m:ss. */+export const clockText = (seconds: number) => `${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`+export const signed = (n: number) => `${n > 0 ? "+" : n < 0 ? "−" : ""}${Math.abs(n)}`++/** How long ago a save was made, roughly: "just now", "12 min ago", "3 h ago", "2 days ago". */+export const ago = (at: number): string => {+ const minutes = Math.floor((Date.now() - at) / 60000)+ if (minutes < 1) return "just now"+ if (minutes < 60) return `${minutes} min ago`+ const hours = Math.floor(minutes / 60)+ if (hours < 24) return `${hours} h ago`+ const days = Math.floor(hours / 24)+ return days === 1 ? "yesterday" : `${days} days ago`+}diff --git a/src/ui/dom/map-views.ts b/src/ui/dom/map-views.tsnew file mode 100644index 0000000..69f481a--- /dev/null+++ b/src/ui/dom/map-views.ts@@ -0,0 +1,45 @@+/**+ * The map views (`view/tools.ts`), and the legend of the one shown. On a+ * phone the list folds away once one is chosen, so the map shows.+ */+import type { Interaction, Legend, View } from "../../view/contracts.ts"+import { mapViews, type MapViewId } from "../../view/tools.ts"+import type { Controls } from "./controls.ts"+import { el, hex } from "./kit/dom.ts"+import { iconEl } from "./kit/icons.ts"++export const createMapViews = (ui: Interaction, controls: Pick<Controls, "setView">) => {+ const views = el("div", "views")+ const legend = el("div", "card legend")+ const viewsTitle = el("div", "views-title", "map overlays")+ viewsTitle.title = "Colour the map by one of these, to see where the town is doing well or badly. Press the key again (or click) to hide it."+ views.append(viewsTitle)+ const buttons = (Object.keys(mapViews) as Array<MapViewId>).map((view) => {+ const v = mapViews[view]+ const button = el("button", "")+ button.title = `Show ${v.label} over the map (${v.key}); again to hide it`+ button.append(iconEl(v.icon, 15), document.createTextNode(v.label), el("kbd", "", v.key))+ button.onclick = () => controls.setView(ui.view === view ? "none" : view)+ views.append(button)+ return { view, button }+ })+ return {+ list: views,+ legend,+ /** Shows which view is on. */+ show: (view: View) => {+ for (const v of buttons) v.button.classList.toggle("on", v.view === view)+ },+ /** The legend of the view shown, if any. */+ showLegend: (l: Legend | null) => {+ const html = l+ ? `<b>${l.title}</b>${l.keys.map((k) => `<span class="key"><i style="background:${hex(k.color)}"></i>${k.label}</span>`).join("")}<small>${l.note}</small>`+ : ""+ if (legend.dataset.html !== html) {+ legend.dataset.html = html+ legend.innerHTML = html+ }+ legend.classList.toggle("show", l !== null)+ }+ }+}diff --git a/src/ui/dom/panels/build-menu.ts b/src/ui/dom/panels/build-menu.tsnew file mode 100644index 0000000..eb35923--- /dev/null+++ b/src/ui/dom/panels/build-menu.ts@@ -0,0 +1,107 @@+/** The build menu: every building by category, then the park pieces, each with its cost, what it does, and its picture. */+import {+ categoryColor, categoryOrder, goods, groundIds, grounds, home, kindOrder, kinds, park as parkRules, propIds, props, type Category, type GroundId, type Kind, type ParkPiece, type PropId+} from "../../../game/content/index.ts"+import type { IconName } from "../../../view/icons.ts"+import type { Controls } from "../controls.ts"+import { costChips } from "../kit/cost-chips.ts"+import { el, hex } from "../kit/dom.ts"+import { goodIcon, icon, iconEl } from "../kit/icons.ts"+import { thumb } from "../kit/picture.ts"+import { section } from "../kit/section.ts"+import { touchScreen } from "../kit/text.ts"+import type { PanelParts } from "./frame.ts"++const categoryIcon: Readonly<Record<Category, IconName>> = {+ storage: "jobs",+ gathering: "wood",+ crafts: "tools",+ food: "bread",+ services: "faith"+}++/** What keeps the town from building it yet, if anything. */+const lockText = (parts: PanelParts, kind: Pick<Kind, "unlock"> | ParkPiece): string | null => {+ const s = parts.town.stats+ if (!s) return null+ const residents = s.settlers + s.citizens+ if (residents < kind.unlock.residents) return `needs ${kind.unlock.residents} residents`+ if (s.citizens < kind.unlock.citizens) return `needs ${kind.unlock.citizens} citizens`+ return null+}++export const buildMenu = (parts: PanelParts, controls: Pick<Controls, "place" | "layGround" | "furnish">) => {+ const { element: panel, ui } = parts+ const has = () => parts.town.stats+ panel.append(parts.header("build", iconEl("build", 30), el("p", "sub", touchScreen() ? "Pick one, then tap the map where it goes" : "Pick one, then click the map · Esc to stop")))+ for (const category of categoryOrder) {+ const options = kindOrder.filter((id) => kinds[id].category === category && kinds[id].buildable)+ if (options.length === 0) continue+ const box = section(category, categoryIcon[category])+ box.style.borderTop = `3px solid ${hex(categoryColor[category])}`+ for (const id of options) {+ const kind = kinds[id]+ const option = el("button", "option")+ const cost = costChips(kind.cost, has)+ cost.box.prepend(el("span", "cap", "cost"))+ const terms = el("span", "chips")+ const r = kind.recipe+ const does = [+ r ? `<span class="chip" title="${r.input ? `turns ${r.inputs} ${goods[r.input].title} into ${r.batch} ${goods[r.output].title}` : `makes ${r.batch} ${goods[r.output].title}`} every ${r.seconds} s">${r.input ? `${goodIcon(r.input, 14)}<i>→</i>` : ""}${goodIcon(r.output, 14)}${r.batch > 1 ? r.batch : ""}</span>` : "",+ kind.service ? `<span class="chip" title="its walkers serve the homes within ${kind.service.range} road cells of its door">${icon(kind.service.service === "goods" ? "trade" : kind.service.service === "faith" ? "faith" : kind.service.service === "repair" ? "repair" : kind.service.service, 14)}${kind.service.range}</span>` : "",+ kind.holds && !kind.recipe && !kind.service ? `<span class="chip" title="holds up to ${kind.holds.cap} of each good${kind.keeps ? `, and keeps food ${kind.keeps} times as long` : ""}">${icon("jobs", 14)}${kind.holds.cap}</span>` : "",+ kind.workers > 0 ? `<span class="chip dim" title="${kind.workers} ${kind.crew} to work it, from homes within 15 road cells">${icon(kind.crew === "citizens" ? "citizens" : "settlers", 14)}${kind.workers}</span>` : "",+ kind.appeal ? `<span class="chip ${kind.appeal.value < 0 ? "bad" : "good"}" title="desirability ${kind.appeal.value > 0 ? "+" : ""}${kind.appeal.value} beside it, out to ${kind.appeal.reach} cells${kind.appeal.value < 0 ? ": keep homes away" : ": homes like it"}">${icon("appeal", 14)}${kind.appeal.value > 0 ? "+" : ""}${kind.appeal.value}</span>` : "",+ `<span class="chip dim" title="upkeep: coins a minute while it stands (a quarter when paused)">${icon("upkeep", 14)}${kind.upkeep}/min</span>`+ ].join("")+ terms.innerHTML = `<span class="cap">does</span>${does}`+ const blurb = el("span", "blurb", kind.blurb)+ option.append(thumb(parts.pictures, id), el("span", "name", kind.title.toLowerCase()), cost.box, terms, blurb)+ option.title = `${kind.title}: ${kind.blurb}${kind.workers > 0 ? ` · ${kind.workers} ${kind.crew === "citizens" ? "citizen" : "settler"} workers from within 15 road cells` : ""}${kind.service ? ` · walkers reach ${kind.service.range} road cells` : ""}${kind.appeal ? ` · desirability ${kind.appeal.value > 0 ? "+" : ""}${kind.appeal.value} out to ${kind.appeal.reach} cells` : ""} · ${kind.upkeep} coins a minute`+ option.onclick = () => controls.place(id)+ parts.live(() => {+ const lock = lockText(parts, kind)+ const short = cost.refresh()+ option.disabled = lock !== null+ option.classList.toggle("short", lock === null && short)+ blurb.textContent = lock ?? kind.blurb+ })+ box.append(option)+ }+ panel.append(box)+ }+ // Parks are laid out piece by piece: ground first, then what stands on it.+ const parks = section(+ "parks",+ "decor",+ `Lay ground, then set furnishings on it. Ground that touches is one park: the more it holds the nicer the streets around it (citizens need ${home.tiers[2].appeal}, townhouses ${home.tiers[3].appeal}). One furnishing counts for every ${parkRules.cellsPerProp} cells; where parks meet, the finest counts in full.`+ )+ parks.style.borderTop = "3px solid #4f9a6a"+ const pieces: ReadonlyArray<{ readonly id: GroundId | PropId; readonly piece: ParkPiece; readonly pick: () => void; readonly ground: boolean }> = [+ ...groundIds.map((id) => ({ id, piece: grounds[id], pick: () => controls.layGround(id), ground: true })),+ ...propIds.map((id) => ({ id, piece: props[id], pick: () => controls.furnish(id), ground: false }))+ ]+ for (const { id, piece, pick, ground } of pieces) {+ if (ground && id === "lawn") parks.append(el("p", "empty", "ground · drag a rectangle"))+ if (!ground && id === "lamp") parks.append(el("p", "empty", "furnishings · click a cell of park"))+ const option = el("button", "option")+ const cost = costChips(piece.cost, has)+ cost.box.prepend(el("span", "cap", "cost"))+ const terms = el("span", "chips")+ terms.innerHTML = `<span class="cap">does</span><span class="chip good" title="beauty it adds to its park">${icon("appeal", 14)}+${piece.beauty}</span><span class="chip dim" title="upkeep, coins a minute">${icon("upkeep", 14)}${piece.upkeep}/min</span>`+ const blurb = el("span", "blurb", piece.blurb)+ option.append(thumb(parts.pictures, `park:${id}`), el("span", "name", piece.title.toLowerCase()), cost.box, terms, blurb)+ option.title = `${piece.title}: ${piece.blurb} · beauty +${piece.beauty}${ground ? " a cell" : ""} · ${piece.upkeep} coins a minute`+ option.onclick = pick+ parts.live(() => {+ const lock = lockText(parts, piece)+ const short = cost.refresh()+ option.disabled = lock !== null+ option.classList.toggle("short", lock === null && short)+ option.classList.toggle("on", (ground ? ui.tool === "ground" && ui.ground === id : ui.tool === "prop" && ui.prop === id))+ blurb.textContent = lock ?? piece.blurb+ })+ parks.append(option)+ }+ panel.append(parks)+}diff --git a/src/ui/dom/panels/frame.ts b/src/ui/dom/panels/frame.tsnew file mode 100644index 0000000..3d8eb52--- /dev/null+++ b/src/ui/dom/panels/frame.ts@@ -0,0 +1,104 @@+/**+ * The side panel: the build menu, the town's panel, or what is picked. It is+ * built once for what it shows, and its numbers are refreshed in place every+ * frame (each part registers a `live` update), so nothing under the pointer+ * is replaced between press and release.+ */+import { kinds, type KindId } from "../../../game/content/index.ts"+import type { Edit } from "../../../game/edits.ts"+import type { Interaction, Panel, TownView } from "../../../view/contracts.ts"+import type { Controls } from "../controls.ts"+import { el } from "../kit/dom.ts"+import { iconEl } from "../kit/icons.ts"+import type { Pictures } from "../kit/picture.ts"+import { buildMenu } from "./build-menu.ts"+import { selectionPanel } from "./selection.ts"+import { townPanel, type TownHistory } from "./town.ts"++/** What a panel's parts are built with. */+export interface PanelParts {+ readonly town: TownView+ readonly ui: Interaction+ readonly send: (edit: Edit) => void+ readonly pictures: Pictures+ /** Registers an update to run every frame while this panel shows. */+ readonly live: (update: () => void) => void+ /** The panel's header: a picture or icon, the title and what goes under it, and the close button. */+ readonly header: (title: string, lead: HTMLElement, below?: HTMLElement) => HTMLElement+ readonly element: HTMLElement+}++export const createPanel = (+ ui: Interaction,+ town: TownView,+ send: (edit: Edit) => void,+ pictures: Pictures,+ history: TownHistory,+ controls: Pick<Controls, "refreshTools" | "place" | "layGround" | "furnish" | "useRef">+) => {+ const panel = el("aside", "panel")+ /** Volatile parts of the panel, updated in place without rebuilding it. */+ let live: Array<() => void> = []+ let shape = ""+ const header = (title: string, lead: HTMLElement, below?: HTMLElement) => {+ const head = el("header", "")+ const text = el("div", "title")+ text.append(el("h2", "", title))+ if (below) text.append(below)+ const closeButton = el("button", "close")+ closeButton.title = "Close (Esc)"+ closeButton.append(iconEl("close", 18))+ closeButton.onclick = () => {+ ui.selected = null+ ui.menu = null+ controls.refreshTools()+ }+ head.append(lead, text, closeButton)+ return head+ }+ const parts: PanelParts = { town, ui, send, pictures, live: (update) => live.push(update), header, element: panel }++ const build = (p: Panel) => {+ live = []+ panel.replaceChildren()+ if (p.type === "build") return buildMenu(parts, controls)+ if (p.type === "town") return townPanel(parts, history)+ selectionPanel(parts, p)+ }++ // A panel row's fix: place the building it names, or pick the tool or panel it names.+ panel.addEventListener("click", (event) => {+ const target = event.target as HTMLElement+ const tool = target.closest<HTMLElement>(".fix [data-tool]")+ if (tool) return controls.useRef(tool.dataset.tool!)+ const chosen = target.closest<HTMLElement>("[data-build], .fix [data-kind]")+ const kind = (chosen?.dataset.build ?? chosen?.dataset.kind) as KindId | undefined+ if (kind && kinds[kind].buildable) {+ ui.selected = null+ controls.place(kind)+ }+ })++ return {+ element: panel,+ /** Rebuilds the panel when what it shows changes, and refreshes its numbers every frame. */+ sync: () => {+ const p = town.panel+ const next =+ p === null+ ? ""+ : p.type === "build" || p.type === "town"+ ? p.type+ : `${p.type}|${p.id}|${p.type === "house" ? p.tier : p.type === "building" ? `${p.level}|${p.upgrade?.title ?? ""}|${p.works !== null}` : ""}`+ if (next !== shape) {+ shape = next+ // Keep the old contents while the closed panel slides away.+ if (p !== null) build(p)+ panel.classList.toggle("open", p !== null)+ document.body.classList.toggle("panel-open", p !== null)+ controls.refreshTools()+ }+ for (const update of live) update()+ }+ }+}diff --git a/src/ui/dom/panels/selection.ts b/src/ui/dom/panels/selection.tsnew file mode 100644index 0000000..0a9b427--- /dev/null+++ b/src/ui/dom/panels/selection.ts@@ -0,0 +1,140 @@+/**+ * The panel of what is picked: a building, a home, a park, or a ruin. How it+ * is, its rows under headings (kept in step with what the view says each+ * frame), its upgrade, and what can be done to it.+ */+import { tierHome } from "../../../game/content/index.ts"+import type { Group, Panel } from "../../../view/contracts.ts"+import { el } from "../kit/dom.ts"+import { iconEl } from "../kit/icons.ts"+import { syncLines } from "../kit/lines.ts"+import { thumb } from "../kit/picture.ts"+import { section } from "../kit/section.ts"+import type { PanelParts } from "./frame.ts"+import { upgradeBox } from "./upgrade.ts"++export const selectionPanel = (parts: PanelParts, p: Exclude<Panel, { readonly type: "build" } | { readonly type: "town" }>) => {+ const panel = parts.element+ const title = p.type === "building" ? p.title.toLowerCase() : p.type === "house" ? tierHome[p.tier] : p.type === "park" ? "park" : "ruin"+ const status = el("span", "pill")+ const about = el(+ "p",+ "sub",+ p.type === "building"+ ? p.blurb+ : p.type === "ruin"+ ? "bulldoze it to clear the plot"+ : p.type === "park"+ ? "every cell of ground that touches this one: lay more, or set furnishings on it, to make the streets around it nicer"+ : p.tier === 1+ ? "settlers live here and do the rough work"+ : p.tier === 2+ ? "citizens live here, do the skilled work, and pay twice as much"+ : "burghers live here, and pay the best taxes"+ )+ const below = el("div", "")+ // A building says how it is in a box of its own, under the header; a home, a park, or a ruin in a word here.+ if (p.type === "building") below.append(about)+ else below.append(status, about)+ panel.append(parts.header(title, p.type === "ruin" ? iconEl("condition", 30) : p.type === "park" ? iconEl("decor", 30) : thumb(parts.pictures, p.type === "building" ? p.kind : p.tier, p.type === "building" ? p.level : 0), below))+ // A building: how it is first, and what to do about it; then its upgrade, when it has one to offer.+ if (p.type === "building") {+ const box = section("status", p.problem !== null ? "warning" : "jobs")+ box.classList.add("lines", "state")+ const rows: Array<HTMLElement> = []+ parts.live(() => {+ const now = parts.town.panel+ if (now?.type !== "building") return+ box.classList.toggle("trouble", now.problem !== null)+ syncLines(parts.pictures, box, rows, [now.state])+ })+ panel.append(box)+ }+ if (p.type === "building" && p.upgrade) panel.append(upgradeBox(parts, p.upgrade))+ // A building or a home: its rows in boxes under headings, kept in step with what the town says each frame.+ // A park or a ruin: one box.+ const holder = el("div", "")+ panel.append(holder)+ const slots: Array<{ box: HTMLElement; rows: Array<HTMLElement>; key: string }> = []+ const syncGroups = (groups: ReadonlyArray<Group>) => {+ groups.forEach((g, i) => {+ const key = `${g.title}|${g.icon}|${g.help ?? ""}`+ let slot = slots[i]+ if (!slot || slot.key !== key) {+ const box = section(g.title, g.icon, g.help)+ box.classList.add("lines")+ const fresh = { box, rows: [] as Array<HTMLElement>, key }+ if (slot) slot.box.replaceWith(box)+ else holder.append(box)+ slots[i] = fresh+ slot = fresh+ }+ slot.box.style.display = ""+ syncLines(parts.pictures, slot.box, slot.rows, g.lines)+ })+ for (const extra of slots.slice(groups.length)) extra.box.style.display = "none"+ }+ parts.live(() => {+ const now = parts.town.panel+ if (!now || now.type === "build" || now.type === "town" || now.type !== p.type) return+ if (now.type === "building") {+ status.textContent = now.problem ?? now.status+ status.className = `pill ${now.problem !== null ? "problem" : now.status === "working" ? "working" : ""}`+ syncGroups(now.groups)+ } else if (now.type === "house") {+ status.textContent = now.summary+ status.className = `pill ${now.trouble ? "problem" : "working"}`+ syncGroups(now.groups)+ } else if (now.type === "park") {+ status.textContent = `${now.value > 0 ? "+" : ""}${now.value} out to ${now.reach} cells`+ status.className = "pill working"+ syncGroups([{ title: "what makes it", icon: "appeal", lines: now.lines }])+ } else {+ status.textContent = now.cause+ status.className = "pill problem"+ syncGroups([{ title: "what is left", icon: "condition", lines: now.lines }])+ }+ })+ if (p.type === "building" && p.kind === "townhall") return+ {+ const actions = el("div", "actions")+ if (p.type === "building") {+ const pause = el("button", "frame", "Pause work")+ pause.title = "A paused building has no workers and pays a quarter of its upkeep"+ pause.onclick = () => {+ const now = parts.town.panel+ if (now?.type === "building") parts.send({ type: "pause", cell: now.cell, on: !now.paused })+ }+ parts.live(() => {+ const now = parts.town.panel+ if (now?.type !== "building") return+ pause.textContent = now.paused ? "Resume work" : "Pause work"+ pause.style.display = now.pausable ? "" : "none"+ })+ actions.append(pause)+ }+ if (p.type === "house") {+ const close = el("button", "frame", "Close home")+ close.title = "A closed home stands empty: no taxes, and no cost to the town hall's administration. Reopen it and settlers move back in."+ close.onclick = () => {+ const now = parts.town.panel+ if (now?.type === "house") parts.send({ type: "pause", cell: now.cell, on: !now.closed })+ }+ parts.live(() => {+ const now = parts.town.panel+ if (now?.type !== "house") return+ close.textContent = now.closed ? "Reopen home" : "Close home"+ close.style.display = now.closable ? "" : "none"+ })+ actions.append(close)+ }+ const remove = el("button", "frame danger", "Remove")+ remove.onclick = () => parts.send({ type: "remove", cell: p.cell })+ parts.live(() => {+ const now = parts.town.panel+ remove.title = now?.type === "building" || now?.type === "house" ? now.salvage : ""+ })+ actions.append(remove)+ panel.append(actions)+ }+}diff --git a/src/ui/dom/panels/town.ts b/src/ui/dom/panels/town.tsnew file mode 100644index 0000000..d261b34--- /dev/null+++ b/src/ui/dom/panels/town.ts@@ -0,0 +1,360 @@+/**+ * The town's panel: its rank and the goals that raise it, approval, the+ * treasury, the tax rates, the people, the homes supplied, the crown, goods+ * and trade, and the chronicle.+ */+import {+ goodIds, goods, LEDGER_TICKS, lands, mood as moodRules, needTitle, RESERVE_MAX, sizes, step, TAX_STEPS, TRADE_LOAD, type GoodId+} from "../../../game/content/index.ts"+import type { TradeMode } from "../../../game/edits.ts"+import { askOf, priceOf } from "../../../game/schedule.ts"+import type { Stats } from "../../../view/contracts.ts"+import type { IconName } from "../../../view/icons.ts"+import { el } from "../kit/dom.ts"+import { goodIcon, icon, iconEl } from "../kit/icons.ts"+import { money, row } from "../kit/rows.ts"+import { section } from "../kit/section.ts"+import { slider } from "../kit/slider.ts"+import { sparkline } from "../kit/sparkline.ts"+import { clockText, signed } from "../kit/text.ts"+import type { PanelParts } from "./frame.ts"++/** The goods in store a minute ago, sampled by the page (see `townHistory`). */+export interface TownHistory {+ readonly minuteAgo: () => { readonly stock: Readonly<Record<GoodId, number>> } | undefined+ /** Starts over: a new town, or one loaded. */+ readonly clear: () => void+}++/** One sample every `SAMPLE_SECONDS`, the last ten minutes of them. */+export const townHistory = (stats: () => Stats | null): TownHistory => {+ interface Sample {+ readonly coins: number+ readonly residents: number+ readonly stock: Readonly<Record<GoodId, number>>+ }+ const samples: Array<Sample> = []+ const SAMPLE_SECONDS = 4+ const HISTORY = 150+ window.setInterval(() => {+ const s = stats()+ if (!s) return+ samples.push({ coins: s.coins, residents: s.settlers + s.citizens, stock: s.stock })+ if (samples.length > HISTORY) samples.shift()+ }, SAMPLE_SECONDS * 1000)+ return {+ minuteAgo: () => samples[Math.max(0, samples.length - 1 - Math.round(60 / SAMPLE_SECONDS))],+ clear: () => {+ samples.length = 0+ }+ }+}++export const townPanel = (parts: PanelParts, history: TownHistory) => {+ const panel = parts.element+ const sub = el("p", "sub")+ panel.append(parts.header("town", iconEl("town", 30), sub))++ // The town's rank, the goals that raise it to the next, and how near each one is.+ const goals = section(+ "goals",+ "goal",+ "The town rises from a hamlet to a village, a town, and a city. Meet every goal of the next rank at once and hold them for two minutes: the crown sends a reward, and the game goes on. A rank is never lost."+ )+ const goalRows: Array<{ line: HTMLElement; mark: HTMLElement; text: HTMLElement; value: HTMLElement }> = []+ const goalNote = el("p", "empty")+ panel.append(goals)++ const mood = section(+ "approval",+ "approval",+ `Homes hold more residents when approval is high and fewer when it is low. At −50 for two minutes, the town turns the mayor out.`+ )+ const moodLine = el("div", "row")+ const moodValue = el("span", "count total")+ moodLine.append(el("span", "label", "now → heading for"), moodValue)+ const moodTrack = el("div", "meter")+ const moodFill = el("span", "fill")+ moodTrack.append(moodFill)+ const moodParts = el("div", "")+ mood.append(moodLine, moodTrack, moodParts)+ panel.append(mood)++ const treasury = section(+ "treasury",+ "treasury",+ "Coins a minute. Taxes come only from homes that are fed, in proportion to the needs they have met. Administration is reckoned by the home and grows faster than the town: in a large town another cottage costs about what it pays, while a citizens' home or a townhouse holds two or three times the people for the same cost. Three minutes in debt and the town is bankrupt."+ )+ const taxes = row(treasury, "taxes", "taxes")+ const supplied = row(treasury, "if every home were supplied", "homes")+ supplied.classList.add("dim")+ const suppliedNote = el("p", "empty")+ treasury.append(suppliedNote)+ const traded = row(treasury, "trade", "trade")+ const upkeep = row(treasury, "upkeep", "upkeep")+ const admin = row(treasury, "administration", "town")+ const net = row(treasury, "a minute", "coins")+ net.classList.add("total")+ const coinLine = el("div", "spark")+ treasury.append(coinLine)+ panel.append(treasury)++ const levers = section(+ "tax rates",+ "taxes",+ `Lower taxes raise approval, higher ones lower it, weighed by how many of each tier there are. Above 100% some residents also move away, settlers sooner than citizens, so a higher rate does not always bring in more: under each rate is what the homes would pay at each step once they settle there. Above ${moodRules.grudge.above}% the town also keeps a grudge that outlasts the rate: cutting taxes at the end does not undo a term of squeezing.`+ )+ for (const tier of ["settlers", "citizens"] as const) {+ const line = el("div", "row lever")+ const value = el("span", "rate")+ const name = el("span", "label")+ name.innerHTML = icon(tier, 16)+ name.append(document.createTextNode(tier))+ line.append(name, value)+ const rate = slider(TAX_STEPS[0], TAX_STEPS.at(-1)!, 5, (to) => parts.send({ type: "tax", tier, rate: to }))+ rate.input.classList.add("tax")+ rate.input.title = "Drag to set the rate; double-click for the usual 100%"+ rate.input.ondblclick = () => parts.send({ type: "tax", tier, rate: 100 })+ const effect = el("p", "effect")+ const outlook = el("p", "effect outlook")+ outlook.title = "Taxes a minute from every home if this rate stayed there and the homes settled: residents move away above 100%, and approval follows the rate"+ levers.append(line, rate.input, effect, outlook)+ parts.live(() => {+ const now = parts.town.stats?.policy[tier] ?? 100+ rate.sync(now)+ value.textContent = `${now}%`+ const sway = Math.round((100 - now) * moodRules.perTaxPoint)+ const grudge = now > moodRules.grudge.above ? " · a grudge builds while it stays this high, and fades slowly" : ""+ effect.textContent = (sway === 0 ? "the usual rate" : `${signed(sway)} approval`) + grudge+ effect.className = `effect ${sway > 0 ? "up" : sway < 0 ? "down" : ""}`+ const steps = parts.town.stats?.taxOutlook[tier] ?? []+ const best = steps.reduce<{ rate: number; taxes: number } | null>((b, o) => (b === null || o.taxes > b.taxes ? o : b), null)+ const text = steps.length === 0 || best === null+ ? ""+ : `taxes a minute, once settled: ${steps.map((o) => `${o.rate}% ${o.taxes}`).join(" · ")} · most at ${best.rate}%`+ if (outlook.textContent !== text) outlook.textContent = text+ outlook.style.display = text ? "" : "none"+ })+ }+ panel.append(levers)++ const town = section("people", "people")+ const settlers = row(town, "settlers", "settlers")+ const citizens = row(town, "citizens", "citizens")+ const burghers = row(town, "of them, burghers", "burghers")+ const homes = row(town, "homes", "homes")+ const work = row(town, "jobs filled", "jobs")+ const workNote = el("p", "empty")+ town.append(workNote)+ const peopleLine = el("div", "spark")+ town.append(peopleLine)+ panel.append(town)++ const needs = section(+ "homes supplied",+ "homes",+ "A hungry home pays no taxes. Settlers need food (fish, smoked fish, or bread) from a market, and a cottage with bread besides pays half again; a visit feeds a home a fish or a loaf for every four residents, so a citizens' home eats twice what a cottage does and a townhouse three times; citizens and burghers need bread, cloth, and faith, and burghers furniture from a carpenter besides. Cloth is only brought to streets with desirability 5 or more, furniture to streets of 12 or more. Fish rots in about three minutes and bread in five; a granary keeps them five times as long."+ )+ const needIcons: Readonly<Record<"food" | "bread" | "cloth" | "faith" | "furniture", IconName>> = {+ food: "fish",+ bread: "bread",+ cloth: "cloth",+ faith: "faith",+ furniture: "furniture"+ }+ const bars = (["food", "bread", "cloth", "faith", "furniture"] as const).map((n) => {+ const line = el("div", "need")+ // The icon alone says which need: the name is its tooltip, so no word runs under the bar.+ const label = el("span", "label")+ label.innerHTML = icon(needIcons[n], 16)+ label.title = needTitle[n]+ label.setAttribute("aria-label", needTitle[n])+ const track = el("span", "track")+ const fill = el("span", "fill")+ track.append(fill)+ const value = el("span", "count")+ line.append(label, track, value)+ needs.append(line)+ return { n, fill, value }+ })+ const larderLine = row(needs, "food in store", "flour")+ const eatenLine = row(needs, "eaten · rotted last minute", "fish")+ const troubleLine = row(needs, "sick homes · on fire", "health")+ panel.append(needs)++ const crown = section(+ "the crown",+ "crown",+ `Sending what the crown asks for earns coins. A missed request is a fine, and the town remembers the snub for a while; too many missed in a row and the crown dismisses the mayor.`+ )+ const crownLine = row(crown, "requests met · missed", "goal")+ const crownNext = el("p", "empty")+ crown.append(crownNext)+ panel.append(crown)++ const store = section(+ "goods and trade",+ "trade",+ "Traders call every few seconds. Drag a good's slider left to sell it beyond what the town keeps, right to buy it up to that (at half again the price), the further the more each call; the middle neither sells nor buys. Prices fall as the town sells and rise, three times as fast, as it buys; a dear price calms only slowly, so buying tops up a shortfall but a town fed on bought food pays more every minute."+ )+ const reserveLine = el("div", "row lever")+ const reserveValue = el("span", "rate")+ reserveLine.append(el("span", "label", "keep in store"), reserveValue)+ const reserve = slider(0, RESERVE_MAX, 5, (amount) => parts.send({ type: "reserve", amount }))+ reserve.input.title = "How much of each good the town keeps: it sells only beyond this, and buys only up to it"+ store.append(reserveLine, reserve.input, el("p", "effect", "of each good: sold only above this, bought only up to it"))+ /** Collections a minute: the traders come at every ledger. */+ const perMinute = 60 / (LEDGER_TICKS * step.dt)+ const goodRows = goodIds.map((g) => {+ const line = el("div", "trade-row")+ const name = el("span", "label")+ name.innerHTML = goodIcon(g, 16)+ name.append(document.createTextNode(goods[g].title))+ const value = el("span", "count")+ const price = el("span", "price")+ const top = el("div", "row")+ const change = el("small", "change")+ const right = el("span", "label")+ right.append(change, value, price)+ top.append(name, right)+ // Left of the middle sells, right of it buys, the middle neither; how far out is how much a collection.+ const deal = slider(-TRADE_LOAD, TRADE_LOAD, 1, (v) =>+ parts.send(v === 0 ? { type: "trade", good: g, mode: "none" } : { type: "trade", good: g, mode: v < 0 ? "sell" : "buy", amount: Math.abs(v) })+ )+ deal.input.classList.add("deal")+ deal.input.title = `Drag left to sell ${goods[g].title}, right to buy it; the further, the more each time the traders call. Double-click to stop trading it.`+ deal.input.ondblclick = () => parts.send({ type: "trade", good: g, mode: "none" })+ const mode = el("span", "mode")+ const bottom = el("div", "deal-line")+ bottom.append(deal.input, mode)+ line.append(top, bottom)+ store.append(line)+ return { g, value, change, mode, price, deal, line }+ })+ panel.append(store)++ const chronicle = section("chronicle", "chronicle")+ const lines = el("div", "chronicle")+ chronicle.append(lines)+ panel.append(chronicle)++ parts.live(() => {+ const s = parts.town.stats+ if (!s) return+ sub.textContent = `${sizes[s.charter.size].title}, ${lands[s.charter.land].title} land · a ${s.fate.rankTitle} · term ${s.fate.term}, ${s.charter.term} minutes`+ sub.title = `seed ${s.charter.seed}`+ // Goals.+ while (goalRows.length < s.goals.length) {+ const line = el("div", "row goal")+ const mark = el("span", "mark", "✓")+ const text = el("span", "")+ const value = el("span", "count")+ const left = el("span", "label")+ left.append(mark, text)+ line.append(left, value)+ goals.append(line)+ goalRows.push({ line, mark, text, value })+ }+ goals.append(goalNote)+ goalRows.forEach((r, i) => (r.line.style.display = i < s.goals.length ? "" : "none"))+ s.goals.forEach((g, i) => {+ const r = goalRows[i]!+ r.line.classList.toggle("met", g.met)+ r.text.textContent = g.text+ r.value.textContent = g.value+ })+ goalNote.textContent = s.fate.nextTitle === null+ ? `A ${s.fate.rankTitle}, the highest rank: keep it standing, term after term.`+ : s.fate.rises !== null+ ? `All met: hold them ${clockText(s.fate.rises)} more and the ${s.fate.rankTitle} is a ${s.fate.nextTitle}.`+ : `A ${s.fate.rankTitle} now: these make it a ${s.fate.nextTitle}.`+ goalNote.style.display = goalNote.textContent ? "" : "none"+ // Approval.+ moodValue.textContent = `${signed(s.mood.approval)} → ${signed(s.mood.target)}`+ moodValue.classList.toggle("down", s.mood.approval < 0)+ moodFill.style.left = `${Math.min(50, 50 + s.mood.approval / 2)}%`+ moodFill.style.width = `${Math.abs(s.mood.approval) / 2}%`+ moodFill.classList.toggle("low", s.mood.approval < 0)+ const moodHtml = s.mood.parts+ .map(+ (p) =>+ `<div class="row"><span class="label">${p.label}</span><span class="count ${p.value > 0 ? "up" : p.value < 0 ? "down" : ""}">${signed(p.value)}</span></div>`+ )+ .join("")+ if (moodParts.dataset.html !== moodHtml) {+ moodParts.dataset.html = moodHtml+ moodParts.innerHTML = moodHtml+ }+ money(taxes, s.taxes)+ supplied.textContent = `+${s.suppliedTaxes}`+ const gap = s.suppliedTaxes - s.taxes+ suppliedNote.textContent = gap > Math.max(10, s.taxes * 0.1) ? `${gap} coins a minute left on the table: a hungry home pays nothing, a cottage with bread pays half again, a fine street a quarter more` : ""+ suppliedNote.style.display = suppliedNote.textContent ? "" : "none"+ money(traded, s.trade)+ money(upkeep, -s.upkeep)+ money(admin, -s.admin)+ money(net, s.net, " coins")+ settlers.textContent = String(s.settlers)+ citizens.textContent = String(s.citizens)+ burghers.textContent = String(s.burghers)+ homes.textContent = String(s.homes)+ work.textContent = `${s.employed} of ${s.jobs}`+ workNote.textContent = s.shortStaffed.length === 0+ ? "every workplace has its crew"+ : `${s.shortStaffed.length} short of workers: ${s.shortStaffed.slice(0, 4).map((w) => `${w.title} ${w.assigned}/${w.needed}${w.nobodyNear ? " (nobody within reach)" : ""}`).join(", ")}${s.shortStaffed.length > 4 ? ", …" : ""} · click the jobs counter to visit them`+ for (const b of bars) {+ // Furniture is counted against the townhouses: no other home keeps it.+ const of = b.n === "furniture" ? Math.max(s.townhouses, s.needs.furniture) : s.homes+ const share = of > 0 ? s.needs[b.n] / of : 0+ b.fill.style.width = `${Math.round(share * 100)}%`+ b.fill.className = `fill ${of === 0 ? "" : share < 0.34 ? "none" : share < 0.67 ? "low" : ""}`+ b.value.textContent = `${s.needs[b.n]}/${of}`+ }+ larderLine.textContent = `${s.larder}`+ eatenLine.textContent = `${s.eaten} · ${s.rotted}`+ troubleLine.textContent = `${s.sick} · ${s.burning}`+ crownLine.textContent = `${s.crown.met} · ${s.crown.missed}`+ crownNext.innerHTML = s.crown.open+ ? `Wants <b>${s.crown.amount} ${goods[s.crown.good].title}</b> · ${clockText(s.crown.left)} left · +${s.crown.reward} coins`+ : `Next request in ${clockText(s.crown.next)} · ${s.crown.patience} missed in a row ends the term`+ // Goods' change over the last minute of samples.+ const ago = history.minuteAgo()+ reserveValue.textContent = String(s.policy.reserve)+ reserve.sync(s.policy.reserve)+ for (const r of goodRows) {+ const change = ago ? s.stock[r.g] - ago.stock[r.g] : 0+ r.change.textContent = change !== 0 ? `${change > 0 ? "+" : ""}${change}/min` : ""+ r.change.className = `change ${change > 0 ? "up" : "down"}`+ r.value.textContent = String(s.stock[r.g])+ const now: TradeMode = s.policy.sells.includes(r.g) ? "sell" : s.policy.buys.includes(r.g) ? "buy" : "none"+ const load = s.policy.loads[r.g]+ r.deal.sync(now === "sell" ? -load : now === "buy" ? load : 0)+ r.mode.textContent = now === "none" ? "no trade" : `${now} ≤${Math.round(load * perMinute)}/min`+ r.mode.className = `mode ${now}`+ r.line.className = `trade-row ${now}`+ const price = now === "buy" ? askOf(r.g, s.prices[r.g]) : Math.round(priceOf(r.g, s.prices[r.g]) * 10) / 10+ r.price.textContent = `${price}c${s.prices[r.g] !== 100 ? ` (${s.prices[r.g]}%)` : ""}`+ r.price.title = now === "buy" ? "what traders ask for one" : "what traders pay for one"+ }+ const samples = s.history+ const coinsSvg = sparkline(samples.map((h) => h.coins))+ if (coinLine.dataset.svg !== coinsSvg) {+ coinLine.dataset.svg = coinsSvg+ coinLine.innerHTML = coinsSvg+ }+ const peopleSvg = sparkline(samples.map((h) => h.residents))+ if (peopleLine.dataset.svg !== peopleSvg) {+ peopleLine.dataset.svg = peopleSvg+ peopleLine.innerHTML = peopleSvg+ }+ const recent = s.chronicle+ .slice(-10)+ .reverse()+ .map((l) => `<p><b>${clockText(Math.round(l.tick / 60))}</b> ${l.text}</p>`)+ .join("")+ if (lines.dataset.html !== recent) {+ lines.dataset.html = recent+ lines.innerHTML = recent+ }+ })+}diff --git a/src/ui/dom/panels/upgrade.ts b/src/ui/dom/panels/upgrade.tsnew file mode 100644index 0000000..a7f1bfd--- /dev/null+++ b/src/ui/dom/panels/upgrade.ts@@ -0,0 +1,68 @@+/**+ * What upgrading a building would cost and give, said in full before the+ * mayor pays, with the button that begins it.+ */+import { goods, type GoodId } from "../../../game/content/index.ts"+import type { Line, Panel } from "../../../view/contracts.ts"+import type { IconName } from "../../../view/icons.ts"+import { el } from "../kit/dom.ts"+import { syncLines } from "../kit/lines.ts"+import { section } from "../kit/section.ts"+import type { PanelParts } from "./frame.ts"++/** An upgrade's gain as a row: what changes, on the left; from what to what, on the right. */+const gainLine = (change: string): Line => {+ const at = change.search(/[+−-]?\d/)+ const label = at > 0 ? change.slice(0, at).trim() : change, rest = at > 0 ? change.slice(at) : undefined+ // "+6 → +8, out to 6 cells": the change on the right, what qualifies it under it.+ const [value, note] = rest?.includes(", ") ? [rest.slice(0, rest.indexOf(", ")), rest.slice(rest.indexOf(", ") + 2)] : [rest, undefined]+ const glyph: IconName =+ /^holds/.test(change) ? "build" : /^carriers/.test(change) ? "trade" : /^walker|^a walker/.test(change) ? "people"+ : /^administration/.test(change) ? "taxes" : /^desirability/.test(change) ? "appeal" : /^workers/.test(change) ? "workers"+ : /^firefighters/.test(change) ? "fire" : /rock/.test(change) ? "stone" : /a minute/.test(change) ? "jobs" : "goal"+ return { icon: glyph, label, value, note, tone: "ok" }+}+/**+ * What upgrading a building would cost and give, said in full before the mayor pays: what is paid now, what+ * carriers bring later and the upkeep after; then what it gives. The button that begins it comes last, alone.+ */+export const upgradeBox = (parts: PanelParts, offer: NonNullable<Extract<Panel, { type: "building" }>["upgrade"]>) => {+ const box = section(`upgrade to ${offer.title.toLowerCase()}`, "build", offer.blurb)+ box.classList.add("lines", "upgrade")+ // Why the town needs it now (the gold arrow over it), or that it can wait.+ const needed = el("p", "needed", "")+ const pays = el("div", "part"), gets = el("div", "part")+ const payRows: Array<HTMLElement> = [], getRows: Array<HTMLElement> = []+ const upkeep = offer.changes.find((c) => c.startsWith("upkeep"))+ syncLines(parts.pictures, gets, getRows, offer.changes.filter((c) => c !== upkeep).map(gainLine))+ const begin = el("button", "frame primary", `Begin the ${offer.title.toLowerCase()}`)+ begin.onclick = () => {+ const now = parts.town.panel+ if (now?.type === "building") parts.send({ type: "upgrade", cell: now.cell })+ }+ const why = el("p", "sub", "")+ parts.live(() => {+ const now = parts.town.panel+ if (now?.type !== "building" || !now.upgrade) return+ needed.textContent = now.upgrade.needed ? `needed now: ${now.upgrade.needed}` : "not needed yet: the gold arrow shows when it is"+ needed.classList.toggle("now", now.upgrade.needed !== null)+ const coins = parts.town.stats?.coins ?? 0, stock = parts.town.stats?.stock+ const materials = (Object.entries(offer.cost.materials) as Array<[GoodId, number]>).filter(([, n]) => n > 0)+ const lacking = materials.filter(([g, n]) => (stock?.[g] ?? 0) < n).map(([g, n]) => `${n - (stock?.[g] ?? 0)} ${goods[g].title}`)+ syncLines(parts.pictures, pays, payRows, [+ { icon: "coins", label: "now, to begin", value: `${offer.cost.coins} coins`, tone: coins < offer.cost.coins ? "bad" : undefined,+ note: coins < offer.cost.coins ? `${offer.cost.coins - coins} coins short` : undefined },+ ...(materials.length > 0+ ? [{ icon: "trade", label: "later, carried in", goods: materials.map(([good, count]) => ({ good, count })), tone: lacking.length > 0 ? "warn" : undefined,+ note: lacking.length > 0 ? `the store lacks ${lacking.join(", ")}: the works wait for them` : "from the store: the works go on as they come" } as Line]+ : []),+ ...(upkeep ? [{ icon: "upkeep", label: "upkeep after", value: upkeep.replace(/^upkeep /, "") } as Line] : [])+ ])+ begin.disabled = now.upgrade.blocked !== null || now.upgrade.short !== null+ why.textContent = now.upgrade.blocked ?? now.upgrade.short ?? "it goes on working while the works go on, and scaffolding rises inside its plot"+ })+ const footer = el("div", "begin")+ footer.append(begin, why)+ box.append(needed, el("p", "cap", "you pay"), pays, el("p", "cap", "you get"), gets, footer)+ return box+}diff --git a/src/ui/dom/screens/charter.ts b/src/ui/dom/screens/charter.tsnew file mode 100644index 0000000..6ff019f--- /dev/null+++ b/src/ui/dom/screens/charter.ts@@ -0,0 +1,213 @@+/**+ * Founding a town: the charter dialog. Pick the land's size, the term, and+ * its richness; the land is drawn small as it would be founded. On the way+ * in, a town saved in this browser can be carried on instead.+ */+import { defaultCharter, landIds, lands, sizeIds, sizes, TERMS, YEARS_PER_TERM, type CharterValue } from "../../../game/content/index.ts"+import type { Edit } from "../../../game/edits.ts"+import { GRID } from "../../../game/world/iso.ts"+import { generate } from "../../../game/world/mapgen.ts"+import type { Interaction } from "../../../view/contracts.ts"+import type { IconName } from "../../../view/icons.ts"+import type { Controls } from "../controls.ts"+import { el, escapeHtml, setHtml } from "../kit/dom.ts"+import { icon } from "../kit/icons.ts"+import { section } from "../kit/section.ts"+import { ago } from "../kit/text.ts"+import type { TownHistory } from "../panels/town.ts"++/**+ * `savedTown`: the newest town saved in this browser, which the first charter offers to carry on with.+ * `founded`: a town is about to be founded from the dialog (a visit to a shared town ends).+ */+export const createCharter = (+ ui: Interaction,+ send: (edit: Edit) => void,+ load: () => string | null,+ history: TownHistory,+ savedTown: { readonly title: string; readonly savedAt: number } | null,+ founded: () => void,+ controls: Pick<Controls, "toast" | "showReckoning" | "choose" | "setSpeed">+) => {+ const dialog = el("div", "dialog")+ const choice: { -readonly [K in keyof CharterValue]: CharterValue[K] } = defaultCharter((Date.now() & 0x7fffffff) >>> 0)+ /** The land a charter would found, drawn small: a dot a cell. */+ const drawLand = (canvas: HTMLCanvasElement) => {+ const land = generate(choice)+ const water = new Set(land.water.map((c) => c.y * GRID + c.x))+ const trees = new Set(land.trees.map((t) => t.cell.y * GRID + t.cell.x))+ const rocks = new Map(land.rocks.map((r) => [r.cell.y * GRID + r.cell.x, r.ore]))+ const scale = 3+ canvas.width = GRID * scale+ canvas.height = GRID * scale+ const g = canvas.getContext("2d")!+ g.clearRect(0, 0, canvas.width, canvas.height)+ for (let y = 0; y < GRID; y++) {+ for (let x = 0; x < GRID; x++) {+ const k = y * GRID + x+ if (land.mask[k] !== "1") continue+ g.fillStyle = water.has(k)+ ? "#b9cfcb"+ : trees.has(k)+ ? "#9bb08a"+ : rocks.has(k)+ ? rocks.get(k)+ ? "#5a6479"+ : "#8f9891"+ : "#f7f8f4"+ g.fillRect(x * scale, y * scale, scale, scale)+ }+ }+ return land+ }+ const openCharter = (cancellable: boolean) => {+ ui.founding = true+ dialog.replaceChildren()+ const card = el("div", "sheet wide")+ const title = el("h2", "title", "Inkline Town ")+ const by = el("small", "by", "by ")+ const author = el("a", "", "Sandro Maglione") as HTMLAnchorElement+ author.href = "https://www.sandromaglione.com/"+ author.target = "_blank"+ author.rel = "noopener"+ by.append(author)+ title.append(by)+ card.append(title, el("p", "sub", "Pick the land, then grow the town to its goal before the term runs out."))+ const body = el("div", "charter")+ const options = el("div", "")+ const preview = el("div", "preview")+ const mapBox = el("div", "map")+ const map = document.createElement("canvas")+ mapBox.append(map)+ const facts = el("p", "note facts")+ const cells = el("span", "ref ware cells")+ mapBox.append(cells)+ preview.append(mapBox, facts)+ body.append(options, preview)+ /** A figure of the charter as the town's messages name things: an icon and a few words. */+ const chip = (glyph: IconName, text: string) => `<span class="ref ware">${icon(glyph, 13)}${escapeHtml(text)}</span>`+ const group = <T extends string | number>(+ title: string,+ glyph: IconName,+ values: ReadonlyArray<T>,+ label: (v: T) => string,+ note: (v: T) => string,+ tip: (v: T) => string,+ get: () => T,+ set: (v: T) => void+ ) => {+ const box = section(title, glyph)+ const row = el("div", "choices")+ const said = el("p", "note")+ const buttons = values.map((v) => {+ const b = el("button", "frame choice", label(v))+ b.title = tip(v)+ b.onclick = () => {+ set(v)+ refresh()+ }+ row.append(b)+ return { v, b }+ })+ box.append(row, said)+ options.append(box)+ return () => {+ for (const { v, b } of buttons) b.classList.toggle("on", v === get())+ setHtml(said, note(get()))+ }+ }+ const refreshers = [+ group(+ "land",+ "pave",+ sizeIds,+ (v) => sizes[v].title,+ (v) => `a town at ${chip("people", `${sizes[v].goal.residents} residents`)}${chip("coins", `+${sizes[v].goal.income}/min`)}`,+ (v) => `The town rank asks ${sizes[v].goal.residents} residents and an income of ${sizes[v].goal.income} coins a minute; a village less, a city more`,+ () => choice.size,+ (v) => (choice.size = v)+ ),+ group(+ "term",+ "clock",+ TERMS,+ (v) => `${v} min`,+ (v) => `${chip("clock", `${YEARS_PER_TERM} years of ${Number((v / YEARS_PER_TERM).toFixed(1))} min`)}${chip("winter", `${YEARS_PER_TERM - 1} winters`)}`,+ (v) => `${v} minutes at 1×: ${YEARS_PER_TERM} years, a winter at the end of each after the first`,+ () => choice.term,+ (v) => (choice.term = v)+ ),+ group(+ "richness",+ "wood",+ landIds,+ (v) => lands[v].title,+ (v) => chip("coins", `${lands[v].coins} coins to start`),+ (v) => lands[v].blurb,+ () => choice.land,+ (v) => (choice.land = v)+ )+ ]+ // The land is drawn from a seed: a fresh one on a click; the number itself only on hover, for whoever wants it.+ const reroll = el("button", "frame reroll", "↻ other land")+ reroll.onclick = () => {+ choice.seed = (Math.random() * 0x7fffffff) >>> 0+ refresh()+ }+ mapBox.append(reroll)+ const refresh = () => {+ for (const r of refreshers) r()+ const land = drawLand(map)+ reroll.title = `Draw other land (this is seed ${choice.seed})`+ const rocks = land.rocks.filter((r) => !r.ore).length+ setHtml(cells, `${icon("pave", 13)}${land.cells} cells`)+ setHtml(facts, chip("wood", `${land.trees.length} trees`) + chip("stone", `${rocks} stone`) + chip("ore", `${land.rocks.length - rocks} ore`))+ }+ refresh()+ const actions = el("div", "actions")+ const found = el("button", "frame primary", "Start new")+ found.onclick = () => {+ founded()+ send({ type: "newTown", charter: { ...choice } })+ history.clear()+ ui.selected = null+ ui.menu = null+ closeCharter()+ controls.showReckoning(false)+ controls.choose("select")+ controls.setSpeed(1)+ }+ actions.append(found)+ // On the way in, a town saved in this browser: carry on with it instead.+ if (!cancellable && savedTown) {+ const saved = savedTown+ const resume = el("button", "frame", "Continue saved")+ resume.title = `Carry on with the town saved in this browser: ${saved.title}${saved.savedAt > 0 ? `, saved ${ago(saved.savedAt)}` : ""}`+ resume.onclick = () => {+ const said = load() ?? "Loaded"+ controls.toast(said)+ if (!said.startsWith("Loaded")) return+ history.clear()+ ui.selected = null+ ui.menu = null+ closeCharter()+ controls.choose("select")+ controls.setSpeed(1)+ }+ actions.append(resume)+ }+ if (cancellable) {+ const cancel = el("button", "frame", "Cancel")+ cancel.onclick = () => closeCharter()+ actions.append(cancel)+ }+ card.append(body, actions)+ dialog.append(card)+ dialog.classList.add("open")+ }+ const closeCharter = () => {+ ui.founding = false+ dialog.classList.remove("open")+ }+ return { element: dialog, open: openCharter, close: closeCharter }+}diff --git a/src/ui/dom/screens/confirm.ts b/src/ui/dom/screens/confirm.tsnew file mode 100644index 0000000..2108cde--- /dev/null+++ b/src/ui/dom/screens/confirm.ts@@ -0,0 +1,49 @@+/** Asks before something that replaces the town: save first, go ahead, or stay. The town waits while it asks. */+import type { Controls } from "../controls.ts"+import { el } from "../kit/dom.ts"++export const createConfirm = (+ save: () => string | null,+ speed: () => number,+ controls: Pick<Controls, "setSpeed" | "toast">+) => {+ const box = el("div", "dialog")+ const ask = (title: string, text: string, go: string, run: () => void) => {+ const wasSpeed = speed()+ controls.setSpeed(0)+ box.replaceChildren()+ const card = el("div", "sheet narrow")+ card.append(el("h2", "", title), el("p", "sub", text))+ const actions = el("div", "actions")+ const close = (resume: boolean) => {+ box.classList.remove("open")+ if (resume) controls.setSpeed(wasSpeed)+ }+ const stay = el("button", "frame primary", "Keep playing")+ stay.onclick = () => close(true)+ const saveFirst = el("button", "frame", `Save, then ${go.toLowerCase()}`)+ saveFirst.onclick = () => {+ const failed = save()+ if (failed) return controls.toast(failed)+ close(true)+ run()+ }+ const leave = el("button", "frame danger", `${go} without saving`)+ leave.onclick = () => {+ close(true)+ run()+ }+ actions.append(stay, saveFirst, leave)+ card.append(actions)+ box.append(card)+ box.classList.add("open")+ stay.focus()+ }+ return {+ element: box,+ ask,+ open: () => box.classList.contains("open"),+ /** Esc: keep playing. */+ dismiss: () => box.querySelector<HTMLButtonElement>(".primary")?.click()+ }+}diff --git a/src/ui/dom/screens/reckoning.ts b/src/ui/dom/screens/reckoning.tsnew file mode 100644index 0000000..a3bb3c2--- /dev/null+++ b/src/ui/dom/screens/reckoning.ts@@ -0,0 +1,121 @@+/**+ * The end of the term: the reckoning. Its verdict, the term in numbers, a+ * chart of residents, coins, and approval over it, and its chronicle; then+ * another term, a new town, or a last look.+ */+import { sizes } from "../../../game/content/index.ts"+import type { Edit } from "../../../game/edits.ts"+import type { Interaction, Stats } from "../../../view/contracts.ts"+import type { Controls } from "../controls.ts"+import { el } from "../kit/dom.ts"+import { clockText, signed } from "../kit/text.ts"+import { verdictOf } from "./share-card.ts"++export const createReckoning = (ui: Interaction, stats: () => Stats | null, send: (edit: Edit) => void, controls: Pick<Controls, "openCharter" | "openShare">) => {+ const reckoning = el("div", "dialog")+ const result = el("button", "result", "")+ result.onclick = () => showReckoning(true)+ /** Lines of residents, coins, and approval over the term, on the term's own time axis. */+ const chart = (s: Stats): string => {+ const w = 520+ const h = 150+ const from = s.fate.began * 3600+ const span = s.charter.term * 3600+ const history = s.history.filter((p) => p.tick >= from)+ const line = (values: ReadonlyArray<number>, lo: number, hi: number, color: string, dash = "") => {+ if (values.length < 2) return ""+ const range = hi - lo || 1+ const points = history+ .map((p, i) => `${(((p.tick - from) / span) * w).toFixed(1)},${(h - 4 - ((values[i]! - lo) / range) * (h - 8)).toFixed(1)}`)+ .join(" ")+ return `<polyline points="${points}" fill="none" stroke="${color}" stroke-width="1.5" stroke-dasharray="${dash}" vector-effect="non-scaling-stroke"/>`+ }+ const residents = history.map((p) => p.residents)+ const coinsLine = history.map((p) => p.coins)+ const approvalLine = history.map((p) => p.approval)+ const target = s.goals.find((g) => g.id === "residents")?.target ?? sizes[s.charter.size].goal.residents+ const top = Math.max(1, ...residents, target)+ const goal = h - 4 - (target / top) * (h - 8)+ // The first term spares its first year a winter; later terms do not.+ const winters = Array.from({ length: 6 }, (_, y) => {+ if (y === 0 && s.fate.term === 1) return ""+ const at = ((y + 0.7) / 6) * w+ return `<rect x="${at.toFixed(1)}" y="0" width="${((w * 0.3) / 6).toFixed(1)}" height="${h}" fill="#3d6db5" opacity="0.06"/>`+ }).join("")+ return `<svg viewBox="0 0 ${w} ${h}" preserveAspectRatio="none">${winters}<line x1="0" x2="${w}" y1="${goal}" y2="${goal}" stroke="#77817a" stroke-dasharray="3 3" vector-effect="non-scaling-stroke"/>${line(coinsLine, Math.min(0, ...coinsLine), Math.max(1, ...coinsLine), "#d6ae2e")}${line(approvalLine, -100, 100, "#3d6db5", "4 3")}${line(residents, 0, top, "#3d4943")}</svg>`+ }+ let reckoned = ""+ let reckoningOpen = false+ const showReckoning = (open: boolean) => {+ reckoningOpen = open+ const s = stats()+ reckoning.classList.toggle("open", open && s !== null && s.fate.state !== "playing")+ }+ const syncReckoning = (s: Stats) => {+ const over = s.fate.state !== "playing"+ result.classList.toggle("show", over && !reckoningOpen && !ui.founding)+ if (!over) {+ reckoned = ""+ reckoning.classList.remove("open")+ return+ }+ const id = `${s.charter.seed}|${s.fate.state}|${s.fate.at}`+ result.textContent = s.fate.state === "lost" ? "the term is lost · see how" : `term ${s.fate.term} is over · see how`+ if (id === reckoned) return+ reckoned = id+ const title = s.fate.state === "lost" ? s.fate.reason.split(":")[0]!.toLowerCase() : `term ${s.fate.term} is over: ${verdictOf(s)}`+ reckoning.replaceChildren()+ const card = el("div", "sheet wide")+ card.append(el("h2", "", title), el("p", "sub", s.fate.reason))+ const facts = el("div", "facts")+ const fact = (label: string, value: string) => {+ const f = el("div", "fact")+ f.append(el("b", "", value), el("span", "", label))+ facts.append(f)+ }+ fact("rank", s.fate.rankTitle)+ fact("minutes in office", `${Math.round(s.fate.at)}`)+ fact("residents", `${s.settlers + s.citizens}`)+ fact("citizens · burghers", `${s.citizens} · ${s.burghers}`)+ fact("coins", `${s.coins}`)+ fact("approval", signed(s.mood.approval))+ fact("crown met · missed", `${s.crown.met} · ${s.crown.missed}`)+ fact("score", `${s.fate.score}`)+ card.append(facts)+ const graph = el("div", "chart")+ graph.innerHTML = chart(s)+ card.append(graph, el("p", "legend", "— residents (dashed: the next rank) · coins · - - approval · shaded: winters"))+ const story = el("div", "chronicle")+ story.innerHTML = s.chronicle+ .filter((l) => l.tick >= s.fate.began * 3600)+ // Timed from the start of this term, as its chart is.+ .map((l) => `<p><b>${clockText(Math.round((l.tick - s.fate.began * 3600) / 60))}</b> ${l.text}</p>`)+ .join("")+ card.append(story)+ const actions = el("div", "actions")+ if (s.fate.state === "over") {+ const stand = el("button", "frame primary", "Next term")+ stand.title = `Stand for another ${s.charter.term} minutes: the same town, new winters, and the crown's requests start small again`+ stand.onclick = () => {+ send({ type: "nextTerm" })+ showReckoning(false)+ }+ actions.append(stand)+ }+ const again = el("button", "frame" + (s.fate.state === "over" ? "" : " primary"), s.fate.state === "over" ? "Retire: found a new town" : "Found a new town")+ again.onclick = () => {+ showReckoning(false)+ controls.openCharter(true)+ }+ const look = el("button", "frame", "Look at the town")+ look.onclick = () => showReckoning(false)+ const share = el("button", "frame", "Share the town")+ share.title = "A picture of the town and its verdict, or a link anyone can open"+ share.onclick = () => controls.openShare()+ actions.append(again, look, share)+ card.append(actions)+ reckoning.append(card)+ showReckoning(true)+ }+ return { element: reckoning, result, show: showReckoning, sync: syncReckoning, isOpen: () => reckoning.classList.contains("open") }+}diff --git a/src/platform/share/card.ts b/src/ui/dom/screens/share-card.tssimilarity index 96%rename from src/platform/share/card.tsrename to src/ui/dom/screens/share-card.tsindex a480172..d7af312 100644--- a/src/platform/share/card.ts+++ b/src/ui/dom/screens/share-card.ts@@ -3,10 +3,10 @@* strip of paper along the bottom saying what it is and how it stands (its* verdict once the term is over), drawn in the page's ink and type.*/-import { lands, sizes } from "../../game/content/index.ts"-import { icon, type IconName } from "../../ui/dom/kit/icons.ts"-import type { Stats } from "../../view/contracts.ts"-import { PICTURE_HEIGHT, PICTURE_WIDTH, type ShareMeta } from "./format.ts"+import { lands, sizes } from "../../../game/content/index.ts"+import { icon, type IconName } from "../kit/icons.ts"+import type { Stats } from "../../../view/contracts.ts"+import { PICTURE_HEIGHT, PICTURE_WIDTH, type ShareMeta } from "../../../platform/share/format.ts"const INK = "#3d4943",MUTED = "#77817a",diff --git a/src/ui/dom/screens/share.ts b/src/ui/dom/screens/share.tsnew file mode 100644index 0000000..fd979a7--- /dev/null+++ b/src/ui/dom/screens/share.ts@@ -0,0 +1,106 @@+/**+ * Sharing: a picture of the town (the card), to post anywhere; and, on+ * asking, a link that opens the town itself, running, for anyone to watch and+ * play on. The town waits while the sheet is open.+ */+import { handheld, offerLink, offerPicture, upload } from "../../../platform/share/client.ts"+import { PICTURE_HEIGHT, PICTURE_QUALITY, PICTURE_WIDTH } from "../../../platform/share/format.ts"+import type { Stats } from "../../../view/contracts.ts"+import type { Controls } from "../controls.ts"+import { el } from "../kit/dom.ts"+import { iconEl } from "../kit/icons.ts"+import { blobOf, cardFacts, drawCard, metaOf, scaled } from "./share-card.ts"++/** What sharing needs of the map: a picture of it as the camera sees it, and where the camera stands. */+export interface ShareMap<Camera> {+ readonly picture: (width: number, height: number, scale: number) => HTMLCanvasElement+ readonly camera: () => Camera+}++export const createShare = <Camera>(+ stats: () => Stats | null,+ speed: () => number,+ visiting: () => boolean,+ map: ShareMap<Camera>,+ shareFile: (camera: Camera) => string,+ controls: Pick<Controls, "setSpeed" | "toast">+) => {+ const shareBox = el("div", "dialog share")+ let shareSpeed = 1+ const closeShare = () => {+ if (!shareBox.classList.contains("open")) return+ shareBox.classList.remove("open")+ for (const img of shareBox.querySelectorAll("img")) URL.revokeObjectURL(img.src)+ if (!visiting()) controls.setSpeed(shareSpeed)+ }+ shareBox.onclick = (event) => {+ if (event.target === shareBox) closeShare()+ }+ const openShare = async () => {+ const s = stats()+ if (!s || shareBox.classList.contains("open")) return+ shareSpeed = speed()+ controls.setSpeed(0)+ const facts = cardFacts(s)+ let card: HTMLCanvasElement, png: Blob+ try {+ card = await drawCard(map.picture(PICTURE_WIDTH, PICTURE_HEIGHT, 2), facts, location.host)+ png = await blobOf(card, "image/png")+ } catch (error) {+ controls.setSpeed(shareSpeed)+ return controls.toast(error instanceof Error ? error.message : "The picture could not be made")+ }+ const name = `inkline-town-${s.fate.state === "playing" ? `year-${s.season.year}` : `term-${s.fate.term}`}.png`+ const caption = `${metaOf(facts).title} · ${metaOf(facts).description}`+ shareBox.replaceChildren()+ const sheet = el("div", "sheet")+ sheet.append(el("h2", "", "share the town"))+ const preview = document.createElement("img")+ preview.className = "card-preview"+ preview.alt = caption+ preview.src = URL.createObjectURL(png)+ sheet.append(preview)+ sheet.append(el("p", "sub", "Post the picture anywhere, or make a link: whoever opens it sees this town running, from where you look at it now, and can play on from here."))+ const actions = el("div", "actions")+ const picture = el("button", "frame primary", "")+ picture.append(iconEl("picture", 15), document.createTextNode(handheld() ? "Share the picture" : "Save the picture"))+ picture.onclick = async () => controls.toast(await offerPicture(png, name, caption))+ const link = el("button", "frame", "")+ link.append(iconEl("link", 15), document.createTextNode("Make a link"))+ const linkRow = el("div", "link-row")+ link.onclick = async () => {+ link.disabled = true+ link.lastChild!.textContent = "Making a link…"+ try {+ const small = await blobOf(scaled(card, PICTURE_WIDTH, PICTURE_HEIGHT), "image/jpeg", PICTURE_QUALITY)+ const made = await upload(shareFile(map.camera()), small, metaOf(facts))+ link.remove()+ const field = document.createElement("input")+ field.readOnly = true+ field.value = made.url+ field.onfocus = () => field.select()+ const copy = el("button", "frame primary", handheld() ? "Share the link" : "Copy the link")+ copy.onclick = async () => controls.toast(await offerLink(made.url, metaOf(facts).title))+ const open = el("a", "frame", "Open")+ open.setAttribute("href", made.url)+ open.setAttribute("target", "_blank")+ open.setAttribute("rel", "noopener")+ linkRow.replaceChildren(field, copy, open)+ linkRow.classList.add("show")+ copy.focus()+ } catch (error) {+ link.disabled = false+ link.lastChild!.textContent = "Try the link again"+ controls.toast(error instanceof Error ? error.message : "Could not share the town")+ }+ }+ const close = el("button", "frame", "Close")+ close.onclick = () => closeShare()+ actions.append(picture, link, close)+ sheet.append(actions, linkRow)+ shareBox.append(sheet)+ shareBox.classList.add("open")+ picture.focus()+ }+ return { element: shareBox, open: openShare, close: closeShare, isOpen: () => shareBox.classList.contains("open") }+}diff --git a/src/ui/dom/screens/visit.ts b/src/ui/dom/screens/visit.tsnew file mode 100644index 0000000..15a64d6--- /dev/null+++ b/src/ui/dom/screens/visit.ts@@ -0,0 +1,54 @@+/**+ * Visiting a shared town: it runs as its mayor left it, with the building+ * tools put away, until the visitor plays on from here (it becomes theirs,+ * and autosaves) or founds a town of their own.+ */+import type { Controls } from "../controls.ts"+import { el } from "../kit/dom.ts"+import { iconEl } from "../kit/icons.ts"++export const createVisit = (+ controls: Pick<Controls, "toast" | "openCharter" | "setSpeed"> & { readonly closeCharter: () => void; readonly resetPage: () => void }+) => {+ const visitBar = el("div", "card visit")+ let visiting = false+ const endVisit = () => {+ visiting = false+ document.body.classList.remove("visiting")+ visitBar.classList.remove("show")+ window.history.replaceState(null, "", "/")+ }+ /** The shared town is loaded: show it running. */+ const visit = (title: string) => {+ visiting = true+ controls.closeCharter()+ controls.resetPage()+ document.body.classList.add("visiting")+ visitBar.replaceChildren()+ const text = el("div", "cell")+ text.append(iconEl("town", 18))+ const words = el("span", "stack")+ words.append(el("b", "", "a shared town"), el("small", "", title))+ text.append(words)+ const play = el("button", "frame primary", "Play from here")+ play.title = "Make this town yours: build, govern, and it autosaves in this browser"+ play.onclick = () => {+ endVisit()+ controls.toast("The town is yours: it autosaves as you play")+ }+ const own = el("button", "frame", "Found your own")+ own.onclick = () => controls.openCharter(true)+ const buttons = el("div", "cell")+ buttons.append(play, own)+ visitBar.append(text, buttons)+ visitBar.classList.add("show")+ controls.setSpeed(1)+ }+ /** The shared town could not be opened: say why, and found a town instead. */+ const visitFailed = (message: string) => {+ window.history.replaceState(null, "", "/")+ controls.openCharter(false)+ controls.toast(message)+ }+ return { element: visitBar, visit, visitFailed, endVisit, visiting: () => visiting }+}diff --git a/src/ui/dom/system-menu.ts b/src/ui/dom/system-menu.tsnew file mode 100644index 0000000..28cbb8d--- /dev/null+++ b/src/ui/dom/system-menu.ts@@ -0,0 +1,59 @@+/**+ * Share, the guide, save, load, and new town: a row of icons in the bottom+ * right (folded into one menu on a phone). Founding or loading replaces the+ * town, so each asks first.+ */+import type { Interaction, Stats } from "../../view/contracts.ts"+import type { IconName } from "../../view/icons.ts"+import type { Controls, Fold } from "./controls.ts"+import { el } from "./kit/dom.ts"+import { iconEl } from "./kit/icons.ts"++export const createSystemMenu = (+ ui: Interaction,+ persistence: () => { readonly save: () => string | null; readonly load: () => string | null },+ resetCamera: () => void,+ fold: () => Fold | null,+ controls: Pick<Controls, "toast" | "confirmLeave" | "openCharter" | "openShare" | "setFold">+) => {+ const system = el("div", "system")+ const action = (name: IconName, label: string, tip: string, run: () => void) => {+ const button = el("button", name === "share" ? "labelled" : "")+ button.title = tip+ button.append(iconEl(name), document.createTextNode(label))+ button.onclick = run+ system.append(button)+ }+ action("share", "share", "Share a picture of the town, or a link anyone can open to watch it and play on", () => controls.openShare())+ // The guide, on or off (it is on for a new mayor).+ const guideButton = el("button", "guide-toggle")+ guideButton.title = "Turn the guide on or off: it tells a new mayor what to do next, and why"+ guideButton.append(iconEl("hint"), document.createTextNode("guide"))+ let guideOff = false+ guideButton.onclick = () => ui.guide.push({ type: guideOff ? "on" : "off" })+ system.append(guideButton)+ action("save", "save", "Save the town in this browser (it also autosaves every two minutes)", () => controls.toast(persistence().save() ?? "Saved"))+ action("load", "load", "Load the latest save: yours or the autosave, whichever is newer", () =>+ controls.confirmLeave("load the latest save?", "The town as it is now is replaced by the latest save (yours or the autosave).", "Load", () =>+ controls.toast(persistence().load() ?? "Loaded")+ )+ )+ action("newTown", "new town", "Found a new town on new land", () =>+ controls.confirmLeave("found a new town?", "This town is left behind for good unless it is saved first.", "Choose new land", () => controls.openCharter(true))+ )+ // A phone has no camera bar: this puts the camera back.+ action("view", "recentre", "Back to the original angle and centre", resetCamera)+ system.lastElementChild!.classList.add("phone-only")+ // A choice from the menu folds it.+ system.addEventListener("click", () => {+ if (fold() === "more") controls.setFold(null)+ })+ return {+ element: system,+ update: (s: Stats) => {+ guideOff = s.guide.off+ guideButton.classList.toggle("on", !s.guide.off)+ guideButton.title = s.guide.off ? "Turn the guide back on: a next step to take, and why, at the top of the ticker" : "Turn the guide off"+ }+ }+}diff --git a/src/ui/dom/ticker.ts b/src/ui/dom/ticker.tsnew file mode 100644index 0000000..19cfdbe--- /dev/null+++ b/src/ui/dom/ticker.ts@@ -0,0 +1,174 @@+/**+ * The town ticker: the guide's step first (what to do next, and why), then+ * what needs looking at now, then the news of the last minute and a half.+ * A line goes where it is about on a click; the buildings and tools it names+ * are chips. On a phone the guide's step stays out above the tool bar until+ * tucked into the bulb, which pulses and shows how many things need looking+ * at. Each new alarm in the chronicle is also a toast.+ */+import { kinds, type KindId } from "../../game/content/index.ts"+import type { Interaction, Stats } from "../../view/contracts.ts"+import type { IconName } from "../../view/icons.ts"+import type { Controls, Fold } from "./controls.ts"+import { phone } from "./folds.ts"+import { el, escapeHtml } from "./kit/dom.ts"+import { icon, iconEl } from "./kit/icons.ts"+import { callouts } from "./kit/text.ts"++export const createTicker = (ui: Interaction, fold: () => Fold | null, controls: Pick<Controls, "useRef" | "toast" | "setFold" | "goTo" | "place">) => {+ const ticker = el("div", "card ticker")+ const bulb = el("button", "card")+ bulb.title = "The guide, and what needs looking at now"+ const badge = el("span", "badge")+ bulb.append(iconEl("hint", 16), badge)+ bulb.onclick = () => controls.setFold(fold() === "guide" ? null : "guide")++ /** Ticker lines the mayor has put away, by what they say (and, for news, when). */+ const dismissed = new Set<string>()+ let lines: { guide: Stats["guide"]["step"]; alerts: Stats["alerts"]; news: Stats["chronicle"] } = { guide: null, alerts: [], news: [] }+ /** The last chronicle line announced. */+ let lastNews = ""+ /**+ * A phone's guide: the step stays above the tool bar until the mayor tucks it away (`tucked`: the step and+ * state they tucked; a new step, or the same one under way, comes out again). Tucked away, the bulb pulses,+ * and the first time a toast says where the guide went.+ */+ let stepNow = ""+ let tucked = ""+ let toldWhere = false++ ticker.onclick = (event) => {+ event.stopPropagation()+ const target = event.target as HTMLElement+ // A tool, panel, or map view the line names: pick it, with the map in view.+ const tool = target.closest<HTMLElement>("[data-tool]")+ if (tool) return controls.useRef(tool.dataset.tool!)+ // On a phone: tuck the step away into the bulb, or open the guide in full.+ if (target.closest("[data-tuck]")) {+ tucked = stepNow+ if (!toldWhere) controls.toast("The guide is in the bulb at the top right")+ toldWhere = true+ return+ }+ if (ticker.classList.contains("peek") && !target.closest("[data-kind]")) return controls.setFold("guide")+ const off = target.closest<HTMLElement>("[data-dismiss]")+ if (off) {+ dismissed.add(off.dataset.dismiss!)+ return+ }+ // The guide: put this step away, or turn the guide off (the bottom-right button turns it back on).+ const skip = target.closest<HTMLElement>("[data-skip]")+ if (skip) {+ ui.guide.push({ type: "skip", id: skip.dataset.skip! })+ return+ }+ if (target.closest("[data-guide-off]")) {+ ui.guide.push({ type: "off" })+ controls.toast("Guide off · turn it back on from the bottom right")+ return+ }+ // A building the line is about: go to it.+ const go = target.closest<HTMLElement>("[data-go]")+ if (go) {+ const row = go.closest<HTMLElement>("[data-alert]")+ const there = row ? lines.alerts[Number(row.dataset.alert)]?.places?.[go.dataset.go as KindId] : undefined+ if (there) controls.goTo(there.at, there.size, there.id)+ // The guide's step names the building it points at.+ const guide = lines.guide+ if (!row && go.closest("[data-guide]") && guide?.at) controls.goTo(guide.at, guide.size, guide.id)+ return+ }+ const ref = target.closest<HTMLElement>("[data-kind]")+ if (ref) {+ const kind = ref.dataset.kind as KindId+ // Straight to placing it, with the map in view: the placement itself says if the town cannot build it yet.+ if (kinds[kind].buildable) {+ controls.setFold(null)+ controls.place(kind)+ }+ return+ }+ const line = target.closest<HTMLElement>(".cell.place")+ if (!line) return+ if (line.dataset.guide !== undefined && lines.guide?.at) return controls.goTo(lines.guide.at, lines.guide.size, lines.guide.id)+ const alert = line.dataset.alert !== undefined ? lines.alerts[Number(line.dataset.alert)] : undefined+ if (alert?.at) return controls.goTo(alert.at, alert.size, alert.id)+ const news = line.dataset.news !== undefined ? lines.news[Number(line.dataset.news)] : undefined+ if (news?.at) controls.goTo(news.at, 1, null)+ }++ const update = (s: Stats) => {+ const minute = Math.round(s.minute * 3600)+ const news = s.chronicle.filter((l) => l.alarm && minute - l.tick < 90 * 60).slice(-3).reverse()+ const ago = (tick: number) => {+ const sec = Math.max(0, Math.round((minute - tick) / 60))+ return sec < 5 ? "just now" : sec < 60 ? `${sec} s ago` : `${Math.round(sec / 60)} min ago`+ }+ const alerts = s.alerts.filter((a) => !dismissed.has(`a:${a.text}`))+ // The crown's request has its own card while it is open, and is settled after: its news line would only repeat it.+ const fresh = news.filter((l) => !dismissed.has(`n:${l.tick}:${l.text}`) && !l.text.startsWith("The crown asks"))+ const toneIcon = { hint: "hint", warn: "caution", bad: "warning" } as const+ const row = (kind: string, tone: string, glyph: IconName, text: string, place: boolean, key: string, data: string) =>+ `<div class="cell ${kind} ${tone}${place ? " place" : ""}" ${data}>${icon(glyph, 16, "mark")}<span class="text">${text}</span><span class="acts">${place ? `<button class="act locate" title="Show me where">${icon("locate", 14)}</button>` : ""}<button class="act dismiss" data-dismiss="${escapeHtml(key)}" title="Dismiss">${icon("close", 12)}</button></span></div>`+ // The guide's step first: what to do next, and why.+ const g = s.guide.step+ const guideHtml = g+ ? (() => {+ const asks = new Set(g.asks)+ const say = (text: string) => callouts(text, new Set(g.near ? [g.near] : []), asks)+ const count = g.count ? `step ${g.count.at} of ${g.count.of}` : ""+ return [+ `<div class="head guide"><span>guide · ${escapeHtml(g.chapter)}</span><span class="progress" title="${g.count ? "The first steps of a new town" : ""}">${count}</span><button class="quiet" data-guide-off title="Turn the guide off (the guide button in the bottom right turns it back on)">hide guide</button></div>`,+ `<div class="cell alert hint guide${g.waiting ? " waiting" : g.locked ? " locked" : ""}${g.at !== null ? " place" : ""}" data-guide>${icon(g.waiting ? "clock" : g.locked ? "caution" : "hint", 16, "mark")}<span class="text">` ++ `<span class="do">${say(g.text)}</span>` ++ (g.waiting ? `<span class="why now">${say(g.waiting)}</span>` : g.locked ? `<span class="why now">${say(g.locked)}</span>` : `<span class="why">${say(g.why)}</span>`) ++ `<span class="goal">${icon("goal", 12)}<span>done when ${say(g.goal)}</span></span>` ++ (g.next ? `<span class="goal next">${icon("clock", 12)}<span>${say(g.next)}</span></span>` : "") ++ `</span>` ++ `<span class="acts">${g.at !== null ? `<button class="act locate" title="Show me where">${icon("locate", 14)}</button>` : ""}<button class="act dismiss" data-skip="${escapeHtml(g.key)}" title="Skip this step">${icon("close", 12)}</button></span>` ++ `<button class="act tuck" data-tuck title="Hide: the guide stays in the bulb at the top right">${icon("close", 12)}</button></div>`+ ]+ })()+ : []+ const tickerHtml = [+ ...guideHtml,+ ...(alerts.length > 0 ? [`<div class="head">now</div>`] : []),+ ...alerts.map((a) => row("alert", a.tone, toneIcon[a.tone], callouts(a.text, new Set(Object.keys(a.places ?? {}) as Array<KindId>)), a.at !== null, `a:${a.text}`, `data-alert="${s.alerts.indexOf(a)}"`)),+ ...(fresh.length > 0 ? [`<div class="head">lately</div>`] : []),+ ...fresh.map((l) => row("news", "", "chronicle", `${callouts(l.text)} <em data-tick="${l.tick}"></em>`, l.at !== null, `n:${l.tick}:${l.text}`, `data-news="${news.indexOf(l)}"`))+ ].join("")+ if (ticker.dataset.html !== tickerHtml) {+ ticker.dataset.html = tickerHtml+ ticker.innerHTML = tickerHtml+ }+ for (const em of ticker.querySelectorAll<HTMLElement>("em[data-tick]")) {+ const text = ago(Number(em.dataset.tick))+ if (em.textContent !== text) em.textContent = text+ }+ lines = { guide: g, alerts: s.alerts, news }+ ticker.classList.toggle("show", tickerHtml !== "" && s.fate.state === "playing")+ // The phone's guide button: the step it is on, or how many things need looking at.+ const bad = alerts.some((a) => a.tone === "bad")+ const things = alerts.length + (g ? 1 : 0)+ const said = g?.count ? `${g.count.at}/${g.count.of}` : things > 0 ? String(things) : ""+ if (badge.textContent !== said) badge.textContent = said+ bulb.classList.toggle("alert", bad)+ // On a phone the step stays out above the tool bar, until tucked away; not behind the town picker or an open sheet.+ const stepKey = g ? `${g.key}${g.waiting ? ":under way" : g.locked ? ":locked" : ""}` : ""+ stepNow = stepKey+ const peeking = stepKey !== "" && phone.matches && fold() === null && tucked !== stepKey && !ui.founding && s.fate.state === "playing"+ if (ticker.classList.contains("peek") !== peeking) ticker.classList.toggle("peek", peeking)+ document.body.classList.toggle("peeking", peeking)+ const nudge = stepKey !== "" && phone.matches && tucked === stepKey && fold() !== "guide"+ if (bulb.classList.contains("nudge") !== nudge) bulb.classList.toggle("nudge", nudge)+ // News as it happens: a toast for each new alarm line.+ const latest = s.chronicle.at(-1)+ const seen = `${s.charter.seed}|${latest?.tick ?? -1}|${s.chronicle.length}`+ if (seen !== lastNews) {+ if (lastNews !== "" && latest?.alarm && minute - latest.tick < 120) controls.toast(latest.text)+ lastNews = seen+ }+ }++ return { element: ticker, bulb, update }+}diff --git a/src/ui/dom/toast.ts b/src/ui/dom/toast.tsnew file mode 100644index 0000000..4a02ee0--- /dev/null+++ b/src/ui/dom/toast.ts@@ -0,0 +1,30 @@+/**+ * The toast: a message for a few seconds, long enough to read (about 15+ * letters a second, and a moment to look), and gone as soon as the mayor taps+ * the map again: by then they have read it, or are acting on it.+ */+import { el, escapeHtml } from "./kit/dom.ts"+import { iconize } from "./kit/text.ts"++export const createToast = () => {+ const box = el("div", "toast")+ let timer = 0+ /** When the toast showing went up: a press on the map just after does not put it away. */+ let at = 0+ return {+ element: box,+ show: (message: string) => {+ box.innerHTML = iconize(escapeHtml(message))+ box.classList.add("show")+ at = Date.now()+ window.clearTimeout(timer)+ timer = window.setTimeout(() => box.classList.remove("show"), Math.min(8000, Math.max(2200, 1200 + message.length * 65)))+ },+ /** A tap on the map puts a toast away (one it raised itself stays). */+ tapped: () => {+ if (!box.classList.contains("show") || Date.now() - at <= 250) return+ window.clearTimeout(timer)+ box.classList.remove("show")+ }+ }+}diff --git a/src/ui/dom/toolbar.ts b/src/ui/dom/toolbar.tsnew file mode 100644index 0000000..631d2a5--- /dev/null+++ b/src/ui/dom/toolbar.ts@@ -0,0 +1,83 @@+/**+ * The tool bar, built from the tool table (`view/tools.ts`): the town's+ * panel first and larger, then the tools, the clock, and (on a phone) the+ * menu the bottom-right buttons fold into.+ */+import type { Interaction } from "../../view/contracts.ts"+import { commands, toolbar as tools, type Command } from "../../view/tools.ts"+import type { Controls } from "./controls.ts"+import { el } from "./kit/dom.ts"+import { iconEl } from "./kit/icons.ts"++export const createToolbar = (ui: Interaction, controls: Pick<Controls, "toggleMenu" | "choose" | "setSpeed" | "togglePause" | "cycleSpeed" | "toggleFold">) => {+ const bar = el("div", "toolbar")+ // The town's panel is opened most: it has a button of its own, first and larger.+ const townButton = el("button", "town-button")+ townButton.title = "The town's goals, numbers, taxes, and trade (T)"+ townButton.append(iconEl("town", 22), el("span", "", "town"), el("kbd", "", "T"))+ townButton.onclick = () => controls.toggleMenu("town")+ // Everything the bottom-right buttons do, folded into one menu on a phone.+ const moreButton = el("button", "more")+ moreButton.title = "Share, the guide, save, load, new town"+ moreButton.append(iconEl("more"))+ moreButton.onclick = () => controls.toggleFold("more")+ const toolGroup = el("div", "group")+ bar.append(townButton, toolGroup)+ const buttons = tools.map(({ id, tip }) => {+ const command: Command = commands[id]+ const button = el("button", "")+ button.title = tip+ button.append(iconEl(command.icon), document.createTextNode(command.label), el("kbd", "", command.key ?? ""))+ const action = command.action+ button.onclick = () => ("menu" in action ? controls.toggleMenu(action.menu) : "tool" in action ? controls.choose(action.tool) : undefined)+ toolGroup.append(button)+ return { action, button }+ })+ const speedGroup = el("div", "group speed")+ bar.append(speedGroup)+ const speeds = [+ { speed: 0, label: "", key: "space" },+ { speed: 1, label: "1×", key: "1" },+ { speed: 2, label: "2×", key: "2" },+ { speed: 4, label: "4×", key: "3" }+ ].map(({ speed, label, key }) => {+ const button = el("button", speed === 0 ? "pause" : "", label)+ if (speed === 0) button.append(iconEl("pause", 14))+ button.title = speed === 0 ? "Pause the town, or run it again (space)" : `Run the town at ${label} (${key})`+ // Pause is a switch: pressed again it runs the town at its last speed.+ button.onclick = () => (speed === 0 ? controls.togglePause() : controls.setSpeed(speed))+ speedGroup.append(button)+ return { speed, button }+ })+ // A phone has room for one speed button: it runs a paused town, else goes on to the next speed.+ const cycle = el("button", "cycle")+ cycle.title = "Change speed: 1×, 2×, 4×"+ cycle.onclick = () => controls.cycleSpeed()+ speedGroup.append(cycle)+ bar.append(moreButton)++ return {+ element: bar,+ moreButton,+ /** Shows which tool and panel are picked. */+ refresh: () => {+ for (const { action, button } of buttons) {+ const on = "menu" in action+ ? action.menu === "build" && (ui.menu === "build" || ui.tool === "place" || ui.tool === "ground" || ui.tool === "prop" || ui.tool === "upgrade")+ : "tool" in action && action.tool === ui.tool+ button.classList.toggle("on", on)+ }+ townButton.classList.toggle("on", ui.menu === "town")+ },+ /** Shows the speed, and what pause would run again (`last`). */+ showSpeed: (speed: number, last: number) => {+ for (const s of speeds) s.button.classList.toggle("on", s.speed === speed)+ // Paused, the pause button shows play.+ const pause = speeds[0]!.button+ pause.replaceChildren(iconEl(speed === 0 ? "play" : "pause", 14))+ pause.setAttribute("aria-label", speed === 0 ? "Run the town" : "Pause the town")+ cycle.textContent = `${speed === 0 ? last : speed}×`+ cycle.title = speed === 0 ? `Run the town at ${last}×` : "Change speed: 1×, 2×, 4×"+ }+ }+}diff --git a/src/view/tools.ts b/src/view/tools.tsnew file mode 100644index 0000000..06ba925--- /dev/null+++ b/src/view/tools.ts@@ -0,0 +1,99 @@+/**+ * Everything the player can pick, defined once: the map tools, the two+ * panels, the clock, and the map views. Each UI builds its tool bar, binds+ * the keys, and turns a message's "[homes]" or "[view:fire]" into a chip from+ * these tables; adding one here is the whole change.+ */+import { costText, home, PAVE_COST, PAVED_PACE, ROAD_COST } from "../game/content/index.ts"+import type { Tool, View } from "./contracts.ts"+import type { IconName } from "./icons.ts"++/** What picking a command does: picks a map tool, opens a panel, or works the clock. */+export type Action = { readonly tool: Tool } | { readonly menu: "build" | "town" } | { readonly clock: "speed" | "pause" }++export interface Command {+ readonly label: string+ readonly icon: IconName+ /** Its key, as a keyboard shows it ("S", "space"); none for the speed. */+ readonly key?: string+ /** What it does, in a line: a chip's tooltip. */+ readonly title: string+ readonly action: Action+}++export const commands = {+ select: { label: "select", icon: "select", key: "S", title: "Select: pick a building or home to see what it does", action: { tool: "select" } },+ road: { label: "road", icon: "road", key: "R", title: "Road: drag to lay one", action: { tool: "road" } },+ pave: { label: "pave", icon: "pave", key: "P", title: "Pave: drag along a road to pave it", action: { tool: "pave" } },+ homes: { label: "homes", icon: "homes", key: "H", title: "Homes: drag along a road to lay a row", action: { tool: "home" } },+ build: { label: "build", icon: "build", key: "B", title: "Open the build menu", action: { menu: "build" } },+ bulldoze: { label: "bulldoze", icon: "bulldoze", key: "X", title: "Bulldoze: remove what stands on a cell", action: { tool: "bulldoze" } },+ town: { label: "town", icon: "town", key: "T", title: "Open the town panel: goals, taxes, and trade", action: { menu: "town" } },+ speed: { label: "speed", icon: "clock", title: "Run time faster: 1×, 2×, 4×", action: { clock: "speed" } },+ pause: { label: "pause", icon: "pause", key: "space", title: "Pause the town, or run it again", action: { clock: "pause" } }+} as const satisfies Record<string, Command>+export type CommandId = keyof typeof commands++/** The tool bar's tools, in order, with what each says on hover there. */+export const toolbar: ReadonlyArray<{ readonly id: CommandId; readonly tip: string }> = [+ { id: "select", tip: "Click a building or home to see what it does · drag to pan · wheel to zoom" },+ { id: "road", tip: `Drag to lay a road · ${ROAD_COST} coins a cell` },+ {+ id: "pave",+ tip: `Drag along a road to pave it: carriers and walkers go ${PAVED_PACE}× as fast on it, and homes beside it like it · ${PAVE_COST.coins} coins and ${PAVE_COST.stone} stone a cell`+ },+ { id: "homes", tip: `Drag to lay a row of homes along a road · ${costText(home.cost)} each` },+ { id: "build", tip: "Choose a building, then click to place it · Esc to stop" },+ { id: "bulldoze", tip: "Click a building, home, road, tree, or field to remove it" }+]++export interface MapView {+ readonly label: string+ readonly icon: IconName+ readonly key: string+}++/** The map views, in order: the list and the "[view:…]" chips both come from here. */+export const mapViews = {+ appeal: { label: "desirability", icon: "appeal", key: "D" },+ workers: { label: "workers", icon: "workers", key: "W" },+ fire: { label: "fire", icon: "fire", key: "F" },+ health: { label: "health", icon: "health", key: "A" },+ order: { label: "order", icon: "order", key: "O" },+ condition: { label: "condition", icon: "condition", key: "C" }+} as const satisfies Record<Exclude<View, "none">, MapView>+export type MapViewId = keyof typeof mapViews++/** What a map view's chip says: "fire map", and what it colours the map by. */+const viewTitles: Readonly<Record<MapViewId, string>> = {+ appeal: "Colour the map by desirability",+ workers: "Colour the map by workers",+ fire: "Colour the map by fire risk",+ health: "Colour the map by health risk",+ order: "Colour the map by the risk of theft",+ condition: "Colour the map by the condition of buildings"+}++/** A reference a message can name in brackets: a command ("[homes]") or a map view ("[view:fire]"). */+export type Ref = { readonly command: CommandId } | { readonly view: MapViewId }++const isView = (name: string): name is MapViewId => name in mapViews+const isCommand = (name: string): name is CommandId => name in commands++export const refOf = (name: string): Ref | null => {+ if (name.startsWith("view:")) {+ const view = name.slice(5)+ return isView(view) ? { view } : null+ }+ return isCommand(name) ? { command: name } : null+}++/** How a reference shows as a chip: its label, icon, key, and tooltip. */+export const refLook = (ref: Ref): { readonly label: string; readonly icon: IconName; readonly key: string | undefined; readonly title: string } => {+ if ("view" in ref) {+ const v = mapViews[ref.view]+ return { label: `${v.label} map`, icon: v.icon, key: v.key, title: viewTitles[ref.view] }+ }+ const c: Command = commands[ref.command]+ return { label: c.label, icon: c.icon, key: c.key, title: c.title }+}diff --git a/test/pictures.test.ts b/test/pictures.test.tsindex e0b1d29..ab2fc8f 100644--- a/test/pictures.test.ts+++ b/test/pictures.test.ts@@ -5,7 +5,7 @@* no picture is left behind when a model changes.*/import assert from "node:assert/strict"-import { readFileSync } from "node:fs"+import { readdirSync, readFileSync } from "node:fs"import { test } from "node:test"import { groundIds, kindIds, kinds, propIds } from "../src/game/content/index.ts"@@ -35,7 +35,11 @@ test("every piece of park is pictured as the map draws it, a furnishing standing})test("the page pictures nothing any other way", () => {- const host = readFileSync(new URL("../src/ui/dom/host.ts", import.meta.url), "utf8")- assert.match(host, /pictureAsset\(/)- for (const other of ["drawParkPiece", "makeBuilding", "serviceBuilding", "dressedBuilding"]) assert.equal(host.includes(other), false, `host.ts pictures with ${other}`)+ const thumbnails = readFileSync(new URL("../src/render/three/thumbnails.ts", import.meta.url), "utf8")+ assert.match(thumbnails, /pictureAsset\(/)+ const page = new URL("../src/ui/dom/", import.meta.url)+ for (const file of readdirSync(page, { recursive: true, encoding: "utf8" }).filter((f) => f.endsWith(".ts"))) {+ const text = readFileSync(new URL(file, page), "utf8")+ for (const other of ["pictureAsset", "drawParkPiece", "makeBuilding", "serviceBuilding", "dressedBuilding"]) assert.equal(text.includes(other), false, `ui/dom/${file} pictures with ${other}`)+ }})diff --git a/ui-kit.html b/ui-kit.htmlnew file mode 100644index 0000000..9616176--- /dev/null+++ b/ui-kit.html@@ -0,0 +1,15 @@+<!doctype html>+<html lang="en">+ <head>+ <meta charset="UTF-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1" />+ <link rel="icon" href="/favicon.svg" type="image/svg+xml" />+ <title>Inkline Town · UI kit</title>+ <link rel="stylesheet" href="/src/ui/dom/styles/factory.css" />+ <style>body { margin: 0; padding: 24px; background: #e2e9e6; overflow: auto; } #kit { display: flex; justify-content: center; }</style>+ </head>+ <body>+ <main id="kit"></main>+ <script type="module" src="/src/labs/ui-kit/main.ts"></script>+ </body>+</html>diff --git a/vite.config.ts b/vite.config.tsindex f7186d2..96e1bc0 100644--- a/vite.config.ts+++ b/vite.config.ts@@ -21,7 +21,8 @@ export default defineConfig({proportions: "proportion-review.html",effects: "effects-lab.html",parks: "park-lab.html",- services: "service-upgrades.html"+ services: "service-upgrades.html",+ kit: "ui-kit.html"}}}