A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D
Ranks ask for an income, not a hoard; a city asks for a guildhall
Claudecd186a395454
diff --git a/docs/how-to-play.md b/docs/how-to-play.mdindex bb76654..4a0b1af 100644--- a/docs/how-to-play.md+++ b/docs/how-to-play.md@@ -86,10 +86,13 @@ six months.pausing a building (no workers, a quarter of its upkeep); answering thecrown; and the speed of time (space pauses, 1, 2, 3 for 1×, 2×, 4×).- **Ranks**: a town is founded as a hamlet and rises to a *village*, a- *town*, and a *city*. Each rank has its goals (residents and coins for- the land's size, a share of them citizens, approval, not losing coins over- the last minute; a city also wants 15% of its people burghers, in- townhouses). Hold every goal of the next rank for two minutes and the town+ *town*, and a *city*. Each rank has its goals (residents and an income+ for the land's size, a share of them citizens, approval; a city also+ wants 15% of its people burghers, in townhouses, and a guildhall). The+ income goal is coins a minute after upkeep and administration, over the+ last minute, out of debt: a medium town asks 150 a minute, a village+ less, a city more. Coins held count for nothing: a treasury is for+ spending. Hold every goal of the next rank for two minutes and the townrises: the crown sends a reward, and play goes on. A rank is never lost.The town panel shows the next rank's goals.- **Upgrades** are made where a building stands, so they need no more@@ -287,7 +290,7 @@ over with a new one.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,+ The income goal 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.diff --git a/scripts/balance.ts b/scripts/balance.tsindex 9a837af..8c726a1 100644--- a/scripts/balance.ts+++ b/scripts/balance.ts@@ -121,7 +121,9 @@ interface World {readonly ruins: ReadonlyArray<{ readonly id: number; readonly tile: Tile }>/** Cells of ore rocks (other rocks are stone). */readonly ore: ReadonlySet<number>- readonly buildings: ReadonlyArray<{ readonly kind: KindId; readonly tile: Tile; readonly status: string; readonly site: boolean; readonly nearby: number }>+ readonly buildings: ReadonlyArray<{ readonly kind: KindId; readonly tile: Tile; readonly status: string; readonly site: boolean; readonly nearby: number; readonly level: number; readonly works: boolean }>+ /** The town hall's upgrades: 1 a guildhall, 2 a city hall. */+ readonly hall: numberreadonly homes: ReadonlyArray<{readonly tile: Tilereadonly tier: number@@ -166,7 +168,7 @@ const read = (snapshot: Snapshot): World => {if (c["Town/Paved"]) paved.add(key(tile.x, tile.y))if (c["Town/Park"]) parks.set(key(tile.x, tile.y), { x: tile.x, y: tile.y, ...c["Town/Park"] })if (c["Town/Ruin"]) ruins.push({ id: e.id, tile })- if (c["Town/Building"]) buildings.push({ kind: c["Town/Building"].kind, tile, status: c["Town/Status"], site: !!c["Town/Site"], nearby: c["Town/Staff"]?.nearby ?? 0 })+ if (c["Town/Building"]) buildings.push({ kind: c["Town/Building"].kind, tile, status: c["Town/Status"], site: !!c["Town/Site"], nearby: c["Town/Staff"]?.nearby ?? 0, level: c["Town/Upgraded"]?.level ?? 0, works: !!c["Town/Works"] })if (c["Town/House"])homes.push({tile,@@ -187,6 +189,7 @@ const read = (snapshot: Snapshot): World => {ruins,ore,buildings,+ hall: buildings.find((b) => b.kind === "townhall")?.level ?? 0,homes,stock: stock as Amounts,prices: r["Town/Market"].prices,@@ -516,7 +519,7 @@ const decide = (w: World): ReadonlyArray<Edit> => {if (sensible()) {// Taxes follow the town's mood; once every other goal is met, they come down to win it over.const goals = goalsOf(w.charter, {- settlers: w.census.settlers!, citizens: w.census.citizens!, coins: w.coins, net: net(w), approval: w.mood.approval+ settlers: w.census.settlers!, citizens: w.census.citizens!, coins: w.coins, net: net(w), approval: w.mood.approval, hall: w.hall}, rules)// Closing in on a complete town: everything but approval and the larder met. Cut taxes, buy food.const closing = goals.every((g) => g.met || g.id === "approval") && w.coins > 1000@@ -767,7 +770,7 @@ const run = (seed: number) => {const income = w.ledger.taxes + Math.max(0, w.ledger.trade)const costs = w.ledger.upkeep + w.ledger.admin + Math.max(0, -w.ledger.trade)const goals = goalsOf(w.charter, {- settlers: w.census.settlers!, citizens: w.census.citizens!, coins: w.coins, net: income - costs, approval: w.mood.approval+ settlers: w.census.settlers!, citizens: w.census.citizens!, coins: w.coins, net: income - costs, approval: w.mood.approval, hall: w.hall}, rules)rows.push({minute,@@ -795,7 +798,7 @@ const run = (seed: number) => {}if (process.env.CHRONICLE) for (const l of town.snapshot().resources["Town/Chronicle"].lines as Array<{ tick: number; text: string }>) console.log(` ${(l.tick / 3600).toFixed(1)} ${l.text}`)const lastGoals = goalsOf(w.charter, {- settlers: w.census.settlers!, citizens: w.census.citizens!, coins: w.coins, net: net(w), approval: w.mood.approval+ settlers: w.census.settlers!, citizens: w.census.citizens!, coins: w.coins, net: net(w), approval: w.mood.approval, hall: w.hall}, rules, w.fate.rank).map((g) => `${g.met ? "✓" : "✗"} ${g.text} (${g.value})`).join(" · ")const hh = Math.max(1, w.homes.length)console.log(` longest continuous goal hold: ${longestGoalHold.toFixed(1)} seconds`)diff --git a/src/content.ts b/src/content.tsindex b59f252..9f8551f 100644--- a/src/content.ts+++ b/src/content.ts@@ -745,16 +745,18 @@ export const TERMS = [45, 60, 90] as const/*** Sizes of land: its rough radius in cells (the coast wanders in and out of* it, with capes reaching further), and what the town must reach to be- * a town (see `ranks`: a village needs less of both, a city more).+ * a town (see `ranks`: a village needs less of both, a city more): its+ * residents, and its income, coins a minute after upkeep and+ * administration. Income, not coins held: a treasury is for spending.*/export const sizes: Readonly<Record<SizeId, {readonly title: stringreadonly radius: number- readonly goal: { readonly residents: number; readonly coins: number }+ readonly goal: { readonly residents: number; readonly income: number }}>> = {- small: { title: "small", radius: 14, goal: { residents: 180, coins: 2500 } },- medium: { title: "medium", radius: 19, goal: { residents: 320, coins: 4000 } },- large: { title: "large", radius: 25, goal: { residents: 520, coins: 5000 } }+ small: { title: "small", radius: 14, goal: { residents: 180, income: 100 } },+ medium: { title: "medium", radius: 19, goal: { residents: 320, income: 150 } },+ large: { title: "large", radius: 25, goal: { residents: 520, income: 200 } }}/**@@ -929,25 +931,27 @@ export const fate = {/*** The town's ranks, from the hamlet it is founded as. It rises to the next* once every goal of that rank has held for `fate.hold` minutes together:- * `residents` and `coins` (not losing any) as shares of its land's goal+ * `residents` and `income` (out of debt) as shares of its land's goal* (see `sizes`), a share of the residents `citizens` (those in townhouses- * count), a share `burghers`, and approval. A rank is never lost, and the- * crown sends `reward` coins on reaching it. The town rank is the old- * "complete" town; nothing ends when a rank is reached.+ * count), a share `burghers`, approval, and (a city) a town hall upgraded+ * `hall` times, a guildhall. A rank is never lost, and the crown sends+ * `reward` coins on reaching it. The town rank is the old "complete" town;+ * nothing ends when a rank is reached.*/export const ranks: ReadonlyArray<{readonly title: stringreadonly residents: number- readonly coins: number+ readonly income: numberreadonly citizens: numberreadonly burghers: numberreadonly approval: number+ readonly hall: numberreadonly reward: number}> = [- { title: "hamlet", residents: 0, coins: 0, citizens: 0, burghers: 0, approval: 0, reward: 0 },- { title: "village", residents: 0.35, coins: 0.3, citizens: 0.1, burghers: 0, approval: 0, reward: 300 },- { title: "town", residents: 1, coins: 1, citizens: fate.citizens, burghers: 0, approval: fate.approval, reward: 800 },- { title: "city", residents: 1.5, coins: 1.5, citizens: 0.5, burghers: 0.15, approval: 25, reward: 1500 }+ { title: "hamlet", residents: 0, income: 0, citizens: 0, burghers: 0, approval: 0, hall: 0, reward: 0 },+ { title: "village", residents: 0.35, income: 0.3, citizens: 0.1, burghers: 0, approval: 0, hall: 0, reward: 300 },+ { title: "town", residents: 1, income: 1, citizens: fate.citizens, burghers: 0, approval: fate.approval, hall: 0, reward: 800 },+ { title: "city", residents: 1.5, income: 1.5, citizens: 0.5, burghers: 0.15, approval: 25, hall: 1, reward: 1500 }]/** The rank the old "complete" town is: the goals a balance run plays for. */export const TOWN_RANK = 2diff --git a/src/host.ts b/src/host.tsindex 2ccce40..a2d5001 100644--- a/src/host.ts+++ b/src/host.ts@@ -1597,8 +1597,8 @@ export const createHost = async ("pave",sizeIds,(v) => sizes[v].title,- (v) => `a town at ${chip("people", `${sizes[v].goal.residents} residents`)}${chip("coins", `${sizes[v].goal.coins} coins`)}`,- (v) => `The town rank asks ${sizes[v].goal.residents} residents and ${sizes[v].goal.coins} coins; a village less, a city more`,+ (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)),diff --git a/src/present.ts b/src/present.tsindex 7e9a4d6..c2d8ca7 100644--- a/src/present.ts+++ b/src/present.ts@@ -1568,7 +1568,8 @@ export const Interact = Game.System(},goals: goalsOf(charter,- { settlers: census.settlers, citizens: census.citizens, burghers: census.burghers, coins, net: steadyNet(resources.history.get().samples, net), approval: mood.approval },+ { settlers: census.settlers, citizens: census.citizens, burghers: census.burghers, coins, net: steadyNet(resources.history.get().samples, net), approval: mood.approval,+ hall: hallRow ? maybe(hallRow.data.upgraded)?.level ?? 0 : 0 },undefined,f.rank),diff --git a/src/rules/goals.ts b/src/rules/goals.tsindex 4a54664..7c41f32 100644--- a/src/rules/goals.ts+++ b/src/rules/goals.ts@@ -1,3 +1,4 @@+import { upgradeAt } from "../content.ts"import { shippedRules, type Rules } from "./config.ts"/** Food in storage: what would carry the town through a winter. */@@ -23,13 +24,14 @@ export const nextRank = (rank: number, rules: Rules = shippedRules) => Math.min(*/export const goalsOf = (charter: { readonly size: keyof Rules["sizes"] },- s: { readonly settlers: number; readonly citizens: number; readonly burghers?: number; readonly coins: number; readonly net: number; readonly approval: number },+ s: { readonly settlers: number; readonly citizens: number; readonly burghers?: number; readonly coins: number; readonly net: number; readonly approval: number; readonly hall?: number },rules: Rules = shippedRules,rank = 1) => {const land = rules.sizes[charter.size].goal, r = rules.ranks[nextRank(rank, rules)]!- const residents = s.settlers + s.citizens, burghers = s.burghers ?? 0- const wanted = { residents: Math.round((land.residents * r.residents) / 10) * 10, coins: Math.round((land.coins * r.coins) / 100) * 100 }+ const residents = s.settlers + s.citizens, burghers = s.burghers ?? 0, hall = s.hall ?? 0+ const wanted = { residents: Math.round((land.residents * r.residents) / 10) * 10, income: Math.round((land.income * r.income) / 5) * 5 }+ const hallTitle = (upgradeAt(rules.kinds.townhall, r.hall)?.title ?? "town hall").toLowerCase()const share = (n: number) => (residents > 0 ? Math.round((n / residents) * 100) : 0)return [{ id: "residents", target: wanted.residents, text: `${wanted.residents} residents`, value: `${residents}`, met: residents >= wanted.residents },@@ -40,17 +42,20 @@ export const goalsOf = (? [{ id: "burghers", target: Math.round(r.burghers * 100), text: `${Math.round(r.burghers * 100)}% of them burghers`, value: `${share(burghers)}%`, met: residents > 0 && burghers >= residents * r.burghers }]: []),{ id: "approval", target: r.approval, text: `approval ${r.approval} or more`, value: `${s.approval}`, met: s.approval >= r.approval },- { id: "coins", target: wanted.coins, text: `${wanted.coins} coins, not losing any`, value: `${s.coins}`, met: s.coins >= wanted.coins && s.net >= 0 }+ { id: "income", target: wanted.income, text: `${wanted.income} coins a minute, out of debt`, value: `${s.net}`, met: s.net >= wanted.income && s.coins >= 0 },+ ...(r.hall > 0+ ? [{ id: "hall", target: r.hall, text: `a ${hallTitle}`, value: hall >= r.hall ? "built" : "not yet", met: hall >= r.hall }]+ : [])]}/**- * A term's score: its people (citizens three times, burghers four), coins,- * approval, the crown's requests met and missed, and the rank the town+ * A term's score: its people (citizens three times, burghers four), its income,+ * approval, the crown's requests met and missed, its town hall's upgrades, and the rank the town* stands at.*/export const scoreOf = (- s: { readonly settlers: number; readonly citizens: number; readonly burghers?: number; readonly coins: number; readonly approval: number; readonly met: number; readonly missed: number },+ s: { readonly settlers: number; readonly citizens: number; readonly burghers?: number; readonly net: number; readonly approval: number; readonly met: number; readonly missed: number; readonly hall?: number },rank: number) =>- Math.max(0, Math.round(s.settlers + 3 * s.citizens + (s.burghers ?? 0) + Math.max(0, s.coins) / 20 + 2 * s.approval + 40 * s.met - 40 * s.missed + 250 * rank))+ Math.max(0, Math.round(s.settlers + 3 * s.citizens + (s.burghers ?? 0) + 2 * Math.max(0, s.net) + 2 * s.approval + 40 * s.met - 40 * s.missed + 100 * (s.hall ?? 0) + 250 * rank))diff --git a/src/sim/term.ts b/src/sim/term.tsindex fde8e9b..dd8beea 100644--- a/src/sim/term.ts+++ b/src/sim/term.ts@@ -7,7 +7,7 @@ import { shippedRules } from "../rules/config.ts"*/import { crown, HISTORY_TICKS, ticks, WINTER_WARNING, type GoodId } from "../content.ts"import * as Random from "@typeonce/bevy-ts-math/Random"-import { Building, Census, Charter, Chronicle, Crown, Fate, Game, Rules, History, House, Inventory, Ledger, Mood, Pantry, Paused, Policy, Recipe, Rising, Rng, Site, Stock, Storage, Time, Treasury } from "../schema.ts"+import { Building, Census, Charter, Chronicle, Crown, Fate, Game, Rules, History, House, Inventory, Ledger, Mood, Pantry, Paused, Policy, Recipe, Rising, Rng, Site, Stock, Storage, Time, Treasury, TownHall, Upgraded, maybe } from "../schema.ts"import { elapsed, seasonAt, termTicks, write } from "./shared.ts"// ── Time ──────────────────────────────────────────────────────────────────@@ -205,6 +205,7 @@ export { goalsOf, larder, nextRank, scoreOf, steadyNet } from "../rules/goals.tsexport const Judge = Game.System("Town/Judge",{+ queries: { halls: Game.Query({ selection: { upgraded: Game.Query.optional(Upgraded) }, with: [TownHall] }) },resources: {time: Game.System.readResource(Time),charter: Game.System.readResource(Charter),@@ -219,9 +220,12 @@ export const Judge = Game.System(},services: { rules: Game.System.service(Rules) }},- ({ resources, services }) => {+ ({ queries, resources, services }) => {const rules = services.rulesconst { crown, fate, ranks } = rules+ // The town hall's upgrades: a city asks for a guildhall.+ const hallRow = queries.halls.each()[0]+ const hall = hallRow ? maybe(hallRow.data.upgraded)?.level ?? 0 : 0const charter = resources.charter.get()const t = elapsed(resources.time.get(), charter)@@ -253,7 +257,7 @@ export const Judge = Game.System(const end = (state: "lost" | "over", reason: string) => {note(reason)- resources.fate.set({ ...next, state, reason, at: t, score: scoreOf({ ...census, coins, approval, met: crownNow.met, missed: crownNow.missed }, next.rank) })+ resources.fate.set({ ...next, state, reason, at: t, score: scoreOf({ ...census, net, approval, met: crownNow.met, missed: crownNow.missed, hall }, next.rank) })}if (next.debtSince >= 0 && t - next.debtSince >= minutes(fate.debt)) return end("lost", "Bankrupt: the crown has taken the treasury out of the mayor's hands.")if (next.revoltSince >= 0 && t - next.revoltSince >= minutes(fate.revolt)) return end("lost", "Revolt: the town has turned the mayor out.")@@ -265,7 +269,7 @@ export const Judge = Game.System(// The next rank.if (nextRank(f.rank, rules) > f.rank) {- const goals = goalsOf(charter, { ...census, coins, net, approval }, rules, f.rank)+ const goals = goalsOf(charter, { ...census, coins, net, approval, hall }, rules, f.rank)if (goals.every((g) => g.met)) {const to = ranks[f.rank + 1]!if (f.goalSince < 0) note(`Every goal of a ${to.title} is met: the town needs only to hold them.`)diff --git a/test/balance-victory.test.ts b/test/balance-victory.test.tsindex 2b6a2cd..2a6d6f9 100644--- a/test/balance-victory.test.ts+++ b/test/balance-victory.test.ts@@ -34,12 +34,9 @@ for (const from of [1, 2]) {assert(final.fate.rank >= 1, stdout)assert.notEqual(final.fate.state, "lost", stdout)})- // Since homes pay to rise, fish ran short, and the treasury started smaller, the sensible mayor comes- // close to the town rank in a 60-minute term (residents, citizens, and approval) but not to 4000 coins held.- test(`generated charter ${1000 + from * 7919} reaches the town rank through ordinary edits`,- { timeout: 180000, todo: "the sensible mayor falls short of 4000 coins held; the coins goal or the mayor is to be decided" }, () => {- const { final, stdout } = play(from)- // The old "complete" town is the town rank: reached (its goals held for `fate.hold`), the term plays on to its end.- assert(final.fate.rank >= TOWN_RANK, stdout)- })+ test(`generated charter ${1000 + from * 7919} reaches the town rank through ordinary edits`, { timeout: 180000 }, () => {+ const { final, stdout } = play(from)+ // The old "complete" town is the town rank: reached (its goals held for `fate.hold`), the term plays on to its end.+ assert(final.fate.rank >= TOWN_RANK, stdout)+ })}diff --git a/test/endless.test.ts b/test/endless.test.tsindex 845c43c..b73ff79 100644--- a/test/endless.test.ts+++ b/test/endless.test.ts@@ -55,8 +55,16 @@ test("each rank asks more than the last, and the city asks for burghers", () =>assert.equal(residents(towns), 180)assert.equal(nextRank(ranks.length - 1), ranks.length - 1)assert.deepEqual(goalsOf(charter, town, undefined, 3), city)+ // Each asks an income, not coins held; the city asks for a guildhall too, and a guildhall meets it.+ const income = (goals: ReadonlyArray<{ id: string; target: number }>) => goals.find((g) => g.id === "income")!.target+ assert.equal(income(towns), 100)+ assert(income(village) < income(towns) && income(towns) < income(city))+ assert.equal(towns.some((g) => g.id === "hall"), false)+ assert.equal(city.find((g) => g.id === "hall")?.met, false)+ assert.equal(goalsOf(charter, { ...town, hall: 1 }, undefined, 2).find((g) => g.id === "hall")?.met, true)+ assert.equal(goalsOf(charter, { ...town, coins: 1_000_000 }, undefined, 1).find((g) => g.id === "income")?.met, false, "a hoard is not an income")// A rank counts for more than its people.- const s = { settlers: 100, citizens: 50, burghers: 10, coins: 2000, approval: 20, met: 2, missed: 0 }+ const s = { settlers: 100, citizens: 50, burghers: 10, net: 150, approval: 20, met: 2, missed: 0 }assert(scoreOf(s, 2) > scoreOf(s, 1))})