A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D
Coins counted over the same rolling minute as the goods: a trend by the treasury, its real change under it, and a coins row in the table
SandroMaglionea7c30f4e5e73
diff --git a/docs/how-to-play.md b/docs/how-to-play.mdindex 217aff1..d3da74f 100644--- a/docs/how-to-play.md+++ b/docs/how-to-play.md@@ -218,6 +218,12 @@ with the town, and starts over with a new one.ten seconds. Click the bar for each good's minute side by side: made,used, traded, in storage, and about town (in workshops, markets, baskets,and crates).+- **The coins** at the top are counted the same way: the arrow and the+ figure under them are the treasury's last minute, every coin in and out+ (taxes and refunds made; upkeep, administration, building, and fines used;+ trade). Click them for the same table, with the treasury on its first row.+ The goal of not losing coins goes by the steady rate (taxes, trade, upkeep,+ administration) shown when you hover over them.- **Materials** (planks, stone, tools) come from storage: a new building is asite until carriers have brought what it costs, then it goes up.- **Homes** fill with settlers once a road touches them. Settlers need fooddiff --git a/src/factory.css b/src/factory.cssindex 7b7bfd8..dd66815 100644--- a/src/factory.css+++ b/src/factory.css@@ -36,6 +36,10 @@ body { font-family: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;.hud .stack .of { font-weight: 400; color: #8a938c; font-size: 12px; }.hud .coins b { font-size: 17px; }.hud .coins.debt b { color: #b04a39; }+.hud .coins { cursor: pointer; }+.hud .coins:hover { background: #eef0ea; }+/* Which way the treasury went over the last minute, as each good shows it. */+.hud .coins .trend { margin-left: 4px; font-size: 14px; font-weight: 700; }.hud .up { color: #4f8a3e; }.hud .down { color: #b04a39; }.hud .muted { color: #8a938c; }diff --git a/src/host.ts b/src/host.tsindex 591c15c..53cabb2 100644--- a/src/host.ts+++ b/src/host.ts@@ -296,6 +296,12 @@ export const createHost = async (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.@@ -1801,8 +1807,18 @@ export const createHost = async (syncPanel()const s = ui.statsif (s) {- setHtml(coins, `${icon("coins", 20)}<span class="stack"><b>${s.coins}</b><small class="${s.net >= 0 ? "up" : "down"}">${s.net >= 0 ? "+" : ""}${s.net}/min</small></span>`)- coins.title = "Coins in the treasury, and what it gains (or loses) a minute"+ // 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 * 60))const severity = (w: typeof s.season.coming) => (w === null ? "" : ` (${winters[w].title})`)@@ -1972,12 +1988,13 @@ export const createHost = async (return { app, stage, ui, syncPage, resetPage, input, visit, visitFailed, visiting: () => visiting }}-/** Which way a good went over the last minute: made and bought against used and sold, past a tenth of the traffic either way. */-const flowTrend = (flow: NonNullable<Stats["flow"]>, g: GoodId): number => {- const net = flow.made[g] + flow.traded[g] - flow.used[g]- const room = Math.max(1, Math.round((flow.made[g] + flow.used[g] + Math.abs(flow.traded[g])) / 10))+/** 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 ? "↓" : "→")@@ -1988,10 +2005,15 @@ const flowHtml = (s: Stats): string => {const f = s.flowif (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)- if (rows.length === 0) return `<p class="sub">Nothing made or used yet.</p>`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 body = rows.map((g) => {+ 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>` +diff --git a/src/schema.ts b/src/schema.tsindex d25209e..b690909 100644--- a/src/schema.ts+++ b/src/schema.ts@@ -341,6 +341,17 @@ export interface FlowSlice {readonly start: Amountsreadonly made: Amountsreadonly traded: Amounts+ readonly coins: CoinSlice+}+/**+ * Ten seconds of the treasury, counted as goods are: what it held as the+ * slice began, what came in (taxes, rewards, refunds), and what trade+ * brought (sold less bought). What went out is what is left over.+ */+export interface CoinSlice {+ readonly start: number+ readonly made: number+ readonly traded: number}/*** Where the town's goods go, over the last minute, a slice of `FLOW_SLICE`@@ -359,10 +370,22 @@ export interface FlowValue {readonly made: Amountsreadonly traded: Amountsreadonly held: Amounts+ /**+ * The treasury's slice under way. `seen` is the treasury the last count+ * saw, and `booked` what the collection has already counted since (its+ * taxes and trade): any other change in between is a coin made, or one+ * left over as used.+ */+ readonly coins: CoinSlice & { readonly seen: number; readonly booked: number }/** The last minute's slices, oldest first. */readonly slices: ReadonlyArray<FlowSlice>/** A minute's worth over the slices kept, or `null` before there are enough to say. */- readonly last: { readonly made: Amounts; readonly used: Amounts; readonly traded: Amounts } | null+ readonly last: {+ readonly made: Amounts+ readonly used: Amounts+ readonly traded: Amounts+ readonly coins: { readonly made: number; readonly used: number; readonly traded: number }+ } | null}export const Flow = Descriptor.TransientResource<FlowValue>()("Town/Flow")diff --git a/src/sim/index.ts b/src/sim/index.tsindex aa4fafa..9d7e201 100644--- a/src/sim/index.ts+++ b/src/sim/index.ts@@ -79,7 +79,7 @@ export const blankResources = () => ({Fires: { fires: [], next: 1 },Grid: { owner: [], use: [] },Stock: amounts({}),- Flow: { town: "", since: 0, start: null, made: amounts({}), traded: amounts({}), held: amounts({}), slices: [], last: null }+ Flow: { town: "", since: 0, start: null, made: amounts({}), traded: amounts({}), held: amounts({}), coins: { start: 0, made: 0, traded: 0, seen: 0, booked: 0 }, slices: [], last: null }})export { CELL_STEPS, elapsed, seasonAt, termTicks, winterOf } from "./shared.ts"diff --git a/src/sim/town.ts b/src/sim/town.tsindex 35579cd..787bdeb 100644--- a/src/sim/town.ts+++ b/src/sim/town.ts@@ -130,8 +130,9 @@ export const Collect = Game.System(const admin = adminOf(homes, rules)const stores = queries.stores.each()+ const before = resources.treasury.get().coinsconst traded = tradeCollection(stores.map(({ data }) => data.inventory.get()), policy, resources.market.get().prices,- resources.treasury.get().coins + baseCollection(taxes, upkeep, homes, rules).delta, rules)+ before + baseCollection(taxes, upkeep, homes, rules).delta, rules)// What changed hands, for the minute's flow of goods: bought less sold.const moved: Partial<Record<GoodId, number>> = {}for (const [i, row] of stores.entries()) {@@ -141,6 +142,12 @@ export const Collect = Game.System(row.data.inventory.set(traded.stores[i]!)}if (Object.keys(moved).length > 0) resources.flow.update((f) => ({ ...f, traded: amounts(Object.fromEntries(goodIds.map((g) => [g, f.traded[g] + (moved[g] ?? 0)]))) }))+ // And the treasury's: the taxes came in, trade changed hands; upkeep and administration are left over as used.+ const collected = Math.round(taxes * share)+ resources.flow.update((f) => ({+ ...f,+ coins: { ...f.coins, made: f.coins.made + collected, traded: f.coins.traded + traded.traded, booked: f.coins.booked + traded.coins - before }+ }))resources.market.set({ prices: traded.prices })resources.treasury.set({ coins: traded.coins })resources.ledger.set({ taxes: Math.round(taxes), trade: Math.round(traded.traded / share), upkeep: Math.round(upkeep), admin })diff --git a/src/sim/work.ts b/src/sim/work.tsindex f3f0d5e..4c8b459 100644--- a/src/sim/work.ts+++ b/src/sim/work.ts@@ -16,7 +16,7 @@ const FLOW_SLICES = 6const each = (f: (g: (typeof goodIds)[number]) => number): Amounts => Object.fromEntries(goodIds.map((g) => [g, f(g)])) as Amountsimport { around, doors, GridBuilder, key, ownerAt, reach, roadPrint, useAt } from "../grid.ts"-import { Building, Burning, Charter, Errand, Flow, Roam, Condition, Field, FieldOf, Game, Rules, Gatherer, Grid, House, Inventory, Land, Paused, Paved, Planter, Recipe, Rising, Rng, Rock, Site, Staff, Status, Stock, Storage, Tile, Time, Tree, maybe, type Status as StatusValue } from "../schema.ts"+import { Building, Burning, Charter, Errand, Flow, Roam, Condition, Field, FieldOf, Game, Rules, Gatherer, Grid, House, Inventory, Land, Paused, Paved, Planter, Recipe, Rising, Rng, Rock, Site, Staff, Status, Stock, Storage, Tile, Time, Treasury, Tree, maybe, type Status as StatusValue } from "../schema.ts"import { connected, elapsed, seasonAt, TileRead, useOf } from "./shared.ts"import { assignWorkers, equipmentPace, effort, paced } from "../rules/production.ts"@@ -75,6 +75,7 @@ export const CountStock = Game.System(time: Game.System.readResource(Time),charter: Game.System.readResource(Charter),stock: Game.System.writeResource(Stock),+ treasury: Game.System.readResource(Treasury),flow: Game.System.writeResource(Flow)}},@@ -104,26 +105,39 @@ export const CountStock = Game.System(const town = `${charter.seed}:${charter.founded}`const f = resources.flow.get()const now = held as Amounts+ const treasury = resources.treasury.get().coins+ const fresh = { start: treasury, made: 0, traded: 0, seen: treasury, booked: 0 }// A new or loaded town, or the clock gone back: start counting again from here.if (f.town !== town || tick < f.since || f.start === null) {- resources.flow.set({ town, since: tick, start: now, made: amounts({}), traded: amounts({}), held: now, slices: [], last: null })+ resources.flow.set({ town, since: tick, start: now, made: amounts({}), traded: amounts({}), held: now, coins: fresh, slices: [], last: null })return}+ // Coins that came in other than by the collection (a reward, a refund) were made; any that went out are left over as used.+ const other = treasury - f.coins.seen - f.coins.booked+ const coins = { ...f.coins, made: f.coins.made + Math.max(0, other), seen: treasury, booked: 0 }if (tick - f.since < ticks(FLOW_SLICE)) {- resources.flow.set({ ...f, held: now })+ resources.flow.set({ ...f, held: now, coins })return}// A slice is done: the last minute rolls on by one, and is reckoned again.- const slices = [...f.slices, { start: f.start, made: f.made, traded: f.traded }].slice(-FLOW_SLICES)+ const slices = [...f.slices, { start: f.start, made: f.made, traded: f.traded, coins: { start: coins.start, made: coins.made, traded: coins.traded } }].slice(-FLOW_SLICES)const enough = slices.length >= FLOW_SLICES / 2const scale = FLOW_SLICES / slices.lengthconst over = (key: "made" | "traded") => each((g) => slices.reduce((n, s) => n + s[key][g], 0))const made = over("made"), traded = over("traded")const used = each((g) => Math.max(0, slices[0]!.start[g] + made[g] + traded[g] - now[g]))const perMinute = (a: Amounts) => each((g) => Math.round(a[g] * scale))+ const coinsMade = slices.reduce((n, s) => n + s.coins.made, 0)+ const coinsTraded = slices.reduce((n, s) => n + s.coins.traded, 0)+ const coinsUsed = Math.max(0, slices[0]!.coins.start + coinsMade + coinsTraded - treasury)resources.flow.set({- town, since: tick, start: now, made: amounts({}), traded: amounts({}), held: now, slices,- last: enough ? { made: perMinute(made), used: perMinute(used), traded: perMinute(traded) } : null+ town, since: tick, start: now, made: amounts({}), traded: amounts({}), held: now, coins: fresh, slices,+ last: enough+ ? {+ made: perMinute(made), used: perMinute(used), traded: perMinute(traded),+ coins: { made: Math.round(coinsMade * scale), used: Math.round(coinsUsed * scale), traded: Math.round(coinsTraded * scale) }+ }+ : null})})diff --git a/src/ui.ts b/src/ui.tsindex 4969e49..f8433d2 100644--- a/src/ui.ts+++ b/src/ui.ts@@ -254,7 +254,14 @@ export interface Stats {* rolling minute, reckoned again every ten seconds; `null` for the* first half minute.*/- readonly flow: { readonly made: Amounts; readonly used: Amounts; readonly traded: Amounts; readonly elsewhere: Amounts } | null+ readonly flow: {+ readonly made: Amounts+ readonly used: Amounts+ readonly traded: Amounts+ readonly elsewhere: Amounts+ /** The treasury over the same minute: taxes and other income, upkeep and spending, and trade. */+ readonly coins: { readonly made: number; readonly used: number; readonly traded: number }+ } | null/** Food the town ate, and food that rotted in store, over the last minute. */readonly eaten: numberreadonly rotted: number