From ae1169ed6a31a6a1bf9a04e6b755ad1685da049e Mon Sep 17 00:00:00 2001 From: mnovak Date: Mon, 2 Nov 2026 09:33:22 +0100 Subject: [PATCH] [engine] Thorns: A touch is answered fondly; a rush brings her thorns, never a tremor --- packages/engine/src/index.ts | 1 + packages/engine/src/touch/touch.ts | 49 +++++++++++++++++++++ packages/engine/test/touch.test.ts | 71 ++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 packages/engine/src/touch/touch.ts create mode 100644 packages/engine/test/touch.test.ts diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 7611e95..00ce3da 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -7,3 +7,4 @@ export { invitation, nextInvitation, sunDay, type NamedPlace, type Place, type S 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'; diff --git a/packages/engine/src/touch/touch.ts b/packages/engine/src/touch/touch.ts new file mode 100644 index 0000000..a3c4a9b --- /dev/null +++ b/packages/engine/src/touch/touch.ts @@ -0,0 +1,49 @@ +/** + * Touching her (.agents/features/thorns.md). Pure: the visit's recent touches and the + * clock are parameters. Nothing here is recorded: a touch is not care, and no number + * grows from it. + */ + +/** The author's to tune (thorns.md T2, T3). */ +export const TOUCH = { + /** This many touches… */ + rushCount: 3, + /** …within this long is a rush: she says she has thorns. */ + rushMs: 2000, + /** Touches closer than this to the one before are one touch: a tremor or a bounce is never fussing. */ + sameMs: 250, + /** After the thorns line she rests this long; touches meanwhile change nothing. */ + restMs: 4000 +} as const; + +export interface TouchState { + /** The touches that still count towards a rush, oldest first. */ + readonly recent: readonly number[]; + /** Until when she rests after a rush; 0 when she is not resting. */ + readonly restUntil: number; + /** Whether a touch has already made her blush in this visit (T4). */ + readonly blushed: boolean; +} + +export const UNTOUCHED: TouchState = { recent: [], restUntil: 0, blushed: false }; + +export type TouchReply = + /** A fond answer. `blush` only on the first of a visit. */ + | { readonly kind: 'fond'; readonly blush: boolean } + | { readonly kind: 'thorns' } + /** Resting after a rush, or the same touch again: nothing changes. */ + | { readonly kind: 'nothing' }; + +/** What a touch at `now` does, and the state after it. */ +export function touch(state: TouchState, now: number): { readonly reply: TouchReply; readonly state: TouchState } { + // Resting; a clock that moved back never makes her rest longer than restMs. + if (now < state.restUntil && state.restUntil - now <= TOUCH.restMs) return { reply: { kind: 'nothing' }, state }; + const last = state.recent.at(-1); + if (last !== undefined && now - last >= 0 && now - last < TOUCH.sameMs) return { reply: { kind: 'nothing' }, state }; + + const recent = [...state.recent.filter((t) => now - t >= 0 && now - t < TOUCH.rushMs), now].slice(-TOUCH.rushCount); + if (recent.length >= TOUCH.rushCount) { + return { reply: { kind: 'thorns' }, state: { recent: [], restUntil: now + TOUCH.restMs, blushed: state.blushed } }; + } + return { reply: { kind: 'fond', blush: !state.blushed }, state: { recent, restUntil: 0, blushed: true } }; +} diff --git a/packages/engine/test/touch.test.ts b/packages/engine/test/touch.test.ts new file mode 100644 index 0000000..e0f6ae0 --- /dev/null +++ b/packages/engine/test/touch.test.ts @@ -0,0 +1,71 @@ +import fc from 'fast-check'; +import { describe, expect, it } from 'vitest'; +import { touch, TOUCH, UNTOUCHED, type TouchReply, type TouchState } from '../src/touch/touch.js'; + +/** Touches at these moments, from no touch at all; the replies in order. */ +function replies(times: readonly number[], from: TouchState = UNTOUCHED): { replies: TouchReply['kind'][]; state: TouchState; all: TouchReply[] } { + let state = from; + const all: TouchReply[] = []; + for (const t of times) { + const next = touch(state, t); + all.push(next.reply); + state = next.state; + } + return { replies: all.map((r) => r.kind), state, all }; +} + +const T0 = 1_000_000; + +describe('a touch (thorns.md)', () => { + it('is answered fondly, and only the first of a visit makes her blush', () => { + const r = replies([T0, T0 + 5000, T0 + 10_000]); + expect(r.all).toEqual([{ kind: 'fond', blush: true }, { kind: 'fond', blush: false }, { kind: 'fond', blush: false }]); + }); + + it('three within two seconds: thorns, then she rests, then all is as before', () => { + const r = replies([T0, T0 + 600, T0 + 1200, T0 + 1500, T0 + 1200 + TOUCH.restMs - 1, T0 + 1200 + TOUCH.restMs]); + expect(r.replies).toEqual(['fond', 'fond', 'thorns', 'nothing', 'nothing', 'fond']); + }); + + it('three touches spanning exactly two seconds are not a rush', () => { + expect(replies([T0, T0 + 1000, T0 + 2000]).replies).toEqual(['fond', 'fond', 'fond']); + }); + + it('touches closer than 250 ms to the one before are one touch: a tremor is never fussing', () => { + const burst = (at: number): number[] => [at, at + 60, at + 120, at + 180]; + expect(replies([...burst(T0), ...burst(T0 + 1100)]).replies).toEqual(['fond', 'nothing', 'nothing', 'nothing', 'fond', 'nothing', 'nothing', 'nothing']); + }); + + it('a clock moved back never makes her rest longer than the rest time', () => { + const rushed = replies([T0, T0 + 500, T0 + 1000]); + expect(rushed.replies.at(-1)).toBe('thorns'); + expect(touch(rushed.state, T0 - 3_600_000).reply.kind).toBe('fond'); + }); + + it('property: touches a second or more apart are never a rush', () => { + fc.assert( + fc.property(fc.array(fc.integer({ min: 1000, max: 60_000 }), { minLength: 1, maxLength: 40 }), (gaps) => { + let t = T0; + const times = gaps.map((g) => (t += g)); + expect(replies(times).replies.every((k) => k === 'fond')).toBe(true); + }) + ); + }); + + it('property: whatever the taps, she keeps no more than a rush of them, and blushes at most once', () => { + fc.assert( + fc.property(fc.array(fc.integer({ min: 0, max: 3000 }), { maxLength: 60 }), (gaps) => { + let t = T0; + let state = UNTOUCHED; + let blushes = 0; + for (const g of gaps) { + const next = touch(state, (t += g)); + if (next.reply.kind === 'fond' && next.reply.blush) blushes++; + state = next.state; + expect(state.recent.length).toBeLessThan(TOUCH.rushCount); + } + expect(blushes).toBeLessThanOrEqual(1); + }) + ); + }); +});