A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D
A market's walker brings home what it did not hand out, without losing it
SandroMaglione8a2266dd159f
diff --git a/package.json b/package.jsonindex fcba6be..cf628f2 100644--- a/package.json+++ b/package.json@@ -11,6 +11,7 @@"balance": "node scripts/balance.ts","balance:model": "node tools/balance/cli.ts","balance:worth": "node scripts/worth.ts",+ "balance:furniture": "node scripts/furniture.ts","balance:model:check": "node --test test/balance-model.test.ts","check": "node scripts/check.ts","check:browser": "node scripts/check-browser.ts",diff --git a/scripts/furniture.ts b/scripts/furniture.tsnew file mode 100644index 0000000..da8a451--- /dev/null+++ b/scripts/furniture.ts@@ -0,0 +1,108 @@+/**+ * How furniture keeps up with the townhouses, in the real simulation,+ * headless: a street of homes beside the town hall, a market and a carpenter+ * across it, planks kept in the store. Everything else a townhouse asks+ * (desirability, the three services, bread, cloth, faith) is given, by rule+ * patches, full larders and homes kept in repair, so what is measured is furniture alone: how soon+ * the homes become townhouses, how many stay townhouses, and what the+ * carpenter makes and the townhouses use.+ *+ * node scripts/furniture.ts [minutes]+ */+import { kinds } from "../src/content.ts"+import { resolveRules } from "../src/rules/config.ts"+import { headless, type Snapshot } from "../src/sim/headless.ts"++const minutes = Number(process.argv[2] ?? 30)+const FOREVER = 1e9+const rules = [+ // Every street fits a townhouse, every service calls, and bread, cloth and faith never run out.+ { path: "home.tiers.2.appeal", value: 0 },+ { path: "home.tiers.3.appeal", value: 0 },+ { path: "risk.kept", value: FOREVER },+ { path: "home.lasts.bread", value: FOREVER },+ { path: "home.lasts.cloth", value: FOREVER },+ { path: "home.lasts.faith", value: FOREVER }+]++const kindOf = (e: Snapshot["entities"][number]) => e.components["Town/Building"]?.kind as string | undefined+const hallOf = (s: Snapshot) => s.entities.find((e) => e.components["Town/TownHall"])!++const run = (homes: number, carpenters: number) => {+ const t = headless({ size: "medium", term: 90, land: "fair", seed: 7 }, { rules: resolveRules(rules) })+ const hall = hallOf(t.snapshot()).components["Town/Tile"]+ const top = () =>+ t.change((s) => {+ const h = hallOf(s)+ const stock = h.components["Town/Inventory"].stock+ h.components["Town/Inventory"].stock = { ...stock, planks: Math.max(stock.planks, 60), stone: Math.max(stock.stone, 60), tools: Math.max(stock.tools, 20) }+ s.resources["Town/Treasury"].coins = Math.max(s.resources["Town/Treasury"].coins, 5000)+ // Nothing wears out or burns: a builder and a fire watch are given too.+ for (const e of s.entities) {+ if (e.components["Town/Condition"]) e.components["Town/Condition"] = { value: 1000 }+ delete e.components["Town/Burning"]+ }+ })+ top()+ // A street east of the hall's ring road, its homes on the south side.+ const length = Math.max(homes, 6 + 2 * carpenters) + 2+ t.edit(+ { type: "roads", cells: Array.from({ length: length + 1 }, (_, i) => ({ x: hall.x + 4 + i, y: hall.y + 1 })) },+ { type: "homes", cells: Array.from({ length: homes }, (_, i) => ({ x: hall.x + 5 + i, y: hall.y + 2 })) }+ )+ t.run(60)+ // The homes as citizens' homes, their larders full for good.+ t.change((s) => {+ for (const e of s.entities) {+ const h = e.components["Town/House"]+ if (!h || e.components["Town/Site"]) continue+ e.components["Town/House"] = { ...h, tier: 2, residents: 8, bread: FOREVER, cloth: FOREVER, faith: FOREVER, food: FOREVER }+ e.components["Town/Rebuilt"] = { tier: 2 }+ }+ })+ t.run(10)+ // Across the street: a market, then the carpenters.+ t.edit({ type: "building", kind: "market", cell: { x: hall.x + 4, y: hall.y - 1 }, open: false })+ for (let i = 0; i < carpenters; i++) t.edit({ type: "building", kind: "carpenter", cell: { x: hall.x + 6 + 2 * i, y: hall.y - 1 }, open: false })+ const started = t.snapshot().resources["Town/Time"].tick+ const samples: Array<Record<string, number | string> & { townhouses: number; made: number }> = []+ let firstAll = -1+ for (let m = 1; m <= minutes; m++) {+ for (let k = 0; k < 6; k++) {+ top()+ t.run(10)+ }+ const s = t.snapshot()+ const houses = s.entities.filter((e) => e.components["Town/House"] && !e.components["Town/Site"])+ const townhouses = houses.filter((e) => e.components["Town/House"].tier === 3).length+ const shops = s.entities.filter((e) => kindOf(e) === "carpenter")+ const market = s.entities.find((e) => kindOf(e) === "market")+ const sample = {+ minute: m,+ townhouses,+ furnished: houses.filter((e) => e.components["Town/House"].furniture > 0).length,+ hall: hallOf(s).components["Town/Inventory"].stock.furniture,+ market: market?.components["Town/Inventory"]?.stock.furniture ?? 0,+ made: shops.reduce((n, e) => n + (e.components["Town/Recipe"]?.made ?? 0), 0),+ working: shops.filter((e) => e.components["Town/Status"] === "working").length,+ status: shops.map((e) => e.components["Town/Status"]).join(","),+ planks: hallOf(s).components["Town/Inventory"].stock.planks,+ tiers: houses.map((e) => `${e.components["Town/House"].tier}/${e.components["Town/House"].residents}/${Math.round(e.components["Town/House"].furniture / 60)}`).join(" ")+ }+ samples.push(sample)+ if (firstAll < 0 && townhouses === homes) firstAll = m+ }+ const ticks = t.snapshot().resources["Town/Time"].tick - started+ const last = samples.at(-1)!+ const tail = samples.slice(Math.floor(samples.length / 2))+ const held = tail.reduce((n, x) => n + x.townhouses, 0) / tail.length+ return { homes, carpenters, firstAll, held, made: last.made, perMinute: last.made / minutes, samples }+}++console.log(`Furniture over ${minutes} minutes: ${kinds.carpenter.recipe!.inputs} planks → 1 furniture every ${kinds.carpenter.recipe!.seconds} s; a piece lasts 300 s, topped up at half.`)+console.log("homes · carpenters · all townhouses by · townhouses held (second half) · furniture made · made a minute")+for (const [homes, carpenters] of (process.env.ONLY ? [JSON.parse(process.env.ONLY)] : [[4, 1], [8, 1], [16, 1], [24, 1], [24, 2]]) as Array<[number, number]>) {+ const r = run(homes, carpenters)+ console.log(`${String(homes).padStart(5)} · ${String(carpenters).padStart(10)} · ${r.firstAll < 0 ? "never" : `minute ${r.firstAll}`} · ${r.held.toFixed(1)} of ${homes} · ${r.made} · ${r.perMinute.toFixed(1)}`)+ if (process.env.VERBOSE) for (const x of r.samples) console.log(" ", JSON.stringify(x))+}diff --git a/src/sim/walkers.ts b/src/sim/walkers.tsindex 7d6ad8b..d76f83e 100644--- a/src/sim/walkers.ts+++ b/src/sim/walkers.ts@@ -215,7 +215,9 @@ export const Wander = Game.System(const inv = queries.inventories.get(from)if (inv.ok && total(roam.basket) > 0) {const i = inv.value.data.inventory.get()- inv.value.data.inventory.set({ ...i, stock: each((g) => Math.min(i.cap, i.stock[g] + roam.basket[g])) })+ // What it did not hand out goes back on the shelf, even past its room (the shelf filled again meanwhile):+ // the building asks for no more until it is down again, and nothing is lost.+ inv.value.data.inventory.set({ ...i, stock: each((g) => i.stock[g] + roam.basket[g]) })}commands.despawn(entity.id)continuediff --git a/test/furniture.test.ts b/test/furniture.test.tsnew file mode 100644index 0000000..0936097--- /dev/null+++ b/test/furniture.test.ts@@ -0,0 +1,59 @@+/**+ * Furniture reaching the townhouses, in the real simulation, headless:+ * everything else a townhouse asks is given by rule patches and full+ * larders (as `scripts/furniture.ts` does), so this is furniture alone.+ */+import assert from "node:assert/strict"+import { test } from "node:test"++import { resolveRules } from "../src/rules/config.ts"+import { headless, type Snapshot } from "../src/sim/headless.ts"++const FOREVER = 1e9+const hallOf = (s: Snapshot) => s.entities.find((e) => e.components["Town/TownHall"])!++test("a market's walker brings home what it did not hand out, so furniture reaches the store and homes can rise into townhouses", () => {+ const rules = resolveRules([+ { path: "home.tiers.2.appeal", value: 0 },+ { path: "home.tiers.3.appeal", value: 0 },+ { path: "risk.kept", value: FOREVER },+ { path: "home.lasts.bread", value: FOREVER },+ { path: "home.lasts.cloth", value: FOREVER },+ { path: "home.lasts.faith", value: FOREVER }+ ])+ const t = headless({ size: "medium", term: 90, land: "fair", seed: 7 }, { rules })+ const hall = hallOf(t.snapshot()).components["Town/Tile"]+ const top = () =>+ t.change((s) => {+ const h = hallOf(s), stock = h.components["Town/Inventory"].stock+ h.components["Town/Inventory"].stock = { ...stock, planks: Math.max(stock.planks, 60), stone: 60, tools: 20 }+ s.resources["Town/Treasury"].coins = 5000+ for (const e of s.entities) if (e.components["Town/Condition"]) e.components["Town/Condition"] = { value: 1000 }+ })+ top()+ t.edit(+ { type: "roads", cells: Array.from({ length: 11 }, (_, i) => ({ x: hall.x + 4 + i, y: hall.y + 1 })) },+ { type: "homes", cells: Array.from({ length: 8 }, (_, i) => ({ x: hall.x + 5 + i, y: hall.y + 2 })) }+ )+ t.run(60)+ t.change((s) => {+ for (const e of s.entities) {+ const h = e.components["Town/House"]+ if (!h || e.components["Town/Site"]) continue+ e.components["Town/House"] = { ...h, tier: 2, residents: 8, bread: FOREVER, cloth: FOREVER, faith: FOREVER, food: FOREVER }+ e.components["Town/Rebuilt"] = { tier: 2 }+ }+ })+ t.run(10)+ t.edit(+ { type: "building", kind: "market", cell: { x: hall.x + 4, y: hall.y - 1 }, open: false },+ { type: "building", kind: "carpenter", cell: { x: hall.x + 6, y: hall.y - 1 }, open: false }+ )+ for (let i = 0; i < 6 * 14; i++) {+ top()+ t.run(10)+ }+ const s = t.snapshot()+ // Its walker away, a market refills; what the walker brings back once was lost over the shelf's room.+ assert(s.entities.filter((e) => e.components["Town/House"]?.tier === 3).length >= 6, JSON.stringify(s.entities.filter((e) => e.components["Town/House"]).map((e) => e.components["Town/House"].tier)))+})