Skip to content

A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D

Draw randomness from the Rng resource with Random.draw

SandroMaglione1829c3fbfe09
diff --git a/src/schema.ts b/src/schema.ts
index 8f1bd2f..745534d 100644
--- a/src/schema.ts
+++ b/src/schema.ts
@@ -13,6 +13,7 @@
*/
import { Clock as SimulationClock, Decode, Descriptor, Schema } from "@typeonce/ritmo"
import type { OptionalReadCell } from "@typeonce/ritmo/Query"
+import * as Random from "@typeonce/ritmo-math/Random"
import type { NodeRegistry } from "@typeonce/ritmo-render"
import { step } from "./content.ts"
@@ -194,8 +195,8 @@ export const { relation: FieldOf } = Descriptor.Hierarchy("Town/FieldOf", "Town/
// ── Resources ─────────────────────────────────────────────────────────────
export const Time = SimulationClock.resource("Town/Time", step)
-/** The deterministic random state (see `@typeonce/ritmo-math/Random`). */
-export const Rng = Descriptor.ConstructedResource(integer)("Town/Rng")
+/** The world's seeded random state: systems draw with `Random.draw(resources.rng)` (see `@typeonce/ritmo-math/Random`). */
+export const Rng = Descriptor.ConstructedResource(Random)("Town/Rng")
export const Treasury = Descriptor.ConstructedResource(Decode.struct({ coins: integer }))("Town/Treasury")
/** The last collection's taxes, trade (sold less bought), upkeep, and administration, per minute. */
export const Ledger = Descriptor.ConstructedResource(Decode.struct({ taxes: integer, trade: integer, upkeep: integer, admin: integer }))("Town/Ledger")
diff --git a/src/sim/decay.ts b/src/sim/decay.ts
index cbc208b..a9baff6 100644
--- a/src/sim/decay.ts
+++ b/src/sim/decay.ts
@@ -72,7 +72,7 @@ export const Decay = Game.System(
// Spoiling: each shelf loses its share of what spoils, a granary's a fifth as much.
if (tick % SPOIL_TICKS === 0) {
- const random = Random.make(resources.rng.get())
+ const random = Random.draw(resources.rng)
let rotted = 0
for (const { data } of queries.shelves.each()) {
const inv = data.inventory.get()
@@ -89,7 +89,6 @@ export const Decay = Game.System(
}
if (stock !== inv.stock) data.inventory.set({ ...inv, stock })
}
- resources.rng.set(random.state)
if (rotted > 0) resources.pantry.update((p) => ({ ...p, rotted: p.rotted + rotted }))
}
diff --git a/src/sim/edits.ts b/src/sim/edits.ts
index f511357..9388f2c 100644
--- a/src/sim/edits.ts
+++ b/src/sim/edits.ts
@@ -495,7 +495,7 @@ export const ApplyEdits = Game.System(
resources.chronicle.set({
lines: [{ tick: 0, text: `Founded on ${sizes[charter.size].title}, ${richness.title} land, for a term of ${charter.term} minutes.`, alarm: false, at: null }]
})
- resources.rng.set(charter.seed)
+ resources.rng.set(Random.seed(charter.seed))
events.selection.emit({ clear: true })
// Nothing else this step: the new land is not on the grid yet.
break edits
diff --git a/src/sim/hazards.ts b/src/sim/hazards.ts
index 5135b8f..2891f07 100644
--- a/src/sim/hazards.ts
+++ b/src/sim/hazards.ts
@@ -132,7 +132,7 @@ export const Hazards = Game.System(
const approval = resources.mood.get().approval
const season = seasonAt(t, charter, rules)
const heating = season.severity !== null && winters[season.severity].heating
- const random = Random.make(resources.rng.get())
+ const random = Random.draw(resources.rng)
const news = (text: string, at: Point | null = null) => write(resources.chronicle, t, text, true, at)
const landmarks: Array<Landmark> = queries.landmarks.each().map(({ data }) => ({ kind: data.building.get().kind, ...data.tile.get() }))
const where = (cell: Point) => placeName(landmarks, cell)
@@ -323,7 +323,6 @@ export const Hazards = Game.System(
news(`Fire on ${where(f)}: ${took.length > 0 ? `${took.join(" and ")} lost` : "put out"}.`, { x: f.x, y: f.y })
}
resources.fires.set({ fires: fires.fires.filter((f) => stillBurning.has(f.id)), next: fires.next })
- resources.rng.set(random.state)
}
)
diff --git a/src/sim/index.ts b/src/sim/index.ts
index 798ec1d..b674599 100644
--- a/src/sim/index.ts
+++ b/src/sim/index.ts
@@ -3,6 +3,7 @@
* renderer, so it runs headless too.
*/
import { Clock } from "@typeonce/ritmo"
+import * as Random from "@typeonce/ritmo-math/Random"
import { amounts, defaultCharter, goodIds, step, TRADE_LOAD } from "../content.ts"
import { Game, Time } from "../schema.ts"
import { ApplyEdits } from "./edits.ts"
@@ -58,7 +59,7 @@ export const simulate = Game.Schedule(
/** The world's resources before any town is founded (a `newTown` edit fills them in). */
export const blankResources = () => ({
Time: Clock.start(step),
- Rng: 0x5eed,
+ Rng: Random.seed(0x5eed),
Treasury: { coins: 0 },
Ledger: { taxes: 0, trade: 0, upkeep: 0, admin: 0 },
Census: { homes: 0, settlers: 0, citizens: 0, burghers: 0, jobs: 0, employed: 0, food: 0, bread: 0, cloth: 0, faith: 0, furniture: 0, sick: 0, burning: 0 },
diff --git a/src/sim/term.ts b/src/sim/term.ts
index 5f6e287..2b88b5d 100644
--- a/src/sim/term.ts
+++ b/src/sim/term.ts
@@ -120,9 +120,7 @@ export const Requests = Game.System(
// Only what keeps: fish and bread would rot on the way.
const options = [...rates.keys()].filter((g) => spoils[g] === undefined).sort()
if (options.length === 0) return
- const random = Random.make(resources.rng.get())
- const good = random.pick(options)!
- resources.rng.set(random.state)
+ const good = Random.draw(resources.rng).pick(options)!
// Larger the later in this term it is: a new term starts the crown small again.
const progress = Math.min(1, Math.max(0, t - resources.fate.get().began) / termTicks(charter, rules))
const window = (crownGap(charter, rules) * crown.window) / 3600
diff --git a/src/sim/work.ts b/src/sim/work.ts
index ee0db4e..bbe1be1 100644
--- a/src/sim/work.ts
+++ b/src/sim/work.ts
@@ -537,7 +537,7 @@ export const Plant = Game.System(
const { staffTicks: STAFF_TICKS } = rules
const grid = resources.grid.get()
- const random = Random.make(resources.rng.get())
+ const random = Random.draw(resources.rng)
const sown = new Set<number>()
const tick = resources.time.get().step
for (const { entity, data } of queries.planters.each()) {
@@ -569,7 +569,6 @@ export const Plant = Game.System(
else commands.spawn(Game.Command.relate(fieldDraft(spot), FieldOf, entity.id))
data.planter.set({ ...planter, cooldown: planter.interval })
}
- resources.rng.set(random.state)
}
)
@@ -593,7 +592,7 @@ export const Spread = Game.System(
if (!every(resources.time.get(), SPREAD_TICKS)) return
const grid = resources.grid.get()
- const random = Random.make(resources.rng.get())
+ const random = Random.draw(resources.rng)
const grown = queries.trees.each().filter(({ data }) => data.tree.get().growth >= GROWN.tree)
const parent = random.pick(grown)
if (parent) {
@@ -601,7 +600,6 @@ export const Spread = Game.System(
const spot = random.pick(wildTreeSpots(grid, t))
if (spot && canSeedTree(grid, spot)) commands.spawn(treeDraft(spot, 0))
}
- resources.rng.set(random.state)
}
)