[engine] Thorns: A touch is answered fondly; a rush brings her thorns, never a tremor

This commit is contained in:
2026-11-02 09:33:22 +01:00
parent 03d41aa893
commit ae1169ed6a
3 changed files with 121 additions and 0 deletions
+1
View File
@@ -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';
+49
View File
@@ -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 } };
}