Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

raea/people: streetMix, the street's mix by the hour from the city's days

shinyobjectzc75455936fc0
diff --git a/docs/character.feature b/docs/character.feature
index 15fb4c7d..4e0698c7 100644
--- a/docs/character.feature
+++ b/docs/character.feature
@@ -1020,7 +1020,8 @@ Feature: The character SDK as one system: SOMA bodies, Kimodo motion, generated
# street) and the hour's share; each a person by the street's mix at their own pace, keeping right, computed
# for any minute alone. A street whose mix changes by the hour (mixAt: the workers at the Works by day, the
# children in school till one) draws each round of a walker, out of a door and in at another, as its own
- # person by the mix when the round began, so the street changes as the day goes and no one changes on it. Places are found by a grid (a slice of Berlin has 16,000 shops); a queue stands on the
+ # person by the mix when the round began, so the street changes as the day goes and no one changes on it;
+ # streetMix(day) gives that mix from the city's own days (each role's minutes walking in each hour). Places are found by a grid (a slice of Berlin has 16,000 shops); a queue stands on the
# world's queue line when it gives one, everyone facing the door; a block with nowhere to happen (no post, no
# workplace) is not lived that day.
Then a city of thousands is placed at any minute, and only those near the camera are characters
diff --git a/sdk/character/src/people.ts b/sdk/character/src/people.ts
index 514b69d9..2223b734 100644
--- a/sdk/character/src/people.ts
+++ b/sdk/character/src/people.ts
@@ -12,6 +12,6 @@ export * from "./people/types";
export { rng, hash, type Rng } from "./people/rng";
export { makePerson, gaitOf, leaned, TRAITS, type Folk, type Names } from "./people/persona";
export { dayOf, whereAt, walkSpeed, straight, SPEED, WALK_UNDER, type DayInput } from "./people/day";
-export { populate, cityDay, snapshot, census, levelAt, react, type CityInput, type Mix, type Day, type Event } from "./people/city";
+export { populate, cityDay, snapshot, census, streetMix, levelAt, react, type CityInput, type Mix, type Day, type Event } from "./people/city";
export { Grid, gridOf } from "./people/near";
export { passersBy, busyAt, STREET_HOURS, type Walk, type Passer, type FlowInput } from "./people/flow";
diff --git a/sdk/character/src/people/city.ts b/sdk/character/src/people/city.ts
index afc09917..9c5d3786 100644
--- a/sdk/character/src/people/city.ts
+++ b/sdk/character/src/people/city.ts
@@ -6,7 +6,7 @@ import { dayOf, whereAt, type DayInput } from "./day";
import { gridOf } from "./near";
import { makePerson, type Folk } from "./persona";
import { hash, rng } from "./rng";
-import type { Activity, Level, Person, Place, Role, Router, Traits, V3 } from "./types";
+import type { Activity, Level, Minute, Person, Place, Role, Router, Traits, V3 } from "./types";
/** How many of each role a place holds (or its share of the population, when `people` is given). */
export type Mix = Record<string, number>;
@@ -138,6 +138,31 @@ export function census(d: Day): Map<string, number[]> {
return out;
}
+/** The street's mix by the hour from the city's own days: for each hour, the minutes each role spends walking (its
+ * trips on foot) in it, so passersBy's walkers (FlowInput.mixAt) are the people whose days put them on the sidewalks
+ * then: the shift going to the Works at half past five, the children out of school at one. An hour no one walks in
+ * takes the day's mix. */
+export function streetMix(d: Day): (m: Minute) => Record<string, number> {
+ const role = new Map(d.people.map((p) => [p.id, p.role]));
+ const hours: Record<string, number>[] = Array.from({ length: 24 }, () => ({})), all: Record<string, number> = {};
+ for (const [id, day] of d.days) {
+ const r = role.get(id)!;
+ for (const a of day) {
+ if (a.kind !== "trip" || a.mode !== "walk") continue;
+ for (let h = Math.max(0, Math.floor(a.from / 60)); h < 24 && h * 60 < a.to; h++) {
+ const on = Math.min(a.to, h * 60 + 60) - Math.max(a.from, h * 60);
+ if (on <= 0) continue;
+ hours[h][r] = (hours[h][r] ?? 0) + on;
+ all[r] = (all[r] ?? 0) + on;
+ }
+ }
+ }
+ return (m) => {
+ const h = hours[Math.floor((((m / 60) % 24) + 24) % 24)];
+ return Object.keys(h).length ? h : all;
+ };
+}
+
/** Something everyone near feels: a gun's report through the ground, a shout, a horse bolting. */
export interface Event { kind: string; at: V3; strength: number }
diff --git a/sdk/character/src/people/flow.ts b/sdk/character/src/people/flow.ts
index 4b16d711..d9a68188 100644
--- a/sdk/character/src/people/flow.ts
+++ b/sdk/character/src/people/flow.ts
@@ -51,7 +51,8 @@ export interface FlowInput {
/** the street's mix by the minute of the day, when it changes over the day (the workers at the Works by day, the
* children in school till one): each round of a walker (out of a door, along the street, in at a door) is then its
* own person, drawn by the mix at the minute the round began, so the street's people change as the day goes and no
- * one changes on it (a passer's id is its round's). Without it every walker keeps one person by `mix` all day. */
+ * one changes on it (a passer's id is its round's). Without it every walker keeps one person by `mix` all day; an
+ * empty mix at a minute takes `mix`. streetMix(day) gives it from a city's own days. */
mixAt?: (m: Minute) => Record<string, number>;
seed: number | string;
/** the hour profile (STREET_HOURS) */
@@ -191,7 +192,8 @@ export function passersBy(list: Walk[], m: Minute, f: FlowInput): Passer[] {
round = most / ROUND_PACE / OUT_SHARE;
const phase = r.next() * round, k0 = Math.floor((sec + phase) / round);
tau = sec + phase;
- p = passer(f, roles, slot, f.mixAt((k0 * round - phase) / 60), home, k0);
+ const mx = f.mixAt((k0 * round - phase) / 60);
+ p = passer(f, roles, slot, Object.keys(mx).length ? mx : f.mix, home, k0);
id = p.id;
v = walkSpeed(p, f.cold ?? 0.5) * pace;
} else {
diff --git a/sdk/character/test/people.test.ts b/sdk/character/test/people.test.ts
index 9f9cccb9..fc86bd7d 100644
--- a/sdk/character/test/people.test.ts
+++ b/sdk/character/test/people.test.ts
@@ -1,7 +1,7 @@
// raea/people (src/people.ts, docs/character.feature CH50-CH53): people drawn from roles, their days, where they are
// at a minute, the levels of detail and how they meet an event.
import { expect, test } from "bun:test";
-import { census, cityDay, dayOf, Grid, levelAt, passersBy, populate, react, rng, snapshot, whereAt, type Walk, type Folk, type Place, type Role, type V3 } from "../src/people";
+import { census, cityDay, dayOf, Grid, levelAt, passersBy, populate, react, rng, snapshot, streetMix, whereAt, type Walk, type Folk, type Place, type Role, type V3 } from "../src/people";
const folk: Folk = {
names: { given: { f: ["Anna", "Martha", "Frieda"], m: ["Karl", "Otto", "Paul"] }, family: ["Schulz", "Müller", "Krüger"] },
@@ -304,3 +304,21 @@ test("passers-by by the hour's mix: the street's people change over the day and
// the same minute asked again gives the same street
expect(passersBy([ring], 11 * 60 + 2, hourly)).toEqual(passersBy([ring], 11 * 60 + 2, hourly));
});
+
+test("the street's mix by the hour comes from the city's days: who is walking then", () => {
+ const people = populate({ roles, places, folk, mix, people: 300, seed: "mix" });
+ const d = cityDay(people, roles, places, { key: "1917-12-03", weekday: 1 });
+ const at = streetMix(d);
+ // the housewives walk to the market at half past ten; at noon the workers are at the Works and the housewives home
+ // again, and only the sentries' patrols are walking
+ const share = (r: Record<string, number>, k: string) => (r[k] ?? 0) / Object.values(r).reduce((x, y) => x + y, 0);
+ expect(share(at(10 * 60 + 30), "housewife")).toBeGreaterThan(0.6);
+ expect(share(at(12 * 60), "munitions-worker")).toBeLessThan(0.05);
+ expect(share(at(12 * 60), "sentry")).toBeGreaterThan(0.8);
+ expect(Object.keys(at(3 * 60)).length).toBeGreaterThan(0);
+ // walked through passersBy: the noon street carries the patrols, though the mix is mostly housewives
+ const ring: Walk = { id: "m", closed: true, width: 4, perMetre: 0.3, path: [[0, 0, 0], [100, 0, 0], [100, 0, 60], [0, 0, 60]] };
+ const noon = passersBy([ring], 12 * 60 + 20, { roles, folk, mix, seed: "mix", mixAt: at });
+ expect(noon.length).toBeGreaterThan(20);
+ expect(noon.filter((p) => p.person.role === "sentry").length).toBeGreaterThan(0.8 * noon.length);
+});