[engine] Thorns: A touch is answered fondly; a rush brings her thorns, never a tremor
This commit is contained in:
@@ -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 } };
|
||||
}
|
||||
Reference in New Issue
Block a user