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: passersBy's street mix by the hour (mixAt)

shinyobjectz2ddcf9fbe10a
diff --git a/docs/character.feature b/docs/character.feature
index 08e53f51..15fb4c7d 100644
--- a/docs/character.feature
+++ b/docs/character.feature
@@ -1018,11 +1018,14 @@ Feature: The character SDK as one system: SOMA bodies, Kimodo motion, generated
# A street carries more than its residents: passersBy() puts walkers on a city's sidewalks by each line's
# length, how busy it is at its busiest hour (THE GAMBIT: 0.3 a metre on a tram street at 5 pm, 0.08 on a side
# 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. Places are found by a grid (a slice of Berlin has 16,000 shops); a queue stands on the
+ # 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
# 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
And a main street at its busiest holds about the people its tile's budget allows, on its sidewalks
+ And a street's people at noon are those its mix puts out at noon, and a walker keeps who they are until they go in
Scenario: CH53. How a person meets an event
# react(): a shock underfoot (a mine's blast felt through the ground), a gun's report, a shout. Felt
diff --git a/sdk/character/src/people/flow.ts b/sdk/character/src/people/flow.ts
index 2357a754..4b16d711 100644
--- a/sdk/character/src/people/flow.ts
+++ b/sdk/character/src/people/flow.ts
@@ -48,6 +48,11 @@ export interface FlowInput {
folk: Folk;
/** the roles' shares among people on the street */
mix: Record<string, number>;
+ /** 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. */
+ mixAt?: (m: Minute) => Record<string, number>;
seed: number | string;
/** the hour profile (STREET_HOURS) */
hours?: number[];
@@ -122,6 +127,30 @@ function stretchShare(a: number, b: number, doors: number[]): number {
return sum / 256;
}
const stretch = new Map<string, number>();
+/** A round when walkers come and go by the hour's mix: as long as the slowest walker's (an old man in the cold, 0.68 m/s)
+ * takes over its longest stretch, so anyone's walk fits in it; walkers are out less of it than of their own round, by
+ * the street's mean pace over it, so a walk has that many more slots */
+const ROUND_PACE = 0.65, MEAN_PACE = 1.2;
+const rounds = new Map<string, { n: number; p: Person }>();
+
+/** The person a slot (or one round of it, `n`) is: drawn by the mix, the same on every call */
+function passer(f: FlowInput, roles: Map<string, Role>, id: string, mix: Record<string, number>, home: Place, n?: number): Person {
+ if (n === undefined) {
+ let p = people.get(`${f.seed}-${id}`);
+ if (!p) {
+ // who they are from a draw of their own, so a walker made earlier walks the same way
+ p = makePerson(id, roles.get(rng(`${f.seed}-${id}-who`).weighted(mix))!, f.folk, home, hash(`${f.seed}-${id}`));
+ people.set(`${f.seed}-${id}`, p);
+ }
+ return p;
+ }
+ // (a slot keeps only its latest round's person: rounds go by, and a minute asked again draws the same one again)
+ const key = `${f.seed}-${id}`, had = rounds.get(key);
+ if (had?.n === n) return had.p;
+ const rid = `${id}~${n}`, p = makePerson(rid, roles.get(rng(`${f.seed}-${rid}-who`).weighted(mix))!, f.folk, home, hash(`${f.seed}-${rid}`));
+ rounds.set(key, { n, p });
+ return p;
+}
/** Everyone walking the given sidewalks at minute `m` (fractional minutes are seconds): who, where, which way. */
export function passersBy(list: Walk[], m: Minute, f: FlowInput): Passer[] {
@@ -147,23 +176,32 @@ export function passersBy(list: Walk[], m: Minute, f: FlowInput): Passer[] {
const key = `${w.id}:${j}`;
let share = stretch.get(key);
if (share === undefined) stretch.set(key, (share = stretchShare(a, b, doors)));
- const slots = Math.round((len * leg.perMetre * (f.busy ?? 1)) / (OUT_SHARE * share));
+ const slots = Math.round(((len * leg.perMetre * (f.busy ?? 1)) / (OUT_SHARE * share)) * (f.mixAt ? MEAN_PACE / ROUND_PACE : 1));
for (let k = 0; k < slots; k++) {
- const id = `${w.id}:${j}:${k}`, r = rng(`${f.seed}-${id}`);
+ const slot = `${w.id}:${j}:${k}`, r = rng(`${f.seed}-${slot}`);
if (r.next() >= busy) continue;
- let p = people.get(`${f.seed}-${id}`);
- if (!p) {
- // who they are from a draw of their own, so a walker made earlier walks the same way
- const role = roles.get(rng(`${f.seed}-${id}-who`).weighted(f.mix))!;
- const home: Place = { id: `passing-${w.id}`, kind: "street", at: w.path[0] };
- p = makePerson(id, role, f.folk, home, hash(`${f.seed}-${id}`));
- people.set(`${f.seed}-${id}`, p);
- }
- const v = walkSpeed(p, f.cold ?? 0.5) * (0.9 + 0.2 * r.next());
+ const home: Place = { id: `passing-${w.id}`, kind: "street", at: w.path[0] };
// a round: out of a door, along the leg (and round its corner by up to SPILL metres), in at a door, a while
// inside. The longest stretch sets the round; a shorter one leaves them inside longer that round.
- const most = longest(r.next(), len), round = most / v / OUT_SHARE;
- const tau = sec + r.next() * round, n = Math.floor(tau / round), u = tau - n * round;
+ let p: Person, v: number, most: number, round: number, tau: number, id = slot;
+ if (f.mixAt) {
+ // (the round at the slowest pace; its person by the mix when it began, at the round's start in minutes)
+ const pace = 0.9 + 0.2 * r.next();
+ most = longest(r.next(), len);
+ 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);
+ id = p.id;
+ v = walkSpeed(p, f.cold ?? 0.5) * pace;
+ } else {
+ p = passer(f, roles, slot, f.mix, home);
+ v = walkSpeed(p, f.cold ?? 0.5) * (0.9 + 0.2 * r.next());
+ most = longest(r.next(), len);
+ round = most / v / OUT_SHARE;
+ tau = sec + r.next() * round;
+ }
+ const n = Math.floor(tau / round), u = tau - n * round;
const { s0, dir, D } = stretchOf(rng(`${f.seed}-${id}-${n}`), a, b, doors, most);
if (u > D / v) continue;
let s = s0 + dir * v * u;
diff --git a/sdk/character/test/people.test.ts b/sdk/character/test/people.test.ts
index c7779c5b..9f9cccb9 100644
--- a/sdk/character/test/people.test.ts
+++ b/sdk/character/test/people.test.ts
@@ -268,3 +268,39 @@ test("passers-by round a block: no one jumps; they come out of doors and go in a
const avenue = at.filter((p) => p.s < 100).length / 100, quiet = at.filter((p) => p.s >= 100).length / 220;
expect(avenue).toBeGreaterThan(2.5 * quiet);
});
+
+test("passers-by by the hour's mix: the street's people change over the day and no one changes on it", () => {
+ const ring: Walk = {
+ id: "h", closed: true, width: 4, perMetre: 0.3,
+ path: [[0, 0, 0], [100, 0, 0], [100, 0, 60], [0, 0, 60]],
+ doors: Array.from({ length: 26 }, (_, i) => 6 + i * 12),
+ };
+ // the workers on the street until eleven, then the housewives
+ const mixAt = (m: number) => (m < 11 * 60 ? { "munitions-worker": 1 } : { housewife: 1 });
+ const plain = { roles, folk, mix, seed: "hour" }, hourly = { ...plain, mixAt };
+ // about as many out at an hour as without it
+ const a = passersBy([ring], 17 * 60, plain).length, b = passersBy([ring], 17 * 60, hourly).length;
+ expect(b).toBeGreaterThan(0.75 * a);
+ expect(b).toBeLessThan(1.33 * a);
+ // well before the change only workers, well after only housewives
+ expect(passersBy([ring], 10 * 60 + 30, hourly).every((p) => p.person.role === "munitions-worker")).toBe(true);
+ expect(passersBy([ring], 11 * 60 + 30, hourly).every((p) => p.person.role === "housewife")).toBe(true);
+ // across the change, a twenty-fourth of a second at a time: anyone still out is the same person a step on; anyone new came out of a door
+ const near = (p: { s: number }) => ring.doors!.some((d) => Math.abs(d - p.s) < 0.15 || Math.abs(Math.abs(d - p.s) - 320) < 0.15);
+ let seen = 0;
+ for (let t = -240; t < 240; t += 4) {
+ const m = 11 * 60 + t / 60, now = passersBy([ring], m, hourly), next = new Map(passersBy([ring], m + 1 / (24 * 60), hourly).map((p) => [p.id, p]));
+ const was = new Set(now.map((p) => p.id));
+ for (const p of now) {
+ const q = next.get(p.id);
+ if (!q) continue;
+ seen++;
+ expect(q.person.role).toBe(p.person.role);
+ expect(Math.hypot(q.at[0] - p.at[0], q.at[2] - p.at[2])).toBeLessThan(0.12);
+ }
+ for (const [id, q] of next) if (!was.has(id)) expect(near(q)).toBe(true);
+ }
+ expect(seen).toBeGreaterThan(500);
+ // the same minute asked again gives the same street
+ expect(passersBy([ring], 11 * 60 + 2, hourly)).toEqual(passersBy([ring], 11 * 60 + 2, hourly));
+});