A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D
A market's panel says what its walkers handed out over the last minute
SandroMaglione05cf9b519175
diff --git a/src/present.ts b/src/present.tsindex 5cdc472..3e6a11e 100644--- a/src/present.ts+++ b/src/present.ts@@ -12,7 +12,7 @@ import { homeHeight, topOf, type Alert, type Load, type Emphasis } from "./art.timport { BuildingView, HouseView, WalkerView, TreeView, RockView, FieldView, RuinView } from "./render3d/views.ts"import type { DoorSet, OverlayCell } from "./render3d/models.ts"import {- amounts, BUILD_TICKS, builtCost, COMMUTE, RESTOCK_TICKS, STEP_SECONDS, condition as wear, CONDITION_MAX, costText, crewOf, crown as crownRules, fate, goodIds, goods, grounds, home, kinds, needTitle,+ amounts, BUILD_TICKS, builtCost, COMMUTE, provisions, RESTOCK_TICKS, STEP_SECONDS, condition as wear, CONDITION_MAX, costText, crewOf, crown as crownRules, fate, goodIds, goods, grounds, home, kinds, needTitle,kindOrder, nextUpgrade, worksNeeded, PAVE_COST, propIds, props, ranks, risk, RISK_MAX, ROAD_COST, tierHome, total, upgradeAt, upkeepOf, winters, type Amounts, type GoodId, type Kind, type KindId, type Need, type Tier} from "./content.ts"import { around, canPlace, cellOf, doors, entrances, isRoad, key, lPath, nearest, reach, useAt, type TileLike } from "./grid.ts"@@ -70,6 +70,7 @@ import {Status,Stock,Flow,+ Handed,Tile,Time,TownHall,@@ -385,6 +386,7 @@ export const Interact = Game.System(ledger: Game.System.readResource(Ledger),stock: Game.System.readResource(Stock),flow: Game.System.readResource(Flow),+ handed: Game.System.readResource(Handed),policy: Game.System.readResource(Policy),time: Game.System.readResource(Time),charter: Game.System.readResource(Charter),@@ -888,10 +890,26 @@ export const Interact = Game.System(label: "walkers out",value: `${maybe(row.data.walkers)?.length ?? 0}`})+ // A provisions walker's building: what its walkers handed to homes over the last minute, which its shelf+ // alone cannot say (food leaves in the basket as soon as it comes; furniture mostly comes back).+ if (service?.service === "goods") {+ const log = resources.handed.get().log.filter((l) => l.from === row.entity.id)+ const given: Record<GoodId, number> = { ...amounts({}) }+ for (const l of log) for (const [g, n] of Object.entries(l.goods) as Array<[GoodId, number]>) given[g] += n+ const homesFed = new Set(log.map((l) => l.to)).size+ work.push(+ homesFed > 0+ ? { icon: "people", label: "handed out, last minute", goods: provisions.filter((g) => given[g] > 0).map((g) => ({ good: g, count: given[g] })),+ note: `to ${homesFed} home${homesFed === 1 ? "" : "s"} on its walks` }+ : { icon: "people", label: "handed out, last minute", value: "nothing", tone: "dim", note: "no home on its walks needed what it had" }+ )+ }const inventory = maybe(row.data.inventory)if (inventory) {const held = inventory.accepts.filter((g) => inventory.stock[g] > 0).map((g) => ({ good: g, count: inventory.stock[g] }))- work.push(held.length > 0 ? { icon: "jobs", label: "holds", goods: held } : { icon: "jobs", label: "holds", value: "nothing yet", tone: "dim" })+ // A market's shelf is only what waits for the next walker.+ const label = kind.id === "market" ? "on the shelf now" : "holds"+ work.push(held.length > 0 ? { icon: "jobs", label, goods: held } : { icon: "jobs", label, value: "nothing", tone: "dim" })}if (kind.wear && inventory)work.push(diff --git a/src/schema.ts b/src/schema.tsindex 857a092..8b233f7 100644--- a/src/schema.ts+++ b/src/schema.ts@@ -17,7 +17,7 @@ import type { NodeRegistry } from "@typeonce/bevy-ts-render"import type { BuildingView, FieldView, HouseView, RockView, RuinView, TreeView, WalkerView } from "./render3d/views.ts"import type { GameScene } from "./render3d/scene.ts"-import { goodIds, groundIds, kindIds, landIds, propIds, serviceIds, sizeIds, type Amounts } from "./content.ts"+import { goodIds, groundIds, kindIds, landIds, propIds, serviceIds, sizeIds, type Amounts, type GoodId } from "./content.ts"import type { Rules as RuleInputs } from "./rules/config.ts"import type { UiValue } from "./ui.ts"@@ -407,6 +407,15 @@ export interface FlowValue {} | null}export const Flow = Descriptor.TransientResource<FlowValue>()("Town/Flow")+/**+ * What provisions walkers handed to homes over the last minute (not saved: it starts over on a load): a line for+ * each home a walker gave something, from which building, and what. A market's panel reads it, as its shelf alone+ * says little: food leaves in the walker's basket as soon as it comes, and furniture mostly comes back.+ */+export interface HandedValue {+ readonly log: ReadonlyArray<{ readonly at: number; readonly from: number; readonly to: number; readonly goods: Partial<Record<GoodId, number>> }>+}+export const Handed = Descriptor.TransientResource<HandedValue>()("Town/Handed")// ── Host services ─────────────────────────────────────────────────────────@@ -431,7 +440,7 @@ export const Game = Schema.bind(},resources: {Time, Rng, Treasury, Ledger, Census, Policy, Charter, Land, Market, Mood, Crown, Fate, History, Chronicle, Guide, Appeal, Pantry, Fires,- Grid, Stock, Flow+ Grid, Stock, Flow, Handed},relations: { WalkerOf, FieldOf }}),diff --git a/src/sim/index.ts b/src/sim/index.tsindex c1c534e..267dc54 100644--- a/src/sim/index.ts+++ b/src/sim/index.ts@@ -79,6 +79,7 @@ export const blankResources = () => ({Fires: { fires: [], next: 1 },Grid: { owner: [], use: [] },Stock: amounts({}),+ Handed: { log: [] },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 }})diff --git a/src/sim/walkers.ts b/src/sim/walkers.tsindex d76f83e..ca289e2 100644--- a/src/sim/walkers.ts+++ b/src/sim/walkers.ts@@ -11,10 +11,10 @@ import { appealAt } from "../appeal.ts"* planks and stone to mend — and walks home when its range is walked.*/-import { amounts, categoryColor, each, provisions, total, type Amounts } from "../content.ts"+import { amounts, categoryColor, each, provisions, ticks, total, type Amounts } from "../content.ts"import { DIRS, doors, isRoad, key, nearest, ownerAt, pathTo, reach, useAt } from "../grid.ts"import { inside, type Point } from "../iso.ts"-import { Building, Burning, Condition, Figure, Game, Rules, Grid, Appeal, Patrol, House, Inventory, Pantry, Paused, Recipe, Rescue, Rising, Risk, Roam, Route, Service, Sick, Site, Staff, Status, Tile, Time, WalkerOf, maybe, type Status as StatusValue } from "../schema.ts"+import { Building, Burning, Condition, Figure, Game, Rules, Grid, Appeal, Handed, Patrol, House, Inventory, Pantry, Paused, Recipe, Rescue, Rising, Risk, Roam, Route, Service, Sick, Site, Staff, Status, Tile, Time, WalkerOf, maybe, type HandedValue, type Status as StatusValue } from "../schema.ts"import { CELL_STEPS } from "./shared.ts"import { paced } from "./work.ts"@@ -189,7 +189,7 @@ export const Wander = Game.System(buildings: Game.Query({ selection: { tile: Game.Query.read(Tile) }, with: [Service] }),inventories: Game.Query({ selection: { inventory: Game.Query.write(Inventory) }, with: [Service] })},- resources: { grid: Game.System.readResource(Grid), appeal: Game.System.readResource(Appeal), pantry: Game.System.writeResource(Pantry) },+ resources: { grid: Game.System.readResource(Grid), appeal: Game.System.readResource(Appeal), pantry: Game.System.writeResource(Pantry), handed: Game.System.writeResource(Handed), time: Game.System.readResource(Time) },services: { rules: Game.System.service(Rules) }},({ queries, resources, commands, services }) => {@@ -203,6 +203,9 @@ export const Wander = Game.System(const cared = new Map<number, ReturnType<typeof queries.cared.each>[number]>()for (const row of queries.cared.each()) cared.set(row.entity.id, row)let eaten = 0+ // What each walker hands each home, for its building's last minute (see `Handed`).+ const tick = resources.time.get().tick+ const handed: Array<HandedValue["log"][number]> = []for (const { entity, data } of walkers) {const route = data.route.get()@@ -243,6 +246,8 @@ export const Wander = Game.System(if (roam.service === "faith") h.faith = home.lasts.faithelse if (roam.service === "goods") {const served = serveGoods(h, amounts(basket), rules, appealAt(resources.appeal.get().cells, x, y))+ const given = Object.fromEntries(Object.entries(served.basket).filter(([g, n]) => (basket[g] ?? 0) > n).map(([g, n]) => [g, (basket[g] ?? 0) - n]))+ if (Object.keys(given).length > 0) handed.push({ at: tick, from: data.home.get(), to: id, goods: given })Object.assign(h, served.house)Object.assign(basket, served.basket)eaten += served.eaten@@ -287,5 +292,8 @@ export const Wander = Game.System(data.roam.set({ ...roam, left: 0, basket: carrying, back: true })}if (eaten > 0) resources.pantry.update((p) => ({ ...p, eaten: p.eaten + eaten }))+ // The last minute only; a log from another town (its clock behind this one's) is dropped.+ const kept = resources.handed.get().log.filter((l) => l.at <= tick && tick - l.at < ticks(60))+ if (handed.length > 0 || kept.length !== resources.handed.get().log.length) resources.handed.set({ log: [...kept, ...handed] })})