From f0767bfed7b7271c287acf5e90c6fd2fc900dd86 Mon Sep 17 00:00:00 2001 From: mnovak Date: Tue, 17 Nov 2026 18:57:32 +0100 Subject: [PATCH] [engine] PlanetGarden: How many weeds and petals a day shows; the gesture recognisers --- packages/engine/src/index.ts | 1 + packages/engine/src/scene/scene.ts | 16 ++++++ packages/engine/test/scene.test.ts | 36 ++++++++++++++ packages/ui/src/scene/gestures.ts | 78 ++++++++++++++++++++++++++++++ packages/ui/test/gestures.test.ts | 72 +++++++++++++++++++++++++++ 5 files changed, 203 insertions(+) create mode 100644 packages/engine/src/scene/scene.ts create mode 100644 packages/engine/test/scene.test.ts create mode 100644 packages/ui/src/scene/gestures.ts create mode 100644 packages/ui/test/gestures.test.ts diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 21fe8d8..f2aa862 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -8,3 +8,4 @@ export { goodMoment, newOpened, openedIdentity, PETAL, petalAt, petalOrder, sentenceOpened, type PetalNow, type PetalSentence } from './petal/petal.js'; export { touch, TOUCH, UNTOUCHED, type TouchReply, type TouchState } from './touch/touch.js'; +export { petalsToday, weedsToday } from './scene/scene.js'; diff --git a/packages/engine/src/scene/scene.ts b/packages/engine/src/scene/scene.ts new file mode 100644 index 0000000..0bc1f1c --- /dev/null +++ b/packages/engine/src/scene/scene.ts @@ -0,0 +1,16 @@ +/** + * What the planet garden shows of her day (.agents/features/planet-garden.md G3). Pure: the + * rose's key and the local day are parameters, so a replay shows the same weeds every time. + */ +import { petalsOn, seeded, weedsOn } from '../rose/rose.js'; +import type { LocalDay } from '../time/localDay.js'; + +/** How many weeds came up in the night: 2–4 on a day with weeds, else none. */ +export function weedsToday(seed: string, day: LocalDay): number { + return weedsOn(seed, day) ? 2 + Math.floor(seeded(seed, day, 'weed-count') * 3) : 0; +} + +/** How many of her petals have fallen: 3–5 on a day with fallen petals, else none. */ +export function petalsToday(seed: string, day: LocalDay): number { + return petalsOn(seed, day) ? 3 + Math.floor(seeded(seed, day, 'petal-count') * 3) : 0; +} diff --git a/packages/engine/test/scene.test.ts b/packages/engine/test/scene.test.ts new file mode 100644 index 0000000..e382257 --- /dev/null +++ b/packages/engine/test/scene.test.ts @@ -0,0 +1,36 @@ +import fc from 'fast-check'; +import { describe, expect, it } from 'vitest'; +import { petalsOn, weedsOn } from '../src/rose/rose.js'; +import { petalsToday, weedsToday } from '../src/scene/scene.js'; +import { addDays } from '../src/time/localDay.js'; + +const KEY = 'K'.repeat(43); +const days = Array.from({ length: 120 }, (_, i) => addDays('2026-01-01', i)); + +describe('what the planet garden shows of her day (planet-garden.md G3)', () => { + it('weeds: none on a day without, 2–4 on a day with', () => { + for (const day of days) { + const n = weedsToday(KEY, day); + expect(n === 0 ? !weedsOn(KEY, day) : weedsOn(KEY, day) && n >= 2 && n <= 4).toBe(true); + } + expect(new Set(days.map((d) => weedsToday(KEY, d)).filter(Boolean)).size).toBeGreaterThan(1); + }); + + it('fallen petals: none on a day without, 3–5 on a day with', () => { + for (const day of days) { + const n = petalsToday(KEY, day); + expect(n === 0 ? !petalsOn(KEY, day) : petalsOn(KEY, day) && n >= 3 && n <= 5).toBe(true); + } + }); + + it('property: the same rose on the same day always shows the same count', () => { + fc.assert( + fc.property(fc.stringMatching(/^[A-Za-z0-9_-]{43}$/), fc.integer({ min: 0, max: 3000 }), (key, offset) => { + const day = addDays('2026-01-01', offset); + expect(weedsToday(key, day)).toBe(weedsToday(key, day)); + expect(petalsToday(key, day)).toBe(petalsToday(key, day)); + }), + { numRuns: 200 } + ); + }); +}); diff --git a/packages/ui/src/scene/gestures.ts b/packages/ui/src/scene/gestures.ts new file mode 100644 index 0000000..46c168c --- /dev/null +++ b/packages/ui/src/scene/gestures.ts @@ -0,0 +1,78 @@ +/** + * The planet garden's gestures (.agents/features/planet-garden.md G2): pure functions of + * pointer samples, in the scene's own units. They read the player's hand; every rule about + * what care does stays in the engine. + */ + +export interface Sample { + readonly x: number; + readonly y: number; + /** Milliseconds. */ + readonly t: number; +} + +export interface Box { + readonly x0: number; + readonly y0: number; + readonly x1: number; + readonly y1: number; +} + +/** The thresholds, generous on purpose: a missed gesture must never feel like failing. */ +export const GESTURE = { + /** Sideways travel before the can starts to tip, and at which it pours fastest. */ + tiltStart: 12, + tiltFull: 140, + /** A pull: this far up, mostly upwards, within this long. */ + pullUp: 48, + pullMs: 2000, + /** A sweep: this far sideways, no more than this far up or down. */ + sweepAcross: 110, + sweepDrift: 70, + /** A stroke: this much travel, taking at least this long (a tap is quicker and shorter). */ + strokeTravel: 30, + strokeMs: 220, + tapTravel: 10 +} as const; + +const first = (s: readonly Sample[]): Sample => s[0] ?? { x: 0, y: 0, t: 0 }; +const last = (s: readonly Sample[]): Sample => s[s.length - 1] ?? first(s); +const travel = (s: readonly Sample[]): number => + s.reduce((sum, p, i) => (i === 0 ? 0 : sum + Math.hypot(p.x - s[i - 1]!.x, p.y - s[i - 1]!.y)), 0); + +/** How far the can is tipped, 0 to 1, from how far the hand has moved sideways since pressing. */ +export function tiltOf(start: Sample, now: Sample): number { + const dx = Math.abs(now.x - start.x); + if (dx <= GESTURE.tiltStart) return 0; + return Math.min(1, (dx - GESTURE.tiltStart) / (GESTURE.tiltFull - GESTURE.tiltStart)); +} + +/** How fast a can tipped by `tilt` pours, as a multiple of a steady hold (the button's pour). */ +export const pourRate = (tilt: number): number => 0.4 + 1.1 * Math.min(1, Math.max(0, tilt)); + +/** Whether the samples pull a weed: up far enough, mostly upwards, quickly enough. */ +export function isPull(samples: readonly Sample[]): boolean { + const a = first(samples), b = last(samples); + const up = a.y - b.y; + return up >= GESTURE.pullUp && Math.abs(b.x - a.x) <= up && b.t - a.t <= GESTURE.pullMs; +} + +/** Whether the samples sweep across everything between `from` and `to` on the x axis. */ +export function isSweep(samples: readonly Sample[], from: number, to: number): boolean { + if (samples.length < 2) return false; + const xs = samples.map((p) => p.x), ys = samples.map((p) => p.y); + const across = Math.abs(last(samples).x - first(samples).x); + const lo = Math.min(...xs), hi = Math.max(...xs); + const covers = lo <= Math.min(from, to) && hi >= Math.max(from, to); + return across >= GESTURE.sweepAcross && Math.max(...ys) - Math.min(...ys) <= GESTURE.sweepDrift && covers; +} + +export const inside = (p: Pick, b: Box): boolean => p.x >= b.x0 && p.x <= b.x1 && p.y >= b.y0 && p.y <= b.y1; + +/** A slow stroke across her petals, a quick tap, or neither. */ +export function strokeOf(samples: readonly Sample[]): 'stroke' | 'tap' | 'none' { + const moved = travel(samples); + const took = last(samples).t - first(samples).t; + if (moved <= GESTURE.tapTravel) return 'tap'; + return moved >= GESTURE.strokeTravel && took >= GESTURE.strokeMs ? 'stroke' : 'none'; +} diff --git a/packages/ui/test/gestures.test.ts b/packages/ui/test/gestures.test.ts new file mode 100644 index 0000000..8fab60a --- /dev/null +++ b/packages/ui/test/gestures.test.ts @@ -0,0 +1,72 @@ +import { describe, expect, it } from 'vitest'; +import { GESTURE, inside, isPull, isSweep, pourRate, strokeOf, tiltOf, type Sample } from '../src/scene/gestures.js'; + +const at = (x: number, y: number, t = 0): Sample => ({ x, y, t }); +/** A straight line from a to b in `n` steps over `ms`. */ +const line = (a: Sample, b: Sample, ms: number, n = 10): Sample[] => + Array.from({ length: n + 1 }, (_, i) => ({ x: a.x + ((b.x - a.x) * i) / n, y: a.y + ((b.y - a.y) * i) / n, t: (ms * i) / n })); + +describe('tilting the can (planet-garden.md G4)', () => { + it('a little movement does not tip it; far enough tips it fully; either way round', () => { + expect(tiltOf(at(100, 100), at(108, 100))).toBe(0); + expect(tiltOf(at(100, 100), at(100 + GESTURE.tiltFull, 100))).toBe(1); + expect(tiltOf(at(100, 100), at(100 - GESTURE.tiltFull, 100))).toBe(1); + expect(tiltOf(at(100, 100), at(176, 100))).toBeCloseTo(0.5, 1); + }); + + it('tilt stays within 0–1 and never falls as the hand moves further (every distance to 500)', () => { + let previous = 0; + for (let d = 0; d <= 500; d++) { + const t = tiltOf(at(0, 0), at(d, 0)); + expect(t).toBeGreaterThanOrEqual(previous); + expect(t).toBeLessThanOrEqual(1); + previous = t; + } + }); + + it('pours slower than a steady hold when barely tipped, faster when fully tipped', () => { + expect(pourRate(0)).toBeLessThan(1); + expect(pourRate(0.55)).toBeCloseTo(1, 1); + expect(pourRate(1)).toBeGreaterThan(1); + expect(pourRate(5)).toBe(pourRate(1)); + }); +}); + +describe('pulling a weed', () => { + it('up and quick enough pulls it; sideways, too short or too slow does not', () => { + expect(isPull(line(at(100, 200), at(104, 140), 300))).toBe(true); + expect(isPull(line(at(100, 200), at(180, 180), 300))).toBe(false); + expect(isPull(line(at(100, 200), at(100, 170), 300))).toBe(false); + expect(isPull(line(at(100, 200), at(100, 120), GESTURE.pullMs + 1))).toBe(false); + expect(isPull(line(at(100, 200), at(100, 280), 300))).toBe(false); + }); +}); + +describe('sweeping the petals', () => { + it('a long sideways swipe across all of them gathers them, either direction', () => { + expect(isSweep(line(at(60, 300), at(260, 310), 400), 100, 220)).toBe(true); + expect(isSweep(line(at(260, 300), at(60, 290), 400), 100, 220)).toBe(true); + }); + + it('one that misses some, wanders up and down, or is too short does not', () => { + expect(isSweep(line(at(60, 300), at(180, 300), 400), 100, 220)).toBe(false); + expect(isSweep(line(at(60, 300), at(260, 420), 400), 100, 220)).toBe(false); + expect(isSweep([at(100, 300)], 100, 220)).toBe(false); + }); +}); + +describe('touching her', () => { + it('a slow stroke across her petals is a stroke; a short press is a tap; a flick is neither', () => { + expect(strokeOf(line(at(100, 100), at(160, 104), 600))).toBe('stroke'); + expect(strokeOf(line(at(100, 100), at(104, 102), 80))).toBe('tap'); + expect(strokeOf(line(at(100, 100), at(160, 100), 60))).toBe('none'); + }); +}); + +describe('dropping the windbreak', () => { + it('lands inside a box or not', () => { + const box = { x0: 40, y0: 100, x1: 120, y1: 200 }; + expect(inside(at(80, 150), box)).toBe(true); + expect(inside(at(130, 150), box)).toBe(false); + }); +});